diff --git a/README.md b/README.md index 0f0986b6897c655a37bab953ed8322c7254d3672..159ceb3c3db2ccb4db04ff5c30a04c4b57b784dc 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,25 @@ --- -title: Q Live -emoji: 🐠 -colorFrom: yellow +title: Q Live — the Serverless Voice +emoji: 🎙️ +colorFrom: indigo colorTo: purple -sdk: gradio -sdk_version: 6.19.0 -python_version: '3.13' -app_file: app.py -pinned: false +sdk: static +app_file: index.html +pinned: true +short_description: Talk to Q — a voice AI running 100% in your browser --- -Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference +# Q Live — the Serverless Voice + +Tap the orb and **talk to Q**. Every model — the brain (BitNet‑2B), the ear (Moonshine), the voice +(Kokoro) — streams by content address from [HOLOGRAMTECH](https://huggingface.co/HOLOGRAMTECH), +is verified per block, and runs **entirely in your browser** on WebGPU/WASM. + +**Zero inference server. Your voice never leaves your device. Works offline after first load.** + +This is the real‑time speech‑to‑speech experience of a datacenter voice pipeline — with no datacenter. +Cerebras throws a wafer at latency; Q throws your own GPU and its idle time (it starts answering on your +confident partial while you're still finishing). One self‑verifying κ‑link boots the whole runtime in any +cold browser. + +Built with the Hologram stack. Best in a Chromium browser with WebGPU. diff --git a/_shared/q/holo-q-mux.js b/_shared/q/holo-q-mux.js new file mode 100644 index 0000000000000000000000000000000000000000..557c0c30971f9c69cf30b866853a2b62a2a92cdc --- /dev/null +++ b/_shared/q/holo-q-mux.js @@ -0,0 +1,203 @@ +// holo-q-mux.js — Mixture-of-Specialists for Holo Q: the OS's helper tasks stop defaulting to one +// big model and each quietly binds the BEST small specialist the open web offers, run on-device and +// proven by receipt. The native orchestrator (Holo Mind, ADR-0081) discovers · selects · binds · +// routes · verifies a per-task model. "auto" stops meaning "use the main model" and starts meaning +// "the right tiny mind for this exact job" (ADR-0084). +// +// THE FACTORING (the honest part): DISCOVERY is one cheap Hugging Face Hub API call per task +// (serverless — a browser fetch, never a server); SELECTION is a PURE ranking over the returned +// metadata (no candidate is downloaded to be judged — that would break "fast" and "serverless"); a +// chosen specialist is a PLAN, not yet a loaded model — loading streams its weights as a +// content-addressed κ-disk (Holo Q, ADR-0052) and BINDS it behind the existing per-task provider +// registry. Until a specialist is bound, routeTask() falls back to the main model — never blocks, +// never fakes (Law L5 voice). DOM-free, dependency-free; sealing/loading is the caller's job, exactly +// like holo-q-ai.js and holo-q-diffusion.js. The ranking + routing are re-derivable (Node witness). + +// ── the faculty surface (the OS surface) → a discovery spec each ──────────────────────────────────── +// `pipeline` is the Hugging Face pipeline_tag the job maps to; `need` the engine capability that runs +// it; `maxParams` the size ceiling that keeps it browser-fast. Two classes share ONE registry + UI: +// • CORE I/O faculties (pinned:true) — Q's own senses: respond/listen/speak/code. Their specialist is +// NOT HF-discovered; it is a precompiled, κ-pinned .holo that ships WITH the OS (see PINNED below, +// sourced from apps/q/forge/.models/holo-ipfs-pins.json). "auto" for these = the OS's own brain. +// • HELPER tasks (the rest) — each quietly binds the best small specialist the open web offers, by a +// cheap HF discovery call. "auto" for these = the right tiny mind, else a fall-back to the main brain. +export const TASKS = [ + // ── CORE I/O — Q's senses (pinned κ .holo, precompiled, content-addressed; not HF-discovered) ── + { id: "respond", label: "Respond", job: "Main chat / reasoning", pipeline: "text-generation", need: "generative", maxParams: "1.5B", pinned: true }, + { id: "listen", label: "Listen", job: "Speech → text (ASR)", pipeline: "automatic-speech-recognition", need: "asr", maxParams: "700M", pinned: true }, + { id: "speak", label: "Speak", job: "Text → speech (TTS)", pipeline: "text-to-speech", need: "tts", maxParams: "100M", pinned: true }, + { id: "code", label: "Code", job: "Agentic coding", pipeline: "text-generation", need: "generative", maxParams: "3B", pinned: true }, + // Deep reasoning — a 9B THINKING brain (Qwen3.5). NOT a silent default: the device-tier gate + // (holo-q-think-tier.mjs) routes the 9B only to capable WebGPU hardware; weak/mobile fall to respond. + { id: "think", label: "Think", job: "Deep reasoning", pipeline: "text-generation", need: "generative", maxParams: "9B", pinned: true }, + // ── HELPER tasks — each discovers + binds the best browser-runnable small specialist (or main) ── + { id: "create", label: "Create", job: "Build a holospace", pipeline: "text-generation", need: "generative", maxParams: "8B" }, + { id: "ask", label: "Ask", job: "Answer about a holospace", pipeline: "text-generation", need: "generative", maxParams: "8B" }, + { id: "vision", label: "Vision", job: "Image analysis", pipeline: "image-to-text", need: "vlm", maxParams: "2B" }, + { id: "web-extract", label: "Web extract", job: "Page summarization", pipeline: "summarization", need: "generative", maxParams: "1B" }, + { id: "compression", label: "Compression", job: "Context compaction", pipeline: "summarization", need: "generative", maxParams: "1B" }, + { id: "session-search", label: "Session search",job: "Recall queries", pipeline: "feature-extraction",need: "embedding", maxParams: "200M" }, + { id: "skills-hub", label: "Skills hub", job: "Skill search", pipeline: "feature-extraction",need: "embedding", maxParams: "200M" }, + { id: "approval", label: "Approval", job: "Smart auto-approve", pipeline: "text-classification",need: "classifier",maxParams: "200M" }, + { id: "mcp", label: "MCP", job: "MCP tool routing", pipeline: "zero-shot-classification", need: "classifier", maxParams: "500M" }, + { id: "title-gen", label: "Title gen", job: "Session titles", pipeline: "summarization", need: "generative", maxParams: "500M" }, + { id: "curator", label: "Curator", job: "Skill-usage review", pipeline: "text-classification",need: "classifier",maxParams: "500M" }, + // DETERMINISTIC tasks — routed like any other, but NOT model-discovered: their specialist is a pure + // encoder (no HF model, no weights, no maxParams). `deterministic:true` makes discovery SKIP them, so + // they ride the same per-task registry + κ-memo spine without ever pretending to pick a model (honest). + { id: "import", label: "Import", job: "Encode a GitHub repo as a Holo app", deterministic: true }, +]; + +// ── PINNED — the OS's own precompiled κ .holo specialists for the core I/O faculties ──────────────── +// Sourced from apps/q/forge/.models/holo-ipfs-pins.json (archiveKappa = the .holo footer = did:holo). +// `instant` is the always-loads core; `upgrade` (optional) is the silent better tier on capable hardware. +// These are NOT discovered — the κ IS the identity (Law L1); the loader resolves path → Release → κ-route +// (IPFS heal) and L5-verifies every block, so no host is trusted. This is "auto = Q's own brain", honestly. +export const PINNED = { + respond: { faculty: "respond", instant: { id: "qwen2.5-0.5b", kappa: "41a930c07450623751f84af6a55bbecd54fe608ad6e94adf17f83c712aaf1b91", bytesMB: 491.4 }, upgrade: { id: "qwen2.5-1.5b", kappa: "ea7323369bfeebb344c9d0b6252de485e2b9833784405678f910a16cd7746202", bytesMB: 1117.4 } }, + code: { faculty: "code", instant: { id: "qwen-coder-3b", kappa: "33ca24ae50bf5649b4c431817ebf15924b8aa929ab87868c33abeeeb8f695a17", bytesMB: 2105.0 } }, + // think — the precompiled 9B reasoner (DavidAU Qwen3.5-9B THINKING, imatrix Q4_K_M, arch qwen35). The OS's + // pinned thinking brain; the device-tier gate (holo-q-think-tier) decides whether THIS device loads it or + // routes to `respond`. No `upgrade` tier — the 9B IS the top tier; weak/mobile fall back, never auto-load it. + think: { faculty: "think", instant: { id: "qwen3.5-9b-thinking", kappa: "2c9265545555b864438c49df81b72e4ae9221f32f0b4f8ab4ceaa846ea38d94f", bytesMB: 5627.1 } }, + listen: { faculty: "listen", instant: { id: "moonshine-tiny-int8", kappa: "bbd89df22c86fc54455779be070395cc8dab0c3438cbe85974c9f02d2a291780", bytesMB: 29.5 }, upgrade: { id: "moonshine-tiny-f16", kappa: "ff7e1c8b3c9e360ab062ce96a297e6f2467608c634f2e4b171078180056a72d8", bytesMB: 56.2 } }, + speak: { faculty: "speak", instant: { id: "kokoro-82m", kappa: "5beb3c2171121fc7afa10a6325e45ed540147a4ed54157a01020409ca8694124", bytesMB: 96.5 } }, +}; + +// markers (in a model's tags/library) that say "this can run IN A TAB" — the hard gate on selection. +export const BROWSER_LIBS = ["onnx", "transformers.js", "transformers.js", "gguf"]; +const OPEN_LICENSES = ["apache-2.0", "mit", "bsd", "openrail", "cc-by", "cc0", "llama"]; +const HF_API = "https://huggingface.co/api/models"; + +// ── pure helpers ───────────────────────────────────────────────────────────────────────────────── +const _tags = (m) => [...(m.tags || []), m.library_name, m.pipeline_tag].filter(Boolean).map((s) => String(s).toLowerCase()); + +// can this model execute in the browser? (an ONNX / transformers.js / GGUF marker present) +export function runnable(m) { const t = _tags(m); return BROWSER_LIBS.some((lib) => t.includes(lib)); } + +// estimate parameter count from the id/tags ("0.5b", "135m", "tiny"/"small"/"base") — an ESTIMATE, +// not a weight fetch (keeping selection cheap). Returns a number or null when unknowable. +export function paramsEstimate(m) { + const s = (m.id || m.modelId || "").toLowerCase() + " " + _tags(m).join(" "); + let mm = s.match(/(\d+(?:\.\d+)?)\s*b(?:\b|illion|-)/); if (mm) return parseFloat(mm[1]) * 1e9; + mm = s.match(/(\d+(?:\.\d+)?)\s*m(?:\b|illion|-)/); if (mm) return parseFloat(mm[1]) * 1e6; + if (/\btiny\b/.test(s)) return 60e6; + if (/\bmini\b|\bsmall\b/.test(s)) return 120e6; + if (/\bbase\b/.test(s)) return 250e6; + return null; +} +export function maxParamsToNum(cap) { + const m = String(cap || "").toLowerCase().match(/(\d+(?:\.\d+)?)\s*([bm])/); + return m ? parseFloat(m[1]) * (m[2] === "b" ? 1e9 : 1e6) : Infinity; +} +const openLicense = (m) => _tags(m).some((t) => OPEN_LICENSES.some((l) => t.includes("license:" + l) || t === l)); + +// the cheap selection signal — a PURE, deterministic score over metadata only. +export function scoreCandidate(m, task) { + const dl = Math.log10((m.downloads || 0) + 1); // popularity (≈0..7) + const lk = Math.log10((m.likes || 0) + 1); // endorsement (≈0..4) + const pipeOk = m.pipeline_tag === task.pipeline ? 1 : 0; + const run = runnable(m) ? 1 : 0; + const est = paramsEstimate(m), cap = maxParamsToNum(task.maxParams); + // within the size cap, smaller is better; over the cap is disqualifying; unknown is neutral-ish. + const sizeFit = est == null ? 0.3 : est <= cap ? 0.5 + (1 - est / cap) * 0.5 : -2; + const lic = openLicense(m) ? 0.5 : 0; + return run * 3 + pipeOk * 2 + dl * 0.6 + lk * 0.3 + sizeFit * 1.5 + lic; +} + +// rank fetched candidates for a task — PURE (no network). Returns a sorted, annotated list; ties are +// broken by id (so the SAME metadata always yields the SAME pick — re-derivable, Law L5). +export function rankCandidates(models, task) { + return (models || []).map((m) => ({ + id: m.id || m.modelId, score: +scoreCandidate(m, task).toFixed(4), + runnable: runnable(m), paramsEstimate: paramsEstimate(m), pipeline: m.pipeline_tag || null, + downloads: m.downloads || 0, likes: m.likes || 0, + })).sort((a, b) => (b.score - a.score) || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0)); +} + +// ── discovery (the one serverless call per task) ──────────────────────────────────────────────────── +// build the Hugging Face Hub query URL for a task (pure — testable without a network). +export function discoverURL(task, { limit = 20 } = {}) { + const q = new URLSearchParams({ pipeline_tag: task.pipeline, sort: "downloads", direction: "-1", limit: String(limit) }); + return `${HF_API}?${q.toString()}`; +} +// discover(task) → ranked, browser-RUNNABLE candidates. `fetch` is injectable (Node witness / browser). +export async function discover(task, { fetch = globalThis.fetch, limit = 20 } = {}) { + const res = await fetch(discoverURL(task, { limit })); + const models = await res.json(); + return rankCandidates(models, task).filter((c) => c.runnable); +} + +// pickSpecialist(taskId) → a PLAN: the top runnable specialist, or an honest fall-back to main when +// none is browser-runnable. Does NOT download or bind — that is an explicit next step (κ-disk load). +export async function pickSpecialist(taskId, opts = {}) { + const task = TASKS.find((t) => t.id === taskId); + if (!task) throw new Error(`holo-q-mux: unknown task "${taskId}"`); + // deterministic tasks (e.g. import) have NO model to discover — return an honest plan, never an HF call. + if (task.deterministic) return { task: taskId, specialist: null, deterministic: true, fallback: null, reason: "deterministic task — a pure encoder is bound, no model is discovered" }; + // pinned core I/O faculties (respond/listen/speak/code) — the OS's OWN precompiled κ .holo, never HF-discovered. + if (task.pinned) { + const p = PINNED[task.id]; + return { task: taskId, pinned: true, specialist: p ? { ...p.instant, faculty: p.faculty } : null, upgrade: (p && p.upgrade) || null, fallback: null, reason: "pinned faculty — a precompiled, κ-addressed .holo ships with the OS (content-addressed, L5-verified); not HF-discovered" }; + } + const ranked = await discover(task, opts); + if (!ranked.length) return { task: taskId, specialist: null, fallback: "main", reason: "no browser-runnable specialist found — using the main model" }; + return { task: taskId, specialist: ranked[0], alternatives: ranked.slice(1, 4), fallback: null }; +} + +// autoAssign() → the "magic" entry: a per-task PLAN across the whole helper-task surface, one cheap +// call each. Pure orchestration over pickSpecialist; the caller loads + binds the ones it wants. +export async function autoAssign(opts = {}) { + const out = []; + for (const t of TASKS) { try { out.push(await pickSpecialist(t.id, opts)); } catch (e) { out.push({ task: t.id, error: String(e && e.message || e) }); } } + return out; +} + +// ── the per-task provider registry — route each helper task to its bound specialist (or main) ──────── +// A provider is the same shape useBrain() takes: { id, generate?|complete?|embed?|classify? }. Loading +// the κ-disk and constructing the provider is the caller's job; here we only ROUTE. +const _bound = new Map(); +export function bindSpecialist(taskId, provider) { + if (!TASKS.find((t) => t.id === taskId)) throw new Error(`holo-q-mux: unknown task "${taskId}"`); + if (!provider) { _bound.delete(taskId); return { task: taskId, provider: null }; } + _bound.set(taskId, provider); + return { task: taskId, provider: provider.id || "specialist" }; +} +export function routeTask(taskId) { return _bound.get(taskId) || { id: "main", fallback: true }; } + +// resolveModel(taskId) — THE single front door every consumer reads to learn which LLM runs a faculty +// RIGHT NOW. One precedence, everywhere (the "one place to select the active model" — ADR-0084): +// 1. an explicit OVERRIDE — a provider bound via bindSpecialist (the settings picker / admin choice) +// 2. a PINNED κ .holo — the OS's own precompiled brain for a core I/O faculty (respond/listen/speak/code) +// 3. the MAIN brain — helper tasks with no bound specialist fall back to the main model (never blocks) +// Pure + re-derivable: it READS the registry, it does not load. The caller loads the κ / builds the provider. +export function resolveModel(taskId) { + const task = TASKS.find((t) => t.id === taskId); + if (!task) throw new Error(`holo-q-mux: unknown task "${taskId}"`); + const bound = _bound.get(taskId); + if (bound) return { task: taskId, source: "override", provider: bound, id: bound.id || "specialist" }; + if (task.pinned && PINNED[taskId]) return { task: taskId, source: "pinned", spec: PINNED[taskId], id: PINNED[taskId].instant.id }; + if (task.deterministic) return { task: taskId, source: "deterministic", id: "encoder" }; + return { task: taskId, source: "main", main: true, id: "main" }; +} +export function boundSpecialists() { return [..._bound.entries()].map(([task, p]) => ({ task, provider: p.id || "specialist" })); } +export function unbindAll() { _bound.clear(); } + +// describeMux() — the seam's honest state: what it routes, how it selects, what is proven vs pending. +export function describeMux() { + return { + tasks: TASKS.map((t) => ({ id: t.id, job: t.job, pipeline: t.pipeline, need: t.need, maxParams: t.maxParams, pinned: !!t.pinned, deterministic: !!t.deterministic })), + pinned: Object.fromEntries(Object.entries(PINNED).map(([k, v]) => [k, { faculty: v.faculty, instant: v.instant.id, upgrade: v.upgrade ? v.upgrade.id : null, kappa: v.instant.kappa }])), + discovery: "Hugging Face Hub API (one cheap call per HELPER task, serverless — a browser fetch); core I/O faculties are κ-pinned (precompiled .holo, not discovered)", + selection: "pure deterministic ranking over metadata; no candidate downloaded to be judged", + execution: "chosen specialist streams as a content-addressed κ-disk (ADR-0052), bound per-task", + fallback: "no browser-runnable specialist (or no WebGPU) → the main model; never blocks, never fakes (Law L5)", + receipt: "decode-agnostic — each task output seals the SAME re-derivable InferenceReceipt, conscience-gated (ADR-0033/0083)", + bound: boundSpecialists(), + }; +} + +export default { + TASKS, PINNED, BROWSER_LIBS, runnable, paramsEstimate, maxParamsToNum, scoreCandidate, rankCandidates, + discoverURL, discover, pickSpecialist, autoAssign, bindSpecialist, routeTask, resolveModel, boundSpecialists, unbindAll, describeMux, +}; diff --git a/_shared/voice/holo-q-faculty-models.mjs b/_shared/voice/holo-q-faculty-models.mjs new file mode 100644 index 0000000000000000000000000000000000000000..47746c9ecdb4a72bea8b94ff60739fa6e0d1d0c4 --- /dev/null +++ b/_shared/voice/holo-q-faculty-models.mjs @@ -0,0 +1,120 @@ +// holo-q-faculty-models.mjs — the ONE bridge from a Q faculty decision to a LOADABLE .holo spec. +// +// holo-q-mux.js is the authority on WHICH model runs a faculty (override → pinned κ → main) but it is a +// pure registry — it carries the κ (identity, Law L1) and the faculty, NOT where the bytes live. This +// module adds the single hosting map (model id → filename) and turns a mux decision into the {url, release, +// kappa, upgrade} every loader (holo-brain-engine, holo-moonshine-ear, kokoro) already takes. The κ is read +// FROM the mux PINNED table, so the two can never drift — change a κ in one place (the mux, sourced from +// .models/holo-ipfs-pins.json) and every consumer follows. Resolution is pure + re-derivable (no load). +// +// Used by: holo-voice-holo-brain.mjs (respond/code chat brain), holo-voice.js (listen/ASR config), and any +// app that wants "the model the user chose for this faculty". Overrides flow through resolveModel() — so the +// settings picker (bindSpecialist) controls every faculty from one place. + +import { resolveModel, PINNED } from "../q/holo-q-mux.js"; + +// where the κ bytes live. The forge dir is the dev/canonical mount; the Release is the prod host (>100MB +// Pages limit); the κ-route (/.holo/sha256/<κ>, SW heals from IPFS) is the universal fallback the loaders +// already try. Override the release base via window.HOLO_MODELS_RELEASE_BASE (e.g. a pinned tag). +const FORGE = "/apps/q/forge/"; +const RELEASE_BASE = (typeof window !== "undefined" && window.HOLO_MODELS_RELEASE_BASE) || "https://github.com/Hologram-Technologies/hologram-apps/releases/download/models-v1/"; +// model id (as named in the mux PINNED table) → its .holo filename. The ONLY hosting fact not in the mux. +const FILE = { + "qwen2.5-0.5b": "qwen2.5-0.5b-instruct.holo", + "qwen2.5-1.5b": "qwen2.5-1.5b-instruct.holo", + "qwen-coder-3b": "qwen2.5-coder-3b-instruct.holo", + // think faculty — the 9B reasoner ships as its OWN sharded .holo (not in the q-models pack; no PACK_ID), + // delivered via its parts manifest + κ-route. specFor builds {url: FORGE/.models/…, release, kappa}. + "qwen3.5-9b-thinking": "qwen3.5-9b-thinking.holo", + "moonshine-tiny-int8": "moonshine-tiny-int8.holo", + "moonshine-tiny-f16": "moonshine-tiny-f16.holo", + // listen 0.6B upgrade (WebGPU) — the κ-native FastConformer-TDT ear. Encoder + joint are separate .holo; + // the parakeet ear (holo-parakeet-ear.mjs) takes the encoder as holoUrl and the joint via jointUrl. + "parakeet-tdt-0.6b-v2": "parakeet-tdt-0.6b-v2-stream.holo", + "parakeet-tdt-0.6b-v2-joint": "parakeet-tdt-0.6b-v2-joint.holo", + "kokoro-82m": "kokoro-82m.holo", + // semantic turn-detector (SmolLM2-135M) — its own loader (createTurnDetectorWeb) but a first-class registry model + // so it shares the pack delivery; standalone lives in its own dir. + "turn-detector": "turn-detector/turn-detector.holo", +}; + +// THE UNIFIED PACK — one κ-addressable .holo holding every faculty model's bodies (deduped, instant-tier-first). +// ONE delivery, one warm OPFS cache, one address: a pack-aware loader opens it ONCE (holo-model-pack.mjs) and +// Range-fetches only the model it needs (proven by q-pack-stream-witness: a faculty reads ≈ its own bytes, never +// the 953MB whole). Standalone .holo stay as the per-model fallback when the pack isn't reachable. PACK_ID maps a +// mux/FILE model id → its id INSIDE the pack manifest (the encoder/joint are named without the version suffix there). +const PACK_FILE = "q-models.holo"; +// EVERY Q model lives in the pack (bundle-everything). The pack is one κ-addressable file; because it exceeds GitHub's +// 2 GiB per-asset cap it's DELIVERED in <2 GiB shards (q-models.holo.partNN, manifest q-models.holo.parts.json) that a +// spanning reader (holo-pack-shards.mjs) stitches back — one address, sharding invisible above the rangeReader. +const PACK_ID = { + "moonshine-tiny-int8": "moonshine-tiny-int8", "moonshine-tiny-f16": "moonshine-tiny-f16", "kokoro-82m": "kokoro-82m", + "parakeet-tdt-0.6b-v2": "parakeet-encoder", "parakeet-tdt-0.6b-v2-joint": "parakeet-joint", + "turn-detector": "turn-detector", "qwen2.5-0.5b": "qwen2.5-0.5b", "qwen2.5-1.5b": "qwen2.5-1.5b", "qwen-coder-3b": "qwen-coder-3b", +}; +// the pack's own coordinates. `url` = monolithic file (dev/FORGE mount, served same-origin when present); `release` +// = label only. `partsManifest` = the shard manifest, shipped SAME-ORIGIN (tiny → in dist, no CORS); it carries an +// IPFS gateway + per-shard CIDs so the bytes stream from a CDN-backed CORS+Range gateway (serverless, any-device). +// GitHub release assets are NOT used for browser delivery (they send no CORS header). Override the gateway via +// window.HOLO_PACK_GATEWAY; override the whole base via window.HOLO_MODELS_RELEASE_BASE. +export const packSpec = { file: PACK_FILE, url: FORGE + ".models/" + PACK_FILE, release: RELEASE_BASE + PACK_FILE, partsManifest: FORGE + ".models/" + PACK_FILE + ".parts.json", sharded: true }; + +// THE SEED FIRST-RESPONDER — a tiny (~7MB int8) context-aligned .holo that speaks an instant qwen-aligned opener +// ("Sure! …") the moment it loads, so a cold user hears audio in <2s while the 485MB brain streams in (speak-while- +// streaming, holo-voice-seed-handoff.mjs). ONE small same-origin asset (no sharding); release is the prod fallback. +// κ is the q-seed.holo archive root (file-bundle: seed.onnx int8 + seed.json cfg, L5-verified). Loaded fail-soft: +// any open/run error → the loop falls back to brain-only, never breaking listen/respond. +export const seedSpec = { file: "q-seed.holo", url: FORGE + ".models/q-seed.holo", release: RELEASE_BASE + "q-seed.holo", kappa: "did:holo:sha256:32edd21a7f80a0645cf5659be51a4002ef2271aa6637fba08314929f57bae4a0", bytesMB: 7 }; + +// a {id, kappa} from the mux → a loadable spec (url path → release → κ-route; every block L5-verified). When the +// model also lives in the unified pack, the spec carries {pack:{url,release,model}} so a pack-aware loader prefers +// the single delivery; loaders that ignore it fall back to the standalone url/release unchanged. +export function specFor(tier) { + if (!tier || !tier.id) return null; + const file = FILE[tier.id]; + if (!file) return { id: tier.id, kappa: tier.kappa || "", url: tier.id, release: "" }; // a direct URL/unknown id — pass through + const spec = { id: tier.id, kappa: tier.kappa || "", url: FORGE + ".models/" + file, release: RELEASE_BASE + file, bytesMB: tier.bytesMB || 0 }; + if (PACK_ID[tier.id]) spec.pack = { url: packSpec.url, release: packSpec.release, model: PACK_ID[tier.id] }; + return spec; +} + +// every pinned TIER the OS ships, indexed by model id (instant + upgrade across all faculties). This is the +// closed set a user override may name — so a steer ("use the 1.5B") resolves to real, κ-verified bytes, not +// an arbitrary string. specById(id) → a loadable spec, or null when the id isn't an OS-pinned model. +const ALL_TIERS = (() => { + const m = {}; + for (const fac of Object.values(PINNED)) { if (fac.instant) m[fac.instant.id] = fac.instant; if (fac.upgrade) m[fac.upgrade.id] = fac.upgrade; } + return m; +})(); +export function specById(id) { return ALL_TIERS[id] ? specFor(ALL_TIERS[id]) : null; } +// the tiers a given faculty is ALLOWED to use (instant + its own upgrade) — the closed choice set a picker +// or a steer offers for that faculty. Returns [{id,kappa,bytesMB,tier:"instant"|"upgrade"}], or [] for helpers. +export function tiersFor(faculty) { + const p = PINNED[faculty]; if (!p) return []; + const out = [{ ...p.instant, tier: "instant" }]; + if (p.upgrade) out.push({ ...p.upgrade, tier: "upgrade" }); + return out; +} + +// resolveFacultyModel(faculty) — THE call a consumer makes. Returns the loadable spec for the active model +// of a faculty, honoring the user/admin override, then the OS-pinned κ. Shape: +// { faculty, source:"override"|"pinned"|"main", instant:{url,release,kappa,...}|null, upgrade:{...}|null, provider?, main? } +// - source "override": the settings picker bound a specific provider — returned verbatim (the caller uses it). +// - source "pinned": the OS's own precompiled κ .holo — instant tier (+ optional silent upgrade tier). +// - source "main": a helper faculty with no binding — defer to the main brain (the caller's main path). +export function resolveFacultyModel(faculty) { + const r = resolveModel(faculty); + if (r.source === "override") return { faculty, source: "override", provider: r.provider, id: r.id, instant: specById(r.id), upgrade: null }; + if (r.source === "pinned") { + const p = r.spec; // { faculty, instant:{id,kappa,bytesMB}, upgrade?:{...} } + return { faculty, source: "pinned", instant: specFor(p.instant), upgrade: p.upgrade ? specFor(p.upgrade) : null }; + } + return { faculty, source: r.source, main: true, id: r.id }; // "main" | "deterministic" +} + +// convenience: the bare {url,release,kappa} for a pinned faculty's instant tier (the common case a loader wants). +export function instantSpec(faculty) { const r = resolveFacultyModel(faculty); return r.source === "pinned" ? r.instant : null; } +export function upgradeSpec(faculty) { const r = resolveFacultyModel(faculty); return r.source === "pinned" ? r.upgrade : null; } + +export { PINNED, resolveModel }; +export default { resolveFacultyModel, instantSpec, upgradeSpec, specFor, specById, tiersFor, packSpec, seedSpec }; diff --git a/_shared/voice/holo-voice-asr.mjs b/_shared/voice/holo-voice-asr.mjs new file mode 100644 index 0000000000000000000000000000000000000000..d83509f7ffd1d6423f2e1c71cebe6e1b4e945383 --- /dev/null +++ b/_shared/voice/holo-voice-asr.mjs @@ -0,0 +1,154 @@ +// holo-voice-asr.mjs — the on-device speech-recognition engine for Holo Voice. +// +// It binds into the QVAC transcription seam (HoloQVAC.useHoloVoice). The recognizer runs ENTIRELY in +// the browser — no inference server, no audio leaves the device. Tiered, mirroring QVAC's own +// "WebGPU, deterministic fallback" pattern (holo-qvac.js): +// +// • WebGPU present → Whisper-base (transformers.js, ONNX/WebGPU) — high accuracy +// • WASM only → Moonshine-tiny (transformers.js, ONNX/WASM) — streaming, any browser +// +// Weights resolve by content address through the OS service worker (Law L5) and live offline in +// CacheStorage after first load — that is what makes recognition serverless. `localPath` points at the +// vendored κ-disk; set `remote:true` (dev only) to bootstrap from a module CDN before weights are +// vendored. The library is imported lazily, so loading this module never blocks and never throws on a +// device that will only use the bring-up fallback. + +const DEFAULTS = { + // All vendor paths are resolved RELATIVE TO THIS MODULE (vendor/ is a sibling dir created by + // tools/vendor-voice-model.mjs), so they work under any mount (_shared, content-addressed, …). + lib: "vendor/transformers/transformers.js", // transformers.js ESM entry + libRemote: "https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.0.2", + ortPath: "vendor/transformers/", // onnxruntime-web wasm lives beside it + // Tiered for LATENCY: the WASM listen path defaults to Whisper-TINY (~3-5x faster than base — it's the + // ASR wall-time that dominates response onset), falling back to base if tiny isn't vendored. WebGPU + // (when it works) keeps base for accuracy. One vendored model per id under localPath. + modelWebGPU: "onnx-community/whisper-base", // quality tier (WebGPU) + modelWASM: "onnx-community/whisper-tiny", // fast listen tier (WASM) + modelWASMFallback: "onnx-community/whisper-base", // used if whisper-tiny isn't vendored + localPath: "vendor/models/", // model id resolves under here + remote: false, // dev escape hatch: load lib + weights from the CDN above + quantized: true, + // WASM is the default: it's the any-browser floor (Firefox/Safari have no stable WebGPU) AND the + // quantized Whisper decoder currently hits an ORT WebGPU kernel bug. Opt into WebGPU explicitly. + preferWebGPU: false, + // κ-native Holo GGUF ear (opt-in): when WebGPU is present, run Whisper 100% on the κ-substrate — + // weights streamed by κ from the .holo (HTTP-Range + per-block L5 + OPFS), GPU encoder-decoder + // forward, no transformers.js/ONNX. Set { module, holoUrl } (resolved relative to this module) to + // enable; ANY failure transparently falls back to the transformers path below. null = off. + knativeEar: null, // e.g. { module: "../../q/forge/gpu/holo-whisper-ear.mjs", holoUrl: "…/whisper-base.holo" } + // κ-served WASM fallback (opt-in): when the κ-native ear is off (no WebGPU) and the transformers/ONNX path + // runs, serve Whisper's ONNX files from its .holo (HTTP-Range + per-block L5 + OPFS + serverless) into the + // SAME engine — so the any-browser floor is ALSO content-addressed/warm/serverless, not a flat download. + // ANY failure restores fetch + falls back to the vendored ONNX files. null = off. Same shim every faculty uses. + knativeServe: { // κ-served whisper-tiny — its ONNX files stream from the .holo (per-block L5 + OPFS warm) into the SAME transformers engine. ANY failure restores fetch → vendored ONNX. matches modelWASM. + module: "/apps/q/forge/gpu/holo-onnx-kserve.mjs", + holoUrl: "/.holo/sha256/361209ec2ff387beb9e763017cd50d18f9cc8b5276346d62420922ca9a5d9185", // κ-pure source; the SW serves this directly once the κ-route heal-fallback lands (then `release` below can go) + modelId: "onnx-community/whisper-tiny", + // delivery TODAY: openHoloFiles (holo-files.mjs:18-21) range-fetches holoUrl; on miss it falls back to + // `release` as a DIRECT URL — CORS + Range from the GitHub Release CDN, per-block re-derived (L5). + release: "https://github.com/Hologram-Technologies/hologram-apps/releases/download/weights-v1/361209ec2ff387beb9e763017cd50d18f9cc8b5276346d62420922ca9a5d9185", + }, + lang: "en", +}; + +function moduleBase() { + try { return new URL("./", import.meta.url).href; } // …/_shared/voice/ + catch (e) { return new URL("./", location.href).href; } +} + +async function hasWebGPU() { + try { return !!(navigator.gpu && (await navigator.gpu.requestAdapter())); } catch (e) { return false; } +} + +// ── the engine ────────────────────────────────────────────────────────────────────────────────── +export function createASR(opts = {}) { + const cfg = Object.assign({}, DEFAULTS, opts); + let pipe = null, knative = null, info = { ready: false, engine: null, model: null, device: null }; + let loading = null; + + async function load(onProgress) { + if (pipe || knative) return info; + if (loading) return loading; + loading = (async () => { + const base = moduleBase(); + // κ-native Holo GGUF ear (opt-in, WebGPU only). Transparently falls back on any failure. + if (cfg.knativeEar && cfg.knativeEar.module && (await hasWebGPU())) { + try { + const mod = await import(/* @vite-ignore */ new URL(cfg.knativeEar.module, base).href); + // UNIFIED PACK (opt-in via cfg.knativeEar.pack): stream the encoder/joint/loose-files from the ONE q-models + // pack instead of per-model .holo. makePackEarDeps maps the ear's URLs → pack views and is FAIL-SOFT (pack + // unreachable → the ear's own standalone url/release), and this whole block already falls back to + // transformers on any error — so enabling the pack can never strand listening. + let earDeps; + if (cfg.knativeEar.pack) { try { + const pp = await import(/* @vite-ignore */ new URL("../../q/forge/gpu/holo-q-pack-provider.mjs", base).href); + const fm = await import(/* @vite-ignore */ new URL("./holo-q-faculty-models.mjs", base).href); + // parakeet ear wants the raw openHoloStream views (+ loose files); the moonshine ear wants the whisper-shaped + // view (getF32/getQuant). Pick by module — both fail-soft to standalone inside the loaders. + earDeps = /parakeet/.test(cfg.knativeEar.module) ? pp.makePackEarDeps({ packSpec: fm.packSpec }) : { openStream: pp.makePackOpenStream({ packSpec: fm.packSpec }) }; + } catch (_) { earDeps = undefined; } } + const ear = (mod.createWhisperEar || mod.default)({ holoUrl: new URL(cfg.knativeEar.holoUrl, base).href, upgradeUrl: cfg.knativeEar.upgradeUrl ? new URL(cfg.knativeEar.upgradeUrl, base).href : null, kappa: cfg.knativeEar.kappa, release: cfg.knativeEar.release || "", upgradeKappa: cfg.knativeEar.upgradeKappa || "", upgradeRelease: cfg.knativeEar.upgradeRelease || "", language: cfg.lang }, earDeps); + await ear.load(onProgress); + knative = ear; info = Object.assign({ ready: true, engine: "holo-gguf-κnative" }, ear.info()); + return info; + } catch (e) { try { console.warn("[HoloVoice ASR] κ-native ear unavailable, using transformers:", e && e.message || e); } catch (_) {} } + } + const libUrl = cfg.remote ? cfg.libRemote : new URL(cfg.lib, base).href; + const tf = await import(/* @vite-ignore */ libUrl); // throws here if not vendored → caller falls back + const { pipeline, env } = tf; + const webgpu = cfg.preferWebGPU && (await hasWebGPU()); + const device = webgpu ? "webgpu" : "wasm"; + const model = webgpu ? cfg.modelWebGPU : cfg.modelWASM; + if (env) { + env.allowRemoteModels = !!cfg.remote; // serverless: weights come from the κ-disk only + env.allowLocalModels = !cfg.remote; + if (!cfg.remote) { + env.localModelPath = new URL(cfg.localPath, base).href; + // point onnxruntime-web at the vendored wasm so NO binary is fetched from a CDN. + const wasmPaths = new URL(cfg.ortPath, base).href; + if (env.backends && env.backends.onnx && env.backends.onnx.wasm) env.backends.onnx.wasm.wasmPaths = wasmPaths; + } + // run ORT in a Web Worker (WASM only) so streaming partial transcriptions don't block the main + // thread / the VAD loop — the UI stays responsive while recognition runs as you speak. + try { if (!webgpu && cfg.proxy !== false && env.backends && env.backends.onnx && env.backends.onnx.wasm) env.backends.onnx.wasm.proxy = true; } catch (e) {} + } + const prog = (p) => { try { onProgress && onProgress({ phase: p.status || "load", file: p.file, loaded: p.loaded, total: p.total, device, model }); } catch (e) {} }; + // κ-served WASM fallback: install the .holo fetch shim BEFORE the pipeline loads, so Whisper's ONNX files + // are served by content address. Transparently falls back to vendored ONNX on any failure. + let kserve = null; + if (cfg.knativeServe && cfg.knativeServe.module && cfg.knativeServe.holoUrl) { + try { + const km = await import(/* @vite-ignore */ new URL(cfg.knativeServe.module, base).href); + kserve = await (km.serveModelFromHolo || km.default)({ holoUrl: new URL(cfg.knativeServe.holoUrl, base).href, modelId: cfg.knativeServe.modelId || model, release: cfg.knativeServe.release || "" }); + } catch (e) { try { console.warn("[HoloVoice ASR] κ-served fallback unavailable, using vendored ONNX:", e && e.message || e); } catch (_) {} kserve = null; } + } + const dtype = cfg.quantized ? "q8" : "fp32"; + const build = (m) => pipeline("automatic-speech-recognition", m, { device, dtype, progress_callback: prog }); + let used = model; + try { pipe = await build(model); } + catch (e) { // tiny not vendored (or load failed) → fall back to base + if (kserve) { try { kserve.restore(); } catch (_) {} kserve = null; } // the κ-served tiny failed → un-shim so base loads vendored + if (!webgpu && cfg.modelWASMFallback && cfg.modelWASMFallback !== model) { used = cfg.modelWASMFallback; pipe = await build(used); } + else throw e; + } + info = { ready: true, engine: kserve ? "transformers-κserved" : "transformers", model: used, device, servedFromHolo: kserve ? kserve.served.length : 0 }; + return info; + })().catch((e) => { loading = null; throw e; }); + return loading; + } + + // transcribe(audio, opts) — audio is a Float32Array of mono PCM at 16 kHz (Holo Voice resamples). + async function transcribe(audio, o = {}) { + if (!pipe && !knative) await load(o.onProgress); + if (knative) return knative.transcribe(audio, o); // κ-native ear (same {text,…} shape) + const args = { language: o.language || null, task: "transcribe", chunk_length_s: 30, stride_length_s: 5 }; + if (o.prompt) args.prompt = o.prompt; // best-effort decoding bias (ignored where unsupported) + const r = await pipe(audio, args); + const text = (r && (Array.isArray(r) ? r.map((x) => x.text).join(" ") : r.text) || "").trim(); + return { text, language: o.language || null, runtime: info.device === "webgpu" ? "browser-webgpu" : "browser-wasm" }; + } + + return { id: "holo-voice-asr", load, transcribe, info: () => info, sampleRate: 16000 }; +} + +export default createASR; diff --git a/_shared/voice/holo-voice-tts.mjs b/_shared/voice/holo-voice-tts.mjs new file mode 100644 index 0000000000000000000000000000000000000000..154d2040aead5cc11f7c37322352032bb9b9e7dc --- /dev/null +++ b/_shared/voice/holo-voice-tts.mjs @@ -0,0 +1,115 @@ +// holo-voice-tts.mjs — Q's natural voice: Kokoro-82M text-to-speech, on-device and serverless. +// +// Kokoro is the best small open TTS; kokoro-js drives it. It imports "@huggingface/transformers" and +// "phonemizer" as bare specifiers — the frames map those (import map) to the vendored copies under +// vendor/kokoro/, so this runs entirely in the browser with no CDN and no server. We import the SAME +// transformers module first and point its ONNX env at the vendored wasm + model, so kokoro inherits a +// fully local, serverless config. WASM by default (any browser); WebGPU opt-in. If anything here fails, +// holo-voice.js falls back to the browser's built-in speechSynthesis — so Q always talks. + +const DEFAULTS = { + tfLib: "vendor/kokoro/transformers/transformers.js", // kokoro's transformers (3.5.1), import-mapped + ortPath: "vendor/kokoro/transformers/", // its bundled onnxruntime-web wasm + kokoroLib: "vendor/kokoro/kokoro.js", + localPath: "vendor/models/", + model: "onnx-community/Kokoro-82M-v1.0-ONNX", + dtype: "q8", // → onnx/model_quantized.onnx + voice: "af_heart", // warm, natural default + preferWebGPU: false, + // κ-served voice (opt-in): serve Kokoro's ONNX files from its .holo (HTTP-Range + per-block L5 + OPFS warm + // cache + serverless multi-source) into the SAME kokoro-js/onnxruntime engine — content-addressed weight + // delivery, no engine change. Set { module, holoUrl, modelId?, kappa, release } to enable; ANY failure + // transparently restores fetch and falls back to the vendored ONNX files below. null = off (vendored path). + knativeVoice: null, // e.g. { module: "../../q/forge/gpu/holo-onnx-kserve.mjs", holoUrl: "…/kokoro-82m.holo" } +}; + +function moduleBase() { try { return new URL("./", import.meta.url).href; } catch (e) { return new URL("./", location.href).href; } } + +export function createTTS(opts = {}) { + const cfg = Object.assign({}, DEFAULTS, opts); + let tts = null, loading = null, info = { ready: false, device: null }; + + async function load(onProgress) { + if (tts) return info; + if (loading) return loading; + loading = (async () => { + const base = moduleBase(); + // configure the shared transformers env BEFORE kokoro imports it (same module URL = same instance). + const TF = await import(/* @vite-ignore */ new URL(cfg.tfLib, base).href); + if (TF.env) { + TF.env.allowRemoteModels = false; TF.env.allowLocalModels = true; + TF.env.localModelPath = new URL(cfg.localPath, base).href; + if (TF.env.backends && TF.env.backends.onnx && TF.env.backends.onnx.wasm) { + TF.env.backends.onnx.wasm.wasmPaths = new URL(cfg.ortPath, base).href; + TF.env.backends.onnx.wasm.proxy = true; // worker → no UI freeze while synthesizing + } + } + // κ-served voice (opt-in): install the .holo fetch shim BEFORE kokoro loads, so its model files are + // served by content address from the .holo. Transparently falls back to the vendored ONNX on any failure. + let kserve = null; + if (cfg.knativeVoice && cfg.knativeVoice.module && cfg.knativeVoice.holoUrl) { + try { + const km = await import(/* @vite-ignore */ new URL(cfg.knativeVoice.module, base).href); + // serve Kokoro's files from the ONE q-models pack when opted in (fail-soft → holoUrl/release standalone) + let openFiles = null; + if (cfg.knativeVoice.pack) { try { const pp = await import(/* @vite-ignore */ new URL("/apps/q/forge/gpu/holo-q-pack-provider.mjs", base).href); const fm = await import(/* @vite-ignore */ new URL("./holo-q-faculty-models.mjs", import.meta.url).href); openFiles = pp.makePackOpenFiles("kokoro-82m", { packSpec: fm.packSpec }); } catch (_) {} } + kserve = await (km.serveModelFromHolo || km.default)({ + holoUrl: new URL(cfg.knativeVoice.holoUrl, base).href, + modelId: cfg.knativeVoice.modelId || cfg.model, + release: cfg.knativeVoice.release || "", + openFiles, + }); + } catch (e) { try { console.warn("[HoloVoice TTS] κ-served voice unavailable, using vendored ONNX:", e && e.message || e); } catch (_) {} kserve = null; } + } + const mod = await import(/* @vite-ignore */ new URL(cfg.kokoroLib, base).href); + const KokoroTTS = mod.KokoroTTS || (mod.default && mod.default.KokoroTTS); + // device: explicit cfg.device wins (the bake-off harness sets it); else preferWebGPU→webgpu when available, else wasm. + const device = cfg.device || ((cfg.preferWebGPU && navigator.gpu) ? "webgpu" : "wasm"); + try { + tts = await KokoroTTS.from_pretrained(cfg.model, { dtype: cfg.dtype, device, progress_callback: onProgress }); + } catch (e) { if (kserve) { try { kserve.restore(); } catch (_) {} } throw e; } // κ-served load failed → un-shim so the caller's fallback fetches the vendored files + info = { ready: true, device, model: cfg.model, dtype: cfg.dtype, engine: kserve ? "kokoro-κserved" : "kokoro-onnx", servedFromHolo: kserve ? kserve.served.length : 0 }; + return info; + })().catch((e) => { loading = null; throw e; }); + return loading; + } + + // synth(text, {voice}) → { audio: Float32Array, sampling_rate } (a kokoro-js RawAudio). + async function synth(text, o = {}) { + if (!tts) await load(o.onProgress); + return tts.generate(String(text || "").trim(), { voice: o.voice || cfg.voice }); + } + function listVoices() { try { return tts && tts.voices ? Object.keys(tts.voices) : []; } catch (e) { return []; } } + + return { id: "holo-voice-tts", load, synth, voices: listVoices, info: () => info }; +} + +// createTieredTTS — the "instant + HD" voice composition, behind the SAME seam ({load, synth, voices, info}). +// Q speaks immediately on the `primary` engine (e.g. Kokoro); a heavier, more natural `hd` engine loads in +// the BACKGROUND and, once ready, transparently takes over future utterances — never blocking the first +// word, never a gap. If HD fails to load (or errors mid-use), it silently stays on / falls back to primary, +// so the voice is always available. This is the production scaffolding any HD model (Parler · StyleTTS2 · +// a κ-sealed clone) plugs into — pass it as `hd` and nothing else changes. `hd` omitted ⇒ pure primary. +export function createTieredTTS(opts = {}) { + const primary = opts.primary, hd = opts.hd || null; + if (!primary) throw new Error("createTieredTTS: a primary engine is required"); + let tier = "primary", hdReady = false, hdFailed = false; + async function load(onProgress) { + const pinfo = await primary.load(onProgress); // the instant tier is ready first — first word never waits + if (hd) { + Promise.resolve().then(() => hd.load()) // promote in the background; never blocks playback + .then(() => { hdReady = true; tier = "hd"; try { opts.onUpgrade && opts.onUpgrade(hd.info ? hd.info() : null); } catch (e) {} }) + .catch(() => { hdFailed = true; }); + } + return info(); + } + async function synth(text, o = {}) { + if (hdReady && hd) { try { return await hd.synth(text, o); } catch (e) { hdReady = false; tier = "primary"; } } // HD error → fall back, keep talking + return primary.synth(text, o); + } + function voices() { try { const s = new Set([].concat(primary.voices ? primary.voices() : [], hd && hd.voices ? hd.voices() : [])); return Array.from(s); } catch (e) { return []; } } + function info() { return { ready: !!(primary.info && primary.info().ready), tier, hdReady, hdFailed, primary: primary.info && primary.info(), hd: hd && hd.info ? hd.info() : null }; } + return { id: "holo-voice-tier", load, synth, voices, info, get tier() { return tier; } }; +} + +export default createTTS; diff --git a/_shared/voice/holo-voice-turn.mjs b/_shared/voice/holo-voice-turn.mjs new file mode 100644 index 0000000000000000000000000000000000000000..9f22afcbc7bab1eac9ea1df5269470870d6c7a0e --- /dev/null +++ b/_shared/voice/holo-voice-turn.mjs @@ -0,0 +1,101 @@ +// holo-voice-turn.mjs — on-device SEMANTIC TURN-DETECTION for Holo Voice (deep-research RANK 1). +// +// Reads the (partial) transcript and predicts whether the USER IS DONE — so the live loop can end the +// turn the instant an utterance is semantically complete (firing before trailing silence) and, just as +// importantly, can VETO a premature endpoint when the user only paused mid-thought. This is the single +// biggest lever toward a human-call response onset (<300ms median): the 550ms fixed silence is the +// largest fixed cost in a turn, and a turn-end model removes it. +// +// Model: LiveKit's open-weights turn-detector (a small fine-tuned LLM; ONNX builds at +// onnx-community/turn-detector-ONNX · livekit/turn-detector). CPU-only via ONNX Runtime Web, ~25ms, +// q4f16 ≈118MB (verified specs; the "~50ms" figure was refuted — it's ~25ms). It outputs the +// probability that the turn is COMPLETE given the conversation so far. +// +// SERVERLESS like the other engines: weights load same-origin from the vendored κ-disk (vendor it with +// `node tools/vendor-voice-model.mjs --turn`), nothing leaves the device. Imported lazily and GATED by +// HOLO_VOICE_CONFIG.turnModel — until it's both vendored AND enabled, holo-voice.js uses its heuristic +// turn-completion scorer, so this module never blocks or breaks the working path. +// +// ⚠ EXPERIMENTAL / UNVERIFIED-ON-REAL-HW: the exact end-of-utterance read below (chat-format → forward → +// P(EOU token) at the last position) follows LiveKit's documented recipe, but the deep-research pass +// verified the model's existence/specs, NOT this transformers.js invocation. predict() returns null on +// ANY mismatch so the caller falls back to the heuristic. Verify on real hardware before trusting it, +// and re-pin the q4f16 inference once confirmed. + +const DEFAULTS = { + lib: "vendor/transformers/transformers.js", + libRemote: "https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.0.2", + ortPath: "vendor/transformers/", + model: "onnx-community/turn-detector-ONNX", + localPath: "vendor/models/", + remote: false, + dtype: "q4f16", // ~118MB; falls back to q8 if the model lacks a q4f16 file + // candidate end-of-turn marker tokens (Qwen-style chat end). The first that resolves to an id is used. + eouTokens: ["<|im_end|>", "<|endoftext|>"], +}; + +function moduleBase() { + try { return new URL("./", import.meta.url).href; } + catch (e) { return new URL("./", location.href).href; } +} + +export function createTurnDetector(opts = {}) { + const cfg = Object.assign({}, DEFAULTS, opts); + let model = null, tok = null, Tensor = null, eouId = null, info = { ready: false, model: cfg.model, device: null }; + let loading = null; + + async function load(onProgress) { + if (model) return info; + if (loading) return loading; + loading = (async () => { + const base = moduleBase(); + const libUrl = cfg.remote ? cfg.libRemote : new URL(cfg.lib, base).href; + const tf = await import(/* @vite-ignore */ libUrl); // throws if not vendored → caller falls back + const { AutoTokenizer, AutoModelForCausalLM, env } = tf; Tensor = tf.Tensor; + if (env) { + env.allowRemoteModels = !!cfg.remote; + env.allowLocalModels = !cfg.remote; + if (!cfg.remote) { + env.localModelPath = new URL(cfg.localPath, base).href; + const wasmPaths = new URL(cfg.ortPath, base).href; + if (env.backends && env.backends.onnx && env.backends.onnx.wasm) env.backends.onnx.wasm.wasmPaths = wasmPaths; + } + } + const prog = (p) => { try { onProgress && onProgress({ phase: p.status || "load", file: p.file, loaded: p.loaded, total: p.total, model: cfg.model }); } catch (e) {} }; + tok = await AutoTokenizer.from_pretrained(cfg.model, { progress_callback: prog }); + let dtype = cfg.dtype; + try { model = await AutoModelForCausalLM.from_pretrained(cfg.model, { device: "wasm", dtype, progress_callback: prog }); } + catch (e) { dtype = "q8"; model = await AutoModelForCausalLM.from_pretrained(cfg.model, { device: "wasm", dtype, progress_callback: prog }); } + // resolve the end-of-turn token id once (the marker whose probability == "the turn is complete"). + for (const t of cfg.eouTokens) { try { const ids = tok.encode(t, { add_special_tokens: false }); if (ids && ids.length === 1) { eouId = ids[0]; break; } } catch (e) {} } + info = { ready: true, model: cfg.model, device: "wasm", dtype, eouId }; + return info; + })().catch((e) => { loading = null; throw e; }); + return loading; + } + + // predict(text) → probability in [0,1] that the user's turn is COMPLETE. null on any failure. + async function predict(text) { + try { + if (!model || eouId == null || !Tensor) return null; + const enc = tok.apply_chat_template([{ role: "user", content: String(text || "") }], { add_generation_prompt: false, tokenize: true }); + const ids = Array.isArray(enc) ? enc : (enc && enc.input_ids) || null; + if (!ids || !ids.length) return null; + const input_ids = new Tensor("int64", BigInt64Array.from(ids.map((x) => BigInt(x))), [1, ids.length]); + const attention_mask = new Tensor("int64", BigInt64Array.from(ids.map(() => 1n)), [1, ids.length]); + const out = await model({ input_ids, attention_mask }); + const logits = out && (out.logits || out.last_hidden_state); + if (!logits || !logits.dims) return null; + const [, seq, vocab] = logits.dims; // [1, seq, vocab] + const off = (seq - 1) * vocab; // logits at the final position + const data = logits.data; + let max = -Infinity; for (let i = 0; i < vocab; i++) { const v = data[off + i]; if (v > max) max = v; } + let sum = 0; for (let i = 0; i < vocab; i++) sum += Math.exp(data[off + i] - max); + return Math.exp(data[off + eouId] - max) / sum; // softmax prob of the EOU token + } catch (e) { return null; } + } + + return { id: "holo-voice-turn", load, predict, info: () => info }; +} + +export default createTurnDetector; diff --git a/_shared/voice/holo-voice-vad.mjs b/_shared/voice/holo-voice-vad.mjs new file mode 100644 index 0000000000000000000000000000000000000000..67b09208befa8e509336da586ae55f56c7e83b88 --- /dev/null +++ b/_shared/voice/holo-voice-vad.mjs @@ -0,0 +1,90 @@ +// holo-voice-vad.mjs — the SOVEREIGN, pure-ONNX stage-1 wake gate: Silero VAD (MIT) running on the +// onnxruntime-web that transformers.js ALREADY bundles. No emscripten build, no account key, no new +// runtime — just a 2 MB ONNX model loaded through the SAME vendored transformers instance as the ASR, so +// it inherits the serverless config (local weights, vendored wasm, no CDN) and shares the loaded ORT. +// +// WHY (first principles): the always-on wake loop should spend Whisper ONLY on real speech. Energy-RMS +// can't tell speech from a slammed door, music, or a TV; Silero VAD can. So it's the cheap stage-1 gate — +// it proposes ("this segment is speech"), Whisper-tiny disposes (confirms "Q" + the carrier). This is the +// pure-ONNX replacement for the sherpa-onnx KWS path (which needed an emscripten wasm runtime we can't +// vendor in-browser). Sovereign AND web-native: it runs on the runtime we already ship. +// +// const vad = await createVAD({ base, threshold }); // loads onnx-community/silero-vad via AutoModel +// const p = await vad.speechProb(frame512_16k); // streaming: one 512-sample frame → P(speech) +// const has = await vad.segmentHasSpeech(float32_16k); // one-shot: scan a whole segment → bool +// vad.reset(); +// +// HONESTY: Silero VAD-on-transformers.js is the documented pattern (the transformers.js realtime-whisper +// demo loads onnx-community/silero-vad with `config:{model_type:'custom'}` — there's no config.json in the +// repo). The model is v5 (I/O: input·sr·state → output·stateN; state [2,1,128]). The exact call is verified +// to LOAD + RUN in-browser here; gating accuracy/threshold needs real-mic tuning. Fails closed: any error +// → the caller keeps its energy-VAD path (no regression). + +const SAMPLE_RATE = 16000; +const FRAME = 512; // Silero v5 expects 512 samples per step at 16 kHz +const STATE_DIMS = [2, 1, 128]; // v5 unified LSTM state +const STATE_LEN = 2 * 1 * 128; + +// Resolve relative to THIS module's URL — same discipline as holo-voice-asr.mjs (vendor/ is a sibling). +function moduleBase() { + try { return new URL("./", import.meta.url).href; } + catch (e) { return new URL("./", (typeof location !== "undefined" ? location.href : "")).href; } +} + +export async function createVAD(opts) { + opts = opts || {}; + const base = opts.base || moduleBase(); + const lib = opts.lib || "vendor/transformers/transformers.js"; + const localPath = opts.localPath || "vendor/models/"; + const ortPath = opts.ortPath || "vendor/transformers/"; + const model = opts.model || "onnx-community/silero-vad"; + const dtype = opts.dtype || "fp32"; // model.onnx (2 MB). 'q8' → model_quantized.onnx (639 KB) if vendored. + const threshold = opts.threshold != null ? opts.threshold : 0.5; + + // Import the SAME vendored transformers module instance the ASR uses (ES-module cache → shared env + ORT). + const tf = await import(/* @vite-ignore */ new URL(lib, base).href); + const { AutoModel, Tensor, env } = tf; + if (!AutoModel || !Tensor) throw new Error("transformers.js missing AutoModel/Tensor"); + if (env) { // idempotent: serverless, weights + wasm are vendored + env.allowRemoteModels = false; env.allowLocalModels = true; + env.localModelPath = new URL(localPath, base).href; + try { const wp = new URL(ortPath, base).href; if (env.backends && env.backends.onnx && env.backends.onnx.wasm) env.backends.onnx.wasm.wasmPaths = wp; } catch (e) {} + } + + // No config.json in the repo → pass a custom config inline (the documented Silero-on-transformers.js trick). + const net = await AutoModel.from_pretrained(model, { config: { model_type: "custom" }, dtype: dtype }); + + const sr = new Tensor("int64", [BigInt(SAMPLE_RATE)], []); + let state = new Tensor("float32", new Float32Array(STATE_LEN), STATE_DIMS); + function reset() { state = new Tensor("float32", new Float32Array(STATE_LEN), STATE_DIMS); } + + // run ONE 512-sample frame → P(speech). Carries the recurrent state forward. + async function speechProb(frame) { + let x = frame; + if (x.length !== FRAME) { const f = new Float32Array(FRAME); f.set(x.subarray ? x.subarray(0, FRAME) : x.slice(0, FRAME)); x = f; } // pad/trim to 512 + const input = new Tensor("float32", x, [1, FRAME]); + const out = await net({ input: input, sr: sr, state: state }); + if (out.stateN) state = out.stateN; + const o = out.output && out.output.data; + return o && o.length ? o[0] : 0; + } + + // one-shot: scan a whole 16 kHz segment in 512-sample frames → did ANY frame exceed the speech threshold? + // Returns { speech, maxProb }. Resets state first so each segment is judged independently. + async function segmentHasSpeech(audio, thr) { + reset(); + const t = thr != null ? thr : threshold; + let max = 0; + for (let i = 0; i + 1 <= audio.length; i += FRAME) { + const end = Math.min(i + FRAME, audio.length); + const p = await speechProb(audio.subarray ? audio.subarray(i, end) : audio.slice(i, end)); + if (p > max) max = p; + if (max >= t) { /* keep scanning a touch could refine, but early-out is enough for a gate */ break; } + } + return { speech: max >= t, maxProb: max }; + } + + return { backend: "silero-vad-onnx", threshold: threshold, model: model, speechProb: speechProb, segmentHasSpeech: segmentHasSpeech, reset: reset }; +} + +export default { createVAD }; diff --git a/_shared/voice/vendor/kokoro/kokoro.js b/_shared/voice/vendor/kokoro/kokoro.js new file mode 100644 index 0000000000000000000000000000000000000000..026692226ca7ecd2271c615692cc340bbe3e87b2 --- /dev/null +++ b/_shared/voice/vendor/kokoro/kokoro.js @@ -0,0 +1 @@ +import{StyleTextToSpeech2Model as e,AutoTokenizer as a,Tensor as t,RawAudio as r,env as n}from"@huggingface/transformers";import{phonemize as l}from"phonemizer";import s from"path";import i from"fs/promises";function o(e){if(e.includes("."))return e;if(e.includes(":")){let[a,t]=e.split(":").map(Number);return 0===t?`${a} o'clock`:t<10?`${a} oh ${t}`:`${a} ${t}`}let a=parseInt(e.slice(0,4),10);if(a<1100||a%1e3<10)return e;let t=e.slice(0,2),r=parseInt(e.slice(2,4),10),n=e.endsWith("s")?"s":"";if(a%1e3>=100&&a%1e3<=999){if(0===r)return`${t} hundred${n}`;if(r<10)return`${t} oh ${r}${n}`}return`${t} ${r}${n}`}function c(e){const a="$"===e[0]?"dollar":"pound";if(isNaN(Number(e.slice(1))))return`${e.slice(1)} ${a}s`;if(!e.includes(".")){let t="1"===e.slice(1)?"":"s";return`${e.slice(1)} ${a}${t}`}const[t,r]=e.slice(1).split("."),n=parseInt(r.padEnd(2,"0"),10);return`${t} ${a}${"1"===t?"":"s"} and ${n} ${"$"===e[0]?1===n?"cent":"cents":1===n?"penny":"pence"}`}function g(e){let[a,t]=e.split(".");return`${a} point ${t.split("").join(" ")}`}const u=new RegExp(`(\\s*[${d=';:,.!?¡¿—…"«»“”(){}[]',d.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}]+\\s*)+`,"g");var d;async function m(e,a="a",t=!0){t&&(e=function(e){return e.replace(/[‘’]/g,"'").replace(/«/g,"“").replace(/»/g,"”").replace(/[“”]/g,'"').replace(/\(/g,"«").replace(/\)/g,"»").replace(/、/g,", ").replace(/。/g,". ").replace(/!/g,"! ").replace(/,/g,", ").replace(/:/g,": ").replace(/;/g,"; ").replace(/?/g,"? ").replace(/[^\S \n]/g," ").replace(/ +/," ").replace(/(?<=\n) +(?=\n)/g,"").replace(/\bD[Rr]\.(?= [A-Z])/g,"Doctor").replace(/\b(?:Mr\.|MR\.(?= [A-Z]))/g,"Mister").replace(/\b(?:Ms\.|MS\.(?= [A-Z]))/g,"Miss").replace(/\b(?:Mrs\.|MRS\.(?= [A-Z]))/g,"Mrs").replace(/\betc\.(?! [A-Z])/gi,"etc").replace(/\b(y)eah?\b/gi,"$1e'a").replace(/\d*\.\d+|\b\d{4}s?\b|(?e.replace(/\./g,"-"))).replace(/(?<=[A-Z])\.(?=[A-Z])/gi,"-").trim()}(e));const r=function(e,a){const t=[];let r=0;for(const n of e.matchAll(a)){const a=n[0];r0&&t.push({match:!0,text:a}),r=n.index+a.length}return re?a:(await l(a,n)).join(" "))))).join("");let i=s.replace(/kəkˈoːɹoʊ/g,"kˈoʊkəɹoʊ").replace(/kəkˈɔːɹəʊ/g,"kˈəʊkəɹəʊ").replace(/ʲ/g,"j").replace(/r/g,"ɹ").replace(/x/g,"k").replace(/ɬ/g,"l").replace(/(?<=[a-zɹː])(?=hˈʌndɹɪd)/g," ").replace(/ z(?=[;:,.!?¡¿—…"«»“” ]|$)/g,"z");return"a"===a&&(i=i.replace(/(?<=nˈaɪn)ti(?!ː)/g,"di")),i.trim()}function p(e,a=!0){return".!?…。?!".includes(e)||a&&"\n"===e}function f(e,a){let t=a;for(;t0&&t0&&this._sentences.push(e),this._buffer="",this._resolve()}_resolve(){this._resolver&&(this._resolver(),this._resolver=null)}_process(){let e=0;const a=this._buffer,t=a.length;let r=0,n=[];const l=e=>{let r=e;for(;r+1=0&&/\S/.test(a[c]);)c--;c=Math.max(e,c+1);const g=f(a,c);if(!g){++r;continue}if((/https?[,:]\/\//.test(g)||g.includes("@"))&&!p(g.at(-1))){r=c+g.length;continue}if(_(g)){++r;continue}if(/^([A-Za-z]\.)+$/.test(g)&&o0&&this._resolve()}async*[Symbol.asyncIterator](){if(this._resolver)throw new Error("Another iterator is already active.");for(;;)if(this._sentences.length>0)yield this._sentences.shift();else{if(this._closed)break;await new Promise((e=>{this._resolver=e}))}}[Symbol.iterator](){this.flush();const e=this._sentences[Symbol.iterator]();return this._sentences=[],e}get sentences(){return this._sentences}}const $=Object.freeze({af_heart:{name:"Heart",language:"en-us",gender:"Female",traits:"❤️",targetQuality:"A",overallGrade:"A"},af_alloy:{name:"Alloy",language:"en-us",gender:"Female",targetQuality:"B",overallGrade:"C"},af_aoede:{name:"Aoede",language:"en-us",gender:"Female",targetQuality:"B",overallGrade:"C+"},af_bella:{name:"Bella",language:"en-us",gender:"Female",traits:"🔥",targetQuality:"A",overallGrade:"A-"},af_jessica:{name:"Jessica",language:"en-us",gender:"Female",targetQuality:"C",overallGrade:"D"},af_kore:{name:"Kore",language:"en-us",gender:"Female",targetQuality:"B",overallGrade:"C+"},af_nicole:{name:"Nicole",language:"en-us",gender:"Female",traits:"🎧",targetQuality:"B",overallGrade:"B-"},af_nova:{name:"Nova",language:"en-us",gender:"Female",targetQuality:"B",overallGrade:"C"},af_river:{name:"River",language:"en-us",gender:"Female",targetQuality:"C",overallGrade:"D"},af_sarah:{name:"Sarah",language:"en-us",gender:"Female",targetQuality:"B",overallGrade:"C+"},af_sky:{name:"Sky",language:"en-us",gender:"Female",targetQuality:"B",overallGrade:"C-"},am_adam:{name:"Adam",language:"en-us",gender:"Male",targetQuality:"D",overallGrade:"F+"},am_echo:{name:"Echo",language:"en-us",gender:"Male",targetQuality:"C",overallGrade:"D"},am_eric:{name:"Eric",language:"en-us",gender:"Male",targetQuality:"C",overallGrade:"D"},am_fenrir:{name:"Fenrir",language:"en-us",gender:"Male",targetQuality:"B",overallGrade:"C+"},am_liam:{name:"Liam",language:"en-us",gender:"Male",targetQuality:"C",overallGrade:"D"},am_michael:{name:"Michael",language:"en-us",gender:"Male",targetQuality:"B",overallGrade:"C+"},am_onyx:{name:"Onyx",language:"en-us",gender:"Male",targetQuality:"C",overallGrade:"D"},am_puck:{name:"Puck",language:"en-us",gender:"Male",targetQuality:"B",overallGrade:"C+"},am_santa:{name:"Santa",language:"en-us",gender:"Male",targetQuality:"C",overallGrade:"D-"},bf_emma:{name:"Emma",language:"en-gb",gender:"Female",traits:"🚺",targetQuality:"B",overallGrade:"B-"},bf_isabella:{name:"Isabella",language:"en-gb",gender:"Female",targetQuality:"B",overallGrade:"C"},bm_george:{name:"George",language:"en-gb",gender:"Male",targetQuality:"B",overallGrade:"C"},bm_lewis:{name:"Lewis",language:"en-gb",gender:"Male",targetQuality:"C",overallGrade:"D+"},bf_alice:{name:"Alice",language:"en-gb",gender:"Female",traits:"🚺",targetQuality:"C",overallGrade:"D"},bf_lily:{name:"Lily",language:"en-gb",gender:"Female",traits:"🚺",targetQuality:"C",overallGrade:"D"},bm_daniel:{name:"Daniel",language:"en-gb",gender:"Male",traits:"🚹",targetQuality:"C",overallGrade:"D"},bm_fable:{name:"Fable",language:"en-gb",gender:"Male",traits:"🚹",targetQuality:"B",overallGrade:"C"}});const G=new Map;async function k(e){if(G.has(e))return G.get(e);const a=new Float32Array(await async function(e){if(i&&Object.hasOwn(i,"readFile")){const a="undefined"!=typeof __dirname?__dirname:import.meta.dirname,t=s.resolve(a,`../voices/${e}.bin`),{buffer:r}=await i.readFile(t);return r}const a=`https://huggingface.co/onnx-community/Kokoro-82M-v1.0-ONNX/resolve/main/voices/${e}.bin`;let t;try{t=await caches.open("kokoro-voices");const e=await t.match(a);if(e)return await e.arrayBuffer()}catch(e){console.warn("Unable to open cache",e)}const r=await fetch(a),n=await r.arrayBuffer();if(t)try{await t.put(a,new Response(n,{headers:r.headers}))}catch(e){console.warn("Unable to cache file",e)}return n}(e));return G.set(e,a),a}class M{constructor(e,a){this.model=e,this.tokenizer=a}static async from_pretrained(t,{dtype:r="fp32",device:n=null,progress_callback:l=null}={}){const s=e.from_pretrained(t,{progress_callback:l,dtype:r,device:n}),i=a.from_pretrained(t,{progress_callback:l}),o=await Promise.all([s,i]);return new M(...o)}get voices(){return $}list_voices(){console.table($)}_validate_voice(e){if(!$.hasOwnProperty(e))throw console.error(`Voice "${e}" not found. Available voices:`),console.table($),new Error(`Voice "${e}" not found. Should be one of: ${Object.keys($).join(", ")}.`);return e.at(0)}async generate(e,{voice:a="af_heart",speed:t=1}={}){const r=this._validate_voice(a),n=await m(e,r),{input_ids:l}=this.tokenizer(n,{truncation:!0});return this.generate_from_ids(l,{voice:a,speed:t})}async generate_from_ids(e,{voice:a="af_heart",speed:n=1}={}){const l=256*Math.min(Math.max(e.dims.at(-1)-2,0),509),s=(await k(a)).slice(l,l+256),i={input_ids:e,style:new t("float32",s,[1,256]),speed:new t("float32",[n],[1])},{waveform:o}=await this.model(i);return new r(o.data,24e3)}async*stream(e,{voice:a="af_heart",speed:t=1,split_pattern:r=null}={}){const n=this._validate_voice(a);let l;if(e instanceof w)l=e;else{if("string"!=typeof e)throw new Error("Invalid input type. Expected string or TextSplitterStream.");{l=new w;const a=r?e.split(r).map((e=>e.trim())).filter((e=>e.length>0)):[e];l.push(...a)}}for await(const e of l){const r=await m(e,n),{input_ids:l}=this.tokenizer(r,{truncation:!0}),s=await this.generate_from_ids(l,{voice:a,speed:t});yield{text:e,phonemes:r,audio:s}}}}const Q={set wasmPaths(e){n.backends.onnx.wasm.wasmPaths=e},get wasmPaths(){return n.backends.onnx.wasm.wasmPaths}};export{M as KokoroTTS,w as TextSplitterStream,Q as env}; diff --git a/_shared/voice/vendor/kokoro/phonemizer.js b/_shared/voice/vendor/kokoro/phonemizer.js new file mode 100644 index 0000000000000000000000000000000000000000..6591bd6e2ba15ffcc9f6cacadd0bc583bd604e4b --- /dev/null +++ b/_shared/voice/vendor/kokoro/phonemizer.js @@ -0,0 +1 @@ +var A=void 0!==A?A:{};A.expectedDataFileDownloads||(A.expectedDataFileDownloads=0);var e="function"==typeof importScripts,g="object"==typeof process&&"object"==typeof process.versions&&"string"==typeof process.versions.node,r="function"==typeof atob?atob:function(A){var e,g,r,C,a,I,f="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",i="",b=0;A=A.replace(/[^A-Za-z0-9\+\/\=]/g,"");do{e=f.indexOf(A.charAt(b++))<<2|(C=f.indexOf(A.charAt(b++)))>>4,g=(15&C)<<4|(a=f.indexOf(A.charAt(b++)))>>2,r=(3&a)<<6|(I=f.indexOf(A.charAt(b++))),i+=String.fromCharCode(e),64!==a&&(i+=String.fromCharCode(g)),64!==I&&(i+=String.fromCharCode(r))}while(b1&&(a=process.argv[1].replace(/\\/g,"/")),process.argv.slice(2),process.on("uncaughtException",(function(A){if(!(A instanceof O))throw A})),process.on("unhandledRejection",(function(A){throw A})),A.inspect=function(){return"[Emscripten Module object]"}),A.expectedDataFileDownloads++,function(){if(A.ENVIRONMENT_IS_PTHREAD)return;const e="/usr/share/espeak-ng-data",C=e+"/lang",a=e+"/voices";!function(I){var f=null,i=null;function b(){function g(A,e){if(!A)throw e+(new Error).stack}function r(A,e,g){this.start=A,this.end=e,this.audio=g}A.FS_createPath("/","usr",!0,!0),A.FS_createPath("/usr","share",!0,!0),A.FS_createPath("/usr/share","espeak-ng-data",!0,!0),A.FS_createPath(e,"lang",!0,!0),A.FS_createPath(C,"aav",!0,!0),A.FS_createPath(C,"art",!0,!0),A.FS_createPath(C,"azc",!0,!0),A.FS_createPath(C,"bat",!0,!0),A.FS_createPath(C,"bnt",!0,!0),A.FS_createPath(C,"ccs",!0,!0),A.FS_createPath(C,"cel",!0,!0),A.FS_createPath(C,"cus",!0,!0),A.FS_createPath(C,"dra",!0,!0),A.FS_createPath(C,"esx",!0,!0),A.FS_createPath(C,"gmq",!0,!0),A.FS_createPath(C,"gmw",!0,!0),A.FS_createPath(C,"grk",!0,!0),A.FS_createPath(C,"inc",!0,!0),A.FS_createPath(C,"ine",!0,!0),A.FS_createPath(C,"ira",!0,!0),A.FS_createPath(C,"iro",!0,!0),A.FS_createPath(C,"itc",!0,!0),A.FS_createPath(C,"jpx",!0,!0),A.FS_createPath(C,"map",!0,!0),A.FS_createPath(C,"miz",!0,!0),A.FS_createPath(C,"myn",!0,!0),A.FS_createPath(C,"poz",!0,!0),A.FS_createPath(C,"roa",!0,!0),A.FS_createPath(C,"sai",!0,!0),A.FS_createPath(C,"sem",!0,!0),A.FS_createPath(C,"sit",!0,!0),A.FS_createPath(C,"tai",!0,!0),A.FS_createPath(C,"trk",!0,!0),A.FS_createPath(C,"urj",!0,!0),A.FS_createPath(C,"zle",!0,!0),A.FS_createPath(C,"zls",!0,!0),A.FS_createPath(C,"zlw",!0,!0),A.FS_createPath(e,"voices",!0,!0),A.FS_createPath(a,"!v",!0,!0),A.FS_createPath(a,"mb",!0,!0),r.prototype={requests:{},open:function(e,g){this.name=g,this.requests[g]=this,A.addRunDependency("fp "+this.name)},send:function(){},onload:function(){var A=this.byteArray.subarray(this.start,this.end);this.finish(A)},finish:function(e){A.FS_createDataFile(this.name,null,e,!0,!0,!0),A.removeRunDependency("fp "+this.name),this.requests[this.name]=null}};for(var b=I.files,s=0;s{const A=function(A){if("boolean"==typeof g&&g){var e=Buffer.from(A,"base64");return new Uint8Array(e.buffer,e.byteOffset,e.byteLength)}try{for(var C=r(A),a=new Uint8Array(C.length),I=0;I=0;--r)g[48+r]=52+r,g[65+r]=r,g[97+r]=26+r;function C(A,e,r){for(var C,a,I=0,f=e,i=r.length,b=e+(3*i>>2)-("="==r[i-2])-("="==r[i-1]);I>4,f>2),f>2]!=f[A+4>>2]){if(qr(f[f[32972]+60>>2]),v=f[47192],iA=e+12|0,cA=e+8|0,V=D=V-6832|0,f[D+6816>>2]=0,f[D+6808>>2]=32,f[D+6800>>2]=0,v){f[47351]=0,f[47350]=0,f[47352]=0,a[189076]=0,A=f[33284],f[47353]=(0|A)>0?A:0,f[47355]=f[47354]+1,ue(O=D+5184|0,0,1600),eA=D+6800|0,V=o=V-2608|0,f[o+2156>>2]=32,f[o+2148>>2]=0,i[134760]&&(a[190280]=0,a[134760]=0),f[v+8216>>2]=0,f[v+8220>>2]=0,f[v+288>>2]=0,f[D+780>>2]=0,a[189360]=0;A:if(A=f[33691])f[o+2152>>2]=A;else{e:{g:{if(!(g=f[33285])){if(A=f[33283],f[A>>2]==f[A+4>>2]){f[o+2152>>2]=0;break A}if(!(g=f[33285]))break g}f[33285]=0;break e}f[33284]=f[33284]+1,A=f[33283],g=0|HC[f[A+8>>2]](A)}f[o+2152>>2]=g}EA=O+2|0,F=32;A:{for(;;){P=F;e:{g:{r:{if(g=f[33691],(C=f[33285])?A=0:(C=f[33285],A=f[33283],A=f[A>>2]==f[A+4>>2]),g|!A|C||!(f[32524]<0)){if(!mr(f[o+2156>>2])){if((C=(0|(A=f[49828]))>0)&(0|(g=A))<(0|(A=f[33284])))break r;if(!((0|(g=f[49845]))<=0|(0|A)<(0|g))){f[49845]=0,a[134760]=1,f[33285]=f[o+2152>>2],r=16384;break A}}F=f[o+2156>>2],f[o+2156>>2]=f[o+2152>>2];C:{a:{I:{f:{i:{if((0|(g=f[32524]))>=0){if(i[g+134736|0])break i;f[32524]=-1}if(g=f[33285])break I;if(A=f[33283],f[A>>2]!=f[A+4>>2])break f;g=32;break C}f[33691]|g||(f[o+2156>>2]=a[134736],g=1),f[32524]=g+1,g=a[g+134736|0];break C}if(!(g=f[33285]))break a}f[33285]=0;break C}f[33284]=f[33284]+1,A=f[33283],g=0|HC[f[A+8>>2]](A)}f[o+2152>>2]=g,f[33691]=0;C:if(!(u|!f[47203])){a:{if(60!=(0|(A=f[o+2156>>2]))){if(35!=(0|g)&g-97>>>0>25|38!=(0|A))break C;for(C=f[33285],k=0;;){I:{if(f[o+2156>>2]=g,!C){if(A=f[33283],f[A>>2]==f[A+4>>2])break I;g=f[o+2156>>2]}if(!(!((g=!!(0|mr(g)))|35==(0|(A=f[o+2156>>2])))|k>>>0>19)){a[(o+112|0)+k|0]=A,k=k+1|0,(g=f[33285])?(f[33285]=0,C=0):(f[33284]=f[33284]+1,A=f[33283],g=0|HC[f[A+8>>2]](A),C=f[33285]);continue}}break}a[(o+112|0)+k|0]=0;I:{f:{if(!(g=f[33285])){if(g=0,A=f[33283],f[A>>2]==f[A+4>>2])break I;if(!(g=f[33285]))break f}f[33285]=0;break I}f[33284]=f[33284]+1,A=f[33283],g=0|HC[f[A+8>>2]](A)}f[o+2152>>2]=g,f[o+100>>2]=f[o+2156>>2],f[o+104>>2]=g,f[o+96>>2]=o+112,dg(134736,84252,o+96|0);I:{if(59==f[o+2156>>2]){k=o+2156|0,M=o+2152|0,V=C=V-32|0;f:if(35!=i[0|(g=o+112|0)])A=-1,-1!=(0|(g=Hr(130752,g)))&&(f[k>>2]=g,f[M>>2]||(f[M>>2]=32),A=g);else{if(120==i[0|(A=g+1|0)]){f[C>>2]=k,A=aA(g+2|0,90005,C);break f}f[C+16>>2]=k,A=aA(A,90070,C+16|0)}if(V=C+32|0,(0|A)>0)break I}f[32524]=0,f[o+2156>>2]=38,f[o+2152>>2]=32;break C}if((0|(A=f[o+2156>>2]))>32)break C;if(!(g=f[33692]-20|0)|16==(0|g))break a;break C}if(47!=(0|g)&&!Mr(g)&&63!=(0|(A=f[o+2152>>2]))&&33!=(0|A))break C;if((0|(A=f[o+2148>>2]))>780){f[33691]=f[o+2156>>2],a[0|(A=A+189424|0)]=32,a[A+1|0]=0,f[33285]=f[o+2152>>2],r=16384;break A}for(M=f[33285],k=0,g=f[o+2152>>2];f[o+2156>>2]=g,C=0,M||(A=f[33283],C=f[A>>2]==f[A+4>>2],g=f[o+2156>>2]),!(62==(0|g)|C|k>>>0>499);)f[(o+144|0)+(k<<2)>>2]=g,k=k+1|0,(g=f[33285])?(M=0,f[33285]=0):(f[33284]=f[33284]+1,A=f[33283],g=0|HC[f[A+8>>2]](A),M=f[33285]);f[(C=o+144|0)+(k<<2)>>2]=0,f[o+2152>>2]=32,y=o+2148|0,P=f[32525],g=0,V=N=V-560|0;I:if(wr(C,84333,3)&&wr(C,84477,4)){for(A=(C+(Rr(C)<<2)|0)-4|0,47==(0|(Z=f[A>>2]))&&(f[A>>2]=32);;){if(A=f[C+(g<<2)>>2]){if(er(A))A=g;else if(a[(N+512|0)+g|0]=ar(A<<24>>24),A=39,39!=(0|(g=g+1|0)))continue}else A=g;break}if(a[(N+512|0)+A|0]=0,47!=i[N+512|0]){if(16!=(0|(M=Hr(130480,N+512|0)))&&(g=f[y>>2],f[y>>2]=g+1,a[g+189424|0]=32),47==(0|Z)&&(g=0,!(502241>>>M&1)))break I}else 16!=(0|(g=Hr(130480,N+512|1)))&&(k=f[y>>2],f[y>>2]=k+1,a[k+189424|0]=32),M=g+32|0;k=C+(A<<2)|0,A=f[33708],u=G(A,76)+133076|0,g=262174;f:{i:{b:{s:switch(M-1|0){case 33:t:if(!((0|A)<=1)){for(;;){if(2==f[G(g=A-1|0,76)+133152>>2])break t;if(f[33708]=g,C=A>>>0>2,A=g,!C)break}A=1}g=MA(k,34,A);break I;case 32:t:if(!((0|A)<=1)){for(;;){if(1==f[G(g=A-1|0,76)+133152>>2])break t;if(f[33708]=g,C=A>>>0>2,A=g,!C)break}A=1}g=MA(k,33,A)+524328|0;break I;case 9:(0|(g=f[33709]))<=18&&(f[33709]=g+1),f[(A=(u=g<<6)+134912|0)>>2]=10,f[A+4>>2]=-1,f[A+8>>2]=-1,f[A+52>>2]=-1,f[A+56>>2]=-1,f[A+44>>2]=-1,f[A+48>>2]=-1,f[A+36>>2]=-1,f[A+40>>2]=-1,f[A+28>>2]=-1,f[A+32>>2]=-1,f[A+20>>2]=-1,f[A+24>>2]=-1,f[A+12>>2]=-1,f[A+16>>2]=-1,f[A+60>>2]=-1,A=ze(k,88301),C=ze(k,88390),jg(A,88479)?jg(A,88528)||(f[28+(134912+(g<<6)|0)>>2]=cg(C,130224)):f[24+(u+134912|0)>>2]=cg(C,130192),XA(y,f[33709]);break b;case 2:for((0|(A=f[33709]))<=18&&(f[33709]=A+1),f[(A=(QA=A<<6)+134912|0)>>2]=3,f[A+4>>2]=-1,f[A+8>>2]=-1,f[A+52>>2]=-1,f[A+56>>2]=-1,f[A+44>>2]=-1,f[A+48>>2]=-1,f[A+36>>2]=-1,f[A+40>>2]=-1,f[A+28>>2]=-1,f[A+32>>2]=-1,f[A+20>>2]=-1,f[A+24>>2]=-1,f[A+12>>2]=-1,f[A+16>>2]=-1,f[A+60>>2]=-1,J=1;;){if(C=ze(k,f[(R=J<<2)+130448>>2])){Z=0,A=f[R+131072>>2];t:if(M=f[A>>2])for(;;){for(g=0;u=a[g+M|0],(P=f[(g<<2)+C>>2])&&(g=g+1|0,(0|u)==(0|P)););n:switch(P-34|0){case 0:case 5:if(!u)break t;break;default:break n}if(!(M=f[A+((Z=Z+1|0)<<3)>>2]))break}DA=R+(QA+134912|0)|0;t:if((0|(A=f[4+(A+(Z<<3)|0)>>2]))>=0)A=(0|G(A,f[4+(R+134912|0)>>2]))/100|0;else{for(;C=(A=C)+4|0,er(f[A>>2]););for(GA=43==f[A>>2],C=((wA=45==f[(A=A+(GA<<2)|0)>>2])<<2)+A|0,Z=N+96|0,V=M=(V=u=V-16|0)-224|0,ue(M+16|0,0,144),A=M+160|4,f[M+24>>2]=A,f[M+60>>2]=A,f[M+92>>2]=-1,f[M+64>>2]=60,f[M+20>>2]=A,f[M+48>>2]=19,g=C;g=(A=g)+4|0,P=(P=f[A>>2])?Pr(124960,P):0;);f[M+100>>2]=A,Tg(g=M+16|0,0,0),AA(M,g,1,1),g=f[M+8>>2],uA=f[M+12>>2],P=f[M>>2],lA=f[M+4>>2],Z&&(xA=Z,Z=f[M+136>>2]+(f[M+20>>2]-f[M+60>>2]|0)|0,f[xA>>2]=Z?A+(Z<<2)|0:C),f[(A=u)+8>>2]=g,f[A+12>>2]=uA,f[A>>2]=P,f[A+4>>2]=lA,V=M+224|0,H=ge(f[A>>2],f[A+4>>2],f[A+8>>2],f[A+12>>2]),V=A+16|0;n:{g=100;k:if((0|(A=C))!=(0|(C=f[N+96>>2]))){g=wA?-1:GA;o:{if(115!=(0|(A=f[C>>2]))){if(37!=(0|A))break o;if(E(H=g?+(0|g)*H+100:H)<2147483648){g=~~H;break k}g=-2147483648;break k}if(116==f[C+4>>2]){n(+(L=H*+(0|g)/12)),A=0|b(1),b(0);B:{if((g=(A=A>>>20&2047)-969|0)>>>0>=63){if(H=L+1,(0|g)<0)break B;if(n(+L),g=0|b(1),C=0|b(0),!(A>>>0<1033)){if(H=0,!C&-1048576==(0|g))break B;if(H=L+1,A>>>0>=2047)break B;if((0|g)>0|(0|g)>=0){Q[(A=V-16|0)+8>>3]=3105036184601418e216,H=3105036184601418e216*Q[A+8>>3];break B}if(!(g>>>0<3230714880)){Q[(A=V-16|0)+8>>3]=12882297539194267e-247,H=12882297539194267e-247*Q[A+8>>3];break B}}u=A,A=!(C<<1)&-2129002496==(0|(A=g<<1|C>>>31))|A>>>0<2165964800?u:0}sA=(L=(H=L-((gA=(H=Q[14416])+L)-H))*H)*L*(H*Q[14421]+Q[14420]),L*=H*Q[14419]+Q[14418],H*=Q[14417],n(+gA),b(1),u=0|b(0),H=sA+(L+(H+Q[(C=u<<4&2032)+115376>>3])),P=f[(C=C+115384|0)>>2],M=f[C+4>>2],C=(g=P)+(P=0)|0,g=(u<<13)+M|0,g=C>>>0

>>0?g+1|0:g,A?(s(0,0|C),s(1,0|g),H=(L=+t())*H+L):-2147483648&u?(s(0,0|C),s(1,g+1071644672|0),(H=(gA=(L=+t())*H)+L)<1&&(f[(A=V-16|0)+8>>2]=0,f[A+12>>2]=1048576,Q[A+8>>3]=22250738585072014e-324*Q[A+8>>3],H=0==(H=(sA=H+1)+(gA+(L-H)+(H+(1-sA)))+-1)?0:H),H*=22250738585072014e-324):(s(0,0|C),s(1,g+-1048576|0),H=(L=+t())*H+L,H+=H)}if(E(H*=100)<2147483648){g=~~H;break k}g=-2147483648;break k}}if(1!=(0|J))break n;if(g)g=(A=E(H=H*+(0|g)*100)<2147483648?~~H:-2147483648)+100|0;else{if(E(H*=100)<2147483648){g=~~H;break k}g=-2147483648}}A=(0|G(g,f[R+134848>>2]))/100|0;break t}A=E(H)<2147483648?~~H:-2147483648,g&&(A=f[R+134848>>2]+G(A,g)|0)}f[DA+4>>2]=A}if(5==(0|(J=J+1|0)))break}XA(y,f[33709]);break b;case 11:(0|(g=f[33709]))<=18&&(f[33709]=g+1),f[(A=134912+(g<<6)|0)>>2]=12,f[A+4>>2]=-1,f[A+8>>2]=-1,f[A+52>>2]=-1,f[A+56>>2]=-1,f[A+44>>2]=-1,f[A+48>>2]=-1,f[A+36>>2]=-1,f[A+40>>2]=-1,f[A+28>>2]=-1,f[A+32>>2]=-1,f[A+20>>2]=-1,f[A+24>>2]=-1,f[A+12>>2]=-1,f[A+16>>2]=-1,f[A+60>>2]=-1,A=(A=ze(k,88658))?cg(A,130400):3,C=134912+(g<<6)|0,1!=f[f[47192]+148>>2]?(f[52+(134912+(g<<6)|0)>>2]=A,A=i[A+102776|0]):(f[20+(134912+(g<<6)|0)>>2]=i[A+102764|0],A=i[A+102770|0]),f[C+12>>2]=A,XA(y,f[33709]);break b;case 34:case 41:case 43:if(!((0|(A=f[33709]))<=0)){if(k=M-32|0,M=0,C=0,g=0,A>>>0>=4)for(u=-4&A,Z=0;J=2|g,R=1|g,C=(0|k)==f[134912+((P=3|g)<<6)>>2]?P:(0|k)==f[134912+(J<<6)>>2]?J:(0|k)==f[134912+(R<<6)>>2]?R:(0|k)==f[134912+(g<<6)>>2]?g:C,g=g+4|0,(0|u)!=(0|(Z=Z+4|0)););if(u=3&A)for(;C=(0|k)==f[134912+(g<<6)>>2]?g:C,g=g+1|0,(0|u)!=(0|(M=M+1|0)););(0|C)<=0||(f[33709]=C,A=C)}XA(y,A);break b;case 7:if(A=ze(k,88741),g=ze(k,88860),1!=(0|cg(A,130176)))break b;A=f[y>>2],f[y>>2]=A+1,a[A+189424|0]=91,A=f[y>>2],f[y>>2]=A+1,a[A+189424|0]=91,A=ng((A=f[y>>2])+189424|0,g,800-A|0)+f[y>>2]|0,f[y>>2]=A+1,a[A+189424|0]=93,A=f[y>>2],f[y>>2]=A+1,a[A+189424|0]=93;break b;case 35:36==f[33692]&&(a[f[y>>2]+189424|0]=0,(C=Hr(131104,g=(A=f[33707])+189424|0))&&(f[y>>2]=Fg(C,g)+A)),A=f[y>>2],f[y>>2]=A+1,a[A+189424|0]=1,A=f[y>>2],f[y>>2]=A+1,a[A+189424|0]=89,f[33692]=0;break b;case 8:if(!(A=ze(k,89299)))break b;a[134824]=1,g=f[y>>2],f[y>>2]=ng(g+189424|0,A,800-g|0)+f[y>>2];break b;case 13:a[134824]=1;break b;case 40:case 45:a[134824]=0;break b;case 4:if(!(A=ze(k,89360)))break b;if(ng(N+352|0,A,160),i[N+352|0]&&!Qr(199328,N+352|0)){a[134760]=1,a[199328]=0,g=16384;break I}if((0|(A=kr(N+352|0)))<0)break b;f[N+20>>2]=A,f[N+16>>2]=1,dg(A=N+352|0,89460,N+16|0),rg(f[y>>2]+189424|0,A),f[y>>2]=f[y>>2]+Lg(A);break b;case 10:(0|(g=f[33709]))<=18&&(f[33709]=g+1),f[(A=134912+(g<<6)|0)>>2]=11,f[A+4>>2]=-1,f[A+8>>2]=-1,f[A+52>>2]=-1,f[A+56>>2]=-1,f[A+44>>2]=-1,f[A+48>>2]=-1,f[A+36>>2]=-1,f[A+40>>2]=-1,f[A+28>>2]=-1,f[A+32>>2]=-1,f[A+20>>2]=-1,f[A+24>>2]=-1,f[A+12>>2]=-1,f[A+16>>2]=-1,f[A+60>>2]=-1;t:if(A=ze(k,89514)){if(ng(N+352|0,A,160),f[34441]){if((0|(A=kr(N+352|0)))<0)break t;if(0|HC[f[34441]](1,A+f[33282]|0,P))break t;f[N+68>>2]=A,f[N+64>>2]=1,dg(N+352|0,89658,N- -64|0)}else{if(!P|47==i[N+352|0]?A=bg(N+352|0):(f[N+48>>2]=P,f[N+52>>2]=N+352,dg(A=N+96|0,89564,N+48|0),A=bg(A)),(0|A)<0)break t;f[N+36>>2]=A,f[N+32>>2]=1,dg(N+352|0,89623,N+32|0)}A=N+352|0,rg(f[y>>2]+189424|0,A),f[y>>2]=f[y>>2]+Lg(A),f[4+(134912+(g<<6)|0)>>2]=1}if(XA(y,f[33709]),47==(0|Z)){Re(11,y),g=16384;break I}a[134772]=1,g=16384;break I;case 42:Re(43,y),a[134772]=0,g=16384;break I;case 12:t:{if(A=ze(k,89714)){if(g=16384,(u=(0|(A=cg(A,130336)))<0?2:A)>>>0<=2&&(A=f[y>>2],f[N+84>>2]=u,f[N+80>>2]=1,dg(A+189424|0,89770,N+80|0),f[y>>2]=f[y>>2]+3,g=0),A=f[102784+(u<<2)>>2],!(C=ze(k,89907)))break t;break i}if(g=16384,C=ze(k,89907))break i;A=21;break f}if(u>>>0<3)break b;break f;case 0:(A=ze(k,89965))&&(ng(g=N+352|0,A,160),kr(g)),g=MA(k,1,f[33708])?147456:0;break I;case 1:g=MA(k,2,A)?147456:0;break I;case 5:g=0,6==f[u>>2]&&(g=MA(k,38,A),A=f[33708]),g=524358+(MA(k,6,A)|g)|0;break I;case 6:g=0,6==(0|(C=f[u>>2]))&&(g=MA(k,38,A),C=f[u>>2]),7==(0|C)&&(g=MA(k,39,f[33708])|g),g=524358+(g|MA(k,7,f[33708]))|0;break I;case 37:if(g=524328,6!=f[u>>2])break I;g=MA(k,38,A)+524328|0;break I;case 38:if(g=524358,6!=(-2&f[u>>2]))break I;g=MA(k,39,A)+524358|0;break I;case 14:case 46:break I;case 3:break s;default:break b}A=ze(k,88893),g=ze(k,88992),C=ze(k,89153),k=cg(A,130272),g=cg(g,130320),A=Ar(C,0),f[N>>2]=1,C=(0|A)<2?193:A- -64|0,g=64==(0|(A=1==(0|g)?19:k))?C:A,f[N+4>>2]=g,dg(A=N+352|0,89230,N),rg(f[y>>2]+189424|0,A),A=f[y>>2]+Lg(A)|0,f[y>>2]=A,f[33707]=A,f[33692]=g}g=0;break I}A=Ar(C,1),C=f[33722],xr(1,f[33713]),(0|(A=(0|(C=(0|G(A,C))/100<<8))/(0|G(f[36429],10))|0))<=199&&(A=(0|C)/(0|G(f[36428],10))|0),g=g||16384}C=A>>>5|0,k=A,g=((A=(0|A)>4095)?C>>>0>=4095?4095:C:k)+(A?8388608|g:g)|0}if(V=N+560|0,g){if(A=f[o+2148>>2]+189424|0,a[0|A]=32,a[A+1|0]=0,!(131072&g)){r=g;break A}rg(189360,134784),r=g;break A}f[o+2156>>2]=32;I:{f:{if(!(g=f[33285])){if(u=0,A=f[33283],f[A>>2]==f[A+4>>2])continue;if(!(g=f[33285]))break f}f[33285]=0;break I}f[33284]=f[33284]+1,A=f[33283],g=0|HC[f[A+8>>2]](A)}f[o+2152>>2]=g,u=0;continue}f[o+2156>>2]=A+57344}if(i[134824])continue;if(C=f[o+2156>>2],!(10!=(0|(A=f[o+2152>>2]))|-1!=f[47268])){16384==(0|(r=SA(C)))?(A=f[o+2148>>2],I[O+(A<<1)>>1]=f[33284]-f[47353],f[eA>>2]=A,r=524328,A=Fg(f[o+2156>>2],A+189424|0)+f[o+2148>>2]|0):A=f[o+2148>>2],a[0|(A=A+189424|0)]=32,a[A+1|0]=0;break A}C:if(1==(0|C)){if(66!=(0|A)){if(86!=(0|A))break C;for(A=f[o+2148>>2],f[o+2148>>2]=A+1,a[A+189424|0]=0;;){a:{I:{f:{if(!(g=f[33285])){if(A=f[33283],f[A>>2]==f[A+4>>2])break a;if(!(g=f[33285]))break f}f[33285]=0;break I}f[33284]=f[33284]+1,A=f[33283],g=0|HC[f[A+8>>2]](A)}if(f[o+2156>>2]=g,!(er(g)||(0|(A=f[o+2148>>2]))>=799)){f[o+2148>>2]=A+1,a[A+189424|0]=f[o+2156>>2];continue}}break}a[f[o+2148>>2]+189424|0]=0,r=147456;break A}g=f[o+2148>>2],a[0|(A=g+189424|0)]=32,a[A+1|0]=32,a[A+2|0]=32,a[A+3|0]=0,f[o+2148>>2]=g+3;a:{I:{f:{i:{b:{s:{if(!(g=f[33285])){if(A=f[33283],f[A>>2]==f[A+4>>2])break i;if(!(g=f[33285]))break s}f[33285]=0;break b}f[33284]=f[33284]+1,A=f[33283],g=0|HC[f[A+8>>2]](A)}if(f[o+2152>>2]=g,C=0,48!=(0|g))break f;break I}g=f[o+2152>>2]}if(f[47208]=0,f[47201]=1,49==(0|g))break a;for(C=f[33285],k=0;;){f:{if(!C){if(A=f[33283],f[A>>2]==f[A+4>>2])break f;g=f[o+2152>>2]}if(!(er(g)|k>>>0>58)){f[188832+(k<<2)>>2]=f[o+2152>>2],(g=f[33285])?(f[33285]=0,C=0):(f[33284]=f[33284]+1,A=f[33283],g=0|HC[f[A+8>>2]](A),C=f[33285]),k=k+1|0,f[o+2152>>2]=g,A=f[o+2148>>2],f[o+2148>>2]=A+1,a[A+189424|0]=32;continue}}break}C=2,f[188832+(k<<2)>>2]=0}f[47201]=C}a:{if(!(g=f[33285])){if(A=f[33283],f[A>>2]==f[A+4>>2])continue;if(!(g=f[33285]))break a}f[33285]=0,f[o+2152>>2]=g;continue}f[33284]=f[33284]+1,A=f[33283],f[o+2152>>2]=HC[f[A+8>>2]](A);continue}j=j+1|0,g=0,A=f[v+340>>2];C:if(k=B[A>>1]){for(;;){if((65535&k)!=(0|C)){if(k=B[A+((g=g+2|0)<<1)>>1])continue;break C}break}a:switch(0|(A=B[A+(g<<1|2)>>1])){case 1:continue;case 0:break C;default:break a}f[o+2156>>2]=A,C=A}mr(C)?(p=1,g=f[o+2156>>2]):W?(f[33285]=f[o+2152>>2],g=1328,f[o+2156>>2]=1328,f[o+2152>>2]=32,W=0):3851!=(0|(g=f[o+2156>>2]))?(W=0,3405!=(0|g)|8205!=f[o+2152>>2]||(g=3406,f[o+2156>>2]=3406)):(g=32,f[o+2156>>2]=32,W=0);C:if(nr(g)){if(f[v+8216>>2]=f[v+8216>>2]+1,f[33692]|2!=f[47200])break C;if(nr(F))break C;if(f[o+2544>>2]=0,f[o+2548>>2]=0,f[o+2304>>2]=84731,!TA(v,o+2304|0,o+2160|0,o+2544|0,0,0))break C;if(fA(v,A=o+2160|0,o+2544|0,-1,0),Ye(g=A,A=o+2336|0),f[o+80>>2]=A,dg(g=o+2240|0,85451,o+80|0),A=f[o+2148>>2],(0|(g=Lg(g)+A|0))>=800)break C;rg(A+189424|0,o+2240|0),f[o+2148>>2]=g}else Mr(f[o+2156>>2])&&(f[v+8220>>2]=f[v+8220>>2]+1);if(g=f[o+2152>>2],A=f[o+2156>>2],f[47204])if((0|u)>0)u=u-1|0;else{if(!(91!=(0|A)|91!=(0|g))){C=0,u=-1;break e}u=93==(0|A)&&93==(0|g)?2:u}if(10==(0|A)){for(C=f[33285],k=0;;){C:{if(!C){if(A=f[33283],f[A>>2]==f[A+4>>2])break C;g=f[o+2152>>2]}if(er(g)){k=(10==f[o+2152>>2])+k|0,(g=f[33285])?(f[33285]=0,C=0):(f[33284]=f[33284]+1,A=f[33283],g=0|HC[f[A+8>>2]](A),C=f[33285]),f[o+2152>>2]=g;continue}}break}if((0|k)>0){r&&ue(A=q+189424|0,32,Te(o+2336|0,A)),A=f[o+2148>>2]+189424|0,a[0|A]=32,a[A+1|0]=0,f[33285]=f[o+2152>>2],r=f[47203]?524358:G((0|k)>=3?3:k,30)+524328|0;break A}if(A=f[47268]<(0|j),j=0,!A){A=f[o+2148>>2]+189424|0,a[0|A]=32,a[A+1|0]=0,f[33285]=f[o+2152>>2],r=262174;break A}}if(C=0,f[33692]|u)break e;if(A=0,!r)break g;if(er(f[o+2156>>2])){A=r;break g}if(kg(f[o+2156>>2])&&ur(f[o+2156>>2]))break g;f[33691]=f[o+2156>>2],a[0|(A=q+189424|0)]=32,a[A+1|0]=0,f[33285]=f[o+2152>>2];break A}W&&(f[o+2148>>2]=Fg(1328,f[o+2148>>2]+189424|0)+f[o+2148>>2]),r&&ue(A=q+189424|0,32,Te(o+2336|0,A)),A=f[o+2148>>2]+189424|0,a[0|A]=32,a[A+1|0]=0}r=589864;break A}if(!(46!=(0|(g=f[o+2156>>2]))|46!=f[o+2152>>2])){g:{r:{C:{if(!(g=f[33285])){if(g=f[33283],f[g>>2]==f[g+4>>2])break g;if(!(g=f[33285]))break C}f[33285]=0;break r}f[33284]=f[33284]+1,g=f[33283],g=0|HC[f[g+8>>2]](g)}if(w=g,46==(0|g))for(f[o+2152>>2]=32,f[o+2156>>2]=8230,w=f[33285];;){r:{C:{if(!w){if(g=f[33283],f[g>>2]==f[g+4>>2]){w=46;break g}if(!(w=f[33285]))break C}f[33285]=0,g=0;break r}f[33284]=f[33284]+1,g=f[33283],w=0|HC[f[g+8>>2]](g),g=f[33285]}if(46!=(0|w))break g;f[o+2152>>2]=32,f[o+2156>>2]=8230,w=g}}8230!=(0|(g=f[o+2156>>2]))?f[33285]=w:(f[o+2152>>2]=w,g=8230)}if(Z=0,16384!=(0|(M=SA(g)))){g:if(536621&M)for(g=f[33285];;){if(!g&&(g=f[33283],f[g>>2]==f[g+4>>2]))break g;if(!(536621&SA(f[o+2152>>2])))break g;(w=f[33285])?(f[33285]=0,g=0):(f[33284]=f[33284]+1,g=f[33283],w=0|HC[f[g+8>>2]](g),g=f[33285]),f[o+2152>>2]=w}if(1048576&M){f[D+780>>2]=M>>>12&15,W=1,u=0,r=A;continue}(er(f[o+2152>>2])|32768&M||CC(f[o+2152>>2])||63==(0|(g=f[o+2152>>2]))||(k=0,f[33285]||(g=f[33283],k=f[g>>2]==f[g+4>>2],g=f[o+2152>>2]),k||1==(0|g)))&&(Z=1)}if(57404==(0|(g=f[o+2156>>2]))&&(f[o+2156>>2]=60,g=60),f[47201]){r=0;g:if(!(1<<(k=Je(g))&1879048255&&k>>>0<=30)){if(!((k=Je(g))>>>0>27)){if(116672&(u=1<>2]))){f[v+288>>2]=0,C=f[o+2156>>2],a[o+2336|0]=0,u=f[o+2152>>2],g=0;g:{r:if(!((0|(r=f[34064]))<=0))for(;;){if((0|C)==f[136272+(g<<4)>>2]){if(f[136276+(g<<4)>>2])break g;if(yA(0,g))break r;break g}if((0|r)==(0|(g=g+1|0)))break}g=-1}g:if((0|g)>=0)f[o>>2]=g,dg(o+2336|0,86007,o),f[33285]=u;else if((k=!Z)|46!=(0|C)|46==(0|u)||(f[o+2600>>2]=0,f[o+2604>>2]=0,f[o+2540>>2]=86036,!TA(v,o+2540|0,o+2544|0,o+2600|0,0,0))?g=WA(o+2240|0,v,C,0):(fA(v,g=o+2544|0,o+2600|0,-1,0),Ye(r=g,g=o+2160|0),f[o+64>>2]=g,dg(g=o+2304|0,85451,o- -64|0)),r=g,k|!f[o+2148>>2]|2&i[v+76|0]){for(g=f[33285],k=1;;){r:{C:{if(!g){if(g=f[33283],f[g>>2]==f[g+4>>2]|60==(0|C)|(0|C)!=(0|u))break r;if(k=k+1|0,u=f[33285])break C;f[33284]=f[33284]+1,g=f[33283],u=0|HC[f[g+8>>2]](g),g=f[33285];continue}if(60==(0|C)|(0|C)!=(0|u))break r;u=g,k=k+1|0}g=0,f[33285]=0;continue}break}if(f[o+2152>>2]=u,Z&&(f[33285]=u),1==(0|k)){f[o+16>>2]=r,dg(o+2336|0,86219,o+16|0);break g}if((0|k)<=3){if(a[o+2336|0]=0,(0|(g=f[50786]))<=299&&(f[o+2336>>2]=i[86728]|i[86729]<<8|i[86730]<<16|i[86731]<<24,I[o+2340>>1]=i[86732]|i[86733]<<8),(0|k)>0){for(;f[o+32>>2]=r,dg(g=o+2160|0,86219,o+32|0),u=k>>>0>1,mC(o+2336|0,g),k=k-1|0,u;);g=f[50786]}if((0|g)>299)break g;f[o+2160>>2]=i[86857]|i[86858]<<8|i[86859]<<16|i[86860]<<24,g=i[86860]|i[86861]<<8|i[86862]<<16|i[86863]<<24,a[o+2163|0]=g,a[o+2164|0]=g>>>8,a[o+2165|0]=g>>>16,a[o+2166|0]=g>>>24,mC(o+2336|0,o+2160|0);break g}f[o+56>>2]=r,f[o+52>>2]=k,f[o+48>>2]=r,dg(o+2336|0,86932,o+48|0)}else f[33691]=C,f[33285]=u,I[o+2336>>1]=32;if(k=Lg(r=o+2336|0),rg((g=f[o+2148>>2])+189424|0,r),f[o+2148>>2]=g+k,Z){if(45==(0|C)){r=16384;break A}if(r=SA(C),!(2&i[v+76|0]|(0|g)<=0)){r=266270==(-32769&r)?262148:4096==(28672&r)?266244:262148;break A}if(!(524288&r)){r=4096==(28672&r)?266244:262148;break A}if((0|r)>=0)break A}C=f[o+2156>>2]}}if(C|!(2097152&M)||(g=rg(f[o+2148>>2]+189424|0,WA(o+2336|0,v,f[o+2156>>2],1)),i[0|g]?(f[o+2148>>2]=f[o+2148>>2]+Lg(g),M&=-28673,C=f[o+2156>>2]):C=0),u=0,Z){g:if(er(w=f[o+2152>>2]))for(k=f[33285],g=0;;){if(!k&&(r=f[33283],f[r>>2]==f[r+4>>2]))break g;if(!er(w))break g;g=(10==(0|w))+g|0,(w=f[33285])?(f[33285]=0,k=0):(f[33284]=f[33284]+1,r=f[33283],w=0|HC[f[r+8>>2]](r),k=f[33285])}else g=0;r=46==(0|(u=f[o+2156>>2]))&&(0|g)<2?4194304|M:M;g:{if(!g){k=1,44!=(0|u)|46!=(0|F)|26741!=f[v+212>>2]|P-48>>>0>=10||w-48>>>0>=10&&!ur(w)||(f[o+2156>>2]=1367,k=0),46!=(0|(M=f[o+2156>>2]))|39!=(0|w)||(u=k,M=f[33283],(0|(N=f[M>>2]))==f[M+4>>2]?k=0:(k=0|HC[f[M+8>>2]](M),f[M>>2]=N),M=f[o+2156>>2],k=115!=(0|k)&u);r:if(46!=(0|M))k&=p;else{C:if(1&a[v+106|0]){if(!(F-48>>>0<10)){if((u=F-73|0)>>>0>15|!(1<>>0<=15||er(P)))break C}k=F-48>>>0>=10?0:!ur(w)&45!=(0|w)&k}if(ur(w)&&(k=0!=i[v+208|0]&k),p){M=f[o+2156>>2];break r}M=32,f[o+2156>>2]=32,k=0}if(!(!k|46!=(0|M)|!f[47203]|60!=(0|w))){q=f[o+2148>>2],A=r;break g}if(!k)break g}if(A=f[o+2148>>2]+189424|0,a[0|A]=32,a[A+1|0]=0,f[33285]=w,F-48>>>0<10&&(r=kg(w)?r:-4194305&r),(0|g)<2)break A;r=536621==(0|r)?536656:532520==(0|r)?532555:524358;break A}(f[33285]||(u=0,g=f[33283],f[g>>2]!=f[g+4>>2]))&&(u=0,er(f[o+2152>>2])&&(f[33285]=w))}r=A}if(1!=f[33712]){if((0|C)!=(0|(g=f[o+2156>>2])))k=f[o+2148>>2],57404==(0|g)&&(g=60,f[o+2156>>2]=60);else{e:{if(CC(C))g=57384;else{if(g=45,45==f[o+2156>>2])break e;g=32}f[o+2156>>2]=g}k=f[o+2148>>2]}f[o+2148>>2]=Fg(g,k+189424|0)+f[o+2148>>2],er(f[o+2156>>2])||CC(f[o+2156>>2])||(A=f[o+2148>>2],I[O+(A<<1)>>1]=f[33284]-f[47353],(0|A)<=(k+1|0)||ue(EA+(k<<1)|0,255,A+~k<<1)),g=f[o+2148>>2],f[eA>>2]=g;e:{g:{if((0|g)>725){if(!kg(f[o+2156>>2]))break g;g=f[o+2148>>2]}if((0|g)<796)continue;break e}if(g=f[o+2148>>2],!(f[o+2156>>2]-48>>>0>=10)&&(0|g)<796)continue}break}}a[0|(A=g+189424|0)]=32,a[A+1|0]=0,f[33285]=f[o+2152>>2],r=16384}V=o+2608|0,W=r,iA&&(A=f[D+780>>2],f[iA>>2]=A||W>>>12&7),A=(D+5184|0)+(f[D+6800>>2]<<1)|0,I[A+6>>1]=0,I[A+2>>1]=0,I[A+4>>1]=32767,M=G(4095&W,8388608&W?320:10),r=189424;A:{e:{g:if(A=i[189424]){for(;;){if(!!(255&(A=A<<24>>24))&A>>>0<33){if(A=i[0|(r=r+1|0)])continue;break g}break}if(i[0|r])break e}M=(0|(g=M-(A=f[47566])|0))>0?g:0,f[47566]=M+A,W=i[190268]?524288|W:W,f[v+8240>>2]=W;break A}f[47566]=M,A=i[190268],f[v+8240>>2]=W,A&&(X=1,f[47568]=f[47568]+1,(0|(A=f[47569]))<=0||(A=A-1|0,f[47569]=A,A||(a[190280]=0)))}f[49572]=1,f[47572]=655360,f[47573]=0,f[v+8184>>2]=0,f[v+8188>>2]=0,A=0,f[v+288>>2]=0,f[(g=v- -8192|0)>>2]=0,f[g+4>>2]=0,f[v+8200>>2]=0,f[v+8224>>2]=0,f[v+8228>>2]=0,f[(g=v+8232|0)>>2]=0,f[g+4>>2]=0,a[D+786|0]=32,I[D+784>>1]=8192,f[D+6812>>2]=32,I[D+1588>>1]=3,f[D+1584>>2]=0,r=0;A:if(!((0|(g=f[D+6800>>2]))<=0)){for(;;){if(I[(D+5184|0)+(r<<1)>>1]>0)break A;if((0|g)==(0|(r=r+1|0)))break}r=g}if(g=B[(D+5184|0)+(r<<1)>>1],I[D+1592>>1]=g,g)for(;A=!!(65535&~g)+A|0,g=B[(D+5184|0)+((r=r+1|0)<<1)>>1];);for(a[D+1594|0]=A,k=3,O=1,r=0;;){P=f[D+6808>>2],Xe(D+6808|0,(y=(D+784|0)+k|0)-1|0),!i[v+170|0]|f[D+6808>>2]-48>>>0>=10||kg(P)&&(f[D+6808>>2]=97),Y?f[D+6812>>2]=Y:x&&Xe(D+6812|0,x+189423|0),g=x;A:{e:if(r||(g=Te(D+6816|0,x+189424|0)+x|0,r=f[D+6816>>2])){if(p=Te(D+6804|0,A=g+189424|0),1==(0|r)){if(C=g-1|0,Y=32,F=0,32!=f[D+6812>>2]){g=C,A=32;break e}x=0,r=g;g:{r:switch(i[0|A]-43|0){case 0:r=g+1|0,x=64;break g;case 2:break r;default:break g}r=g+1|0,x=96}if(a[0|(A=r+189424|0)]-48>>>0>=10)w=r+1|0,p=-1;else{for(p=Dg(A);r=(A=r)+1|0,a[A+189424|0]-48>>>0<10;);w=r,r=A}if((0|(Y=f[47350]))>247)A=0;else if(A=0,!((0|(r=a[r+189424|0]))<0)&&(r=qe(84868,255&r,14))){g=(A=r-84868|0)+1|0,-1==(0|p)&&(p=f[105536+(g<<2)>>2],x=0);g:{r:switch(A-8|0){case 0:f[49574]=0,f[49573]=p;break g;case 4:break r;default:break g}(0|p)>=3?a[199304]=1:a[199304]=0}A=1,f[47350]=Y+1,f[198304+(Y<<2)>>2]=(g+x|0)+(p<<8),g=w}Y=f[D+6812>>2],ue(C+189424|0,32,g-C|0),rA=A+rA|0,r=0;break A}Y=0,32==(0|r)|36!=f[49573]?(F=0,A=r):(32!=f[D+6812>>2]|32!=f[D+6804>>2]||(f[49573]=20),F=0,A=Sr(r,v))}else f[D+6804>>2]=32,F=1,Y=0,p=0,A=32;e:if(U){if(U=1,O=8,r=0,93!=(0|A)|93!=f[D+6804>>2])break e;g=g+1|0,A=32,U=0}else if(64!=(240&(r=f[49573])))if(U=0,16&r)r=0;else{g:{r:{C:{a:{if(!(8216!=(0|(w=8242==(0|A)||8217==(0|A)||146==(0|A)||180==(0|A)?39:A))&63!=(0|w)))if(kg(f[D+6808>>2])){if(w=A,kg(f[D+6804>>2])){w=39;break a}}else w=A;I:{if(1367!=(0|w)){if(1328==(0|w)){m|=1024,w=32;break a}if((A=w-44032|0)>>>0>11183)break a;if(C=((r=((u=65535&A)>>>0)/28|0)>>>0)%21|0,A=A-G(r,28)&65535,w-50500>>>0>587)break I;r=A?A+4519|0:0,C=C+4449|0;break r}m|=131072,q=f[D+6804>>2],A=f[D+6812>>2],w=32;break C}r=50500+(A+G(C,28)|0)|0,C=(u>>>0)/588|4352;break r}if(q=f[D+6804>>2],A=f[D+6812>>2],!((r=w-12592|0)>>>0>51)){C=4352|i[r+103296|0],r=0;break r}}o=g+189424|0;C:if(!(28268!=(0|(r=f[v+212>>2]))&24934!=(0|r)|39!=(0|w))&&!Mr(A)&&(Te(D+6820|0,o+1|0),Vr(f[D+6820>>2]))){C=601,r=0;a:switch(q-110|0){case 6:break r;case 0:break a;default:break C}if(24934!=f[v+212>>2])break r;a[0|o]=32;break r}if(f[D+6824>>2]=32,(0|(A=f[49897]))>0)f[49897]=A-1,r=0;else{if(!w){r=0,C=0;break g}C:{a:{I:{f:if((Z=f[v+180>>2])&&(u=w,(N=nr(w))&&(u=Sr(w,v)),!fC(Z)))for(;;){f[D+16>>2]=0,f[D+624>>2]=u,A=Te(D+16|0,Z)+Z|0;i:if(f[D+624>>2]==f[D+16>>2]){if(i[0|A]){for(C=1,J=0,r=o;R=Te(D+16|0,A),j=Te(D+624|0,r),eA=Sr(f[D+624>>2],v),f[D+624>>2]=eA,r=r+j|0,J=(j=(0|eA)==f[D+16>>2])+J|0,C&=j,i[0|(A=A+R|0)];);if(!C)break i;f[49897]=J}if(!(A=A+1|0))break f;if(8&i[188788]&&(f[D>>2]=Z,f[D+4>>2]=A,eC(f[47195],85187,D)),A=Te(D+6828|0,A)+A|0,i[0|A])break I;r=0;break a}for(;r=A,A=A+1|0,i[0|r];);for(;i[0|(r=(A=r)+1|0)];);if(fC(Z=A+2|0))break}r=0,C=w;break C}Te(D+6824|0,A),N&&nr(q)&&(f[D+6824>>2]=Ir(f[D+6824>>2])),r=f[D+6824>>2]}C=f[D+6828>>2],m|=2097152,N&&(C=Ir(C))}if(8!=(0|C))break r}C=g;break A}r?f[D+6804>>2]=r:r=0}kg(C)||Vr(C)||Pr(f[v+336>>2],C)||!kg(f[D+6808>>2])|!(!i[v+170|0]|C-48>>>0>=10)&f[D+6804>>2]-48>>>0>=10||(C=32,z=1);g:{r:{C:{a:{I:{if(f[D+6808>>2]-48>>>0<10){if(C-48>>>0<10){A=d;break I}if(1<<(A=C-32|0)&20481&&A>>>0<=14)break a;z=1}else{if(A=0,44!=f[D+6812>>2])break I;if(A=d,44!=(0|C))break I}C=32;break a}if(91==(0|C)){if(2==(0|(w=f[D+6804>>2])))break C;if(C=91,91==(0|w)&&f[47204])break C}d=A}if(kg(C)){a:{I:{f:{if(kg(f[D+6808>>2])){if(!i[v+171|0])break f;if(A=f[D+6808>>2],!((0|C)>12352)&&(0|A)<12353)break f}else A=f[D+6808>>2];if(_=Pr(f[v+336>>2],A)?_:0,32!=(0|(A=f[D+6808>>2]))&&!Pr(f[v+336>>2],A)){A=32,h=CC(f[D+6808>>2])?h:256|h;break I}m=nr(C)?2|m:m,32!=f[D+6808>>2]|a[y-2|0]-48>>>0>=10|f[D+6812>>2]-48>>>0<10||(a[(D+784|0)+k|0]=32,A=1588+(G(K,12)+D|0)|0,I[A>>1]=B[A>>1]+1,k=k+1|0)}if(A=32,32==(0|C))break a;if(_=_+1|0,(0|(w=f[v+600>>2]))<=0){A=C;break a}if(!((0|C)<=591&(0|(u=f[D+6808>>2]))>=(0|w))){if((0|C)<(0|w)){A=C;break a}if((0|_)<2){A=C;break a}if(!((0|u)<=591)){A=C;break a}}if(!kg(u)){A=C;break a}m|=16384,h|=128}z=1}if(kA=kA+1|0,nr(A)){if(w=Sr(A,v),f[v- -64>>2]){A=oA?w:712,r=oA?r:w,oA=1;break e}if(ur(f[D+6812>>2])){if(32==f[D+6808>>2]){A=w;break e}if(A=32,26465!=f[v+212>>2])break g;for(C=85240,u=(D+784|0)+k|0,p=0;;){if(o=Lg(C),32==i[0|(P=u-o|0)]&&!pg(P+1|0,C,o=o-1|0)){if((0|(C=a[C+o|0]))==(0|w)){A=w;break e}if(65==(0|C)&&zg(v,w)){A=w;break e}}if(C=f[131184+((p=p+1|0)<<2)>>2],11==(0|p))break}break g}if(A=32,32==(0|w))break e;if(!nr(f[D+6812>>2])){A=w;break e}if(!ur(f[D+6804>>2])){A=w;break e}if(Te(D+16|0,189424+(g+p|0)|0),!(28268!=f[v+212>>2]|2!=(0|_)|106!=(0|w)|73!=f[D+6812>>2])){A=w;break e}if(32==f[D+6808>>2]){A=w;break e}if(!kg(f[D+16>>2])){A=w;break e}h|=256,Y=32,z=1;break e}if(!O){O=0;break e}if((0|_)<3){O=0;break e}if(115!=(0|A)){O=0;break e}if(25966!=f[v+212>>2]){O=0;break e}if(32!=f[D+6804>>2]){O=0;break e}if(O|=4,A=32,39!=i[(C=k+D|0)+783|0])break e;a[C+783|0]=32;break e}A=32;a:{I:{f:{i:{b:switch(C-39|0){default:if(95==(0|C))break e;case 1:case 2:case 3:case 4:case 5:if(C-48>>>0>=10)break a;if(i[v+170|0]&&kg(f[D+6808>>2])&&!((w=f[D+6804>>2])-48>>>0<10|w-2406>>>0<10))break a;if(32==(0|(u=f[D+6808>>2])))break I;if(w=f[D+6808>>2],u-48>>>0<10)break f;if((0|(u=w))==(0|(w=f[v+128>>2])))break i;z=1;break e;case 6:if(!Vr(f[D+6812>>2])&&kg(f[D+6804>>2])){if(32!=f[D+6808>>2]){z=1;break e}if(m|=128,(0|K)<=0)break e;C=1572+(G(K,12)+D|0)|0,f[C>>2]=16384|f[C>>2];break e}if(C=f[D+6804>>2],!(32!=f[D+6812>>2]|32!=(0|C))){T=4;break e}if(45==(0|C)){g=g+1|0,T=4;break e}if(A=45,32!=f[D+6808>>2])break e;if(!kg(P))break e;if(kg(f[D+6812>>2]))break e;a[(D+784|0)+k|0]=32,C=1588+(G(K,12)+D|0)|0,I[C>>1]=B[C>>1]+1,k=k+1|0;break e;case 7:if(46==f[D+6808>>2]){z=1;break e}if(A=46,(0|K)<=0)break e;if(C=1572+(G(K,12)+D|0)|0,1&a[C+1|0])break e;if(!kg(f[D+6812>>2]))break e;f[C>>2]=65536|f[C>>2],A=(A=Vr(f[D+6804>>2]))||45==f[D+6804>>2]?32:46;break e;case 0:break b}b:{if(46!=(0|(w=f[D+6812>>2]))||(C=115,115!=f[D+6804>>2])){if(!mr(w))break b;C=f[D+6804>>2]}if(kg(C))break r}if(1&(C=f[v+88>>2])){if(kg(f[D+6804>>2]))break r;C=f[v+88>>2]}if(2&C&&kg(f[D+6812>>2]))break r;if(!(!Pr(f[v+332>>2],f[D+6812>>2])|32!=(0|P))){g=(32==f[D+6804>>2])+g|0;break r}if(w=115!=(0|(C=f[D+6808>>2]))|BA,BA=0,!(1&w))break e;BA=!!(0|Vr(C)),T=4;break e}if(44==(0|w)&d){z=1;break e}d=1;break a}if(32!=(0|w))break a}kg(P)&&(kg(f[D+6812>>2])||(a[(D+784|0)+k|0]=32,A=1588+(G(K,12)+D|0)|0,I[A>>1]=B[A>>1]+1,k=k+1|0))}A=C;break e}U=1,C=g+1|0,d=A;break A}A=39,BA=0;break e}z=1,Y=32}else{if(A-48>>>0<10){r=0,C=(0|(w=f[49574]+1|0))>(15&f[49573]),f[49574]=C?0:w,A=C?32:A,z|=C,U=0;break e}r=0,f[49574]=0,A=(C=f[D+6808>>2]-48>>>0<10)?32:A,z|=C,U=0}if(Vr(A)){if(32==f[D+6808>>2]){m|=262144,C=g;break A}if(C=f[D+6816>>2]-9>>>0<2,u=1&z){p=0;e:if(!((0|l)>(0|(A=g-1|0))))for(;;){if(!(w=I[(D+5184|0)+(A<<1)>>1]))break e;if(p=((0|w)>0)+p|0,!((0|l)<=(0|(A=A-1|0))))break}a[1594+(G(K,12)+D|0)|0]=p}if(h=C?262144|h:h,a[(D+784|0)+k|0]=32,A=k+1|0,!((0|K)>298||(C=(D+1584|0)+G(K,12)|0,(0|(w=B[C+4>>1]))>=(0|A)))){if((0|rA)<=0?l=f[C>>2]:(l=198300+(f[47350]<<2)|0,f[l>>2]=128|f[l>>2],rA=0,l=64|f[C>>2]),o=f[47352],a[C+6|0]=o,f[C>>2]=l|(kA?O:-2&O)|(i[199304]?2048:0)|m,(0|o)>0){for(;m=(l=D+784|0)+A|0,l=l+(A=A-1|0)|0,a[0|m]=i[0|l],(0|A)>(0|w););a[0|l]=32,I[C+4>>1]=w+1,A=k+2|0}w=(D+1584|0)+G(K=K+1|0,12)|0,f[w>>2]=0,I[w+4>>1]=A,k=g;e:if(!((0|(C=f[D+6800>>2]))<=(0|g))){for(;;){if(I[(D+5184|0)+(k<<1)>>1]>0)break e;if((0|C)==(0|(k=k+1|0)))break}k=C}if(p=B[(D+5184|0)+(k<<1)>>1],I[w+8>>1]=p,kA=0,C=0,p)for(;C=!!(65535&~p)+C|0,p=B[(D+5184|0)+((k=k+1|0)<<1)>>1];);a[w+10|0]=C,f[47352]=0,O=1,m=h,h=0,oA=0}z=0,r=u?0:r,C=u?x:g}else(0|k)>795?(C=g,g=l,A=k):(A=Fg(A,(D+784|0)+k|0)+k|0,C=g,g=l);f[47352]<(0|T)&&(f[47352]=T),T=0,l=g,k=A}if(F||(x=C,!((0|k)<799)))break}(0|rA)<=0|K||(A=198300+(f[47350]<<2)|0,f[A>>2]=128|f[A>>2],f[D+1584>>2]=64|f[D+1584>>2],K=1),A=(D+784|0)+k|0,f[v+8204>>2]=A-1,g=0,a[0|A]=0,a[D+1590|0]=0,a[1590+(G(K,12)+D|0)|0]=8;A:if((0|K)<=0)f[D+1584>>2]=512|f[D+1584>>2],k=f[49572];else{A=K-1|0;e:if(1!=(0|K))for(r=A;;){if(!CC(a[B[1588+(G(r,12)+D|0)>>1]+(D+784|0)|0])){g=r;break e}if(w=(0|r)>1,r=r-1|0,!w)break}if(g=(D+1584|0)+G(g,12)|0,f[g>>2]=16|f[g>>2],4194304&W&&(A=(D+1584|0)+G(A,12)|0,256&(g=f[A>>2])||(f[A>>2]=65536|g)),f[D+1584>>2]=512|f[D+1584>>2],!((0|K)<=0|(0|(k=f[49572]))>990))for(w=3|(A=D+624|0),u=2|A,Y=D+754|0,F=!(4194304&W),x=0,l=0;;){f[47354]=f[47354]+1;e:{if((0|(A=f[49827]))<=0||(A=A-1|0,f[49827]=A,A)){if(i[190280])break e}else a[190280]=0;A=B[1588+(G(x,12)+D|0)>>1]+(D+784|0)|0;g:if(!(a[0|A]-48>>>0>=10)&&(g=D+624|0,r=A,1227133512!=f[v+112>>2])){for(;;){r:{if(a[0|r]-48>>>0<10)a[0|g]=i[0|r],g=g+1|0,r=r+1|0;else{if(f[v+124>>2]!=a[0|r]|32!=i[r+1|0])break r;if(k=r+2|0,32==i[r+3|0]|a[0|k]-48>>>0>=10|32==i[r+4|0])break r;x=x+1|0,r=k}if(g>>>0>>0)continue;break g}break}ue(A+(g=g-(d=D+624|0)|0)|0,32,(r=(k=r-A|0)-g|0)>>>0<=k>>>0?r:0),_A(A,d,g)}for(g=0;r=g,g=g+1|0,a[A+r|0]-48>>>0<10;);g:if(r-5>>>0<=27){for(a[D+626|0]=32,I[D+624>>1]=8224,48!=i[0|A]&f[v+132>>2]>=(0|r)||(g=(D+1584|0)+G(x,12)|0,f[g>>2]=524288|f[g>>2]),h=(D+1584|0)+G(x,12)|0,p=0,k=w;g=A,!((A=a[0|A])-48>>>0>=10&(0|A)!=f[v+128>>2])&&(a[0|k]=A,A=k+1|0,d=r,(0|(r=r-1|0))<=0?k=A:f[v+112>>2]>>>r&1?(o=f[h+4>>2],m=(D+16|0)+G(p,12)|0,f[m>>2]=f[h>>2],f[m+4>>2]=o,f[m+8>>2]=f[h+8>>2],p=p+1|0,32!=(0|(m=f[v+124>>2]))&&(a[k+1|0]=m,A=k+2|0),a[0|A]=32,k=A+1|0,8&i[h+2|0]||((T=f[v+112>>2])>>>d-2&1&&(a[A+1|0]=48,a[A+2|0]=48,T=f[v+112>>2],k=A+3|0),T>>>d-3&1&&(a[0|k]=48,k=k+1|0))):k=A,A=g+1|0,k>>>0>>0););if(r=f[h+4>>2],A=(D+16|0)+G(p,12)|0,f[A>>2]=f[h>>2],f[A+4>>2]=r,r=f[h+20>>2],f[A+16>>2]=f[h+16>>2],f[A+20>>2]=r,r=f[h+12>>2],f[A+8>>2]=f[h+8>>2],f[A+12>>2]=r,r=1,(0|p)>0)for(;A=(D+16|0)+G(r,12)|0,f[A>>2]=-262209&f[A>>2],(0|p)>=(0|(r=r+1|0)););if(A=i[g+4|0]|i[g+5|0]<<8|i[g+6|0]<<16|i[g+7|0]<<24,r=i[0|g]|i[g+1|0]<<8|i[g+2|0]<<16|i[g+3|0]<<24,a[0|k]=r,a[k+1|0]=r>>>8,a[k+2|0]=r>>>16,a[k+3|0]=r>>>24,a[k+4|0]=A,a[k+5|0]=A>>>8,a[k+6|0]=A>>>16,a[k+7|0]=A>>>24,A=i[g+12|0]|i[g+13|0]<<8|i[g+14|0]<<16|i[g+15|0]<<24,g=i[g+8|0]|i[g+9|0]<<8|i[g+10|0]<<16|i[g+11|0]<<24,a[k+8|0]=g,a[k+9|0]=g>>>8,a[k+10|0]=g>>>16,a[k+11|0]=g>>>24,a[k+12|0]=A,a[k+13|0]=A>>>8,a[k+14|0]=A>>>16,a[k+15|0]=A>>>24,a[k+16|0]=0,k>>>0<=w>>>0)break g;for(A=i[h+6|0],p=0,r=w;;){for(l=tA(v,r,(D+16|0)+G(p,12)|0,255&A);A=i[0|r],r=r+1|0,32!=(0|A););if(A=0,a[h+6|0]=0,p=p+1|0,!(r>>>0>>0))break}}else{if(f[47352]=0,l=tA(v,A,g=(D+1584|0)+G(x,12)|0,i[g+6|0]),(0|(r=f[47352]))>i[g+18|0]&&(a[g+18|0]=r,f[47352]=0),!(!(4096&l)|32==i[0|A]))for(;ue(D+624|0,0,150),f[D+624>>2]=538976288,f[D+628>>2]=538976288,a[D+632|0]=32,tA(v,_A(u,A,r=Te(D+16|0,A)),g,0),32!=i[0|(A=A+r|0)];);50331648&l&&(M=(A=F|(~f[33264]+K|0)!=(0|x))?M:10,A|!iA||(f[iA>>2]=4,M=10))}if(128&l&&!((0|(g=f[33264]))<=0)){if(A=0,r=g,k=3&g)for(;d=(D+1584|0)+G(r+x|0,12)|0,f[d>>2]=1048576|f[d>>2],r=r-1|0,(0|k)!=(0|(A=A+1|0)););if(g>>>0>=4)for(;A=(D+1584|0)+G(r+x|0,12)|0,f[A>>2]=1048576|f[A>>2],f[(g=A-12|0)>>2]=1048576|f[g>>2],f[(g=A-24|0)>>2]=1048576|f[g>>2],f[(A=A-36|0)>>2]=1048576|f[A>>2],r=r-4|0;);f[33264]=r}}if(k=f[49572],(0|K)<=(0|(x=x+1|0)))break A;if(!((0|k)<991))break}}if((0|(r=f[47351]))<(0|(w=f[47350]))){for(Y=f[47202],F=f[49846],p=f[47352];;){A=(g=f[198304+(r<<2)>>2])>>8;A:{e:switch((31&g)-9|0){case 0:Y=A;break A;case 4:F=A;break A;case 3:break e;default:break A}p=g>>>0>=256?A+p|0:0}if(!(!(128&g)&(0|w)>(0|(r=r+1|0))))break}f[47352]=p,f[47351]=r,f[49846]=F,f[47202]=Y}f[49572]=k+2,f[(A=190288+(k<<3)|0)>>2]=589824,I[A+4>>1]=C,f[A+8>>2]=589824,I[A+12>>1]=C,g=K&&f[47199]?M:10,f[33285]?A=0:(A=f[33283],A=f[A>>2]==f[A+4>>2]),M=A?g:M,x=X,w=0,K=0,V=u=V-32192|0,f[u+24>>2]=0,f[u+28>>2]=0,f[u+16>>2]=0,f[u+20>>2]=0,f[u+8>>2]=0,f[u+12>>2]=0,f[u>>2]=0,f[u+4>>2]=0,C=f[49572],o=B[190284+(C<<3)>>1];A:{if((0|(r=C-3|0))<0)A=r;else{for(;;){if(w=(0|(g=127&i[(A=190288+(r<<3)|0)+3|0]))<(0|w)?w:g,B[A+4>>1])A=r;else if(A=-1,g=(0|r)>0,r=r-1|0,g)continue;break}if(w>>>0>3)break A}for(;;){if((0|(A=A-1|0))<0)break A;if(64&i[0|(g=190288+(A<<3)|0)]){a[g+3|0]=4;break A}if(!(i[g+3|0]<4))break}}if(A=f[v+292>>2],r=0,(0|C)<=0)w=0;else for(d=-1,w=0;;){g=A,f[v+292>>2]!=(0|A)&&(I[(A=190288+(r<<3)|0)>>1]=32|B[A>>1]),(0|w)>0&&(h=f[(l=190288+(r<<3)|0)+4>>2],f[(k=(A=r-w<<3)+190288|0)>>2]=f[l>>2],f[k+4>>2]=h,-1!=(0|d)&&(I[4+(A+190288|0)>>1]=d),d=-1);A:{if(21==i[2+((k=r<<3)+190288|0)|0]){if(A=i[(l=k+190288|0)+7|0],2&i[0|l])break A;e:if((0|A)!=(0|g)){if(l=i[10+(k+190288|0)|0]-9|0){if(12==(0|l))break e;break A}if(21!=i[18+(k+190288|0)|0])break A}-1==(0|d)&&(d=(A=B[4+(k+190288|0)>>1])||-1),w=w+1|0}A=g}if((0|C)==(0|(r=r+1|0)))break}if(f[49572]=C-w,qr(A),(g=f[v+36>>2])&&!((0|(A=(w=f[49572])-1|0))<0))for(Y=256&g,m=4&g,F=8&g,h=15&g,z=16&g,X=2&g,g=g>>>8&1,r=0;;){if(k=w,C=r,w=A,21==(0|(A=i[(l=(O=A<<3)+190288|0)+2|0]))){A:{e:{if((0|(r=k-2|0))>=0)for(;;){if(21==i[2+((A=r<<3)+190288|0)|0])break e;if(A=(0|r)>0,r=r-1|0,!A)break}A=f[v+292>>2];break A}A=i[7+(A+190288|0)|0]}qr(A),A=i[l+2|0]}if(r=C,(A=f[144464+((255&A)<<2)>>2])&&(r=g,!(32&i[0|l]))){r=i[A+11|0],d=0,X&&(118!=(0|(k=i[0|A]))&82!=(0|k)||(C=z?0:C,d=1));A:{e:{g:{r:switch((k=253&r)-4|0){case 1:break g;case 0:break r;default:break e}if(h&&(r=1,!C))break A;if(2!=(0|(r=C)))break A;if(r=2,!(A=i[A+13|0]))break A;a[l+2|0]=A;break A}if(h&&(r=2,!C))break A;if(1!=(0|(r=C)))break A;if(r=1,!(A=i[A+13|0]))break A;a[l+2|0]=A;break A}r=0,F&&(r=k?C:0)}r=A=d?0:r,B[4+(O+190288|0)>>1]&&(r=A=m?0:A,Y&&(r=A||1))}if(!((0|(A=w-1|0))>=0))break}if(qr(f[v+292>>2]),f[49572]<=0)z=-2,r=0;else{for(r=-1,C=0,l=0,F=0;;){A=l<<3,-1!=(0|r)&&(I[4+(A+190288|0)>>1]=r),21==i[(d=A+190288|0)+2|0]&&qr(i[7+(A+190288|0)|0]),h=f[49572];A:{if(!(32&i[0|(k=A+190288|0)])){C=(0|(g=h-1|0))>(0|l)?f[144464+(i[10+(A+190288|0)|0]<<2)>>2]:C,!(B[k+12>>1]|(0|g)==(0|l))&&(p=0,i[C+11|0]|!C)||(p=1),w=i[d+2|0];e:if(!((0|(z=f[49848]))<=0))for(g=A+190288|0,r=0;;){if(Y=G(r,3),i[Y+199408|0]==(255&w)&&!((m=i[2+(Y+199408|0)|0])&(1^p)|(4&i[g+3|0]?2&m:0)|(B[g+4>>1]?0:4&m))){if(w=i[1+(Y+199408|0)|0],a[d+2|0]=w,!(2&i[f[144464+(w<<2)>>2]+4|0])|i[g+3|0]<2)break e;a[g+3|0]=0;break e}if((0|z)==(0|(r=r+1|0)))break}if(!(255&w)){r=B[4+(A+190288|0)>>1];break A}}r=f[k+4>>2],A=(u+32|0)+(F<<5)|0,g=f[k>>2],f[A>>2]=g,f[A+4>>2]=r,g=f[144464+(g>>>14&1020)>>2],f[A+8>>2]=g,a[A+17|0]=i[g+11|0],F=F+1|0,r=-1}if(!((0|F)<1e3&(0|h)>(0|(l=l+1|0))))break}if(w=0,r=0,!((0|(z=F-2|0))<=0))for(;;){A:if(B[4+((u+32|0)+(w<<5)|0)>>1]){for(g=(0|w)>(0|z)?w:z,r=0,A=w;;){if((0|A)!=(0|g)){if(r=(0|r)>(0|(k=i[3+((C=u+32|0)+(A<<5)|0)|0]))?r:k,!B[4+(C+((A=A+1|0)<<5)|0)>>1])continue}else A=g;break}if((0|A)<=(0|w))break A;if(g=~w+A|0,C=0,k=A-w&7)for(;a[6+((u+32|0)+(w<<5)|0)|0]=r,w=w+1|0,(0|k)!=(0|(C=C+1|0)););if(g>>>0<7)break A;for(;a[(g=(u+32|0)+(w<<5)|0)+6|0]=r,a[g+38|0]=r,a[g+70|0]=r,a[g+102|0]=r,a[g+134|0]=r,a[g+166|0]=r,a[g+198|0]=r,a[g+230|0]=r,(0|(w=w+8|0))!=(0|A););}else A=w+1|0;if(w=A,!((0|z)>(0|A)))break}}for(f[u+40>>2]=f[36125],qr(f[v+292>>2]),P=(0|r)<4,l=1,Y=1,h=0,g=0,r=0,X=0;;){A:{e:{g:{r:{if(g){if(C=(A=u+32|0)+((d=r-1|0)<<5)|0,h=i[2+(A+(r<<5)|0)|0],(0|d)>0){if(A=l-(r=(0|l)>0)|0,d>>>0>=(w=r?l:2)>>>0)for(;k=(r=(u+32|0)+(w<<5)|0)-32|0,l=f[r+12>>2],f[k+8>>2]=f[r+8>>2],f[k+12>>2]=l,l=f[r+4>>2],f[k>>2]=f[r>>2],f[k+4>>2]=l,l=f[r+28>>2],f[k+24>>2]=f[r+24>>2],f[k+28>>2]=l,l=f[r+20>>2],f[k+16>>2]=f[r+16>>2],f[k+20>>2]=l,(0|d)>=(0|(w=w+1|0)););l=A}p=f[144464+(h<<2)>>2],f[C>>2]=0,f[C+4>>2]=0,f[C+24>>2]=0,f[C+28>>2]=0,f[C+16>>2]=0,f[C+20>>2]=0,f[C+8>>2]=0,f[C+12>>2]=0,a[C+2|0]=g,A=f[144464+(g<<2)>>2],f[C+8>>2]=A,h=C}else{if((0|r)>=(0|z)|(0|X)>=997)break r;k=i[(C=(w=r<<5)+(u+32|0)|0)+2|0],A=f[144464+(k<<2)>>2],f[C+8>>2]=A,d=B[C+4>>1],21==(0|k)&&qr(i[7+(w+(u+32|0)|0)|0]),l=d?r:l,p=f[144464+(i[C+34|0]<<2)>>2],f[C+40>>2]=p,d=r}if(!A){g=0,r=d+1|0;continue}if(bA(v,256,C,u+32040|0,u),(0|(r=f[u+32052>>2]))>0&&(w=(u+32|0)+(d<<5)|0,p=f[144464+(r<<2)>>2],f[w+40>>2]=p,a[w+34|0]=r,a[w+49|0]=i[p+11|0]),r=0,g)g=A;else if((0|(w=f[u+32056>>2]))<=0)g=A;else{g=f[144464+(w<<2)>>2],f[C+8>>2]=g,r=i[C+2|0],a[C+2|0]=w,w=B[C>>1];C:if(2!=i[g+11|0])I[C>>1]=65531&w;else{if(I[C>>1]=4|w,2==i[A+11|0])break C;a[C+3|0]=0}bA(v,256,C,u+32040|0,u)}if((0|(k=f[u+32048>>2]))<=0)w=g;else{if(w=f[144464+(k<<2)>>2],a[C+2|0]=k,f[C+8>>2]=w,A=i[w+11|0],F=1,1==(0|k)){O=2==(0|A);break e}k=B[C>>1];C:if(2!=(0|A))I[C>>1]=65531&k;else{if(I[C>>1]=4|k,2==i[g+11|0])break C;a[C+3|0]=0}bA(v,256,C,u+32040|0,u)}if(O=0,2!=(0|(A=i[w+11|0]))){F=0;break e}if(O=1,F=0,A=2,i[C+3|0]>1){K=0;break e}k=C+3|0,K=K+1|0,g=C;C:{if(8&(m=f[v+12>>2])){for(;;){a:switch(m=g,g=g+32|0,i[m+49|0]){case 0:break e;case 2:break a;default:continue}break}if(i[0|(g=m+35|0)]>1)break e;if(i[C+6|0]<=3&&(a[0|k]=0),i[m+38|0]<4)break C;break e}if(1&K|(0|K)<2)break e;if(2&m)break g;if(P)g=k;else if(g=k,B[C+36>>1])break g}a[0|g]=0;break e}f[36423]=X+2,I[(A=145840+(X<<5)|0)>>1]=0,a[A+2|0]=9,a[A+20|0]=2,f[A+12>>2]=M,I[A+4>>1]=o,a[A+17|0]=0,a[A+18|0]=0,f[A+8>>2]=f[36125],I[A+32>>1]=0,a[A+34|0]=9,a[A+52|0]=0,f[A+44>>2]=0,I[A+36>>1]=0,a[A+49|0]=0,a[A+50|0]=0,f[A+40>>2]=f[36126],qr(f[v+292>>2]),V=u+32192|0;break A}K=1}if(!(8&(g=B[C+32>>1]))|(0|d)<=0||(k=i[p+11|0])>>>0>15|!(1<>1]=8^g),N=B[C+36>>1]){e:if(g=f[v+4>>2]){g:switch(0|A){default:r=512&g?11:r;break;case 0:break e;case 2:break g}if(2==i[p+11|0]){(k=12&g)&&(r=12!=(0|k)?23:11);g:if(O){r:switch(3&g){case 2:r=10;break g;case 0:break g;default:break r}r=23}i[C+35|0]<4||(r=256&g?10:r)}}if(!((0|C)==(0|h)|(0|X)<=0)){e:{g:{r:switch(0|(g=7&f[v>>2])){case 0:break e;case 1:break r;default:break g}if(r-12>>>0>4294967293)break e}r=i[g+101916|0]}r=f[47205]>0?24:r}}if(f[C+72>>2]=f[144464+(i[C+66|0]<<2)>>2],g=f[u+32060>>2],g=r||(g||r),!F){a[(k=(m=X<<5)+145840|0)+17|0]=A,f[k+8>>2]=w,a[k+16|0]=0,I[k>>1]=B[C>>1],a[k+3|0]=15&i[C+3|0],a[k+6|0]=i[C+6|0],r=i[C+7|0],I[k+4>>1]=0,a[k+7|0]=r,F=i[w+10|0],a[k+2|0]=F;e:if(r=B[C+4>>1]){if(I[k+4>>1]=r,x=1&x?5:1,a[(C=m+145840|0)+20|0]=x,r=Y,Y=0,!r){x=0;break e}a[C+20|0]=8|x,x=0}else a[20+(m+145840|0)|0]=0;f[(r=m+145840|0)+12>>2]=f[u+32084>>2]<<1,!N|24!=(0|F)||(0|(C=f[47205]))<=0||(f[k+8>>2]=f[36126],f[r+12>>2]=G(C,14)),(1<>>0<=8:0)|2&i[w+7|0]&&(f[r+12>>2]=128,a[k+16|0]=0),a[(A=m+145840|0)+21|0]=255,a[A+22|0]=255,I[A+18>>1]=5120,X=X+1|0}r=d+1|0;continue}break}I[88922]=1,f[44462]=0,rA&&(I[145776+(f[36423]<<5)>>1]=2,A=198304+(f[47350]<<2)|0,f[A>>2]=128,f[(A=A-4|0)>>2]=128|f[A>>2]),a[190268]=W>>>19&1,cA&&(f[cA>>2]=W<<14>>31&189360)}V=D+6832|0,z=f[47192],O=f[e+12>>2],g=0,k=0,l=0,m=0,h=0,T=0,M=0,V=o=V-6e3|0;A:if(!((0|(C=(X=f[36423])-1|0))<=0)){for(;a[2+(o+G(g,6)|0)|0]=0,4&i[(A=g<<5)+145840|0]?(r=o+G(m,6)|0,a[r+1|0]=0,A=A+145840|0,a[r+3|0]=i[A+49|0],A=i[A+3|0],a[0|r]=A,m=m+1|0,T=(A>>>0>3)+T|0):27!=i[f[8+(A+145840|0)>>2]+10|0]|(0|m)<=0||(A=(o+G(m,6)|0)-4|0,a[0|A]=4|i[0|A]),(0|C)!=(0|(g=g+1|0)););if(a[o+G(m,6)|0]=0,m)if(1==f[z+148>>2]){if(!((0|X)<=0)){for(A=-2&X,r=1&X,g=145840;l=2==i[g+17|0]&&i[g+3|0]>3?k:l,l=2==i[g+49|0]&&i[g+35|0]>3?1|k:l,g=g- -64|0,k=k+2|0,(0|A)!=(0|(h=h+2|0)););!r|2!=i[g+17|0]||(l=i[g+3|0]>3?k:l)}if(a[(w=(A=l<<5)+145840|0)+3|0]=7,30313==f[z+212>>2]&&(i[(A=A+145840|0)+7|0]||(a[A+7|0]=Er(55),X=f[36423])),!((0|X)<=0)){for(h=0,g=145840,A=145840,k=d=f[36125],C=0,Y=1;;){if(i[g+17|0]?x=f[36125]:(x=f[36125],d=(r=i[f[g+8>>2]+14|0]>50)?x:d,Y|=r),r=i[g+20|0]?x:k,4&i[0|g]){x=i[g+7|0],k=f[144464+(x<<2)>>2];e:{if(6840683==(0|(m=f[z+212>>2]))){if(49!=f[r>>2])break e;if((m=f[k>>2]-49|0)>>>0>5|!(1<>2]}if(!(6516078!=(0|m)&31336!=(0|m))){m=0,x||(C=Er(1&(m=C|Y)?13621:12593),a[g+7|0]=C,k=f[144464+(C<<2)>>2]),(0|l)!=(0|h)|13621!=(1024|f[k>>2])||(a[w+3|0]=6),3420466==f[d>>2]&&(a[A+7|0]=Er(3420466==f[k>>2]?13619:12594));g:{if(12597==f[r>>2]){if(12597!=(0|(x=f[k>>2])))break g;a[A+7|0]=Er(13109)}x=f[k>>2]}C=m,12593==(0|x)&&(13621==(0|(x=f[d>>2]))&&(a[g+7|0]=Er(12850),x=f[d>>2]),13619==(0|x)&&(a[g+7|0]=Er(13107),x=f[d>>2]),3420466==(0|x)&&(a[g+7|0]=Er(13364)),a[g+3|0]=0)}}Y=0,d=k,A=g}else k=r;if(g=g+32|0,!((0|(h=h+1|0))<(0|(r=f[36423]))))break}if(k=0,g=145840,!((0|r)<=0))for(;4&i[0|g]&&((A=i[g+7|0])||(a[g+7|0]=17,A=17),A=f[144464+(A<<2)>>2],a[g+21|0]=i[A+12|0],a[g+22|0]=i[A+13|0]),g=g+32|0,(0|r)!=(0|(k=k+1|0)););}}else{if(A=f[z+152>>2],A=z+G(P=(0|A)>7?1:A,6)|0,v=i[0|(P?A+637:z+157)],W=i[0|(P?636+(A+O|0):156+(z+O|0))],a[133068]=4==(0|O),!((0|m)<=0)){for(F=m-1|0,p=O-1>>>0>1,r=0,C=0;;){D=o+G(C,6)|0,M=((A=i[0|D])<<24>>24>3)+M|0;e:if(6==(0|A)){A=C-3|0,g=C;g:{for(;;){if((0|g)<=(0|r)|(0|A)>=(0|g))break g;r:switch(w=o+G(g=g-1|0,6)|0,i[0|w]-4|0){case 2:break g;case 0:break r;default:continue}break}a[0|w]=3}g=C;g:{for(;;){if((0|m)<=(0|(g=g+1|0)))break g;r:switch(i[o+G(g,6)|0]-4|0){case 0:break g;case 2:break r;default:continue}break}a[D+2|0]=2,a[0|D]=5,A=r;break e}if(6==i[0|D]){a[D+2|0]=2,u=0;g:if((0|m)<=(0|(A=C+1|0)))w=C,U=0;else if(U=1,(0|(k=a[o+G(A,6)|0]))>4)w=C;else{for(l=(T-M|0)>1,w=C;;){if(g=A,4==(255&k)&&(A=l+1|0,l=1,!((0|A)<=1))){A=g;break g}if(U=(0|m)>(0|(A=g+1|0)),(0|A)==(0|m))break;if(w=g,(0|(k=a[o+G(A,6)|0]))>4)break g}w=F,A=m}k=-1,x=0,l=0,Y=-1,h=0,d=-1;g:{if((0|(g=r))<(0|A)){for(;k=(N=(0|(d=a[o+G(g,6)|0]))>3)&&(0|k)<0?g-r|0:k,u=(h=(0|l)>(0|d))?u:(0|l)<(0|d)?g:x,Y=N?g:Y,x=h?x:g,N=(0|g)!=(0|w),l=h?l:d,g=g+1|0,N;);if(h=x,d=Y,(0|k)>=0)break g}k=A,x=h,Y=d}f[33269]=w-x,f[33268]=k,f[33270]=x,f[33271]=u;g:if(i[133068])f[33270]=A,f[33271]=A;else if((0|Y)>=0){if((0|A)!=(0|m))break g;a[o+G(Y,6)|0]=7}else a[o+G(x,6)|0]=7;nA(o,P,r,A,W),!U&!!(0|O)||(W=p?i[z+156|0]:i[z+157|0])}else A=r}else A=r;if((0|A)>=(0|C))r=A;else if(4&i[D+2|0]){for(r=C+1|0,k=-1,Y=0,x=0,l=0,g=A,u=-1;k=(h=(0|(w=a[o+G(g,6)|0]))>3)&&(0|k)<0?g-A|0:k,Y=(d=(0|w)<(0|l))?Y:(0|w)>(0|l)?g:x,u=h?g:u,x=d?x:g,h=(0|g)!=(0|C),l=d?l:w,g=g+1|0,h;);f[33269]=C-x,f[33270]=x,f[33271]=Y,f[33268]=(0|k)<0?r:k,i[133068]?(f[33270]=r,f[33271]=r):(0|u)>=0?a[o+G(u,6)|0]=7:a[o+G(x,6)|0]=7,nA(o,P,A,r,v)}else r=A;if((0|m)==(0|(C=C+1|0)))break}if(!((0|r)>=(0|m))){for(k=-1,Y=0,x=0,l=0,g=r,u=-1;k=(w=(0|(A=a[o+G(g,6)|0]))>3)&&(0|k)<0?g-r|0:k,Y=(C=(0|A)<(0|l))?Y:(0|A)>(0|l)?g:x,u=w?g:u,x=C?x:g,l=C?l:A,(0|m)!=(0|(g=g+1|0)););f[33270]=x,f[33271]=Y,f[33269]=~x+m,f[33268]=(0|k)<0?m:k,i[133068]?(f[33270]=m,f[33271]=m):(0|u)>=0?a[o+G(u,6)|0]=7:a[o+G(x,6)|0]=7,nA(o,P,r,m,W)}}if((0|X)<=0)break A;for(g=0,h=0;;){if(d=r=(w=g<<5)+145840|0,A=o+G(h,6)|0,k=i[0|A],a[r+3|0]=k,4&i[0|r]){C=w+145840|0,r=i[A+4|0],a[C+21|0]=r,l=i[A+5|0],a[C+16|0]=0,a[C+22|0]=l;e:{if(1&(x=i[A+2|0]))A=2;else{if(k>>>0<6)break e;A=i[A+1|0]}a[C+16|0]=A}r>>>0<=(255&l)>>>0?(A=l,l=r):(a[C+21|0]=l,a[C+22|0]=r,A=r),(r=i[7+(w+145840|0)|0])&&(A=(255&A)+(255&l)>>>1|0,r=f[144464+(r<<2)>>2],a[C+22|0]=A+i[r+13|0],a[C+21|0]=A+i[r+12|0]),2&x&&(a[d+3|0]=8|k),h=h+1|0}if((0|X)==(0|(g=g+1|0)))break}}}if(V=o+6e3|0,h=f[47192],g=0,p=0,u=0,M=0,V=o=V-160|0,f[36423]>=2)for(O=f[30450],k=1;;){if(k=(A=k)+1|0,m=i[(C=(l=A<<5)+145840|0)+3|0],2&(x=B[C>>1])){for(;2==(31&(w=f[198304+(M<<2)>>2]))&&(VA(127&w,w>>>8|0),f[36432]=110,f[36433]=100,f[36434]=450,f[36430]=5,x=f[50786],d=f[32972],(0|(r=f[d+84>>2]))>0&&(x=(0|G(r,x))/100|0),Y=i[((0|(r=(0|x)>=359?359:x))<=80?80:r)+101856|0],r=(0|(r=(0|x)>=450?450:x))>399?6:(0|r)>379?7:Y,f[32526]=(0|G(r,f[d+72>>2]))/256,f[32527]=(0|G(r,f[d+76>>2]))/256,f[32528]=(0|G(r,f[d+80>>2]))/256,r>>>0>7||(d=r-1|0,f[32528]=d,f[32526]=r,f[32527]=d)),M=M+1|0,!(128&w););x=B[C>>1]}d=k<<5,Y=A-1|0,r=7&m;A:{e:{g:{r:{C:{a:{I:{f:{i:{b:switch(W=i[17+(l+145840|0)|0],0|(w=4&x?2:W)){case 2:break C;case 3:case 8:break a;case 5:break I;case 6:case 7:break f;case 4:break i;case 0:break b;default:break A}g=0;break A}if(6!=(0|(A=i[17+(145840+(Y<<5)|0)|0]))?(r=4==(0|A)?60:f[34063]>0||r>>>0<4?48:60,a[18+(l+145840|0)|0]=r):(r=25,a[18+(l+145840|0)|0]=25),!(16&i[0|h])|!i[20+(l+145840|0)|0]||(a[18+(l+145840|0)|0]=60,r=60),64&i[f[8+(l+145840|0)>>2]+6|0]&&(r=r+30|0,a[18+(l+145840|0)|0]=r),g=0,!(8&x))break A;a[18+(l+145840|0)|0]=i[h+164|0]+r;break A}!(C=i[(A=l+145840|0)+20|0])|1&a[f[A+8>>2]+7|0]&2==i[17+(145840+(Y<<5)|0)|0]||(a[18+(l+145840|0)|0]=15),r=i[17+(d+145840|0)|0],8&i[f[8+(l+145840|0)>>2]+4|0]|r|8!=i[17+(145840+(Y<<5)|0)|0]||(a[18+(l+145840|0)|0]=25),64&i[f[8+((A=Y<<5)+145840|0)>>2]+5|0]&&(a[18+(l+145840|0)|0]=30),!C|!(16&f[h>>2])||(a[18+(l+145840|0)|0]=30);f:if(i[20+(d+145840|0)|0]|!(32&i[f[8+(l+145840|0)>>2]+4|0])|4!=(0|r))f[12+(l+145840|0)>>2]=256;else{if(C=l+145840|0,2==i[17+(A+145840|0)|0]){f[C+12>>2]=200;break f}f[C+12>>2]=150}if(7!=(0|w))break A;if(p|=2==(0|r),2!=(254&i[17+(A+145840|0)|0]))break A;f[12+(l+145840|0)>>2]=f[12+(A+145840|0)>>2]+255>>>1;break A}6==(254&(r=i[17+((A=Y<<5)+145840|0)|0]))|3==(0|r)|32&f[f[8+(A+145840|0)>>2]+4>>2]&&(a[18+(l+145840|0)|0]=30);I:if(2==(254&(C=i[17+(d+145840|0)|0]))){p=i[20+(d+145840|0)|0]&&2!=(0|C)?p:1,a[(C=l+145840|0)+18|0]=40,m=0;f:{i:switch(0|r){case 0:if((A=f[12+(A+145840|0)>>2])>>>0>39)break f;m=40-A|0;break f;case 2:break f;default:break i}if(i[20+(l+145840|0)|0])break I;m=20;i:switch(r-3|0){case 1:if(m=0,!(8&i[f[8+(A+145840|0)>>2]+4|0]))break f;break I;case 0:break f;case 5:break i;default:break I}m=12}a[C+18|0]=m}if(!(16&i[0|h])|!i[20+(l+145840|0)|0])break A;if(i[(A=l+145840|0)+18|0]>19)break A;a[A+18|0]=20;break A}C=i[h+296|0],w=r=l+145840|0,f[r+12>>2]=256,a[r+19|0]=C;a:if(i[r+20|0]){x=25;I:switch(i[17+(145840+(Y<<5)|0)|0]-2|0){case 0:if(x=12,1&a[f[8+(l+145840|0)>>2]+7|0])break a;break;case 1:break I;default:break a}a[18+(l+145840|0)|0]=x}if(2==(0|(d=i[17+(d+145840|0)|0]))){u=1;break A}if(a[(C=l+145840|0)+22|0]=g,2==(254&i[17+((r=Y<<5)+145840|0)|0]))break r;if(r=g,(0|(w=f[36423]))<=(0|A))break e;for(;;){if(2==i[17+((r=A<<5)+145840|0)|0]){r=i[22+(r+145840|0)|0],a[C+22|0]=r;break e}if((0|w)==(0|(A=A+1|0)))break}break g}if(z=l+145840|0,F=r^r>>>0<2,r=(X=8&m)?25:i[296+(F+h|0)|0]-u|0,a[z+19|0]=r,(f[36423]-3|0)>(0|A)||(0|(w=255&r))<=(0|(r=f[h+52>>2]))||(a[z+19|0]=r),r=0,m=0,!(x=i[C+52|0]))for(;w=f[C+40>>2],m=2==i[C+49|0]?(~f[w+4>>2]>>>20&1)+m|0:m,r=27==i[w+10|0]?2:r,w=C,C=C+32|0,!(x=i[w+84|0]););w=z+96|0,A=(P=A+2<<5)+145840|0,f[34063]=m,D=i[f[C+40>>2]+10|0],d=d+145840|0,i[17+(P+145840|0)|0]|23!=i[f[d+8>>2]+10|0]?(C=w,w=A,A=d):C=l+145968|0,d=i[f[w+8>>2]+15|0];C:if(m)d=i[f[h+96>>2]+(i[f[A+8>>2]+15|0]+G(d,10)|0)|0],8!=i[A+17|0]|4!=(254&i[w+17|0])||(d=8&i[f[C+8>>2]+4|0]?d-15|0:d);else{if(v=f[h+100>>2],P=i[f[A+8>>2]+15|0],C=i[A+20|0],d=i[v+(P+G(C|i[w+20|0]?1==(0|d):d,10)|0)|0],!C|!(32&i[0|h]))break C;d=i[1+(v+G(P,10)|0)|0]+d>>>1|0}C=x>>>1|0,P=!m,x=(0|(d=(0|G(f[130104+(m?1==(0|m)?4:8:0)>>2],d))/128|0))<=8?8:d;C:if(7!=(0|F))X&&(x=f[h+200>>2]+x|0);else{if(x=(d=f[h+200>>2])+x|0,!X)break C;x=((0|d)/2|0)+x|0}d=C&P|27==(0|D),(C=B[304+(h+(F<<1)|0)>>1])||(C=B[h+316>>1]),x=G(C<<16>>16,x),(m=i[(C=l+145840|0)+7|0])&&(F=i[f[144464+(m<<2)>>2]+14|0])&&(x=(0|G(x,F))/100|0),1==(d|2==(0|r))&&(2097152&(r=f[h+12>>2])||(x=(0|G(262144&r?282:256+((280-(i[f[8+(l+145840|0)>>2]+14|0]<<1)|0)/3|0)&65535,x))/256|0)),F=l+145840|0,r=G(f[32526],f[h+196>>2]),X=2!=(0|W)?256:(0|((0|r)>(0|x)?x:r))/128|0,f[F+12>>2]=X,(r=i[F+16|0])>>>0>=19&&(gC(84371,28,O),a[F+16|0]=0,m=i[C+7|0],r=0),x=r+1|0,(r=255&m)?(_g(r,o+8|0),r=_r(f[o+132>>2])):r=f[129280+((255&x)<<2)>>2],d=l+145840|0,1&(u|p)&&(C=(l=Y<<5)+145840|0,u=i[0|r],r=i[d+21|0],r=((0|G(u,i[d+22|0]-r|0))/256|0)+r|0,a[C+22|0]=r,g=(r-(g=255==(0|r)?255:g)|0)>16?r-16|0:g,a[C+21|0]=g,C=0,(0|g)<(0|r)&&(a[F+16|0]=x,C=2),f[(g=l+145840|0)+12>>2]=X,a[g+16|0]=C,r=i[z+19|0],a[g+19|0]=3!=i[g+17|0]&&r>>>0>18?18:r),g=2!=(0|W),C=-2&(r=B[A>>1]),I[A>>1]=C;C:{a:{I:switch(i[A+17|0]-3|0){case 5:if(2==i[w+17|0])break C;C=1|r;break a;case 0:break I;default:break C}if(I[A>>1]=1|r,2!=i[w+17|0]&&12146!=f[f[A+8>>2]>>2])break C}I[A>>1]=C}g?(0|(A=g<<4))<=((r=i[d+22|0])-(C=i[d+21|0])|0)||(C=(0|(A=r-A|0))>0?A:0,a[d+21|0]=C):(r=i[d+22|0],C=i[d+21|0]),A=255&C,g=((0|G(i[f[129280+(i[F+16|0]<<2)>>2]+127|0],r-A|0))/256|0)+A|0,u=0,p=0;break A}C=f[12+(r+145840|0)>>2],f[w+12>>2]=C,3==(0|W)&&(C=f[32526],f[w+12>>2]=C),r=g;r:switch(d-5|0){case 0:f[w+12>>2]=(G(C,160)>>>0)/100;break g;case 2:break r;default:break e}f[w+12>>2]=(G(C,120)>>>0)/100}r=g}p=0,a[(A=l+145840|0)+16|0]=0,C=A,r=(A=255&r)-16|0,a[C+21|0]=A>>>0>=r>>>0?r:0}if(!(f[36423]>(0|k)))break}if(V=o+160|0,15&(C=f[47197])|f[36456]){A=0,w=0,V=r=V-80|0;A:if((g=f[33222])||(f[33223]=500,g=IA(500),f[33222]=g,g)){if(!((f[36423]-2|0)<2)){for(A=C>>8,h=128&C?0:A,m=A&C<<24>>31,d=2&C,C=r+32|1,l=1;;){if(Ce(r,f[(k=(Y=l<<5)+145840|0)+8>>2],k,d,r+72|0),A=r+32|0,1==(13&(g=i[k+20|0]))&&(a[r+32|0]=32,A=C),!h|32!=(0|h)&!!(0|g)|l>>>0<2||(Te(r+76|0,r),f[r+76>>2]-880>>>0>4294967103||(A=Fg(h,A)+A|0)),4&i[0|k]&&((g=i[3+(Y+145840|0)|0])>>>0<2||(g=g>>>0>=5?5:g,g=d?g>>>0>3?712:716:a[g+94144|0],f[r+76>>2]=g,A=Fg(g,A)+A|0)),x=0,f[r+72>>2]=0,i[0|(g=r)])for(;g=Te(r+76|0,g)+g|0,f[r+72>>2]>>>x-1&1|!m|(0|x)<=0||(u=f[r+76>>2])-880>>>0>4294967103||Zr(u)&&(A=Fg(m,A)+A|0),x=x+1|0,A=Fg(f[r+76>>2],A)+A|0,i[0|g];);if(21!=i[f[k+8>>2]+10|0]&&(8&(g=B[k>>1])&&(A=Ce(A,f[36128],k,d,0),g=B[k>>1]),!(4&g)|2==i[17+(Y+145840|0)|0]||(A=Ce(A,f[36136],k,d,0)),(g=i[7+(Y+145840|0)|0])&&(A=Ce(A,f[144464+(g<<2)>>2],k,d,0))),(A=(k=A-(r+32|0)|0)+w|0)>>>0>2]=g,eC(f[47195],84367,e)),(A=f[36456])&&HC[0|A](g)}i[190280]?(f[36423]=0,A=1):($(0),(A=f[e+8>>2])?(V=g=V+-64|0,oC(g,A,60),Ag(g,1),r=0,(A=CA(g,0))&&(r=A,i[202976]&&(r=CA(202976,2))),V=g- -64|0,f[44468]=r):r=f[44468],A=1,r&&(g=f[32972],(r=IA(1344))&&(g=_A(r,g,1344),r=216192+(f[50758]<<4)|0,f[r>>2]=11,f[r+8>>2]=g,g=f[50758]+1|0,f[50758]=(0|g)<=169?g:0),f[44468]=0))}else A=0,a[190280]=0;else A=0,f[36423]=0,f[50758]=0,f[50757]=0;return V=e+16|0,A}function S(A,e){var g=0,r=0,C=0,a=0;g=1073741825;A:{e:{g:{r:{C:{a:{I:{f:{i:{b:{s:{t:{n:{k:{o:{B:{c:{Q:{G:{w:{E:{D:{u:{l:{x:{d:{m:{M:{v:{h:{p:{Y:{H:{N:{P:{F:{y:{z:{O:{Z:{K:{W:{X:{L:{T:{V:{J:{R:{U:{j:{S:{q:{_:{$:{AA:{eA:{gA:{rA:{CA:{aA:{IA:{fA:{iA:{bA:{sA:{tA:{nA:{kA:{oA:{BA:{cA:{QA:{GA:{wA:{EA:{DA:{uA:{lA:{xA:{dA:{mA:{MA:{vA:{hA:{pA:{YA:{HA:{NA:{PA:{FA:{yA:{zA:{OA:{ZA:{KA:{WA:{XA:{LA:{TA:{VA:{JA:{RA:{UA:switch(0|e){case 0:e=A-9>>>0<5?1073741825:0,e=(A=133==(0|A))?1073741825:e;break x;case 1:jA:{SA:{qA:{_A:{$A:{Ae:{ee:{ge:{re:{Ce:{ae:{Ie:{fe:{ie:{if((0|(e=-256&A))<=2047){if(!e)break ie;if(1536==(0|e))break fe;if(1792!=(0|e))break jA;if(g=0,r=1,1807!=(0|A))break jA;break X}if((0|e)<=69631){if(2048==(0|e))break Ie;if(8192!=(0|e))break jA;switch(g=131076,A-8204|0){case 1:break ae;case 0:break X;default:break Ce}}if(69632==(0|e))break re;if(917504!=(0|e))break jA;switch(g=8388608,A-917505|0){case 62:break qA;case 58:break _A;case 57:break $A;case 45:break Ae;case 43:break ee;case 32:break ge;case 0:break X;default:break SA}}if(g=16,173!=(0|A))break jA;break X}if(g=0,r=1,A-1536>>>0<6)break X;r=1757==(0|A),e=(A=1564==(0|A))?2:0;break l}if(g=0,r=1,2274!=(0|A))break jA;break X}return U=64,4}if(g=1073741826,8206==(-2&A))break X;if(A-8234>>>0<5)return U=0,2;if(g=128,A-8289>>>0<4)break X;if(g=2,A-8294>>>0<4)break X;if(g=8388608,A-8298>>>0>=6)break jA;break X}e=!(A-69821&-17),A=0;break u}U=536870976;break D}U=268435520;break D}U=-2147483584;break D}U=134217792;break D}U=67108928;break D}U=1073741888;break D}if(g=131072,r=64,A-917536>>>0<96)break X}break Z;case 2:jA:{SA:{qA:{_A:{$A:{Ae:{ee:{if((0|(e=-256&A))<=130303){if((0|e)<=127743){if((0|e)<=64767){if((0|e)<=11007){if(8192==(0|e))break ee;if(9216!=(0|e))break jA;if(A-9255>>>0>=25)break Ae;break O}if(11008==(0|e))break $A;if(11776!=(0|e))break jA;if(g=-2147483648,A-11845>>>0>=59)break jA;break X}if((0|e)<=126975){if(64768==(0|e))break _A;if(65280!=(0|e))break jA;if(g=4194304,A-65520>>>0>=9)break jA;break X}if(126976==(0|e)|127232==(0|e)|127488==(0|e))break W;break jA}if((0|e)<=129023){if((0|e)<=128255){if(127744==(0|e)|128e3==(0|e))break W;break jA}if(128256==(0|e)|128512==(0|e)|128768==(0|e))break W;break jA}if((0|e)<=129535){if(129024==(0|e)|129280==(0|e))break W;break jA}if(129536==(0|e)|129792==(0|e)|130048==(0|e))break W;break jA}if((0|e)<=919039){if((0|e)<=917759){if((0|e)<=130815){if(130304==(0|e))break W;if(130560!=(0|e))break jA;break W}if(130816==(0|e))break W;if(917504!=(0|e))break jA;if(917632!=(-128&A))break qA;break z}if((0|e)<=918271){if(917760==(0|e))break SA;if(g=4194304,918016!=(0|e))break jA;break X}if(918272==(0|e)|918528==(0|e))break z;if(g=4194304,918784!=(0|e))break jA;break X}if((0|e)<=920319){if((0|e)<=919551){if(919040==(0|e))break z;if(g=4194304,919296!=(0|e))break jA;break X}if(919552==(0|e)|919808==(0|e))break z;if(g=4194304,920064!=(0|e))break jA;break X}if((0|e)<=920831){if(920320==(0|e))break z;if(g=4194304,920576!=(0|e))break jA;break X}if(920832==(0|e)|921088==(0|e))break z;if(g=4194304,921344!=(0|e))break jA;break X}if(g=4194304,8293!=(0|A))break jA;break X}if(g=-2147483648,A-9291>>>0>=21)break jA;break X}if(11248==(-16&A)|A-11219>>>0<25|11209==(0|A)|A-11194>>>0<3)break O;if(11124==(0|(e=-2&A)))break O;if(g=-2147483648,11158!=(0|e))break jA;break X}if(g=65536,A-64976>>>0>=32)break jA;break X}if(917504==(0|A))break z;if(g=4194304,A-917506>>>0>=30)break jA;break X}if(g=4194304,A>>>0>917999)break X}e=(A=!(65534&~A))>>>16|0,A<<=16;break u;case 6:jA:{SA:{qA:{_A:{$A:{Ae:{ee:{ge:{re:{Ce:{ae:{Ie:{if((0|(e=-256&A))<=7679){if((0|e)<=767){if(!e)break Ie;if(256==(0|e))break ae;if(512!=(0|e))break Z;if(585!=(0|A))break Ce;break E}if(768==(0|e))break re;if(1024==(0|e))break ge;if(7424!=(0|e))break Z;if(g=16777216,7574!=(0|A))break Z;break X}if((0|e)<=119807){if(7680==(0|e))break ee;if(8448==(0|e))break Ae;if(65280!=(0|e))break Z;if(g=256,A-65345>>>0>=6)break Z;break X}if((0|e)<=120319){if(119808==(0|e))break $A;if(120064!=(0|e))break Z;if(A>>>0>=120070)break _A;break y}if(120320==(0|e))break qA;if(120576!=(0|e))break Z;if(A>>>0>=120597)break SA;break y}if(g=768,A-97>>>0<6)break X;if(g=16777216,A-105>>>0>=2)break Z;break X}g=(e=329==(0|A))>>>9|0,e=(A=303==(0|A))?16777216:e<<23;break w}if(616==(0|A))break E;if(g=16777216,669!=(0|A))break Z;break X}g=128;re:switch(A-976|0){case 35:break jA;case 0:case 1:case 2:case 5:case 32:case 33:break X;default:break re}if(1012!=(-2&A))break Z;break X}r=(A=!(A-1110&-3))>>>8|0,A<<=24;break G}r=(e=7883==(0|A))>>>8|0,e=(A=7725==(0|A))?16777216:e<<24;break l}if(A-8458>>>0<10)break y;if((e=A-8495|0)>>>0<11)break RA;break L}if(119842==(0|(e=-2&A)))break F;if(A-119808>>>0<85)break y;if(A-119894>>>0<2|A-119946>>>0<2)break F;if(119995==(0|A)|A-119896>>>0<69|A-119982>>>0<12)break y;if(119998==(0|e))break F;if(A-119997>>>0<7)break y;if(g=16777344,120050==(0|e))break X;if(g=128,A>>>0<=120004)break Z;break X}if(120102==(-2&A))break F;if(A-120094>>>0<28)break y;_A:{if((0|A)<=120257){if(A-120154>>>0<2)break F;if(g=16777344,A-120206>>>0>=2)break _A;break X}if(A-120258>>>0<2)break F;if(g=16777344,A-120310>>>0<2)break X}if(g=128,A>>>0<=120145)break Z;break X}if(A-120362>>>0<2|A-120414>>>0<2)break F;if(g=16777344,A-120466>>>0<2)break X;if(A-120540>>>0<31|A>>>0>120571|A>>>0<120486)break y;if(g=128,A-120514>>>0>=25)break Z;break X}if(A-120772>>>0<8|A-120746>>>0<25|A-120714>>>0<31|A-120688>>>0<25)break y;if(120597!=(0|A)&A>>>0<120629|A-120656>>>0<31)break y;if(g=128,A-120630>>>0<25)break X;break Z}break E;case 7:jA:{SA:{qA:{_A:{$A:{Ae:{ee:{ge:{re:{Ce:{ae:{Ie:{fe:{ie:{be:{se:{te:{ne:{ke:{oe:{Be:{ce:{if((0|(e=-256&A))<=11263){if((0|e)<=3583){if((0|e)<=1535){if(512==(0|e))break ce;if(768==(0|e))break Be;if(1280!=(0|e))break Z;if(g=4096,1369!=(0|A))break Z;break X}if(1536==(0|e))break oe;if(1792==(0|e))break ke;if(2304!=(0|e))break Z;if(g=4096,2417!=(0|A))break Z;break X}if((0|e)<=7167){if(3584==(0|e))break ne;if(6144==(0|e))break te;if(6656!=(0|e))break Z;if(g=8192,6823!=(0|A))break Z;break X}if(7168==(0|e))break se;if(7424==(0|e))break be;if(8192!=(0|e))break Z;if(g=16793600,!(e=A-8305|0))break X;if(14==(0|e))break ie;break fe}if((0|e)<=43263){if((0|e)<=40959){if(11264==(0|e))break Ie;if(11776==(0|e))break ae;if(12288!=(0|e))break Z;switch(g=8192,A-12293|0){case 1:case 2:case 3:case 4:case 5:case 6:case 7:case 8:case 9:case 10:case 11:case 12:case 13:case 14:case 15:case 16:case 17:case 18:case 19:case 20:case 21:case 22:case 23:case 24:case 25:case 26:case 27:case 28:case 29:case 30:case 31:case 32:case 33:case 34:case 35:case 36:case 37:case 38:case 39:case 40:case 41:case 42:case 43:break re;case 0:case 44:case 45:case 46:case 47:case 48:break X;default:break Ce}}if(40960==(0|e))break ge;if(42496==(0|e))break ee;if(42752!=(0|e))break Z;if(A-42775>>>0>=9)break Ae;break P}if((0|e)<=65279){if(43264==(0|e))break $A;if(43520==(0|e))break _A;if(43776!=(0|e))break Z;if(g=20480,43868!=(-4&A))break Z;break X}if(65280==(0|e))break qA;if(92928==(0|e))break SA;if(93952!=(0|e))break Z;if(g=4096,A-94099>>>0<13)break X;if(g=8192,94176!=(-2&A))break Z;break X}if(g=16797696,690==(0|A))break X;if(A-688>>>0<9)return U=0,20480;if(A-697>>>0<7)break P;if(704==(0|(e=-2&A)))return U=0,20480;if(g=4096,A-710>>>0<10)break X;if(g=12288,720==(0|e))break X;if(g=20480,A-736>>>0<5)break X;g=(A=748==(-3&A))>>>20|0,A<<=12;break Q}e=890==(0|A)?20480:0,e=(A=884==(0|A))?4096:e;break x}if(g=8192,1600==(0|A))break X;if(g=4096,A-1765>>>0>=2)break Z;break X}if(g=4096,2036==(-2&A))break X;if(g=8192,2042!=(0|A))break Z;break X}e=(A=!(A-3654&-129))>>>19|0,A<<=13;break u}if(g=8192,6211!=(0|A))break Z;break X}if(g=12288,7291==(0|A))break X;if(g=4096,A-7288>>>0>=6)break Z;break X}if(g=16797696,7522==(0|A))break X;if(g=20480,A-7468>>>0<63)break X;g=16384;be:switch(A-7588|0){default:if(7544==(0|A))break X;case 1:case 2:case 3:if(A-7579>>>0>=37)break Z;break X;case 0:case 4:break be}return U=0,16793600}return U=0,16384}if(g=16384,A-8336>>>0>=13)break Z;break X}e=(g=11389==(0|A))>>>18|0,g=(A=11388==(0|A))?16793600:g<<14;break c}if(g=-2147479552,11823!=(0|A))break Z;break X}if(A-12445>>>0<2)break X;if(12540==(0|A))break jA}if(A-12541>>>0>=2)break Z;break X}if(g=8192,40981!=(0|A))break Z;break X}if(42508==(0|A))break B;if(42623==(0|A))break P;if(g=20480,42652!=(-2&A))break Z;break X}if(42864==(0|A))return U=0,16384;if(42888==(0|A))break P;if(g=20480,43e3!=(-2&A))break Z;break X}g=(e=43494==(0|A))>>>19|0,e=(A=43471==(0|A))?8192:e<<13;break w}if(43632==(0|A))break B;if(43741==(0|A))break B;if(g=8192,A-43763>>>0>=2)break Z;break X}if(g=12288,65392==(0|A))break X;if(g=135168,65438!=(-2&A))break Z;break X}if(g=8192,92994==(-2&A))break X;break Z}return U=0,12288;case 8:g=128;jA:{SA:{qA:{_A:{$A:{Ae:{ee:{ge:{re:{if((0|(e=-256&A))<=12543){if((0|e)<=5887){if((0|e)<=3583){if(!e)break re;if(1536!=(0|e))break q;if(e=8388608,1651!=(0|A))break q;break U}if(3584==(0|e))break ge;if(4352!=(0|e))break q;if(g=4194304,A-4447>>>0>=2)break q;break j}if((0|e)<=8447){if(5888==(0|e))break ee;if(6400!=(0|e))break q;if((e=A-6581|0)>>>0>=6)break q;g=f[(e=81432+(e<<3)|0)>>2],r=f[e+4>>2];break j}if(8448==(0|e))break Ae;if(12288!=(0|e))break q;if(e=2048,12294!=(0|A))break q;break U}if((0|e)<=68863){if((0|e)<=63999){if(12544==(0|e))break $A;if(43520!=(0|e))break q;if((e=A-43701|0)>>>0<8)break SA;break S}if(64e3==(0|e))break jA;if(65280!=(0|e))break q;if(e=4194304,65440!=(0|A))break q;break U}if((0|e)<=100095){if(68864==(0|e))break _A;if(70400!=(0|e))break q;if(g=8192,70493!=(0|A))break q;break J}if(100096==(0|e))break qA;if(126464!=(0|e))break q;break j}r=(e=!(A-170&-17))>>>18|0,g=e<<14;break j}if(g=33554432,A-3648>>>0<5)break j;if(e=0,a=33554432,3759==(0|A))break U;if(A-3776>>>0>=5)break q;break j}if(g=8388608,A-6051>>>0>=2)break q;break j}if(A-8501>>>0>=4)break q;break j}if(e=4194304,12644!=(0|A))break q;break U}if(g=4096,68898!=(-2&A))break q;break j}if(g=2048,A-100333>>>0<5)break j;break q}if(g=33554432,!(211>>>e&1))break S;break j}if((e=A-64014|0)>>>0>=28)break q;g=f[(e=81480+(e<<3)|0)>>2],r=f[e+4>>2];break j;case 10:jA:{SA:{qA:{_A:{$A:{Ae:{if((0|(e=-256&A))<=119807){if((0|e)<=8447){if(!e)break Ae;if(768!=(0|e))break Z;switch(g=128,A-976|0){case 0:case 1:case 2:case 36:case 37:break X;default:break Z}}if(8448==(0|e))break $A;if(65280!=(0|e))break Z;if(g=256,A-65313>>>0>=6)break Z;break X}if((0|e)<=120319){if(119808==(0|e))break _A;if(120064!=(0|e))break Z;if(A>>>0>=120070)break qA;break y}if(120320==(0|e))break SA;if(120576!=(0|e))break Z;if(A-120772>>>0>=8)break jA;break y}if(g=768,A-65>>>0>=6)break Z;break X}g=128;$A:switch(A-8450|0){case 0:case 5:break X;default:break $A}if(A-8458>>>0<10)break y;if((e=A-8469|0)>>>0<20)break JA;if(8508==(-4&A))break X;break _}if(A-119982>>>0<12|A>>>0>120004|A-119977>>>0<4|A-119973>>>0<2)break y;if(119970==(0|A)|119966==(-2&A)|A-119808>>>0<85)break y;if(g=128,A-119894>>>0>=71)break Z;break X}if(A-120138>>>0<7|A>>>0>120145|120134==(0|A)|A-120128>>>0<5)break y;if(A-120123>>>0<4|A-120094>>>0<28|120070!=(0|A)&A>>>0<120075|A-120086>>>0<7)break y;if(g=128,A-120077>>>0>=8)break Z;break X}if(A-120540>>>0<31|A>>>0<120486)break y;if(g=128,A-120488>>>0>=25)break Z;break X}if(A-120714>>>0<31|A-120598>>>0<31)break y;if(g=128,A-120656>>>0<31)break X;break Z;case 11:jA:{SA:{qA:{_A:{$A:{Ae:{ee:{ge:{re:{Ce:{ae:{Ie:{fe:{ie:{be:{se:{te:{if((0|(e=-256&A))<=43263){if((0|e)<=3839){if((0|e)<=3071){if(2304==(0|e))break te;if(2816!=(0|e))break N;if((0|A)>3005)break be;if(2878!=(0|A))break se;return U=0,132096}if(3072==(0|e))break ie;if(3328!=(0|e))break N;switch(g=132096,A-3535|0){case 0:case 16:break X;case 1:case 2:case 3:case 4:case 5:case 6:case 7:case 8:case 9:case 10:case 11:case 12:case 13:case 14:case 15:break N;default:break fe}}if((0|e)<=6911){if(3840==(0|e))break Ie;if(4096!=(0|e))break N;if(g=1024,A-4139>>>0<2)break X;switch(A-4145|0){case 0:case 7:case 10:case 11:case 37:case 38:case 49:case 54:case 55:case 82:case 83:break X;case 86:case 87:case 88:case 89:case 90:case 91:case 94:case 105:case 106:break jA;default:break ae}}if(6912==(0|e))break Ce;if(7168==(0|e))break re;if(12288!=(0|e))break N;if(g=135168,12334!=(-2&A))break N;break X}if((0|e)<=70399){if((0|e)<=43775){if(43264==(0|e))break ge;if(43520!=(0|e))break N;return U=0,A-43643&-3?1024:4096}if(43776==(0|e))break ee;if(69888==(0|e))break Ae;if(70144!=(0|e))break N;if(g=4096,70197!=(0|A))break N;break X}if((0|e)<=70911){if(70400==(0|e))break $A;if(70656!=(0|e))break N;e=70845==(0|A)?132096:1024,e=(A=70832==(0|A))?132096:e;break x}if(70912==(0|e))break _A;if(71168==(0|e))break qA;if(119040!=(0|e))break N;switch(g=131072,A-119141|0){case 8:break P;case 1:break Z;case 0:break X;default:break SA}}e=2519==(0|A)?132096:1024,e=(A=2494==(0|A))?132096:e;break x}if(g=132096,2903!=(0|A))break N;break X}if(3006==(0|A))return U=0,132096;if(g=132096,3031!=(0|A))break N;break X}if((A=A-3266|0)>>>0>20)break N;if(g=132096,!(1<>>22|0,A<<=10;break Q}g=1024;Ce:switch(A-6965|0){default:if(6916==(0|A))break X;break;case 0:case 6:break X;case 1:case 2:case 3:case 4:case 5:break Ce}if(A-6973>>>0<5)break X;Ce:switch(A-6979|0){case 1:break P;case 0:break X;default:break Ce}if(7042==(0|A)|7073==(0|A)|7078==(-2&A))break X;if(7082==(0|A))break P;if(7143==(0|A)|A-7146>>>0<3)break X;g=(A=7150==(0|A))>>>22|0,A<<=10;break Q}e=7415==(0|A)?4096:1024,e=(A=7393==(0|A))?4096:e;break x}e=43456==(0|A)?4096:1024,e=(A=43347==(0|A))?4096:e;break x}if(g=4096,44012!=(0|A))break N;break X}if(g=4096,70080!=(0|A))break N;break X}g=132096;$A:switch(A-70462|0){case 0:case 25:break X;case 15:break $A;default:break N}break P}if(g=132096,71087!=(0|A))break N;break X}if(g=4096,71350!=(0|A))break N;break X}if(g=135168,A-119150>>>0<5)break X;break N}break P;case 12:e=(8419==(0|A))<<6,A=0;break u;case 13:jA:{SA:{qA:{_A:{$A:{Ae:{ee:{ge:{re:{Ce:{ae:{Ie:{fe:{ie:{be:{se:{te:{ne:{ke:{oe:{Be:{ce:{Qe:{Ge:{we:{Ee:{De:{ue:{le:{xe:{de:{me:{Me:{ve:{he:{pe:{Ye:{He:{Ne:{Pe:{Fe:{ye:{ze:{Oe:{Ze:{Ke:{We:{Xe:{Le:{Te:{Ve:{if((0|(e=-256&A))<=43775){if((0|e)<=5887){if((0|e)<=2559){if((0|e)<=1535){if(768==(0|e))break Ve;if(1024==(0|e))break Te;if(1280!=(0|e))break Z;if(A-1425>>>0>=17)break Le;break P}if((0|e)<=2047){if(1536==(0|e))break Xe;if(1792!=(0|e))break Z;if(g=1024,1809==(0|A))break X;if(1840!=(-16&A))break We;return U=0,5120}if(2048==(0|e))break Ke;if(2304!=(0|e))break Z;if(A>>>0>=2307)break Ze;break N}if((0|e)<=3583){if((0|e)<=3071){if(2560==(0|e))break Oe;if(2816!=(0|e))break Z;switch(g=1024,A-2876|0){case 0:break P;case 3:break X;case 1:case 2:break ye;default:break ze}}if(3072==(0|e))break Fe;if(3328!=(0|e))break Z;if(3328!=(0|(e=-2&A)))break Pe;break N}if((0|e)<=4095){if(3584==(0|e))break Ne;if(3840!=(0|e))break Z;if(3864!=(0|(e=-2&A)))break He;break P}if(4096==(0|e))break Ye;if(4864!=(0|e))break Z;if(g=1024,4959!=(0|A))break Z;break X}if((0|e)<=8191){if((0|e)<=6655){if(5888==(0|e))break pe;if(6144==(0|e))break he;if(6400!=(0|e))break Z;if((e=A-6432|0)>>>0<=18&&(g=1024,1<>>0>=3)break Z;break X}if((0|e)<=7167){if(6656==(0|e))break ve;if(6912!=(0|e))break Z;if(g=1024,6912==(-4&A))break X;if(6964!=(0|A))break Me;break P}if(7168==(0|e))break me;if(7424!=(0|e))break Z;if(g=4096,A-7620>>>0<12)break X;if(g=1024,A-7655>>>0<14)break X;if((A=A-7669|0)>>>0>=11)break Z;e=f[(A=82104+(A<<3)|0)>>2];break o}if((0|e)<=42495){if((0|e)<=11519){if(8192==(0|e))break de;if(11264!=(0|e))break Z;if(g=4096,A-11503>>>0>=3)break Z;break X}if(11520==(0|e))break xe;if(12288!=(0|e))break Z;if(A-12330>>>0>=4)break le;break P}if((0|e)<=43263){if(42496==(0|e))break ue;if(43008!=(0|e))break Z;if(A-43045>>>0>=2)break De;break N}if(43264==(0|e))break Ee;if(43520!=(0|e))break Z;switch(g=1024,A-43561|0){case 83:case 150:case 152:break P;case 0:case 1:case 2:case 3:case 4:case 5:case 8:case 9:case 12:case 13:case 26:case 35:case 135:case 137:case 138:case 139:case 142:case 143:case 149:break X;default:break we}}if((0|e)<=71423){if((0|e)<=69375){if((0|e)<=66047){if(43776==(0|e))break Ge;if(64256==(0|e))break Qe;if(65024!=(0|e))break Z;if(g=536870912,A-65024>>>0<15)break X;if(r=64,65039==(0|A))break X;if(g=4096,r=0,65056!=(-16&A))break Z;break X}if((0|e)<=68095){if(66048==(0|e))break ce;if(66304!=(0|e))break Z;if(g=1024,A-66422>>>0>=5)break Z;break X}if(68096==(0|e))break Be;if(68864!=(0|e))break Z;if(g=5120,68900!=(-4&A))break Z;break X}if((0|e)<=70399){if((0|e)<=69887){if(69376==(0|e))break oe;if(69632!=(0|e))break Z;if(A-69688>>>0>=14)break ke;break N}if(69888==(0|e))break ne;if(70144!=(0|e))break Z;if(g=1024,A-70191>>>0<3)break X;switch(A-70196|0){case 2:break P;case 0:case 3:case 10:break X;case 1:case 4:case 5:case 6:case 7:case 8:case 9:break se;default:break te}}if((0|e)<=70911){if(70400==(0|e))break be;if(70656!=(0|e))break Z;if(70712!=(-8&A))break ie;break N}if(70912==(0|e))break fe;if(71168!=(0|e))break Z;if(g=1024,A-71219>>>0<8)break X;switch(A-71229|0){case 2:break P;case 0:case 3:break X;case 1:break ae;default:break Ie}}if((0|e)<=92927){if((0|e)<=72703){if(71424==(0|e))break KA;if(71680==(0|e))break Ce;if(72192!=(0|e))break Z;if(A-72193>>>0>=10)break re;break N}if((0|e)<=73215){if(72704==(0|e))break ge;if(72960!=(0|e))break Z;if((e=A-73009|0)>>>0<19)break OA;break $}if(73216==(0|e))break ee;if(92672!=(0|e))break Z;if(g=4096,A-92912>>>0>=5)break Z;break X}if((0|e)<=122879){if((0|e)<=113663){if(92928==(0|e))break Ae;if(93952!=(0|e))break Z;if(g=4096,A-94095>>>0>=4)break Z;break X}if(113664==(0|e))break $A;if(119040!=(0|e))break Z;switch(g=4096,A-119143|0){case 0:case 1:case 2:case 20:case 21:case 22:case 23:case 24:case 25:case 26:case 27:case 30:case 31:case 32:case 33:case 34:case 35:case 36:case 67:case 68:case 69:case 70:break X;default:break Z}}if((0|e)<=125183){if(122880==(0|e))break _A;if(124928!=(0|e))break Z;if(g=4096,A-125136>>>0>=7)break Z;break X}if(125184==(0|e))break qA;if(917760!=(0|e))break Z;if(g=536870912,A-917760>>>0>=240)break Z;break X}if(A-768>>>0<69)break P;if(g=21504,837==(0|A))break X;if(A-838>>>0<9)break P;if(g=4194304,847==(0|A))break X;if(848==(-8&A))break P;if(g=4096,A-861>>>0>=6)break Z;break X}if(g=4096,A-1155>>>0>=5)break Z;break X}if(g=4096,A-1443>>>0<13)break X;if(g=5120,A-1456>>>0<14)break X;if((A=A-1471|0)>>>0>=9)break Z;e=f[(A=81944+(A<<3)|0)>>2];break o}if(A-1552>>>0<11)break N;if(g=5120,A-1611>>>0<8)break X;if(A-1619>>>0<4)break N;Xe:switch(A-1623|0){case 1:break P;case 0:break X;case 2:case 3:case 4:case 5:case 6:case 7:case 8:case 25:case 127:case 128:case 129:case 130:case 131:case 132:case 133:break N;default:break Xe}if(g=4096,A-1759>>>0<2)break X;if((e=A-1761|0)>>>0<8)break VA;break AA}if(A-1856>>>0<11)break P;if(g=5120,A-1958>>>0<11)break X;if(g=4096,A-2027>>>0>=9)break Z;break X}g=1024;Ke:switch((-2&A)-2070|0){case 2:break P;case 0:break X;default:break Ke}if(A-2260>>>0<12|A-2089>>>0<4|A-2075>>>0<9|A-2085>>>0<3)break N;if(A-2275>>>0<7)return U=0,5120;if(g=4096,A-2282>>>0<6)break X;if(g=5120,A-2288>>>0<15)break X;if(g=1024,2303!=(0|A))break Z;break X}g=1024;Ze:switch(A-2362|0){case 2:break P;case 0:break X;default:break Ze}if(A-2369>>>0<8)break N;g=4096;Ze:switch(A-2381|0){case 0:case 4:case 5:case 6:case 7:case 111:break X;case 8:case 9:case 10:case 21:case 22:case 52:break N;default:break Ze}if(A-2497>>>0<4)break N;if(2509==(0|A))break X;if(g=1024,2530!=(-2&A))break Z;break X}if(A-2561>>>0<2)break N;g=4096;Oe:switch(A-2620|0){case 0:case 17:case 128:case 145:break X;case 5:case 6:case 11:case 12:case 15:case 16:case 21:case 52:case 53:case 57:case 69:case 70:case 133:case 134:case 135:case 136:case 137:case 139:case 140:case 166:case 167:case 190:case 191:case 192:break N;default:break Oe}if(A-2813>>>0>=3)break Z;break X}if(2817==(0|A))break N}if(A-2881>>>0<4)break N;g=4096;ye:switch(A-2893|0){case 0:break X;case 9:break N;default:break ye}if(2914==(-2&A))break N;g=1024;ye:switch(A-3008|0){default:if(2946!=(0|A))break Z;break X;case 1:case 2:case 3:case 4:case 5:case 6:case 7:case 8:case 9:case 10:case 11:case 12:break Z;case 0:break X;case 13:break ye}break P}g=1024;Fe:switch(A-3072|0){case 77:case 188:case 205:break P;case 0:case 62:case 63:case 64:case 70:case 71:case 72:case 74:case 75:case 76:case 85:case 86:case 98:case 99:case 129:case 191:case 198:case 204:break X;default:break Fe}if(3298!=(-2&A))break Z;break X}if(g=4096,A-3387>>>0<2)break X;if(A-3393>>>0<4)break N;if(3405==(0|A))break X;if(3426==(0|e))break N;switch(A-3530|0){case 0:break X;case 8:case 9:case 10:case 12:break N;default:break Z}}if((e=A-3633|0)>>>0<10)break TA;break eA}if((g=A-3893|0)>>>0>4|!(1<>>22|0,g=(A=4237==(0|A))?4096:g<<10;break c}g=1024;pe:{Ye:switch(A-5906|0){case 0:case 1:case 32:case 33:break X;case 2:case 3:case 4:case 5:case 6:case 7:case 8:case 9:case 10:case 11:case 12:case 13:case 14:case 15:case 16:case 17:case 18:case 19:case 20:case 21:case 22:case 23:case 24:case 25:case 26:case 27:case 28:case 29:case 30:case 31:break pe;default:break Ye}switch(A-5970|0){case 0:case 1:case 32:case 33:break X;default:break pe}}if(g=4194304,6068==(-2&A))break X;if((e=A-6071|0)>>>0<16)break LA;break rA}if(g=536870912,A-6155>>>0<3)break X;if(g=67109888,A-6277>>>0<2)break X;if(g=1024,6313!=(0|A))break Z;break X}if(A-6679>>>0<2)break N;g=1024;ve:switch(A-6683|0){case 0:case 59:case 61:case 62:case 63:case 64:case 65:case 66:case 67:case 71:case 74:case 75:case 76:case 77:case 78:case 79:case 80:case 81:case 88:case 89:break X;default:break ve}if(g=4096,A-6832>>>0<14)break X;if((A=A-6773|0)>>>0>=11)break Z;e=f[(A=82016+(A<<3)|0)>>2];break o}if(A-6966>>>0<5)break X;Me:switch(A-6972|0){case 0:case 6:break X;default:break Me}if(A-7019>>>0<9)break P;switch(A-7040|0){case 43:break P;case 0:case 1:case 34:case 35:case 36:case 37:case 40:case 41:case 44:case 45:case 104:case 105:case 109:case 111:case 112:case 113:break X;default:break Z}}if(g=1024,A-7212>>>0<8)break X;g=12288;me:switch(A-7222|0){case 1:break P;case 0:break X;default:break me}if(A-7380>>>0<13)break P;g=4096;me:switch(A-7376|0){case 0:case 1:case 2:case 18:case 19:case 20:case 21:case 22:case 23:case 24:case 29:case 36:break X;default:break me}if(7416!=(-2&A))break Z;break X}if(g=128,A-8400>>>0<13)break X;if((A=A-8417|0)>>>0>=15)break Z;e=f[(A=82192+(A<<3)|0)>>2];break o}if(g=1024,11744!=(-32&A))break Z;break X}if(g=4096,A-12441>>>0>=2)break Z;break X}if(42607==(0|A))break P;if(A-42612>>>0<8)break N;if(42620==(0|(A&=-2)))break P;if(42654==(0|A))break jA;if(g=4096,42736!=(0|A))break Z;break X}g=4096;De:switch(A-43204|0){case 0:break X;case 1:break N;default:break De}if(A-43232>>>0>=18)break Z;break X}if(A-43302>>>0<5)break N;if(A-43307>>>0<3)break P;if(A-43335>>>0<11|A-43392>>>0<3)break N;if(g=4096,43443==(0|A))break X;if(g=1024,A-43446>>>0<4)break X;r=(e=43493==(0|A))>>>20|0,e=(A=43452==(0|A))?1024:e<<12;break l}if(43756==(-2&A))break X;if(g=4096,43766!=(0|A))break Z;break X}g=1024;Ge:switch(A-44005|0){case 0:case 3:break X;case 8:break Ge;default:break Z}break P}if(g=5120,64286!=(0|A))break Z;break X}if(g=4096,66272!=(0|A))break Z;break X}if(68108==(-4&A))break N;if((e=A-68097|0)>>>0<6)break XA;break CA}if(g=4096,A-69446>>>0>=11)break Z;break X}if(A-69811>>>0<4)break N;if(g=1024,69633==(0|A))break X;if(g=4096,A-69817>>>0>=2)break Z;break X}g=1024;ne:switch(A-69888|0){case 51:case 52:case 115:break P;case 0:case 1:case 2:case 39:case 40:case 41:case 42:case 43:case 45:case 46:case 47:case 48:case 49:case 50:break X;default:break ne}if(70016==(-2&A)|A-70070>>>0<9)break X;if(g=4096,A-70090>>>0>=3)break Z;break X}if(70367==(0|A))break X}if(A-70371>>>0<6)break X;if(g=4096,A-70377>>>0>=2)break Z;break X}if(70400==(-2&A))break N;g=4096;be:switch(A-70460|0){case 0:case 42:case 43:case 44:case 45:case 46:case 47:case 48:case 52:case 53:case 54:case 55:case 56:break X;case 4:break be;default:break Z}break N}if(70722==(0|A))break P;if(A-70723>>>0<2)break N;if((e=A-70835|0)>>>0<=13)break SA;break aA}if((e=A-71090|0)>>>0<12)break WA;break IA}switch(A-71339|0){case 0:case 2:break X;default:break ae}}if(A-71344>>>0<6)break X;if(g=4096,71351!=(0|A))break Z;break X}if(g=1024,A-71727>>>0<10)break X;if(g=4096,A-71737>>>0>=2)break Z;break X}if(72244==(0|A))break P;if(A-72245>>>0<10)break N;if(g=4096,72263==(0|A))break X;if(A-72273>>>0<11)break N;if(g=1024,A-72330>>>0<13)break X;g=(e=72345==(0|A))>>>20|0,e=(A=72344==(0|A))?8192:e<<12;break w}if((e=A-72752|0)>>>0<16)break ZA;break fA}if(g=1024,A-73459>>>0>=2)break Z;break X}if(g=1024,A-92976>>>0>=7)break Z;break X}if(g=1024,113822!=(0|A))break Z;break X}if(g=1024,A-122888>>>0<17)break X;if((A=A-122880|0)>>>0>=43)break Z;e=f[(A=82816+(A<<3)|0)>>2];break o}if(g=12288,A-125252>>>0<3)break X;if(g=1024,125255==(0|A))break X;if(g=4096,A-125256>>>0<3)break X;break Z}if(!(1<>>0>=10)break jA;break X}if(g=256,A-65296>>>0>=10)break jA;break X}if(g=128,A-120782>>>0<50)break X}break Z;case 15:jA:{if(12288!=(0|(e=-256&A))){if(8448!=(0|e))break jA;e=(A=8560==(0|(g=-16&A)))>>>18|0,r=A<<14,g=(A=8544==(0|g))?32768:r;break c}if(A-12321>>>0<9)return U=0,2048;if(A-12344>>>0<3)return U=0,2048;if(g=2048,12295==(0|A))break X}break Z;case 16:jA:{SA:{qA:{if((0|(e=-256&A))<=9215){if(4864==(0|e))break qA;if(6400!=(0|e))break jA;if(g=134217728,6618!=(0|A))break jA;break X}if(9216==(0|e))break SA;if(127232!=(0|e))break jA;if(g=0,r=-2147483648,127232==(0|A))break X;if(r=268435456,A-127233>>>0>=10)break jA;break X}if(g=134217728,A-4969>>>0>=9)break jA;break X}if(g=0,r=-2147483648,A-9352>>>0<20)break X}break Z;case 17:r=(A=8256==(0|A))>>>25|0,A<<=7;break G;case 18:jA:{SA:{qA:{_A:{if((0|(e=-256&A))<=11775){if((0|e)<=6143){if(g=-2147483624,!e)break X;if(1280!=(0|e))break jA;if(g=24,1418!=(0|A))break jA;break X}if(6144==(0|e))break _A;if(8192!=(0|e))break jA;if(g=-2147483624,8208==(-2&A))break X;A=A-8211>>>0<2,e=-2147483640;break k}if((0|e)<=65023){if(11776==(0|e))break qA;if(12288!=(0|e))break jA;e=12336==(0|A),g=(A=12316==(0|A))||e?-2147483640:8,U=A?0:e?130:0;break n}if(65024==(0|e))break SA;if(65280!=(0|e))break jA;if(g=24,65293!=(0|A))break jA;break X}if(g=24,6150!=(0|A))break jA;break X}if(g=-2147483624,11799==(0|A))break X;A=11834==(-2&A),e=-2147483640;break k}if(g=8,r=8388608,A-65073>>>0<2)break X;if(g=152,r=0,65123==(0|A))break X}return U=0,8;case 19:jA:{SA:{qA:{_A:{$A:{Ae:{if((0|(e=-256&A))<=11775){if((0|e)<=8959){if(e)break Ae;break O}if(8960==(0|e))break $A;if(9984==(0|e))break _A;if(10496!=(0|e))break Z;break H}if((0|e)<=64767){if(g=-2147483648,11776==(0|e))break X;if(12288!=(0|e))break Z;switch(g=-2147483616,A-12301|0){case 0:case 2:break X;default:break qA}}if(64768==(0|e))break SA;if(65024==(0|e))break jA;if(65280!=(0|e))break Z;if(g=32,65379==(0|A))break X;break Z}if(8192!=(0|e))break Z;Ae:switch(A-8318|0){default:if(g=-2147483648,8262!=(0|A))break Z;break X;case 1:case 2:case 3:case 4:case 5:case 6:case 7:case 8:case 9:case 10:case 11:case 12:case 13:case 14:case 15:break Z;case 0:case 16:break Ae}break y}g=-2147483520;$A:switch(A-8969|0){case 1:break Z;case 0:case 2:break X;default:break $A}if(9002!=(0|A))break Z;return U=0,-2139095040}if(g=-2147483520,10182==(0|A))break X;break Y}return U=0,12318==(-2&A)?-2147483616:-2147483648}if(g=-2147483648,64830!=(0|A))break Z;break X}r=(A=!(A-65090&-3))>>>27|0,A<<=5;break G;case 20:case 21:if(!(A&=-256))break t;if(g=-2147483616,8192==(0|A))break X;break K;case 22:jA:{SA:{qA:{_A:{$A:{Ae:{ee:{ge:{re:{Ce:{ae:{Ie:{fe:{ie:{be:{se:{te:{ne:{ke:{oe:{Be:{ce:{Qe:{Ge:{we:{Ee:{De:{ue:{le:{xe:{de:{me:{Me:{ve:{he:{pe:{Ye:{He:{Ne:{Pe:{Fe:{ye:{ze:{Oe:{Ze:{Ke:{We:{Xe:{if((0|(e=-256&A))<=43519){if((0|e)<=5887){if((0|e)<=2303){if((0|e)<=1535){if(!e)break Xe;if(768==(0|e))break We;if(1280!=(0|e))break Z;if(g=0,r=538968064,A-1371>>>0<2)break X;switch(r=268435456,A-1373|0){case 0:break X;case 1:break Ze;default:break Ke}}if(1536==(0|e))break ze;if(1792==(0|e))break ye;if(2048!=(0|e))break Z;if((e=A-2103|0)>>>0<8)break yA;break iA}if((0|e)<=3839){if(2304==(0|e))break Fe;if(3328==(0|e))break Pe;if(3584!=(0|e))break Z;if(g=64,3674!=(-2&A))break Z;break X}if((0|e)<=4863){if(3840==(0|e))break Ne;if(4096!=(0|e))break Z;if(g=268435520,4170==(-2&A))break X;if(g=0,r=16777216,4347!=(0|A))break Z;break X}if(4864==(0|e))break He;if(5632!=(0|e))break Z;switch(g=64,A-5741|0){case 1:break C;case 0:break X;default:break Ye}}if((0|e)<=11263){if((0|e)<=6655){if(5888==(0|e))break pe;if(6144==(0|e))break PA;if(6400!=(0|e))break Z;e=6469==(0|A),g=(A=6468==(0|A))||e?268435520:0,U=A?536870912:e?1073741824:0;break n}if((0|e)<=7167){if(6656==(0|e))break he;if(6912!=(0|e))break Z;if(7002!=(0|(e=-2&A)))break ve;break p}if(7168==(0|e))break Me;if(8192!=(0|e))break Z;switch(g=-2147483520,A-8214|0){case 1:break O;case 0:break X;case 16:break me;default:break de}}if((0|e)<=41983){if(11264==(0|e))break xe;if(11776==(0|e))break le;if(12288!=(0|e))break Z;switch(g=-2147483584,r=272629760,A-12289|0){case 2:break O;case 0:break X;case 1:break De;default:break ue}}if((0|e)<=43007){if(41984==(0|e))break Ee;if(42496!=(0|e))break Z;switch(g=64,r=268435456,A-42739|0){case 4:break a;case 0:break C;case 3:break g;case 2:break X;case 1:break Ge;default:break we}}if(43008==(0|e))break Qe;if(43264!=(0|e))break Z;switch(g=4096,A-43310|0){case 0:break X;case 1:break p;default:break ce}}if((0|e)<=70655){if((0|e)<=67839){if((0|e)<=65279){if(43520==(0|e))break Be;if(43776==(0|e))break oe;if(65024!=(0|e))break Z;if(g=0,r=268435456,65040==(0|(a=-2&A)))break X;if((e=A-65042|0)>>>0<8)break HA;break bA}if(65280==(0|e))break ke;if(66304==(0|e))break ne;if(67584!=(0|e))break Z;if(g=64,67671!=(0|A))break Z;break X}if((0|e)<=69375){if(67840==(0|e))break te;if(68096==(0|e))break se;if(68352!=(0|e))break Z;switch(g=64,A-68410|0){case 0:case 1:case 2:case 3:case 4:case 5:case 95:case 96:case 97:case 98:break X;default:break Z}}if((0|e)<=69887){if(69376==(0|e))break be;if(69632!=(0|e))break Z;if(A-69703>>>0>=2)break ie;break p}if(69888==(0|e))break fe;if(70144!=(0|e))break Z;if((e=A-70200|0)>>>0<=4)break Ie;if(70313!=(0|A))break Z;break p}if((0|e)<=74751){if((0|e)<=71423){if(70656==(0|e))break ae;if(70912==(0|e))break Ce;if(71168!=(0|e))break Z;if(g=268435520,A-71233>>>0>=2)break Z;break X}if((0|e)<=72703){if(71424==(0|e))break re;if(72192!=(0|e))break Z;if(A-72258>>>0>=2)break ge;break p}if(72704==(0|e))break ee;if(73216!=(0|e))break Z;if(g=268435520,A-73463>>>0>=2)break Z;break X}if((0|e)<=93695){if(74752==(0|e))break Ae;if(92672==(0|e))break _A;if(92928!=(0|e))break Z;if(g=268435520,A-92983>>>0<2)break X;e=92996==(0|A)?268435520:0,e=(A=92985==(0|A))?64:e;break x}if((0|e)<=121343){if(93696==(0|e))break $A;if(113664!=(0|e))break Z;if(g=268435520,r=-2147483648,113823!=(0|A))break Z;break X}if(121344==(0|e))break YA;if(125184!=(0|e))break Z;g=(125279==(0|A))<<30,e=0,U=(A=125278==(0|A))?536870912:g;break b}g=-1879048128,r=536870912;Xe:{Le:{Te:{Ve:{Je:switch(A-33|0){default:switch(A-183|0){case 1:case 2:case 3:case 4:case 5:case 6:case 7:break Xe;case 8:break Te;case 0:break Ve;default:break Le}case 2:case 9:U=66;break f;case 11:return U=268435456,-2147483584;case 13:return U=-2147483648,-1879048128;case 25:return U=134217728,-2147483584;case 26:return U=67108864,-2147483584;case 1:case 6:break t;case 0:break X;case 3:case 4:case 5:case 7:case 8:case 10:case 12:case 14:case 15:case 16:case 17:case 18:case 19:case 20:case 21:case 22:case 23:case 24:case 27:case 28:case 29:break Xe;case 30:break Je}return U=1073741824,-1879048128}return U=0,134230016}U=1078984704;break f}if(161==(0|A))break sA}break O}e=903==(0|A),g=(A=894==(0|A))?64:e?134217792:0,U=A?1073741824:e?67108864:0;break n}if(1417==(0|A))break Oe;if(1475!=(0|A))break Z;break h}U=1075838976;break I}U=-2143289344;break r}g=64,r=268435456;ze:switch(A-1548|0){case 15:break g;case 1:case 2:case 3:case 4:case 5:case 6:case 7:case 8:case 9:case 10:case 11:case 12:case 13:case 14:case 16:case 17:break Z;case 0:break X;case 19:break ze;case 18:break p;default:break qA}break a}if((e=A-1792|0)>>>0<6)break zA;if(g=64,r=134217728,1798==(-2&A))break X;r=67108864;ye:{ze:{Oe:switch(A-1800|0){default:switch(A-2040|0){case 1:break ye;case 0:break ze;default:break Z}case 3:break Z;case 0:break X;case 1:break Oe;case 2:case 4:break h}U=1073741824;break e}U=268435456;break e}U=536870912;break r}e=2405==(0|A),g=(A=2404==(0|A))||e?268435520:0,U=A?-2143289344:e?16777216:0;break n}if(g=0,r=-2143289344,3572!=(0|A))break Z;break X}g=64;Ne:switch(A-3848|0){case 5:U=-2143289344;break e;case 6:U=16777216;break e;case 0:break X;default:break Ne}if(A-3854>>>0<5)break X;if(g=0,r=268435456,3860!=(0|A))break Z;break X}if((e=A-4961|0)>>>0<4)break FA;if(g=64,r=134217728,A-4965>>>0<2)break X;e=4968==(0|A),g=(A=4967==(0|A))||e?268435520:0,U=A?1073741824:e?16777216:0;break n}if(A-5867>>>0>=3)break Z;break X}if(g=268435520,A-5941>>>0<2)break X;if((A=A-6100|0)>>>0>=7)break Z;e=f[(A=83240+(A<<3)|0)>>2];break o}if(g=268435520,6824!=(-4&A))break Z;break X}if(g=64,7005==(0|A))break X;if(g=268435520,7006!=(0|e))break Z;break X}if(A-7227>>>0<2)break p;if(g=64,A-7229>>>0<3)break X;if(g=268435520,7294==(-2&A))break X;if(g=4096,7379!=(0|A))break Z;break X}U=33554432;break f}if(8224==(-8&A))break O;if(A-8242>>>0<3)break X;if(A-8240>>>0<9)break O;if((e=A-8251|0)>>>0<21)break NA;break tA}if(11513==(0|A)){U=-2147483648;break I}if(g=0,r=1073741824,11514==(-2&A))break X;if(r=-2147483648,11518!=(0|A))break Z;break X}g=-1879048128;le:switch(A-11822|0){case 4:case 6:U=268435456;break f;case 5:U=-2147483648;break f;case 7:U=67108864;break f;case 14:return U=-2147483648,-1879048128;case 19:return U=268435456,-2147483584;case 30:case 32:return U=0,-2147483584;case 0:break X;default:break le}break O}if(12349==(0|A))break v;if(12539!=(0|A))break Z;return U=0,16}return U=-2143289344,-1879048128}e=42239==(0|A),g=(A=42238==(0|A))?64:e?268435520:0,U=A?268435456:e?-2147483648:0;break n}we:switch(A-42509|0){case 1:break C;case 0:break X;case 2:break we;default:break Z}break a}U=134217728;break e}if(A-43126>>>0<2)break p;if(g=268435520,A-43214>>>0>=2)break Z;break X}if(43463==(0|A))break h;if(g=268435520,43464!=(-2&A))break Z;break X}if(A-43613>>>0<3)break p;if(g=64,43743==(0|A))break X;if(g=268435520,43760!=(-2&A))break Z;break X}if(g=268435520,44011!=(0|A))break Z;break X}g=268435520,r=541065216;ke:switch(A-65281|0){case 1:case 6:return U=0,32;case 11:U=272629760;break e;case 13:U=-2143289344;break r;case 100:return U=0,16;case 25:U=138412032;break e;case 26:U=71303168;break e;case 30:U=1077936128;break r;case 96:break C;case 59:break y;case 0:break X;case 99:break ke;default:break Z}U=268435456;break e}g=(e=66512==(0|A))>>>26|0,e=(A=66463==(0|A))?64:e<<6;break w}if(g=64,67871!=(0|A))break Z;break X}if(g=268435520,68182==(-2&A))break X;if(g=64,A-68336>>>0>=6)break Z;break X}if(g=268435520,A-69461>>>0>=5)break Z;break X}if(g=64,A-69705>>>0<5)break X;if(g=268435520,A-69822>>>0>=4)break Z;break X}if(A-69953>>>0<2)break p;if((e=A-70085|0)>>>0<=26)break SA;break nA}if(2!=(0|e))break p;break h}if(g=268435520,A-70731>>>0<2)break X;r=(e=70747==(0|A))>>>26|0,e=(A=70733==(0|A))?64:e<<6,U=A?268435456:r;break b}g=268435520;Ce:switch((-2&A)-71106|0){case 0:break X;case 2:break jA;default:break Ce}if(g=8192,A-71110>>>0<3)break X;if(g=268435520,A-71113>>>0>=15)break Z;break X}if(g=268435520,A-71484>>>0>=3)break Z;break X}if(g=268435520,A-72347>>>0<2)break X;if(g=64,A-72353>>>0>=2)break Z;break X}if(g=268435520,A-72769>>>0<2)break X;g=(e=72817==(0|A))>>>26|0,e=(A=72771==(0|A))?64:e<<6;break w}if(g=64,r=134217728,A-74865>>>0<2)break X;if(r=0,A-74864>>>0>=5)break Z;break X}e=93848==(0|A)?268435520:0,e=(A=93847==(0|A))?64:e;break x}if(g=268435520,92782==(-2&A))break X;if(r=-2147483648,92917!=(0|A))break Z;break X}if(1748==(0|A))break C;break Z}if(!(1<>>0<=17&&(g=-2147483616,1<>>27|0,A<<=5;break Q;case 24:g=(A=A>>>0<256)>>>1|0,A<<=31;break Q;case 25:jA:{SA:{qA:{_A:{$A:{Ae:{ee:{ge:{if((0|(e=-256&A))<=12287){if((0|e)<=767){if(!e)break ge;if(512!=(0|e))break jA;if(A-751>>>0>=17)break ee;break P}if(768==(0|e))break Ae;if(7936!=(0|e))break jA;switch(g=4096,A-8125|0){case 0:case 2:case 3:case 4:case 16:case 17:case 18:case 32:case 33:case 34:case 48:case 49:case 50:case 64:case 65:break X;default:break jA}}if((0|e)<=43775){if(12288==(0|e))break $A;if(42752!=(0|e))break jA;if(g=4096,42784!=(-2&A))break jA;break X}if(43776==(0|e))break _A;if(65280==(0|e))break qA;if(g=0,r=78,127744!=(0|e))break jA;break X}ge:switch(A-168|0){default:g=-2147479424;re:switch(A-94|0){case 0:break X;case 2:break re;default:break jA}return U=0,-2147479552;case 0:case 7:break P;case 1:case 2:case 3:case 4:case 5:case 6:case 8:case 9:case 10:case 11:case 13:case 14:case 15:break jA;case 12:case 16:break ge}break P}if(749==(0|A)|A-741>>>0<7)break P;if(A-706>>>0>=4)break SA;break P}if((A=A-885|0)>>>0>16)break jA;if(g=4096,!(1<>>0>=2)break jA;break X}if(g=4096,43867!=(0|A))break jA;break X}g=4224;qA:switch(A-65342|0){default:if(65507!=(0|A))break jA;break;case 0:break X;case 1:break jA;case 2:break qA}break P}if(g=4096,A-722>>>0<14)break X}break Z;case 26:jA:{SA:{qA:{_A:{$A:{Ae:{if((0|(e=-256&A))<=9471){if((0|e)<=8447){if(e)break jA;break O}if(8448==(0|e))break Ae;if(8704==(0|e))break $A;if(g=-2147483648,8960!=(0|e))break Z;break X}if((0|e)<=10495){if(9472==(0|e))break _A;if(9728==(0|e))break qA;if(9984==(0|e))break O;break Z}if(10496==(0|e))break SA;if(10752==(0|e))break O;if(g=-2147483648,11008!=(0|e))break Z;break X}if(8472==(0|A))return U=0,67108864;if(8596==(0|A))break M;if(g=-2147483648,A>>>0<=8591)break Z;break X}if(g=-2147483640,8722==(0|A))break X;e=-2147483648,U=(A=A-8942>>>0<4)?33554432:0;break b}if(g=-2147483648,r=130,A-9723>>>0<2)break X;e=-2147483648,U=(A=A-9725>>>0<2)?134:0;break b}if(g=-2147483648,r=128,9839==(0|A))break X;break O}if(g=-2147483648,r=130,10548==(-2&A))break X;e=-2147483648,U=(A=10626==(0|A))?134217728:0;break b}if(8192==(0|e))break kA;break Z;case 27:jA:{SA:{qA:{_A:{$A:{Ae:{ee:{ge:{re:{Ce:{ae:{Ie:{fe:{ie:{be:{se:{te:{ne:{ke:{oe:{Be:{ce:{Qe:{Ge:{if((0|(e=-256&A))<=12287){if((0|e)<=9727){if((0|e)<=8959){if(!e)break Ge;if(8448!=(0|e))break Z;switch(g=0,r=130,A-8482|0){case 7:break y;case 0:break X;case 1:case 2:case 3:case 4:case 5:case 6:case 8:case 9:case 10:case 11:break oe;case 12:break ce;default:break Qe}}if(8960==(0|e))break ke;if(9216==(0|e))break ne;if(9472!=(0|e))break Z;switch(g=-2147483520,(-2&A)-9632|0){case 0:break X;case 10:break M;default:break te}}if((0|e)<=11007){if(9728==(0|e))break se;if(9984==(0|e))break be;if(g=-2147483648,10240!=(0|e))break Z;break X}if(11008==(0|e))break ie;if(11776==(0|e))break fe;if(12032!=(0|e))break Z;if(g=1048576,A>>>0<12246)break X;switch(g=262144,(-2&A)-12272|0){case 0:break X;case 2:break jA;default:break Ie}}if((0|e)<=127999){if((0|e)<=127231){if(12288==(0|e))break ae;if(12800==(0|e))break Ce;if(126976!=(0|e))break Z;e=127183==(0|A),g=0,U=(A=126980==(0|A))||e?134:128;break n}if(127232==(0|e))break re;if(127488==(0|e))break ge;if(127744!=(0|e))break Z;if(A>>>0>=127777)break ee;break m}if((0|e)<=128767){if(128e3==(0|e))break Ae;if(128256==(0|e))break $A;if(128512!=(0|e))break Z;if((e=A-128581|0)>>>0<11)break DA;break oA}if(128768==(0|e))break _A;if(129280==(0|e))break qA;if(129536!=(0|e))break Z;U=128;break I}e=174==(0|A),g=-2147483648,U=(A=169==(0|A))||e?130:0;break n}switch(A-8616|0){case 0:break O;case 1:case 2:break Be;default:break oe}}return U=0,67108864}return U=130,-2147483520}if(g=-2147483520,A-8597>>>0<5)break X;if(A-8604>>>0<18)break H;if((e=A-8624|0)>>>0<8)break pA;break BA}if(A>>>0<8968)break O;if(8986==(0|(e=-2&A)))break i;if(A-8972>>>0<20|A-8994>>>0<6)break O;if(9e3==(0|A))break M;if(A-9003>>>0<81)break O;if(g=-2147483648,r=128,9096==(0|A))break X;if(A-9085>>>0<30)break O;if(g=-2147483520,r=0,9140==(0|e))break X;ke:switch(A-9143|0){case 0:case 25:break X;case 24:break M;default:break ke}if(A-9140>>>0<40)break O;if(9186==(0|A))break X;if((e=A-9193|0)>>>0<4)break i;g=-2147483648,r=134;ke:switch(A-9200|0){case 0:case 3:break X;default:break ke}if(e>>>0<11)break M;if(r=130,A-9208>>>0<3)break X;if(r=0,A>>>0<=9186)break Z;break X}if(g=-2147483648,A-9216>>>0<75)break X;if(g=33792,r=130,9410==(0|A))break X;if(r=0,A-9398>>>0<26)break X;if(g=17408,A-9424>>>0>=26)break Z;break X}if(A-9646>>>0<8)break H;if(r=130,9654==(0|A))break X;if(9660==(-4&A))break H;te:switch(A-9664|0){case 0:break X;case 6:case 7:case 10:case 11:case 15:case 16:case 17:case 18:case 19:case 34:case 36:break H;default:break te}return U=0,A-9703>>>0<6?-2147483520:-2147483648}se:switch((-16&A)-9728>>>4|0){case 0:if(A>>>0<9733)break M;g=-2147483520,r=128;te:switch(A-9733|0){case 0:break X;case 9:break te;case 1:break H;default:break cA}break M;case 2:if((e=A-9760|0)>>>0<11)break vA;if(g=-2147483648,r=130,A>>>0<=9773)break cA;break X;case 3:if(g=-2147483648,r=130,A-9784>>>0>=3)break cA;break X;case 4:g=-2147483520,r=130;te:switch(A-9792|0){case 0:case 2:break X;default:break te}if(g=-2147483648,r=134,A>>>0<=9799)break cA;break X;case 5:if(g=-2147483648,r=134,A>>>0<9812)break X;if(r=130,9823!=(0|A))break cA;break X;case 6:if(9734==(0|A))break H;if(9824==(0|A))return U=130,-2147483520;if(g=-2147483520,r=128,A-9825>>>0<2)break X;if((e=A-9827|0)>>>0<6)break MA;break QA;case 8:if(g=-2147483648,A>>>0<=9861)break cA;break X;case 10:g=-2147483648,r=130;te:switch(A-9888|0){case 1:break i;case 0:break X;default:break te}if(r=134,9898!=(-2&A))break cA;break X;case 11:if(g=-2147483648,r=130,9904==(-2&A))break X;if(r=134,A-9917>>>0>=2)break cA;break X;case 12:if(9924==(-2&A))break i;g=-2147483648,r=130;te:switch(A-9928|0){case 0:case 7:break X;case 6:break te;default:break cA}break i;case 14:e=9962==(0|A),g=-2147483648,U=(A=9961==(0|A))?130:e?134:128;break n;case 13:break xA;case 9:break dA;case 15:break se;case 7:break mA;case 1:break hA;default:break cA}if(9972==(0|A)|A>>>0<9970)break M;if(g=-2147483648,r=134,A>>>0<9974)break X;if(9974!=(0|A)&&(r=130,A>>>0<9977))break X;if((A=A-9977|0)>>>0<5)break lA;break cA}be:{se:{te:{ne:{ke:switch((-16&A)-9984>>>4|0){case 0:if(g=0,r=130,9986==(0|A))break be;if(r=128,A>>>0<9989)break be;if(r=134,9989==(0|A))break be;r=150;oe:switch((-2&A)-9994|0){case 0:break be;case 2:break te;default:break oe}if(r=130,A-9992>>>0<6)break be;e=9999==(0|A),g=0,r=(A=9998==(0|A))?128:e?130:0;break be;case 1:if(g=0,r=128,A>>>0<10002)break be;if((A=A-10002|0)>>>0>11)break ne;if(r=130,!(1<>>0<2)break be;r=(A=A-10069&-3)?0:536871046;break be;case 6:if((e=A-10082|0)>>>0<3)break se;if(g=0,r=128,A>>>0<=10084)break ne;break be;case 9:if(g=0,r=134,A-10133>>>0>=3)break ne;break be;case 10:if(g=0,r=130,10145!=(0|A))break ne;break be;case 11:e=10175==(0|A),g=0,r=(A=10160==(0|A))||e?134:0;break be;case 3:break ke;default:break ne}if(g=0,r=130,A-10035>>>0<2)break be}g=0,r=0;break be}r=146;break be}g=f[(A=83992+(e<<3)|0)>>2],r=f[A+4>>2]}return U=r,-2147483648|g}if(g=-2147483648,r=130,A-11013>>>0<3)break X;if(r=134,A-11035>>>0<2)break X;e=11093==(0|A),g=-2147483648,U=(A=11088==(0|A))||e?134:0;break n}if(A-11904>>>0>=26)break SA;return U=0,1048576}if(A-12276>>>0>=8)break Z;break X}if((A=A-12306|0)>>>0>14)break Z;if(g=-2147483648,!(1<>>0<26)return U=0,33792;if(A-127312>>>0<26)return U=0,33792;if((e=A-127344|0)>>>0<=15&&(g=33792,r=130,1<>>0<26)break X;if(127374==(0|A))break m;if(g=0,r=134,A-127377>>>0<10)break X;if(r=102,A>>>0<=127461)break Z;break X}g=0,r=134;ge:{re:switch(A-127489|0){case 0:case 25:break X;case 1:break v;case 2:case 3:case 4:case 5:case 6:case 7:case 8:case 9:case 10:case 11:case 12:case 13:case 14:case 15:case 16:case 17:case 18:case 19:case 20:case 21:case 22:case 23:case 24:break ge;default:break re}switch(A-127535|0){case 0:break X;case 8:break v;default:break ge}}if(127568==(-2&A)|A-127538>>>0<9)break X;if(r=128,A>>>0<=127583)break Z;break X}if(127777==(0|A))break v;if(A-127789>>>0<9|A-127799>>>0<70)break m;if(127877==(0|A))break d;if(A-127870>>>0<22)break m;if(A-127780>>>0<112)break v;g=0,r=130;ee:switch(A-127894|0){case 44:case 45:case 46:case 49:break d;case 0:case 1:case 3:case 4:case 5:break X;default:break ee}if(A-127904>>>0<42)break m;if(r=150,127946==(0|A))break X;if(r=146,A-127947>>>0<2)break X;if(A-127951>>>0<5)break m;if(r=134,A-127968>>>0<17)break X;if(r=130,A-127902>>>0<83)break X;if((e=A-127987|0)>>>0<5)break uA;break GA}g=0,r=130;Ae:switch(A-128063|0){case 0:case 2:break X;default:break Ae}if(128124==(0|A)|(-5&A)-128129>>>0<3|128110==(0|A)|A-128112>>>0<9)break d;if(128066==(-2&A)|A-128102>>>0<4|A-128070>>>0<11)break d;Ae:switch(A-128253|0){case 1:U=128;break I;case 0:break v;default:break Ae}if(r=150,128170==(0|A))break X;break m}if(A>>>0<128318)break m;if(g=0,A>>>0<128326)break X;if(A-128329>>>0<2)break v;if(A-128331>>>0<4|A-128336>>>0<24)break m;if(A-128367>>>0<2)break v;if(128372==(-2&A)){U=146;break I}if(A-128371>>>0<7)break v;if(r=150,!(e=A-128378|0))break X;if(13==(0|e)|A-128394>>>0<4)break v;if(r=146,128400==(0|A))break X;if(r=150,A-128405>>>0<2)break X;r=134;$A:switch(A-128420|0){case 0:break X;case 1:case 4:case 13:case 14:case 24:case 30:case 31:case 32:case 45:case 46:case 47:case 56:case 57:case 58:case 61:case 63:case 68:case 75:case 79:case 86:break v;default:break $A}A=A>>>0>128506,e=0;break s}if(g=0,r=128,A-128981>>>0>=4)break Z;break X}if(A>>>0<129292)break Z;if(A-129328>>>0<10)break d;g=0,r=150;qA:switch(A-129304|0){case 35:break Z;case 0:case 1:case 2:case 3:case 4:case 6:case 7:case 14:break X;default:break qA}if(A-129341>>>0<2)break d;if(r=0,129350==(0|A))break X;if(r=198,129456==(-4&A))break X;if((e=A-129461|0)>>>0<5)break EA;break wA}if(g=1048576,A-11931>>>0<89)break X;break Z}return U=0,524288;case 29:return U=16777216,1073741825;case 28:break X;case 30:break UA;default:break Z}return U=0,32==(0|A)?1073741825:1}if(!(1079>>>e&1))break L;e=f[(A=81344+(e<<3)|0)>>2];break o}if(557553>>>e&1)break y;if(8508!=(-4&A))break _;break X}if(!(207>>>e&1))break AA;break N}if(g=1024,!(1017>>>e&1))break eA;break X}if(g=1024,!(32895>>>e&1))break rA;break X}if(g=1024,!(55>>>e&1))break CA;break X}if(g=1024,!(3087>>>e&1))break IA;break X}if((A=A-71453|0)>>>0>=15)break Z;e=f[(A=82312+(A<<3)|0)>>2];break o}if(!(49023>>>e&1))break fA;e=f[(A=82432+(e<<3)|0)>>2];break o}if(!(514623>>>e&1))break $;e=f[(A=82664+(e<<3)|0)>>2];break o}e=f[(A=83160+(e<<3)|0)>>2];break o}if(g=268435520,!(197>>>e&1))break iA;break X}e=f[(A=83208+(e<<3)|0)>>2];break o}if((A=A-6145|0)>>>0>=10)break Z;e=f[(A=83296+(A<<3)|0)>>2];break o}if(!(1077711>>>e&1))break tA;e=f[(A=83376+(e<<3)|0)>>2];break o}if(!(159>>>e&1))break bA;e=f[(A=83544+(e<<3)|0)>>2];break o}if((A=A-121479|0)>>>0>=4)break Z;e=f[(A=83608+(A<<3)|0)>>2];break o}if(!(195>>>e&1))break BA;break H}if((A=A-9745|0)>>>0>=13)break cA;e=f[(A=83640+(A<<3)|0)>>2];break o}if(1101>>>e&1)break M;if(g=-2147483648,r=130,A>>>0<=9773)break cA;break X}if(!(45>>>e&1))break QA;e=f[(A=83744+(e<<3)|0)>>2];break o}if((A=A-9851|0)>>>0>=5)break cA;e=f[(A=83792+(A<<3)|0)>>2];break o}if((A=A-9874|0)>>>0>=11)break cA;e=f[(A=83832+(A<<3)|0)>>2];break o}if((A=A-9937|0)>>>0>=4)break cA;e=f[(A=83920+(A<<3)|0)>>2];break o}e=f[(A=83952+(A<<3)|0)>>2];break o}if(!(23>>>e&1))break GA;e=f[(A=84016+(e<<3)|0)>>2];break o}if(g=0,r=150,!(1991>>>e&1))break oA;break X}if(r=150,27>>>e&1)break X}e=0,U=(A=A-129489>>>0<13)?150:134;break b}A=A-127992>>>0<3,e=0;break s}if(A-9837>>>0<2)break X}U=128;break f}if(A-8623>>>0<13)break O;if(A-8636>>>0<18)break H;if((e=A-8656|0)>>>0<22&&(r=0,3157995>>>e&1))break X;if(g=-2147483648,r=0,A-8661>>>0<31)break X;break Z}if(A>>>0<128592)break m;if(g=0,r=0,A>>>0<128640)break X;if(!((e=A-128675|0)>>>0>29|!(1<>>0<128710)break m;if(r=150,128716==(0|A))break X;if(A-128715>>>0<5)break v;if(A-128720>>>0<3)break m;if(!((e=A-128736|0)>>>0>=10|!(575>>>e&1)))break v;if(r=134,A-128747>>>0<2)break X;r=130;oA:switch(A-128752|0){case 0:case 3:break X;default:break oA}A=A-128756>>>0<6,e=0;break s}g=-2147483648;kA:switch(A-8260|0){case 1:case 2:case 3:case 4:case 5:case 6:case 7:case 8:case 9:case 10:case 11:case 12:case 13:break Z;case 0:case 14:break X;default:break kA}if(16!=(0|(A=A-8315|0))&&A)break Z;return U=0,8}if(g=268435520,r=1073741824,69955!=(0|A))break Z;break X}if(A-8266>>>0<8)break O;if(g=-2147483640,8275==(0|A))break X;if(g=-2147483648,A-8277>>>0>=10)break Z;break X}U=542113792;break f}if(g=-2147483648,r=0,A-65093>>>0<2)break X;if(g=64,r=268435456,65104==(0|a))break X;g=268435520,r=-2147483648;bA:switch(A-65106|0){case 3:U=134217728;break e;case 5:U=536870912;break r;case 4:break a;case 2:break g;case 0:break X;case 15:case 22:break bA;default:break Z}break y}if(2142==(0|A))break h;if(g=64,A-2096>>>0<15)break X;break Z}if(g=1024,A-72850>>>0<22)break X;if((A=A-72874|0)>>>0>=13)break Z;e=f[(A=82560+(A<<3)|0)>>2];break o}if(g=4096,A-71103>>>0<2)break X;if(g=1024,71132!=(-2&A))break Z;break X}if(70726==(0|A))break P;if(g=4096,70850!=(-2&A))break Z;break X}if(g=4096,A-68325>>>0>=2)break Z;break X}if(6109==(0|A))break P;if(g=4096,A-6089>>>0>=11)break Z;break X}gA:switch(A-3959|0){case 0:case 2:return U=0,8389632;default:break gA}if(3968==(0|e)|A-3953>>>0<14)break N;if(!((e=A-3970|0)>>>0>=6|!(55>>>e&1)))break P;if(A-3981>>>0<11)break N;if(g=1024,A-3993>>>0<36)break X;if(g=4096,4038!=(0|A))break Z;break X}if(A-3655>>>0<6)break P;if(!((e=A-3761|0)>>>0>11|!(1<>>0<5)break X;if(g=1024,3789!=(0|A))break Z;break X}if(A-1770>>>0<3)break X;if(g=1024,1773!=(0|A))break Z;break X}if(73028==(-2&A))break P;g=1024;$:switch(A-73104|0){default:if(73031!=(0|A))break Z;break X;case 2:case 3:case 4:case 6:break Z;case 0:case 1:case 5:break X;case 7:break $}break P}if((A=A-8492|0)>>>0>=30)break Z;e=f[(A=81704+(A<<3)|0)>>2];break o}g=0;break j}r=(e=43712==(-3&A))>>>20|0,g=e<<12}if(131072==(0|(C=-65536&A)))break R;if(65536==(0|C))break J;if(e=g,a=r,C)break V}if(A-13312>>>0<6582)break A;if(A-19968>>>0<20976)break A;if(C=2048,A-63744>>>0<366)return U=a,2048|e;if(g=e,r=a,A-64112>>>0>=106)break V;break T}if(C=2099200,A-183984>>>0<7473|A-178208>>>0<5762|A-177984>>>0<222|A-131072>>>0<42711)break T;if(A-173824>>>0<4149)break T;if(C=2048,A-194560>>>0<542)break T;break V}if(C=2048,A-110960>>>0<396|A-94208>>>0<6125|A-100352>>>0<755)break T}C=0}return U=r,g|C}if(8508==(-4&A))break y;if(g=128,!(A-8517>>>0<3)&&(g=16777344,8520!=(-2&A)))break Z}U=r;break n}e=(A=A>>>0>131069)?65536:0,U=A?0:128;break b}if(11776==(0|A))break O}U=0;break I}U=0;break f}return U=0,4194304}return U=0,128}return U=0,16777344}return U=0,4096}return U=0,1024}return U=0,-2147483520}return U=0,A-10214>>>0<10?-2147483520:-2147483648}U=0;break r}U=0;break e}U=130;break I}U=130;break f}U=134;break I}return U=150,0}U=0;break b}U=A?0:r;break b}return U=e,A}return 131072}return U=0,16777216}U=A?0:g;break b}return U=r,A}return U=g,A}U=A?0:e;break n}return U=0,8192}U=f[A+4>>2];break b}U=A?8388608:0;break b}return g}return U=0,-2147483616}U=A?134:128}return e}U=134}return-2147483648}return 0}U=1073741824;break r}U=-2147483648}return 268435520}U=67108864}return 64}return U=a,2099200|e}function q(A){var e,g=0,r=0,C=0,b=0,s=0;(e=IA(8244))&&(f[e+328>>2]=2,a[132848]=0,f[e+684>>2]=0,f[e+688>>2]=0,f[e+320>>2]=0,f[e+324>>2]=0,a[e+268|0]=0,a[e+228|0]=0,f[e+8216>>2]=0,f[e+8220>>2]=0,f[e+224>>2]=104944,f[e+216>>2]=383,f[e+220>>2]=96,ue(e+344|0,0,292),f[e+8196>>2]=0,f[(g=e+8188|0)>>2]=0,f[g+4>>2]=0,f[e+8180>>2]=0,f[e+8184>>2]=0,a[e+460|0]=22,a[e+461|0]=129,a[e+466|0]=38,a[e+462|0]=38,a[e+463|0]=36,a[e+464|0]=22,a[e+465|0]=224,a[e+456|0]=22,a[e+457|0]=22,a[e+458|0]=44,a[e+459|0]=22,a[e+454|0]=46,a[e+455|0]=129,a[e+446|0]=22,a[e+447|0]=38,a[e+448|0]=28,a[e+449|0]=193,a[e+450|0]=38,a[e+451|0]=22,a[e+452|0]=46,a[e+453|0]=46,a[e+441|0]=129,a[e+442|0]=38,a[e+443|0]=22,a[e+444|0]=38,a[e+445|0]=193,f[e+332>>2]=104912,f[e+336>>2]=104916,f[e+340>>2]=105232,a[e+296|0]=18,a[e+297|0]=18,I[e+304>>1]=182,I[e+306>>1]=140,a[e+298|0]=20,I[e+308>>1]=220,I[e+310>>1]=220,I[e+312>>1]=220,a[e+299|0]=20,a[e+300|0]=20,I[e+314>>1]=240,a[e+301|0]=22,I[e+316>>1]=260,I[e+318>>1]=280,a[e+302|0]=22,a[e+303|0]=20,g=ue(e,0,212),f[g+200>>2]=20,f[g+192>>2]=25966,f[g+196>>2]=500,f[g+80>>2]=95,f[g+16>>2]=1,f[g+20>>2]=3,f[g+8>>2]=2,f[g+52>>2]=19,a[g+168|0]=3,f[g+92>>2]=2,f[g+72>>2]=4,f[g+40>>2]=115,f[g+44>>2]=95,f[g+140>>2]=105244,pr(g,201),f[g+120>>2]=2,f[g+124>>2]=44,f[g+164>>2]=100,f[g+128>>2]=46,f[g+132>>2]=14,f[g+112>>2]=1227133512,f[g+116>>2]=49,f[g+104>>2]=1,r=f[26313],f[g+636>>2]=f[26312],f[g+640>>2]=r,r=f[26315],f[g+644>>2]=f[26314],f[g+648>>2]=r,r=f[26317],f[g+652>>2]=f[26316],f[g+656>>2]=r,r=f[26319],f[g+660>>2]=f[26318],f[g+664>>2]=r,r=f[26321],f[g+668>>2]=f[26320],f[g+672>>2]=r,r=f[26323],f[g+676>>2]=f[26322],f[g+680>>2]=r,r=i[104928]|i[104929]<<8,a[g+160|0]=r,a[g+161|0]=r>>>8,r=i[104924]|i[104925]<<8|i[104926]<<16|i[104927]<<24,a[g+156|0]=r,a[g+157|0]=r>>>8,a[g+158|0]=r>>>16,a[g+159|0]=r>>>24),C=rg(e+228|0,A),g=0;A:{e:if(r=i[0|A]){for(;g=(r<<24>>24)+(g<<8)|0,r=i[0|(A=A+1|0)];);g:{r:{C:{a:{I:{f:{i:{b:{s:{t:{n:{k:{o:{B:{c:{Q:{G:{w:{E:{D:{u:{l:{x:{d:{m:{M:{v:{h:{p:{Y:{H:{N:{P:{F:{y:{z:{O:{Z:{K:{W:{X:{L:{T:{V:{J:{R:{U:{j:{S:{q:{_:{$:{AA:{eA:{gA:{rA:{CA:{aA:{IA:{fA:{iA:{bA:{sA:{tA:{nA:{kA:{oA:{BA:{cA:{QA:{if((0|g)<=28008){if((0|g)<=26464){if((0|g)<=25696){GA:switch(g-24934|0){case 20:break b;case 1:case 2:case 3:case 4:case 5:case 6:case 9:case 10:case 11:case 14:case 15:case 16:case 17:case 18:case 19:break e;case 8:break $;case 12:break iA;case 7:break bA;case 0:break sA;case 13:break QA;default:break GA}GA:switch(g-25189|0){case 1:case 3:case 4:case 5:case 6:case 7:case 8:case 10:case 11:case 12:case 13:break e;case 14:break L;case 2:break IA;case 0:break fA;case 9:break QA;default:break GA}switch(g-25441|0){case 18:break Q;case 0:break $;case 24:break CA;default:break e}}GA:switch(g-25964|0){case 1:case 4:case 5:case 6:break e;case 8:break S;case 9:break _;case 7:break $;case 3:break AA;case 2:break eA;case 0:break BA;default:break GA}GA:switch(g-26209|0){case 1:case 2:case 3:case 4:case 5:case 6:case 7:case 9:case 10:case 11:case 12:case 13:case 14:case 15:case 16:break e;case 17:break U;case 8:break j;case 0:break q;default:break GA}switch(g-25697|0){case 4:break gA;case 0:break rA;default:break e}}if((0|g)<=27488){GA:switch(g-26729|0){case 1:case 2:case 3:case 4:case 5:case 6:case 7:case 8:case 10:case 13:case 14:case 15:break e;case 16:break K;case 12:break W;case 11:break X;case 9:break L;case 0:break T;default:break GA}GA:switch(g-26977|0){case 1:case 2:case 4:case 5:case 6:case 7:case 8:case 9:case 10:case 11:case 12:case 13:case 15:case 16:case 17:break e;case 19:break y;case 18:break z;case 3:break O;case 14:break Z;case 0:break $;default:break GA}switch(g-26465|0){case 20:break T;case 13:break V;case 0:case 3:break J;default:break e}}GA:switch(g-27489|0){case 13:break s;case 1:case 2:case 3:case 4:case 5:case 6:case 7:case 8:case 9:case 12:case 15:case 16:case 17:case 18:case 19:case 21:case 22:case 23:break e;case 24:break h;case 20:break p;case 14:break Y;case 11:break H;case 10:break N;case 0:break P;default:break GA}switch(g-27745|0){case 19:break M;case 0:break v;case 21:break kA;default:break e}}if((0|g)<=29792){if((0|g)<=28768){GA:switch(g-28009|0){case 3:break s;case 11:break d;case 2:break m;case 1:case 4:case 5:case 6:case 7:case 8:case 12:case 13:case 14:case 15:break e;case 10:break O;case 9:break T;case 0:case 16:break tA;default:break GA}GA:switch(g-28258|0){case 0:break l;case 10:break x;case 1:case 2:case 4:case 5:case 6:case 7:case 8:case 9:break e;case 3:break T;default:break GA}switch(g-28525|0){case 0:break u;case 5:break T;default:break e}}if((0|g)<=29539){GA:switch(g-28769|0){case 19:break E;case 11:break D;case 1:case 2:case 3:case 4:case 5:case 6:case 7:case 8:case 9:case 10:case 12:case 13:case 14:case 15:case 16:case 17:case 18:case 20:case 21:case 22:case 23:break e;case 0:break T;case 24:break tA;default:break GA}GA:switch(g-29295|0){case 6:break G;case 0:break w;case 1:case 2:case 3:case 4:case 5:break e;default:break GA}if(29045==(0|g))break tA;break e}switch(g-29540|0){case 19:break t;case 18:break n;case 13:break k;case 8:break B;case 5:break c;case 7:break Q;case 14:break L;case 0:break I;default:break e}}if((0|g)>6514801)break cA;if((0|g)<=30058)switch(g-29793|0){case 19:break i;case 17:break b;case 0:case 4:break s;case 13:break t;case 7:break tA;default:break e}if((0|g)<=30312)switch(g-30059|0){case 0:break f;case 15:break tA;case 7:break I;default:break e}if(30313==(0|g))break a;if(31336==(0|g))break C;if(6451321!=(0|g))break e}if(f[e+296>>2]=303174162,f[e+300>>2]=370545684,f[e+600>>2]=2432,f[e+8>>2]=0,f[e+12>>2]=65540,f[e+100>>2]=f[e+96>>2],A=f[25889],f[e+304>>2]=f[25888],f[e+308>>2]=A,A=f[25891],f[e+312>>2]=f[25890],f[e+316>>2]=A,NA(e),a[e+345|0]=2|i[e+345|0],a[e+406|0]=16|i[e+406|0],a[e+407|0]=16|i[e+407|0],a[e+408|0]=16|i[e+408|0],a[e+409|0]=16|i[e+409|0],a[e+410|0]=16|i[e+410|0],a[e+411|0]=16|i[e+411|0],a[e+412|0]=16|i[e+412|0],a[e+413|0]=16|i[e+413|0],a[e+414|0]=16|i[e+414|0],a[e+415|0]=16|i[e+415|0],a[e+416|0]=16|i[e+416|0],a[e+417|0]=16|i[e+417|0],a[e+418|0]=16|i[e+418|0],a[e+419|0]=16|i[e+419|0],a[e+420|0]=16|i[e+420|0],a[e+456|0]=4|i[e+456|0],a[e+457|0]=4|i[e+457|0],f[e+112>>2]=613567144,f[e+104>>2]=16,6451321!=(0|g))break A;f[e+104>>2]=1,f[e+108>>2]=512,g=6451321;break A}if((0|g)>7364975)break nA;if((0|g)>6840682)break oA;if(6514802==(0|g))break aA;if(6516078==(0|g))break C;if(6779491!=(0|g))break e}if(f[e+600>>2]=896,f[e+328>>2]=8,f[e+296>>2]=336858127,f[e+300>>2]=353768980,f[e+332>>2]=103632,A=f[25905],f[e+304>>2]=f[25904],f[e+308>>2]=A,A=f[25907],f[e+312>>2]=f[25906],f[e+316>>2]=A,ue(e+344|0,0,256),a[e+388|0]=129,a[e+389|0]=129,a[e+390|0]=129,a[e+391|0]=129,a[e+420|0]=129,a[e+421|0]=129,a[e+422|0]=129,a[e+423|0]=129,a[e+360|0]=129,a[e+392|0]=129,a[e+393|0]=129,a[e+417|0]=129,a[e+418|0]=129,a[e+419|0]=129,a[e+420|0]=129,a[e+408|0]=6,a[e+409|0]=4,a[e+410|0]=6,a[e+411|0]=6,a[e+412|0]=6,a[e+413|0]=193,a[e+414|0]=6,a[e+415|0]=6,a[e+406|0]=6,a[e+407|0]=129,a[e+398|0]=4,a[e+399|0]=193,a[e+400|0]=6,a[e+401|0]=193,a[e+402|0]=6,a[e+403|0]=4,a[e+404|0]=4,a[e+405|0]=4,a[e+394|0]=4,a[e+395|0]=4,a[e+396|0]=4,a[e+397|0]=193,f[e+44>>2]=130,f[e+8>>2]=2,f[e+12>>2]=6,f[e+16>>2]=0,f[e+20>>2]=2,f[e+104>>2]=264,f[e+108>>2]=6146,a[e+391|0]=193,a[e+389|0]=193,a[e+390|0]=193,a[e+421|0]=193,f[e+100>>2]=f[e+96>>2],a[e+416|0]=4|i[e+416|0],6779491!=(0|g))break A;f[e+40>>2]=1,g=6779491;break A}if(6840683==(0|g))break R;if(6972015==(0|g))break F;if(7107687!=(0|g))break e}f[e+296>>2]=134875662,f[e+300>>2]=252968960,f[e+328>>2]=5,a[e+169|0]=1,f[e+132>>2]=33,f[e+104>>2]=99336,f[e+8>>2]=0,f[e+12>>2]=262182,A=f[26069],f[e+304>>2]=f[26068],f[e+308>>2]=A,A=f[26071],f[e+312>>2]=f[26070],f[e+316>>2]=A;break A}if((0|g)<=7564649){if(7364976==(0|g))break $;if(7435619==(0|g))break tA;if(7563374!=(0|g))break e;f[e+148>>2]=1,f[e+112>>2]=24,f[e+104>>2]=1,f[e+100>>2]=f[e+96>>2],g=7563374;break A}if(7564650==(0|g))break o;if(7959909==(0|g))break C;if(1885958500!=(0|g))break e}f[e+104>>2]=0;break A}f[e+4>>2]=48,f[e+8>>2]=0,f[e+144>>2]=1,f[e+104>>2]=16779472,f[e+32>>2]=1,f[e+24>>2]=1,A=f[25881],f[e+304>>2]=f[25880],f[e+308>>2]=A,A=f[25883],f[e+312>>2]=f[25882],f[e+316>>2]=A,a[e+465|0]=64&i[e+465|0]|129,g=24934;break A}f[e+600>>2]=4608,f[e+296>>2]=303173650,f[e+300>>2]=303174162,f[e+8>>2]=0,f[e+12>>2]=36,f[e+104>>2]=1024,f[e+100>>2]=f[e+96>>2],f[e+40>>2]=1,A=f[25865],f[e+304>>2]=f[25864],f[e+308>>2]=A,A=f[25867],f[e+312>>2]=f[25866],f[e+316>>2]=A,g=24941;break A}for(f[e+600>>2]=1536,f[e+224>>2]=0,f[e+216>>2]=1631,f[e+220>>2]=1536,f[e+104>>2]=2884720,f[e+328>>2]=7,f[e+40>>2]=1,V=g=V-16|0,f[g+12>>2]=-1,A=89684;C=Te(g+12|0,A),(0|(r=f[g+12>>2]))>=33&&(a[0|(b=(e+r|0)-1192|0)]=1|i[0|b]),A=A+C|0,r;);for(f[g+12>>2]=-1,A=89743;C=Te(g+12|0,A),(0|(r=f[g+12>>2]))>=33&&(a[0|(b=(e+r|0)-1192|0)]=2|i[0|b]),A=A+C|0,r;);for(f[g+12>>2]=-1,A=89795;C=Te(g+12|0,A),(0|(r=f[g+12>>2]))>=33&&(a[0|(b=(e+r|0)-1192|0)]=4|i[0|b]),A=A+C|0,r;);for(f[g+12>>2]=-1,A=89941;C=Te(g+12|0,A),(0|(r=f[g+12>>2]))>=33&&(a[0|(b=(e+r|0)-1192|0)]=16|i[0|b]),A=A+C|0,r;);for(f[g+12>>2]=-1,A=90045;C=Te(g+12|0,A),(0|(r=f[g+12>>2]))>=33&&(a[0|(b=(e+r|0)-1192|0)]=32|i[0|b]),A=A+C|0,r;);for(f[g+12>>2]=-1,A=90045;C=Te(g+12|0,A),(0|(r=f[g+12>>2]))>=33&&(a[0|(b=(e+r|0)-1192|0)]=8|i[0|b]),A=A+C|0,r;);for(f[g+12>>2]=-1,A=90045;C=Te(g+12|0,A),(0|(r=f[g+12>>2]))>=33&&(a[0|(b=(e+r|0)-1192|0)]=64|i[0|b]),A=A+C|0,r;);V=g+16|0,g=24946;break A}f[e+600>>2]=1056,f[e+12>>2]=34,f[e+216>>2]=1118,f[e+220>>2]=1072,ue(e+344|0,0,256),a[e+406|0]=4,a[e+366|0]=4,a[e+367|0]=4,a[e+369|0]=4,a[e+370|0]=4,a[e+371|0]=4,a[e+372|0]=4,a[e+361|0]=4,a[e+362|0]=4,a[e+363|0]=4,a[e+364|0]=4,a[e+373|0]=4,a[e+380|0]=4,a[e+381|0]=4,a[e+382|0]=4,a[e+383|0]=4,a[e+375|0]=4,a[e+376|0]=4,a[e+377|0]=4,a[e+378|0]=4,a[e+384|0]=4,a[e+360|0]=129,f[e+328>>2]=6,f[e+296>>2]=134744588,f[e+300>>2]=286261248,f[e+40>>2]=1,f[e+8>>2]=0,f[e+104>>2]=1032,f[e+108>>2]=66,A=f[25885],f[e+304>>2]=f[25884],f[e+308>>2]=A,A=f[25887],f[e+312>>2]=f[25886],f[e+316>>2]=A,g=25189;break A}LA(e),f[e+328>>2]=6,f[e+56>>2]=2,f[e+36>>2]=263,f[e+40>>2]=1074,f[e+124>>2]=32,f[e+104>>2]=184554728,f[e+8>>2]=2,a[e+386|0]=64&i[e+386|0]|129,g=25191;break A}f[e+12>>2]=262182,f[e+40>>2]=1,g=6514802;break A}f[e+328>>2]=14,f[e+296>>2]=303173393,f[e+300>>2]=336986112,f[e+104>>2]=1024,f[e+16>>2]=0,f[e+20>>2]=2,f[e+8>>2]=2,f[e+12>>2]=22,f[e+44>>2]=120,A=f[25893],f[e+304>>2]=f[25892],f[e+308>>2]=A,A=f[25895],f[e+312>>2]=f[25894],f[e+316>>2]=A,a[e+463|0]=64&i[e+463|0]|129,a[e+465|0]=64&i[e+465|0]|129,g=25465;break A}f[e+8>>2]=0,f[e+104>>2]=184618072,f[e+32>>2]=1,A=f[26101],f[e+304>>2]=f[26100],f[e+308>>2]=A,A=f[26103],f[e+312>>2]=f[26102],f[e+316>>2]=A,a[e+465|0]=64&i[e+465|0]|129,g=25697;break A}f[e+296>>2]=336860180,f[e+300>>2]=336991764,f[e+8>>2]=0,f[e+104>>2]=16846872,f[e>>2]=8,f[e+4>>2]=48,f[e+80>>2]=87,f[e+32>>2]=1,f[e+36>>2]=256,f[e+40>>2]=2,A=f[25897],f[e+304>>2]=f[25896],f[e+308>>2]=A,A=f[25899],f[e+312>>2]=f[25898],f[e+316>>2]=A,a[e+465|0]=64&i[e+465|0]|129,g=25701;break A}f[e+132>>2]=33,f[e+104>>2]=16779328,f[e+8>>2]=0,f[e+12>>2]=8,f[e+204>>2]=101,f[e+60>>2]=2,f[e+40>>2]=2,A=f[25901],f[e+304>>2]=f[25900],f[e+308>>2]=A,A=f[25903],f[e+312>>2]=f[25902],f[e+316>>2]=A,a[e+441|0]=64|i[e+441|0],a[e+445|0]=64|i[e+445|0],a[e+449|0]=64|i[e+449|0],a[e+455|0]=64|i[e+455|0],a[e+461|0]=64|i[e+461|0],a[e+465|0]=64|i[e+465|0],g=25966;break A}f[e+328>>2]=4,f[e+296>>2]=336858640,f[e+300>>2]=353768980,f[e+104>>2]=16782344,f[e+20>>2]=2,f[e+12>>2]=22,f[e+4>>2]=2,f[e+8>>2]=2,f[e+332>>2]=103640,A=f[25997],f[e+304>>2]=f[25996],f[e+308>>2]=A,A=f[25999],f[e+312>>2]=f[25998],f[e+316>>2]=A,g=25967;break A}f[e+296>>2]=269422096,f[e+300>>2]=370545684,f[e+104>>2]=86017320,f[e+108>>2]=6144,f[e+16>>2]=0,f[e+20>>2]=2,f[e+8>>2]=2,f[e+12>>2]=534,f[e+100>>2]=f[e+96>>2],f[e+44>>2]=120,A=f[25913],f[e+304>>2]=f[25912],f[e+308>>2]=A,A=f[25915],f[e+312>>2]=f[25914],f[e+316>>2]=A;$:{AA:{if((0|g)<=26976){if(24942==(0|g))break AA;if(25441!=(0|g))break $;f[e+12>>2]=566,f[e+336>>2]=103664,g=25441;break A}if(26977!=(0|g)){if(7364976!=(0|g))break $;f[e+8>>2]=3,f[e+12>>2]=310,g=7364976;break A}f[e+104>>2]=85984264,g=26977;break A}f[e+104>>2]=153093416,f[e+108>>2]=2048,f[e+140>>2]=103676,g=24942;break A}f[e+40>>2]=2;break A}f[e+296>>2]=303173648,f[e+300>>2]=303174162,f[e+104>>2]=3147080,f[e+12>>2]=65792,f[e+84>>2]=1,A=f[25921],f[e+304>>2]=f[25920],f[e+308>>2]=A,A=f[25923],f[e+312>>2]=f[25922],f[e+316>>2]=A,g=25973;break A}f[e+600>>2]=1536,f[e+216>>2]=1740,f[e+220>>2]=1568,f[e+104>>2]=96,f[e+224>>2]=103696,f[e+340>>2]=103872,f[e+40>>2]=1,g=26209;break A}f[e+328>>2]=5}f[e+104>>2]=86024,f[e+164>>2]=130,a[e+465|0]=64&i[e+465|0]|129;break A}f[e+296>>2]=303173650,f[e+300>>2]=303174162,f[e+8>>2]=3,f[e+12>>2]=36,f[e+144>>2]=2,f[e+104>>2]=118658312,f[e+28>>2]=1,f[e+100>>2]=f[e+96>>2],A=f[25865],f[e+304>>2]=f[25864],f[e+308>>2]=A,A=f[25867],f[e+312>>2]=f[25866],f[e+316>>2]=A,a[e+465|0]=64&i[e+465|0]|129,g=26226;break A}I[e+170>>1]=257,f[e+148>>2]=1,f[e+12>>2]=2,g=6840683;break A}f[e+144>>2]=2,f[e+104>>2]=2098176,f[e+8>>2]=0,f[e+12>>2]=32,f[e+40>>2]=3,f[e+28>>2]=1;break A}f[e+8>>2]=3,f[e+100>>2]=f[e+96>>2],g=26478;break A}f[e+328>>2]=18,f[e+296>>2]=320081425,f[e+300>>2]=353768980,f[e+600>>2]=2304,f[e+112>>2]=84648,f[e+104>>2]=16,f[e+8>>2]=6,f[e+12>>2]=65540,f[e+100>>2]=f[e+96>>2],A=f[25973],f[e+304>>2]=f[25972],f[e+308>>2]=A,A=f[25975],f[e+312>>2]=f[25974],f[e+316>>2]=A;T:{V:{J:{if((0|g)<=28529){if(26485==(0|g))break J;if(28261!=(0|g))break T;A=f[25861],f[e+304>>2]=f[25860],f[e+308>>2]=A,A=f[25863],f[e+312>>2]=f[25862],f[e+316>>2]=A,f[e+296>>2]=320017171,f[e+300>>2]=320017171,f[e+132>>2]=22,f[e+112>>2]=-1431655768,f[e+108>>2]=32768|f[e+108>>2],NA(e);break A}if(28530==(0|g))break V;if(28769!=(0|g))break T;f[e+600>>2]=2560,NA(e);break A}A=f[25861],f[e+304>>2]=f[25860],f[e+308>>2]=A,A=f[25863],f[e+312>>2]=f[25862],f[e+316>>2]=A,f[e+600>>2]=2688,f[e+296>>2]=320017171,f[e+300>>2]=320017171,f[e+8>>2]=2,NA(e);break A}f[e+600>>2]=2816}NA(e);break A}a[0|C]=104,a[C+1|0]=98,a[C+2|0]=115,a[C+3|0]=0,29554!=(0|g)?(A=f[26093],f[e+304>>2]=f[26092],f[e+308>>2]=A,A=f[26095],f[e+312>>2]=f[26094],f[e+316>>2]=A):(A=f[25977],f[e+304>>2]=f[25976],f[e+308>>2]=A,A=f[25979],f[e+312>>2]=f[25978],f[e+316>>2]=A),f[e+328>>2]=3,f[e+296>>2]=336859409,f[e+300>>2]=353768980,I[e+168>>1]=261,f[e+8>>2]=0,f[e+12>>2]=16,f[e+144>>2]=1,f[e+184>>2]=1056,f[e+104>>2]=33572172,f[e+108>>2]=330,f[e+36>>2]=3,a[e+465|0]=64&i[e+465|0]|129,a[e+458|0]=64&i[e+458|0]|129;break A}f[e+104>>2]=17990912,f[e+8>>2]=3,f[e+12>>2]=36,g=26740;break A}f[e+328>>2]=3,f[e+296>>2]=320016657,f[e+300>>2]=353768980,f[e+124>>2]=32,f[e+128>>2]=44,f[e+104>>2]=186758144,f[e+12>>2]=1081398,f[e+16>>2]=2,f[e+4>>2]=32,f[e+8>>2]=0,f[e+116>>2]=899,f[e+120>>2]=1,a[e+169|0]=1,f[e+76>>2]=2,A=f[25981],f[e+304>>2]=f[25980],f[e+308>>2]=A,A=f[25983],f[e+312>>2]=f[25982],f[e+316>>2]=A,a[e+465|0]=64&i[e+465|0]|129,pr(e,3),g=26741;break A}for(f[e+600>>2]=1328,f[e+8>>2]=3,A=f[25985],f[e+304>>2]=f[25984],f[e+308>>2]=A,A=f[25987],f[e+312>>2]=f[25986],f[e+316>>2]=A,C=ue(e+344|0,0,256),a[e+429|0]=129,a[e+416|0]=129,a[e+403|0]=129,a[e+399|0]=129,a[e+400|0]=129,a[e+397|0]=129,a[e+393|0]=129,r=103952,b=50,s=50;a[0|(A=C+s|0)]=2|i[0|A],A=C+i[r+1|0]|0,a[0|A]=2|i[0|A],A=C+i[r+2|0]|0,a[0|A]=2|i[0|A],s=i[0|(r=r+3|0)],A=103952,103982!=(0|r););for(r=e+344|0;a[0|(C=r+b|0)]=4|i[0|C],C=r+i[A+1|0]|0,a[0|C]=4|i[0|C],C=r+i[A+2|0]|0,a[0|C]=4|i[0|C],b=i[0|(A=A+3|0)],103982!=(0|A););a[e+168|0]=6,f[e+104>>2]=5128,a[e+413|0]=4|i[e+413|0];break A}f[e+328>>2]=4,f[e+296>>2]=336858640,f[e+300>>2]=353768980,f[e+104>>2]=16782440,f[e+20>>2]=2,f[e+12>>2]=22,f[e+4>>2]=2,f[e+8>>2]=2,f[e+332>>2]=104e3,A=f[25997],f[e+304>>2]=f[25996],f[e+308>>2]=A,A=f[25999],f[e+312>>2]=f[25998],f[e+316>>2]=A,g=26991;break A}f[e+296>>2]=303174160,f[e+300>>2]=353768980,f[e+104>>2]=16781320,f[e+144>>2]=2,f[e+8>>2]=2,f[e+12>>2]=22,A=f[26005],f[e+304>>2]=f[26004],f[e+308>>2]=A,A=f[26007],f[e+312>>2]=f[26006],f[e+316>>2]=A;break A}for(f[e+8>>2]=0,f[e+12>>2]=16,f[e+56>>2]=2,f[e+28>>2]=17,A=f[26009],f[e+304>>2]=f[26008],f[e+308>>2]=A,A=f[26011],f[e+312>>2]=f[26010],f[e+316>>2]=A,A=0,r=e+344|0;a[0|(C=A+r|0)]=231&i[0|C],a[0|(C=r+(1|A)|0)]=231&i[0|C],a[0|(C=r+(2|A)|0)]=231&i[0|C],a[0|(C=r+(3|A)|0)]=231&i[0|C],256!=(0|(A=A+4|0)););f[e+104>>2]=2280,f[e+108>>2]=2,f[e+608>>2]=104048,a[e+451|0]=16|i[e+451|0],a[e+456|0]=16|i[e+456|0],a[e+459|0]=16|i[e+459|0],a[e+460|0]=16|i[e+460|0],a[e+450|0]=8|i[e+450|0],a[e+462|0]=8|i[e+462|0],a[e+458|0]=8|i[e+458|0],a[e+465|0]=64&i[e+465|0]|129;break A}f[e+296>>2]=269618961,f[e+300>>2]=370546196,f[e+12>>2]=131110,f[e+144>>2]=2,f[e+104>>2]=184559112,f[e+108>>2]=8192,f[e+16>>2]=0,f[e+20>>2]=2,f[e+4>>2]=1,f[e+8>>2]=2,f[e+100>>2]=f[e+96>>2],f[e+140>>2]=103676,f[e+68>>2]=2,f[e+56>>2]=1,f[e+44>>2]=130,f[e+28>>2]=2,A=f[26025],f[e+304>>2]=f[26024],f[e+308>>2]=A,A=f[26027],f[e+312>>2]=f[26026],f[e+316>>2]=A,a[e+465|0]=64&i[e+465|0]|129,g=26996;break A}f[e+4>>2]=524,f[e+8>>2]=2,f[e+196>>2]=368,f[e+104>>2]=0,f[e+336>>2]=104128,f[e- -64>>2]=1,A=f[26029],f[e+304>>2]=f[26028],f[e+308>>2]=A,A=f[26031],f[e+312>>2]=f[26030],f[e+316>>2]=A,a[e+465|0]=64&i[e+465|0]|129,g=6972015;break A}for(f[e+296>>2]=303174162,f[e+300>>2]=370545684,A=f[25873],f[e+304>>2]=f[25872],f[e+308>>2]=A,A=f[25875],f[e+312>>2]=f[25874],f[e+316>>2]=A,C=ue(e+344|0,0,256),a[e+431|0]=1,a[e+429|0]=1,a[e+411|0]=1,a[e+405|0]=1,a[e+400|0]=1,a[e+396|0]=1,a[e+392|0]=1,A=104160,r=49;a[0|(r=r+C|0)]=4|i[0|r],r=C+i[A+1|0]|0,a[0|r]=4|i[0|r],r=C+i[A+2|0]|0,a[0|r]=4|i[0|r],r=i[0|(A=A+3|0)],104193!=(0|A););f[e+600>>2]=4256,f[e+8>>2]=0,f[e+12>>2]=16,a[e+168|0]=7,f[e+132>>2]=32,a[e+392|0]=128|i[e+392|0],a[e+396|0]=128|i[e+396|0],a[e+400|0]=128|i[e+400|0],a[e+405|0]=128|i[e+405|0],a[e+411|0]=128|i[e+411|0],a[e+429|0]=128|i[e+429|0],a[e+431|0]=128|i[e+431|0],f[e+188>>2]=1056,f[e+192>>2]=29301,f[e+104>>2]=19964960;break A}for(f[e+600>>2]=1056,C=ue(e+344|0,0,256),a[e+393|0]=1,a[e+365|0]=1,a[e+360|0]=1,a[e+545|0]=1,a[e+529|0]=1,a[e+391|0]=1,a[e+389|0]=1,a[e+390|0]=1,a[e+387|0]=1,a[e+379|0]=1,a[e+374|0]=1,a[e+368|0]=1,a[e+489|0]=1,a[e+487|0]=1,a[e+398|0]=1,A=104224,r=17;a[0|(r=r+C|0)]=4|i[0|r],r=C+i[A+1|0]|0,a[0|r]=4|i[0|r],r=C+i[A+2|0]|0,a[0|r]=4|i[0|r],r=i[0|(A=A+3|0)],104251!=(0|A););a[e+360|0]=128|i[e+360|0],a[e+365|0]=128|i[e+365|0],a[e+393|0]=128|i[e+393|0],a[e+368|0]=128|i[e+368|0],a[e+374|0]=128|i[e+374|0],a[e+379|0]=128|i[e+379|0],a[e+387|0]=128|i[e+387|0],a[e+389|0]=128|i[e+389|0],a[e+390|0]=128|i[e+390|0],a[e+391|0]=128|i[e+391|0],a[e+529|0]=128|i[e+529|0],a[e+545|0]=128|i[e+545|0],a[e+489|0]=128|i[e+489|0],a[e+487|0]=128|i[e+487|0],a[e+398|0]=128|i[e+398|0],A=f[26055],f[e+312>>2]=f[26054],f[e+316>>2]=A,A=f[26053],f[e+304>>2]=f[26052],f[e+308>>2]=A,f[e+296>>2]=353636370,f[e+300>>2]=336925972,f[e+200>>2]=0,f[e+8>>2]=7,f[e+12>>2]=2097184,a[e+168|0]=2,f[e+104>>2]=50176,f[e+84>>2]=1,pr(e,3);break A}f[e+296>>2]=320017171,f[e+300>>2]=320017171,f[e+104>>2]=184618072,f[e+8>>2]=12,f[e+12>>2]=32,A=f[25861],f[e+304>>2]=f[25860],f[e+308>>2]=A,A=f[25863],f[e+312>>2]=f[25862],f[e+316>>2]=A,g=27500;break A}f[e+184>>2]=42752,f[e+600>>2]=4352,ue(e+344|0,0,256),a[e+456|0]=1,a[e+457|0]=1,a[e+458|0]=1,a[e+459|0]=1,a[e+449|0]=1,a[e+450|0]=1,a[e+451|0]=1,a[e+452|0]=1,a[e+453|0]=1,a[e+454|0]=1,a[e+455|0]=1,a[e+456|0]=1,a[e+441|0]=1,a[e+442|0]=1,a[e+443|0]=1,a[e+444|0]=1,a[e+445|0]=1,a[e+446|0]=1,a[e+447|0]=1,a[e+448|0]=1,a[e+460|0]=65,a[e+461|0]=65,a[e+532|0]=32,a[e+527|0]=32,a[e+519|0]=32,a[e+515|0]=32,a[e+349|0]=32,a[e+350|0]=32,a[e+346|0]=32,f[e+132>>2]=20,f[e+112>>2]=286331152,f[e+104>>2]=1024,f[e+108>>2]=16384,f[e+40>>2]=1,f[e+8>>2]=8,a[e+458|0]=65,a[e+453|0]=65,a[e+447|0]=65,a[e+448|0]=65,a[e+443|0]=65,a[e+444|0]=65,g=27503;break A}f[e+328>>2]=10,f[e+296>>2]=336859666,f[e+300>>2]=353768980,a[e+168|0]=2,f[e+104>>2]=263264,f[e+8>>2]=7,A=f[26065],f[e+304>>2]=f[26064],f[e+308>>2]=A,A=f[26067],f[e+312>>2]=f[26066],f[e+316>>2]=A,g=27509;break A}f[e+104>>2]=1,g=27513;break A}f[e+116>>2]=5e3,f[e+104>>2]=16777216,f[e+24>>2]=1,f[e+16>>2]=0,f[e+20>>2]=2,f[e+8>>2]=2,f[e+12>>2]=32,f[e+328>>2]=5,g=27745;break A}f[e+116>>2]=5e3,f[e+104>>2]=99336,f[e+108>>2]=256,f[e+24>>2]=1,f[e+16>>2]=0,f[e+20>>2]=2,f[e+8>>2]=2,f[e+12>>2]=32,f[e+328>>2]=5,g=27764;break A}f[e+328>>2]=6,f[e+296>>2]=336859409,f[e+300>>2]=353768980,f[e+600>>2]=1056,f[e+104>>2]=2114600,f[e+108>>2]=138,f[e+8>>2]=4,f[e+632>>2]=104288,f[e+604>>2]=104288,A=f[26093],f[e+304>>2]=f[26092],f[e+308>>2]=A,A=f[26095],f[e+312>>2]=f[26094],f[e+316>>2]=A,g=28011;break A}f[e+328>>2]=4,f[e+104>>2]=1,f[e+8>>2]=2,f[e+36>>2]=256,g=28020;break A}f[e+4>>2]=48,f[e+8>>2]=0,f[e+12>>2]=128,f[e+104>>2]=2169880,f[e+32>>2]=1,f[e+36>>2]=256,f[e+24>>2]=1,f[e+136>>2]=85767,A=f[26097],f[e+304>>2]=f[26096],f[e+308>>2]=A,A=f[26099],f[e+312>>2]=f[26098],f[e+316>>2]=A,a[e+465|0]=64&i[e+465|0]|129,g=28268;break A}f[e+8>>2]=0,f[e+104>>2]=71752,A=f[26101],f[e+304>>2]=f[26100],f[e+308>>2]=A,A=f[26103],f[e+312>>2]=f[26102],f[e+316>>2]=A,a[e+465|0]=64&i[e+465|0]|129,g=28258;break A}f[e+296>>2]=336858898,f[e+300>>2]=370546196,f[e+104>>2]=1088,f[e+108>>2]=512,f[e+8>>2]=2,f[e+12>>2]=524310,A=f[26105],f[e+304>>2]=f[26104],f[e+308>>2]=A,A=f[26107],f[e+312>>2]=f[26106],f[e+316>>2]=A,g=28525;break A}f[e+328>>2]=3,f[e+296>>2]=320015633,f[e+300>>2]=353768980,a[e+168|0]=7,f[e+8>>2]=2,f[e+12>>2]=6,f[e+104>>2]=20488,f[e+108>>2]=192,f[e+36>>2]=9,f[e+60>>2]=260,A=f[26109],f[e+304>>2]=f[26108],f[e+308>>2]=A,A=f[26111],f[e+312>>2]=f[26110],f[e+316>>2]=A,a[e+465|0]=64&i[e+465|0]|129,g=28780;break A}for(f[e+296>>2]=353569552,f[e+300>>2]=353768980,f[e+116>>2]=5e3,f[e+104>>2]=33570920,f[e+108>>2]=14336,f[e+8>>2]=3,f[e+12>>2]=139286,f[e+100>>2]=f[e+96>>2],A=f[26113],f[e+304>>2]=f[26112],f[e+308>>2]=A,A=f[26115],f[e+312>>2]=f[26114],f[e+316>>2]=A,a[e+465|0]=64&i[e+465|0]|129,A=0,r=e+344|0;a[0|(C=A+r|0)]=253&i[0|C],a[0|(C=r+(1|A)|0)]=253&i[0|C],a[0|(C=r+(2|A)|0)]=253&i[0|C],a[0|(C=r+(3|A)|0)]=253&i[0|C],256!=(0|(A=A+4|0)););a[e+442|0]=2|i[e+442|0],a[e+443|0]=2|i[e+443|0],a[e+444|0]=2|i[e+444|0],a[e+446|0]=2|i[e+446|0],a[e+447|0]=2|i[e+447|0],a[e+450|0]=2|i[e+450|0],a[e+451|0]=2|i[e+451|0],a[e+453|0]=2|i[e+453|0],a[e+454|0]=2|i[e+454|0],a[e+456|0]=2|i[e+456|0],a[e+457|0]=2|i[e+457|0],a[e+459|0]=2|i[e+459|0],a[e+460|0]=2|i[e+460|0],a[e+462|0]=2|i[e+462|0],a[e+464|0]=2|i[e+464|0],a[e+466|0]=2|i[e+466|0],f[e+144>>2]=2,f[e+68>>2]=2;break A}f[e+296>>2]=303172879,f[e+300>>2]=353768980,f[e+328>>2]=3,f[e+8>>2]=3,f[e+12>>2]=262,f[e+104>>2]=16805928,f[e+108>>2]=30,A=f[26117],f[e+304>>2]=f[26116],f[e+308>>2]=A,A=f[26119],f[e+312>>2]=f[26118],f[e+316>>2]=A,g=29295;break A}fg(e),g=29301;break A}for(f[e+328>>2]=3,f[e+296>>2]=336859153,f[e+300>>2]=353768980,I[e+168>>1]=261,f[e+8>>2]=0,f[e+12>>2]=22,f[e+124>>2]=0,f[e+128>>2]=44,f[e+104>>2]=16794624,f[e+108>>2]=128,f[e+36>>2]=3,f[e+60>>2]=4,A=f[25869],f[e+304>>2]=f[25868],f[e+308>>2]=A,A=f[25871],f[e+312>>2]=f[25870],f[e+316>>2]=A,25459==(0|g)&&(f[e+108>>2]=136),a[e+465|0]=64&i[e+465|0]|129,a[e+458|0]=64&i[e+458|0]|129,A=0,r=e+344|0;a[0|(C=A+r|0)]=223&i[0|C],a[0|(C=r+(1|A)|0)]=223&i[0|C],a[0|(C=r+(2|A)|0)]=223&i[0|C],a[0|(C=r+(3|A)|0)]=223&i[0|C],256!=(0|(A=A+4|0)););a[e+442|0]=32|i[e+442|0],a[e+444|0]=32|i[e+444|0],a[e+447|0]=32|i[e+447|0],a[e+450|0]=32|i[e+450|0],a[e+452|0]=32|i[e+452|0],a[e+453|0]=32|i[e+453|0],a[e+454|0]=32|i[e+454|0],a[e+458|0]=32|i[e+458|0],a[e+462|0]=32|i[e+462|0],a[e+463|0]=32|i[e+463|0],a[e+466|0]=32|i[e+466|0],a[e+441|0]=32|i[e+441|0],a[e+445|0]=32|i[e+445|0],a[e+449|0]=32|i[e+449|0],a[e+455|0]=32|i[e+455|0],a[e+461|0]=32|i[e+461|0],a[e+465|0]=32|i[e+465|0];break A}for(f[e+296>>2]=303174162,f[e+300>>2]=370545684,f[e+600>>2]=3456,a[e+169|0]=1,f[e+8>>2]=0,f[e+12>>2]=22,f[e+100>>2]=f[e+96>>2],A=f[25873],f[e+304>>2]=f[25872],f[e+308>>2]=A,A=f[25875],f[e+312>>2]=f[25874],f[e+316>>2]=A,ue(e+344|0,0,256),a[e+365|0]=1,a[e+366|0]=1,a[e+357|0]=1,a[e+358|0]=1,a[e+359|0]=1,a[e+360|0]=1,a[e+361|0]=1,a[e+362|0]=1,a[e+363|0]=1,a[e+364|0]=1,a[e+349|0]=1,a[e+350|0]=1,a[e+351|0]=1,a[e+352|0]=1,a[e+353|0]=1,a[e+354|0]=1,a[e+355|0]=1,a[e+356|0]=1,A=74,r=74;a[(C=e+r|0)+344|0]=1|i[C+344|0],a[C+345|0]=1|i[C+345|0],a[C+346|0]=1|i[C+346|0],116!=(0|(r=r+3|0)););for(;a[(r=A+e|0)+344|0]=2|i[r+344|0],a[r+345|0]=2|i[r+345|0],a[r+346|0]=2|i[r+346|0],116!=(0|(A=A+3|0)););for(r=26;a[(A=e+r|0)+344|0]=4|i[A+344|0],a[A+345|0]=4|i[A+345|0],a[A+346|0]=4|i[A+346|0],a[A+347|0]=4|i[A+347|0],a[A+348|0]=4|i[A+348|0],71!=(0|(r=r+5|0)););f[e+112>>2]=84648,f[e+104>>2]=270589952,f[e+108>>2]=65536,f[e+40>>2]=1,f[e+204>>2]=f[e+600>>2]+74;break A}f[e+8>>2]=2,f[e+12>>2]=32,f[e+328>>2]=3,f[e+124>>2]=32,f[e+104>>2]=16864280,f[e+108>>2]=256,f[e+68>>2]=2,f[e+36>>2]=259,f[e+40>>2]=118,f[e+28>>2]=1,a[e+458|0]=128|i[e+458|0],g=29548;break A}f[e+296>>2]=370544658,f[e+300>>2]=370546196,f[e+164>>2]=130,f[e+8>>2]=0,f[e+12>>2]=86,f[e+104>>2]=87064,a[e+169|0]=1,f[e+152>>2]=3,A=f[26121],f[e+304>>2]=f[26120],f[e+308>>2]=A,A=f[26123],f[e+312>>2]=f[26122],f[e+316>>2]=A,a[e+465|0]=64&i[e+465|0]|129,g=7564650;break A}f[e+296>>2]=269487120,f[e+300>>2]=320148500,f[e+8>>2]=3,f[e+12>>2]=278,f[e+144>>2]=2,f[e+104>>2]=32872,A=f[26125],f[e+304>>2]=f[26124],f[e+308>>2]=A,A=f[26127],f[e+312>>2]=f[26126],f[e+316>>2]=A,a[e+465|0]=64&i[e+465|0]|129,g=29553;break A}f[e+296>>2]=336859152,f[e+300>>2]=353768980,f[e+8>>2]=0,f[e+144>>2]=1,f[e+104>>2]=6408,A=f[26129],f[e+304>>2]=f[26128],f[e+308>>2]=A,A=f[26131],f[e+312>>2]=f[26130],f[e+316>>2]=A,a[e+465|0]=64&i[e+465|0]|129,g=29558;break A}f[e+296>>2]=320015376,f[e+300>>2]=353768980,a[e+168|0]=4,f[e+12>>2]=22,f[e+4>>2]=1,f[e+8>>2]=2,f[e+104>>2]=1248,f[e+100>>2]=f[e+96>>2],A=f[26133],f[e+304>>2]=f[26132],f[e+308>>2]=A,A=f[26135],f[e+312>>2]=f[26134],f[e+316>>2]=A;break A}f[e+296>>2]=303174162,f[e+300>>2]=370545684,a[e+169|0]=1,f[e+8>>2]=0,f[e+12>>2]=22,f[e+112>>2]=5288,f[e+100>>2]=f[e+96>>2],A=f[25877],f[e+304>>2]=f[25876],f[e+308>>2]=A,A=f[25879],f[e+312>>2]=f[25878],f[e+316>>2]=A;s:switch(g-29793|0){default:if(27502!=(0|g)){if(28012!=(0|g))break g;A=f[26137],f[e+304>>2]=f[26136],f[e+308>>2]=A,A=f[26139],f[e+312>>2]=f[26138],f[e+316>>2]=A,f[e+600>>2]=3328,f[e+296>>2]=320017171,f[e+300>>2]=320017171,f[e+104>>2]=2098176,f[e+108>>2]=131072,f[e+8>>2]=13;break g}f[e+104>>2]=1,f[e+600>>2]=3200;break g;case 4:break s;case 1:case 2:case 3:break g;case 0:break r}f[e+104>>2]=1,f[e+108>>2]=524288,f[e+600>>2]=3072;break g}f[e+328>>2]=10,f[e+296>>2]=353636370,f[e+300>>2]=336925972,a[e+173|0]=1,f[e+8>>2]=7,f[e+12>>2]=32,a[e+168|0]=2,f[e+84>>2]=1,A=f[26141],f[e+304>>2]=f[26140],f[e+308>>2]=A,f[e+104>>2]=24954==(0|g)?2118920:2114824,A=f[26143],f[e+312>>2]=f[26142],f[e+316>>2]=A;break A}LA(e),f[e+296>>2]=303173650,f[e+300>>2]=303174162,f[e+104>>2]=2131208,f[e+8>>2]=3,f[e+12>>2]=32,A=f[25865],f[e+304>>2]=f[25864],f[e+308>>2]=A,A=f[25867],f[e+312>>2]=f[25866],f[e+316>>2]=A,g=29812;break A}fg(e),g=30059;break A}f[e+112>>2]=21160,f[e+104>>2]=16,f[e+600>>2]=1536,f[e+40>>2]=1;break A}f[e+296>>2]=269488144,f[e+300>>2]=370546198,f[e+8>>2]=0,f[e>>2]=33,f[e+148>>2]=1,f[e+104>>2]=12615688,f[e+16>>2]=2,f[e+100>>2]=f[e+96>>2],f[e+632>>2]=104592,f[e+604>>2]=104592,A=f[26145],f[e+304>>2]=f[26144],f[e+308>>2]=A,A=f[26147],f[e+312>>2]=f[26146],f[e+316>>2]=A,g=30313;break A}if(f[e+296>>2]=370544662,f[e+300>>2]=370546198,f[e+8>>2]=3,f[e+12>>2]=2,f[e+148>>2]=1,f[e+184>>2]=12544,I[e+170>>1]=257,f[e+176>>2]=1,a[e+172|0]=1,f[e>>2]=33,f[e+4>>2]=0,f[e+100>>2]=f[e+96>>2],A=f[26225],f[e+304>>2]=f[26224],f[e+308>>2]=A,A=f[26227],f[e+312>>2]=f[26226],f[e+316>>2]=A,7959909!=(0|g))break A;f[e+112>>2]=24,f[e+104>>2]=1,f[e+108>>2]=1048576,g=7959909;break A}A=f[25873],f[e+304>>2]=f[25872],f[e+308>>2]=A,A=f[25875],f[e+312>>2]=f[25874],f[e+316>>2]=A,f[e+600>>2]=2944,f[e+104>>2]=2097152,f[e+108>>2]=262144,f[e+48>>2]=1}NA(e),a[e+422|0]=2|i[e+422|0];break A}f[e+40>>2]=1}return f[e+212>>2]=g,8&(A=f[e+104>>2])&&(f[e+124>>2]=46,f[e+128>>2]=44),4&A&&(f[e+124>>2]=0),e}function _(A){var e=0,g=0,r=0,C=0,b=0,s=0,t=0,n=0,k=0,o=0,w=0,D=0,u=0,l=0,x=0,d=0,m=0,M=0,v=0,h=0,p=0,Y=0,H=0,N=0,P=0;A:{g=f[32538],f[47354]=0,f[47568]=0,f[49828]=0,f[47569]=0,a[199328]=0,f[49827]=0,f[49845]=0,a[190280]=0,a[190268]=1,f[47202]=0,f[49573]=0,f[49846]=0,a[199304]=0,a[199388]=0,f[33691]=0,f[33285]=0,f[33708]=1,f[33709]=1,f[33288]=0,e=f[33730],f[33712]=f[33729],f[33713]=e,e=f[33732],f[33714]=f[33731],f[33715]=e,e=f[33734],f[33716]=f[33733],f[33717]=e,e=f[33736],f[33718]=f[33735],f[33719]=e,e=f[33738],f[33720]=f[33737],f[33721]=e,e=f[33740],f[33722]=f[33739],f[33723]=e,e=f[33742],f[33724]=f[33741],f[33725]=e,f[33726]=f[33743],a[134784]=0,a[134824]=0,a[134772]=0,a[134760]=0,f[33284]=-1,f[33692]=0,f[32525]=0,f[47201]=f[33717],f[47200]=f[33718],AC(),f[34438]=0,f[34437]=0,e=f[33730],f[34048]=f[33729],f[34049]=e,e=f[33732],f[34050]=f[33731],f[34051]=e,e=f[33734],f[34052]=f[33733],f[34053]=e,e=f[33736],f[34054]=f[33735],f[34055]=e,e=f[33738],f[34056]=f[33737],f[34057]=e,e=f[33740],f[34058]=f[33739],f[34059]=e,e=f[33742],f[34060]=f[33741],f[34061]=e,f[34062]=f[33743];e:{g:{if(1&g){if(f[f[32539]>>2]=0,f[47569]|f[49845]|f[49827])break g;break e}if(f[f[32539]>>2]=0,!(f[49845]|f[49827]||f[47569]))break e}a[190280]=1}f[49828]=0,e=268436735;e:if(!(!f[34391]|!f[34388])&&(f[47204]=0,f[47203]=0,f[47199]=0,f[34439]=0,(f[47192]||!(e=Ue(86228)))&&((e=f[33283])||((e=IA(16))&&(f[e>>2]=0,f[e+4>>2]=0,f[e+8>>2]=0,f[e+12>>2]=0),f[33283]=e),b=268439807,(g=f[f[47192]+328>>2])>>>0>20|!f[129104+(g<<3)>>2]?e=b:(A?(b=4,C=Lg(A)+1|0):(b=2,C=0),f[e+8>>2]=b,f[e>>2]=A,f[e+12>>2]=f[129108+(g<<3)>>2],f[e+4>>2]=A?A+C|0:0,e=0),!e))){j(0);g:{for(;;){f[34436]=0,A=f[34391],f[51290]=A,f[54046]=A+f[34390],f[50767]<=102399&&(f[50767]=102400);r:if(!((s=f[51290])>>>0>=c[54046]))for(;;){if((((0|(A=(C=f[50757])-(t=f[50758])|0))<=0?A+170|0:A)-171|0)>=-1){if((0|(A=f[54731]))<=0)break r;b=0,f[50763]=0,f[50762]=0,f[50765]=2147483647,A=i[218920]?f[54732]:A;C:{for(;;){if(g=A-1|0,f[54732]=g,(0|A)<=0)break C;if(f[51290]=s+1,e=(A=f[51293])+1|0,f[51293]=(0|e)<=5499?e:0,e=(A=G(f[50755],I[205184+(A<<1)>>1]))>>8,a[0|s]=e,C=f[51290],f[51290]=C+1,a[0|C]=A>>>16,(C=f[50756])?(A=g,(g=f[C+4>>2])&&(HC[0|g](e<<16>>16),A=f[54732])):A=g,C=(g=f[51292])+1|0,f[51292]=C,I[205184+(g<<1)>>1]=e,(0|C)>=5500&&(f[51292]=0),s=f[51290],!(c[54046]>=s+2>>>0))break}b=1}a[218920]=b;break r}A=f[(g=216192+(C<<4)|0)+4>>2];C:{a:{I:{f:{i:{b:{s:{t:{n:{k:{o:{B:{c:{Q:{G:{w:switch((255&(e=f[g>>2]))-1|0){case 9:break I;case 7:break f;case 10:break i;case 11:break b;case 13:break s;case 0:break t;case 1:break n;case 2:break k;case 3:break o;case 6:break B;case 5:break c;case 4:break Q;case 15:break G;case 8:break w;default:break a}if(!f[50759])break a;e=f[g+12>>2],g=f[g+8>>2],f[50768]=0,s=g||99232,f[50766]=s,f[50769]=A?2097152/(0|A)|0:0,A=f[50971],C=(0|G(A,f[50788]))/50|0,g=G(C-A|0,-18),A=((0|(A=f[50785]))>=101?101:A)-f[50790]|0,e=(b=g+((0|G(f[50970],i[((0|A)>0?A:0)+105680|0]))/128|0)|0)+((0|G(C,(0|(A=e>>16))<(0|(g=65535&e))?A:g))/2|0)|0,f[50770]=e,A=(b+((0|G(C,(0|A)>(0|g)?A:g))/2|0)|0)-e|0,f[50771]=A,g=i[0|s],f[33072]=f[50976],f[50767]=e+(G(A,g)>>8);break a}HC[f[f[50756]>>2]](A,f[g+8>>2]),mA(A);break a}if(i[218920]||(f[54731]=f[54731]-A),f[50781]=100,f[50773]=0,fe(),f[50763]=0,f[50762]=0,f[50765]=2147483647,!A)break a;for(A=i[218920]?f[54732]:A;;){if(g=A-1|0,f[54732]=g,(0|A)<=0)break a;if(A=f[51290],f[51290]=A+1,C=(e=f[51293])+1|0,f[51293]=(0|C)<=5499?C:0,C=A,e=(A=G(f[50755],I[205184+(e<<1)>>1]))>>8,a[0|C]=e,C=f[51290],f[51290]=C+1,a[0|C]=A>>>16,(C=f[50756])?(A=g,(g=f[C+4>>2])&&(HC[0|g](e<<16>>16),A=f[54732])):A=g,C=(g=f[51292])+1|0,f[51292]=C,I[205184+(g<<1)>>1]=e,(0|C)>=5500&&(f[51292]=0),C=1,!(c[54046]>=f[51290]+2>>>0))break}break C}for(f[50773]=0,f[54731]=f[54729],fe(),e=f[g+12>>2],C=f[g+8>>2],i[218920]?A=f[54733]:f[54734]=0,t=e>>8,b=255&e,f[50762]=0,f[50763]=0;;){if(g=A-1|0,f[54733]=g,(0|A)<=0)break a;if(e=(A=f[54734])+1|0,b?s=G(b,a[A+C|0]):(s=i[A+C|0]|a[e+C|0]<<8,e=A+2|0),f[54734]=e,n=(A=f[51293])+1|0,f[51293]=n,e=(0|(A=(0|(A=((0|G(t,G(f[33037],G(f[33038],s))>>10))/32|0)+(G(f[50755],I[205184+(A<<1)>>1])>>8)|0))<=-32768?-32768:A))>=32767?32767:A,(0|n)>=5500&&(f[51293]=0),a[f[51290]]=e,a[f[51290]+1|0]=e>>>8,(s=f[50756])?(A=g,(g=f[s+12>>2])&&(HC[0|g](e<<16>>16),A=f[54733])):A=g,g=f[51290],f[51290]=g+2,n=(s=f[51292])+1|0,f[51292]=n,I[205184+(s<<1)>>1]=(0|G(e,3))/4,(0|n)>=5500&&(f[51292]=0),!(c[54046]>=g+4>>>0))break}C=1;break C}e=f[g+12>>2],C=A>>>16|0,f[50777]=C,A&=65535,f[50773]=A,b=255&e,f[50774]=b,f[50775]=e>>8,b||(f[50777]=C<<1,f[50773]=A<<1),f[50778]=0,f[50776]=0,f[50772]=f[g+8>>2];break a}f[50773]=0}if(f[54731]=f[54729],e=f[50759],i[218920]){if(!e)break a}else{if(!e)break a;for(b=f[g+12>>2],s=f[g+8>>2],g=A>>16,f[55912]=255&g,a[218960]=1,f[55908]=0,67108864&A&&(f[55908]=3,f[55909]=f[110496+(g>>>6&12)>>2]),134217728&A&&(f[55908]=4,f[55909]=f[110512+(g>>>6&12)>>2]),A&=65504;;){if((0|t)!=(0|(C=(0|(g=C+1|0))<=169?g:0)))if(3!=(0|(g=f[216192+(C<<4)>>2]))){if(g-5>>>0>1)continue}else a[218960]=0;break}for(f[55913]=f[50762],A=(A=A+32&131008)||64,f[50763]=A+f[50763],f[55684]=G(I[101997],7800)+(B[102024]<<8)<<8,f[55704]=G(I[101998],9e3)+(B[102025]<<8)<<8,u=f[50980],r=+(0|A),w=+(A>>>2|0),A=0;7!=(0|A)&&(C=I[(n=(g=A<<1)+e|0)+218>>1]<<8,t=G(A,80)+222176|0,n=I[n+164>>1],D=C+G(n,I[2+(g+s|0)>>1])<<8,f[t>>2]=D,o=+(0|D),Q[t+16>>3]=o,Q[t+48>>3]=16*(+(C+G(n,I[2+(g+b|0)>>1])<<8)-o)/w),C=(g=G(A,80))+222176|0,n=I[(D=e+(A<<1)|0)+182>>1],x=G(n,i[(t=A+s|0)+18|0])<<6,f[C+4>>2]=x,o=+(0|x),Q[C+24>>3]=o,l=C,C=A+b|0,Q[l+56>>3]=64*(+(G(n,i[C+18|0])<<6)-o)/r,(0|A)>(0|u)|A>>>0>5||(n=g+222176|0,x=I[D+200>>1],D=G(x,i[t+26|0])<<10,f[n+8>>2]=D,o=+(0|D),Q[n+32>>3]=o,l=n- -64|0,n=x<<10,Q[l>>3]=64*(+(0|G(n,i[C+26|0]))-o)/r,A>>>0<=2?(g=g+222176|0,t=G(n,i[t+32|0]),f[g+12>>2]=t,o=+(0|t),Q[g+40>>3]=o,Q[g+72>>3]=64*(+(0|G(n,i[C+32|0]))-o)/r):f[g+222188>>2]=D),8!=(0|(A=A+1|0)););}for(;;){if(A=f[50762],!i[218960]&(0|A)==f[50763])break a;k:{o:{if(63&A){if(7&A)break k;B:if(!((0|(g=f[54736]))<=0||(0|(e=f[54735]))<=0))for(C=f[50826],A=1;;){if(f[(s=(b=A<<2)+C|0)>>2]=f[s>>2]+f[b+203312>>2],A>>>0>28|(0|A)>=(0|g))break B;if(b=(0|A)<(0|e),A=A+1|0,!b)break}if((0|(A=f[33073]))>255)break k;f[33073]=A+1;break k}if(A)if(f[50759]){if(g=f[50768]+f[50769]|0,f[50768]=g,g=(e=f[50766])?G(i[e+((0|(g>>=8))>=127?127:g)|0],f[50771])>>8:0,f[55911]=f[55911]+f[55915],e=(0|(e=f[55914]))<=23551?e:0,f[55914]=e+f[50761],e=(g+f[50770]|0)+G(f[33072],i[110528+(e>>6)|0]-128|0)|0,f[50767]=e,!(C=f[51291])&(0|(g=C?C<<12:e))>102399||(e=(0|g)<=102400?102400:g,f[50767]=e),(0|A)!=f[55913]){if(A=0,!((0|(t=f[50980]))<0)){for(;s=G(A,80),r=Q[(g=s+222176|0)+48>>3]+Q[g+16>>3],Q[g+16>>3]=r,w=Q[g+56>>3]+Q[g+24>>3],Q[g+24>>3]=w,o=Q[g- -64>>3]+Q[g+32>>3],Q[g+32>>3]=o,C=E(r)<2147483648?~~r:-2147483648,f[g>>2]=C,C=E(o)<2147483648?~~o:-2147483648,f[g+8>>2]=C,b=E(w)<2147483648?~~w:-2147483648,f[g+4>>2]=(0|b)>0?b:0,(0|A)>2||(r=Q[(g=s+222176|0)+72>>3]+Q[g+40>>3],Q[g+40>>3]=r,C=E(r)<2147483648?~~r:-2147483648),f[s+222188>>2]=C,(0|t)>=(0|(A=A+1|0)););if((0|A)>=8)break o}for(;7!=(0|A)&&(g=G(A,80)+222176|0,r=Q[g+48>>3]+Q[g+16>>3],Q[g+16>>3]=r,C=E(r)<2147483648?~~r:-2147483648,f[g>>2]=C),g=G(A,80)+222176|0,r=Q[g+56>>3]+Q[g+24>>3],Q[g+24>>3]=r,C=E(r)<2147483648?~~r:-2147483648,f[g+4>>2]=(0|C)>0?C:0,8!=(0|(A=A+1|0)););}}else e=f[50767];else f[50826]=218976,f[54742]=0,f[54736]=KA(f[50767]<<4,218976,0),e=f[50767],f[54737]=890/(e>>12),f[54739]=(0|G(f[50781],G(f[50779],e>>8)))/8e4}if(f[55906]=e>>11,f[54735]=f[54736],f[55904]=G(f[50760],e>>7),f[55905]=f[50754]/(e>>12),g=1^(A=f[54742]),f[54742]=g,f[50826]=G(A,1600)+218976,f[54736]=KA(e<<4,G(g,1600)+218976|0,1),!(!(e=f[50759])|!f[51022]))for(w=Q[25430],o=Q[25429],A=1;f[(g=e+(A<<2)|0)+272>>2]&&(C=f[g+308>>2],g=G(A,40)+203456|0,k=Cg(w*+I[2+(G(A,80)+222176|0)>>1]),k*=r=$A(o*+(0|C)),k+=k,Q[g+8>>3]=k,r*=-r,Q[g+16>>3]=r,Q[g>>3]=1-k-r),9!=(0|(A=A+1|0)););}if(b=f[50762]+1|0,f[50762]=b,s=(A=f[50765])+f[55904]|0,f[50765]=s,(0|s)<0&(0|A)>0){if(n=f[55905],e=f[50800]+((0|n)/-2|0)|0,f[55907]=e,(0|(D=f[50763]))<(0|b))break a;if(u=f[54738]+1|0,f[54738]=u,t=f[50767],!((0|(A=(g=f[50980])+1|0))>8)&&(C=t<<3,1&g&&(f[203264+(A<<2)>>2]=(1+(f[G(A,80)+222176>>2]/(0|C)|0)|0)/2,A=g+2|0),7!=(0|g)))for(;g=203264+(A<<2)|0,x=G(A,80)+222176|0,f[g>>2]=(1+(f[x>>2]/(0|C)|0)|0)/2,f[g+4>>2]=(1+(f[x+80>>2]/(0|C)|0)|0)/2,9!=(0|(A=A+2|0)););A=(0|G(f[50781],G(f[50779],t>>8)))/8e4|0,f[54739]=A;k:if(!((0|(g=f[55908]))<=0)){o:switch(g-3|0){case 0:if((D-b|0)>=n<<1)break k;f[55908]=2,A=(0|G(f[55909],A))/256|0,f[54739]=A;break k;case 1:f[55908]=2,A=(0|G(f[55909],A))/256|0,f[54739]=A;break k;default:break o}f[55908]=g-1}(g=f[55910])&&(C=A,A=f[55911]>>8,A=(0|G(C,i[g+((0|A)>=127?127:A)|0]))/128|0,f[54739]=A),(0|(g=f[f[32972]+92>>2]))>7||(g=15&(C=i[f[55912]+(106336+(g<<3)|0)|0]),(C=C>>>4|0)&&(15!=(0|C)?(0|u)%(0|C)|0||(f[54739]=(0|G(A,g))/16):(f[55912]=0,f[54739]=(0|G(A,g))/16)))}else e=f[55907];if(b=e+1|0,f[55907]=b,g=s>>>16|0,C=0,!((0|b)<0|(0|b)>=f[50799])){if(!((0|(A=(e=f[50980])+1|0))>8)){if(t=1&(s=8-e|0),7!=(0|e))for(n=-2&s,e=0;C=G(f[(D=(s=A<<2)+4|0)+203216>>2],I[106400+(G(g,f[D+203264>>2])>>>4&4094)>>1])+(G(f[s+203216>>2],I[106400+(G(g,f[s+203264>>2])>>>4&4094)>>1])+C|0)|0,A=A+2|0,(0|n)!=(0|(e=e+2|0)););t&&(C=G(f[(A<<=2)+203216>>2],I[106400+(G(g,f[A+203264>>2])>>>4&4094)>>1])+C|0)}C=G(i[b+132160|0],(0|C)/f[55906]|0)}if(A=1,(0|(b=f[54737]))<=0)e=g;else for(s=f[50826],e=g;C=G(f[s+(A<<2)>>2],I[106400+((65504&e)>>>4|0)>>1])+C|0,e=e+g|0,(0|b)>=(0|(A=A+1|0)););if((0|(b=f[54735]))>=(0|A))for(s=f[50826];C=C-G(f[s+(A<<2)>>2],I[106400+((65504&e)>>>4|0)>>1])|0,e=e+g|0,(0|b)>=(0|(A=A+1|0)););if(b=64==(0|(A=f[54728]))?C:G(A,C>>6),f[51022]){if(f[50759])for(g=Cr(f[33209],0,1103515245,0),A=U,A=tC(g=g+12345|0,A=g>>>0<12345?A+1|0:A),f[33209]=A,w=+((16383&A)- -8192|0),e=f[50759],s=0,A=1;(C=f[272+(e+(A<<2)|0)>>2])&&(t=f[G(A,80)+222180>>2],g=G(A,40)+203456|0,r=Q[g+32>>3],o=Q[g+24>>3],Q[g+32>>3]=o,r=r*Q[g+16>>3]+(Q[g>>3]*w+o*Q[g+8>>3]),Q[g+24>>3]=r,g=E(r)<2147483648?~~r:-2147483648,s=G(g,G(C,t>>14))+s|0),9!=(0|(A=A+1|0)););else s=0;b=b+s|0}e=0,(0|(A=f[50776]))>=f[50773]||(g=f[50778],e=f[50772],(s=f[50774])?(C=A+1|0,f[50776]=C,A=G(s,a[e+(A+g|0)|0])):(s=i[0|(e=e+(A+g|0)|0)],e=a[e+1|0],C=A+2|0,f[50776]=C,A=s|e<<8),e=(0|G(G(A,f[50780])>>10,f[50775]))/32|0,(0|(A=f[50777]))>(g+C|0)||(f[50778]=g+((0|G(A,3))/-4|0))),g=(A=f[51293])+1|0,f[51293]=g,A=((G(f[54739],b>>8)>>13)+e|0)+(G(f[50755],I[205184+(A<<1)>>1])>>8)|0,(0|g)>=5500&&(f[51293]=0),g=f[33073];k:{o:{if((0|(e=G(g,A)))>=8388608){if((0|g)>=(0|(s=8388608/(0|A)|0)))break o;break k}if((0|e)>-8388353)break k;if((0|g)<(0|(s=-8388608/(0|A)|0)))break k}g=s-1|0,f[33073]=g,e=G(A,g)}if(A=f[51290],f[51290]=A+1,g=A,A=e>>8,a[0|g]=A,g=f[51290],f[51290]=g+1,a[0|g]=e>>>16,(g=f[50756])&&(g=f[g+8>>2])&&HC[0|g](A<<16>>16),e=(g=f[51292])+1|0,f[51292]=e,I[205184+(g<<1)>>1]=A,(0|e)>=5500&&(f[51292]=0),!(c[54046]>=f[51290]+2>>>0))break}C=1;break C}f[50773]=0}f[54731]=f[54729],C=1,t=65535&A,e=i[218920],s=f[g+8>>2],b=f[g+12>>2],r=0,o=0,V=g=V+-64|0,n=f[50759];t:if(6!=(0|(A=f[n+132>>2]))){if(!e){for(A-1>>>0<=4&&(f[55921]=A,f[55964]=f[110896+(A<<2)>>2]),A=f[n+88>>2],f[54741]=1,f[55922]=(0|A)/32,e=f[50758],A=f[50757];;){n:if((0|e)!=(0|(A=(0|(A=A+1|0))<=169?A:0)))if(1!=(0|(D=f[216192+(A<<4)>>2]))){if(D-5>>>0>1)continue}else{if(f[54741]=0,A=f[8+(216192+(A<<4)|0)>>2],!(B[b+4>>1]!=B[A+4>>1]|B[A+6>>1]!=B[b+6>>1]|B[A+8>>1]!=B[b+8>>1]|B[A+10>>1]!=B[b+10>>1])&&B[A+12>>1]==B[b+12>>1])break n;f[54741]=2}break}for((B[s+4>>1]!=B[113564]|B[s+6>>1]!=B[113565]|B[s+8>>1]!=B[113566]|B[s+10>>1]!=B[113567]||B[s+12>>1]!=B[113568])&&(vr(),f[55974]=0,f[55975]=0,f[55972]=0,f[55973]=0,f[55988]=0,f[55989]=0,f[55990]=0,f[55991]=0,f[56004]=0,f[56005]=0,f[56006]=0,f[56007]=0,f[56020]=0,f[56021]=0,f[56022]=0,f[56023]=0,f[56036]=0,f[56037]=0,f[56038]=0,f[56039]=0,f[56052]=0,f[56053]=0,f[56054]=0,f[56055]=0,f[56068]=0,f[56069]=0,f[56070]=0,f[56071]=0,f[56086]=0,f[56087]=0,f[56084]=0,f[56085]=0,f[56102]=0,f[56103]=0,f[56100]=0,f[56101]=0,f[56118]=0,f[56119]=0,f[56116]=0,f[56117]=0,f[56134]=0,f[56135]=0,f[56132]=0,f[56133]=0,f[56150]=0,f[56151]=0,f[56148]=0,f[56149]=0,f[56166]=0,f[56167]=0,f[56164]=0,f[56165]=0,f[56182]=0,f[56183]=0,f[56180]=0,f[56181]=0,f[56198]=0,f[56199]=0,f[56196]=0,f[56197]=0,f[56214]=0,f[56215]=0,f[56212]=0,f[56213]=0,f[56230]=0,f[56231]=0,f[56228]=0,f[56229]=0),A=B[b+4>>1]|B[b+6>>1]<<16,e=B[b>>1]|B[b+2>>1]<<16,I[113562]=e,I[113563]=e>>>16,I[113564]=A,I[113565]=A>>>16,A=B[b+60>>1]|B[b+62>>1]<<16,e=B[b+56>>1]|B[b+58>>1]<<16,I[113590]=e,I[113591]=e>>>16,I[113592]=A,I[113593]=A>>>16,A=B[b+52>>1]|B[b+54>>1]<<16,e=B[b+48>>1]|B[b+50>>1]<<16,I[113586]=e,I[113587]=e>>>16,I[113588]=A,I[113589]=A>>>16,A=B[b+44>>1]|B[b+46>>1]<<16,e=B[b+40>>1]|B[b+42>>1]<<16,I[113582]=e,I[113583]=e>>>16,I[113584]=A,I[113585]=A>>>16,A=B[b+36>>1]|B[b+38>>1]<<16,e=B[b+32>>1]|B[b+34>>1]<<16,I[113578]=e,I[113579]=e>>>16,I[113580]=A,I[113581]=A>>>16,A=B[b+28>>1]|B[b+30>>1]<<16,e=B[b+24>>1]|B[b+26>>1]<<16,I[113574]=e,I[113575]=e>>>16,I[113576]=A,I[113577]=A>>>16,A=B[b+20>>1]|B[b+22>>1]<<16,e=B[b+16>>1]|B[b+18>>1]<<16,I[113570]=e,I[113571]=e>>>16,I[113572]=A,I[113573]=A>>>16,A=B[b+12>>1]|B[b+14>>1]<<16,e=B[b+8>>1]|B[b+10>>1]<<16,I[113566]=e,I[113567]=e>>>16,I[113568]=A,I[113569]=A>>>16,w=+(0|t),(l=1&I[s>>1])?(A=i[s+39|0],f[56680]=A,Q[28364]=A>>>0,Q[28354]=+(i[b+39|0]-A<<6)/w,A=i[s+40|0],Q[28366]=A>>>0,Q[28356]=+(i[b+40|0]-A<<6)/w,A=i[s+41|0],f[56682]=A,Q[28368]=A>>>0,Q[28358]=+(i[b+41|0]-A<<6)/w,e=i[s+42|0],f[56684]=e,Q[28370]=e>>>0,A=i[s+43|0],r=+(i[b+43|0]-A<<6)/w,o=+(i[b+42|0]-e<<6)/w,k=+(A>>>0)):(f[56728]=0,f[56729]=0,A=0,f[56680]=0,f[56708]=0,f[56709]=0,f[56732]=0,f[56733]=0,f[56712]=0,f[56713]=0,f[56682]=0,f[56736]=0,f[56737]=0,f[56716]=0,f[56717]=0,f[56684]=0,f[56740]=0,f[56741]=0,k=0),f[56688]=A,Q[28360]=o,Q[28372]=k,Q[28362]=r,f[56692]=0,f[56748]=0,f[56749]=0,f[56694]=0,f[56752]=0,f[56753]=0,f[56696]=0,f[56756]=0,f[56757]=0,f[56700]=0,f[56760]=0,f[56761]=0,f[56704]=0,f[56764]=0,f[56765]=0,f[50764]=t,A=1;u=I[(D=(t=A<<1)+n|0)+164>>1],e=(h=G(A,80))+222896|0,o=+I[D+218>>1],r=.00390625*+(0|G(u,I[2+(s+t|0)>>1]))+o,Q[e+16>>3]=r,x=E(r)<2147483648?~~r:-2147483648,f[e>>2]=x,Q[e+48>>3]=64*(.00390625*+(0|G(u,I[2+(b+t|0)>>1]))+o-r)/w,A>>>0<=3&&(e=h+222896|0,r=.00390625*+I[D+200>>1]*+(i[35+(A+s|0)|0]<<1),Q[e+24>>3]=r,t=E(r)<2147483648?~~r:-2147483648,f[e+4>>2]=t,Q[e+56>>3]=64*(+(i[35+(A+b|0)|0]<<1)-r)/w),6!=(0|(A=A+1|0)););if(r=+((A=i[s+40|0])<<1),Q[27864]=r,e=f[56618],A||(r=+(0|e),Q[27864]=r),A=E(r)<2147483648?~~r:-2147483648,f[55724]=A,t=i[b+40|0],f[55730]=0,f[55731]=1079394304,f[55738]=0,f[55739]=0,f[55725]=89,A=1,Q[27868]=64*(+(0|(t?t<<1:e))-r)/w,l)for(;e=G(A,80)+222896|0,n=i[(t=A+s|0)+56|0]<<2,f[e+12>>2]=n,r=+(0|n),Q[e+40>>3]=r,n=A+b|0,Q[e+72>>3]=64*(+(i[n+56|0]<<2)-r)/w,t=i[t+49|0],f[e+8>>2]=t,r=+(t>>>0),Q[e+32>>3]=r,Q[e- -64>>3]=64*(+i[n+49|0]-r)/w,7!=(0|(A=A+1|0)););f[56606]=0}for(;;){if((0|(x=f[50764]))>(0|(b=f[56606]))){for(A=f[50767],f[56609]=f[55724],f[56619]=f[55725],f[56610]=f[55744],f[56611]=f[55764],f[56612]=f[55784],f[56613]=f[55804],l=G(A,10),f[56607]=(0|l)/4096,f[56620]=f[55745],f[56621]=f[55765],f[56622]=f[55785],f[56614]=f[55824],f[56630]=f[55746],f[56631]=f[55766],f[56632]=f[55786],f[56633]=f[55806],f[56634]=f[55826],f[56635]=f[55846],h=f[56680],f[56608]=h,s=f[56694],f[56656]=s,t=f[56696],f[56653]=t,n=f[56700],f[56655]=n,D=f[56684],f[56649]=D,f[56651]=f[56704],f[56654]=f[56688],f[56652]=f[56682],f[56650]=f[56692],A=0;e=G(A,80)+222896|0,r=Q[e+48>>3]+Q[e+16>>3],Q[e+16>>3]=r,w=Q[e+56>>3]+Q[e+24>>3],Q[e+24>>3]=w,o=Q[e+72>>3]+Q[e+40>>3],Q[e+40>>3]=o,k=Q[e- -64>>3]+Q[e+32>>3],Q[e+32>>3]=k,u=E(r)<2147483648?~~r:-2147483648,f[e>>2]=u,u=E(w)<2147483648?~~w:-2147483648,f[e+4>>2]=u,u=E(o)<2147483648?~~o:-2147483648,f[e+12>>2]=u,u=E(k)<2147483648?~~k:-2147483648,f[e+8>>2]=u,9!=(0|(A=A+1|0)););for(r=Q[28354]+Q[28364],Q[28364]=r,Q[28366]=Q[28356]+Q[28366],w=Q[28358]+Q[28368],Q[28368]=w,o=Q[28360]+Q[28370],Q[28370]=o,k=Q[28362]+Q[28372],Q[28372]=k,A=E(r)<2147483648?~~r:-2147483648,f[56680]=A,A=E(w)<2147483648?~~w:-2147483648,f[56682]=A,A=E(o)<2147483648?~~o:-2147483648,f[56684]=A,A=E(k)<2147483648?~~k:-2147483648,f[56688]=A,r=Q[28374]+0,Q[28374]=r,A=E(r)<2147483648?~~r:-2147483648,f[56692]=A,r=Q[28376]+0,Q[28376]=r,A=E(r)<2147483648?~~r:-2147483648,f[56694]=A,r=Q[28378]+0,Q[28378]=r,A=E(r)<2147483648?~~r:-2147483648,f[56696]=A,r=Q[28380]+0,Q[28380]=r,A=E(r)<2147483648?~~r:-2147483648,f[56700]=A,r=Q[28382]+0,Q[28382]=r,A=E(r)<2147483648?~~r:-2147483648,f[56704]=A,f[56659]=f[55724],f[56669]=f[55725],f[56660]=f[55744],f[56670]=f[55745],f[56661]=f[55764],f[56671]=f[55765],f[56662]=f[55784],f[56672]=f[55785],f[56663]=f[55804],f[56664]=f[55824],f[56665]=f[55844],A=f[50768]+f[50769]|0,f[50768]=A,A>>=8,f[50767]=f[50770]+(G(f[50771],i[f[50766]+((0|A)>=127?127:A)|0])>>8),A=x-b|0,f[55923]=(0|A)>=64?64:A,f[55961]=(0|l)/40960,A=h-7|0,f[56658]=(0|A)>0?A:0,Q[27974]=D>>>0<=87?.001*+I[111136+(D<<1)>>1]*.05:0,Q[27975]=t>>>0<=87?.001*+I[111136+(t<<1)>>1]*.25:0,Q[27973]=s>>>0<=87?.001*+I[111136+(s<<1)>>1]:0,Q[27971]=n>>>0<=87?.001*+I[111136+(n<<1)>>1]*.05:0,r=(A=f[56629])>>>0<=87?.001*+I[111136+(A<<1)>>1]*.6:0,Q[g>>3]=r,r=(A=f[56630])>>>0<=87?.001*+I[111136+(A<<1)>>1]*.4:0,Q[g+8>>3]=r,r=(A=f[56631])>>>0<=87?.001*+I[111136+(A<<1)>>1]*.15:0,Q[g+16>>3]=r,r=(A=f[56632])>>>0<=87?.001*+I[111136+(A<<1)>>1]*.06:0,Q[g+24>>3]=r,r=(A=f[56633])>>>0<=87?.001*+I[111136+(A<<1)>>1]*.04:0,Q[g+32>>3]=r,r=(A=f[56634])>>>0<=87?.001*+I[111136+(A<<1)>>1]*.022:0,Q[g+40>>3]=r,r=(A=f[56635])>>>0<=87?.001*+I[111136+(A<<1)>>1]*.03:0,Q[g+48>>3]=r,r=(A=(0|(A=f[56657]-3|0))<=0?57:A)>>>0<=87?.001*+I[111136+(A<<1)>>1]:0,Q[27977]=r/+f[55964],w=Q[27968],o=Q[27967],A=1;e=(b=A<<6)+223664|0,k=(r=$A(o*+f[(t=(s=A<<2)+226428|0)+48>>2]))*-r,Q[e+216>>3]=k,r*=Cg(w*+f[t+8>>2]),r+=r,Q[e+208>>3]=r,M=1-r-k,Q[e+200>>3]=M,A>>>0<=5&&(t=f[(s=s+226428|0)+208>>2],e=b+223664|0,m=(d=$A(o*+f[s+248>>2]))*Cg(w*+(0|t)),m+=m,Q[e+1488>>3]=m,d*=-d,Q[e+1496>>3]=d,v=1-m-d,Q[e+1480>>3]=v,Q[e+256>>3]=.015625*(d-k),Q[e+248>>3]=.015625*(m-r),Q[e+240>>3]=.015625*(v-M)),10!=(0|(A=A+1|0)););for(d=(r=$A(o*+f[56619]))*-r,Q[27985]=d,m=(r*=Cg(w*+(0-f[56609]|0)))+r,Q[27984]=m,r=1-m-d,Q[27983]=r,0!=r&&(r=1/r,Q[27983]=r,d*=k=-r,Q[27985]=d,m*=k,Q[27984]=m),M=(k=$A(o*+f[56669]))*-k,Q[28145]=M,v=(k*=Cg(w*+(0-f[56659]|0)))+k,Q[28144]=v,k=1-v-M,Q[28143]=k,0!=k&&(k=1/k,Q[28143]=k,M*=H=-k,Q[28145]=M,v*=H,Q[28144]=v),Q[27990]=.015625*(M-d),Q[27989]=.015625*(v-m),Q[27988]=.015625*(k-r),A=0;s=f[(b=226428+(A<<2)|0)+8>>2],e=223664+(A<<6)|0,k=(r=$A(o*+f[b+128>>2]))*Cg(w*+(0|s)),k+=k,Q[e+848>>3]=k,r*=-r,Q[e+856>>3]=r,Q[e+840>>3]=Q[g+(A<<3)>>3]*(1-k-r),7!=(0|(A=A+1|0)););if(o=(r=$A(o*+(f[55918]/2|0)))*-r,Q[28137]=o,r*=Cg(0*w),r+=r,Q[28136]=r,Q[28135]=1-r-o,A=1,1!=(0|BA()))continue;break t}break}A=1,f[54741]>0&&(f[54741]=0,f[55963]=64,f[56606]=b+-64,f[55923]=64,1==(0|BA()))||(A=0)}else{if(V=D=V-752|0,!e){ue(A=D+376|0,0,376),jA(n,s,A),jA(n,b,A=ue(D,0,376)),e=f[50768]+G(f[50769],t>>>6|0)|0,f[50768]=e,e>>=8,e=f[50770]+(G(f[50771],i[f[50766]+((0|e)>=127?127:e)|0])>>8)|0,f[50767]=e,Q[A+368>>3]=(0|e)/4096|0,f[50773]&&(Q[A+736>>3]=Q[A+736>>3]/5,Q[A+360>>3]=Q[A+360>>3]/5),e=f[f[56797]+4>>2],HC[f[f[e>>2]>>2]](e,A+376|0,110,110,-1,0),b=t-110|0,n=f[50758],e=f[50757];n:{for(;;){if((0|n)!=(0|(e=(e+1|0)%170|0))&&!((u=f[216192+(e<<4)>>2])-5>>>0<2)){if(s=1,1!=(0|u))continue;break n}break}b=t-220|0,s=0}(0|b)>0&&(e=f[f[56797]+4>>2],HC[f[f[e>>2]>>2]](e,A,b,b||1,-1,0)),s||(f[A+352>>2]=0,f[A+356>>2]=0,Q[A>>3]=Q[A+368>>3],e=f[f[56797]+4>>2],HC[f[f[e>>2]>>2]](e,A,55,55,-1,0),f[A+360>>2]=0,f[A+364>>2]=0,e=f[f[56797]+4>>2],HC[f[f[e>>2]>>2]](e,A,55,55,-1,0))}A=f[f[56797]+8>>2],e=f[51290],t=0|HC[f[f[A>>2]>>2]](A,f[54046]-e>>>1|0,e),n=f[51290];n:if(t&&!((0|(e=f[50776]))>=(0|(h=f[50773]))))for(p=f[50777],N=(0|G(p,3))/-4|0,r=.0009765625*+f[50780],u=f[50772],b=f[50778],P=f[50775],x=f[50774],A=0;;){if(l=i[u+(s=e+b|0)|0],x?l=G(x,l<<24>>24):(e=e+1|0,f[50776]=e,l|=a[u+(s=e+b|0)|0]<<8),l=E(w=r*+(0|l))<2147483648?~~w:-2147483648,I[(Y=n+(A<<1)|0)>>1]=B[Y>>1]+((0|G(l,P))/40|0),(0|s)>=(0|p)&&(b=b+N|0,f[50778]=b),e=e+1|0,f[50776]=e,(0|e)>=(0|h))break n;if(!(t>>>0>(A=A+1|0)>>>0))break}A=n+(t<<1)|0,f[51290]=A,V=D+752|0,A=c[54046]<=A>>>0}if(V=g- -64|0,A)break C;break a}f[50781]=A||100;break a}VA(A,f[g+8>>2]);break a}A=f[g+8>>2],f[50759]=_A(203816,A,1344),f[50801]=f[A+108>>2]?105792:106064,A=(0|G(f[A+120>>2],26))/100|0,f[33038]=A,(0|(C=f[50754]))<=11e3&&(a[203300]=1,f[33038]=A<<1),f[54728]=f[50982],A=f[50979],e=f[50978],ue(205184,0,11e3),f[51293]=0,A=(e=(s=(0|(b=f[50789]))>0)?130:(0|e)>=5499?5499:e)?s?b:(0|A)>=100?100:A:0,f[50755]=A,e=(0|G(e,C))/1e3|0,f[51292]=e,f[54729]=(0|A)>20?e<<1:A?e:0,f[33037]=(0|G(500-A|0,(0|G(i[f[50797]+105596|0],(0|G(f[50787],55))/100|0))/16|0))/500,A=256,(0|(e=(0|(e=f[50785]))>=101?101:e))>=51&&(A=256+(((G(e,25)-1250&65535)>>>0)/50|0)|0),I[101990]=(0|G(I[102026],A))/256,I[101991]=(0|G(I[102027],A))/256,I[101992]=(0|G(I[102028],A))/256,I[101993]=(0|G(I[102029],A))/256,I[101994]=(0|G(I[102030],A))/256,I[101995]=(0|G(I[102031],A))/256,A=f[50790],I[101999]=(0|G(I[102035],G(A,-6)+256|0))/256,I[102e3]=(0|G(I[102036],G(A,-3)+256|0))/256,Ze(8,0,f[50986],0,f[51290]),mA(f[g+8>>2]);break a}if(!f[50759])break a;e=f[g+12>>2],g=f[g+8>>2],f[55911]=0,f[55915]=A?2097152/(0|A)|0:0,f[55910]=g,A=(0|G(e,f[33037]))/16|0,f[50779]=A,f[50780]=(0|G(G(A,f[50985]),15))/100;break a}Ze(e>>8,A,f[g+8>>2],f[g+12>>2],s)}C=0,A=f[50757]+1|0,f[50757]=(0|A)<=169?A:0}if(a[218920]=C,!((s=f[51290])>>>0>2]=0,f[A>>2]=0,f[A+24>>2]=f[34438];r:if(2&(A=f[32538])){if(s=f[34388],2==(-2&A)&&(!(g=e?s:0)|8!=f[g>>2]||(0|(g=f[g+28>>2]))!=f[34389]&&(f[34389]=g)),C=1,(0|e)<2)break r;for(;;){g=e?s+G(C,36)|0:0;C:{a:switch(0|A){case 2:case 3:if(!g|8!=f[g>>2])break C;if((0|(g=f[g+28>>2]))==f[34389])break C;f[34389]=g;break C;case 0:break a;default:break C}(A=f[34440])?(HC[0|A](b,0,g),e=f[34436],A=f[32538]):A=0}if(!((0|(C=C+1|0))<(0|e)))break}}else if((A=f[34440])&&0|HC[0|A](b,g,f[34388]))break g;if(!$(1)&&!(170-((0|(A=f[50757]-f[50758]|0))<=0?A+170|0:A)|0||(A=f[34388],f[A>>2]=0,f[A+4>>2]=f[34437],f[A+24>>2]=f[34438],j(1))))break}if(e=0,2&i[130152])break e;if(!(A=f[34440]))break e;if(!(0|HC[0|A](0,0,f[34388])))break e}j(2),e=268439295}if((0|e)<=268437502){if(!e|268436479==(0|e)|268437247!=(0|e))break A;return}}}function $(A){var e,g=0,r=0,C=0,b=0,s=0,t=0,n=0,k=0,o=0,c=0,Q=0,G=0,w=0,E=0,D=0,u=0,l=0,x=0,d=0,m=0,M=0;V=e=V-720|0,g=f[47198],A||(f[36443]=0,f[36442]=1,f[36444]=0,f[36440]=0,f[36441]=0,A=f[50758],f[36454]=A,f[36427]=-1,f[36424]=-1,f[36446]=0,f[36447]=0,f[36439]=-1,f[36426]=0,f[36455]=A,f[36448]=0,f[36449]=0,f[36450]=0,f[36451]=0,f[36452]=0,f[36453]=0,kA(),f[36427]=-1,A=216192+(f[50758]<<4)|0,f[A>>2]=5,f[A+4>>2]=0,A=f[50758]+1|0,f[50758]=(0|A)<=169?A:0,f[36426]=0,f[36438]&&(f[36438]=0,A=216192+(f[50758]<<4)|0,f[A>>2]=14,f[A+4>>2]=0,A=f[50758]+1|0,f[50758]=(0|A)<=169?A:0));A:{e:if(!((0|(A=f[36442]))>997|f[36423]<=(0|A)))for(w=g>>>1&1,E=e+48|4,D=e+348|0,u=e+652|0,l=f[32322],c=f[32320],x=e+60|0,d=e+648|0,m=e+56|0,M=e+620|0,Q=e- -64|0;;){if(r=145840+(A<<5)|0,!(A=f[50756])|!f[A>>2]||(f[e+12>>2]=0,Ce(A=e+16|0,f[r+8>>2],r,0,e+12|0),g=$r(A),C=i[r+17|0],A=216192+(f[50758]<<4)|0,f[A>>2]=16,f[A+8>>2]=C,f[A+4>>2]=g,A=f[50758]+1|0,f[50758]=(0|A)<=169?A:0),A=1,(0|(C=(0|(g=f[50757]-f[50758]|0))<=0?g+170|0:g))<=(0|((g=i[r+17|0])?2==(0|g)?25:15:10)))break A;if(k=f[36442],2&i[0|r])for(o=2047&B[r+4>>1];;){if(C=f[36443],t=127&(g=f[198304+(C<<2)>>2])){A=g>>>8|0,f[36443]=C+1;g:{r:{C:switch((31&g)-2|0){case 0:VA(96&g|8,A),UA(2);break g;case 5:if((0|A)>=f[34064])break g;if(!f[(t=(C=A<<4)+136272|0)+4>>2])break g;he(10,0),A=216192+(f[50758]<<4)|0,f[A>>2]=6,t=f[t+4>>2],f[A+8>>2]=f[8+(C+136272|0)>>2]+44,f[A+12>>2]=5376,f[A+4>>2]=t;break r;case 8:if((0|((0|(C=f[50757]-f[50758]|0))<=0?C+170:C))<6)break g;t=f[47353],C=216192+(f[50758]<<4)|0,f[C>>2]=778,f[C+8>>2]=A,f[C+4>>2]=t+o&16777215;break r;case 9:if((0|((0|(C=f[50757]-f[50758]|0))<=0?C+170:C))<6)break g;t=f[33284],C=216192+(f[50758]<<4)|0,f[C>>2]=1034,f[C+8>>2]=A,f[C+4>>2]=t+1&16777215;break r;default:break C}he(10,0),C=216192+(f[50758]<<4)|0,f[C>>2]=12,f[C+8>>2]=A,f[C+4>>2]=t}A=f[50758]+1|0,f[50758]=(0|A)<=169?A:0}if(!(128&g))continue}break}(A=i[r+20|0])&&(1&a[f[47192]+48|0]&2==i[r+17|0]|1&a[f[r+8>>2]+7|0]||(f[36426]=0),g=f[47353]+(2047&B[r+4>>1])|0,f[36445]=g,4&A&&((0|((0|(A=f[50757]-f[50758]|0))<=0?A+170:A))<6||(C=f[47568],A=216192+(f[50758]<<4)|0,f[A>>2]=522,f[A+8>>2]=C,f[A+4>>2]=16777215&g,A=f[50758]+1|0,f[50758]=(0|A)<=169?A:0)),1&a[r+20|0]&&(g=B[r+4>>1],C=f[36444],f[36444]=C+1,(0|((0|(A=f[50757]-f[50758]|0))<=0?A+170:A))<6||(o=f[36445],t=f[47355],A=216192+(f[50758]<<4)|0,f[A>>2]=266,f[A+8>>2]=C+t,f[A+4>>2]=16777215&o|(63488&g)<<13,A=f[50758]+1|0,f[50758]=(0|A)<=169?A:0))),(0|(A=f[36441]))>0&&(g=216192+(f[36425]<<4)|0,f[g+4>>2]||(f[g+4>>2]=A),f[36441]=0),A=k+1<<5,g=k-1<<5,!(C=i[r+18|0])|2&i[f[r+8>>2]+7|0]||he(C,1),t=A+145840|0,b=g+145840|0,o=1;g:{r:{if(f[47198]&&(n=f[r+8>>2],15!=i[n+10|0])){C:if(2==i[r+17|0])switch(i[b+17|0]-3|0){case 0:case 5:break r;default:break C}o=0,Ce(e+704|0,n,r,w,0),(0|((0|(A=f[50757]-f[50758]|0))<=0?A+170:A))<6||(g=f[36445],A=216192+(f[50758]<<4)|0,f[A>>2]=1802,f[A+4>>2]=16777215&g,g=f[e+708>>2],f[A+8>>2]=f[e+704>>2],f[A+12>>2]=g,A=f[50758]+1|0,f[50758]=(0|A)<=169?A:0)}C:switch(i[r+17|0]){case 0:he(f[r+12>>2],0),a[r+23|0]=i[f[r+8>>2]+14|0];break g;case 4:g=f[r+8>>2],2==(0|(A=i[t+17|0]))|!i[t+20|0]&3==(0|A)||(I[r>>1]=8192|B[r>>1]),2&i[g+7|0]&&(f[e+88>>2]=0,f[e+92>>2]=0,f[e+80>>2]=0,f[e+84>>2]=0,f[e+72>>2]=0,f[e+76>>2]=0,f[Q>>2]=0,f[Q+4>>2]=0,f[e+56>>2]=0,f[e+60>>2]=0,f[e+48>>2]=0,f[e+52>>2]=0,bA(0,1,r,e+552|0,145784),f[e+56>>2]=f[e+620>>2],f[Q>>2]=f[e+640>>2],f[36424]<0&&(C=i[t+19|0],A=f[50758],f[36425]=A,f[36441]=0,f[(A=216192+(A<<4)|0)+12>>2]=C,f[A+8>>2]=0,f[A>>2]=8,f[A+4>>2]=0,A=f[50758]+1|0,f[50758]=(0|A)<=169?A:0,o=i[t+22|0],C=i[t+21|0],t=f[129280+(i[r+16|0]<<2)>>2],(0|(A=f[36424]))<0|(0|(n=f[36440]))<=0||f[(A=216192+(A<<4)|0)+4>>2]||(f[A+4>>2]=n),A=f[50758],f[36424]=A,f[36440]=0,f[(A=216192+(A<<4)|0)>>2]=9,f[A+4>>2]=0,o|=C<<16,C=255==(0|C),f[A+12>>2]=C?3604556:o,f[A+8>>2]=C?c:t,A=f[50758]+1|0,f[50758]=(0|A)<=169?A:0),eA(g,0,e+48|0,r,0)),bA(0,0,r,e+552|0,145784),f[e+552>>2]=4|f[e+552>>2],(0|(A=f[36440]))<=0||(0|(g=f[36424]))<0||(f[(g=216192+(g<<4)|0)+4>>2]||(f[g+4>>2]=A),f[36440]=0),f[36426]=0,f[36439]=-1,f[36455]=f[50758],kA(),f[36427]=-1,f[36422]=0,(A=f[e+624>>2])&&(C=A,A=f[e+644>>2],qA(C,2,f[e+596>>2]<<1,f[e+552>>2],0,A?(A<<5)/100|0:32)),f[36426]=0;break g;case 6:bA(0,0,r,e+552|0,145784),8&i[0|r]&&(A=f[r+12>>2],(0|(g=f[36440]))<=0||(0|(C=f[36424]))<0||(f[(C=216192+(C<<4)|0)+4>>2]||(f[C+4>>2]=g),f[36440]=0),f[36426]=0,f[36439]=-1,f[36455]=f[50758],kA(),f[36427]=-1,f[36422]=0,(g=f[e+624>>2])&&(C=A,A=f[e+644>>2],qA(g,2,f[e+596>>2]<<1,f[e+552>>2],C,A?(A<<5)/100|0:32))),A=f[r+12>>2],(0|(g=f[36440]))<=0||(0|(C=f[36424]))<0||(f[(C=216192+(C<<4)|0)+4>>2]||(f[C+4>>2]=g),f[36440]=0),f[36426]=0,f[36439]=-1,f[36455]=f[50758],kA(),f[36427]=-1,f[36422]=0,(g=f[e+624>>2])&&(C=A,A=f[e+644>>2],qA(g,2,f[e+596>>2]<<1,f[e+552>>2],C,A?(A<<5)/100|0:32)),f[36426]=0;break g;case 5:C=f[r+8>>2],f[E+40>>2]=0,f[(A=E)+32>>2]=0,f[A+36>>2]=0,f[A+24>>2]=0,f[A+28>>2]=0,f[A+16>>2]=0,f[A+20>>2]=0,f[A+8>>2]=0,f[A+12>>2]=0,f[A>>2]=0,f[A+4>>2]=0,f[e+48>>2]=4;a:{I:{f:{i:switch(i[t+17|0]-2|0){case 0:g=i[r+19|0],A=f[50758],f[36425]=A,f[36441]=0,f[(A=216192+(A<<4)|0)+12>>2]=g,f[A+8>>2]=0,f[A>>2]=8,f[A+4>>2]=0,A=f[50758]+1|0,f[50758]=(0|A)<=169?A:0,g=i[r+22|0],n=i[r+21|0],o=f[129280+(i[r+16|0]<<2)>>2],(0|(A=f[36424]))<0|(0|(s=f[36440]))<=0||f[(A=216192+(A<<4)|0)+4>>2]||(f[A+4>>2]=s),A=(s=!(255&~n))?c:o,o=1;break I;case 1:break i;default:break f}if(!i[t+20|0]){g=i[t+19|0],A=f[50758],f[36425]=A,f[36441]=0,f[(A=216192+(A<<4)|0)+12>>2]=g,f[A+8>>2]=0,f[A>>2]=8,f[A+4>>2]=0,A=f[50758]+1|0,f[50758]=(0|A)<=169?A:0,g=i[t+22|0],n=i[t+21|0],o=f[129280+(i[t+16|0]<<2)>>2],(0|(A=f[36424]))<0|(0|(s=f[36440]))<=0||f[(A=216192+(A<<4)|0)+4>>2]||(f[A+4>>2]=s),A=(s=!(255&~n))?c:o,o=1;break I}}if(o=0,f[36424]>=0)break a;g=i[t+19|0],A=f[50758],f[36425]=A,f[36441]=0,f[(A=216192+(A<<4)|0)+12>>2]=g,f[A+8>>2]=0,f[A>>2]=8,f[A+4>>2]=0,A=f[50758]+1|0,f[50758]=(0|A)<=169?A:0,g=i[r+22|0],n=i[r+21|0],o=f[129280+(i[r+16|0]<<2)>>2],(0|(A=f[36424]))<0|(0|(s=f[36440]))<=0||f[(A=216192+(A<<4)|0)+4>>2]||(f[A+4>>2]=s),A=(s=!(255&~n))?c:o,o=0}G=f[50758],f[36424]=G,f[36440]=0,f[(G=216192+(G<<4)|0)>>2]=9,f[G+4>>2]=0,f[G+12>>2]=s?3604556:255&g|(255&n)<<16,f[G+8>>2]=A,A=f[50758]+1|0,f[50758]=(0|A)<=169?A:0}a:if(!(2&i[C+7|0])&2!=i[b+17|0])8&i[0|r]&&he(50,0);else{if(bA(0,1,r,e+552|0,145784),f[e+56>>2]=f[e+620>>2],f[e+64>>2]=f[e+640>>2],eA(C,0,e+48|0,r,0),!(8&i[0|r]))break a;he(25,1),eA(C,0,e+48|0,r,0)}a:if(o){if(f[36455]!=f[36454])break a;f[36455]=f[50758]}else I[r>>1]=8192|B[r>>1];if(bA(0,0,r,e+552|0,145784),f[e+56>>2]=f[e+620>>2],f[e+64>>2]=f[e+640>>2],f[e+76>>2]=f[e+636>>2],f[e+80>>2]=f[e+656>>2],eA(C,0,e+48|0,r,0),i[r+20|0]|i[84+(145840+(k<<5)|0)|0])break g;if(7==(0|(A=i[t+17|0]))&&(he(20,0),A=i[t+17|0]),6!=(255&A))break g;he(12,0);break g;case 7:a:{I:{f:{i:{b:switch((A=i[t+17|0])-2|0){case 1:break i;case 0:break b;default:break f}g=i[r+19|0],A=f[50758],f[36425]=A,f[36441]=0,f[(A=216192+(A<<4)|0)+12>>2]=g,f[A+8>>2]=0,f[A>>2]=8,f[A+4>>2]=0,A=f[50758]+1|0,f[50758]=(0|A)<=169?A:0,o=i[r+22|0],A=i[r+21|0],C=f[129280+(i[r+16|0]<<2)>>2],(0|(g=f[36424]))<0|(0|(n=f[36440]))<=0||f[(g=216192+(g<<4)|0)+4>>2]||(f[g+4>>2]=n),g=(n=!(255&~A))?c:C;break I}g=i[t+19|0],A=f[50758],f[36425]=A,f[36441]=0,f[(A=216192+(A<<4)|0)+12>>2]=g,f[A+8>>2]=0,f[A>>2]=8,f[A+4>>2]=0,A=f[50758]+1|0,f[50758]=(0|A)<=169?A:0,o=i[t+22|0],A=i[t+21|0],C=f[129280+(i[t+16|0]<<2)>>2],(0|(g=f[36424]))<0|(0|(n=f[36440]))<=0||f[(g=216192+(g<<4)|0)+4>>2]||(f[g+4>>2]=n),g=(n=!(255&~A))?c:C;break I}if(f[36424]>=0)break a;g=i[r+19|0],A=f[50758],f[36425]=A,f[36441]=0,f[(A=216192+(A<<4)|0)+12>>2]=g,f[A+8>>2]=0,f[A>>2]=8,f[A+4>>2]=0,A=f[50758]+1|0,f[50758]=(0|A)<=169?A:0,o=i[r+22|0],A=i[r+21|0],C=f[129280+(i[r+16|0]<<2)>>2],(0|(g=f[36424]))<0|(0|(n=f[36440]))<=0||f[(g=216192+(g<<4)|0)+4>>2]||(f[g+4>>2]=n),g=(n=!(255&~A))?c:C}C=f[50758],f[36424]=C,f[36440]=0,f[(C=216192+(C<<4)|0)>>2]=9,f[C+4>>2]=0,f[C+12>>2]=n?3604556:(255&A)<<16|o,f[C+8>>2]=g,A=f[50758]+1|0,f[50758]=(0|A)<=169?A:0,A=i[t+17|0]}a:{I:{f:switch((255&A)-2|0){case 1:if(i[t+20|0])break I;break;case 0:break f;default:break I}if(f[36455]!=f[36454])break a;f[36455]=f[50758];break a}I[r>>1]=8192|B[r>>1]}bA(0,0,r,e+552|0,145784),f[e+56>>2]=0,f[e+60>>2]=0,f[Q>>2]=0,f[Q+4>>2]=0,f[e+80>>2]=0,f[e+84>>2]=0,f[e+72>>2]=0,f[e+76>>2]=0,f[e+88>>2]=0,f[e+56>>2]=f[e+620>>2],f[Q>>2]=f[e+640>>2],f[e+80>>2]=f[e+656>>2],f[e+48>>2]=0,f[e+52>>2]=0,f[e+76>>2]=f[e+636>>2],f[e+92>>2]=f[e+596>>2]<<1,8&i[0|r]&&eA(f[r+8>>2],0,e+48|0,r,0),eA(f[r+8>>2],0,e+48|0,r,0);break g;case 8:if(f[e+88>>2]=0,f[e+92>>2]=0,f[e+80>>2]=0,f[e+84>>2]=0,f[e+72>>2]=0,f[e+76>>2]=0,f[Q>>2]=0,f[Q+4>>2]=0,f[e+56>>2]=0,f[e+60>>2]=0,f[e+48>>2]=0,f[e+52>>2]=0,1&a[0|r]||(g=i[r+19|0],A=f[50758],f[36425]=A,f[36441]=0,f[(A=216192+(A<<4)|0)+12>>2]=g,f[A+8>>2]=0,f[A>>2]=8,f[A+4>>2]=0,A=f[50758]+1|0,f[50758]=(0|A)<=169?A:0,C=i[r+22|0],g=i[r+21|0],o=f[129280+(i[r+16|0]<<2)>>2],(0|(A=f[36424]))<0|(0|(n=f[36440]))<=0||f[(A=216192+(A<<4)|0)+4>>2]||(f[A+4>>2]=n),A=f[50758],f[36424]=A,f[36440]=0,f[(A=216192+(A<<4)|0)>>2]=9,f[A+4>>2]=0,C|=g<<16,g=255==(0|g),f[A+12>>2]=g?3604556:C,f[A+8>>2]=g?c:o,A=f[50758]+1|0,f[50758]=(0|A)<=169?A:0),8==i[b+17|0]&&(f[36426]=0),bA(0,0,r,e+552|0,145784),f[e+56>>2]=f[e+620>>2],f[e+64>>2]=f[e+640>>2],f[e+92>>2]=f[e+596>>2]<<1,2==i[t+17|0]){f[36455]==f[36454]&&(f[36455]=f[50758]),eA(f[r+8>>2],0,e+48|0,r,0);break g}if(!(!(1&a[0|r])|2!=i[b+17|0])){eA(f[r+8>>2],0,e+48|0,r,0);break g}f[36426]=0,eA(f[r+8>>2],0,e+48|0,r,0),f[36426]=0;break g;case 3:f[e+88>>2]=0,f[e+92>>2]=0,f[e+80>>2]=0,f[e+84>>2]=0,f[e+72>>2]=0,f[e+76>>2]=0,f[Q>>2]=0,f[Q+4>>2]=0,f[e+56>>2]=0,f[e+60>>2]=0,f[e+48>>2]=0,f[e+52>>2]=0,C=f[f[r+8>>2]+4>>2],1&a[0|r]||(g=i[r+19|0],A=f[50758],f[36425]=A,f[36441]=0,f[(A=216192+(A<<4)|0)+12>>2]=g,f[A+8>>2]=0,f[A>>2]=8,f[A+4>>2]=0,A=f[50758]+1|0,f[50758]=(0|A)<=169?A:0,o=i[r+22|0],g=i[r+21|0],n=f[129280+(i[r+16|0]<<2)>>2],(0|(A=f[36424]))<0|(0|(s=f[36440]))<=0||f[(A=216192+(A<<4)|0)+4>>2]||(f[A+4>>2]=s),A=f[50758],f[36424]=A,f[36440]=0,f[(A=216192+(A<<4)|0)>>2]=9,f[A+4>>2]=0,o|=g<<16,g=255==(0|g),f[A+12>>2]=g?3604556:o,f[A+8>>2]=g?c:n,A=f[50758]+1|0,f[50758]=(0|A)<=169?A:0),8==i[b+17|0]&&(f[36426]=0),2!=i[t+17|0]|f[36455]!=f[36454]||(f[36455]=f[50758]),bA(0,0,r,e+552|0,145784),(0|(A=f[e+584>>2]-i[r+18|0]|0))>0&&he(A,1),f[e+56>>2]=f[e+620>>2],f[e+64>>2]=f[e+640>>2],f[e+76>>2]=f[e+636>>2],f[e+80>>2]=f[e+656>>2],f[e+92>>2]=f[e+596>>2]<<1,eA(f[r+8>>2],0,e+48|0,r,C<<24>>31&5);break g;case 2:break C;default:break g}n=f[r+8>>2]}k=i[r+3|0],f[e+88>>2]=0,f[e+92>>2]=0,f[e+80>>2]=0,f[e+84>>2]=0,f[e+72>>2]=0,f[e+76>>2]=0,f[Q>>2]=0,f[Q+4>>2]=0,f[e+56>>2]=0,f[e+60>>2]=0,f[e+48>>2]=0,f[e+52>>2]=0,bA(0,0,r,e+552|0,145784),A=f[e+628>>2],f[e+56>>2]=A,f[e+92>>2]=f[e+596>>2]<<1;r:{if(!A||(s=0,C=d,g=x,2&i[e+552|0])){if(i[b+17|0]?(s=0,bA(0,0,b,e+400|0,0),A=f[e+476>>2],f[e+56>>2]=A,!A|!(2&i[e+400|0])||(f[e+72>>2]=f[e+496>>2],s=1),g=f[e+512>>2],f[e+84>>2]=f[e+508>>2],f[e+88>>2]=g):s=0,A)break r;f[e+48>>2]=1,f[e+52>>2]=1,C=M,g=m}f[g>>2]=f[C>>2]}f[e+64>>2]=f[e+640>>2],g=i[r+16|0],A=0,(C=i[r+7|0])?(_g(C,e+96|0),g=_r(f[e+220>>2]),(0|(C=f[e+224>>2]))<=0||(A=_r(C))):g=f[129280+(g<<2)>>2],f[36455]==f[36454]&&(f[36455]=f[50758]),C=(C=15&k)>>>0<2?1:C>>>0>6?3:2;r:{C:switch(i[b+17|0]-3|0){case 2:case 4:s=i[r+19|0],b=f[50758],f[36425]=b,f[36441]=0,f[(b=216192+(b<<4)|0)+12>>2]=s,f[b+8>>2]=A,f[b>>2]=8,f[b+4>>2]=0,A=f[50758]+1|0,f[50758]=(0|A)<=169?A:0,s=i[r+22|0],b=i[r+21|0],(0|(A=f[36440]))<=0||(0|(k=f[36424]))<0||f[(k=216192+(k<<4)|0)+4>>2]||(f[k+4>>2]=A),A=f[50758],f[36424]=A,f[36440]=0,f[(A=216192+(A<<4)|0)>>2]=9,f[A+4>>2]=0,s|=b<<16,b=255==(0|b),f[A+12>>2]=b?3604556:s,f[A+8>>2]=b?c:g,A=f[50758]+1|0,f[50758]=(0|A)<=169?A:0,eA(n,1,e+48|0,r,C);break r;case 0:case 5:s=i[r+19|0],b=f[50758],f[36425]=b,f[36441]=0,f[(b=216192+(b<<4)|0)+12>>2]=s,f[b+8>>2]=A,f[b>>2]=8,f[b+4>>2]=0,A=f[50758]+1|0,f[50758]=(0|A)<=169?A:0,eA(n,1,e+48|0,r,C),s=i[r+22|0],b=i[r+21|0],(0|(A=f[36440]))<=0||(0|(k=f[36424]))<0||f[(k=216192+(k<<4)|0)+4>>2]||(f[k+4>>2]=A),A=f[50758],f[36424]=A,f[36440]=0,f[(A=216192+(A<<4)|0)>>2]=9,f[A+4>>2]=0,s|=b<<16,b=255==(0|b),f[A+12>>2]=b?3604556:s,f[A+8>>2]=b?c:g,A=f[50758]+1|0,f[50758]=(0|A)<=169?A:0;break r;default:break C}s?(s=i[r+22|0],(0|(b=f[36440]))<=0||(0|(k=f[36424]))<0||f[(k=216192+(k<<4)|0)+4>>2]||(f[k+4>>2]=b),b=f[50758],f[36424]=b,f[36440]=0,f[(b=216192+(b<<4)|0)>>2]=9,f[b+4>>2]=0,f[b+12>>2]=(s|s<<16)-983040,f[b+8>>2]=l,b=f[50758]+1|0,f[50758]=(0|b)<=169?b:0,s=i[r+19|0],b=f[50758],f[36425]=b,f[36441]=0,f[(b=216192+(b<<4)|0)>>2]=8,f[b+4>>2]=0,f[b+12>>2]=s-1,f[b+8>>2]=A,A=f[50758]+1|0,f[50758]=(0|A)<=169?A:0,eA(n,1,e+48|0,r,C),b=i[r+21|0],s=i[r+22|0],(0|(A=f[36440]))<=0||(0|(k=f[36424]))<0||f[(k=216192+(k<<4)|0)+4>>2]||(f[k+4>>2]=A),A=f[50758],f[36424]=A,f[36440]=0,f[(A=216192+(A<<4)|0)>>2]=9,f[A+4>>2]=0,s|=b<<16,b=255==(0|b),f[A+12>>2]=b?3604556:s,f[A+8>>2]=b?c:g,A=f[50758]+1|0,f[50758]=(0|A)<=169?A:0):(1&a[0|r]||(s=i[r+19|0],b=f[50758],f[36425]=b,f[36441]=0,f[(b=216192+(b<<4)|0)+12>>2]=s,f[b+8>>2]=A,f[b>>2]=8,f[b+4>>2]=0,A=f[50758]+1|0,f[50758]=(0|A)<=169?A:0,s=i[r+22|0],b=i[r+21|0],(0|(A=f[36440]))<=0||(0|(k=f[36424]))<0||f[(k=216192+(k<<4)|0)+4>>2]||(f[k+4>>2]=A),A=f[50758],f[36424]=A,f[36440]=0,f[(A=216192+(A<<4)|0)>>2]=9,f[A+4>>2]=0,s|=b<<16,b=255==(0|b),f[A+12>>2]=b?3604556:s,f[A+8>>2]=b?c:g,A=f[50758]+1|0,f[50758]=(0|A)<=169?A:0),eA(n,1,e+48|0,r,C))}!f[47198]|1^o||(Ce(e+704|0,f[r+8>>2],r,w,0),(0|((0|(A=f[50757]-f[50758]|0))<=0?A+170:A))<6||(g=f[36445],A=216192+(f[50758]<<4)|0,f[A>>2]=1802,f[A+4>>2]=16777215&g,g=f[e+708>>2],f[A+8>>2]=f[e+704>>2],f[A+12>>2]=g,A=f[50758]+1|0,f[50758]=(0|A)<=169?A:0)),f[e+56>>2]=f[e+620>>2],f[e+84>>2]=0,f[e+88>>2]=0,f[e+64>>2]=f[e+640>>2],g=f[e+632>>2],f[e+68>>2]=g,A=u;r:{if(!g){if(!i[t+17|0])break r;if(f[e+72>>2]=0,bA(0,0,t,e+248|0,0),f[e+52>>2]=1,A=f[e+368>>2],f[e+84>>2]=f[e+364>>2],f[e+88>>2]=A,g=f[e+328>>2],f[e+68>>2]=g,A=D,!g)break r}f[e+72>>2]=f[A>>2]}eA(n,2,e+48|0,r,C)}if(A=f[36442]+1|0,f[36442]=A,(0|A)>997)break e;if(!(f[36423]>(0|A)))break}(0|(A=f[36440]))<=0||(0|(g=f[36424]))<0||(f[(g=216192+(g<<4)|0)+4>>2]||(f[g+4>>2]=A),f[36440]=0),f[36426]=0,f[36439]=-1,f[36455]=f[50758],kA(),f[36427]=-1,A=0,f[36423]<=0||(g=f[47568],C=f[33284],(0|((0|(A=f[50757]-f[50758]|0))<=0?A+170:A))>=6&&(A=216192+(f[50758]<<4)|0,f[A>>2]=1290,f[A+8>>2]=g,f[A+4>>2]=16777215&C,A=f[50758]+1|0,f[50758]=(0|A)<=169?A:0),f[36423]=0,A=0)}return V=e+720|0,A}function AA(A,e,g,r){var C,I=0,s=0,t=0,n=0,k=0,B=0,Q=0,E=0,u=0,l=0,x=0,d=0,m=0,M=0,v=0,h=0,p=0,Y=0,H=0,N=0,P=0,F=0,y=0,z=0,O=0;V=C=V-48|0;A:{if(g>>>0<=2){for(P=f[(g<<=2)+124732>>2],F=f[g+124720>>2];(0|(g=f[e+4>>2]))==f[e+104>>2]?g=ce(e):(f[e+4>>2]=g+1,g=i[0|g]),32==(0|g)|g-9>>>0<5;);u=1;e:{g:switch(g-43|0){case 0:case 2:break g;default:break e}u=45==(0|g)?-1:1,(0|(g=f[e+4>>2]))==f[e+104>>2]?g=ce(e):(f[e+4>>2]=g+1,g=i[0|g])}e:{g:{for(;;){if(a[t+84056|0]==(32|g)){if(t>>>0>6||((0|(g=f[e+4>>2]))==f[e+104>>2]?g=ce(e):(f[e+4>>2]=g+1,g=i[0|g])),8!=(0|(t=t+1|0)))continue;break g}break}if(3!=(0|t)){if(8==(0|t))break g;if(!r|t>>>0<4)break e;if(8==(0|t))break g}if((0|(g=f[e+116>>2]))>0|(0|g)>=0&&(f[e+4>>2]=f[e+4>>2]-1),!(!r|t>>>0<4))for(g=(0|g)<0;g||(f[e+4>>2]=f[e+4>>2]-1),(t=t-1|0)>>>0>3;);}V=Q=V-16|0,o(w(w(0|u)*w(1/0))),(e=2147483647&(n=b(2)))-8388608>>>0<=2130706431?(g=e,g<<=25,r=(e=e>>>7|0)+1065353216|0):(g=n<<25,r=n>>>7|2147418112,e>>>0>=2139095040||(g=0,r=0,e&&(Ve(Q,g=e,0,0,0,(e=D(e))+81|0),B=f[Q>>2],k=f[Q+4>>2],g=f[Q+8>>2],r=65536^f[Q+12>>2]|16265-e<<16))),f[C>>2]=B,f[C+4>>2]=k,f[C+8>>2]=g,f[C+12>>2]=-2147483648&n|r,V=Q+16|0,B=f[C+8>>2],k=f[C+12>>2],n=f[C>>2],E=f[C+4>>2];break A}e:{g:{r:if(!t){for(t=0;;){if(a[t+84473|0]!=(32|g))break r;if(t>>>0>1||((0|(g=f[e+4>>2]))==f[e+104>>2]?g=ce(e):(f[e+4>>2]=g+1,g=i[0|g])),3==(0|(t=t+1|0)))break}break g}r:switch(0|t){case 0:if(48==(0|g)){if((0|(t=f[e+4>>2]))==f[e+104>>2]?t=ce(e):(f[e+4>>2]=t+1,t=i[0|t]),88==(-33&t)){V=s=V-432|0,(0|(g=f[e+4>>2]))==f[e+104>>2]?t=ce(e):(f[e+4>>2]=g+1,t=i[0|g]);C:{a:{for(;;){if(48!=(0|t)){if(46!=(0|t))break C;if((0|(g=f[e+4>>2]))!=f[e+104>>2]){f[e+4>>2]=g+1,t=i[0|g];break a}break}(0|(g=f[e+4>>2]))!=f[e+104>>2]?(N=1,f[e+4>>2]=g+1,t=i[0|g]):(N=1,t=ce(e))}t=ce(e)}if(d=1,48==(0|t)){for(;m=(g=m)-1|0,M=M-!g|0,(0|(g=f[e+4>>2]))==f[e+104>>2]?t=ce(e):(f[e+4>>2]=g+1,t=i[0|g]),48==(0|t););N=1}}for(E=1073676288;;){C:{g=32|t;a:{if(!((y=t-48|0)>>>0<10)){if(46!=(0|t)&g-97>>>0>=6)break C;if(46==(0|t)){if(d)break C;d=1,m=B,M=k;break a}}g=(0|t)>57?g-87|0:y,(0|k)<=0&B>>>0<=7|(0|k)<0?I=g+(I<<4)|0:!k&B>>>0<=28?(Yg(s+48|0,g),QA(s+32|0,Y,H,n,E,0,0,0,1073414144),Y=f[s+32>>2],H=f[s+36>>2],n=f[s+40>>2],E=f[s+44>>2],QA(s+16|0,f[s+48>>2],f[s+52>>2],f[s+56>>2],f[s+60>>2],Y,H,n,E),dA(s,f[s+16>>2],f[s+20>>2],f[s+24>>2],f[s+28>>2],Q,x,h,p),h=f[s+8>>2],p=f[s+12>>2],Q=f[s>>2],x=f[s+4>>2]):l|!g||(QA(s+80|0,Y,H,n,E,0,0,0,1073610752),dA(s- -64|0,f[s+80>>2],f[s+84>>2],f[s+88>>2],f[s+92>>2],Q,x,h,p),h=f[s+72>>2],p=f[s+76>>2],l=1,Q=f[s+64>>2],x=f[s+68>>2]),k=(B=B+1|0)?k:k+1|0,N=1}(0|(g=f[e+4>>2]))!=f[e+104>>2]?(f[e+4>>2]=g+1,t=i[0|g]):t=ce(e);continue}break}C:if(N){if((0|k)<=0&B>>>0<=7|(0|k)<0)for(n=B,E=k;I<<=4,8!=(0|(n=n+1|0))|(E=n?E:E+1|0););a:{I:{f:{if(80==(-33&t)){if(n=RA(e,r),E=g=U,n|-2147483648!=(0|g))break a;if(r){if((0|(g=f[e+116>>2]))>0|(0|g)>=0)break f;break I}Q=0,x=0,Tg(e,0,0),g=0,e=0;break C}if(n=0,E=0,f[e+116>>2]<0)break a}f[e+4>>2]=f[e+4>>2]-1}n=0,E=0}if(I)if(g=n+((e=d?m:B)<<2)|0,e=(k=(d?M:k)<<2|e>>>30)+E|0,(B=g-32|0)>>>0>0-P>>>0&(0|(e=k=(g>>>0>>0?e+1|0:e)-(g>>>0<32)|0))>=0|(0|e)>0)f[56798]=68,Yg(s+160|0,u),QA(s+144|0,f[s+160>>2],f[s+164>>2],f[s+168>>2],f[s+172>>2],-1,-1,-1,2147418111),QA(s+128|0,f[s+144>>2],f[s+148>>2],f[s+152>>2],f[s+156>>2],-1,-1,-1,2147418111),Q=f[s+128>>2],x=f[s+132>>2],g=f[s+140>>2],e=f[s+136>>2];else if((0|k)>=(0|(g=(e=P-226|0)>>31))&e>>>0<=B>>>0|(0|g)<(0|k)){if((0|I)>=0)for(;dA(s+416|0,Q,x,h,p,0,0,0,-1073807360),dA(s+400|0,Q,x,h,p,(e=g=(0|(e=$e(Q,x,h,p,1073610752)))>=0)?f[s+416>>2]:Q,e?f[s+420>>2]:x,e?f[s+424>>2]:h,e?f[s+428>>2]:p),B=(e=B)-1|0,k=k-!e|0,h=f[s+408>>2],p=f[s+412>>2],Q=f[s+400>>2],x=f[s+404>>2],(0|(I=g|I<<1))>=0;);e=k-((P>>31)+(B>>>0

>>0)|0)|0,(0|(g=(g=32+(B-P|0)|0)>>>0>>0&(0|(e=g>>>0<32?e+1|0:e))<=0|(0|e)<0?(0|g)>0?g:0:F))>=113?(Yg(s+384|0,u),m=f[s+392>>2],M=f[s+396>>2],Y=f[s+384>>2],H=f[s+388>>2],n=0,e=0):(Ne(s+352|0,Qg(1,144-g|0)),Yg(s+336|0,u),Y=f[s+336>>2],H=f[s+340>>2],m=f[s+344>>2],M=f[s+348>>2],Kr(s+368|0,f[s+352>>2],f[s+356>>2],f[s+360>>2],f[s+364>>2],Y,H,m,M),v=f[s+376>>2],z=f[s+380>>2],n=f[s+372>>2],e=f[s+368>>2]),Zg(s+320|0,(r=!(1&I)&!!(0|pe(Q,x,h,p,0,0,0,0))&(0|g)<32)+I|0),QA(s+304|0,Y,H,m,M,f[s+320>>2],f[s+324>>2],f[s+328>>2],f[s+332>>2]),g=e,dA(s+272|0,f[s+304>>2],f[s+308>>2],f[s+312>>2],f[s+316>>2],e,n,v,z),QA(s+288|0,Y,H,m,M,(e=r)?0:Q,e?0:x,e?0:h,e?0:p),dA(s+256|0,f[s+288>>2],f[s+292>>2],f[s+296>>2],f[s+300>>2],f[s+272>>2],f[s+276>>2],f[s+280>>2],f[s+284>>2]),cr(s+240|0,f[s+256>>2],f[s+260>>2],f[s+264>>2],f[s+268>>2],g,n,v,z),pe(e=f[s+240>>2],r=f[s+244>>2],g=f[s+248>>2],n=f[s+252>>2],0,0,0,0)||(f[56798]=68),we(s+224|0,e,r,g,n,B),Q=f[s+224>>2],x=f[s+228>>2],g=f[s+236>>2],e=f[s+232>>2]}else f[56798]=68,Yg(s+208|0,u),QA(s+192|0,f[s+208>>2],f[s+212>>2],f[s+216>>2],f[s+220>>2],0,0,0,65536),QA(s+176|0,f[s+192>>2],f[s+196>>2],f[s+200>>2],f[s+204>>2],0,0,0,65536),Q=f[s+176>>2],x=f[s+180>>2],g=f[s+188>>2],e=f[s+184>>2];else Ne(s+112|0,0*+(0|u)),Q=f[s+112>>2],x=f[s+116>>2],g=f[s+124>>2],e=f[s+120>>2]}else{a:{I:{if((0|(g=f[e+116>>2]))>0|(0|g)>=0){if(g=f[e+4>>2],f[e+4>>2]=g-1,!r)break I;if(f[e+4>>2]=g-2,!d)break a;f[e+4>>2]=g-3;break a}if(r)break a}Tg(e,0,0)}Ne(s+96|0,0*+(0|u)),Q=f[s+96>>2],x=f[s+100>>2],g=f[s+108>>2],e=f[s+104>>2]}f[C+16>>2]=Q,f[C+20>>2]=x,f[C+24>>2]=e,f[C+28>>2]=g,V=s+432|0,B=f[C+24>>2],k=f[C+28>>2],n=f[C+16>>2],E=f[C+20>>2];break A}f[e+116>>2]<0||(f[e+4>>2]=f[e+4>>2]-1)}t=e,v=u,s=r,e=0,u=0,V=I=V-8976|0,z=(y=0-P|0)-F|0;C:{a:{for(;;){if(48!=(0|g)){if(46!=(0|g))break C;if((0|(g=f[t+4>>2]))!=f[t+104>>2]){f[t+4>>2]=g+1,g=i[0|g];break a}break}(0|(e=f[t+4>>2]))!=f[t+104>>2]?(f[t+4>>2]=e+1,g=i[0|e]):g=ce(t),e=1}g=ce(t)}if(l=1,48==(0|g)){for(;B=(e=B)-1|0,k=k-!e|0,(0|(e=f[t+4>>2]))==f[t+104>>2]?g=ce(t):(f[t+4>>2]=e+1,g=i[0|e]),48==(0|g););e=1}}f[I+784>>2]=0;C:{a:{I:{f:{i:{if((r=46==(0|g))|(Q=g-48|0)>>>0<=9)for(;;){b:{if(1&r){if(!l){B=n,k=E,l=1;break b}r=!e;break i}E=(n=n+1|0)?E:E+1|0,(0|u)<=2044?(N=48==(0|g)?N:n,e=(I+784|0)+(u<<2)|0,d&&(Q=(G(f[e>>2],10)+g|0)-48|0),f[e>>2]=Q,e=1,d=(g=9==(0|(r=d+1|0)))?0:r,u=g+u|0):48!=(0|g)&&(f[I+8960>>2]=1|f[I+8960>>2],N=18396)}if((0|(g=f[t+4>>2]))==f[t+104>>2]?g=ce(t):(f[t+4>>2]=g+1,g=i[0|g]),!((r=46==(0|g))|(Q=g-48|0)>>>0<10))break}if(B=l?B:n,k=l?k:E,!(!e|69!=(-33&g))){if(Q=RA(t,s),x=e=U,!(Q|-2147483648!=(0|e))){if(!s)break I;Q=0,x=0,f[t+116>>2]<0||(f[t+4>>2]=f[t+4>>2]-1)}k=k+x|0,k=(B=B+Q|0)>>>0>>0?k+1|0:k;break a}if(r=!e,(0|g)<0)break f}f[t+116>>2]<0||(f[t+4>>2]=f[t+4>>2]-1)}if(!r)break a;f[56798]=28}n=0,E=0,Tg(t,0,0),g=0,e=0;break C}if(e=f[I+784>>2])if(n>>>0>9&(0|E)>=0|(0|E)>0|(0|n)!=(0|B)|(0|k)!=(0|E)|(e>>>F|0?(0|F)<=30:0))if(B>>>0>y>>>1>>>0&(0|k)>=0|(0|k)>0)f[56798]=68,Yg(I+96|0,v),QA(I+80|0,f[I+96>>2],f[I+100>>2],f[I+104>>2],f[I+108>>2],-1,-1,-1,2147418111),QA(I- -64|0,f[I+80>>2],f[I+84>>2],f[I+88>>2],f[I+92>>2],-1,-1,-1,2147418111),n=f[I+64>>2],E=f[I+68>>2],g=f[I+76>>2],e=f[I+72>>2];else if((g=B>>>0<(e=P-226|0)>>>0)&(0|k)<=(0|(e>>=31))|(0|e)>(0|k))f[56798]=68,Yg(I+144|0,v),QA(I+128|0,f[I+144>>2],f[I+148>>2],f[I+152>>2],f[I+156>>2],0,0,0,65536),QA(I+112|0,f[I+128>>2],f[I+132>>2],f[I+136>>2],f[I+140>>2],0,0,0,65536),n=f[I+112>>2],E=f[I+116>>2],g=f[I+124>>2],e=f[I+120>>2];else{if(d){if((0|d)<=8){for(t=f[(e=(I+784|0)+(u<<2)|0)>>2];t=G(t,10),9!=(0|(d=d+1|0)););f[e>>2]=t}u=u+1|0}if(l=B,!((0|N)>(0|B)|(0|N)>=9|(0|B)>17)){if(9==(0|l)){Yg(I+192|0,v),Zg(I+176|0,f[I+784>>2]),QA(I+160|0,f[I+192>>2],f[I+196>>2],f[I+200>>2],f[I+204>>2],f[I+176>>2],f[I+180>>2],f[I+184>>2],f[I+188>>2]),n=f[I+160>>2],E=f[I+164>>2],g=f[I+172>>2],e=f[I+168>>2];break C}if((0|l)<=8){Yg(I+272|0,v),Zg(I+256|0,f[I+784>>2]),QA(I+240|0,f[I+272>>2],f[I+276>>2],f[I+280>>2],f[I+284>>2],f[I+256>>2],f[I+260>>2],f[I+264>>2],f[I+268>>2]),Yg(I+224|0,f[124720+(0-l<<2)>>2]),iA(I+208|0,f[I+240>>2],f[I+244>>2],f[I+248>>2],f[I+252>>2],f[I+224>>2],f[I+228>>2],f[I+232>>2],f[I+236>>2]),n=f[I+208>>2],E=f[I+212>>2],g=f[I+220>>2],e=f[I+216>>2];break C}if(e=27+(G(l,-3)+F|0)|0,!((g=f[I+784>>2])>>>e|0&&(0|e)<=30)){Yg(I+352|0,v),Zg(I+336|0,g),QA(I+320|0,f[I+352>>2],f[I+356>>2],f[I+360>>2],f[I+364>>2],f[I+336>>2],f[I+340>>2],f[I+344>>2],f[I+348>>2]),Yg(I+304|0,f[124648+(l<<2)>>2]),QA(I+288|0,f[I+320>>2],f[I+324>>2],f[I+328>>2],f[I+332>>2],f[I+304>>2],f[I+308>>2],f[I+312>>2],f[I+316>>2]),n=f[I+288>>2],E=f[I+292>>2],g=f[I+300>>2],e=f[I+296>>2];break C}}for(;!f[(I+784|0)+((u=(g=u)-1|0)<<2)>>2];);if(d=0,e=(0|l)%9|0){if(r=0,e=(0|l)<0?e+9|0:e,g){for(k=1e9/(0|(B=f[124720+(0-e<<2)>>2]))|0,Q=0,t=0;n=(n=Q)+(u=((E=f[(Q=(I+784|0)+(t<<2)|0)>>2])>>>0)/(B>>>0)|0)|0,f[Q>>2]=n,r=(n=!n&(0|r)==(0|t))?r+1&2047:r,l=n?l-9|0:l,Q=G(k,E-G(B,u)|0),(0|(t=t+1|0))!=(0|g););Q&&(f[(I+784|0)+(g<<2)>>2]=Q,g=g+1|0)}else g=0;l=9+(l-e|0)|0}else r=0;for(;;){t=(I+784|0)+(r<<2)|0;a:{for(;;){if((36!=(0|l)|c[t>>2]>=10384593)&(0|l)>=36)break a;for(u=g+2047|0,Q=0,e=g;g=e,B=Q,Q=(e=f[(u=(I+784|0)+((n=2047&u)<<2)|0)>>2])<<29,e=E=e>>>3|0,!(k=(B=B+Q|0)>>>0>>0?e+1|0:e)&B>>>0<1000000001?Q=0:B=(e=B)-Cr(Q=xC(e,k,1e9),U,1e9,0)|0,f[u>>2]=B,e=(0|n)!=(g-1&2047)||(0|r)==(0|n)||B?g:n,u=n-1|0,(0|r)!=(0|n););if(d=d-29|0,Q)break}(0|(r=r-1&2047))==(0|e)&&(t=g=(B=I+784|0)+((e+2046&2047)<<2)|0,k=f[g>>2],g=e-1&2047,f[t>>2]=k|f[B+(g<<2)>>2]),l=l+9|0,f[(I+784|0)+(r<<2)>>2]=Q;continue}break}a:{I:for(;;){for(B=g+1&2047,Q=(I+784|0)+((g-1&2047)<<2)|0;;){n=(0|l)>45?9:1;f:{for(;;){e=r,t=0;i:{for(;;){if((0|(r=e+t&2047))!=(0|g)&&!((r=f[(I+784|0)+(r<<2)>>2])>>>0<(k=f[124672+(t<<2)>>2])>>>0)){if(r>>>0>k>>>0)break i;if(4!=(0|(t=t+1|0)))continue}break}if(36==(0|l)){for(B=0,k=0,t=0,n=0,E=0;(0|(r=e+t&2047))==(0|g)&&(f[780+(I+((g=g+1&2047)<<2)|0)>>2]=0),Zg(I+768|0,f[(I+784|0)+(r<<2)>>2]),QA(I+752|0,B,k,n,E,0,0,1342177280,1075633366),dA(I+736|0,f[I+752>>2],f[I+756>>2],f[I+760>>2],f[I+764>>2],f[I+768>>2],f[I+772>>2],f[I+776>>2],f[I+780>>2]),n=f[I+744>>2],E=f[I+748>>2],B=f[I+736>>2],k=f[I+740>>2],4!=(0|(t=t+1|0)););if(Yg(I+720|0,v),QA(I+704|0,B,k,n,E,f[I+720>>2],f[I+724>>2],f[I+728>>2],f[I+732>>2]),n=f[I+712>>2],E=f[I+716>>2],B=0,k=0,Q=f[I+704>>2],x=f[I+708>>2],(0|(r=(u=(0|(t=(s=d+113|0)-P|0))<(0|F))?(0|t)>0?t:0:F))<=112)break f;break a}}if(d=n+d|0,r=g,(0|e)!=(0|g))break}for(E=1e9>>>n|0,u=~(-1<>2])>>>n|0)|0,f[t>>2]=k,r=(k=!k&(0|e)==(0|r))?r+1&2047:r,l=k?l-9|0:l,t=G(E,s&u),(0|g)!=(0|(e=e+1&2047)););if(!t)continue;if((0|r)!=(0|B)){f[(I+784|0)+(g<<2)>>2]=t,g=B;continue I}f[Q>>2]=1|f[Q>>2];continue}break}break}Ne(I+656|0,Qg(1,225-r|0)),Kr(I+688|0,f[I+656>>2],f[I+660>>2],f[I+664>>2],f[I+668>>2],Q,x,n,E),Y=f[I+696>>2],H=f[I+700>>2],h=f[I+688>>2],p=f[I+692>>2],Ne(I+640|0,Qg(1,113-r|0)),PA(I+672|0,Q,x,n,E,f[I+640>>2],f[I+644>>2],f[I+648>>2],f[I+652>>2]),cr(I+624|0,Q,x,n,E,B=f[I+672>>2],k=f[I+676>>2],m=f[I+680>>2],M=f[I+684>>2]),dA(I+608|0,h,p,Y,H,f[I+624>>2],f[I+628>>2],f[I+632>>2],f[I+636>>2]),n=f[I+616>>2],E=f[I+620>>2],Q=f[I+608>>2],x=f[I+612>>2]}if((0|(l=e+4&2047))!=(0|g)){a:if((l=f[(I+784|0)+(l<<2)>>2])>>>0<=499999999){if(!l&(e+5&2047)==(0|g))break a;Ne(I+496|0,.25*+(0|v)),dA(I+480|0,B,k,m,M,f[I+496>>2],f[I+500>>2],f[I+504>>2],f[I+508>>2]),m=f[I+488>>2],M=f[I+492>>2],B=f[I+480>>2],k=f[I+484>>2]}else 5e8==(0|l)?(O=+(0|v),(e+5&2047)!=(0|g)?(Ne(I+560|0,.75*O),dA(I+544|0,B,k,m,M,f[I+560>>2],f[I+564>>2],f[I+568>>2],f[I+572>>2]),m=f[I+552>>2],M=f[I+556>>2],B=f[I+544>>2],k=f[I+548>>2]):(Ne(I+528|0,.5*O),dA(I+512|0,B,k,m,M,f[I+528>>2],f[I+532>>2],f[I+536>>2],f[I+540>>2]),m=f[I+520>>2],M=f[I+524>>2],B=f[I+512>>2],k=f[I+516>>2])):(Ne(I+592|0,.75*+(0|v)),dA(I+576|0,B,k,m,M,f[I+592>>2],f[I+596>>2],f[I+600>>2],f[I+604>>2]),m=f[I+584>>2],M=f[I+588>>2],B=f[I+576>>2],k=f[I+580>>2]);(0|r)>111||(PA(I+464|0,B,k,m,M,0,0,0,1073676288),pe(f[I+464>>2],f[I+468>>2],f[I+472>>2],f[I+476>>2],0,0,0,0)||(dA(I+448|0,B,k,m,M,0,0,0,1073676288),m=f[I+456>>2],M=f[I+460>>2],B=f[I+448>>2],k=f[I+452>>2]))}dA(I+432|0,Q,x,n,E,B,k,m,M),cr(I+416|0,f[I+432>>2],f[I+436>>2],f[I+440>>2],f[I+444>>2],h,p,Y,H),n=f[I+424>>2],E=f[I+428>>2],Q=f[I+416>>2],x=f[I+420>>2],(z-2|0)>=(2147483647&s)||(f[I+408>>2]=n,f[I+412>>2]=2147483647&E,f[I+400>>2]=Q,f[I+404>>2]=x,QA(I+384|0,Q,x,n,E,0,0,0,1073610752),n=(e=(0|(e=$e(f[I+400>>2],f[I+404>>2],f[I+408>>2],f[I+412>>2],1081081856)))>=0)?f[I+392>>2]:n,E=e?f[I+396>>2]:E,Q=e?f[I+384>>2]:Q,x=e?f[I+388>>2]:x,d=e+d|0,!(!!(0|pe(B,k,m,M,0,0,0,0))&(e?u&(0|r)!=(0|t):u))&(d+110|0)<=(0|z)||(f[56798]=68)),we(I+368|0,Q,x,n,E,d),n=f[I+368>>2],E=f[I+372>>2],g=f[I+380>>2],e=f[I+376>>2]}else Yg(I+48|0,v),Zg(I+32|0,e),QA(I+16|0,f[I+48>>2],f[I+52>>2],f[I+56>>2],f[I+60>>2],f[I+32>>2],f[I+36>>2],f[I+40>>2],f[I+44>>2]),n=f[I+16>>2],E=f[I+20>>2],g=f[I+28>>2],e=f[I+24>>2];else Ne(I,0*+(0|v)),n=f[I>>2],E=f[I+4>>2],g=f[I+12>>2],e=f[I+8>>2]}f[C+40>>2]=e,f[C+44>>2]=g,f[C+32>>2]=n,f[C+36>>2]=E,V=I+8976|0,B=f[C+40>>2],k=f[C+44>>2],n=f[C+32>>2],E=f[C+36>>2];break A;case 3:break g;default:break r}(0|(g=f[e+116>>2]))>0|(0|g)>=0&&(f[e+4>>2]=f[e+4>>2]-1);break e}if((0|(g=f[e+4>>2]))==f[e+104>>2]?g=ce(e):(f[e+4>>2]=g+1,g=i[0|g]),40!=(0|g)){if(k=2147450880,f[e+116>>2]<0)break A;f[e+4>>2]=f[e+4>>2]-1;break A}for(t=1;(0|(g=f[e+4>>2]))==f[e+104>>2]?g=ce(e):(f[e+4>>2]=g+1,g=i[0|g]),g-48>>>0<10|g-65>>>0<26|95==(0|g)||!(g-97>>>0>=26);)t=t+1|0;if(k=2147450880,41==(0|g))break A;(0|(g=f[e+116>>2]))>0|(0|g)>=0&&(f[e+4>>2]=f[e+4>>2]-1);g:{if(r){if(t)break g;break A}break e}for(;t=t-1|0,(0|g)>0|(0|g)>=0&&(f[e+4>>2]=f[e+4>>2]-1),t;);break A}f[56798]=28,Tg(e,0,0)}k=0}f[A>>2]=n,f[A+4>>2]=E,f[A+8>>2]=B,f[A+12>>2]=k,V=C+48|0}function eA(A,e,g,r,C){var b,s=0,t=0,n=0,k=0,o=0,c=0,Q=0,w=0,E=0,D=0,u=0,l=0,x=0,d=0,m=0,M=0,v=0,h=0,p=0,Y=0,H=0;if(V=b=V-112|0,f[g+8>>2]){Y=f[50754]/70|0,D=(c=f[r+12>>2])||256;A:if(2!=(0|e)){if(1==(0|e)){e:if(3!=i[A+11|0])switch(i[r-15|0]-3|0){case 0:case 5:break e;default:break A}D=(0|(c=f[f[47192]+44>>2]))<(0|D)?D:c}}else{if((0|(c=f[f[47192]+80>>2]))<=0|!(8&i[0|r]|c>>>0<=i[A+14|0]|32&i[A+6|0]))break A;Y<<=1}if(f[36436]=0,x=A,H=e,o=r,V=E=V-16|0,A=f[34460]+f[g+8>>2]|0,e=(e=i[A+2|0])>>>0>=24?24:e,f[E+12>>2]=e,t=f[g+12>>2]+f[g+24>>2]|0,f[36422]=t,e){for(c=A+4|0,s=1&I[A+4>>1];A=145488+(k<<3)|0,r=c+(s?k<<6:G(k,44))|0,f[A+4>>2]=r,w=B[r>>1],I[A+2>>1]=w,I[A>>1]=i[r+16|0],n=2&w?k:n,(0|(k=k+1|0))!=(0|e););c=145488,(0|n)<=0||(1!=(0|H)?(e=e-n|0,f[E+12>>2]=e,c=145488+(n<<3)|0):(e=n+1|0,f[E+12>>2]=e,c=145488))}else e=0,c=145488;if(!(!f[g+4>>2]|f[g+20>>2]|2!=i[x+11|0])){if(s=f[g+36>>2],r=f[g+40>>2],A=0,(0|(e=f[E+12>>2]))>=2){n=s>>>12|0,w=r>>>26&7,d=r>>>18&248,u=G(l=63&r,50),M=63&(v=s>>>6|0),k=s<<1&126,h=G(r>>>16&31,50)-750|0,p=G(r>>>11&31,50)-750|0,m=G(r>>>6&31,50)-750|0;A:{e:if(1!=(0|H)){if(!(n|l))break A;if(8&n?(e=f[4+(c+((t=e-1|0)<<3)|0)>>2],I[e>>1]<0?A=e:(A=(0|(A=f[44469]+1|0))<=169?A:0,f[44469]=A,t=B[e+20>>1]|B[e+22>>1]<<16,A=177888+(A<<6)|0,s=B[e+16>>1]|B[e+18>>1]<<16,I[A+16>>1]=s,I[A+18>>1]=s>>>16,I[A+20>>1]=t,I[A+22>>1]=t>>>16,t=B[e+4>>1]|B[e+6>>1]<<16,s=B[e>>1]|B[e+2>>1]<<16,I[A>>1]=s,I[A+2>>1]=s>>>16,I[A+4>>1]=t,I[A+6>>1]=t>>>16,t=B[e+12>>1]|B[e+14>>1]<<16,s=B[e+8>>1]|B[e+10>>1]<<16,I[A+8>>1]=s,I[A+10>>1]=s>>>16,I[A+12>>1]=t,I[A+14>>1]=t>>>16,t=B[e+28>>1]|B[e+30>>1]<<16,s=B[e+24>>1]|B[e+26>>1]<<16,I[A+24>>1]=s,I[A+26>>1]=s>>>16,I[A+28>>1]=t,I[A+30>>1]=t>>>16,t=B[e+36>>1]|B[e+38>>1]<<16,s=B[e+32>>1]|B[e+34>>1]<<16,I[A+32>>1]=s,I[A+34>>1]=s>>>16,I[A+36>>1]=t,I[A+38>>1]=t>>>16,t=B[e+44>>1]|B[e+46>>1]<<16,s=B[e+40>>1]|B[e+42>>1]<<16,I[A+40>>1]=s,I[A+42>>1]=s>>>16,I[A+44>>1]=t,I[A+46>>1]=t>>>16,t=B[e+52>>1]|B[e+54>>1]<<16,s=B[e+48>>1]|B[e+50>>1]<<16,I[A+48>>1]=s,I[A+50>>1]=s>>>16,I[A+52>>1]=t,I[A+54>>1]=t>>>16,t=B[e+60>>1]|B[e+62>>1]<<16,e=B[e+56>>1]|B[e+58>>1]<<16,I[A+56>>1]=e,I[A+58>>1]=e>>>16,I[A+60>>1]=t,I[A+62>>1]=t>>>16,a[A+16|0]=0,I[A>>1]=32768|B[A>>1],t=f[E+12>>2]-1|0),f[4+(c+(t<<3)|0)>>2]=A,e=1792,(0|(t=I[A+4>>1]))<300||(e=1536,t>>>0<400||(e=t>>>0<500?1280:1024)),f[36436]=e,s=35):(f[E+12>>2]=e+1,I[(A=(t=c+(e<<3)|0)-8|0)>>1]=k,e=f[A+4>>2],A=(0|(A=f[44469]+1|0))<=169?A:0,f[44469]=A,(A=(v=A<<6)+177888|0)&&(s=B[e+4>>1]|B[e+6>>1]<<16,Q=B[e>>1]|B[e+2>>1]<<16,I[A>>1]=Q,I[A+2>>1]=Q>>>16,I[A+4>>1]=s,I[A+6>>1]=s>>>16,s=B[e+60>>1]|B[e+62>>1]<<16,Q=B[e+56>>1]|B[e+58>>1]<<16,I[A+56>>1]=Q,I[A+58>>1]=Q>>>16,I[A+60>>1]=s,I[A+62>>1]=s>>>16,s=B[e+52>>1]|B[e+54>>1]<<16,Q=B[e+48>>1]|B[e+50>>1]<<16,I[A+48>>1]=Q,I[A+50>>1]=Q>>>16,I[A+52>>1]=s,I[A+54>>1]=s>>>16,s=B[e+44>>1]|B[e+46>>1]<<16,Q=B[e+40>>1]|B[e+42>>1]<<16,I[A+40>>1]=Q,I[A+42>>1]=Q>>>16,I[A+44>>1]=s,I[A+46>>1]=s>>>16,s=B[e+36>>1]|B[e+38>>1]<<16,Q=B[e+32>>1]|B[e+34>>1]<<16,I[A+32>>1]=Q,I[A+34>>1]=Q>>>16,I[A+36>>1]=s,I[A+38>>1]=s>>>16,s=B[e+28>>1]|B[e+30>>1]<<16,Q=B[e+24>>1]|B[e+26>>1]<<16,I[A+24>>1]=Q,I[A+26>>1]=Q>>>16,I[A+28>>1]=s,I[A+30>>1]=s>>>16,s=B[e+20>>1]|B[e+22>>1]<<16,Q=B[e+16>>1]|B[e+18>>1]<<16,I[A+16>>1]=Q,I[A+18>>1]=Q>>>16,I[A+20>>1]=s,I[A+22>>1]=s>>>16,s=B[e+12>>1]|B[e+14>>1]<<16,e=B[e+8>>1]|B[e+10>>1]<<16,I[A+8>>1]=e,I[A+10>>1]=e>>>16,I[A+12>>1]=s,I[A+14>>1]=s>>>16,a[v+177904|0]=0,I[A>>1]=32768|B[A>>1]),I[t>>1]=0,f[t+4>>2]=A,k>>>0>=37&&(f[36422]=(k+f[36422]|0)-36),s=M<<1,l&&oe(A,u,m,p,w,h,d,n)),f[f[32972]+132>>2]||(e=i[A+17|0])&&(e=I[102896+(((0|(e=(s<<6>>>0)/(e>>>0)|0))>=199?199:e)<<1)>>1],a[A+18|0]=(0|G(e,i[A+18|0]))/512,a[A+19|0]=(0|G(e,i[A+19|0]))/512,a[A+20|0]=(0|G(e,i[A+20|0]))/512,a[A+21|0]=(0|G(e,i[A+21|0]))/512,a[A+22|0]=(0|G(e,i[A+22|0]))/512,a[A+23|0]=(0|G(e,i[A+23|0]))/512,a[A+24|0]=(0|G(e,i[A+24|0]))/512,a[A+25|0]=(0|G(e,i[A+25|0]))/512),r-536870912>>>0<=1073741823){if(w=f[44469],(0|(t=f[E+12>>2]))>0)for(A=G(r>>>29|0,10)+102854|0,d=I[A+4>>1],l=I[A+2>>1],u=I[A>>1],h=I[A+6>>1],p=I[A+8>>1],s=0;e=f[(m=c+(s<<3)|0)+4>>2],I[e>>1]<0?A=e:(A=(M=(w=(0|(A=w+1|0))<=169?A:0)<<6)+177888|0)?(r=B[e+4>>1]|B[e+6>>1]<<16,t=B[e>>1]|B[e+2>>1]<<16,I[A>>1]=t,I[A+2>>1]=t>>>16,I[A+4>>1]=r,I[A+6>>1]=r>>>16,r=B[e+60>>1]|B[e+62>>1]<<16,t=B[e+56>>1]|B[e+58>>1]<<16,I[A+56>>1]=t,I[A+58>>1]=t>>>16,I[A+60>>1]=r,I[A+62>>1]=r>>>16,r=B[e+52>>1]|B[e+54>>1]<<16,t=B[e+48>>1]|B[e+50>>1]<<16,I[A+48>>1]=t,I[A+50>>1]=t>>>16,I[A+52>>1]=r,I[A+54>>1]=r>>>16,r=B[e+44>>1]|B[e+46>>1]<<16,t=B[e+40>>1]|B[e+42>>1]<<16,I[A+40>>1]=t,I[A+42>>1]=t>>>16,I[A+44>>1]=r,I[A+46>>1]=r>>>16,r=B[e+36>>1]|B[e+38>>1]<<16,t=B[e+32>>1]|B[e+34>>1]<<16,I[A+32>>1]=t,I[A+34>>1]=t>>>16,I[A+36>>1]=r,I[A+38>>1]=r>>>16,r=B[e+28>>1]|B[e+30>>1]<<16,t=B[e+24>>1]|B[e+26>>1]<<16,I[A+24>>1]=t,I[A+26>>1]=t>>>16,I[A+28>>1]=r,I[A+30>>1]=r>>>16,r=B[e+20>>1]|B[e+22>>1]<<16,t=B[e+16>>1]|B[e+18>>1]<<16,I[A+16>>1]=t,I[A+18>>1]=t>>>16,I[A+20>>1]=r,I[A+22>>1]=r>>>16,r=B[e+12>>1]|B[e+14>>1]<<16,e=B[e+8>>1]|B[e+10>>1]<<16,I[A+8>>1]=e,I[A+10>>1]=e>>>16,I[A+12>>1]=r,I[A+14>>1]=r>>>16,a[M+177904|0]=0,I[A>>1]=32768|B[A>>1],t=f[E+12>>2]):A=0,f[m+4>>2]=A,I[A+8>>1]=(0|G(d,I[A+8>>1]))/256,I[A+6>>1]=(0|G(l,I[A+6>>1]))/256,I[A+4>>1]=(0|G(u,I[A+4>>1]))/256,I[A+12>>1]=(0|G(p,I[A+12>>1]))/256,I[A+10>>1]=(0|G(h,I[A+10>>1]))/256,(0|t)>(0|(s=s+1|0)););f[44469]=w}if(!A)break A}else{e=f[c+4>>2],(0|(t=I[e>>1]))<0?A=e:(r=(0|(r=f[44469]+1|0))<=169?r:0,f[44469]=r,(r=(t=r<<6)+177888|0)&&(A=B[e+4>>1]|B[e+6>>1]<<16,Q=B[e>>1]|B[e+2>>1]<<16,I[r>>1]=Q,I[r+2>>1]=Q>>>16,I[r+4>>1]=A,I[r+6>>1]=A>>>16,A=B[e+60>>1]|B[e+62>>1]<<16,Q=B[e+56>>1]|B[e+58>>1]<<16,I[r+56>>1]=Q,I[r+58>>1]=Q>>>16,I[r+60>>1]=A,I[r+62>>1]=A>>>16,A=B[e+52>>1]|B[e+54>>1]<<16,Q=B[e+48>>1]|B[e+50>>1]<<16,I[r+48>>1]=Q,I[r+50>>1]=Q>>>16,I[r+52>>1]=A,I[r+54>>1]=A>>>16,A=B[e+44>>1]|B[e+46>>1]<<16,Q=B[e+40>>1]|B[e+42>>1]<<16,I[r+40>>1]=Q,I[r+42>>1]=Q>>>16,I[r+44>>1]=A,I[r+46>>1]=A>>>16,A=B[e+36>>1]|B[e+38>>1]<<16,Q=B[e+32>>1]|B[e+34>>1]<<16,I[r+32>>1]=Q,I[r+34>>1]=Q>>>16,I[r+36>>1]=A,I[r+38>>1]=A>>>16,A=B[e+28>>1]|B[e+30>>1]<<16,Q=B[e+24>>1]|B[e+26>>1]<<16,I[r+24>>1]=Q,I[r+26>>1]=Q>>>16,I[r+28>>1]=A,I[r+30>>1]=A>>>16,A=B[e+20>>1]|B[e+22>>1]<<16,Q=B[e+16>>1]|B[e+18>>1]<<16,I[r+16>>1]=Q,I[r+18>>1]=Q>>>16,I[r+20>>1]=A,I[r+22>>1]=A>>>16,A=B[e+12>>1]|B[e+14>>1]<<16,e=B[e+8>>1]|B[e+10>>1]<<16,I[r+8>>1]=e,I[r+10>>1]=e>>>16,I[r+12>>1]=A,I[r+14>>1]=A>>>16,a[t+177904|0]=0,t=-32768|B[r>>1],I[r>>1]=t,A=r)),f[c+4>>2]=A,I[c>>1]=k||50,I[c+2>>1]=16384|B[c+2>>1],I[A>>1]=16384|t,t=f[c+12>>2],e=i[t+17|0],r=f[32972],f[r+132>>2]&&(a[A+39|0]=i[t+39|0]-4);g:if(l){if(2048&s){e=(G(e,31&v)>>>0)/30|0,f[r+132>>2]||(r=i[A+17|0])&&(e=I[102896+(((0|(e=(e<<6>>>0)/(r>>>0)|0))>=199?199:e)<<1)>>1],a[A+18|0]=(0|G(e,i[A+18|0]))/512,a[A+19|0]=(0|G(e,i[A+19|0]))/512,a[A+20|0]=(0|G(e,i[A+20|0]))/512,a[A+21|0]=(0|G(e,i[A+21|0]))/512,a[A+22|0]=(0|G(e,i[A+22|0]))/512,a[A+23|0]=(0|G(e,i[A+23|0]))/512,a[A+24|0]=(0|G(e,i[A+24|0]))/512,a[A+25|0]=(0|G(e,i[A+25|0]))/512),oe(A,u,m,p,w,h,d,n);break g}if(oe(A,u,m,p,w,h,d,n),f[f[32972]+132>>2])break g;if(!(e=i[A+17|0]))break g;e=I[102896+(((0|(e=(M<<7>>>0)/(e>>>0)|0))>=199?199:e)<<1)>>1],a[A+18|0]=(0|G(e,i[A+18|0]))/512,a[A+19|0]=(0|G(e,i[A+19|0]))/512,a[A+20|0]=(0|G(e,i[A+20|0]))/512,a[A+21|0]=(0|G(e,i[A+21|0]))/512,a[A+22|0]=(0|G(e,i[A+22|0]))/512,a[A+23|0]=(0|G(e,i[A+23|0]))/512,a[A+24|0]=(0|G(e,i[A+24|0]))/512,a[A+25|0]=(0|G(e,i[A+25|0]))/512}else if(r=f[r+132>>2],8&n){if(r)break g;if(!(r=i[A+17|0]))break g;e=((16320&G(e,48))>>>0)/(r>>>0)|0,e=I[102896+((e>>>0>=199?199:e)<<1)>>1],a[A+18|0]=(0|G(e,i[A+18|0]))/512,a[A+19|0]=(0|G(e,i[A+19|0]))/512,a[A+20|0]=(0|G(e,i[A+20|0]))/512,a[A+21|0]=(0|G(e,i[A+21|0]))/512,a[A+22|0]=(0|G(e,i[A+22|0]))/512,a[A+23|0]=(0|G(e,i[A+23|0]))/512,a[A+24|0]=(0|G(e,i[A+24|0]))/512,a[A+25|0]=(0|G(e,i[A+25|0]))/512}else r||(e=i[A+17|0])&&(e=I[102896+(((e=1792/(e>>>0)|0)>>>0>=199?199:e)<<1)>>1],a[A+18|0]=(0|G(e,i[A+18|0]))/512,a[A+19|0]=(0|G(e,i[A+19|0]))/512,a[A+20|0]=(0|G(e,i[A+20|0]))/512,a[A+21|0]=(0|G(e,i[A+21|0]))/512,a[A+22|0]=(0|G(e,i[A+22|0]))/512,a[A+23|0]=(0|G(e,i[A+23|0]))/512,a[A+24|0]=(0|G(e,i[A+24|0]))/512,a[A+25|0]=(0|G(e,i[A+25|0]))/512);if(!(8&n))break e;e=2816,(0|(r=I[A+4>>1]))<300||(e=2560,r>>>0<400||(e=r>>>0<500?2304:2048)),f[36436]=e}4&n&&(I[A>>1]=32|B[A>>1]),2&n&&(I[A>>1]=16|B[A>>1])}64&n&&he(20,0),A=k&n<<27>>31}else A=0;t=A+f[36422]|0,f[36422]=t,e=f[E+12>>2]}if((0|(r=e-1|0))<=0)s=0;else{if(A=0,k=0,s=0,e-2>>>0>=3)for(d=-4&r,w=0;s=(((I[(n=k<<3)+c>>1]+s|0)+I[c+(8|n)>>1]|0)+I[c+(16|n)>>1]|0)+I[c+(24|n)>>1]|0,k=k+4|0,(0|d)!=(0|(w=w+4|0)););if(n=3&r)for(;s=I[c+(k<<3)>>1]+s|0,k=k+1|0,(0|n)!=(0|(A=A+1|0)););}if(A=e,(n=f[g+20>>2])&&(A=r,k=n+f[34460]|0,(w=i[k+2|0])&&(l=B[k+4>>1],I[c+(r<<3)>>1]=i[k+20|0],n=1,A=e,1!=(0|w)))){if(d=k+4|0,l&=1,h=1&(k=w-1|0),2!=(0|w))for(p=-2&k,w=0;m=d+(n<<6)|0,M=d+G(n,44)|0,v=i[(u=l?m:M)+16|0],f[(k=c+(A<<3)|0)+4>>2]=u,I[k>>1]=v,I[k+2>>1]=B[u>>1],m=i[(u=l?m- -64|0:M+44|0)+16|0],f[k+12>>2]=u,I[k+8>>1]=m,I[k+10>>1]=B[u>>1],n=n+2|0,A=A+2|0,(0|p)!=(0|(w=w+2|0)););h&&(n=d+(l?n<<6:G(n,44))|0,w=i[n+16|0],f[(k=c+(A<<3)|0)+4>>2]=n,I[k>>1]=w,I[k+2>>1]=B[n>>1],A=A+1|0)}A:if(!((0|s)<=0)){e:{g:switch(H-1|0){case 1:if(n=(0|(n=(f[g+44>>2]+t|0)-45|0))<=10?10:n,8&i[0|o]&&(n=n+(i[f[36128]+14|0]<<1)|0),(0|r)<=0)break A;if(o=(n<<8)/(0|s)|0,k=0,2!=(0|e))for(e=-2&r,n=0;I[(s=(t=k<<3)+c|0)>>1]=(0|G(o,I[s>>1]))/256,I[(t=c+(8|t)|0)>>1]=(0|G(o,I[t>>1]))/256,k=k+2|0,(0|e)!=(0|(n=n+2|0)););if(!(1&r))break A;I[(e=c+(k<<3)|0)>>1]=(0|G(o,I[e>>1]))/256;break A;case 0:if(1!=f[g>>2])break e;if((0|(n=f[g+44>>2]))>129)break e;I[c>>1]=(0|G(n,I[c>>1]))/130;break e;default:break g}(0|(n=f[g+44>>2]))<=0||(t=(n-s|0)+t|0,f[36422]=t)}if(!(!t|(0|r)<=0)){if(o=(s+t<<8)/(0|s)|0,k=0,2!=(0|e))for(e=-2&r,n=0;I[(s=(t=k<<3)+c|0)>>1]=(0|G(o,I[s>>1]))/256,I[(t=c+(8|t)|0)>>1]=(0|G(o,I[t>>1]))/256,k=k+2|0,(0|e)!=(0|(n=n+2|0)););1&r&&(I[(e=c+(k<<3)|0)>>1]=(0|G(o,I[e>>1]))/256)}}if(f[b+108>>2]=A,V=E+16|0,c){if((0|(A=f[g+16>>2]))!=f[36438]&&(f[36438]=A,e=216192+(f[50758]<<4)|0,f[e>>2]=14,f[e+4>>2]=A,A=f[50758]+1|0,f[50758]=(0|A)<=169?A:0),k=(A=f[f[32972]+132>>2])?1:3,e=f[c+4>>2],f[g+28>>2]|!i[145748]||(a[145748]=0,k=A?2:4),(r=f[36426])&&(!((2&(A=B[r>>1]))>>>1|i[r+16|0]<2)|16&A||(t=216192+(f[36439]<<4)|0,f[t+12>>2]=e,8&A&&(A=(0|(A=f[44469]+1|0))<=169?A:0,f[44469]=A,(A=(n=A<<6)+177888|0)&&(o=B[e+4>>1]|B[e+6>>1]<<16,s=B[e>>1]|B[e+2>>1]<<16,I[A>>1]=s,I[A+2>>1]=s>>>16,I[A+4>>1]=o,I[A+6>>1]=o>>>16,o=B[e+60>>1]|B[e+62>>1]<<16,s=B[e+56>>1]|B[e+58>>1]<<16,I[A+56>>1]=s,I[A+58>>1]=s>>>16,I[A+60>>1]=o,I[A+62>>1]=o>>>16,o=B[e+52>>1]|B[e+54>>1]<<16,s=B[e+48>>1]|B[e+50>>1]<<16,I[A+48>>1]=s,I[A+50>>1]=s>>>16,I[A+52>>1]=o,I[A+54>>1]=o>>>16,o=B[e+44>>1]|B[e+46>>1]<<16,s=B[e+40>>1]|B[e+42>>1]<<16,I[A+40>>1]=s,I[A+42>>1]=s>>>16,I[A+44>>1]=o,I[A+46>>1]=o>>>16,o=B[e+36>>1]|B[e+38>>1]<<16,s=B[e+32>>1]|B[e+34>>1]<<16,I[A+32>>1]=s,I[A+34>>1]=s>>>16,I[A+36>>1]=o,I[A+38>>1]=o>>>16,o=B[e+28>>1]|B[e+30>>1]<<16,s=B[e+24>>1]|B[e+26>>1]<<16,I[A+24>>1]=s,I[A+26>>1]=s>>>16,I[A+28>>1]=o,I[A+30>>1]=o>>>16,o=B[e+20>>1]|B[e+22>>1]<<16,s=B[e+16>>1]|B[e+18>>1]<<16,I[A+16>>1]=s,I[A+18>>1]=s>>>16,I[A+20>>1]=o,I[A+22>>1]=o>>>16,o=B[e+12>>1]|B[e+14>>1]<<16,s=B[e+8>>1]|B[e+10>>1]<<16,I[A+8>>1]=s,I[A+10>>1]=s>>>16,I[A+12>>1]=o,I[A+14>>1]=o>>>16,a[n+177904|0]=0,I[A>>1]=32768|B[A>>1]),I[(n=n+177888|0)+8>>1]=B[r+8>>1],a[n+21|0]=i[r+21|0],I[n+10>>1]=B[r+10>>1],a[n+22|0]=i[r+22|0],I[n+12>>1]=B[r+12>>1],a[n+23|0]=i[r+23|0],I[n+14>>1]=B[r+14>>1],a[n+24|0]=i[r+24|0],a[n+25|0]=i[r+25|0],f[t+12>>2]=A))),2!=(0|H)|2!=i[x+11|0]||(kA(),f[36427]=f[50758]),!((0|(x=f[b+108>>2]))<2)){for(A=f[36433],o=(G(256-A|0,D)+(A<<8)|0)/256|0,A=f[36432],t=(G(256-A|0,D)+(A<<8)|0)/256|0,s=f[50754],A=0,r=1;n=B[(E=(c+(r<<3)|0)-8|0)+2>>1],n=(0|G((0|G(s,I[E>>1]))/1e3|0,4&n?t:16384&n?o:D))/256|0,f[(r<<2)+b>>2]=n,A=A+n|0,(0|x)!=(0|(r=r+1|0)););if(!((0|A)<=0|(0|A)>=(0|Y)|(0|x)<2)){if(r=1,o=1&(n=x-1|0),2!=(0|x))for(t=-2&n,D=0;f[(n=(r<<2)+b|0)>>2]=(0|G(f[n>>2],Y))/(0|A),f[n+4>>2]=(0|G(f[n+4>>2],Y))/(0|A),r=r+2|0,(0|t)!=(0|(D=D+2|0)););o&&(f[(r=(r<<2)+b|0)>>2]=(0|G(f[r>>2],Y))/(0|A))}if(D=0,!((0|x)<2))for(x=H+256|0,r=1;A=f[4+(c+(r<<3)|0)>>2],!(n=f[g+28>>2])|128&i[0|e]||(f[36422]=0,qA(n,x,0,s=f[g>>2],0,o=(o=f[g+32>>2])?(o<<5)/100|0:32),a[145748]=1,f[g+28>>2]=0),(0|C)<0||(C=64&i[0|e]?6:C,(f[b+108>>2]-1|0)==(0|r)&&(C=(n=C)|(3840&(C=f[36436])?C:0))),n=f[(r<<2)+b>>2],f[36440]=n+f[36440],f[36441]=n+f[36441],n?(o=f[50758],f[36439]=o,(0|C)>=0&&(f[(o=216192+(o<<4)|0)>>2]=k,f[o+12>>2]=A,f[o+8>>2]=e,f[o+4>>2]=n+(C<<16),e=f[50758]+1|0,f[50758]=(0|e)<=169?e:0),f[36426]=A,D=n+D|0):f[36426]=0,e=A,(0|(r=r+1|0))>2];);}!f[36438]|1==(0|H)||(f[36438]=0,A=216192+(f[50758]<<4)|0,f[A>>2]=14,f[A+4>>2]=0,A=f[50758]+1|0,f[50758]=(0|A)<=169?A:0)}}V=b+112|0}function gA(A,e,g,r){var C,I=0,b=0,s=0,t=0,n=0,k=0,o=0,c=0,Q=0,w=0,E=0,D=0,u=0,l=0,x=0,d=0,m=0,M=0,v=0,h=0,p=0,Y=0;V=C=V-1856|0,f[C+164>>2]=0,g?c=f[g>>2]:ue(g=C- -64|0,0,96),f[33264]=0,f[C+1824>>2]=0,f[C+1828>>2]=0,f[C+1832>>2]=0,f[C+1836>>2]=0,a[C+1616|0]=0,a[C+992|0]=0,a[C+1200|0]=0,a[C+784|0]=0;A:if(f[A+688>>2]){for(f[C+1840>>2]=e,32==i[0|(I=e)]&&(I=e+1|0,f[C+1840>>2]=I),Q=C+416|1,M=C+1844|1,h=C+1848|1,p=C+1852|1,f[C+1820>>2]=I,Te(C+168|0,I),I=f[C+1820>>2];32!=(32|i[0|I]);)I=Te(C+164|0,I)+f[C+1820>>2]|0,f[C+1820>>2]=I,D=D+1|0;_A(C+256|0,e,v=(0|(s=I-e|0))>=159?159:s),!(d=4194304&c)|1!=(0|D)?(l=(36==(0|(I=f[47202])))<<2,1==(0|D)|36!=(0|I)||(I=f[C+1840>>2]-1|0,f[C+1840>>2]=I,a[0|I]=95,l=0,b=!!(0|TA(A,C+1840|0,C+1616|0,C+1832|0,0,g)),I=f[47202])):(b=1,Te(C+172|0,I+1|0),kg(f[C+172>>2])&&(0|ae(f[C+164>>2]))==(0|ae(f[C+172>>2]))&&(b=0),l=36==(0|(I=f[47202]))?4:b,b=0);e:{g:{r:{C:{a:if(16&I)w=15&I,l=0;else{if(u=1,b||(u=!!(0|TA(A,C+1840|0,C+1616|0,C+1832|0,2,g))),50331648&(I=f[C+1832>>2])&&(s=f[C+1820>>2],46==i[s+1|0]&&(a[s+1|0]=32,I=f[C+1832>>2])),536870912&I){if(!r)break A;rg(r,f[C+1840>>2]);break A}if(8192&I|!(128&I)|u)r=f[33264];else if(I=f[C+1840>>2],f[C+1820>>2]=I,!((0|(r=f[33264]))<=0))for(b=0;32==i[0|I]&&(a[0|I]=45,b=b+1|0,I=f[C+1820>>2],r=f[33264]),I=I+1|0,f[C+1820>>2]=I,(0|r)>(0|b););I:if(!(r|1!=(0|D))&&(s=Te(C+576|0,o=f[C+1840>>2]),32==i[s+o|0])){b=C+1408|0,r=o;f:{i:{b:{for(;;){s:if(kg(f[C+576>>2])){t:{if(46==i[(t=r+s|0)+1|0]){w=0;n:switch(i[(I=s+2|0)+r|0]-32|0){case 0:break t;case 7:break n;default:break s}if(w=1,s=I,115==i[t+3|0])break t;break s}if(w=1,(0|k)<=0)break b}if(!((0|s)<=0)){if(E=3&s,n=0,s>>>0<4)I=0;else for(x=-4&s,I=0,t=0;a[0|b]=i[r+I|0],a[b+1|0]=i[(1|I)+r|0],a[b+2|0]=i[(2|I)+r|0],a[b+3|0]=i[(3|I)+r|0],I=I+4|0,b=b+4|0,(0|x)!=(0|(t=t+4|0)););if(E)for(;a[0|b]=i[r+I|0],I=I+1|0,b=b+1|0,(0|E)!=(0|(n=n+1|0)););}if(k=k+1|0,w)r=r+s|0;else if(s=Te(C+576|0,r=3+(r+s|0)|0),32==i[r+s|0])continue}break}if(!((0|k)<2)){(s=(s=b-(I=C+1408|0)|0)+(I=_A(o,I,s))|0)>>>0>>0&&ue(s,32,(C+1408|0)+r-(I+b)|0),f[33264]=(k<<1)-2,f[C+1836>>2]=0;break i}}if(!k)break I;if(f[C+1832>>2]=0,f[C+1836>>2]=0,!f[33264])break f}f[C+1832>>2]=128}l=1}if(21==i[C+1616|0]){rg(189088,C+1616|0),I=0;break A}if(Y=i[C+1833|0],b=1,!u){if(f[C+168>>2]-48>>>0<10){if(Mg(A,84174,189088),I=0,21==i[189088])break A;if(!(!(128&i[A+109|0])|32&i[g+2|0])){a[189088]=21,a[189089]=0;break A}b=!!(0|sA(A,f[C+1840>>2],C+1616|0,C+1832|0,g,0))}else b=0;if(!(b|2==(3&c))&&(16777216&(r=f[A+104>>2])||(b=0,!(!(33554432&r)|!(1&c))))&&(16&c||(b=0,!(1&a[g+13|0])))){x=f[C+1840>>2],I=0,k=0,t=0,V=E=V-224|0,a[0|(n=C+1616|0)]=0,f[E+216>>2]=0,f[E+220>>2]=0;I:if(!(a[x-2|0]-48>>>0<10|(1&a[0|g]?0:2&i[A+107|0])||(r=i[0|(s=x+1|0)],(!(2561&B[A+106>>1])|!(1&a[g+2|0]))&32==(0|r)))){if(32!=(0|(k=i[0|x]))){for(w=32767,r=0;;){if(!(I=qe(101868,k<<24>>24,8))){k=0;break I}if(o=0,(0|(I=f[(I<<2)-305584>>2]))==(0|r)&&!((0|(o=t+1|0))<=2)){k=0;break I}f:{i:{b:{s:{if(!((0|r)<2)){if(10==(0|r)|100==(0|r))break s;if(!((0|r)>(0|I))){k=0;break I}}if(!r)break i;if((0|r)<(0|I))break b;break i}if((0|r)>=(0|I))break i}if(k=0,(0|m)%10|(0|G(r,10))<(0|I))break I;I=I-r|0,w=r;break f}if((0|I)>=(0|w)){k=0;break I}m=r+m|0}if(k=i[0|s],s=b=s+1|0,r=I,t=o,32==(0|k))break}r=i[0|b]}else b=s;if((r<<24>>24)-48>>>0<10)k=0;else if((0|(r=I+m|0))>2])k=0;else if((0|r)>f[A+116>>2])k=0;else if(Mg(A,85600,E+176|0),I=n,4&i[A+107|0]||(I=rg(n,I=E+176|0)+Lg(I)|0),f[E+4>>2]=f[A+140>>2],f[E>>2]=r,dg(E+16|0,85839,E),k=0,46!=i[0|b]){me(A,x,b,g,1)&&(f[g>>2]=32768|f[g>>2]),s=0;f:if(8&i[A+107|0]){if(t=f[g>>2],26741==f[A+212>>2]){if(32768&t)break f;if(!(16384&t))break I;s=1,t=0;i:{b:switch(i[0|b]-97|0){case 0:case 4:break b;default:break i}b:{s:{t:{n:switch((w=i[b+1|0])-116|0){case 6:break i;case 1:case 2:case 3:case 4:case 5:break s;case 0:break n;default:break t}if(116!=i[b+2|0])break b;break i}if(32==(0|w))break i}if(!((0|r)%1e3|0)&&108==(0|w))break i}t=1}if(t)break f;break I}f[g>>2]=32768|t}f[(r=A+8232|0)>>2]=0,f[r+4>>2]=0,sA(A,E+16|2,I,E+216|0,g,s),k=1,4&i[A+107|0]&&mC(n,E+176|0)}}V=E+224|0,k?(f[C+1832>>2]=8192|f[C+1832>>2],b=1):b=0}}if(w=u?l:32&Y?1:l,l=0,!(!(1&c)|(0|D)<2)&&Mr(f[C+168>>2])){I:{if(1&a[188785]){if(!(!(r=8192&(I=f[C+1832>>2]))|b))break I;l=r>>>2^2048;break a}if(b)break C;I=f[C+1832>>2]}if(!(128&I|D>>>0>3)&&!((0|(I=f[A+8220>>2]))<4)&&(r=1,(0|I)>=f[A+8216>>2]))break e}}if(n=0,(0|w)<=0)break r;r=w;break e}if((0|(r=w))>0)break e;n=0,o=0,t=0,k=0;break g}if(b)o=0,t=0,k=0;else{r=f[C+1840>>2],f[C+1820>>2]=r,I=999,k=0,o=0,Q=0;r:{C:{a:{for(;;){I:{f:{i:{if(I-1>>>0>=2){if((0|D)<2)break i;if(Te(C+1408|0,r),(0|(I=f[C+1408>>2]))<577&f[A+600>>2]>0)break i;if(I=ae(I),(f[I+4>>2]!=f[A+600>>2]?I:0)|1==f[A+40>>2])break i;I=i[0|r],f[C+1408>>2]=I<<24>>24;b:switch(I-32|0){default:if(!I)break i;break;case 0:case 7:break i;case 1:case 2:case 3:case 4:case 5:case 6:break b}n=Te(C+1408|0,r),u=9;b:{s:{t:{n:if(-33&(I=f[C+1408>>2])){for(b=0,t=0;;){k:{o:{if(39==(0|I)){if((0|o)>0|(0|b)>1)break n;if(t=b?t:39,3!=f[A+40>>2])break o;break k}t=b?t:I}b=b+1|0}if(!zg(A,I)){if(39!=(0|(I=f[C+1408>>2]))&&!Mr(I))break i;if(n=Te(C+1408|0,r+n|0)+n|0,-33&(I=f[C+1408>>2]))continue;break n}break}if((0|b)<=2)break t;u=b}else t=0;if(2!=(0|(I=f[A+40>>2])))break s;V=I=V-208|0,a[0|I]=0,b=i[0|(s=r-1|0)],a[0|s]=32,r=GA(A,r,I,200,0,-2147483648,0),a[0|s]=b,V=I+208|0,r=!r|(32768&r)>>>15;break b}I=f[A+40>>2],u=b}r=(a[A+168|0]+1|0)<(u-((0|I)==(0|t))|0)}if(!r)break i;r=f[C+1820>>2]}if(39!=i[0|r])break f;k=67108864,l=0}if(n=0,ve(A,C+992|0,0,o),r=f[C+1820>>2],32!=(0|(I=i[0|r])))break I;o=0,t=0;break g}if(l=0,r=DA(A,r,C+992|0,1&(Q|=(0|o)>0))+f[C+1820>>2]|0,f[C+1820>>2]=r,21==i[C+992|0])break a;for(o=o+1|0,b=0;b=(I=b)+1|0,32!=i[r+I|0];);k=67108864;continue}break}if(!i[C+992|0]|39==(0|I)||(a[r-1|0]=32,r=f[C+1820>>2]),s=GA(A,r,C+1616|0,200,C+784|0,c,C+1832|0),21==(0|(r=i[C+1616|0]))){rg(189088,C+1616|0),I=0;break A}if(!(r|i[C+784|0])&&(Te(C+1408|0,f[C+1820>>2]),1==(0|D)&&(kg(f[C+1408>>2])||rr(f[C+1408>>2])))){Pg(A,f[C+1820>>2],C+1616|0,w)&&rg(189088,C+1616|0),I=0;break A}f[C+172>>2]=a[f[C+1820>>2]-1|0];I:if(1024&s)for(x=C+176|1,w=0,I=1,Q=0,o=0;;){if((u=131072&s)|!(1&I)||(a[C+1408|0]=0,!(r=GA(A,f[C+1820>>2],C+1408|0,200,C+576|0,805306368|c,C+1832|0)))){2048&s&&(f[A+8184>>2]=1),a[f[C+1820>>2]-1|0]=f[C+172>>2];f:{i:{b:{s:if(u){if(a[C+176|0]=0,I=f[C+1820>>2],r=1,t=63&s){if(E=1&s,o=t-1|0,b=0,1!=(0|t))for(m=t-E|0,t=0;n=I,f[C+1820>>2]=I+1,a[0|(d=(C+176|0)+r|0)]=(0|b)!=(0|o)?i[0|I]:0,I=I+2|0,f[C+1820>>2]=I,a[d+1|0]=(0|o)!=(1|b)?i[n+1|0]:0,b=b+2|0,r=r+2|0,(0|m)!=(0|(t=t+2|0)););E&&(t=I+1|0,f[C+1820>>2]=t,a[(C+176|0)+r|0]=(0|b)!=(0|o)?i[0|I]:0,r=r+1|0,I=t)}a[(C+176|0)+r|0]=0}else{if(I=f[C+1820>>2],!(t=15&s))break b;if(r=0,b=t,n=3&s)for(;I=I+1|0,f[C+1820>>2]=I,128==(192&i[0|I])||(b=b-1|0,(0|n)!=(0|(r=r+1|0))););if(t>>>0<4)break s;for(;;)if(I=I+1|0,f[C+1820>>2]=I,128!=(192&i[0|I])){for(;I=I+1|0,f[C+1820>>2]=I,128==(192&i[0|I]););for(;I=I+1|0,f[C+1820>>2]=I,128==(192&i[0|I]););for(;I=I+1|0,f[C+1820>>2]=I,128==(192&i[0|I]););if(r=(0|b)>4,b=b-4|0,!r)break}}if(r=I-1|0,f[C+172>>2]=a[0|r],a[0|r]=32,r=c|=8388608,!u)break i;if(oC(C+576|0,C+784|0,12),f[C+1852>>2]=x,r=rg(C+1200|0,I=C+1616|0),TA(A,C+1852|0,I,C+1832|0,0,g)&&rg(r,C+1616|0),!(32&i[C+1833|0]))break f;a[0|r]=0,Pg(A,f[C+1852>>2],r,1);break f}r=I-1|0,f[C+172>>2]=a[0|r],a[0|r]=32,r=8388608|c}c=r,mC(C+1200|0,C+784|0)}if(a[C+784|0]=0,t=1,r=TA(A,C+1820|0,C+1616|0,C+1824|0,1024,g),f[C+1832>>2]||(I=f[C+1828>>2],f[C+1832>>2]=f[C+1824>>2],f[C+1836>>2]=I,t=Q),r){n=0,o=s;break r}if(n=GA(A,f[C+1820>>2],C+1616|0,200,C+784|0,8404992&c,C+1832|0),I=1,o=s,Q=t,21==i[C+1616|0]){a[f[C+1820>>2]-1|0]=f[C+172>>2],rg(189088,C+1616|0),I=0;break A}}else I=C+416|0,xA(A,f[C+1820>>2],r,I),n=GA(A,f[C+1820>>2],C+1616|0,200,C+784|0,268435456|c,C+1832|0),_A(f[C+1820>>2],I,Lg(I)),1024&n||(rg(C+1616|0,C+1408|0),I=rg(C+784|0,C+576|0),8&i[188788]&&(Ye(t=I,I=C+576|0),s=f[47195],f[C+48>>2]=I,eC(s,85205,C+48|0)),n=r),I=0;if(r=(b=1024&n)>>>10|0,w>>>0>48)break I;if(w=w+1|0,s=n,!b)break}else n=s,o=0,Q=0,r=0;if(r|!n)break C;for(s=rg(C+1408|0,C+1616|0),b=xA(A,f[C+1820>>2],n,C+416|0),t=Q,I=n;;){I:{if(a[C+1616|0]=0,i[C+1200|0]){if(a[f[C+1820>>2]-1|0]=f[C+172>>2],r=TA(A,C+1840|0,C+1616|0,C+1824|0,b,g),a[f[C+1820>>2]-1|0]=32,21==i[C+1616|0]){A=C+416|0,_A(f[C+1820>>2],A,Lg(A)),rg(189088,C+1616|0),I=0;break A}if(f[C+1832>>2]||(Q=f[C+1828>>2],f[C+1832>>2]=f[C+1824>>2],f[C+1836>>2]=Q),r){a[C+1200|0]=0;break I}t=f[C+1824>>2]?1:t}if(r=TA(A,C+1820|0,C+1616|0,C+1824|0,b,g),21==i[C+1616|0]){A=C+416|0,_A(f[C+1820>>2],A,Lg(A)),rg(189088,C+1616|0),I=0;break A}if(f[C+1832>>2]||(Q=f[C+1828>>2],f[C+1832>>2]=f[C+1824>>2],f[C+1836>>2]=Q),!r)if(16384&I)rg(C+1616|0,s);else{c|=b<<11&8192|I<<9&134217728;f:if(524288&I){if(Q=rg(C+576|0,r=C+784|0),I=GA(A,f[C+1820>>2],C+1616|0,200,r,c,C+1832|0),mC(r,Q),r=0,!I){I=0;break f}if(1024&I)break f;r=1,b=xA(A,f[C+1820>>2],I,0)}else I=0,GA(A,f[C+1820>>2],C+1616|0,200,0,c,C+1832|0),r=0;if(21==i[C+1616|0]){rg(189088,C+1616|0),A=C+416|0,_A(f[C+1820>>2],A,Lg(A)),a[f[C+1820>>2]-1|0]=f[C+172>>2],I=0;break A}if(r)continue}}break}65536&n||(ag(A,C+1616|0,200,C+784|0),a[C+784|0]=0),r=C+416|0,_A(f[C+1820>>2],r,Lg(r));break r}rg(189088,A=C+992|0),I=!pg(1|A,84744,3)<<12;break A}n=0,t=Q}a[f[C+1820>>2]-1|0]=f[C+172>>2]}}if(r=f[C+164>>2],f[C+1852>>2]=8026656,f[C+1848>>2]=8022304,f[C+1844>>2]=7566112,4&c){g:{if(I=255&r){if(102==(0|I))break g;if(M=h,IC(r<<24>>24))break g}M=p}GA(A,M,189088,200,0,0,0)}for(r=0,I=C+1200|0,Q=i[C+784|0];;){g:{r:{C:switch(0|(s=i[0|I])){case 0:break g;case 6:case 7:break C;default:break r}r=s}I=I+1|0;continue}break}g:if(r|t){if(f[A+32>>2]|65536&o){for(b=0,fA(A,C+1616|0,C+1832|0,3,0),I=C+1200|0;;){r:switch(i[0|I]){case 6:b&&(a[0|I]=5),b=1;default:I=I+1|0;continue;case 0:break r}break}f[C+24>>2]=C+1616,f[C+20>>2]=C+1200,f[C+16>>2]=C+992,Gg(189088,200,85233,C+16|0),a[189287]=0,fA(A,189088,C+1832|0,-1,0);break g}f[C+8>>2]=C+1616,f[C+4>>2]=C+1200,f[C>>2]=C+992,Gg(189088,200,85233,C),a[189287]=0,fA(A,189088,C+1832|0,-1,0)}else fA(A,r=C+1616|0,C+1832|0,-1,!!(0|Q)<<1),f[C+40>>2]=r,f[C+36>>2]=C+1200,f[C+32>>2]=C+992,Gg(189088,200,85233,C+32|0),a[189287]=0;i[C+784|0]&&(r=Lg(189088),a[983+(C-r|0)|0]=0,rg(r+189088|0,C+784|0)),16&(r=c|l)&&(f[C+1832>>2]=-268435457&f[C+1832>>2]);g:if(!(128&r)|!(16&i[A+14|0]))if(3072&r){if(te(A,6),!(2048&r))break g;f[C+1832>>2]=268435456|f[C+1832>>2]}else 16&i[G(f[33264],12)+g|0]&&(1536&(g=f[C+1832>>2])?te(A,4):2048&g&&te(A,3));else te(A,3);8192&n&&(f[A+8192>>2]=2,f[A+8184>>2]=2);g:{if(8&(g=f[C+1836>>2]))f[A+8184>>2]=0,f[A+8188>>2]=3,r=A+8196|0;else if(1&g)f[A+8192>>2]=0,f[A+8184>>2]=2,r=A+8196|0;else if(2&g)f[A+8192>>2]=2,f[A+8184>>2]=0,f[A+8188>>2]=0,r=A+8196|0;else{if(!(4&g))break g;f[A+8184>>2]=0,f[A+8192>>2]=0,f[A+8196>>2]=2,r=A+8188|0}f[r>>2]=0}!i[f[C+1820>>2]]|256&g||((0|(g=f[A+8184>>2]))>0&&(f[A+8184>>2]=g-1),(0|(g=f[A+8192>>2]))>0&&(f[A+8192>>2]=g-1),(0|(g=f[A+8196>>2]))>0&&(f[A+8196>>2]=g-1),(0|(g=f[A+8188>>2]))<=0||(f[A+8188>>2]=g-1)),1!=(0|D)|25966!=f[A+212>>2]||!Mr(f[C+168>>2])|105==f[C+168>>2]||(f[C+1832>>2]=16777216|f[C+1832>>2]);g:if(2&i[A+68|0]&&98304&(r=f[C+1832>>2])&&!((0|(g=Lg(189088)-1|0))<=0))for(I=0;;){if(A=I+1|0,6==i[I+189088|0]){g=a[0|(A=A+189088|0)];r:{if(65536&r){if((0|Er(69))==(0|g)?(D=Er(101),a[0|A]=D):D=i[0|A],b=111,(0|Er(79))==D<<24>>24)break r;break g}if((0|Er(101))==(0|g)?(D=Er(69),a[0|A]=D):D=i[0|A],b=79,(0|Er(111))!=D<<24>>24)break g}a[0|A]=Er(b);break g}if((0|g)==(0|(I=A)))break}A=f[C+1832>>2],_A(e,C+256|0,v),I=A|k;break A}if(I=0,a[C+1616|0]=0,Pg(A,f[C+1840>>2],C+1616|0,r)){if(s=rg(189088,C+1616|0),!d){if(r=f[C+164>>2],f[C+1408>>2]=8026656,f[C+576>>2]=8022304,f[C+416>>2]=7566112,4&c){e=C+576|1,g=C+1408|1;e:{if(I=255&r){if(102==(0|I))break e;if(Q=e,IC(r<<24>>24))break e}Q=g}GA(A,Q,s,200,0,0,0)}I=128&f[C+1832>>2]}}else I=((0|D)>1)<<12}else a[189088]=0;return V=C+1856|0,I}function rA(A,e,g,r,C,I,b,s){var t,n,k=0,o=0,B=0,c=0,Q=0,w=0,E=0,D=0,u=0,l=0,x=0,d=0,m=0,M=0,v=0,h=0,p=0,Y=0,H=0,N=0,P=0,F=0,y=0,z=0,O=0,Z=0,K=0,W=0,X=0,L=0,T=0,J=0,R=0,U=0,j=0,S=0,q=0,_=0,$=0,AA=0,eA=0;V=t=V-384|0,n=f[e>>2];A:{e:{g:{r:{if(C){if(7!=i[0|C])break r;f[e>>2]=(r||1)+n;break g}f[I>>2]=0,f[e>>2]=n+1;break A}P=86135,X=268435456&b,L=134217728&b,T=8388608&b,J=16384&b,R=8192&b,z=g-1|0,O=r-g|0,U=2&b,j=128&b,S=b>>>31|0,q=-2147483648&b,Z=t+96|1;r:for(;;)for(f[t+268>>2]=0,E=(B=f[e>>2])+r|0,v=-2,m=-6,g=C,h=q,Y=0,F=0,M=1,k=0,p=0,N=0;;){c=B,D=k;C:{a:{I:{f:{i:{b:{s:{t:{n:{k:{o:{B:{c:{Q:{G:{w:{for(;l=g,g=g+1|0,!((o=i[0|l])>>>0>9);){Q=g;E:switch(0|o){case 0:if(!(g=K)){K=0,g=86135;break G}for(;;){o=1;D:{u:switch(0|(k=i[0|g])){case 0:case 3:break w;case 5:break u;default:break D}o=2}g=(g+o|0)+((9==(0|k))<<1)|0}case 1:if(N=1,!S)continue;break i;case 2:N=2;continue;case 4:K=g;continue;case 5:g=l+2|0,k=f[A+320>>2];D:{if((B=i[l+1|0])>>>0>=32){if(!(k>>>B-32&1))break D;break i}if(!(k>>>B&1))break i}M=M+1|0;continue;case 9:g=l+3|0;continue;case 8:N=1,F=1,h=0;break;case 3:break Q;default:break E}}Q=0,k=D,B=c;E:switch(0|N){case 0:D:{u:{if((0|(k=i[0|E]))!=(0|o)){if(69!=(0|k))break i;if(101==(0|o))break u;break i}if(Q=0,128==(192&o))break D}Q=21}E=E+1|0,Y=Y+1|0;break C;case 1:break c;case 2:break E;default:break k}if(f[t+264>>2]=f[t+268>>2],!i[E-1|0])break i;m=(0|(k=m+6|0))>=19?19:k,d=E+1|0,x=Te(t+268|0,E),u=i[0|E],Q=20,k=D;E:{D:switch(o-11|0){case 6:g=l+2|0,k=f[t+268>>2],B=a[l+1|0];u:if(o=f[604+(((B=((0|B)<65?191:-65)+B|0)<<2)+A|0)>>2])k=!!(0|Pr(o,k));else{if((0|B)>7)break i;l:{if((0|(o=f[A+600>>2]))>0){if((k=k-o|0)-1>>>0<255)break l;break i}if((o=k-192|0)>>>0<=413){k=i[344+(i[o+94240|0]+A|0)|0]&1<>>0>255)break i}k=i[344+(A+k|0)|0]&1<>2]))break i;u:for(;;){if(7==(0|(Q=i[0|k])))break i;if(126==(0|Q)){Q=20-m|0;break C}l:if(u){if(w=E,o=k,(0|Q)==(0|u))for(;;){if((0|(Q=i[0|(o=o+1|0)]))!=(0|(k=i[0|(w=w+1|0)])))break l;if(!k)break}}else o=k,w=E;if(Q)for(;;)if(B=i[0|o],o=k=o+1|0,!B)continue u;break}if((0|(k=w-E|0))<0)break i;E=k+E|0,Q=20-m|0;break C;case 14:o=f[t+268>>2];u:{l:if(k=f[A+604>>2])k=!!(0|Pr(k,o));else{x:{if((0|(k=f[A+600>>2]))>0){if((w=o-k|0)-1>>>0<255)break x;break u}if((k=o-192|0)>>>0<=413){k=1&a[344+(i[k+94240|0]+A|0)|0];break l}if(w=o,o>>>0>255)break a}k=1&a[344+(A+w|0)|0]}if(k)break i;o=f[t+268>>2]}if(!L)break a;if(32==(0|o))break i;break a;case 4:if((k=f[t+268>>2])-48>>>0<10|k-2406>>>0<10)break a;if(!i[A+170|0])break i;Q=20-m|0;break C;case 5:if(Zr(f[t+268>>2]))break i;break I;case 0:if(f[t+268>>2]==f[t+264>>2])break I;break i;case 17:g=l+2|0,k=32768,Q=0;u:{l:switch((o=i[l+1|0])-1|0){case 0:break k;case 1:break l;default:break u}if(Q=1,k=D,!T)break k;break i}if(16==(0|(k=240&o))){if(Q=23,k=D,s>>>(15&o)&16384)break k;break i}if(3!=(0|o)&32!=(0|k))break C;if(_A(k=t+96|0,z,B=1+(f[e>>2]+(Y+O|0)|0)|0),a[0|(k=k+B|0)]=32,a[k+1|0]=0,f[33265]=0,f[33266]=0,f[t+16>>2]=Z,TA(A,t+16|0,t+272|0,133060,0,0),Q=23,!(3!=(0|o)|(0|(w=f[33265]))>=0|16384&f[33266]))break C;if(k=D,B=c,w>>>(15&o)&16384)break k;break i;case 34:u:{if(k=u-32|0){if(13==(0|k))break u;break i}if(!J)break i}w=22-m|0;break f;case 10:if(k=1,21==i[0|g])break E;break b;case 18:if(32!=(0|(w=f[t+268>>2])))for(o=E+x|0;;){u:{l:if(k=f[A+632>>2])k=!!(0|Pr(k,w));else{x:{if((0|(k=f[A+600>>2]))>0){if((w=w-k|0)-1>>>0<255)break x;break u}if((k=w-192|0)>>>0<=413){k=128&i[344+(i[k+94240|0]+A|0)|0];break l}if(w>>>0>255)break u}k=128&i[344+(A+w|0)|0]}if(k)break i}if(o=Te(t+268|0,o)+o|0,32==(0|(w=f[t+268>>2])))break}w=19-m|0;break f;case 49:break s;case 2:break t;case 3:break n;case 1:break k;case 13:break o;case 12:break D;default:break B}Te(t+272|0,g),x=-1;D:if((0|(k=f[t+272>>2]))!=(0|(o=f[t+268>>2])))if(-33&o)for(;;){u=E,x=-1;u:if(18==(0|k)&&(k=a[l+2|0],Q=f[4788+((((0|k)<65?191:-65)+k<<2)+A|0)>>2])){for(;;){if(7==(0|(B=i[0|Q])))break u;if(126==(0|B)){x=0;break u}l:if((0|(E=i[0|u]))==(0|B)){if(k=u,o=Q,E)for(;;){if((0|(B=i[0|(o=o+1|0)]))!=(0|(Q=i[0|(k=k+1|0)])))break l;if(!Q)break}}else o=Q,k=u;if(!B){k=k-u|0;break}for(;k=i[0|o],o=Q=o+1|0,k;);}x=k}if(B=Te(t+268|0,u),(0|(k=f[t+272>>2]))==(0|(o=f[t+268>>2]))|!(-33&o))break D;if(E=B+u|0,-1!=(0|x))break}else u=E;else u=E;E=(0|k)==(0|o)||(0|x)>=0?u:d,Q=0;break C}for(;k=k+1|0,21==i[0|(g=g+1|0)];);break b}g=g+1|0}Q=l}if(!(32!=i[c-1|0]&F|h||((0|(o=F?M+4|0:M))>=(0|y)&&(_=p,$=D,y=o,AA=Y,P=g),!(8&f[47197])|X|(0|o)<=0))){for(Ye(g,l=t+272|0),d=f[47195],B=t+16|0,g=0,u=0,h=0,p=0,V=E=V-496|0,a[E+80|0]=0,(0|r)>0?(_A(E+288|0,n,r),D=r):D=0,a[D+(k=E+288|0)|0]=0,c=Lg(k)+k|0,x=(0|b)<0;;){if(w=i[0|C],D=C,C=C+1|0,w>>>0>9)for(;;){Q:{G:{w:{E:{D:{u:switch((k=255&w)-14|0){case 4:break w;case 3:break E;case 0:break D;case 14:break u;default:break G}if(D=D+2|0,w=32,!x&1==(0|(C=i[0|C])))break Q;a[0|c]=36,rg(k=c+1|0,C=Gr(128960,C)),c=Lg(C)+k|0;break Q}k=i[D+2|0],w=i[0|C],f[E+36>>2]=127&i[D+3|0],f[E+32>>2]=4&k?80:83,dg(E+48|0,85131,E+32|0),1&k&&(C=Lg(C=E+48|0)+C|0,a[0|C]=101,a[C+1|0]=0),2&(C=127&k)&&(k=Lg(k=E+48|0)+k|0,a[0|k]=105,a[k+1|0]=0),4&C&&(k=Lg(k=E+48|0)+k|0,a[0|k]=112,a[k+1|0]=0),8&C&&(k=Lg(k=E+48|0)+k|0,a[0|k]=118,a[k+1|0]=0),16&C&&(k=Lg(k=E+48|0)+k|0,a[0|k]=100,a[k+1|0]=0),32&C&&(k=Lg(k=E+48|0)+k|0,a[0|k]=102,a[k+1|0]=0),C>>>0>=64&&(C=Lg(C=E+48|0)+C|0,a[0|C]=113,a[C+1|0]=0),1&w&&(C=Lg(C=E+48|0)+C|0,a[0|C]=116,a[C+1|0]=0),D=D+4|0,c=rg(c,C=E+48|0)+Lg(C)|0,w=32;break Q}D=D+2|0,w=i[a[0|C]+93871|0];break Q}C=a[0|C],a[0|c]=76,k=((C=C+((0|C)<65?191:-65)|0)>>>0)/10|0,a[c+1|0]=k+48,w=C-G(k,10)|48,1==(0|p)&&(a[0|c]=w,w=76),D=D+2|0,c=c+2|0;break Q}w=k>>>0<=31?i[k+93904|0]:32==(0|k)?95:w,D=C}if(a[0|c]=w,C=D+1|0,c=c+1|0,!((w=i[0|D])>>>0>=10))break}k=1;Q:switch(0|w){case 1:k=g;case 8:a[0|c]=0,c=E+80|0,g=k,p=1;continue;case 2:a[0|c]=0,D=Lg(k=E+288|0)+k|0,c=i[84899]|i[84900]<<8,a[0|D]=c,a[D+1|0]=c>>>8,p=2,a[D+2|0]=i[84901],c=Lg(k)+k|0;continue;case 5:u=a[0|C],C=D+2|0;continue;case 9:h=(i[0|C]+G(i[D+2|0],255)|0)-256|0,C=D+3|0;continue;case 0:case 3:break Q;default:continue}break}if(a[0|c]=0,c=B,(0|h)>0&&(f[E+16>>2]=h,dg(B,85581,E+16|0),c=B+7|0),(0|u)>0&&(f[E>>2]=u,dg(c,85694,E),c=Lg(c)+c|0),1&((0|(C=Lg(E+80|0)))>0|g)){1&g&&(a[0|c]=95,c=c+1|0);Q:if(!((0|(D=C-1|0))<0|c>>>0>=B>>>0))for(;;){if(a[0|c]=i[(E+80|0)+D|0],c=c+1|0,(0|D)<=0)break Q;if(D=D-1|0,!(c>>>0>>0))break}a[0|c]=41,a[c+1|0]=32,c=c+2|0}a[0|c]=0,a[(g=E+288|0)+((B+3|0)-c|0)|0]=0,mC(c,g),(0|(g=Lg(B)))<=7&&(ue(g+B|0,32,8-g|0),g=8),a[g+B|0]=0,V=E+496|0,f[t+4>>2]=B,f[t>>2]=(0|r)>1?o+35|0:o,f[t+8>>2]=l,eC(d,89088,t)}g=Q;break i}if(!i[0|c])break i;v=(0|(k=v+2|0))>=19?19:k,Te(t+264|0,c),k=Xe(t+268|0,B=c-1|0),u=i[0|B],d=B;c:{Q:{G:{w:{E:{D:switch(o-10|0){case 13:if(k=(0|(H=i[0|g]))==(0|(o=i[0|c])),x=-1,32==(0|o)|(0|o)==(0|H))break w;if(o)break E;break G;case 7:g=l+2|0,Q=f[t+268>>2],c=a[l+1|0];u:if(o=f[604+(((c=((0|c)<65?191:-65)+c|0)<<2)+A|0)>>2])o=!!(0|Pr(o,Q));else{if((0|c)>7)break i;l:{if((0|(o=f[A+600>>2]))>0){if((Q=Q-o|0)-1>>>0<255)break l;break i}if((o=Q-192|0)>>>0<=413){o=i[344+(i[o+94240|0]+A|0)|0]&1<>>0>255)break i}o=i[344+(A+Q|0)|0]&1<>2]))break i;u:{for(;;){if(7==(0|(u=i[0|k])))break i;if(126==(0|u)){x=0;break u}o=B;l:{x:{if((0|(l=(x=Lg(k))-1|0))>0)for(o=c-x|0,Q=0,w=B;;){if(!i[0|(w=w-1|0)])break x;if((0|l)==(0|(Q=Q+1|0)))break}d:if(!((0|(Q=i[0|o]))!=(0|u)|!Q))for(;;){if((0|(u=i[0|(k=k+1|0)]))!=(0|(Q=i[0|(o=o+1|0)])))break d;if(!Q)break}if(!u)break l}for(o=k;Q=i[0|o],o=k=o+1|0,Q;);continue}break}if((0|x)<0)break i}Q=20-m|0,B=1+(B-x|0)|0;break c;case 15:o=f[t+268>>2];u:{l:if(c=f[A+604>>2])c=!!(0|Pr(c,o));else{x:{if((0|(c=f[A+600>>2]))>0){if((o=o-c|0)-1>>>0<255)break x;break u}if((c=o-192|0)>>>0<=413){c=1&a[344+(i[c+94240|0]+A|0)|0];break l}if(o>>>0>255)break u}c=1&a[344+(A+o|0)|0]}if(c)break i}Q=20-v|0,B=1+(B-k|0)|0;break c;case 1:if(f[t+268>>2]!=f[t+264>>2])break i;Q=21-v|0,B=1+(B-k|0)|0;break c;case 5:if(!((c=f[t+268>>2])-48>>>0<10|c-2406>>>0<10))break i;Q=21-v|0,B=1+(B-k|0)|0;break c;case 6:if(Zr(f[t+268>>2]))break i;Q=21-m|0,B=1+(B-k|0)|0;break c;case 18:if(g=l+2|0,!(3==(0|(o=i[l+1|0]))|32==(240&o)))break C;if(_A(k=t+96|0,z,B=1+(f[e>>2]+(Y+O|0)|0)|0),a[0|(k=k+B|0)]=32,a[k+1|0]=0,f[33265]=0,f[33266]=0,f[t+16>>2]=Z,TA(A,t+16|0,t+272|0,133060,0,0),Q=23,!(3!=(0|o)|(0|(w=f[33265]))>=0|16384&f[33266]))break C;if(k=D,B=c,w>>>(15&o)&16384)break k;break i;case 11:if(o=1,21==i[0|g])for(;o=o+1|0,21==i[0|(g=g+1|0)];);if(f[A+8208>>2]<(0|o))break i;Q=18+(o-v|0)|0;break c;case 0:if(Q=19,k=D,B=c,f[A+8212>>2]>0)break k;break i;case 19:if(Q=3,32==(0|(w=f[t+268>>2])))break c;for(o=1+(B-k|0)|0;;){u:{l:if(k=f[A+632>>2])k=!!(0|Pr(k,w));else{x:{if((0|(k=f[A+600>>2]))>0){if((w=w-k|0)-1>>>0<255)break x;break u}if((k=w-192|0)>>>0<=413){k=128&i[344+(i[k+94240|0]+A|0)|0];break l}if(w>>>0>255)break u}k=128&i[344+(A+w|0)|0]}if(k)break i}if(o=o-Xe(t+268|0,o-1|0)|0,32==(0|(w=f[t+268>>2])))break}break c;case 16:if(Q=1,k=D,B=c,f[A+8184>>2])break k;break i;case 9:if(Q=1,k=D,B=c,U)break k;break i;case 36:for(;;){if(Q=50,k=D,!(c=(255&u)-32|0))break i;if(14==(0|c))break k;u=i[0|(d=d-1|0)]}case 35:break D;default:break Q}D:{if(k=u-32|0){if(13==(0|k))break D;break i}if(!j)break i}Q=22-m|0;break c}E:{for(;;){x=-1,w=c,c=c-1|0;D:if(18==(0|H)&&(k=a[l+2|0],Q=f[4788+((((0|k)<65?191:-65)+k<<2)+A|0)>>2]))for(eA=w+1|0;;){if(7==(0|(d=i[0|Q]))){x=-1;break D}if(126==(0|d)){x=0;break D}o=w;u:{if((0|(W=(x=Lg(Q))-1|0))>0)for(o=eA-x|0,u=0,k=w;;){if(!i[0|(k=k-1|0)])break u;if((0|W)==(0|(u=u+1|0)))break}l:if(!((0|(k=i[0|o]))!=(0|d)|!k))for(;;){if((0|(d=i[0|(Q=Q+1|0)]))!=(0|(k=i[0|(o=o+1|0)])))break l;if(!k)break}if(!d)break D}for(o=Q;k=i[0|o],o=Q=o+1|0,k;);}if(k=(0|(o=i[0|c]))==(0|H),32==(0|o)|(0|o)==(0|H))break E;if(!o){c=w;break G}if(-1!=(0|x))break}c=w;break G}c=w}B=k?c:B}Q=0,B=(0|x)<0?B:c+1|0;break c}if((0|o)!=(0|u))break i;Q=4,32!=(0|o)&&(Q=128!=(192&o)?21-v|0:0)}k=D;break k}if((0|o)!=(0|u))break i;w=128!=(192&o)?21-m|0:0;break f}if(Q=1,R)break i}M=Q+M|0;continue}if(k=a[l+1|0],D=i[l+3|0],B=i[l+2|0],1&!(f[A+8208>>2]|4&B)&a[A+84|0])break i;g=l+4|0,w=0,D=127&D|(127&B)<<8|k<<16;break f}t:if(!(d>>>0<=(o=f[e>>2]+r|0)>>>0)){for(;;){if(101!=i[0|o]){if(k=o>>>0>>0,o=o+1|0,k)continue;break t}break}w=0,p=o;break f}w=0;break f}Q=-20;break C}if(u=0,32!=(0|(o=f[t+268>>2])))for(w=E+x|0,Q=0;;){if(!Q){b:{s:if(B=f[A+632>>2])o=!!(0|Pr(B,o));else{t:{if((0|(B=f[A+600>>2]))>0){if((o=o-B|0)-1>>>0<255)break t;break b}if((B=o-192|0)>>>0<=413){o=128&i[344+(i[B+94240|0]+A|0)|0];break s}if(o>>>0>255)break b}o=128&i[344+(A+o|0)|0]}o&&(u=u+1|0)}o=f[t+268>>2]}b:if(B=f[A+632>>2])Q=!!(0|Pr(B,o));else{s:{if((0|(B=f[A+600>>2]))>0){if(Q=0,(o=o-B|0)-1>>>0<255)break s;break b}if((B=o-192|0)>>>0<=413){Q=128&i[344+(i[B+94240|0]+A|0)|0];break b}if(Q=0,o>>>0>255)break b}Q=128&i[344+(A+o|0)|0]}if(w=Te(t+268|0,w)+w|0,32==(0|(o=f[t+268>>2])))break}if(!((0|k)>(0|u))){w=18+(k-m|0)|0;break f}}for(;k=i[0|g],g=C=g+1|0,k;);if(7!=i[0|C])continue r;if(A=r+AA|0,f[e>>2]=f[e>>2]+(A||1),y)break e;break g}k=D,E=d,B=c,M=w+M|0;continue}E=E+x|0,Q=21-m|0;break C}E=E+x|0,Q=20-m|0}k=D,B=c,M=Q+M|0}}P=86135}f[I+12>>2]=_,f[I+8>>2]=$,f[I+4>>2]=P,f[I>>2]=y}V=t+384|0}function CA(A,e){var g,r=0,C=0,b=0,s=0,t=0,n=0,k=0,o=0,c=0,Q=0,w=0,D=0,u=0,l=0,x=0,d=0,m=0,M=0,v=0,h=0,p=0,Y=0,H=0,N=0,P=0,F=0,y=0,z=0,O=0,Z=0;V=g=V-1168|0,f[g+928>>2]=0,f[g+932>>2]=0,f[g+920>>2]=0,f[g+924>>2]=0,f[g+912>>2]=0,f[g+916>>2]=0,f[g+904>>2]=0,f[g+908>>2]=0,f[g+896>>2]=0,f[g+900>>2]=0;A:{e:{if(A){if(i[0|A]|8&e)break e;break A}if(!(8&e))break A}if(oC(g+1088|0,A,40),16&e){if((0|fr(rg(g+704|0,A)))<=0)break A;u=8&e}else(u=8&e)|i[g+1088|0]||(I[g+1088>>1]=i[85055]|i[85056]<<8,a[g+1090|0]=i[85057]),f[g+496>>2]=137584,f[g+500>>2]=47,f[g+504>>2]=47,dg(r=g+512|0,85286,g+496|0),f[g+484>>2]=g+1088,f[g+480>>2]=r,dg(r=g+704|0,85425,g+480|0),(0|fr(r))>0||(f[g+468>>2]=47,f[g+472>>2]=47,f[g+464>>2]=137584,dg(r=g+512|0,85648,g+464|0),f[g+452>>2]=g+1088,f[g+448>>2]=r,dg(g+704|0,85425,g+448|0));if(C=u?86012:85055,!(l=Ae(g+704|0,85712))){if(r=0,3&e)break A;C=(0|Ng(r=g+1088|0))<0?C:r}if((D=2&e)||(r=f[47192])&&(aC(r),f[47192]=0),m=rg(g+992|0,C),d=rg(g+944|0,C),D?((r=sC(200992,43))&&(a[0|r]=0),f[g+432>>2]=A+3,dg(A=g+704|0,86030,g+432|0),mC(200992,A)):(f[32972]=199592,oC(200992,A,40),a[201088]=0,a[201040]=0,f[50299]=200992,f[50298]=201088,f[50297]=201040),YA(D),l){for(v=f[30450],h=g+548|0,p=g+544|0,Y=g+540|0,H=g+536|0,N=g+532|0,P=g+528|0,F=12|(A=g+512|0),y=8|A,z=4|A;xe(g+704|0,190,l);){A=g+704|0;e:{if(35!=i[g+704|0]){g:if(!((0|(A=Lg(g+704|0)-1|0))<=0))for(;;){if(!(32==(0|(C=a[0|(r=(g+704|0)+A|0)]))|C-9>>>0<5))break g;if(a[0|r]=0,!((0|(A=A-1|0))>0))break}if(!(A=hA(g+704|0)))break e}a[0|A]=0}A=g+704|0;e:if(r=i[g+704|0])for(;;){if(32==(0|(r=r<<24>>24))|r-9>>>0<5)break e;if(!(r=i[0|(A=A+1|0)]))break}if(a[0|A]=0,i[g+704|0])if(A=A+1|0,r=Hr(129744,g+704|0)){b=0,V=C=V-416|0;e:if(s=f[47192]){g:switch(r-19|0){case 16:if(f[C+32>>2]=C+412,1!=(0|aA(A,84249,C+32|0)))break e;f[s+324>>2]=f[C+412>>2];break e;case 8:tg(A,s+320|0,27);break e;case 2:if(f[C+48>>2]=188784,aA(A,84249,C+48|0),!(A=i[188784]))break e;f[s+152>>2]=A;break e;case 11:if(i[0|A])for(t=f[30450];;)if(r=A,A=A+1|0,!(32==(0|(b=a[0|r]))|b-9>>>0<5)){for(b=Dg(r),f[C+412>>2]=b,(0|b)>0&&(b>>>0<=31?f[s+104>>2]=f[s+104>>2]|1<>>0<=63?f[s+108>>2]=f[s+108>>2]|1<>2]=b,eC(t,84700,C- -64|0)),r=A);r=(A=r)+1|0,(b=a[0|A])-48>>>0<10|(32|b)-97>>>0<26;);if(!b)break}8&(A=f[s+104>>2])&&(f[s+124>>2]=46,f[s+128>>2]=44),4&A&&(f[s+124>>2]=0);break e;default:if(256!=(65280&r))break e;f[C+16>>2]=24+(s+((255&r)<<2)|0),aA(A,84249,C+16|0);break e;case 1:f[C+144>>2]=s,f[C+148>>2]=s+4,aA(A,85642,C+144|0);break e;case 3:if(r=0,ue(b=C+160|0,0,240),f[C+132>>2]=C+360,f[C+128>>2]=C+320,f[C+124>>2]=C+280,f[C+120>>2]=C+240,f[C+116>>2]=C+200,f[C+112>>2]=b,b=aA(A,85037,C+112|0),f[C+412>>2]=b,f[s+152>>2]=0,(0|b)<=0)break e;for(o=f[30450];;){r:if(Qr(t=(C+160|0)+G(r,40)|0,85301)){C:{if((0|(k=f[34454]))>0)for(w=f[34455],A=0;;){if(!Qr(t,w+G(A,68)|0))break C;if((0|k)==(0|(A=A+1|0)))break}f[C+96>>2]=t,eC(o,85562,C+96|0),b=f[C+412>>2];break r}a[156+(r+s|0)|0]=A}if(!((0|b)>(0|(r=r+1|0))))break}break e;case 9:f[C+88>>2]=s+20,f[C+84>>2]=s+16,f[C+80>>2]=s+8,aA(A,84778,C+80|0);break e;case 10:tg(A,s+12|0,29);break e;case 5:if((0|(o=ug(A,C+160|0)))<=0)break e;if(r=0,A=0,o>>>0>=4)for(w=-4&o,t=s+304|0;k=C+160|0,I[t+(A<<1)>>1]=f[k+(A<<2)>>2],I[t+((n=1|A)<<1)>>1]=f[k+(n<<2)>>2],I[t+((n=2|A)<<1)>>1]=f[k+(n<<2)>>2],I[t+((n=3|A)<<1)>>1]=f[k+(n<<2)>>2],A=A+4|0,(0|w)!=(0|(b=b+4|0)););if(!(b=3&o))break e;for(;I[304+(s+(A<<1)|0)>>1]=f[(C+160|0)+(A<<2)>>2],A=A+1|0,(0|b)!=(0|(r=r+1|0)););break e;case 6:if((0|(o=ug(A,C+160|0)))<=0)break e;if(r=0,A=0,o>>>0>=4)for(w=-4&o,t=s+296|0;k=C+160|0,a[A+t|0]=f[k+(A<<2)>>2],a[(n=1|A)+t|0]=f[k+(n<<2)>>2],a[(n=2|A)+t|0]=f[k+(n<<2)>>2],a[(n=3|A)+t|0]=f[k+(n<<2)>>2],A=A+4|0,(0|w)!=(0|(b=b+4|0)););if(!(b=3&o))break e;for(;a[296+(A+s|0)|0]=f[(C+160|0)+(A<<2)>>2],A=A+1|0,(0|b)!=(0|(r=r+1|0)););break e;case 7:if((0|(o=ug(A,C+160|0)))<=0)break e;if(r=0,A=0,o>>>0>=4)for(w=-4&o,t=s+304|0;n=k=t+(A<<1)|0,c=B[k>>1],k=C+160|0,I[n>>1]=c+B[k+(A<<2)>>1],I[(c=t+((n=1|A)<<1)|0)>>1]=B[c>>1]+B[k+(n<<2)>>1],I[(c=t+((n=2|A)<<1)|0)>>1]=B[c>>1]+B[k+(n<<2)>>1],I[(c=t+((n=3|A)<<1)|0)>>1]=B[c>>1]+B[k+(n<<2)>>1],A=A+4|0,(0|w)!=(0|(b=b+4|0)););if(!(b=3&o))break e;for(;I[(t=s+(A<<1)|0)+304>>1]=B[t+304>>1]+B[(C+160|0)+(A<<2)>>1],A=A+1|0,(0|b)!=(0|(r=r+1|0)););break e;case 4:a[s+169|0]=1;break e;case 0:break g}a[s+208|0]=1}else f[C>>2]=Gr(129568,r),eC(f[30450],89101,C);V=C+416|0}else{e:switch(Hr(131904,g+704|0)-1|0){case 1:if(D)continue;if(a[g+1040|0]=0,f[g+512>>2]=5,f[g+32>>2]=g+1040,f[g+36>>2]=g+512,aA(A,86237,g+32|0),1769103734==f[g+1040>>2]&7630433==f[g+1044>>2])continue;if((A=Lg(g+1040|0)+2|0)>>>0<99-x>>>0&&(a[0|(r=x+201088|0)]=f[g+512>>2],rg(r+1|0,g+1040|0),x=A+x|0),!O){if(A=0,(r=t=g+1040|0)||(r=f[57150])){if(A=86875,f[(C=V-32|0)+24>>2]=0,f[C+28>>2]=0,f[C+16>>2]=0,f[C+20>>2]=0,f[C+8>>2]=0,f[C+12>>2]=0,f[C>>2]=0,f[C+4>>2]=0,s=0,b=i[86875])if(i[86876]){for(;f[(s=C+(b>>>3&28)|0)>>2]=f[s>>2]|1<>>3&28)>>2]>>>b&1))break g;if(b=i[A+1|0],A=A+1|0,!b)break}s=A-r|0}else{for(A=r;C=A,A=A+1|0,i[0|C]==(0|b););s=C-r|0}if(i[0|(A=s+r|0)]){r=86875,V=b=V-32|0,C=a[86875];g:if(i[86876]&&C){if(ue(b,0,32),C=i[86875])for(;f[(s=b+(C>>>3&28)|0)>>2]=f[s>>2]|1<>>3&28)>>2]>>>C&1)break g;if(C=i[r+1|0],r=r+1|0,!C)break}}else r=_e(A,C);V=b+32|0,i[0|(r=(r-A|0)+A|0)]?(f[57150]=r+1,a[0|r]=0):f[57150]=0}else f[57150]=0,A=0}r=rg(m,A),rg(d,A),Ng(rg(g+896|0,A)),f[47192]=q(r),oC(f[32972]+40|0,t,20)}O=1;continue;case 0:if(D)continue;for(;r=A,A=A+1|0,32==(0|(C=a[0|r]))|C-9>>>0<5;);oC(201040,r,40);continue;case 2:f[g+1152>>2]=0,r=g+512|0,f[g+48>>2]=r,f[g+52>>2]=g+1152,aA(A,86237,g+48|0),a[201200]=Hr(132112,r),a[201201]=f[g+1152>>2];continue;case 4:f[g+64>>2]=d,aA(A,86939,g- -64|0);continue;case 3:f[g+80>>2]=g+896,aA(A,86939,g+80|0);continue;case 8:if(f[g+1152>>2]=100,f[g+1164>>2]=100,f[g+1148>>2]=100,f[g+112>>2]=g+1144,f[g+1144>>2]=0,f[g+96>>2]=g+512,f[g+100>>2]=g+1152,f[g+104>>2]=g+1164,f[g+108>>2]=g+1148,(0|aA(A,91156,g+96|0))<2)continue;if((A=f[g+512>>2])>>>0>8)continue;if((0|(r=f[g+1152>>2]))>=0&&(C=f[32972]+(A<<1)|0,r=E(Q=2.56001*+(0|r))<2147483648?~~Q:-2147483648,I[C+236>>1]=r,I[C+164>>1]=r),(0|(r=f[g+1164>>2]))>=0&&(C=f[32972]+(A<<1)|0,r=E(Q=2.56001*+(0|r))<2147483648?~~Q:-2147483648,I[C+254>>1]=r,I[C+182>>1]=r),(0|(C=f[g+1148>>2]))<0?r=f[32972]:(t=(r=f[32972])+(A<<1)|0,C=E(Q=2.56001*+(0|C))<2147483648?~~Q:-2147483648,I[t+200>>1]=C),I[218+((A<<1)+r|0)>>1]=f[g+1144>>2],A)continue;I[r+200>>1]=(0|G(I[r+200>>1],105))/100;continue;case 9:if(f[g+132>>2]=g+696,f[g+128>>2]=g+700,2!=(0|aA(A,87106,g+128|0)))continue;if(A=f[32972],r=f[g+700>>2],f[A+64>>2]=(r<<12)-36864,f[A+68>>2]=G(f[g+696>>2]-r|0,108),E(Q=256*(+(r-82|0)/82*.25+1))<2147483648){f[A+116>>2]=~~Q;continue}f[A+116>>2]=-2147483648;continue;case 35:Z||Ng(g+896|0),f[g+1164>>2]=0,a[g+1156|0]=i[91267],f[g+1152>>2]=i[91263]|i[91264]<<8|i[91265]<<16|i[91266]<<24,f[g+144>>2]=g+1164,f[g+148>>2]=g+512,f[g+152>>2]=g+1152,(0|aA(A,91302,g+144|0))<2|f[49848]>59||(A=lg(g+512|0))&&(a[G(f[49848],3)+199408|0]=A,A=lg(g+1152|0),r=f[49848],C=G(r,3)+199408|0,a[C+1|0]=A,f[49848]=r+1,a[C+2|0]=f[g+1164>>2]),Z=1;continue;case 10:f[g+1140>>2]=0,r=f[32972],f[r+100>>2]=0,f[g+164>>2]=r+100,f[g+160>>2]=r+96,aA(A,87106,g+160|0);continue;case 11:if(f[g+176>>2]=g+1140,1!=(0|aA(A,87268,g+176|0)))continue;f[f[32972]+88>>2]=f[g+1140>>2]<<5;continue;case 12:if(f[g+192>>2]=g+1140,1!=(0|aA(A,87268,g+192|0)))continue;f[f[32972]+92>>2]=f[g+1140>>2];continue;case 13:if(f[g+208>>2]=g+1140,1!=(0|aA(A,87268,g+208|0)))continue;r=f[32972],(0|(A=f[g+1140>>2]))>=5&&(f[r+108>>2]=1,f[g+1140>>2]=4,A=4),f[r+104>>2]=A+1;continue;case 14:for(f[g+552>>2]=-1,f[g+556>>2]=-1,f[g+544>>2]=-1,f[g+548>>2]=-1,f[g+536>>2]=-1,f[g+540>>2]=-1,f[g+528>>2]=-1,f[g+532>>2]=-1,f[g+240>>2]=P,f[g+244>>2]=N,f[g+248>>2]=H,f[g+252>>2]=Y,f[g+256>>2]=p,f[g+260>>2]=h,f[g+520>>2]=-1,f[g+524>>2]=-1,f[g+512>>2]=-1,f[g+516>>2]=-1,f[g+228>>2]=z,f[g+232>>2]=y,f[g+236>>2]=F,f[g+224>>2]=g+512,aA(A,84222,g+224|0),w=f[32972],A=0,b=f[g+516>>2],C=0;;){if(r=C,s=b,t=A,-1==(0|(C=f[(b=(A<<=2)+(g+512|0)|0)>>2]))&&(C=8e3,f[b>>2]=8e3,t&&(f[(g+512|0)+(4|A)>>2]=f[508+(A+g|0)>>2])),b=f[(g+512|0)+(4|A)>>2],!((0|r)>=(0|(C=(0|C)/8|0))||(0|(k=C-r|0))<=0||(o=r+1|0,A=r,1&k&&(a[344+(r+w|0)|0]=(0|s)>=255?255:s,A=o),(0|C)==(0|o))))for(o=b-s|0;c=w+344|0,n=s+((0|G(o,A-r|0))/(0|k)|0)|0,a[c+A|0]=(0|n)>=255?255:n,n=s+((0|G(o,(M=A+1|0)-r|0))/(0|k)|0)|0,a[c+M|0]=(0|n)>=255?255:n,(0|C)!=(0|(A=A+2|0)););if(A=t+2|0,!(t>>>0<10))break}continue;case 15:if(f[g+272>>2]=g+1140,1!=(0|aA(A,87268,g+272|0)))continue;f[f[32972]+112>>2]=(f[g+1140>>2]<<6)/100;continue;case 16:r=f[32972],f[(C=r+300|0)>>2]=0,f[C+4>>2]=0,f[(b=r+292|0)>>2]=0,f[b+4>>2]=0,f[(s=r+284|0)>>2]=0,f[s+4>>2]=0,f[(t=r+276|0)>>2]=0,f[t+4>>2]=0,f[g+316>>2]=r+304,f[g+312>>2]=C,f[g+308>>2]=r+296,f[g+304>>2]=b,f[g+300>>2]=r+288,f[g+296>>2]=s,f[g+292>>2]=r+280,f[g+288>>2]=t,r=aA(A,84553,g+288|0),A=f[32972],f[A+272>>2]=r,f[A+276>>2]=0-f[A+276>>2],f[A+284>>2]=0-f[A+284>>2],f[A+292>>2]=0-f[A+292>>2],f[A+300>>2]=0-f[A+300>>2];continue;case 17:r=f[32972],f[(C=r+336|0)>>2]=0,f[C+4>>2]=0,f[(b=r+328|0)>>2]=0,f[b+4>>2]=0,f[(s=r+320|0)>>2]=0,f[s+4>>2]=0,f[(t=r+312|0)>>2]=0,f[t+4>>2]=0,f[g+348>>2]=r+340,f[g+344>>2]=C,f[g+340>>2]=r+332,f[g+336>>2]=b,f[g+332>>2]=r+324,f[g+328>>2]=s,f[g+324>>2]=r+316,f[g+320>>2]=t,A=aA(A,84553,g+320|0),f[f[32972]+308>>2]=A;continue;case 36:r=f[32972],f[g+352>>2]=r+120,f[g+356>>2]=r+124,f[g+1140>>2]=aA(A,87106,g+352|0);continue;case 33:f[g+368>>2]=f[32972]+84,aA(A,87268,g+368|0),UA(3);continue;case 31:r=f[32972],f[(C=r+156|0)>>2]=0,f[C+4>>2]=0,f[(b=r+148|0)>>2]=0,f[b+4>>2]=0,f[(s=r+140|0)>>2]=0,f[s+4>>2]=0,f[(t=r+132|0)>>2]=0,f[t+4>>2]=0,f[g+412>>2]=r+160,f[g+408>>2]=C,f[g+404>>2]=r+152,f[g+400>>2]=b,f[g+396>>2]=r+144,f[g+392>>2]=s,f[g+388>>2]=r+136,f[g+384>>2]=t,aA(A,84553,g+384|0),A=f[32972],f[A+152>>2]=f[A+152>>2]-40;continue;case 32:f[g+416>>2]=145740,aA(A,87268,g+416|0),UA(3);continue;case 6:case 7:continue;default:break e}f[g+16>>2]=g+704,eC(v,87359,g+16|0)}}tr(l)}e:{if((A=f[47192])|D){if(D)break e}else A=q(m),f[47192]=A;g:{if(!u){if((0|(A=Ng(g+896|0)))<0&&(f[g>>2]=g+896,eC(f[30450],87567,g),A=0),f[f[32972]+60>>2]=A,r=f[47192],f[r+292>>2]=A,HA(r,d,4&e),i[132848])break g;aC(f[47192]),r=0;break A}f[f[32972]+60>>2]=0,f[A+292>>2]=0}a[x+201088|0]=0}r=f[32972]}return V=g+1168|0,r}function aA(A,e,g){var r,C,b,s=0,t=0,n=0,k=0,o=0,B=0,c=0,w=0,E=0,D=0,u=0,l=0,x=0,d=0,m=0,M=0,v=0,h=0,p=0;V=b=V-16|0,f[b+12>>2]=g,V=s=V-144|0,r=ue(s,0,144),f[r+76>>2]=-1,f[r+44>>2]=A,f[r+32>>2]=18,f[r+84>>2]=A,s=e,v=g,A=0,V=C=V-304|0;A:{e:{g:if(f[r+4>>2]||(Wg(r),f[r+4>>2])){if(!(e=i[0|s]))break A;r:{C:{a:{I:{for(;;){f:{i:if(32==(0|(e&=255))|e-9>>>0<5){for(;e=s,s=s+1|0,32==(0|(g=i[e+1|0]))|g-9>>>0<5;);for(Tg(r,0,0);(0|(g=f[r+4>>2]))==f[r+104>>2]?g=ce(r):(f[r+4>>2]=g+1,g=i[0|g]),32==(0|g)|g-9>>>0<5;);s=f[r+4>>2],(0|(g=f[r+116>>2]))>0|(0|g)>=0&&(s=s-1|0,f[r+4>>2]=s),s=g=s-f[r+44>>2]|0,o=l+f[r+124>>2]|0,o=(t=g>>31)+((g=E+f[r+120>>2]|0)>>>0>>0?o+1|0:o)|0,l=(E=g+s|0)>>>0>>0?o+1|0:o}else{b:{s:{t:{if(37==i[0|s]){if(42==(0|(e=i[s+1|0])))break t;if(37!=(0|e))break s}if(Tg(r,0,0),37!=i[0|s])(0|(e=f[r+4>>2]))==f[r+104>>2]?e=ce(r):(f[r+4>>2]=e+1,e=i[0|e]);else{for(;(0|(e=f[r+4>>2]))==f[r+104>>2]?e=ce(r):(f[r+4>>2]=e+1,e=i[0|e]),32==(0|e)|e-9>>>0<5;);s=s+1|0}if(i[0|s]!=(0|e)){if((0|(g=f[r+116>>2]))>0|(0|g)>=0&&(f[r+4>>2]=f[r+4>>2]-1),(0|e)>=0)break A;if(n=0,h)break A;break g}k=(t=e=f[r+4>>2]-f[r+44>>2]|0)>>31,e=l+f[r+124>>2]|0,n=((g=E+f[r+120>>2]|0)>>>0>>0?e+1|0:e)+k|0,l=(E=g+t|0)>>>0>>0?n+1|0:n,e=s;break i}B=0,e=s+2|0;break b}36!=i[s+2|0]|e-48>>>0>=10?(B=f[v>>2],v=v+4|0,e=s+1|0):(e=i[s+1|0]-48|0,f[(g=V-16|0)+12>>2]=v,e=(e>>>0>1?(e<<2)-4|0:0)+v|0,f[g+8>>2]=e+4,B=f[e>>2],e=s+3|0)}if(c=0,s=0,i[0|e]-48>>>0<10)for(;s=(i[0|e]+G(s,10)|0)-48|0,g=i[e+1|0],e=e+1|0,g-48>>>0<10;);109==(0|(D=i[0|e]))&&(w=0,c=!!(0|B),D=i[e+1|0],A=0,e=e+1|0),e=(g=e)+1|0,t=3,n=c;b:{s:switch(D-65|0){case 39:t=g+2|0,e=(g=104==i[g+1|0])?t:e,t=g?-2:-1;break b;case 43:t=g+2|0,e=(g=108==i[g+1|0])?t:e,t=g?3:1;break b;case 51:case 57:t=1;break b;case 11:t=2;break b;case 41:break b;case 0:case 2:case 4:case 5:case 6:case 18:case 23:case 26:case 32:case 34:case 35:case 36:case 37:case 38:case 40:case 45:case 46:case 47:case 50:case 52:case 55:break s;default:break r}t=0,e=g}n=t,m=(t=3==(47&(g=i[0|e])))?1:n;b:if(91!=(0|(d=t?32|g:g))){s:{if(110!=(0|d)){if(99!=(0|d))break s;s=(0|s)<=1?1:s;break b}Br(B,m,E,l);break i}for(Tg(r,0,0);(0|(g=f[r+4>>2]))==f[r+104>>2]?g=ce(r):(f[r+4>>2]=g+1,g=i[0|g]),32==(0|g)|g-9>>>0<5;);g=f[r+4>>2],(0|(t=f[r+116>>2]))>0|(0|t)>=0&&(g=g-1|0,f[r+4>>2]=g),t=g=g-f[r+44>>2]|0,o=l+f[r+124>>2]|0,l=(n=g>>31)+((g=E+f[r+120>>2]|0)>>>0>>0?o+1|0:o)|0,l=(E=g+t|0)>>>0>>0?l+1|0:l}if(u=s,Tg(r,s,x=s>>31),(0|(g=f[r+4>>2]))==f[r+104>>2]){if((0|ce(r))<0)break C}else f[r+4>>2]=g+1;(0|(g=f[r+116>>2]))>0|(0|g)>=0&&(f[r+4>>2]=f[r+4>>2]-1),g=16;b:{s:{t:{n:{k:switch(d-88|0){default:if((g=d-65|0)>>>0>6|!(1<>2]-f[r+44>>2]|0,f[r+120>>2]!=(0-g|0)|f[r+124>>2]!=(0-((g>>31)+!!(0|g)|0)|0))break t;break a;case 3:case 11:case 27:if(115==(16|d)){if(ue(C+32|0,-1,257),a[C+32|0]=0,115!=(0|d))break s;a[C+65|0]=0,a[C+46|0]=0,I[C+42>>1]=0,I[C+44>>1]=0;break s}ue(C+32|0,k=94==(0|(t=i[e+1|0])),257),a[C+32|0]=0,g=k?e+2|0:e+1|0;o:{B:{c:{if(45!=(0|(e=i[(k?2:1)+e|0]))){if(93==(0|e))break c;t=94!=(0|t),e=g;break o}t=94!=(0|t),a[C+78|0]=t;break B}t=94!=(0|t),a[C+126|0]=t}e=g+1|0}for(;;){if(45==(0|(g=i[0|e]))){if(g=45,!(!(k=i[e+1|0])|93==(0|k))){if(n=e+1|0,k>>>0<=(e=i[e-1|0])>>>0)g=k;else for(;a[(e=e+1|0)+(C+32|0)|0]=t,(g=i[0|n])>>>0>e>>>0;);e=n}}else{if(!g)break C;if(93==(0|g))break s}a[33+(g+C|0)|0]=t,e=e+1|0}case 23:g=8;break n;case 12:case 29:g=10;break n;case 1:case 2:case 4:case 5:case 6:case 7:case 8:case 10:case 16:case 18:case 19:case 20:case 21:case 22:case 25:case 26:case 28:case 30:case 31:break b;case 0:case 24:case 32:break n;case 17:break k}g=0}k=0,o=0,t=0,n=0,D=0,V=M=V-16|0;n:if(1!=(0|g)&g>>>0<=36){for(;(0|(s=f[r+4>>2]))==f[r+104>>2]?s=ce(r):(f[r+4>>2]=s+1,s=i[0|s]),32==(0|s)|s-9>>>0<5;);k:{o:switch(s-43|0){case 0:case 2:break o;default:break k}D=45==(0|s)?-1:0,(0|(s=f[r+4>>2]))==f[r+104>>2]?s=ce(r):(f[r+4>>2]=s+1,s=i[0|s])}k:{o:{B:{c:{if(!(!!(0|g)&16!=(0|g)|48!=(0|s))){if((0|(s=f[r+4>>2]))==f[r+104>>2]?s=ce(r):(f[r+4>>2]=s+1,s=i[0|s]),88==(-33&s)){if(g=16,(0|(s=f[r+4>>2]))==f[r+104>>2]?s=ce(r):(f[r+4>>2]=s+1,s=i[0|s]),i[s+121329|0]<16)break B;(0|(g=f[r+116>>2]))>0|(0|g)>=0&&(f[r+4>>2]=f[r+4>>2]-1),Tg(r,0,0);break n}if(g)break c;g=8;break B}if(!((g=g||10)>>>0>i[s+121329|0])){(0|(g=f[r+116>>2]))>0|(0|g)>=0&&(f[r+4>>2]=f[r+4>>2]-1),Tg(r,0,0),f[56798]=28;break n}}if(10==(0|g)){if((t=s-48|0)>>>0<=9){for(g=0;n=(g=G(g,10)+t|0)>>>0<429496729,(0|(s=f[r+4>>2]))==f[r+104>>2]?s=ce(r):(f[r+4>>2]=s+1,s=i[0|s]),n&(t=s-48|0)>>>0<=9;);k=g}c:if(!(t>>>0>9)){for(g=Cr(k,0,10,0),n=U;;){if(o=n,n=429496729==(0|(o=(k=g+t|0)>>>0>>0?o+1|0:o))&k>>>0>=2576980378|o>>>0>429496729,(0|(g=f[r+4>>2]))==f[r+104>>2]?s=ce(r):(f[r+4>>2]=g+1,s=i[0|g]),n|(t=s-48|0)>>>0>9)break c;if(g=Cr(k,o,10,0),!(-1==(0|(n=U))&~t>>>0>=g>>>0|-1!=(0|n)))break}g=10;break o}if(g=10,t>>>0<=9)break o;break k}}if(g-1&g){if((n=i[s+121329|0])>>>0>>0){for(;k=(t=G(g,t)+n|0)>>>0<119304647,(0|(s=f[r+4>>2]))==f[r+104>>2]?s=ce(r):(f[r+4>>2]=s+1,s=i[0|s]),k&(n=i[s+121329|0])>>>0>>0;);k=t}if(g>>>0<=n>>>0)break o;for(;;){if(t=Cr(k,o,g,0),-1==(0|(u=U))&~(n&=255)>>>0>>0)break o;if(o=u,o=(k=t+n|0)>>>0>>0?o+1|0:o,(0|(s=f[r+4>>2]))==f[r+104>>2]?s=ce(r):(f[r+4>>2]=s+1,s=i[0|s]),g>>>0<=(n=i[s+121329|0])>>>0)break o;if(gg(M,g,0,0,0,k,o,0,0),f[M+8>>2]|f[M+12>>2])break}}else{if(u=a[84400+(G(g,23)>>>5&7)|0],(t=i[s+121329|0])>>>0>>0){for(;k=(n=n<>>0<134217728,(0|(s=f[r+4>>2]))==f[r+104>>2]?s=ce(r):(f[r+4>>2]=s+1,s=i[0|s]),k&(t=i[s+121329|0])>>>0>>0;);k=n}if(!(g>>>0<=t>>>0||(x=31&u,(63&u)>>>0>=32?(n=0,x=-1>>>x|0):x=(n=-1>>>x|0)|(1<>>0>x>>>0)))for(;;){if(p=255&t,t=k,s=31&u,(63&u)>>>0>=32?(o=t<>>32-s|o<>2]))==f[r+104>>2]?s=ce(r):(f[r+4>>2]=s+1,s=i[0|s]),g>>>0<=(t=i[s+121329|0])>>>0)break o;if(!((0|n)==(0|o)&k>>>0<=x>>>0|n>>>0>o>>>0))break}}}if(!(i[s+121329|0]>=g>>>0)){for(;(0|(s=f[r+4>>2]))==f[r+104>>2]?s=ce(r):(f[r+4>>2]=s+1,s=i[0|s]),i[s+121329|0]>>0;);f[56798]=68,D=0,k=-1,o=-1}}(0|(g=f[r+116>>2]))>0|(0|g)>=0&&(f[r+4>>2]=f[r+4>>2]-1),k=(g=k^D)-D|0,o=((s=D>>31)^o)-((g>>>0>>0)+s|0)|0}else f[56798]=28;if(V=M+16|0,g=f[r+4>>2]-f[r+44>>2]|0,f[r+120>>2]==(0-g|0)&f[r+124>>2]==(0-((g>>31)+!!(0|g)|0)|0))break a;if(!(!B|112!=(0|d))){f[B>>2]=k;break b}Br(B,m,k,o);break b}if(!B)break b;s=f[C+16>>2],g=f[C+20>>2],t=f[C+8>>2],c=f[C+12>>2];t:switch(0|m){case 0:V=o=V-32|0;n:if(0|(n=(k=2147483647&g)-1065418752|0)>>>0<(u=k-1082064896|0)>>>0){if(k=(33554431&g)<<7|s>>>25,u=n=0,!(!n&16777216==(0|(s&=33554431))?!(t|c):!n&s>>>0<16777216)){n=k+1073741825|0;break n}if(n=k+1073741824|0,16777216^s|t|c|u)break n;n=(1&k)+n|0}else(!s&2147418112==(0|k)?!(t|c):k>>>0<2147418112)?(n=2139095040,k>>>0>1082064895||(n=0,(k=k>>>16|0)>>>0<16145||(Ve(o+16|0,t,c,s,n=65535&g|65536,k-16129|0),Ke(o,t,c,s,n,16257-k|0),s=f[o+8>>2],n=(33554431&f[o+12>>2])<<7|s>>>25,k=f[o>>2]|!!(f[o+16>>2]|f[o+24>>2]|f[o+20>>2]|f[o+28>>2]),c=f[o+4>>2],(!(t=0)&16777216==(0|(s&=33554431))?!(k|c):!t&s>>>0<16777216)?16777216^s|k|t|c||(n=(1&n)+n|0):n=n+1|0))):n=4194303&((33554431&g)<<7|s>>>25)|2143289344;V=o+32|0,f[B>>2]=-2147483648&g|n;break b;case 1:Q[B>>3]=ge(t,c,s,g);break b;case 2:break t;default:break b}f[B>>2]=t,f[B+4>>2]=c,f[B+8>>2]=s,f[B+12>>2]=g;break b}t=(M=99!=(0|d))?31:s+1|0;s:if(1!=(0|m)){if(c){if(s=0,!(g=IA(t)))break I;for(;;){for(A=g;;){if((0|(g=f[r+4>>2]))==f[r+104>>2]?g=ce(r):(f[r+4>>2]=g+1,g=i[0|g]),!i[33+(g+C|0)|0]){t=0,w=A;break s}if(a[A+s|0]=g,(0|t)==(0|(s=s+1|0)))break}if(n=1,!(g=OA(A,t=t<<1|1)))break}w=A,A=0;break r}if(s=0,B)for(;;){if((0|(A=f[r+4>>2]))==f[r+104>>2]?A=ce(r):(f[r+4>>2]=A+1,A=i[0|A]),!i[33+(A+C|0)|0]){t=0,w=A=B;break s}a[s+B|0]=A,s=s+1|0}for(;(0|(A=f[r+4>>2]))==f[r+104>>2]?A=ce(r):(f[r+4>>2]=A+1,A=i[0|A]),i[33+(A+C|0)|0];);A=0,w=0,t=0}else{if(g=B,c&&!(g=IA(t<<2)))break I;for(f[C+296>>2]=0,f[C+300>>2]=0,s=0;;){A=g;t:{for(;;){if((0|(g=f[r+4>>2]))==f[r+104>>2]?g=ce(r):(f[r+4>>2]=g+1,g=i[0|g]),!i[33+(g+C|0)|0])break t;a[C+27|0]=g,k=C+28|0,g=f[(n=(g=C+296|0)||228604)>>2];n:{k:{o:{B:{if(!(w=C+27|0)){if(g)break B;g=0;break n}if(!g){if((0|(o=(g=i[0|w])<<24>>24))>=0){k&&(f[k>>2]=g),g=!!(0|o);break n}if(!f[f[56841]>>2]){if(g=1,!k)break o;f[k>>2]=57343&o,g=1;break n}if((g=g-194|0)>>>0>50)break B;g=f[124752+(g<<2)>>2];break k}if(D=1,!(((m=(o=i[0|w])>>>3|0)-16|(g>>26)+m)>>>0>7))for(;;){if(D=D-1|0,(0|(g=o-128|g<<6))>=0){f[n>>2]=0,k&&(f[k>>2]=g),g=1-D|0;break n}if(!D)break k;if(128!=(192&(o=i[0|(w=w+1|0)])))break}}f[n>>2]=0,f[56798]=25,g=-1}break n}f[n>>2]=g,g=-2}if(-2!=(0|g)){if(w=0,-1==(0|g))break C;if(A&&(f[(s<<2)+A>>2]=f[C+28>>2],s=s+1|0),!(!c|(0|s)!=(0|t)))break}}if(n=1,g=OA(A,(t=t<<1|1)<<2))continue;break r}break}if(w=0,t=A,C+296|0&&f[C+296>>2])break C}if(g=f[r+4>>2],(0|(k=f[r+116>>2]))>0|(0|k)>=0&&(g=g-1|0,f[r+4>>2]=g),g=(k=g-f[r+44>>2]|0)+f[r+120>>2]|0,o=f[r+124>>2]+(k>>31)|0,!((o=g>>>0>>0?o+1|0:o)|g)|!(M|(0|g)==(0|u)&(0|o)==(0|x)))break f;c&&(f[B>>2]=A),99!=(0|d)&&(t&&(f[(s<<2)+t>>2]=0),w?a[s+w|0]=0:w=0),A=t}s=g=f[r+4>>2]-f[r+44>>2]|0,n=l+f[r+124>>2]|0,l=(t=g>>31)+((g=E+f[r+120>>2]|0)>>>0>>0?n+1|0:n)|0,l=(E=g+s|0)>>>0>>0?l+1|0:l,h=!!(0|B)+h|0}if(s=e+1|0,e=i[e+1|0])continue;break A}break}A=t;break a}n=1,w=0,A=0;break r}n=c;break e}n=c}if(h)break e}h=-1}n&&(mA(w),mA(A))}return V=C+304|0,V=r+144|0,V=b+16|0,h}function IA(A){var e,g=0,r=0,C=0,a=0,I=0,b=0,s=0,t=0,n=0,k=0;V=e=V-16|0;A:{e:{g:{r:{C:{a:{I:{f:{i:{if((A|=0)>>>0<=244){if(3&(g=(b=f[57152])>>>(r=(s=A>>>0<11?16:A+11&-8)>>>3|0)|0)){g=(A=(r=r+(1&~g)|0)<<3)+228648|0,C=f[A+228656>>2],(0|g)!=(0|(A=f[C+8>>2]))?(f[A+12>>2]=g,f[g+8>>2]=A):f[57152]=jr(-2,r)&b,A=C+8|0,g=r<<3,f[C+4>>2]=3|g,f[(g=g+C|0)+4>>2]=1|f[g+4>>2];break A}if((k=f[57154])>>>0>=s>>>0)break i;if(g){g=(A=(C=nC(0-(A=(0-(A=2<>2],(0|g)!=(0|(A=f[a+8>>2]))?(f[A+12>>2]=g,f[g+8>>2]=A):(b=jr(-2,C)&b,f[57152]=b),f[a+4>>2]=3|s,C=(A=C<<3)-s|0,f[(r=a+s|0)+4>>2]=1|C,f[A+a>>2]=C,k&&(g=228648+(-8&k)|0,I=f[57157],(A=1<<(k>>>3))&b?A=f[g+8>>2]:(f[57152]=A|b,A=g),f[g+8>>2]=I,f[A+12>>2]=I,f[I+12>>2]=g,f[I+8>>2]=A),A=a+8|0,f[57157]=r,f[57154]=C;break A}if(!(n=f[57153]))break i;for(r=f[228912+(nC(0-n&n)<<2)>>2],I=(-8&f[r+4>>2])-s|0,g=r;(A=f[g+16>>2])||(A=f[g+20>>2]);)I=(C=(g=(-8&f[A+4>>2])-s|0)>>>0>>0)?g:I,r=C?A:r,g=A;if(t=f[r+24>>2],(0|(C=f[r+12>>2]))!=(0|r)){A=f[r+8>>2],f[A+12>>2]=C,f[C+8>>2]=A;break e}if(!(A=f[(g=r+20|0)>>2])){if(!(A=f[r+16>>2]))break f;g=r+16|0}for(;a=g,C=A,(A=f[(g=A+20|0)>>2])||(g=C+16|0,A=f[C+16>>2]););f[a>>2]=0;break e}if(s=-1,!(A>>>0>4294967231)&&(s=-8&(A=A+11|0),n=f[57153])){I=0-s|0,b=0,s>>>0<256||(b=31,s>>>0>16777215||(b=62+((s>>>38-(A=D(A>>>8|0))&1)-(A<<1)|0)|0));b:{s:{if(g=f[228912+(b<<2)>>2])for(A=0,r=s<<(31!=(0|b)?25-(b>>>1|0):0);;){if(!((a=(-8&f[g+4>>2])-s|0)>>>0>=I>>>0||(C=g,I=a,a))){I=0,A=g;break s}if(a=f[g+20>>2],g=f[16+((r>>>29&4)+g|0)>>2],A=a?(0|a)==(0|g)?A:a:A,r<<=1,!g)break}else A=0;if(!(A|C)){if(C=0,!(A=(0-(A=2<>2]}if(!A)break b}for(;I=(r=(g=(-8&f[A+4>>2])-s|0)>>>0>>0)?g:I,C=r?A:C,A=(g=f[A+16>>2])||f[A+20>>2];);}if(!(!C|f[57154]-s>>>0<=I>>>0)){if(b=f[C+24>>2],(0|C)!=(0|(r=f[C+12>>2]))){A=f[C+8>>2],f[A+12>>2]=r,f[r+8>>2]=A;break g}if(!(A=f[(g=C+20|0)>>2])){if(!(A=f[C+16>>2]))break I;g=C+16|0}for(;a=g,r=A,(A=f[(g=A+20|0)>>2])||(g=r+16|0,A=f[r+16>>2]););f[a>>2]=0;break g}}}if((A=f[57154])>>>0>=s>>>0){C=f[57157],(g=A-s|0)>>>0>=16?(f[(r=C+s|0)+4>>2]=1|g,f[A+C>>2]=g,f[C+4>>2]=3|s):(f[C+4>>2]=3|A,f[(A=A+C|0)+4>>2]=1|f[A+4>>2],r=0,g=0),f[57154]=g,f[57157]=r,A=C+8|0;break A}if((t=f[57155])>>>0>s>>>0){g=t-s|0,f[57155]=g,A=(r=f[57158])+s|0,f[57158]=A,f[A+4>>2]=1|g,f[r+4>>2]=3|s,A=r+8|0;break A}if(A=0,n=s+47|0,f[57270]?r=f[57272]:(f[57273]=-1,f[57274]=-1,f[57271]=4096,f[57272]=4096,f[57270]=e+12&-16^1431655768,f[57275]=0,f[57263]=0,r=4096),(g=(a=n+r|0)&(I=0-r|0))>>>0<=s>>>0)break A;if((C=f[57262])&&C>>>0<(b=(r=f[57260])+g|0)>>>0|r>>>0>=b>>>0)break A;i:{if(!(4&i[229052])){b:{s:{t:{n:{if(C=f[57158])for(A=229056;;){if((r=f[A>>2])>>>0<=C>>>0&C>>>0>2]>>>0)break n;if(!(A=f[A+8>>2]))break}if(-1==(0|(r=Dr(0))))break b;if(b=g,(A=(C=f[57271])-1|0)&r&&(b=(g-r|0)+(A+r&0-C)|0),b>>>0<=s>>>0)break b;if((C=f[57262])&&C>>>0<(I=(A=f[57260])+b|0)>>>0|A>>>0>=I>>>0)break b;if((0|r)!=(0|(A=Dr(b))))break t;break i}if((0|(r=Dr(b=I&a-t)))==(f[A>>2]+f[A+4>>2]|0))break s;A=r}if(-1==(0|A))break b;if(s+48>>>0<=b>>>0){r=A;break i}if(-1==(0|Dr(r=(r=f[57272])+(n-b|0)&0-r)))break b;b=r+b|0,r=A;break i}if(-1!=(0|r))break i}f[57263]=4|f[57263]}if(-1==(0|(r=Dr(g)))|-1==(0|(A=Dr(0)))|A>>>0<=r>>>0)break r;if((b=A-r|0)>>>0<=s+40>>>0)break r}A=f[57260]+b|0,f[57260]=A,A>>>0>c[57261]&&(f[57261]=A);i:{if(a=f[57158]){for(A=229056;;){if(((C=f[A>>2])+(g=f[A+4>>2])|0)==(0|r))break i;if(!(A=f[A+8>>2]))break}break a}for((A=f[57156])>>>0<=r>>>0&&A||(f[57156]=r),A=0,f[57265]=b,f[57264]=r,f[57160]=-1,f[57161]=f[57270],f[57267]=0;g=(C=A<<3)+228648|0,f[C+228656>>2]=g,f[C+228660>>2]=g,32!=(0|(A=A+1|0)););g=(C=b-40|0)-(A=r+8&7?-8-r&7:0)|0,f[57155]=g,A=A+r|0,f[57158]=A,f[A+4>>2]=1|g,f[4+(r+C|0)>>2]=40,f[57159]=f[57274];break C}if(8&i[A+12|0]|C>>>0>a>>>0|r>>>0<=a>>>0)break a;f[A+4>>2]=g+b,r=(A=a+8&7?-8-a&7:0)+a|0,f[57158]=r,A=(g=f[57155]+b|0)-A|0,f[57155]=A,f[r+4>>2]=1|A,f[4+(g+a|0)>>2]=40,f[57159]=f[57274];break C}C=0;break e}r=0;break g}c[57156]>r>>>0&&(f[57156]=r),g=r+b|0,A=229056;a:{I:{f:{i:{b:{s:{for(;;){if((0|g)!=f[A>>2]){if(A=f[A+8>>2])continue;break s}break}if(!(8&i[A+12|0]))break b}for(A=229056;;){if((g=f[A>>2])>>>0<=a>>>0&&(I=g+f[A+4>>2]|0)>>>0>a>>>0)break i;A=f[A+8>>2]}}if(f[A>>2]=r,f[A+4>>2]=f[A+4>>2]+b,f[(n=(r+8&7?-8-r&7:0)+r|0)+4>>2]=3|s,A=(b=g+(g+8&7?-8-g&7:0)|0)-(t=s+n|0)|0,(0|a)==(0|b)){f[57158]=t,A=f[57155]+A|0,f[57155]=A,f[t+4>>2]=1|A;break I}if(f[57157]==(0|b)){f[57157]=t,A=f[57154]+A|0,f[57154]=A,f[t+4>>2]=1|A,f[A+t>>2]=A;break I}if(1==(3&(I=f[b+4>>2]))){a=-8&I;b:if(I>>>0<=255){if(C=f[b+8>>2],g=I>>>3|0,(0|(r=f[b+12>>2]))==(0|C)){f[57152]=f[57152]&jr(-2,g);break b}f[C+12>>2]=r,f[r+8>>2]=C}else{if(s=f[b+24>>2],(0|b)==(0|(r=f[b+12>>2])))if((g=f[(I=b+20|0)>>2])||(g=f[(I=b+16|0)>>2])){for(;C=I,(g=f[(I=(r=g)+20|0)>>2])||(I=r+16|0,g=f[r+16>>2]););f[C>>2]=0}else r=0;else g=f[b+8>>2],f[g+12>>2]=r,f[r+8>>2]=g;if(s){C=f[b+28>>2];s:{if(f[(g=228912+(C<<2)|0)>>2]==(0|b)){if(f[g>>2]=r,r)break s;f[57153]=f[57153]&jr(-2,C);break b}if(f[s+(f[s+16>>2]==(0|b)?16:20)>>2]=r,!r)break b}f[r+24>>2]=s,(g=f[b+16>>2])&&(f[r+16>>2]=g,f[g+24>>2]=r),(g=f[b+20>>2])&&(f[r+20>>2]=g,f[g+24>>2]=r)}}I=f[(b=a+b|0)+4>>2],A=A+a|0}if(f[b+4>>2]=-2&I,f[t+4>>2]=1|A,f[A+t>>2]=A,A>>>0<=255){g=228648+(-8&A)|0,(r=f[57152])&(A=1<<(A>>>3))?A=f[g+8>>2]:(f[57152]=A|r,A=g),f[g+8>>2]=t,f[A+12>>2]=t,f[t+12>>2]=g,f[t+8>>2]=A;break I}if(I=31,A>>>0<=16777215&&(I=62+((A>>>38-(g=D(A>>>8|0))&1)-(g<<1)|0)|0),f[t+28>>2]=I,f[t+16>>2]=0,f[t+20>>2]=0,g=228912+(I<<2)|0,(C=f[57153])&(r=1<>>1|0):0),r=f[g>>2];;){if(g=r,(-8&f[r+4>>2])==(0|A))break f;if(r=I>>>29|0,I<<=1,!(r=f[(C=(4&r)+g|0)+16>>2]))break}f[C+16>>2]=t}else f[57153]=r|C,f[g>>2]=t;f[t+24>>2]=g,f[t+12>>2]=t,f[t+8>>2]=t;break I}for(g=(C=b-40|0)-(A=r+8&7?-8-r&7:0)|0,f[57155]=g,A=A+r|0,f[57158]=A,f[A+4>>2]=1|g,f[4+(r+C|0)>>2]=40,f[57159]=f[57274],f[(C=(A=(I+(I-39&7?39-I&7:0)|0)-47|0)>>>0>>0?a:A)+4>>2]=27,A=f[57267],f[C+16>>2]=f[57266],f[C+20>>2]=A,A=f[57265],f[C+8>>2]=f[57264],f[C+12>>2]=A,f[57266]=C+8,f[57265]=b,f[57264]=r,f[57267]=0,A=C+24|0;f[A+4>>2]=7,g=A+8|0,A=A+4|0,g>>>0>>0;);if((0|C)==(0|a))break C;if(f[C+4>>2]=-2&f[C+4>>2],I=C-a|0,f[a+4>>2]=1|I,f[C>>2]=I,I>>>0<=255){g=228648+(-8&I)|0,(r=f[57152])&(A=1<<(I>>>3))?A=f[g+8>>2]:(f[57152]=A|r,A=g),f[g+8>>2]=a,f[A+12>>2]=a,f[a+12>>2]=g,f[a+8>>2]=A;break C}if(A=31,I>>>0<=16777215&&(A=62+((I>>>38-(A=D(I>>>8|0))&1)-(A<<1)|0)|0),f[a+28>>2]=A,f[a+16>>2]=0,f[a+20>>2]=0,g=228912+(A<<2)|0,(C=f[57153])&(r=1<>>1|0):0),C=f[g>>2];;){if((0|I)==(-8&f[(g=C)+4>>2]))break a;if(r=A>>>29|0,A<<=1,!(C=f[(r=(4&r)+g|0)+16>>2]))break}f[r+16>>2]=a}else f[57153]=r|C,f[g>>2]=a;f[a+24>>2]=g,f[a+12>>2]=a,f[a+8>>2]=a;break C}A=f[g+8>>2],f[A+12>>2]=t,f[g+8>>2]=t,f[t+24>>2]=0,f[t+12>>2]=g,f[t+8>>2]=A}A=n+8|0;break A}A=f[g+8>>2],f[A+12>>2]=a,f[g+8>>2]=a,f[a+24>>2]=0,f[a+12>>2]=g,f[a+8>>2]=A}if(!((A=f[57155])>>>0<=s>>>0)){g=A-s|0,f[57155]=g,A=(r=f[57158])+s|0,f[57158]=A,f[A+4>>2]=1|g,f[r+4>>2]=3|s,A=r+8|0;break A}}f[56798]=48,A=0;break A}g:if(b){g=f[C+28>>2];r:{if(f[(A=228912+(g<<2)|0)>>2]==(0|C)){if(f[A>>2]=r,r)break r;n=jr(-2,g)&n,f[57153]=n;break g}if(f[b+(f[b+16>>2]==(0|C)?16:20)>>2]=r,!r)break g}f[r+24>>2]=b,(A=f[C+16>>2])&&(f[r+16>>2]=A,f[A+24>>2]=r),(A=f[C+20>>2])&&(f[r+20>>2]=A,f[A+24>>2]=r)}g:if(I>>>0<=15)A=I+s|0,f[C+4>>2]=3|A,f[(A=A+C|0)+4>>2]=1|f[A+4>>2];else if(f[C+4>>2]=3|s,f[(a=C+s|0)+4>>2]=1|I,f[a+I>>2]=I,I>>>0<=255)g=228648+(-8&I)|0,(r=f[57152])&(A=1<<(I>>>3))?A=f[g+8>>2]:(f[57152]=A|r,A=g),f[g+8>>2]=a,f[A+12>>2]=a,f[a+12>>2]=g,f[a+8>>2]=A;else{A=31,I>>>0<=16777215&&(A=62+((I>>>38-(A=D(I>>>8|0))&1)-(A<<1)|0)|0),f[a+28>>2]=A,f[a+16>>2]=0,f[a+20>>2]=0,g=228912+(A<<2)|0;r:{if((r=1<>>1|0):0),s=f[g>>2];;){if((-8&f[(g=s)+4>>2])==(0|I))break r;if(r=A>>>29|0,A<<=1,!(s=f[(r=(4&r)+g|0)+16>>2]))break}f[r+16>>2]=a}else f[57153]=r|n,f[g>>2]=a;f[a+24>>2]=g,f[a+12>>2]=a,f[a+8>>2]=a;break g}A=f[g+8>>2],f[A+12>>2]=a,f[g+8>>2]=a,f[a+24>>2]=0,f[a+12>>2]=g,f[a+8>>2]=A}A=C+8|0;break A}e:if(t){g=f[r+28>>2];g:{if(f[(A=228912+(g<<2)|0)>>2]==(0|r)){if(f[A>>2]=C,C)break g;f[57153]=jr(-2,g)&n;break e}if(f[t+(f[t+16>>2]==(0|r)?16:20)>>2]=C,!C)break e}f[C+24>>2]=t,(A=f[r+16>>2])&&(f[C+16>>2]=A,f[A+24>>2]=C),(A=f[r+20>>2])&&(f[C+20>>2]=A,f[A+24>>2]=C)}I>>>0<=15?(A=I+s|0,f[r+4>>2]=3|A,f[(A=A+r|0)+4>>2]=1|f[A+4>>2]):(f[r+4>>2]=3|s,f[(C=r+s|0)+4>>2]=1|I,f[C+I>>2]=I,k&&(g=228648+(-8&k)|0,a=f[57157],(A=1<<(k>>>3))&b?A=f[g+8>>2]:(f[57152]=A|b,A=g),f[g+8>>2]=a,f[A+12>>2]=a,f[a+12>>2]=g,f[a+8>>2]=A),f[57157]=C,f[57154]=I),A=r+8|0}return V=e+16|0,0|A}function fA(A,e,g,r,C){var I,b=0,s=0,t=0,n=0,o=0,B=0,c=0,Q=0,G=0,w=0,E=0,D=0,u=0,l=0,x=0,d=0,m=0;V=I=V-544|0,ue(I+320|0,0,100),ue(I+208|0,0,100),n=g?f[g>>2]:n,G=f[A+12>>2],o=f[36115];A:{e:{g:{r:{for(;;){if(b=(0|(b=i[e+s|0]))>=(0|o)?13:b,a[I+s|0]=b,!b){b=s;break r}if(t=(0|o)<=(0|(t=i[(b=1|s)+e|0]))?13:t,a[b+I|0]=t,!t)break r;if(200==(0|(s=s+2|0)))break}s=198,E=i[I+199|0];break g}if(!b)break e;E=i[I+(s=b-1|0)|0],1!=(0|b)&&(s=b-2|0)}if(x=8&n,f[I+536>>2]=(x>>>3|0?3:7)&n,n=i[I+s|0],o=1,c=(0|(B=zA(A,I,I+432|0,I+540|0,I+536|0,1)))>=0?B:0,u=i[0|I])for(s=I,b=u;b=f[144464+((255&b)<<2)>>2],2==i[b+11|0]&&(1048576&(b=f[b+4>>2])||(t=(2097152&b)>>>21|0,t|=b=12==i[f[144464+(i[0|(Q=s+1|0)]<<2)>>2]+10|0],a[(I+208|0)+o|0]=t,w=f[144464+(i[(b?2:1)+s|0]<<2)>>2],t=i[w+11|0]-10>>>0<4294967289|!(32&i[w+6|0])&2==i[f[144464+(i[(b?3:2)+s|0]<<2)>>2]+11|0]?t:t?2:1,s=b?Q:s,a[(I+320|0)+o|0]=t,o=o+1|0)),b=i[0|(s=s+1|0)];);s=g=g?c:B;g:{r:{C:{a:{I:{f:{i:{b:{s:{t:{n:{k:switch(f[A+8>>2]-1|0){case 11:if((0|(g=f[I+540>>2]))<2)break s;if(s=1,c=1&(b=g-1|0),2!=(0|g))break n;o=0;break t;case 8:if((0|(b=f[I+540>>2]))<2)break r;if(o=3&(t=b-1|0),s=1,b-2>>>0>=3)for(c=-4&t,t=0;n=a[0|(b=(I+432|0)+s|0)],a[0|b]=(0|n)<0?4:n,n=a[b+1|0],a[b+1|0]=(0|n)<0?4:n,n=a[b+2|0],a[b+2|0]=(0|n)<0?4:n,n=b,b=a[b+3|0],a[n+3|0]=(0|b)<0?4:b,s=s+4|0,(0|c)!=(0|(t=t+4|0)););if(!o)break r;for(b=0;t=a[0|(n=(I+432|0)+s|0)],a[0|n]=(0|t)<0?4:t,s=s+1|0,(0|o)!=(0|(b=b+1|0)););break r;case 7:if(!i[I+322|0]|a[I+321|0]>0)break r;case 0:if(f[I+536>>2]|f[I+540>>2]<3)break r;if(f[I+536>>2]=2,s=4,g)break g;a[I+434|0]=4;break g;case 1:if(f[I+536>>2])break g;o:{B:{if((0|(o=f[I+540>>2]))>=3){g=o-2|0,f[I+536>>2]=g,s=g;c:if(512&G&&(b=f[144464+(E<<2)>>2],2!=(0|(t=i[b+11|0])))){s=f[b>>2],b=I;Q:{G:{if(26977!=(0|(c=f[A+212>>2]))){if(24942!=(0|c))break G;w:switch(s-110|0){case 0:case 5:if(s=g,2==i[f[144464+(n<<2)>>2]+11|0])break c;break;default:break w}s=o-1|0;break Q}if(115==(0|s)&&(s=g,2==i[f[144464+(n<<2)>>2]+11|0]))break c;s=o-1|0;break Q}G:{w:{if(115==(0|s)){if(s=g,8!=(0|(t=i[f[144464+(n<<2)>>2]+11|0])))break w;break c}if(8!=(0|t))break G;t=i[f[144464+(n<<2)>>2]+11|0]}if(s=g,2==(255&t))break c}s=o-1|0}f[b+536>>2]=s}if(524288&G&&(a[(b=o-1|0)+(o=I+208|0)|0]<=a[g+o|0]||(f[I+536>>2]=b,s=b)),i[(I+432|0)+s|0]>1){b=s;break o}if(b=2,g=s-1|0,s>>>0>=2)break B;f[I+536>>2]=s+1;break o}g=1}b=g,f[I+536>>2]=b}if(s=4,a[0|(g=(I+432|0)+b|0)]>=0)break g;if(a[(b=(I+432|0)+b|0)-1|0]>=4&a[b+1|0]>3)break g;a[0|g]=4;break g;case 2:if(f[I+536>>2])break g;for(g=(s=f[I+540>>2])-1|0,g&=g>>31;;){if((0|(s=s-1|0))<=0)break C;if(!(a[0|(b=(I+432|0)+s|0)]>=0))break}f[I+536>>2]=s,s=4,a[0|b]=4;break g;case 3:if(f[I+536>>2])break r;if(b=(0|(s=f[I+540>>2]-3|0))<=1?1:s,f[I+536>>2]=b,s=4,g)break g;a[b+(I+432|0)|0]=4;break g;case 4:if(f[I+536>>2])break g;if(b=(g=f[I+540>>2])-3|0,f[I+536>>2]=b,(0|g)<=15){o:{B:switch(i[f[144464+(E<<2)>>2]+11|0]-2|0){case 0:b=a[g+94176|0];break o;case 2:b=a[g+94192|0];break o;default:break B}b=a[g+94160|0]}f[I+536>>2]=b}s=4,a[(I+432|0)+b|0]=4;break g;case 5:if(f[I+536>>2])break g;if(o=-1,t=0,!((0|(g=(b=f[I+540>>2])-1|0))<2)){if(s=1,Q=1&b,3!=(0|b))for(w=(-2&b)-4|0,b=0;a[(I+432|0)+s|0]<0&&(t=(n=(0|(c=a[(I+320|0)+s|0]))<(0|o))?t:s,o=n?o:c),a[(n=s+1|0)+(I+432|0)|0]<0&&(t=(c=(0|(l=a[n+(I+320|0)|0]))<(0|o))?t:n,o=c?o:l),s=s+2|0,n=(0|b)!=(0|w),b=b+2|0,n;);!Q|a[(I+432|0)+s|0]>=0||(o=(b=(0|(n=a[(I+320|0)+s|0]))<(0|o))?o:n,t=b?t:s)}f[I+536>>2]=t,2!=i[g+(I+320|0)|0]|(0|o)>1?(0|o)>0||(t=1,f[I+536>>2]=1):(f[I+536>>2]=g,t=g),s=4,a[(I+432|0)+t|0]=4;break g;case 14:break a;case 12:break I;case 6:break k;default:break g}if(f[I+536>>2])break g;o=(g=f[I+540>>2])-1|0,f[I+536>>2]=o;k:if(!((0|g)<2))for(s=1;;){if(1==i[(I+432|0)+s|0]){o=s-1|0,f[I+536>>2]=o;break k}if((0|g)==(0|(s=s+1|0)))break}s=4,a[(I+432|0)+o|0]=4;break g}for(Q=-2&b,o=0,t=0;b=i[0|(w=(n=I+432|0)+s|0)],D=w,l=a[(w=I+208|0)+s|0]>0,a[0|D]=l||4==(0|b)?3:b,d=4==(0|(n=i[0|(D=(b=s+1|0)+n|0)]))?3:n,n=a[b+w|0]>0,a[0|D]=n?3:d,o=n?b:l?s:o,s=s+2|0,(0|Q)!=(0|(t=t+2|0)););}if(c&&(n=4==(0|(b=i[0|(t=(I+432|0)+s|0)]))?3:b,b=a[(I+208|0)+s|0]>0,a[0|t]=b?3:n,o=b?s:o),b=f[I+536>>2])break f;if((0|o)>0){f[I+536>>2]=o,b=o;break f}if((0|g)<6)break b;b=g-3|0;break i}if(b=f[I+536>>2])break f}b=g-1|0}f[I+536>>2]=b}s=4,a[(I+432|0)+b|0]=4;break g}if(f[I+536>>2])break g;b=1,f[I+536>>2]=1,i[I+209|0]|f[I+540>>2]<3|a[I+210|0]<=0||(b=2,f[I+536>>2]=2),s=4,a[I+432|b]=4;break g}if(f[I+536>>2])break r;if((0|(b=f[I+540>>2]))<3)break r;if(ue(I+432|1,0,b-1|0),f[I+536>>2]=2,g||(a[I+434|0]=4),s=4,b>>>0<4)break g;a[431+(b+I|0)|0]=3;break g}f[I+536>>2]=g,s=4;break g}s=g}!(256&G)|2&C||(0|(g=f[I+540>>2]))<3|(0|B)>2||4!=i[0|(b=(g=g+(I+432|0)|0)-1|0)]|2!=i[f[144464+(E<<2)>>2]+11|0]||(a[0|b]=1,a[g-2|0]=4);g:{r:{if(x)B=f[I+540>>2];else{if(g=a[I+433|0],!(!(4096&G)|3!=(0|(B=f[I+540>>2])))){if(4==(0|g)){a[I+434|0]=3;break r}if(4==i[I+434|0]){a[I+433|0]=3;break r}}if(!(!(8192&G)|(0|g)>=0|(0|B)<4|a[I+434|0]<4)){a[I+433|0]=3;break r}}if(t=0,(0|B)<2)break g}for(o=(0|s)<4?4:3,w=128&G,l=64&G,D=32&G,c=B-1|0,d=16&G,m=!(32768&G),E=0,n=0,s=1;;){r:{C:if((0|(b=a[0|(Q=(I+432|0)+s|0)]))>=0)t=o;else{t=3;a:{I:if(!(!(!d|(0|o)>3)&(0|s)==(0|c))){if(!(1&(E|m)))break a;if(!(a[431+(I+s|0)|0]>1)){if((0|(b=a[(g=s+1|0)+(I+432|0)|0]))>=2){if(4!=(0|o))break I;if(t=4,b>>>0>=3)break I}else if(!(!D|3!=(0|o))){o=3;break r}if(!l|s>>>0<2)break a;if(t=i[(I+320|0)+s|0])break a;if((0|c)>(0|(b=s))){for(;;){if(a[(I+320|0)+b|0]>0)break r;if((0|c)==(0|(b=b+1|0)))break}if(t)break a}if(a[g+(I+320|0)|0]<=0)break a;break r}t=o}b=i[0|Q];break C}a[0|Q]=o,E=1,t=3,b=o}C:{if(b<<24>>24>=4){if(g=n||s,!n|!w)break C;a[0|Q]=3}o=t;break r}o=t,n=g}if(t=1,(0|B)==(0|(s=s+1|0)))break}}if(r=!x|(0|r)>=0?r:f[((0|B)<3?16:20)+A>>2],o=0,b=0,t){if(E=3&(g=B-1|0),t=0,B-2>>>0<3)s=1;else for(x=-4&g,s=1,n=0;b=(g=(0|(b=(Q=(0|(b=(c=(0|(b=(B=(0|(c=a[(g=I+432|0)+s|0]))<(0|b))?b:c))>(0|(Q=a[(w=s+1|0)+g|0])))?b:Q))>(0|(D=a[(l=s+2|0)+g|0])))?b:D))>(0|(d=a[(D=s+3|0)+g|0])))?b:d,o=g?Q?c?B?o:s:w:l:D,s=s+4|0,(0|x)!=(0|(n=n+4|0)););if(E)for(;b=(g=(0|(n=a[(I+432|0)+s|0]))<(0|b))?b:n,o=g?o:s,s=s+1|0,(0|E)!=(0|(t=t+1|0)););}if((0|r)<0?r=b:(0|r)<=(0|b)&(0|b)>4||(a[(I+432|0)+o|0]=r),E=e+197|0,B=1,!(1&C)&&(g=f[144464+(u<<2)>>2])){if(b=I,!(1!=(0|(t=i[g+11|0]))&15!=(0|u)))for(;g=i[0|(b=b+1|0)],1==(0|(t=i[f[144464+(g<<2)>>2]+11|0]))|15==(0|g););!(48&(g=f[A+4>>2]))|2!=(0|t)||(a[0|e]=(32&g)>>>5|0&&a[I+433|0]>3?11:23,e=e+1|0)}g:if(!(e>>>0>=E>>>0))for(c=65536&G,Q=2&G,x=4&G,s=I;;){if(!(b=i[0|s]))break g;if(C=s,s=s+1|0,g=f[144464+(b<<2)>>2]){r:{C:{a:switch(i[g+11|0]){case 0:f[A+8200>>2]=0;break r;case 2:if(!(16&i[g+6|0]))break C;break;default:break a}if(20!=i[0|s])break r}if((0|(u=f[I+540>>2]))<(0|B))break A;n=a[0|(G=(I+432|0)+B|0)],f[A+8200>>2]=n;C:{a:{if(!((0|(g=n))>1)){if(t=u-1|0,!(!x|(0|B)<2|(0|r)<2)&&(g=0,(0|t)==(0|B)))break a;if(g=1,!(1==(0|B)|Q|(u-2|0)==(0|B)&a[t+(I+432|0)|0]<2|(0|t)==(0|B)||a[431+(I+B|0)|0]>=0&&(g=n,c))){g=0,a[0|G]=0;break a}}if(g&&(0|g)<2)break C}a[0|e]=i[g+94151|0],e=e+1|0,n=a[0|G]}t=(0|r)>(0|n),12==i[0|s]&&1&(u=f[A+28>>2])&&(s=(16&u?(0|o)!=(0|B):(0|g)<4)?C+2|0:s),r=t?r:n,B=B+1|0}if(1!=(0|b)&&(a[0|e]=b,e=e+1|0),!(e>>>0>>0))break}}a[0|e]=0}return void(V=I+544|0)}p(86136,86634,1353,94208),k()}function iA(A,e,g,r,C,a,I,i,b){var s,t=0,n=0,k=0,o=0,B=0,Q=0,G=0,w=0,E=0,u=0,l=0,x=0,d=0,m=0,M=0,v=0,h=0,p=0,Y=0,H=0,N=0,P=0,F=0,y=0,z=0,O=0,Z=0,K=0,W=0,X=0;V=s=V-336|0,o=i,Q=65535&b,B=r,k=65535&C,l=-2147483648&(C^b);A:{if(!((E=b>>>16&32767)-32767>>>0>4294934529&(G=C>>>16&32767)-32767>>>0>=4294934530)){if(!(!r&2147418112==(0|(t=2147483647&C))?!(e|g):t>>>0<2147418112)){w=r,l=32768|C;break A}if(!(!i&2147418112==(0|(C=2147483647&b))?!(a|I):C>>>0<2147418112)){w=i,l=32768|b,e=a,g=I;break A}if(!(e|r|2147418112^t|g)){if(!(a|i|2147418112^C|I)){e=0,g=0,l=2147450880;break A}l|=2147418112,e=0,g=0;break A}if(!(a|i|2147418112^C|I)){e=0,g=0;break A}if(!(e|r|g|t)){w=(e=!(a|i|C|I))?0:w,l=e?2147450880:l,e=0,g=0;break A}if(!(a|i|C|I)){l|=2147418112,e=0,g=0;break A}65535==(0|t)|t>>>0<65535&&(b=r=!(k|B),t=r?e:B,i=r<<=6,Ve(s+320|0,e,g,B,k,(r=r+(32==(0|(b=D(b?g:k)))?D(t)+32|0:b)|0)-15|0),x=16-r|0,B=f[s+328>>2],k=f[s+332>>2],g=f[s+324>>2],e=f[s+320>>2]),C>>>0>65535||(i=r=!(o|Q),b=r?a:o,C=r<<=6,Ve(s+304|0,a,I,o,Q,(r=r+(32==(0|(i=D(i?I:Q)))?D(b)+32|0:i)|0)-15|0),x=(r+x|0)-16|0,o=f[s+312>>2],Q=f[s+316>>2],a=f[s+304>>2],I=f[s+308>>2])}if(h=C=65536|Q,p=o,t=C<<15|(r=o)>>>17,gg(s+288|0,r=C=r<<15|I>>>17,i=t,0,0,b=0-r|0,t=1963258675-(t+!!(0|r)|0)|0,0,0),gg(s+272|0,0-(r=f[s+296>>2])|0,0-(f[s+300>>2]+!!(0|r)|0)|0,0,0,b,t,0,0),gg(s+256|0,b=(r=f[s+280>>2])<<1|f[s+276>>2]>>>31,r=f[s+284>>2]<<1|r>>>31,0,0,C,i,0,0),gg(s+240|0,b,r,0,0,0-(t=f[s+264>>2])|0,0-(f[s+268>>2]+!!(0|t)|0)|0,0,0),gg(s+224|0,t=(b=f[s+248>>2])<<1|f[s+244>>2]>>>31,r=f[s+252>>2]<<1|b>>>31,0,0,C,i,0,0),gg(s+208|0,t,r,0,0,0-(b=f[s+232>>2])|0,0-(f[s+236>>2]+!!(0|b)|0)|0,0,0),gg(s+192|0,b=(r=f[s+216>>2])<<1|f[s+212>>2]>>>31,r=f[s+220>>2]<<1|r>>>31,0,0,C,i,0,0),gg(s+176|0,b,r,0,0,0-(t=f[s+200>>2])|0,0-(f[s+204>>2]+!!(0|t)|0)|0,0,0),gg(s+160|0,b=C,r=i,0,0,i=(o=(C=f[s+184>>2])<<1|f[s+180>>2]>>>31)-1|0,C=(f[s+188>>2]<<1|C>>>31)-!o|0,0,0),gg(s+144|0,a<<15,I<<15|a>>>17,0,0,r=i,C,0,0),u=s+112|0,M=f[s+168>>2],i=f[s+172>>2],n=(o=f[s+160>>2])+(b=f[s+152>>2])|0,t=(Q=f[s+164>>2])+f[s+156>>2]|0,b=t=b>>>0>n>>>0?t+1|0:t,t=(t=(0|Q)==(0|t)&n>>>0>>0|t>>>0>>0)>>>0>(Q=t+M|0)>>>0?i+1|0:i,gg(u,r,C,0,0,0-(i=(o=!b&n>>>0>1|!!(0|b))+Q|0)|0,0-(!!(0|i)+(t=o>>>0>i>>>0?t+1|0:t)|0)|0,0,0),gg(s+128|0,1-n|0,0-((n>>>0>1)+b|0)|0,0,0,r,C,0,0),y=(G-E|0)+x|0,u=C=f[s+116>>2],o=(r=f[s+112>>2])<<1,G=t=C<<1|r>>>31,r=t,v=i=f[s+140>>2],r=r+(t=i<<1|(C=f[s+136>>2])>>>31)|0,C=r=(i=(b=C<<1|f[s+132>>2]>>>31)+o|0)>>>0>>0?r+1|0:r,m=r=r-(i>>>0<13927)|0,M=r,z=t=65536|k,O=B,K=(r=B)<<1,W=t=t<<1|r>>>31,H=t,N=r=Cr(m,n=0,t,0),d=t=U,x=e<<1,E=r=g<<1|e>>>31,Q=t=0,m=(0|C)==(0|m)&(b=i-13927|0)>>>0>>0|C>>>0>m>>>0,C=(0|C)==(0|G)&i>>>0>>0|C>>>0>>0,r=f[s+120>>2],t=i=f[s+124>>2]<<1|r>>>31,t=(r=(n=v>>>31|0)+(r=r<<1|u>>>31)|0)>>>0>>0?t+1|0:t,n=(i=r)>>>0>(r=r+C|0)>>>0?t+1|0:t,n=(C=r)>>>0>(r=r+m|0)>>>0?n+1|0:n,C=r-1|0,t=Cr(E,Q,m=n-!r|0,G=0),i=U+d|0,u=(0|d)==(0|(i=(r=t+N|0)>>>0>>0?i+1|0:i))&r>>>0>>0|i>>>0>>0,v=C,C=Cr(C,t=0,Y=(Z=g>>>31|0)|B<<1,d=0),t=U+i|0,n=0,o=t=C>>>0>(B=C+r|0)>>>0?t+1|0:t,n=(C=r=(0|t)==(0|i)&r>>>0>B>>>0|i>>>0>t>>>0)>>>0>(r=r+u|0)>>>0?1:n,C=Cr(H,Q,m,G),t=U+n|0,u=r=C+r|0,r=r>>>0>>0?t+1|0:t,C=Cr(H,Q,v,d),k=U,i=C,C=Cr(Y,d,m,G),t=U+k|0,C=t=C>>>0>(n=i+C|0)>>>0?t+1|0:t,r=r+(t=(0|k)==(0|t)&i>>>0>n>>>0|t>>>0>>0)|0,u=k=u+C|0,k=r=k>>>0>>0?r+1|0:r,t=n+o|0,C=t=(r=(C=0)+B|0)>>>0>>0?t+1|0:t,i=(0|t)==(0|o)&r>>>0>>0|t>>>0>>0,t=k,n=i,P=i=i+u|0,n=t=n>>>0>i>>>0?t+1|0:t,u=r,k=r,B=C,N=b,r=Cr(b,0,Y,d),i=U,C=r,b=Cr(M,w,E,w),t=U+i|0,b=(0|i)==(0|(t=(r=r+b|0)>>>0>>0?t+1|0:t))&r>>>0>>0|i>>>0>t>>>0,i=t,C=Cr(v,d,F=-2&x,0),t=U+t|0,C=t=C>>>0>(o=C+r|0)>>>0?t+1|0:t,r=(0|t)==(0|i)&r>>>0>o>>>0|i>>>0>t>>>0,i=0,r=((b=r+b|0)>>>0>>0?1:i)+B|0,t=n,b=r=(k=b+k|0)>>>0>>0?r+1|0:r,i=r=(0|r)==(0|B)&k>>>0>>0|r>>>0>>0,X=r=r+P|0,u=t=i>>>0>r>>>0?t+1|0:t,r=Cr(H,Q,N,w),P=U,H=r,i=Cr(m,G,F,w),t=U+P|0,B=r=r+i|0,n=r+(Q=Cr(M,w,Y,d))|0,r=(i=r>>>0>>0?t+1|0:t)+U|0,r=n>>>0>>0?r+1|0:r,G=n,Q=n+(t=Cr(E,w,v,d))|0,n=U+r|0,Y=(0|r)==(0|(n=t>>>0>Q>>>0?n+1|0:n))&Q>>>0>>0|r>>>0>n>>>0,t=((r=(r=(r=(0|r)==(0|i)&B>>>0>G>>>0|r>>>0>>0)+(t=(0|i)==(0|P)&B>>>0>>0|i>>>0

>>0)|0)+Y|0)|(v=0))+b|0,B=t=(i=n)>>>0>(G=i+k|0)>>>0?t+1|0:t,r=(0|b)==(0|t)&k>>>0>G>>>0|b>>>0>t>>>0,t=u,i=r,v=r=r+X|0,b=t=i>>>0>r>>>0?t+1|0:t,r=Cr(M,w,F,w),M=U,k=r,i=Cr(E,w,N,w),t=U+M|0,i=(0|(t=(r=r+i|0)>>>0>>0?t+1|0:t))==(0|M)&r>>>0>>0|t>>>0>>0,k=t,r=t+o|0,t=(i|(u=0))+C|0,k=(0|C)==(0|(t=r>>>0>>0?t+1|0:t))&r>>>0>>0|C>>>0>t>>>0,n=(i=t)+(t=Q)|0,t=0,t=((C=r=(0|i)==(0|(n=(o=(Q=0)+r|0)>>>0>>0?n+1|0:n))&r>>>0>o>>>0|i>>>0>n>>>0)>>>0>(r=r+k|0)>>>0?1:t)+B|0,n=b,C=t=(C=r)>>>0>(r=r+G|0)>>>0?t+1|0:t,b=n=(b=i=(0|B)==(0|t)&r>>>0>>0|t>>>0>>0)>>>0>(i=i+v|0)>>>0?n+1|0:n,131071==(0|n)|n>>>0<131071?(O=K|Z,z=d|W,gg(s+80|0,r,C,i,b,a,I,p,h),Q=n=f[s+84>>2],t=e<<17,B=(g=(o=0)-(k=f[s+88>>2])|0)-(n=!!(n|(e=f[s+80>>2])))|0,k=(t-(f[s+92>>2]+(k>>>0>o>>>0)|0)|0)-(g>>>0>>0)|0,o=0-e|0,Q=0-(!!(0|e)+Q|0)|0,e=y+16382|0):(gg(s+96|0,r=(1&C)<<31|r>>>1,C=i<<31|C>>>1,i=(1&b)<<31|i>>>1,b=b>>>1|0,a,I,p,h),E=B=f[s+100>>2],B=(o=0-(x=f[s+104>>2])|0)-(k=!!(B|(n=f[s+96>>2])))|0,k=((e<<16)-(f[s+108>>2]+(Q>>>0>>0)|0)|0)-(k>>>0>o>>>0)|0,o=0-n|0,Q=0-(!!(0|n)+E|0)|0,x=e,E=g,e=y+16383|0),(0|e)>=32767)l|=2147418112,e=0,g=0;else{if((0|e)>0)n=k<<1|B>>>31,B=B<<1|Q>>>31,k=n,x=i,E=65535&b|e<<16,n=Q<<1|o>>>31,b=o<<1;else{if((0|e)<=-113){e=0,g=0;break A}Ke(s- -64|0,r,C,i,b,1-e|0),Ve(s+48|0,x,E,O,z,e+112|0),gg(s+32|0,a,I,p,h,r=f[s+64>>2],C=f[s+68>>2],x=f[s+72>>2],E=f[s+76>>2]),e=f[s+40>>2],o=(g=f[s+56>>2])-(B=e<<1|(n=f[s+36>>2])>>>31)|0,k=f[s+60>>2]-((f[s+44>>2]<<1|e>>>31)+(g>>>0>>0)|0)|0,t=(e=f[s+32>>2])<<1,B=o-(e=(0|(i=n<<1|e>>>31))==(0|(b=f[s+52>>2]))&t>>>0>(g=f[s+48>>2])>>>0|i>>>0>b>>>0)|0,k=k-(e>>>0>o>>>0)|0,n=b-((g>>>0>>0)+i|0)|0,b=g-t|0}e=b,gg(s+16|0,a,I,p,h,3,0,0,0),gg(s,a,I,p,h,5,0,0,0),i=n+(g=0)|0,i=b>>>0>(e=e+(t=1&r)|0)>>>0?i+1|0:i,b=e,I=(0|I)==(0|i)&e>>>0>a>>>0|I>>>0>>0,n=k,e=(0|g)==(0|i)&e>>>0>>0|g>>>0>i>>>0,t=C,g=t=(g=e=(e=(0|(n=e>>>0>(a=e+B|0)>>>0?n+1|0:n))==(0|h))&(0|a)==(0|p)?I:e&a>>>0>p>>>0|n>>>0>h>>>0)>>>0>(e=e+r|0)>>>0?t+1|0:t,r=(0|C)==(0|t)&e>>>0>>0|C>>>0>t>>>0,t=E,t=(C=r)>>>0>(r=r+x|0)>>>0?t+1|0:t,I=r,k=(0|(C=f[s+20>>2]))==(0|i)&c[s+16>>2]>>0|C>>>0>>0,C=f[s+28>>2],C=t>>>0<2147418112&((0|(r=f[s+24>>2]))==(0|a)&(0|C)==(0|n)?k:(0|C)==(0|n)&r>>>0>>0|C>>>0>>0),r=g,t=(g=e=(0|g)==(0|(r=(k=C)>>>0>(C=e+C|0)>>>0?r+1|0:r))&e>>>0>C>>>0|g>>>0>r>>>0)>>>0>(e=e+I|0)>>>0?t+1|0:t,I=e,i=(0|(g=f[s+4>>2]))==(0|i)&c[s>>2]>>0|g>>>0>>0,g=f[s+12>>2],g=n=(g=e=t>>>0<2147418112&((0|(e=f[s+8>>2]))==(0|a)&(0|g)==(0|n)?i:(0|g)==(0|n)&e>>>0>>0|g>>>0>>0))>>>0>(e=e+C|0)>>>0?r+1|0:r,C=(0|r)==(0|n)&e>>>0>>0|r>>>0>n>>>0,r=t,a=C,w|=C=C+I|0,l|=r=a>>>0>C>>>0?r+1|0:r}}f[A>>2]=e,f[A+4>>2]=g,f[A+8>>2]=w,f[A+12>>2]=l,V=s+336|0}function bA(A,e,g,r,C){var I,b,s,t=0,n=0,k=0,o=0,c=0,Q=0,G=0,w=0,E=0,D=0,u=0,l=0,x=0,d=0,m=0,M=0,v=0,h=0,p=0,Y=0,H=0;if(V=b=V-80|0,s=f[g+8>>2],!C|!B[g+4>>1]||(f[C+8>>2]=0),I=ue(r,0,152),f[I+44>>2]=i[s+14|0],f[I+40>>2]=i[s+15|0],r=B[s+8>>1]){for(r=f[34459]+(r<<1)|0,D=256&e,u=g+32|0,d=g-32|0,m=g- -64|0,M=g+96|0,v=g+-64|0,h=g-28|0,p=1&e,Y=g-24|0,l=f[30450];;){t=15&(c=(e=B[r>>1])>>>8|0);A:{e:{g:{r:{C:{a:{I:{f:{i:{b:{s:{t:switch(0|(k=e>>>12|0)){case 10:break I;case 9:break f;case 6:break i;case 2:case 3:break b;case 1:break s;case 0:break t;case 11:case 12:case 13:case 14:case 15:break a;default:break C}n=255&e;t:{n:{k:switch(0|t){case 13:if(n)break n;t=r,e=0;break t;case 0:t=r;o:switch(n-1|0){case 1:break A;case 0:break e;default:break o}f[b+20>>2]=Ur(b+75|0,f[s>>2]),f[b+16>>2]=e,eC(l,85851,b+16|0);break A;case 5:if(2!=i[f[144464+(i[g+34|0]<<2)>>2]+11|0])break A;f[I+20>>2]=n;break A;case 12:break k;default:break r}f[I+44>>2]=f[I+44>>2]+(e<<24>>31&-256|n);break A}a[I+132|0]=i[r+3|0],t=r+2|0,a[I+133|0]=i[0|t],e=2,n>>>0<3||(a[I+134|0]=i[r+5|0],t=r+4|0,a[I+135|0]=i[0|t],e=4,n>>>0<5||(a[I+136|0]=i[r+7|0],t=r+6|0,a[I+137|0]=i[0|t],e=6,n>>>0<7||(a[I+138|0]=i[r+9|0],t=r+8|0,a[I+139|0]=i[0|t],e=8,n>>>0<9||(a[I+140|0]=i[r+11|0],t=r+10|0,a[I+141|0]=i[0|t],e=10,n>>>0<11||(a[I+142|0]=i[r+13|0],t=r+12|0,a[I+143|0]=i[0|t],e=12,n>>>0<13||(a[I+144|0]=i[r+15|0],t=r+14|0,a[I+145|0]=i[0|t],e=14,n>>>0<15||(a[I+146|0]=i[r+17|0],t=r+16|0,a[I+147|0]=i[0|t],e=16)))))))}a[132+(e+I|0)|0]=0,n=G;break e}if(!A|t>>>0>7)break A;if(k=g,2!=i[f[144464+(i[g+2|0]<<2)>>2]+11|0]&&(k=u,2!=i[f[144464+(i[g+34|0]<<2)>>2]+11|0]))break A;if(!(1&(t=f[A+56>>2]))&&16&i[0|g])break A;n=15&i[k+3|0],n=2&t&&i[k+6|0]<=n>>>0?4:n;s:{t:{n:{k:switch((t=7&c)-3|0){case 1:break t;case 0:break k;default:break n}if(n>>>0>3)break s;break A}if(f[102832+(t<<2)>>2]>(0|n))break s;break A}if(i[k+6|0]>n>>>0)break A}f[I+8>>2]=255&e,n=1;break g}if(8192==(57344&e)){for(H=f[32972],c=1,x=0;;){o=255&e,t=(Q=4095&e)>>>8|0;b:if(Q>>>0<=3583){6==(0|(n=(t>>>0)%7|0))&&(n=B[r+2>>1]),E=0,t=g;s:{t:{n:switch(0|n){case 6:if(k=0,B[g+36>>1]|B[g+68>>1])break b;case 3:t=m;break t;case 9:if(k=0,B[g+36>>1]|B[g+68>>1])break b;if(t=M,!B[g+100>>1])break s;break b;case 7:if(k=0,B[g+36>>1])break b;for(n=1;;){if(2==i[f[144464+(i[(t=(n<<5)+g|0)+2|0]<<2)>>2]+11|0])break t;if(B[4+(((n=n+1|0)<<5)+g|0)>>1])break}break b;case 5:if(k=0,B[g+4>>1])break b;case 0:E=1,t=d;break t;case 4:if(k=0,B[g+36>>1])break b;case 2:t=u;break t;case 8:if(k=0,!C)break b;if(E=1,f[(t=C)+8>>2])break s;break b;case 10:break n;default:break t}if(k=0,B[g+4>>1]|B[h>>1])break b;E=1,t=v;break s}t:switch(0|n){case 0:case 5:break t;default:break s}t=(1==i[t+2|0]?-32:0)+t|0}if(D?(n=f[144464+(i[t+2|0]<<2)>>2],f[t+8>>2]=n):n=f[t+8>>2],Q>>>0<=1791){if(k=1,f[f[144464+(o<<2)>>2]>>2]==f[n>>2])break b;if(!(!E|2!=i[n+11|0])){k=(0|o)==i[n+13|0];break b}k=(0|o)==i[n+12|0];break b}o=31&Q,k=0;s:switch(Q>>>5&7){case 0:k=(0|o)==i[n+11|0];break b;case 1:k=(0|o)==(15&B[n+6>>1]);break b;case 2:k=f[n+4>>2]>>>o&1;break b;case 4:break s;default:break b}s:switch(0|o){case 0:case 1:case 2:case 3:case 4:if(2!=i[f[144464+(i[t+2|0]<<2)>>2]+11|0]){if(2!=i[f[144464+(i[t+34|0]<<2)>>2]+11|0])break b;t=t+32|0}n=15&i[t+3|0],n=!A|!(2&i[A+56|0])?n:i[t+6|0]<=n>>>0?4:n;t:{n:switch(o-3|0){case 1:k=i[t+6|0]<=n>>>0;break b;case 0:if(k=1,n>>>0<=3)break t;break b;default:break n}if(k=1,f[102832+(o<<2)>>2]>(0|n))break b}k=0;break b;case 17:if(!i[n+11|0]){k=1;break b}k=(32&i[g+1|0])>>>5|0;break b;case 18:k=0!=B[t+4>>1];break b;case 19:if(k=1,B[t+36>>1])break b;k=!i[f[t+40>>2]+11|0];break b;case 9:if(B[t+4>>1])break b;for(;;){if(k=!!(0|(n=12&i[t-29|0])),n)break b;if(B[(t=t-32|0)+4>>1])break}break b;case 10:k=2!=i[n+11|0];break b;case 11:for(;;){if(k=!!(0|(n=B[t+36>>1])),n)break b;if(n=t,t=t+32|0,2==i[f[n+40>>2]+11|0])break}break b;case 12:if(k=1,2==(254&i[n+11|0]))break b;k=(16&i[n+4|0])>>>4|0;break b;case 13:for(;k=(2==i[f[t+8>>2]+11|0])+k|0,n=B[t+4>>1],t=t-32|0,!n;);k=1==(0|k);break b;case 14:for(;k=(2==i[f[t+8>>2]+11|0])+k|0,n=B[t+4>>1],t=t-32|0,!n;);k=2==(0|k);break b;case 16:break s;default:break b}k=(16&i[0|t])>>>4|0}else if(k=0,15==(0|t)){s:switch(o-1|0){case 0:k=p;break b;case 1:break s;default:break b}k=0!=f[H+132>>2]}b:if(1970>>>(t=(n=65535&e)>>>12|0)&1)t=a[t+102848|0];else{s:switch(0|t){case 0:if(t=1,3328!=(3840&n))break b;t=1+(1+(255&n)>>>1|0)|0;break b;case 6:t=(n>>>9&7)-5>>>0<2?12:1;break b;case 2:case 3:t=3328==(0|(t=3840&n))||1536==(0|t)?2:1;break b;default:break s}t=4,(n=B[r+4>>1])>>>0>61439||(t=2==(0|n)?3:2)}if(r=((t=3==B[(r=(t<<1)+r|0)>>1])<<1)+r|0,t^=k,c=x?t|c:t&c,x=4096&e,8192!=(57344&(e=B[r>>1])))break}if(!(1&c))if(26624!=(63488&e)){b:if(1970>>>(t=e>>>12|0)&1)t=a[t+102848|0];else{s:switch(0|t){case 0:if(t=1,3328!=(3840&e))break b;t=1+(1+(255&e)>>>1|0)|0;break b;case 6:t=(e>>>9&7)-5>>>0<2?12:1;break b;case 2:case 3:t=3328==(0|(e&=3840))||1536==(0|e)?2:1;break b;default:break s}t=4,(e=B[r+4>>1])>>>0>61439||(t=2==(0|e)?3:2)}r=((24576==(65024&B[(e=(t<<1)+r|0)>>1]))<<1)+e|0}else r=((255&e)<<1)+r|0}t=r-2|0,n=G;break e}i:switch(t>>>1|0){case 0:r=(((255&e)<<1)+r|0)-2|0;break A;case 5:f[I>>2]=2|f[I>>2],((e=i[f[g+40>>2]+12|0])-28&255)>>>0<=5&&(t=B[(e=((e<<2)+r|0)-112|0)+4>>1],e=B[e+2>>1],f[I+96>>2]=e>>>4<<24>>24,f[I+76>>2]=(15&e)<<18|t<<2),r=r+24|0;break A;case 6:break i;default:break A}((e=i[f[Y>>2]+13|0])-28&255)>>>0<=5&&(t=B[(e=((e<<2)+r|0)-112|0)+4>>1],e=B[e+2>>1],f[I+100>>2]=e>>>4<<24>>24,f[I+80>>2]=(15&e)<<18|t<<2),r=r+24|0;break A}e=B[(r=r+2|0)>>1]|e<<16&983040;f:switch(t-1|0){case 0:if((0|w)>9)break A;f[(b+32|0)+(w<<2)>>2]=r,r=(f[34459]+(e<<1)|0)-2|0,w=w+1|0;break A;case 1:f[I+124>>2]=e;break A;case 2:break f;default:break A}f[I+128>>2]=e;break A}f[(t=((1!=(0|t))<<3)+I|0)+108>>2]=B[r+2>>1]|(255&e)<<16,e=B[r+4>>1]<<16,r=r+6|0,f[t+112>>2]=e|B[r>>1];break A}if(n=B[(t=r+2|0)>>1],Q=e>>>4|0,f[(c=((o=k-11|0)<<2)+I|0)+88>>2]=255&Q,f[c+68>>2]=e<<18&3932160|n<<2,2==(0|(r=B[r+4>>1]))){r=t;break A}if(n=e>>>0<=53247?r>>>0>61439?2:1:G-(4==(0|o))|0,k-13>>>0>1)break e;f[c+88>>2]=Q<<24>>24;break e}f[b+4>>2]=Ur(b+75|0,f[s>>2]),f[b>>2]=e,eC(l,85851,b);break A}f[4+((t<<2)+I|0)>>2]=n,n=D&&1==(0|t)?1:G}t=r}1!=(0|n)|(0|w)<=0?(r=t,G=n):(r=f[(b+32|0)+((w=w-1|0)<<2)>>2],G=0)}if(r=r+2|0,1==(0|G))break}!C|2!=i[g+17|0]||(A=f[g+4>>2],f[C>>2]=f[g>>2],f[C+4>>2]=A,A=f[g+28>>2],f[C+24>>2]=f[g+24>>2],f[C+28>>2]=A,A=f[g+20>>2],f[C+16>>2]=f[g+16>>2],f[C+20>>2]=A,A=f[g+12>>2],f[C+8>>2]=f[g+8>>2],f[C+12>>2]=A),a[g+23|0]=f[I+44>>2],(A=f[I+68>>2])?(f[g+24>>2]=A,A=I+88|0):(f[g+24>>2]=f[I+72>>2],A=I+92|0),f[g+28>>2]=f[A>>2]}V=b+80|0}function sA(A,e,g,r,C,I){var b,s=0,t=0,n=0,k=0,o=0,B=0,c=0,Q=0,w=0,E=0,D=0,u=0,l=0,x=0,d=0,m=0,M=0,v=0;V=b=V-848|0;A:if(!(!f[A+104>>2]|8&i[C+2|0]|193==f[47202])){for(f[r>>2]=0,f[33272]=0,f[33274]=I,a[b+192|0]=0,f[33273]=b+192;k=(I=k)+1|0,a[0|(u=e+I|0)]-48>>>0<10;);f[56798]=0,m=b+188|0,B=-2147483648,V=D=V-16|0;e:if(s=i[0|e]){o=e;g:{for(;;){if(!(32==(0|(s=s<<24>>24))|s-9>>>0<5))break g;if(s=i[o+1|0],o=o+1|0,!s)break}break e}g:switch((s=i[0|o])-43|0){case 0:case 2:break g;default:break e}w=45==(0|s)?-1:0,o=o+1|0}else o=e;for(;s=-48,(((c=a[0|o])-48&255)>>>0<10||(s=-87,(c-97&255)>>>0<26||(s=-55,!((c-65&255)>>>0>25))))&&!((0|(c=s+c|0))>=10);)gg(D,10,0,0,0,Q,E,0,0),s=1,f[D+8>>2]|f[D+12>>2]||(x=Cr(Q,E,10,0),-1==(0|(d=U))&~c>>>0>>0||(s=d,E=(Q=c+x|0)>>>0>>0?s+1|0:s,l=1,s=n)),o=o+1|0,n=s;m&&(f[m>>2]=l?o:e);e:{g:{if(n)f[56798]=68,Q=-2147483648,E=0;else if(!E&Q>>>0<2147483648)break g;if(!w){f[56798]=68,B=2147483647;break e}if(!(!E&Q>>>0<=2147483648)){f[56798]=68;break e}}B=(w^Q)-w|0}if(V=D+16|0,w=B,!(f[56798]|f[b+188>>2]==(0|e))){e:{g:{r:{if(!(!((0|(c=64&i[A+109|0]?4:3))!=(0|I)|f[A+124>>2]!=a[e-2|0])&a[e-3|0]-48>>>0<10)){C:{if(32!=f[A+124>>2]){if(!(16&i[A+105|0]))break r;if(3==(0|I))break C;break r}if(3!=(0|I))break r}if(4&i[C+2|0]|a[e-2|0]-48>>>0>=10)break r}a[133104]=0,a[b+288|0]=0,M=1;break g}if(a[133104]=0,f[33275]=0,a[b+288|0]=0,v=1,l=0,48==i[0|e])break e}l=me(A,e,u,C,0)}46!=i[0|u]|a[e+k|0]-48>>>0<10|1&a[C+13|0]|a[u+2|0]-48>>>0<10||(a[0|u]=0);e:if(!l||(x=1,26741==f[A+212>>2])){if(B=b+256|0,f[b+844>>2]=B,t=k,64&i[C+1|0]&&(a[b+256|0]=45,B=b+256|1,f[b+844>>2]=B,t=I+2|0),o=i[e+t|0]){for(;!(32==(255&o)|(0|t)>28)&&(s=f[b+844>>2],f[b+844>>2]=s+1,a[0|s]=o,o=i[(t=t+1|0)+e|0]););B=f[b+844>>2]}if(a[0|B]=0,x=1,s=a[b+256|0]){if(!(n=f[A+136>>2])||Qr(b+256|0,n)){if(s-48>>>0<10)break e;if(f[b+176>>2]=b+256,dg(s=b+800|0,88653,b+176|0),!Mg(A,s,133104))break e;f[r>>2]=128|f[r>>2],f[b+160>>2]=b+256,dg(s=b+800|0,88773,b+160|0),Mg(A,s,133116),x=0}l=2}}m=f[C>>2],a[b+352|0]=0,a[b+624|0]=0;e:{g:{r:if(!(!v|48!=i[0|e]||32==(0|(s=a[e+1|0]))|(0|s)==f[A+128>>2])){C:{if(2==(0|I)){if(58!=i[e+3|0]|a[e+5|0]-48>>>0>=10)break C;if(!(32==(0|(s=a[e+7|0]))|s-9>>>0<5))break C;break r}if((0|I)>3)break g}if(48==i[0|e]&&!((0|(s=I-1|0))<=0))for(t=0;;){if(Mg(A,88875,Lg(n=b+288|0)+n|0),48!=i[(t=t+1|0)+e|0])break r;if(!((0|s)>(0|t)))break}}r:{C:{if(!(32==(0|(s=a[0|u]))&&16&i[A+105|0])){if(d=2,Q=I+2|0,(0|s)==f[A+124>>2])break C;E=1,B=0,s=0;break r}d=1,Q=I+2|0}if(4&i[C+14|0])B=1,s=0,E=1;else for(s=0,o=1,t=I,E=1;;){n=s,s=o,o=(D=t+d|0)+e|0,t=0;C:{for(;;){if(B=1,!(a[t+o|0]-48>>>0>=10)){if((0|c)!=(0|(t=t+1|0)))continue;break C}break}s=n;break r}if(a[o+c|0]-48>>>0<10){s=n;break r}if(t=0,a[o-1|0]-48>>>0<10){s=n;break r}C:{for(;;){if(48==i[(t+D|0)+e|0]){if((0|c)!=(0|(t=t+1|0)))continue;break C}break}E=0}if((0|(n=a[(t=c+D|0)+e|0]))!=f[A+124>>2]&(!(16&i[A+105|0])|32!=(0|n)))break r;if(Q=t+2|0,4&i[2+(G(o=s+1|0,12)+C|0)|0])break}}t=!w;r:if(!(!E|!(64&i[1+(G(s,12)+C|0)|0])|26741!=f[A+212>>2])){C:switch(i[0|(n=e+Q|0)]-97|0){case 0:case 4:break C;default:break r}C:{a:{I:{f:switch((o=i[n+1|0])-116|0){case 6:break r;case 1:case 2:case 3:case 4:case 5:break a;case 0:break f;default:break I}if(116!=i[n+2|0])break C;break r}if(32==(0|o))break r}if(!(!!((0|w)%1e3|0)&1!=(0|s))&&108==(0|o))break r}f[33274]=1|f[33274]}Q=32768&m,t&=M;r:if(f[A+128>>2]!=a[0|u]|a[e+k|0]-48>>>0>=10){C:{if(!t){if(t=0,n=1,!((0|s)>0&B))break C;w=(k=lA(A,w,s,E,b+624|0))?0:w,t=!!(0|k),o=0;break r}t=1,w=0,1==f[33275]&&(f[b+144>>2]=s+1,dg(k=b+800|0,89026,b+144|0),Mg(A,k,b+688|0)||(f[b+128>>2]=s,dg(k=b+800|0,89026,b+128|0),Mg(A,k,b+624|0)))}n=1,o=0}else Mg(A,88882,b+624|0),n=0,o=256;B=Q?2:l;r:{if(s|i[b+624|0]|46!=i[0|u]){if(s)break r}else Mg(A,89192,b+624|0);if(f[b+844>>2]=e,a[e+1|0]-48>>>0<10)for(;k=f[b+844>>2],f[b+844>>2]=k+1,a[k+2|0]-48>>>0<10;);if(a[f[b+844>>2]-1|0]-48>>>0>=10||(f[b+416>>2]=f[b+844>>2]-1,TA(A,b+416|0,b+192|0,r,4,C)&&(f[33272]=2)),i[b+192|0]|48==i[f[b+844>>2]]||TA(A,b+844|0,b+192|0,r,4,C)&&(f[33272]=1),v){if(!B&n&&(f[b+112>>2]=w,dg(C=b+800|0,89214,b+112|0),Mg(A,C,g)))break e;if(1&a[A+110|0]){for(k=e;32!=(32|i[0|k]);)k=k+1|0;f[b+416>>2]=k,37==i[k+1|0]&&(Mg(A,89328,g),C=Lg(g),a[f[b+416>>2]+1|0]=32,g=g+C|0)}}}pA(A,w,b+416|0,t,s,B|o|M),!(2&i[A+109|0])|(0|s)<=0?(f[b+60>>2]=15,f[b- -64>>2]=b+624,f[b+56>>2]=b+416,f[b+52>>2]=b+352,f[b+48>>2]=b+288,dg(g,89415,b+48|0)):(f[b+88>>2]=15,f[b+96>>2]=b+416,f[b+92>>2]=b+352,f[b+84>>2]=b+624,f[b+80>>2]=b+288,dg(g,89346,b+80|0));r:if(!n)for(;;){for(I=I+1|0,n=0;n=(k=n)+1|0,a[(s=I+k|0)+e|0]-48>>>0<10;);C=2;C:{a:{I:{f:{i:{b:switch((n=57344&f[A+104>>2])+-8192>>>13|0){case 6:break a;case 2:break I;case 0:case 4:case 5:break f;case 1:break i;case 3:break b;default:break C}C=5}if(48==(0|(n=i[0|(t=e+I|0)])))for(;Mg(A,88875,s=b+688|0),mC(g,s),k=k-1|0,48==(0|(n=i[0|(t=(I=I+1|0)+e|0)])););if((0|C)<(0|k)|(n<<24>>24)-48>>>0>=10)break C;C=b+688|0,pA(A,Dg(t),C,0,0,0),mC(g,C),I=I+k|0;break C}if(pA(A,Dg(C=e+I|0),b+416|0,0,0,0),!(8192==(0|n)&48!=i[0|C])){if(f[b+16>>2]=k,dg(C=b+800|0,89508,b+16|0),!Mg(A,C,b+688|0))break C;mC(49152==(0|n)?g:b+416|0,b+688|0)}mC(g,b+416|0),I=s;break C}if((0|k)>4)break C;if(48==i[0|(C=e+I|0)])break C;pA(A,I=Dg(C),C=b+688|0,0,0,0),mC(g,C),I=s;break C}if(!((0|k)<=1))for(;;){if(f[b+32>>2]=a[e+I|0],dg(C=b+800|0,89575,b+32|0),!Mg(A,C,b+688|0))break C;if(mC(g,b+688|0),I=I+1|0,!((0|(k=k-1|0))>1))break}}C:if(!((n=i[0|(k=e+I|0)])-48>>>0>=10||Lg(g)>>>0>=190))for(;;){if(C=b+688|0,EA(A,a[0|k]-48|0,0,2,C),s=Lg(g),f[b>>2]=15,f[b+4>>2]=C,dg(g+s|0,89594,b),(n=i[0|(k=(I=I+1|0)+e|0)])-48>>>0>=10)break C;if(!(Lg(g)>>>0<=189))break}if(Mg(A,89678,b+688|0)&&mC(g,b+688|0),f[A+128>>2]!=(0|n)|a[1+(e+I|0)|0]-48>>>0>=10)break r;Mg(A,88882,C=b+688|0),mC(g,C)}if(!(C=i[0|g])|21==(0|C)||(C=Te(b+184|0,e=1+(e+I|0)|0),I=f[b+184>>2],!(2&i[A+106|0])|32!=(0|I)||(Te(b+184|0,e+C|0),I=f[b+184>>2]),Mr(I)|E||(A=Lg(g)+g|0,a[0|A]=11,a[A+1|0]=0)),f[r>>2]=-2147483648|f[r>>2],f[33275]=f[33275]-1,t=1,x)break A;f[33264]=1;break A}f[r>>2]=-129&f[r>>2],t=0;break A}t=1}}return V=b+848|0,t}function tA(A,e,g,r){var C,b,s=0,t=0,n=0,k=0,o=0,c=0,Q=0,w=0,E=0,D=0,u=0,l=0,x=0,d=0,m=0,M=0,v=0,h=0,p=0,Y=0,H=0;if(V=C=V-416|0,d=B[g+8>>1],E=i[g+10|0],64&(b=f[g>>2])){for(f[g>>2]=-65&b,a[199388]=1,n=f[47202],Q=f[49846],o=f[47352],c=f[47351],k=f[47350];;){s=(t=f[198304+(c<<2)>>2])>>8;A:{e:switch((31&t)-9|0){case 0:n=s;break A;case 4:Q=s;break A;case 3:break e;default:break A}o=t>>>0>=256?s+o|0:0}if(!(!(128&t)&(0|k)>(0|(c=c+1|0))))break}f[47352]=o,f[47351]=c,f[49846]=Q,f[47202]=n}Q=0;A:if(!((0|(s=f[49572]))>997))if(1048576&b||!i[0|e])i[199388]&&(f[49572]=s+1,a[199388]=0,f[(A=190288+(s<<3)|0)>>2]=983042,f[A+4>>2]=0),a[189088]=0;else if(!((0|s)>990)){(0|(s=f[A+8224>>2]))>0&&(f[A+8224>>2]=s-1),n=512&b?r:r+4|0,o=16==(240&f[47202]),(l=2&b)&&((0|(t=f[47200]))<3||(0|(s=f[47350]))>243||(i[199388]&&(f[(c=198300+(s<<2)|0)>>2]=-129&f[c>>2]),f[47350]=s+1,a[199388]=1,m=3==(0|t)?20:t,f[198304+(s<<2)>>2]=m<<8|193)),k=o?n:r;e:{g:{if(!(8&b)){for(;r=i[(s=w)+e|0],a[s+(C+240|0)|0]=r,223&r&&(w=s+1|0,s>>>0<160););if(a[C+66|0]=0,w=C- -64|2,Q=ke(f[47192],e,g,w),f[C+412>>2]=Q,!(4096&Q))break g;_A(e,C+240|0,s);break A}r:if(pg(e,87276,3))JA(e,189088,C+240|0);else{if(c=0,223&(s=i[0|(w=e+3|0)]))for(;a[(C+240|0)+c|0]=ar(s<<24>>24),c=c+1|0,223&(s=i[0|(w=w+1|0)]););a[(r=C+240|0)+c|0]=0,s=0;C:if(!((0|(g=f[34461]))<=0)){for(;;){if(!Qr(r,G(s,44)+137856|0)){f[34457]=s;break C}if((0|g)==(0|(s=s+1|0)))break}s=g}if((0|(g=(0|g)==(0|s)?-1:s))<=0)break r;qr(g),a[189090]=0,a[189089]=g,a[189088]=21}Q=-2147483648,f[C+412>>2]=-2147483648,r=-1;break e}if(!(!(8388608&Q)|8&i[g+12|0])){for(V=o=V-208|0,c=f[A+60>>2],t=e;r=t,t=t+1|0,32!=i[0|r];);Te(o+204|0,t);g:if(Mr(f[o+204>>2])){n=rg(o,189088),t=(!(256&c)|(u=gA(A,t,g+12|0,0))>>>15)&!(67108864&u)&21!=i[189088];r:{C:{if(512&c){if(!(t&!(16&i[g+12|0])))break C;break r}if(t)break r}rg(189088,n);break g}if(a[0|r]=45,f[g>>2]=-2&f[g>>2],Q=0,t=189088,D=gA(f[47192],e,g,0),f[C+412>>2]=D,!((0|c)<=0)&&(n=i[189088])){for(;Q=(2==i[f[144464+(n<<2)>>2]+11|0])+Q|0,n=i[0|(t=t+1|0)];);if(!((31&c)>=(0|Q))){a[0|r]=32,f[C+412>>2]=gA(f[47192],e,g,0);break g}}f[C+412>>2]=128|(D||u),f[33264]=1}V=o+208|0}if(r=-1,21==i[189088]){if(c=rg(C+16|0,132848),o=f[C+412>>2],t=_A(e,C+240|0,s),n=C- -64|1,(0|(r=vg(i[189089]?189089:87315,188772,189296)))<0||(f[g>>2]=4194304|f[g>>2],i[C+66|0]?(I[C+64>>1]=8192,o=gA(f[47193],n,g,0)):o=ke(f[47193],t,g,w)),21==i[189088]&&(s=_A(t,C+240|0,s),(0|(r=vg(i[189089]?189089:87315,188772,189296)))<0||(f[g>>2]=4194304|f[g>>2],i[C+66|0]?(I[C+64>>1]=8192,o=gA(f[47193],n,g,0)):o=ke(f[47193],s,g,w)),Q=4096,21==i[189088]))break A;f[C+412>>2]=o,(0|r)>=0||(a[189090]=0,I[94544]=3341,-1==(0|r)&&(rg(132848,c),qr(f[f[32972]+60>>2]),r=f[f[32972]+60>>2]))}Q=f[C+412>>2],128&b||(k=268435456&Q&&(0|k)<=1?1:k,!(256&Q)|528&b|f[A+8224>>2]|2&i[g-11|0]||(f[A+8224>>2]=3,k=(0|k)<=4?4:k)),k=(0|k)<=0&&f[49846]>2?1:k}if(g=i[199388],!((0|k)<=0|(0|(w=f[49572]))>990)){f[49572]=w+1,t=1&g,g=0,I[(s=190288+(w<<3)|0)>>1]=t?2:0,a[s+7|0]=0,a[s+3|0]=0,t=k>>>0>1,a[s+2|0]=t?9:11,I[s+4>>1]=0,f[A+8236>>2]=0;e:if(!(!(s=t?k-2|0:0)|(0|(w=f[49572]))>990))for(n=A+8236|0;;){if(f[49572]=w+1,I[(t=190288+(w<<3)|0)>>1]=0,a[t+7|0]=0,a[t+3|0]=0,o=s>>>0>1,a[t+2|0]=o?9:11,I[t+4>>1]=0,f[n>>2]=0,w=f[49572],(0|(s=o?s-2|0:0))<=0)break e;if(!((0|w)<991))break}f[A+8228>>2]=0,f[A+8232>>2]=0}a[199388]=1&g,!l|1!=f[47200]||(f[49572]=w+2,a[199388]=0,I[(s=190288+(w<<3)|0)>>1]=1&g?2:0,a[s+7|0]=0,I[s+2>>1]=10,I[s+4>>1]=0,I[s+12>>1]=0,f[s+8>>2]=1179648,a[s+15|0]=0,1&b&&kg(a[e+1|0])&&(g=i[199388],a[199388]=0,e=f[49572],f[49572]=e+2,I[(e=190288+(e<<3)|0)>>1]=g?2:0,a[e+7|0]=0,I[e+2>>1]=10,I[e+4>>1]=0,I[e+12>>1]=0,f[e+8>>2]=1179648,a[e+15|0]=0)),s=E>>>0<31;e:if(!((0|r)<0))if(e=f[49572],g=i[190290+((k=e-1|0)<<3)|0],9!=i[189088]|21!=i[189089])21!=(0|g)&&(t=i[199388],a[199388]=0,a[(g=190288+(e<<3)|0)+7|0]=0,I[g+2>>1]=21,I[g+4>>1]=0,I[g>>1]=t?2:0,k=e),f[49572]=k+1,a[190295+(k<<3)|0]=r;else{if(21!=(0|g))break e;f[49572]=k}e=2047&d,g=(s?E:31)<<11,o=(M=128&b)?i[f[144464+(i[189088]<<2)>>2]+11|0]?189088:189089:189088,(k=i[0|o])|!(1&a[199388])||(k=23,a[0|o]=23,a[o+1|0]=0),v=e|g,n=f[49572];e:if(k)if((0|n)>994)e=0;else for(Y=(-2147483648==(-1610612736&Q))<<4,d=v+1|0,H=A+8233|0,e=0,t=1,u=0,h=1,E=-1,l=-1,s=0;;){c=o+1|0;g:{if(255!=(0|(D=255&k))){if(g=f[144464+(D<<2)>>2])break g;f[C>>2]=D,V=g=V-16|0,f[g+12>>2]=C,QC(132552,87474,C),V=g+16|0,n=f[49572]}if(!(k=i[0|c]))break e;if(o=c,(0|n)<995)continue;break e}g:if(21!=(0|(x=255&k)))if(1!=(0|(o=i[g+11|0]))){g=d;r:switch(x-12|0){case 8:a[(g=190288+((E=n-1|0)<<3)|0)+3|0]=t,I[g>>1]=4|B[g>>1],g=s;break g;case 0:I[(g=190280+(n<<3)|0)>>1]=8|B[g>>1],g=s;break g;case 10:Q|=16384,f[C+412>>2]=Q,g=s;break g;case 3:break g;default:break r}D=i[199388],a[199388]=0,a[(g=(x=n<<3)+190288|0)+7|0]=0,a[g+2|0]=k,I[g+4>>1]=s,s=(D?2:0)|Y,I[g>>1]=s,2==(0|o)?((0|t)>=4&&(a[189076]=1),(0|E)<0||(0|(e=n-1|0))!=(0|E)&&(a[190291+(e<<3)|0]=t),I[g>>1]=4|s,p=(e=(0|t)>(0|l))?n:p,l=e?t:l,s=1,u&&(a[g+7|0]=u),E=n,u=0,e=t):(!h|!(64&i[0|H])||(I[g>>1]=8|s),s=t),n=n+1|0,f[49572]=n,a[x+190291|0]=e,g=0,h=0,t=s}else{if(!B[g+8>>1]){t=i[g+14|0],g=s;break g}if((0|E)<0){g=s,u=D;break g}a[190295+(E<<3)|0]=k,g=s}else f[(g=190288+(n<<3)|0)>>2]=1376256,I[g+4>>1]=0,a[g+7|0]=i[o+1|0],f[49572]=n+1,qr(i[o+1|0]),c=o+2|0,n=f[49572],g=s;if(!(k=i[0|c]))break e;if(o=c,s=g,!((0|n)<995))break}else e=0;131072&b&&(g=n+1|0,f[49572]=g,t=i[199388],a[199388]=0,a[(s=190288+(n<<3)|0)+7|0]=0,I[s+2>>1]=27,I[s+4>>1]=0,I[s>>1]=t?2:0,n=g),M||(I[190292+(w<<3)>>1]=v),f[A+8228>>2]=0,2!=i[f[144464+(i[190282+(n<<3)|0]<<2)>>2]+11|0]|(0|e)<4||(f[A+8228>>2]=1),(0|r)>=0&&(rg(132848,C+16|0),qr(f[f[32972]+60>>2]),g=i[199388],a[199388]=0,r=f[49572],I[(e=190288+(r<<3)|0)>>1]=g?2:0,I[e+2>>1]=21,I[e+4>>1]=0,a[e+7|0]=f[f[32972]+60>>2],n=r+1|0,f[49572]=n),(0|m)>0&&(a[199388]=0,f[49572]=n+1,f[(e=190288+(n<<3)|0)>>2]=655362,g=f[47350],f[47350]=g+1,a[e+7|0]=0,I[e+4>>1]=0,f[198304+(g<<2)>>2]=m<<8|225),1024&Q&&(I[(e=190288+(p<<3)|0)>>1]=64|B[e>>1]),f[A+8232>>2]=Q}return V=C+416|0,Q}function nA(A,e,g,r,C){var I=0,b=0,s=0,t=0,n=0,k=0,o=0,B=0,c=0,Q=0,w=0,E=0,D=0,u=0,l=0,x=0,d=0,m=0,M=0,v=0,h=0,p=0,Y=0,H=0;A:if(e){if(D=C<<4,b=(e=f[33268])+g|0,!((0|e)<=0||(I=i[0|(t=D+129360|0)],o=i[t+1|0]-I<<8,o=1!=(0|e)?(0|o)/(0|e)|0:o,(0|g)>=(0|b))))for(B=o>>>0>255,w=o>>>8|0,I<<=8,t=0-o>>>8|0,x=(0|o)<=0,e=g;x?(n=(k=(k=(0|(s=(0|(s=f[101024+(a[G(e,6)+A|0]<<2)>>2]))<(0|t)?t:s))>=18?18:s)+(s=(0|(s=(0|(I=I+o|0))/256|0))>0?s:0)|0)>>>0>=254?254:k,s=s>>>0>=254?254:s):(s=G(e,6)+A|0,a[s+2|0]=B|i[s+2|0],s=(k=(0|(s=(0|I)/256|0))>0?s:0)>>>0>=254?254:k,n=(k=k+w|0)>>>0>=254?254:k,I=I+o|0),k=G(e,6)+A|0,a[k+5|0]=s,a[k+4|0]=n,(0|b)!=(0|(e=e+1|0)););if(2&i[188785]?(t=f[33271],f[33270]=t):t=f[33270],w=(B=i[(I=(e=C<<4)+129360|0)+3|0])-(s=i[I+2|0])<<8,x=f[I+4>>2],(0|g)>0?(m=101056,u=5,k=(0|w)/(i[e+129368|0]-1|0)|0):(m=f[(e=e+129360|0)+12>>2],u=i[e+10|0],k=0),(0|b)<(0|t)){for(o=(0|g)<=0,E=((e=w>>31)^w)-e|0,c=129360+(C<<4)|0,s<<=8,d=B<<8,B=0;;){e:{if(n=G(b,6)+A|0,!(!(1&o)&(0|(I=a[0|n]))<4)){g=0,e=b;g:if(1&(5==(0|I)|o)){for(;(0|(k=a[G(e,6)+A|0]))<=6&&(g=((0|k)>3)+g|0,(0|t)!=(0|(e=e+1|0))););if(Q=0,(0|(l=(0|(e=i[c+8|0]))>(0|g)?g:e))<2){k=0,B=s;break g}k=(0|w)/(l-1|0)|0,B=s}else(0|l)>0?B=k+B|0:(B=d+(G(E,a[Q+m|0])>>6)|0,(0|u)>(0|(Q=Q+1|0))||(m=f[c+12>>2],Q=0));if(l=l-1|0,!((0|I)<4)){a[0|n]=6,g=(e=(0|(e=(0|B)/256|0))>0?e:0)+(((g=(I=f[x+(I<<2)>>2])>>31)^I)-g|0)|0;break e}}3!=(0|I)?(e=(0|B)/256|0,(63&i[n-6|0])>>>0>=3?(g=(I=f[x+(I<<2)>>2])>>31,g=(e=(0|(e=e-a[c+9|0]|0))>0?e:0)+((g^I)-g|0)|0):g=(e=(0|e)>0?e:0)+(((g=(I=f[x+(I<<2)>>2])>>31)^I)-g|0)|0):g=(e=(0|(e=(0|B)/256|0))>0?e:0)+(((g=(I=f[x+12>>2])>>31)^I)-g|0)|0}if(a[n+5|0]=e>>>0>=254?254:e,o=0,e=(0|g)>0?g:0,a[n+4|0]=e>>>0>=254?254:e,a[n+2|0]=i[n+2|0]|I>>>31,(0|t)==(0|(b=b+1|0)))break}b=t}if(!(i[133068]||(3==(268435455&C)&&(e=G(b,6)+A|0,a[e+2|0]=2|i[e+2|0]),f[33269]?(e=i[(I=100768+(C<<4)|0)+5|0],s=I+3|0,g=i[I+4|0]-e|0):(e=i[(g=100768+(C<<4)|0)+2|0],s=D+100768|0,g=i[g+1|0]-e|0),I=G(b,6)+A|0,a[I+5|0]=e>>>0>=254?254:e,a[I+2|0]=i[I+2|0]|g>>>31,e=(k=e)+(((e=g>>31)^g)-e|0)|0,a[I+4|0]=e>>>0>=254?254:e,g=G(t,6)+A|0,a[g+1|0]=i[0|s],e=b+1|0,4==i[0|g]&&(a[0|g]=6),(0|(g=r-e|0))<=0||(C=i[(b=100768+(C<<4)|0)+12|0],b=i[b+13|0]-C<<8,b=1!=(0|g)?(0|b)/(0|g)|0:b,(0|e)>=(0|r)))))for(k=b>>>0>255,o=b>>>8|0,g=C<<8,C=0-b>>>8|0,B=(0|b)<=0;B?(s=(t=(s=(0|(I=(0|C)>(0|(I=f[101024+(a[G(e,6)+A|0]<<2)>>2]))?C:I))>=18?18:I)+(I=(0|(I=(0|(g=g+b|0))/256|0))>0?I:0)|0)>>>0>=254?254:t,t=I>>>0>=254?254:I):(I=G(e,6)+A|0,a[I+2|0]=k|i[I+2|0],t=(I=(0|(I=(0|g)/256|0))>0?I:0)>>>0>=254?254:I,s=(I=I+o|0)>>>0>=254?254:I,g=g+b|0),I=G(e,6)+A|0,a[I+5|0]=t,a[I+4|0]=s,(0|r)!=(0|(e=e+1|0)););}else{if(e=(u=f[34455])+G(C,68)|0,b=i[e+24|0],t=i[e+25|0],I=(e=f[33268])+g|0,!((0|e)<=0||(n=t-b<<8,n=1!=(0|e)?(0|n)/(0|e)|0:n,(0|g)>=(0|I))))for(k=n>>>0>255,o=n>>>8|0,e=b<<8,t=0-n>>>8|0,B=(0|n)<=0;B?(b=(b=(w=(0|(b=(0|(b=f[101024+(a[G(g,6)+A|0]<<2)>>2]))<(0|t)?t:b))>=18?18:b)+(s=(0|(b=(0|(e=e+n|0))/256|0))>0?b:0)|0)>>>0>=254?254:b,s=s>>>0>=254?254:s):(b=G(g,6)+A|0,a[b+2|0]=k|i[b+2|0],s=(b=(0|(b=(0|e)/256|0))>0?b:0)>>>0>=254?254:b,b=(b=b+o|0)>>>0>=254?254:b,e=e+n|0),w=G(g,6)+A|0,a[w+5|0]=s,a[w+4|0]=b,(0|I)!=(0|(g=g+1|0)););2&i[188785]?(g=f[33271],f[33270]=g):g=f[33270],t=g,e=u+G(C,68)|0;e:if(255!=(0|(o=i[e+33|0])))for(;;){if((0|(t=t-1|0))<(0|I)){t=g;break e}if(!(a[G(t,6)+A|0]<4))break}if((0|g)>(0|I)){for(p=((b=(v=(B=i[e+32|0])-(n=i[0|(s=e+31|0)])<<8)>>31)^v)-b|0,d=255==(0|(e=i[0|(k=e+30|0)])),w=o<<8,x=n<<8,Y=B<<8,D=u+G(C,68)|0,H=255!=(0|e),B=0,o=0,e=1,n=0;;){e:{if(c=G(I,6)+A|0,!(!(1&e)&(0|(E=a[0|c]))<4)){g:if(1&(5==(0|E)|e)){r:if(H){if(M=1,e=0,b=k,!((0|t)<=(0|(n=I+1|0)))){for(;;){if(b=k,(0|(o=a[G(n,6)+A|0]))>6)break r;if(e=((0|o)>3)+e|0,(0|t)==(0|(n=n+1|0)))break}b=k}}else{if(e=0,n=I,b=s,(0|t)<=(0|I))break r;for(;;){if(b=s,(0|(o=a[G(n,6)+A|0]))>6)break r;if(e=((0|o)>3)+e|0,(0|t)==(0|(n=n+1|0)))break}b=s}if(o=i[0|b]<<8,h=0,(0|(n=(0|e)<(0|(b=i[D+34|0]))?e:b))<2){m=0;break g}m=(0|v)/(n-1|0)|0}else(0|I)!=(0|t)?M?(d=1,n=n+1|0,M=0,o=x):(0|n)>0?(o=o+m|0,M=0):(M=0,o=(G(a[16+(D+h|0)|0],p)>>6)+Y|0,h=(0|(e=h+1|0))1)break g;if(!((0|g)>=(0|(e=e+1|0))))break}a[0|c]=6,a[c+1|0]=i[D+26|0],B=0,Q=i[D+27|0],I=(0|(I=(0|o)/256|0))>0?I:0,a[c+5|0]=I>>>0>=254?254:I,I=I+Q|0,a[c+4|0]=I>>>0>=254?254:I,l=e-b|0;break e}}if((0|E)>=2){e=b=I+1|0;g:if(!((0|g)<=(0|I)))for(;;){if(a[G(e,6)+A|0]>1)break g;if(!((0|g)>=(0|(e=e+1|0))))break}B=0,I=(0|(I=(0|o)/256|0))>0?I:0,a[c+5|0]=I>>>0>=254?254:I,Q=f[100976+(E<<2)>>2],a[c+2|0]=i[c+2|0]|Q>>>31,I=(0|(I=(E=I)+(((I=Q>>31)^Q)-I|0)|0))>0?I:0,a[c+4|0]=I>>>0>=254?254:I,l=e-b|0}else(0|l)<=1?(Q=a[36+(D+d|0)|0],e=0):(Q=a[(e=D+d|0)+36|0],e=(a[e+39|0]-Q|0)/(l-1|0)|0),b=f[100976+(E<<2)>>2],a[c+2|0]=i[c+2|0]|b>>>31,e=(0|(e=(((0|o)/256|0)+Q|0)+G(e,B)|0))>0?e:0,a[c+5|0]=e>>>0>=254?254:e,e=(0|(e=e+(((E=b)^(b>>=31))-b|0)|0))>0?e:0,a[c+4|0]=e>>>0>=254?254:e,B=B+1|0,b=I+1|0}if(e=0,(0|g)==(0|(I=b)))break}I=g}if(i[133068])break A;if(b=G(I,6)+A|0,f[33269]?(s=u+G(C,68)|0,e=i[s+47|0],t=i[s+46|0]-e|0,s=s+45|0):(s=u+G(C,68)|0,e=i[s+44|0],t=i[s+43|0]-e|0,s=s+42|0),s=i[0|s],k=(((k=t>>31)^t)-k|0)+e|0,a[b+4|0]=k>>>0>=254?254:k,a[b+5|0]=e>>>0>=254?254:e,e=G(I,6)+A|0,a[e+2|0]=i[e+2|0]|t>>>31,g=G(g,6)+A|0,a[g+1|0]=s,e=I+1|0,4==i[0|g]&&(a[0|g]=6),(0|(g=r-e|0))<=0)break A;if(b=u+G(C,68)|0,C=i[b+48|0],b=i[b+49|0]-C<<8,b=1!=(0|g)?(0|b)/(0|g)|0:b,(0|e)>=(0|r))break A;for(k=b>>>0>255,o=b>>>8|0,g=C<<8,C=0-b>>>8|0,B=(0|b)<=0;B?(s=(t=(s=(0|(I=(0|C)>(0|(I=f[101024+(a[G(e,6)+A|0]<<2)>>2]))?C:I))>=18?18:I)+(I=(0|(I=(0|(g=g+b|0))/256|0))>0?I:0)|0)>>>0>=254?254:t,t=I>>>0>=254?254:I):(I=G(e,6)+A|0,a[I+2|0]=k|i[I+2|0],t=(I=(0|(I=(0|g)/256|0))>0?I:0)>>>0>=254?254:I,s=(I=I+o|0)>>>0>=254?254:I,g=g+b|0),I=G(e,6)+A|0,a[I+5|0]=t,a[I+4|0]=s,(0|r)!=(0|(e=e+1|0)););}}function kA(){var A,e=0,g=0,r=0,C=0,i=0,b=0,s=0,t=0,n=0,k=0,o=0,c=0,Q=0,w=0,E=0;if((0|(A=f[36455]))!=(0|(t=f[36454]))){A:if(!((0|(Q=f[36427]))<0|(0|t)==(0|Q))){g=o=f[8+(216192+((n=Q)<<4)|0)>>2];e:{for(;;){if((r=f[(b=216192+((n=(0|(r=n-1|0))<0?169:r)<<4)|0)>>2])-5>>>0<2)break e;g:{if((0|r)<=4){if(f[b+12>>2]!=(0|g))break e;if(r=B[b+4>>1],f[b+12>>2]=o,g=f[b+8>>2],16&(e=B[g>>1]))break g;for(E=32&e?(G(r,12)>>>0)/10|0:r,C=0,r=g,s=0;;){e=B[g>>1];r:if(!(s>>>0<3&&8&e)){if(e=e<<16>>16,(0|(w=(k=I[2+((c=s<<1)+g|0)>>1])-(i=I[(c=o+c|0)+2>>1])|0))>(0|(k=(0|G(E,(0|G(f[200944+(s<<2)>>2],(0|w)>0?k+(i<<1)|0:(k<<1)+i|0))/3e3|0))/256|0)))C||((0|e)<0?r=g:(r=0,e=(0|(e=f[44469]+1|0))<=169?e:0,f[44469]=e,(e=(C=e<<6)+177888|0)&&(r=B[g+4>>1]|B[g+6>>1]<<16,i=B[g>>1]|B[g+2>>1]<<16,I[e>>1]=i,I[e+2>>1]=i>>>16,I[e+4>>1]=r,I[e+6>>1]=r>>>16,r=B[g+60>>1]|B[g+62>>1]<<16,i=B[g+56>>1]|B[g+58>>1]<<16,I[e+56>>1]=i,I[e+58>>1]=i>>>16,I[e+60>>1]=r,I[e+62>>1]=r>>>16,r=B[g+52>>1]|B[g+54>>1]<<16,i=B[g+48>>1]|B[g+50>>1]<<16,I[e+48>>1]=i,I[e+50>>1]=i>>>16,I[e+52>>1]=r,I[e+54>>1]=r>>>16,r=B[g+44>>1]|B[g+46>>1]<<16,i=B[g+40>>1]|B[g+42>>1]<<16,I[e+40>>1]=i,I[e+42>>1]=i>>>16,I[e+44>>1]=r,I[e+46>>1]=r>>>16,r=B[g+36>>1]|B[g+38>>1]<<16,i=B[g+32>>1]|B[g+34>>1]<<16,I[e+32>>1]=i,I[e+34>>1]=i>>>16,I[e+36>>1]=r,I[e+38>>1]=r>>>16,r=B[g+28>>1]|B[g+30>>1]<<16,i=B[g+24>>1]|B[g+26>>1]<<16,I[e+24>>1]=i,I[e+26>>1]=i>>>16,I[e+28>>1]=r,I[e+30>>1]=r>>>16,r=B[g+20>>1]|B[g+22>>1]<<16,i=B[g+16>>1]|B[g+18>>1]<<16,I[e+16>>1]=i,I[e+18>>1]=i>>>16,I[e+20>>1]=r,I[e+22>>1]=r>>>16,r=B[g+12>>1]|B[g+14>>1]<<16,i=B[g+8>>1]|B[g+10>>1]<<16,I[e+8>>1]=i,I[e+10>>1]=i>>>16,I[e+12>>1]=r,I[e+14>>1]=r>>>16,a[C+177904|0]=0,I[e>>1]=32768|B[e>>1],r=e))),e=k+B[c+2>>1]|0;else{if((0-k|0)<=(0|w))break r;C||((0|e)<0?r=g:(r=0,e=(0|(e=f[44469]+1|0))<=169?e:0,f[44469]=e,(e=(C=e<<6)+177888|0)&&(r=B[g+4>>1]|B[g+6>>1]<<16,i=B[g>>1]|B[g+2>>1]<<16,I[e>>1]=i,I[e+2>>1]=i>>>16,I[e+4>>1]=r,I[e+6>>1]=r>>>16,r=B[g+60>>1]|B[g+62>>1]<<16,i=B[g+56>>1]|B[g+58>>1]<<16,I[e+56>>1]=i,I[e+58>>1]=i>>>16,I[e+60>>1]=r,I[e+62>>1]=r>>>16,r=B[g+52>>1]|B[g+54>>1]<<16,i=B[g+48>>1]|B[g+50>>1]<<16,I[e+48>>1]=i,I[e+50>>1]=i>>>16,I[e+52>>1]=r,I[e+54>>1]=r>>>16,r=B[g+44>>1]|B[g+46>>1]<<16,i=B[g+40>>1]|B[g+42>>1]<<16,I[e+40>>1]=i,I[e+42>>1]=i>>>16,I[e+44>>1]=r,I[e+46>>1]=r>>>16,r=B[g+36>>1]|B[g+38>>1]<<16,i=B[g+32>>1]|B[g+34>>1]<<16,I[e+32>>1]=i,I[e+34>>1]=i>>>16,I[e+36>>1]=r,I[e+38>>1]=r>>>16,r=B[g+28>>1]|B[g+30>>1]<<16,i=B[g+24>>1]|B[g+26>>1]<<16,I[e+24>>1]=i,I[e+26>>1]=i>>>16,I[e+28>>1]=r,I[e+30>>1]=r>>>16,r=B[g+20>>1]|B[g+22>>1]<<16,i=B[g+16>>1]|B[g+18>>1]<<16,I[e+16>>1]=i,I[e+18>>1]=i>>>16,I[e+20>>1]=r,I[e+22>>1]=r>>>16,r=B[g+12>>1]|B[g+14>>1]<<16,i=B[g+8>>1]|B[g+10>>1]<<16,I[e+8>>1]=i,I[e+10>>1]=i>>>16,I[e+12>>1]=r,I[e+14>>1]=r>>>16,a[C+177904|0]=0,I[e>>1]=32768|B[e>>1],r=e))),e=B[c+2>>1]-k|0}C=1,I[2+((s<<1)+r|0)>>1]=e,f[b+8>>2]=r}if(6==(0|(s=s+1|0)))break}o=r}if((0|t)!=(0|n))continue;break e}break}o=g}for(r=0;;){if((g=f[(n=216192+(Q<<4)|0)>>2])-5>>>0<2)break A;if((0|g)<=4){if(g=f[n+8>>2],e=B[n+4>>1],r){if((0|g)!=(0|r))break A;f[n+8>>2]=o}else o=g;if(16&(r=B[o>>1]))break A;for(c=32&r?(G(e,6)>>>0)/5|0:e,C=0,g=r=f[n+12>>2],s=0;;){e:{g:if((0|(t=(b=I[2+((e=s<<1)+r|0)>>1])-(e=I[(k=e+o|0)+2>>1])|0))>(0|(b=(0|G(c,(0|G(f[200944+(s<<2)>>2],(0|t)>0?b+(e<<1)|0:(b<<1)+e|0))/3e3|0))/256|0))){if(!C){if(I[r>>1]<0){g=r,e=e+b|0;break g}g=(0|(g=f[44469]+1|0))<=169?g:0,f[44469]=g,C=B[r+20>>1]|B[r+22>>1]<<16,e=(g=177888+(g<<6)|0)+16|0,t=B[r+16>>1]|B[r+18>>1]<<16,I[e>>1]=t,I[e+2>>1]=t>>>16,I[e+4>>1]=C,I[e+6>>1]=C>>>16,e=B[r+4>>1]|B[r+6>>1]<<16,C=B[r>>1]|B[r+2>>1]<<16,I[g>>1]=C,I[g+2>>1]=C>>>16,I[g+4>>1]=e,I[g+6>>1]=e>>>16,e=B[r+12>>1]|B[r+14>>1]<<16,C=B[r+8>>1]|B[r+10>>1]<<16,I[g+8>>1]=C,I[g+10>>1]=C>>>16,I[g+12>>1]=e,I[g+14>>1]=e>>>16,e=B[r+28>>1]|B[r+30>>1]<<16,C=B[r+24>>1]|B[r+26>>1]<<16,I[g+24>>1]=C,I[g+26>>1]=C>>>16,I[g+28>>1]=e,I[g+30>>1]=e>>>16,e=B[r+36>>1]|B[r+38>>1]<<16,C=B[r+32>>1]|B[r+34>>1]<<16,I[g+32>>1]=C,I[g+34>>1]=C>>>16,I[g+36>>1]=e,I[g+38>>1]=e>>>16,e=B[r+44>>1]|B[r+46>>1]<<16,C=B[r+40>>1]|B[r+42>>1]<<16,I[g+40>>1]=C,I[g+42>>1]=C>>>16,I[g+44>>1]=e,I[g+46>>1]=e>>>16,e=B[r+52>>1]|B[r+54>>1]<<16,C=B[r+48>>1]|B[r+50>>1]<<16,I[g+48>>1]=C,I[g+50>>1]=C>>>16,I[g+52>>1]=e,I[g+54>>1]=e>>>16,e=B[r+60>>1]|B[r+62>>1]<<16,C=B[r+56>>1]|B[r+58>>1]<<16,I[g+56>>1]=C,I[g+58>>1]=C>>>16,I[g+60>>1]=e,I[g+62>>1]=e>>>16,a[g+16|0]=0,I[g>>1]=32768|B[g>>1],e=B[k+2>>1]}e=e+b|0}else{if((0|t)>=(0-b|0))break e;C||(I[r>>1]<0?g=r:(g=(0|(g=f[44469]+1|0))<=169?g:0,f[44469]=g,C=B[r+20>>1]|B[r+22>>1]<<16,e=(g=177888+(g<<6)|0)+16|0,t=B[r+16>>1]|B[r+18>>1]<<16,I[e>>1]=t,I[e+2>>1]=t>>>16,I[e+4>>1]=C,I[e+6>>1]=C>>>16,e=B[r+4>>1]|B[r+6>>1]<<16,C=B[r>>1]|B[r+2>>1]<<16,I[g>>1]=C,I[g+2>>1]=C>>>16,I[g+4>>1]=e,I[g+6>>1]=e>>>16,e=B[r+12>>1]|B[r+14>>1]<<16,C=B[r+8>>1]|B[r+10>>1]<<16,I[g+8>>1]=C,I[g+10>>1]=C>>>16,I[g+12>>1]=e,I[g+14>>1]=e>>>16,e=B[r+28>>1]|B[r+30>>1]<<16,C=B[r+24>>1]|B[r+26>>1]<<16,I[g+24>>1]=C,I[g+26>>1]=C>>>16,I[g+28>>1]=e,I[g+30>>1]=e>>>16,e=B[r+36>>1]|B[r+38>>1]<<16,C=B[r+32>>1]|B[r+34>>1]<<16,I[g+32>>1]=C,I[g+34>>1]=C>>>16,I[g+36>>1]=e,I[g+38>>1]=e>>>16,e=B[r+44>>1]|B[r+46>>1]<<16,C=B[r+40>>1]|B[r+42>>1]<<16,I[g+40>>1]=C,I[g+42>>1]=C>>>16,I[g+44>>1]=e,I[g+46>>1]=e>>>16,e=B[r+52>>1]|B[r+54>>1]<<16,C=B[r+48>>1]|B[r+50>>1]<<16,I[g+48>>1]=C,I[g+50>>1]=C>>>16,I[g+52>>1]=e,I[g+54>>1]=e>>>16,e=B[r+60>>1]|B[r+62>>1]<<16,C=B[r+56>>1]|B[r+58>>1]<<16,I[g+56>>1]=C,I[g+58>>1]=C>>>16,I[g+60>>1]=e,I[g+62>>1]=e>>>16,a[g+16|0]=0,I[g>>1]=32768|B[g>>1],e=B[k+2>>1])),e=e-b|0}C=1,I[2+((s<<1)+g|0)>>1]=e,f[n+12>>2]=g}if(6==(0|(s=s+1|0)))break}o=g}if((0|A)==(0|(Q=(0|(g=Q+1|0))<=169?g:0)))break}}f[36454]=A}}function oA(A,e){var g,r,C=0,a=0,I=0,i=0,k=0,o=0,B=0,c=0,w=0,D=0,u=0,x=0,d=0,m=0,M=0,v=0,h=0,p=0,Y=0,H=0,N=0,P=0,F=0;V=g=V-48|0,n(+A),a=0|b(1),C=0|b(0),r=a;A:{e:{g:{if((k=2147483647&a)>>>0<=1074752122){if(598523==(1048575&a))break g;if(k>>>0<=1073928572){if((0|r)>0|(0|r)>=0){i=(A+=-1.5707963267341256)+-6077100506506192e-26,Q[e>>3]=i,Q[e+8>>3]=A-i-6077100506506192e-26,a=1;break A}i=(A+=1.5707963267341256)+6077100506506192e-26,Q[e>>3]=i,Q[e+8>>3]=A-i+6077100506506192e-26,a=-1;break A}if((0|r)>0|(0|r)>=0){i=(A+=-3.1415926534682512)+-1.2154201013012384e-10,Q[e>>3]=i,Q[e+8>>3]=A-i-1.2154201013012384e-10,a=2;break A}i=(A+=3.1415926534682512)+1.2154201013012384e-10,Q[e>>3]=i,Q[e+8>>3]=A-i+1.2154201013012384e-10,a=-2;break A}if(k>>>0<=1075594811){if(k>>>0<=1075183036){if(1074977148==(0|k))break g;if((0|r)>0|(0|r)>=0){i=(A+=-4.712388980202377)+-1.8231301519518578e-10,Q[e>>3]=i,Q[e+8>>3]=A-i-1.8231301519518578e-10,a=3;break A}i=(A+=4.712388980202377)+1.8231301519518578e-10,Q[e>>3]=i,Q[e+8>>3]=A-i+1.8231301519518578e-10,a=-3;break A}if(1075388923==(0|k))break g;if((0|r)>0|(0|r)>=0){i=(A+=-6.2831853069365025)+-2.430840202602477e-10,Q[e>>3]=i,Q[e+8>>3]=A-i-2.430840202602477e-10,a=4;break A}i=(A+=6.2831853069365025)+2.430840202602477e-10,Q[e>>3]=i,Q[e+8>>3]=A-i+2.430840202602477e-10,a=-4;break A}if(k>>>0>1094263290)break e}C=(h=(i=A+-1.5707963267341256*(c=.6366197723675814*A+6755399441055744-6755399441055744))-(D=6077100506506192e-26*c))<-.7853981633974483,a=E(c)<2147483648?~~c:-2147483648,C?(a=a-1|0,D=6077100506506192e-26*(c+=-1),i=A+-1.5707963267341256*c):h>.7853981633974483&&(a=a+1|0,D=6077100506506192e-26*(c+=1),i=A+-1.5707963267341256*c),A=i-D,Q[e>>3]=A,n(+A),C=0|b(1),b(0),((I=k>>>20|0)-(C>>>20&2047)|0)<17||(D=i,A=(i-=A=6077100506303966e-26*c)-(D=20222662487959506e-37*c-(D-i-A)),Q[e>>3]=A,n(+A),C=0|b(1),b(0),(I-(C>>>20&2047)|0)<50||(D=i,A=(i-=A=20222662487111665e-37*c)-(D=84784276603689e-45*c-(D-i-A)),Q[e>>3]=A)),Q[e+8>>3]=i-A-D;break A}if(k>>>0>=2146435072)A-=A,Q[e>>3]=A,Q[e+8>>3]=A,a=0;else{for(s(0,0|C),s(1,1048575&r|1096810496),A=+t(),a=0,C=1;I=(g+16|0)+(a<<3)|0,i=+(0|(a=E(A)<2147483648?~~A:-2147483648)),Q[I>>3]=i,A=16777216*(A-i),a=1,I=C,C=0,I;);for(Q[g+32>>3]=A,a=2;a=(C=a)-1|0,0==Q[(g+16|0)+(C<<3)>>3];);if(p=g+16|0,I=0,V=o=V-560|0,k=G(v=(0|(k=((a=(k>>>20|0)-1046|0)-3|0)/24|0))>0?k:0,-24)+a|0,((u=f[28105])+(B=(m=C+1|0)-1|0)|0)>=0)for(a=u+m|0,C=v-B|0;Q[(o+320|0)+(I<<3)>>3]=(0|C)<0?0:+f[112432+(C<<2)>>2],C=C+1|0,(0|a)!=(0|(I=I+1|0)););for(d=k-24|0,a=0,I=(0|u)>0?u:0,x=(0|m)<=0;;){if(x)A=0;else for(w=a+B|0,C=0,A=0;A=Q[(C<<3)+p>>3]*Q[(o+320|0)+(w-C<<3)>>3]+A,(0|m)!=(0|(C=C+1|0)););if(Q[(a<<3)+o>>3]=A,C=(0|a)==(0|I),a=a+1|0,C)break}P=47-k|0,Y=48-k|0,F=k-25|0,a=u;e:{for(;;){if(A=Q[(a<<3)+o>>3],C=0,I=a,!(w=(0|a)<=0))for(;x=(o+480|0)+(C<<2)|0,B=E(i=5.960464477539063e-8*A)<2147483648?~~i:-2147483648,B=E(A=-16777216*(i=+(0|B))+A)<2147483648?~~A:-2147483648,f[x>>2]=B,A=Q[((I=I-1|0)<<3)+o>>3]+i,(0|a)!=(0|(C=C+1|0)););A=Qg(A,d),A+=-8*l(.125*A),A-=+(0|(x=E(A)<2147483648?~~A:-2147483648));g:{r:{C:{if(H=(0|d)<=0){if(d)break C;B=f[476+((a<<2)+o|0)>>2]>>23}else M=I=(a<<2)+o|0,I=(B=f[I+476>>2])-((C=B>>Y)<>2]=I,x=C+x|0,B=I>>P;if((0|B)<=0)break g;break r}if(B=2,!(A>=.5)){B=0;break g}}if(C=0,I=0,!w)for(;N=f[(M=(o+480|0)+(C<<2)|0)>>2],w=16777215,I||(w=16777216,N)?(f[M>>2]=w-N,I=1):I=0,(0|a)!=(0|(C=C+1|0)););r:if(!H){C=8388607;C:switch(0|F){case 1:C=4194303;break;case 0:break C;default:break r}f[(w=(a<<2)+o|0)+476>>2]=f[w+476>>2]&C}x=x+1|0,2==(0|B)&&(A=1-A,B=2,I&&(A-=Qg(1,d)))}if(0!=A)break;if(I=0,!((0|u)>=(0|(C=a)))){for(;I=f[(o+480|0)+((C=C-1|0)<<2)>>2]|I,(0|C)>(0|u););if(I){for(k=d;k=k-24|0,!f[(o+480|0)+((a=a-1|0)<<2)>>2];);break e}}for(C=1;I=C,C=C+1|0,!f[(o+480|0)+(u-I<<2)>>2];);for(I=a+I|0;;){if(B=a+m|0,a=a+1|0,Q[(o+320|0)+(B<<3)>>3]=f[112432+(v+a<<2)>>2],C=0,A=0,(0|m)>0)for(;A=Q[(C<<3)+p>>3]*Q[(o+320|0)+(B-C<<3)>>3]+A,(0|m)!=(0|(C=C+1|0)););if(Q[(a<<3)+o>>3]=A,!((0|a)<(0|I)))break}a=I}(A=Qg(A,24-k|0))>=16777216?(d=(o+480|0)+(a<<2)|0,C=E(i=5.960464477539063e-8*A)<2147483648?~~i:-2147483648,I=E(A=-16777216*+(0|C)+A)<2147483648?~~A:-2147483648,f[d>>2]=I,a=a+1|0):(C=E(A)<2147483648?~~A:-2147483648,k=d),f[(o+480|0)+(a<<2)>>2]=C}if(A=Qg(1,k),!((0|a)<0)){for(C=a;I=C,Q[(C<<3)+o>>3]=A*+f[(o+480|0)+(C<<2)>>2],C=C-1|0,A*=5.960464477539063e-8,I;);if(w=0,!((0|a)<0))for(k=(0|u)>0?u:0,I=a;;){for(d=k>>>0>>0?k:w,u=a-I|0,C=0,A=0;A=Q[115200+(C<<3)>>3]*Q[(C+I<<3)+o>>3]+A,m=(0|C)!=(0|d),C=C+1|0,m;);if(Q[(o+160|0)+(u<<3)>>3]=A,I=I-1|0,C=(0|a)!=(0|w),w=w+1|0,!C)break}}if(A=0,(0|a)>=0)for(C=a;I=C,C=C-1|0,A+=Q[(o+160|0)+(I<<3)>>3],I;);if(Q[g>>3]=B?-A:A,A=Q[o+160>>3]-A,C=1,(0|a)>0)for(;A+=Q[(o+160|0)+(C<<3)>>3],I=(0|C)!=(0|a),C=C+1|0,I;);Q[g+8>>3]=B?-A:A,V=o+560|0,a=7&x,A=Q[g>>3],(0|r)<0?(Q[e>>3]=-A,Q[e+8>>3]=-Q[g+8>>3],a=0-a|0):(Q[e>>3]=A,Q[e+8>>3]=Q[g+8>>3])}}return V=g+48|0,a}function BA(){var A=0,e=0,g=0,r=0,C=0,b=0,s=0,t=0,n=0,k=0,o=0,B=0,w=0,D=0,u=0,l=0,x=0,d=0,m=0,M=0,v=0,h=0,p=0,Y=0,H=0,N=0,P=0,F=0,y=0,z=0,O=0,Z=0,K=0,W=0,X=0,L=0,T=0,V=0,J=0,R=0,j=0,S=0,q=0,_=0,$=0,AA=0;f[55925]=0,A=f[56772],f[56772]=A+1,g=f[55961],w=f[55922],u=Ig(39.89822670059037*(e=+(0|A))),k=Ig(22.30530784048753*e),e=+(0|w)/50*(+(0|g)/100)*(Ig(14.765485471872028*e)+(u+k))*10,A=E(e)<2147483648?~~e:-2147483648,f[56607]=A+f[56607];A:if(!(f[55923]<=0)){for(;;){for(g=Cr(f[33209],0,1103515245,0),A=U,A=tC(g=g+12345|0,A=g>>>0<12345?A+1|0:A),f[33209]=A,A=8191+((A>>>0)%16383|0)|0,f[55929]=A,z=+(0|A),e=.75*Q[28387]+z,Q[28387]=e,O=(0|(A=f[55924]))>(0|(p=f[55928]))?.5*e:e,X=(C=.033*+f[56652])>0?1-C:1,g=f[56650]<<2,N=1==(0|(H=f[55921]))&&(0|g)>263?263:g,L=111136+((Z=f[56651])<<1)|0,T=111136+((P=f[56658])<<1)|0,V=(0|P)>0,K=f[55918],J=G(K,40),R=f[55925],j=Q[27967],W=f[56607],e=Q[27979],x=Q[27969],t=Q[27970],F=f[56780],M=f[56654],B=Q[27976],D=Q[27972],d=Q[28388],m=Q[28389],w=f[55927],r=Q[28383],o=f[55926],v=f[55956],S=Q[27975],k=Q[28131],b=Q[28130],s=Q[28123],l=Q[28121],n=Q[28122],Y=Q[28120],h=Q[28119],q=Q[28129],_=Q[28128],$=Q[28127],AA=Cg(0*Q[27968]),y=0;;){u=b;e:{g:switch(H-1|0){case 0:d=0,b=l*s,s=n,n=r=b+(h*(d=(0|A)<=2?Q[111312+(A<<3)>>3]:d)+Y*s);break e;case 1:if(r=0,(0|A)>=(0|w)){m=0;break e}b=Q[27965]-Q[27966],Q[27965]=b,r=.028*(m=b+m);break e;case 2:if(!o){v=100,r=0;break e}v=100,g=E(b=+(0|A)/+(0|o)*100)<2147483648?~~b:-2147483648,r=+I[110928+((0|g)%100<<1)>>1],r=e*((+I[110928+((g+1|0)%100<<1)>>1]-r)*(b-+(0|g))+r);break e;case 3:break g;default:break e}o?(v=256,g=E(b=+(0|A)/+(0|o)*256)<2147483648?~~b:-2147483648,r=+I[111344+((0|g)%256<<1)>>1],r=e*((+I[111344+((g+1|0)%256<<1)>>1]-r)*(b-+(0|g))+r)):(v=256,r=0)}if((0|A)>=(0|o)&&((0|W)>0?(A=(0|J)/(0|W)|0,B=0,D=0,D=P>>>0<=87?.001*+I[T>>1]:D,B=Z>>>0<=87?.001*+I[L>>1]*.1:B,p=A>>V,w=(0|(g=(o=(A-1|0)<=(0|N))?A-2|0:N))<=40?40:g,b=+I[111776+((w=o||(0|g)<40?w:N)<<1)>>1],Q[27966]=b,l=b,b=+(0|w),Q[27965]=l*b*.333,M=(0|(g=A-w|0))>(0|M)?M:g,F=0-(g=(0|F)<0?0-M|0:M)|0,h=(b*=.00833)*b,h*=1-(Y=(l=(b=$A(j*+((0|K)/(0|w)|0)))*AA)+l)-(l=b*-b),A=4!=(0|(o=A+g|0))):(f[55930]=0,f[55931]=0,f[55932]=0,f[55933]=0,p=4,B=0,D=0,o=4,A=0),t=(A|=!R)?C:t,x=A?X:x,A=0),A=A+1|0,b=r=q*k+($*r+_*u),k=u,4==(0|(y=y+1|0)))break}if(f[55926]=o,f[55956]=v,f[55924]=A,Q[28383]=r,f[55927]=w,Q[28389]=m,Q[28388]=d,Q[27972]=D,Q[27976]=B,f[55928]=p,f[56654]=M,f[56780]=F,Q[27970]=t,Q[27969]=x,Q[28122]=n,Q[28120]=Y,Q[28119]=h,Q[28123]=s,Q[28121]=l,Q[28131]=k,Q[28130]=r,5==(0|H)&&(r=6e3*((e=+(0|A)/+(0|o))+e+-1),Q[28383]=r),e=r*x+Q[28384]*t,Q[28383]=e,Q[28384]=e,(0|A)<(0|w)&&(e=B*z+e,Q[28383]=e),t=O*Q[27974],u=e*Q[27973]+t,k=0,2!=f[55916]&&(k=Q[27987],n=Q[27986],Q[27987]=n,e=t+e*D,Q[27986]=e,t=Q[28059],s=Q[28058],Q[28059]=s,e=t*Q[28057]+(Q[28055]*(k*Q[27985]+(Q[27983]*e+n*Q[27984]))+s*Q[28056]),Q[28058]=e,k=Q[28049],t=Q[28051],n=Q[28048],s=Q[28047],C=Q[28050],Q[28051]=C,e=k*t+(s*e+n*C),Q[28050]=e,k=Q[28043],t=Q[28041],n=Q[28040],s=Q[28039],C=Q[28042],Q[28043]=C,e=t*k+(s*e+n*C),Q[28042]=e,k=Q[28035],t=Q[28033],n=Q[28032],s=Q[28031],C=Q[28034],Q[28035]=C,e=t*k+(s*e+n*C),Q[28034]=e,k=Q[28027],t=Q[28025],n=Q[28024],s=Q[28023],C=Q[28026],Q[28027]=C,e=t*k+(s*e+n*C),Q[28026]=e,k=Q[28019],t=Q[28017],n=Q[28016],s=Q[28015],C=Q[28018],Q[28019]=C,e=t*k+(s*e+n*C),Q[28018]=e,k=Q[28011],t=Q[28009],n=Q[28008],s=Q[28007],C=Q[28010],Q[28011]=C,e=t*k+(s*e+n*C),Q[28010]=e,k=Q[28003],t=Q[28001],n=Q[28e3],s=Q[27999],C=Q[28002],Q[28003]=C,e=t*k+(s*e+n*C),Q[28002]=e,k=Q[27995],t=Q[27993],n=Q[27992],s=Q[27991],C=Q[27994],Q[27995]=C,k=t*k+(s*e+n*C),Q[27994]=k),e=Q[28385],Q[28385]=u,t=Q[28075],n=Q[28074],Q[28075]=n,s=Q[28067],C=Q[28066],Q[28067]=C,t=t*Q[28073]+(Q[28071]*u+n*Q[28072]),Q[28074]=t,n=s*Q[28065]+(Q[28063]*u+C*Q[28064]),Q[28066]=n,s=Q[28081],C=Q[28083],b=Q[28079],r=Q[28080],B=Q[28082],Q[28083]=B,u=s*C+(b*(e=S*O+u-e)+r*B),Q[28082]=u,s=Q[28091],C=Q[28089],b=Q[28087],r=Q[28088],B=Q[28090],Q[28091]=B,s=C*s+(b*e+r*B),Q[28090]=s,C=Q[28099],b=Q[28097],r=Q[28095],B=Q[28096],D=Q[28098],Q[28099]=D,C=b*C+(r*e+B*D),Q[28098]=C,b=Q[28107],r=Q[28105],B=Q[28103],D=Q[28104],x=Q[28106],Q[28107]=x,b=r*b+(B*e+D*x),Q[28106]=b,r=Q[28115],B=Q[28113],D=Q[28111],x=Q[28112],d=Q[28114],Q[28115]=d,r=B*r+(D*e+x*d),Q[28114]=r,B=Q[28139],D=Q[28137],x=Q[28136],d=Q[28135],m=Q[27971],l=Q[28138],Q[28139]=l,e=D*B+(d*(e*m-(r-(b-(C-(s-(u-(k+t+n)))))))+x*l),Q[28138]=e,e=Q[27977]*(e*+f[50779]),r=+(0|(A=E(e)<2147483648?~~e:-2147483648)),(0|(A=f[50776]))>1],f[50755])>>8,A=E(r)<2147483648?~~r:-2147483648,(0|g)>=5500&&(f[51293]=0),g=f[51290],f[51290]=g+1,A=(0|(A=(0|(A=A+o|0))<=-32768?-32768:A))>=32767?32767:A,a[0|g]=A,g=f[51290],f[51290]=g+1,a[0|g]=A>>>8,o=(g=f[51292])+1|0,f[51292]=o,I[205184+(g<<1)>>1]=A,(0|o)>=5500&&(f[51292]=0),o=1,f[56606]=f[56606]+1,c[54046]>>0)break A;if(A=f[55925]+1|0,f[55925]=A,!((0|A)>2]=e,k=t+55|0,n=t+56|0;A:{e:{g:{r:{C:for(;;){if(c=e,(2147483647^l)<(0|o))break r;l=o+l|0;a:{I:{f:{if(B=i[0|(o=c)])for(;;){i:{b:if(e=255&B){if(37!=(0|e))break i;for(B=o;;){if(37!=i[B+1|0]){e=B;break b}if(o=o+1|0,E=i[B+2|0],B=e=B+2|0,37!=(0|E))break}}else e=o;if((0|(o=o-c|0))>(0|(p=2147483647^l)))break r;if(A&&kC(A,c,o),o)continue C;f[t+76>>2]=e,o=e+1|0,x=-1,36!=i[e+2|0]|a[e+1|0]-48>>>0>=10||(x=a[e+1|0]-48|0,M=1,o=e+3|0),f[t+76>>2]=o,D=0;b:if((e=(B=a[0|o])-32|0)>>>0>31)u=o;else if(u=o,75913&(e=1<>2]=u,D|=e,(e=(B=a[o+1|0])-32|0)>>>0>=32)break b;if(o=u,!(75913&(e=1<>2]}else{if(36!=i[u+2|0]|a[u+1|0]-48>>>0>=10){if(M)break f;if(B=u+1|0,!A){f[t+76>>2]=B,M=0,d=0;break b}e=f[g>>2],f[g>>2]=e+4,M=0,e=f[e>>2]}else f[((a[u+1|0]<<2)+C|0)-192>>2]=10,B=u+3|0,M=1,e=f[((a[u+1|0]<<3)+r|0)-384>>2];if(f[t+76>>2]=B,d=e,(0|e)>=0)break b;d=0-d|0,D|=8192}if(o=0,w=-1,46==i[0|B])if(42!=i[B+1|0])f[t+76>>2]=B+1,w=$g(t+76|0),e=f[t+76>>2],h=1;else{if(36!=i[B+3|0]|a[B+2|0]-48>>>0>=10){if(M)break f;e=B+2|0,w=0,A&&(B=f[g>>2],f[g>>2]=B+4,w=f[B>>2])}else f[((a[B+2|0]<<2)+C|0)-192>>2]=10,e=B+4|0,w=f[((a[B+2|0]<<3)+r|0)-384>>2];f[t+76>>2]=e,h=~w>>>31|0}else e=B,h=0;for(;;){if(m=o,u=28,E=e,(o=a[0|e])-123>>>0<4294967238)break g;if(e=E+1|0,!((o=i[123983+(o+G(m,58)|0)|0])-1>>>0<8))break}f[t+76>>2]=e;b:{s:{if(27!=(0|o)){if(!o)break g;if((0|x)>=0){f[(x<<2)+C>>2]=o,o=f[(B=(x<<3)+r|0)+4>>2],f[t+64>>2]=f[B>>2],f[t+68>>2]=o;break s}if(!A)break a;Ge(t- -64|0,o,g,s);break b}if((0|x)>=0)break g}if(o=0,!A)continue C}B=-65537&D,D=8192&D?B:D,x=0,v=84065,u=n;b:{s:{t:{n:{k:{o:{B:{c:{Q:{G:{w:{E:{D:{u:{l:{x:switch(o=a[0|E],(o=m&&3==(15&o)?-33&o:o)-88|0){case 11:break b;case 9:case 13:case 14:case 15:break s;case 27:break B;case 12:case 17:break G;case 23:break w;case 0:case 32:break E;case 24:break D;case 22:break u;case 29:break l;case 1:case 2:case 3:case 4:case 5:case 6:case 7:case 8:case 10:case 16:case 18:case 19:case 20:case 21:case 25:case 26:case 28:case 30:case 31:break I;default:break x}x:switch(o-65|0){case 0:case 4:case 5:case 6:break s;case 2:break k;case 1:case 3:break I;default:break x}if(83==(0|o))break o;break I}B=f[t+64>>2],E=f[t+68>>2],v=84065;break Q}o=0;u:switch(255&m){case 0:case 1:case 6:f[f[t+64>>2]>>2]=l;continue C;case 2:c=f[t+64>>2],f[c>>2]=l,f[c+4>>2]=l>>31;continue C;case 3:I[f[t+64>>2]>>1]=l;continue C;case 4:a[f[t+64>>2]]=l;continue C;case 7:break u;default:continue C}c=f[t+64>>2],f[c>>2]=l,f[c+4>>2]=l>>31;continue C}w=w>>>0<=8?8:w,D|=8,o=120}if(c=n,Y=32&o,(B=f[t+64>>2])|(E=f[t+68>>2]))for(;a[0|(c=c-1|0)]=Y|i[124512+(15&B)|0],H=!E&B>>>0>15|!!(0|E),m=E,E=E>>>4|0,B=(15&m)<<28|B>>>4,H;);if(!(f[t+64>>2]|f[t+68>>2])|!(8&D))break c;v=84065+(o>>>4|0)|0,x=2;break c}if(o=n,E=c=f[t+68>>2],c|(B=f[t+64>>2]))for(;a[0|(o=o-1|0)]=7&B|48,m=!E&B>>>0>7|!!(0|E),E=(c=E)>>>3|0,B=(7&c)<<29|B>>>3,m;);if(c=o,!(8&D))break c;w=(0|(o=n-c|0))<(0|w)?w:o+1|0;break c}B=f[t+64>>2],E=o=f[t+68>>2],(0|o)<0?(E=c=0-(E+!!(0|B)|0)|0,B=0-B|0,f[t+64>>2]=B,f[t+68>>2]=c,x=1,v=84065):2048&D?(x=1,v=84066):v=(x=1&D)?84067:84065}c=Ug(B,E,n)}if((0|w)<0&&h)break r;if(D=h?-65537&D:D,!(w|!!((o=f[t+64>>2])|(B=f[t+68>>2])))){c=n,w=0;break I}w=(0|(o=!(o|B)+(n-c|0)|0))<(0|w)?w:o;break I}if(u=(o=(o=qe(c=(o=f[t+64>>2])||84639,0,E=w>>>0>=2147483647?2147483647:w))?o-c|0:E)+c|0,(0|w)>=0){D=B,w=o;break I}if(D=B,w=o,i[0|u])break r;break I}if(w){B=f[t+64>>2];break n}o=0,br(A,32,d,0,D);break t}f[t+12>>2]=0,f[t+8>>2]=f[t+64>>2],B=t+8|0,f[t+64>>2]=B,w=-1}o=0;n:{for(;;){if(!(c=f[B>>2]))break n;if(!((c=(0|(E=je(t+4|0,c)))<0)|E>>>0>w-o>>>0)){if(B=B+4|0,w>>>0>(o=o+E|0)>>>0)continue;break n}break}if(c)break e}if(u=61,(0|o)<0)break g;if(br(A,32,d,o,D),o)for(u=0,B=f[t+64>>2];;){if(!(c=f[B>>2]))break t;if((u=(c=je(t+4|0,c))+u|0)>>>0>o>>>0)break t;if(kC(A,t+4|0,c),B=B+4|0,!(o>>>0>u>>>0))break}else o=0}br(A,32,d,o,8192^D),o=(0|o)<(0|d)?d:o;continue C}if((0|w)<0&&h)break r;if(u=61,(0|(o=0|HC[0|b](A,Q[t+64>>3],d,w,D,o)))>=0)continue C;break g}a[t+55|0]=f[t+64>>2],w=1,c=k,D=B;break I}B=i[o+1|0],o=o+1|0}if(A)break A;if(!M)break a;for(o=1;;){if(A=f[(o<<2)+C>>2]){if(Ge((o<<3)+r|0,A,g,s),l=1,10!=(0|(o=o+1|0)))continue;break A}break}if(l=1,o>>>0>=10)break A;for(;;){if(f[(o<<2)+C>>2])break f;if(10==(0|(o=o+1|0)))break}break A}u=28;break g}if((0|(B=(0|w)>(0|(E=u-c|0))?w:E))>(2147483647^x))break r;if(u=61,(0|p)<(0|(o=(0|(w=B+x|0))<(0|d)?d:w)))break g;br(A,32,o,w,D),kC(A,v,x),br(A,48,o,w,65536^D),br(A,48,B,E,0),kC(A,c,E),br(A,32,o,w,8192^D);continue}break}l=0;break A}u=61}f[56798]=u}l=-1}return V=t+80|0,l}function QA(A,e,g,r,C,a,I,i,b){var s,t,n,k=0,o=0,B=0,c=0,Q=0,G=0,w=0,E=0,u=0,l=0,x=0,d=0,m=0,M=0,v=0,h=0,p=0,Y=0,H=0,N=0,P=0,F=0,y=0,z=0,O=0,Z=0,K=0,W=0,X=0,L=0,T=0;V=s=V-96|0,x=65535&b,Q=-2147483648&(C^b),h=w=65535&C;A:{if(!((t=b>>>16&32767)-32767>>>0>4294934529&(n=C>>>16&32767)-32767>>>0>=4294934530)){if(k=r,!(!r&2147418112==(0|(E=u=2147483647&C))?!(e|g):E>>>0<2147418112)){B=r,Q=32768|C;break A}if(!(!(C=i)&2147418112==(0|(G=u=2147483647&b))?!(a|I):G>>>0<2147418112)){B=i,Q=32768|b,e=a,g=I;break A}if(!(e|k|2147418112^E|g)){if(!(C|a|I|G)){Q=2147450880,e=0,g=0;break A}Q|=2147418112,e=0,g=0;break A}if(!(C|a|2147418112^G|I)){if(C=e|k,r=g|E,e=0,g=0,!(r|C)){Q=2147450880;break A}Q|=2147418112;break A}if(!(e|k|g|E)){e=0,g=0;break A}if(!(C|a|I|G)){e=0,g=0;break A}65535==(0|E)|E>>>0<65535&&(u=(k=!(r|w))<<6,C=D(b=k?e:r)+32|0,Ve(s+80|0,e,g,r,w,(b=u+(32==(0|(b=D(k?g:w)))?C:b)|0)-15|0),l=16-b|0,r=f[s+88>>2],h=f[s+92>>2],g=f[s+84>>2],e=f[s+80>>2]),G>>>0>65535||(w=(b=!(i|x))<<6,k=D(C=b?a:i)+32|0,Ve(s- -64|0,a,I,i,x,(C=w+(32==(0|(C=D(b?I:x)))?k:C)|0)-15|0),l=16+(l-C|0)|0,i=f[s+72>>2],x=f[s+76>>2],a=f[s+64>>2],I=f[s+68>>2])}if(C=a,a=I<<15|a>>>17,Z=g,u=Cr(d=-32768&(b=C<<15),C=0,g,0),v=C=U,K=a,E=e,e=Cr(a,0,e,0),a=U+C|0,g=e>>>0>(b=e+u|0)>>>0?a+1|0:a,k=0,e=Cr(E,o,d,o),C=(a=b)+U|0,w=C=e>>>0>(G=k+e|0)>>>0?C+1|0:C,W=(0|a)==(0|C)&k>>>0>G>>>0|C>>>0>>0,X=r,m=Cr(d,o,r,0),L=U,e=Cr(Z,o,K,o),k=U+L|0,k=e>>>0>(M=e+m|0)>>>0?k+1|0:k,e=x<<15|i>>>17,r=Cr(p=i<<15|I>>>17,0,E,o),a=U+k|0,H=a=r>>>0>(Y=r+M|0)>>>0?a+1|0:a,a=(r=(0|g)==(0|v)&b>>>0>>0|g>>>0>>0)+a|0,x=a=g>>>0>(N=g+Y|0)>>>0?a+1|0:a,I=N,g=a,h=Cr(d,o,P=65536|h,c),T=U,r=Cr(X,B,K,o),C=U+T|0,b=C=r>>>0>(F=r+h|0)>>>0?C+1|0:C,e=Cr(y=-2147483648|e,0,E,o),a=U+C|0,a=e>>>0>(z=e+F|0)>>>0?a+1|0:a,e=Cr(p,B,Z,o),O=a,a=a+U|0,u=e>>>0>(v=e+z|0)>>>0?a+1|0:a,C=g+v|0,a=d=(e=0)>>>0>(E=e+I|0)>>>0?C+1|0:C,g=(e=E+W|0)>>>0>>0?a+1|0:a,l=((n+t|0)+l|0)-16383|0,r=Cr(y,B,Z,o),i=U,C=Cr(P,B,K,o),a=U+i|0,c=(0|i)==(0|(a=C>>>0>(I=C+r|0)>>>0?a+1|0:a))&r>>>0>I>>>0|a>>>0>>0,i=a,C=Cr(p,B,X,B),a=U+a|0,C=a=(r=C+I|0)>>>0>>0?a+1|0:a,I=(0|a)==(0|i)&r>>>0>>0|a>>>0>>0,a=0,a=(i=I)>>>0>(I=I+c|0)>>>0?1:a,i=I,I=Cr(y,B,P,B),a=U+a|0,W=i=i+I|0,I=I>>>0>i>>>0?a+1|0:a,i=r,o=C,C=(0|k)==(0|L)&m>>>0>M>>>0|k>>>0>>0,a=0,C=((c=k=(0|k)==(0|H)&M>>>0>Y>>>0|k>>>0>H>>>0)>>>0>(k=C+k|0)>>>0?1:a)+o|0,a=I,c=C=(r=r+k|0)>>>0>>0?C+1|0:C,m=r,C=r=(0|C)==(0|o)&r>>>0>>0|C>>>0>>0,k=r=r+W|0,I=a=C>>>0>r>>>0?a+1|0:a,C=Cr(p,B,P,B),o=U,r=Cr(y,B,X,B),a=U+o|0,r=a=r>>>0>(i=r+C|0)>>>0?a+1|0:a,C=(a=(0|o)==(0|a)&C>>>0>i>>>0|a>>>0>>0)+I|0,I=C=r>>>0>(M=r+k|0)>>>0?C+1|0:C,a=i+c|0,C=a=(r=(C=0)+m|0)>>>0>>0?a+1|0:a,i=(0|c)==(0|a)&r>>>0>>0|a>>>0>>0,a=I,a=(I=i+(o=M)|0)>>>0>>0?a+1|0:a,m=I,i=r,k=C,C=(r=(r=(r=(0|b)==(0|T)&h>>>0>F>>>0|b>>>0>>0)+(b=(0|b)==(0|O)&F>>>0>z>>>0|b>>>0>O>>>0)|0)+(C=(0|u)==(0|O)&v>>>0>>0|u>>>0>>0)|0)+k|0,a=I=a,k=I=(i=(0|(C=(r=b=(c=u)+i|0)>>>0>>0?C+1|0:C))==(0|k)&i>>>0>r>>>0|C>>>0>>0)+m|0,I=a=i>>>0>I>>>0?a+1|0:a,i=r,a=0,b=C,C=C+((o=c=(0|x)==(0|d)&E>>>0>>0|x>>>0>d>>>0)>>>0>(c=c+((0|x)==(0|H)&Y>>>0>N>>>0|x>>>0>>0)|0)>>>0?1:a)|0,a=I,b=a=(I=i=(0|(C=(r=r+c|0)>>>0>>0?C+1|0:C))==(0|b)&r>>>0>>0|C>>>0>>0)>>>0>(i=i+k|0)>>>0?a+1|0:a,65536&a?l=l+1|0:(k=w>>>31|0,a=b<<1|i>>>31,i=i<<1|C>>>31,b=a,a=C<<1|r>>>31,r=r<<1|g>>>31,C=a,a=w<<1|G>>>31,G<<=1,w=a,a=g<<1|e>>>31,e=e<<1|k,g=a|(I=0)),(0|l)>=32767)Q|=2147418112,e=0,g=0;else{e:{if((0|l)<=0){if((I=1-l|0)>>>0<=127){Ve(s+48|0,G,w,e,g,a=l+127|0),Ve(s+32|0,r,C,i,b,a),Ke(s+16|0,G,w,e,g,I),Ke(s,r,C,i,b,I),G=f[s+32>>2]|f[s+16>>2]|!!(f[s+48>>2]|f[s+56>>2]|f[s+52>>2]|f[s+60>>2]),w=f[s+36>>2]|f[s+20>>2],e=f[s+40>>2]|f[s+24>>2],g=f[s+44>>2]|f[s+28>>2],r=f[s>>2],C=f[s+4>>2],I=f[s+8>>2],a=f[s+12>>2];break e}e=0,g=0;break A}I=i,a=65535&b|l<<16}B|=I,Q|=a,(!e&-2147483648==(0|g)?!(G|w):(0|g)>0|(0|g)>=0)?e|G|-2147483648^g|w?(e=r,g=C):(k=Q,Q=(r=(0|(a=C))==(0|(g=(g=e=1&r)>>>0>(e=e+r|0)>>>0?a+1|0:a))&e>>>0>>0|g>>>0>>0)>>>0>(B=r+B|0)>>>0?k+1|0:k):(r=(0|C)==(0|(g=(e=r+1|0)?C:C+1|0))&e>>>0>>0|g>>>0>>0,C=Q,Q=(B=r+B|0)>>>0>>0?C+1|0:C)}}f[A>>2]=e,f[A+4>>2]=g,f[A+8>>2]=B,f[A+12>>2]=Q,V=s+96|0}function GA(A,e,g,r,C,I,b){var s,t=0,n=0,k=0,o=0,B=0,c=0,Q=0,G=0,w=0,E=0,D=0,u=0,l=0,x=0,d=0;V=s=V-480|0,f[s+476>>2]=0,f[s+456>>2]=0,f[s+460>>2]=0,f[s+448>>2]=0,f[s+452>>2]=0,f[s+440>>2]=0,f[s+444>>2]=0,f[s+432>>2]=0,f[s+436>>2]=0,t=0;A:if(f[A+684>>2]){for(Q=b?f[b>>2]:Q;k=i[e+t|0],a[(s+112|0)+t|0]=k,n=t+1|0,k&&(k=t>>>0<158,t=n,k););if(a[n+(s+112|0)|0]=0,!((E=268435456&I)|!(8&f[47197]))){n=0;e:if(223&(k=i[0|e]))for(t=0;;){if(a[(s+272|0)+t|0]=k,!(223&(k=i[(n=t+1|0)+e|0])))break e;if(o=t>>>0<118,t=n,!o)break}a[(t=s+272|0)+n|0]=0,f[s+48>>2]=t,eC(f[47195],(0|I)>=0?87019:86877,s+48|0)}f[s+464>>2]=e,f[A+8208>>2]=0,f[A+8212>>2]=0,C&&(a[0|C]=0);e:{g:if(223&(t=i[0|e]))for(D=536870912&I,u=4096&I,l=s+105|0,n=e,k=0;;){o=Te(s+476|0,n),w=!!(0|kg(f[s+476>>2]))+w|0,c=i[(t=(B=255&t)+A|0)+7668|0];r:if(!((G=f[s+476>>2])-48>>>0<10|G-2406>>>0<10)|(w?i[A+170|0]:0)){C:if((k=f[s+476>>2]-f[A+600>>2]|0)>>>0>127||!(k=f[6192+((k<<2)+A|0)>>2])){if(c){for(G=5168+((B<<2)+A|0)|0,c=c+(t=i[t+7924|0])|0,x=B|i[n+1|0]<<8,k=0;f[(n=(t<<2)+A|0)+7184>>2]==(0|x)&&(f[s+472>>2]=f[s+464>>2],rA(A,s+472|0,e,2,f[n+6704>>2],s+432|0,I,Q),(0|(n=f[s+432>>2]))>0&&(n=n+35|0,f[s+432>>2]=n),k=1,rA(A,s+464|0,e,1,f[G>>2],s+448|0,I,Q),f[s+448>>2]>(0|n)||(n=f[s+444>>2],f[s+456>>2]=f[s+440>>2],f[s+460>>2]=n,n=f[s+436>>2],f[s+448>>2]=f[s+432>>2],f[s+452>>2]=n,f[s+464>>2]=f[s+472>>2])),c>>>0>(t=t+1|0)>>>0;);if(k)break C}a:{I:{if(!(t=f[5168+((B<<2)+A|0)>>2])){if(rA(A,s+464|0,e,0,f[A+5168>>2],s+448|0,I,Q),f[s+448>>2])break a;if(16&i[188808])break I;if(k=Te(s+468|0,B=(n=f[s+464>>2])-1|0),t=f[s+468>>2],!(f[A+600>>2]<=0|(0|t)>577)){if(Zr(t)){f[s+32>>2]=21,dg(g,87049,s+32|0);break e}t=f[s+468>>2]}if(57384==(0|t)&&((0|(c=f[A+92>>2]))<=f[47352]||(f[47352]=c)),CC(t)&&((0|(t=f[A+72>>2]))<=f[47352]||(f[47352]=t)),!((c=(t=f[s+468>>2])-192|0)>>>0>413)&&(c=i[c+94240|0])&&(k=k-1|0,!(32==i[n-2|0]&32==i[k+n|0]))){for(f[s+472>>2]=B,a[0|B]=c;o=i[(t=n)+k|0],a[0|t]=o,n=t+1|0,32!=(0|o););if((0|k)>0&&ue(t,32,k),f[A+24>>2]&&!((0|Fr(94222,f[s+468>>2]))<=0)){f[s+464>>2]=B,k=0;break r}k=0,a[0|g]=0,f[s+464>>2]=e,f[A+8208>>2]=0,f[A+8212>>2]=0;break r}if(!(t=ae(t)))break I;if((0|(n=f[t+4>>2]))==f[A+600>>2])break I;if((0|n)==f[A+188>>2]){f[s+4>>2]=lr(s- -64|0,f[A+192>>2]),f[s>>2]=21,dg(g,87218,s);break e}if(!(4&i[t+16|0]))break I;f[s+20>>2]=lr(s- -64|0,f[t+12>>2]),f[s+16>>2]=21,dg(g,87218,s+16|0);break e}if(rA(A,s+464|0,e,1,t,s+448|0,I,Q),f[s+448>>2])break a}I:if(!((t=f[s+476>>2])-768>>>0<112)){if(kg(t)){if(a[(o+f[s+464>>2]|0)-1|0]<33&(0|w)<=1)break I;if(a[0|g]=0,!b)break g;f[b>>2]=4096|f[b>>2];break g}De(A,f[s+476>>2],-1,s+272|0,0),i[s+272|0]&&(f[s+448>>2]=1,f[s+452>>2]=s+272)}f[s+464>>2]=(o+f[s+464>>2]|0)-1;break C}f[A+288>>2]=0}else rA(A,s+464|0,e,o,k,s+448|0,I,Q);if(n=(t=f[s+452>>2])||86135,f[s+452>>2]=n,k=0,!(f[s+448>>2]<=0)){if(t=1|f[s+456>>2],(0|I)<0)break A;if(!(21!=i[0|n]|u)){rg(g,n);break e}if(!(!(8&f[47197])|E))C:if(t=f[47195],(0|(o=f[t+76>>2]))>=0&(!o|f[56823]!=(-1073741825&o)))B=f[(o=t+76|0)>>2],f[o>>2]=B||1073741823,10==f[t+80>>2]||(0|(B=f[t+20>>2]))==f[t+16>>2]?Kg(t):(f[t+20>>2]=B+1,a[0|B]=10),f[o>>2]=0;else{if(10!=f[t+80>>2]&&(0|(o=f[t+20>>2]))!=f[t+16>>2]){f[t+20>>2]=o+1,a[0|o]=10;break C}Kg(t)}if(o=-32769&(t=f[s+456>>2]),f[s+456>>2]=o,!(!C|!o|(1024&t?D:0))){A=f[s+464>>2],rg(C,n),t=o|(d=(g=A)-_A(e,A=s+112|0,Lg(A))|0,1024==(1151&t)?d:0);break A}(t=f[s+460>>2])&&(a[0|t]=69),ag(A,g,r,n)}}else a[s+104|0]=95,_A(l,n,o),t=1,a[105+(s+o|0)|0]=0,Mg(A,s+104|0,s- -64|0),k-1>>>0<=4294967293&&(t=Lg(t=s- -64|0)+t|0,a[0|t]=11,a[t+1|0]=0,t=0),ag(A,g,r,s- -64|0),f[s+464>>2]=n+o,k=t;if(n=f[s+464>>2],!(223&(t=i[0|n])))break}_A(e,A=s+112|0,Lg(A))}t=0}return V=s+480|0,t}function wA(A,e){var g,r=0,C=0,I=0,b=0,s=0,t=0,n=0,k=0,o=0,B=0,c=0,Q=0,w=0,E=0,D=0,u=0,l=0,x=0,d=0,m=0,M=0,v=0;V=g=V-2976|0,f[e>>2]=1,r=f[A+20>>2],f[(s=g+2960|0)>>2]=f[A+16>>2],f[s+4>>2]=r,r=f[A+12>>2],f[(s=g+2952|0)>>2]=f[A+8>>2],f[s+4>>2]=r,r=f[A+4>>2],f[g+2944>>2]=f[A>>2],f[g+2948>>2]=r,f[50303]||le();A:if(A=f[g+2948>>2],i[0|A]&&A||((A=f[g+2944>>2])||(A=(A=f[g+2952>>2])||85055,f[g+2944>>2]=A),oC(r=g+80|0,A,60),Ag(r,0),!(I=He(201216,r))||(f[g+2948>>2]=f[I+4>>2]+1,i[g+2958|0]|i[g+2956|0]|i[g+2957|0]))){E=g+1536|0,V=Q=V-336|0;e:if(!(!(A=f[(w=g+2944|0)+4>>2])|!i[0|A])){if((0|(D=Lg(A)))>=0){for(r=D>>>0>=79?79:D,k=1;A=ar(a[f[w+4>>2]+C|0]),a[(Q+256|0)+C|0]=A,k=(45==(255&A))+k|0,A=(0|r)!=(0|C),C=C+1|0,A;);if(1!=(0|k))break e}k=1}if((0|(o=f[50303]))<=0)f[E>>2]=0,A=0;else{for(s=(0|k)>=0;;){n=f[201216+(x<<2)>>2];e:if(pg(f[n+8>>2],88032,3)){if((A=f[w+4>>2])&&pg(A,91687,3)){if(s){A=100;g:if(k){if(r=0,C=f[n+4>>2],!(u=i[0|C])){if(!pg(Q+256|0,90013,9))break g;break e}for(;;){for(d=1,t=C+1|0,l=1,m=0,C=0;(0|C)<(0|D)&&45!=(0|(A=a[(Q+256|0)+C|0]))||(A=0),m=((B=45==(0|(c=i[C+t|0])))&!!(0|(l=(B?0:c)<<24>>24==(0|A)?l:0)))+m|0,C=C+1|0,d=B+d|0,c;);if(C=C+t|0,(B=l+m|0)&&(r=(0|(A=G((t=(0|(A=k-B|0))<=0?5:5-A|0)-((0|(A=d-B|0))>0?A:0)|0,100)-(u<<24>>24<<1)|0))>(0|r)?A:r),!(u=i[0|C]))break}if(!(A=r))break e}(r=f[w>>2])&&(A=Qr(r,f[n>>2])?Qr(r,f[n+8>>2])?A:A+400|0:A+500|0),((C=i[w+12|0])-1&255)>>>0>1||((r=i[n+12|0])-1&255)>>>0>1||(A=(0|r)!=(0|C)?A-50|0:A+50|0),C=i[w+13|0],A=2!=i[n+12|0]|C>>>0>12?A:i[n+13|0]>12?A+5|0:A,(r=i[n+13|0])&&((r=((C?G(C,100):3e3)>>>0)/(r>>>0)|0)>>>0<=99&&(r=1e4/(r>>>0)|0),A=(t=A)+((A=5-(((r-100&65535)>>>0)/10|0)|0)>>31&A)|0,A=C?A+10|0:A),A=(0|A)<=1?1:A}else{if(pg(f[n+8>>2],Q+256|0,D))break e;A=100}f[E+(b<<2)>>2]=n,f[n+16>>2]=A}else f[E+(b<<2)>>2]=n;b=b+1|0}if((0|o)==(0|(x=x+1|0)))break}f[E+(b<<2)>>2]=0,A=0,b&&(ee(E,b,8),A=b)}if(V=Q+336|0,k=A,A||(f[e>>2]=0,A=He(201216,85055),f[g+1536>>2]=A,k=!!(0|A)),e=i[g+2957|0],s=2,2!=(0|(A=i[g+2956|0]))&&(s=2,(e-1&255)>>>0<12||(v=1!=(0|A),s=1==(0|A))),o=(b=f[132136+(s<<2)>>2])+(c=e>>>0<60)|0,A=0,(0|k)>0)for(r=0;;){I=f[(g+1536|0)+(M<<2)>>2];e:{g:{r:{if(v){if(c)break g;if(e=0,r)break g}else{if(e=i[I+12|0],r|c)break r;e=(0|e)!=(0|s)}if(C=0,e|i[I+13|0]<60)break e;break g}if((0|e)!=(0|s)){C=r;break e}}f[(g+80|0)+(r<<2)>>2]=I,C=r+1|0}e:if(i[I+15|0]){if(B=0,e=A,r=C,!((0|A)>11))for(;;){if((C=i[0|o])||(o=b,C=i[0|b]),A=f[I+12>>2],t=G(e,24)+202624|0,f[t+8>>2]=f[I+8>>2],f[t+12>>2]=A,A=f[I+4>>2],f[t>>2]=f[I>>2],f[t+4>>2]=A,A=f[I+20>>2],f[t+16>>2]=f[I+16>>2],f[t+20>>2]=A,a[t+14|0]=C,f[(g+80|0)+(r<<2)>>2]=t,o=o+1|0,r=r+1|0,A=e+1|0,(B=B+1|0)>>>0>=i[I+15|0])break e;if(C=(0|e)<11,e=A,!C)break}}else r=C;if((0|(M=M+1|0))==(0|k))break}else{if(!I)break A;r=0}e:if(!(!(C=i[0|o])|(0|A)>=12))for(;;){if(e=f[I+12>>2],b=G(A,24)+202624|0,f[b+8>>2]=f[I+8>>2],f[b+12>>2]=e,e=f[I+4>>2],f[b>>2]=f[I>>2],f[b+4>>2]=e,e=f[I+20>>2],f[b+16>>2]=f[I+16>>2],f[b+20>>2]=e,a[b+14|0]=C,f[(g+80|0)+(r<<2)>>2]=b,r=r+1|0,!(C=i[0|(o=o+1|0)]))break e;if(e=(0|A)<11,A=A+1|0,!e)break}r?(A=f[(g+80|0)+(i[g+2958|0]%(0|r)<<2)>>2],(e=i[A+14|0])?(a[202976]=0,f[g+48>>2]=47,dg(g+2971|0,91351,g+48|0),a[g+2971|0]=0,e>>>0<=9?(f[g+20>>2]=e,f[g+16>>2]=g+2971,dg(202976,91378,g+16|0)):(f[g+36>>2]=e-10,f[g+32>>2]=g+2971,dg(202976,91503,g+32|0)),A=f[A+8>>2],f[g+4>>2]=202976,f[g>>2]=A,A=202912,dg(202912,87760,g)):A=f[A+8>>2]):A=0}else{if(A=f[I+8>>2],!i[202976])break A;f[g+64>>2]=A,f[g+68>>2]=202976,A=202912,dg(202912,87760,g- -64|0)}return V=g+2976|0,A}function EA(A,e,g,r,C){var I,b,s=0,t=0,n=0,k=0,o=0,B=0,c=0;V=I=V-464|0,a[I+432|0]=0,a[I+368|0]=0,a[I+304|0]=0,a[I+292|0]=0,o=(0|e)/10|0,s=f[33273];A:{if(!(b=2&r)|2!=f[33272]){c=32&r?113:111,k=1&r,B=e-G(o,10)|0;e:{g:{r:{C:{a:{I:{f:{i:if(i[0|s])s=0;else{b:{if(8&r){if(f[I+288>>2]=e,dg(t=I+452|0,91198,I+288|0),s=Mg(A,t,I+304|0)){t=0;break i}f[I+272>>2]=e,dg(t=I+452|0,91314,I+272|0),s=Mg(A,t,I+304|0),t=0}else{if(!k)break b;if(n=rg(I+432|0,133104),4&r){if(f[I+260>>2]=c,f[I+256>>2]=e,dg(t=I+452|0,91324,I+256|0),s=Mg(A,t,I+304|0),i[133116]&&s)break f;if(t=s,s)break i}f[I+244>>2]=c,f[I+240>>2]=e,dg(t=I+452|0,91384,I+240|0),t=s=Mg(A,t,I+304|0)}if(s)break i}b:{if(b){if(!(1&a[133096]))break b;f[I+208>>2]=e,dg(s=I+452|0,91498,I+208|0),s=Mg(A,s,I+304|0)}else n=f[A+108>>2],f[I+224>>2]=e,dg(s=I+452|0,(0|g)>=2?91700:(262144&n)>>>18|0?91534:91700,I+224|0),s=Mg(A,s,I+304|0);if(s)break i}!k|!(32&i[A+109|0])?(f[I+192>>2]=e,dg(s=I+452|0,91766,I+192|0),s=Mg(A,s,I+304|0)):s=0}if(!(16&r)|(0|e)>9)break a;s=t;break I}if(rg(n,133116),!(16&r)|(0|e)>9)break C}Mg(A,88875,I+368|0);break g}if(!s)break r;s=t}a[I+368|0]=0;break g}r:if(k&&(f[I+180>>2]=c,f[I+176>>2]=o,dg(s=I+452|0,91846,I+176|0),Mg(A,s,I+368|0))){if(t=1,!B|!(16&i[A+109|0]))break r;mC(I+368|0,133104)}else t||(f[I+160>>2]=o,dg(t=I+452|0,512&r?91936:92016,I+160|0),Mg(A,t,I+368|0),t=0);if(s=B,i[I+368|0]||(s=B,16&i[A+106|0]&&(f[I+144>>2]=254&o,dg(s=I+452|0,92016,I+144|0),Mg(A,s,I+368|0),s=(0|e)%20|0)),a[I+304|0]=0,o=s,(0|s)<=0)s=t;else{if(b&&(s=f[33273],i[0|s])){rg(I+304|0,s),a[I+432|0]=0,n=k;break e}if(n=0,8&r&&(f[I+128>>2]=o,dg(r=I+452|0,91314,I+128|0),n=Mg(A,r,I+304|0)),!k|16&i[A+104|0]||(f[I+116>>2]=c,f[I+112>>2]=o,dg(r=I+452|0,91384,I+112|0),t=(n=Mg(A,r,I+304|0))?1:t),s=t,!n){r:{if(!b|!(1&f[33274])){if(!(16&i[A+104|0])&&b)break r;t=f[A+108>>2],f[I+96>>2]=o,dg(r=I+452|0,(0|g)>=2?91700:(262144&t)>>>18|0?91534:91700,I+96|0),g=Mg(A,r,I+304|0)}else f[I+80>>2]=o,dg(g=I+452|0,91498,I+80|0),g=Mg(A,g,I+304|0);if(g)break g}f[I+64>>2]=o,dg(g=I+452|0,91766,I- -64|0),Mg(A,g,I+304|0)}}}n=k,i[I+432|0]|s|!k||((0|e)<20|(16&i[A+104|0]?0:B)||(Mg(A,92162,I+432|0),n=1,!i[I+432|0]))&&(Mg(A,92205,I+432|0),n=1)}if(!(!(g=a[I+304|0])|!(48&(e=f[A+104>>2]))|!i[I+368|0])){if(Mg(A,90824,I+292|0),!n|!(8&i[A+109|0])||(a[I+292|0]=0),16&i[A+104|0]){f[I+28>>2]=I+432,f[I+24>>2]=I+368,f[I+20>>2]=I+292,f[I+16>>2]=I+304,dg(C,91059,I+16|0),r=1;break A}f[I+12>>2]=I+432,f[I+8>>2]=I+304,f[I+4>>2]=I+292,f[I>>2]=I+368,dg(C,91059,I),r=1;break A}512&e&&(!g|(0|(e=Lg(I+368|0)-1|0))<0||(s=2!=i[f[144464+(a[0|(e=e+(I+368|0)|0)]<<2)>>2]+11|0],1==(0|(r=i[f[144464+(g<<2)>>2]+11|0]))&&(r=i[f[144464+(a[I+305|0]<<2)>>2]+11|0]),s|2!=(255&r)||(a[0|e]=0))),!(8&i[A+110|0])|!i[I+432|0]?(f[I+56>>2]=I+432,f[I+52>>2]=I+304,f[I+48>>2]=I+368,dg(C,92282,I+48|0)):(f[I+36>>2]=I+304,f[I+32>>2]=I+368,(0|(e=dg(C,90368,I+32|0)))>0&&(e=2==i[f[144464+(i[(g=e-1|0)+C|0]<<2)>>2]+11|0]?g:e),rg(e+C|0,I+432|0))}else rg(C,s);r=0}A:if(268435456&(A=f[A+104>>2])){if((0|Lg(C))<=0)break A;for(e=0,A=0;6==i[0|(g=A+C|0)]&&(e&&(a[0|g]=5),e=1),A=A+1|0,(0|Lg(C))>(0|A););}else if(256&A&&(e=0,!((0|(A=(k=Lg(C))-1|0))<0))){if(A)for(B=-2&k,s=0;6==i[0|(t=A+C|0)]?(g=1,e&&(a[0|t]=5)):g=e,6==i[0|(t=t-1|0)]?(e=1,g&&(a[0|t]=5)):e=g,A=A-2|0,(0|B)!=(0|(s=s+2|0)););1&k&&(!e|6!=i[0|(A=A+C|0)]||(a[0|A]=5))}return V=I+464|0,r}function DA(A,e,g,r){var C,b,s=0,t=0,n=0,k=0,o=0,B=0,c=0;if(V=C=V-352|0,a[C+304|0]=0,a[C+224|0]=0,a[C+64|0]=0,n=f[f[47192]+292>>2],c=Te(C+348|0,e),57344==(1048320&(s=f[C+348>>2]))&&(s&=255,f[C+348>>2]=s),2&r&&nr(s)&&Mg(A,85437,C+304|0),s=Sr(f[C+348>>2],A),f[C+348>>2]=s,B=1&r,De(A,s,a[0|(b=e+c|0)],C+224|0,B),!(e=i[C+224|0])){A:if((e=rr(f[C+348>>2]))&&(f[C+348>>2]=16383&e,4&r)){e:switch(1073741823&(e>>=14)){case 0:case 3:break A;default:break e}Mg(A,e=f[131232+(e<<2)>>2],C+304|0),i[C+304|0]||(a[C+306|0]=BC(84744),o=e,e=C+304|3,Mg(f[47194],o,e),i[C+307|0]&&(I[C+304>>1]=5385,e=Lg(e)+(C+304|0)|0,a[e+5|0]=0,a[e+4|0]=n,a[e+3|0]=21))}De(A,f[C+348>>2],a[0|b],C+224|0,B),e=i[C+224|0]}A:{e:{if(e&=255){if(21!=(0|e))break e;rg(g,C+224|0),c=0;break A}if(e=1632,!((0|(s=f[C+348>>2]))<1632)){for(k=103360;;){if((0|s)>=(e+10|0)){if(!(e=f[(k=k+4|0)>>2]))break e;if((0|e)<=(0|s))continue;break e}break}(0|(e=48+(s-e|0)|0))<=0||De(A,e,0,C+224|0,B)}}e:{g:{r:{C:{if(e=ae(f[C+348>>2])){if(s=f[e+4>>2],!e|1&(k=f[e+16>>2])||(t=f[47192],f[t+600>>2]==(0|s)|f[t+188>>2]==(0|s)|f[t+184>>2]==(0|s)||(a[C+144|0]=0,Mg(t,f[e>>2],C- -64|0)?(0|(t=f[47192]))!=(0|A)&&(n=f[A+292>>2],rg(C+144|0,C- -64|0),a[C+66|0]=f[t+292>>2]):(a[C+66|0]=BC(84744),Mg(f[47194],f[e>>2],C+144|0)),i[C+144|0]&&(I[C+64>>1]=5385,rg(3|(t=C- -64|0),o=C+144|0),t=Lg(o)+t|0,a[t+5|0]=0,a[t+4|0]=n,a[t+3|0]=21))),i[C+224|0])break e;if(!s)break C;if(n=f[47192],f[n+188>>2]!=(0|s))break C;e=f[n+192>>2];break g}if(i[C+224|0])break e;k=0,s=0;break r}if((e=f[e+12>>2])&&!(2&k))break g}e=25966}if((f[A+212>>2]==(0|e)&27503!=(0|e)||(a[C+226|0]=BC(lr(C+47|0,e)),(e=f[47194])&&((0|(n=f[C+348>>2]))>55215||(0|(t=n-44032|0))<0?De(e,n,a[0|b],C+224|3,B):(a[C+52|0]=32,e=o=C+53|0,n-50500>>>0>=588&&(e=Fg(4352+((t>>>0)/588|0)|0,o)+o|0),Fg(4449+(((n=(t>>>0)/28|0)>>>0)%21|0)|0,e),Fg(4519+(t-G(n,28)|0)|0,e+3|0),a[e+6|0]=32,a[e+7|0]=0,a[C+227|0]=0,e=C+224|3,GA(f[47194],o,e,77,0,0,0),fA(f[47194],e,0,-1,0)),e=C+224|3,21==i[C+227|0]&&(a[C+226|0]=BC(C+224|4),De(f[47194],f[C+348>>2],a[0|b],e,B)),qr(f[f[32972]+60>>2]),i[C+227|0]&&(I[C+224>>1]=5385,e=Lg(e)+(C+224|0)|0,a[e+3|0]=21,B=f[A+292>>2],a[e+5|0]=0,a[e+4|0]=B)),!i[C+224|0]))&&(16&k||(Mr(f[C+348>>2])&&Mg(f[47192],85683,C+224|0),i[C+224|0]||(er(f[C+348>>2])||Mg(f[47192],85778,C+224|0),i[C+224|0]||JA(85992,C+224|0,0))),!(8&k)||4&r)){if(e=f[C+348>>2],10240!=(0|s)?(f[C+32>>2]=e,dg(C+52|0,86013,C+32|0)):(s=C+52|0,1&e&&(a[C+52|0]=49,s=C+53|0),2&e&&(a[0|s]=50,s=s+1|0),4&e&&(a[0|s]=51,s=s+1|0,e=f[C+348>>2]),8&e&&(a[0|s]=52,s=s+1|0,e=f[C+348>>2]),16&e&&(a[0|s]=53,s=s+1|0,e=f[C+348>>2]),32&e&&(a[0|s]=54,s=s+1|0,e=f[C+348>>2]),64&e&&(a[0|s]=55,s=s+1|0,e=f[C+348>>2]),128&e&&(a[0|s]=56,s=s+1|0),a[0|s]=0),e=C+224|0,k=i[C+52|0])for(s=C+52|0;e=Lg(e)+e|0,a[0|e]=23,e=e+1|0,De(f[47192],k<<24>>24,0,e,1),(r=i[0|e])&&21!=(0|r)||(0|(r=a[0|s]))<97||JA(f[130860+((255&r)<<2)>>2],e,0),k=i[0|(s=s+1|0)];);e=Lg(e)+e|0,a[0|e]=9,a[e+1|0]=0}}e=Lg(g),2&i[A+144|0]?(f[C+16>>2]=255,f[C+28>>2]=C+304,f[C+24>>2]=C+224,f[C+20>>2]=C- -64,dg(C+144|0,86210,C+16|0)):(f[C>>2]=255,f[C+12>>2]=C+224,f[C+8>>2]=C+304,f[C+4>>2]=C- -64,dg(C+144|0,86210,C)),Lg(C+144|0)+e>>>0>199||rg(e+g|0,C+144|0)}return V=C+352|0,c}function uA(A,e,g,r,C,b,s){var t,n=0,o=0,B=0,Q=0,w=0,E=0,D=0,u=0,l=0,x=0,d=0,m=0,M=0,v=0,h=0,Y=0,H=0,N=0,P=0,F=0,y=0,z=0;V=t=V-528|0,x=s?f[s>>2]:0,Y=f[C+4>>2];A:{e:{if(f[A+220>>2]>0){oC(o=t+352|0,e,160),V=E=V-176|0,u=1-(D=f[A+220>>2])|0,l=f[A+224>>2],d=f[A+216>>2],Q=o;g:{r:{for(;;){if(m=Te(E+172|0,Q),n=f[E+172>>2]){if((0|n)<(0|D)|(0|n)>(0|d))break r;if(l){if((0|(n=a[l+(n-D|0)|0]))<=0)break r}else n=n+u|0;if(Q=Q+m|0,a[w+E|0]=n,n=160,160!=(0|(w=w+1|0)))continue}else n=w;break}if(l=0,a[n+E|0]=0,u=a[0|E],f[E+172>>2]=u,u){for(H=2+(d-D|0)|0,D=n=E;;){d=n+1|0;C:{if((m=f[A+8180>>2])&&(w=0,!((0|(Q=I[m>>1]))>(0|(v=(a[0|d]<<8)+u|0)))))for(;;){if((0|Q)==(0|v)){u=w+H|0,f[E+172>>2]=u,n=n+2|0;break C}if(!((0|v)>=(0|(Q=I[m+((w=w+1|0)<<1)>>1]))))break}n=d}if(M=63&u|M<<6,(0|(w=l+6|0))<8?l=w:(l=l-2|0,a[0|D]=M>>l,D=D+1|0),u=a[0|n],f[E+172>>2]=u,!u)break}(0|l)<=0||(a[0|D]=M<<8-l,D=D+1|0)}else D=E;a[0|D]=0,_A(o,E,n=D-E|0),D=64|n;break g}D=Lg(o)}V=E+176|0,E=o}else D=Lg(e),E=e;if(o=i[0|E]){for(n=0,w=E;B=1023&(B=(B<<3)+o|0)^B>>>8,n=n+1|0,o=i[0|(w=w+1|0)];);n=n+B&1023}else n=0;if(B=f[692+((n<<2)+A|0)>>2]){if(n=i[0|B])break e;n=0;break A}if(n=0,!C)break A;f[C>>2]=0;break A}for(m=1073741824&Y,v=2048&b,Y=512&x,H=65536&x,N=1&x,P=2&x,x=8&b,F=1024&b,d=4&b,y=63&D,z=A+8233|0;;){b=(255&n)+B|0;e:{g:if((127&(n=i[B+1|0]))==(0|D)&&!pg(E,B+2|0,y)){B=2+((63&n)+B|0)|0;r:{if(n<<24>>24<0)l=0,a[0|r]=0;else{if((0|(l=Lg(B)))>=160)break r;rg(r,B),B=1+(B+l|0)|0}if(o=0,b>>>0<=B>>>0)n=g,Q=0;else{w=0,Q=0;C:{for(;;){B=(n=B)+1|0;a:if((n=i[0|n])>>>0>=100){if(u=f[A+320>>2],n>>>0>=132){w|=u>>>n-132&1;break a}w|=!(u>>>n-100&1)}else{if(n>>>0>=81){u=n-80|0,M=b-B|0;I:if(s)for(n=0;;){if(h=G(n,12)+s|0,!i[h+10|0])break I;if(w=!!(12&i[h+1|0])|w,h=(0|n)!=(0|u),n=n+1|0,!h)break}if(qg(g,B,M)|1&w)break g;f[33264]=u,n=g+M|0,Q|=128,B=b;break C}n>>>0>=65?(Q=15&n|-16&Q,Q=12&~n?Q:512|Q):n>>>0>=32?o|=1<>>0>B>>>0))break}if(n=g,1&w)break e}if(65536&o&&!d)break e;if(F&&49152&o)break e}if(d){if(16384&o)break e;if(!x&&32768&o)break e}if((P?0:512&o)|(N?0:1024&o)|(H?0:33554432&Q))break e;if(!(!(131072&o)|c[f[47192]+8204>>2]<=n>>>0|m)|(Y?0:262144&o)|(8&i[f[47192]+8242|0]?0:8192&o))break e;if(16&o){if(!f[A+8184>>2]&(!x|!f[A+8192>>2]))break e;if(!(!x|25966!=f[A+212>>2])&&2097152&f[A+8232>>2])break e}if((f[A+8188>>2]?0:64&o)|(!f[A+8196>>2]|v?32&o:0))break e;if(!(!(65536&Q)|26741!=f[A+212>>2]|128&i[0|z])|(f[47192]!=(0|A)?524288&o:0))break e;C:{a:{I:{if(!C){if(!l)break I;break C}if(f[C+4>>2]=o,f[C>>2]=1073741824|Q,l)break a}if(n=0,!(8&i[188788]))break A;sg(C,A=t+272|0),f[t>>2]=e,f[t+4>>2]=A,eC(f[47195],89330,t);break A}f[C>>2]=-1073741824|Q}if(8&i[188788]&&(Ye(r,t- -64|0),i[f[47192]+172|0]==(Q>>>29&1)&&(!s|!(128&Q)?(f[t+48>>2]=e,eC(f[47195],89426,t+48|0)):(_A(A=t+352|0,r=g,g=n-g|0),a[351+(g+t|0)|0]=0,f[t+32>>2]=e,f[t+36>>2]=A,eC(f[47195],89397,t+32|0)),sg(C,A=t+272|0),e=f[47195],f[t+16>>2]=t- -64,f[t+20>>2]=A,eC(e,89534,t+16|0))),i[Te(t- -64|0,E)+E|0]|!C)break A;if(kg(f[t+64>>2]))break A;f[C>>2]=134217728|f[C>>2];break A}p(89236,86634,2467,94846),k()}B=b}if(!(n=i[0|B]))break}n=0}return V=t+528|0,n}function lA(A,e,g,r,C){var I,b=0,s=0;V=I=V-304|0,a[I+278|0]=0;A:{if((0|e)>0){if(1&r){if(2&r&&(f[I+164>>2]=g,f[I+160>>2]=e,dg(b=I+290|0,89701,I+160|0),b=Mg(A,b,I+224|0)))break A;if(1&a[133096]&&(f[I+148>>2]=g,f[I+144>>2]=e,dg(b=I+290|0,89757,I+144|0),b=Mg(A,b,I+224|0)))break A;if(f[I+132>>2]=g,f[I+128>>2]=e,dg(b=I+290|0,89894,I+128|0),b=Mg(A,b,I+224|0))break A}if(f[I+116>>2]=g,f[I+112>>2]=e,dg(b=I+290|0,89974,I+112|0),b=Mg(A,b,I+224|0))break A}if((0|(s=(0|e)%100|0))>=20&&Mg(A,90022,I+278|0),1&r){if(2&r){b=s-11|0;e:{g:{r:switch((448&f[f[47192]+108>>2])-64>>>6|0){case 0:if(b>>>0<9)break g;if(r=90418,1==(0|(b=(0|e)%10|0)))break e;if(b-2>>>0>=3)break g;r=90453;break e;case 1:if(e-2>>>0>=3)break g;r=90453;break e;case 2:if(b>>>0<9|((0|e)%10|0)-2>>>0>=3)break g;r=90453;break e;case 3:if(r=90508,b>>>0<9)break e;r=(r=(0|e)%10|0)?1==(0|r)?90453:90586:90508;break e;case 4:break r;default:break g}if(!(b>>>0<9)){if(r=90537,1==(0|(b=(0|e)%10|0)))break e;if(!(b-2>>>0>=3)){r=90453;break e}}}r=90586}if(f[I+100>>2]=g,f[I+96>>2]=r,dg(r=I+290|0,90058,I+96|0),b=0,Mg(A,r,I+224|0))break A}if(r=s-11|0,1&a[133096]){e:{g:{r:switch((448&f[f[47192]+108>>2])-64>>>6|0){case 0:if(r>>>0<9)break g;if(b=90418,1==(0|(s=(0|e)%10|0)))break e;if(s-2>>>0>=3)break g;b=90453;break e;case 1:if(e-2>>>0>=3)break g;b=90453;break e;case 2:if(r>>>0<9|((0|e)%10|0)-2>>>0>=3)break g;b=90453;break e;case 3:if(b=90508,r>>>0<9)break e;b=(b=(0|e)%10|0)?1==(0|b)?90453:90586:90508;break e;case 4:break r;default:break g}if(!(r>>>0<9)){if(b=90537,1==(0|(s=(0|e)%10|0)))break e;if(!(s-2>>>0>=3)){b=90453;break e}}}b=90586}if(f[I+84>>2]=g,f[I+80>>2]=b,dg(s=I+290|0,90110,I+80|0),b=0,Mg(A,s,I+224|0))break A}e:{g:{r:switch((448&f[f[47192]+108>>2])-64>>>6|0){case 0:if(r>>>0<9)break g;if(b=90418,1==(0|(s=(0|e)%10|0)))break e;if(s-2>>>0>=3)break g;b=90453;break e;case 1:if(e-2>>>0>=3)break g;b=90453;break e;case 2:if(r>>>0<9|((0|e)%10|0)-2>>>0>=3)break g;b=90453;break e;case 3:if(b=90508,r>>>0<9)break e;b=(b=(0|e)%10|0)?1==(0|b)?90453:90586:90508;break e;case 4:break r;default:break g}if(!(r>>>0<9)){if(b=90537,1==(0|(s=(0|e)%10|0)))break e;if(!(s-2>>>0>=3)){b=90453;break e}}}b=90586}if(f[I+68>>2]=g,f[I+64>>2]=b,dg(s=I+290|0,90139,I- -64|0),b=0,Mg(A,s,I+224|0))break A}else r=s-11|0;e:{g:{r:switch((448&f[f[47192]+108>>2])-64>>>6|0){case 0:if(r>>>0<9)break g;if(b=90418,1==(0|(r=(0|e)%10|0)))break e;if(r-2>>>0>=3)break g;b=90453;break e;case 1:if(e-2>>>0>=3)break g;b=90453;break e;case 2:if(r>>>0<9|((0|e)%10|0)-2>>>0>=3)break g;b=90453;break e;case 3:if(b=90508,r>>>0<9)break e;b=(r=(0|e)%10|0)?1==(0|r)?90453:90586:90508;break e;case 4:break r;default:break g}if(!(r>>>0<9)){if(b=90537,1==(0|(r=(0|e)%10|0)))break e;if(!(r-2>>>0>=3)){b=90453;break e}}}b=90586}f[I+52>>2]=g,f[I+48>>2]=b,dg(r=I+290|0,90218,I+48|0),b=0,Mg(A,r,I+224|0)||((0|g)<4||(f[I+32>>2]=g-1,dg(r=I+290|0,89026,I+32|0),Mg(A,r,I+176|0)||(Mg(A,90273,I+224|0),f[33275]=3)),i[I+224|0]||(f[I+16>>2]=e,dg(r=I+290|0,90303,I+16|0),(b=Mg(A,r,I+224|0))||Mg(A,90347,I+224|0),f[33275]=2))}return f[I+4>>2]=I+224,f[I>>2]=I+278,dg(C,90368,I),V=I+304|0,!(1!=(0|e)|1!=(0|g))&&(e=1,32&i[A+106|0])||(e=b),e}function xA(A,e,g,r){var C,b=0,s=0,t=0,n=0,k=0,o=0,c=0;V=C=V+-64|0,I[C+48>>1]=0,f[C+40>>2]=0,f[C+44>>2]=0,f[C+32>>2]=0,f[C+36>>2]=0,f[C+24>>2]=0,f[C+28>>2]=0,f[C+16>>2]=0,f[C+20>>2]=0,f[C+8>>2]=0,f[C+12>>2]=0,f[C>>2]=0,f[C+4>>2]=0,b=e;A:{for(;;){e:{g:{if(69!=(0|(s=i[0|b]))){if(32!=(0|s))break g;if(r&&(a[_A(s=r,e,r=(0|(r=b-e|0))>=159?159:r)+r|0]=0),r=63&g)break e;break A}a[0|b]=101}b=b+1|0;continue}break}if(1&g){e:if((b=b-1|0)>>>0>>0)s=r;else for(s=r;;){if(128!=(192&i[0|b]))break e;if(s=s+1|0,!((b=b-1|0)>>>0>=e>>>0))break}t=r-1|0}else t=r,s=r;if(1!=(0|r))for(;;){r=t;e:if(!((b=b-1|0)>>>0>>0))for(;;){if(128!=(192&i[0|b]))break e;if(s=s+1|0,!((b=b-1|0)>>>0>=e>>>0))break}e:if(!((b=b-1|0)>>>0>>0))for(;;){if(128!=(192&i[0|b]))break e;if(s=s+1|0,!((b=b-1|0)>>>0>=e>>>0))break}if(t=r-2|0,!((0|r)>2))break}if((0|s)<=0)t=0;else{if(n=3&(t=(r=(e=s-1|0)>>>0>=48?48:e)+1|0),e=0,s=0,r>>>0>=3)for(c=-4&t,r=0;k=b+s|0,a[s+C|0]=i[0|k],a[0|k]=32,k=(o=1|s)+b|0,a[C+o|0]=i[0|k],a[0|k]=32,k=(o=2|s)+b|0,a[C+o|0]=i[0|k],a[0|k]=32,k=(o=3|s)+b|0,a[C+o|0]=i[0|k],a[0|k]=32,s=s+4|0,(0|c)!=(0|(r=r+4|0)););if(n)for(;r=b+s|0,a[s+C|0]=i[0|r],a[0|r]=32,s=s+1|0,(0|n)!=(0|(e=e+1|0)););}}if(a[C+t|0]=0,n=65520&g,!(512&g)|105!=i[0|(r=b-1|0)]||(a[0|r]=121),s=4|n,256&g){A:{e:{g:{if(25966!=(0|(e=f[A+212>>2]))){if(28268!=(0|e))break g;if(a[0|r]<0)break A;if(128&(e=a[0|(t=b-2|0)]))break A;if(n=f[A+632>>2])e=!!(0|Pr(n,e));else{if((0|(n=f[A+600>>2]))>0&&(e=e-n|0)-1>>>0>254)break A;e=128&i[344+(A+e|0)|0]}if(!e)break A;if(e=a[0|r],n=f[A+612>>2])e=!!(0|Pr(n,e));else{r:{if((0|(n=f[A+600>>2]))>0){if((e=e-n|0)-1>>>0<255)break r;break A}if((0|e)<0)break A}e=4&i[344+(A+e|0)|0]}if(!e)break A;e=a[b-3|0];r:{if(n=f[A+632>>2])e=!!(0|Pr(n,e));else{C:{if((0|(n=f[A+600>>2]))>0){if((e=e-n|0)-1>>>0<255)break C;break r}if((0|e)<0)break r}e=128&i[344+(A+e|0)|0]}if(e)break A}a[0|b]=i[0|r],a[0|r]=i[0|t],a[b+1|0]=32;break A}if(t=a[b-2|0],e=f[A+632>>2])e=!!(0|Pr(e,t));else{r:{if((0|(e=f[A+600>>2]))>0){if((t=t-e|0)-1>>>0<255)break r;break e}if((0|t)<0)break e}e=128&i[344+(A+t|0)|0]}if(!e)break e;if(t=a[0|r],e=f[A+608>>2])e=!!(0|Pr(e,t));else{if((0|(e=f[A+600>>2]))>0){if((t=t-e|0)-1>>>0>=255)break e}else if((0|t)<0)break e;e=2&i[344+(A+t|0)|0]}if(!e)break e;s=pg(87771,b-3|0,3)?20|n:s;break A}s=f[A+204>>2]?20|n:s;break A}(99==i[0|r]||(t=i[(e=b-2|0)+1|0]<<8,29554==(i[0|e]|t)|29289==(t|i[0|e])||29301==(i[0|(e=b-2|0)]|i[e+1|0]<<8)||!pg(88115,b-3|0,3)||29550==(i[0|(e=b-2|0)]|i[e+1|0]<<8)|117==i[0|r]||!pg(88384,b-5|0,5)||1735287154==(i[0|(e=b-4|0)]|i[e+1|0]<<8|i[e+2|0]<<16|i[e+3|0]<<24)||1735549292==(i[0|e]|i[e+1|0]<<8|i[e+2|0]<<16|i[e+3|0]<<24)))&&(s=20|n)}16&s&&(Fg(f[A+204>>2],b),8&i[188788]&&gC(88683,6,f[47195]))}return f[A+8184>>2]|!(2048&g)||(f[A+8184>>2]=1),115!=B[C>>1]&&pg(C,88850,3)||(s|=8),V=C- -64|0,39==i[0|C]?65531&s:s}function dA(A,e,g,r,C,a,I,i,b){var s,t=0,n=0,k=0,o=0,B=0,c=0,Q=0,G=0,w=0,E=0;V=s=V-112|0,t=2147483647&b;A:{if(k=!(e|g),(r|(n=2147483647&C)?n-2147418112>>>0<2147549184:k)||!(!i&-2147418112==(0|(B=t-2147418112|0))?a|I:-2147418112==(0|B)&!!(0|i)|B>>>0>2147549184)){if(!(!r&2147418112==(0|n)?k:n>>>0<2147418112)){i=r,b=32768|C,a=e,I=g;break A}if(!(!i&2147418112==(0|t)?!(a|I):t>>>0<2147418112)){b|=32768;break A}if(!(e|r|2147418112^n|g)){o=r,i=(r=!(e^a|r^i|g^I|C^b^-2147483648))?0:o,b=r?2147450880:C,a=r?0:e,I=r?0:g;break A}if(!(a|i|2147418112^t|I))break A;if(!(e|r|g|n)){if(a|i|I|t)break A;a&=e,I&=g,i&=r,b&=C;break A}if(!(a|i|I|t)){a=e,I=g,i=r,b=C;break A}}n=(k=G=(o=(0|t)==(0|n))&(0|r)==(0|i)?(0|g)==(0|I)&e>>>0>>0|g>>>0>>0:o&r>>>0>>0|t>>>0>n>>>0)?a:e,B=k?I:g,w=o=k?b:C,k=k?i:r,Q=65535&o,r=G?r:i,E=C=G?C:b,o=C>>>16&32767,(c=w>>>16&32767)||(b=C=!(k|Q),t=C?n:k,i=C<<=6,Ve(s+96|0,n,B,k,Q,(C=C+(32==(0|(b=D(b?B:Q)))?D(t)+32|0:b)|0)-15|0),k=f[s+104>>2],Q=f[s+108>>2],B=f[s+100>>2],c=16-C|0,n=f[s+96>>2]),a=G?e:a,I=G?g:I,i=r,b=65535&E,o||(C=e=!(i|b),t=e?a:i,g=e<<=6,Ve(s+80|0,a,I,i,b,(e=e+(32==(0|(C=D(C?I:b)))?D(t)+32|0:C)|0)-15|0),o=16-e|0,i=f[s+88>>2],b=f[s+92>>2],I=f[s+84>>2],a=f[s+80>>2]),g=b<<3|i>>>29,e=i<<3|I>>>29,g|=524288,i=k<<3|B>>>29,b=Q<<3|k>>>29,G=w^E,C=I<<3|a>>>29,r=a<<3,(0|o)!=(0|c)&&((a=c-o|0)>>>0>127?(e=0,g=0,C=0,r=1):(Ve(s- -64|0,r,C,e,g,128-a|0),Ke(s+48|0,r,C,e,g,a),e=f[s+56>>2],g=f[s+60>>2],C=f[s+52>>2],r=f[s+48>>2]|!!(f[s+64>>2]|f[s+72>>2]|f[s+68>>2]|f[s+76>>2]))),k=r,t=C,o=i,Q=524288|b,C=B<<3|n>>>29,B=n<<3,n=C;e:if((0|G)<0){if(a=0,I=0,i=0,b=0,!(k^B|e^o|t^n|g^Q))break A;if(r=B-k|0,C=n-((k>>>0>B>>>0)+t|0)|0,i=(a=o-e|0)-(I=(0|t)==(0|n)&k>>>0>B>>>0|t>>>0>n>>>0)|0,b=e=(Q-((e>>>0>o>>>0)+g|0)|0)-(a>>>0>>0)|0,e>>>0>524287)break e;a=e=!(i|b),I=e?r:i,g=e<<=6,Ve(s+32|0,r,C,i,b,e=(e=e+(32==(0|(a=D(a?C:b)))?D(I)+32|0:a)|0)-12|0),c=c-e|0,i=f[s+40>>2],b=f[s+44>>2],r=f[s+32>>2],C=f[s+36>>2]}else C=t+n|0,a=(0|t)==(0|(C=(r=k+B|0)>>>0>>0?C+1|0:C))&r>>>0>>0|C>>>0>>0,t=g+Q|0,t=(e=e+o|0)>>>0>>0?t+1|0:t,1048576&(b=(i=e+a|0)>>>0>>0?t+1|0:t)&&(r=1&k|(1&C)<<31|r>>>1,C=i<<31|C>>>1,c=c+1|0,i=(1&b)<<31|i>>>1,b=b>>>1|0);if(g=0,n=-2147483648&w,(0|c)>=32767)i=g,b=2147418112|n,a=0,I=0;else if(o=0,(0|c)>0?o=c:(Ve(s+16|0,r,C,i,b,c+127|0),Ke(s,r,C,i,b,1-c|0),r=f[s>>2]|!!(f[s+16>>2]|f[s+24>>2]|f[s+20>>2]|f[s+28>>2]),C=f[s+4>>2],i=f[s+8>>2],b=f[s+12>>2]),B=7&r,r=(0|(e=i<<29|C>>>3))==(0|(I=(r=(7&C)<<29|r>>>3)>>>0>(a=(B>>>0>4)+r|0)>>>0?e+1|0:e))&r>>>0>a>>>0|e>>>0>I>>>0,e=g|(7&b)<<29|i>>>3,b=n|b>>>3&65535|o<<16,b=e>>>0>(i=r+e|0)>>>0?b+1|0:b,4!=(0|B)){if(!B)break A}else t=I+(e=0)|0,b=(e=(0|e)==(0|(I=(r=a)>>>0>(a=a+(g=1&a)|0)>>>0?t+1|0:t))&g>>>0>a>>>0|e>>>0>I>>>0)>>>0>(i=e+i|0)>>>0?b+1|0:b}f[A>>2]=a,f[A+4>>2]=I,f[A+8>>2]=i,f[A+12>>2]=b,V=s+112|0}function mA(A){var e=0,g=0,r=0,C=0,a=0,I=0,i=0;A:if(A|=0){a=(r=A-8|0)+(A=-8&(e=f[A-4>>2]))|0;e:if(!(1&e)){if(!(3&e))break A;if((r=r-(e=f[r>>2])|0)>>>0>2])))return f[57154]=A,f[a+4>>2]=-2&e,f[r+4>>2]=1|A,void(f[A+r>>2]=A)}else{if(e>>>0<=255){if(C=f[r+8>>2],e=e>>>3|0,(0|(g=f[r+12>>2]))==(0|C)){f[57152]=f[57152]&jr(-2,e);break e}f[C+12>>2]=g,f[g+8>>2]=C;break e}if(i=f[r+24>>2],(0|r)==(0|(e=f[r+12>>2])))if((g=f[(C=r+20|0)>>2])||(g=f[(C=r+16|0)>>2])){for(;I=C,(g=f[(C=(e=g)+20|0)>>2])||(C=e+16|0,g=f[e+16>>2]););f[I>>2]=0}else e=0;else g=f[r+8>>2],f[g+12>>2]=e,f[e+8>>2]=g;if(!i)break e;C=f[r+28>>2];g:{if(f[(g=228912+(C<<2)|0)>>2]==(0|r)){if(f[g>>2]=e,e)break g;f[57153]=f[57153]&jr(-2,C);break e}if(f[i+(f[i+16>>2]==(0|r)?16:20)>>2]=e,!e)break e}if(f[e+24>>2]=i,(g=f[r+16>>2])&&(f[e+16>>2]=g,f[g+24>>2]=e),!(g=f[r+20>>2]))break e;f[e+20>>2]=g,f[g+24>>2]=e}}if(!(r>>>0>=a>>>0)&&1&(e=f[a+4>>2])){e:{if(!(2&e)){if(f[57158]==(0|a)){if(f[57158]=r,A=f[57155]+A|0,f[57155]=A,f[r+4>>2]=1|A,f[57157]!=(0|r))break A;return f[57154]=0,void(f[57157]=0)}if(f[57157]==(0|a))return f[57157]=r,A=f[57154]+A|0,f[57154]=A,f[r+4>>2]=1|A,void(f[A+r>>2]=A);A=(-8&e)+A|0;g:if(e>>>0<=255){if(C=f[a+8>>2],e=e>>>3|0,(0|(g=f[a+12>>2]))==(0|C)){f[57152]=f[57152]&jr(-2,e);break g}f[C+12>>2]=g,f[g+8>>2]=C}else{if(i=f[a+24>>2],(0|a)==(0|(e=f[a+12>>2])))if((g=f[(C=a+20|0)>>2])||(g=f[(C=a+16|0)>>2])){for(;I=C,(g=f[(C=(e=g)+20|0)>>2])||(C=e+16|0,g=f[e+16>>2]););f[I>>2]=0}else e=0;else g=f[a+8>>2],f[g+12>>2]=e,f[e+8>>2]=g;if(i){C=f[a+28>>2];r:{if(f[(g=228912+(C<<2)|0)>>2]==(0|a)){if(f[g>>2]=e,e)break r;f[57153]=f[57153]&jr(-2,C);break g}if(f[i+(f[i+16>>2]==(0|a)?16:20)>>2]=e,!e)break g}f[e+24>>2]=i,(g=f[a+16>>2])&&(f[e+16>>2]=g,f[g+24>>2]=e),(g=f[a+20>>2])&&(f[e+20>>2]=g,f[g+24>>2]=e)}}if(f[r+4>>2]=1|A,f[A+r>>2]=A,f[57157]!=(0|r))break e;return void(f[57154]=A)}f[a+4>>2]=-2&e,f[r+4>>2]=1|A,f[A+r>>2]=A}if(A>>>0<=255)return e=228648+(-8&A)|0,(g=f[57152])&(A=1<<(A>>>3))?A=f[e+8>>2]:(f[57152]=A|g,A=e),f[e+8>>2]=r,f[A+12>>2]=r,f[r+12>>2]=e,void(f[r+8>>2]=A);C=31,A>>>0<=16777215&&(C=62+((A>>>38-(e=D(A>>>8|0))&1)-(e<<1)|0)|0),f[r+28>>2]=C,f[r+16>>2]=0,f[r+20>>2]=0,I=228912+(C<<2)|0;e:{g:{if((g=f[57153])&(e=1<>>1|0):0),e=f[I>>2];;){if(g=e,(-8&f[e+4>>2])==(0|A))break g;if(e=C>>>29|0,C<<=1,!(e=f[(I=g+(4&e)|0)+16>>2]))break}f[I+16>>2]=r,f[r+24>>2]=g}else f[57153]=e|g,f[I>>2]=r,f[r+24>>2]=I;f[r+12>>2]=r,f[r+8>>2]=r;break e}A=f[g+8>>2],f[A+12>>2]=r,f[g+8>>2]=r,f[r+24>>2]=0,f[r+12>>2]=g,f[r+8>>2]=A}A=f[57160]-1|0,f[57160]=A||-1}}}function MA(A,e,g){var r,C=0,b=0,s=0,t=0,n=0,k=0,o=0,B=0,c=0,Q=0,w=0,E=0;V=r=V-176|0;A:{if(32&e)Q=g-((0|g)>1)|0;else{b=ze(A,93302);e:{g:{r:{if(2!=(0|e)){if(b)break r;e=0;break A}if(Q=g+1|0,B=(w=G(g,76)+133152|0)+56|0,c=ze(A,89360),s=ze(A,93318),t=ze(A,93426),n=ze(A,93499),b)break g;break e}Q=g+1|0,B=(w=G(g,76)+133152|0)+56|0,n=0}for(A=0,k=34!=(0|(C=f[b-4>>2]))?39==(0|C)?C:0:C,E=G(g,76)+133208|0;;){if(C=A,!(A=f[b>>2]))break e;g:{if(!k){if(32==(0|A)|A-9>>>0<5)break e;if(47!=(0|A))break g;break e}if(92!=(0|C)&&(0|A)==(0|k))break e}if(b=b+4|0,!((0|(o=Fg(A,o+E|0)+o|0))<16))break}}k=0,a[o+B|0]=0,o=G(g,76)+133168|0,b=0;e:if(c)for(A=0,B=34!=(0|(C=f[c-4>>2]))?39==(0|C)?C:0:C;;){if(C=A,!(A=f[c>>2]))break e;g:{if(!B){if(32==(0|A)|A-9>>>0<5)break e;if(47!=(0|A))break g;break e}if(92!=(0|C)&&(0|A)==(0|B))break e}if(c=c+4|0,!((0|(b=Fg(A,b+o|0)+b|0))<36))break}if(a[b+o|0]=0,!(!s|f[s>>2]-48>>>0>=10)){for(;k=(f[s>>2]+G(k,10)|0)-48|0,f[(s=s+4|0)>>2]-48>>>0<10;);(0|k)<=0||(k=k-1|0)}if(C=G(g,76)+133152|0,f[C+4>>2]=k,A=0,b=0,!(!t|f[t>>2]-48>>>0>=10))for(;b=(f[t>>2]+G(b,10)|0)-48|0,f[(t=t+4|0)>>2]-48>>>0<10;);f[C+12>>2]=b,b=G(g,76)+133152|0;e:{g:if(n){for(;g=a[A+93099|0],(C=f[(A<<2)+n>>2])&&(A=A+1|0,(0|g)==(0|C)););r:{C:switch(C-34|0){case 0:case 5:break C;default:break r}if(!g){A=0;break e}}for(A=0;g=a[A+93116|0],(C=f[(A<<2)+n>>2])&&(A=A+1|0,(0|g)==(0|C)););r:{C:switch(C-34|0){case 0:case 5:break C;default:break r}if(!g){A=1;break e}}for(A=0;g=a[A+93197|0],(C=f[(A<<2)+n>>2])&&(A=A+1|0,(0|g)==(0|C)););r:switch(C-34|0){case 0:case 5:break r;default:break g}if(!g){A=2;break e}}A=3}f[b+8>>2]=f[131156+(A<<3)>>2],f[w>>2]=e}if(rg(137776,133168),g=rg(r+96|0,133208),a[r+157|0]=f[33291],a[r+156|0]=f[33290],A=f[33289],f[r+152>>2]=0,a[r+158|0]=A,(0|Q)>0)for(s=0;;){if(t=1,e=G(s,76)+133152|0,i[0|(A=e+16|0)]&&He(0,A)&&(rg(137776,A),t=0,a[0|g]=0,a[r+158|0]=0,I[r+156>>1]=0),i[0|(A=e+56|0)]){n=rg(g,A),C=f[33679];e:if(i[0|(A=C)])for(;;){if(!Qr(A=A+1|0,n)){rg(n,C+1|0);break e}if(A=1+(Lg(A)+A|0)|0,!i[0|A])break}t&&(a[137776]=0)}if((A=f[e+8>>2])&&(a[r+156|0]=A),(A=f[e+12>>2])&&(a[r+157|0]=A),(A=f[e+4>>2])&&(a[r+158|0]=A),(0|Q)==(0|(s=s+1|0)))break}f[r+148>>2]=g,f[r+144>>2]=137776,(A=wA(r+144|0,r+172|0))?sC(A,43)||(e=i[r+156|0],!i[134672]|((0|e)!=i[134724]?e:0)||(f[r>>2]=A,f[r+4>>2]=134672,dg(e=r+16|0,93533,r),A=137776,oC(137776,e,40))):A=92003,e=0,Qr(A,134784)&&(rg(134784,A),e=131072)}return V=r+176|0,e}function vA(A,e){var g,r=0,C=0,a=0,I=0,i=0;g=A+e|0;A:{e:if(!(1&(r=f[A+4>>2]))){if(!(3&r))break A;e=(r=f[A>>2])+e|0;g:{if((0|(A=A-r|0))!=f[57157]){if(r>>>0<=255){if(a=f[A+8>>2],r=r>>>3|0,(0|(C=f[A+12>>2]))!=(0|a))break g;f[57152]=f[57152]&jr(-2,r);break e}if(i=f[A+24>>2],(0|(r=f[A+12>>2]))==(0|A))if((C=f[(a=A+20|0)>>2])||(C=f[(a=A+16|0)>>2])){for(;I=a,(C=f[(a=(r=C)+20|0)>>2])||(a=r+16|0,C=f[r+16>>2]););f[I>>2]=0}else r=0;else C=f[A+8>>2],f[C+12>>2]=r,f[r+8>>2]=C;if(!i)break e;a=f[A+28>>2];r:{if(f[(C=228912+(a<<2)|0)>>2]==(0|A)){if(f[C>>2]=r,r)break r;f[57153]=f[57153]&jr(-2,a);break e}if(f[i+(f[i+16>>2]==(0|A)?16:20)>>2]=r,!r)break e}if(f[r+24>>2]=i,(C=f[A+16>>2])&&(f[r+16>>2]=C,f[C+24>>2]=r),!(C=f[A+20>>2]))break e;f[r+20>>2]=C,f[C+24>>2]=r;break e}if(3&~(r=f[g+4>>2]))break e;return f[57154]=e,f[g+4>>2]=-2&r,f[A+4>>2]=1|e,void(f[g>>2]=e)}f[a+12>>2]=C,f[C+8>>2]=a}e:{if(!(2&(r=f[g+4>>2]))){if(f[57158]==(0|g)){if(f[57158]=A,e=f[57155]+e|0,f[57155]=e,f[A+4>>2]=1|e,f[57157]!=(0|A))break A;return f[57154]=0,void(f[57157]=0)}if(f[57157]==(0|g))return f[57157]=A,e=f[57154]+e|0,f[57154]=e,f[A+4>>2]=1|e,void(f[A+e>>2]=e);e=(-8&r)+e|0;g:if(r>>>0<=255){if(a=f[g+8>>2],r=r>>>3|0,(0|(C=f[g+12>>2]))==(0|a)){f[57152]=f[57152]&jr(-2,r);break g}f[a+12>>2]=C,f[C+8>>2]=a}else{if(i=f[g+24>>2],(0|g)==(0|(r=f[g+12>>2])))if((a=f[(C=g+20|0)>>2])||(a=f[(C=g+16|0)>>2])){for(;I=C,(a=f[(C=(r=a)+20|0)>>2])||(C=r+16|0,a=f[r+16>>2]););f[I>>2]=0}else r=0;else C=f[g+8>>2],f[C+12>>2]=r,f[r+8>>2]=C;if(i){a=f[g+28>>2];r:{if(f[(C=228912+(a<<2)|0)>>2]==(0|g)){if(f[C>>2]=r,r)break r;f[57153]=f[57153]&jr(-2,a);break g}if(f[i+(f[i+16>>2]==(0|g)?16:20)>>2]=r,!r)break g}f[r+24>>2]=i,(C=f[g+16>>2])&&(f[r+16>>2]=C,f[C+24>>2]=r),(C=f[g+20>>2])&&(f[r+20>>2]=C,f[C+24>>2]=r)}}if(f[A+4>>2]=1|e,f[A+e>>2]=e,f[57157]!=(0|A))break e;return void(f[57154]=e)}f[g+4>>2]=-2&r,f[A+4>>2]=1|e,f[A+e>>2]=e}if(e>>>0<=255)return r=228648+(-8&e)|0,(C=f[57152])&(e=1<<(e>>>3))?e=f[r+8>>2]:(f[57152]=e|C,e=r),f[r+8>>2]=A,f[e+12>>2]=A,f[A+12>>2]=r,void(f[A+8>>2]=e);a=31,e>>>0<=16777215&&(a=62+((e>>>38-(r=D(e>>>8|0))&1)-(r<<1)|0)|0),f[A+28>>2]=a,f[A+16>>2]=0,f[A+20>>2]=0,I=228912+(a<<2)|0;e:{if((C=f[57153])&(r=1<>>1|0):0),r=f[I>>2];;){if(C=r,(-8&f[r+4>>2])==(0|e))break e;if(r=a>>>29|0,a<<=1,!(r=f[(I=C+(4&r)|0)+16>>2]))break}f[I+16>>2]=A,f[A+24>>2]=C}else f[57153]=r|C,f[I>>2]=A,f[A+24>>2]=I;return f[A+12>>2]=A,void(f[A+8>>2]=A)}e=f[C+8>>2],f[e+12>>2]=A,f[C+8>>2]=A,f[A+24>>2]=0,f[A+12>>2]=C,f[A+8>>2]=e}}function hA(A){var e=0,g=0,r=0,C=0,I=0,b=0,s=0,t=0,n=0,k=0,o=0,B=0,c=0;if(!(e=a[88105]))return A;if(A=sC(A,e)){if(!i[88106])return A;if(i[A+1|0]){if(!i[88107]){C=!!(0|(g=i[A+1|0]));A:if(g&&(0|(e=g|i[0|A]<<8))!=(0|(s=i[88106]|i[88105]<<8)))for(g=A+1|0;;){if(C=!!(0|(r=i[(A=g)+1|0])),!r)break A;if(g=A+1|0,(0|s)==(0|(e=r|e<<8&65280)))break}return C?A:0}if(i[A+2|0]){if(!i[88108]){g=A+2|0,C=!!(0|(e=i[A+2|0]));A:if(e&&(0|(e=i[A+1|0]<<16|i[0|A]<<24|e<<8))!=(0|(s=i[88106]<<16|i[88105]<<24|i[88107]<<8)))for(;;){if(A=g+1|0,C=!!(0|(r=i[g+1|0])),!r)break A;if(g=A,(0|s)==(0|(e=(e|r)<<8)))break}else A=g;return C?A-2|0:0}if(i[A+3|0]){if(!i[88109]){g=A+3|0,C=!!(0|(e=i[A+3|0]));A:if(e&&(0|(e=e|i[A+1|0]<<16|i[0|A]<<24|i[A+2|0]<<8))!=(0|(s=(A=i[88105]|i[88106]<<8|i[88107]<<16|i[88108]<<24)<<24|(65280&A)<<8|A>>>8&65280|A>>>24)))for(;;){if(A=g+1|0,C=!!(0|(r=i[g+1|0])),!r)break A;if(g=A,(0|s)==(0|(e=r|e<<8)))break}else A=g;return C?A-3|0:0}s=A,V=t=V-1056|0,f[(A=t+1048|0)>>2]=0,f[A+4>>2]=0,f[(A=t+1040|0)>>2]=0,f[A+4>>2]=0,f[t+1032>>2]=0,f[t+1036>>2]=0,f[t+1024>>2]=0,f[t+1028>>2]=0;A:{e:{g:{r:{if(e=i[88105]){for(;;){if(!i[I+s|0])break e;if(I=I+1|0,f[((255&e)<<2)+t>>2]=I,f[(A=(t+1024|0)+(e>>>3&28)|0)>>2]=f[A>>2]|1<>>0>1)break r}else n=-1,A=1;b=-1,g=1;break g}for(r=1,e=1;;){r:if((0|(b=i[88105+(e+n|0)|0]))!=(0|(C=i[A+88105|0])))C>>>0>>0?(r=A-n|0,g=A,e=1):(n=g,g=g+1|0,r=1,e=1);else{if((0|e)==(0|r)){g=g+r|0,e=1;break r}e=e+1|0}if(!(I>>>0>(A=g+e|0)>>>0))break}if(g=1,b=-1,I>>>0<=1)A=r;else{for(A=0,C=1,e=1;;){r:if((0|(o=i[88105+(e+b|0)|0]))!=(0|(k=i[g+88105|0])))k>>>0>o>>>0?(C=g-b|0,A=g,e=1):(b=A,A=A+1|0,C=1,e=1);else{if((0|e)==(0|C)){A=A+C|0,e=1;break r}e=e+1|0}if(!(I>>>0>(g=A+e|0)>>>0))break}A=r,g=C}}for(e=A,pg(88105,(r=(A=b+1>>>0>n+1>>>0)?g:e)+88105|0,o=(k=A?b:n)+1|0)?(r=((A=~k+I|0)>>>0>>0?k:A)+1|0,C=0):C=I-r|0,c=I-1|0,B=63|I,b=0,A=s;;){if(!(s-A>>>0>=I>>>0))if(g=qe(s,0,B)){if(s=g,g-A>>>0>>0)break e}else s=s+B|0;g=i[A+c|0],e=I;g:{if(f[(t+1024|0)+(g>>>3&28)>>2]>>>g&1)if((0|(g=f[(g<<2)+t>>2]))==(0|I)){r:{if(n=i[(g=(e=o)>>>0>b>>>0?e:b)+88105|0])for(;;){if(i[A+g|0]!=(255&n))break r;if(!(n=i[(g=g+1|0)+88105|0]))break}for(;;){if(e>>>0<=b>>>0)break A;if(i[(e=e-1|0)+88105|0]!=i[A+e|0])break}e=r,b=C;break g}e=g-k|0}else e=(g=I-g|0)>>>0>b>>>0?g:b;b=0}A=A+e|0}}A=0}V=t+1056|0,g=A}}}}return g}function pA(A,e,g,r,C,I){var b,s,t,n,k=0,o=0,B=0;if(V=b=V-560|0,a[b+448|0]=0,a[b+144|0]=0,a[b+120|0]=0,t=34&I,s=e-G(o=(0|e)/100|0,100)|0,1&(n=64&i[A+106|0]?(0|e)>999|I:0)|(0|e)>99){A:{e:{g:{if(!(!t|s)){if(!Mg(A,90606,b+304|0))break g;break A}if(s)break e}if(Mg(A,90691,b+304|0))break A}Mg(A,90725,b+304|0)}k=I,(0|e)<1e3||(k=I,!(8&i[A+105|0])|e-2e3>>>0<4294967196&&(a[b+208|0]=0,lA(A,k=(o>>>0)/10|0,r=16384&f[A+108>>2]?0:C+1|0,!((e>>>0)%1e3|0)|t,b+272|0)||EA(A,k,C,28012==f[A+212>>2]?520:(0|r)<4?(f[A+108>>2]>>>r&1)<<3:0,b+208|0),2&i[A+109|0]?(f[b+108>>2]=15,f[b+100>>2]=15,f[b+104>>2]=b+208,f[b+96>>2]=b+272,dg(b+144|0,90761,b+96|0)):(f[b+92>>2]=15,f[b+84>>2]=15,f[b+88>>2]=b+272,f[b+80>>2]=b+208,dg(b+144|0,90761,b+80|0)),r=1,1&(!!(0|(o=o-G(k,10)|0))|n)||(a[b+304|0]=0),k=1|I)),a[b+208|0]=0;A:if(!(~n&(0|o)<=0))if(!(4&i[A+106|0])|!(1&k|i[b+144|0])||Mg(A,90824,b+120|0),!t|(16&i[A+109|0]?0:s)||(f[b+64>>2]=o,dg(r=b+548|0,90875,b- -64|0),B=Mg(A,r,b+208|0),!(4096&f[A+108>>2])|(0|s)<=0||mC(b+208|0,133104)),r=1,1&(~n|!!(0|o))){e:{g:{r:{C:{if(!(131072&f[A+108>>2])||1&k|1!=(0|o)){if(s|B||(f[b+48>>2]=o,dg(r=b+548|0,90985,b+48|0),B=Mg(A,r,b+208|0)),B)break C;if(f[b+32>>2]=o,dg(r=b+548|0,91027,b+32|0),Mg(A,r,b+208|0))break C;if(1!=(0|o))break g;break r}if(!B)break r}a[b+304|0]=0;break e}if(r=1,4&i[A+105|0])break A}EA(A,o,C,0,b+208|0)}r=1}else Mg(A,88875,b+208|0);f[b+28>>2]=b+304,f[b+24>>2]=b+208,f[b+20>>2]=b+120,f[b+16>>2]=b+144,dg(b+448|0,91059,b+16|0)}else k=I;a[b+132|0]=0;A:{e:{if((0|s)>0)16&i[A+109|0]&&2&k||(!(1&k)|C&&(0|e)<=100||!(64&(r=f[A+104>>2]))&(!(8388608&r)|s>>>0>9)||Mg(A,90824,b+132|0),!(1&k|i[b+144|0])|!(524288&f[A+104>>2])|o||Mg(A,90824,b+132|0)),a[b+336|0]=0;else if(a[b+336|0]=0,!s&r)break e;if(C?(I=f[A+108>>2],e=(0|C)<4?(I>>>C&1)<<3:0):(r=t?3:2,e=32&I|((0|e)<100?1&k?r:4|r:r),I=f[A+108>>2]),e=1==(0|C)&&28012==f[A+212>>2]?520|e:e,1048576&I&&(r=16|e,e=(0|o)>0||1&k?r:e),!EA(A,s,C,256&k|e,b+336|0)|!(128&i[A+104|0]))break A;a[b+132|0]=0;break A}i[133104]&&((0|(A=Lg(b+448|0)))<=0||10==i[(A=A+b|0)+447|0]&&(a[A+447|0]=0),rg(b+336|0,133104))}f[b+8>>2]=15,f[b+12>>2]=b+336,f[b+4>>2]=b+132,f[b>>2]=b+448,dg(g,91101,b),V=b+560|0}function YA(A){var e,g,r,C=0,b=0,s=0,t=0,n=0,k=0,o=0,B=0,c=0,w=0,E=0,D=0;for(C=f[32972],f[C+64>>2]=290816,f[C+68>>2]=4104,f[C+96>>2]=0,f[C+100>>2]=0,f[C+120>>2]=90,f[C+124>>2]=100,f[C+112>>2]=64,f[C+116>>2]=256,f[C+104>>2]=5,f[C+108>>2]=0,f[C+84>>2]=100,f[C+88>>2]=64,b=f[50754],f[C+132>>2]=0,f[C+136>>2]=0,f[C+128>>2]=b,f[C+140>>2]=0,f[C+144>>2]=0,f[C+148>>2]=0,f[C+152>>2]=0,f[C+156>>2]=0,f[C+160>>2]=0,f[36435]=450,f[C+92>>2]=2,f[50870]=0,f[50871]=0,f[50872]=0,f[50873]=0,f[50880]=0,f[50881]=0,f[50882]=0,f[50883]=0,f[50890]=0,f[50891]=0,f[50892]=0,f[50893]=0,s=-3.141592653589793/+f[50754],Q[25429]=s,n=-2*s,Q[25430]=n,e=(s=$A(200*s))*-s,Q[25434]=e,Q[25439]=e,Q[25444]=e,s=(n=s*Cg(2e3*n))+n,Q[25433]=s,Q[25438]=s,Q[25443]=s,n=1-s-e,Q[25432]=n,Q[25437]=n,Q[25442]=n,f[50902]=0,f[50903]=0,f[50900]=0,f[50901]=0,Q[25449]=e,Q[25448]=s,f[50912]=0,f[50913]=0,Q[25447]=n,f[50910]=0,f[50911]=0,Q[25454]=e,Q[25453]=s,f[50922]=0,f[50923]=0,Q[25452]=n,f[50920]=0,f[50921]=0,Q[25459]=e,Q[25458]=s,f[50932]=0,f[50933]=0,Q[25457]=n,f[50930]=0,f[50931]=0,Q[25464]=e,Q[25463]=s,Q[25462]=n,f[50942]=0,f[50943]=0,f[50940]=0,f[50941]=0,Q[25469]=e,Q[25468]=s,Q[25467]=n,f[50952]=0,f[50953]=0,f[50950]=0,f[50951]=0,Q[25474]=e,Q[25473]=s,Q[25472]=n,g=f[32972],b=0;I[(k=(b<<1)+g|0)+236>>1]=256,I[k+164>>1]=256,C=i[b+105376|0]<<1,I[k+254>>1]=C,I[k+182>>1]=C,I[k+200>>1]=i[b+105385|0]<<1,f[(C=(t=b<<2)+g|0)+308>>2]=f[t+105408>>2],f[C+272>>2]=0,I[k+218>>1]=0,f[t+200944>>2]=(0|G(f[t+105456>>2],22050))/f[50754],9!=(0|(b=b+1|0)););for(w=f[32961],b=0,r=g+344|0;;){if(C=o,B=w,k=b,-1==(0|(o=f[(b=(t=b<<2)+131840|0)>>2]))&&(o=8e3,f[b>>2]=8e3,k&&(f[131840+(4|t)>>2]=f[t+131836>>2])),w=f[131840+(4|t)>>2],!((0|C)>=(0|(o=(0|o)/8|0))||(0|(c=o-C|0))<=0||(t=C+1|0,b=C,1&c&&(a[344+(C+g|0)|0]=(0|B)>=255?255:B,b=t),(0|t)==(0|o))))for(E=w-B|0;t=((0|G(b-C|0,E))/(0|c)|0)+B|0,a[b+r|0]=(0|t)>=255?255:t,D=((0|G((t=b+1|0)-C|0,E))/(0|c)|0)+B|0,a[t+r|0]=(0|D)>=255?255:D,(0|o)!=(0|(b=b+2|0)););if(b=k+2|0,!(k>>>0<10))break}b=f[32972],f[b+80>>2]=232,f[b+72>>2]=256,f[b+76>>2]=238,A||(f[49848]=0),I[b+200>>1]=(0|G(I[b+200>>1],105))/100}function HA(A,e,g){var r,C=0,I=0,b=0,s=0,t=0,n=0,k=0,o=0;V=r=V-288|0,132848!=(0|e)&&Pe(132848,e,40),(0|(C=A+228|0))!=(0|e)&&Pe(C,e,40),f[r+88>>2]=e,f[r+84>>2]=47,f[r+80>>2]=137584,dg(C=r+96|0,84089,r+80|0),I=fr(C),(C=f[A+688>>2])&&(mA(C),f[A+688>>2]=0),C=Ae(r+96|0,84577);A:if((0|I)>0&&C)if(g=IA(I),f[A+688>>2]=g,g)if(n=Eg(g,I,C),tr(C),n>>>0<=1032)f[r+16>>2]=r+96,eC(f[30450],85164,r+16|0),g=2;else if(C=f[A+688>>2],g=f[C+4>>2],!(1024!=(0|(I=f[C>>2]))|(0|g)<=0)&(0|g)<134217729){C=g+C|0,f[A+684>>2]=C,o=ue(A+5168|0,0,1024),ue(A+7664|0,0,260),ue(A+7924|0,255,256),ue(A+4788|0,0,380),ue(A+6192|0,0,512);e:if(7!=(0|(g=i[0|C])))for(;;){if(6!=(0|(I=255&g))){if(!I)break e;I=f[A+684>>2],f[r+72>>2]=g<<24>>24,f[r+64>>2]=132848,f[r+68>>2]=C-I,eC(f[30450],88950,r- -64|0);break}g:{r:{C:{a:{I:switch((I=i[0|(g=C+1|0)])-18|0){case 0:break a;case 2:break I;default:break C}for(g=4+(-4&g)|0,f[A+180>>2]=g;g=(C=g)+1|0,!fC(C););for(;I=i[0|C],g=C,C=C+1|0,7!=(0|I););break g}if(g=C+3|0,(0|(C=((0|(C=a[C+2|0]))<65?191:-65)+C|0))>94)break r;f[4788+((C<<2)+A|0)>>2]=g;break r}g=1+((s=Lg(g))+g|0)|0;C:switch(0|s){case 1:f[5168+((I<<2)+A|0)>>2]=g;break r;case 0:f[o>>2]=g;break r;default:break C}s=i[C+2|0],1!=(0|I)?(C=f[A+7664>>2],255==i[0|(k=(b=A+I|0)+7924|0)]&&(a[0|k]=C),a[0|(b=b+7668|0)]=i[0|b]+1,f[(b=(C<<2)+A|0)+6704>>2]=g,f[A+7664>>2]=C+1,f[b+7184>>2]=I|s<<8):f[6188+((s<<2)+A|0)>>2]=g}if(7!=i[0|g])for(;g=1+(Lg(g)+g|0)|0,7!=i[0|g];);}g=i[0|(C=g+1|0)]}for(C=f[A+688>>2]+8|0;;){for(f[692+((g=t<<2)+A|0)>>2]=C;I=i[0|C];)C=C+I|0;for(C=C+1|0,f[692+((4|g)+A|0)>>2]=C;g=i[0|C];)C=g+C|0;if(C=C+1|0,1024==(0|(t=t+2|0)))break}g=0,(0|(A=f[A+324>>2]))<=0|A>>>0<=n>>>0||(f[r+48>>2]=e,eC(f[30450],85519,r+48|0))}else f[r+40>>2]=g,f[r+36>>2]=I,f[r+32>>2]=r+96,eC(f[30450],85349,r+32|0),g=2;else tr(C),g=3;else{if(g||(f[r>>2]=r+96,eC(f[30450],84963,r)),g=1,!C)break A;tr(C)}return V=r+288|0,g}function NA(A){var e,g=0,r=0;for(e=ue(A+344|0,0,256),a[A+364|0]=1,a[A+356|0]=1,a[A+357|0]=1,a[A+358|0]=1,a[A+359|0]=1,a[A+360|0]=1,a[A+361|0]=1,a[A+362|0]=1,a[A+363|0]=1,a[A+348|0]=1,a[A+349|0]=1,a[A+350|0]=1,a[A+351|0]=1,a[A+352|0]=1,a[A+353|0]=1,a[A+354|0]=1,a[A+355|0]=1,a[A+431|0]=3,a[A+429|0]=3,a[A+430|0]=3,a[A+406|0]=3,a[A+407|0]=3,a[A+408|0]=3,a[A+409|0]=3,a[A+410|0]=3,a[A+411|0]=3,a[A+412|0]=3,a[A+413|0]=3,a[A+414|0]=3,a[A+415|0]=3,a[A+416|0]=3,a[A+417|0]=3,a[A+418|0]=3,a[A+419|0]=3,a[A+420|0]=3,a[A+421|0]=3,a[A+440|0]=3,a[A+441|0]=3,a[A+442|0]=3,a[A+443|0]=3,r=21;a[(g=A+r|0)+344|0]=4|i[g+344|0],58!=(0|(g=r+1|0));)a[0|(g=g+e|0)]=4|i[0|g],a[(g=r+e|0)+2|0]=4|i[g+2|0],a[g+3|0]=4|i[g+3|0],r=r+4|0;a[A+346|0]=4|i[A+346|0],a[A+347|0]=4|i[A+347|0],a[A+432|0]=4|i[A+432|0],a[A+433|0]=4|i[A+433|0],a[A+434|0]=4|i[A+434|0],a[A+435|0]=4|i[A+435|0],a[A+436|0]=4|i[A+436|0],a[A+437|0]=4|i[A+437|0],a[A+438|0]=4|i[A+438|0],a[A+439|0]=4|i[A+439|0],a[A+467|0]=4|i[A+467|0],a[A+468|0]=4|i[A+468|0],a[A+470|0]=4|i[A+470|0],a[A+471|0]=4|i[A+471|0],a[A+348|0]=64|i[A+348|0],a[A+349|0]=64|i[A+349|0],a[A+350|0]=64|i[A+350|0],a[A+351|0]=64|i[A+351|0],a[A+352|0]=64|i[A+352|0],a[A+353|0]=64|i[A+353|0],a[A+354|0]=64|i[A+354|0],a[A+355|0]=64|i[A+355|0],a[A+356|0]=64|i[A+356|0],a[A+357|0]=64|i[A+357|0],a[A+358|0]=64|i[A+358|0],a[A+359|0]=64|i[A+359|0],a[A+360|0]=64|i[A+360|0],a[A+361|0]=64|i[A+361|0],a[A+362|0]=64|i[A+362|0],a[A+363|0]=64|i[A+363|0],a[A+364|0]=64|i[A+364|0],a[A+406|0]=64|i[A+406|0],a[A+407|0]=64|i[A+407|0],a[A+408|0]=64|i[A+408|0],a[A+409|0]=64|i[A+409|0],a[A+410|0]=64|i[A+410|0],a[A+411|0]=64|i[A+411|0],a[A+412|0]=64|i[A+412|0],a[A+413|0]=64|i[A+413|0],a[A+414|0]=64|i[A+414|0],a[A+415|0]=64|i[A+415|0],a[A+416|0]=64|i[A+416|0],a[A+417|0]=64|i[A+417|0],a[A+418|0]=64|i[A+418|0],a[A+419|0]=64|i[A+419|0],a[A+420|0]=64|i[A+420|0],a[A+440|0]=64|i[A+440|0],a[A+441|0]=64|i[A+441|0],a[A+429|0]=64|i[A+429|0],a[A+430|0]=64|i[A+430|0],a[A+431|0]=64|i[A+431|0],a[A+442|0]=64|i[A+442|0],a[A+443|0]=64|i[A+443|0],f[A+40>>2]=1,f[A+204>>2]=f[A+600>>2]+77}function PA(A,e,g,r,C,a,I,i,b){var s,t=0,n=0,k=0,o=0,B=0,c=0,Q=0,G=0,w=0,E=0;V=s=V-128|0;A:{e:{if(pe(a,I,i,b,0,0,0,0)){n=65535&b;g:r:{if(32767!=(0|(o=b>>>16&32767))){if(t=4,o)break r;t=a|i|I|n?3:2;break g}t=!(a|i|I|n)}if(32767!=(0|(B=32767&(w=C>>>16|0)))&&t)break e}QA(s+16|0,e,g,r,C,a,I,i,b),iA(s,e=f[s+16>>2],r=f[s+20>>2],C=f[s+24>>2],g=f[s+28>>2],e,r,C,g),r=f[s+8>>2],C=f[s+12>>2],i=f[s>>2],b=f[s+4>>2];break A}if(o=i,(0|pe(e,g,t=r,c=2147483647&C,a,I,i,k=2147483647&b))<=0){if(pe(e,g,t,c,a,I,o,k)){i=e,b=g;break A}QA(s+112|0,e,g,r,C,0,0,0,0),r=f[s+120>>2],C=f[s+124>>2],i=f[s+112>>2],b=f[s+116>>2]}else{if(Q=b>>>16&32767,B?(b=g,i=e):(QA(s+96|0,e,g,t,c,0,0,0,1081540608),t=f[s+104>>2],c=i=f[s+108>>2],B=(i>>>16|0)-120|0,b=f[s+100>>2],i=f[s+96>>2]),Q||(QA(s+80|0,a,I,o,k,0,0,0,1081540608),o=f[s+88>>2],k=a=f[s+92>>2],Q=(a>>>16|0)-120|0,I=f[s+84>>2],a=f[s+80>>2]),G=o,E=65535&k|65536,c=65535&c|65536,(0|B)>(0|Q)){for(;;){if(o=(k=t-G|0)-(n=(0|I)==(0|b)&a>>>0>i>>>0|I>>>0>b>>>0)|0,(0|(n=(c-((t>>>0>>0)+E|0)|0)-(n>>>0>k>>>0)|0))>0|(0|n)>=0){if(t=i,!((i=i-a|0)|o|(b=b-((a>>>0>t>>>0)+I|0)|0)|n)){QA(s+32|0,e,g,r,C,0,0,0,0),r=f[s+40>>2],C=f[s+44>>2],i=f[s+32>>2],b=f[s+36>>2];break A}n=n<<1|o>>>31,t=o<<1|b>>>31}else n=c<<1|t>>>31,t=t<<1|b>>>31;if(c=n,n=b<<1|i>>>31,i<<=1,b=n,!((0|(B=B-1|0))>(0|Q)))break}B=Q}if(o=(k=t-G|0)-(n=(0|I)==(0|b)&a>>>0>i>>>0|I>>>0>b>>>0)|0,k=n=(c-((t>>>0>>0)+E|0)|0)-(n>>>0>k>>>0)|0,(0|n)<0)o=t,k=c;else if(t=i,!((i=i-a|0)|o|(b=b-((a>>>0>t>>>0)+I|0)|0)|k)){QA(s+48|0,e,g,r,C,0,0,0,0),r=f[s+56>>2],C=f[s+60>>2],i=f[s+48>>2],b=f[s+52>>2];break A}if(65535==(0|k)|k>>>0<65535)for(;e=b>>>31|0,B=B-1|0,c=b<<1|i>>>31,i<<=1,b=c,g=e,e=k<<1|o>>>31,o=g|o<<1,k=e,e>>>0<65536;);e=32768&w,(0|B)<=0?(QA(s- -64|0,i,b,o,65535&k|(e|B+120)<<16,0,0,0,1065811968),r=f[s+72>>2],C=f[s+76>>2],i=f[s+64>>2],b=f[s+68>>2]):(r=o,C=65535&k|(e|B)<<16)}}f[A>>2]=i,f[A+4>>2]=b,f[A+8>>2]=r,f[A+12>>2]=C,V=s+128|0}function FA(A,e,g){var r,C,I=0,b=0,s=0,t=0,n=0,k=0,o=0,B=0,c=0;if(V=r=V-1040|0,(0|(b=Sg(A,589824,0)))>=0&&((I=hr(1,2072))?f[I+8>>2]=b:(d(0|b),I=0)),C=I,I){A:if(I=yg(C))for(c=f[30450],B=(r+96|0)+e|0;;){if((0|(b=f[50303]))>=348){f[r+4>>2]=350,f[r>>2]=b+1,eC(c,91860,r);break A}if(46!=i[I+19|0])if(f[r+88>>2]=I+19,f[r+84>>2]=47,f[r+80>>2]=A,dg(I=r+96|0,91924,r+80|0),-31!=(0|(I=fr(I)))){if(!((0|I)<=0)&&(o=Ae(r+96|0,85712))){s=0,a[r+832|0]=0,a[r+752|0]=0,f[r+360>>2]=0,f[r+356>>2]=4,n=0;e:for(;;){for(k=299-n|0;xe(r+912|0,120,o);){I=r+912|0;g:{if(35!=i[r+912|0]){r:if(!((0|(I=Lg(r+912|0)-1|0))<=0))for(;;){if(!(32==(0|(b=a[0|(t=(r+912|0)+I|0)]))|b-9>>>0<5))break r;if(a[0|t]=0,!((0|(I=I-1|0))>0))break}if(!(I=hA(r+912|0)))break g}a[0|I]=0}I=r+912|0;g:if(b=i[r+912|0])for(;;){if(er(b<<24>>24))break g;if(!(b=i[0|(I=I+1|0)]))break}if(a[0|I]=0,i[r+912|0]){I=I+1|0;g:switch(Hr(131904,r+912|0)-1|0){case 0:for(;b=I,I=I+1|0,32==(0|(t=a[0|b]))|t-9>>>0<5;);oC(r+832|0,b,80);continue;case 1:if(a[r+672|0]=0,f[r+364>>2]=5,b=r+672|0,f[r+16>>2]=b,f[r+20>>2]=r+364,aA(I,86237,r+16|0),(b=Lg(b)+2|0)>>>0>=k>>>0)continue;a[0|(I=(r+368|0)+n|0)]=f[r+364>>2],rg(I+1|0,r+672|0),s=s+1|0,n=b+n|0;continue e;case 2:if(f[r+52>>2]=r+360,f[r+48>>2]=r+752,aA(I,86237,r+48|0),!g)continue;f[r+32>>2]=B,eC(c,92042,r+32|0);continue;case 5:break g;default:continue}f[r+64>>2]=r+356,aA(I,87268,r- -64|0)}}break}a[(r+368|0)+n|0]=0,b=Hr(132112,r+752|0),s?(t=Lg(B)+n|0,k=_A((s=hr(28+(Lg(r+832|0)+t|0)|0,1))+24|0,r+368|0,I=n+1|0),f[s+4>>2]=k,I=rg(I+k|0,B),f[s>>2]=I,f[s+8>>2]=I,i[r+832|0]&&(f[s>>2]=rg(2+(t+k|0)|0,r+832|0)),I=f[r+360>>2],a[s+14|0]=0,a[s+12|0]=b,a[s+13|0]=I,a[s+15|0]=f[r+356>>2],tr(o),I=f[50303],f[50303]=I+1,f[201216+(I<<2)>>2]=s):tr(o)}}else FA(r+96|0,e,g);if(!(I=yg(C)))break}iC(f[C+8>>2]),mA(C)}V=r+1040|0}function yA(A,e){var g,r=0,C=0,b=0,s=0,t=0,n=0,k=0,o=0,B=0;V=g=V-352|0;A:if(A||(A=f[136284+(e<<4)>>2])){47!=i[0|A]&&(f[g+12>>2]=A,f[g+4>>2]=47,f[g+8>>2]=47,f[g>>2]=137584,dg(A=g+16|0,84114,g)),a[g+240|0]=0;e:{g:{if(r=Ae(A,84577)){if(-1==(0|Jr(r,20)))break e;if(b=Tr(r),C=Tr(r),t=Tr(r),!(65537!=(0|b)|(0|C)!=f[50754])&(0|t)==C<<1)break g;tr(r),I[g+256>>1]=i[84864]|i[84865]<<8,A=i[84852]|i[84853]<<8|i[84854]<<16|i[84855]<<24,f[g+240>>2]=i[84848]|i[84849]<<8|i[84850]<<16|i[84851]<<24,f[g+244>>2]=A,A=i[84860]|i[84861]<<8|i[84862]<<16|i[84863]<<24,f[g+248>>2]=i[84856]|i[84857]<<8|i[84858]<<16|i[84859]<<24,f[g+252>>2]=A,V=b=V-16|0;r:{if((r=Lg(A=g+240|0))>>>0>=6&&!pg(t=(A+r|0)-6|0,84274,6)){for(k=100;;){for(n=0,V=r=V-16|0,i[227196]||(a[227197]=H(),a[227196]=1),B=+h(),E(s=B/1e3)<0x8000000000000000?(o=E(s)>=1?~~(s>0?u(l(2.3283064365386963e-10*s),4294967295):x(2.3283064365386963e-10*(s-+(~~s>>>0>>>0))))>>>0:0,C=~~s>>>0):(o=-2147483648,C=0),f[r>>2]=C,f[r+4>>2]=o,s=1e3*(B-(+(Cr(C,o,1e3,0)>>>0)+4294967296*+(0|U)))*1e3,C=E(s)<2147483648?~~s:-2147483648,f[r+8>>2]=C,C=t+(r>>>4|0)^G(f[r+8>>2],65537);a[t+n|0]=65+(15&C|C<<1&32),C=C>>>5|0,6!=(0|(n=n+1|0)););if(V=r+16|0,f[b>>2]=384,(0|(r=Sg(A,194,b)))>=0)break r;if(k=k-1|0,20!=f[56798]||!k)break}_A(t,84274,6)}else f[56798]=28;r=-1}V=b+16|0,(0|r)<0||iC(r)}if(!(r=Ae(A,84577))){A=sr(0,f[56798],A);break A}}if((0|(b=fr(A)))<0){tr(r),A=sr(0,0-b|0,A);break A}if(-1==(0|Jr(r,0))){e=f[56798],tr(r),A=sr(0,e,A);break A}if(!(C=OA(f[(t=136280+(e<<4)|0)>>2],b))){tr(r),A=48;break A}if((0|Eg(C,b,r))!=(0|b)){e=f[56798],tr(r),i[g+240|0]&&Xr(g+240|0),mA(C),A=sr(0,e,A);break A}tr(r),i[g+240|0]&&Xr(g+240|0),f[136276+(e<<4)>>2]=(i[C+40|0]|i[C+41|0]<<8|i[C+42|0]<<16|i[C+43|0]<<24)/2,f[t>>2]=C,A=0;break A}e=f[56798],tr(r),A=sr(0,e,A)}else A=28;return V=g+352|0,A}function zA(A,e,g,r,C,I){var b,s=0,t=0,n=0,k=0,o=0,c=0,Q=0,G=0,w=0,E=0,D=0,u=0,l=0,x=0;for(a[0|g]=1,b=1&I,l=1,w=-1,E=-1,k=1,I=e;;){x=u-2|0,D=o,Q=E;A:{e:{for(;;){if(!(c=i[0|I])){E=Q,o=D;break A}if(I=I+1|0,G=f[144464+(c<<2)>>2]){g:{if(1!=(0|(s=i[G+11|0]))){if(16&i[G+6|0]|2!=(0|s))break g;a[0|(o=g+k|0)]=w,s=(0|w)<4|(0|Q)>(0|w),!(2&i[G+4|0])|!b|(0|w)>=0||(a[0|o]=1),o=s?D:k,E=s?Q:w,w=-1,k=k+1|0;break e}if(!B[G+8>>1]){s=0,n=k;r:if(8!=(0|c))s=i[G+14|0],f[C>>2]&&s>>>0>=4||(Q=(0|s)<(0|Q)?Q:s,w=s);else{for(;;){if(c=s,n=n-1|0,f[C>>2]|(0|n)<=0)break r;if((0|(G=a[0|(t=g+n|0)]))>3)break r;if(s=c+1|0,!(G>>>0<2))break}if(a[0|t]=4,D=(0|Q)<4?n:D,Q=(0|Q)<=4?4:Q,n>>>0<2)break r;if(G=3&(n=~c+u|0),s=1,x-c>>>0>=3)for(c=-4&n,n=0;4==i[0|(t=g+s|0)]&&(a[0|t]=3),4==i[t+1|0]&&(a[t+1|0]=3),4==i[t+2|0]&&(a[t+2|0]=3),4==i[t+3|0]&&(a[t+3|0]=3),s=s+4|0,(0|c)!=(0|(n=n+4|0)););if(t=0,!G)break r;for(;4==i[0|(c=g+s|0)]&&(a[0|c]=3),s=s+1|0,(0|G)!=(0|(t=t+1|0)););}if(l)continue;break A}}break}}E=Q,o=D,20==(0|c)&&(a[g+k|0]=b&&(0|w)<0?1:w,k=k+1|0)}if(a[0|e]=c,u=k-1|0,e=e+1|0,l=(0|k)<99)continue}break}a[g+k|0]=1,a[0|e]=0;A:if((0|(e=f[C>>2]))>0)E=4,(0|e)>=(0|k)&&(f[C>>2]=u,e=u),a[e+g|0]=4,o=f[C>>2];else if(5==(0|E)&&(E=4,!((0|k)<2))){if(I=1,Q=1&(e=k-1|0),2!=(0|k))for(D=-2&e,n=0;;){t=4;e:{g:{r:switch(i[0|(e=(s=I)+g|0)]-4|0){case 1:break g;case 0:break r;default:break e}t=2&i[A+14|0]?1:3,s=o}a[0|e]=t,o=s}t=4;e:{g:{r:switch(i[0|(e=(s=I+1|0)+g|0)]-4|0){case 1:break g;case 0:break r;default:break e}t=2&i[A+14|0]?1:3,s=o}a[0|e]=t,o=s}if(I=I+2|0,(0|D)==(0|(n=n+2|0)))break}if(Q){t=4;e:{g:switch(i[0|(e=g+I|0)]-4|0){case 1:break e;case 0:break g;default:break A}t=2&i[A+14|0]?1:3,I=o}a[0|e]=t,o=I}}return f[C>>2]=o,f[r>>2]=k,E}function OA(A,e){var g,r,C=0,a=0,I=0,i=0,b=0,s=0,t=0,n=0,k=0;if(!A)return IA(e);if(e>>>0>=4294967232)return f[56798]=48,0;g=e>>>0<11?16:e+11&-8,I=-8&(r=f[(i=A-8|0)+4>>2]);A:if(3&r){b=I+i|0;e:if(I>>>0>=g>>>0){if((a=I-g|0)>>>0<16)break e;f[i+4>>2]=1&r|g|2,f[(C=i+g|0)+4>>2]=3|a,f[b+4>>2]=1|f[b+4>>2],vA(C,a)}else if(f[57158]!=(0|b))if(f[57157]!=(0|b)){if(2&(a=f[b+4>>2]))break A;if((t=I+(-8&a)|0)>>>0>>0)break A;k=t-g|0;g:if(a>>>0<=255){if(I=f[b+8>>2],C=a>>>3|0,(0|(a=f[b+12>>2]))==(0|I)){f[57152]=f[57152]&jr(-2,C);break g}f[I+12>>2]=a,f[a+8>>2]=I}else{if(n=f[b+24>>2],(0|(s=f[b+12>>2]))==(0|b))if((C=f[(I=b+20|0)>>2])||(C=f[(I=b+16|0)>>2])){for(;a=I,s=C,(C=f[(I=C+20|0)>>2])||(I=s+16|0,C=f[s+16>>2]););f[a>>2]=0}else s=0;else C=f[b+8>>2],f[C+12>>2]=s,f[s+8>>2]=C;if(n){a=f[b+28>>2];r:{if(f[(C=228912+(a<<2)|0)>>2]==(0|b)){if(f[C>>2]=s,s)break r;f[57153]=f[57153]&jr(-2,a);break g}if(f[(f[n+16>>2]==(0|b)?16:20)+n>>2]=s,!s)break g}f[s+24>>2]=n,(C=f[b+16>>2])&&(f[s+16>>2]=C,f[C+24>>2]=s),(C=f[b+20>>2])&&(f[s+20>>2]=C,f[C+24>>2]=s)}}k>>>0<=15?(f[i+4>>2]=1&r|t|2,f[(C=i+t|0)+4>>2]=1|f[C+4>>2]):(f[i+4>>2]=1&r|g|2,f[(a=i+g|0)+4>>2]=3|k,f[(C=i+t|0)+4>>2]=1|f[C+4>>2],vA(a,k))}else{if((a=I+f[57154]|0)>>>0>>0)break A;(C=a-g|0)>>>0>=16?(f[i+4>>2]=1&r|g|2,f[(I=i+g|0)+4>>2]=1|C,f[(a=a+i|0)>>2]=C,f[a+4>>2]=-2&f[a+4>>2]):(f[i+4>>2]=a|1&r|2,f[(C=a+i|0)+4>>2]=1|f[C+4>>2],C=0,I=0),f[57157]=I,f[57154]=C}else{if((I=I+f[57155]|0)>>>0<=g>>>0)break A;f[i+4>>2]=1&r|g|2,C=I-g|0,f[(a=i+g|0)+4>>2]=1|C,f[57155]=C,f[57158]=a}C=i}else{if(g>>>0<256)break A;if(I>>>0>=g+4>>>0&&(C=i,I-g>>>0<=f[57272]<<1>>>0))break A;C=0}return C?C+8|0:(i=IA(e))?(_A(i,A,e>>>0>(C=(3&(C=f[A-4>>2])?-4:-8)+(-8&C)|0)>>>0?C:e),mA(A),i):0}function ZA(){var A,e,g=0,r=0;return A=Or(12),f[A>>2]=22050,e=Or(432),f[(r=e)+4>>2]=0,f[r+8>>2]=0,f[r>>2]=132304,f[r+32>>2]=0,f[r+12>>2]=0,f[r+16>>2]=0,f[r+20>>2]=0,f[r+24>>2]=0,ue(r+40|0,0,376),f[r+420>>2]=0,f[r+424>>2]=-1,a[r+416|0]=1,g=ue(Or(408),0,408),f[r+28>>2]=g,a[g+8|0]=1,f[A+4>>2]=r,g=Or(1096),f[g+8>>2]=22050,f[g+4>>2]=22050,f[g>>2]=132352,f[g+64>>2]=22050,f[g+56>>2]=0,f[g+60>>2]=0,f[g+32>>2]=0,f[g+36>>2]=0,f[g+24>>2]=22050,f[g+16>>2]=0,f[g+20>>2]=0,f[g+40>>2]=0,f[g+44>>2]=0,a[g+48|0]=0,f[g+128>>2]=0,f[g+132>>2]=0,I[g+96>>1]=0,f[g+72>>2]=22050,f[g+136>>2]=0,f[g+140>>2]=0,I[g+168>>1]=0,f[g+144>>2]=22050,f[g+200>>2]=0,f[g+204>>2]=0,f[g+208>>2]=0,f[g+212>>2]=0,f[g+216>>2]=22050,I[g+240>>1]=0,f[g+280>>2]=0,f[g+284>>2]=0,f[g+272>>2]=0,f[g+276>>2]=0,f[g+288>>2]=22050,I[g+312>>1]=0,f[g+344>>2]=0,f[g+348>>2]=0,f[g+352>>2]=0,f[g+356>>2]=0,f[g+360>>2]=22050,I[g+384>>1]=0,f[g+416>>2]=0,f[g+420>>2]=0,f[g+424>>2]=0,f[g+428>>2]=0,f[g+432>>2]=22050,I[g+456>>1]=0,f[g+488>>2]=0,f[g+492>>2]=0,f[g+496>>2]=0,f[g+500>>2]=0,f[g+504>>2]=22050,I[g+528>>1]=1,f[g+560>>2]=0,f[g+564>>2]=0,f[g+568>>2]=0,f[g+572>>2]=0,I[g+600>>1]=0,f[g+576>>2]=22050,f[g+640>>2]=0,f[g+644>>2]=0,f[g+632>>2]=0,f[g+636>>2]=0,I[g+680>>1]=0,f[g+656>>2]=22050,f[g+648>>2]=22050,f[g+720>>2]=0,f[g+724>>2]=0,f[g+712>>2]=0,f[g+716>>2]=0,I[g+752>>1]=0,f[g+728>>2]=22050,f[g+792>>2]=0,f[g+796>>2]=0,f[g+784>>2]=0,f[g+788>>2]=0,I[g+824>>1]=0,f[g+800>>2]=22050,f[g+864>>2]=0,f[g+868>>2]=0,f[g+856>>2]=0,f[g+860>>2]=0,f[g+872>>2]=22050,I[g+896>>1]=0,f[g+936>>2]=0,f[g+940>>2]=0,f[g+928>>2]=0,f[g+932>>2]=0,I[g+968>>1]=0,f[g+944>>2]=22050,f[g+1008>>2]=0,f[g+1012>>2]=0,f[g+1e3>>2]=0,f[g+1004>>2]=0,I[g+1040>>1]=0,f[g+1016>>2]=22050,f[g+1088>>2]=0,f[(r=g+1080|0)>>2]=0,f[r+4>>2]=0,f[(r=g+1072|0)>>2]=0,f[r+4>>2]=0,f[A+8>>2]=g,HC[f[f[g>>2]+4>>2]](g,e),A}function KA(A,e,g){var r=0,C=0,a=0,I=0,b=0,s=0,t=0,n=0,k=0,o=0,B=0;A:if(k=f[50759]){if(r=f[50980],C=G(r,80)+222176|0,(0|(s=(0|(C=(0|(C=(f[C+12>>2]+f[C>>2]|0)/(0|A)|0))<(0|(n=((0|G(f[50754],19))/40<<16)/(0|A)|0))?C:n))>=399?399:C))>=0&&(ue(e,0,4+(s<<2)|0),r=f[50980]),(0|r)>=0)for(o=f[50801];;){if(a=G(I,80)+222176|0,f[a+4>>2]&&(b=f[a>>2])){if(B=b+f[a+12>>2]|0,r=1+((b-f[a+8>>2]|0)/(0|A)|0)|0,(0|b)>(0|(r=G(C=(0|r)<=1?1:r,A))))for(;f[(t=(C<<2)+e|0)>>2]=f[t>>2]+G(f[a+4>>2],i[((b-r|0)/(f[a+8>>2]>>8)|0)+o|0]),C=C+1|0,(0|b)>(0|(r=A+r|0)););if(!((0|r)>=(0|B)))for(;f[(t=(C<<2)+e|0)>>2]=f[t>>2]+G(f[a+4>>2],i[((r-b|0)/(f[a+12>>2]>>8)|0)+o|0]),C=C+1|0,(0|B)>(0|(r=A+r|0)););}if(!((0|(I=I+1|0))<=f[50980]))break}if(C=1,!((0|(a=65536e3/(0|A)|0))<=0||(0|(r=G(f[55565],10)))<=0))for(a=(0|r)/(0|a)|0;f[(b=(C<<2)+e|0)>>2]=f[b>>2]+r,C=C+1|0,(0|(r=r-a|0))>0;);if((0|I)<=8)for(;C=(r=I<<2)+203216|0,a=G(I,80)+222176|0,b=f[a+4>>2]>>14,f[C>>2]=(0|G(G(b,b),5))/2,g?r=f[r+203264>>2]:(b=r+203264|0,r=f[a>>2]/(0|A)|0,f[b>>2]=r),(0|r)>=(0|n)&&(f[C>>2]=0),9!=(0|(I=I+1|0)););if(C=0,(0|s)>=0)for(r=0;a=f[(I=(r<<2)+e|0)>>2]>>15,a=G(a,a)>>8,f[I>>2]=a,(0|C)<=524287999&&(f[I>>2]=G(a,i[344+((C>>19)+k|0)|0])>>13),C=A+C|0,I=(0|r)!=(0|s),r=r+1|0,I;);if(f[e+4>>2]=(0|G(f[e+4>>2],i[203300]?6:10))/8,1&g)for(A=f[50826],r=1;;){if(f[(g=r<<2)+203312>>2]=f[e+g>>2]-f[A+g>>2]>>3,30==(0|(g=r+1|0)))break A;f[(g<<=2)+203312>>2]=f[e+g>>2]-f[A+g>>2]>>3,r=r+2|0}}else s=1;return s}function WA(A,e,g,r){var C,b=0,s=0;V=C=V-176|0,a[0|A]=0,I[C+80>>1]=24320,f[C+104>>2]=0,f[C+108>>2]=0,a[82+(Fg(g,b=C+80|2)+C|0)|0]=0;A:{e:{if(!r){if(r=C+80|1,f[C+12>>2]=r,TA(e,C+12|0,C+16|0,C+104|0,0,0)||(f[C+12>>2]=b,TA(e,C+12|0,C+16|0,C+104|0,0,0)||(a[C+81|0]=32,GA(e,b,C+16|0,60,0,0,0))),(g=i[C+16|0])&&21!=(0|g))break e;g:{if(25966!=f[e+212>>2]){if(vg(85719,188772,189296),a[C+81|0]=95,f[C+12>>2]=r,TA(f[47193],C+12|0,C+16|0,C+104|0,0,0)||(f[C+12>>2]=b,TA(f[47193],C+12|0,C+16|0,C+104|0,0,0)),i[C+16|0])break g;qr(f[f[32972]+60>>2]),g=i[C+16|0]}if(255&g)break e;e=i[87124]|i[87125]<<8|i[87126]<<16|i[87127]<<24,g=i[87120]|i[87121]<<8|i[87122]<<16|i[87123]<<24,a[0|A]=g,a[A+1|0]=g>>>8,a[A+2|0]=g>>>16,a[A+3|0]=g>>>24,a[A+4|0]=e,a[A+5|0]=e>>>8,a[A+6|0]=e>>>16,a[A+7|0]=e>>>24,a[A+16|0]=i[87136],e=i[87132]|i[87133]<<8|i[87134]<<16|i[87135]<<24,g=i[87128]|i[87129]<<8|i[87130]<<16|i[87131]<<24,a[A+8|0]=g,a[A+9|0]=g>>>8,a[A+10|0]=g>>>16,a[A+11|0]=g>>>24,a[A+12|0]=e,a[A+13|0]=e>>>8,a[A+14|0]=e>>>16,a[A+15|0]=e>>>24;break A}r=C+16|0,b=C+104|0,V=g=V-112|0,(s=f[47193])?(fA(s,r,b,-1,0),Ye(b=r,r=g+48|0),e=f[e+212>>2],a[g+43|0]=e>>>24,a[0|(s=(b=g+43|0)+(e>>>0>16777215)|0)]=e>>>16,a[0|(s=s+!!(16711680&e)|0)]=e>>>8,a[0|(s=s+!!(65280&e)|0)]=e,a[s+!!(255&e)|0]=0,f[g+16>>2]=85719,f[g+24>>2]=b,f[g+20>>2]=r,dg(A,85662,g+16|0)):(fA(e,r,b,-1,0),Ye(r,e=g+48|0),f[g>>2]=e,dg(A,85451,g)),V=g+112|0,qr(f[f[32972]+60>>2]);break A}if(f[C+12>>2]=b,TA(e,C+12|0,C+16|0,C+104|0,0,0),!i[C+16|0])break A}fA(g=e,e=C+16|0,C+104|0,-1,0),Ye(g=e,e=C+112|0),f[C>>2]=e,dg(A,85451,C)}return V=C+176|0,A}function XA(A,e){var g,r=0,C=0,I=0,i=0,b=0,s=0,t=0,n=0,k=0,o=0,B=0,c=0,Q=0,G=0,w=0,E=0,D=0,u=0;if(V=g=V-112|0,f[g+72>>2]=-1,f[(r=g- -64|0)>>2]=-1,f[r+4>>2]=-1,f[g+56>>2]=-1,f[g+60>>2]=-1,f[g+48>>2]=-1,f[g+52>>2]=-1,f[g+40>>2]=-1,f[g+44>>2]=-1,f[g+32>>2]=-1,f[g+36>>2]=-1,f[g+24>>2]=-1,f[g+28>>2]=-1,f[g+16>>2]=-1,f[g+20>>2]=-1,(0|e)>0){for(I=f[g+72>>2],i=f[g+68>>2],b=f[g+64>>2],s=f[g+60>>2],t=f[g+56>>2],n=f[g+52>>2],k=f[g+48>>2],o=f[g+44>>2],B=f[g+40>>2],c=f[g+36>>2],Q=f[g+32>>2],G=f[g+28>>2],w=f[g+24>>2],E=f[g+20>>2],D=f[g+16>>2];C=I,I=(0|(I=f[(r=134912+(u<<6)|0)+60>>2]))<0?C:I,C=i,i=(0|(i=f[r+56>>2]))<0?C:i,C=b,b=(0|(b=f[r+52>>2]))<0?C:b,C=s,s=(0|(s=f[r+48>>2]))<0?C:s,C=t,t=(0|(t=f[r+44>>2]))<0?C:t,C=n,n=(0|(n=f[r+40>>2]))<0?C:n,C=k,k=(0|(k=f[r+36>>2]))<0?C:k,C=o,o=(0|(o=f[r+32>>2]))<0?C:o,C=B,B=(0|(B=f[r+28>>2]))<0?C:B,C=c,c=(0|(c=f[r+24>>2]))<0?C:c,C=Q,Q=(0|(Q=f[r+20>>2]))<0?C:Q,C=G,G=(0|(G=f[r+16>>2]))<0?C:G,C=w,w=(0|(w=f[r+12>>2]))<0?C:w,C=E,E=(0|(E=f[r+8>>2]))<0?C:E,D=(0|(r=f[r+4>>2]))<0?D:r,(0|(u=u+1|0))!=(0|e););f[g+72>>2]=I,f[g+68>>2]=i,f[g+64>>2]=b,f[g+60>>2]=s,f[g+56>>2]=t,f[g+52>>2]=n,f[g+48>>2]=k,f[g+44>>2]=o,f[g+40>>2]=B,f[g+36>>2]=c,f[g+32>>2]=Q,f[g+28>>2]=G,f[g+24>>2]=w,f[g+20>>2]=E,f[g+16>>2]=D}for(e=0;;){if((0|(r=f[(I=e<<2)+(g+16|0)>>2]))!=f[(I=I+134848|0)>>2]){a[g+80|0]=0;A:{e:switch(e-1|0){case 4:f[47201]=r-1;break A;case 5:f[47200]=r;break A;case 0:case 1:case 2:case 3:case 11:break e;default:break A}f[g+4>>2]=r,f[g>>2]=1,f[g+8>>2]=a[e+102812|0],dg(g+80|0,91942,g)}f[I>>2]=r,r=g+80|0,rg(f[A>>2]+189424|0,r),f[A>>2]=f[A>>2]+Lg(r)}if(15==(0|(e=e+1|0)))break}V=g+112|0}function LA(A){var e,g,r,C=0,I=0,b=0,s=0;for(C=17,f[A+328>>2]=17,f[A+224>>2]=0,f[A+216>>2]=1105,f[A+220>>2]=1072,f[A+600>>2]=1056,f[A+8180>>2]=105296,b=ue(A+344|0,0,256),a[A+393|0]=1,a[A+365|0]=1,a[A+360|0]=1,a[A+545|0]=1,a[A+529|0]=1,a[A+391|0]=1,a[A+379|0]=1,a[A+374|0]=1,a[A+489|0]=1,a[A+487|0]=1,a[A+398|0]=1,a[A+387|0]=1,a[A+388|0]=2,a[A+389|0]=1,a[A+390|0]=1,a[A+385|0]=2,a[A+383|0]=2,a[A+368|0]=1,a[A+369|0]=2,I=104224;a[0|(C=C+b|0)]=4|i[0|C],C=b+i[I+1|0]|0,a[0|C]=4|i[0|C],C=b+i[I+2|0]|0,a[0|C]=4|i[0|C],C=i[0|(I=I+3|0)],104251!=(0|I););a[A+386|0]=8|i[A+386|0],a[A+382|0]=8|i[A+382|0],a[A+384|0]=8|i[A+384|0],a[A+369|0]=16|i[A+369|0],a[A+370|0]=16|i[A+370|0],a[A+371|0]=16|i[A+371|0],I=i[A+361|0],b=i[A+362|0],C=i[A+363|0],s=i[A+364|0],e=i[A+366|0],g=i[A+367|0],a[A+372|0]=16|i[A+372|0],a[A+373|0]=16|i[A+373|0],a[A+375|0]=16|i[A+375|0],a[A+376|0]=16|i[A+376|0],a[A+377|0]=16|i[A+377|0],a[A+378|0]=16|i[A+378|0],a[A+380|0]=16|i[A+380|0],a[A+381|0]=16|i[A+381|0],a[A+383|0]=16|i[A+383|0],a[A+385|0]=16|i[A+385|0],r=i[A+388|0],a[A+367|0]=48|g,a[A+366|0]=40|e,a[A+364|0]=48|s,a[A+363|0]=48|C,a[A+362|0]=48|b,a[A+361|0]=48|I,a[A+388|0]=80|r,I=i[A+390|0],b=i[A+391|0],C=i[A+393|0],a[A+360|0]=128|i[A+360|0],s=i[A+365|0],a[A+393|0]=192|C,a[A+365|0]=128|s,a[A+368|0]=128|i[A+368|0],a[A+374|0]=128|i[A+374|0],a[A+379|0]=128|i[A+379|0],a[A+387|0]=128|i[A+387|0],C=i[A+389|0],a[A+391|0]=192|b,a[A+390|0]=192|I,a[A+389|0]=128|C,a[A+529|0]=128|i[A+529|0],a[A+545|0]=128|i[A+545|0],a[A+489|0]=128|i[A+489|0],a[A+487|0]=128|i[A+487|0],a[A+398|0]=128|i[A+398|0]}function TA(A,e,g,r,C,b){var s,t=0,n=0,k=0,o=0,B=0,c=0;V=s=V-192|0,t=B=f[e>>2];A:{e:{for(;n=1,(0|(k=a[0|t]))>=0||(n=2,k>>>0<4294967264||(n=k>>>0<4294967280?3:4)),!(32!=i[0|(k=n+t|0)]|46!=i[k+1|0]);){if(o-160>>>0<4294967135)break e;_A((c=s+32|0)+o|0,t,n),a[(n=n+o|0)+c|0]=46,t=k+3|0,o=n+1|0}if(o){for(n=0;k=n,n=n+1|0,223&i[t+k|0];);if(!((c=k+o|0)+1>>>0>160)&&(_A((n=s+32|0)+o|0,t,k),a[n+c|0]=0,uA(A,n,t,g,r,C,b))){f[r>>2]=128|f[r>>2],f[33264]=o,A=1;break A}}}for(t=0;;){if(B=(n=B)+1|0,223&(n=i[0|n]))if(!t|46!=(0|n)|a[31+(t+s|0)|0]-48>>>0>=10){if(a[(s+32|0)+t|0]=n,n=159,159!=(0|(t=t+1|0)))continue}else n=t;else n=t;break}a[(t=s+32|0)+n|0]=0,t=uA(A,t,B,g,r,C,b);e:if(8&i[r+3|0]){if(!Qr(g,k=A+268|0)){if(k=f[A+288>>2]+1|0,f[A+288>>2]=k,(0|k)<4)break e;a[0|g]=0;break e}oC(k,g,20),f[A+288>>2]=1}else f[A+288>>2]=0;e:{if(!t){if(t=0,8&i[r+5|0]&&(k=Te(s+28|0,t=s+32|95==i[s+32|0]),Ie(A,f[s+28>>2],g),t=t+k|0),!(n>>>0<2|t)){if(a[0|g]=0,!(16&C&&101==i[0|(t=31+(n+s|0)|0)])){if(!(4096&C))break e;if(i[0|(t=(n=(s+32|0)+n|0)-1|0)]!=i[n-2|0])break e}a[0|t]=0,t=uA(A,s+32|0,B,g,r,C,b)}if(!t)break e}if(o=f[r>>2],i[A+172|0]&&(o^=536870912,f[r>>2]=o),A=1,!(536870912&o))break A;2&C&&(I[66448]=8192,f[s+16>>2]=g,dg(132898,87470,s+16|0),A=f[e>>2],f[e>>2]=132898,8&i[188788]&&(_A(e=s+32|0,r=A,A=t-A|0),a[A+e|0]=0,f[s+4>>2]=132898,A=f[47195],f[s>>2]=e,eC(A,87652,s)))}a[0|g]=0,A=0}return V=s+192|0,A}function VA(A,e){var g=0,r=0,C=0;g=31&A;A:{e:{g:{if(96==(0|(A&=96)))A=-1;else{if(64!=(0|A))break g;A=1}if(g>>>0>=15)break A;e=f[203136+(g<<2)>>2]+G(A,e)|0;break e}if(g>>>0>=15)break A}A=f[(r=g<<2)+105616>>2],f[r+203136>>2]=(0|e)>=0?(0|A)>(0|e)?e:A:0}A:{e:{g:{r:{C:switch(g-1|0){case 5:if(!(A=f[50759]))break e;f[54728]=f[50982],e=f[50979],g=f[50978],ue(205184,0,11e3),f[51293]=0,e=(g=(C=(0|(r=f[50789]))>0)?130:(0|g)>=5499?5499:g)?C?r:(0|e)>=100?100:e:0,f[50755]=e,g=(0|G(g,f[50754]))/1e3|0,f[51292]=g,f[54729]=(0|e)>20?g<<1:e?g:0,f[33037]=(0|G(500-e|0,(0|G(i[f[50797]+105596|0],(0|G(f[50787],55))/100|0))/16|0))/500;break r;case 0:break C;case 2:case 12:break A;case 4:break g;default:break e}if(!(A=f[50759]))break e}return e=256,(0|(g=(0|(g=f[50785]))>=101?101:g))>=51&&(e=256+(((G(g,25)-1250&65535)>>>0)/50|0)|0),I[A+164>>1]=(0|G(I[A+236>>1],e))/256,I[A+166>>1]=(0|G(I[A+238>>1],e))/256,I[A+168>>1]=(0|G(I[A+240>>1],e))/256,I[A+170>>1]=(0|G(I[A+242>>1],e))/256,I[A+172>>1]=(0|G(I[A+244>>1],e))/256,I[A+174>>1]=(0|G(I[A+246>>1],e))/256,A=f[50790],I[102e3]=(0|G(I[102036],G(A,-3)+256|0))/256,void(I[101999]=(0|G(I[102035],G(A,-6)+256|0))/256)}f[50759]&&(f[54728]=f[50982],A=f[50979],e=f[50978],ue(205184,0,11e3),f[51293]=0,A=(e=(r=(0|(g=f[50789]))>0)?130:(0|e)>=5499?5499:e)?r?g:(0|A)>=100?100:A:0,f[50755]=A,e=(0|G(e,f[50754]))/1e3|0,f[51292]=e,f[54729]=(0|A)>20?e<<1:A?e:0,f[33037]=(0|G(500-A|0,(0|G(i[f[50797]+105596|0],(0|G(f[50787],55))/100|0))/16|0))/500)}return}f[33037]=(0|G(i[f[50797]+105596|0],(0|G(f[50787],55))/100|0))/16}function JA(A,e,g){var r=0,C=0,I=0,b=0,s=0,t=0,n=0,k=0,o=0,B=0;g&&(f[g>>2]=0);A:{e:if(!((0|(r=a[0|A]))<0)){for(;;){if(32==(0|(I=255&r))|I-9>>>0<5){if((0|(r=a[0|(A=A+1|0)]))>=0)continue;break e}break}if(!(255&r))break A}for(;;){if(32==(0|(r=t=255&r))|r-9>>>0<5)break A;if(124!=(0|t)||124==(0|(r=i[0|(I=A+1|0)]))){e:{if((0|(o=f[36115]))>=2){for(r=1,I=-1,n=0;;){g:if(!(!(k=f[144464+(r<<2)>>2])|15==i[k+11|0])){s=f[k>>2];r:{C:{if(t>>>0>=33){if(B=0,b=0,(255&s)==(0|t)&&(b=1,(C=i[A+1|0])>>>0<33|(0|C)!=(s>>>8&255)||(b=2,(C=i[A+2|0])>>>0<33|(0|C)!=(s>>>16&255)||(b=(C=(C=i[A+3|0])>>>0>32&(0|C)==(s>>>24|0))?4:3,B=0-C|0))),(0|I)>=(0|b))break g;if(C=4,!(1&B))break C;break r}if(b=0,(0|I)>=0)break g}if(s>>>((C=b)<<3)&255)break g}n=i[k+10|0],I=C}if((0|o)==(0|(r=r+1|0)))break}if(n)break e}return g&&Te(g,A),void(a[0|e]=0)}a[0|e]=n,A=((0|I)<=1?1:I)+A|0,e=I=e+1|0;e:if(21==(0|n)){g:if(32==(0|(C=i[0|A]))|C-9>>>0<5)r=I;else if(r=I,C)for(;;){if(a[0|r]=ar(C),r=r+1|0,32==(0|(C=i[0|(A=A+1|0)]))|C-9>>>0<5)break g;if(!C)break}if(a[0|r]=0,!C){if(e=r,Qr(I,85593))break e;return void(a[0|I]=0)}a[0|r]=124,e=r+1|0}r=i[0|A]}else A=I;if(!(255&r))break}}a[0|e]=0}function RA(A,e){var g=0,r=0,C=0,a=0,I=0;A:{e:{g:{r:{C:switch((0|(g=f[A+4>>2]))==f[A+104>>2]?g=ce(A):(f[A+4>>2]=g+1,g=i[0|g]),g-43|0){case 0:case 2:break C;default:break r}if(a=45==(0|g),I=!e,(0|(g=f[A+4>>2]))==f[A+104>>2]?g=ce(A):(f[A+4>>2]=g+1,g=i[0|g]),I|(e=g-58|0)>>>0>4294967285)break g;if(f[A+116>>2]<0)break e;f[A+4>>2]=f[A+4>>2]-1;break e}e=g-58|0}if(!(e>>>0<4294967286)){if((e=g-48|0)>>>0<10){for(;C=(0|(r=(r=G(r,10)+g|0)-48|0))<214748364,(0|(e=f[A+4>>2]))==f[A+104>>2]?g=ce(A):(f[A+4>>2]=e+1,g=i[0|e]),C&(e=g-48|0)>>>0<=9;);C=r>>31}g:if(!(e>>>0>=10))for(;;){if(e=(r=Cr(r,C,10,0))+g|0,g=U,g=e>>>0>>0?g+1|0:g,r=e-48|0,C=g-(e>>>0<48)|0,(0|(e=f[A+4>>2]))==f[A+104>>2]?g=ce(A):(f[A+4>>2]=e+1,g=i[0|e]),(e=g-48|0)>>>0>9)break g;if(!(r>>>0<2061584302&(0|C)<=21474836|(0|C)<21474836))break}if(e>>>0<10)for(;(0|(e=f[A+4>>2]))==f[A+104>>2]?e=ce(A):(f[A+4>>2]=e+1,e=i[0|e]),e-48>>>0<10;);(0|(e=f[A+116>>2]))>0|(0|e)>=0&&(f[A+4>>2]=f[A+4>>2]-1),A=r,r=a?0-A|0:A,C=a?0-(!!(0|A)+C|0)|0:C;break A}}if(C=-2147483648,!(f[A+116>>2]<0))return f[A+4>>2]=f[A+4>>2]-1,U=-2147483648,0}return U=C,r}function UA(A){var e=0,g=0,r=0,C=0;if(f[36432]=110,f[36433]=100,f[36434]=450,f[36430]=5,e=f[203136+(2==(0|A)?32:8)>>2],r=f[32972],(0|(g=f[r+84>>2]))>0&&(e=(0|G(e,g))/100|0),g=(0|e)>=359?359:e,g=(0|(e=(0|e)>=450?450:e))>399?6:(0|e)>379?7:i[((0|g)<=80?80:g)+101856|0],1&A&&(f[32526]=(0|G(g,f[r+72>>2]))/256,f[32527]=(0|G(g,f[r+76>>2]))/256,f[32528]=(0|G(g,f[r+80>>2]))/256,g>>>0>7||(C=g-1|0,f[32528]=C,f[32526]=g,f[32527]=C)),2&A){A=f[r+72>>2];A:{e:{g:{r:{C:{a:{I:{f:{if((0|e)>=351)r=e-350|0,f[36432]=85-(((255&r)>>>0)/3|0)&255,r=60-(r>>>3|0)|0;else{if((0|e)<251)break f;r=e-250|0,f[36432]=110-(r>>>2|0),r=110-(r>>>1|0)|0}if(f[36433]=r,A=(0|G(A,g))/256|0,f[36431]=110+((0|G(A,150))/128|0),e>>>0<=349)break I;if(g=e-350|0,f[36431]=i[g+102224|0],e>>>0<390)break C;if(f[36434]=450+((e+112<<24>>24)/-2<<24>>24),e>>>0<441)break a;f[36434]=860-e,A=12;break e}A=(0|G(A,g))/256|0,f[36431]=(0|e)>=170?110+((0|G(A,150))/128|0)|0:128+((A<<7)/130|0)|0}A=(A<<8)/115|0;break e}if(A=12,e>>>0>430)break e;if(A=13,e>>>0<=400)break r;break e}if(A=(A<<8)/115|0,f[36428]=A,e>>>0<375)break g}A=14;break e}if((0|e)<351)break A;A=i[g+102336|0]}f[36428]=A}f[36429]=(0|A)<=16?16:A}}function jA(A,e,g){var r,C,a,b;r=.000244140625*+f[50767],Q[g>>3]=r,Q[g+40>>3]=.015625*+f[A+112>>2],Q[g+48>>3]=.015625*+f[A+276>>2],Q[g+56>>3]=.00390625*+(0|G(I[A+166>>1],I[e+4>>1]))+ +I[A+220>>1],Q[g+64>>3]=.00390625*+(0|G(I[A+168>>1],I[e+6>>1]))+ +I[A+222>>1],Q[g+72>>3]=.00390625*+(0|G(I[A+170>>1],I[e+8>>1]))+ +I[A+224>>1],Q[g+80>>3]=.00390625*+(0|G(I[A+172>>1],I[e+10>>1]))+ +I[A+226>>1],Q[g+88>>3]=.00390625*+(0|G(I[A+174>>1],I[e+12>>1]))+ +I[A+228>>1],C=I[A+230>>1],a=I[A+176>>1],b=I[e+14>>1],f[g+112>>2]=0,f[g+116>>2]=1080623104,f[g+104>>2]=0,f[g+108>>2]=1081032704,Q[g+96>>3]=.00390625*+(0|G(a,b))+ +(0|C),i[e+40|0]?(f[g+184>>2]=0,f[g+188>>2]=1072693248,Q[g+104>>3]=i[e+40|0]<<1):(f[g+184>>2]=0,f[g+188>>2]=0),Q[g+120>>3]=.00390625*+I[A+202>>1]*+(i[e+35|0]<<1),Q[g+128>>3]=.00390625*+I[A+204>>1]*+(i[e+36|0]<<1),Q[g+136>>3]=.00390625*+I[A+206>>1]*+(i[e+37|0]<<1),e=i[e+38|0],A=I[A+208>>1],f[g+176>>2]=0,f[g+180>>2]=1079574528,f[g+160>>2]=0,f[g+164>>2]=1083129856,f[g+152>>2]=0,f[g+156>>2]=1083129856,f[g+352>>2]=0,f[g+356>>2]=1072693248,f[g+168>>2]=0,f[g+172>>2]=1079574528,Q[g+144>>3]=.00390625*+(0|A)*+(e<<1),A=f[50779],Q[g+368>>3]=r,Q[g+360>>3]=+(0|A)/100*3}function SA(A){var e=0;S(A,Je(A));A:{e:{g:{r:{C:{a:{I:{f:{i:{b:{if((0|(A=-1048576&U))<268435455|(0|A)<=268435455){s:{t:{if((0|A)<33554431|(0|A)<=33554431){if((0|A)<8388607|(0|A)<=8388607){if(e=524328,!0&-2147483648==(0|A))break A;if(0|-2143289344!=(0|A))break e;return 557096}if(!0&8388608==(0|A))break t;if(0|16777216!=(0|A))break e;return 524358}if((0|A)>71303167)break s;if(!0&33554432==(0|A))break g;if(0|67108864!=(0|A))break e}return 266270}if(!0&71303168==(0|A))break b;if(!0&134217728==(0|A))break r;if(0|138412032!=(0|A))break e;return 294942}if((0|A)<542113791|(0|A)<=542113791){if((0|A)<536870911|(0|A)<=536870911){if(!0&268435456==(0|A))break C;if(0|272629760!=(0|A))break e;return 299028}if(!0&536870912==(0|A))break I;if(!0&538968064==(0|A))break a;if(0|541065216!=(0|A))break e;return 569389}if((0|A)<1075838975|(0|A)<=1075838975){if(!0&542113792==(0|A))break b;if(0|1073741824!=(0|A))break e;return 532520}if(!0&1075838976==(0|A))break f;if(!0&1077936128==(0|A))break i;if(0|1078984704!=(0|A))break e}return 299038}return 565288}return 1581096}return 536621}return 1585197}return 266260}return 262174}return 2396190}e=16384}return e}function qA(A,e,g,r,C,a){var I,b,s=0,t=0,n=0,k=0;if(b=8388607&A,t=f[34456],s=i[0|(A=b+t|0)]|i[A+1|0]<<8){I=!(n=i[A+2|0]),A=f[36434]<0?(0|G(g,C))/256|0:g,g=(0|G(C,f[36431]))/256|0,g=(0|A)<(0|(g=(4&r)>>>2|0&&(0|g)>(0|C)?C:g))?g:A,n||(s=s>>>1|0,g=(0|g)/2|0);A:if(!((0|a)<0)){if(r=b+4|0,256&e)A=f[50758],f[36439]=A,f[(e=216192+(A<<4)|0)>>2]=7,f[e+8>>2]=r+t,f[e+4>>2]=s<<16|g,a=n|a<<8;else{if(A=f[50758],f[36439]=A,f[(A=216192+(A<<4)|0)>>2]=6,a=n|a<<8,f[A+12>>2]=a,f[A+8>>2]=r+t,t=A,A=G(e=s>>>2|0,3),C=(0|g)>(0|s),f[t+4>>2]=C?A:g,t=f[50758]+1|0,f[50758]=(0|t)<=169?t:0,(0|A)<(0|(g=C?g-A|0:0)))for(C=e<<1,n=r+(n?e:C)|0;e=f[50758],f[36439]=e,f[(e=216192+(e<<4)|0)>>2]=6,f[e+4>>2]=C,f[e+12>>2]=a,f[e+8>>2]=n+f[34456],e=f[50758]+1|0,f[50758]=(0|e)<=169?e:0,(0|A)<(0|(g=g-C|0)););if((0|g)<=0)break A;A=f[50758],f[36439]=A,f[(e=216192+(A<<4)|0)>>2]=6,f[e+4>>2]=g,f[e+8>>2]=f[34456]+(r+(s-g<>2]=a,A=f[50758]+1|0,f[50758]=(0|A)<=169?A:0}}}function _A(A,e,g){var r,C=0,I=0;if(g>>>0>=512)return N(0|A,0|e,0|g),A;r=A+g|0;A:if(3&(A^e))if(r>>>0<4)g=A;else if((C=r-4|0)>>>0>>0)g=A;else for(g=A;a[0|g]=i[0|e],a[g+1|0]=i[e+1|0],a[g+2|0]=i[e+2|0],a[g+3|0]=i[e+3|0],e=e+4|0,C>>>0>=(g=g+4|0)>>>0;);else{e:if(3&A)if(g)for(g=A;;){if(a[0|g]=i[0|e],e=e+1|0,!(3&(g=g+1|0)))break e;if(!(g>>>0>>0))break}else g=A;else g=A;if(!((C=-4&r)>>>0<64||(I=C+-64|0)>>>0>>0))for(;f[g>>2]=f[e>>2],f[g+4>>2]=f[e+4>>2],f[g+8>>2]=f[e+8>>2],f[g+12>>2]=f[e+12>>2],f[g+16>>2]=f[e+16>>2],f[g+20>>2]=f[e+20>>2],f[g+24>>2]=f[e+24>>2],f[g+28>>2]=f[e+28>>2],f[g+32>>2]=f[e+32>>2],f[g+36>>2]=f[e+36>>2],f[g+40>>2]=f[e+40>>2],f[g+44>>2]=f[e+44>>2],f[g+48>>2]=f[e+48>>2],f[g+52>>2]=f[e+52>>2],f[g+56>>2]=f[e+56>>2],f[g+60>>2]=f[e+60>>2],e=e- -64|0,I>>>0>=(g=g- -64|0)>>>0;);if(g>>>0>=C>>>0)break A;for(;f[g>>2]=f[e>>2],e=e+4|0,C>>>0>(g=g+4|0)>>>0;);}if(g>>>0>>0)for(;a[0|g]=i[0|e],e=e+1|0,(0|r)!=(0|(g=g+1|0)););return A}function $A(A){var e=0,g=0,r=0,C=0,a=0,I=0,i=0,k=0;n(+A),e=0|b(1),b(0);A:{if((r=(e=e>>>20&2047)-969|0)>>>0<63)k=e;else{if((0|r)<0)return A+1;if(!(e>>>0<1033)){if(n(+A),r=0|b(1),g=0,!(0|b(0))&-1048576==(0|r))break A;return e>>>0>=2047?A+1:(0|r)<0?(Q[(e=V-16|0)+8>>3]=12882297539194267e-247,12882297539194267e-247*Q[e+8>>3]):(Q[(e=V-16|0)+8>>3]=3105036184601418e216,3105036184601418e216*Q[e+8>>3])}}if(g=Q[14409],a=(g=(A=(g=(C=Q[14408]*A+g)-g)*Q[14411]+(g*Q[14410]+A))*A)*g*(A*Q[14415]+Q[14414]),g*=A*Q[14413]+Q[14412],n(+C),b(1),i=0|b(0),A=a+(g+(Q[(r=i<<4&2032)+115376>>3]+A)),I=f[(r=r+115384|0)>>2],e=(i<<13)+(e=f[r+4>>2])|0,e=(r=(r=I)+(I=0)|0)>>>0>>0?e+1|0:e,!k)return-2147483648&i?(s(0,0|r),s(1,e+1071644672|0),(A=(C=(g=+t())*A)+g)<1&&(f[(e=V-16|0)+8>>2]=0,f[e+12>>2]=1048576,Q[e+8>>3]=22250738585072014e-324*Q[e+8>>3],A=0==(A=(a=A+1)+(C+(g-A)+(A+(1-a)))+-1)?0:A),A*=22250738585072014e-324):(s(0,0|r),s(1,e-1058013184|0),A=5486124068793689e288*((g=+t())*A+g)),A;s(0,0|r),s(1,0|e),g=(g=+t())*A+g}return g}function Ae(A,e){var g,r=0,C=0,I=0,b=0;V=g=V-16|0;A:{if(sC(84270,a[0|e])){if(C=2,sC(e,43)||(C=114!=i[0|e]),C=sC(e,120)?128|C:C,I=C=sC(e,101)?524288|C:C,b=64|C,I=114==(0|(C=i[0|e]))?I:b,I=119==(0|C)?512|I:I,f[g>>2]=438,f[g+4>>2]=0,(A=0|v(-100,0|A,32768|(97==(0|C)?1024|I:I),0|g))>>>0>=4294963201&&(f[56798]=0-A,A=-1),(0|A)<0)break A;V=C=V-32|0;e:{g:{if(sC(84270,a[0|e])){if(r=IA(1176))break g}else f[56798]=28;e=0;break e}ue(r,0,144),sC(e,43)||(f[r>>2]=114==i[0|e]?8:4),97==i[0|e]?(1024&(e=0|M(0|A,3,0))||(e|=1024,f[C+16>>2]=e,f[C+20>>2]=e>>31,M(0|A,4,C+16|0)),e=128|f[r>>2],f[r>>2]=e):e=f[r>>2],f[r+80>>2]=-1,f[r+48>>2]=1024,f[r+60>>2]=A,f[r+44>>2]=r+152,8&e||(f[C>>2]=C+24,f[C+4>>2]=0,0|Y(0|A,21523,0|C)||(f[r+80>>2]=10)),f[r+40>>2]=10,f[r+36>>2]=11,f[r+32>>2]=12,f[r+12>>2]=13,i[227205]||(f[r+76>>2]=-1),f[r+56>>2]=f[56816],(e=f[56816])&&(f[e+52>>2]=r),f[56816]=r,e=r}if(V=C+32|0,r=e)break A;d(0|A)}else f[56798]=28;r=0}return V=g+16|0,r}function ee(A,e,g){var r,C=0,a=0,I=0,i=0,b=0;if(I=A,V=r=V-208|0,f[r+8>>2]=1,f[r+12>>2]=0,i=e<<2){for(f[r+16>>2]=4,f[r+20>>2]=4,e=4,C=4,a=2;A=e,e=(C+4|0)+e|0,f[(r+16|0)+(a<<2)>>2]=e,a=a+1|0,C=A,e>>>0>>0;);if((A=(I+i|0)-4|0)>>>0<=I>>>0)a=0,e=1,A=0;else{for(a=1,e=1;3&~a?(c[(r+16|0)+((C=e-1|0)<<2)>>2]>=A-I>>>0?Fe(I,g,r+8|0,e,0,r+16|0):xg(I,g,e,r+16|0),1!=(0|e)?(yr(r+8|0,C),e=1):(yr(r+8|0,1),e=0)):(xg(I,g,e,r+16|0),zr(r+8|0,2),e=e+2|0),a=1|(C=f[r+8>>2]),f[r+8>>2]=a,A>>>0>(I=I+4|0)>>>0;);a=C>>>0>1,A=0!=f[r+12>>2]}if(Fe(I,g,r+8|0,e,0,r+16|0),a|1!=(0|e)|A)for(;(0|e)<=1?(zr(C=r+8|0,A=dr(C)),a=f[r+8>>2],A=A+e|0):(yr(C=r+8|0,2),f[r+8>>2]=7^f[r+8>>2],zr(C,1),Fe((b=I-4|0)-f[(i=r+16|0)+((A=e-2|0)<<2)>>2]|0,g,C,e-1|0,1,i),yr(C,1),a=1|f[r+8>>2],f[r+8>>2]=a,Fe(b,g,C,A,1,i)),e=A,I=I-4|0,f[r+12>>2]|1!=(0|e)|1!=(0|a););}V=r+208|0}function ge(A,e,g,r){var C,a=0,I=0,i=0;V=C=V-32|0,i=a=2147483647&r,I=a-1006698496|0;A:if(0|(a=a-1140785152|0)>>>0>I>>>0){if(a=g<<4|e>>>28,g=r<<4|g>>>28,134217728==(0|(e&=268435455))&!!(0|A)|e>>>0>134217728){I=g+1073741824|0,I=(a=a+1|0)?I:I+1|0;break A}if(I=g+1073741824|0,A|134217728!=(0|e))break A;I=(A=1&a)>>>0>(a=A+a|0)>>>0?I+1|0:I}else(!g&2147418112==(0|i)?!(A|e):i>>>0<2147418112)?(a=0,I=2146435072,i>>>0>1140785151||(I=0,(i=i>>>16|0)>>>0<15249||(Ve(C+16|0,A,e,g,a=65535&r|65536,i-15233|0),Ke(C,A,e,g,a,15361-i|0),a=(e=f[C+8>>2])<<4,e=f[C+12>>2]<<4|e>>>28,g=f[C>>2],i=I=f[C+4>>2],a|=I>>>28,I=e,134217728==(0|(A=268435455&i))&!!(0|(e=g|!!(f[C+16>>2]|f[C+24>>2]|f[C+20>>2]|f[C+28>>2])))|A>>>0>134217728?I=(a=a+1|0)?I:I+1|0:e|134217728!=(0|A)||(I=(A=a)>>>0>(a=a+(1&a)|0)>>>0?I+1|0:I)))):(a=g<<4|e>>>28,I=524287&(A=r<<4|g>>>28)|2146959360);return V=C+32|0,s(0,0|a),s(1,-2147483648&r|I),+t()}function re(A){var e,g=0,r=0,C=0,a=0,I=0;if(n(+A),I=0|b(1),C=0|b(0),2047==(0|(a=I>>>20&2047)))return(A*=1)/A;if(!(r=C<<1)&2145386496==(0|(g=I<<1|C>>>31))|g>>>0<2145386496)return!r&2145386496==(0|g)?0*A:A;if(a)g=1048575&I|1048576;else{if(a=0,r=C<<12,(0|(g=I<<12|C>>>20))>0|(0|g)>=0)for(;a=a-1|0,g=g<<1|r>>>31,r<<=1,(0|g)>0|(0|g)>=0;);r=31&(g=1-a|0),(63&g)>>>0>=32?(g=C<>>32-r|I<1023){for(;;){if(!((0|(C=g+-1048576|0))<0||(g=C)|r))return 0*A;if(g=g<<1|r>>>31,r<<=1,!((0|(a=a-1|0))>1023))break}a=1023}if(!((0|(C=g+-1048576|0))<0||(g=C)|r))return 0*A;if(1048575==(0|g)|g>>>0<1048575)for(;a=a-1|0,C=g>>>0<524288,g=g<<1|r>>>31,r<<=1,C;);return e=-2147483648&I,(0|a)>0?g=g+-1048576|a<<20:(C=1-a|0,I=g,a=r,r=31&C,(63&C)>>>0>=32?(g=0,r=I>>>r|0):(g=I>>>r|0,r=((1<>>r)),s(0,0|r),s(1,g|e),+t()}function Ce(A,e,g,r,C){var I,b=0,s=0;V=I=V-160|0;A:{e:{g:{r:switch((b=i[e+10|0])-15|0){case 6:break g;case 0:break r;default:break e}a[0|A]=0;break A}f[I>>2]=G(i[g+7|0],44)+137856,dg(A,86002,I),A=Lg(A)+A|0;break A}if(r){if(a[I+140|0]=0,g?bA(0,0,g,I+8|0,0):_g(b,I+8|0),g=I+140|0,b=i[I+140|0]){if(32==(0|b)){a[0|A]=0;break A}224&(b=b<<24>>24)||(C&&(f[C>>2]=b),g=I+141|0)}if(!((0|(C=Lg(g)))<=0)){A=rg(A,g)+C|0,a[0|A]=0;break A}}C=0;e:if(!(!(g=255&(b=f[e>>2]))|47==(0|g))){if(r){if(95==(0|(s=255&b)))break e;g:{r:{if(35==(0|s)){if(s=3,2!=i[e+11|0])break r;break e}if((s=g-32|0)>>>0>95)break g}g=B[93952+(s<<1)>>1]}C=Fg(g,A)}else a[0|A]=b,C=1;for(;;){if(!(g=255&(b>>=8))|47==(0|g))break e;if(r){if(35==(0|g)&2==i[e+11|0])break e;if(g-48>>>0<10)continue;(s=g-32|0)>>>0<=95&&(g=B[93952+(s<<1)>>1]),C=Fg(g,A+C|0)+C|0}else a[A+C|0]=b,C=C+1|0}}a[0|(A=A+C|0)]=0}return V=I+160|0,A}function ae(A){var e,g=0;e=A,g=131280;A:{e:{if(!((0|A)<=1023||(g=131300,A>>>0<1328||(g=131320,A>>>0<1424||(g=131340,A>>>0<1536||(g=131360,A>>>0<1792||(g=131380,A>>>0<1872||(g=131400,A>>>0<2432||(g=131420,A>>>0<2560||(g=131440,A>>>0<2688||(g=131460,A>>>0<2816||(g=131480,A>>>0<2944||(g=131500,A>>>0<3072||(g=131520,A>>>0<3200||(g=131540,A>>>0<3328||(g=131560,A>>>0<3456||(g=131580,A>>>0<3584||(g=131600,A>>>0<3712||(g=131620,A>>>0<3840||(g=131640,A>>>0<4096||(g=131660,A>>>0<4256||(g=131680,A>>>0<4352||(g=131700,A>>>0<4608||(g=131720,A>>>0<5024||(g=131740,A>>>0<10496||(g=131760,A>>>0<12544||(g=131780,A>>>0<40960))))))))))))))))))))))))))){if(A>>>0>=55296)break e;g=131800}if((0|e)>=B[(A=g)+8>>1])break A}A=0}return A}function Ie(A,e,g){var r,C=0,i=0,b=0,s=0;V=r=V-208|0,a[r+80|0]=0;A:{if((C=e-224|0)>>>0<=158)e=101072+(C<<1)|0;else{if((e=e-592|0)>>>0>88)break A;e=101392+(e<<1)|0}if(e=B[e>>1]){if(b=e<<16>>16,s=(C=63&e)>>>0>37?C+59|0:I[101584+(C<<1)>>1],C=e>>>6|0,(0|b)<0)C=59+(63&C)|0,e=e>>>12&7;else{if(!(i=31&C))break A;C=0,e=e>>>11&15}(i=Mg(A,f[129920+(i<<3)>>2],r+112|0))&&gr(A,s,r+176|0)&&(e&&4096&Mg(A,f[129920+(e<<3)>>2],r+80|0)&&(g=Lg(e=rg(g,r+80|0)),a[r+80|0]=0,g=e+g|0),C?(gr(e=A,C,A=r+144|0),f[r+68>>2]=r+80,f[r- -64>>2]=A,f[r+60>>2]=6,f[r+52>>2]=23,f[r+56>>2]=r+176,f[r+48>>2]=r+112,dg(g,84101,r+48|0)):(0|b)<0?rg(g,r+176|0):1&f[A+144>>2]|4096&i?(f[r+36>>2]=23,f[r+40>>2]=6,f[r+44>>2]=r+176,f[r+32>>2]=r+112,dg(g,84430,r+32|0)):(f[r+16>>2]=23,f[r+8>>2]=23,f[r>>2]=4,f[r+12>>2]=r+112,f[r+4>>2]=r+176,dg(g,84802,r)))}}V=r+208|0}function fe(){vr(),f[55928]=0,f[55926]=0,f[55927]=0,f[55924]=0,f[56244]=0,f[56245]=0,f[56246]=0,f[56247]=0,f[56260]=0,f[56261]=0,f[56262]=0,f[56263]=0,f[56276]=0,f[56277]=0,f[56278]=0,f[56279]=0,f[55974]=0,f[55975]=0,f[55972]=0,f[55973]=0,f[55988]=0,f[55989]=0,f[55990]=0,f[55991]=0,f[56004]=0,f[56005]=0,f[56006]=0,f[56007]=0,f[56020]=0,f[56021]=0,f[56022]=0,f[56023]=0,f[56036]=0,f[56037]=0,f[56038]=0,f[56039]=0,f[56052]=0,f[56053]=0,f[56054]=0,f[56055]=0,f[56068]=0,f[56069]=0,f[56070]=0,f[56071]=0,f[56086]=0,f[56087]=0,f[56084]=0,f[56085]=0,f[56102]=0,f[56103]=0,f[56100]=0,f[56101]=0,f[56118]=0,f[56119]=0,f[56116]=0,f[56117]=0,f[56134]=0,f[56135]=0,f[56132]=0,f[56133]=0,f[56150]=0,f[56151]=0,f[56148]=0,f[56149]=0,f[56166]=0,f[56167]=0,f[56164]=0,f[56165]=0,f[56182]=0,f[56183]=0,f[56180]=0,f[56181]=0,f[56198]=0,f[56199]=0,f[56196]=0,f[56197]=0,f[56214]=0,f[56215]=0,f[56212]=0,f[56213]=0,f[56230]=0,f[56231]=0,f[56228]=0,f[56229]=0}function ie(A,e){var g=0,r=0,C=0,a=0,I=0,i=0,b=0,s=0,t=0,n=0;A:{if((0|(a=f[A+4>>2]))==f[A>>2])if((I=f[A+8>>2])>>>0<(g=f[A+12>>2])>>>0)g=(C=(1+(g-I>>2)|0)/2<<2)+I|0,(0|a)!=(0|I)&&(Qe(g=g-(r=I-a|0)|0,a,r),a=f[A+8>>2]),f[A+4>>2]=g,f[A+8>>2]=C+a;else{if((r=(0|g)==(0|a)?1:g-a>>1)>>>0>=1073741824)break A;if(t=(b=Or(g=r<<2))+g|0,i=g=(r+3&-4)+b|0,(0|a)!=(0|I)){if(n=-4&(I=I-a|0),C=g,r=a,I=1+((s=I-4|0)>>>2|0)&7)for(i=0;f[C>>2]=f[r>>2],r=r+4|0,C=C+4|0,(0|I)!=(0|(i=i+1|0)););if(i=g+n|0,!(s>>>0<28))for(;f[C>>2]=f[r>>2],f[C+4>>2]=f[r+4>>2],f[C+8>>2]=f[r+8>>2],f[C+12>>2]=f[r+12>>2],f[C+16>>2]=f[r+16>>2],f[C+20>>2]=f[r+20>>2],f[C+24>>2]=f[r+24>>2],f[C+28>>2]=f[r+28>>2],r=r+32|0,(0|i)!=(0|(C=C+32|0)););}f[A+12>>2]=t,f[A+8>>2]=i,f[A+4>>2]=g,f[A>>2]=b,a&&(mA(a),g=f[A+4>>2])}else g=a;return f[g-4>>2]=f[e>>2],void(f[A+4>>2]=f[A+4>>2]-4)}Lr(),k()}function be(A,e,g){var r=0,C=0,a=0,I=0,f=0,i=0,b=0,s=0,t=0;A:{e:{g:{r:{C:{a:{I:{f:{i:{if(e){if(!g)break i;break f}return J=(e=A)-G(A=(A>>>0)/(g>>>0)|0,g)|0,R=0,U=0,A}if(!A)break I;break a}if(!((r=g-1|0)&g))break C;a=0-(I=(D(g)+33|0)-D(e)|0)|0;break g}return J=0,R=e-G(A=(e>>>0)/0|0,0)|0,U=0,A}if((r=32-D(e)|0)>>>0<31)break r;break e}if(J=A&r,R=0,1==(0|g))break A;return g=31&(r=nC(g)),(63&r)>>>0>=32?A=e>>>g|0:(C=e>>>g|0,A=((1<>>g),U=C,A}I=r+1|0,a=63-r|0}if(r=31&(C=63&I),C>>>0>=32?(C=0,f=e>>>r|0):(C=e>>>r|0,f=((1<>>r),r=31&(a&=63),a>>>0>=32?(e=A<>>32-r|e<>>31,f=(C=f<<1|e>>>31)-(b=g&(a=s-(i+(C>>>0>r>>>0)|0)>>31))|0,C=i-(C>>>0>>0)|0,e=e<<1|A>>>31,A=t|A<<1,t=i=1&a,I=I-1|0;);return J=f,R=C,U=e<<1|A>>>31,i|A<<1}J=A,R=e,A=0,e=0}return U=e,A}function se(A,e){var g=0,r=0,C=0,a=0,I=0,i=0,b=0,s=0,t=0,n=0;A:{if((0|(g=f[A+8>>2]))==f[A+12>>2])if((r=f[A+4>>2])>>>0>(I=f[A>>2])>>>0)C=Qe((a=(1+(r-I>>2)|0)/-2<<2)+r|0,r,g=g-r|0)+g|0,f[A+8>>2]=C,f[A+4>>2]=a+f[A+4>>2];else{if((a=(0|g)==(0|I)?1:g-I>>1)>>>0>=1073741824)break A;if(t=(i=Or(C=a<<2))+C|0,C=a=(-4&a)+i|0,(0|g)!=(0|r)){if(n=-4&(g=g-r|0),s=1+((b=g-4|0)>>>2|0)&7)for(C=0,g=a;f[g>>2]=f[r>>2],r=r+4|0,g=g+4|0,(0|s)!=(0|(C=C+1|0)););else g=a;if(C=a+n|0,!(b>>>0<28))for(;f[g>>2]=f[r>>2],f[g+4>>2]=f[r+4>>2],f[g+8>>2]=f[r+8>>2],f[g+12>>2]=f[r+12>>2],f[g+16>>2]=f[r+16>>2],f[g+20>>2]=f[r+20>>2],f[g+24>>2]=f[r+24>>2],f[g+28>>2]=f[r+28>>2],r=r+32|0,(0|C)!=(0|(g=g+32|0)););}f[A+12>>2]=t,f[A+8>>2]=C,f[A+4>>2]=a,f[A>>2]=i,I&&(mA(I),C=f[A+8>>2])}else C=g;return f[C>>2]=f[e>>2],void(f[A+8>>2]=f[A+8>>2]+4)}Lr(),k()}function te(A,e){var g,r=0,C=0,I=0,b=0,s=0;C=189088,V=g=V-320|0,f[g+312>>2]=0,I=zA(A,b=rg(g+112|0,189088),g,g+316|0,g+312|0,0),r=f[g+316>>2];A:if((0|e)<=3){if((0|r)<2)break A;if(I=3&(e=r-1|0),A=1,r-2>>>0>=3)for(s=-4&e,e=0;a[0|(r=A+g|0)]>=4&&(a[0|r]=3),a[(r=A+g|0)+1|0]>=4&&(a[r+1|0]=3),a[r+2|0]>=4&&(a[r+2|0]=3),a[r+3|0]>=4&&(a[r+3|0]=3),A=A+4|0,(0|s)!=(0|(e=e+4|0)););if(!I)break A;for(e=0;a[0|(r=A+g|0)]>=4&&(a[0|r]=3),A=A+1|0,(0|I)!=(0|(e=e+1|0)););}else if(A=1,!((0|r)<=1)){for(;;){if((0|I)>a[0|(s=A+g|0)]){if((0|r)!=(0|(A=A+1|0)))continue;break A}break}a[0|s]=e}if(A=i[0|b])for(e=1;r=f[144464+((255&A)<<2)>>2],2!=i[r+11|0]|16&i[r+6|0]||(I=255&(r=a[e+g|0]),(0|r)<2&&I||(a[0|C]=i[I+94151|0],C=C+1|0,A=i[0|b]),e=e+1|0),a[0|C]=A,C=C+1|0,A=i[0|(b=b+1|0)];);a[0|C]=0,V=g+320|0}function ne(A){var e=0,g=0,r=0,C=0,a=0,I=0;g=e=f[(A|=0)>>2],f[A>>2]=e+1;A:{e:{g:{r:{C:{a:{I:switch(((a=i[0|e])>>>4|0)-8|0){case 0:case 1:case 2:case 3:break e;case 7:break C;case 6:break a;case 4:case 5:break I;default:break A}if((r=e+2|0)>>>0>=(C=f[A+4>>2])>>>0)break r;if(f[A>>2]=r,128!=(192&(g=i[g+1|0])))break g;return 63&g|a<<6&1984}if((r=e+3|0)>>>0>=(C=f[A+4>>2])>>>0)break r;if(g=e+2|0,f[A>>2]=g,128!=(192&(e=i[e+1|0]))){r=g;break g}if(f[A>>2]=r,128!=(192&(g=i[0|g])))break g;return 63&g|(63&e|a<<6&960)<<6}if(!((C=f[A+4>>2])>>>0<=(g=e+4|0)>>>0)){if(r=e+2|0,f[A>>2]=r,128!=(192&(C=i[e+1|0])))break g;if(r=e+3|0,f[A>>2]=r,128!=(192&(I=i[e+2|0])))break g;if(f[A>>2]=g,e=i[0|r],r=g,128!=(192&e))break g;return 0|((A=63&e|I<<6&4032|(63&C|a<<6&960)<<12)>>>0>=1114112?65533:A)}}f[A>>2]=C;break e}f[A>>2]=r-1}a=65533}return 0|a}function ke(A,e,g,r){var C,b,s=0,t=0,n=0,k=0,o=0,B=0;if(V=C=V-432|0,!(!r|!(536870912&(b=gA(A,e,g,r))))&&(I[C+48>>1]=8192,r=rg(C+48|2,r),i[0|r])){for(n=C+224|0,s=1,k=200;;){if(Te(C+44|0,r),e=nr(f[C+44>>2]),t=f[g>>2],e?(f[g>>2]=2|t,Fg(ar(f[C+44>>2]),r)):f[g>>2]=-3&t,B=f[33264],gA(A,r,g,0),1&s?(f[C+16>>2]=189088,t=Gg(n,k,84130,C+16|0)):(f[C+32>>2]=15,f[C+36>>2]=189088,t=Gg(n,k,84434,C+32|0)),s=(e=f[33264])+1|0,f[33264]=s,e>>>0<=2147483646)for(;;)if(e=r,r=r+1|0,32==(0|(o=a[0|e]))|o-9>>>0<5){for(;e=(r=e)+1|0,32==(0|(o=a[0|r]))|o-9>>>0<5;);if(s=s-1|0,f[33264]=s,!((0|s)>0))break}if(n=n+t|0,f[33264]=B,!(i[0|r]&&(s=0,(0|(k=k-t|0))>1)))break}(C+224|0)!=(0|n)&&(f[C>>2]=C+224,Gg(189088,200,84130,C))}return V=C+432|0,b}function oe(A,e,g,r,C,b,s,t){var n,k;n=f[32972],k=f[n+116>>2],I[A+8>>1]=B[A+8>>1]+b,b=32&t?0-b|0:b,I[A+10>>1]=b+B[A+10>>1],I[A+12>>1]=b+B[A+12>>1],r=(0|r)>(0|(b=((b=(0|G(e,k))/256|0)-(e=I[A+6>>1])|0)/2|0))?b:r,I[A+6>>1]=((0|g)<(0|r)?r:g)+e;A:{e:switch(C-1|0){case 0:e=(0|(e=235-(g=I[A+4>>1])|0))<=-100?-100:e,I[A+4>>1]=((0|e)>=-60?-60:e)+g;break A;case 1:e=(0|(e=(0|(e=235-(g=I[A+4>>1])|0))<=-300?-300:e))>=-150?-150:e,I[A+4>>1]=e+g,I[A+2>>1]=e+B[A+2>>1];break A;case 2:break e;default:break A}e=(0|(e=(0|(e=100-(g=I[A+4>>1])|0))<=-400?-400:e))>-300?-400:e,I[A+4>>1]=e+g,I[A+2>>1]=e+B[A+2>>1]}f[n+132>>2]||(a[A+20|0]=(G(i[A+20|0],s)>>>0)/100,a[A+21|0]=(G(i[A+21|0],s)>>>0)/100,a[A+22|0]=(G(i[A+22|0],s)>>>0)/100,a[A+23|0]=(G(i[A+23|0],s)>>>0)/100,a[A+24|0]=(G(i[A+24|0],s)>>>0)/100,a[A+25|0]=(G(i[A+25|0],s)>>>0)/100)}function Be(A){var e,g=0,r=0,C=0,I=0;V=e=V-48|0;A:{if(A){i[0|A]||(A=Hg(84285),i[0|A]&&A||(A=Hg(121696),i[0|A]&&A||(A=Hg(84614),i[0|A]&&A||(A=84891))));e:{for(;;){if(!(!(r=i[A+g|0])|47==(0|r))){if(C=23,23!=(0|(g=g+1|0)))continue;break e}break}C=g}r=84891;e:{g:{if(g=i[0|A],(i[A+C|0]|46==(0|g)||(r=A,67==(0|g)))&&!i[r+1|0]||!Qr(r,84891)||!Qr(r,85136)){if(g=121652,46==i[r+1|0])break g;A=0;break e}if(g=f[56851])for(;;){if(!Qr(r,g+8|0))break g;if(!(g=f[g+32>>2]))break}(A=IA(36))&&(g=f[30414],f[A>>2]=f[30413],f[A+4>>2]=g,_A(g=A+8|0,r,C),a[g+C|0]=0,f[A+32>>2]=f[56851],f[56851]=A),g=A||121652}A=g}if(-1==(0|A))break A;f[56809]=A}else A=f[56809];I=A?A+8|0:84309}return V=e+48|0,I}function ce(A){var e=0,g=0,r=0,C=0,I=0,i=0,b=0,s=0,t=0;s=!!((e=f[A+112>>2])|(r=f[A+116>>2])),C=e,i=e=(I=f[A+4>>2])-(b=f[A+44>>2])|0,g=e+f[A+120>>2]|0,e=f[A+124>>2]+(e>>31)|0;A:{if(!(((0|(e=g>>>0>>0?e+1|0:e))>=(0|r)&g>>>0>=C>>>0|(0|e)>(0|r))&s)){if((0|(s=Nr(A)))>=0)break A;I=f[A+4>>2],b=f[A+44>>2]}return f[A+112>>2]=-1,f[A+116>>2]=-1,f[A+104>>2]=I,r=(i=g)+(g=b-I|0)|0,e=(g>>31)+e|0,f[A+120>>2]=r,f[A+124>>2]=g>>>0>r>>>0?e+1|0:e,-1}return e=(r=g+1|0)?e:e+1|0,I=f[A+4>>2],b=f[A+8>>2],i=C=f[A+116>>2],C|(g=f[A+112>>2])&&(C=g-r|0,(0|(g=i-(e+(g>>>0>>0)|0)|0))>=(0|(i=(t=b-I|0)>>31))&C>>>0>=t>>>0|(0|g)>(0|i)||(b=C+I|0)),f[A+104>>2]=b,r=(C=(g=f[A+44>>2])-I|0)+r|0,e=(C>>31)+e|0,f[A+120>>2]=r,f[A+124>>2]=r>>>0>>0?e+1|0:e,g>>>0>=I>>>0&&(a[I-1|0]=s),s}function Qe(A,e,g){var r=0,C=0;A:if((0|A)!=(0|e)){if(e-(C=A+g|0)>>>0<=0-(g<<1)>>>0)return _A(A,e,g);if(r=3&(A^e),A>>>0>>0){if(r)r=A;else{if(3&A)for(r=A;;){if(!g)break A;if(a[0|r]=i[0|e],e=e+1|0,g=g-1|0,!(3&(r=r+1|0)))break}else r=A;if(!(g>>>0<=3))for(;f[r>>2]=f[e>>2],e=e+4|0,r=r+4|0,(g=g-4|0)>>>0>3;);}if(g)for(;a[0|r]=i[0|e],r=r+1|0,e=e+1|0,g=g-1|0;);}else{if(!r){if(3&C)for(;;){if(!g)break A;if(a[0|(r=(g=g-1|0)+A|0)]=i[e+g|0],!(3&r))break}if(!(g>>>0<=3))for(;f[(g=g-4|0)+A>>2]=f[e+g>>2],g>>>0>3;);}if(!g)break A;for(;a[(g=g-1|0)+A|0]=i[e+g|0],g;);}}return A}function Ge(A,e,g,r){A:switch(e-9|0){case 0:return e=f[g>>2],f[g>>2]=e+4,void(f[A>>2]=f[e>>2]);case 6:return e=f[g>>2],f[g>>2]=e+4,e=I[e>>1],f[A>>2]=e,void(f[A+4>>2]=e>>31);case 7:return e=f[g>>2],f[g>>2]=e+4,f[A>>2]=B[e>>1],void(f[A+4>>2]=0);case 8:return e=f[g>>2],f[g>>2]=e+4,e=a[0|e],f[A>>2]=e,void(f[A+4>>2]=e>>31);case 9:return e=f[g>>2],f[g>>2]=e+4,f[A>>2]=i[0|e],void(f[A+4>>2]=0);case 16:return e=f[g>>2]+7&-8,f[g>>2]=e+8,void(Q[A>>3]=Q[e>>3]);case 17:HC[0|r](A,g);default:return;case 1:case 4:case 14:return e=f[g>>2],f[g>>2]=e+4,e=f[e>>2],f[A>>2]=e,void(f[A+4>>2]=e>>31);case 2:case 5:case 11:case 15:return e=f[g>>2],f[g>>2]=e+4,f[A>>2]=f[e>>2],void(f[A+4>>2]=0);case 3:case 10:case 12:case 13:break A}e=f[g>>2]+7&-8,f[g>>2]=e+8,g=f[e+4>>2],f[A>>2]=f[e>>2],f[A+4>>2]=g}function we(A,e,g,r,C,a){var I;V=I=V-80|0;A:if((0|a)>=16384){if(QA(I+32|0,e,g,r,C,0,0,0,2147352576),r=f[I+40>>2],C=f[I+44>>2],e=f[I+32>>2],g=f[I+36>>2],a>>>0<32767){a=a-16383|0;break A}QA(I+16|0,e,g,r,C,0,0,0,2147352576),a=((0|a)>=49149?49149:a)-32766|0,r=f[I+24>>2],C=f[I+28>>2],e=f[I+16>>2],g=f[I+20>>2]}else(0|a)>-16383||(QA(I- -64|0,e,g,r,C,0,0,0,7471104),r=f[I+72>>2],C=f[I+76>>2],e=f[I+64>>2],g=f[I+68>>2],a>>>0>4294934644?a=a+16269|0:(QA(I+48|0,e,g,r,C,0,0,0,7471104),a=((0|a)<=-48920?-48920:a)+32538|0,r=f[I+56>>2],C=f[I+60>>2],e=f[I+48>>2],g=f[I+52>>2]));QA(I,e,g,r,C,0,0,0,a+16383<<16),e=f[I+12>>2],f[A+8>>2]=f[I+8>>2],f[A+12>>2]=e,e=f[I+4>>2],f[A>>2]=f[I>>2],f[A+4>>2]=e,V=I+80|0}function Ee(A,e){var g,r,C=0;V=g=V+-64|0,C=f[A>>2],r=f[C-4>>2],C=f[C-8>>2],f[g+32>>2]=0,f[g+36>>2]=0,f[g+40>>2]=0,f[g+44>>2]=0,f[g+48>>2]=0,f[g+52>>2]=0,a[g+55|0]=0,a[g+56|0]=0,a[g+57|0]=0,a[g+58|0]=0,a[g+59|0]=0,a[g+60|0]=0,a[g+61|0]=0,a[g+62|0]=0,f[g+24>>2]=0,f[g+28>>2]=0,f[g+20>>2]=0,f[g+16>>2]=125084,f[g+12>>2]=A,f[g+8>>2]=e,A=A+C|0,C=0;A:if(Wr(r,e,0))f[g+56>>2]=1,HC[f[f[r>>2]+20>>2]](r,g+8|0,A,A,1,0),C=1==f[g+32>>2]?A:0;else{HC[f[f[r>>2]+24>>2]](r,g+8|0,A,1,0);e:switch(f[g+44>>2]){case 0:C=1==f[g+48>>2]&&1==f[g+36>>2]&&1==f[g+40>>2]?f[g+28>>2]:0;break A;case 1:break e;default:break A}1!=f[g+32>>2]&&f[g+48>>2]|1!=f[g+36>>2]|1!=f[g+40>>2]||(C=f[g+24>>2])}return V=g- -64|0,C}function De(A,e,g,r,C){var b,s,t=0;V=b=V-80|0,I[b+72>>1]=0,f[b+64>>2]=0,f[b+68>>2]=0,a[0|r]=0,t=Fg(e,s=2|(t=b- -64|0))+t|0,a[t+2|0]=32;A:if(-1!=(0|g))e>>>0>=33&&!er(e)?(a[t+3|0]=32==(0|g)?32:31,a[b+65|0]=95,Mg(A,b- -64|1,b+16|0)||(a[b+65|0]=32,Mg(A,s,b+16|0)||GA(A,s,b+16|0,40,0,268435456,0)),i[b+16|0]||Ie(A,e,b+16|0),e=rg(r,b+16|0),!(g=i[0|e])|21==(0|g)||(f[b+56>>2]=0,f[b+60>>2]=0,fA(A,e,b+56|0,-1,1&C))):(f[b>>2]=e,dg(e=b- -64|1,85485,b),Mg(A,e,r));else{if(Mg(A,s,r))break A;if(a[b+65|0]=95,Mg(A,b- -64|1,b+16|0)|25966==f[A+212>>2])break A;BC(85055),Mg(f[47194],s,b+16|0)&&(a[0|r]=21,a[r+1|0]=0),qr(f[f[32972]+60>>2])}V=b+80|0}function ue(A,e,g){var r=0,C=0,I=0,i=0;if(g&&(a[0|A]=e,a[(r=A+g|0)-1|0]=e,!(g>>>0<3||(a[A+2|0]=e,a[A+1|0]=e,a[r-3|0]=e,a[r-2|0]=e,g>>>0<7||(a[A+3|0]=e,a[r-4|0]=e,g>>>0<9||(C=(r=0-A&3)+A|0,e=G(255&e,16843009),f[C>>2]=e,f[(g=(r=g-r&-4)+C|0)-4>>2]=e,r>>>0<9||(f[C+8>>2]=e,f[C+4>>2]=e,f[g-8>>2]=e,f[g-12>>2]=e,r>>>0<25||(f[C+24>>2]=e,f[C+20>>2]=e,f[C+16>>2]=e,f[C+12>>2]=e,f[g-16>>2]=e,f[g-20>>2]=e,f[g-24>>2]=e,f[g-28>>2]=e,(g=r-(i=4&C|24)|0)>>>0<32))))))))for(r=Cr(e,0,1,1),I=U,e=C+i|0;f[e+24>>2]=r,f[e+28>>2]=I,f[e+16>>2]=r,f[e+20>>2]=I,f[e+8>>2]=r,f[e+12>>2]=I,f[e>>2]=r,f[e+4>>2]=I,e=e+32|0,(g=g-32|0)>>>0>31;);return A}function le(){var A,e=0,g=0,r=0,C=0,a=0;if(V=A=V-208|0,(0|(r=f[50303]))>0)for(;(C=f[(g=201216+(e<<2)|0)>>2])&&(mA(C),f[g>>2]=0),(0|r)!=(0|(e=e+1|0)););if(f[50303]=0,f[A+16>>2]=137584,f[A+20>>2]=47,dg(e=A+32|0,87827,A+16|0),FA(e,Lg(e)+1|0,0),f[A+4>>2]=47,f[A>>2]=137584,dg(e,87933,A),FA(e,Lg(e)+1|0,1),e=f[50303],f[(g=e<<2)+201216>>2]=0,g=OA(r=f[50741],g+4|0)){if(f[50741]=g,ee(201216,e,7),r=f[50741],g=0,e=f[50304])for(C=0;a=f[e+4>>2],i[0|a]&&Qr(a+1|0,86589)&&pg(f[e+8>>2],88032,3)&&(f[(g<<2)+r>>2]=e,g=g+1|0),e=f[201216+((C=C+1|0)<<2)>>2];);f[(g<<2)+r>>2]=0}return V=A+208|0,r}function xe(A,e,g){var r=0,C=0,I=0,b=0;if(I=e-1|0,(0|e)>=2){e=A;A:{for(;;){e:{g:{if((0|(r=f[g+4>>2]))!=(0|(C=f[g+8>>2]))){if((b=qe(r,10,C-r|0))?C=1+(b-(r=f[g+4>>2])|0)|0:(r=f[g+4>>2],C=f[g+8>>2]-r|0),_A(e,r,r=C>>>0>>0?C:I),C=r+f[g+4>>2]|0,f[g+4>>2]=C,e=e+r|0,b)break e;if(!(I=I-r|0))break e;if((0|C)!=f[g+8>>2]){f[g+4>>2]=C+1,r=i[0|C];break g}}if(!((0|(r=Nr(g)))>=0)){if(r=0,(0|A)==(0|e))break A;if(16&i[0|g])break e;break A}}if(a[0|e]=r,e=e+1|0,10!=(255&r)&&(I=I-1|0))continue}break}A?(a[0|e]=0,r=A):r=0}}else if(e=f[g+72>>2],f[g+72>>2]=e-1|e,!I)return a[0|A]=0,A;return r}function de(A){var e=0,g=0,r=0,C=0,a=0,I=0,b=0,s=0;if(e=G(A,44),(0|(A=f[e+137896>>2]))>0&&de(A-1|0),A=f[36115],!((0|(e=f[(g=e+137856|0)+36>>2]))<=0)){if(C=f[g+32>>2],b=1&e,1!=(0|e))for(s=-2&e,g=0;e=i[(a=(r=g<<4)+C|0)+10|0],f[144464+(e<<2)>>2]=a,(0|A)>=(0|e)?e=A:ue(144464+((A=A+1|0)<<2)|0,0,e-A<<2),A=i[(r=(16|r)+C|0)+10|0],f[144464+(A<<2)>>2]=r,(0|A)<=(0|e)?A=e:ue(144464+((e=e+1|0)<<2)|0,0,A-e<<2),g=g+2|0,(0|s)!=(0|(I=I+2|0)););else g=0;b&&(e=i[(g=(g<<4)+C|0)+10|0],f[144464+(e<<2)>>2]=g,(0|A)>=(0|e)||(ue(144464+((A=A+1|0)<<2)|0,0,e-A<<2),A=e))}f[36115]=A}function me(A,e,g,r,C){var I,b=0,s=0,t=0;V=I=V-16|0;A:if(1&a[A+106|0]&&(b=i[0|g],!(!(1&a[r+2|0])&46!=(0|b)||256&(t=f[r+12>>2])|!(!(2&t)||C)||(Te(I+12|0,46!=(0|b)?g:g+2|0),!(b=i[0|g])|!i[g+1|0])))){if(!(!(t=f[I+12>>2])|2&i[r+2|0])){if(!kg(t))break A;b=i[0|g]}46==(0|b)&&(a[0|g]=32),s=2,26741!=f[A+212>>2]|C||(g=kg(f[I+12>>2])?ke(A,g+2|0,0,0):0,128&i[A+8233|0]&&(!(!(C=f[I+12>>2])|2&i[r+2|0])&C-48>>>0>=10||(s=0)),s=32768&g?0:s,131072&g&&(s=163840&f[A+8232>>2]?34:45!=i[e-2|0]?s:0))}return V=I+16|0,s}function Me(A,e,g,r,C){var a,I=0,i=0;if(V=a=V-208|0,f[a+204>>2]=g,ue(g=a+160|0,0,40),f[a+200>>2]=f[a+204>>2],(0|cA(0,e,a+200|0,a+80|0,g,r,C))<0)C=-1;else{f[A+76>>2]>=0,I=f[A>>2],f[A+72>>2]<=0&&(f[A>>2]=-33&I);A:{e:{if(f[A+48>>2]){if(f[A+16>>2])break e}else f[A+48>>2]=80,f[A+28>>2]=0,f[A+16>>2]=0,f[A+20>>2]=0,i=f[A+44>>2],f[A+44>>2]=a;if(g=-1,or(A))break A}g=cA(A,e,a+200|0,a+80|0,a+160|0,r,C)}i&&(HC[f[A+36>>2]](A,0,0),f[A+48>>2]=0,f[A+44>>2]=i,f[A+28>>2]=0,e=f[A+20>>2],f[A+16>>2]=0,f[A+20>>2]=0,g=e?g:-1),e=A,A=f[A>>2],f[e>>2]=A|32&I,C=32&A?-1:g}return V=a+208|0,C}function ve(A,e,g,r){var C,I=0,f=0,b=0,s=0,t=0,n=0,k=0;if(V=C=V-208|0,f=i[0|e])for(;a[I+C|0]=f,s=(6==(255&f)&21!=(0|b))+s|0,b=f<<24>>24,f=i[(I=I+1|0)+e|0];);if(a[I+C|0]=0,I=i[0|C])for(n=s-2|0,b=0,k=(0|r)<2,f=0;;){A:{e:if(6!=(255&I)|k|21==(0|f)){if(255==(0|(r=255&I))){if(!t|(0|g)<2)break A;r=g>>>0>2?11:(0|b)%3|0?23:11}f=r,r=b}else{if(I=b+1|0,i[A+169|0]){f=(0|I)>1?5:6,r=I;break e}if(f=6,r=s,(0|I)==(0|s))break e;f=(0|I)%3|0||(0|b)==(0|n)?5:6,r=I}b=r,a[0|e]=f,e=e+1|0}if(!(I=i[(t=t+1|0)+C|0]))break}(0|g)>=2&&(a[0|e]=11,e=e+1|0),a[0|e]=0,V=C+208|0}function he(A,e){var g=0;g=0,A&&(g=f[50754],g=(A=(A=(0|G(f[145712+(e?12:((0|A)>199)<<2)>>2],A))/256|0)>>>0>(e=f[36430])>>>0?A:e)>>>0<=89999?(G(A,g)>>>0)/1e3|0:(G(A,(0|g)/25|0)>>>0)/40|0),(0|(A=f[36440]))<=0||(0|(e=f[36424]))<0||(f[(e=216192+(e<<4)|0)+4>>2]||(f[e+4>>2]=A),f[36440]=0),f[36426]=0,f[36439]=-1,f[36455]=f[50758],kA(),f[36427]=-1,A=216192+(f[50758]<<4)|0,f[A>>2]=5,f[A+4>>2]=g,A=f[50758]+1|0,f[50758]=(0|A)<=169?A:0,f[36426]=0,f[36438]&&(f[36438]=0,A=216192+(f[50758]<<4)|0,f[A>>2]=14,f[A+4>>2]=0,A=f[50758]+1|0,f[50758]=(0|A)<=169?A:0)}function pe(A,e,g,r,C,a,I,f){var i,b=0,s=0,t=0;b=1,i=s=2147483647&r;A:if(!((t=2147418112==(0|s))&!g?A|e:t&!!(0|g)|s>>>0>2147418112)&&!((t=2147418112==(0|(s=2147483647&f)))&!I?C|a:t&!!(0|I)|s>>>0>2147418112)){if(!(A|C|g|I|e|a|s|i))return 0;if((0|(b=r&f))>0|(0|b)>=0){if(b=-1,(0|g)==(0|I)&(0|r)==(0|f)?(0|e)==(0|a)&A>>>0>>0|e>>>0>>0:g>>>0>>0&(0|r)<=(0|f)|(0|r)<(0|f))break A;return!!(A^C|g^I|e^a|r^f)}b=-1,((0|g)==(0|I)&(0|r)==(0|f)?(0|e)==(0|a)&A>>>0>C>>>0|e>>>0>a>>>0:g>>>0>I>>>0&(0|r)>=(0|f)|(0|r)>(0|f))||(b=!!(A^C|g^I|e^a|r^f))}return b}function Ye(A,e){var g=0,r=0,C=0,I=0;for(g=i[85836]|i[85837]<<8,a[0|e]=g,a[e+1|0]=g>>>8,a[e+2|0]=i[85838];;)if(C=i[0|A],A=g=A+1|0,255!=(0|C)){if(!C)break;if(!(r=f[144464+(C<<2)>>2]))continue;if(1==i[r+11|0]&&(I=i[r+14|0],!(B[r+8>>1]|I>>>0>4))){if(I>>>0<2)continue;a[0|e]=i[I+93943|0],e=e+1|0;continue}if(255&(A=f[r>>2]))for(;a[0|e]=A,e=e+1|0,r=65280&A,A=A>>>8|0,r;);if(A=g,21!=(0|C))continue;if((32|(g=a[0|A]))-97>>>0>=26)continue;for(;a[0|e]=g,e=e+1|0,(32|(g=a[0|(A=A+1|0)]))-97>>>0<26;);}a[0|e]=0}function He(A,e){var g,r,C=0,a=0,I=0;V=g=V-112|0,A||(f[50303]||le(),A=201216),oC(C=g+16|0,e,40),f[g>>2]=47,f[g+4>>2]=C,dg(e=g- -64|0,87599,g),r=Lg(e),I=-1;A:{e:{g:{if(a=f[A>>2]){e=0,C=-1;r:{for(;;){if(wg(g+16|0,f[a>>2])){if(wg(g+16|0,a=f[a+8>>2])?I=wg(g- -64|0,a+(Lg(a)-r|0)|0)?I:e:C=e,a=f[((e=e+1|0)<<2)+A>>2])continue;break r}break}if((0|e)>=0)break e;e=(0|C)<0?I:C;break g}if((0|(e=C))>=0)break g}e=I}if(C=0,(0|e)<0)break A}C=f[(e<<2)+A>>2]}return V=g+112|0,C}function Ne(A,e){var g,r,C=0,a=0,I=0,i=0,s=0;V=g=V-16|0,n(+e),r=0|b(1),a=0|b(0),2145386495==(0|(I=(C=2147483647&r)+-1048576|0))|I>>>0<2145386495?(i=a<<28,I=C>>>4|0,C=(15&C)<<28|a>>>4,a=I+1006632960|0):2146435072==(0|C)|C>>>0>2146435072?(i=a<<28,C=(15&r)<<28|a>>>4,a=r>>>4|2147418112):C|a?(Ve(g,a,I=C,0,0,(C=C?D(C):D(a)+32|0)+49|0),s=f[g>>2],i=f[g+4>>2],I=15372-C<<16,C=f[g+8>>2],a=I|65536^f[g+12>>2]):(C=0,a=0),f[A>>2]=s,f[A+4>>2]=i,f[A+8>>2]=C,f[A+12>>2]=-2147483648&r|a,V=g+16|0}function Pe(A,e,g){var r=0,C=0;A:{e:{g:{if(!(3&((C=A)^e))){r=!!(0|g);r:if(!(!(3&e)|!g))for(;;){if(r=i[0|e],a[0|C]=r,!r)break A;if(C=C+1|0,r=!!(0|(g=g-1|0)),!(3&(e=e+1|0)))break r;if(!g)break}if(!r)break e;if(!i[0|e])break A;if(!(g>>>0<4))for(;;){if(~(r=f[e>>2])&r-16843009&-2139062144)break g;if(f[C>>2]=r,C=C+4|0,e=e+4|0,!((g=g-4|0)>>>0>3))break}}if(!g)break e}for(;;){if(r=i[0|e],a[0|C]=r,!r)break A;if(C=C+1|0,e=e+1|0,!(g=g-1|0))break}}g=0}return ue(C,0,g),A}function Fe(A,e,g,r,C,a){var I,i=0,b=0,s=0;V=I=V-240|0,i=f[g>>2],f[I+232>>2]=i,g=f[g+4>>2],f[I>>2]=A,f[I+236>>2]=g,s=1;A:{e:{g:{if(g|1!=(0|i)){for(i=A;;){if((0|bC(g=i-f[(b=(r<<2)+a|0)>>2]|0,A,e))<=0){g=i;break g}r:{if(!((0|r)<2|C)){if(C=f[b-8>>2],(0|bC(b=i-4|0,g,e))>=0)break r;if((0|bC(b-C|0,g,e))>=0)break r}if(f[(s<<2)+I>>2]=g,zr(i=I+232|0,C=dr(i)),s=s+1|0,r=r+C|0,C=0,i=g,f[I+236>>2]|1!=f[I+232>>2])continue;break e}break}g=i;break e}g=A}if(C)break A}mg(I,s),xg(g,e,r,a)}V=I+240|0}function ye(A,e){var g=0;f[4+((g=A<<2)+134912|0)>>2]=e,f[g+136192>>2]=e,g=28;A:{e:{g:switch(A-1|0){case 0:f[50792]=e,f[50786]=e,UA(3);break e;case 1:f[50787]=e,f[33037]=(0|G(i[f[50797]+105596|0],(0|G(f[50787],55))/100|0))/16;break e;case 2:A=(0|e)>=99?99:e,f[50785]=(0|A)>0?A:0;break e;case 3:f[50788]=(0|e)>=99?99:e;break e;case 12:f[47268]=e;break e;case 6:f[47205]=e;break e;case 9:break e;case 8:break g;default:break A}(A=255&e)&&(f[f[47192]+152>>2]=A),f[47196]=e}g=0}return g}function ze(A,e){var g=0,r=0,C=0;A:if(f[A>>2])for(;;){if(er(f[A-4>>2])){if(r=0,(0|(g=a[0|e]))==f[A>>2])for(;(0|(g=a[(r=r+1|0)+e|0]))==f[(A=A+4|0)>>2];);if(!g){for(;e=A,A=A+4|0,er(f[e>>2]););for(C=e+((61==f[e>>2])<<2)|0;C=(A=C)+4|0,er(f[A>>2]););e:switch((e=f[A>>2])-34|0){case 0:case 5:break A;default:break e}return er(e)||47==f[A>>2]?102808:A}}if(!f[(A=A+4|0)>>2])break}return C}function Oe(A,e,g,r){var C,I,b=0,s=0,t=0;return!i[A+25|0]|Q[A+8>>3]!=g|Q[A+16>>3]!=r?(Q[A+16>>3]=r,Q[A+8>>3]=g,r=(b=$A(-3.141592653589793/(s=+f[A>>2])*r))*-b,Q[A+48>>3]=r,b*=Cg(-6.283185307179586/s*g),b+=b,Q[A+40>>3]=b,s=1-b-r,Q[A+32>>3]=s,!(t=i[A+24|0])|0==g||(s=1/s,Q[A+32>>3]=s,r*=g=-s,Q[A+48>>3]=r,b*=g,Q[A+40>>3]=b,t=1)):(t=i[A+24|0],r=Q[A+48>>3],b=Q[A+40>>3],s=Q[A+32>>3]),a[A+25|0]=1,g=Q[A+64>>3],C=Q[A+56>>3],Q[A+64>>3]=C,I=e,e=r*g+(s*e+b*C),Q[A+56>>3]=t?I:e,e}function Ze(A,e,g,r,C){var a=0,I=0,i=0,b=0,s=0;if((i=f[34388])&&!((0|(I=f[34436]))>=(f[34393]-2|0))){if(f[34436]=I+1,a=G(I,36)+i|0,f[a>>2]=A,f[a+4>>2]=f[34437],s=f[34438],f[a+12>>2]=e>>>24,f[a+8>>2]=16777215&e,f[a+24>>2]=s,e=f[50754],C=f[34439]+((C-f[34392]|0)/2|0)|0,f[a+20>>2]=C,e=E(b=1e3*+(0|C)/+(0|e))<2147483648?~~b:-2147483648,f[a+16>>2]=e,A-3>>>0<=1)return void(f[28+(G(I,36)+i|0)>>2]=f[33282]+g);e=28+(G(I,36)+i|0)|0,f[e>>2]=g,7==(0|A)&&(f[e+4>>2]=r)}}function Ke(A,e,g,r,C,a){var I=0,i=0,b=0,s=0;64&a?(e=31&(g=a+-64|0),(63&g)>>>0>=32?(g=0,e=C>>>e|0):(g=C>>>e|0,e=((1<>>e),r=0,C=0):a&&(b=r,I=31&(i=64-a|0),(63&i)>>>0>=32?(i=b<>>32-I|C<>>0>=32?(I=0,e=g>>>e|0):(I=g>>>e|0,e=((1<>>e),e|=s,g=I|i,I=r,r=31&a,(63&a)>>>0>=32?(i=0,r=C>>>r|0):(i=C>>>r|0,r=((1<>>r),C=i),f[A>>2]=e,f[A+4>>2]=g,f[A+8>>2]=r,f[A+12>>2]=C}function We(A){var e=0,g=0;if(!A){if(f[33174]&&(e=We(f[33174])),f[33136]&&(e=We(f[33136])|e),A=f[56816])for(;f[A+20>>2]!=f[A+28>>2]&&(e=We(A)|e),A=f[A+56>>2];);return e}return f[A+76>>2]>=0,f[A+20>>2]==f[A+28>>2]||(HC[f[A+36>>2]](A,0,0),f[A+20>>2])?((0|(e=f[A+8>>2]))!=(0|(g=f[A+4>>2]))&&(e=g-e|0,HC[f[A+40>>2]](A,e,e>>31,1)),e=0,f[A+28>>2]=0,f[A+16>>2]=0,f[A+20>>2]=0,f[A+4>>2]=0,f[A+8>>2]=0):e=-1,e}function Xe(A,e){var g=0,r=0,C=0,a=0,I=0;if(128==(192&(g=i[0|e])))for(;128==(192&(g=i[0|(e=e-1|0)])););A:if(128&(g=g<<24>>24)){if(r=1,192!=(0|(C=224&g)))if(224!=(240&g)){if(r=3,240!=(248&g)){g&=255,r=0;break A}}else r=2,I=1;g=i[r+93846|0]&g,(a=i[e+1|0])?(g=63&a|g<<6,192!=(0|C)&&((C=i[e+2|0])?(g=63&C|g<<6,I||((e=i[e+3|0])?g=63&e|g<<6:r=2)):r=1)):r=0}return f[A>>2]=g,r+1|0}function Le(A,e,g,r){var C,a=0,I=0;return V=C=V-224|0,A?(f[C>>2]=137584,f[C+4>>2]=47,f[C+8>>2]=e,dg(e=C+16|0,85430,C),(0|(a=fr(e)))<0?e=sr(r,0-a|0,C+16|0):(e=Ae(C+16|0,85659))?((I=f[A>>2])&&mA(I),a?(I=IA(a),f[A>>2]=I,I?(0|Eg(I,a,e))==(0|a)?(tr(e),e=0,g&&(f[g>>2]=a)):(g=f[56798],tr(e),mA(f[A>>2]),f[A>>2]=0,e=sr(r,g,C+16|0)):(tr(e),e=48)):(e=0,f[A>>2]=0)):e=sr(r,f[56798],C+16|0)):e=28,V=C+224|0,e}function Te(A,e){var g=0,r=0,C=0,a=0,I=0;if(128==(192&(g=i[0|e])))for(;128==(192&(g=i[0|(e=e+1|0)])););A:if(128&(g=g<<24>>24)){if(r=1,192!=(0|(C=224&g)))if(224!=(240&g)){if(r=3,240!=(248&g)){g&=255,r=0;break A}}else r=2,I=1;g=i[r+93846|0]&g,(a=i[e+1|0])?(g=63&a|g<<6,192!=(0|C)&&((C=i[e+2|0])?(g=63&C|g<<6,I||((e=i[e+3|0])?g=63&e|g<<6:r=2)):r=1)):r=0}return f[A>>2]=g,r+1|0}function Ve(A,e,g,r,C,a){var I=0,i=0,b=0;64&a?(r=e,e=31&(C=a+-64|0),(63&C)>>>0>=32?(C=r<>>32-e|g<>>0>=32?(i=I<>>32-r|C<>>0>=32?(C=0,e=g>>>r|0):(C=g>>>r|0,e=((1<>>r),r=b|e,C|=i,e=31&a,(63&a)>>>0>=32?(i=I<>>32-e|g<>2]=e,f[A+4>>2]=g,f[A+8>>2]=r,f[A+12>>2]=C}function Je(A){var e=0;return A>>>0<=55295?e=i[f[125552+(A>>>6&67108860)>>2]+(255&A)|0]:(e=4,A>>>0<57344||(A>>>0<63488?e=3:A>>>0<=195327?e=i[f[126416+(A-63488>>>6&67108860)>>2]+(255&A)|0]:(e=2,A>>>0<917504||(A>>>0<=918015?e=i[f[128476+(A-917504>>>6&67108860)>>2]+(255&A)|0]:A>>>0<983040||(A>>>0<1048574?e=3:A>>>0<1048576||(e=3,A>>>0<1114110||(e=A>>>0<1114112?2:5))))))),255&e}function Re(A,e){var g=0,r=0,C=0,a=0,I=0,i=0,b=0,s=0,t=0;if(!((0|(g=f[33709]))<=0)){if(C=(0|A)>31?A-32|0:A,A=0,g>>>0>=4)for(t=-4&g;i=2|A,b=1|A,r=f[134912+((I=3|A)<<6)>>2]==(0|C)?I:f[134912+(i<<6)>>2]==(0|C)?i:f[134912+(b<<6)>>2]==(0|C)?b:f[134912+(A<<6)>>2]==(0|C)?A:r,A=A+4|0,(0|t)!=(0|(a=a+4|0)););if(a=3&g)for(;r=f[134912+(A<<6)>>2]==(0|C)?A:r,A=A+1|0,(0|a)!=(0|(s=s+1|0)););(0|r)<=0||(f[33709]=r,g=r)}XA(e,g)}function Ue(A){var e,g=0,r=0,C=0;for(V=e=V-96|0,oC(e,A,60),Ag(e,1);C=ar(a[0|(r=e+g|0)]),a[0|r]=C,g=g+1|0,255&C;);f[e+92>>2]=0,f[e+84>>2]=0,f[e+88>>2]=0,f[e+76>>2]=0,f[e+80>>2]=0,f[e+72>>2]=A;A:{e:{g:{if(CA(e,1)){if(i[202976])break g;break e}if(f[50303]||le(),g=268437247,!(A=He(201216,e)))break A;if(!CA(f[A+8>>2],0))break A;if(!i[202976])break e}CA(202976,2)}Yr(f[32972]),f[e+76>>2]=f[32972]+40,og(e+72|0,202976),g=0}return V=e+96|0,g}function je(A,e){if(!A)return 0;A:{e:{if(A){if(e>>>0<=127)break e;if(f[f[56841]>>2]){if(e>>>0<=2047){a[A+1|0]=63&e|128,a[0|A]=e>>>6|192,A=2;break A}if(!(57344!=(-8192&e)&e>>>0>=55296)){a[A+2|0]=63&e|128,a[0|A]=e>>>12|224,a[A+1|0]=e>>>6&63|128,A=3;break A}if(e-65536>>>0<=1048575){a[A+3|0]=63&e|128,a[0|A]=e>>>18|240,a[A+2|0]=e>>>6&63|128,a[A+1|0]=e>>>12&63|128,A=4;break A}}else if(57216==(-128&e))break e;f[56798]=25,A=-1}else A=1;break A}a[0|A]=e,A=1}return A}function Se(A){var e=0,g=0,r=0,C=0;if(f[A+20>>2]=0,(r=(g=f[A+8>>2])-(e=f[A+4>>2])|0)>>>0>=9)for(;mA(f[e>>2]),e=f[A+4>>2]+4|0,f[A+4>>2]=e,(r=(g=f[A+8>>2])-e|0)>>>0>8;);C=512;A:switch((r>>>2|0)-1|0){case 1:C=1024;case 0:f[A+16>>2]=C;break;default:break A}if((0|e)!=(0|g)){for(;mA(f[e>>2]),(0|g)!=(0|(e=e+4|0)););(0|(e=f[A+8>>2]))!=(0|(g=f[A+4>>2]))&&(f[A+8>>2]=e+(3+(g-e|0)&-4))}(A=f[A>>2])&&mA(A)}function qe(A,e,g){var r=0,C=0;r=!!(0|g);A:{e:{g:if(!(!(3&A)|!g))for(C=255&e;;){if((0|C)==i[0|A])break e;if(r=!!(0|(g=g-1|0)),!(3&(A=A+1|0)))break g;if(!g)break}if(!r)break A;if(!(i[0|A]==(255&e)|g>>>0<4))for(r=G(255&e,16843009);;){if(~(C=r^f[A>>2])&C-16843009&-2139062144)break e;if(A=A+4|0,!((g=g-4|0)>>>0>3))break}if(!g)break A}for(e&=255;;){if((0|e)==i[0|A])return A;if(A=A+1|0,!(g=g-1|0))break}}return 0}function _e(A,e){var g=0,r=0;A:{if(r=255&e){if(3&A)for(;;){if(!(g=i[0|A])|(0|g)==(255&e))break A;if(!(3&(A=A+1|0)))break}e:if(!(~(g=f[A>>2])&g-16843009&-2139062144))for(r=G(r,16843009);;){if(~(g^=r)&g-16843009&-2139062144)break e;if(g=f[A+4>>2],A=A+4|0,g-16843009&~g&-2139062144)break}for(;(r=i[0|(g=A)])&&(A=g+1|0,(0|r)!=(255&e)););return g}return Lg(A)+A|0}return A}function $e(A,e,g,r,C){var a,I=0,f=0;f=-1;A:if(!(((I=2147418112==(0|(a=2147483647&r)))&!g?A|e:I&!!(0|g)|a>>>0>2147418112)||(I=2147483647&C)>>>0>2147418112&2147418112!=(0|I))){if(!(A|g|I|a|e))return 0;if((0|(I=r&C))>0|(0|I)>=0){if((!!(0|g)|(0|r)!=(0|C))&(0|r)<(0|C))break A;return!!(A|g|r^C|e)}(!g&(0|r)==(0|C)?A|e:!!(0|g)&(0|r)>=(0|C)|(0|r)>(0|C))||(f=!!(A|g|r^C|e))}return f}function Ag(A,e){var g,r=0;V=g=V+-64|0,a[202976]=0,f[g+48>>2]=47,dg(g+59|0,91351,g+48|0),e||(a[g+59|0]=0);A:{e:{if(A&&(A=sC(A,43))){if(a[0|A]=0,a[0|(A=A+1|0)]-48>>>0>=10)break e;r=Dg(A)}if((0|r)<=0)break A;if(r>>>0<=9){f[g+4>>2]=r,f[g>>2]=g+59,dg(202976,91378,g);break A}f[g+20>>2]=r-10,f[g+16>>2]=g+59,dg(202976,91503,g+16|0);break A}f[g+36>>2]=A,f[g+32>>2]=g+59,dg(202976,85425,g+32|0)}V=g- -64|0}function eg(A){var e,g=0,r=0,C=0;V=e=V-80|0,g=wA(A,e+12|0),f[e+12>>2]?(oC(r=e+16|0,g,60),g=0,Ag(r,1),!CA(r,0)|!i[202976]||CA(202976,2),Yr(f[32972]),og(A,86012)):g=268437247,V=e+80|0;A:{e:{g:{r:{if((0|g)<=268437502){if(!g)break A;if(268436479==(0|g))break e;if(268437247!=(0|g))break r;return 2}if(268437503==(0|g)|268437759==(0|g))break g;if(268439295==(0|g))break A}return-1}return 2}C=1}return C}function gg(A,e,g,r,C,a,I,i,b){var s,t,n;b=Cr(e,g,i,b),i=U,C=Cr(r,C,a,I),r=U+i|0,i=C>>>0>(b=C+b|0)>>>0?r+1|0:r,s=I,t=g,I=(g=Cr(I,C=0,g,r=0))+b|0,b=U+i|0,n=I,g=g>>>0>I>>>0?b+1|0:b,I=Cr(a,0,e,0),i=U,r=Cr(a,b=0,t,r),a=U+b|0,a=r>>>0>(i=i+r|0)>>>0?a+1|0:a,r=g,a=a>>>0>(b=a+n|0)>>>0?r+1|0:r,g=Cr(e,0,s,C)+i|0,C=U,i=(C=g>>>0>>0?C+1|0:C)+b|0,b=a,f[A+8>>2]=i,f[A+12>>2]=C>>>0>i>>>0?b+1|0:b,f[A>>2]=I,f[A+4>>2]=g}function rg(A,e){var g=0,r=0;A:{if(3&((r=A)^e))g=i[0|e];else{if(3&e)for(;;){if(g=i[0|e],a[0|r]=g,!g)break A;if(r=r+1|0,!(3&(e=e+1|0)))break}if(!(~(g=f[e>>2])&g-16843009&-2139062144))for(;f[r>>2]=g,g=f[e+4>>2],r=r+4|0,e=e+4|0,!(g-16843009&~g&-2139062144););}if(a[0|r]=g,255&g)for(;g=i[e+1|0],a[r+1|0]=g,r=r+1|0,e=e+1|0,g;);}return A}function Cg(A){var e,g=0,r=0;V=e=V-16|0,n(+A),r=0|b(1),b(0);A:if((r&=2147483647)>>>0<=1072243195){if(g=1,r>>>0<1044816030)break A;g=Rg(A,0)}else if(g=A-A,!(r>>>0>=2146435072)){e:switch(3&oA(A,e)){case 0:g=Rg(Q[e>>3],Q[e+8>>3]);break A;case 1:g=-Og(Q[e>>3],Q[e+8>>3],1);break A;case 2:g=-Rg(Q[e>>3],Q[e+8>>3]);break A;default:break e}g=Og(Q[e>>3],Q[e+8>>3],1)}return V=e+16|0,A=g}function ag(A,e,g,r){var C=0,a=0,I=0;if(!((Lg(r)+Lg(e)|0)>=(0|g))){for(I=f[36115],g=r;C=i[0|g];)if(g=g+1|0,!((0|C)>=(0|I))){A:{e:switch(C=f[144464+(C<<2)>>2],i[C+11|0]-1|0){case 1:break A;case 0:break e;default:continue}a=i[C+14|0]<4|a;continue}1&(i[C+4|0]>>>1|a)||(f[A+8212>>2]=f[A+8212>>2]+1),f[A+8208>>2]=f[A+8208>>2]+1,a=0}e&&mC(e,r)}}function Ig(A){var e,g=0;V=e=V-16|0,n(+A),g=0|b(1),b(0);A:if((g&=2147483647)>>>0<=1072243195){if(g>>>0<1045430272)break A;A=Og(A,0,0)}else if(g>>>0>=2146435072)A-=A;else{e:switch(3&oA(A,e)){case 0:A=Og(Q[e>>3],Q[e+8>>3],1);break A;case 1:A=Rg(Q[e>>3],Q[e+8>>3]);break A;case 2:A=-Og(Q[e>>3],Q[e+8>>3],1);break A;default:break e}A=-Rg(Q[e>>3],Q[e+8>>3])}return V=e+16|0,A}function fg(A){var e=0;f[A+296>>2]=303173648,f[A+300>>2]=370677780,e=f[26341],f[A+304>>2]=f[26340],f[A+308>>2]=e,e=f[26343],f[A+312>>2]=f[26342],f[A+316>>2]=e,LA(A),f[A+56>>2]=2,f[A+36>>2]=3,f[A+40>>2]=1074,a[A+168|0]=5,f[A+132>>2]=32,f[A+104>>2]=1032,f[A+108>>2]=66,f[A+8>>2]=5,f[A+12>>2]=32,a[A+365|0]=64|i[A+365|0],a[A+368|0]=64|i[A+368|0],a[A+396|0]=64|i[A+396|0],a[A+399|0]=64|i[A+399|0]}function ig(A,e,g){var r=0,C=0,a=0;A:{if(!(r=f[g+16>>2])){if(or(g))break A;r=f[g+16>>2]}if(r-(a=f[g+20>>2])>>>0>>0)return 0|HC[f[g+36>>2]](g,A,e);e:if(f[g+80>>2]<0)r=0;else{for(C=e;;){if(!(r=C)){r=0;break e}if(10==i[(C=r-1|0)+A|0])break}if((C=0|HC[f[g+36>>2]](g,A,r))>>>0>>0)break A;A=A+r|0,e=e-r|0,a=f[g+20>>2]}_A(a,A,e),f[g+20>>2]=f[g+20>>2]+e,C=e+r|0}return C}function bg(A){var e,g=0,r=0;A:{if((0|(e=f[34064]))>0)for(;;){if((r=f[136284+(g<<4)>>2])&&!Qr(A,r)){if(f[136276+(g<<4)>>2])return g;if(r=-1,yA(0,g))break A;return g}if((0|e)==(0|(g=g+1|0)))break}r=-1,yA(A,e)||(g=OA(f[12+(136272+(f[34064]<<4)|0)>>2],Lg(A)+1|0),r=f[34064],f[12+(136272+(r<<4)|0)>>2]=g,rg(g,A),f[34064]=r+1)}return r}function sg(A,e){var g,r=0,C=0,I=0,i=0;for(V=g=V-16|0,a[0|e]=0,(r=15&f[A>>2])&&(e=(C=Lg(e=rg(e,Gr(128496,64|r))))+e|0),r=8;;){A:{e:{if(r>>>0<=29){if(f[A>>2]>>>r&1)break e;break A}if(!(f[A+4>>2]>>>r-32&1)|r>>>0<32)break A}(0|(C=(i=Lg(I=Gr(128496,r))+1|0)+C|0))>=80||(f[g>>2]=I,dg(e,84439,g),e=e+i|0)}if(64==(0|(r=r+1|0)))break}V=g+16|0}function tg(A,e,g){var r,C=0,I=0,b=0;if(V=r=V-16|0,i[0|A])for(b=f[30450];;)if(I=A,A=A+1|0,!(32==(0|(C=a[0|I]))|C-9>>>0<5)){for((0|(C=Dg(I)))>0&&((0|C)<32?f[e>>2]=f[e>>2]|1<>2]=C,f[r>>2]=I,eC(b,84902,r)),I=A);I=(A=I)+1|0,(C=a[0|A])-48>>>0<10|(32|C)-97>>>0<26;);if(!C)break}V=r+16|0}function ng(A,e,g){var r=0,C=0,I=0,i=0;A:if(e&&!((0|(C=g-4|0))<=0))for(I=34!=(0|(g=f[e-4>>2]))?39==(0|g)?g:0:g,g=0;;){if(i=g,!(g=f[e>>2]))break A;e:{if(!I){if(32==(0|g)|g-9>>>0<5)break A;if(47!=(0|g))break e;break A}if(92!=(0|i)&&(0|g)==(0|I))break A}if(e=e+4|0,!((0|C)>(0|(r=Fg(g,A+r|0)+r|0))))break}return a[A+r|0]=0,r}function kg(A){var e=0;A:if(!Mr(A)){e=0;e:if(!(A>>>0<768)){if(A-2305>>>0<=1270){if((124&A)>>>0<100)break A;if(e=1,Fr(93850,A))break e;return A-3450>>>0<6}if(1541==(0|A)|A-1456>>>0<19|1648==(0|A))break A;if(10240==(0|(e=-256&A))|4352==(0|e)|A-3904>>>0<125|A>>>0<880)break A;e=1,A-1611>>>0<20||(e=A-12353>>>0<30400)}return e}return 1}function og(A,e){var g=0;A?((g=f[A+4>>2])&&rg(133208,g),(g=f[A>>2])&&oC(133168,g,40),f[33289]=i[A+14|0],f[33291]=i[A+13|0],f[33290]=i[A+12|0],oC(134672,33!=i[0|e]|118!=i[e+1|0]?e:(47==i[e+2|0]?3:0)+e|0,40),A=f[50298],f[33678]=f[50297],f[33679]=A,A=f[50302],f[33682]=f[50301],f[33683]=A,A=f[50300],f[33680]=f[50299],f[33681]=A):ue(133152,0,76)}function Bg(A,e,g,r){a[A+53|0]=1;A:if(f[A+4>>2]==(0|g)){a[A+52|0]=1;e:{if(!(g=f[A+16>>2])){if(f[A+36>>2]=1,f[A+24>>2]=r,f[A+16>>2]=e,1!=(0|r))break A;if(1==f[A+48>>2])break e;break A}if((0|e)==(0|g)){if(2==(0|(g=f[A+24>>2]))&&(f[A+24>>2]=r,g=r),1!=f[A+48>>2])break A;if(1==(0|g))break e;break A}f[A+36>>2]=f[A+36>>2]+1}a[A+54|0]=1}}function cg(A,e){var g=0,r=0,C=0,I=0,i=0;A:if(g=f[e>>2])for(;;){r=0;e:if(A){for(;i=a[r+g|0],(C=f[(r<<2)+A>>2])&&(r=r+1|0,(0|i)==(0|C)););g:switch(C-34|0){case 0:case 5:break g;default:break e}if(!i)break A}if(!(g=f[((I=I+1|0)<<3)+e>>2]))break}return f[4+((I<<3)+e|0)>>2]}function Qg(A,e){A:if((0|e)>=1024){if(A*=898846567431158e293,e>>>0<2047){e=e-1023|0;break A}A*=898846567431158e293,e=((0|e)>=3069?3069:e)-2046|0}else(0|e)>-1023||(A*=2004168360008973e-307,e>>>0>4294965304?e=e+969|0:(A*=2004168360008973e-307,e=((0|e)<=-2960?-2960:e)+1938|0));return s(0,0),s(1,e+1023<<20),A*+t()}function Gg(A,e,g,r){var C,I,i,b=0;return V=I=V-16|0,f[I+12>>2]=r,V=C=V-160|0,i=e?A:C+158|0,f[C+144>>2]=i,b=-1,A=e-1|0,f[C+148>>2]=A>>>0<=e>>>0?A:0,A=ue(C,0,144),f[A+76>>2]=-1,f[A+36>>2]=17,f[A+80>>2]=-1,f[A+44>>2]=A+159,f[A+84>>2]=A+144,(0|e)<0?f[56798]=61:(a[0|i]=0,b=Me(A,g,r,15,16)),V=A+160|0,V=I+16|0,b}function wg(A,e){var g=0,r=0,C=0;A:if(g=i[0|A])for(;;){if(!(r=i[0|e])){C=g;break A}if((0|g)!=(0|r)&&(0|(r=g-65>>>0<26?32|g:g))!=(0|((g=i[0|e])-65>>>0<26?32|g:g))){C=i[0|A];break A}if(e=e+1|0,g=i[A+1|0],A=A+1|0,!g)break}return(C=(A=255&C)-65>>>0<26?32|A:A)-((A=i[0|e])-65>>>0<26?32|A:A)|0}function Eg(A,e,g){var r=0,C=0;if(r=f[g+72>>2],f[g+72>>2]=r-1|r,(0|(r=f[g+4>>2]))==(0|(C=f[g+8>>2]))?r=e:(_A(A,r,r=e>>>0>(r=C-r|0)>>>0?r:e),f[g+4>>2]=r+f[g+4>>2],A=A+r|0,r=e-r|0),r)for(;;){if(Wg(g)||!(C=0|HC[f[g+32>>2]](g,A,r)))return e-r|0;if(A=A+C|0,!(r=r-C|0))break}return e}function Dg(A){for(var e=0,g=0,r=0,C=0;A=(e=A)+1|0,32==(0|(g=a[0|e]))|g-9>>>0<5;);A:{e:{g:switch((g=a[0|e])-43|0){case 0:break e;case 2:break g;default:break A}C=1}g=a[0|A],e=A}if(g-48>>>0<10)for(;r=48+(G(r,10)-a[0|e]|0)|0,A=a[e+1|0],e=e+1|0,A-48>>>0<10;);return C?r:0-r|0}function ug(A,e){var g,r,C,a=0;return V=g=V-32|0,f[e>>2]=0,f[e+4>>2]=0,f[(a=r=e+24|0)>>2]=0,f[a+4>>2]=0,f[(a=C=e+16|0)>>2]=0,f[a+4>>2]=0,f[(a=e+8|0)>>2]=0,f[a+4>>2]=0,f[g+28>>2]=e+28,f[g+24>>2]=r,f[g+20>>2]=e+20,f[g+16>>2]=C,f[g+12>>2]=e+12,f[g+8>>2]=a,f[g+4>>2]=e+4,f[g>>2]=e,A=aA(A,84553,g),V=g+32|0,A}function lg(A){var e=0,g=0,r=0;if((e=i[0|A])&&((g=i[A+1|0])?(g=e|g<<8,(e=i[A+2|0])&&(g|=e<<16,(A=i[A+3|0])&&(g|=A<<24))):g=e),(0|(e=f[36115]))>0)for(A=0;;){if(!(!(r=f[144464+(A<<2)>>2])|f[r>>2]!=(0|g)))return i[r+10|0];if((0|e)==(0|(A=A+1|0)))break}return 0}function xg(A,e,g,r){var C,a=0,I=0,i=0,b=0,s=0;V=C=V-240|0,f[C>>2]=A,i=1;A:if(!((0|g)<2))for(a=A;;){if((0|bC(A,I=(a=a-4|0)-f[((b=g-2|0)<<2)+r>>2]|0,e))>=0&&(0|bC(A,a,e))>=0)break A;if(s=I,a=(I=(0|bC(I,a,e))>=0)?s:a,f[(i<<2)+C>>2]=a,i=i+1|0,!((0|(g=I?g-1|0:b))>1))break}mg(C,i),V=C+240|0}function dg(A,e,g){var r,C,I,i=0;return V=C=V-16|0,f[C+12>>2]=g,V=r=V-160|0,_A(I=r+8|0,124528,144),f[r+52>>2]=A,f[r+28>>2]=A,i=(i=-2-A|0)>>>0>2147483647?2147483647:i,f[r+56>>2]=i,A=A+i|0,f[r+36>>2]=A,f[r+24>>2]=A,A=QC(I,e,g),i&&(e=f[r+28>>2],a[e-((0|e)==f[r+24>>2])|0]=0),V=r+160|0,V=C+16|0,A}function mg(A,e){var g,r=0,C=0,a=0,I=0,i=0;if(r=4,V=g=V-256|0,(0|e)>=2)for(f[(i=(e<<2)+A|0)>>2]=g;;){for(a=r>>>0>=256?256:r,_A(f[i>>2],f[A>>2],a),C=0;I=(C<<2)+A|0,C=C+1|0,_A(f[I>>2],f[(C<<2)+A>>2],a),f[I>>2]=f[I>>2]+a,(0|e)!=(0|C););if(!(r=r-a|0))break}V=g+256|0}function Mg(A,e,g){var r,C=0;return V=r=V-96|0,f[r+88>>2]=0,f[r+92>>2]=1073741824,f[r+84>>2]=e,e=TA(A,r+84|0,g,r+88|0,2,0),536870912&(C=f[r+88>>2])?(e=f[47202],f[47202]=0,a[r+2|0]=32,I[r>>1]=8192,oC(C=3|r,f[r+84>>2],77),A=ke(A,C,0,0),rg(g,189088),f[47202]=e):A=e?C:0,V=r+96|0,A}function vg(A,e,g){var r=0,C=0,I=0;C=Ng(A),r=f[e>>2];A:{e:if((0|C)>=0){if(r){if(!Qr(A,g))break e;(I=f[r+688>>2])&&mA(I),mA(r),f[e>>2]=0}f[e>>2]=q(A),A=rg(g,A),HA(g=f[e>>2],g+228|0,0)&&(qr(f[f[32972]+60>>2]),a[0|A]=0,C=-1),r=f[e>>2],f[r+292>>2]=C}else if(!r)break A;a[r+268|0]=0}return C}function hg(A){var e=0,g=0;return(0|(e=f[A+76>>2]))>=0&(!e|f[56823]!=(-1073741825&e))?(g=f[(e=A+76|0)>>2],f[e>>2]=g||1073741823,(0|(g=f[A+4>>2]))==f[A+8>>2]?A=Nr(A):(f[A+4>>2]=g+1,A=i[0|g]),f[e>>2]=0,A):(0|(e=f[A+4>>2]))!=f[A+8>>2]?(f[A+4>>2]=e+1,i[0|e]):Nr(A)}function pg(A,e,g){var r=0,C=0;A:{e:{if(g>>>0>=4){if(3&(A|e))break e;for(;;){if(f[A>>2]!=f[e>>2])break e;if(e=e+4|0,A=A+4|0,!((g=g-4|0)>>>0>3))break}}if(!g)break A}for(;;){if((0|(r=i[0|A]))==(0|(C=i[0|e]))){if(e=e+1|0,A=A+1|0,g=g-1|0)continue;break A}break}return r-C|0}return 0}function Yg(A,e){var g,r=0,C=0,a=0;V=g=V-16|0,e?(Ve(g,C=((r=e>>31)^e)-r|0,0,0,0,(r=D(C))+81|0),C=0+f[g+8>>2]|0,r=(65536^f[g+12>>2])+(16414-r<<16)|0,a=-2147483648&e|(r=C>>>0>>0?r+1|0:r),r=f[g+4>>2],e=f[g>>2]):e=0,f[A>>2]=e,f[A+4>>2]=r,f[A+8>>2]=C,f[A+12>>2]=a,V=g+16|0}function Hg(A){var e,g=0,r=0,C=0;if((0|(g=_e(A,61)))==(0|A))return 0;A:if(!i[(e=g-A|0)+A|0]&&(g=f[56800])&&(r=f[g>>2])){for(;;){if(qg(A,r,e)||(r=f[g>>2]+e|0,61!=i[0|r])){if(r=f[g+4>>2],g=g+4|0,r)continue;break A}break}C=r+1|0}return C}function Ng(A){var e=0,g=0;A:if(!((0|(g=f[34461]))<=0)){for(;;){if(!Qr(A,G(e,44)+137856|0)){f[34457]=e;break A}if((0|g)==(0|(e=e+1|0)))break}return-1}return(A=(0|e)==(0|g))?-1:((0|(A=A?-1:e))!=f[36114]&&(f[36115]=0,de(A),f[36114]=A,f[36115]=f[36115]+1),e)}function Pg(A,e,g,r){var C=0,a=0;A:if(32!=(32|i[0|e])){if(C=((0|r)>2)<<1,a=DA(A,e,g,C=(0|r)>1?4|C:C),21!=i[0|g])for(C|=1,e=e+a|0,a=1;;){if(32==(32|i[0|e]))break A;if(e=DA(A,e,g,C)+e|0,a=a+1|0,21==i[0|g])break}return rg(189088,g),0}return ve(A,g,r,a),e}function Fg(A,e){var g,r=0,C=0,I=0;if(A>>>0<=127)return a[0|e]=A,1;if(A>>>0>=1114112)return a[0|e]=32,1;for(r=G(g=A>>>0<2048?1:A>>>0<65536?2:3,6),a[0|e]=i[g+93842|0]|A>>>r;r=r-6|0,a[(C=C+1|0)+e|0]=A>>>r&63|128,(0|(I=I+1|0))!=(0|g););return g+1|0}function yg(A){var e=0,g=0;A:{if((0|(e=f[A+12>>2]))>=f[A+16>>2]){if(e=0,(0|(g=0|K(f[A+8>>2],A+24|0,2048)))<=0){if(!g|-44==(0|g))break A;return f[56798]=0-g,0}f[A+16>>2]=g}g=e,e=A+e|0,f[A+12>>2]=g+B[e+40>>1],g=f[e+36>>2],f[A>>2]=f[e+32>>2],f[A+4>>2]=g,e=e+24|0}return e}function zg(A,e){var g,r=0;if(r=f[A+632>>2])return!!(0|Pr(r,e));A:{e:{if((0|(g=f[A+600>>2]))>0){if(r=0,(e=e-g|0)-1>>>0<255)break e;break A}if((r=e-192|0)>>>0<=413)return 128&i[344+(i[r+94240|0]+A|0)|0];if(r=0,e>>>0>255)break A}r=128&i[344+(A+e|0)|0]}return r}function Og(A,e,g){var r,C,a;return a=(r=A*A)*(r*r)*(1.58969099521155e-10*r-2.5050760253406863e-8)+(r*(27557313707070068e-22*r-.0001984126982985795)+.00833333333332249),C=r*A,g?A-(r*(.5*e-a*C)-e+.16666666666666632*C):C*(r*a-.16666666666666632)+A}function Zg(A,e){var g,r=0,C=0,a=0;V=g=V-16|0,e?(Ve(g,r=e,0,0,0,(e=D(e))+81|0),r=0+f[g+8>>2]|0,e=(65536^f[g+12>>2])+(16414-e<<16)|0,a=C>>>0>r>>>0?e+1|0:e,C=f[g+4>>2],e=f[g>>2]):e=0,f[A>>2]=e,f[A+4>>2]=C,f[A+8>>2]=r,f[A+12>>2]=a,V=g+16|0}function Kg(A){var e,g=0;V=e=V-16|0,a[e+15|0]=10;A:{if(!(g=f[A+16>>2])){if(or(A))break A;g=f[A+16>>2]}(0|g)==(0|(g=f[A+20>>2]))|10==f[A+80>>2]?HC[f[A+36>>2]](A,e+15|0,1):(f[A+20>>2]=g+1,a[0|g]=10)}V=e+16|0}function Wg(A){var e,g=0;return g=f[A+72>>2],f[A+72>>2]=g-1|g,f[A+20>>2]!=f[A+28>>2]&&HC[f[A+36>>2]](A,0,0),f[A+28>>2]=0,f[A+16>>2]=0,f[A+20>>2]=0,4&(g=f[A>>2])?(f[A>>2]=32|g,-1):(e=f[A+44>>2]+f[A+48>>2]|0,f[A+8>>2]=e,f[A+4>>2]=e,g<<27>>31)}function Xg(A){var e=0;A:{e:{g:{r:{if((0|(A=Ue(A)))<=268437502){if(!A)break A;if(268436479==(0|A))break e;if(268437247!=(0|A))break r;return 2}if(268437503==(0|A)|268437759==(0|A))break g;if(268439295==(0|A))break A}return-1}return 2}e=1}return e}function Lg(A){var e=0,g=0,r=0;A:{if(3&(e=A))for(;;){if(!i[0|e])break A;if(!(3&(e=e+1|0)))break}for(;g=e,e=e+4|0,!(~(r=f[g>>2])&r-16843009&-2139062144););for(;g=(e=g)+1|0,i[0|e];);}return e-A|0}function Tg(A,e,g){var r,C=0,a=0,I=0;f[A+112>>2]=e,f[A+116>>2]=g,r=f[A+4>>2],C=f[A+44>>2]-r|0,f[A+120>>2]=C,f[A+124>>2]=C>>31,C=f[A+8>>2],e|g&&((0|g)>=(0|(I=(a=C-r|0)>>31))&e>>>0>=a>>>0|(0|g)>(0|I)||(C=e+r|0)),f[A+104>>2]=C}function Vg(A,e){var g,r,C=0;if(n(+A),g=0|b(1),r=0|b(0),2047!=(0|(C=g>>>20&2047))){if(!C)return 0==A?C=0:(A=Vg(0x10000000000000000*A,e),C=f[e>>2]+-64|0),f[e>>2]=C,A;f[e>>2]=C-1022,s(0,0|r),s(1,-2146435073&g|1071644672),A=+t()}return A}function Jg(A,e,g){return f[A+20>>2]!=f[A+28>>2]&&(HC[f[A+36>>2]](A,0,0),!f[A+20>>2])||(f[A+28>>2]=0,f[A+16>>2]=0,f[A+20>>2]=0,HC[f[A+40>>2]](A,e,g,0),(0|U)<0)?-1:(f[A+4>>2]=0,f[A+8>>2]=0,f[A>>2]=-17&f[A>>2],0)}function Rg(A,e){var g,r,C=0;return(r=1-(C=.5*(g=A*A)))+(1-r-C+(g*(g*(g*(2480158728947673e-20*g-.001388888888887411)+.0416666666666666)+(C=g*g)*C*(g*(-11359647557788195e-27*g+2.087572321298175e-9)-2.7557314351390663e-7))-A*e))}function Ug(A,e,g){var r=0,C=0;if(e)for(;g=g-1|0,C=A,A=xC(A,e,10),r=U,a[0|g]=C-Cr(A,r,10,0)|48,C=e>>>0>9,e=r,C;);if(A)for(;e=(A>>>0)/10|0,a[0|(g=g-1|0)]=A-G(e,10)|48,r=A>>>0>9,A=e,r;);return g}function jg(A,e){var g=0,r=0,C=0;A:if(A){for(;C=a[e+g|0],(r=f[(g<<2)+A>>2])&&(g=g+1|0,(0|C)==(0|r)););e:switch(r-34|0){case 0:case 5:break e;default:break A}if(!C)return 0}return 1}function Sg(A,e,g){var r,C=0;return V=r=V-16|0,!(64&e)&&(C=0,4259840&~e)||(f[r+12>>2]=g+4,C=f[g>>2]),f[r>>2]=C,f[r+4>>2]=0,(A=0|v(-100,0|A,32768|e,0|r))>>>0>=4294963201&&(f[56798]=0-A,A=-1),V=r+16|0,A}function qg(A,e,g){var r=0,C=0,a=0;if(!g)return 0;A:if(r=i[0|A]){for(;;){if((C=i[0|e])&&!(!(g=g-1|0)|(0|r)!=(0|C))){if(e=e+1|0,r=i[A+1|0],A=A+1|0,r)continue;break A}break}a=r}return(255&a)-i[0|e]|0}function _g(A,e){var g,r=0;V=r=V-128|0,r=ue(r,0,128),a[r+98|0]=9,a[r+66|0]=9,a[r+34|0]=A,I[r+68>>1]=1,g=f[36125],f[r+104>>2]=g,f[r+72>>2]=g,f[r+40>>2]=f[144464+(A<<2)>>2],a[r+2|0]=9,f[r+8>>2]=g,bA(0,0,r+32|0,e,0),V=r+128|0}function $g(A){var e=0,g=0,r=0;if(a[f[A>>2]]-48>>>0>=10)return 0;for(;r=f[A>>2],g=-1,e>>>0<=214748364&&(g=(0|(g=a[0|r]-48|0))>(2147483647^(e=G(e,10)))?-1:g+e|0),f[A>>2]=r+1,e=g,a[r+1|0]-48>>>0<10;);return e}function Ar(A,e){var g=0;if(!(!A|f[A>>2]-48>>>0>=10)){if(f[A>>2]-48>>>0<10)for(;g=(f[A>>2]+G(g,10)|0)-48|0,f[(A=A+4|0)>>2]-48>>>0<10;);1==(0|e)&&(g=115==(0|ar(f[A>>2]))?G(g,1e3):g)}return g}function er(A){var e=0;e=1;A:{e:switch(0|Je(A)){case 30:if(e=0,160==(0|A)|8199==(0|A)|8239==(0|A))break A;return 1;case 0:if(A-9>>>0<5)return 1;if(133==(0|A))break A;break;case 28:case 29:break A;default:break e}e=0}return e}function gr(A,e,g){var r,C=0;return V=r=V-16|0,a[r+6|0]=0,a[r+7|0]=95,C=Fg(C=e,e=r+8|0)+r|0,a[C+8|0]=32,a[C+9|0]=0,Mg(A,r+7|0,g)||(a[r+7|0]=32,Mg(A,e,g)||GA(A,e,g,20,0,0,0)),V=r+16|0,a[0|g]}function rr(A){var e=0,g=0;g=170;A:if(!((0|A)<170))for(;;){if((0|A)==(0|g))return B[101616+(e<<1|2)>>1];if(124==(2147483646&(e=e+2|0)))break A;if(!((0|(g=B[101616+(e<<1)>>1]))<=(0|A)))break}return 0}function Cr(A,e,g,r){var C,a,I,f,i=0,b=0;return f=G(i=g>>>16|0,b=A>>>16|0),i=(65535&(b=((I=G(C=65535&g,a=65535&A))>>>16|0)+G(b,C)|0))+G(i,a)|0,U=(G(e,g)+f|0)+G(A,r)+(b>>>16)+(i>>>16)|0,65535&I|i<<16}function ar(A){var e=0,g=0,r=0,C=0;for(e=2778;;){if((0|(g=f[1040+((r=(e+C|0)/2|0)<<4)>>2]))==(0|A))return(e=f[1048+(r<<4)>>2])||A;if(!((0|(C=(g=A>>>0>g>>>0)?r+1|0:C))<=(0|(e=g?e:r-1|0))))break}return A}function Ir(A){var e=0,g=0,r=0,C=0;for(e=2778;;){if((0|(g=f[1040+((r=(e+C|0)/2|0)<<4)>>2]))==(0|A))return(e=f[1044+(r<<4)>>2])||A;if(!((0|(C=(g=A>>>0>g>>>0)?r+1|0:C))<=(0|(e=g?e:r-1|0))))break}return A}function fr(A){var e,g=0;return V=e=V-112|0,(A=0|z(0|A,0|e))>>>0>=4294963201&&(f[56798]=0-A,A=-1),g=0-f[56798]|0,A||(g=-31,16384!=(61440&f[e+12>>2])&&(g=f[e+40>>2])),V=e+112|0,A=g}function ir(A,e,g){var r;if(!(r=f[A+16>>2]))return f[A+36>>2]=1,f[A+24>>2]=g,void(f[A+16>>2]=e);A:{if((0|e)==(0|r)){if(2!=f[A+24>>2])break A;return void(f[A+24>>2]=g)}a[A+54|0]=1,f[A+24>>2]=2,f[A+36>>2]=f[A+36>>2]+1}}function br(A,e,g,r,C){var a;if(V=a=V-256|0,!(73728&C|(0|g)<=(0|r))){if(ue(a,255&e,(g=(r=g-r|0)>>>0<256)?r:256),!g)for(;kC(A,a,256),(r=r-256|0)>>>0>255;);kC(A,a,r)}V=a+256|0}function sr(A,e,g){var r=0;A:{if(A){if(r=f[A>>2]){mA(f[r+4>>2]),r=f[A>>2];break A}if(r=IA(16),f[A>>2]=r,r)break A;e=48}return e}return f[r>>2]=0,f[r+4>>2]=$r(g),A=f[A>>2],f[A+8>>2]=0,f[A+12>>2]=0,e}function tr(A){var e=0,g=0;We(A),HC[f[A+12>>2]](A),1&a[0|A]||((e=f[A+52>>2])&&(f[e+56>>2]=f[A+56>>2]),(g=f[A+56>>2])&&(f[g+52>>2]=e),f[56816]==(0|A)&&(f[56816]=g),mA(f[A+96>>2]),mA(A))}function nr(A){var e=0,g=0;e=1;A:{e:{g:{r:switch((g=Je(A))-9|0){case 1:break A;case 6:case 18:break g;case 0:break r;default:break e}return(0|ar(A))!=(0|A)}return S(A,g)>>>15&1}e=0}return e}function kr(A){var e,g,r,C=0,a=0;if(r=Lg(A)+1|0,C=f[33282],(0|(g=(e=f[33287])+r|0))>=f[33286]){if(!(C=OA(C,a=g+1e3|0)))return-1;f[33286]=a,f[33282]=C}return _A(C+e|0,A,r),f[33287]=g,e}function or(A){var e=0;return e=f[A+72>>2],f[A+72>>2]=e-1|e,8&(e=f[A>>2])?(f[A>>2]=32|e,-1):(f[A+4>>2]=0,f[A+8>>2]=0,e=f[A+44>>2],f[A+28>>2]=e,f[A+20>>2]=e,f[A+16>>2]=e+f[A+48>>2],0)}function Br(A,e,g,r){A:if(A){e:switch(e+2|0){case 0:return void(a[0|A]=g);case 1:return void(I[A>>1]=g);case 2:case 3:return void(f[A>>2]=g);case 5:break e;default:break A}f[A>>2]=g,f[A+4>>2]=r}}function cr(A,e,g,r,C,a,I,i,b){var s;V=s=V-16|0,dA(s,e,g,r,C,a,I,i,-2147483648^b),r=f[s>>2],g=f[s+4>>2],e=f[s+12>>2],f[A+8>>2]=f[s+8>>2],f[A+12>>2]=e,f[A>>2]=r,f[A+4>>2]=g,V=s+16|0}function Qr(A,e){var g=0,r=0;A:if(!(!(g=i[0|A])|(0|g)!=(0|(r=i[0|e]))))for(;;){if(r=i[e+1|0],!(g=i[A+1|0]))break A;if(e=e+1|0,A=A+1|0,(0|g)!=(0|r))break}return g-r|0}function Gr(A,e){var g=0,r=0,C=0;if(!(g=f[A>>2]))return 84399;if(f[A+4>>2]!=(0|e))for(;;){if(!(g=f[(r=A+8|0)>>2]))return 84399;if(C=A,A=r,f[C+12>>2]==(0|e))break}return g}function wr(A,e,g){var r=0,C=0,a=0;A:if(g){for(;;){if(!(!(r=f[e>>2])|!(C=f[A>>2])|(0|r)!=(0|C))){if(e=e+4|0,A=A+4|0,g=g-1|0)continue;break A}break}a=C-r|0}return a}function Er(A){var e,g=0,r=0;if((0|(e=f[36115]))>0)for(;;){if(!(!(r=f[144464+(g<<2)>>2])|f[r>>2]!=(0|A)))return i[r+10|0];if((0|e)==(0|(g=g+1|0)))break}return 0}function Dr(A){var e,g;return(A=(e=f[33175])+(g=A+7&-8)|0)>>>0<=e>>>0&&g||A>>>0>r.byteLength/65536<<16>>>0&&!(0|y(0|A))?(f[56798]=48,-1):(f[33175]=A,e)}function ur(A){var e=0;A:if(!((e=Je(A))>>>0>27)){if(!(1<>>14&1}return 0}function lr(A,e){var g=0;return a[0|A]=e>>>24,a[0|(g=(e>>>0>16777215)+A|0)]=e>>>16,a[0|(g=g+!!(16711680&e)|0)]=e>>>8,a[0|(g=g+!!(65280&e)|0)]=e,a[g+!!(255&e)|0]=0,A}function xr(A,e){A:if((0|(A=ye(A,e)))<=268437502){if(!A|268436479==(0|A)|268437247!=(0|A))break A;return}}function dr(A){var e=0;return e=f[A>>2]-1|0,(e=a[121600+(G(0-e&e,124511785)>>>27|0)|0])||(A=f[A+4>>2],e=(A=a[121600+(G(0-A&A,124511785)>>>27|0)|0])?A+32|0:0),e}function mr(A){var e=0,g=0,r=0;A:{if(!((e=Je(A))>>>0>27)){if(g=1,116672&(r=1<>>10&1}g=0}return g}function Mr(A){var e=0,g=0,r=0;A:{if(!((e=Je(A))>>>0>27)){if(g=1,34752&(r=1<>>10&1}g=0}return g}function vr(){var A,e=0;(A=f[56797])&&((e=f[A+8>>2])&&HC[f[f[e>>2]+12>>2]](e),(e=f[A+4>>2])&&HC[f[f[e>>2]+16>>2]](e),mA(A)),f[56797]=0,f[56797]=ZA()}function hr(A,e){var g=0;return A&&(g=Cr(A,0,e,0),(A|e)>>>0<65536||(g=U?-1:g)),!(A=IA(g))|!(3&i[A-4|0])||ue(A,0,g),A}function pr(A,e){var g,r;r=f[130128+(e-G(g=(0|e)/100|0,100)<<2)>>2],f[A+100>>2]=r,f[A+96>>2]=r,e+99>>>0>=199&&(f[A+100>>2]=f[130128+(g<<2)>>2])}function Yr(A){var e=0;(e=IA(1344))&&(A=_A(e,A,1344),e=216192+(f[50758]<<4)|0,f[e>>2]=11,f[e+8>>2]=A,A=f[50758]+1|0,f[50758]=(0|A)<=169?A:0)}function Hr(A,e){var g=0;A:if(g=f[A>>2])for(;;){if(e&&!Qr(e,g))break A;if(!(g=f[(A=A+8|0)>>2]))break}return f[A+4>>2]}function Nr(A){var e,g=0;return V=e=V-16|0,g=-1,Wg(A)||1==(0|HC[f[A+32>>2]](A,e+15|0,1))&&(g=i[e+15|0]),V=e+16|0,g}function Pr(A,e){var g=0,r=0;if(e){for(;r=A,(g=f[A>>2])&&(A=r+4|0,(0|e)!=(0|g)););return g?r:0}return(Rr(A)<<2)+A|0}function Fr(A,e){var g=0,r=0;if(g=B[A>>1])for(;;){if(r=r+1|0,(0|e)==(0|g))return r;if(!(g=B[(r<<1)+A>>1]))break}return 0}function yr(A,e){var g=0,r=0;e>>>0<=31?(r=f[A>>2],g=A+4|0):(e=e-32|0,g=A),g=f[g>>2],f[A>>2]=r<>2]=g<>>32-e}function zr(A,e){var g=0,r=0;g=f[A+4>>2],e>>>0<=31?r=f[A>>2]:(e=e-32|0,r=g,g=0),f[A+4>>2]=g>>>e,f[A>>2]=g<<32-e|r>>>e}function Or(A){var e=0;A=A||1;A:{for(;;){if(e=IA(A))break A;if(!(e=f[57276]))break;HC[0|e]()}F(),k()}return e}function Zr(A){return A>>>0<=131071?i[117424+(A>>>3&31|i[117424+(A>>>8|0)|0]<<5)|0]>>>(7&A)&1:A>>>0<196606}function Kr(A,e,g,r,C,a,I,i,b){f[A>>2]=e,f[A+4>>2]=g,f[A+8>>2]=r,f[A+12>>2]=65535&C|(b>>>16&32768|C>>>16&32767)<<16}function Wr(A,e,g){return g?(0|A)==(0|e)?1:!Qr(f[A+4>>2],f[e+4>>2]):f[A+4>>2]==f[e+4>>2]}function Xr(A){var e=0;-31==(0|(e=0|Z(-100,0|A,0)))&&(e=0|O(0|A)),e>>>0>=4294963201&&(f[56798]=0-e)}function Lr(){var A;A=IA(84)+80|0,f[A>>2]=125420,f[A>>2]=125380,f[A>>2]=125400,T(0|A,125512,9),k()}function Tr(A){var e,g;return e=hg(A),g=hg(A),hg(A)<<16&16711680|g<<8&65280|255&e|hg(A)<<24}function Vr(A){var e=0;return A?(e=1,A-9472>>>0<160|A-65529>>>0<7||(e=er(A)),e):0}function Jr(A,e){var g;return g=e>>31,A=(f[A+76>>2],Jg(A,e,g))}function Rr(A){var e=0,g=0;for(g=A;g=(e=g)+4|0,f[e>>2];);return e-A>>2}function Ur(A,e){return a[0|A]=e,a[A+4|0]=0,a[A+3|0]=e>>>24,a[A+2|0]=e>>>16,a[A+1|0]=e>>>8,A}function jr(A,e){var g=0;return(-1>>>(g=31&e)&A)<>>A}function Sr(A,e){var g=0;return 73==(0|A)&&(g=305,i[e+173|0])||(g=ar(A)),g}function qr(A){f[36114]!=(0|A)&&(f[36115]=0,de(A),f[36114]=A,f[36115]=f[36115]+1)}function _r(A){return A?f[34460]+A|0:(gC(85328,20,f[30450]),f[32320])}function $r(A){var e,g;return(g=IA(e=Lg(A)+1|0))?_A(g,A,e):0}function AC(){var A;f[33287]=0,(A=f[33282])&&(mA(A),f[33286]=0,f[33282]=0)}function eC(A,e,g){var r;V=r=V-16|0,f[r+12>>2]=g,QC(A,e,g),V=r+16|0}function gC(A,e,g){A=(f[g+76>>2],ig(A,e,g))}function rC(A,e){return e?A<<24|(65280&A)<<8|A>>>8&65280|A>>>24:A}function CC(A){return A=A-8212>>>0>=12?Fr(93856,A):1}function aC(A){var e=0;A&&((e=f[A+688>>2])&&mA(e),mA(A))}function IC(A){return A=(0|A)<=127?sC(87712,A):0}function fC(A){return!(A=i[0|A]?1:pg(A,A+1|0,3))}function iC(A){(A=27!=(0|(A=0|d(0|A)))?A:0)&&(f[56798]=A)}function bC(A,e,g){return A|=0,e|=0,0|HC[0|(g|=0)](A,e)}function sC(A,e){return A=_e(A,e),i[0|A]==(255&e)?A:0}function tC(A,e){return be(A,e,2147483647),U=R,J}function nC(A){return A?31-D(A-1^A)|0:32}function kC(A,e,g){32&i[0|A]||ig(e,g,A)}function oC(A,e,g){a[(Pe(A,e,g)+g|0)-1|0]=0}function BC(A){return vg(A,188776,189328)}function cC(A,e){e|=0,f[(A|=0)+8>>2]=e}function QC(A,e,g){return Me(A,e,g,0,0)}function GC(A){return f[(A|=0)+12>>2]}function wC(A){return f[(A|=0)+16>>2]}function EC(A){return f[(A|=0)+20>>2]}function DC(A){return f[(A|=0)+8>>2]}function uC(A){return f[(A|=0)+4>>2]}function lC(A){return f[(A|=0)>>2]}function xC(A,e,g){return be(A,e,g)}function dC(A){(A|=0)&&mA(A)}function mC(A,e){rg(Lg(A)+A|0,e)}function MC(A){return 0|(A|=0)}function vC(A){return 0}function hC(A){mA(A|=0)}function pC(A){}C(e=i,1024,"ZGVmYXVsdAB3YgAAAAAAAEEAAAAAAAAAYQAAAAAAAABCAAAAAAAAAGIAAAAAAAAAQwAAAAAAAABjAAAAAAAAAEQAAAAAAAAAZAAAAAAAAABFAAAAAAAAAGUAAAAAAAAARgAAAAAAAABmAAAAAAAAAEcAAAAAAAAAZwAAAAAAAABIAAAAAAAAAGgAAAAAAAAASQAAAAAAAABpAAAAAAAAAEoAAAAAAAAAagAAAAAAAABLAAAAAAAAAGsAAAAAAAAATAAAAAAAAABsAAAAAAAAAE0AAAAAAAAAbQAAAAAAAABOAAAAAAAAAG4AAAAAAAAATwAAAAAAAABvAAAAAAAAAFAAAAAAAAAAcAAAAAAAAABRAAAAAAAAAHEAAAAAAAAAUgAAAAAAAAByAAAAAAAAAFMAAAAAAAAAcwAAAAAAAABUAAAAAAAAAHQAAAAAAAAAVQAAAAAAAAB1AAAAAAAAAFYAAAAAAAAAdgAAAAAAAABXAAAAAAAAAHcAAAAAAAAAWAAAAAAAAAB4AAAAAAAAAFkAAAAAAAAAeQAAAAAAAABaAAAAAAAAAHoAAAAAAAAAYQAAAEEAAAAAAAAAQQAAAGIAAABCAAAAAAAAAEIAAABjAAAAQwAAAAAAAABDAAAAZAAAAEQAAAAAAAAARAAAAGUAAABFAAAAAAAAAEUAAABmAAAARgAAAAAAAABGAAAAZwAAAEcAAAAAAAAARwAAAGgAAABIAAAAAAAAAEgAAABpAAAASQAAAAAAAABJAAAAagAAAEoAAAAAAAAASgAAAGsAAABLAAAAAAAAAEsAAABsAAAATAAAAAAAAABMAAAAbQAAAE0AAAAAAAAATQAAAG4AAABOAAAAAAAAAE4AAABvAAAATwAAAAAAAABPAAAAcAAAAFAAAAAAAAAAUAAAAHEAAABRAAAAAAAAAFEAAAByAAAAUgAAAAAAAABSAAAAcwAAAFMAAAAAAAAAUwAAAHQAAABUAAAAAAAAAFQAAAB1AAAAVQAAAAAAAABVAAAAdgAAAFYAAAAAAAAAVgAAAHcAAABXAAAAAAAAAFcAAAB4AAAAWAAAAAAAAABYAAAAeQAAAFkAAAAAAAAAWQAAAHoAAABaAAAAAAAAAFoAAAC1AAAAnAMAAAAAAACcAwAAwAAAAAAAAADgAAAAAAAAAMEAAAAAAAAA4QAAAAAAAADCAAAAAAAAAOIAAAAAAAAAwwAAAAAAAADjAAAAAAAAAMQAAAAAAAAA5AAAAAAAAADFAAAAAAAAAOUAAAAAAAAAxgAAAAAAAADmAAAAAAAAAMcAAAAAAAAA5wAAAAAAAADIAAAAAAAAAOgAAAAAAAAAyQAAAAAAAADpAAAAAAAAAMoAAAAAAAAA6gAAAAAAAADLAAAAAAAAAOsAAAAAAAAAzAAAAAAAAADsAAAAAAAAAM0AAAAAAAAA7QAAAAAAAADOAAAAAAAAAO4AAAAAAAAAzwAAAAAAAADvAAAAAAAAANAAAAAAAAAA8AAAAAAAAADRAAAAAAAAAPEAAAAAAAAA0gAAAAAAAADyAAAAAAAAANMAAAAAAAAA8wAAAAAAAADUAAAAAAAAAPQAAAAAAAAA1QAAAAAAAAD1AAAAAAAAANYAAAAAAAAA9gAAAAAAAADYAAAAAAAAAPgAAAAAAAAA2QAAAAAAAAD5AAAAAAAAANoAAAAAAAAA+gAAAAAAAADbAAAAAAAAAPsAAAAAAAAA3AAAAAAAAAD8AAAAAAAAAN0AAAAAAAAA/QAAAAAAAADeAAAAAAAAAP4AAAAAAAAA4AAAAMAAAAAAAAAAwAAAAOEAAADBAAAAAAAAAMEAAADiAAAAwgAAAAAAAADCAAAA4wAAAMMAAAAAAAAAwwAAAOQAAADEAAAAAAAAAMQAAADlAAAAxQAAAAAAAADFAAAA5gAAAMYAAAAAAAAAxgAAAOcAAADHAAAAAAAAAMcAAADoAAAAyAAAAAAAAADIAAAA6QAAAMkAAAAAAAAAyQAAAOoAAADKAAAAAAAAAMoAAADrAAAAywAAAAAAAADLAAAA7AAAAMwAAAAAAAAAzAAAAO0AAADNAAAAAAAAAM0AAADuAAAAzgAAAAAAAADOAAAA7wAAAM8AAAAAAAAAzwAAAPAAAADQAAAAAAAAANAAAADxAAAA0QAAAAAAAADRAAAA8gAAANIAAAAAAAAA0gAAAPMAAADTAAAAAAAAANMAAAD0AAAA1AAAAAAAAADUAAAA9QAAANUAAAAAAAAA1QAAAPYAAADWAAAAAAAAANYAAAD4AAAA2AAAAAAAAADYAAAA+QAAANkAAAAAAAAA2QAAAPoAAADaAAAAAAAAANoAAAD7AAAA2wAAAAAAAADbAAAA/AAAANwAAAAAAAAA3AAAAP0AAADdAAAAAAAAAN0AAAD+AAAA3gAAAAAAAADeAAAA/wAAAHgBAAAAAAAAeAEAAAABAAAAAAAAAQEAAAAAAAABAQAAAAEAAAAAAAAAAQAAAgEAAAAAAAADAQAAAAAAAAMBAAACAQAAAAAAAAIBAAAEAQAAAAAAAAUBAAAAAAAABQEAAAQBAAAAAAAABAEAAAYBAAAAAAAABwEAAAAAAAAHAQAABgEAAAAAAAAGAQAACAEAAAAAAAAJAQAAAAAAAAkBAAAIAQAAAAAAAAgBAAAKAQAAAAAAAAsBAAAAAAAACwEAAAoBAAAAAAAACgEAAAwBAAAAAAAADQEAAAAAAAANAQAADAEAAAAAAAAMAQAADgEAAAAAAAAPAQAAAAAAAA8BAAAOAQAAAAAAAA4BAAAQAQAAAAAAABEBAAAAAAAAEQEAABABAAAAAAAAEAEAABIBAAAAAAAAEwEAAAAAAAATAQAAEgEAAAAAAAASAQAAFAEAAAAAAAAVAQAAAAAAABUBAAAUAQAAAAAAABQBAAAWAQAAAAAAABcBAAAAAAAAFwEAABYBAAAAAAAAFgEAABgBAAAAAAAAGQEAAAAAAAAZAQAAGAEAAAAAAAAYAQAAGgEAAAAAAAAbAQAAAAAAABsBAAAaAQAAAAAAABoBAAAcAQAAAAAAAB0BAAAAAAAAHQEAABwBAAAAAAAAHAEAAB4BAAAAAAAAHwEAAAAAAAAfAQAAHgEAAAAAAAAeAQAAIAEAAAAAAAAhAQAAAAAAACEBAAAgAQAAAAAAACABAAAiAQAAAAAAACMBAAAAAAAAIwEAACIBAAAAAAAAIgEAACQBAAAAAAAAJQEAAAAAAAAlAQAAJAEAAAAAAAAkAQAAJgEAAAAAAAAnAQAAAAAAACcBAAAmAQAAAAAAACYBAAAoAQAAAAAAACkBAAAAAAAAKQEAACgBAAAAAAAAKAEAACoBAAAAAAAAKwEAAAAAAAArAQAAKgEAAAAAAAAqAQAALAEAAAAAAAAtAQAAAAAAAC0BAAAsAQAAAAAAACwBAAAuAQAAAAAAAC8BAAAAAAAALwEAAC4BAAAAAAAALgEAADABAAAAAAAAaQAAAAAAAAAxAQAASQAAAAAAAABJAAAAMgEAAAAAAAAzAQAAAAAAADMBAAAyAQAAAAAAADIBAAA0AQAAAAAAADUBAAAAAAAANQEAADQBAAAAAAAANAEAADYBAAAAAAAANwEAAAAAAAA3AQAANgEAAAAAAAA2AQAAOQEAAAAAAAA6AQAAAAAAADoBAAA5AQAAAAAAADkBAAA7AQAAAAAAADwBAAAAAAAAPAEAADsBAAAAAAAAOwEAAD0BAAAAAAAAPgEAAAAAAAA+AQAAPQEAAAAAAAA9AQAAPwEAAAAAAABAAQAAAAAAAEABAAA/AQAAAAAAAD8BAABBAQAAAAAAAEIBAAAAAAAAQgEAAEEBAAAAAAAAQQEAAEMBAAAAAAAARAEAAAAAAABEAQAAQwEAAAAAAABDAQAARQEAAAAAAABGAQAAAAAAAEYBAABFAQAAAAAAAEUBAABHAQAAAAAAAEgBAAAAAAAASAEAAEcBAAAAAAAARwEAAEoBAAAAAAAASwEAAAAAAABLAQAASgEAAAAAAABKAQAATAEAAAAAAABNAQAAAAAAAE0BAABMAQAAAAAAAEwBAABOAQAAAAAAAE8BAAAAAAAATwEAAE4BAAAAAAAATgEAAFABAAAAAAAAUQEAAAAAAABRAQAAUAEAAAAAAABQAQAAUgEAAAAAAABTAQAAAAAAAFMBAABSAQAAAAAAAFIBAABUAQAAAAAAAFUBAAAAAAAAVQEAAFQBAAAAAAAAVAEAAFYBAAAAAAAAVwEAAAAAAABXAQAAVgEAAAAAAABWAQAAWAEAAAAAAABZAQAAAAAAAFkBAABYAQAAAAAAAFgBAABaAQAAAAAAAFsBAAAAAAAAWwEAAFoBAAAAAAAAWgEAAFwBAAAAAAAAXQEAAAAAAABdAQAAXAEAAAAAAABcAQAAXgEAAAAAAABfAQAAAAAAAF8BAABeAQAAAAAAAF4BAABgAQAAAAAAAGEBAAAAAAAAYQEAAGABAAAAAAAAYAEAAGIBAAAAAAAAYwEAAAAAAABjAQAAYgEAAAAAAABiAQAAZAEAAAAAAABlAQAAAAAAAGUBAABkAQAAAAAAAGQBAABmAQAAAAAAAGcBAAAAAAAAZwEAAGYBAAAAAAAAZgEAAGgBAAAAAAAAaQEAAAAAAABpAQAAaAEAAAAAAABoAQAAagEAAAAAAABrAQAAAAAAAGsBAABqAQAAAAAAAGoBAABsAQAAAAAAAG0BAAAAAAAAbQEAAGwBAAAAAAAAbAEAAG4BAAAAAAAAbwEAAAAAAABvAQAAbgEAAAAAAABuAQAAcAEAAAAAAABxAQAAAAAAAHEBAABwAQAAAAAAAHABAAByAQAAAAAAAHMBAAAAAAAAcwEAAHIBAAAAAAAAcgEAAHQBAAAAAAAAdQEAAAAAAAB1AQAAdAEAAAAAAAB0AQAAdgEAAAAAAAB3AQAAAAAAAHcBAAB2AQAAAAAAAHYBAAB4AQAAAAAAAP8AAAAAAAAAeQEAAAAAAAB6AQAAAAAAAHoBAAB5AQAAAAAAAHkBAAB7AQAAAAAAAHwBAAAAAAAAfAEAAHsBAAAAAAAAewEAAH0BAAAAAAAAfgEAAAAAAAB+AQAAfQEAAAAAAAB9AQAAfwEAAFMAAAAAAAAAUwAAAIABAABDAgAAAAAAAEMCAACBAQAAAAAAAFMCAAAAAAAAggEAAAAAAACDAQAAAAAAAIMBAACCAQAAAAAAAIIBAACEAQAAAAAAAIUBAAAAAAAAhQEAAIQBAAAAAAAAhAEAAIYBAAAAAAAAVAIAAAAAAACHAQAAAAAAAIgBAAAAAAAAiAEAAIcBAAAAAAAAhwEAAIkBAAAAAAAAVgIAAAAAAACKAQAAAAAAAFcCAAAAAAAAiwEAAAAAAACMAQAAAAAAAIwBAACLAQAAAAAAAIsBAACOAQAAAAAAAN0BAAAAAAAAjwEAAAAAAABZAgAAAAAAAJABAAAAAAAAWwIAAAAAAACRAQAAAAAAAJIBAAAAAAAAkgEAAJEBAAAAAAAAkQEAAJMBAAAAAAAAYAIAAAAAAACUAQAAAAAAAGMCAAAAAAAAlQEAAPYBAAAAAAAA9gEAAJYBAAAAAAAAaQIAAAAAAACXAQAAAAAAAGgCAAAAAAAAmAEAAAAAAACZAQAAAAAAAJkBAACYAQAAAAAAAJgBAACaAQAAPQIAAAAAAAA9AgAAnAEAAAAAAABvAgAAAAAAAJ0BAAAAAAAAcgIAAAAAAACeAQAAIAIAAAAAAAAgAgAAnwEAAAAAAAB1AgAAAAAAAKABAAAAAAAAoQEAAAAAAAChAQAAoAEAAAAAAACgAQAAogEAAAAAAACjAQAAAAAAAKMBAACiAQAAAAAAAKIBAACkAQAAAAAAAKUBAAAAAAAApQEAAKQBAAAAAAAApAEAAKYBAAAAAAAAgAIAAAAAAACnAQAAAAAAAKgBAAAAAAAAqAEAAKcBAAAAAAAApwEAAKkBAAAAAAAAgwIAAAAAAACsAQAAAAAAAK0BAAAAAAAArQEAAKwBAAAAAAAArAEAAK4BAAAAAAAAiAIAAAAAAACvAQAAAAAAALABAAAAAAAAsAEAAK8BAAAAAAAArwEAALEBAAAAAAAAigIAAAAAAACyAQAAAAAAAIsCAAAAAAAAswEAAAAAAAC0AQAAAAAAALQBAACzAQAAAAAAALMBAAC1AQAAAAAAALYBAAAAAAAAtgEAALUBAAAAAAAAtQEAALcBAAAAAAAAkgIAAAAAAAC4AQAAAAAAALkBAAAAAAAAuQEAALgBAAAAAAAAuAEAALwBAAAAAAAAvQEAAAAAAAC9AQAAvAEAAAAAAAC8AQAAvwEAAPcBAAAAAAAA9wEAAMQBAAAAAAAAxgEAAMUBAADFAQAAxAEAAMYBAADFAQAAxgEAAMQBAAAAAAAAxQEAAMcBAAAAAAAAyQEAAMgBAADIAQAAxwEAAMkBAADIAQAAyQEAAMcBAAAAAAAAyAEAAMoBAAAAAAAAzAEAAMsBAADLAQAAygEAAMwBAADLAQAAzAEAAMoBAAAAAAAAywEAAM0BAAAAAAAAzgEAAAAAAADOAQAAzQEAAAAAAADNAQAAzwEAAAAAAADQAQAAAAAAANABAADPAQAAAAAAAM8BAADRAQAAAAAAANIBAAAAAAAA0gEAANEBAAAAAAAA0QEAANMBAAAAAAAA1AEAAAAAAADUAQAA0wEAAAAAAADTAQAA1QEAAAAAAADWAQAAAAAAANYBAADVAQAAAAAAANUBAADXAQAAAAAAANgBAAAAAAAA2AEAANcBAAAAAAAA1wEAANkBAAAAAAAA2gEAAAAAAADaAQAA2QEAAAAAAADZAQAA2wEAAAAAAADcAQAAAAAAANwBAADbAQAAAAAAANsBAADdAQAAjgEAAAAAAACOAQAA3gEAAAAAAADfAQAAAAAAAN8BAADeAQAAAAAAAN4BAADgAQAAAAAAAOEBAAAAAAAA4QEAAOABAAAAAAAA4AEAAOIBAAAAAAAA4wEAAAAAAADjAQAA4gEAAAAAAADiAQAA5AEAAAAAAADlAQAAAAAAAOUBAADkAQAAAAAAAOQBAADmAQAAAAAAAOcBAAAAAAAA5wEAAOYBAAAAAAAA5gEAAOgBAAAAAAAA6QEAAAAAAADpAQAA6AEAAAAAAADoAQAA6gEAAAAAAADrAQAAAAAAAOsBAADqAQAAAAAAAOoBAADsAQAAAAAAAO0BAAAAAAAA7QEAAOwBAAAAAAAA7AEAAO4BAAAAAAAA7wEAAAAAAADvAQAA7gEAAAAAAADuAQAA8QEAAAAAAADzAQAA8gEAAPIBAADxAQAA8wEAAPIBAADzAQAA8QEAAAAAAADyAQAA9AEAAAAAAAD1AQAAAAAAAPUBAAD0AQAAAAAAAPQBAAD2AQAAAAAAAJUBAAAAAAAA9wEAAAAAAAC/AQAAAAAAAPgBAAAAAAAA+QEAAAAAAAD5AQAA+AEAAAAAAAD4AQAA+gEAAAAAAAD7AQAAAAAAAPsBAAD6AQAAAAAAAPoBAAD8AQAAAAAAAP0BAAAAAAAA/QEAAPwBAAAAAAAA/AEAAP4BAAAAAAAA/wEAAAAAAAD/AQAA/gEAAAAAAAD+AQAAAAIAAAAAAAABAgAAAAAAAAECAAAAAgAAAAAAAAACAAACAgAAAAAAAAMCAAAAAAAAAwIAAAICAAAAAAAAAgIAAAQCAAAAAAAABQIAAAAAAAAFAgAABAIAAAAAAAAEAgAABgIAAAAAAAAHAgAAAAAAAAcCAAAGAgAAAAAAAAYCAAAIAgAAAAAAAAkCAAAAAAAACQIAAAgCAAAAAAAACAIAAAoCAAAAAAAACwIAAAAAAAALAgAACgIAAAAAAAAKAgAADAIAAAAAAAANAgAAAAAAAA0CAAAMAgAAAAAAAAwCAAAOAgAAAAAAAA8CAAAAAAAADwIAAA4CAAAAAAAADgIAABACAAAAAAAAEQIAAAAAAAARAgAAEAIAAAAAAAAQAgAAEgIAAAAAAAATAgAAAAAAABMCAAASAgAAAAAAABICAAAUAgAAAAAAABUCAAAAAAAAFQIAABQCAAAAAAAAFAIAABYCAAAAAAAAFwIAAAAAAAAXAgAAFgIAAAAAAAAWAgAAGAIAAAAAAAAZAgAAAAAAABkCAAAYAgAAAAAAABgCAAAaAgAAAAAAABsCAAAAAAAAGwIAABoCAAAAAAAAGgIAABwCAAAAAAAAHQIAAAAAAAAdAgAAHAIAAAAAAAAcAgAAHgIAAAAAAAAfAgAAAAAAAB8CAAAeAgAAAAAAAB4CAAAgAgAAAAAAAJ4BAAAAAAAAIgIAAAAAAAAjAgAAAAAAACMCAAAiAgAAAAAAACICAAAkAgAAAAAAACUCAAAAAAAAJQIAACQCAAAAAAAAJAIAACYCAAAAAAAAJwIAAAAAAAAnAgAAJgIAAAAAAAAmAgAAKAIAAAAAAAApAgAAAAAAACkCAAAoAgAAAAAAACgCAAAqAgAAAAAAACsCAAAAAAAAKwIAACoCAAAAAAAAKgIAACwCAAAAAAAALQIAAAAAAAAtAgAALAIAAAAAAAAsAgAALgIAAAAAAAAvAgAAAAAAAC8CAAAuAgAAAAAAAC4CAAAwAgAAAAAAADECAAAAAAAAMQIAADACAAAAAAAAMAIAADICAAAAAAAAMwIAAAAAAAAzAgAAMgIAAAAAAAAyAgAAOgIAAAAAAABlLAAAAAAAADsCAAAAAAAAPAIAAAAAAAA8AgAAOwIAAAAAAAA7AgAAPQIAAAAAAACaAQAAAAAAAD4CAAAAAAAAZiwAAAAAAAA/AgAAfiwAAAAAAAB+LAAAQAIAAH8sAAAAAAAAfywAAEECAAAAAAAAQgIAAAAAAABCAgAAQQIAAAAAAABBAgAAQwIAAAAAAACAAQAAAAAAAEQCAAAAAAAAiQIAAAAAAABFAgAAAAAAAIwCAAAAAAAARgIAAAAAAABHAgAAAAAAAEcCAABGAgAAAAAAAEYCAABIAgAAAAAAAEkCAAAAAAAASQIAAEgCAAAAAAAASAIAAEoCAAAAAAAASwIAAAAAAABLAgAASgIAAAAAAABKAgAATAIAAAAAAABNAgAAAAAAAE0CAABMAgAAAAAAAEwCAABOAgAAAAAAAE8CAAAAAAAATwIAAE4CAAAAAAAATgIAAFACAABvLAAAAAAAAG8sAABRAgAAbSwAAAAAAABtLAAAUgIAAHAsAAAAAAAAcCwAAFMCAACBAQAAAAAAAIEBAABUAgAAhgEAAAAAAACGAQAAVgIAAIkBAAAAAAAAiQEAAFcCAACKAQAAAAAAAIoBAABZAgAAjwEAAAAAAACPAQAAWwIAAJABAAAAAAAAkAEAAFwCAACrpwAAAAAAAKunAABgAgAAkwEAAAAAAACTAQAAYQIAAKynAAAAAAAArKcAAGMCAACUAQAAAAAAAJQBAABlAgAAjacAAAAAAACNpwAAZgIAAKqnAAAAAAAAqqcAAGgCAACXAQAAAAAAAJcBAABpAgAAlgEAAAAAAACWAQAAagIAAK6nAAAAAAAArqcAAGsCAABiLAAAAAAAAGIsAABsAgAAracAAAAAAACtpwAAbwIAAJwBAAAAAAAAnAEAAHECAABuLAAAAAAAAG4sAAByAgAAnQEAAAAAAACdAQAAdQIAAJ8BAAAAAAAAnwEAAH0CAABkLAAAAAAAAGQsAACAAgAApgEAAAAAAACmAQAAgwIAAKkBAAAAAAAAqQEAAIcCAACxpwAAAAAAALGnAACIAgAArgEAAAAAAACuAQAAiQIAAEQCAAAAAAAARAIAAIoCAACxAQAAAAAAALEBAACLAgAAsgEAAAAAAACyAQAAjAIAAEUCAAAAAAAARQIAAJICAAC3AQAAAAAAALcBAACdAgAAsqcAAAAAAACypwAAngIAALCnAAAAAAAAsKcAAEUDAACZAwAAAAAAAJkDAABwAwAAAAAAAHEDAAAAAAAAcQMAAHADAAAAAAAAcAMAAHIDAAAAAAAAcwMAAAAAAABzAwAAcgMAAAAAAAByAwAAdgMAAAAAAAB3AwAAAAAAAHcDAAB2AwAAAAAAAHYDAAB7AwAA/QMAAAAAAAD9AwAAfAMAAP4DAAAAAAAA/gMAAH0DAAD/AwAAAAAAAP8DAAB/AwAAAAAAAPMDAAAAAAAAhgMAAAAAAACsAwAAAAAAAIgDAAAAAAAArQMAAAAAAACJAwAAAAAAAK4DAAAAAAAAigMAAAAAAACvAwAAAAAAAIwDAAAAAAAAzAMAAAAAAACOAwAAAAAAAM0DAAAAAAAAjwMAAAAAAADOAwAAAAAAAJEDAAAAAAAAsQMAAAAAAACSAwAAAAAAALIDAAAAAAAAkwMAAAAAAACzAwAAAAAAAJQDAAAAAAAAtAMAAAAAAACVAwAAAAAAALUDAAAAAAAAlgMAAAAAAAC2AwAAAAAAAJcDAAAAAAAAtwMAAAAAAACYAwAAAAAAALgDAAAAAAAAmQMAAAAAAAC5AwAAAAAAAJoDAAAAAAAAugMAAAAAAACbAwAAAAAAALsDAAAAAAAAnAMAAAAAAAC8AwAAAAAAAJ0DAAAAAAAAvQMAAAAAAACeAwAAAAAAAL4DAAAAAAAAnwMAAAAAAAC/AwAAAAAAAKADAAAAAAAAwAMAAAAAAAChAwAAAAAAAMEDAAAAAAAAowMAAAAAAADDAwAAAAAAAKQDAAAAAAAAxAMAAAAAAAClAwAAAAAAAMUDAAAAAAAApgMAAAAAAADGAwAAAAAAAKcDAAAAAAAAxwMAAAAAAACoAwAAAAAAAMgDAAAAAAAAqQMAAAAAAADJAwAAAAAAAKoDAAAAAAAAygMAAAAAAACrAwAAAAAAAMsDAAAAAAAArAMAAIYDAAAAAAAAhgMAAK0DAACIAwAAAAAAAIgDAACuAwAAiQMAAAAAAACJAwAArwMAAIoDAAAAAAAAigMAALEDAACRAwAAAAAAAJEDAACyAwAAkgMAAAAAAACSAwAAswMAAJMDAAAAAAAAkwMAALQDAACUAwAAAAAAAJQDAAC1AwAAlQMAAAAAAACVAwAAtgMAAJYDAAAAAAAAlgMAALcDAACXAwAAAAAAAJcDAAC4AwAAmAMAAAAAAACYAwAAuQMAAJkDAAAAAAAAmQMAALoDAACaAwAAAAAAAJoDAAC7AwAAmwMAAAAAAACbAwAAvAMAAJwDAAAAAAAAnAMAAL0DAACdAwAAAAAAAJ0DAAC+AwAAngMAAAAAAACeAwAAvwMAAJ8DAAAAAAAAnwMAAMADAACgAwAAAAAAAKADAADBAwAAoQMAAAAAAAChAwAAwgMAAKMDAAAAAAAAowMAAMMDAACjAwAAAAAAAKMDAADEAwAApAMAAAAAAACkAwAAxQMAAKUDAAAAAAAApQMAAMYDAACmAwAAAAAAAKYDAADHAwAApwMAAAAAAACnAwAAyAMAAKgDAAAAAAAAqAMAAMkDAACpAwAAAAAAAKkDAADKAwAAqgMAAAAAAACqAwAAywMAAKsDAAAAAAAAqwMAAMwDAACMAwAAAAAAAIwDAADNAwAAjgMAAAAAAACOAwAAzgMAAI8DAAAAAAAAjwMAAM8DAAAAAAAA1wMAAAAAAADQAwAAkgMAAAAAAACSAwAA0QMAAJgDAAAAAAAAmAMAANUDAACmAwAAAAAAAKYDAADWAwAAoAMAAAAAAACgAwAA1wMAAM8DAAAAAAAAzwMAANgDAAAAAAAA2QMAAAAAAADZAwAA2AMAAAAAAADYAwAA2gMAAAAAAADbAwAAAAAAANsDAADaAwAAAAAAANoDAADcAwAAAAAAAN0DAAAAAAAA3QMAANwDAAAAAAAA3AMAAN4DAAAAAAAA3wMAAAAAAADfAwAA3gMAAAAAAADeAwAA4AMAAAAAAADhAwAAAAAAAOEDAADgAwAAAAAAAOADAADiAwAAAAAAAOMDAAAAAAAA4wMAAOIDAAAAAAAA4gMAAOQDAAAAAAAA5QMAAAAAAADlAwAA5AMAAAAAAADkAwAA5gMAAAAAAADnAwAAAAAAAOcDAADmAwAAAAAAAOYDAADoAwAAAAAAAOkDAAAAAAAA6QMAAOgDAAAAAAAA6AMAAOoDAAAAAAAA6wMAAAAAAADrAwAA6gMAAAAAAADqAwAA7AMAAAAAAADtAwAAAAAAAO0DAADsAwAAAAAAAOwDAADuAwAAAAAAAO8DAAAAAAAA7wMAAO4DAAAAAAAA7gMAAPADAACaAwAAAAAAAJoDAADxAwAAoQMAAAAAAAChAwAA8gMAAPkDAAAAAAAA+QMAAPMDAAB/AwAAAAAAAH8DAAD0AwAAAAAAALgDAAAAAAAA9QMAAJUDAAAAAAAAlQMAAPcDAAAAAAAA+AMAAAAAAAD4AwAA9wMAAAAAAAD3AwAA+QMAAAAAAADyAwAAAAAAAPoDAAAAAAAA+wMAAAAAAAD7AwAA+gMAAAAAAAD6AwAA/QMAAAAAAAB7AwAAAAAAAP4DAAAAAAAAfAMAAAAAAAD/AwAAAAAAAH0DAAAAAAAAAAQAAAAAAABQBAAAAAAAAAEEAAAAAAAAUQQAAAAAAAACBAAAAAAAAFIEAAAAAAAAAwQAAAAAAABTBAAAAAAAAAQEAAAAAAAAVAQAAAAAAAAFBAAAAAAAAFUEAAAAAAAABgQAAAAAAABWBAAAAAAAAAcEAAAAAAAAVwQAAAAAAAAIBAAAAAAAAFgEAAAAAAAACQQAAAAAAABZBAAAAAAAAAoEAAAAAAAAWgQAAAAAAAALBAAAAAAAAFsEAAAAAAAADAQAAAAAAABcBAAAAAAAAA0EAAAAAAAAXQQAAAAAAAAOBAAAAAAAAF4EAAAAAAAADwQAAAAAAABfBAAAAAAAABAEAAAAAAAAMAQAAAAAAAARBAAAAAAAADEEAAAAAAAAEgQAAAAAAAAyBAAAAAAAABMEAAAAAAAAMwQAAAAAAAAUBAAAAAAAADQEAAAAAAAAFQQAAAAAAAA1BAAAAAAAABYEAAAAAAAANgQAAAAAAAAXBAAAAAAAADcEAAAAAAAAGAQAAAAAAAA4BAAAAAAAABkEAAAAAAAAOQQAAAAAAAAaBAAAAAAAADoEAAAAAAAAGwQAAAAAAAA7BAAAAAAAABwEAAAAAAAAPAQAAAAAAAAdBAAAAAAAAD0EAAAAAAAAHgQAAAAAAAA+BAAAAAAAAB8EAAAAAAAAPwQAAAAAAAAgBAAAAAAAAEAEAAAAAAAAIQQAAAAAAABBBAAAAAAAACIEAAAAAAAAQgQAAAAAAAAjBAAAAAAAAEMEAAAAAAAAJAQAAAAAAABEBAAAAAAAACUEAAAAAAAARQQAAAAAAAAmBAAAAAAAAEYEAAAAAAAAJwQAAAAAAABHBAAAAAAAACgEAAAAAAAASAQAAAAAAAApBAAAAAAAAEkEAAAAAAAAKgQAAAAAAABKBAAAAAAAACsEAAAAAAAASwQAAAAAAAAsBAAAAAAAAEwEAAAAAAAALQQAAAAAAABNBAAAAAAAAC4EAAAAAAAATgQAAAAAAAAvBAAAAAAAAE8EAAAAAAAAMAQAABAEAAAAAAAAEAQAADEEAAARBAAAAAAAABEEAAAyBAAAEgQAAAAAAAASBAAAMwQAABMEAAAAAAAAEwQAADQEAAAUBAAAAAAAABQEAAA1BAAAFQQAAAAAAAAVBAAANgQAABYEAAAAAAAAFgQAADcEAAAXBAAAAAAAABcEAAA4BAAAGAQAAAAAAAAYBAAAOQQAABkEAAAAAAAAGQQAADoEAAAaBAAAAAAAABoEAAA7BAAAGwQAAAAAAAAbBAAAPAQAABwEAAAAAAAAHAQAAD0EAAAdBAAAAAAAAB0EAAA+BAAAHgQAAAAAAAAeBAAAPwQAAB8EAAAAAAAAHwQAAEAEAAAgBAAAAAAAACAEAABBBAAAIQQAAAAAAAAhBAAAQgQAACIEAAAAAAAAIgQAAEMEAAAjBAAAAAAAACMEAABEBAAAJAQAAAAAAAAkBAAARQQAACUEAAAAAAAAJQQAAEYEAAAmBAAAAAAAACYEAABHBAAAJwQAAAAAAAAnBAAASAQAACgEAAAAAAAAKAQAAEkEAAApBAAAAAAAACkEAABKBAAAKgQAAAAAAAAqBAAASwQAACsEAAAAAAAAKwQAAEwEAAAsBAAAAAAAACwEAABNBAAALQQAAAAAAAAtBAAATgQAAC4EAAAAAAAALgQAAE8EAAAvBAAAAAAAAC8EAABQBAAAAAQAAAAAAAAABAAAUQQAAAEEAAAAAAAAAQQAAFIEAAACBAAAAAAAAAIEAABTBAAAAwQAAAAAAAADBAAAVAQAAAQEAAAAAAAABAQAAFUEAAAFBAAAAAAAAAUEAABWBAAABgQAAAAAAAAGBAAAVwQAAAcEAAAAAAAABwQAAFgEAAAIBAAAAAAAAAgEAABZBAAACQQAAAAAAAAJBAAAWgQAAAoEAAAAAAAACgQAAFsEAAALBAAAAAAAAAsEAABcBAAADAQAAAAAAAAMBAAAXQQAAA0EAAAAAAAADQQAAF4EAAAOBAAAAAAAAA4EAABfBAAADwQAAAAAAAAPBAAAYAQAAAAAAABhBAAAAAAAAGEEAABgBAAAAAAAAGAEAABiBAAAAAAAAGMEAAAAAAAAYwQAAGIEAAAAAAAAYgQAAGQEAAAAAAAAZQQAAAAAAABlBAAAZAQAAAAAAABkBAAAZgQAAAAAAABnBAAAAAAAAGcEAABmBAAAAAAAAGYEAABoBAAAAAAAAGkEAAAAAAAAaQQAAGgEAAAAAAAAaAQAAGoEAAAAAAAAawQAAAAAAABrBAAAagQAAAAAAABqBAAAbAQAAAAAAABtBAAAAAAAAG0EAABsBAAAAAAAAGwEAABuBAAAAAAAAG8EAAAAAAAAbwQAAG4EAAAAAAAAbgQAAHAEAAAAAAAAcQQAAAAAAABxBAAAcAQAAAAAAABwBAAAcgQAAAAAAABzBAAAAAAAAHMEAAByBAAAAAAAAHIEAAB0BAAAAAAAAHUEAAAAAAAAdQQAAHQEAAAAAAAAdAQAAHYEAAAAAAAAdwQAAAAAAAB3BAAAdgQAAAAAAAB2BAAAeAQAAAAAAAB5BAAAAAAAAHkEAAB4BAAAAAAAAHgEAAB6BAAAAAAAAHsEAAAAAAAAewQAAHoEAAAAAAAAegQAAHwEAAAAAAAAfQQAAAAAAAB9BAAAfAQAAAAAAAB8BAAAfgQAAAAAAAB/BAAAAAAAAH8EAAB+BAAAAAAAAH4EAACABAAAAAAAAIEEAAAAAAAAgQQAAIAEAAAAAAAAgAQAAIoEAAAAAAAAiwQAAAAAAACLBAAAigQAAAAAAACKBAAAjAQAAAAAAACNBAAAAAAAAI0EAACMBAAAAAAAAIwEAACOBAAAAAAAAI8EAAAAAAAAjwQAAI4EAAAAAAAAjgQAAJAEAAAAAAAAkQQAAAAAAACRBAAAkAQAAAAAAACQBAAAkgQAAAAAAACTBAAAAAAAAJMEAACSBAAAAAAAAJIEAACUBAAAAAAAAJUEAAAAAAAAlQQAAJQEAAAAAAAAlAQAAJYEAAAAAAAAlwQAAAAAAACXBAAAlgQAAAAAAACWBAAAmAQAAAAAAACZBAAAAAAAAJkEAACYBAAAAAAAAJgEAACaBAAAAAAAAJsEAAAAAAAAmwQAAJoEAAAAAAAAmgQAAJwEAAAAAAAAnQQAAAAAAACdBAAAnAQAAAAAAACcBAAAngQAAAAAAACfBAAAAAAAAJ8EAACeBAAAAAAAAJ4EAACgBAAAAAAAAKEEAAAAAAAAoQQAAKAEAAAAAAAAoAQAAKIEAAAAAAAAowQAAAAAAACjBAAAogQAAAAAAACiBAAApAQAAAAAAAClBAAAAAAAAKUEAACkBAAAAAAAAKQEAACmBAAAAAAAAKcEAAAAAAAApwQAAKYEAAAAAAAApgQAAKgEAAAAAAAAqQQAAAAAAACpBAAAqAQAAAAAAACoBAAAqgQAAAAAAACrBAAAAAAAAKsEAACqBAAAAAAAAKoEAACsBAAAAAAAAK0EAAAAAAAArQQAAKwEAAAAAAAArAQAAK4EAAAAAAAArwQAAAAAAACvBAAArgQAAAAAAACuBAAAsAQAAAAAAACxBAAAAAAAALEEAACwBAAAAAAAALAEAACyBAAAAAAAALMEAAAAAAAAswQAALIEAAAAAAAAsgQAALQEAAAAAAAAtQQAAAAAAAC1BAAAtAQAAAAAAAC0BAAAtgQAAAAAAAC3BAAAAAAAALcEAAC2BAAAAAAAALYEAAC4BAAAAAAAALkEAAAAAAAAuQQAALgEAAAAAAAAuAQAALoEAAAAAAAAuwQAAAAAAAC7BAAAugQAAAAAAAC6BAAAvAQAAAAAAAC9BAAAAAAAAL0EAAC8BAAAAAAAALwEAAC+BAAAAAAAAL8EAAAAAAAAvwQAAL4EAAAAAAAAvgQAAMAEAAAAAAAAzwQAAAAAAADBBAAAAAAAAMIEAAAAAAAAwgQAAMEEAAAAAAAAwQQAAMMEAAAAAAAAxAQAAAAAAADEBAAAwwQAAAAAAADDBAAAxQQAAAAAAADGBAAAAAAAAMYEAADFBAAAAAAAAMUEAADHBAAAAAAAAMgEAAAAAAAAyAQAAMcEAAAAAAAAxwQAAMkEAAAAAAAAygQAAAAAAADKBAAAyQQAAAAAAADJBAAAywQAAAAAAADMBAAAAAAAAMwEAADLBAAAAAAAAMsEAADNBAAAAAAAAM4EAAAAAAAAzgQAAM0EAAAAAAAAzQQAAM8EAADABAAAAAAAAMAEAADQBAAAAAAAANEEAAAAAAAA0QQAANAEAAAAAAAA0AQAANIEAAAAAAAA0wQAAAAAAADTBAAA0gQAAAAAAADSBAAA1AQAAAAAAADVBAAAAAAAANUEAADUBAAAAAAAANQEAADWBAAAAAAAANcEAAAAAAAA1wQAANYEAAAAAAAA1gQAANgEAAAAAAAA2QQAAAAAAADZBAAA2AQAAAAAAADYBAAA2gQAAAAAAADbBAAAAAAAANsEAADaBAAAAAAAANoEAADcBAAAAAAAAN0EAAAAAAAA3QQAANwEAAAAAAAA3AQAAN4EAAAAAAAA3wQAAAAAAADfBAAA3gQAAAAAAADeBAAA4AQAAAAAAADhBAAAAAAAAOEEAADgBAAAAAAAAOAEAADiBAAAAAAAAOMEAAAAAAAA4wQAAOIEAAAAAAAA4gQAAOQEAAAAAAAA5QQAAAAAAADlBAAA5AQAAAAAAADkBAAA5gQAAAAAAADnBAAAAAAAAOcEAADmBAAAAAAAAOYEAADoBAAAAAAAAOkEAAAAAAAA6QQAAOgEAAAAAAAA6AQAAOoEAAAAAAAA6wQAAAAAAADrBAAA6gQAAAAAAADqBAAA7AQAAAAAAADtBAAAAAAAAO0EAADsBAAAAAAAAOwEAADuBAAAAAAAAO8EAAAAAAAA7wQAAO4EAAAAAAAA7gQAAPAEAAAAAAAA8QQAAAAAAADxBAAA8AQAAAAAAADwBAAA8gQAAAAAAADzBAAAAAAAAPMEAADyBAAAAAAAAPIEAAD0BAAAAAAAAPUEAAAAAAAA9QQAAPQEAAAAAAAA9AQAAPYEAAAAAAAA9wQAAAAAAAD3BAAA9gQAAAAAAAD2BAAA+AQAAAAAAAD5BAAAAAAAAPkEAAD4BAAAAAAAAPgEAAD6BAAAAAAAAPsEAAAAAAAA+wQAAPoEAAAAAAAA+gQAAPwEAAAAAAAA/QQAAAAAAAD9BAAA/AQAAAAAAAD8BAAA/gQAAAAAAAD/BAAAAAAAAP8EAAD+BAAAAAAAAP4EAAAABQAAAAAAAAEFAAAAAAAAAQUAAAAFAAAAAAAAAAUAAAIFAAAAAAAAAwUAAAAAAAADBQAAAgUAAAAAAAACBQAABAUAAAAAAAAFBQAAAAAAAAUFAAAEBQAAAAAAAAQFAAAGBQAAAAAAAAcFAAAAAAAABwUAAAYFAAAAAAAABgUAAAgFAAAAAAAACQUAAAAAAAAJBQAACAUAAAAAAAAIBQAACgUAAAAAAAALBQAAAAAAAAsFAAAKBQAAAAAAAAoFAAAMBQAAAAAAAA0FAAAAAAAADQUAAAwFAAAAAAAADAUAAA4FAAAAAAAADwUAAAAAAAAPBQAADgUAAAAAAAAOBQAAEAUAAAAAAAARBQAAAAAAABEFAAAQBQAAAAAAABAFAAASBQAAAAAAABMFAAAAAAAAEwUAABIFAAAAAAAAEgUAABQFAAAAAAAAFQUAAAAAAAAVBQAAFAUAAAAAAAAUBQAAFgUAAAAAAAAXBQAAAAAAABcFAAAWBQAAAAAAABYFAAAYBQAAAAAAABkFAAAAAAAAGQUAABgFAAAAAAAAGAUAABoFAAAAAAAAGwUAAAAAAAAbBQAAGgUAAAAAAAAaBQAAHAUAAAAAAAAdBQAAAAAAAB0FAAAcBQAAAAAAABwFAAAeBQAAAAAAAB8FAAAAAAAAHwUAAB4FAAAAAAAAHgUAACAFAAAAAAAAIQUAAAAAAAAhBQAAIAUAAAAAAAAgBQAAIgUAAAAAAAAjBQAAAAAAACMFAAAiBQAAAAAAACIFAAAkBQAAAAAAACUFAAAAAAAAJQUAACQFAAAAAAAAJAUAACYFAAAAAAAAJwUAAAAAAAAnBQAAJgUAAAAAAAAmBQAAKAUAAAAAAAApBQAAAAAAACkFAAAoBQAAAAAAACgFAAAqBQAAAAAAACsFAAAAAAAAKwUAACoFAAAAAAAAKgUAACwFAAAAAAAALQUAAAAAAAAtBQAALAUAAAAAAAAsBQAALgUAAAAAAAAvBQAAAAAAAC8FAAAuBQAAAAAAAC4FAAAxBQAAAAAAAGEFAAAAAAAAMgUAAAAAAABiBQAAAAAAADMFAAAAAAAAYwUAAAAAAAA0BQAAAAAAAGQFAAAAAAAANQUAAAAAAABlBQAAAAAAADYFAAAAAAAAZgUAAAAAAAA3BQAAAAAAAGcFAAAAAAAAOAUAAAAAAABoBQAAAAAAADkFAAAAAAAAaQUAAAAAAAA6BQAAAAAAAGoFAAAAAAAAOwUAAAAAAABrBQAAAAAAADwFAAAAAAAAbAUAAAAAAAA9BQAAAAAAAG0FAAAAAAAAPgUAAAAAAABuBQAAAAAAAD8FAAAAAAAAbwUAAAAAAABABQAAAAAAAHAFAAAAAAAAQQUAAAAAAABxBQAAAAAAAEIFAAAAAAAAcgUAAAAAAABDBQAAAAAAAHMFAAAAAAAARAUAAAAAAAB0BQAAAAAAAEUFAAAAAAAAdQUAAAAAAABGBQAAAAAAAHYFAAAAAAAARwUAAAAAAAB3BQAAAAAAAEgFAAAAAAAAeAUAAAAAAABJBQAAAAAAAHkFAAAAAAAASgUAAAAAAAB6BQAAAAAAAEsFAAAAAAAAewUAAAAAAABMBQAAAAAAAHwFAAAAAAAATQUAAAAAAAB9BQAAAAAAAE4FAAAAAAAAfgUAAAAAAABPBQAAAAAAAH8FAAAAAAAAUAUAAAAAAACABQAAAAAAAFEFAAAAAAAAgQUAAAAAAABSBQAAAAAAAIIFAAAAAAAAUwUAAAAAAACDBQAAAAAAAFQFAAAAAAAAhAUAAAAAAABVBQAAAAAAAIUFAAAAAAAAVgUAAAAAAACGBQAAAAAAAGEFAAAxBQAAAAAAADEFAABiBQAAMgUAAAAAAAAyBQAAYwUAADMFAAAAAAAAMwUAAGQFAAA0BQAAAAAAADQFAABlBQAANQUAAAAAAAA1BQAAZgUAADYFAAAAAAAANgUAAGcFAAA3BQAAAAAAADcFAABoBQAAOAUAAAAAAAA4BQAAaQUAADkFAAAAAAAAOQUAAGoFAAA6BQAAAAAAADoFAABrBQAAOwUAAAAAAAA7BQAAbAUAADwFAAAAAAAAPAUAAG0FAAA9BQAAAAAAAD0FAABuBQAAPgUAAAAAAAA+BQAAbwUAAD8FAAAAAAAAPwUAAHAFAABABQAAAAAAAEAFAABxBQAAQQUAAAAAAABBBQAAcgUAAEIFAAAAAAAAQgUAAHMFAABDBQAAAAAAAEMFAAB0BQAARAUAAAAAAABEBQAAdQUAAEUFAAAAAAAARQUAAHYFAABGBQAAAAAAAEYFAAB3BQAARwUAAAAAAABHBQAAeAUAAEgFAAAAAAAASAUAAHkFAABJBQAAAAAAAEkFAAB6BQAASgUAAAAAAABKBQAAewUAAEsFAAAAAAAASwUAAHwFAABMBQAAAAAAAEwFAAB9BQAATQUAAAAAAABNBQAAfgUAAE4FAAAAAAAATgUAAH8FAABPBQAAAAAAAE8FAACABQAAUAUAAAAAAABQBQAAgQUAAFEFAAAAAAAAUQUAAIIFAABSBQAAAAAAAFIFAACDBQAAUwUAAAAAAABTBQAAhAUAAFQFAAAAAAAAVAUAAIUFAABVBQAAAAAAAFUFAACGBQAAVgUAAAAAAABWBQAAoBAAAAAAAAAALQAAAAAAAKEQAAAAAAAAAS0AAAAAAACiEAAAAAAAAAItAAAAAAAAoxAAAAAAAAADLQAAAAAAAKQQAAAAAAAABC0AAAAAAAClEAAAAAAAAAUtAAAAAAAAphAAAAAAAAAGLQAAAAAAAKcQAAAAAAAABy0AAAAAAACoEAAAAAAAAAgtAAAAAAAAqRAAAAAAAAAJLQAAAAAAAKoQAAAAAAAACi0AAAAAAACrEAAAAAAAAAstAAAAAAAArBAAAAAAAAAMLQAAAAAAAK0QAAAAAAAADS0AAAAAAACuEAAAAAAAAA4tAAAAAAAArxAAAAAAAAAPLQAAAAAAALAQAAAAAAAAEC0AAAAAAACxEAAAAAAAABEtAAAAAAAAshAAAAAAAAASLQAAAAAAALMQAAAAAAAAEy0AAAAAAAC0EAAAAAAAABQtAAAAAAAAtRAAAAAAAAAVLQAAAAAAALYQAAAAAAAAFi0AAAAAAAC3EAAAAAAAABctAAAAAAAAuBAAAAAAAAAYLQAAAAAAALkQAAAAAAAAGS0AAAAAAAC6EAAAAAAAABotAAAAAAAAuxAAAAAAAAAbLQAAAAAAALwQAAAAAAAAHC0AAAAAAAC9EAAAAAAAAB0tAAAAAAAAvhAAAAAAAAAeLQAAAAAAAL8QAAAAAAAAHy0AAAAAAADAEAAAAAAAACAtAAAAAAAAwRAAAAAAAAAhLQAAAAAAAMIQAAAAAAAAIi0AAAAAAADDEAAAAAAAACMtAAAAAAAAxBAAAAAAAAAkLQAAAAAAAMUQAAAAAAAAJS0AAAAAAADHEAAAAAAAACctAAAAAAAAzRAAAAAAAAAtLQAAAAAAANAQAACQHAAAAAAAANAQAADREAAAkRwAAAAAAADREAAA0hAAAJIcAAAAAAAA0hAAANMQAACTHAAAAAAAANMQAADUEAAAlBwAAAAAAADUEAAA1RAAAJUcAAAAAAAA1RAAANYQAACWHAAAAAAAANYQAADXEAAAlxwAAAAAAADXEAAA2BAAAJgcAAAAAAAA2BAAANkQAACZHAAAAAAAANkQAADaEAAAmhwAAAAAAADaEAAA2xAAAJscAAAAAAAA2xAAANwQAACcHAAAAAAAANwQAADdEAAAnRwAAAAAAADdEAAA3hAAAJ4cAAAAAAAA3hAAAN8QAACfHAAAAAAAAN8QAADgEAAAoBwAAAAAAADgEAAA4RAAAKEcAAAAAAAA4RAAAOIQAACiHAAAAAAAAOIQAADjEAAAoxwAAAAAAADjEAAA5BAAAKQcAAAAAAAA5BAAAOUQAAClHAAAAAAAAOUQAADmEAAAphwAAAAAAADmEAAA5xAAAKccAAAAAAAA5xAAAOgQAACoHAAAAAAAAOgQAADpEAAAqRwAAAAAAADpEAAA6hAAAKocAAAAAAAA6hAAAOsQAACrHAAAAAAAAOsQAADsEAAArBwAAAAAAADsEAAA7RAAAK0cAAAAAAAA7RAAAO4QAACuHAAAAAAAAO4QAADvEAAArxwAAAAAAADvEAAA8BAAALAcAAAAAAAA8BAAAPEQAACxHAAAAAAAAPEQAADyEAAAshwAAAAAAADyEAAA8xAAALMcAAAAAAAA8xAAAPQQAAC0HAAAAAAAAPQQAAD1EAAAtRwAAAAAAAD1EAAA9hAAALYcAAAAAAAA9hAAAPcQAAC3HAAAAAAAAPcQAAD4EAAAuBwAAAAAAAD4EAAA+RAAALkcAAAAAAAA+RAAAPoQAAC6HAAAAAAAAPoQAAD9EAAAvRwAAAAAAAD9EAAA/hAAAL4cAAAAAAAA/hAAAP8QAAC/HAAAAAAAAP8QAACgEwAAAAAAAHCrAAAAAAAAoRMAAAAAAABxqwAAAAAAAKITAAAAAAAAcqsAAAAAAACjEwAAAAAAAHOrAAAAAAAApBMAAAAAAAB0qwAAAAAAAKUTAAAAAAAAdasAAAAAAACmEwAAAAAAAHarAAAAAAAApxMAAAAAAAB3qwAAAAAAAKgTAAAAAAAAeKsAAAAAAACpEwAAAAAAAHmrAAAAAAAAqhMAAAAAAAB6qwAAAAAAAKsTAAAAAAAAe6sAAAAAAACsEwAAAAAAAHyrAAAAAAAArRMAAAAAAAB9qwAAAAAAAK4TAAAAAAAAfqsAAAAAAACvEwAAAAAAAH+rAAAAAAAAsBMAAAAAAACAqwAAAAAAALETAAAAAAAAgasAAAAAAACyEwAAAAAAAIKrAAAAAAAAsxMAAAAAAACDqwAAAAAAALQTAAAAAAAAhKsAAAAAAAC1EwAAAAAAAIWrAAAAAAAAthMAAAAAAACGqwAAAAAAALcTAAAAAAAAh6sAAAAAAAC4EwAAAAAAAIirAAAAAAAAuRMAAAAAAACJqwAAAAAAALoTAAAAAAAAiqsAAAAAAAC7EwAAAAAAAIurAAAAAAAAvBMAAAAAAACMqwAAAAAAAL0TAAAAAAAAjasAAAAAAAC+EwAAAAAAAI6rAAAAAAAAvxMAAAAAAACPqwAAAAAAAMATAAAAAAAAkKsAAAAAAADBEwAAAAAAAJGrAAAAAAAAwhMAAAAAAACSqwAAAAAAAMMTAAAAAAAAk6sAAAAAAADEEwAAAAAAAJSrAAAAAAAAxRMAAAAAAACVqwAAAAAAAMYTAAAAAAAAlqsAAAAAAADHEwAAAAAAAJerAAAAAAAAyBMAAAAAAACYqwAAAAAAAMkTAAAAAAAAmasAAAAAAADKEwAAAAAAAJqrAAAAAAAAyxMAAAAAAACbqwAAAAAAAMwTAAAAAAAAnKsAAAAAAADNEwAAAAAAAJ2rAAAAAAAAzhMAAAAAAACeqwAAAAAAAM8TAAAAAAAAn6sAAAAAAADQEwAAAAAAAKCrAAAAAAAA0RMAAAAAAAChqwAAAAAAANITAAAAAAAAoqsAAAAAAADTEwAAAAAAAKOrAAAAAAAA1BMAAAAAAACkqwAAAAAAANUTAAAAAAAApasAAAAAAADWEwAAAAAAAKarAAAAAAAA1xMAAAAAAACnqwAAAAAAANgTAAAAAAAAqKsAAAAAAADZEwAAAAAAAKmrAAAAAAAA2hMAAAAAAACqqwAAAAAAANsTAAAAAAAAq6sAAAAAAADcEwAAAAAAAKyrAAAAAAAA3RMAAAAAAACtqwAAAAAAAN4TAAAAAAAArqsAAAAAAADfEwAAAAAAAK+rAAAAAAAA4BMAAAAAAACwqwAAAAAAAOETAAAAAAAAsasAAAAAAADiEwAAAAAAALKrAAAAAAAA4xMAAAAAAACzqwAAAAAAAOQTAAAAAAAAtKsAAAAAAADlEwAAAAAAALWrAAAAAAAA5hMAAAAAAAC2qwAAAAAAAOcTAAAAAAAAt6sAAAAAAADoEwAAAAAAALirAAAAAAAA6RMAAAAAAAC5qwAAAAAAAOoTAAAAAAAAuqsAAAAAAADrEwAAAAAAALurAAAAAAAA7BMAAAAAAAC8qwAAAAAAAO0TAAAAAAAAvasAAAAAAADuEwAAAAAAAL6rAAAAAAAA7xMAAAAAAAC/qwAAAAAAAPATAAAAAAAA+BMAAAAAAADxEwAAAAAAAPkTAAAAAAAA8hMAAAAAAAD6EwAAAAAAAPMTAAAAAAAA+xMAAAAAAAD0EwAAAAAAAPwTAAAAAAAA9RMAAAAAAAD9EwAAAAAAAPgTAADwEwAAAAAAAPATAAD5EwAA8RMAAAAAAADxEwAA+hMAAPITAAAAAAAA8hMAAPsTAADzEwAAAAAAAPMTAAD8EwAA9BMAAAAAAAD0EwAA/RMAAPUTAAAAAAAA9RMAAIAcAAASBAAAAAAAABIEAACBHAAAFAQAAAAAAAAUBAAAghwAAB4EAAAAAAAAHgQAAIMcAAAhBAAAAAAAACEEAACEHAAAIgQAAAAAAAAiBAAAhRwAACIEAAAAAAAAIgQAAIYcAAAqBAAAAAAAACoEAACHHAAAYgQAAAAAAABiBAAAiBwAAEqmAAAAAAAASqYAAJAcAAAAAAAA0BAAAAAAAACRHAAAAAAAANEQAAAAAAAAkhwAAAAAAADSEAAAAAAAAJMcAAAAAAAA0xAAAAAAAACUHAAAAAAAANQQAAAAAAAAlRwAAAAAAADVEAAAAAAAAJYcAAAAAAAA1hAAAAAAAACXHAAAAAAAANcQAAAAAAAAmBwAAAAAAADYEAAAAAAAAJkcAAAAAAAA2RAAAAAAAACaHAAAAAAAANoQAAAAAAAAmxwAAAAAAADbEAAAAAAAAJwcAAAAAAAA3BAAAAAAAACdHAAAAAAAAN0QAAAAAAAAnhwAAAAAAADeEAAAAAAAAJ8cAAAAAAAA3xAAAAAAAACgHAAAAAAAAOAQAAAAAAAAoRwAAAAAAADhEAAAAAAAAKIcAAAAAAAA4hAAAAAAAACjHAAAAAAAAOMQAAAAAAAApBwAAAAAAADkEAAAAAAAAKUcAAAAAAAA5RAAAAAAAACmHAAAAAAAAOYQAAAAAAAApxwAAAAAAADnEAAAAAAAAKgcAAAAAAAA6BAAAAAAAACpHAAAAAAAAOkQAAAAAAAAqhwAAAAAAADqEAAAAAAAAKscAAAAAAAA6xAAAAAAAACsHAAAAAAAAOwQAAAAAAAArRwAAAAAAADtEAAAAAAAAK4cAAAAAAAA7hAAAAAAAACvHAAAAAAAAO8QAAAAAAAAsBwAAAAAAADwEAAAAAAAALEcAAAAAAAA8RAAAAAAAACyHAAAAAAAAPIQAAAAAAAAsxwAAAAAAADzEAAAAAAAALQcAAAAAAAA9BAAAAAAAAC1HAAAAAAAAPUQAAAAAAAAthwAAAAAAAD2EAAAAAAAALccAAAAAAAA9xAAAAAAAAC4HAAAAAAAAPgQAAAAAAAAuRwAAAAAAAD5EAAAAAAAALocAAAAAAAA+hAAAAAAAAC9HAAAAAAAAP0QAAAAAAAAvhwAAAAAAAD+EAAAAAAAAL8cAAAAAAAA/xAAAAAAAAB5HQAAfacAAAAAAAB9pwAAfR0AAGMsAAAAAAAAYywAAAAeAAAAAAAAAR4AAAAAAAABHgAAAB4AAAAAAAAAHgAAAh4AAAAAAAADHgAAAAAAAAMeAAACHgAAAAAAAAIeAAAEHgAAAAAAAAUeAAAAAAAABR4AAAQeAAAAAAAABB4AAAYeAAAAAAAABx4AAAAAAAAHHgAABh4AAAAAAAAGHgAACB4AAAAAAAAJHgAAAAAAAAkeAAAIHgAAAAAAAAgeAAAKHgAAAAAAAAseAAAAAAAACx4AAAoeAAAAAAAACh4AAAweAAAAAAAADR4AAAAAAAANHgAADB4AAAAAAAAMHgAADh4AAAAAAAAPHgAAAAAAAA8eAAAOHgAAAAAAAA4eAAAQHgAAAAAAABEeAAAAAAAAER4AABAeAAAAAAAAEB4AABIeAAAAAAAAEx4AAAAAAAATHgAAEh4AAAAAAAASHgAAFB4AAAAAAAAVHgAAAAAAABUeAAAUHgAAAAAAABQeAAAWHgAAAAAAABceAAAAAAAAFx4AABYeAAAAAAAAFh4AABgeAAAAAAAAGR4AAAAAAAAZHgAAGB4AAAAAAAAYHgAAGh4AAAAAAAAbHgAAAAAAABseAAAaHgAAAAAAABoeAAAcHgAAAAAAAB0eAAAAAAAAHR4AABweAAAAAAAAHB4AAB4eAAAAAAAAHx4AAAAAAAAfHgAAHh4AAAAAAAAeHgAAIB4AAAAAAAAhHgAAAAAAACEeAAAgHgAAAAAAACAeAAAiHgAAAAAAACMeAAAAAAAAIx4AACIeAAAAAAAAIh4AACQeAAAAAAAAJR4AAAAAAAAlHgAAJB4AAAAAAAAkHgAAJh4AAAAAAAAnHgAAAAAAACceAAAmHgAAAAAAACYeAAAoHgAAAAAAACkeAAAAAAAAKR4AACgeAAAAAAAAKB4AACoeAAAAAAAAKx4AAAAAAAArHgAAKh4AAAAAAAAqHgAALB4AAAAAAAAtHgAAAAAAAC0eAAAsHgAAAAAAACweAAAuHgAAAAAAAC8eAAAAAAAALx4AAC4eAAAAAAAALh4AADAeAAAAAAAAMR4AAAAAAAAxHgAAMB4AAAAAAAAwHgAAMh4AAAAAAAAzHgAAAAAAADMeAAAyHgAAAAAAADIeAAA0HgAAAAAAADUeAAAAAAAANR4AADQeAAAAAAAANB4AADYeAAAAAAAANx4AAAAAAAA3HgAANh4AAAAAAAA2HgAAOB4AAAAAAAA5HgAAAAAAADkeAAA4HgAAAAAAADgeAAA6HgAAAAAAADseAAAAAAAAOx4AADoeAAAAAAAAOh4AADweAAAAAAAAPR4AAAAAAAA9HgAAPB4AAAAAAAA8HgAAPh4AAAAAAAA/HgAAAAAAAD8eAAA+HgAAAAAAAD4eAABAHgAAAAAAAEEeAAAAAAAAQR4AAEAeAAAAAAAAQB4AAEIeAAAAAAAAQx4AAAAAAABDHgAAQh4AAAAAAABCHgAARB4AAAAAAABFHgAAAAAAAEUeAABEHgAAAAAAAEQeAABGHgAAAAAAAEceAAAAAAAARx4AAEYeAAAAAAAARh4AAEgeAAAAAAAASR4AAAAAAABJHgAASB4AAAAAAABIHgAASh4AAAAAAABLHgAAAAAAAEseAABKHgAAAAAAAEoeAABMHgAAAAAAAE0eAAAAAAAATR4AAEweAAAAAAAATB4AAE4eAAAAAAAATx4AAAAAAABPHgAATh4AAAAAAABOHgAAUB4AAAAAAABRHgAAAAAAAFEeAABQHgAAAAAAAFAeAABSHgAAAAAAAFMeAAAAAAAAUx4AAFIeAAAAAAAAUh4AAFQeAAAAAAAAVR4AAAAAAABVHgAAVB4AAAAAAABUHgAAVh4AAAAAAABXHgAAAAAAAFceAABWHgAAAAAAAFYeAABYHgAAAAAAAFkeAAAAAAAAWR4AAFgeAAAAAAAAWB4AAFoeAAAAAAAAWx4AAAAAAABbHgAAWh4AAAAAAABaHgAAXB4AAAAAAABdHgAAAAAAAF0eAABcHgAAAAAAAFweAABeHgAAAAAAAF8eAAAAAAAAXx4AAF4eAAAAAAAAXh4AAGAeAAAAAAAAYR4AAAAAAABhHgAAYB4AAAAAAABgHgAAYh4AAAAAAABjHgAAAAAAAGMeAABiHgAAAAAAAGIeAABkHgAAAAAAAGUeAAAAAAAAZR4AAGQeAAAAAAAAZB4AAGYeAAAAAAAAZx4AAAAAAABnHgAAZh4AAAAAAABmHgAAaB4AAAAAAABpHgAAAAAAAGkeAABoHgAAAAAAAGgeAABqHgAAAAAAAGseAAAAAAAAax4AAGoeAAAAAAAAah4AAGweAAAAAAAAbR4AAAAAAABtHgAAbB4AAAAAAABsHgAAbh4AAAAAAABvHgAAAAAAAG8eAABuHgAAAAAAAG4eAABwHgAAAAAAAHEeAAAAAAAAcR4AAHAeAAAAAAAAcB4AAHIeAAAAAAAAcx4AAAAAAABzHgAAch4AAAAAAAByHgAAdB4AAAAAAAB1HgAAAAAAAHUeAAB0HgAAAAAAAHQeAAB2HgAAAAAAAHceAAAAAAAAdx4AAHYeAAAAAAAAdh4AAHgeAAAAAAAAeR4AAAAAAAB5HgAAeB4AAAAAAAB4HgAAeh4AAAAAAAB7HgAAAAAAAHseAAB6HgAAAAAAAHoeAAB8HgAAAAAAAH0eAAAAAAAAfR4AAHweAAAAAAAAfB4AAH4eAAAAAAAAfx4AAAAAAAB/HgAAfh4AAAAAAAB+HgAAgB4AAAAAAACBHgAAAAAAAIEeAACAHgAAAAAAAIAeAACCHgAAAAAAAIMeAAAAAAAAgx4AAIIeAAAAAAAAgh4AAIQeAAAAAAAAhR4AAAAAAACFHgAAhB4AAAAAAACEHgAAhh4AAAAAAACHHgAAAAAAAIceAACGHgAAAAAAAIYeAACIHgAAAAAAAIkeAAAAAAAAiR4AAIgeAAAAAAAAiB4AAIoeAAAAAAAAix4AAAAAAACLHgAAih4AAAAAAACKHgAAjB4AAAAAAACNHgAAAAAAAI0eAACMHgAAAAAAAIweAACOHgAAAAAAAI8eAAAAAAAAjx4AAI4eAAAAAAAAjh4AAJAeAAAAAAAAkR4AAAAAAACRHgAAkB4AAAAAAACQHgAAkh4AAAAAAACTHgAAAAAAAJMeAACSHgAAAAAAAJIeAACUHgAAAAAAAJUeAAAAAAAAlR4AAJQeAAAAAAAAlB4AAJseAABgHgAAAAAAAGAeAACeHgAAAAAAAN8AAAAAAAAAoB4AAAAAAAChHgAAAAAAAKEeAACgHgAAAAAAAKAeAACiHgAAAAAAAKMeAAAAAAAAox4AAKIeAAAAAAAAoh4AAKQeAAAAAAAApR4AAAAAAAClHgAApB4AAAAAAACkHgAAph4AAAAAAACnHgAAAAAAAKceAACmHgAAAAAAAKYeAACoHgAAAAAAAKkeAAAAAAAAqR4AAKgeAAAAAAAAqB4AAKoeAAAAAAAAqx4AAAAAAACrHgAAqh4AAAAAAACqHgAArB4AAAAAAACtHgAAAAAAAK0eAACsHgAAAAAAAKweAACuHgAAAAAAAK8eAAAAAAAArx4AAK4eAAAAAAAArh4AALAeAAAAAAAAsR4AAAAAAACxHgAAsB4AAAAAAACwHgAAsh4AAAAAAACzHgAAAAAAALMeAACyHgAAAAAAALIeAAC0HgAAAAAAALUeAAAAAAAAtR4AALQeAAAAAAAAtB4AALYeAAAAAAAAtx4AAAAAAAC3HgAAth4AAAAAAAC2HgAAuB4AAAAAAAC5HgAAAAAAALkeAAC4HgAAAAAAALgeAAC6HgAAAAAAALseAAAAAAAAux4AALoeAAAAAAAAuh4AALweAAAAAAAAvR4AAAAAAAC9HgAAvB4AAAAAAAC8HgAAvh4AAAAAAAC/HgAAAAAAAL8eAAC+HgAAAAAAAL4eAADAHgAAAAAAAMEeAAAAAAAAwR4AAMAeAAAAAAAAwB4AAMIeAAAAAAAAwx4AAAAAAADDHgAAwh4AAAAAAADCHgAAxB4AAAAAAADFHgAAAAAAAMUeAADEHgAAAAAAAMQeAADGHgAAAAAAAMceAAAAAAAAxx4AAMYeAAAAAAAAxh4AAMgeAAAAAAAAyR4AAAAAAADJHgAAyB4AAAAAAADIHgAAyh4AAAAAAADLHgAAAAAAAMseAADKHgAAAAAAAMoeAADMHgAAAAAAAM0eAAAAAAAAzR4AAMweAAAAAAAAzB4AAM4eAAAAAAAAzx4AAAAAAADPHgAAzh4AAAAAAADOHgAA0B4AAAAAAADRHgAAAAAAANEeAADQHgAAAAAAANAeAADSHgAAAAAAANMeAAAAAAAA0x4AANIeAAAAAAAA0h4AANQeAAAAAAAA1R4AAAAAAADVHgAA1B4AAAAAAADUHgAA1h4AAAAAAADXHgAAAAAAANceAADWHgAAAAAAANYeAADYHgAAAAAAANkeAAAAAAAA2R4AANgeAAAAAAAA2B4AANoeAAAAAAAA2x4AAAAAAADbHgAA2h4AAAAAAADaHgAA3B4AAAAAAADdHgAAAAAAAN0eAADcHgAAAAAAANweAADeHgAAAAAAAN8eAAAAAAAA3x4AAN4eAAAAAAAA3h4AAOAeAAAAAAAA4R4AAAAAAADhHgAA4B4AAAAAAADgHgAA4h4AAAAAAADjHgAAAAAAAOMeAADiHgAAAAAAAOIeAADkHgAAAAAAAOUeAAAAAAAA5R4AAOQeAAAAAAAA5B4AAOYeAAAAAAAA5x4AAAAAAADnHgAA5h4AAAAAAADmHgAA6B4AAAAAAADpHgAAAAAAAOkeAADoHgAAAAAAAOgeAADqHgAAAAAAAOseAAAAAAAA6x4AAOoeAAAAAAAA6h4AAOweAAAAAAAA7R4AAAAAAADtHgAA7B4AAAAAAADsHgAA7h4AAAAAAADvHgAAAAAAAO8eAADuHgAAAAAAAO4eAADwHgAAAAAAAPEeAAAAAAAA8R4AAPAeAAAAAAAA8B4AAPIeAAAAAAAA8x4AAAAAAADzHgAA8h4AAAAAAADyHgAA9B4AAAAAAAD1HgAAAAAAAPUeAAD0HgAAAAAAAPQeAAD2HgAAAAAAAPceAAAAAAAA9x4AAPYeAAAAAAAA9h4AAPgeAAAAAAAA+R4AAAAAAAD5HgAA+B4AAAAAAAD4HgAA+h4AAAAAAAD7HgAAAAAAAPseAAD6HgAAAAAAAPoeAAD8HgAAAAAAAP0eAAAAAAAA/R4AAPweAAAAAAAA/B4AAP4eAAAAAAAA/x4AAAAAAAD/HgAA/h4AAAAAAAD+HgAAAB8AAAgfAAAAAAAACB8AAAEfAAAJHwAAAAAAAAkfAAACHwAACh8AAAAAAAAKHwAAAx8AAAsfAAAAAAAACx8AAAQfAAAMHwAAAAAAAAwfAAAFHwAADR8AAAAAAAANHwAABh8AAA4fAAAAAAAADh8AAAcfAAAPHwAAAAAAAA8fAAAIHwAAAAAAAAAfAAAAAAAACR8AAAAAAAABHwAAAAAAAAofAAAAAAAAAh8AAAAAAAALHwAAAAAAAAMfAAAAAAAADB8AAAAAAAAEHwAAAAAAAA0fAAAAAAAABR8AAAAAAAAOHwAAAAAAAAYfAAAAAAAADx8AAAAAAAAHHwAAAAAAABAfAAAYHwAAAAAAABgfAAARHwAAGR8AAAAAAAAZHwAAEh8AABofAAAAAAAAGh8AABMfAAAbHwAAAAAAABsfAAAUHwAAHB8AAAAAAAAcHwAAFR8AAB0fAAAAAAAAHR8AABgfAAAAAAAAEB8AAAAAAAAZHwAAAAAAABEfAAAAAAAAGh8AAAAAAAASHwAAAAAAABsfAAAAAAAAEx8AAAAAAAAcHwAAAAAAABQfAAAAAAAAHR8AAAAAAAAVHwAAAAAAACAfAAAoHwAAAAAAACgfAAAhHwAAKR8AAAAAAAApHwAAIh8AACofAAAAAAAAKh8AACMfAAArHwAAAAAAACsfAAAkHwAALB8AAAAAAAAsHwAAJR8AAC0fAAAAAAAALR8AACYfAAAuHwAAAAAAAC4fAAAnHwAALx8AAAAAAAAvHwAAKB8AAAAAAAAgHwAAAAAAACkfAAAAAAAAIR8AAAAAAAAqHwAAAAAAACIfAAAAAAAAKx8AAAAAAAAjHwAAAAAAACwfAAAAAAAAJB8AAAAAAAAtHwAAAAAAACUfAAAAAAAALh8AAAAAAAAmHwAAAAAAAC8fAAAAAAAAJx8AAAAAAAAwHwAAOB8AAAAAAAA4HwAAMR8AADkfAAAAAAAAOR8AADIfAAA6HwAAAAAAADofAAAzHwAAOx8AAAAAAAA7HwAANB8AADwfAAAAAAAAPB8AADUfAAA9HwAAAAAAAD0fAAA2HwAAPh8AAAAAAAA+HwAANx8AAD8fAAAAAAAAPx8AADgfAAAAAAAAMB8AAAAAAAA5HwAAAAAAADEfAAAAAAAAOh8AAAAAAAAyHwAAAAAAADsfAAAAAAAAMx8AAAAAAAA8HwAAAAAAADQfAAAAAAAAPR8AAAAAAAA1HwAAAAAAAD4fAAAAAAAANh8AAAAAAAA/HwAAAAAAADcfAAAAAAAAQB8AAEgfAAAAAAAASB8AAEEfAABJHwAAAAAAAEkfAABCHwAASh8AAAAAAABKHwAAQx8AAEsfAAAAAAAASx8AAEQfAABMHwAAAAAAAEwfAABFHwAATR8AAAAAAABNHwAASB8AAAAAAABAHwAAAAAAAEkfAAAAAAAAQR8AAAAAAABKHwAAAAAAAEIfAAAAAAAASx8AAAAAAABDHwAAAAAAAEwfAAAAAAAARB8AAAAAAABNHwAAAAAAAEUfAAAAAAAAUR8AAFkfAAAAAAAAWR8AAFMfAABbHwAAAAAAAFsfAABVHwAAXR8AAAAAAABdHwAAVx8AAF8fAAAAAAAAXx8AAFkfAAAAAAAAUR8AAAAAAABbHwAAAAAAAFMfAAAAAAAAXR8AAAAAAABVHwAAAAAAAF8fAAAAAAAAVx8AAAAAAABgHwAAaB8AAAAAAABoHwAAYR8AAGkfAAAAAAAAaR8AAGIfAABqHwAAAAAAAGofAABjHwAAax8AAAAAAABrHwAAZB8AAGwfAAAAAAAAbB8AAGUfAABtHwAAAAAAAG0fAABmHwAAbh8AAAAAAABuHwAAZx8AAG8fAAAAAAAAbx8AAGgfAAAAAAAAYB8AAAAAAABpHwAAAAAAAGEfAAAAAAAAah8AAAAAAABiHwAAAAAAAGsfAAAAAAAAYx8AAAAAAABsHwAAAAAAAGQfAAAAAAAAbR8AAAAAAABlHwAAAAAAAG4fAAAAAAAAZh8AAAAAAABvHwAAAAAAAGcfAAAAAAAAcB8AALofAAAAAAAAuh8AAHEfAAC7HwAAAAAAALsfAAByHwAAyB8AAAAAAADIHwAAcx8AAMkfAAAAAAAAyR8AAHQfAADKHwAAAAAAAMofAAB1HwAAyx8AAAAAAADLHwAAdh8AANofAAAAAAAA2h8AAHcfAADbHwAAAAAAANsfAAB4HwAA+B8AAAAAAAD4HwAAeR8AAPkfAAAAAAAA+R8AAHofAADqHwAAAAAAAOofAAB7HwAA6x8AAAAAAADrHwAAfB8AAPofAAAAAAAA+h8AAH0fAAD7HwAAAAAAAPsfAACAHwAAiB8AAAAAAACIHwAAgR8AAIkfAAAAAAAAiR8AAIIfAACKHwAAAAAAAIofAACDHwAAix8AAAAAAACLHwAAhB8AAIwfAAAAAAAAjB8AAIUfAACNHwAAAAAAAI0fAACGHwAAjh8AAAAAAACOHwAAhx8AAI8fAAAAAAAAjx8AAIgfAAAAAAAAgB8AAAAAAACJHwAAAAAAAIEfAAAAAAAAih8AAAAAAACCHwAAAAAAAIsfAAAAAAAAgx8AAAAAAACMHwAAAAAAAIQfAAAAAAAAjR8AAAAAAACFHwAAAAAAAI4fAAAAAAAAhh8AAAAAAACPHwAAAAAAAIcfAAAAAAAAkB8AAJgfAAAAAAAAmB8AAJEfAACZHwAAAAAAAJkfAACSHwAAmh8AAAAAAACaHwAAkx8AAJsfAAAAAAAAmx8AAJQfAACcHwAAAAAAAJwfAACVHwAAnR8AAAAAAACdHwAAlh8AAJ4fAAAAAAAAnh8AAJcfAACfHwAAAAAAAJ8fAACYHwAAAAAAAJAfAAAAAAAAmR8AAAAAAACRHwAAAAAAAJofAAAAAAAAkh8AAAAAAACbHwAAAAAAAJMfAAAAAAAAnB8AAAAAAACUHwAAAAAAAJ0fAAAAAAAAlR8AAAAAAACeHwAAAAAAAJYfAAAAAAAAnx8AAAAAAACXHwAAAAAAAKAfAACoHwAAAAAAAKgfAAChHwAAqR8AAAAAAACpHwAAoh8AAKofAAAAAAAAqh8AAKMfAACrHwAAAAAAAKsfAACkHwAArB8AAAAAAACsHwAApR8AAK0fAAAAAAAArR8AAKYfAACuHwAAAAAAAK4fAACnHwAArx8AAAAAAACvHwAAqB8AAAAAAACgHwAAAAAAAKkfAAAAAAAAoR8AAAAAAACqHwAAAAAAAKIfAAAAAAAAqx8AAAAAAACjHwAAAAAAAKwfAAAAAAAApB8AAAAAAACtHwAAAAAAAKUfAAAAAAAArh8AAAAAAACmHwAAAAAAAK8fAAAAAAAApx8AAAAAAACwHwAAuB8AAAAAAAC4HwAAsR8AALkfAAAAAAAAuR8AALMfAAC8HwAAAAAAALwfAAC4HwAAAAAAALAfAAAAAAAAuR8AAAAAAACxHwAAAAAAALofAAAAAAAAcB8AAAAAAAC7HwAAAAAAAHEfAAAAAAAAvB8AAAAAAACzHwAAAAAAAL4fAACZAwAAAAAAAJkDAADDHwAAzB8AAAAAAADMHwAAyB8AAAAAAAByHwAAAAAAAMkfAAAAAAAAcx8AAAAAAADKHwAAAAAAAHQfAAAAAAAAyx8AAAAAAAB1HwAAAAAAAMwfAAAAAAAAwx8AAAAAAADQHwAA2B8AAAAAAADYHwAA0R8AANkfAAAAAAAA2R8AANgfAAAAAAAA0B8AAAAAAADZHwAAAAAAANEfAAAAAAAA2h8AAAAAAAB2HwAAAAAAANsfAAAAAAAAdx8AAAAAAADgHwAA6B8AAAAAAADoHwAA4R8AAOkfAAAAAAAA6R8AAOUfAADsHwAAAAAAAOwfAADoHwAAAAAAAOAfAAAAAAAA6R8AAAAAAADhHwAAAAAAAOofAAAAAAAAeh8AAAAAAADrHwAAAAAAAHsfAAAAAAAA7B8AAAAAAADlHwAAAAAAAPMfAAD8HwAAAAAAAPwfAAD4HwAAAAAAAHgfAAAAAAAA+R8AAAAAAAB5HwAAAAAAAPofAAAAAAAAfB8AAAAAAAD7HwAAAAAAAH0fAAAAAAAA/B8AAAAAAADzHwAAAAAAACYhAAAAAAAAyQMAAAAAAAAqIQAAAAAAAGsAAAAAAAAAKyEAAAAAAADlAAAAAAAAADIhAAAAAAAATiEAAAAAAABOIQAAMiEAAAAAAAAyIQAAYCEAAAAAAABwIQAAAAAAAGEhAAAAAAAAcSEAAAAAAABiIQAAAAAAAHIhAAAAAAAAYyEAAAAAAABzIQAAAAAAAGQhAAAAAAAAdCEAAAAAAABlIQAAAAAAAHUhAAAAAAAAZiEAAAAAAAB2IQAAAAAAAGchAAAAAAAAdyEAAAAAAABoIQAAAAAAAHghAAAAAAAAaSEAAAAAAAB5IQAAAAAAAGohAAAAAAAAeiEAAAAAAABrIQAAAAAAAHshAAAAAAAAbCEAAAAAAAB8IQAAAAAAAG0hAAAAAAAAfSEAAAAAAABuIQAAAAAAAH4hAAAAAAAAbyEAAAAAAAB/IQAAAAAAAHAhAABgIQAAAAAAAGAhAABxIQAAYSEAAAAAAABhIQAAciEAAGIhAAAAAAAAYiEAAHMhAABjIQAAAAAAAGMhAAB0IQAAZCEAAAAAAABkIQAAdSEAAGUhAAAAAAAAZSEAAHYhAABmIQAAAAAAAGYhAAB3IQAAZyEAAAAAAABnIQAAeCEAAGghAAAAAAAAaCEAAHkhAABpIQAAAAAAAGkhAAB6IQAAaiEAAAAAAABqIQAAeyEAAGshAAAAAAAAayEAAHwhAABsIQAAAAAAAGwhAAB9IQAAbSEAAAAAAABtIQAAfiEAAG4hAAAAAAAAbiEAAH8hAABvIQAAAAAAAG8hAACDIQAAAAAAAIQhAAAAAAAAhCEAAIMhAAAAAAAAgyEAALYkAAAAAAAA0CQAAAAAAAC3JAAAAAAAANEkAAAAAAAAuCQAAAAAAADSJAAAAAAAALkkAAAAAAAA0yQAAAAAAAC6JAAAAAAAANQkAAAAAAAAuyQAAAAAAADVJAAAAAAAALwkAAAAAAAA1iQAAAAAAAC9JAAAAAAAANckAAAAAAAAviQAAAAAAADYJAAAAAAAAL8kAAAAAAAA2SQAAAAAAADAJAAAAAAAANokAAAAAAAAwSQAAAAAAADbJAAAAAAAAMIkAAAAAAAA3CQAAAAAAADDJAAAAAAAAN0kAAAAAAAAxCQAAAAAAADeJAAAAAAAAMUkAAAAAAAA3yQAAAAAAADGJAAAAAAAAOAkAAAAAAAAxyQAAAAAAADhJAAAAAAAAMgkAAAAAAAA4iQAAAAAAADJJAAAAAAAAOMkAAAAAAAAyiQAAAAAAADkJAAAAAAAAMskAAAAAAAA5SQAAAAAAADMJAAAAAAAAOYkAAAAAAAAzSQAAAAAAADnJAAAAAAAAM4kAAAAAAAA6CQAAAAAAADPJAAAAAAAAOkkAAAAAAAA0CQAALYkAAAAAAAAtiQAANEkAAC3JAAAAAAAALckAADSJAAAuCQAAAAAAAC4JAAA0yQAALkkAAAAAAAAuSQAANQkAAC6JAAAAAAAALokAADVJAAAuyQAAAAAAAC7JAAA1iQAALwkAAAAAAAAvCQAANckAAC9JAAAAAAAAL0kAADYJAAAviQAAAAAAAC+JAAA2SQAAL8kAAAAAAAAvyQAANokAADAJAAAAAAAAMAkAADbJAAAwSQAAAAAAADBJAAA3CQAAMIkAAAAAAAAwiQAAN0kAADDJAAAAAAAAMMkAADeJAAAxCQAAAAAAADEJAAA3yQAAMUkAAAAAAAAxSQAAOAkAADGJAAAAAAAAMYkAADhJAAAxyQAAAAAAADHJAAA4iQAAMgkAAAAAAAAyCQAAOMkAADJJAAAAAAAAMkkAADkJAAAyiQAAAAAAADKJAAA5SQAAMskAAAAAAAAyyQAAOYkAADMJAAAAAAAAMwkAADnJAAAzSQAAAAAAADNJAAA6CQAAM4kAAAAAAAAziQAAOkkAADPJAAAAAAAAM8kAAAALAAAAAAAADAsAAAAAAAAASwAAAAAAAAxLAAAAAAAAAIsAAAAAAAAMiwAAAAAAAADLAAAAAAAADMsAAAAAAAABCwAAAAAAAA0LAAAAAAAAAUsAAAAAAAANSwAAAAAAAAGLAAAAAAAADYsAAAAAAAABywAAAAAAAA3LAAAAAAAAAgsAAAAAAAAOCwAAAAAAAAJLAAAAAAAADksAAAAAAAACiwAAAAAAAA6LAAAAAAAAAssAAAAAAAAOywAAAAAAAAMLAAAAAAAADwsAAAAAAAADSwAAAAAAAA9LAAAAAAAAA4sAAAAAAAAPiwAAAAAAAAPLAAAAAAAAD8sAAAAAAAAECwAAAAAAABALAAAAAAAABEsAAAAAAAAQSwAAAAAAAASLAAAAAAAAEIsAAAAAAAAEywAAAAAAABDLAAAAAAAABQsAAAAAAAARCwAAAAAAAAVLAAAAAAAAEUsAAAAAAAAFiwAAAAAAABGLAAAAAAAABcsAAAAAAAARywAAAAAAAAYLAAAAAAAAEgsAAAAAAAAGSwAAAAAAABJLAAAAAAAABosAAAAAAAASiwAAAAAAAAbLAAAAAAAAEssAAAAAAAAHCwAAAAAAABMLAAAAAAAAB0sAAAAAAAATSwAAAAAAAAeLAAAAAAAAE4sAAAAAAAAHywAAAAAAABPLAAAAAAAACAsAAAAAAAAUCwAAAAAAAAhLAAAAAAAAFEsAAAAAAAAIiwAAAAAAABSLAAAAAAAACMsAAAAAAAAUywAAAAAAAAkLAAAAAAAAFQsAAAAAAAAJSwAAAAAAABVLAAAAAAAACYsAAAAAAAAViwAAAAAAAAnLAAAAAAAAFcsAAAAAAAAKCwAAAAAAABYLAAAAAAAACksAAAAAAAAWSwAAAAAAAAqLAAAAAAAAFosAAAAAAAAKywAAAAAAABbLAAAAAAAACwsAAAAAAAAXCwAAAAAAAAtLAAAAAAAAF0sAAAAAAAALiwAAAAAAABeLAAAAAAAADAsAAAALAAAAAAAAAAsAAAxLAAAASwAAAAAAAABLAAAMiwAAAIsAAAAAAAAAiwAADMsAAADLAAAAAAAAAMsAAA0LAAABCwAAAAAAAAELAAANSwAAAUsAAAAAAAABSwAADYsAAAGLAAAAAAAAAYsAAA3LAAABywAAAAAAAAHLAAAOCwAAAgsAAAAAAAACCwAADksAAAJLAAAAAAAAAksAAA6LAAACiwAAAAAAAAKLAAAOywAAAssAAAAAAAACywAADwsAAAMLAAAAAAAAAwsAAA9LAAADSwAAAAAAAANLAAAPiwAAA4sAAAAAAAADiwAAD8sAAAPLAAAAAAAAA8sAABALAAAECwAAAAAAAAQLAAAQSwAABEsAAAAAAAAESwAAEIsAAASLAAAAAAAABIsAABDLAAAEywAAAAAAAATLAAARCwAABQsAAAAAAAAFCwAAEUsAAAVLAAAAAAAABUsAABGLAAAFiwAAAAAAAAWLAAARywAABcsAAAAAAAAFywAAEgsAAAYLAAAAAAAABgsAABJLAAAGSwAAAAAAAAZLAAASiwAABosAAAAAAAAGiwAAEssAAAbLAAAAAAAABssAABMLAAAHCwAAAAAAAAcLAAATSwAAB0sAAAAAAAAHSwAAE4sAAAeLAAAAAAAAB4sAABPLAAAHywAAAAAAAAfLAAAUCwAACAsAAAAAAAAICwAAFEsAAAhLAAAAAAAACEsAABSLAAAIiwAAAAAAAAiLAAAUywAACMsAAAAAAAAIywAAFQsAAAkLAAAAAAAACQsAABVLAAAJSwAAAAAAAAlLAAAViwAACYsAAAAAAAAJiwAAFcsAAAnLAAAAAAAACcsAABYLAAAKCwAAAAAAAAoLAAAWSwAACksAAAAAAAAKSwAAFosAAAqLAAAAAAAACosAABbLAAAKywAAAAAAAArLAAAXCwAACwsAAAAAAAALCwAAF0sAAAtLAAAAAAAAC0sAABeLAAALiwAAAAAAAAuLAAAYCwAAAAAAABhLAAAAAAAAGEsAABgLAAAAAAAAGAsAABiLAAAAAAAAGsCAAAAAAAAYywAAAAAAAB9HQAAAAAAAGQsAAAAAAAAfQIAAAAAAABlLAAAOgIAAAAAAAA6AgAAZiwAAD4CAAAAAAAAPgIAAGcsAAAAAAAAaCwAAAAAAABoLAAAZywAAAAAAABnLAAAaSwAAAAAAABqLAAAAAAAAGosAABpLAAAAAAAAGksAABrLAAAAAAAAGwsAAAAAAAAbCwAAGssAAAAAAAAaywAAG0sAAAAAAAAUQIAAAAAAABuLAAAAAAAAHECAAAAAAAAbywAAAAAAABQAgAAAAAAAHAsAAAAAAAAUgIAAAAAAAByLAAAAAAAAHMsAAAAAAAAcywAAHIsAAAAAAAAciwAAHUsAAAAAAAAdiwAAAAAAAB2LAAAdSwAAAAAAAB1LAAAfiwAAAAAAAA/AgAAAAAAAH8sAAAAAAAAQAIAAAAAAACALAAAAAAAAIEsAAAAAAAAgSwAAIAsAAAAAAAAgCwAAIIsAAAAAAAAgywAAAAAAACDLAAAgiwAAAAAAACCLAAAhCwAAAAAAACFLAAAAAAAAIUsAACELAAAAAAAAIQsAACGLAAAAAAAAIcsAAAAAAAAhywAAIYsAAAAAAAAhiwAAIgsAAAAAAAAiSwAAAAAAACJLAAAiCwAAAAAAACILAAAiiwAAAAAAACLLAAAAAAAAIssAACKLAAAAAAAAIosAACMLAAAAAAAAI0sAAAAAAAAjSwAAIwsAAAAAAAAjCwAAI4sAAAAAAAAjywAAAAAAACPLAAAjiwAAAAAAACOLAAAkCwAAAAAAACRLAAAAAAAAJEsAACQLAAAAAAAAJAsAACSLAAAAAAAAJMsAAAAAAAAkywAAJIsAAAAAAAAkiwAAJQsAAAAAAAAlSwAAAAAAACVLAAAlCwAAAAAAACULAAAliwAAAAAAACXLAAAAAAAAJcsAACWLAAAAAAAAJYsAACYLAAAAAAAAJksAAAAAAAAmSwAAJgsAAAAAAAAmCwAAJosAAAAAAAAmywAAAAAAACbLAAAmiwAAAAAAACaLAAAnCwAAAAAAACdLAAAAAAAAJ0sAACcLAAAAAAAAJwsAACeLAAAAAAAAJ8sAAAAAAAAnywAAJ4sAAAAAAAAniwAAKAsAAAAAAAAoSwAAAAAAAChLAAAoCwAAAAAAACgLAAAoiwAAAAAAACjLAAAAAAAAKMsAACiLAAAAAAAAKIsAACkLAAAAAAAAKUsAAAAAAAApSwAAKQsAAAAAAAApCwAAKYsAAAAAAAApywAAAAAAACnLAAApiwAAAAAAACmLAAAqCwAAAAAAACpLAAAAAAAAKksAACoLAAAAAAAAKgsAACqLAAAAAAAAKssAAAAAAAAqywAAKosAAAAAAAAqiwAAKwsAAAAAAAArSwAAAAAAACtLAAArCwAAAAAAACsLAAAriwAAAAAAACvLAAAAAAAAK8sAACuLAAAAAAAAK4sAACwLAAAAAAAALEsAAAAAAAAsSwAALAsAAAAAAAAsCwAALIsAAAAAAAAsywAAAAAAACzLAAAsiwAAAAAAACyLAAAtCwAAAAAAAC1LAAAAAAAALUsAAC0LAAAAAAAALQsAAC2LAAAAAAAALcsAAAAAAAAtywAALYsAAAAAAAAtiwAALgsAAAAAAAAuSwAAAAAAAC5LAAAuCwAAAAAAAC4LAAAuiwAAAAAAAC7LAAAAAAAALssAAC6LAAAAAAAALosAAC8LAAAAAAAAL0sAAAAAAAAvSwAALwsAAAAAAAAvCwAAL4sAAAAAAAAvywAAAAAAAC/LAAAviwAAAAAAAC+LAAAwCwAAAAAAADBLAAAAAAAAMEsAADALAAAAAAAAMAsAADCLAAAAAAAAMMsAAAAAAAAwywAAMIsAAAAAAAAwiwAAMQsAAAAAAAAxSwAAAAAAADFLAAAxCwAAAAAAADELAAAxiwAAAAAAADHLAAAAAAAAMcsAADGLAAAAAAAAMYsAADILAAAAAAAAMksAAAAAAAAySwAAMgsAAAAAAAAyCwAAMosAAAAAAAAyywAAAAAAADLLAAAyiwAAAAAAADKLAAAzCwAAAAAAADNLAAAAAAAAM0sAADMLAAAAAAAAMwsAADOLAAAAAAAAM8sAAAAAAAAzywAAM4sAAAAAAAAziwAANAsAAAAAAAA0SwAAAAAAADRLAAA0CwAAAAAAADQLAAA0iwAAAAAAADTLAAAAAAAANMsAADSLAAAAAAAANIsAADULAAAAAAAANUsAAAAAAAA1SwAANQsAAAAAAAA1CwAANYsAAAAAAAA1ywAAAAAAADXLAAA1iwAAAAAAADWLAAA2CwAAAAAAADZLAAAAAAAANksAADYLAAAAAAAANgsAADaLAAAAAAAANssAAAAAAAA2ywAANosAAAAAAAA2iwAANwsAAAAAAAA3SwAAAAAAADdLAAA3CwAAAAAAADcLAAA3iwAAAAAAADfLAAAAAAAAN8sAADeLAAAAAAAAN4sAADgLAAAAAAAAOEsAAAAAAAA4SwAAOAsAAAAAAAA4CwAAOIsAAAAAAAA4ywAAAAAAADjLAAA4iwAAAAAAADiLAAA6ywAAAAAAADsLAAAAAAAAOwsAADrLAAAAAAAAOssAADtLAAAAAAAAO4sAAAAAAAA7iwAAO0sAAAAAAAA7SwAAPIsAAAAAAAA8ywAAAAAAADzLAAA8iwAAAAAAADyLAAAAC0AAKAQAAAAAAAAoBAAAAEtAAChEAAAAAAAAKEQAAACLQAAohAAAAAAAACiEAAAAy0AAKMQAAAAAAAAoxAAAAQtAACkEAAAAAAAAKQQAAAFLQAApRAAAAAAAAClEAAABi0AAKYQAAAAAAAAphAAAActAACnEAAAAAAAAKcQAAAILQAAqBAAAAAAAACoEAAACS0AAKkQAAAAAAAAqRAAAAotAACqEAAAAAAAAKoQAAALLQAAqxAAAAAAAACrEAAADC0AAKwQAAAAAAAArBAAAA0tAACtEAAAAAAAAK0QAAAOLQAArhAAAAAAAACuEAAADy0AAK8QAAAAAAAArxAAABAtAACwEAAAAAAAALAQAAARLQAAsRAAAAAAAACxEAAAEi0AALIQAAAAAAAAshAAABMtAACzEAAAAAAAALMQAAAULQAAtBAAAAAAAAC0EAAAFS0AALUQAAAAAAAAtRAAABYtAAC2EAAAAAAAALYQAAAXLQAAtxAAAAAAAAC3EAAAGC0AALgQAAAAAAAAuBAAABktAAC5EAAAAAAAALkQAAAaLQAAuhAAAAAAAAC6EAAAGy0AALsQAAAAAAAAuxAAABwtAAC8EAAAAAAAALwQAAAdLQAAvRAAAAAAAAC9EAAAHi0AAL4QAAAAAAAAvhAAAB8tAAC/EAAAAAAAAL8QAAAgLQAAwBAAAAAAAADAEAAAIS0AAMEQAAAAAAAAwRAAACItAADCEAAAAAAAAMIQAAAjLQAAwxAAAAAAAADDEAAAJC0AAMQQAAAAAAAAxBAAACUtAADFEAAAAAAAAMUQAAAnLQAAxxAAAAAAAADHEAAALS0AAM0QAAAAAAAAzRAAAECmAAAAAAAAQaYAAAAAAABBpgAAQKYAAAAAAABApgAAQqYAAAAAAABDpgAAAAAAAEOmAABCpgAAAAAAAEKmAABEpgAAAAAAAEWmAAAAAAAARaYAAESmAAAAAAAARKYAAEamAAAAAAAAR6YAAAAAAABHpgAARqYAAAAAAABGpgAASKYAAAAAAABJpgAAAAAAAEmmAABIpgAAAAAAAEimAABKpgAAAAAAAEumAAAAAAAAS6YAAEqmAAAAAAAASqYAAEymAAAAAAAATaYAAAAAAABNpgAATKYAAAAAAABMpgAATqYAAAAAAABPpgAAAAAAAE+mAABOpgAAAAAAAE6mAABQpgAAAAAAAFGmAAAAAAAAUaYAAFCmAAAAAAAAUKYAAFKmAAAAAAAAU6YAAAAAAABTpgAAUqYAAAAAAABSpgAAVKYAAAAAAABVpgAAAAAAAFWmAABUpgAAAAAAAFSmAABWpgAAAAAAAFemAAAAAAAAV6YAAFamAAAAAAAAVqYAAFimAAAAAAAAWaYAAAAAAABZpgAAWKYAAAAAAABYpgAAWqYAAAAAAABbpgAAAAAAAFumAABapgAAAAAAAFqmAABcpgAAAAAAAF2mAAAAAAAAXaYAAFymAAAAAAAAXKYAAF6mAAAAAAAAX6YAAAAAAABfpgAAXqYAAAAAAABepgAAYKYAAAAAAABhpgAAAAAAAGGmAABgpgAAAAAAAGCmAABipgAAAAAAAGOmAAAAAAAAY6YAAGKmAAAAAAAAYqYAAGSmAAAAAAAAZaYAAAAAAABlpgAAZKYAAAAAAABkpgAAZqYAAAAAAABnpgAAAAAAAGemAABmpgAAAAAAAGamAABopgAAAAAAAGmmAAAAAAAAaaYAAGimAAAAAAAAaKYAAGqmAAAAAAAAa6YAAAAAAABrpgAAaqYAAAAAAABqpgAAbKYAAAAAAABtpgAAAAAAAG2mAABspgAAAAAAAGymAACApgAAAAAAAIGmAAAAAAAAgaYAAICmAAAAAAAAgKYAAIKmAAAAAAAAg6YAAAAAAACDpgAAgqYAAAAAAACCpgAAhKYAAAAAAACFpgAAAAAAAIWmAACEpgAAAAAAAISmAACGpgAAAAAAAIemAAAAAAAAh6YAAIamAAAAAAAAhqYAAIimAAAAAAAAiaYAAAAAAACJpgAAiKYAAAAAAACIpgAAiqYAAAAAAACLpgAAAAAAAIumAACKpgAAAAAAAIqmAACMpgAAAAAAAI2mAAAAAAAAjaYAAIymAAAAAAAAjKYAAI6mAAAAAAAAj6YAAAAAAACPpgAAjqYAAAAAAACOpgAAkKYAAAAAAACRpgAAAAAAAJGmAACQpgAAAAAAAJCmAACSpgAAAAAAAJOmAAAAAAAAk6YAAJKmAAAAAAAAkqYAAJSmAAAAAAAAlaYAAAAAAACVpgAAlKYAAAAAAACUpgAAlqYAAAAAAACXpgAAAAAAAJemAACWpgAAAAAAAJamAACYpgAAAAAAAJmmAAAAAAAAmaYAAJimAAAAAAAAmKYAAJqmAAAAAAAAm6YAAAAAAACbpgAAmqYAAAAAAACapgAAIqcAAAAAAAAjpwAAAAAAACOnAAAipwAAAAAAACKnAAAkpwAAAAAAACWnAAAAAAAAJacAACSnAAAAAAAAJKcAACanAAAAAAAAJ6cAAAAAAAAnpwAAJqcAAAAAAAAmpwAAKKcAAAAAAAAppwAAAAAAACmnAAAopwAAAAAAACinAAAqpwAAAAAAACunAAAAAAAAK6cAACqnAAAAAAAAKqcAACynAAAAAAAALacAAAAAAAAtpwAALKcAAAAAAAAspwAALqcAAAAAAAAvpwAAAAAAAC+nAAAupwAAAAAAAC6nAAAypwAAAAAAADOnAAAAAAAAM6cAADKnAAAAAAAAMqcAADSnAAAAAAAANacAAAAAAAA1pwAANKcAAAAAAAA0pwAANqcAAAAAAAA3pwAAAAAAADenAAA2pwAAAAAAADanAAA4pwAAAAAAADmnAAAAAAAAOacAADinAAAAAAAAOKcAADqnAAAAAAAAO6cAAAAAAAA7pwAAOqcAAAAAAAA6pwAAPKcAAAAAAAA9pwAAAAAAAD2nAAA8pwAAAAAAADynAAA+pwAAAAAAAD+nAAAAAAAAP6cAAD6nAAAAAAAAPqcAAECnAAAAAAAAQacAAAAAAABBpwAAQKcAAAAAAABApwAAQqcAAAAAAABDpwAAAAAAAEOnAABCpwAAAAAAAEKnAABEpwAAAAAAAEWnAAAAAAAARacAAESnAAAAAAAARKcAAEanAAAAAAAAR6cAAAAAAABHpwAARqcAAAAAAABGpwAASKcAAAAAAABJpwAAAAAAAEmnAABIpwAAAAAAAEinAABKpwAAAAAAAEunAAAAAAAAS6cAAEqnAAAAAAAASqcAAEynAAAAAAAATacAAAAAAABNpwAATKcAAAAAAABMpwAATqcAAAAAAABPpwAAAAAAAE+nAABOpwAAAAAAAE6nAABQpwAAAAAAAFGnAAAAAAAAUacAAFCnAAAAAAAAUKcAAFKnAAAAAAAAU6cAAAAAAABTpwAAUqcAAAAAAABSpwAAVKcAAAAAAABVpwAAAAAAAFWnAABUpwAAAAAAAFSnAABWpwAAAAAAAFenAAAAAAAAV6cAAFanAAAAAAAAVqcAAFinAAAAAAAAWacAAAAAAABZpwAAWKcAAAAAAABYpwAAWqcAAAAAAABbpwAAAAAAAFunAABapwAAAAAAAFqnAABcpwAAAAAAAF2nAAAAAAAAXacAAFynAAAAAAAAXKcAAF6nAAAAAAAAX6cAAAAAAABfpwAAXqcAAAAAAABepwAAYKcAAAAAAABhpwAAAAAAAGGnAABgpwAAAAAAAGCnAABipwAAAAAAAGOnAAAAAAAAY6cAAGKnAAAAAAAAYqcAAGSnAAAAAAAAZacAAAAAAABlpwAAZKcAAAAAAABkpwAAZqcAAAAAAABnpwAAAAAAAGenAABmpwAAAAAAAGanAABopwAAAAAAAGmnAAAAAAAAaacAAGinAAAAAAAAaKcAAGqnAAAAAAAAa6cAAAAAAABrpwAAaqcAAAAAAABqpwAAbKcAAAAAAABtpwAAAAAAAG2nAABspwAAAAAAAGynAABupwAAAAAAAG+nAAAAAAAAb6cAAG6nAAAAAAAAbqcAAHmnAAAAAAAAeqcAAAAAAAB6pwAAeacAAAAAAAB5pwAAe6cAAAAAAAB8pwAAAAAAAHynAAB7pwAAAAAAAHunAAB9pwAAAAAAAHkdAAAAAAAAfqcAAAAAAAB/pwAAAAAAAH+nAAB+pwAAAAAAAH6nAACApwAAAAAAAIGnAAAAAAAAgacAAICnAAAAAAAAgKcAAIKnAAAAAAAAg6cAAAAAAACDpwAAgqcAAAAAAACCpwAAhKcAAAAAAACFpwAAAAAAAIWnAACEpwAAAAAAAISnAACGpwAAAAAAAIenAAAAAAAAh6cAAIanAAAAAAAAhqcAAIunAAAAAAAAjKcAAAAAAACMpwAAi6cAAAAAAACLpwAAjacAAAAAAABlAgAAAAAAAJCnAAAAAAAAkacAAAAAAACRpwAAkKcAAAAAAACQpwAAkqcAAAAAAACTpwAAAAAAAJOnAACSpwAAAAAAAJKnAACWpwAAAAAAAJenAAAAAAAAl6cAAJanAAAAAAAAlqcAAJinAAAAAAAAmacAAAAAAACZpwAAmKcAAAAAAACYpwAAmqcAAAAAAACbpwAAAAAAAJunAACapwAAAAAAAJqnAACcpwAAAAAAAJ2nAAAAAAAAnacAAJynAAAAAAAAnKcAAJ6nAAAAAAAAn6cAAAAAAACfpwAAnqcAAAAAAACepwAAoKcAAAAAAAChpwAAAAAAAKGnAACgpwAAAAAAAKCnAACipwAAAAAAAKOnAAAAAAAAo6cAAKKnAAAAAAAAoqcAAKSnAAAAAAAApacAAAAAAAClpwAApKcAAAAAAACkpwAApqcAAAAAAACnpwAAAAAAAKenAACmpwAAAAAAAKanAACopwAAAAAAAKmnAAAAAAAAqacAAKinAAAAAAAAqKcAAKqnAAAAAAAAZgIAAAAAAACrpwAAAAAAAFwCAAAAAAAArKcAAAAAAABhAgAAAAAAAK2nAAAAAAAAbAIAAAAAAACupwAAAAAAAGoCAAAAAAAAsKcAAAAAAACeAgAAAAAAALGnAAAAAAAAhwIAAAAAAACypwAAAAAAAJ0CAAAAAAAAs6cAAAAAAABTqwAAAAAAALSnAAAAAAAAtacAAAAAAAC1pwAAtKcAAAAAAAC0pwAAtqcAAAAAAAC3pwAAAAAAALenAAC2pwAAAAAAALanAAC4pwAAAAAAALmnAAAAAAAAuacAALinAAAAAAAAuKcAAFOrAACzpwAAAAAAALOnAABwqwAAoBMAAAAAAACgEwAAcasAAKETAAAAAAAAoRMAAHKrAACiEwAAAAAAAKITAABzqwAAoxMAAAAAAACjEwAAdKsAAKQTAAAAAAAApBMAAHWrAAClEwAAAAAAAKUTAAB2qwAAphMAAAAAAACmEwAAd6sAAKcTAAAAAAAApxMAAHirAACoEwAAAAAAAKgTAAB5qwAAqRMAAAAAAACpEwAAeqsAAKoTAAAAAAAAqhMAAHurAACrEwAAAAAAAKsTAAB8qwAArBMAAAAAAACsEwAAfasAAK0TAAAAAAAArRMAAH6rAACuEwAAAAAAAK4TAAB/qwAArxMAAAAAAACvEwAAgKsAALATAAAAAAAAsBMAAIGrAACxEwAAAAAAALETAACCqwAAshMAAAAAAACyEwAAg6sAALMTAAAAAAAAsxMAAISrAAC0EwAAAAAAALQTAACFqwAAtRMAAAAAAAC1EwAAhqsAALYTAAAAAAAAthMAAIerAAC3EwAAAAAAALcTAACIqwAAuBMAAAAAAAC4EwAAiasAALkTAAAAAAAAuRMAAIqrAAC6EwAAAAAAALoTAACLqwAAuxMAAAAAAAC7EwAAjKsAALwTAAAAAAAAvBMAAI2rAAC9EwAAAAAAAL0TAACOqwAAvhMAAAAAAAC+EwAAj6sAAL8TAAAAAAAAvxMAAJCrAADAEwAAAAAAAMATAACRqwAAwRMAAAAAAADBEwAAkqsAAMITAAAAAAAAwhMAAJOrAADDEwAAAAAAAMMTAACUqwAAxBMAAAAAAADEEwAAlasAAMUTAAAAAAAAxRMAAJarAADGEwAAAAAAAMYTAACXqwAAxxMAAAAAAADHEwAAmKsAAMgTAAAAAAAAyBMAAJmrAADJEwAAAAAAAMkTAACaqwAAyhMAAAAAAADKEwAAm6sAAMsTAAAAAAAAyxMAAJyrAADMEwAAAAAAAMwTAACdqwAAzRMAAAAAAADNEwAAnqsAAM4TAAAAAAAAzhMAAJ+rAADPEwAAAAAAAM8TAACgqwAA0BMAAAAAAADQEwAAoasAANETAAAAAAAA0RMAAKKrAADSEwAAAAAAANITAACjqwAA0xMAAAAAAADTEwAApKsAANQTAAAAAAAA1BMAAKWrAADVEwAAAAAAANUTAACmqwAA1hMAAAAAAADWEwAAp6sAANcTAAAAAAAA1xMAAKirAADYEwAAAAAAANgTAACpqwAA2RMAAAAAAADZEwAAqqsAANoTAAAAAAAA2hMAAKurAADbEwAAAAAAANsTAACsqwAA3BMAAAAAAADcEwAArasAAN0TAAAAAAAA3RMAAK6rAADeEwAAAAAAAN4TAACvqwAA3xMAAAAAAADfEwAAsKsAAOATAAAAAAAA4BMAALGrAADhEwAAAAAAAOETAACyqwAA4hMAAAAAAADiEwAAs6sAAOMTAAAAAAAA4xMAALSrAADkEwAAAAAAAOQTAAC1qwAA5RMAAAAAAADlEwAAtqsAAOYTAAAAAAAA5hMAALerAADnEwAAAAAAAOcTAAC4qwAA6BMAAAAAAADoEwAAuasAAOkTAAAAAAAA6RMAALqrAADqEwAAAAAAAOoTAAC7qwAA6xMAAAAAAADrEwAAvKsAAOwTAAAAAAAA7BMAAL2rAADtEwAAAAAAAO0TAAC+qwAA7hMAAAAAAADuEwAAv6sAAO8TAAAAAAAA7xMAACH/AAAAAAAAQf8AAAAAAAAi/wAAAAAAAEL/AAAAAAAAI/8AAAAAAABD/wAAAAAAACT/AAAAAAAARP8AAAAAAAAl/wAAAAAAAEX/AAAAAAAAJv8AAAAAAABG/wAAAAAAACf/AAAAAAAAR/8AAAAAAAAo/wAAAAAAAEj/AAAAAAAAKf8AAAAAAABJ/wAAAAAAACr/AAAAAAAASv8AAAAAAAAr/wAAAAAAAEv/AAAAAAAALP8AAAAAAABM/wAAAAAAAC3/AAAAAAAATf8AAAAAAAAu/wAAAAAAAE7/AAAAAAAAL/8AAAAAAABP/wAAAAAAADD/AAAAAAAAUP8AAAAAAAAx/wAAAAAAAFH/AAAAAAAAMv8AAAAAAABS/wAAAAAAADP/AAAAAAAAU/8AAAAAAAA0/wAAAAAAAFT/AAAAAAAANf8AAAAAAABV/wAAAAAAADb/AAAAAAAAVv8AAAAAAAA3/wAAAAAAAFf/AAAAAAAAOP8AAAAAAABY/wAAAAAAADn/AAAAAAAAWf8AAAAAAAA6/wAAAAAAAFr/AAAAAAAAQf8AACH/AAAAAAAAIf8AAEL/AAAi/wAAAAAAACL/AABD/wAAI/8AAAAAAAAj/wAARP8AACT/AAAAAAAAJP8AAEX/AAAl/wAAAAAAACX/AABG/wAAJv8AAAAAAAAm/wAAR/8AACf/AAAAAAAAJ/8AAEj/AAAo/wAAAAAAACj/AABJ/wAAKf8AAAAAAAAp/wAASv8AACr/AAAAAAAAKv8AAEv/AAAr/wAAAAAAACv/AABM/wAALP8AAAAAAAAs/wAATf8AAC3/AAAAAAAALf8AAE7/AAAu/wAAAAAAAC7/AABP/wAAL/8AAAAAAAAv/wAAUP8AADD/AAAAAAAAMP8AAFH/AAAx/wAAAAAAADH/AABS/wAAMv8AAAAAAAAy/wAAU/8AADP/AAAAAAAAM/8AAFT/AAA0/wAAAAAAADT/AABV/wAANf8AAAAAAAA1/wAAVv8AADb/AAAAAAAANv8AAFf/AAA3/wAAAAAAADf/AABY/wAAOP8AAAAAAAA4/wAAWf8AADn/AAAAAAAAOf8AAFr/AAA6/wAAAAAAADr/AAAABAEAAAAAACgEAQAAAAAAAQQBAAAAAAApBAEAAAAAAAIEAQAAAAAAKgQBAAAAAAADBAEAAAAAACsEAQAAAAAABAQBAAAAAAAsBAEAAAAAAAUEAQAAAAAALQQBAAAAAAAGBAEAAAAAAC4EAQAAAAAABwQBAAAAAAAvBAEAAAAAAAgEAQAAAAAAMAQBAAAAAAAJBAEAAAAAADEEAQAAAAAACgQBAAAAAAAyBAEAAAAAAAsEAQAAAAAAMwQBAAAAAAAMBAEAAAAAADQEAQAAAAAADQQBAAAAAAA1BAEAAAAAAA4EAQAAAAAANgQBAAAAAAAPBAEAAAAAADcEAQAAAAAAEAQBAAAAAAA4BAEAAAAAABEEAQAAAAAAOQQBAAAAAAASBAEAAAAAADoEAQAAAAAAEwQBAAAAAAA7BAEAAAAAABQEAQAAAAAAPAQBAAAAAAAVBAEAAAAAAD0EAQAAAAAAFgQBAAAAAAA+BAEAAAAAABcEAQAAAAAAPwQBAAAAAAAYBAEAAAAAAEAEAQAAAAAAGQQBAAAAAABBBAEAAAAAABoEAQAAAAAAQgQBAAAAAAAbBAEAAAAAAEMEAQAAAAAAHAQBAAAAAABEBAEAAAAAAB0EAQAAAAAARQQBAAAAAAAeBAEAAAAAAEYEAQAAAAAAHwQBAAAAAABHBAEAAAAAACAEAQAAAAAASAQBAAAAAAAhBAEAAAAAAEkEAQAAAAAAIgQBAAAAAABKBAEAAAAAACMEAQAAAAAASwQBAAAAAAAkBAEAAAAAAEwEAQAAAAAAJQQBAAAAAABNBAEAAAAAACYEAQAAAAAATgQBAAAAAAAnBAEAAAAAAE8EAQAAAAAAKAQBAAAEAQAAAAAAAAQBACkEAQABBAEAAAAAAAEEAQAqBAEAAgQBAAAAAAACBAEAKwQBAAMEAQAAAAAAAwQBACwEAQAEBAEAAAAAAAQEAQAtBAEABQQBAAAAAAAFBAEALgQBAAYEAQAAAAAABgQBAC8EAQAHBAEAAAAAAAcEAQAwBAEACAQBAAAAAAAIBAEAMQQBAAkEAQAAAAAACQQBADIEAQAKBAEAAAAAAAoEAQAzBAEACwQBAAAAAAALBAEANAQBAAwEAQAAAAAADAQBADUEAQANBAEAAAAAAA0EAQA2BAEADgQBAAAAAAAOBAEANwQBAA8EAQAAAAAADwQBADgEAQAQBAEAAAAAABAEAQA5BAEAEQQBAAAAAAARBAEAOgQBABIEAQAAAAAAEgQBADsEAQATBAEAAAAAABMEAQA8BAEAFAQBAAAAAAAUBAEAPQQBABUEAQAAAAAAFQQBAD4EAQAWBAEAAAAAABYEAQA/BAEAFwQBAAAAAAAXBAEAQAQBABgEAQAAAAAAGAQBAEEEAQAZBAEAAAAAABkEAQBCBAEAGgQBAAAAAAAaBAEAQwQBABsEAQAAAAAAGwQBAEQEAQAcBAEAAAAAABwEAQBFBAEAHQQBAAAAAAAdBAEARgQBAB4EAQAAAAAAHgQBAEcEAQAfBAEAAAAAAB8EAQBIBAEAIAQBAAAAAAAgBAEASQQBACEEAQAAAAAAIQQBAEoEAQAiBAEAAAAAACIEAQBLBAEAIwQBAAAAAAAjBAEATAQBACQEAQAAAAAAJAQBAE0EAQAlBAEAAAAAACUEAQBOBAEAJgQBAAAAAAAmBAEATwQBACcEAQAAAAAAJwQBALAEAQAAAAAA2AQBAAAAAACxBAEAAAAAANkEAQAAAAAAsgQBAAAAAADaBAEAAAAAALMEAQAAAAAA2wQBAAAAAAC0BAEAAAAAANwEAQAAAAAAtQQBAAAAAADdBAEAAAAAALYEAQAAAAAA3gQBAAAAAAC3BAEAAAAAAN8EAQAAAAAAuAQBAAAAAADgBAEAAAAAALkEAQAAAAAA4QQBAAAAAAC6BAEAAAAAAOIEAQAAAAAAuwQBAAAAAADjBAEAAAAAALwEAQAAAAAA5AQBAAAAAAC9BAEAAAAAAOUEAQAAAAAAvgQBAAAAAADmBAEAAAAAAL8EAQAAAAAA5wQBAAAAAADABAEAAAAAAOgEAQAAAAAAwQQBAAAAAADpBAEAAAAAAMIEAQAAAAAA6gQBAAAAAADDBAEAAAAAAOsEAQAAAAAAxAQBAAAAAADsBAEAAAAAAMUEAQAAAAAA7QQBAAAAAADGBAEAAAAAAO4EAQAAAAAAxwQBAAAAAADvBAEAAAAAAMgEAQAAAAAA8AQBAAAAAADJBAEAAAAAAPEEAQAAAAAAygQBAAAAAADyBAEAAAAAAMsEAQAAAAAA8wQBAAAAAADMBAEAAAAAAPQEAQAAAAAAzQQBAAAAAAD1BAEAAAAAAM4EAQAAAAAA9gQBAAAAAADPBAEAAAAAAPcEAQAAAAAA0AQBAAAAAAD4BAEAAAAAANEEAQAAAAAA+QQBAAAAAADSBAEAAAAAAPoEAQAAAAAA0wQBAAAAAAD7BAEAAAAAANgEAQCwBAEAAAAAALAEAQDZBAEAsQQBAAAAAACxBAEA2gQBALIEAQAAAAAAsgQBANsEAQCzBAEAAAAAALMEAQDcBAEAtAQBAAAAAAC0BAEA3QQBALUEAQAAAAAAtQQBAN4EAQC2BAEAAAAAALYEAQDfBAEAtwQBAAAAAAC3BAEA4AQBALgEAQAAAAAAuAQBAOEEAQC5BAEAAAAAALkEAQDiBAEAugQBAAAAAAC6BAEA4wQBALsEAQAAAAAAuwQBAOQEAQC8BAEAAAAAALwEAQDlBAEAvQQBAAAAAAC9BAEA5gQBAL4EAQAAAAAAvgQBAOcEAQC/BAEAAAAAAL8EAQDoBAEAwAQBAAAAAADABAEA6QQBAMEEAQAAAAAAwQQBAOoEAQDCBAEAAAAAAMIEAQDrBAEAwwQBAAAAAADDBAEA7AQBAMQEAQAAAAAAxAQBAO0EAQDFBAEAAAAAAMUEAQDuBAEAxgQBAAAAAADGBAEA7wQBAMcEAQAAAAAAxwQBAPAEAQDIBAEAAAAAAMgEAQDxBAEAyQQBAAAAAADJBAEA8gQBAMoEAQAAAAAAygQBAPMEAQDLBAEAAAAAAMsEAQD0BAEAzAQBAAAAAADMBAEA9QQBAM0EAQAAAAAAzQQBAPYEAQDOBAEAAAAAAM4EAQD3BAEAzwQBAAAAAADPBAEA+AQBANAEAQAAAAAA0AQBAPkEAQDRBAEAAAAAANEEAQD6BAEA0gQBAAAAAADSBAEA+wQBANMEAQAAAAAA0wQBAIAMAQAAAAAAwAwBAAAAAACBDAEAAAAAAMEMAQAAAAAAggwBAAAAAADCDAEAAAAAAIMMAQAAAAAAwwwBAAAAAACEDAEAAAAAAMQMAQAAAAAAhQwBAAAAAADFDAEAAAAAAIYMAQAAAAAAxgwBAAAAAACHDAEAAAAAAMcMAQAAAAAAiAwBAAAAAADIDAEAAAAAAIkMAQAAAAAAyQwBAAAAAACKDAEAAAAAAMoMAQAAAAAAiwwBAAAAAADLDAEAAAAAAIwMAQAAAAAAzAwBAAAAAACNDAEAAAAAAM0MAQAAAAAAjgwBAAAAAADODAEAAAAAAI8MAQAAAAAAzwwBAAAAAACQDAEAAAAAANAMAQAAAAAAkQwBAAAAAADRDAEAAAAAAJIMAQAAAAAA0gwBAAAAAACTDAEAAAAAANMMAQAAAAAAlAwBAAAAAADUDAEAAAAAAJUMAQAAAAAA1QwBAAAAAACWDAEAAAAAANYMAQAAAAAAlwwBAAAAAADXDAEAAAAAAJgMAQAAAAAA2AwBAAAAAACZDAEAAAAAANkMAQAAAAAAmgwBAAAAAADaDAEAAAAAAJsMAQAAAAAA2wwBAAAAAACcDAEAAAAAANwMAQAAAAAAnQwBAAAAAADdDAEAAAAAAJ4MAQAAAAAA3gwBAAAAAACfDAEAAAAAAN8MAQAAAAAAoAwBAAAAAADgDAEAAAAAAKEMAQAAAAAA4QwBAAAAAACiDAEAAAAAAOIMAQAAAAAAowwBAAAAAADjDAEAAAAAAKQMAQAAAAAA5AwBAAAAAAClDAEAAAAAAOUMAQAAAAAApgwBAAAAAADmDAEAAAAAAKcMAQAAAAAA5wwBAAAAAACoDAEAAAAAAOgMAQAAAAAAqQwBAAAAAADpDAEAAAAAAKoMAQAAAAAA6gwBAAAAAACrDAEAAAAAAOsMAQAAAAAArAwBAAAAAADsDAEAAAAAAK0MAQAAAAAA7QwBAAAAAACuDAEAAAAAAO4MAQAAAAAArwwBAAAAAADvDAEAAAAAALAMAQAAAAAA8AwBAAAAAACxDAEAAAAAAPEMAQAAAAAAsgwBAAAAAADyDAEAAAAAAMAMAQCADAEAAAAAAIAMAQDBDAEAgQwBAAAAAACBDAEAwgwBAIIMAQAAAAAAggwBAMMMAQCDDAEAAAAAAIMMAQDEDAEAhAwBAAAAAACEDAEAxQwBAIUMAQAAAAAAhQwBAMYMAQCGDAEAAAAAAIYMAQDHDAEAhwwBAAAAAACHDAEAyAwBAIgMAQAAAAAAiAwBAMkMAQCJDAEAAAAAAIkMAQDKDAEAigwBAAAAAACKDAEAywwBAIsMAQAAAAAAiwwBAMwMAQCMDAEAAAAAAIwMAQDNDAEAjQwBAAAAAACNDAEAzgwBAI4MAQAAAAAAjgwBAM8MAQCPDAEAAAAAAI8MAQDQDAEAkAwBAAAAAACQDAEA0QwBAJEMAQAAAAAAkQwBANIMAQCSDAEAAAAAAJIMAQDTDAEAkwwBAAAAAACTDAEA1AwBAJQMAQAAAAAAlAwBANUMAQCVDAEAAAAAAJUMAQDWDAEAlgwBAAAAAACWDAEA1wwBAJcMAQAAAAAAlwwBANgMAQCYDAEAAAAAAJgMAQDZDAEAmQwBAAAAAACZDAEA2gwBAJoMAQAAAAAAmgwBANsMAQCbDAEAAAAAAJsMAQDcDAEAnAwBAAAAAACcDAEA3QwBAJ0MAQAAAAAAnQwBAN4MAQCeDAEAAAAAAJ4MAQDfDAEAnwwBAAAAAACfDAEA4AwBAKAMAQAAAAAAoAwBAOEMAQChDAEAAAAAAKEMAQDiDAEAogwBAAAAAACiDAEA4wwBAKMMAQAAAAAAowwBAOQMAQCkDAEAAAAAAKQMAQDlDAEApQwBAAAAAAClDAEA5gwBAKYMAQAAAAAApgwBAOcMAQCnDAEAAAAAAKcMAQDoDAEAqAwBAAAAAACoDAEA6QwBAKkMAQAAAAAAqQwBAOoMAQCqDAEAAAAAAKoMAQDrDAEAqwwBAAAAAACrDAEA7AwBAKwMAQAAAAAArAwBAO0MAQCtDAEAAAAAAK0MAQDuDAEArgwBAAAAAACuDAEA7wwBAK8MAQAAAAAArwwBAPAMAQCwDAEAAAAAALAMAQDxDAEAsQwBAAAAAACxDAEA8gwBALIMAQAAAAAAsgwBAKAYAQAAAAAAwBgBAAAAAAChGAEAAAAAAMEYAQAAAAAAohgBAAAAAADCGAEAAAAAAKMYAQAAAAAAwxgBAAAAAACkGAEAAAAAAMQYAQAAAAAApRgBAAAAAADFGAEAAAAAAKYYAQAAAAAAxhgBAAAAAACnGAEAAAAAAMcYAQAAAAAAqBgBAAAAAADIGAEAAAAAAKkYAQAAAAAAyRgBAAAAAACqGAEAAAAAAMoYAQAAAAAAqxgBAAAAAADLGAEAAAAAAKwYAQAAAAAAzBgBAAAAAACtGAEAAAAAAM0YAQAAAAAArhgBAAAAAADOGAEAAAAAAK8YAQAAAAAAzxgBAAAAAACwGAEAAAAAANAYAQAAAAAAsRgBAAAAAADRGAEAAAAAALIYAQAAAAAA0hgBAAAAAACzGAEAAAAAANMYAQAAAAAAtBgBAAAAAADUGAEAAAAAALUYAQAAAAAA1RgBAAAAAAC2GAEAAAAAANYYAQAAAAAAtxgBAAAAAADXGAEAAAAAALgYAQAAAAAA2BgBAAAAAAC5GAEAAAAAANkYAQAAAAAAuhgBAAAAAADaGAEAAAAAALsYAQAAAAAA2xgBAAAAAAC8GAEAAAAAANwYAQAAAAAAvRgBAAAAAADdGAEAAAAAAL4YAQAAAAAA3hgBAAAAAAC/GAEAAAAAAN8YAQAAAAAAwBgBAKAYAQAAAAAAoBgBAMEYAQChGAEAAAAAAKEYAQDCGAEAohgBAAAAAACiGAEAwxgBAKMYAQAAAAAAoxgBAMQYAQCkGAEAAAAAAKQYAQDFGAEApRgBAAAAAAClGAEAxhgBAKYYAQAAAAAAphgBAMcYAQCnGAEAAAAAAKcYAQDIGAEAqBgBAAAAAACoGAEAyRgBAKkYAQAAAAAAqRgBAMoYAQCqGAEAAAAAAKoYAQDLGAEAqxgBAAAAAACrGAEAzBgBAKwYAQAAAAAArBgBAM0YAQCtGAEAAAAAAK0YAQDOGAEArhgBAAAAAACuGAEAzxgBAK8YAQAAAAAArxgBANAYAQCwGAEAAAAAALAYAQDRGAEAsRgBAAAAAACxGAEA0hgBALIYAQAAAAAAshgBANMYAQCzGAEAAAAAALMYAQDUGAEAtBgBAAAAAAC0GAEA1RgBALUYAQAAAAAAtRgBANYYAQC2GAEAAAAAALYYAQDXGAEAtxgBAAAAAAC3GAEA2BgBALgYAQAAAAAAuBgBANkYAQC5GAEAAAAAALkYAQDaGAEAuhgBAAAAAAC6GAEA2xgBALsYAQAAAAAAuxgBANwYAQC8GAEAAAAAALwYAQDdGAEAvRgBAAAAAAC9GAEA3hgBAL4YAQAAAAAAvhgBAN8YAQC/GAEAAAAAAL8YAQBAbgEAAAAAAGBuAQAAAAAAQW4BAAAAAABhbgEAAAAAAEJuAQAAAAAAYm4BAAAAAABDbgEAAAAAAGNuAQAAAAAARG4BAAAAAABkbgEAAAAAAEVuAQAAAAAAZW4BAAAAAABGbgEAAAAAAGZuAQAAAAAAR24BAAAAAABnbgEAAAAAAEhuAQAAAAAAaG4BAAAAAABJbgEAAAAAAGluAQAAAAAASm4BAAAAAABqbgEAAAAAAEtuAQAAAAAAa24BAAAAAABMbgEAAAAAAGxuAQAAAAAATW4BAAAAAABtbgEAAAAAAE5uAQAAAAAAbm4BAAAAAABPbgEAAAAAAG9uAQAAAAAAUG4BAAAAAABwbgEAAAAAAFFuAQAAAAAAcW4BAAAAAABSbgEAAAAAAHJuAQAAAAAAU24BAAAAAABzbgEAAAAAAFRuAQAAAAAAdG4BAAAAAABVbgEAAAAAAHVuAQAAAAAAVm4BAAAAAAB2bgEAAAAAAFduAQAAAAAAd24BAAAAAABYbgEAAAAAAHhuAQAAAAAAWW4BAAAAAAB5bgEAAAAAAFpuAQAAAAAAem4BAAAAAABbbgEAAAAAAHtuAQAAAAAAXG4BAAAAAAB8bgEAAAAAAF1uAQAAAAAAfW4BAAAAAABebgEAAAAAAH5uAQAAAAAAX24BAAAAAAB/bgEAAAAAAGBuAQBAbgEAAAAAAEBuAQBhbgEAQW4BAAAAAABBbgEAYm4BAEJuAQAAAAAAQm4BAGNuAQBDbgEAAAAAAENuAQBkbgEARG4BAAAAAABEbgEAZW4BAEVuAQAAAAAARW4BAGZuAQBGbgEAAAAAAEZuAQBnbgEAR24BAAAAAABHbgEAaG4BAEhuAQAAAAAASG4BAGluAQBJbgEAAAAAAEluAQBqbgEASm4BAAAAAABKbgEAa24BAEtuAQAAAAAAS24BAGxuAQBMbgEAAAAAAExuAQBtbgEATW4BAAAAAABNbgEAbm4BAE5uAQAAAAAATm4BAG9uAQBPbgEAAAAAAE9uAQBwbgEAUG4BAAAAAABQbgEAcW4BAFFuAQAAAAAAUW4BAHJuAQBSbgEAAAAAAFJuAQBzbgEAU24BAAAAAABTbgEAdG4BAFRuAQAAAAAAVG4BAHVuAQBVbgEAAAAAAFVuAQB2bgEAVm4BAAAAAABWbgEAd24BAFduAQAAAAAAV24BAHhuAQBYbgEAAAAAAFhuAQB5bgEAWW4BAAAAAABZbgEAem4BAFpuAQAAAAAAWm4BAHtuAQBbbgEAAAAAAFtuAQB8bgEAXG4BAAAAAABcbgEAfW4BAF1uAQAAAAAAXW4BAH5uAQBebgEAAAAAAF5uAQB/bgEAX24BAAAAAABfbgEAAOkBAAAAAAAi6QEAAAAAAAHpAQAAAAAAI+kBAAAAAAAC6QEAAAAAACTpAQAAAAAAA+kBAAAAAAAl6QEAAAAAAATpAQAAAAAAJukBAAAAAAAF6QEAAAAAACfpAQAAAAAABukBAAAAAAAo6QEAAAAAAAfpAQAAAAAAKekBAAAAAAAI6QEAAAAAACrpAQAAAAAACekBAAAAAAAr6QEAAAAAAArpAQAAAAAALOkBAAAAAAAL6QEAAAAAAC3pAQAAAAAADOkBAAAAAAAu6QEAAAAAAA3pAQAAAAAAL+kBAAAAAAAO6QEAAAAAADDpAQAAAAAAD+kBAAAAAAAx6QEAAAAAABDpAQAAAAAAMukBAAAAAAAR6QEAAAAAADPpAQAAAAAAEukBAAAAAAA06QEAAAAAABPpAQAAAAAANekBAAAAAAAU6QEAAAAAADbpAQAAAAAAFekBAAAAAAA36QEAAAAAABbpAQAAAAAAOOkBAAAAAAAX6QEAAAAAADnpAQAAAAAAGOkBAAAAAAA66QEAAAAAABnpAQAAAAAAO+kBAAAAAAAa6QEAAAAAADzpAQAAAAAAG+kBAAAAAAA96QEAAAAAABzpAQAAAAAAPukBAAAAAAAd6QEAAAAAAD/pAQAAAAAAHukBAAAAAABA6QEAAAAAAB/pAQAAAAAAQekBAAAAAAAg6QEAAAAAAELpAQAAAAAAIekBAAAAAABD6QEAAAAAACLpAQAA6QEAAAAAAADpAQAj6QEAAekBAAAAAAAB6QEAJOkBAALpAQAAAAAAAukBACXpAQAD6QEAAAAAAAPpAQAm6QEABOkBAAAAAAAE6QEAJ+kBAAXpAQAAAAAABekBACjpAQAG6QEAAAAAAAbpAQAp6QEAB+kBAAAAAAAH6QEAKukBAAjpAQAAAAAACOkBACvpAQAJ6QEAAAAAAAnpAQAs6QEACukBAAAAAAAK6QEALekBAAvpAQAAAAAAC+kBAC7pAQAM6QEAAAAAAAzpAQAv6QEADekBAAAAAAAN6QEAMOkBAA7pAQAAAAAADukBADHpAQAP6QEAAAAAAA/pAQAy6QEAEOkBAAAAAAAQ6QEAM+kBABHpAQAAAAAAEekBADTpAQAS6QEAAAAAABLpAQA16QEAE+kBAAAAAAAT6QEANukBABTpAQAAAAAAFOkBADfpAQAV6QEAAAAAABXpAQA46QEAFukBAAAAAAAW6QEAOekBABfpAQAAAAAAF+kBADrpAQAY6QEAAAAAABjpAQA76QEAGekBAAAAAAAZ6QEAPOkBABrpAQAAAAAAGukBAD3pAQAb6QEAAAAAABvpAQA+6QEAHOkBAAAAAAAc6QEAP+kBAB3pAQAAAAAAHekBAEDpAQAe6QEAAAAAAB7pAQBB6QEAH+kBAAAAAAAf6QEAQukBACDpAQAAAAAAIOkBAEPpAQAh6QEAAAAAACHpAQ=="),C(e,45536,"HhYWFhgWFhYXExYaFhIWFg4ODg4ODg4ODg4WFhoaGhYWCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoXFhMZERkGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBhcaExo="),C(e,45664,"HhYYGBgYGxYZGwgVGgEbGRsaEBAZBhYWGRAIFBAQEBYKCgoKCgoKCgoKCgoKCgoKCgoKCgoKChoKCgoKCgoKBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGGgYGBgYGBgYGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYGCgYKBgoGCgYKBgoGCgYKBgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgoGCgYKBgYGCgoGCgYKCgYKCgoGBgoKCgoGCgoGCgoKBgYGCgoGCgoGCgYKBgoKBgoGBgoGCgoGCgoKBgoGCgoGBggKBgYGCAgICAoJBgoJBgoJBgoGCgYKBgoGCgYKBgoGCgYGCgYKBgoGCgYKBgoGCgYKBgoGBgoJBgoGCgoKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYGBgYGBgYKCgYKCgYGCgYKCgoKBgoGCgYKBgoGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYIBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBwcHBwcHBwcHBwcHBwcHBwcHGRkZGQcHBwcHBwcHBwcHBxkZGRkZGRkZGRkZGRkZBwcHBwcZGRkZGRkZBxkHGRkZGRkZGRkZGRkZGRkZGRkNDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NCgYKBgcZCgYCAgcGBgYWCgICAgIZGQoWCgoKAgoCCgoGCgoKCgoKCgoKCgoKCgoKCgoCCgoKCgoKCgoKBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYKBgYKCgoGBgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYGBgYGCgYaCgYKCgYGCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBhsNDQ0NDQwMCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgoGCgYKBgoGCgYKBgoGBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgIKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgICBxYWFhYWFgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGFhICAhsbGAINDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0SDRYNDRYNDRYNAgICAgICAgIICAgICAgICAgICAgICAgICAgICAgICAgICAgCAgICCAgICBYWAgICAgICAgICAgIBAQEBAQEaGhoWFhgWFhsbDQ0NDQ0NDQ0NDQ0WAQIWFggICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIBwgICAgICAgICAgNDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0ODg4ODg4ODg4OFhYWFggIDQgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBYIDQ0NDQ0NDQEbDQ0NDQ0NBwcNDRsNDQ0NCAgODg4ODg4ODg4OCAgIGxsIFhYWFhYWFhYWFhYWFhYCAQgNCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NAgIICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICA0NDQ0NDQ0NDQ0NCAICAgICAgICAgICAgICDg4ODg4ODg4ODggICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICA0NDQ0NDQ0NDQcHGxYWFgcCAg0YGAgICAgICAgICAgICAgICAgICAgICAgNDQ0NBw0NDQ0NDQ0NDQcNDQ0HDQ0NDQ0CAhYWFhYWFhYWFhYWFhYWFgIICAgICAgICAgICAgICAgICAgICAgICAgIDQ0NAgIWAggICAgICAgICAgIAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIICAgICAgICAgICAgICAgICAgICAgCCAgICAgICAgCAgICAgICAgICAgICAgICAgICAgINDQ0NDQ0NDQ0NDQ0NDQ0BDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0LCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDQsNCAsLCw0NDQ0NDQ0NCwsLCw0LCwgNDQ0NDQ0NCAgICAgICAgICA0NFhYODg4ODg4ODg4OFgcICAgICAgICAgICAgICAgNCwsCCAgICAgICAgCAggIAgIICAgICAgICAgICAgICAgICAgICAgIAggICAgICAgCCAICAggICAgCAg0ICwsLDQ0NDQICCwsCAgsLDQgCAgICAgICAgsCAgICCAgCCAgIDQ0CAg4ODg4ODg4ODg4ICBgYEBAQEBAQGxgIFg0CAg0NCwIICAgICAgCAgICCAgCAggICAgICAgICAgICAgICAgICAgICAgCCAgICAgICAIICAIICAIICAICDQILCwsNDQICAgINDQICDQ0NAgICDQICAgICAgIICAgIAggCAgICAgICDg4ODg4ODg4ODg0NCAgIDRYCAgICAgICAgICDQ0LAggICAgICAgICAIICAgCCAgICAgICAgICAgICAgICAgICAgICAIICAgICAgIAggIAggICAgIAgINCAsLCw0NDQ0NAg0NCwILCw0CAggCAgICAgICAgICAgICAgIICA0NAgIODg4ODg4ODg4OFhgCAgICAgICCA0NDQ0NDQINCwsCCAgICAgICAgCAggIAgIICAgICAgICAgICAgICAgICAgICAgIAggICAgICAgCCAgCCAgICAgCAg0ICw0LDQ0NDQICCwsCAgsLDQICAgICAgICDQsCAgICCAgCCAgIDQ0CAg4ODg4ODg4ODg4bCBAQEBAQEAICAgICAgICAgINCAIICAgICAgCAgIICAgCCAgICAICAggIAggCCAgCAgIICAICAggICAICAggICAgICAgICAgICAICAgILCw0LCwICAgsLCwILCwsNAgIIAgICAgICCwICAgICAgICAgICAgICDg4ODg4ODg4ODhAQEBsbGxsbGxgbAgICAgINCwsLDQgICAgICAgIAggICAIICAgICAgICAgICAgICAgICAgICAgICAIICAgICAgICAgICAgICAgIAgICCA0NDQsLCwsCDQ0NAg0NDQ0CAgICAgICDQ0CCAgIAgICAgIICA0NAgIODg4ODg4ODg4OAgICAgICAgIQEBAQEBAQGwgNCwsWCAgICAgICAgCCAgIAggICAgICAgICAgICAgICAgICAgICAgIAggICAgICAgICAgCCAgICAgCAg0ICw0LCwsLCwINCwsCCwsNDQICAgICAgILCwICAgICAgIIAggIDQ0CAg4ODg4ODg4ODg4CCAgCAgICAgICAgICAgICDQ0LCwIICAgICAgICAIICAgCCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgNDQgLCwsNDQ0NAgsLCwILCwsNCBsCAgICCAgICxAQEBAQEBAICAgNDQICDg4ODg4ODg4ODhAQEBAQEBAQEBsICAgICAgCAgsLAggICAgICAgICAgICAgICAgICAICAggICAgICAgICAgICAgICAgICAgICAgICAIICAgICAgICAgCCAICCAgICAgICAICAg0CAgICCwsLDQ0NAg0CCwsLCwsLCwsCAgICAgIODg4ODg4ODg4OAgILCxYCAgICAgICAgICAgIICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgNCAgNDQ0NDQ0NAgICAhgICAgICAgHDQ0NDQ0NDQ0WDg4ODg4ODg4ODhYWAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAggIAggCAggIAggCAggCAgICAgIICAgIAggICAgICAgCCAgIAggCCAICCAgCCAgICA0ICA0NDQ0NDQINDQgCAggICAgIAgcCDQ0NDQ0NAgIODg4ODg4ODg4OAgIICAgIAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIIGxsbFhYWFhYWFhYWFhYWFhYWGxYbGxsNDRsbGxsbGw4ODg4ODg4ODg4QEBAQEBAQEBAQGw0bDRsNFxMXEwsLCAgICAgICAgCCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAgICAg0NDQ0NDQ0NDQ0NDQ0NCw0NDQ0NFg0NCAgICAgNDQ0NDQ0NDQ0NDQINDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0CGxsbGxsbGxsNGxsbGxsbAhsbFhYWFhYbGxsbFhYCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAsLDQ0NDQsNDQ0NDQ0LDQ0LCw0NCA4ODg4ODg4ODg4WFhYWFhYICAgICAgLCw0NCAgICA0NDQgLCwsICAsLCwsLCwsICAgNDQ0NCAgICAgICAgICAgICA0LCw0NCwsLCwsLDQgLDg4ODg4ODg4ODgsLCw0bGwoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKAgoCAgICAgoCAgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYWBwYGBggICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAggICAgCAggICAgICAgCCAIICAgIAgIICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAIICAgIAgIICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCCAgICAICCAgICAgICAIIAggICAgCAggICAgICAgICAgICAgICAIICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCCAgICAICCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAICDQ0NFhYWFhYWFhYWEBAQEBAQEBAQEBAQEBAQEBAQEBACAgIICAgICAgICAgICAgICAgIGxsbGxsbGxsbGwICAgICAgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKAgIGBgYGBgYCAhIICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIFhYICAgICAgICAgICAgICAgICB4ICAgICAgICAgICAgICAgICAgICAgICAgICBcTAgICCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIFhYWDw8PCAgICAgICAgCAgICAgICCAgICAgICAgICAgICAIICAgIDQ0NAgICAgICAgICAgIICAgICAgICAgICAgICAgICAgNDQ0WFgICAgICAgICAggICAgICAgICAgICAgICAgICA0NAgICAgICAgICAgICCAgICAgICAgICAgICAIICAgCDQ0CAgICAgICAgICAgIICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDQ0LDQ0NDQ0NDQsLCwsLCwsLDQsLDQ0NDQ0NDQ0NDQ0WFhYHFhYWGAgNAgIODg4ODg4ODg4OAgICAgICEBAQEBAQEBAQEAICAgICAhYWFhYWFhIWFhYWDQ0NAQIODg4ODg4ODg4OAgICAgICCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgHCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCAgICAgICCAgICAgNDQgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgNCAICAgICCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAICAgICAgICAgIICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAg0NDQsLCwsNDQsLCwICAgILCw0LCwsLCwsNDQ0CAgICGwICAhYWDg4ODg4ODg4ODggICAgICAgICAgICAgICAgICAgICAgICAgICAgICAICCAgICAgCAgICAgICAgICAggICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAgICAggICAgICAgICAgICAgICAgICAgICAgICAgIAgICAgICDg4ODg4ODg4ODhACAgIbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbCAgICAgICAgICAgICAgICAgICAgICAgNDQsLDQICFhYICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAsNCw0NDQ0NDQ0CDQsNCwsNDQ0NDQ0NDQsLCwsLCw0NDQ0NDQ0NDQ0CAg0ODg4ODg4ODg4OAgICAgICDg4ODg4ODg4ODgICAgICAhYWFhYWFhYHFhYWFhYWAgINDQ0NDQ0NDQ0NDQ0NDQwCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg0NDQ0LCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgNCw0NDQ0NCw0LCwsLCw0LCwgICAgICAgCAgICDg4ODg4ODg4ODhYWFhYWFhYbGxsbGxsbGxsbDQ0NDQ0NDQ0NGxsbGxsbGxsbAgICDQ0LCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICw0NDQ0LCw0NCw0NDQgIDg4ODg4ODg4ODggICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDQsNDQsLCw0LDQ0NCwsCAgICAgICAhYWFhYICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgLCwsLCwsLCw0NDQ0NDQ0NCwsNDQICAhYWFhYWDg4ODg4ODg4ODgICAggICA4ODg4ODg4ODg4ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgHBwcHBwcWFgYGBgYGBgYGBgICAgICAgIKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKAgIKCgoWFhYWFhYWFgICAgICAgICDQ0NFg0NDQ0NDQ0NDQ0NDQ0LDQ0NDQ0NDQgICAgNCAgICAsLDQgICw0NAgICAgICBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcGBgYGBgYGBgYGBgYGBwYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQINDQ0NDQoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgYGBgYGBgYGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYGBgYGBgYGBgoKCgoKCgoKBgYGBgYGAgIKCgoKCgoCAgYGBgYGBgYGCgoKCgoKCgoGBgYGBgYGBgoKCgoKCgoKBgYGBgYGAgIKCgoKCgoCAgYGBgYGBgYGAgoCCgIKAgoGBgYGBgYGBgoKCgoKCgoKBgYGBgYGBgYGBgYGBgYCAgYGBgYGBgYGCQkJCQkJCQkGBgYGBgYGBgkJCQkJCQkJBgYGBgYGBgYJCQkJCQkJCQYGBgYGAgYGCgoKCgkZBhkZGQYGBgIGBgoKCgoJGRkZBgYGBgICBgYKCgoKAhkZGQYGBgYGBgYGCgoKCgoZGRkCAgYGBgIGBgoKCgoJGRkCHh4eHh4eHh4eHh4BAQEBARISEhISEhYWFRQXFRUUFxUWFhYWFhYWFhwdAQEBAQEeFhYWFhYWFhYWFRQWFhYWEREWFhYaFxMWFhYWFhYWFhYWFhoWERYWFhYWFhYWFhYeAQEBAQECAQEBAQEBAQEBARAHAgIQEBAQEBAaGhoXEwcQEBAQEBAQEBAQGhoaFxMCBwcHBwcHBwcHBwcHBwICAhgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYAgICAgICAgICAgICAgICAg0NDQ0NDQ0NDQ0NDQ0MDAwMDQwMDA0NDQ0NDQ0NDQ0NDQICAgICAgICAgICAgICAhsbChsbGxsKGxsGCgoKBgYKCgoGGwobGxoKCgoKChsbGxsbGwobChsKGwoKCgobBgoKCgoGCAgICAYbGwYGCgoaGhoaGgoGBgYGGxobGwYbEBAQEBAQEBAQEBAQEBAQEA8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PCgYPDw8PEBsbAgICAhoaGhoaGxsbGxsaGhsbGxsaGxsaGxsaGxsbGxsbGxobGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGhobGxobGhsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGxsbGxsbGxsXExcTGxsbGxsbGxsbGxsbGxsbGxsbGxsaGhsbGxsbGxsXExsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxobGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxoaGhoaGhsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGwICAgICAgICAgICAgICAgICAgICAgICAgIbGxsbGxsbGxsbGwICAgICAgICAgICAgICAgICAgICAhAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxAQEBAQEBAQEBAQEBAQEBAQEBAQEBAbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsaGxsbGxsbGxsbGhsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxoaGhoaGhoaGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGhsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbFxMXExcTFxMXExcTFxMQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxoaGhoaFxMaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaFxMXExcTFxMXExoaGhoaGhoaGhoaGhoaGhobGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoXExcTFxMXExcTFxMXExcTFxMXExcTGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaFxMXExoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaFxMaGhsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxoaGhoaGhoaGhoaGhoaGhoaGhoaGhsbGhoaGhoaGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbAgIbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGwICGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGwIbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGwIKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgIGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgIKBgoKCgYGCgYKBgoGCgoKCgYKBgYKBgYGBgYGBwcKCgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYGGxsbGxsbCgYKBg0NDQoGAgICAgIWFhYWEBYWBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYCBgICAgICBgICCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCAgICAgICBxYCAgICAgICAgICAgICAg0ICAgICAgICAgICAgICAgICAgICAgICAICAgICAgICAggICAgICAgCCAgICAgICAIICAgICAgIAggICAgICAgCCAgICAgICAIICAgICAgIAggICAgICAgCCAgICAgICAINDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDRYWFRQVFBYWFhUUFhUUFhYWFhYWFhYWEhYWEhYVFBYWFRQXExcTFxMXExYWFhYWBxYWFhYWFhYWFhYSEhYWFhYSFhcWFhYWFhYWFhYWFhYCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsCGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsCAgICAgICAgICAgIbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbAgICAgICAgICAgICAgICAgICAgICAgICAgIbGxsbGxsbGxsbGxsCAgICHhYWFhsHCA8XExcTFxMXExcTGxsXExcTFxMXExIXExMbDw8PDw8PDw8PDQ0NDQsLEgcHBwcHGxsPDw8HCBYbGwIICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAICDQ0ZGQcHCBIICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgWBwcHCAICAgICCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAIICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAhsbEBAQEBsbGxsbGxsbGxsICAgICAgICAgICAgICAgICAgICAgICAgICAgCAgICAhsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGwICAgICAgICAgICAggICAgICAgICAgICAgICAgbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbAhAQEBAQEBAQEBAbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsQEBAQEBAQEBsQEBAQEBAQEBAQEBAQEBAbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxAQEBAQEBAQEBAbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsQEBAQEBAQEBAQEBAQEBAbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsCCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCAgICAgICAgICGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGwgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAICAgICAgICAgICAgICAgIICAgICAgICAgICAgICAgICAgICAgHCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAgICGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGwICAgICAgICAggICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgHBwcHBwcWFggICAgICAgICAgICAcWFhYICAgICAgICAgICAgICAgIDg4ODg4ODg4ODggIAgICAgICAgICAgICAgICAgICAgIKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCA0MDAwWDQ0NDQ0NDQ0NDRYHCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgcHDQ0ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDw8PDw8PDw8PDw0NFhYWFhYWAgICAgICAgIZGRkZGRkZGRkZGRkZGRkZGRkZGRkZGQcHBwcHBwcHBxkZCgYKBgoGCgYKBgoGCgYGBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGBwYGBgYGBgYGCgYKBgoKBgoGCgYKBgoGBxkZCgYKBggKBgoGBgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoKCgoKBgoKCgoKBgoGCgYCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICCAcHBggICAgICAgNCAgIDQgICAgNCAgICAgICAgICAgICAgICAgICAgICAgLCw0NCxsbGxsCAgICEBAQEBAQGxsYGwICAgICAggICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgWFhYWAgICAgICAgILCwgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICwsLCwsLCwsLCwsLCwsLCw0NAgICAgICAgIWFg4ODg4ODg4ODg4CAgICAgINDQ0NDQ0NDQ0NDQ0NDQ0NDQ0ICAgICAgWFhYIFggIDQ4ODg4ODg4ODg4ICAgICAgICAgICAgICAgICAgICAgICAgICAgIDQ0NDQ0NDQ0WFggICAgICAgICAgICAgICAgICAgICAgIDQ0NDQ0NDQ0NDQ0LCwICAgICAgICAgICFggICAgICAgICAgICAgICAgICAgICAgICAgICAgIAgICDQ0NCwgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDQsLDQ0NDQsLDQsLCwsWFhYWFhYWFhYWFhYWAgcODg4ODg4ODg4OAgICAhYWCAgICAgNBwgICAgICAgICA4ODg4ODg4ODg4ICAgICAIICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICA0NDQ0NDQsLDQ0LCw0NAgICAgICAgICCAgIDQgICAgICAgIDQsCAg4ODg4ODg4ODg4CAhYWFhYICAgICAgICAgICAgICAgIBwgICAgICBsbGwgLDQsICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICA0IDQ0NCAgNDQgICAgIDQ0IDQgCAgICAgICAgICAgICAgICAgICAgICAgIICAcWFggICAgICAgICAgICw0NCwsWFggHBwsNAgICAgICAgICAggICAgICAICCAgICAgIAgIICAgICAgCAgICAgICAgIICAgICAgIAggICAgICAgCBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBhkHBwcHBgYGBgYGAgICAgICAgICAgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgLCw0LCw0LCxYLDQICDg4ODg4ODg4ODgICAgICAggICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAgICAgICAgICAgICCAgICAgICAgICAgICAgICAgICAgICAgCAgICCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAICAgIDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCAggICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgYGBgYGBgYCAgICAgICAgICAgIGBgYGBgICAgICCA0ICAgICAgICAgIGggICAgICAgICAgICAgCCAgICAgCCAIICAIICAIICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgZGRkZGRkZGRkZGRkZGRkZAgICAgICAgICAgICAgICAgIICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBMXAgICAgICAgICAgICAgICAggICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCAggICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIICAgICAgICAgICAgYGwICDQ0NDQ0NDQ0NDQ0NDQ0NDRYWFhYWFhYXExYCAgICAgINDQ0NDQ0NDQ0NDQ0NDQ0NFhISEREXExcTFxMXExcTFxMXExcTFhYXExYWFhYREREWFhYCFhYWFhIXExcTFxMWFhYaEhoaGgIWGBYWAgICAggICAgIAggICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAICAQIWFhYYFhYWFxMWGhYSFhYODg4ODg4ODg4OFhYaGhoWFgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKFxYTGREZBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYXGhMaFxMWFxMWFggICAgICAgICAgHCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIBwcICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAgICCAgICAgIAgIICAgICAgCAggICAgICAICCAgIAgICGBgaGRsYGAIbGhoaGhsbAgICAgICAgICAgEBARsbAgIICAgICAgICAgICAgCCAgICAgICAgICAgICAgICAgICAgICAgICAgCCAgICAgICAgICAgICAgICAgICAIICAIICAgICAgICAgICAgICAgCAggICAgICAgICAgICAgIAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAggICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAICAgICFhYWAgICAhAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEAICAhsbGxsbGxsbGw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PEBAQEBsbGxsbGxsbGxsbGxsbGxsbEBAbGxsCGxsbGxsbGxsbGxsbAgICAhsCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAhsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGw0CAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCAgIICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAgICAgICAgICAgICAgICDRAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEAICAgIICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBAQEBACAgICAgICAgIICAgICAgICAgICAgICAgICAgICA8ICAgICAgICA8CAgICAggICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDQ0NDQ0CAgICAggICAgICAgICAgICAgICAgICAgICAgICAgICAgICAIWCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAgICAggICAgICAgIFg8PDw8PAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCAg4ODg4ODg4ODg4CAgICAgIKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoCAgICBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGAgICAggICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCAgICAgICAggICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCAgICAgICAgICAhYCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAgICAgICAgICCAgICAgICAgICAgICAgICAgICAgICAICAgICAgICAgIICAgICAgICAICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICCAgICAgIAgIIAggICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAggIAgICCAICCAgICAgICAgICAgICAgICAgICAgICAgCFhAQEBAQEBAQCAgICAgICAgICAgICAgICAgICAgICAgbGxAQEBAQEBAICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAgICAgICAgIQEBAQEBAQEBACAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIICAgICAgICAgICAgICAgICAgIAggIAgICAgIQEBAQEAgICAgICAgICAgICAgICAgICAgICAgQEBAQEBACAgIWCAgICAgICAgICAgICAgICAgICAgICAgICAgCAgICAhYCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCAgICEBAICBAQEBAQEBAQEBAQEBAQEBACAhAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAIDQ0NAg0NAgICAgINDQ0NCAgICAIICAgCCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCAg0NDQICAgINEBAQEBAQEBAQAgICAgICAhYWFhYWFhYWFgICAgICAgIICAgICAgICAgICAgICAgICAgICAgICAgICAgICBAQFggICAgICAgICAgICAgICAgICAgICAgICAgICAgIEBAQAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIICAgICAgICBsICAgICAgICAgICAgICAgICAgICAgICAgICAgIDQ0CAgICEBAQEBAWFhYWFhYWAgICAgICAgICCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAgICFhYWFhYWFggICAgICAgICAgICAgICAgICAgICAgCAhAQEBAQEBAQCAgICAgICAgICAgICAgICAgICAICAgICEBAQEBAQEBAICAgICAgICAgICAgICAgICAgCAgICAgICFhYWFgICAgICAgICAgICAhAQEBAQEBACAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAggICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKAgICAgICAgICAgICAgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgICAgICAgIQEBAQEBAICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgNDQ0NAgICAgICAgIODg4ODg4ODg4OAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEAICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAggICAgICAgICAgICAgICAgICAgICAgICAgICAgIEBAQEBAQEBAQEAgCAgICAgICAggICAgICAgICAgICAgICAgICAgICAgNDQ0NDQ0NDQ0NDRAQEBAWFhYWFgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgILDQsICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICA0NDQ0NDQ0NDQ0NDQ0NDRYWFhYWFhYCAgICEBAQEBAQEBAQEBAQEBAQEBAQEBAODg4ODg4ODg4OAgICAgICAgICAgICAgICDQ0NCwgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAsLCw0NDQ0LCw0NFhYBFhYWFgICAgICAgICAgICAQICCAgICAgICAgICAgICAgICAgICAgICAgICAICAgICAgIODg4ODg4ODg4OAgICAgICDQ0NCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDQ0NDQ0LDQ0NDQ0NDQ0CDg4ODg4ODg4ODhYWFhYICwsCAgICAgICAgIICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICA0WFggCAgICAgICAgINDQsICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgLCwsNDQ0NDQ0NDQ0LCwgICAgWFhYWDQ0NDRYCAg4ODg4ODg4ODg4IFggWFhYCEBAQEBAQEBAQEBAQEBAQEBAQEBACAgICAgICAgICAggICAgICAgICAgICAgICAgICAIICAgICAgICAgICAgICAgICAgICAgICAgICwsLDQ0NCwsNCw0NFhYWFhYWDQICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICCAgICAgICAIIAggICAgCCAgICAgICAgICAgICAgIAggICAgICAgICAgWAgICAgICCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgNCwsLDQ0NDQ0NDQ0CAgICAg4ODg4ODg4ODg4CAgICAgINDQsLAggICAgICAgIAgIICAICCAgICAgICAgICAgICAgICAgICAgICAIICAgICAgIAggIAggICAgIAg0NCAsLDQsLCwsCAgsLAgILCwsCAggCAgICAgILAgICAgIICAgICAsLAgINDQ0NDQ0NAgICDQ0NDQ0CAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgLCwsNDQ0NDQ0NDQsLDQ0NCw0ICAgIFhYWFhYODg4ODg4ODg4OAhYCFg0CAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgLCwsNDQ0NDQ0LDQsLCwsNDQsNDQgIFggCAgICAgICAg4ODg4ODg4ODg4CAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgLCwsNDQ0NAgILCwsLDQ0LDQ0WFhYWFhYWFhYWFhYWFhYWFhYWFhYWFggICAgNDQICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgLCwsNDQ0NDQ0NDQsLDQsNDRYWFggCAgICAgICAgICAg4ODg4ODg4ODg4CAgICAgIWFhYWFhYWFhYWFhYWAgICAgICAgICAgICAgICAgICAggICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgNCw0LCw0NDQ0NDQsNAgICAgICAgIODg4ODg4ODg4OAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICCAgICAgICAgICAgICAgICAgICAgICAgICAgIAgINDQ0LCw0NDQ0LDQ0NDQ0CAgICDg4ODg4ODg4ODhAQFhYWGwICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAggICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICwsLDQ0NDQ0NDQ0NCw0NFgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGDg4ODg4ODg4ODhAQEBAQEBAQEAICAgICAgICAgICAggCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICCA0NDQ0NDQ0NDQ0ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDQ0NDQ0NCwgNDQ0NFhYWFhYWFhYNAgICAgICAgIIDQ0NDQ0NCwsNDQ0ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAgIICAgIDQ0NDQ0NDQ0NDQ0NDQsNDRYWFggWFhYWFgICAgICAgICAgICAgICAgICAgICAgICAgICAgICCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAgICAgICAggICAgICAgICAIICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICw0NDQ0NDQ0CDQ0NDQ0NCw0IFhYWFhYCAgICAgICAgICDg4ODg4ODg4ODhAQEBAQEBAQEBAQEBAQEBAQEBACAgIWFggICAgICAgICAgICAgICAgICAgICAgICAgICAgICAICDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQILDQ0NDQ0NDQsNDQsNDQICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIICAgICAgIAggIAggICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDQ0NDQ0NAgICDQINDQINDQ0NDQ0NCA0CAgICAgICAg4ODg4ODg4ODg4CAgICAgIICAgICAgCCAgCCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgLCwsLCwINDQILCw0LDQgCAgICAgICDg4ODg4ODg4ODgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIICAgICAgICAgICAgICAgICAgIDQ0LCxYWAgICAgICAggICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIPDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8PDw8CFhYWFhYCAgICAgICAgICAggICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAggICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAgICAgICAggICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCDg4ODg4ODg4ODgICAgIWFgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAggICAgICAgICAgICAgICAgICAgICAgICAgICAgICAICDQ0NDQ0WAgICAgICAgICAggICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICA0NDQ0NDQ0WFhYWFhsbGxsHBwcHFhsCAgICAgICAgICDg4ODg4ODg4ODgIQEBAQEBAQAggICAgICAgICAgICAgICAgICAgICAICAgICCAgICAgICAgICAgICAgICAgICAICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBhAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQFhYWFgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAgICAgICAgICAgIICwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwsLCwICAgICAgICAgICAgICAgINDQ0NBwcHBwcHBwcHBwcHBwICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHBwICAgICAgICAgICAgICAgICAgICAgICAgICAgICAggICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAgICAgICAgICAgICAgIICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCAgICAgICAgICAgICCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAggICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAICAgIICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAICAgICCAgICAgICAgICAgICAICAggICAgICAgICAICAgICAgIICAgICAgICAgIAgIbDQ0WAQEBAQICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbAgICAgICAgICAhsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGwICGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbCwsNDQ0bGxsLCwsLCwsBAQEBAQEBAQ0NDQ0NDQ0NGxsNDQ0NDQ0NGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbDQ0NDRsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbAgICAgICAgICAgICAgICAgICAgICAgIbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsNDQ0bAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAhAQEBAQEBAQEBAQEBAQEBAQEBAQAgICAgICAgICAgICGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbAgICAgICAgICEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEAICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgYGBgYGBgYCBgYGBgYGBgYGBgYGBgYGBgYGCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgoCCgoCAgoCAgoKAgIKCgoKAgoKCgoKCgoKBgYGBgIGAgYGBgYGBgYCBgYGBgYGBgYGBgYKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGCgoCCgoKCgICCgoKCgoKCgoCCgoKCgoKCgIGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgoKAgoKCgoCCgoKCgoCCgICAgoKCgoKCgoCBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgICCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKChoGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGGgYGBgYGBgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoaBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBhoGBgYGBgYKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKGgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYaBgYGBgYGCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKChoGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGGgYGBgYGBgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoaBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBhoGBgYGBgYKBgICDg4ODg4ODg4ODg4ODg4ODg4ODg4ODg4ODg4ODg4ODg4ODg4ODg4ODg4ODg4ODg4ODg4NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NGxsbGw0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NGxsbGxsbGxsNGxsbGxsbGxsbGxsbGxsNGxsWFhYWFgICAgICAgICAgICAgICAg0NDQ0NAg0NDQ0NDQ0NDQ0NDQ0NDQICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICDQ0NDQ0NDQINDQ0NDQ0NDQ0NDQ0NDQ0NDQICDQ0NDQ0NDQINDQINDQ0NDQICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAggICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAgIQEBAQEBAQEBANDQ0NDQ0NAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBg0NDQ0NDQ0CAgICAg4ODg4ODg4ODg4CAgICFhYCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBsQEBAYEBAQEAICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAggICAgCCAgICAgICAgICAgICAgICAgICAgICAgICAgIAggIAggCAggCCAgICAgICAgICAIICAgIAggCCAICAgICAggCAgICCAIIAggCCAgIAggIAggCAggCCAIIAggCCAIICAIIAgIICAgIAggICAgICAgCCAgICAIICAgIAggCCAgICAgICAgICAIICAgICAgICAgICAgICAgICAICAgICCAgIAggICAgIAggICAgICAgICAgICAgICAgIAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAhoaAgICAgICAgICAgICAgIbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGwICAgIbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbAgICAgICAgICAgICGxsbGxsbGxsbGxsbGxsbAgIbGxsbGxsbGxsbGxsbGxsCGxsbGxsbGxsbGxsbGxsbAhsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsCAgICAgICAgICEBAQEBAQEBAQEBAQEAICAhsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbAgICAhsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGwICAgICAgICAgICAgIbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGwICAgIbGxsbGxsbGxsCAgICAgICGxsCAgICAgICAgICAgICAhsbGxsbGwICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxkZGRkZGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbAgICAgICAgICAgIbGxsbGxsbGxsbGxsbAgICGxsbGxsbGxsbGwICAgICAhsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbAgICAgICAgICAgICGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIbGxsbGxsbGxsbGxsCAgICGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsCAgICAgICAhsbGxsbGxsbGxsCAgICAgIbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbAgICAgICAgIbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICGxsbGxsbGxsbGxsbAgICAhsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbAhsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsCAhsbGxsCAgIbAhsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGwICAgICAgICAgICAgIbGxsbGxsbGxsbAgICAgICGxsbAgICAgICAgICAgICAhsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGwICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAhsbGxsbGxsbGxsbGxsbAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCAgICAgICAgICAggICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAICCAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCAgICAgICAgICAgICAggICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAggICAgICAgICAgICAgICAgICAgICAgICAgICAgICAICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAQICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NAgICAgICAgICAgICAgICAoAAAAAAAAAAgAAAAAAAAACAAAAAAAAAAIAAAAAAAAAAgAAAAAAAAACAAAAAAAAAAIAAAAAAAAAAgAAAAAAAAACAAAAAAAAAAIA="),C(e,81428,"ggAAAAAAAAIAAAAAAAAAAgAAAAAAAAAC"),C(e,81475,"AgAAAAAAACAAAAAAAAAAIA=="),C(e,81506,"IA=="),C(e,81522,"IAAAAAAAAAAg"),C(e,81618,"IA=="),C(e,81634,"IA=="),C(e,81650,"IAAAAAAAAAAg"),C(e,81682,"IAAAAAAAAAAgAAAAAAAAACAAAAAAAIAAAAAAAAAAgA=="),C(e,81728,"gAAAAAAAAACAAAAAAAAAAIA="),C(e,81760,"gAAAAAAAAACA"),C(e,81904,"gAAAAAAAAACAAAAAAAAAAIAAAAAAAAAAgAAAAAAAAACAAAAAAAAAAAAU"),C(e,81961,"FAAAAAAAAAAU"),C(e,81985,"FAAAAAAAAAAE"),C(e,82009,"BAAAAAAAAAAQAAAAAAAAABAAAAAAAAAAEAAAAAAAAAAQAAAAAAAAABAAAAAAAAAAEAAAAAAAAAAQAAAAAAAAABA="),C(e,82097,"EAAAAAAAAAAQAAAAAAAAABAAAAAAAAAAEAAAAAAAAAAQAAAAAAAAABA="),C(e,82169,"EAAAAAAAAAAQAAAAAAAAABAAAAAAAACA"),C(e,82224,"gAAAAAAAAACA"),C(e,82272,"gAAAAAAAAACAAAAAAAAAAIAAAAAAAAAAgAAAAAAAAACAAAAAAAAAAAAEAAAAAAAAAAQAAAAAAAAABA=="),C(e,82353,"BAAAAAAAAAAEAAAAAAAAAAQAAAAAAAAABA=="),C(e,82393,"BAAAAAAAAAAEAAAAAAAAAAQAAAAAAAAABAAAAAAAAAAQAAAAAAAAAAQAAAAAAAAABAAAAAAAAAAEAAAAAAAAAAQAAAAAAAAABAAAAAAAAAAEAAAAAAAAAAQAAAAAAAAABAAAAAAAAAAEAAAAAAAAAAQAAAAAAAAABAAAAAAAAAAEAAAAAAAAAAQAAAAAAAAABAAAAAAAAAAEAAAAAAAAABAAAAAAAAAABAAAAAAAAAAEAAAAAAAAAAQAAAAAAAAABAAAAAAAAAAEAAAAAAAAAAQAAAAAAAAABA=="),C(e,82625,"BAAAAAAAAAAE"),C(e,82649,"BAAAAAAAAAAEAAAAAAAAAAQAAAAAAAAABAAAAAAAAAAEAAAAAAAAAAQAAAAAAAAABAAAAAAAAAAEAAAAAAAAAAQAAAAAAAAABAAAAAAAAAAEAAAAAAAAAAQAAAAAAAAABAAAAAAAAAAEAAAAAAAAAAQAAAAAAAAABAAAAAAAAAAEAAAAAAAAAAQAAAAAAAAABAAAAAAAAAAQAAAAAAAAAAQAAAAAAAAABAAAAAAAAAAEAAAAAAAAAAQAAAAAAAAABAAAAAAAAAAEAAAAAAAAAAQAAAAAAAAABA=="),C(e,83033,"BAAAAAAAAAAEAAAAAAAAAAQAAAAAAAAABAAAAAAAAAAEAAAAAAAAAAQAAAAAAAAABA=="),C(e,83097,"BAAAAAAAAAAE"),C(e,83121,"BAAAAAAAAAAEAAAAAAAAAAQAAAAAAAAABAAAAAAAAAAEAAAAAAAAQAAAEAAAAAFAAAAQAAAAgEAAABAAAAAQQAAAAAAAACBAAAAAAAAAgEAAAAAAAAAAQAAAAAAAAABAAAAQAAAAgEAAAAAAAAAQQAAAAAAAAARAAAAAAAAAAEAAAAAAAAAAQA=="),C(e,83288,"QA=="),C(e,83303,"AkAAAAAAAAAQQAAAEAAAAIBAAAAAAAAACEA="),C(e,83352,"QAAAAAAAABBAAAAQAAAAgAAg"),C(e,83379,"gAAAAABAAACQggAAIEAAAJAAAAAAAAAAgAAAAABAAACQggAAIEAAAJCCAAAgAAAAgAAAAAAAAACAAAAAAAAAAIAAAAAAQAAAkIIAACBAAACQggAAIEAAAJCCAAAgQAAAkAAAAEBAAACQAAAAYEAAAJCCAABgQAAAkIIAACBAAACQggAAIEAAAJCCAAAgQAAAkIIAACBAAACQggAAIAAAAIAAAAAEAAAAAAAAAIAAAAAAAAAACAAAAAAAAAAEAAAAAAAAACAAAAAAAAAAQAAAAAAAAACAAAAAAAAAAIAAAAAAAAAAAkAAAAAAAAAQQAAAEAAAAIBAAAAAAAAABEAAAAAAAAAIAAAAgIIAAAAAAACAgAAAAAAAAIAAAAAAAAAAgIYAAAAAAACAhgAAAAAAAICAAAAAAAAAgIAAAAAAAACAggAAAAAAAICAAAAAAAAAgIAAAAAAAACAgAAAAAAAAICAAAAAAAAAgJIAAACAAACAggAAAIAAAICCAAAAAAAAgIIAAAAAAACAggAAAIAAAICCAAAAAAAAgIIAAAAAAACAggAAAAAAAICAAAAAAAAAgIAAAAAAAACAggAAAAAAAICGAAAAAAAAgIIAAAAAAACAhgAAAAAAAICCAAAAAAAAgIIAAAAAAACAggAAAAAAAICCAAAAAAAAgIAAAAAAAACAggAAAAAAAICAAAAAAAAAgIIAAAAAAACAggAAAAAAAICCAAAAAAAAgIAAAAAAAACAggAAAAAAAICGAAAAAAAAgJIAAAAAAACAhgAAAAAAAICAAAAAAAAAgIAAAAAAAACAhg=="),C(e,83999,"IAAAAACCAAAgAAAAAIIAAAAAAAAAggAAAAAAAACGAAAAAAAAAIIAAAAAAAAAggAAAAAAAACCAAAAaW5maW5pdHkALSsgICAwWDB4AHN0ZDo6YmFkX2Nhc3QAJXMlYyVzX2RpY3QAJXMlYyVzJWMlcyVzACVzJWNzb3VuZGljb25zJWMlcwBDb21waWxlIGVycm9yAHN0ZDo6ZXhjZXB0aW9uAHRlcm1pbmF0aW5nAF8wbGFuZwB1bmV4cGVjdGVkX2hhbmRsZXIgdW5leHBlY3RlZGx5IHJldHVybmVkACVkICVkICVkICVkICVkICVkICVkICVkICVkICVkACVzJWMlYwByYgBwaG9udGFiAHJ3YQBYWFhYWFgAR01UAExDX0FMTABFU1BFQUtfREFUQV9QQVRIAEMAQU5TSV9YMy40LTE5NjgAMy4xLjMwACEtLQBQdXJlIHZpcnR1YWwgZnVuY3Rpb24gY2FsbGVkIQAlcwoAZXNwZWFrOiBCYWQgaW50b25hdGlvbiBkYXRhCgAAAQIEBwMGBQAAAABAAAAAAAAAAHBob25pbmRleAAlcyVjJWMlcwAgJXMAc3RkOjpiYWRfZXhjZXB0aW9uAEVtc2NyaXB0ZW4AbmFuAD94bWwAJXMvLi4vcGhzb3VyY2UAc3RkOjpiYWRfdHlwZWlkAHRlcm1pbmF0ZV9oYW5kbGVyIHVuZXhwZWN0ZWRseSByZXR1cm5lZAAlZCAlZCAlZCAlZCAlZCAlZCAlZCAlZAByYgBXcm9uZyB2ZXJzaW9uIG9mIGVzcGVhay1uZy1kYXRhAEkATEFORwBIT01FAEFOU0lfWDMuNC0xOTg2AChudWxsKQBGYWlsZWQgdG8gb3BlbjogJyVzJwBEZWxldGVkIHZpcnR1YWwgZnVuY3Rpb24gY2FsbGVkIQBudW1iZXJzOiBCYWQgb3B0aW9uIG51bWJlciAlZAoAX2NhcABlbXNjcmlwdGVuAFRoZSBGSUZPIGJ1ZmZlciBpcyBmdWxsAGVzcGVhawAlZCAlZCAlZABzdGQ6OmJhZF9hbGxvYwAlYyVzJWMlcyVjAHBob25kYXRhAC91c3Ivc2hhcmUvZXNwZWFrLW5nLWRhdGEAL3RtcC9lc3BlYWtYWFhYWFgATABQU0FSSFRJVllNVUJGAEFTTU8tNzA4AEMuVVRGLTgAICgAJXM6IEJhZCBvcHRpb24gbnVtYmVyICVkCgBVbnN1cHBvcnRlZCBzcGVjdHJhbCBmaWxlIGZvcm1hdC4KAENhbid0IHJlYWQgZGljdGlvbmFyeSBmaWxlOiAnJXMnCgAtMFgrMFggMFgtMHgrMHggMHgAaW50b25hdGlvbnMAcGhvbmVtZXMAJXMgJXMgJXMgJXMgJXMgJXMAZW4AYmFkX2FycmF5X25ld19sZW5ndGgAbm9uZQBUaGUgZXNwZWFrLW5nIGxpYnJhcnkgaGFzIG5vdCBiZWVuIGluaXRpYWxpemVkACVjJWQAUE9TSVgATQBDLlVURi04AEVDTUEtMTE0ACMxAEVtcHR5IF9kaWN0IGZpbGU6ICclcwoAUmVwbGFjZTogJXMgPiAlcwoAICBzdWZmaXggWyVzXQoKACVzL3Bob25lbWVzACVzJXMlcwBicABhbGwAaW5mAENhbm5vdCBpbml0aWFsaXplIHRoZSBhdWRpbyBkZXZpY2UAJXMlY3ZvaWNlcyVjAE4ATlVMTABFQ01BLTExOABVVEYtOAB3YXNtMzIAZXNwZWFrOiBObyBlbnZlbG9wZQoAQmFkIGRhdGE6ICclcycgKCV4IGxlbmd0aD0leCkKAFRoZSBzcGVjaWZpZWQgZXNwZWFrLW5nIHZvaWNlIGRvZXMgbm90IGV4aXN0ACVzJXMAJXMlYyVzAF9jYXAAYmhmAHNvbWUAWwIlc11dAFAASU5GAEVMT1RfOTI4AGVuX1VTLlVURi04AF8jJWQgAENvbXBpbGluZyBwaG9uZW1lIGRhdGE6ICVzCgBGdWxsIGRpY3Rpb25hcnkgaXMgbm90IGluc3RhbGxlZCBmb3IgJyVzJwoAVW5rbm93biB0dW5lICclcycKACU1ZDoJAGR0AG5vAGVuAG5hbgBfcm9tYW4AQ291bGQgbm90IGxvYWQgdGhlIG1icm9sYS5kbGwgZmlsZQAlZCAlZAAlcyVjbGFuZyVjAHJiAFsCX15fJXMgJXMgX15fJXNdXQBTAF8/QQBJQk0zNjcAPyVkIAAlcyVjJXMAJXMvJXMAcgBpY29uAGVuAENvdWxkIG5vdCBsb2FkIHRoZSBzcGVjaWZpZWQgbWJyb2xhIHZvaWNlIGZpbGUAZ2MAWgBOQU4AXz8/ADxzYXktYXMgaW50ZXJwcmV0LWFzPSJ0dHM6Y2hhciI+JiMlZDs8L3NheS1hcz4ASUJNODE5ACogACAgJWQgJXMgICAgAEludmFsaWQgaW5zdHJ1Y3Rpb24gJS40eCBmb3IgcGhvbmVtZSAnJXMnCgAAcGhvbmRhdGEtbWFuaWZlc3QAVGhlIGV2ZW50IGJ1ZmZlciBpcyBmdWxsAHNwZWxsaW5nAF9saWcAY29uZmlnACVzL2VzcGVhay1uZy1kYXRhACVzJXNfAElTQ0lJAGhBAGwnZXRAAC4ALQAoJXMpAAElZEkgACV4AHcAJXNydWxlcy50eHQAKyVzAHIAXy5wAHBpdGNoAFRoZSByZXF1ZXN0ZWQgZnVuY3Rpb25hbGl0eSBoYXMgbm90IGJlZW4gYnVpbHQgaW50byBlc3BlYWstbmcAX3NtYwBDYwBtYgBJU09fNjQ2LmlydjoxOTkxAHYgPD0gdm93ZWxfY291bnQAVGhlIHBob25lbWUgZmlsZSBpcyBub3QgaW4gYSBzdXBwb3J0ZWQgZm9ybWF0AGNoYXJhY3RlcnMAJWMlcyVzJXMAICVzAF90dXIAZW4AQ2YAbmQAJXMgJWQASVNPXzg4NTktMQAjIFRoaXMgZmlsZSBsaXN0cyB0aGUgdHlwZSBvZiBkYXRhIHRoYXQgaGFzIGJlZW4gY29tcGlsZWQgaW50byB0aGUKIyBwaG9uZGF0YSBmaWxlCiMKIyBUaGUgZmlyc3QgY2hhcmFjdGVyIG9mIGEgbGluZSBpbmRpY2F0ZXMgdGhlIHR5cGUgb2YgZGF0YToKIyAgIFMgLSBBIFNQRUNUX1NFUSBzdHJ1Y3R1cmUKIyAgIFcgLSBBIHdhdmVmaWxlIHNlZ21lbnQKIyAgIEUgLSBBbiBlbnZlbG9wZQojCiMgQWRkcmVzcyBpcyB0aGUgZGlzcGxhY2VtZW50IHdpdGhpbiBwaG9uZGF0YSBvZiB0aGlzIGl0ZW0KIwojICBBZGRyZXNzICBEYXRhIGZpbGUKIyAgLS0tLS0tLSAgLS0tLS0tLS0tCgBfcmV2AHZhcmlhbnQAJXNydWxlcwB0dHM6Y2hhcgBzb3VuZGljb24AQ24AX2VsAG5nAHNyYy9saWJlc3BlYWstbmcvZGljdGlvbmFyeS5jAF9zdWIAcGhvbmRhdGEAVGhlIHNwZWN0cmFsIGZpbGUgZG9lcyBub3QgY29udGFpbiBhbnkgZnJhbWUgZGF0YQABKzEwUwBJU09fODg1OS0xOjE5ODcAdHRzOmtleQAlcyVjJXNfZGljdAB0cwBUaGUgcGhvbmVtZSBtYW5pZmVzdCBmaWxlIGRvZXMgbm90IGNvbnRhaW4gYW55IHBob25lbWVzAF8lYyAlcwBfY3lyAF9zdXAAQ28AX2NybAB3YgAgAS0xMFMASVNPXzg4NTktMgAtAFVucHJvbm91bmNhYmxlPyAnJXMnCgBfaHkAcGhvbmluZGV4AF9hY3UAdHRzOmRpZ2l0cwBDcwAgJXMgJWQgJXMAJ2U6agBhcG9zdHJvcGhlAFRoZSBwaG9uZW1lIGZlYXR1cmUgaXMgbm90IHJlY29nbmlzZWQAdEEASVNPXzg4NTktMjoxOTg3AHdiKwBUcmFuc2xhdGUgJyVzJwoAX2JydgBicmFja2V0cwAlY2VuAElpAHRlbGVwaG9uZQBfaGUAVGhlIHRleHQgZW5jb2RpbmcgaXMgbm90IHN1cHBvcnRlZAAlZCAlZABwaG9udGFiAFsCKFgxKShYMSkoWDEpXV0AbkEAYidpOgBJU09fODg1OS0zAFVzaW5nIHBob25lbWV0YWJsZTogJyVzJwoAVW5zcGVjaWZpZWQgZXJyb3IgMHgleAByb290cwBnbHlwaHMAJWMlcwBfYXIATGwAJXMvY29tcGlsZV9wcm9nX2xvZwBicmFja2V0c0Fubm91bmNlZAAlZABfaGFjAF9eXwBzJ2k6AElTT184ODU5LTM6MTk4OABsaXN0AGRpY3RfbWluAGVuAExtAHgtd2VhawBiYXNlAF9jZWQAX3N5YwBkJ2k6AElTT184ODU5LTQAQmFkIHZvaWNlIGF0dHJpYnV0ZTogJXMKAEVycm9yIHByb2Nlc3NpbmcgZmlsZSAnJXMnOiAlcy4KAGxpc3R4AGRpY3RydWxlcwBfY2lyAExvAHdlYWsAX2hpACdpOgBJU09fODg1OS00OjE5ODgAJXMgAEludmFsaWQgcGhvbmVtZSBjb2RlICVkCgAKUmVmcyAlZCwgIFJldXNlZCAlZAoARXJyb3I6ICVzIGF0ICclcycgKGV4cGVjdGVkIDB4JXgsIGdvdCAweCV4KS4KAFVua25vd24gcGhvbmVtZSB0YWJsZTogJyVzJwoATHQAJWMlcwBpbnRvbmF0aW9uAF9ibgBtZWRpdW0AZW1vamkAJ2VmAF9kaWEASVNPXzg4NTktNQBSZXBsYWNlOiAlcyAgJXMKAENvbXBpbGVkIHBob25lbWVzOiAlZCBlcnJvcnMuCgBFcnJvcjogJXMuCgBoc3gATHUAJXMvLi4vcGhzb3VyY2UvaW50b25hdGlvbi50eHQAbF9kaWVyZXNpcwAlcyslcwBfZ3VyAGlvbgBzdHJvbmcAZXh0cmEASVNPXzg4NTktNToxOTg4AF9hYzIAbF9wcmVmaXgAX2d1AF9kb3QAJXMlY3ZvaWNlcwByAHgtc3Ryb25nAE1jAElTT184ODU5LTYAQ29tcGlsaW5nOiAnJXMnCgBfZ3J2AGxfcmVncmVzc2l2ZV92AHJzAF9vcgAlcy8uLi9waHNvdXJjZS9pbnRvbmF0aW9uACVzJWNsYW5nAE1lAHJlZHVjZWQASVNPXzg4NTktNjoxOTg3ACQxAGlyAF9tY24ATW4AbW9kZXJhdGUAdHVuZQBsX3VucHJvbm91bmNhYmxlAF90YQBJU09fODg1OS03ACQyAG1iLwAlcy9pbnRvbmF0aW9ucwB1cgBfb2dvAGxfc29ub3JhbnRfbWluAHJhdGUAX3RlAE5kAElTT184ODU5LTc6MTk4NwAkMwAvLwBfa24ATmwAYXRoAF9ybmcAdm9sdW1lAGFwb3N0cm9waGUAbG93ZXJjYXNlU2VudGVuY2UASVNPXzg4NTktOAAkNABEdXBsaWNhdGUgdHVuZSBuYW1lOiAnJXMnAGJyYWNrZXRzAG51bWJlcnMAbnMATm8AX21sAF9zdGsAcmFuZ2UAQmFkIHR1bmUgbmFtZTogJyVzOwBJU09fODg1OS04OjE5ODgAJDUAc3BlbGxpbmdTdHJlc3MAX3NpAF90bGQAZmllbGQAYnJhY2tldHNBbm5vdW5jZWQAUGMASVNPXzg4NTktOQAkNgBCYWQgZW52ZWxvcGUgbmFtZTogJyVzJwBfYmFyAGRpY3RfbWluAF90aABzcG9uZwBtb2RlAFR1bmUgJyVzJyBub3QgZm91bmQAc3RyZXNzQWRkAFBkAElTT184ODU5LTk6MTk4OQAkNwBfcmZ4ACR1AGRpY3RydWxlcwBzdHJlc3NBbXAAX2xvAHB1bmN0dWF0aW9uAHJhbmcAUGUASVNPXzg4NTktMTAAVW5leHBlY3RlZDogJyVzJwBjYXBpdGFsX2xldHRlcnMAaW50b25hdGlvbgBfaG9rAF90aQBzdHJlc3NMZW5ndGgAbGFyZwBQZgBUdW5lICclcycgbm90IGRlZmluZWQASVNPXzg4NTktMTA6MTk5MgAkdTEAX215AHN0cmVzc09wdABsX2RpZXJlc2lzAF8jJXMAbGV2ZWwAUGkASVNPXzg4NTktMTQAJHUyAGFkZCBlCgBDb21waWxlZCAlZCBpbnRvbmF0aW9uIHR1bmVzOiAlZCBlcnJvcnMuCgBsX3ByZWZpeABhbHBoYWJldABUb28gbWFueSBwaG9uZW1ldGFibGVzAF94IyVzAFBvAHN0cmVzc1J1bGUAX2thAElTT184ODU5LTE0OjE5OTgAJHUzAE91dCBvZiBtZW1vcnkAbF9yZWdyZXNzaXZlX3YAdHVuZXMAUHMAX2tvAHBoAElTT184ODU5LTE1AF8wACR1KwBfZHB0AHdvcmRzAGludGVycHJldC1hcwBfZXRoAGxfdW5wcm9ub3VuY2FibGUAU2MAXwBJU09fODg1OS0xNgAkdTErAEJhZCBydWxlcyBkYXRhIGluICclc19kaWN0JyBhdCAweCV4ICglYykKAGZvcm1hdABsX3Nvbm9yYW50X21pbgBTawBfYnJhaWxsZQBfME0lZABJU09fODg1OS0xNjoyMDAxACR1MisAQ2FuJ3QgZmluZCBiYXNlIHBob25lbWV0YWJsZSAnJXMnACUzZAklcyBbJXNdCgBDYW5ub3Qgc2V0ICVzOiBsYW5ndWFnZSBub3Qgc2V0LCBvciBpcyBpbnZhbGlkLgoAU20AZGV0YWlsAGxvd2VyY2FzZVNlbnRlbmNlAF9qYQBJU082NDYtVVMAXy4AJHUzKwBfAQBudW1iZXJzAFNvAF8lZG4AX3poACRwYXVzZQAlYyVkWQBwaG9uZW1lX2xlbiA8IE5fUEhPTkVNRV9CWVRFUwBJU08tMTA2NDYtVUNTLTIAJQBzcGVsbGluZ1N0cmVzcwBhbGlhcwBabAAkc3RyZW5kAElTTy04ODU5LTEAJSUARmxhZ3M6ICAlcyAgJXMKACVzJXMlYyVzJXMAWnAAbmFtZQBzdHJlc3NBZGQAJHN0cmVuZDIASVNPLTg4NTktMgAsAEZvdW5kOiAnJXMgJXMKAFpzACVzJXMlcyVjJXMARm91bmQ6ICclcwBzdHJlc3NBbXAAJHVuc3RyZXNzZW5kACVjJWRNAElTTy04ODU5LTMALCwAc3RyZXNzTGVuZ3RoACRhY2NlbnRfYmVmb3JlAF8wWiVkAHNyYwBJU08tODg1OS00AC0tACcAJyBbJXNdICAlcwoAJGFiYnJldgBzdHJlc3NPcHQAJXMvJXMAQWRsbQBfJWNkAElTTy04ODU5LTUAJycAJWMlcwBBZmFrAHN0cmVzc1J1bGUAJGRvdWJsZQAlYyVkSQA9AElTTy04ODU5LTYAJGFsdAB0dW5lcwBBZ2hiACVjJWRVAF86AElTTy04ODU5LTcAX2RwdDIA2Y4gINmPICDZkAB3b3JkcwBfJWRNJWRvAEFob20Ac3RyZW5ndGgASVNPLTg4NTktOAAkYWx0MQBfIQDYpyDZiCDZigBuYW1lAF8lZE0lZGUAQXJhYgAlYyVkQgA6AElTTy04ODU5LTkAJGFsdDIA2Kgg2b4g2Kog2Kkg2Ksg2Kwg2K0g2K4g2K8g2LAg2LEg2LIg2LMg2LQg2LUg2LYg2Lcg2Lgg2Lkg2Log2YEg2YIg2YMg2YQg2YUg2YYg2KYg2KQg2KEg2KMg2KIg2KUg2YcAXyVkTSVkeABBcm1pAHRpbWUAbGFuZ3VhZ2UAQAAkYWx0MwBJU08tODg1OS0xMADYtSDYtiDYtyDYuABnZW5kZXIAQXJtbgB4bWw6YmFzZQBfJWRNJWQAJGFsdDQASVNPLTg4NTktMTEAQC0AfHwAJXgAQXZzdAB2YXJpYW50cwBfMG9mACRhbHQ1AElTTy04ODU5LTEzACDZkSAAZm9ybWFudABfJXMlZG8AQmFsaQAlZAAkYWx0NgBJU08tODg1OS0xNAAxAEJhbXUAc3BlYWsAcGl0Y2gAXyVzJWRlACRhbHQ3AElTTy04ODU5LTE1ACNYMQBfJXMlZHgAQmFzcwBwaG9uZW1lcwAkY29tYmluZQB2b2ljZQA/AElTTy04ODU5LTE2AGRpY3Rpb25hcnkAcHJvc29keQAkZG90AEJhdGsAXyVzJWQAS09JOC1SAC0AJGhhc2RvdABzYXktYXMAQmVuZwByZXBsYWNlAF9eXwBMYXRpbi05AF8wTTIAQmhrcwBlY2hvAG1hcmsAJG1heDMAX1gxAF8lZE0xAFRJUy02MjAAX3wAQmxpcwBmbHV0dGVyACRicmsAVVMtQVNDSUkAXzBNMQAkdGV4dAByb3VnaG5lc3MAJXMlcwBwAEJvcG8AXzo6AFVURi04AGNsYXJpdHkAQnJhaAAkdmVyYmYAcGhvbmVtZQAxTUEAY3AzNjcAdCMAQnJhaQAkdmVyYnNmAHRvbmUAc3ViADBNQQBjcDgxOQAnIQBCdWdpAHZvaWNpbmcAJG5vdW5mAHR0czpzdHlsZQBfO18AY3NBU0NJSQAwTUIAYXVkaW8AYnJlYXRoACRwYXN0ZgBCdWhkADFNACNAAGNzSVNPODg1OTEzAGJyZWF0aHcAZW1waGFzaXMAQ2FrbQAkdmVyYgAjYQAwTQBjc0lTTzg4NTkxNABDYW5zAF8wQ28AJG5vdW4AYnJlYWsAI2UAbWJyb2xhAGNzSVNPODg1OTE1ACRwYXN0AGNvbnNvbmFudHMAQ2FyaQAjaQBtZXRhZGF0YQBjc0lTTzg4NTkxNgBfMEMwAGtsYXR0AGJyACNvAENoYW0AJHZlcmJleHRlbmQAXzBDAGNzSVNPTGF0aW4xACN1AENoZXIAJGNhcGl0YWwAbGkAJXMlYyVzJWMAZmFzdF90ZXN0MgBjc0lTT0xhdGluMgBDaXJ0ACRhbGxjYXBzAE1pc3NpbmcgZmlsZTogJXMAXzBhbmQAc3BlZWQAZGQAY3NJU09MYXRpbjMAQ29wdAAkYWNjZW50AG1haW50YWluZXIAXyVkQ28AcGhvbmVtZXRhYmxlIGlzIG1pc3NpbmcAaW1nAGNzSVNPTGF0aW40AENwcnQAc3RhdHVzACRzZW50ZW5jZQB0ZABLZXl3b3JkICdwaG9uZW1lJyBleHBlY3RlZABjc0lTT0xhdGluNQBfJWRDMAAkb25seQBUb28gbWFueSBwcm9jZWR1cmVzAEN5cmwAbWFsZQBfJWRDAGNzSVNPTGF0aW42AGgxACRvbmx5cwBDeXJzACVzJXMlcyVzAGZlbWFsZQBjc0lTT0xhdGluQXJhYmljACUuM2RQAGgyACVzJXMlYyVzACRzdGVtAE1pc3NpbmcgJ2VuZHBob25lbWUnIGJlZm9yZSBlbmQtb2YtZmlsZQAlZCAlZCAlZCAlZCAlZABjc0lTT0xhdGluQ3lyaWxsaWMARGV2YQBoMwBfJWRmeABNb3JlIHRoYW4gb25lIHBob25lbWUgdHlwZTogJXMARG9ncgBjc0lTT0xhdGluR3JlZWsAJGF0ZW5kAE5VTEwAaDQAY3NJU09MYXRpbkhlYnJldwBEc3J0ACRhdHN0YXJ0ACVkICVzICVzAGhyAF8lZGYATlVMTABfJWQlY3gAc2NyaXB0AER1cGwAJG5hdGl2ZQAhdiVjAGNzS09JOFIAJXgAc3R5bGUARWd5ZAAlc20lZABfJWQlYwAkPwBjc1RJUzYyMABmb250AEVneWgAYSBwaG9uZW1lIHR5cGUgb3IgbWFubmVyIG9mIGFydGljdWxhdGlvbiBtdXN0IGJlIHNwZWNpZmllZCBiZWZvcmUgc3RhcnR0eXBlACR0ZXh0bW9kZQBfJWRlACVzZiVkAGNzVVRGOAAlcy92b2ljZXMvJXMARWd5cABfJWRvAGEgcGhvbmVtZSB0eXBlIG9yIG1hbm5lciBvZiBhcnRpY3VsYXRpb24gbXVzdCBiZSBzcGVjaWZpZWQgYmVmb3JlIGVuZHR5cGUAJHBob25lbWVtb2RlAGNzVW5pY29kZQBiAGVuZHR5cGUgbXVzdCBlcXVhbCBzdGFydHR5cGUgZm9yIGNvbnNvbmFudHMAdW5wcgBhbGwAaQBhcmFiaWMAXyVkYQBFbGJhAG5vcHJlZml4AHZvaWNpbmdzd2l0Y2ggY2Fubm90IGJlIHVzZWQgb24gdm93ZWxzAGVtAEV0aGkAXyVkAGN5cmlsbGljAHN0cmVzcyBwaG9uZW1lcyBjYW4ndCBjb250YWluIHByb2dyYW0gaW5zdHJ1Y3Rpb25zAEdlb2sAZ3JlZWsAY29kZQBfJWRYJWMAd19hbHQxAFdhcm5pbmc6IG1heGltdW0gbnVtYmVyICVkIG9mIChOX1ZPSUNFU19MSVNUID0gJWQgLSAxKSByZWFjaGVkCgAlcyVjJXMAR2VvcgBfJWRYZgAlYyVkJWMAZ3JlZWs4AHdfYWx0MgBNaXNzaW5nICdlbmRwaG9uZW1lJyBiZWZvcmUgJyVzJwBoZWJyZXcAZGVmYXVsdABHbGFnAF8lZFgATWlzc2luZyBFTkRJRgB3X2FsdDMARXJyb3IgKCVzKTogZ2VuZGVyIGF0dHJpYnV0ZSBzcGVjaWZpZWQgb24gYSBsYW5ndWFnZSBmaWxlCgBzaWxlbnQAR29uZwAnZW5kcGhvbmVtZScgbm90IGV4cGVjdGVkIGhlcmUAaXNvLWNlbHRpYwB3X2FsdDQAX29yZDIwAHgtc29mdABHb25tAFBob25lbWUgdHlwZSBpcyBtaXNzaW5nAF9vcmQAaXNvLWlyLTYAd19hbHQ1AHNvZnQAR290aAB3X2FsdDYAaXNvLWlyLTEwMABCYWQgcGhvbmVtZSBuYW1lICclcycAd19hbHQAJXMlcyVzAEdyYW4AbG91ZABpc28taXItMTAxACVzOiAnJXMnLgBHcmVrAHgtbG91ZABpc28taXItMTA5AHBfYWx0MQB4LXNsb3cAR3VqcgBwX2FsdDIAaXNvLWlyLTExMABFeHBlY3RlZCAnKCcAc2xvdwBHdXJ1AGlzby1pci0xMjYAcF9hbHQzAEV4cGVjdGVkICcpJwBmYXN0AEhhbmcAVmFsdWUgJWQgaXMgZ3JlYXRlciB0aGFuIG1heGltdW0gJWQAaXNvLWlyLTEyNwBwX2FsdDQAeC1mYXN0AEhhbmkAaXNvLWlyLTEzOABwX2FsdDUAQ2Fubm90IGZpbmQgcGhvbmVtZSAnJXMnIHRvIGltcG9ydC4AeC1sb3cASGFubwBwX2FsdDYAaXNvLWlyLTE0NABQaG9uZW1lIGltcG9ydCB3aWxsIG92ZXJyaWRlIHNldCBwcm9wZXJ0aWVzLgBsb3cAcF9hbHQASGFucwBpc28taXItMTQ4AFBob25lbWUgcmVmZXJlbmNlIG5vdCBmb3VuZDogJyVzJwAlcyVzLnR4dABIYW50AGhpZ2gAaXNvLWlyLTE1NwBjb21waWxlOiB1bmtub3duIHBob25lbWUgdGFibGU6ICclcycAJXMlcwBIYXRyAHgtaGlnaABQaG9uZW1lIHByb2dyYW0gdG9vIGxhcmdlAGlzby1pci0xOTkASGVicgBpc28taXItMjI2AEV4cGVjdGVkIGEgY29uZGl0aW9uLCBub3QgJyVzJwBzcGFjZSAAQ2FuJ3QgYWxsb2NhdGUgbWVtb3J5CgBFeHBlY3RlZCBsaXN0IG9mIHN0cmVzcyBsZXZlbHMASGlyYQBsYXRpbjEAdGFiIAAJJWQgZW50cmllcwoASGx1dwBsYXRpbjIAVW5leHBlY3RlZCBrZXl3b3JkICclcycAdW5kZXJzY29yZSAAJTVkOiBVbmtub3duIGtleXdvcmQ6ICVzCgBIbW5nAHBob25lbWUAbGF0aW4zAGRvdWJsZS1xdW90ZSAAJTVkOiBNaXNzaW5nICcoJwoASHJrdABlbmRwaG9uZW1lAG1hbGUAbGF0aW40AEh1bmcAZmVtYWxlAEV4cGVjdGVkIEFORCwgT1IsIFRIRU4AbGF0aW41ACU1ZDogTmVlZCB0byBjb21waWxlIGRpY3Rpb25hcnkgYWdhaW4KAEluZHMAbmV1dHJhbABFTFNFIG5vdCBleHBlY3RlZABsYXRpbjYAJTVkOiBCYWQgcGhvbmVtZSBbJXNdIChVKyV4KSBpbjogJXMgICVzCgBVKyV4AEl0YWwASUYgYmxvY2sgaXMgdG9vIGxvbmcAeG1sOmxhbmcAbGF0aW44AHZhcmlhbnQARUxJRiBub3QgZXhwZWN0ZWQASmF2YQBsYXRpbjEwACU1ZDogRGljdGlvbmFyeSBsaW5lIGxlbmd0aCB3b3VsZCBvdmVyZmxvdyB0aGUgZGF0YSBidWZmZXI6ICVkCgBKcGFuAGFnZQBFTkRJRiBub3QgZXhwZWN0ZWQAbDEAJTVkOiBUd28gbWFueSBwYXJ0cyBpbiBhIG11bHRpLXdvcmQgZW50cnk6ICVkCgBnZW5kZXIASnVyYwBQYXJhbWV0ZXIgPiAxMjcAbDIALy8AJXMrJXMAS2FsaQAuTABQYXJhbWV0ZXIgPCAtMTI4AGwzAGd0AC5yZXBsYWNlAEthbmEAUGFyYW1ldGVyID4gMjU1AGw0AGx0AEtoYXIALmdyb3VwAERGVABsNQAweCV4ACVzLyVzLndhdgBLaG1yAGFtcABsNgBxdW90AENhbid0IHJlYWQgZmlsZTogJXMAS2hvagBsOAAlNWQ6IEdyb3VwIG5hbWUgbG9uZ2VyIHRoYW4gMiBieXRlcyAoVVRGOCkARmlsZSBub3QgU1BFQyBvciBSSUZGOiAlcwBuYnNwAEtuZGEAbDEwAApFeGNlZWRlZCBsaW1pdCBvZiBydWxlcyAoJWQpIGluIGdyb3VwICclcycKAHVzAGFwb3MAS29yZQAlYyAgMHglLjV4ICAlcwoAAMDg8P8fDwdwCnEKAAAoACkAWwBdAHsAfQA8AD4AIgAnAGAAqwC7AAowCzA84A=="),C(e,93904,"ICAgICAgICAgICYlKyNTRFpBTCEgQD9KTktWP1RYP1dBQkNIRkdZPT0sLCcqICAAIAAhACIAsAIkACUA5gDIAigAKQB+AisAzAItAC4ALwBSAjEAMgBcAjQANQA2ADcAdQI5ANACsgI8AD0APgCUAlkCUQKyA+cA8ABbAkYAYgInAWoCXwJLAGsCcQJLAVQCpgNjAoACgwK4A4oCjAJTAccD+ACSAioDXABdAF4AXwBgAGEAYgBjAGQAZQBmAGECaABpAGoAawBsAG0AbgBvAHAAcQByAHMAdAB1AHYAdwB4AHkAegB7AHwAfQADA38APT0sLCcnAAMCBAUGBxoAAAAAAQECAwMEBQYHBwgJCgsAAAEBAgIDAwQFBgcHCAkKAAABAgMDAwQFBgcHBwgJClNldFdvcmRTdHJlc3MA5ADrAO8A9gD8AP8AAAAAAAAAYWFhYWFhYWNlZWVlaWlpaWRub29vb28Ab3V1dXV5dHNhYWFhYWFhY2VlZWVpaWlpZG5vb29vbwBvdXV1dXl0eWFhYWFhYWNjY2NjY2NjZGRkZGVlZWVlZWVlZWVnZ2dnZ2dnZ2hoaGhpaWlpaWlpaWlpaWlqamtra2xsbGxsbGxsbGxubm5ubm5ubm5vb29vb29vb3JycnJycnNzc3Nzc3NzdHR0dHR0dXV1dXV1dXV1dXV1d3d5eXl6enp6enpzYmJiYgAAb2NjZGRkZGRlZWVmZmdnaGlpa2tsbG1ubm9vb29vcHB5AABzc3R0dHR1dXV2eXl6enp6enp6AAAAd3R0dGtkZGRsbGxubm5hYWlpb291dXV1dXV1dXV1ZWFhYWFhYWdnZ2dra29vb296empkZGRnZ3d3bm5hYWFhb29hYWFhZWVlZWlpaWlvb29vcnJycnV1dXVzc3R0eXloaG5kb296emFhZWVvb29vb29vb3l5bG50amRxYWNjbHRzegAAYnV2ZWVqanFxcnJ5eWFhYWJvY2RkZWVlZWVl"),C(e,94846,"TG9va3VwRGljdDIAAAAAAAAAgACBAIIAgwCEAIUAhgCHAIgAiQCKAIsAjACNAI4AjwCQAJEAkgCTAJQAlQCWAJcAmACZAJoAmwCcAJ0AngCfAKAAoQCiAKMApAClAKYApwCoAKkAqgCrAKwArQCuAK8AsACxALIAswC0ALUAtgC3ALgAuQC6ALsAvAC9AL4AvwDAAMEAwgDDAMQAxQDGAMcAyADJAMoAywDMAM0AzgDPANAA0QDSANMA1ADVANYA1wDYANkA2gDbANwA3QDeAN8A4ADhAOIA4wDkAOUA5gDnAOgA6QDqAOsA7ADtAO4A7wDwAPEA8gDzAPQA9QD2APcA+AD5APoA+wD8AP0A/gD/AIAAgQCCAIMAhACFAIYAhwCIAIkAigCLAIwAjQCOAI8AkACRAJIAkwCUAJUAlgCXAJgAmQCaAJsAnACdAJ4AnwCgAAQB2AJBAaQAPQFaAacAqABgAV4BZAF5Aa0AfQF7AbAABQHbAkIBtAA+AVsBxwK4AGEBXwFlAXoB3QJ+AXwBVAHBAMIAAgHEADkBBgHHAAwByQAYAcsAGgHNAM4ADgEQAUMBRwHTANQAUAHWANcAWAFuAdoAcAHcAN0AYgHfAFUB4QDiAAMB5AA6AQcB5wANAekAGQHrABsB7QDuAA8BEQFEAUgB8wD0AFEB9gD3AFkBbwH6AHEB/AD9AGMB2QKAAIEAggCDAIQAhQCGAIcAiACJAIoAiwCMAI0AjgCPAJAAkQCSAJMAlACVAJYAlwCYAJkAmgCbAJwAnQCeAJ8AoAAmAdgCowCkAP3/JAGnAKgAMAFeAR4BNAGtAP3/ewGwACcBsgCzALQAtQAlAbcAuAAxAV8BHwE1Ab0A/f98AcAAwQDCAP3/xAAKAQgBxwDIAMkAygDLAMwAzQDOAM8A/f/RANIA0wDUACAB1gDXABwB2QDaANsA3ABsAVwB3wDgAOEA4gD9/+QACwEJAecA6ADpAOoA6wDsAO0A7gDvAP3/8QDyAPMA9AAhAfYA9wAdAfkA+gD7APwAbQFdAdkCgACBAIIAgwCEAIUAhgCHAIgAiQCKAIsAjACNAI4AjwCQAJEAkgCTAJQAlQCWAJcAmACZAJoAmwCcAJ0AngCfAKAABAE4AVYBpAAoATsBpwCoAGABEgEiAWYBrQB9Aa8AsAAFAdsCVwG0ACkBPAHHArgAYQETASMBZwFKAX4BSwEAAcEAwgDDAMQAxQDGAC4BDAHJABgBywAWAc0AzgAqARABRQFMATYB1ADVANYA1wDYAHIB2gDbANwAaAFqAd8AAQHhAOIA4wDkAOUA5gAvAQ0B6QAZAesAFwHtAO4AKwERAUYBTQE3AfQA9QD2APcA+ABzAfoA+wD8AGkBawHZAoAAgQCCAIMAhACFAIYAhwCIAIkAigCLAIwAjQCOAI8AkACRAJIAkwCUAJUAlgCXAJgAmQCaAJsAnACdAJ4AnwCgAAEEAgQDBAQEBQQGBAcECAQJBAoECwQMBK0ADgQPBBAEEQQSBBMEFAQVBBYEFwQYBBkEGgQbBBwEHQQeBB8EIAQhBCIEIwQkBCUEJgQnBCgEKQQqBCsELAQtBC4ELwQwBDEEMgQzBDQENQQ2BDcEOAQ5BDoEOwQ8BD0EPgQ/BEAEQQRCBEMERARFBEYERwRIBEkESgRLBEwETQROBE8EFiFRBFIEUwRUBFUEVgRXBFgEWQRaBFsEXASnAF4EXwSAAIEAggCDAIQAhQCGAIcAiACJAIoAiwCMAI0AjgCPAJAAkQCSAJMAlACVAJYAlwCYAJkAmgCbAJwAnQCeAJ8AoAD9//3//f+kAP3//f/9//3//f/9//3/DAatAP3//f/9//3//f/9//3//f/9//3//f/9//3/Gwb9//3//f8fBv3/IQYiBiMGJAYlBiYGJwYoBikGKgYrBiwGLQYuBi8GMAYxBjIGMwY0BjUGNgY3BjgGOQY6Bv3//f/9//3//f9ABkEGQgZDBkQGRQZGBkcGSAZJBkoGSwZMBk0GTgZPBlAGUQZSBv3//f/9//3//f/9//3//f/9//3//f/9//3/gACBAIIAgwCEAIUAhgCHAIgAiQCKAIsAjACNAI4AjwCQAJEAkgCTAJQAlQCWAJcAmACZAJoAmwCcAJ0AngCfAKAAGCAZIKMArCCvIKYApwCoAKkAegOrAKwArQD9/xUgsACxALIAswCEA4UDhgO3AIgDiQOKA7sAjAO9AI4DjwOQA5EDkgOTA5QDlQOWA5cDmAOZA5oDmwOcA50DngOfA6ADoQP9/6MDpAOlA6YDpwOoA6kDqgOrA6wDrQOuA68DsAOxA7IDswO0A7UDtgO3A7gDuQO6A7sDvAO9A74DvwPAA8EDwgPDA8QDxQPGA8cDyAPJA8oDywPMA80DzgP9/4AAgQCCAIMAhACFAIYAhwCIAIkAigCLAIwAjQCOAI8AkACRAJIAkwCUAJUAlgCXAJgAmQCaAJsAnACdAJ4AnwCgAP3/ogCjAKQApQCmAKcAqACpANcAqwCsAK0ArgCvALAAsQCyALMAtAC1ALYAtwC4ALkA9wC7ALwAvQC+AP3//f/9//3//f/9//3//f/9//3//f/9//3//f/9//3//f/9//3//f/9//3//f/9//3//f/9//3//f/9//3//f8XINAF0QXSBdMF1AXVBdYF1wXYBdkF2gXbBdwF3QXeBd8F4AXhBeIF4wXkBeUF5gXnBegF6QXqBf3//f8OIA8g/f+AAIEAggCDAIQAhQCGAIcAiACJAIoAiwCMAI0AjgCPAJAAkQCSAJMAlACVAJYAlwCYAJkAmgCbAJwAnQCeAJ8AoAChAKIAowCkAKUApgCnAKgAqQCqAKsArACtAK4ArwCwALEAsgCzALQAtQC2ALcAuAC5ALoAuwC8AL0AvgC/AMAAwQDCAMMAxADFAMYAxwDIAMkAygDLAMwAzQDOAM8AHgHRANIA0wDUANUA1gDXANgA2QDaANsA3AAwAV4B3wDgAOEA4gDjAOQA5QDmAOcA6ADpAOoA6wDsAO0A7gDvAB8B8QDyAPMA9AD1APYA9wD4APkA+gD7APwAMQFfAf8AgACBAIIAgwCEAIUAhgCHAIgAiQCKAIsAjACNAI4AjwCQAJEAkgCTAJQAlQCWAJcAmACZAJoAmwCcAJ0AngCfAKAABAESASIBKgEoATYBpwA7ARABYAFmAX0BrQBqAUoBsAAFARMBIwErASkBNwG3ADwBEQFhAWcBfgEVIGsBSwEAAcEAwgDDAMQAxQDGAC4BDAHJABgBywAWAc0AzgDPANAARQFMAdMA1ADVANYAaAHYAHIB2gDbANwA3QDeAN8AAQHhAOIA4wDkAOUA5gAvAQ0B6QAZAesAFwHtAO4A7wDwAEYBTQHzAPQA9QD2AGkB+ABzAfoA+wD8AP0A/gA4AYAAgQCCAIMAhACFAIYAhwCIAIkAigCLAIwAjQCOAI8AkACRAJIAkwCUAJUAlgCXAJgAmQCaAJsAnACdAJ4AnwCgAAEOAg4DDgQOBQ4GDgcOCA4JDgoOCw4MDg0ODg4PDhAOEQ4SDhMOFA4VDhYOFw4YDhkOGg4bDhwOHQ4eDh8OIA4hDiIOIw4kDiUOJg4nDigOKQ4qDisOLA4tDi4OLw4wDjEOMg4zDjQONQ42DjcOOA45DjoO/f/9//3//f8/DkAOQQ5CDkMORA5FDkYORw5IDkkOSg5LDkwOTQ5ODk8OUA5RDlIOUw5UDlUOVg5XDlgOWQ5aDlsO/f/9//3//f+AAIEAggCDAIQAhQCGAIcAiACJAIoAiwCMAI0AjgCPAJAAkQCSAJMAlACVAJYAlwCYAJkAmgCbAJwAnQCeAJ8AoAAdIKIAowCkAB4gpgCnANgAqQBWAasArACtAK4AxgCwALEAsgCzABwgtQC2ALcA+AC5AFcBuwC8AL0AvgDmAAQBLgEAAQYBxADFABgBEgEMAckAeQEWASIBNgEqATsBYAFDAUUB0wBMAdUA1gDXAHIBQQFaAWoB3AB7AX0B3wAFAS8BAQEHAeQA5QAZARMBDQHpAHoBFwEjATcBKwE8AWEBRAFGAfMATQH1APYA9wBzAUIBWwFrAfwAfAF+ARkggACBAIIAgwCEAIUAhgCHAIgAiQCKAIsAjACNAI4AjwCQAJEAkgCTAJQAlQCWAJcAmACZAJoAmwCcAJ0AngCfAKAAAh4DHqMACgELAQoepwCAHqkAgh4LHvIerQCuAHgBHh4fHiABIQFAHkEetgBWHoEeVx6DHmAe8x6EHoUeYR7AAMEAwgDDAMQAxQDGAMcAyADJAMoAywDMAM0AzgDPAHQB0QDSANMA1ADVANYAah7YANkA2gDbANwA3QB2Ad8A4ADhAOIA4wDkAOUA5gDnAOgA6QDqAOsA7ADtAO4A7wB1AfEA8gDzAPQA9QD2AGse+AD5APoA+wD8AP0AdwH/AIAAgQCCAIMAhACFAIYAhwCIAIkAigCLAIwAjQCOAI8AkACRAJIAkwCUAJUAlgCXAJgAmQCaAJsAnACdAJ4AnwCgAKEAogCjAKwgpQBgAacAYQGpAKoAqwCsAK0ArgCvALAAsQCyALMAfQG1ALYAtwB+AbkAugC7AFIBUwF4Ab8AwADBAMIAwwDEAMUAxgDHAMgAyQDKAMsAzADNAM4AzwDQANEA0gDTANQA1QDWANcA2ADZANoA2wDcAN0A3gDfAOAA4QDiAOMA5ADlAOYA5wDoAOkA6gDrAOwA7QDuAO8A8ADxAPIA8wD0APUA9gD3APgA+QD6APsA/AD9AP4A/wCAAIEAggCDAIQAhQCGAIcAiACJAIoAiwCMAI0AjgCPAJAAkQCSAJMAlACVAJYAlwCYAJkAmgCbAJwAnQCeAJ8AoAAEAQUBQQGsIB4gYAGnAGEBqQAYAqsAeQGtAHoBewGwALEADAFCAX0BHSC2ALcAfgENARkCuwBSAVMBeAF8AcAAwQDCAAIBxAAGAcYAxwDIAMkAygDLAMwAzQDOAM8AEAFDAdIA0wDUAFAB1gBaAXAB2QDaANsA3AAYARoC3wDgAOEA4gADAeQABwHmAOcA6ADpAOoA6wDsAO0A7gDvABEBRAHyAPMA9ABRAfYAWwFxAfkA+gD7APwAGQEbAv8AgACBAIIAgwCEAIUAhgCHAIgAiQCKAIsAjACNAI4AjwCQAJEAkgCTAJQAlQCWAJcAmACZAJoAmwCcAJ0AngCfAKAABAEFAUEBrCAeIGABpwBhAakAGAKrAHkBrQB6AXsBsACxAAwBQgF9AR0gtgC3AH4BDQEZArsAUgFTAXgBfAHAAMEAwgACAcQABgHGAMcAyADJAMoAywDMAM0AzgDPABABQwHSANMA1ABQAdYAWgFwAdkA2gDbANwAGAEaAt8A4ADhAOIAAwHkAAcB5gDnAOgA6QDqAOsA7ADtAO4A7wARAUQB8gDzAPQAUQH2AFsBcQH5APoA+wD8ABkBGwL/AP3//f/9//3//f/9//3//f/9//3//f/9//3//f/9//3//f/9//3//f/9//3//f/9//3//f/9//3//f/9//3//f/9/wEJAgkDCQUJBgkHCQgJCQkKCQsJDgkPCRAJDQkSCRMJFAkRCRUJFgkXCRgJGQkaCRsJHAkdCR4JHwkgCSEJIgkjCSQJJQkmCScJKAkpCSoJKwksCS0JLgkvCV8JMAkxCTIJMwk0CTUJNgk3CTgJOQkgAD4JPwlACUEJQglDCUYJRwlICUUJSglLCUwJSQlNCTwJZAn9//3//f/9//3/IAAwADEAMgAzADQANQA2ADcAOAA5AP3//f/9//3//f8CAAAAAAAAAAEAAAADAAAA//36+Pb08vDu7Oro5uTi4N7c2tjW1NLQzszKyMbEwsC+vLq4trSysK6sqqimpKKgnpyamJaUkpCOjIqIhoSCgH58enh2dHJwbmxqaGZkYmBeXFpYVlRSUE5MSkhGREJAPjw6ODY0MjAuLCooJiQiIB4cGhgWFBIQDgwKCAYEAgAAAgQGCAoMDhASFBYYGhweICIkJigqLC4wMjQ2ODo8PkBCREZISkxOUFJUVlhaXF5gYmRmaGpsbnBydHZ4enx+gIKEhoiKjI6QkpSWmJqcnqCipKaoqqyusLK0tri6vL7AwsTGyMrMztDS1NbY2tze4OLk5ujq7O7w8vT2+Pr9///06uDWzMO6saifl4+Hf3hxamNcVlBKRD85NC8rJiIeGhcTEA0LCAYEAgEAAAAAAAAAAQIDBAUHCAoMDhATFRcaHR8iJSgsLjAyNDY5Oz1AQkVHSkxPUVRXWl1fYmVoa25xdHh7foGFiIuPkpaZnaCkqKyvs7e7v8PHy8/T19vgz8zJxsPAvbm0sKunopyXkoyGgXt1b2ljXVdQSkQ+ODMtJyIcFxINCAQCAgEAAAAAAAEBAgMEBQcICgwNDxIUFhkbHiEkJyotMDQ2ODo8P0FDRkhLTVBSVVhaXWBjZmlsb3J1eHt+gYWIi4+SlpmdoKSorK+zt7u/w8fLz9PX2+D/+fTu6eTf2tXQy8bBvbizr6qmoZ2ZlZCMiISAfXl1cW5qZ2NgXVlWU1BNSkdEQT48OTc0Mi8tKygmJCIgHhwaGRcVFBIRDw4NDAoJCAcGBQUEAwICAQE="),C(e,99845,"AQECAgMEBAUGBwgJCgsMDg8QEhMVFxgaHB4g0M7NzMrIx8XDwcC9u7i1s7CtqqejoJ2ZlpKPi4eEgHx4dHBtaWVhXVlVUU1KRkI+Ozc0MS8tKigmJCIgHhwaGRcVFBIRDw4NDAoJCAcGBQUEAwICAQE="),C(e,99973,"AQECAgMEBAUGBwgJCgsMDg8QEhMVFxgaHB4gmJmZmpydn6Gkp6mssLO2ur7BxcnN0dTY3N/j5urt8PL19/n7/P3+/v////////79+/r49vPx7uzp5uTg3drX09DMyMTAvLi0sKyno5+alpGNiIR/e3ZybWllYFxYVFBMSERAPDk1Mi8rKCYjIB0aFxUSDw0KCAcFAwIBAAAAAAD+///////+/fz6+Pb08e7r6OXh3trW0s3JxL+6trCrpqGclpGLhoB7dW9qZF9ZVE9JRD86NTArJiIdGRURDQoHBAMBAAAAAAAAAAABAQIEBQcJCw0QEhUYGx4iJSktMTU6PkNITFFXW15iZWhrbnF0dnh7fH6AgYKDg4SEg4OCgXJycXFwb21samhmZGFfXFpXVFFOS0hFQj87ODUyLywpJiMgHRsYFhQSEA4MCwoJCAcHBwcHBgUFBQUFBQUFBQYHBwgJCgwNDxASFBYYGx0gIyYpLC8zNzs/Q0dMUVZbYGVqb3R5f4SJj5Wboaets7rAx87V3OPq8fX3+vz9/v///v79/fz7+/r6+fj49/f29vX09PPz8vLx8PDv7u7t7Ovq6uno5+bl5OPi4eDe3dzb2djW1dPS0M7My8nHxcPAvry5t7Wyr62qp6ShnpqXlJCNiYWBfXl1cW1oZGFeW1dUUU1KRkNAPDk1Mi4qJyMfHBgUEQ0LCQcFBAMCAQAAAAAAAAAAAQEBAgIDAwQEBQYGBwgICQoLDAwNDg8QERITFBYXGBkbHB0fICIjJSYoKSstLzAyNDY4Ojw+QEJER0lLTlBSVVdaXV9iZWdqbXBzdnl8f4KGiYyQk5aanaCjpqmsr7K1uLu+wcTHys3Q09bZ3N/i5Ofp7O7w8vT2+Pr7/X9/f4CBg4SHiYyPkpaZnaGlqq6yt7vAxcnN0tba3uLm6u3w8/X4+vv8/f7+/fz7+ff08Ozn4tzVzsa9tKmekoiCfXdybGZgWlROSUI8NzItKCQfGxgUEQ4MCQcGBQQEBAQFBggKDRAUGB0jKS83PkdQWmRwfIOFiIqMjo+RkpOTAEAIAEYSAAAAAAAAGAwAAARQEgZOFgAAAAAAACI0AAAEWBYGUhYAAAAAAAAiQAAAAFwIAFxQAAAAAAAATAgBAABWBABeQgAAAAAAACIKAAAAPgoAPhQAAAAAAAAcEAAABEQSBkQWAAAAAAAAHiwAAAZAEABCIAAAAAAAACASAAACRC4AKiAAAAAAAAAuOgAABE4YBkgWAAAAAAAAKjQAAARYIgBAIAAAAAAAAC5SAAAAOAwAOBQAAAAAAAAYDAAAAEYSAEYYAAAAAAAAIBQAAAkAAAAJAAAAEAAAABAAAAAQAAAAFwAAADcAAAAgAAAAACgYCAAKNCAUCgYmGA4EAAYAAAAHAAAACQAAAAkAAAAUAAAAFAAAABQAAAAZAAAA5iAUCA=="),C(e,101072,"ZgNmAWYCpgSmAiYEpoooAmoDagFqAqoCbgNuAW4CrgIpALMEdAN0AXQCtAS0AgAAdAR6A3oBegK6An4BOQC+AgAApgMAAKYBAADmAwAAaAEAAGgCAAAoAwAA6AEAAOkBAABpBAAAqgMAAKoBAAAqAwAA6gMAAOoBAABsAgAArAEAACwDAAAsAgAAbQIAAG0EAACuBAAArgMAAK4BAADuAwAALgAAAO6LAABvAgAAMAIwAAAAcQEAADECAADxAQAAMQMAAHEEAABzAQAAMwIAAPMBMwAAADMAAAC0AwAAtAEAAPQCAAC0igAAdwEAADcCAAD3AQAAeAEAAHgCAAA4AgAA+AEAADkCAAD5AQAAeQQAALoEAAC6AwAAugEAADoEAAD6AgAA+gMAAHwCAAB+AgAAAAB/AQAAPwMAAP8BOACmAAEAgQBnBQAAKAEpBWkF6gAAAEIFAADDAEMdAADvBGwFLABsAAQAAACtAG0FAADuBAUAbgCxBPEEMQXxj7IAAAByBQAAMwVzAPQEtJoAAAgAtwCOADcVAAA3BQAAzQB3ALcIOAUAAG8FyQAJAbkAOQX6BAoAewW7ALwAvgB+AD8FPwEAAAsBAADMAIwAAAAAAGcAAABsDW0ALwGwAHEAdgVMBEwc6Y8AAOnPOY4AADnO"),C(e,101586,"sQNZAlsCswO5A1MByQPGA4MCxQOSApQCfgJ8AgAAqgBhgLIAMoCzADOAuQAxgLoAb4CwAmiAsQJmgrICaoCzAnKAtAJ5grUCe4K2AoGCtwJ3gLgCeYDAApSCwQKVguACY4LhAmyA4gJzgOMCeIBwIDCAcSBpgHQgNIB1IDWAdiA2gHcgN4B4IDiAeSA5gHogK4B7IC2AfCA9gH0gKIB+ICmAfyBugIAgMECBIDFAgiAyQIMgM0CEIDRAhSA1QIYgNkCHIDdAiCA4QIkgOUCKICtAiyAtQIwgPUCNIChAjiApQJAgYUCRIGVAkiBvQJMgeECUIFlClSBoQJYga0CXIGxAmCBtQJkgbkCaIHBAmyBzQJwgdEAAAAAAaXhjbXZsZA=="),C(e,101888,"AQAAAAoAAABkAAAA6AMAAAUAAAAyAAAA9AEAAAAXCgkYExgY"),C(e,101936,"///////9+fXy7uvo5OHe2tjV0s/MycbEwb+8ure1s7CurKmopaOhn56bmZiWlJKRj42LiYiHhYOCgX9+fHt6eHd2dXNycXBvbm1ramloZ2ZlZGNiYWBfXl1cW1pZWVhXVlVUU1JSUVBQT05NTExLS0pJSEdHRkVFRENDQkJBQEA/Pj49PTw7Ozo6OTk4ODc2NjU1NDQ0MzIyMTEwMC8vLi4uLS0sLCwrKyopKCgoJycnJiYmJSUlJCQjIyMjIiIiISEhICAfHx8eHh4dHR0dHBwbGxsbGhoaGhkZGRgYGBgXFxcXFhYWFRUVFRQUFBQTExMSEhERERAQEBAQEA8PDw8ODg4NDQ0MDAwMCwsLCwoKCgkJCQgICAAAAAAAAAAAeHl4d3d2dnV0dHNycXBwb29ubWxrampoZ2dmZmZlZWNiYmFgYF9eXVtaW1pZWFZVVlVVVFJRUE9NTk5MTUtLSklHSEZFRUVDQUA/Pz89PTs7Ozo4OTo4NjU0NDU0NDIwLy8tLi0="),C(e,102336,"FhYWFhYWFhUVFRUUFBMTEhEQDw8PDw8PDwAAAAAAAABkeGRpZG5uZF9kaXhpbn2Ch3N9ZGl4S2RLaXhVS2RpeFVpX3N4ZF9kbnhfaWRzeGRkZGl4ZGlfc3huX2RpeGRpaXp9bmlkaXhkaWl6fW5pZGl4X2lkc3huZGRkeGRkZGRkZGRk"),C(e,102480,"ZJZkaW5zbm5uZGmWaW59h4xzh2RpllppWnqHZFpkaZZkaWR6h2RkZGmWZGlpc4duaWRplmRpaXqCeH1kaZZkaW56fXNuZGmWZGlpeod4aWRplmRpaXOHbmlkZGRkZGRkZGRkZA=="),C(e,102592,"bnhkbm5ubm5ubm54ZG5ubm5ubm5ueGRuZG5ubmRubnhkbm5ubm5ubm54ZG5ubm5ubm5ueGRubm5ubm5ubnhkbm5ubm5ubm54ZG5ubm5ubm5ueGRubm5ubm5ubnhkbm5ubm5ubg=="),C(e,102708,"rwAAAGQAAAAyAAAAMg=="),C(e,102744,"ZA=="),C(e,102764,"MjIoRlpkZGRGboeWZGRLZHiWAAAAAAAABwAAAA4AAAAVAAAAKAAAAFAAAAAAAAAAAFNBUFIAQwAAAAAARgAAAAAAAAABAAAAAgAAAAQAAAAPAAAAAAEAAAEBAAEBAgQAAAAAAPMAEAEAAQABAAEAAQAB8ADwAPA="),C(e,102898,"QABaAG4AgACPAJwAqQC1AMAAygDUAN0A5gDvAPcAAAEHAQ8BFgEeASUBLAEyATkBQAFGAUwBUgFYAV4BZAFqAW8BdQF6AYABhQGKAY8BlAGZAZ4BowGoAa0BsgG2AbsBwAHEAckBzQHRAdYB2gHeAeMB5wHrAe8B8wH3AfsBAAIDAgcCCwIPAhMCFwIbAh8CIgImAioCLQIxAjUCOAI8AkACQwJHAkoCTgJRAlQCWAJbAl8CYgJlAmkCbAJvAnMCdgJ5AnwCgAKDAoYCiQKMAo8CkgKWApkCnAKfAqICpQKoAqsCrgKxArQCtwK6Ar0CwALCAsUCyALLAs4C0QLUAtYC2QLcAt8C4gLkAucC6gLtAu8C8gL1AvcC+gL9AgADAgMFAwcDCgMNAw8DEgMVAxcDGgMcAx8DIQMkAycDKQMsAy4DMQMzAzYDOAM7Az0DQANCA0QDRwNJA0wDTgNRA1MDVQNYA1oDXQNfA2EDZANmA2gDawNtA28DcgN0A3YDeQN7A30DgAOCA4QDhgMAAAGqAqytAwQFsLGys7S0tgYHCLkJCrwMDQ4PEBESYWJjZGVmZ2hpamtsbW5vcHFyc3R1"),C(e,103360,"YAYAAPAGAABmCQAA5gkAAGYKAADmCgAAZgsAAOYLAABmDAAA5gwAAGYNAABQDgAA0A4AACAPAABAEAAAkBA="),C(e,103440,"5gDmAOYA5gAAAAAA5gDmAL4AqgC+AMgAAAAAAL4A8AC+AL4A0gDSAAAAAADSANIAyADIANIA0gAAAAAA5gDmAOYA5gDwAPAAAAAAAAQBBAGqAIwA3ADcAAAAAAD6AA4BoACMAMgAjAAAAAAA8ACgALQAtADSANIAAAAAAOYA8ACqANwAtAC0AAAAAAD6AA4BlgCCAMgAyAAAAAAADgEOAbYAjADcANwAAAAAAPgAEwGbALQA0gDSAAAAAAAOASwBwwMAAAAAAABsAAAAAAAAAKAAkQCbAJYAAAAAAMgA9QAnAAAAtwAAAAAAAADCugAAyADIAMgAyAAAAAAA0gDmAAABAgMEBQYHCAkKCwwNDg8QERITFBUWFxgZGgAAAAAAABscHR4fICEiIyQlJicoKSorAAAs"),C(e,103790,"LQAAAAAAAAAu"),C(e,103816,"Lw=="),C(e,103833,"MAAAAAAAMQ=="),C(e,103856,"Mg=="),C(e,103868,"MwAAAK0AAQBABgEADCAtAAAAAAC+AL4A0gDSAAAAAADmAPoAoACWAMgAyAAAAAAA+gAEAbkAwwDDAL4AAAAAANIA3AD6AMgA+gD6AAAAAAD6APoAMjM0Njk6PD0+P0BBQkNERkdJSktMTU5PUFFSU1RWAACWAIwAtAC0AAAAAADIAMgAbA=="),C(e,104016,"oADIALQAtAAAAAAA3ADwALQAoADIAMgAAAAAAPAA+gBjAAAAZgAAAGgAAABrAAAAcAAAAHQAAAB4AAAA/g=="),C(e,104096,"oACMAJYApQAAAAAA2gAxAZEAkQCqAKAAAAAAAEoBXgEuAAAALAAAACcAAADIAg=="),C(e,104160,"MTIzNTY3OTo7PD4/QEFCREVGR0hJSktMTU5PUFFSU1RW"),C(e,104208,"vgC0AOYA5gAAAAAA+gD6ABESExQWFxkaGxwdHyAhIiQlJicoKSosc3uDmwAAAAAAtAC0AL4AtAAAAAAA5gDwALQAtAC0AKAAAAAAAOYAtABABAAAMAQAADUEAAA4BAAAOQQAAD4EAABDBAAASwQAAE0EAABOBAAATwQAAFAEAABRBAAAVgQAAFcEAABdBAAAXgQ="),C(e,104368,"tACgAMgAyAAAAAAA3ADmAKAAhwDSANIAAAAAAAQBGAGgAIwAyADIAAAAAADcAOYAyADIAMgAyAAAAAAAyADIAKAAvgCvAK8AAAAAAMgA0gCqAHMA0gDwAAAAAAAEARgBqgCqALQAtAAAAAAA8AAEAZYAtADIAMgAAAAAANIA+gCWAJYAtAC0AAAAAAAsASwBoACHANwA3AAAAAAA+gAYAaAAqgDIAMgAAAAAAEABVAG0AKAA8ADwAAAAAAAEAQQBvgC0AMgA5gAAAAAA8AD6AJYAlgC0ALQA0gDmAOYA8ABhAAAA4AAAAOEAAACjHgAA4wAAAKEeAAADAQAAsR4AAK8eAACzHgAAtR4AALceAADiAAAApx4AAKUeAACpHgAAqx4AAK0eAABlAAAA6AAAAOkAAAC7HgAAvR4AALkeAADqAAAAwR4AAL8eAADDHgAAxR4AAMceAABpAAAA7AAAAO0AAADJHgAAKQEAAMseAABvAAAA8gAAAPMAAADPHgAA9QAAAM0eAAD0AAAA0x4AANEeAADVHgAA1x4AANkeAAChAQAA3R4AANseAADfHgAA4R4AAOMeAAB1AAAA+QAAAPoAAADnHgAAaQEAAOUeAACwAQAA6x4AAOkeAADtHgAA7x4AAPEeAAB5AAAA8x4AAP0AAAD3HgAA+R4AAPUe"),C(e,104896,"5gCWAOYA5gDmAAAA8AD6AAAAAAAnAAAAAAAAAAABAgM="),C(e,104945,"AQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGRo="),C(e,105072,"GxwdAAAeHyAhIiMkACUmAAAAACcAACgAKQAqACsAAAAAAAAsAC0ALgAAAAAALwAAADAAAAAAAAAAMQ=="),C(e,105170,"MgAz"),C(e,105195,"NAAAAAAANQA2"),C(e,105226,"NwA4ADkArQABAAwgAQ=="),C(e,105249,"AQIDAAQAAQIDAAQFBgIDAAQFBwEDAAQICQoDAAAICAoDAAALCwsLAAAMDAwMAAAMAQ4BEwEBAw8DDgYRBgMJAQsPCwEMCQwBDgYOCQ4ODg8OHA4DDxEPEg8PEBEQAREGEQkRDxETEiAS/38AAAAAlgCMANwA3AAAAAAABAEYAYKAeHRkZICAgIyAgKCrq4CAgA=="),C(e,105412,"yAAAAMgAAACQAQAAkAEAAJABAABYAgAAWAIAAFgC"),C(e,105456,"8AAAAKoAAACqAAAAqgAAAKoAAACqAAAAqgAAAKoAAACqAAAAAQIMAw0EDgULAAAAAQIDBAUGAAAAAAAACwwNDg=="),C(e,105540,"MgAAAK8AAABkAAAAMg=="),C(e,105568,"rw=="),C(e,105596,"EBAKEBY="),C(e,105620,"/38AANAHAAAsAQAAYwAAAGMAAABjAAAAAAAAANAH"),C(e,105668,"BA=="),C(e,105680,"QEFCQ0RFRkdISUpLTE1OT1BRUlNUVldYWVtcXV5gYWJkZWdoaWtsbm9xc3R2d3l7fH6AgoSFh4mLjY+Rk5WXmZueoKKkp6mrrrCztbi6vb/CxcfKzdDT1tnc3+Ll6Ozv8vb5/P7/"),C(e,105792,"//7+/v7+/v7+/v39/f38/Pz7+/v6+vn5+Pj39/b19fTz8/Lx7+3r6efl4+Hf3drY1dPQzcvIxcK/u7i1sq6rp6OgnJiUkIyIhH97d3JuaWRgXltYVlNRTkxKR0VCQD48OTc1MzEvLCooJiQiIB4dGxkXFRMSEA4MCwkHBgQDAQ=="),C(e,106064,"//7+/v7+/f38+/v6+fj39vX08vHv7uzq6efl4+Hf3NrY1dPRz83LycfFw8G/vbu5t7SysK2rqaakoZ+cmpeUko+MioeEgX57eHZzcGxpZmNgX11bWlhWVVNSUE9NTEpJSEZFRENCQD8+PTw7Ojk4Nzc2NTQ0MzIyMTAwLy8uLi4tLS0sLCwsLCwsKysrKywrKiopKCgnJiYlJCQjIyIhISAgHx4eHR0cHBsaGhkZGBgXFxYWFRUUFBMTEhISEREQEA8PDw4ODQ0NDAwLCwsKCgoJCQkICAgHBwcHBgYGBQUFBQQEBAQEAwMDAwICAgICAgEBAQEBAQ=="),C(e,106341,"RvIpAC8ALwBF8ikALwAuAEXyKAAuAC0ANPIoAC0tLAA08igAKysrADTyKAAqKioANPIoACkpKQA08igAAOf/zv+1/5z/g/9q/1H/N/8e/wX/7P7T/rr+of6I/m/+Vf48/iP+Cv7x/dj9v/2m/Y39dP1b/UL9Kf0Q/ff83vzF/Kz8k/x6/GH8SPwv/Bb8/fvk+8v7svuZ+4D7Z/tP+zb7HfsE++v60vq6+qH6iPpv+lf6Pvol+g369Pnb+cP5qvmR+Xn5YPlH+S/5Fvn++OX4zfi0+Jz4g/hr+FP4Ovgi+Ar48ffZ98H3qPeQ93j3YPdH9y/3F/f/9uf2z/a39p/2h/Zv9lf2P/Yn9g/29/Xf9cf1sPWY9YD1aPVR9Tn1IfUK9fL02/TD9Kz0lPR99GX0TvQ29B/0CPTw89nzwvOr85TzfPNl807zN/Mg8wnz8vLb8sTyrvKX8oDyafJS8jzyJfIO8vjx4fHL8bTxnvGH8XHxW/FE8S7xGPEB8evw1fC/8Knwk/B98GfwUfA78CXwEPD67+Tvzu+576Pvje9472LvTe847yLvDe/47uLuze647qPuju557mTuT+467iXuEO777eft0u297antlO2A7WvtV+1C7S7tGu0F7fHs3ezJ7LXsoeyN7HnsZexR7D3sKuwW7ALs7+vb68jrtOuh643reutn61TrQest6xrrB+v06uLqz+q86qnqluqE6nHqX+pM6jrqJ+oV6gPq8One6czpuumo6ZbphOly6WHpT+k96SzpGukI6ffo5ujU6MPosuig6I/ofuht6FzoS+g76CroGegI6Pjn5+fX58bntuel55Xnhed152XnVedF5zXnJecV5wXn9ubm5tfmx+a45qjmmeaK5nvma+Zc5k3mPuYw5iHmEuYD5vXl5uXY5cnlu+Wt5Z7lkOWC5XTlZuVY5UrlPOUv5SHlE+UG5fjk6+Te5NDkw+S25KnknOSP5ILkdeRo5FzkT+RD5DbkKuQd5BHkBeT54+3j4ePV48njveOx46XjmuOO44Pjd+Ns42HjVuNL4z/jNOMq4x/jFOMJ4//i9OLq4t/i1eLK4sDituKs4qLimOKO4oTie+Jx4mfiXuJU4kviQuI54i/iJuId4hTiDOID4vrh8eHp4eDh2OHQ4cfhv+G34a/hp+Gf4Zfhj+GI4YDheOFx4WrhYuFb4VThTeFG4T/hOOEx4SrhJOEd4RbhEOEK4QPh/eD34PHg6+Dl4N/g2eDU4M7gyeDD4L7guOCz4K7gqeCk4J/gmuCV4JHgjOCH4IPgfuB64HbgcuBu4GrgZuBi4F7gWuBX4FPgT+BM4EngReBC4D/gPOA54DbgM+Ax4C7gK+Ap4CfgJOAi4CDgHuAc4BrgGOAW4BTgEuAR4A/gDuAN4AvgCuAJ4AjgB+AG4AXgBeAE4APgA+AC4ALgAuAC4ALgAeAC4ALgAuAC4ALgA+AD4ATgBeAF4AbgB+AI4AngCuAL4A3gDuAP4BHgEuAU4BbgGOAa4BzgHuAg4CLgJOAn4CngK+Au4DHgM+A24DngPOA/4ELgReBJ4EzgT+BT4FfgWuBe4GLgZuBq4G7gcuB24HrgfuCD4IfgjOCR4JXgmuCf4KTgqeCu4LPguOC+4MPgyeDO4NTg2eDf4OXg6+Dx4Pfg/eAD4QrhEOEW4R3hJOEq4THhOOE/4UbhTeFU4VvhYuFq4XHheOGA4Yjhj+GX4Z/hp+Gv4bfhv+HH4dDh2OHg4enh8eH64QPiDOIU4h3iJuIv4jniQuJL4lTiXuJn4nHie+KE4o7imOKi4qzituLA4sri1eLf4uri9OL/4gnjFOMf4yrjNOM/40vjVuNh42zjd+OD447jmuOl47HjvePJ49Xj4ePt4/njBeQR5B3kKuQ25EPkT+Rc5GjkdeSC5I/knOSp5Lbkw+TQ5N7k6+T45AblE+Uh5S/lPOVK5VjlZuV05YLlkOWe5a3lu+XJ5djl5uX15QPmEuYh5jDmPuZN5lzma+Z75ormmeao5rjmx+bX5ubm9uYF5xXnJec150XnVedl53XnheeV56XntufG59fn5+f45wjoGegq6DvoS+hc6G3ofuiP6KDosujD6NTo5uj36AjpGuks6T3pT+lh6XLphOmW6ajpuunM6d7p8OkD6hXqJ+o66kzqX+px6oTqluqp6rzqz+ri6vTqB+sa6y3rQetU62freuuN66HrtOvI69vr7+sC7BbsKuw97FHsZex57I3soey17Mns3ezx7AXtGu0u7ULtV+1r7YDtlO2p7b3t0u3n7fvtEO4l7jruT+5k7nnuju6j7rjuze7i7vjuDe8i7zjvTe9i73jvje+j77nvzu/k7/rvEPAl8DvwUfBn8H3wk/Cp8L/w1fDr8AHxGPEu8UTxW/Fx8YfxnvG08cvx4fH48Q7yJfI88lLyafKA8pfyrvLE8tvy8vIJ8yDzN/NO82XzfPOU86vzwvPZ8/DzCPQf9Db0TvRl9H30lPSs9MP02/Ty9Ar1IfU59VH1aPWA9Zj1sPXH9d/19/UP9if2P/ZX9m/2h/af9rf2z/bn9v/2F/cv90f3YPd495D3qPfB99n38fcK+CL4OvhT+Gv4g/ic+LT4zfjl+P74Fvkv+Uf5YPl5+ZH5qvnD+dv59PkN+iX6PvpX+m/6iPqh+rr60vrr+gT7Hfs2+0/7Z/uA+5n7svvL++T7/fsW/C/8SPxh/Hr8k/ys/MX83vz3/BD9Kf1C/Vv9dP2N/ab9v/3Y/fH9Cv4j/jz+Vf5v/oj+of66/tP+7P4F/x7/N/9R/2r/g/+c/7X/zv/n/wAAGQAyAEsAZAB9AJYArwDJAOIA+wAUAS0BRgFfAXgBkQGrAcQB3QH2AQ8CKAJBAloCcwKMAqUCvgLXAvACCQMiAzsDVANtA4YDnwO4A9ED6gMDBBwENQROBGcEgASZBLEEygTjBPwEFQUuBUYFXwV4BZEFqQXCBdsF8wUMBiUGPQZWBm8GhwagBrkG0QbqBgIHGwczB0wHZAd9B5UHrQfGB94H9gcPCCcIPwhYCHAIiAigCLkI0QjpCAEJGQkxCUkJYQl5CZEJqQnBCdkJ8QkJCiEKOQpQCmgKgAqYCq8KxwrfCvYKDgslCz0LVAtsC4MLmwuyC8oL4Qv4CxAMJww+DFUMbAyEDJsMsgzJDOAM9wwODSUNPA1SDWkNgA2XDa4NxA3bDfINCA4fDjUOTA5iDnkOjw6lDrwO0g7oDv8OFQ8rD0EPVw9tD4MPmQ+vD8UP2w/wDwYQHBAyEEcQXRBzEIgQnhCzEMgQ3hDzEAgRHhEzEUgRXRFyEYcRnBGxEcYR2xHwEQUSGRIuEkMSVxJsEoASlRKpEr4S0hLmEvsSDxMjEzcTSxNfE3MThxObE68TwxPWE+oT/hMRFCUUOBRMFF8UcxSGFJkUrBS/FNMU5hT5FAwVHhUxFUQVVxVqFXwVjxWhFbQVxhXZFesV/RUQFiIWNBZGFlgWahZ8Fo4WnxaxFsMW1BbmFvgWCRcaFywXPRdOF2AXcReCF5MXpBe1F8UX1hfnF/gXCBgZGCkYOhhKGFsYaxh7GIsYmxirGLsYyxjbGOsY+xgKGRoZKRk5GUgZWBlnGXYZhRmVGaQZsxnCGdAZ3xnuGf0ZCxoaGigaNxpFGlMaYhpwGn4ajBqaGqgathrEGtEa3xrtGvoaCBsVGyIbMBs9G0obVxtkG3EbfhuLG5gbpBuxG70byhvWG+Mb7xv7GwccExwfHCscNxxDHE8cWxxmHHIcfRyJHJQcnxyqHLUcwRzMHNYc4RzsHPccAR0MHRYdIR0rHTYdQB1KHVQdXh1oHXIdfB2FHY8dmR2iHawdtR2+Hccd0R3aHeMd7B30Hf0dBh4PHhceIB4oHjAeOR5BHkkeUR5ZHmEeaR5xHngegB6IHo8elh6eHqUerB6zHroewR7IHs8e1h7cHuMe6h7wHvYe/R4DHwkfDx8VHxsfIR8nHywfMh83Hz0fQh9IH00fUh9XH1wfYR9mH2sfbx90H3kffR+CH4Yfih+OH5Iflh+aH54foh+mH6kfrR+xH7Qftx+7H74fwR/EH8cfyh/NH88f0h/VH9cf2R/cH94f4B/iH+Qf5h/oH+of7B/uH+8f8R/yH/Mf9R/2H/cf+B/5H/of+x/7H/wf/R/9H/4f/h/+H/4f/h//H/4f/h/+H/4f/h/9H/0f/B/7H/sf+h/5H/gf9x/2H/Uf8x/yH/Ef7x/uH+wf6h/oH+Yf5B/iH+Af3h/cH9kf1x/VH9Ifzx/NH8ofxx/EH8Efvh+7H7cftB+xH60fqR+mH6Ifnh+aH5Yfkh+OH4ofhh+CH30feR90H28fax9mH2EfXB9XH1IfTR9IH0IfPR83HzIfLB8nHyEfGx8VHw8fCR8DH/0e9h7wHuoe4x7cHtYezx7IHsEeuh6zHqwepR6eHpYejx6IHoAeeB5xHmkeYR5ZHlEeSR5BHjkeMB4oHiAeFx4PHgYe/R30Hewd4x3aHdEdxx2+HbUdrB2iHZkdjx2FHXwdch1oHV4dVB1KHUAdNh0rHSEdFh0MHQEd9xzsHOEc1hzMHMEctRyqHJ8clByJHH0cchxmHFscTxxDHDccKxwfHBMcBxz7G+8b4xvWG8obvRuxG6QbmBuLG34bcRtkG1cbShs9GzAbIhsVGwgb+hrtGt8a0RrEGrYaqBqaGowafhpwGmIaUxpFGjcaKBoaGgsa/RnuGd8Z0BnCGbMZpBmVGYUZdhlnGVgZSBk5GSkZGhkKGfsY6xjbGMsYuxirGJsYixh7GGsYWxhKGDoYKRgZGAgY+BfnF9YXxRe1F6QXkxeCF3EXYBdOFz0XLBcaFwkX+BbmFtQWwxaxFp8WjhZ8FmoWWBZGFjQWIhYQFv0V6xXZFcYVtBWhFY8VfBVqFVcVRBUxFR4VDBX5FOYU0xS/FKwUmRSGFHMUXxRMFDgUJRQRFP4T6hPWE8MTrxObE4cTcxNfE0sTNxMjEw8T+xLmEtISvhKpEpUSgBJsElcSQxIuEhkSBRLwEdsRxhGxEZwRhxFyEV0RSBEzER4RCBHzEN4QyBCzEJ4QiBBzEF0QRxAyEBwQBhDwD9sPxQ+vD5kPgw9tD1cPQQ8rDxUP/w7oDtIOvA6lDo8OeQ5iDkwONQ4fDggO8g3bDcQNrg2XDYANaQ1SDTwNJQ0ODfcM4AzJDLIMmwyEDGwMVQw+DCcMEAz4C+ELyguyC5sLgwtsC1QLPQslCw4L9grfCscKrwqYCoAKaApQCjkKIQoJCvEJ2QnBCakJkQl5CWEJSQkxCRkJAQnpCNEIuQigCIgIcAhYCD8IJwgPCPYH3gfGB60HlQd9B2QHTAczBxsHAgfqBtEGuQagBocGbwZWBj0GJQYMBvMF2wXCBakFkQV4BV8FRgUuBRUF/ATjBMoEsQSZBIAEZwROBDUEHAQDBOoD0QO4A58DhgNtA1QDOwMiAwkD8ALXAr4CpQKMAnMCWgJBAigCDwL2Ad0BxAGrAZEBeAFfAUYBLQEUAfsA4gDJAK8AlgB9AGQASwAyABkAMAAAADAAAABAAAAAUAAAAJAAAACgAAAAsAAAAMAAAACAm7XL3Ojt7Obczr+wo5iQjIuMj5KUlZKMg3hpWUk8MSopLTZEVml9j5+qsbKtpJaHeGlcU09PVV5reoiWoquwsa6ooJiRi4iJjZSdqLK7wMG9tKWSfGNKMh4OBQIFDx4wRFltf4yWnJ+fnZuZmZyhqbO/ytXc4N7YzLumj3dgSzouKCkvOkhZanqGkJSVkYmAdWtiXFpcYWl0gIqUmp6dmJCGfHFoYmBja3iIm6/C0t/m5+LXxrKchG9bS0A5Nzg9Q0pQVFZVUk1IQj8+QUlWZ3yTq8PZ6vb8+/Tn1cCqlIBxZF1aXGFocHd9f397dGthV05IRkhOWWZ1hJOfp6uqpJmLe2pbTkZDRU1aa3+SprjFz9PSzcS5raGWjomHh4qNkZKRjIR4aFVBLhwOBQEFDx80TWiBmrDBzdPT0Mi/taukn5ydoKWqrrGwq6OWh3ZjUUI2Ly0xOkhZa36OnKaqqaOYintsXVJKSEpQWmd1gi0AAAAmAAAALQAAAC0AAAA3AAAALQ=="),C(e,110928,"yv5w/hICZAHgAFkAFwD2/8b/8P/NAVcCGAK9AgIDXQLxAc0BMAKUAW4A4ACDAGgAn/+bABYBZv9z+6r94QJ9ALD9KQALAAn/9v9BAFwAUADQ/kcApwD//3oA6QChANX/FgHfAeUBlwEKAYoChgBQAOwARAAEAQ0BswA1AIwAEwElASgBaAABAZgANwG2AAcB9QB9ADoBjAAsAMsA5gAV/+L+FwBrAFwApf8mANABuwGwAGIA8Pxv9p346/vA+Ur6mPoT+0v8Jv0="),C(e,111162,"BgAHAAgACQAKAAsADQAOABAAEgAUABYAGQAcACAAIwAoAC0AMwA5AEAARwBQAFoAZQByAIAAjgCfALMAygDjAAABHAE+AWcBlQHHAQACOAJ+As8CcQOPAwAEcQT8BJ4FVgYfBwAI4Qj4CTsLrAw9DgAQwxHwE3cWWBl7HAAghSPfJ+4ssDL2OABArkekUIVbZmYzc/9/"),C(e,111324,"qMtoQQAAAACoy2jBAAAAAAAAAAAXCtQJkglQCQ8JzgiPCE8IEwjVB5oHYgcoB/MGvgaLBloGKwb9BdMFqQWBBVwFOAUWBfcE1wS7BKAEhgRuBFcEQQQtBBkEBwT1A+QD1APFA7YDqAOZA40DfwNxA2UDVwNLAz4DMgMkAxgDCwP+AvIC5ALYAssCvgKxAqQClwKLAn0CcgJkAlkCTAJAAjQCKAIcAhICBQL7AfAB5QHbAdABxgG7AbIBqAGeAZQBigGBAXcBbgFjAVsBUAFIAT0BNAErASABGAENAQQB+gDwAOcA3ADUAMgAwAC1AKwAoQCYAI4AhQB7AHEAaQBeAFYATABDADkAMQAnAB4AFgALAAQA+//y/+n/4P/X/87/xP+7/7L/qf+g/5X/jf+C/3r/cP9m/1z/Uv9J/z//Nf8r/yL/F/8O/wT/+v7x/uf+3f7T/sr+wP62/q3+o/6b/pD+h/59/nP+av5f/lb+TP5C/jj+Lf4j/hn+Df4D/vf97P3h/dX9yf29/bH9pf2Y/Yz9f/1z/Wb9Wf1M/T/9M/0k/Rn9Cv3//PH85fzY/Mz8vvyz/KT8mPyL/H78cfxi/FX8Rfw4/Cb8GfwG/Pb74vvQ+7r7pfuO+3b7XPtB+yT7Bvvl+sT6n/p5+lP6KPr++dD5oflw+Tz5CfnR+Jr4YPgm+Oj3q/ds9yz36/aq9mj2Jvbj9bAEdgRABA4E3wO0A4sDZQNBAx8DAAPiAsYCqwKSAnoCZAJOAjoCJwIVAgMC8wHjAdQBxgG4AasBnwGTAYcBfAFyAWgBXgFVAUwBQwE7ATMBLAEkAR0BFgEQAQkBAwH9APcA8gDtAOcA4gDdANkA1ADQAMwAxwDDAMAAvAC4ALQAsQCuAKoApwCkAKEAngCbAJkAlgCTAJEAjgCMAIkAhwCFAIMAgAB+AHwAegB4AHcAdQBzAHEAbwBuAGwAagBpAGcAZgBkAGMAYQBgAF8AXQBcAFsAWgBYAFcAVgBVAFQAUwBSAFAATwBOAE0ATABLAEsASgBJAEgARwBGAEUARABEAEMAQgBBAEAAQAA/AD4APQA9ADwAOwA7ADoAOQA5ADgAOAA3ADcANgA2ADUANQA0ADQAMwAzADIAMgAxADEAMAAwAC8ALwAuAC4ALQAtACwALAArACsAKgAqACkAKQApACkAKAAoACcAJwAmACYAJgAmACUAJQAkACQAJAAkACMAIwAjACMAIgAiACEAIQAhACEAIAAgACAAIAAfAB8AHwAfAB4AHgAeAB4AHQAdAB0AHQAcABwAHAAcABsAGwAxNkZyYW1lTWFuYWdlckltcGwAMTJGcmFtZU1hbmFnZXIAMjNTcGVlY2hXYXZlR2VuZXJhdG9ySW1wbAAxOVNwZWVjaFdhdmVHZW5lcmF0b3IAMTNXYXZlR2VuZXJhdG9y"),C(e,112416,"AwAAAAQAAAAEAAAABgAAAIP5ogBETm4A/CkVANFXJwDdNPUAYtvAADyZlQBBkEMAY1H+ALveqwC3YcUAOm4kANJNQgBJBuAACeouAByS0QDrHf4AKbEcAOg+pwD1NYIARLsuAJzphAC0JnAAQX5fANaROQBTgzkAnPQ5AItfhAAo+b0A+B87AN7/lwAPmAUAES/vAApaiwBtH20Az342AAnLJwBGT7cAnmY/AC3qXwC6J3UA5evHAD178QD3OQcAklKKAPtr6gAfsV8ACF2NADADVgB7/EYA8KtrACC8zwA29JoA46kdAF5hkQAIG+YAhZllAKAUXwCNQGgAgNj/ACdzTQAGBjEAylYVAMmocwB74mAAa4zAABnERwDNZ8MACejcAFmDKgCLdsQAphyWAESv3QAZV9EApT4FAAUH/wAzfj8AwjLoAJhP3gC7fTIAJj3DAB5r7wCf+F4ANR86AH/yygDxhx0AfJAhAGokfADVbvoAMC13ABU7QwC1FMYAwxmdAK3EwgAsTUEADABdAIZ9RgDjcS0Am8aaADNiAAC00nwAtKeXADdV1QDXPvYAoxAYAE12/ABknSoAcNerAGN8+AB6sFcAFxXnAMBJVgA71tkAp4Q4ACQjywDWincAWlQjAAAfuQDxChsAGc7fAJ8x/wBmHmoAmVdhAKz7RwB+f9gAImW3ADLoiQDmv2AA78TNAGw2CQBdP9QAFt7XAFg73gDem5IA0iIoACiG6ADiWE0AxsoyAAjjFgDgfcsAF8BQAPMdpwAY4FsALhM0AIMSYgCDSAEA9Y5bAK2wfwAe6fIASEpDABBn0wCq3dgArl9CAGphzgAKKKQA05m0AAam8gBcd38Ao8KDAGE8iACKc3gAr4xaAG/XvQAtpmMA9L/LAI2B7wAmwWcAVcpFAMrZNgAoqNIAwmGNABLJdwAEJhQAEkabAMRZxADIxUQATbKRAAAX8wDUQ60AKUnlAP3VEAAAvvwAHpTMAHDO7gATPvUA7PGAALPnwwDH+CgAkwWUAMFxPgAuCbMAC0XzAIgSnACrIHsALrWfAEeSwgB7Mi8ADFVtAHKnkABr5x8AMcuWAHkWSgBBeeIA9N+JAOiUlwDi5oQAmTGXAIjtawBfXzYAu/0OAEiatABnpGwAcXJCAI1dMgCfFbgAvOUJAI0xJQD3dDkAMAUcAA0MAQBLCGgALO5YAEeqkAB05wIAvdYkAPd9pgBuSHIAnxbvAI6UpgC0kfYA0VNRAM8K8gAgmDMA9Ut+ALJjaADdPl8AQF0DAIWJfwBVUikAN2TAAG3YEAAySDIAW0x1AE5x1ABFVG4ACwnBACr1aQAUZtUAJwedAF0EUAC0O9sA6nbFAIf5FwBJa30AHSe6AJZpKQDGzKwArRRUAJDiagCI2YkALHJQAASkvgB3B5QA8zBwAAD8JwDqcagAZsJJAGTgPQCX3YMAoz+XAEOU/QANhowAMUHeAJI5nQDdcIwAF7fnAAjfOwAVNysAXICgAFqAkwAQEZIAD+jYAGyArwDb/0sAOJAPAFkYdgBipRUAYcu7AMeJuQAQQL0A0vIEAEl1JwDrtvYA2yK7AAoUqgCJJi8AZIN2AAk7MwAOlBoAUTqqAB2jwgCv7a4AXCYSAG3CTQAtepwAwFaXAAM/gwAJ8PYAK0CMAG0xmQA5tAcADCAVANjDWwD1ksQAxq1LAE7KpQCnN80A5qk2AKuSlADdQmgAGWPeAHaM7wBoi1IA/Ns3AK6hqwDfFTEAAK6hAAz72gBkTWYA7QW3ACllMABXVr8AR/86AGr5uQB1vvMAKJPfAKuAMABmjPYABMsVAPoiBgDZ5B0APbOkAFcbjwA2zQkATkLpABO+pAAzI7UA8KoaAE9lqADSwaUACz8PAFt4zQAj+XYAe4sEAIkXcgDGplMAb27iAO/rAACbSlgAxNq3AKpmugB2z88A0QIdALHxLQCMmcEAw613AIZI2gD3XaAAxoD0AKzwLwDd7JoAP1y8ANDebQCQxx8AKtu2AKMlOgAAr5oArVOTALZXBAApLbQAS4B+ANoHpwB2qg4Ae1mhABYSKgDcty0A+uX9AInb/gCJvv0A5HZsAAap/AA+gHAAhW4VAP2H/wAoPgcAYWczACoYhgBNveoAs+evAI9tbgCVZzkAMb9bAITXSAAw3xYAxy1DACVhNQDJcM4AMMu4AL9s/QCkAKIABWzkAFrdoAAhb0cAYhLSALlchABwYUkAa1bgAJlSAQBQVTcAHtW3ADPxxAATbl8AXTDkAIUuqQAdssMAoTI2AAi3pADqsdQAFvchAI9p5AAn/3cADAOAAI1ALQBPzaAAIKWZALOi0wAvXQoAtPlCABHaywB9vtAAm9vBAKsXvQDKooEACGpcAC5VFwAnAFUAfxTwAOEHhgAUC2QAlkGNAIe+3gDa/SoAayW2AHuJNAAF8/4Aub+eAGhqTwBKKqgAT8RaAC34vADXWpgA9MeVAA1NjQAgOqYApFdfABQ/sQCAOJUAzCABAHHdhgDJ3rYAv2D1AE1lEQABB2sAjLCsALLA0ABRVUgAHvsOAJVywwCjBjsAwEA1AAbcewDgRcwATin6ANbKyADo80EAfGTeAJtk2ADZvjEApJfDAHdY1ABp48UA8NoTALo6PABGGEYAVXVfANK99QBuksYArC5dAA5E7QAcPkIAYcSHACn96QDn1vMAInzKAG+RNQAI4MUA/9eNAG5q4gCw/cYAkwjBAHxddABrrbIAzW6dAD5yewDGEWoA98+pAClz3wC1yboAtwBRAOKyDQB0uiQA5X1gAHTYigANFSwAgRgMAH5mlAABKRYAn3p2AP39vgBWRe8A2X42AOzZEwCLurkAxJf8ADGoJwDxbsMAlMU2ANioVgC0qLUAz8wOABKJLQBvVzQALFaJAJnO4wDWILkAa16qAD4qnAARX8wA/QtKAOH0+wCOO20A4oYsAOnUhAD8tKkA7+7RAC41yQAvOWEAOCFEABvZyACB/AoA+0pqAC8c2ABTtIQATpmMAFQizAAqVdwAwMbWAAsZlgAacLgAaZVkACZaYAA/Uu4AfxEPAPS1EQD8y/UANLwtADS87gDoXcwA3V5gAGeOmwCSM+8AyRe4AGFYmwDhV7wAUYPGANg+EADdcUgALRzdAK8YoQAhLEYAWfPXANl6mACeVMAAT4b6AFYG/ADlea4AiSI2ADitIgBnk9wAVeiqAIImOADK55sAUQ2kAJkzsQCp1w4AaQVIAGWy8AB/iKcAiEyXAPnRNgAhkrMAe4JKAJjPIQBAn9wA3EdVAOF0OgBn60IA/p3fAF7UXwB7Z6QAuqx6AFX2ogAriCMAQbpVAFluCAAhKoYAOUeDAInj5gDlntQASftAAP9W6QAcD8oAxVmKAJT6KwDTwcUAD8XPANtargBHxYYAhUNiACGGOwAseZQAEGGHACpMewCALBoAQ78SAIgmkAB4PIkAqMTkAOXbewDEOsIAJvTqAPdnigANkr8AZaMrAD2TsQC9fAsApFHcACfdYwBp4d0AmpQZAKgplQBozigACe20AESfIABOmMoAcIJjAH58IwAPuTIAp/WOABRW5wAh8QgAtZ0qAG9+TQClGVEAtfmrAILf1gCW3WEAFjYCAMQ6nwCDoqEAcu1tADmNegCCuKkAazJcAEYnWwAANO0A0gB3APz0VQABWU0A4HGA"),C(e,115203,"QPsh+T8AAAAALUR0PgAAAICYRvg8AAAAYFHMeDsAAACAgxvwOQAAAEAgJXo4AAAAgCKC4zYAAAAAHfNpNf6CK2VHFWdAAAAAAAAAOEMAAPr+Qi52vzo7nrya9wy9vf3/////3z88VFVVVVXFP5ErF89VVaU/F9CkZxERgT8AAAAAAADIQu85+v5CLuY/JMSC/72/zj+19AzXCGusP8xQRtKrsoM/hDpOm+DXVT8="),C(e,115390,"8D9uv4gaTzubPDUz+6k99u8/XdzYnBNgcbxhgHc+muzvP9FmhxB6XpC8hX9u6BXj7z8T9mc1UtKMPHSFFdOw2e8/+o75I4DOi7ze9t0pa9DvP2HI5mFO92A8yJt1GEXH7z+Z0zNb5KOQPIPzxso+vu8/bXuDXaaalzwPiflsWLXvP/zv/ZIatY4890dyK5Ks7z/RnC9wPb4+PKLR0zLso+8/C26QiTQDarwb0/6vZpvvPw69LypSVpW8UVsS0AGT7z9V6k6M74BQvMwxbMC9iu8/FvTVuSPJkbzgLamumoLvP69VXOnj04A8UY6lyJh67z9Ik6XqFRuAvHtRfTy4cu8/PTLeVfAfj7zqjYw4+WrvP79TEz+MiYs8dctv61tj7z8m6xF2nNmWvNRcBITgW+8/YC86PvfsmjyquWgxh1TvP504hsuC54+8Hdn8IlBN7z+Nw6ZEQW+KPNaMYog7Ru8/fQTksAV6gDyW3H2RST/vP5SoqOP9jpY8OGJ1bno47z99SHTyGF6HPD+msk/OMe8/8ucfmCtHgDzdfOJlRSvvP14IcT97uJa8gWP14d8k7z8xqwlt4feCPOHeH/WdHu8/+r9vGpshPbyQ2drQfxjvP7QKDHKCN4s8CwPkpoUS7z+Py86JkhRuPFYvPqmvDO8/tquwTXVNgzwVtzEK/gbvP0x0rOIBQoY8MdhM/HAB7z9K+NNdOd2PPP8WZLII/O4/BFuOO4Cjhrzxn5JfxfbuP2hQS8ztSpK8y6k6N6fx7j+OLVEb+AeZvGbYBW2u7O4/0jaUPujRcbz3n+U02+fuPxUbzrMZGZm85agTwy3j7j9tTCqnSJ+FPCI0Ekym3u4/imkoemASk7wcgKwERdruP1uJF0iPp1i8Ki73IQrW7j8bmklnmyx8vJeoUNn10e4/EazCYO1jQzwtiWFgCM7uP+9kBjsJZpY8VwAd7UHK7j95A6Ha4cxuPNA8wbWixu4/MBIPP47/kzze09fwKsPuP7CvervOkHY8Jyo21dq/7j934FTrvR2TPA3d/ZmyvO4/jqNxADSUj7ynLJ12srnuP0mjk9zM3oe8QmbPotq27j9fOA+9xt54vIJPnVYrtO4/9lx77EYShrwPkl3KpLHuP47X/RgFNZM82ie1Nkev7j8Fm4ovt5h7PP3Hl9QSre4/CVQc4uFjkDwpVEjdB6vuP+rGGVCFxzQ8t0ZZiiap7j81wGQr5jKUPEghrRVvp+4/n3aZYUrkjLwJ3Ha54aXuP6hN7zvFM4y8hVU6sH6k7j+u6SuJeFOEvCDDzDRGo+4/WFhWeN3Ok7wlIlWCOKLuP2QZfoCqEFc8c6lM1FWh7j8oIl6/77OTvM07f2aeoO4/grk0h60Sary/2gt1EqDuP+6pbbjvZ2O8LxplPLKf7j9RiOBUPdyAvISUUfl9n+4/zz5afmQfeLx0X+zodZ/uP7B9i8BK7oa8dIGlSJqf7j+K5lUeMhmGvMlnQlbrn+4/09QJXsuckDw/Xd5PaaDuPx2lTbncMnu8hwHrcxSh7j9rwGdU/eyUPDLBMAHtoe4/VWzWq+HrZTxiTs8286LuP0LPsy/FoYi8Eho+VCek7j80NzvxtmmTvBPOTJmJpe4/Hv8ZOoRegLytxyNGGqfuP25XcthQ1JS87ZJEm9mo7j8Aig5bZ62QPJlmitnHqu4/tOrwwS+3jTzboCpC5azuP//nxZxgtmW8jES1FjKv7j9EX/NZg/Z7PDZ3FZmuse4/gz0epx8Jk7zG/5ELW7TuPykebIu4qV285cXNsDe37j9ZuZB8+SNsvA9SyMtEuu4/qvn0IkNDkrxQTt6fgr3uP0uOZtdsyoW8ugfKcPHA7j8nzpEr/K9xPJDwo4KRxO4/u3MK4TXSbTwjI+MZY8juP2MiYiIExYe8ZeVde2bM7j/VMeLjhhyLPDMtSuyb0O4/Fbu809G7kbxdJT6yA9XuP9Ix7pwxzJA8WLMwE57Z7j+zWnNuhGmEPL/9eVVr3u4/tJ2Ol83fgrx689O/a+PuP4czy5J3Gow8rdNamZ/o7j/62dFKj3uQvGa2jSkH7u4/uq7cVtnDVbz7FU+4ovPuP0D2pj0OpJC8OlnljXL57j80k6049NZovEde+/J2/+4/NYpYa+LukbxKBqEwsAXvP83dXwrX/3Q80sFLkB4M7z+smJL6+72RvAke11vCEu8/swyvMK5uczycUoXdmxnvP5T9n1wy4448etD/X6sg7z+sWQnRj+CEPEvRVy7xJ+8/ZxpOOK/NYzy15waUbS/vP2gZkmwsa2c8aZDv3CA37z/StcyDGIqAvPrDXVULP+8/b/r/P12tj7x8iQdKLUfvP0mpdTiuDZC88okNCIdP7z+nBz2mhaN0PIek+9wYWO8/DyJAIJ6RgryYg8kW42DvP6ySwdVQWo48hTLbA+Zp7z9LawGsWTqEPGC0AfMhc+8/Hz60ByHVgrxfm3szl3zvP8kNRzu5Kom8KaH1FEaG7z/TiDpgBLZ0PPY/i+cukO8/cXKdUezFgzyDTMf7UZrvP/CR048S94+82pCkoq+k7z99dCPimK6NvPFnji1Ir+8/CCCqQbzDjjwnWmHuG7rvPzLrqcOUK4Q8l7prNyvF7z/uhdExqWSKPEBFblt20O8/7eM75Lo3jrwUvpyt/dvvP53NkU07iXc82JCegcHn7z+JzGBBwQVTPPFxjyvC8+8/EhETFBUWFxgZGhscHR4fICERIiMkESUmJygpKissES0uLxAQMBAQEBAQEBAxMjMQNDUQEBERERERERERERERERERERERERERERERERE2ERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERNxERERE4ETk6Ozw9PhERERERERERERERERERERERERERERERERERERERERERERERERERERERERE/EBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEUBBEUJDREVGR0hJShFLTE1OT1BREFJTVFVWV1hZWltcXRBeX2AQERERYWJjEBAQEBAQEBAQEBERERFkEBAQEBAQEBAQEBAQEBAQERFlEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQERFmZxAQaGkREREREREREREREREREREREREREREREWoREWsQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEWxtEBAQEBAQEBAQbhAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQb3BxchAQEBAQEBAQc3R1EBAQEBB2dxAQEBB4EBB5EBAQEBAQEBAQEBAQEBA="),C(e,117968,"//////////////////////////////////////////8AAAAAAAAAAP7//wf+//8HAAAAAAAEIAT//3////9//////////////////////////////////8P/AwAfUA=="),C(e,118072,"IAAAAAAA37xA1///+////////////7///////////////////////wP8///////////////////////////+////fwL//////wEAAAAA/7+2AP///4cHAAAA/wf//////////v/D////////////////7x/+4f+fAAD///////8A4P///////////////wMA//////8HMAT////8/x8AAP///wH/BwAAAAAAAP//3z8AAPD/+AP////////////v/9/h/8///v/vn/n///3F459ZgLDP/wMQ7of5///9bcOHGQJewP8/AO6/+////e3jvxsBAM//AB7un/n///3t458ZwLDP/wIA7Mc91hjH/8PHHYEAwP8AAO/f/f///f/j3x1gB8//AADv3/3///3v498dYEDP/wYA79/9/////+ffXfCAz/8A/Oz/f/z///svf4Bf/8D/DAD+/////3//Bz8g/wMAAAAA1vf//6///ztfIP/zAAAAAAEAAAD/AwAA//7///8f/v8D///+////HwAAAAAAAAAA////////f/n/A////////////z//////vyD///////f///////////89fz3//////z3/////PX89/3//////////Pf//////////BwAAAAD//wAA/////////////z8//v//////////////////////////////////////////////////////////n////v//B////////////8f/Af/fDwD//w8A//8PAP/fDQD////////P//8BgBD/AwAAAAD/A///////////////Af//////B///////////PwD///9//w//AcD/////Px8A//////8P////A/8DAAAAAP///w//////////f/7/HwD/A/8DgA=="),C(e,118768,"////////7//vD/8DAAAAAP//////8////////7//AwD///////9/AP/j//////8//wH//////+cAAAAAAN5vBP///////////////////////////////wAAAACA/x8A//8/P/////8/P/+q////P////////99f3B/PD/8f3B8="),C(e,118910,"AoAAAP8f"),C(e,118928,"hPwvPlC9//PgQwAA//////8B"),C(e,118982,"wP///////wMAAP//////f///////f/////////////////////8feAwA/////78g/////////4AAAP//fwB/f39/f39/f/////8AAAAAAIA="),C(e,119088,"4AAAAP4DPh/+////////////f+D+//////////////fg///////+/////////////38AAP///wcAAAAAAAD///////////////////////////////8/"),C(e,119184,"////////////////////////////////////////AAD//////////////////////x8AAAAAAAAAAP//////P/8f////DwAA//////9/8I///////////////////wAAAACA//z////////////////5////////fAAAAAAAgP+//////wAAAP///////w8A//////////8vAP8DAAD86P//////B/////8HAP///x/////////3/wCA/wP///9/////////fwD/P/8D//9//P////////9/BQAAOP//PAB+fn4Af3////////f/AP///////////////////wf/A///////////////////////////DwD//3/4//////8P/////////////////z//////////////////AwAAAAB/APjg//1/X9v/////////////////AwAAAPj///////////////8/AAD///////////z///////8AAAAAAP8P"),C(e,119582,"3/////////////////////8fAAD/A/7//wf+//8HwP////////////9//Pz8HAAAAAD/7///f///t/8//z8AAAAA////////////////////BwAAAAAAAAAA////////Hw=="),C(e,119712,"////H////////wEAAAAAAP////8A4P///wf//////wf///8//////w//PgAAAAAA/////////////////////////z//A/////8P/////w///////wD///////8P"),C(e,119824,"////////fwD//z8A/w=="),C(e,119856,"P/3/////v5H//z8A//9/AP///38AAAAAAAAAAP//NwD//z8A////AwAAAAAAAAAA/////////8AAAAAAAAAAAG/w7/7//z8AAAAAAP///x////8fAAAAAP/+//8fAAAA////////PwD//z8A//8HAP//Aw=="),C(e,119984,"////////////AQAAAAAAAP///////wcA////////BwD//////wD/Aw=="),C(e,120048,"////H4AA//8/"),C(e,120076,"//9/AP//////////PwAAAMD/AAD8////////AQAA////Af8D////////x/9wAP////9HAP//////////HgD/FwAAAAD///v///+fQAAAAAAAAAAAf73/v/8B/////////wH/A++f+f///e3jnxmB4A8="),C(e,120208,"//////////+7B/+DAAAAAP//////////swD/Aw=="),C(e,120256,"////////P38AAAA/AAAAAP////////9/EQD/AwAAAAD///////8/Af8DAAAAAAAA////5/8H/wM="),C(e,120336,"/////////wE="),C(e,120356,"////////////AwCA"),C(e,120388,"//z///////waAAAA////////538AAP///////////yAAAAAA/////////wH//f////9/fwEA/wMAAPz////8///+fw=="),C(e,120464,"f/v/////f7TLAP8Dv/3///9/ewH/Aw=="),C(e,120524,"//9/AP////////////////////////8D"),C(e,120560,"/////////////////38AAP///////////////////////////////w8="),C(e,120624,"//////9/"),C(e,120656,"//////////9/"),C(e,120688,"/////////wH///9//wM="),C(e,120714,"////PwAA////////AAAPAP8D+P//4P//"),C(e,120760,"//////////8="),C(e,120784,"////////////h/////////+A//8AAAAAAAAAAAsAAAD/////////////////////////////////////////AP///////////////////////////////////////wcA////fwAAAAAAAAcA8AD/////////////////////////////////////////////////////////////////D/////////////////8H/x//Af9D"),C(e,120976,"/////////////9///////////99k3v/r7/////////+/59/f////e1/8/f//////////////////////////////////////////////////////P/////3///f////3///f////3///f////3/////9/////f//98////////9////52wc="),C(e,121136,"//////8fgD//Qw=="),C(e,121192,"//////8P/wP///////////////////////////////8fAAAAAAAAAP//////////jwj/Aw=="),C(e,121264,"7////5b+9wqE6paqlvf3Xv/7/w/u+/8P"),C(e,121302,"////A////wP///8D"),C(e,121328,"/////////////////////////////////////////////////////////////////wABAgMEBQYHCAn/////////CgsMDQ4PEBESExQVFhcYGRobHB0eHyAhIiP///////8KCwwNDg8QERITFBUWFxgZGhscHR4fICEiI/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////8="),C(e,121601,"ARcCHRgTAx4bGQsUCAQNHxYcEhoKBwwVEQkGEAUPDt4SBJUAAAAA////////////////INsBABQAAABDLlVURi04"),C(e,121696,"TENfQ1RZUEUAAAAATENfTlVNRVJJQwAATENfVElNRQAAAAAATENfQ09MTEFURQAATENfTU9ORVRBUlkATENfTUVTU0FHRVM="),C(e,121776,"Qy5VVEYtOA=="),C(e,121800,"MAUCAE5vIGVycm9yIGluZm9ybWF0aW9uAElsbGVnYWwgYnl0ZSBzZXF1ZW5jZQBEb21haW4gZXJyb3IAUmVzdWx0IG5vdCByZXByZXNlbnRhYmxlAE5vdCBhIHR0eQBQZXJtaXNzaW9uIGRlbmllZABPcGVyYXRpb24gbm90IHBlcm1pdHRlZABObyBzdWNoIGZpbGUgb3IgZGlyZWN0b3J5AE5vIHN1Y2ggcHJvY2VzcwBGaWxlIGV4aXN0cwBWYWx1ZSB0b28gbGFyZ2UgZm9yIGRhdGEgdHlwZQBObyBzcGFjZSBsZWZ0IG9uIGRldmljZQBPdXQgb2YgbWVtb3J5AFJlc291cmNlIGJ1c3kASW50ZXJydXB0ZWQgc3lzdGVtIGNhbGwAUmVzb3VyY2UgdGVtcG9yYXJpbHkgdW5hdmFpbGFibGUASW52YWxpZCBzZWVrAENyb3NzLWRldmljZSBsaW5rAFJlYWQtb25seSBmaWxlIHN5c3RlbQBEaXJlY3Rvcnkgbm90IGVtcHR5AENvbm5lY3Rpb24gcmVzZXQgYnkgcGVlcgBPcGVyYXRpb24gdGltZWQgb3V0AENvbm5lY3Rpb24gcmVmdXNlZABIb3N0IGlzIGRvd24ASG9zdCBpcyB1bnJlYWNoYWJsZQBBZGRyZXNzIGluIHVzZQBCcm9rZW4gcGlwZQBJL08gZXJyb3IATm8gc3VjaCBkZXZpY2Ugb3IgYWRkcmVzcwBCbG9jayBkZXZpY2UgcmVxdWlyZWQATm8gc3VjaCBkZXZpY2UATm90IGEgZGlyZWN0b3J5AElzIGEgZGlyZWN0b3J5AFRleHQgZmlsZSBidXN5AEV4ZWMgZm9ybWF0IGVycm9yAEludmFsaWQgYXJndW1lbnQAQXJndW1lbnQgbGlzdCB0b28gbG9uZwBTeW1ib2xpYyBsaW5rIGxvb3AARmlsZW5hbWUgdG9vIGxvbmcAVG9vIG1hbnkgb3BlbiBmaWxlcyBpbiBzeXN0ZW0ATm8gZmlsZSBkZXNjcmlwdG9ycyBhdmFpbGFibGUAQmFkIGZpbGUgZGVzY3JpcHRvcgBObyBjaGlsZCBwcm9jZXNzAEJhZCBhZGRyZXNzAEZpbGUgdG9vIGxhcmdlAFRvbyBtYW55IGxpbmtzAE5vIGxvY2tzIGF2YWlsYWJsZQBSZXNvdXJjZSBkZWFkbG9jayB3b3VsZCBvY2N1cgBTdGF0ZSBub3QgcmVjb3ZlcmFibGUAUHJldmlvdXMgb3duZXIgZGllZABPcGVyYXRpb24gY2FuY2VsZWQARnVuY3Rpb24gbm90IGltcGxlbWVudGVkAE5vIG1lc3NhZ2Ugb2YgZGVzaXJlZCB0eXBlAElkZW50aWZpZXIgcmVtb3ZlZABEZXZpY2Ugbm90IGEgc3RyZWFtAE5vIGRhdGEgYXZhaWxhYmxlAERldmljZSB0aW1lb3V0AE91dCBvZiBzdHJlYW1zIHJlc291cmNlcwBMaW5rIGhhcyBiZWVuIHNldmVyZWQAUHJvdG9jb2wgZXJyb3IAQmFkIG1lc3NhZ2UARmlsZSBkZXNjcmlwdG9yIGluIGJhZCBzdGF0ZQBOb3QgYSBzb2NrZXQARGVzdGluYXRpb24gYWRkcmVzcyByZXF1aXJlZABNZXNzYWdlIHRvbyBsYXJnZQBQcm90b2NvbCB3cm9uZyB0eXBlIGZvciBzb2NrZXQAUHJvdG9jb2wgbm90IGF2YWlsYWJsZQBQcm90b2NvbCBub3Qgc3VwcG9ydGVkAFNvY2tldCB0eXBlIG5vdCBzdXBwb3J0ZWQATm90IHN1cHBvcnRlZABQcm90b2NvbCBmYW1pbHkgbm90IHN1cHBvcnRlZABBZGRyZXNzIGZhbWlseSBub3Qgc3VwcG9ydGVkIGJ5IHByb3RvY29sAEFkZHJlc3Mgbm90IGF2YWlsYWJsZQBOZXR3b3JrIGlzIGRvd24ATmV0d29yayB1bnJlYWNoYWJsZQBDb25uZWN0aW9uIHJlc2V0IGJ5IG5ldHdvcmsAQ29ubmVjdGlvbiBhYm9ydGVkAE5vIGJ1ZmZlciBzcGFjZSBhdmFpbGFibGUAU29ja2V0IGlzIGNvbm5lY3RlZABTb2NrZXQgbm90IGNvbm5lY3RlZABDYW5ub3Qgc2VuZCBhZnRlciBzb2NrZXQgc2h1dGRvd24AT3BlcmF0aW9uIGFscmVhZHkgaW4gcHJvZ3Jlc3MAT3BlcmF0aW9uIGluIHByb2dyZXNzAFN0YWxlIGZpbGUgaGFuZGxlAFJlbW90ZSBJL08gZXJyb3IAUXVvdGEgZXhjZWVkZWQATm8gbWVkaXVtIGZvdW5kAFdyb25nIG1lZGl1bSB0eXBlAE11bHRpaG9wIGF0dGVtcHRlZABSZXF1aXJlZCBrZXkgbm90IGF2YWlsYWJsZQBLZXkgaGFzIGV4cGlyZWQAS2V5IGhhcyBiZWVuIHJldm9rZWQAS2V5IHdhcyByZWplY3RlZCBieSBzZXJ2aWNl"),C(e,123730,"pQJbAPABtQWMBSUBgwYdA5QE/wDHAzEDCwa8AY8BfwPKBCsA2gavAEIDTgPcAQ4EFQChBg0BlAILAjgGZAK8Av8CXQPnBAsHzwLLBe8F2wXhAh4GRQKFAIICbANvBPEA8wMYBdkA2gNMBlQCewGdA70EAABRABUCuwCzA20A/wGFBC8F+QQ4AGUBRgGfALcGqAFzAlMB"),C(e,123928,"IQQAAAAAAAAAAC8C"),C(e,123960,"NQRHBFYE"),C(e,123982,"oAQ="),C(e,124002,"RgVgBW4FYQYAAM8BAAAAAAAAAADJBukG+QYeBzkHSQdeBw=="),C(e,124048,"GQAKABkZGQAAAAAFAAAAAAAACQAAAAALAAAAAAAAAAAZABEKGRkZAwoHAAEACQsYAAAJBgsAAAsABhkAAAAZGRk="),C(e,124129,"DgAAAAAAAAAAGQAKDRkZGQANAAACAAkOAAAACQAOAAAO"),C(e,124187,"DA=="),C(e,124199,"EwAAAAATAAAAAAkMAAAAAAAMAAAM"),C(e,124245,"EA=="),C(e,124257,"DwAAAAQPAAAAAAkQAAAAAAAQAAAQ"),C(e,124303,"Eg=="),C(e,124315,"EQAAAAARAAAAAAkSAAAAAAASAAASAAAaAAAAGhoa"),C(e,124370,"GgAAABoaGgAAAAAAAAk="),C(e,124419,"FA=="),C(e,124431,"FwAAAAAXAAAAAAkUAAAAAAAUAAAU"),C(e,124477,"Fg=="),C(e,124489,"FQAAAAAVAAAAAAkWAAAAAAAWAAAWAAAwMTIzNDU2Nzg5QUJDREVG"),C(e,124564,"EQ=="),C(e,124604,"//////////8="),C(e,124672,"0XSeAFedvSqAcFIP//8+JwoAAABkAAAA6AMAABAnAACghgEAQEIPAICWmAAA4fUFGAAAADUAAABxAAAAa////877//+Sv///AAAAAAAAAAACAADAAwAAwAQAAMAFAADABgAAwAcAAMAIAADACQAAwAoAAMALAADADAAAwA0AAMAOAADADwAAwBAAAMARAADAEgAAwBMAAMAUAADAFQAAwBYAAMAXAADAGAAAwBkAAMAaAADAGwAAwBwAAMAdAADAHgAAwB8AAMAAAACzAQAAwwIAAMMDAADDBAAAwwUAAMMGAADDBwAAwwgAAMMJAADDCgAAwwsAAMMMAADDDQAA0w4AAMMPAADDAAAMuwEADMMCAAzDAwAMwwQADNsAAAAAIAAAAAkAAAAKAAAADQAAAAsAAAAMAAAAhQAAAAAgAAABIAAAAiAAAAMgAAAEIAAABSAAAAYgAAAIIAAACSAAAAogAAAoIAAAKSAAAF8gAAAAMAAAAAAAAE4xMF9fY3h4YWJpdjExNl9fc2hpbV90eXBlX2luZm9FAAAAAGjpAQB46AEAZOoBAE4xMF9fY3h4YWJpdjExN19fY2xhc3NfdHlwZV9pbmZvRQAAAGjpAQCo6AEAnOgBAE4xMF9fY3h4YWJpdjExN19fcGJhc2VfdHlwZV9pbmZvRQAAAGjpAQDY6AEAnOgBAE4xMF9fY3h4YWJpdjExOV9fcG9pbnRlcl90eXBlX2luZm9FAGjpAQAI6QEA/OgBAAAAAADM6AEAFAAAABUAAAAWAAAAFwAAABgAAAAZAAAAGgAAABsAAAAAAAAAsOkBABQAAAAcAAAAFgAAABcAAAAYAAAAHQAAAB4AAAAfAAAATjEwX19jeHhhYml2MTIwX19zaV9jbGFzc190eXBlX2luZm9FAAAAAGjpAQCI6QEAzOgBAAAAAAAg6gEACQAAACAAAAAhAAAAAAAAAEjqAQAJAAAAIgAAACMAAAAAAAAACOoBAAkAAAAkAAAAJQAAAFN0OWV4Y2VwdGlvbgAAAABA6QEA+OkBAFN0OWJhZF9hbGxvYwAAAABo6QEAEOoBAAjqAQBTdDIwYmFkX2FycmF5X25ld19sZW5ndGgAAAAAaOkBACzqAQAg6gEAU3Q5dHlwZV9pbmZvAAAAAEDpAQBU6gE="),C(e,125552,"wLEAAMCyAADAswAAwLQAAMC1AADAtgAAwLcAAMC4AADAuQAAwLoAAMC7AADAvAAAwL0AAMC+AADAvwAAwMAAAMDBAADAwgAAwMMAAMDEAADAxQAAwMIAAMDGAADAxwAAwMgAAMDJAADAygAAwMsAAMDMAADAzQAAwM4AAMDPAADA0AAAwNEAAMDSAADA0wAAwNQAAMDVAADA1gAAwNcAAMDYAADA2QAAwNIAAMDaAADA2wAAwNwAAMDdAADA3gAAwN8AAMDgAADA4QAAwNgAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADA4gAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwOMAAMDkAADAwgAAwMIAAMDCAADA5QAAwMIAAMDmAADA5wAAwOgAAMDpAADA6gAAwOsAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADA7AAAwO0AAMDCAADA7gAAwO8AAMDCAADA8AAAwPEAAMDyAADA8wAAwPQAAMD1AADA9gAAwPcAAMD4AADAwgAAwPkAAMD6AADA+wAAwPwAAMD9AADA/gAAwP8AAMAAAQDAAQEAwAIBAMADAQDABAEAwAUBAMAGAQDABwEAwAgBAMAJAQDACgEAwAsBAMAMAQDACwEAwA0BAMAOAQDADwEAwAsBAMDCAADAwgAAwMIAAMAQAQDAEQEAwBIBAMALAQDACwEAwAsBAMALAQDACwEAwAsBAMALAQDACwEAwAsBAMALAQDAwgAAwMIAAMDCAADAwgAAwBMBAMALAQDACwEAwAsBAMALAQDACwEAwAsBAMALAQDACwEAwAsBAMALAQDACwEAwAsBAMALAQDACwEAwAsBAMDCAADAwgAAwBQBAMALAQDACwEAwAsBAMALAQDACwEAwAsBAMALAQDACwEAwAsBAMALAQDACwEAwAsBAMALAQDACwEAwAsBAMALAQDACwEAwAsBAMALAQDACwEAwAsBAMALAQDACwEAwAsBAMALAQDACwEAwAsBAMALAQDACwEAwAsBAMALAQDACwEAwAsBAMDCAADAwgAAwBUBAMAWAQDACwEAwAsBAMAXAQDAGAEAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAGQEAwMIAAMDCAADAGgEAwAsBAMALAQDACwEAwAsBAMALAQDACwEAwAsBAMALAQDACwEAwAsBAMALAQDACwEAwAsBAMALAQDACwEAwAsBAMALAQDACwEAwAsBAMALAQDACwEAwAsBAMALAQDACwEAwAsBAMALAQDACwEAwAsBAMALAQDACwEAwAsBAMALAQDACwEAwAsBAMALAQDACwEAwAsBAMDCAADAGwEAwBwBAMALAQDACwEAwAsBAMALAQDACwEAwAsBAMALAQDACwEAwAsBAMAdAQDACwEAwAsBAMALAQDACwEAwAsBAMALAQDACwEAwAsBAMALAQDACwEAwAsBAMALAQDACwEAwAsBAMALAQDACwEAwAsBAMALAQDACwEAwB4BAMAfAQDAIAEAwCEBAMAiAQDAIwEAwCQBAMAlAQDA2AAAwNgAAMAmAQDACwEAwAsBAMALAQDACwEAwAsBAMAnAQDACwEAwAsBAMALAQDACwEAwAsBAMALAQDACwEAwCgBAMApAQDACwEAwAsBAMAqAQDACwEAwCsBAMALAQDALAEAwC0BAMAuAQDALwEAwNgAAMDYAADAMAEAwDEBAMAyAQDAMwEAwDQBAMALAQDACwEAwAsBAMALAQDACwEAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMA1AQDAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwDYBAMA3AQDAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAOAEAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMDCAADAwgAAwMIAAMA5AQDACwEAwAsBAMALAQDACwEAwAsBAMALAQDACwEAwAsBAMALAQDACwEAwAsBAMALAQDAwgAAwMIAAMA6AQDAOwEAwDwB"),C(e,128496,"oVcBAEEAAADdVwEAQgAAACZYAQBDAAAAa1gBAEQAAADSWAEARQAAABNZAQBGAAAAfFkBAEcAAACEWQEASAAAADBaAQBJAAAAZ1oBAEoAAADuWgEASwAAAC5bAQBMAAAAcVsBAE0AAADZWwEATgAAAGtcAQBPAAAAh1wBAAgAAADcXAEACQAAAB9dAQAKAAAAZ10BAAsAAACVXQEADAAAAMpdAQANAAAAD14BAA4AAAAqXgEADwAAAIZeAQAPAAAAvV4BABAAAABDXwEAEQAAAH1fAQASAAAAq18BABMAAADZXwEAFAAAAAVgAQAVAAAAMGABABcAAABgYAEAGAAAAHlgAQAZAAAAtWABABsAAADdYAEAHAAAAPBgAQAdAAAAI2EBACAAAABEYQEAIQAAAG9hAQAiAAAAnWEBACMAAADRYQEAJAAAAPNhAQAlAAAAFWIBACYAAABZYgEAKAAAAH1iAQApAAAArmIBACoAAADoYgEAKwAAADVjAQAtAAAAb2MBAC4AAACnYwEALwAAAOZjAQAwAAAAeGQBADEAAACdZAEAMgAAAM9kAQAzAAAA/mQBAGQAAABgZQEAyAAAAN1lAQDJAAAAAAAAAP////8iZgEAAQAAAD5mAQACAAAABVUBAAMAAADNZgEAEQAAADRnAQASAAAAg2cBABMAAAD7ZwEAFAAAADtoAQAVAAAATGgBABYAAAB0aAEAEQAAALdoAQAhAAAAymgBACIAAAD+aAEAIwAAAEtpAQAkAAAAaWkBACUAAACfaQEAJgAAAOJpAQAhAAAAAAAAAP////8AAAAAAAAAACYAAAAAAAAAAQAAAJByAQABAAAAkHMBAAEAAACQdAEAAQAAAJB1AQABAAAAkHYBAAEAAACQdwEAAQAAAJB4AQABAAAAkHkBAAEAAACQegEAAQAAAJB7AQABAAAAkHwBAAEAAACQfQEAAQAAAJB+AQABAAAAkH8BAAEAAACQgAEAAQAAAJCBAQABAAAAkIIBAAUAAAAAAAAABg=="),C(e,129280,"oIMBAKCDAQAghAEAIIQBAKCEAQAghQEAoIUBACCGAQCghgEAoIYBACCHAQAghwEAoIcBAKCHAQAgiAEAIIgBAKCIAQCgiAEAIIkBACCJAQAuOU4ycIoBAAMHBQCQigEALjlOLnCKAQADBwUAkIoBAC45Ti5wigEAAwcFAJCKAQAuOVoycIoBAAMJBQCVigEALjlOMnCKAQADBwUAkIoBAC45SjdwigEABAcFAJqKAQAuOUo3cIoBAAQHBQCaigEALjlKN3CKAQAEBwUAmooBAC45TjJwigEAAwcFAJCKAQAuOU4ucIoBAAMHBQCQigEALjlOMnCKAQADBwUAkIoBACIpKSBwigEAAwcFAJqKAQAuOTcycIoBAAMHBQCaigEAo1MBABABAAAAVAEADAEAANJUAQARAQAAClUBACMAAAB/VQEAGwAAADRWAQAVAAAAxVYBAAABAAABVwEAAgEAAE1XAQADAQAAvVcBAAQBAAD7VwEABQEAAE5YAQATAAAAkVgBAB4AAADVWAEAFwAAAF9ZAQAaAAAAkVkBABkAAAD0WQEAGAAAADhaAQAdAAAAzloBABwAAAAPWwEAFgAAADdbAQAU"),C(e,129744,"Q1gBABABAACIWAEADAEAAPNYAQARAQAAM1kBACMAAACHWQEAGwAAAOBZAQAVAAAAQloBAAABAACcWgEAAgEAAABbAQADAQAAT1sBAAQBAACnWwEABQEAAEhcAQATAAAAc1wBAB4AAADEXAEAFwAAABVdAQAaAAAAXV0BABkAAACIXQEAGAAAANJdAQAdAAAABF4BABwAAAAvXgEAFgAAAF9eAQAU"),C(e,129920,"u08BAAEAAABcUAEAAAAAAM9QAQAAAAAAOFIBAAAAAABBUwEAAAAAAIFTAQAAAAAA+1MBAAAAAADnVAEAAAAAACVVAQAAAAAAiVUBAAAAAABUVgEAAAAAAPxWAQAAAAAADlcBAAAAAABIVwEAAAAAAKdXAQAAAAAA9lcBAAAAAAA3WAEAAAAAAKNYAQAAAAAA6FgBAAAAAAAuWQEAAAAAAH9ZAQAAAAAA61kBAAAAAAD/////g00BAIIAAAB5AAAAdg=="),C(e,130128,"4I8BAOCPAQBQkAEAwJABAMCQAQDAkAEAAQAAANQKAwCMCgMAROcC"),C(e,130176,"I0sBAAEAAAAAAAAA/////1dMAQABAAAA+0wBAAIAAADGTQEAAwAAAAAAAAD/////Vk4BAAAAAADSTgEAAQAAALJPAQACAAAAGFABABQAAAAAAAAA/////wAAAAAAAAAAt1ABABIAAABNUgEAFAAAAN5SAQAkAAAAhlMBAEAAAAARVAEAwQAAAAAAAAD/////q1QBAAEAAAAAAAAA/////1dMAQAAAAAAGVUBAAEAAACRVQEAAgAAAENWAQADAAAA31YBAAQAAAAgVwEABQAAAAAAAAD/////AAAAAAAAAABXTAEAAQAAAIlXAQACAAAAr1cBAAMAAADfVgEABAAAACBXAQAFAAAAAAAAAP////8AAAAAClgBADxYAQAYUAEAqFgB"),C(e,130480,"8l8BAAEAAAA5YAEAAgAAAFhgAQADAAAAgWABAAQAAACwYAEABQAAANNgAQAGAAAABWEBAAcAAAAqYQEACAAAAFFhAQAJAAAAdmEBAAoAAACQYQEACwAAAMNhAQAMAAAA+WEBAA0AAAAuYgEADgAAAE5iAQAPAAAAhmIBAA8AAADUYgEADwAAABljAQAPAAAAP2MBAA8AAACkYwEABwAAANpjAQAHAAAAO2QBAAcAAACEZAEABwAAAK9kAQAHAAAAw2QBAA4AAADnZAEADgAAAAplAQAQAAAA9GUBABAAAAArZgEAEAAAAN9WAQAQAAAAbmYBABAAAADBZgEAEA=="),C(e,130752,"f20BAD4AAACjbQEAPOAAAM1tAQAmAAAA1G0BACIAAAA6bgEAIAAAAHhuAQAnAAAAAAAAAP////8AAAAAAAAAAGNnAQBkAAAAxWcBAAAAAAAJaAEAHgAAAEJoAQBBAAAAQ1YBAGQAAACGaAEAlgAAAKVoAQDmAAAAAAAAAP////9jZwEAZAAAAL5oAQA8AAAA6WgBAFAAAABDVgEAZAAAABJpAQB9AAAAUmkBAKAAAAAAAAAA/////wAAAAAAAAAAY2cBAGQAAACUaQEARgAAAN5pAQBVAAAAQ1YBAGQAAAAoagEAbgAAAGdqAQB4AAAAAAAAAP////8AAAAAAAAAAGNnAQBkAAAAlGkBABQAAADeaQEAMgAAAENWAQBkAAAAKGoBAIwAAABnagEAtAAAAAAAAAD/////"),C(e,131076,"QP8BAAD/AQCA/wEAwP8B"),C(e,131104,"wmoBACDgAAALawEACeAAAEFrAQBf4AAAe2sBACI="),C(e,131152,"q2sBAAEAAAC8awEAAgAAAA1sAQAD"),C(e,131184,"+EwBAMJNAQBTTgEACU8BAOVPAQBkUAEA2lABAGdSAQDyUgEA1FMBAGFUAQ=="),C(e,131236,"iFIBADlTAQAAAAAAnlMBAGRUAQDwVAEAL1UBAJpVAQBQVgE="),C(e,131280,"Y1IBAIADAACAA/8DbGUAAAcAAAA0UwEAIAQAAAAELwUAAAAAAAAAAHNTAQAwBQAAMAWPBXloAAAEAAAAG1QBAJAFAACQBf8FAAAAAAAAAAC3VAEAAAYAAAAG/wYAAAAAAAAAACpVAQAABwAAAAdPBwAAAAAAAAAAllUBAAAJAAAACX8JaWgAAAQAAAA/VgEAgAkAAIAJ/wluYgAABAAAANZWAQAACgAAAAp/CmFwAAAEAAAAClcBAIAKAACACv8KdWcAAAQAAABfVwEAAAsAAAALfwsAAAAAAAAAAM5XAQCACwAAgAv/C2F0AAAEAAAAD1gBAAAMAAAADH8MZXQAAAAAAAAsWAEAgAwAAIAM/wxuawAABAAAAJ9YAQAADQAAAA1/DWxtAAAEAAAA5FgBAIANAACADf8NaXMAAAQAAAA8WQEAAA4AAAAOfw4AAAAAAAAAAJtZAQCADgAAgA7/DgAAAAAAAAAA8FkBAAAPAAAAD/8PAAAAAAAAAAA0WgEAABAAAAAQnxAAAAAAAAAAANlaAQCgEAAAoBD/EGFrAAAEAAAAGFsBAAARAAAAEf8Rb2sAAAQAAABKWwEAABIAAAASnxMAAAAAAAAAALlbAQAAKAAAACj/KAAAAAAQAAAAWlwBAEAwAABAMP8wAAAAAAgAAACDXAEAADEAAAAx/58AAAAACAAAABhbAQAApwAAAKf/129rAAAM"),C(e,131840,"WAIAAKoAAACwBAAAhwAAANAHAABuAAAAuAsAAG4AAAD/////"),C(e,131888,"qAsD"),C(e,131904,"mF4BAAEAAAA4XwEAAgAAAGFfAQADAAAAnV8BAAYAAADCXwEACQAAAPhfAQAKAAAAJ2ABAAQAAABNYAEABQAAAI1gAQAkAAAAq2ABAAsAAADVYAEADAAAAPZgAQANAAAAFmEBAA4AAABMYQEADwAAAGdhAQAQAAAAlmEBABEAAAC7YQEAEgAAAAJiAQAfAAAAG2IBACUAAABIYgEAIAAAAJJiAQAhAAAAzmIBACIAAADwYgEABwAAAC5jAQAI"),C(e,132112,"jmMBAAEAAAC8YwEAAgAAAAAAAAABAAAAFJwBACCcAQAsnAEAPAAAABo="),C(e,132163,"AgMFCAsOEhYbICUrMTc+RUxTWmJpcXmAiJCYn6autbzCyc/V2uDk6e3w9Pb5+/z9/f39/Pv59vTw7enk4NrVz8nCvLWupp+YkIiAeXFpYlpTTEU+NzErJSAbFhIOCwgFAwI="),C(e,132288,"QAAAAAABAAAAAAAA7AQCACcAAAAoAAAAKQAAACoAAAArAAAAQOkBAMO2AQBo6QEAsLYBAOQEAgAAAAAAJAUCACwAAAAtAAAALgAAAC8AAABA6QEAArcBAGjpAQDstgEAEAUCAGjpAQDStgEAGAUCAAU="),C(e,132412,"DQ=="),C(e,132436,"CwAAAAoAAADoeAM="),C(e,132460,"Ag=="),C(e,132476,"//////////8="),C(e,132544,"MAUCAAAAAAAF"),C(e,132564,"MA=="),C(e,132588,"CwAAADEAAAD4eAMAAAQ="),C(e,132612,"AQ=="),C(e,132628,"/////wo="),C(e,132696,"yAUCAAB/BA==");var YC,HC=(YC=[null,function(A){var e,g=0;return g=f[(A|=0)>>2],f[A>>2]=g+1,g=255&(e=a[0|g]),(0|e)<0&&(g=B[(f[A+12>>2]+(g<<1)|0)-256>>1]),0|g},vC,function(A){var e,g;return e=f[(A|=0)>>2],g=f[e>>2],f[A>>2]=e+4,0|g},function(A){var e=0,g=0;return e=f[(A|=0)>>2],65533==(0|(g=ne(A)))&&(f[A>>2]=e+1,f[A+8>>2]=1,g=255&(e=a[0|e]),(0|e)>=0||(g=B[(f[A+12>>2]+(g<<1)|0)-256>>1])),0|g},ne,function(A){var e,g,r=0;return(r=f[4+(A|=0)>>2])>>>0<=(g=(e=f[A>>2])+1|0)>>>0?(f[A>>2]=r,65533):(f[A>>2]=g,r=i[0|e],f[A>>2]=e+2,r|i[e+1|0]<<8)},function(A,e){var g,r,C;return e|=0,A=f[(A|=0)>>2],g=f[A+4>>2],r=f[e>>2],(e=Qr(g+1|0,(C=f[r+4>>2])+1|0))||(e=a[0|g]-a[0|C]|0)||(e=Qr(f[A>>2],f[r>>2])),0|e},function(A,e){var g;return A|=0,e=f[(e|=0)>>2],g=f[A>>2],(A=f[e+16>>2]-f[g+16>>2]|0)||(A=Qr(f[g>>2],f[e>>2])),0|A},MC,function(A,e,g,r){var C;return e|=0,g|=0,r|=0,V=C=V-16|0,(A=0|P(f[60+(A|=0)>>2],0|e,0|g,255&r,C+8|0))?(f[56798]=A,A=-1):A=0,V=C+16|0,U=A?-1:f[C+12>>2],0|(A?-1:f[C+8>>2])},function(A,e,g){e|=0,g|=0;var r,C=0,a=0,I=0,i=0,b=0,s=0;V=r=V-32|0,C=f[28+(A|=0)>>2],f[r+16>>2]=C,I=f[A+20>>2],f[r+28>>2]=g,f[r+24>>2]=e,e=I-C|0,f[r+20>>2]=e,I=e+g|0,b=2;A:{e:{e=r+16|0,(C=0|m(f[A+60>>2],0|e,2,r+12|0))?(f[56798]=C,C=-1):C=0;g:{if(C)C=e;else for(;;){if((0|(a=f[r+12>>2]))==(0|I))break g;if((0|a)<0){C=e;break e}if(i=a-((s=(i=f[e+4>>2])>>>0>>0)?i:0)|0,f[(C=(s<<3)+e|0)>>2]=i+f[C>>2],f[(e=(s?12:4)+e|0)>>2]=f[e>>2]-i,I=I-a|0,e=C,b=b-s|0,(a=0|m(f[A+60>>2],0|e,0|b,r+12|0))?(f[56798]=a,a=-1):a=0,a)break}if(-1!=(0|I))break e}e=f[A+44>>2],f[A+28>>2]=e,f[A+20>>2]=e,f[A+16>>2]=e+f[A+48>>2],A=g;break A}f[A+28>>2]=0,f[A+16>>2]=0,f[A+20>>2]=0,f[A>>2]=32|f[A>>2],A=0,2!=(0|b)&&(A=g-f[C+4>>2]|0)}return V=r+32|0,0|A},function(A,e,g){A|=0,e|=0,g|=0;var r,C=0,I=0,b=0;V=r=V-32|0,f[r+16>>2]=e,C=f[A+48>>2],f[r+20>>2]=g-!!(0|C),I=f[A+44>>2],f[r+28>>2]=C,f[r+24>>2]=I;A:{e:{if((C=0|L(f[A+60>>2],r+16|0,2,r+12|0))?(f[56798]=C,C=-1):C=0,C)e=32;else{if((0|(C=f[r+12>>2]))>0)break e;e=C?32:16}f[A>>2]=e|f[A>>2];break A}b=C,(I=f[r+20>>2])>>>0>=C>>>0||(C=f[A+44>>2],f[A+4>>2]=C,f[A+8>>2]=C+(b-I|0),f[A+48>>2]&&(f[A+4>>2]=C+1,a[(e+g|0)-1|0]=i[0|C]),b=g)}return V=r+32|0,0|b},function(A){return 0|d(f[60+(A|=0)>>2])},bC,function(A,e,g,r,C,I){A|=0,e=+e,g|=0,r|=0,C|=0,I|=0;var s,t=0,k=0,o=0,B=0,c=0,Q=0,w=0,D=0,u=0,l=0,x=0,d=0,m=0,M=0,v=0,h=0,p=0,Y=0;V=s=V-560|0,f[s+44>>2]=0,n(+e),t=0|b(1),b(0),(0|t)<0?(d=1,p=84997,n(+(e=-e)),t=0|b(1),b(0)):2048&C?(d=1,p=85e3):(p=(d=1&C)?85003:84998,Y=!d);A:if(2146435072&~t){M=s+16|0;e:{g:{r:{if(e=Vg(e,s+44|0),0!=(e+=e)){if(t=f[s+44>>2],f[s+44>>2]=t-1,97!=(0|(v=32|I)))break r;break e}if(97==(0|(v=32|I)))break e;c=f[s+44>>2],Q=(0|r)<0?6:r;break g}c=t-29|0,f[s+44>>2]=c,e*=268435456,Q=(0|r)<0?6:r}for(k=l=(s+48|0)+((0|c)>=0?288:0)|0;r=e<4294967296&e>=0?~~e>>>0:0,f[k>>2]=r,k=k+4|0,0!=(e=1e9*(e-+(r>>>0))););if((0|c)<=0)r=c,t=k,o=l;else for(o=l,r=c;;){if(D=(0|r)>=29?29:r,!(o>>>0>(t=k-4|0)>>>0)){for(r=0;B=f[t>>2],h=r,r=31&D,(63&D)>>>0>=32?(w=B<>>32-r,r=B<>>0>h>>>0?B+1|0:B,1e9),f[t>>2]=h-Cr(r,U,1e9,0),o>>>0<=(t=t-4|0)>>>0;);r&&(f[(o=o-4|0)>>2]=r)}for(;o>>>0<(t=k)>>>0&&!f[(k=t-4|0)>>2];);if(r=f[s+44>>2]-D|0,f[s+44>>2]=r,k=t,!((0|r)>0))break}if((0|r)<0)for(m=1+((Q+25>>>0)/9|0)|0,u=102==(0|v);;){if(w=(0|(r=0-r|0))>=9?9:r,t>>>0<=o>>>0)k=f[o>>2];else{for(D=1e9>>>w|0,B=~(-1<>2],f[k>>2]=h+(r>>>w|0),r=G(D,r&B),(k=k+4|0)>>>0>>0;);k=f[o>>2],r&&(f[t>>2]=r,t=t+4|0)}if(r=w+f[s+44>>2]|0,f[s+44>>2]=r,o=(!k<<2)+o|0,t=t-(k=u?l:o)>>2>(0|m)?k+(m<<2)|0:t,!((0|r)<0))break}if(r=0,!(t>>>0<=o>>>0||(r=G(l-o>>2,9),k=10,(B=f[o>>2])>>>0<10)))for(;r=r+1|0,B>>>0>=(k=G(k,10))>>>0;);if((0|(k=(Q-(102!=(0|v)?r:0)|0)-(103==(0|v)&!!(0|Q))|0))<(G(t-l>>2,9)-9|0)){if(c=((((0|c)<0?4:292)+s|0)+((B=(0|(D=k+9216|0))/9|0)<<2)|0)-4048|0,k=10,(0|(w=D-G(B,9)|0))<=7)for(;k=G(k,10),8!=(0|(w=w+1|0)););if(!(!(u=(D=f[c>>2])-G(k,m=(D>>>0)/(k>>>0)|0)|0)&(0|(B=c+4|0))==(0|t))&&(!(1&m)&&(e=9007199254740992,!(1&a[c-4|0])|1e9!=(0|k)|o>>>0>=c>>>0)||(e=9007199254740994),x=(0|t)==(0|B)?1:1.5,x=(B=k>>>1|0)>>>0>u>>>0?.5:(0|B)==(0|u)?x:1.5,45!=i[0|p]|Y||(x=-x,e=-e),B=D-u|0,f[c>>2]=B,e+x!=e)){if(r=k+B|0,f[c>>2]=r,r>>>0>=1e9)for(;f[c>>2]=0,(c=c-4|0)>>>0>>0&&(f[(o=o-4|0)>>2]=0),r=f[c>>2]+1|0,f[c>>2]=r,r>>>0>999999999;);if(r=G(l-o>>2,9),k=10,!((B=f[o>>2])>>>0<10))for(;r=r+1|0,B>>>0>=(k=G(k,10))>>>0;);}t=t>>>0>(k=c+4|0)>>>0?k:t}for(;B=t,!(D=t>>>0<=o>>>0)&&!f[(t=B-4|0)>>2];);if(103==(0|v)){if(Q=((t=(0|(k=Q||1))>(0|r)&(0|r)>-5)?~r:-1)+k|0,I=(t?-1:-2)+I|0,!(c=8&C)){if(t=-9,!D&&(c=f[B-4>>2])&&(w=10,t=0,!((c>>>0)%10|0))){for(;k=t,t=t+1|0,!((c>>>0)%((w=G(w,10))>>>0)|0););t=~k}k=G(B-l>>2,9),70!=(-33&I)?(c=0,Q=(0|(t=(0|(t=((r+k|0)+t|0)-9|0))>0?t:0))>(0|Q)?Q:t):(c=0,Q=(0|(t=(0|(t=(t+k|0)-9|0))>0?t:0))>(0|Q)?Q:t)}}else c=8&C;if(w=-1,(0|((D=c|Q)?2147483645:2147483646))<(0|Q))break A;if(u=1+(!!(0|D)+Q|0)|0,70!=(0|(k=-33&I))){if((M-(t=Ug(((t=r>>31)^r)-t|0,0,M))|0)<=1)for(;a[0|(t=t-1|0)]=48,(M-t|0)<2;);if(a[0|(m=t-2|0)]=I,a[t-1|0]=(0|r)<0?45:43,(0|(t=M-m|0))>(2147483647^u))break A}else{if((2147483647^u)<(0|r))break A;t=(0|r)>0?r:0}if((0|(r=t+u|0))>(2147483647^d))break A;br(A,32,g,u=r+d|0,C),kC(A,p,d),br(A,48,g,u,65536^C);g:{r:{C:{if(70==(0|k)){for(r=8|(I=s+16|0),c=9|I,o=k=o>>>0>l>>>0?l:o;;){t=Ug(f[o>>2],0,c);a:if((0|k)==(0|o))(0|t)==(0|c)&&(a[s+24|0]=48,t=r);else{if(s+16>>>0>=t>>>0)break a;for(;a[0|(t=t-1|0)]=48,s+16>>>0>>0;);}if(kC(A,t,c-t|0),!(l>>>0>=(o=o+4|0)>>>0))break}if(D&&kC(A,85998,1),(0|Q)<=0|o>>>0>=B>>>0)break C;for(;;){if((t=Ug(f[o>>2],0,c))>>>0>s+16>>>0)for(;a[0|(t=t-1|0)]=48,s+16>>>0>>0;);if(kC(A,t,(0|Q)>=9?9:Q),t=Q-9|0,B>>>0<=(o=o+4|0)>>>0)break r;if(r=(0|Q)>9,Q=t,!r)break}break r}a:if(!((0|Q)<0))for(l=o>>>0>>0?B:o+4|0,r=8|(I=s+16|0),B=9|I,k=o;;){(0|B)==(0|(t=Ug(f[k>>2],0,B)))&&(a[s+24|0]=48,t=r);I:if((0|k)==(0|o))kC(A,t,1),t=t+1|0,c|Q&&kC(A,85998,1);else{if(s+16>>>0>=t>>>0)break I;for(;a[0|(t=t-1|0)]=48,s+16>>>0>>0;);}if(kC(A,t,(0|(I=B-t|0))>(0|Q)?Q:I),Q=Q-I|0,l>>>0<=(k=k+4|0)>>>0)break a;if(!((0|Q)>=0))break}br(A,48,Q+18|0,18,0),kC(A,m,M-m|0);break g}t=Q}br(A,48,t+9|0,9,0)}br(A,32,g,u,8192^C),w=(0|g)<(0|u)?u:g;break A}if(c=(I<<26>>31&9)+p|0,!(r>>>0>11)){for(t=12-r|0,x=16;x*=16,t=t-1|0;);e=45!=i[0|c]?e+x-x:-(x+(-e-x))}for((0|M)==(0|(t=Ug(((t=f[s+44>>2])^(k=t>>31))-k|0,0,M)))&&(a[s+15|0]=48,t=s+15|0),l=2|d,o=32&I,k=f[s+44>>2],a[0|(Q=t-2|0)]=I+15,a[t-1|0]=(0|k)<0?45:43,t=8&C,k=s+16|0;I=k,B=E(e)<2147483648?~~e:-2147483648,a[0|k]=o|i[B+124512|0],!((0|r)>0|t)&0==(e=16*(e-+(0|B)))|1!=((k=I+1|0)-(s+16|0)|0)||(a[I+1|0]=46,k=I+2|0),0!=e;);w=-1,(2147483645-(I=(t=M-Q|0)+l|0)|0)<(0|r)||(br(A,32,g,I=(r=!r||((o=k-(s+16|0)|0)-2|0)>=(0|r)?o=k-(s+16|0)|0:r+2|0)+I|0,C),kC(A,c,l),br(A,48,g,I,65536^C),kC(A,s+16|0,o),br(A,48,r-o|0,0,0),kC(A,Q,t),br(A,32,g,I,8192^C),w=(0|g)<(0|I)?I:g)}else br(A,32,g,t=d+3|0,-65537&C),kC(A,p,d),r=32&I,kC(A,e!=e?r?85596:85774:r?85247:85460,3),br(A,32,g,t,8192^C),w=(0|g)<(0|t)?t:g;return V=s+560|0,0|w},function(A,e){var g;A|=0,g=e|=0,e=f[e>>2]+7&-8,f[g>>2]=e+16,Q[A>>3]=ge(f[e>>2],f[e+4>>2],f[e+8>>2],f[e+12>>2])},function(A,e,g){e|=0,g|=0;var r,C,I=0,i=0;return r=f[84+(A|=0)>>2],i=f[r+4>>2],C=f[A+28>>2],(I=(I=f[A+20>>2]-C|0)>>>0>i>>>0?i:I)&&(_A(f[r>>2],C,I),f[r>>2]=I+f[r>>2],i=f[r+4>>2]-I|0,f[r+4>>2]=i),I=f[r>>2],(i=g>>>0>i>>>0?i:g)&&(_A(I,e,i),I=i+f[r>>2]|0,f[r>>2]=I,f[r+4>>2]=f[r+4>>2]-i),a[0|I]=0,e=f[A+44>>2],f[A+28>>2]=e,f[A+20>>2]=e,0|g},function(A,e,g){g|=0;var r,C,a=0;return _A(e|=0,r=f[84+(A|=0)>>2],g=g>>>0>(a=(C=qe(r,0,a=g+256|0))?C-r|0:a)>>>0?a:g),e=r+a|0,f[A+84>>2]=e,f[A+8>>2]=e,f[A+4>>2]=g+r,0|g},function(A,e,g){e|=0,g|=0;var r,C=0,I=0,b=0;C=f[84+(A|=0)>>2],r=f[C>>2]?C:84412,C=0;A:if(f[A+48>>2])for(;;){if(!(I=f[(C<<2)+r>>2]))break A;if(a[f[A+44>>2]+C|0]=(0|I)>=128?64:I,!((C=C+1|0)>>>0>2]))break}return I=f[A+44>>2],f[A+4>>2]=I,f[A+84>>2]=(C<<2)+r,f[A+8>>2]=C+I,!g|!C||(f[A+4>>2]=I+1,a[0|e]=i[0|I],b=1),0|b},MC,hC,pC,pC,function(A,e,g){g|=0;var r,C=0;return V=r=V+-64|0,C=1,Wr(A|=0,e|=0,0)||(C=0,e&&(C=0,(e=Ee(e,125132))&&(ue(4|(C=r+8|0),0,52),f[r+56>>2]=1,f[r+20>>2]=-1,f[r+16>>2]=A,f[r+8>>2]=e,HC[f[f[e>>2]+28>>2]](e,C,f[g>>2],1),1==(0|(A=f[r+32>>2]))&&(f[g>>2]=f[r+24>>2]),C=1==(0|A)))),V=r- -64|0,0|C},function(A,e,g,r,C,a){g|=0,r|=0,C|=0,a|=0,Wr(A|=0,f[8+(e|=0)>>2],a)&&Bg(e,g,r,C)},function(A,e,g,r,C){if(g|=0,r|=0,C|=0,Wr(A|=0,f[8+(e|=0)>>2],C))1==f[e+28>>2]|f[e+4>>2]!=(0|g)||(f[e+28>>2]=r);else A:if(Wr(A,f[e>>2],C)){if(!(f[e+16>>2]!=(0|g)&f[e+20>>2]!=(0|g))){if(1!=(0|r))break A;return void(f[e+32>>2]=1)}f[e+20>>2]=g,f[e+32>>2]=r,f[e+40>>2]=f[e+40>>2]+1,1!=f[e+36>>2]|2!=f[e+24>>2]||(a[e+54|0]=1),f[e+44>>2]=4}},function(A,e,g,r){g|=0,r|=0,Wr(A|=0,f[8+(e|=0)>>2],0)&&ir(e,g,r)},hC,function(A,e,g,r,C,a){g|=0,r|=0,C|=0,a|=0,Wr(A|=0,f[8+(e|=0)>>2],a)?Bg(e,g,r,C):(A=f[A+8>>2],HC[f[f[A>>2]+20>>2]](A,e,g,r,C,a))},function(A,e,g,r,C){if(g|=0,r|=0,C|=0,Wr(A|=0,f[8+(e|=0)>>2],C))1==f[e+28>>2]|f[e+4>>2]!=(0|g)||(f[e+28>>2]=r);else A:{if(Wr(A,f[e>>2],C)){if(!(f[e+16>>2]!=(0|g)&f[e+20>>2]!=(0|g))){if(1!=(0|r))break A;return void(f[e+32>>2]=1)}f[e+32>>2]=r;e:if(4!=f[e+44>>2]){if(I[e+52>>1]=0,A=f[A+8>>2],HC[f[f[A>>2]+20>>2]](A,e,g,g,1,C),i[e+53|0]){if(f[e+44>>2]=3,!i[e+52|0])break e;break A}f[e+44>>2]=4}if(f[e+20>>2]=g,f[e+40>>2]=f[e+40>>2]+1,1!=f[e+36>>2]|2!=f[e+24>>2])break A;return void(a[e+54|0]=1)}A=f[A+8>>2],HC[f[f[A>>2]+24>>2]](A,e,g,r,C)}},function(A,e,g,r){g|=0,r|=0,Wr(A|=0,f[8+(e|=0)>>2],0)?ir(e,g,r):(A=f[A+8>>2],HC[f[f[A>>2]+28>>2]](A,e,g,r))},hC,function(A){return 84787},hC,function(A){return 85058},hC,function(A){return 84147},function(A){var e;return e=A|=0,A=f[A>>2],f[e>>2]=A+1,0|((0|(A=a[0|A]))<0?65533:255&A)},function(A,e,g,r,C,I){A|=0,e|=0,g|=0,r|=0,C|=0,I|=0;var b,s=0,t=0,n=0,o=0,B=0,c=0,G=0,w=0;if(b=Or(408),f[b+4>>2]=r,f[b>>2]=g,e?(_A(b+16|0,e,376),Q[b+392>>3]=(Q[e+368>>3]-Q[e>>3])/+(g>>>0),e=0):e=1,f[b+400>>2]=C,a[b+8|0]=e,I){if(g=f[A+24>>2])for(e=f[A+20>>2],r=f[A+8>>2];(C=f[f[(e>>>8&16777212)+r>>2]+((1023&e)<<2)>>2])&&(mA(C),g=f[A+24>>2],r=f[A+8>>2],e=f[A+20>>2]),e=e+1|0,f[A+20>>2]=e,g=g-1|0,f[A+24>>2]=g,e>>>0>=2048&&(mA(f[r>>2]),r=f[A+8>>2]+4|0,f[A+8>>2]=r,e=f[A+20>>2]-1024|0,f[A+20>>2]=e,g=f[A+24>>2]),g;);e=f[A+28>>2],f[A+420>>2]=f[e>>2],(g=f[A+32>>2])&&(a[e+8|0]=i[g+8|0],_A(e+16|0,A+40|0,376),(e=f[A+32>>2])&&mA(e),f[A+32>>2]=0)}if(g=(r=f[A+24>>2])+f[A+20>>2]|0,C=f[A+12>>2],(0|g)==(0|((0|(e=f[A+8>>2]))!=(0|C)?(C-e<<8)-1:0))){V=s=V-32|0;A:{e:{g:{r:{if((e=f[16+(I=A+4|0)>>2])>>>0>=1024){if(f[I+16>>2]=e-1024,e=f[I+4>>2],c=f[e>>2],C=e+4|0,f[I+4>>2]=C,(0|(e=f[I+8>>2]))==f[I+12>>2])if((t=f[I>>2])>>>0>>0)g=Qe((r=(1+(C-t>>2)|0)/-2<<2)+C|0,C,e=e-C|0)+e|0,f[I+8>>2]=g,f[I+4>>2]=r+f[I+4>>2];else{if((g=(0|e)==(0|t)?1:e-t>>1)>>>0>=1073741824)break r;if(G=(r=g<<2)+(n=Or(r))|0,g=r=n+(-4&g)|0,(0|e)!=(0|C)){if(w=-4&(e=e-C|0),B=1+((o=e-4|0)>>>2|0)&7)for(g=0,e=r;f[e>>2]=f[C>>2],C=C+4|0,e=e+4|0,(0|B)!=(0|(g=g+1|0)););else e=r;if(g=r+w|0,!(o>>>0<28))for(;f[e>>2]=f[C>>2],f[e+4>>2]=f[C+4>>2],f[e+8>>2]=f[C+8>>2],f[e+12>>2]=f[C+12>>2],f[e+16>>2]=f[C+16>>2],f[e+20>>2]=f[C+20>>2],f[e+24>>2]=f[C+24>>2],f[e+28>>2]=f[C+28>>2],C=C+32|0,(0|g)!=(0|(e=e+32|0)););}f[I+12>>2]=G,f[I+8>>2]=g,f[I+4>>2]=r,f[I>>2]=n,t&&(mA(t),g=f[I+8>>2])}else g=e;f[g>>2]=c,f[I+8>>2]=f[I+8>>2]+4;break A}if((t=(C=f[I+8>>2])-f[I+4>>2]>>2)>>>0<(r=(e=f[I+12>>2])-(g=f[I>>2])|0)>>2>>>0){if((0|e)!=(0|C)){f[s+8>>2]=Or(4096),se(I,s+8|0);break A}if(f[s+8>>2]=Or(4096),ie(I,s+8|0),e=f[I+4>>2],c=f[e>>2],C=e+4|0,f[I+4>>2]=C,(0|(e=f[I+8>>2]))==f[I+12>>2])if((t=f[I>>2])>>>0>>0)g=Qe((r=(1+(C-t>>2)|0)/-2<<2)+C|0,C,e=e-C|0)+e|0,f[I+8>>2]=g,f[I+4>>2]=r+f[I+4>>2];else{if((g=(0|e)==(0|t)?1:e-t>>1)>>>0>=1073741824)break r;if(G=(r=g<<2)+(n=Or(r))|0,g=r=n+(-4&g)|0,(0|e)!=(0|C)){if(w=-4&(e=e-C|0),B=1+((o=e-4|0)>>>2|0)&7)for(g=0,e=r;f[e>>2]=f[C>>2],C=C+4|0,e=e+4|0,(0|B)!=(0|(g=g+1|0)););else e=r;if(g=r+w|0,!(o>>>0<28))for(;f[e>>2]=f[C>>2],f[e+4>>2]=f[C+4>>2],f[e+8>>2]=f[C+8>>2],f[e+12>>2]=f[C+12>>2],f[e+16>>2]=f[C+16>>2],f[e+20>>2]=f[C+20>>2],f[e+24>>2]=f[C+24>>2],f[e+28>>2]=f[C+28>>2],C=C+32|0,(0|g)!=(0|(e=e+32|0)););}f[I+12>>2]=G,f[I+8>>2]=g,f[I+4>>2]=r,f[I>>2]=n,t&&(mA(t),g=f[I+8>>2])}else g=e;f[g>>2]=c,f[I+8>>2]=f[I+8>>2]+4;break A}if(f[s+24>>2]=I+12,!((e=(0|e)==(0|g)?1:r>>1)>>>0>=1073741824)){if(e=Or(g=e<<2),f[s+8>>2]=e,r=e+(t<<2)|0,f[s+16>>2]=r,f[s+20>>2]=e+g,f[s+12>>2]=r,f[s+4>>2]=Or(4096),se(s+8|0,s+4|0),(0|(C=f[I+8>>2]))==f[I+4>>2]){e=C;break e}for(;ie(s+8|0,C=C-4|0),f[I+4>>2]!=(0|C););break g}}Lr(),k()}e=f[I+8>>2]}g=f[I>>2],f[I>>2]=f[s+8>>2],f[s+8>>2]=g,f[I+4>>2]=f[s+12>>2],f[s+12>>2]=C,f[I+8>>2]=f[s+16>>2],f[s+16>>2]=e,r=f[I+12>>2],f[I+12>>2]=f[s+20>>2],f[s+20>>2]=r,(0|e)!=(0|C)&&(f[s+16>>2]=e+(3+(C-e|0)&-4)),g&&mA(g)}V=s+32|0,g=(r=f[A+24>>2])+f[A+20>>2]|0,e=f[A+8>>2]}f[f[e+(g>>>8&16777212)>>2]+((1023&g)<<2)>>2]=b,f[A+24>>2]=r+1},function(A){var e=0,g=0,r=0,C=0,I=0,b=0,s=0,t=0,n=0;g=f[420+(A|=0)>>2]+1|0,f[A+420>>2]=g;A:{if(e=f[A+32>>2]){if(g>>>0>(s=f[e+4>>2])>>>0){(g=f[A+28>>2])&&(mA(g),e=f[A+32>>2]),f[A+32>>2]=0,f[A+28>>2]=e;break A}for(I=A+40|0,b=e+16|0,t=f[A+28>>2]+16|0,n=+(g>>>0)/+(s>>>0),e=0;;){if(r=Q[(g=e<<3)+b>>3],C=Q[g+t>>3],Q[g+I>>3]=r==r?(r-C)*n+C:C,47==(0|(g=1|e)))break A;r=Q[(g<<=3)+b>>3],C=Q[g+t>>3],Q[g+I>>3]=r==r?(r-C)*n+C:C,e=e+2|0}}if(e=f[A+28>>2],g>>>0>c[e>>2]){if(I=f[A+24>>2]){if(a[A+416|0]=0,b=f[A+8>>2],g=f[A+20>>2],e=f[f[b+(g>>>8&16777212)>>2]+((1023&g)<<2)>>2],f[A+32>>2]=e,f[A+24>>2]=I-1,g=g+1|0,f[A+20>>2]=g,g>>>0>=2048&&(mA(f[b>>2]),f[A+8>>2]=f[A+8>>2]+4,f[A+20>>2]=f[A+20>>2]-1024,e=f[A+32>>2]),i[e+8|0])_A(e+16|0,f[A+28>>2]+16|0,376),e=f[A+32>>2],f[e+368>>2]=0,f[e+372>>2]=0,r=Q[A+40>>3],f[e+392>>2]=0,f[e+396>>2]=0,Q[e+16>>3]=r;else if(g=f[A+28>>2],i[g+8|0]&&(_A(g+16|0,e+16|0,376),e=f[A+28>>2],f[e+368>>2]=0,f[e+372>>2]=0,!(e=f[A+32>>2])))break A;-1!=(0|(g=f[e+400>>2]))&&(f[A+424>>2]=g),f[A+420>>2]=0,Q[e+16>>3]=Q[e+392>>3]*+c[e+4>>2]+Q[e+16>>3];break A}a[A+416|0]=1}else r=Q[e+392>>3]+Q[A+40>>3],Q[A+40>>3]=r,Q[e+16>>3]=r}return 0|(i[A+416|0]?0:A+40)},function(A){return f[424+(A|=0)>>2]},function(A){var e=0;return f[(A|=0)>>2]=132304,(e=f[A+28>>2])&&mA(e),(e=f[A+32>>2])&&mA(e),Se(A+4|0),0|A},function(A){var e=0;f[(A|=0)>>2]=132304,(e=f[A+28>>2])&&mA(e),(e=f[A+32>>2])&&mA(e),Se(A+4|0),mA(A)},function(A,e,g){e|=0,g|=0;var r=0,C=0,i=0,b=0,s=0,t=0,n=0,k=0,o=0,B=0,c=0,G=0,w=0,D=0,u=0,l=0,x=0,d=0,m=0;if(!f[1088+(A|=0)>>2])return 0;A:if(e){for(n=A+648|0,t=A- -64|0;;){if(r=f[A+1088>>2],!(r=0|HC[f[f[r>>2]+4>>2]](r)))break A;if(C=re(Q[A+32>>3]+Q[r+16>>3]/+f[A+24>>2]),Q[A+32>>3]=C,C=Ig(6.283185307179586*C),C=re(Q[A+16>>3]+Q[r>>3]*(.06*C*Q[r+8>>3]+1)/+f[A+8>>2]),Q[A+16>>3]=C,s=Cr(f[56848],f[56849],1284865837,1481765933),i=U,i=(s=s+1|0)?i:i+1|0,f[56848]=s,f[56849]=i,b=.75*Q[A+40>>3]+ +(i>>>1|0)/2147483647,Q[A+40>>3]=b,k=Q[r+24>>3],i=C>=Q[r+32>>3],a[A+48|0]=i,b=(B=k)*(k=.2*b),b=Oe(t+512|0,Oe(t+440|0,C=Q[r+352>>3]*(k*Q[r+48>>3]+Q[r+40>>3]*(C+C+-1+(i?b:.01*b)))*.5,Q[r+104>>3],Q[r+168>>3]),Q[r+112>>3],Q[r+176>>3]),b=Oe(t+8|0,Oe(t+80|0,Oe(t+152|0,Oe(t+224|0,Oe(t+296|0,Oe(t+368|0,b==b?(b-C)*Q[r+184>>3]+C:C,Q[r+96>>3],Q[r+160>>3]),Q[r+88>>3],Q[r+152>>3]),Q[r+80>>3],Q[r+144>>3]),Q[r+72>>3],Q[r+136>>3]),Q[r+64>>3],Q[r+128>>3]),Q[r+56>>3],Q[r+120>>3]),s=Cr(f[56848],f[56849],1284865837,1481765933),i=U,i=(s=s+1|0)?i:i+1|0,f[56848]=s,f[56849]=i,C=.75*Q[A+56>>3]+ +(i>>>1|0)/2147483647,Q[A+56>>3]=C,i=(o<<1)+g|0,k=Oe(n+8|0,C=Q[r+352>>3]*(Q[r+192>>3]*(.3*C))*.5,Q[r+200>>3],Q[r+248>>3]),c=Q[r+296>>3],G=Oe(n+80|0,C,Q[r+208>>3],Q[r+256>>3]),w=Q[r+304>>3],D=Oe(n+152|0,C,Q[r+216>>3],Q[r+264>>3]),u=Q[r+312>>3],l=Oe(n+224|0,C,Q[r+224>>3],Q[r+272>>3]),x=Q[r+320>>3],d=Oe(n+296|0,C,Q[r+232>>3],Q[r+280>>3]),m=Q[r+328>>3],B=b,b=(Oe(n+368|0,C,Q[r+240>>3],Q[r+288>>3])-C)*Q[r+336>>3]+(m*(d-C)+(x*(l-C)+(u*(D-C)+(w*(G-C)+(c*(k-C)+0))))),C=(B+(C==C?(C-b)*Q[r+344>>3]+b:b))*Q[r+360>>3]*4e3,r=(0|(r=E(C)<2147483648?~~C:-2147483648))>=32e3?32e3:r,I[i>>1]=(0|r)<=-32e3?-32e3:r,(0|(o=o+1|0))==(0|e))break}o=e}return 0|(e>>>0>o>>>0?o:e)},function(A,e){e|=0,f[1088+(A|=0)>>2]=e},MC,hC,vC,function(A,e,g,r){return U=0,0}],YC.grow=function(A){var e=this.length;return this.length=this.length+A,e},YC.set=function(A,e){this[A]=e},YC.get=function(A){return this[A]},YC);return{v:function(){var A,e=0;V=A=V-16|0,0|X(A+12|0,A+8|0)||(e=IA(4+(f[A+12>>2]<<2)|0),f[56800]=e,e&&(!(e=IA(f[A+8>>2]))||(f[f[56800]+(f[A+12>>2]<<2)>>2]=0,0|W(f[56800],0|e)))&&(f[56800]=0)),V=A+16|0,f[56841]=227236,f[56823]=42},w:dC,x:lC,y:function(A,e){e|=0,f[(A|=0)>>2]=e},z:function(A,e){return e|=0,a[f[(A|=0)+4>>2]+e|0]},A:DC,B:cC,C:function(A){return i[(A|=0)+12|0]},D:function(A,e){e|=0,a[(A|=0)+12|0]=e},E:function(A){return i[(A|=0)+13|0]},F:function(A,e){e|=0,a[(A|=0)+13|0]=e},G:function(A){return i[(A|=0)+14|0]},H:function(A,e){e|=0,a[(A|=0)+14|0]=e},I:function(A){return i[(A|=0)+15|0]},J:function(A,e){e|=0,a[(A|=0)+15|0]=e},K:wC,L:function(A,e){e|=0,f[(A|=0)+16>>2]=e},M:EC,N:function(A,e){e|=0,f[(A|=0)+20>>2]=e},O:dC,P:lC,Q:uC,R:DC,S:GC,T:wC,U:EC,V:function(A){return f[(A|=0)+24>>2]},W:dC,X:function(){var A,e=0,g=0,r=0,C=0,b=0,s=0,t=0,n=0,k=0,o=0,w=0,D=0,u=0,l=0,x=0,d=0,m=0,M=0,v=0,p=0,Y=0,H=0,N=0;if(A=Or(20),f[A+16>>2]=0,f[A+8>>2]=175,f[A+12>>2]=50,!(e=f[33208])){V=e=(V=v=V-16|0)-80|0;A:{if(g=Hg(84292)){if(f[e+32>>2]=g,Gg(137584,160,85959,e+32|0),-31==(0|fr(137584)))break A;if(f[e+16>>2]=g,Gg(137584,160,86031,e+16|0),-31==(0|fr(137584)))break A}(g=Hg(84619))&&(f[e>>2]=g,Gg(137584,160,85959,e),-31==(0|fr(137584)))||(g=i[84826]|i[84827]<<8|i[84828]<<16|i[84829]<<24,f[34396]=i[84822]|i[84823]<<8|i[84824]<<16|i[84825]<<24,f[34397]=g,I[68804]=i[84846]|i[84847]<<8,g=i[84842]|i[84843]<<8|i[84844]<<16|i[84845]<<24,f[34400]=i[84838]|i[84839]<<8|i[84840]<<16|i[84841]<<24,f[34401]=g,g=i[84834]|i[84835]<<8|i[84836]<<16|i[84837]<<24,f[34398]=i[84830]|i[84831]<<8|i[84832]<<16|i[84833]<<24,f[34399]=g)}if(V=e+80|0,f[v+12>>2]=0,C=v+12|0,V=t=V-16|0,f[t+12>>2]=22050,Be(85144)||Be(85315)||Be(85473)||Be(85698),w=t+12|0,V=b=V-16|0,f[b+12>>2]=0,!((e=Le(137832,84262,0,C))||(e=Le(137836,84420,0,C))||(e=Le(137840,84813,0,C))||(e=Le(137820,85016,b+12|0,C))))if(f[34454]=c[b+12>>2]/68,g=f[34460],f[34456]=g,g&&83969==(0|(k=i[0|g]|i[g+1|0]<<8|i[g+2|0]<<16|i[g+3|0]<<24))){if(D=i[g+4|0]|i[g+5|0]<<8|i[g+6|0]<<16|i[g+7|0]<<24,g=f[34458],l=i[0|g],f[34461]=l,l)for(e=g+4|0,k=0;r=G(k,44)+137856|0,C=i[0|e],f[r+36>>2]=C,f[r+40>>2]=i[e+1|0],s=i[e+8|0]|i[e+9|0]<<8|i[e+10|0]<<16|i[e+11|0]<<24,g=i[e+4|0]|i[e+5|0]<<8|i[e+6|0]<<16|i[e+7|0]<<24,a[0|r]=g,a[r+1|0]=g>>>8,a[r+2|0]=g>>>16,a[r+3|0]=g>>>24,a[r+4|0]=s,a[r+5|0]=s>>>8,a[r+6|0]=s>>>16,a[r+7|0]=s>>>24,s=i[e+16|0]|i[e+17|0]<<8|i[e+18|0]<<16|i[e+19|0]<<24,g=i[e+12|0]|i[e+13|0]<<8|i[e+14|0]<<16|i[e+15|0]<<24,a[r+8|0]=g,a[r+9|0]=g>>>8,a[r+10|0]=g>>>16,a[r+11|0]=g>>>24,a[r+12|0]=s,a[r+13|0]=s>>>8,a[r+14|0]=s>>>16,a[r+15|0]=s>>>24,s=i[e+24|0]|i[e+25|0]<<8|i[e+26|0]<<16|i[e+27|0]<<24,g=i[e+20|0]|i[e+21|0]<<8|i[e+22|0]<<16|i[e+23|0]<<24,a[r+16|0]=g,a[r+17|0]=g>>>8,a[r+18|0]=g>>>16,a[r+19|0]=g>>>24,a[r+20|0]=s,a[r+21|0]=s>>>8,a[r+22|0]=s>>>16,a[r+23|0]=s>>>24,s=i[e+32|0]|i[e+33|0]<<8|i[e+34|0]<<16|i[e+35|0]<<24,g=i[e+28|0]|i[e+29|0]<<8|i[e+30|0]<<16|i[e+31|0]<<24,a[r+24|0]=g,a[r+25|0]=g>>>8,a[r+26|0]=g>>>16,a[r+27|0]=g>>>24,a[r+28|0]=s,a[r+29|0]=s>>>8,a[r+30|0]=s>>>16,a[r+31|0]=s>>>24,g=e+36|0,f[r+32>>2]=g,e=g+(C<<4)|0,(0|l)!=(0|(k=k+1|0)););(0|l)<=f[34457]&&(f[34457]=0),e=0,w&&(f[w>>2]=D)}else A:{e:{if(C){if(g=f[C>>2]){mA(f[g+4>>2]),e=f[C>>2];break e}if(e=IA(16),f[C>>2]=e,e)break e;e=48}else e=268436223;break A}f[e>>2]=1,f[e+4>>2]=$r(137584),g=f[C>>2],f[g+12>>2]=83969,f[g+8>>2]=k,e=268436223}if(V=b+16|0,g=e,!e){if(w=f[t+12>>2],f[50754]=w,f[50759]=0,f[50760]=134217728/(0|w),f[50762]=0,f[50763]=0,f[50765]=2147483647,f[50781]=100,f[50779]=32,f[50761]=(w<<6)/(0|w),e=f[26385],f[50784]=f[26384],f[50785]=e,e=f[26387],f[50786]=f[26386],f[50787]=e,e=f[26389],f[50788]=f[26388],f[50789]=e,e=f[26391],f[50790]=f[26390],f[50791]=e,e=f[26393],f[50792]=f[26392],f[50793]=e,e=f[26395],f[50794]=f[26394],f[50795]=e,e=f[26397],f[50796]=f[26396],f[50797]=e,f[50798]=f[26398],D=(0|(e=(0|(C=G(w,60)))/12800|0))>=128?128:e,f[50799]=D,f[50800]=(0|D)/2,!(22050==(0|w)|(0|C)<12800)){if(w=1&(e=(0|D)<=1?1:D),m=+(0|D),k=0,(0|D)>=2)for(D=2147483646&e,e=0;x=k+132160|0,n=127*(1-Cg(6.283185307179586*+(0|k)/m)),u=E(n)<2147483648?~~n:-2147483648,a[0|x]=u,x=(C=1|k)+132160|0,n=127*(1-Cg(6.283185307179586*+(0|C)/m)),u=E(n)<2147483648?~~n:-2147483648,a[0|x]=u,k=k+2|0,(0|D)!=(0|(e=e+2|0)););w&&(e=k+132160|0,n=127*(1-Cg(6.283185307179586*+(0|k)/m)),x=E(n)<2147483648?~~n:-2147483648,a[0|e]=x)}if(f[50801]=105792,f[56797]=ZA(),f[55964]=38,f[55921]=1,f[55918]=22050,f[56606]=0,f[55960]=110928,f[55958]=0,f[55959]=1074266112,f[55956]=100,f[55922]=20,f[55923]=220,f[55916]=1,f[55917]=0,vr(),f[56244]=0,f[56245]=0,f[55928]=0,f[55926]=0,f[55927]=0,f[55924]=0,f[56246]=0,f[56247]=0,f[56260]=0,f[56261]=0,f[56262]=0,f[56263]=0,f[56276]=0,f[56277]=0,f[56278]=0,f[56279]=0,f[55974]=0,f[55975]=0,f[55972]=0,f[55973]=0,m=-3.141592653589793/+(0|(e=f[55918])),Q[27967]=m,C=(0|G(e,630))/1e4|0,f[55920]=C,e=(0|G(e,950))/1e4|0,f[55919]=e,n=-2*m,Q[27968]=n,m=(H=$A(m*+(0|C)))*-H,Q[28129]=m,n=H*Cg(n*+(0|e)),n+=n,Q[28128]=n,Q[28127]=1-n-m,f[55990]=0,f[55991]=0,f[55988]=0,f[55989]=0,f[56006]=0,f[56007]=0,f[56004]=0,f[56005]=0,f[56022]=0,f[56023]=0,f[56020]=0,f[56021]=0,f[56038]=0,f[56039]=0,f[56036]=0,f[56037]=0,f[56054]=0,f[56055]=0,f[56052]=0,f[56053]=0,f[56070]=0,f[56071]=0,f[56068]=0,f[56069]=0,f[56086]=0,f[56087]=0,f[56084]=0,f[56085]=0,f[56102]=0,f[56103]=0,f[56100]=0,f[56101]=0,f[56118]=0,f[56119]=0,f[56116]=0,f[56117]=0,f[56134]=0,f[56135]=0,f[56132]=0,f[56133]=0,f[56150]=0,f[56151]=0,f[56148]=0,f[56149]=0,f[56166]=0,f[56167]=0,f[56164]=0,f[56165]=0,f[56182]=0,f[56183]=0,f[56180]=0,f[56181]=0,f[56198]=0,f[56199]=0,f[56196]=0,f[56197]=0,f[56214]=0,f[56215]=0,f[56212]=0,f[56213]=0,f[56230]=0,f[56231]=0,f[56228]=0,f[56229]=0,f[56639]=59,f[56640]=59,f[56629]=0,f[56630]=59,f[56619]=89,f[56620]=160,f[56609]=280,f[56610]=688,f[56611]=1064,f[56621]=70,f[56631]=59,f[56612]=2806,f[56613]=3260,f[56622]=160,f[56623]=200,f[56632]=59,f[56633]=59,f[56641]=89,f[56642]=149,f[56643]=200,f[56644]=200,f[56634]=59,f[56635]=59,f[56624]=200,f[56625]=500,f[56614]=3700,f[56615]=6500,f[56645]=500,f[56646]=0,f[56616]=7e3,f[56626]=500,f[56636]=0,f[56647]=0,f[56637]=0,f[56627]=500,f[56617]=8e3,f[56669]=89,f[56648]=0,f[56638]=0,f[56628]=89,f[56618]=280,f[56657]=62,f[56655]=0,f[56656]=0,f[56653]=50,f[56654]=0,f[56651]=0,f[56652]=0,f[56649]=0,f[56650]=40,f[56607]=1e3,f[56608]=59,V=b=V-416|0,f[b+16>>2]=137584,f[b+20>>2]=47,f[b+24>>2]=85952,dg(e=b+240|0,85699,b+16|0),s=Ae(e,86034)){if(xe(b+240|0,170,s))for(w=5|(e=b+240|0),D=10|e;47!=i[b+240|0]&&(1701736308!=f[b+240>>2]?pg(b+240|0,86614,9)||(f[b+4>>2]=b+32,f[b>>2]=b+239,2==(0|aA(D,86829,b))&&(C=f[34064],f[(l=136272+(C<<4)|0)>>2]=a[b+239|0],e=$r(b+32|0),f[34064]=C+1,f[l+12>>2]=e,f[l+4>>2]=0)):(V=e=V-48|0,f[32960]=-1,f[32961]=-1,f[32970]=-1,f[32971]=-1,f[32968]=-1,f[32969]=-1,f[32966]=-1,f[32967]=-1,f[32964]=-1,f[32965]=-1,f[32962]=-1,f[32963]=-1,f[e+36>>2]=131876,f[e+32>>2]=131872,f[e+28>>2]=131868,f[e+24>>2]=131864,f[e+20>>2]=131860,f[e+16>>2]=131856,f[e+12>>2]=131852,f[e+8>>2]=131848,f[e+4>>2]=131844,f[e>>2]=131840,aA(w,84222,e),V=e+48|0)),xe(b+240|0,170,s););tr(s)}V=b+416|0,f[50297]=0,f[50298]=0,f[50301]=0,f[50302]=0,f[50299]=0,f[50300]=0,og(0,85698),f[36425]=0,f[36424]=0,f[36426]=0,f[36427]=-1,AC(),YA(0),r=f[25690],f[34062]=r,o=f[25689],b=f[25688],f[34060]=b,f[34061]=o,d=f[25687],s=f[25686],f[34058]=s,f[34059]=d,M=f[25685],l=f[25684],f[34056]=l,f[34057]=M,p=f[25683],w=f[25682],f[34054]=w,f[34055]=p,Y=f[25681],D=f[25680],f[34052]=D,f[34053]=Y,u=f[25679],C=f[25678],f[34050]=C,f[34051]=u,x=f[25677],e=f[25676],f[34048]=e,f[34049]=x,f[33729]=e,f[33730]=x,f[33731]=C,f[33732]=u,f[33733]=D,f[33734]=Y,f[33735]=w,f[33736]=p,f[33737]=l,f[33738]=M,f[33739]=s,f[33740]=d,f[33741]=b,f[33742]=o,f[33743]=r,ye(1,175),ye(2,100),ye(6,f[47200]),ye(5,f[47201]),ye(7,0),f[47198]=0,f[47197]=0,n=+h()/1e3,C=Cr(e=E(n)<0x8000000000000000?~~n>>>0:0,0,1103515245,0),e=U,e=(C=C+12345|0)>>>0<12345?e+1|0:e,f[33209]=tC(C,e)}if(V=t+16|0,g){x=f[30450],p=f[v+12>>2],t=(o=V-560|0)+48|0,V=r=(V=o)-16|0;A:{e:switch(0|jr(g-268435967|0,24)){case 0:oC(t,84133,512);break A;case 1:oC(t,84580,512);break A;case 2:oC(t,84747,512);break A;case 3:oC(t,85084,512);break A;case 4:oC(t,85251,512);break A;case 5:oC(t,85380,512);break A;case 6:oC(t,85607,512);break A;case 7:oC(t,85722,512);break A;case 8:oC(t,85913,512);break A;case 9:oC(t,86046,512);break A;case 10:oC(t,86153,512);break A;case 11:oC(t,86678,512);break A;case 12:oC(t,86773,512);break A;case 14:oC(t,86958,512);break A;case 15:oC(t,87071,512);break A;default:break e}if(1879048192&g)f[r>>2]=g,Gg(t,512,87182,r);else{if(k=0,b=B[123728+((g>>>0<=153?g:0)<<1)>>1]+121804|0,g=f[f[56841]+20>>2]){Y=f[g+4>>2],d=f[g>>2],M=f[d>>2]+1794895138|0,u=rC(f[d+8>>2],M),C=rC(f[d+12>>2],M),e=rC(f[d+16>>2],M);e:if(!(Y>>>2>>>0<=u>>>0||3&(e|C)|(g=Y-(u<<2)|0)>>>0<=C>>>0|e>>>0>=g>>>0))for(w=e>>>2|0,D=C>>>2|0;;){if(l=rC(f[(g=((e=(C=(s=u>>>1|0)+N|0)<<1)+D<<2)+d|0)>>2],M),(g=rC(f[g+4>>2],M))>>>0>=Y>>>0|l>>>0>=Y-g>>>0|i[(g+l|0)+d|0])break e;if(!(g=Qr(b,g+d|0))){if(e=rC(f[(g=(e+w<<2)+d|0)>>2],M),(g=rC(f[g+4>>2],M))>>>0>=Y>>>0|e>>>0>=Y-g>>>0)break e;k=i[(e+g|0)+d|0]?0:g+d|0;break e}if(1==(0|u))break e;u=(g=(0|g)<0)?s:u-s|0,N=g?N:C}}if((g=Lg(e=k||b))>>>0>=512){_A(t,e,511),a[t+511|0]=0;break A}_A(t,e,g+1|0)}}V=r+16|0;A:if(p){e:switch(f[p>>2]){case 0:f[o+16>>2]=f[p+4>>2],f[o+20>>2]=o+48,eC(x,87384,o+16|0);break A;case 1:break e;default:break A}e=f[p+12>>2],g=f[p+8>>2],f[o+36>>2]=f[p+4>>2],U=g,f[o+40>>2]=e,f[o+44>>2]=U,f[o+32>>2]=o+48,eC(x,87521,o+32|0)}else f[o>>2]=o+48,eC(x,87700,o);V=o+560|0,-12!=(0|v)&&(g=f[v+12>>2])&&(mA(f[g+4>>2]),mA(f[v+12>>2]),f[v+12>>2]=0)}g=f[24806],f[34389]=0,f[32538]=g,g=(1e3+((g=G(f[50754],100))-((0|g)%1e3|0)|0)|0)/500|0,f[34390]=g,g=OA(f[34391],g),f[34392]=g,g&&(f[34391]=g,f[34393]=40,(g=OA(f[34388],1440))&&(f[34388]=g)),f[47198]=0,V=v+16|0,e=f[50754],f[33208]=e}return f[A+4>>2]=e,f[A>>2]=le(),0|A},Y:function(A,e,g){A|=0,e|=0,g|=0,f[34440]=g,xr(3,f[A+12>>2]),xr(1,f[A+8>>2]),(A=f[A+16>>2])?eg(A):Xg(1024),_(e),f[34440]=0},Z:function(A,e,g){return A|=0,e|=0,g|=0,f[34440]=0,(A=Ae(g,1032))?(f[47195]=A,f[47197]=130,A||(f[47195]=f[30450]),_(e),f[47195]=0,f[47197]=0,f[47195]=f[30450],tr(A),0):-1},_:function(A){return 36},$:function(A,e,g){var r;return A|=0,e|=0,V=r=V-32|0,(g|=0)?(f[r+24>>2]=0,f[r+28>>2]=0,f[r+16>>2]=0,f[r+20>>2]=0,f[r+12>>2]=g,f[r+8>>2]=e,a[r+21|0]=0,e=eg(r+8|0)):e=Xg(e),f[A+16>>2]=201188,V=r+32|0,0|e},aa:function(A,e,g,r){var C;return A|=0,e|=0,V=C=V-32|0,(g|=0)|(r|=0)?(f[C+24>>2]=0,f[C+28>>2]=0,f[C+16>>2]=0,f[C+20>>2]=0,f[C+12>>2]=g,f[C+8>>2]=e,a[C+22|0]=0,a[C+20|0]=r,e=eg(C+8|0)):e=Xg(e),f[A+16>>2]=201188,V=C+32|0,0|e},ba:function(A,e,g,r,C){var I;return A|=0,e|=0,V=I=V-32|0,(r|=0)|(C|=0)|(g|=0)?(f[I+24>>2]=0,f[I+28>>2]=0,f[I+16>>2]=0,f[I+20>>2]=0,f[I+12>>2]=g,f[I+8>>2]=e,a[I+21|0]=C,a[I+20|0]=r,e=eg(I+8|0)):e=Xg(e),f[A+16>>2]=201188,V=I+32|0,0|e},ca:function(A,e,g,r,C,I){var i;return A|=0,e|=0,V=i=V-32|0,(r|=0)|(C|=0)|(I|=0)|(g|=0)?(f[i+24>>2]=0,f[i+28>>2]=0,f[i+16>>2]=0,f[i+20>>2]=0,f[i+12>>2]=g,f[i+8>>2]=e,a[i+22|0]=I,a[i+21|0]=C,a[i+20|0]=r,e=eg(i+8|0)):e=Xg(e),f[A+16>>2]=201188,V=i+32|0,0|e},da:function(A,e){return e|=0,f[f[(A|=0)>>2]+(e<<2)>>2]},ea:function(A,e,g){e|=0,g|=0,f[f[(A|=0)>>2]+(e<<2)>>2]=g},fa:uC,ga:DC,ha:cC,ia:GC,ja:function(A,e){e|=0,f[(A|=0)+12>>2]=e},ka:dC,la:function(){return 0},ma:function(){return 1},na:function(){return 2},oa:function(){return 3},pa:function(){return 4},qa:function(){return 5},ra:function(){return 6},sa:function(){return 7},ta:function(){return 8},ua:HC,va:function(){return 227192},wa:mA,xa:IA,ya:function(A){return(A|=0)?0|!!(0|Ee(A,125228)):0}}}(A)}(e)},instantiate:function(A,e){return{then:function(g){var r=new b.Module(A);g({instance:new b.Instance(r,e)})}}},RuntimeError:Error};"object"!=typeof b&&z("no native wasm support detected");var s=!1;function t(A,e){A||z(e)}var n,k,o,B,c,Q,G,w="undefined"!=typeof TextDecoder?new TextDecoder("utf8"):void 0;function E(A,e,g){for(var r=e+g,C=e;A[C]&&!(C>=r);)++C;if(C-e>16&&A.buffer&&w)return w.decode(A.subarray(e,C));for(var a="";e>10,56320|1023&b)}}else a+=String.fromCharCode((31&I)<<6|f)}else a+=String.fromCharCode(I)}return a}function D(A,e){return A?E(k,A,e):""}function u(A,e,g,r){if(!(r>0))return 0;for(var C=g,a=g+r-1,I=0;I=55296&&f<=57343)f=65536+((1023&f)<<10)|1023&A.charCodeAt(++I);if(f<=127){if(g>=a)break;e[g++]=f}else if(f<=2047){if(g+1>=a)break;e[g++]=192|f>>6,e[g++]=128|63&f}else if(f<=65535){if(g+2>=a)break;e[g++]=224|f>>12,e[g++]=128|f>>6&63,e[g++]=128|63&f}else{if(g+3>=a)break;e[g++]=240|f>>18,e[g++]=128|f>>12&63,e[g++]=128|f>>6&63,e[g++]=128|63&f}}return e[g]=0,g-C}function l(A){for(var e=0,g=0;g=55296&&r<=57343?(e+=4,++g):e+=3}return e}var x,d=A.INITIAL_MEMORY||16777216;i=A.wasmMemory?A.wasmMemory:new b.Memory({initial:d/65536,maximum:d/65536}),x=i.buffer,A.HEAP8=n=new Int8Array(x),A.HEAP16=o=new Int16Array(x),A.HEAP32=B=new Int32Array(x),A.HEAPU8=k=new Uint8Array(x),A.HEAPU16=new Uint16Array(x),A.HEAPU32=c=new Uint32Array(x),A.HEAPF32=Q=new Float32Array(x),A.HEAPF64=G=new Float64Array(x),d=i.buffer.byteLength;var m=[],M=[],v=[],h=!1;function p(A){M.unshift(A)}var Y,H,N=0,P=null;function F(e){N++,A.monitorRunDependencies&&A.monitorRunDependencies(N)}function y(e){if(N--,A.monitorRunDependencies&&A.monitorRunDependencies(N),0==N&&P){var g=P;P=null,g()}}function z(e){throw A.onAbort&&A.onAbort(e),f(e="Aborted("+e+")"),s=!0,e+=". Build with -sASSERTIONS for more info.",new b.RuntimeError(e)}function O(A){this.name="ExitStatus",this.message="Program terminated with exit("+A+")",this.status=A}function Z(e){for(;e.length>0;)e.shift()(A)}function K(A,e="i8"){switch(e.endsWith("*")&&(e="*"),e){case"i1":case"i8":return n[A|0];case"i16":return o[A>>1];case"i32":case"i64":return B[A>>2];case"float":return Q[A>>2];case"double":return G[A>>3];case"*":return c[A>>2];default:z("invalid type for getValue: "+e)}return null}function W(A){this.excPtr=A,this.ptr=A-24,this.set_type=function(A){c[this.ptr+4>>2]=A},this.get_type=function(){return c[this.ptr+4>>2]},this.set_destructor=function(A){c[this.ptr+8>>2]=A},this.get_destructor=function(){return c[this.ptr+8>>2]},this.set_refcount=function(A){B[this.ptr>>2]=A},this.set_caught=function(A){A=A?1:0,n[this.ptr+12|0]=A},this.get_caught=function(){return 0!=n[this.ptr+12|0]},this.set_rethrown=function(A){A=A?1:0,n[this.ptr+13|0]=A},this.get_rethrown=function(){return 0!=n[this.ptr+13|0]},this.init=function(A,e){this.set_adjusted_ptr(0),this.set_type(A),this.set_destructor(e),this.set_refcount(0),this.set_caught(!1),this.set_rethrown(!1)},this.add_ref=function(){var A=B[this.ptr>>2];B[this.ptr>>2]=A+1},this.release_ref=function(){var A=B[this.ptr>>2];return B[this.ptr>>2]=A-1,1===A},this.set_adjusted_ptr=function(A){c[this.ptr+16>>2]=A},this.get_adjusted_ptr=function(){return c[this.ptr+16>>2]},this.get_exception_ptr=function(){if(Ae(this.get_type()))return c[this.excPtr>>2];var A=this.get_adjusted_ptr();return 0!==A?A:this.excPtr}}var X={isAbs:A=>"/"===A.charAt(0),splitPath:A=>/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/.exec(A).slice(1),normalizeArray:(A,e)=>{for(var g=0,r=A.length-1;r>=0;r--){var C=A[r];"."===C?A.splice(r,1):".."===C?(A.splice(r,1),g++):g&&(A.splice(r,1),g--)}if(e)for(;g;g--)A.unshift("..");return A},normalize:A=>{var e=X.isAbs(A),g="/"===A.substr(-1);return(A=X.normalizeArray(A.split("/").filter((A=>!!A)),!e).join("/"))||e||(A="."),A&&g&&(A+="/"),(e?"/":"")+A},dirname:A=>{var e=X.splitPath(A),g=e[0],r=e[1];return g||r?(r&&(r=r.substr(0,r.length-1)),g+r):"."},basename:A=>{if("/"===A)return"/";var e=(A=(A=X.normalize(A)).replace(/\/$/,"")).lastIndexOf("/");return-1===e?A:A.substr(e+1)},join:function(){var A=Array.prototype.slice.call(arguments);return X.normalize(A.join("/"))},join2:(A,e)=>X.normalize(A+"/"+e)},L={resolve:function(){for(var A="",e=!1,g=arguments.length-1;g>=-1&&!e;g--){var r=g>=0?arguments[g]:U.cwd();if("string"!=typeof r)throw new TypeError("Arguments to path.resolve must be strings");if(!r)return"";A=r+"/"+A,e=X.isAbs(r)}return(e?"/":"")+(A=X.normalizeArray(A.split("/").filter((A=>!!A)),!e).join("/"))||"."},relative:(A,e)=>{function g(A){for(var e=0;e=0&&""===A[g];g--);return e>g?[]:A.slice(e,g-e+1)}A=L.resolve(A).substr(1),e=L.resolve(e).substr(1);for(var r=g(A.split("/")),C=g(e.split("/")),a=Math.min(r.length,C.length),I=a,f=0;f0&&(I(E(A.output,0)),A.output=[])}},default_tty1_ops:{put_char:function(A,e){null===e||10===e?(f(E(A.output,0)),A.output=[]):0!=e&&A.output.push(e)},fsync:function(A){A.output&&A.output.length>0&&(f(E(A.output,0)),A.output=[])}}};function J(A){z()}var R={ops_table:null,mount:function(A){return R.createNode(null,"/",16895,0)},createNode:function(A,e,g,r){if(U.isBlkdev(g)||U.isFIFO(g))throw new U.ErrnoError(63);R.ops_table||(R.ops_table={dir:{node:{getattr:R.node_ops.getattr,setattr:R.node_ops.setattr,lookup:R.node_ops.lookup,mknod:R.node_ops.mknod,rename:R.node_ops.rename,unlink:R.node_ops.unlink,rmdir:R.node_ops.rmdir,readdir:R.node_ops.readdir,symlink:R.node_ops.symlink},stream:{llseek:R.stream_ops.llseek}},file:{node:{getattr:R.node_ops.getattr,setattr:R.node_ops.setattr},stream:{llseek:R.stream_ops.llseek,read:R.stream_ops.read,write:R.stream_ops.write,allocate:R.stream_ops.allocate,mmap:R.stream_ops.mmap,msync:R.stream_ops.msync}},link:{node:{getattr:R.node_ops.getattr,setattr:R.node_ops.setattr,readlink:R.node_ops.readlink},stream:{}},chrdev:{node:{getattr:R.node_ops.getattr,setattr:R.node_ops.setattr},stream:U.chrdev_stream_ops}});var C=U.createNode(A,e,g,r);return U.isDir(C.mode)?(C.node_ops=R.ops_table.dir.node,C.stream_ops=R.ops_table.dir.stream,C.contents={}):U.isFile(C.mode)?(C.node_ops=R.ops_table.file.node,C.stream_ops=R.ops_table.file.stream,C.usedBytes=0,C.contents=null):U.isLink(C.mode)?(C.node_ops=R.ops_table.link.node,C.stream_ops=R.ops_table.link.stream):U.isChrdev(C.mode)&&(C.node_ops=R.ops_table.chrdev.node,C.stream_ops=R.ops_table.chrdev.stream),C.timestamp=Date.now(),A&&(A.contents[e]=C,A.timestamp=C.timestamp),C},getFileDataAsTypedArray:function(A){return A.contents?A.contents.subarray?A.contents.subarray(0,A.usedBytes):new Uint8Array(A.contents):new Uint8Array(0)},expandFileStorage:function(A,e){var g=A.contents?A.contents.length:0;if(!(g>=e)){e=Math.max(e,g*(g<1048576?2:1.125)>>>0),0!=g&&(e=Math.max(e,256));var r=A.contents;A.contents=new Uint8Array(e),A.usedBytes>0&&A.contents.set(r.subarray(0,A.usedBytes),0)}},resizeFileStorage:function(A,e){if(A.usedBytes!=e)if(0==e)A.contents=null,A.usedBytes=0;else{var g=A.contents;A.contents=new Uint8Array(e),g&&A.contents.set(g.subarray(0,Math.min(e,A.usedBytes))),A.usedBytes=e}},node_ops:{getattr:function(A){var e={};return e.dev=U.isChrdev(A.mode)?A.id:1,e.ino=A.id,e.mode=A.mode,e.nlink=1,e.uid=0,e.gid=0,e.rdev=A.rdev,U.isDir(A.mode)?e.size=4096:U.isFile(A.mode)?e.size=A.usedBytes:U.isLink(A.mode)?e.size=A.link.length:e.size=0,e.atime=new Date(A.timestamp),e.mtime=new Date(A.timestamp),e.ctime=new Date(A.timestamp),e.blksize=4096,e.blocks=Math.ceil(e.size/e.blksize),e},setattr:function(A,e){void 0!==e.mode&&(A.mode=e.mode),void 0!==e.timestamp&&(A.timestamp=e.timestamp),void 0!==e.size&&R.resizeFileStorage(A,e.size)},lookup:function(A,e){throw U.genericErrors[44]},mknod:function(A,e,g,r){return R.createNode(A,e,g,r)},rename:function(A,e,g){if(U.isDir(A.mode)){var r;try{r=U.lookupNode(e,g)}catch(A){}if(r)for(var C in r.contents)throw new U.ErrnoError(55)}delete A.parent.contents[A.name],A.parent.timestamp=Date.now(),A.name=g,e.contents[g]=A,e.timestamp=A.parent.timestamp,A.parent=e},unlink:function(A,e){delete A.contents[e],A.timestamp=Date.now()},rmdir:function(A,e){var g=U.lookupNode(A,e);for(var r in g.contents)throw new U.ErrnoError(55);delete A.contents[e],A.timestamp=Date.now()},readdir:function(A){var e=[".",".."];for(var g in A.contents)A.contents.hasOwnProperty(g)&&e.push(g);return e},symlink:function(A,e,g){var r=R.createNode(A,e,41471,0);return r.link=g,r},readlink:function(A){if(!U.isLink(A.mode))throw new U.ErrnoError(28);return A.link}},stream_ops:{read:function(A,e,g,r,C){var a=A.node.contents;if(C>=A.node.usedBytes)return 0;var I=Math.min(A.node.usedBytes-C,r);if(I>8&&a.subarray)e.set(a.subarray(C,C+I),g);else for(var f=0;f0||g+e{if(!(A=L.resolve(A)))return{path:"",node:null};if((e=Object.assign({follow_mount:!0,recurse_count:0},e)).recurse_count>8)throw new U.ErrnoError(32);for(var g=A.split("/").filter((A=>!!A)),r=U.root,C="/",a=0;a40)throw new U.ErrnoError(32)}}return{path:C,node:r}},getPath:A=>{for(var e;;){if(U.isRoot(A)){var g=A.mount.mountpoint;return e?"/"!==g[g.length-1]?g+"/"+e:g+e:g}e=e?A.name+"/"+e:A.name,A=A.parent}},hashName:(A,e)=>{for(var g=0,r=0;r>>0)%U.nameTable.length},hashAddNode:A=>{var e=U.hashName(A.parent.id,A.name);A.name_next=U.nameTable[e],U.nameTable[e]=A},hashRemoveNode:A=>{var e=U.hashName(A.parent.id,A.name);if(U.nameTable[e]===A)U.nameTable[e]=A.name_next;else for(var g=U.nameTable[e];g;){if(g.name_next===A){g.name_next=A.name_next;break}g=g.name_next}},lookupNode:(A,e)=>{var g=U.mayLookup(A);if(g)throw new U.ErrnoError(g,A);for(var r=U.hashName(A.id,e),C=U.nameTable[r];C;C=C.name_next){var a=C.name;if(C.parent.id===A.id&&a===e)return C}return U.lookup(A,e)},createNode:(A,e,g,r)=>{var C=new U.FSNode(A,e,g,r);return U.hashAddNode(C),C},destroyNode:A=>{U.hashRemoveNode(A)},isRoot:A=>A===A.parent,isMountpoint:A=>!!A.mounted,isFile:A=>32768==(61440&A),isDir:A=>16384==(61440&A),isLink:A=>40960==(61440&A),isChrdev:A=>8192==(61440&A),isBlkdev:A=>24576==(61440&A),isFIFO:A=>4096==(61440&A),isSocket:A=>!(49152&~A),flagModes:{r:0,"r+":2,w:577,"w+":578,a:1089,"a+":1090},modeStringToFlags:A=>{var e=U.flagModes[A];if(void 0===e)throw new Error("Unknown file open mode: "+A);return e},flagsToPermissionString:A=>{var e=["r","w","rw"][3&A];return 512&A&&(e+="w"),e},nodePermissions:(A,e)=>U.ignorePermissions||(!e.includes("r")||292&A.mode)&&(!e.includes("w")||146&A.mode)&&(!e.includes("x")||73&A.mode)?0:2,mayLookup:A=>{var e=U.nodePermissions(A,"x");return e||(A.node_ops.lookup?0:2)},mayCreate:(A,e)=>{try{U.lookupNode(A,e);return 20}catch(A){}return U.nodePermissions(A,"wx")},mayDelete:(A,e,g)=>{var r;try{r=U.lookupNode(A,e)}catch(A){return A.errno}var C=U.nodePermissions(A,"wx");if(C)return C;if(g){if(!U.isDir(r.mode))return 54;if(U.isRoot(r)||U.getPath(r)===U.cwd())return 10}else if(U.isDir(r.mode))return 31;return 0},mayOpen:(A,e)=>A?U.isLink(A.mode)?32:U.isDir(A.mode)&&("r"!==U.flagsToPermissionString(e)||512&e)?31:U.nodePermissions(A,U.flagsToPermissionString(e)):44,MAX_OPEN_FDS:4096,nextfd:(A=0,e=U.MAX_OPEN_FDS)=>{for(var g=A;g<=e;g++)if(!U.streams[g])return g;throw new U.ErrnoError(33)},getStream:A=>U.streams[A],createStream:(A,e,g)=>{U.FSStream||(U.FSStream=function(){this.shared={}},U.FSStream.prototype={},Object.defineProperties(U.FSStream.prototype,{object:{get:function(){return this.node},set:function(A){this.node=A}},isRead:{get:function(){return 1!=(2097155&this.flags)}},isWrite:{get:function(){return!!(2097155&this.flags)}},isAppend:{get:function(){return 1024&this.flags}},flags:{get:function(){return this.shared.flags},set:function(A){this.shared.flags=A}},position:{get:function(){return this.shared.position},set:function(A){this.shared.position=A}}})),A=Object.assign(new U.FSStream,A);var r=U.nextfd(e,g);return A.fd=r,U.streams[r]=A,A},closeStream:A=>{U.streams[A]=null},chrdev_stream_ops:{open:A=>{var e=U.getDevice(A.node.rdev);A.stream_ops=e.stream_ops,A.stream_ops.open&&A.stream_ops.open(A)},llseek:()=>{throw new U.ErrnoError(70)}},major:A=>A>>8,minor:A=>255&A,makedev:(A,e)=>A<<8|e,registerDevice:(A,e)=>{U.devices[A]={stream_ops:e}},getDevice:A=>U.devices[A],getMounts:A=>{for(var e=[],g=[A];g.length;){var r=g.pop();e.push(r),g.push.apply(g,r.mounts)}return e},syncfs:(A,e)=>{"function"==typeof A&&(e=A,A=!1),U.syncFSRequests++,U.syncFSRequests>1&&f("warning: "+U.syncFSRequests+" FS.syncfs operations in flight at once, probably just doing extra work");var g=U.getMounts(U.root.mount),r=0;function C(A){return U.syncFSRequests--,e(A)}function a(A){if(A)return a.errored?void 0:(a.errored=!0,C(A));++r>=g.length&&C(null)}g.forEach((e=>{if(!e.type.syncfs)return a(null);e.type.syncfs(e,A,a)}))},mount:(A,e,g)=>{var r,C="/"===g,a=!g;if(C&&U.root)throw new U.ErrnoError(10);if(!C&&!a){var I=U.lookupPath(g,{follow_mount:!1});if(g=I.path,r=I.node,U.isMountpoint(r))throw new U.ErrnoError(10);if(!U.isDir(r.mode))throw new U.ErrnoError(54)}var f={type:A,opts:e,mountpoint:g,mounts:[]},i=A.mount(f);return i.mount=f,f.root=i,C?U.root=i:r&&(r.mounted=f,r.mount&&r.mount.mounts.push(f)),i},unmount:A=>{var e=U.lookupPath(A,{follow_mount:!1});if(!U.isMountpoint(e.node))throw new U.ErrnoError(28);var g=e.node,r=g.mounted,C=U.getMounts(r);Object.keys(U.nameTable).forEach((A=>{for(var e=U.nameTable[A];e;){var g=e.name_next;C.includes(e.mount)&&U.destroyNode(e),e=g}})),g.mounted=null;var a=g.mount.mounts.indexOf(r);g.mount.mounts.splice(a,1)},lookup:(A,e)=>A.node_ops.lookup(A,e),mknod:(A,e,g)=>{var r=U.lookupPath(A,{parent:!0}).node,C=X.basename(A);if(!C||"."===C||".."===C)throw new U.ErrnoError(28);var a=U.mayCreate(r,C);if(a)throw new U.ErrnoError(a);if(!r.node_ops.mknod)throw new U.ErrnoError(63);return r.node_ops.mknod(r,C,e,g)},create:(A,e)=>(e=void 0!==e?e:438,e&=4095,e|=32768,U.mknod(A,e,0)),mkdir:(A,e)=>(e=void 0!==e?e:511,e&=1023,e|=16384,U.mknod(A,e,0)),mkdirTree:(A,e)=>{for(var g=A.split("/"),r="",C=0;C(void 0===g&&(g=e,e=438),e|=8192,U.mknod(A,e,g)),symlink:(A,e)=>{if(!L.resolve(A))throw new U.ErrnoError(44);var g=U.lookupPath(e,{parent:!0}).node;if(!g)throw new U.ErrnoError(44);var r=X.basename(e),C=U.mayCreate(g,r);if(C)throw new U.ErrnoError(C);if(!g.node_ops.symlink)throw new U.ErrnoError(63);return g.node_ops.symlink(g,r,A)},rename:(A,e)=>{var g,r,C=X.dirname(A),a=X.dirname(e),I=X.basename(A),f=X.basename(e);if(g=U.lookupPath(A,{parent:!0}).node,r=U.lookupPath(e,{parent:!0}).node,!g||!r)throw new U.ErrnoError(44);if(g.mount!==r.mount)throw new U.ErrnoError(75);var i,b=U.lookupNode(g,I),s=L.relative(A,a);if("."!==s.charAt(0))throw new U.ErrnoError(28);if("."!==(s=L.relative(e,C)).charAt(0))throw new U.ErrnoError(55);try{i=U.lookupNode(r,f)}catch(A){}if(b!==i){var t=U.isDir(b.mode),n=U.mayDelete(g,I,t);if(n)throw new U.ErrnoError(n);if(n=i?U.mayDelete(r,f,t):U.mayCreate(r,f))throw new U.ErrnoError(n);if(!g.node_ops.rename)throw new U.ErrnoError(63);if(U.isMountpoint(b)||i&&U.isMountpoint(i))throw new U.ErrnoError(10);if(r!==g&&(n=U.nodePermissions(g,"w")))throw new U.ErrnoError(n);U.hashRemoveNode(b);try{g.node_ops.rename(b,r,f)}catch(A){throw A}finally{U.hashAddNode(b)}}},rmdir:A=>{var e=U.lookupPath(A,{parent:!0}).node,g=X.basename(A),r=U.lookupNode(e,g),C=U.mayDelete(e,g,!0);if(C)throw new U.ErrnoError(C);if(!e.node_ops.rmdir)throw new U.ErrnoError(63);if(U.isMountpoint(r))throw new U.ErrnoError(10);e.node_ops.rmdir(e,g),U.destroyNode(r)},readdir:A=>{var e=U.lookupPath(A,{follow:!0}).node;if(!e.node_ops.readdir)throw new U.ErrnoError(54);return e.node_ops.readdir(e)},unlink:A=>{var e=U.lookupPath(A,{parent:!0}).node;if(!e)throw new U.ErrnoError(44);var g=X.basename(A),r=U.lookupNode(e,g),C=U.mayDelete(e,g,!1);if(C)throw new U.ErrnoError(C);if(!e.node_ops.unlink)throw new U.ErrnoError(63);if(U.isMountpoint(r))throw new U.ErrnoError(10);e.node_ops.unlink(e,g),U.destroyNode(r)},readlink:A=>{var e=U.lookupPath(A).node;if(!e)throw new U.ErrnoError(44);if(!e.node_ops.readlink)throw new U.ErrnoError(28);return L.resolve(U.getPath(e.parent),e.node_ops.readlink(e))},stat:(A,e)=>{var g=U.lookupPath(A,{follow:!e}).node;if(!g)throw new U.ErrnoError(44);if(!g.node_ops.getattr)throw new U.ErrnoError(63);return g.node_ops.getattr(g)},lstat:A=>U.stat(A,!0),chmod:(A,e,g)=>{var r;"string"==typeof A?r=U.lookupPath(A,{follow:!g}).node:r=A;if(!r.node_ops.setattr)throw new U.ErrnoError(63);r.node_ops.setattr(r,{mode:4095&e|-4096&r.mode,timestamp:Date.now()})},lchmod:(A,e)=>{U.chmod(A,e,!0)},fchmod:(A,e)=>{var g=U.getStream(A);if(!g)throw new U.ErrnoError(8);U.chmod(g.node,e)},chown:(A,e,g,r)=>{var C;"string"==typeof A?C=U.lookupPath(A,{follow:!r}).node:C=A;if(!C.node_ops.setattr)throw new U.ErrnoError(63);C.node_ops.setattr(C,{timestamp:Date.now()})},lchown:(A,e,g)=>{U.chown(A,e,g,!0)},fchown:(A,e,g)=>{var r=U.getStream(A);if(!r)throw new U.ErrnoError(8);U.chown(r.node,e,g)},truncate:(A,e)=>{if(e<0)throw new U.ErrnoError(28);var g;"string"==typeof A?g=U.lookupPath(A,{follow:!0}).node:g=A;if(!g.node_ops.setattr)throw new U.ErrnoError(63);if(U.isDir(g.mode))throw new U.ErrnoError(31);if(!U.isFile(g.mode))throw new U.ErrnoError(28);var r=U.nodePermissions(g,"w");if(r)throw new U.ErrnoError(r);g.node_ops.setattr(g,{size:e,timestamp:Date.now()})},ftruncate:(A,e)=>{var g=U.getStream(A);if(!g)throw new U.ErrnoError(8);if(!(2097155&g.flags))throw new U.ErrnoError(28);U.truncate(g.node,e)},utime:(A,e,g)=>{var r=U.lookupPath(A,{follow:!0}).node;r.node_ops.setattr(r,{timestamp:Math.max(e,g)})},open:(e,g,r)=>{if(""===e)throw new U.ErrnoError(44);var C;if(r=void 0===r?438:r,r=64&(g="string"==typeof g?U.modeStringToFlags(g):g)?4095&r|32768:0,"object"==typeof e)C=e;else{e=X.normalize(e);try{C=U.lookupPath(e,{follow:!(131072&g)}).node}catch(A){}}var a=!1;if(64&g)if(C){if(128&g)throw new U.ErrnoError(20)}else C=U.mknod(e,r,0),a=!0;if(!C)throw new U.ErrnoError(44);if(U.isChrdev(C.mode)&&(g&=-513),65536&g&&!U.isDir(C.mode))throw new U.ErrnoError(54);if(!a){var I=U.mayOpen(C,g);if(I)throw new U.ErrnoError(I)}512&g&&!a&&U.truncate(C,0),g&=-131713;var f=U.createStream({node:C,path:U.getPath(C),flags:g,seekable:!0,position:0,stream_ops:C.stream_ops,ungotten:[],error:!1});return f.stream_ops.open&&f.stream_ops.open(f),!A.logReadFiles||1&g||(U.readFiles||(U.readFiles={}),e in U.readFiles||(U.readFiles[e]=1)),f},close:A=>{if(U.isClosed(A))throw new U.ErrnoError(8);A.getdents&&(A.getdents=null);try{A.stream_ops.close&&A.stream_ops.close(A)}catch(A){throw A}finally{U.closeStream(A.fd)}A.fd=null},isClosed:A=>null===A.fd,llseek:(A,e,g)=>{if(U.isClosed(A))throw new U.ErrnoError(8);if(!A.seekable||!A.stream_ops.llseek)throw new U.ErrnoError(70);if(0!=g&&1!=g&&2!=g)throw new U.ErrnoError(28);return A.position=A.stream_ops.llseek(A,e,g),A.ungotten=[],A.position},read:(A,e,g,r,C)=>{if(r<0||C<0)throw new U.ErrnoError(28);if(U.isClosed(A))throw new U.ErrnoError(8);if(1==(2097155&A.flags))throw new U.ErrnoError(8);if(U.isDir(A.node.mode))throw new U.ErrnoError(31);if(!A.stream_ops.read)throw new U.ErrnoError(28);var a=void 0!==C;if(a){if(!A.seekable)throw new U.ErrnoError(70)}else C=A.position;var I=A.stream_ops.read(A,e,g,r,C);return a||(A.position+=I),I},write:(A,e,g,r,C,a)=>{if(r<0||C<0)throw new U.ErrnoError(28);if(U.isClosed(A))throw new U.ErrnoError(8);if(!(2097155&A.flags))throw new U.ErrnoError(8);if(U.isDir(A.node.mode))throw new U.ErrnoError(31);if(!A.stream_ops.write)throw new U.ErrnoError(28);A.seekable&&1024&A.flags&&U.llseek(A,0,2);var I=void 0!==C;if(I){if(!A.seekable)throw new U.ErrnoError(70)}else C=A.position;var f=A.stream_ops.write(A,e,g,r,C,a);return I||(A.position+=f),f},allocate:(A,e,g)=>{if(U.isClosed(A))throw new U.ErrnoError(8);if(e<0||g<=0)throw new U.ErrnoError(28);if(!(2097155&A.flags))throw new U.ErrnoError(8);if(!U.isFile(A.node.mode)&&!U.isDir(A.node.mode))throw new U.ErrnoError(43);if(!A.stream_ops.allocate)throw new U.ErrnoError(138);A.stream_ops.allocate(A,e,g)},mmap:(A,e,g,r,C)=>{if(2&r&&!(2&C)&&2!=(2097155&A.flags))throw new U.ErrnoError(2);if(1==(2097155&A.flags))throw new U.ErrnoError(2);if(!A.stream_ops.mmap)throw new U.ErrnoError(43);return A.stream_ops.mmap(A,e,g,r,C)},msync:(A,e,g,r,C)=>A.stream_ops.msync?A.stream_ops.msync(A,e,g,r,C):0,munmap:A=>0,ioctl:(A,e,g)=>{if(!A.stream_ops.ioctl)throw new U.ErrnoError(59);return A.stream_ops.ioctl(A,e,g)},readFile:(A,e={})=>{if(e.flags=e.flags||0,e.encoding=e.encoding||"binary","utf8"!==e.encoding&&"binary"!==e.encoding)throw new Error('Invalid encoding type "'+e.encoding+'"');var g,r=U.open(A,e.flags),C=U.stat(A).size,a=new Uint8Array(C);return U.read(r,a,0,C,0),"utf8"===e.encoding?g=E(a,0):"binary"===e.encoding&&(g=a),U.close(r),g},writeFile:(A,e,g={})=>{g.flags=g.flags||577;var r=U.open(A,g.flags,g.mode);if("string"==typeof e){var C=new Uint8Array(l(e)+1),a=u(e,C,0,C.length);U.write(r,C,0,a,void 0,g.canOwn)}else{if(!ArrayBuffer.isView(e))throw new Error("Unsupported data type");U.write(r,e,0,e.byteLength,void 0,g.canOwn)}U.close(r)},cwd:()=>U.currentPath,chdir:A=>{var e=U.lookupPath(A,{follow:!0});if(null===e.node)throw new U.ErrnoError(44);if(!U.isDir(e.node.mode))throw new U.ErrnoError(54);var g=U.nodePermissions(e.node,"x");if(g)throw new U.ErrnoError(g);U.currentPath=e.path},createDefaultDirectories:()=>{U.mkdir("/tmp"),U.mkdir("/home"),U.mkdir("/home/web_user")},createDefaultDevices:()=>{U.mkdir("/dev"),U.registerDevice(U.makedev(1,3),{read:()=>0,write:(A,e,g,r,C)=>r}),U.mkdev("/dev/null",U.makedev(1,3)),V.register(U.makedev(5,0),V.default_tty_ops),V.register(U.makedev(6,0),V.default_tty1_ops),U.mkdev("/dev/tty",U.makedev(5,0)),U.mkdev("/dev/tty1",U.makedev(6,0)),U.mkdir("/dev/shm"),U.mkdir("/dev/shm/tmp")},createSpecialDirectories:()=>{U.mkdir("/proc");var A=U.mkdir("/proc/self");U.mkdir("/proc/self/fd"),U.mount({mount:()=>{var e=U.createNode(A,"fd",16895,73);return e.node_ops={lookup:(A,e)=>{var g=+e,r=U.getStream(g);if(!r)throw new U.ErrnoError(8);var C={parent:null,mount:{mountpoint:"fake"},node_ops:{readlink:()=>r.path}};return C.parent=C,C}},e}},{},"/proc/self/fd")},createStandardStreams:()=>{A.stdin?U.createDevice("/dev","stdin",A.stdin):U.symlink("/dev/tty","/dev/stdin"),A.stdout?U.createDevice("/dev","stdout",null,A.stdout):U.symlink("/dev/tty","/dev/stdout"),A.stderr?U.createDevice("/dev","stderr",null,A.stderr):U.symlink("/dev/tty1","/dev/stderr"),U.open("/dev/stdin",0),U.open("/dev/stdout",1),U.open("/dev/stderr",1)},ensureErrnoError:()=>{U.ErrnoError||(U.ErrnoError=function(A,e){this.node=e,this.setErrno=function(A){this.errno=A},this.setErrno(A),this.message="FS error"},U.ErrnoError.prototype=new Error,U.ErrnoError.prototype.constructor=U.ErrnoError,[44].forEach((A=>{U.genericErrors[A]=new U.ErrnoError(A),U.genericErrors[A].stack=""})))},staticInit:()=>{U.ensureErrnoError(),U.nameTable=new Array(4096),U.mount(R,{},"/"),U.createDefaultDirectories(),U.createDefaultDevices(),U.createSpecialDirectories(),U.filesystems={MEMFS:R}},init:(e,g,r)=>{U.init.initialized=!0,U.ensureErrnoError(),A.stdin=e||A.stdin,A.stdout=g||A.stdout,A.stderr=r||A.stderr,U.createStandardStreams()},quit:()=>{U.init.initialized=!1;for(var A=0;A{var g=0;return A&&(g|=365),e&&(g|=146),g},findObject:(A,e)=>{var g=U.analyzePath(A,e);return g.exists?g.object:null},analyzePath:(A,e)=>{try{A=(r=U.lookupPath(A,{follow:!e})).path}catch(A){}var g={isRoot:!1,exists:!1,error:0,name:null,path:null,object:null,parentExists:!1,parentPath:null,parentObject:null};try{var r=U.lookupPath(A,{parent:!0});g.parentExists=!0,g.parentPath=r.path,g.parentObject=r.node,g.name=X.basename(A),r=U.lookupPath(A,{follow:!e}),g.exists=!0,g.path=r.path,g.object=r.node,g.name=r.node.name,g.isRoot="/"===r.path}catch(A){g.error=A.errno}return g},createPath:(A,e,g,r)=>{A="string"==typeof A?A:U.getPath(A);for(var C=e.split("/").reverse();C.length;){var a=C.pop();if(a){var I=X.join2(A,a);try{U.mkdir(I)}catch(A){}A=I}}return I},createFile:(A,e,g,r,C)=>{var a=X.join2("string"==typeof A?A:U.getPath(A),e),I=U.getMode(r,C);return U.create(a,I)},createDataFile:(A,e,g,r,C,a)=>{var I=e;A&&(A="string"==typeof A?A:U.getPath(A),I=e?X.join2(A,e):A);var f=U.getMode(r,C),i=U.create(I,f);if(g){if("string"==typeof g){for(var b=new Array(g.length),s=0,t=g.length;s{var C=X.join2("string"==typeof A?A:U.getPath(A),e),a=U.getMode(!!g,!!r);U.createDevice.major||(U.createDevice.major=64);var I=U.makedev(U.createDevice.major++,0);return U.registerDevice(I,{open:A=>{A.seekable=!1},close:A=>{r&&r.buffer&&r.buffer.length&&r(10)},read:(A,e,r,C,a)=>{for(var I=0,f=0;f{for(var I=0;I{if(A.isDevice||A.isFolder||A.link||A.contents)return!0;throw"undefined"!=typeof XMLHttpRequest?new Error("Lazy loading should have been performed (contents set) in createLazyFile, but it was not. Lazy loading only works in web workers. Use --embed-file or --preload-file in emcc on the main thread."):new Error("Cannot load without read() or XMLHttpRequest.")},createLazyFile:(A,g,r,C,a)=>{function f(){this.lengthKnown=!1,this.chunks=[]}if(f.prototype.get=function(A){if(!(A>this.length-1||A<0)){var e=A%this.chunkSize,g=A/this.chunkSize|0;return this.getter(g)[e]}},f.prototype.setDataGetter=function(A){this.getter=A},f.prototype.cacheLength=function(){var A=new XMLHttpRequest;if(A.open("HEAD",r,!1),A.send(null),!(A.status>=200&&A.status<300||304===A.status))throw new Error("Couldn't load "+r+". Status: "+A.status);var e,g=Number(A.getResponseHeader("Content-length")),C=(e=A.getResponseHeader("Accept-Ranges"))&&"bytes"===e,a=(e=A.getResponseHeader("Content-Encoding"))&&"gzip"===e,f=1048576;C||(f=g);var i=this;i.setDataGetter((A=>{var e=A*f,C=(A+1)*f-1;if(C=Math.min(C,g-1),void 0===i.chunks[A]&&(i.chunks[A]=((A,e)=>{if(A>e)throw new Error("invalid range ("+A+", "+e+") or no bytes requested!");if(e>g-1)throw new Error("only "+g+" bytes available! programmer error!");var C=new XMLHttpRequest;if(C.open("GET",r,!1),g!==f&&C.setRequestHeader("Range","bytes="+A+"-"+e),C.responseType="arraybuffer",C.overrideMimeType&&C.overrideMimeType("text/plain; charset=x-user-defined"),C.send(null),!(C.status>=200&&C.status<300||304===C.status))throw new Error("Couldn't load "+r+". Status: "+C.status);return void 0!==C.response?new Uint8Array(C.response||[]):T(C.responseText||"",!0)})(e,C)),void 0===i.chunks[A])throw new Error("doXHR failed!");return i.chunks[A]})),!a&&g||(f=g=1,g=this.getter(0).length,f=g,I("LazyFiles on gzip forces download of the whole file when length is accessed")),this._length=g,this._chunkSize=f,this.lengthKnown=!0},"undefined"!=typeof XMLHttpRequest){if(!e)throw"Cannot do synchronous binary XHRs outside webworkers in modern browsers. Use --embed-file or --preload-file in emcc";var i=new f;Object.defineProperties(i,{length:{get:function(){return this.lengthKnown||this.cacheLength(),this._length}},chunkSize:{get:function(){return this.lengthKnown||this.cacheLength(),this._chunkSize}}});var b={isDevice:!1,contents:i}}else b={isDevice:!1,url:r};var s=U.createFile(A,g,b,C,a);b.contents?s.contents=b.contents:b.url&&(s.contents=null,s.url=b.url),Object.defineProperties(s,{usedBytes:{get:function(){return this.contents.length}}});var t={};function k(A,e,g,r,C){var a=A.node.contents;if(C>=a.length)return 0;var I=Math.min(a.length-C,r);if(a.slice)for(var f=0;f{var e=s.stream_ops[A];t[A]=function(){return U.forceLoadFile(s),e.apply(null,arguments)}})),t.read=(A,e,g,r,C)=>(U.forceLoadFile(s),k(A,e,g,r,C)),t.mmap=(A,e,g,r,C)=>{U.forceLoadFile(s);var a=J();if(!a)throw new U.ErrnoError(48);return k(A,n,a,e,g),{ptr:a,allocated:!0}},s.stream_ops=t,s},indexedDB:()=>window.indexedDB||window.mozIndexedDB||window.webkitIndexedDB||window.msIndexedDB,DB_NAME:()=>"EM_FS_"+window.location.pathname,DB_VERSION:20,DB_STORE_NAME:"FILE_DATA",saveFilesToDB:(A,e,g)=>{e=e||(()=>{}),g=g||(()=>{});var r=U.indexedDB();try{var C=r.open(U.DB_NAME(),U.DB_VERSION)}catch(A){return g(A)}C.onupgradeneeded=()=>{I("creating db"),C.result.createObjectStore(U.DB_STORE_NAME)},C.onsuccess=()=>{var r=C.result.transaction([U.DB_STORE_NAME],"readwrite"),a=r.objectStore(U.DB_STORE_NAME),I=0,f=0,i=A.length;function b(){0==f?e():g()}A.forEach((A=>{var e=a.put(U.analyzePath(A).object.contents,A);e.onsuccess=()=>{++I+f==i&&b()},e.onerror=()=>{f++,I+f==i&&b()}})),r.onerror=g},C.onerror=g},loadFilesFromDB:(A,e,g)=>{e=e||(()=>{}),g=g||(()=>{});var r=U.indexedDB();try{var C=r.open(U.DB_NAME(),U.DB_VERSION)}catch(A){return g(A)}C.onupgradeneeded=g,C.onsuccess=()=>{var r=C.result;try{var a=r.transaction([U.DB_STORE_NAME],"readonly")}catch(A){return void g(A)}var I=a.objectStore(U.DB_STORE_NAME),f=0,i=0,b=A.length;function s(){0==i?e():g()}A.forEach((A=>{var e=I.get(A);e.onsuccess=()=>{U.analyzePath(A).exists&&U.unlink(A),U.createDataFile(X.dirname(A),X.basename(A),e.result,!0,!0,!0),++f+i==b&&s()},e.onerror=()=>{i++,f+i==b&&s()}})),a.onerror=g},C.onerror=g}},j={DEFAULT_POLLMASK:5,calculateAt:function(A,e,g){if(X.isAbs(e))return e;var r;-100===A?r=U.cwd():r=j.getStreamFromFD(A).path;if(0==e.length){if(!g)throw new U.ErrnoError(44);return r}return X.join2(r,e)},doStat:function(A,e,g){try{var r=A(e)}catch(A){if(A&&A.node&&X.normalize(e)!==X.normalize(U.getPath(A.node)))return-54;throw A}B[g>>2]=r.dev,B[g+8>>2]=r.ino,B[g+12>>2]=r.mode,c[g+16>>2]=r.nlink,B[g+20>>2]=r.uid,B[g+24>>2]=r.gid,B[g+28>>2]=r.rdev,H=[r.size>>>0,(Y=r.size,+Math.abs(Y)>=1?Y>0?(0|Math.min(+Math.floor(Y/4294967296),4294967295))>>>0:~~+Math.ceil((Y-+(~~Y>>>0))/4294967296)>>>0:0)],B[g+40>>2]=H[0],B[g+44>>2]=H[1],B[g+48>>2]=4096,B[g+52>>2]=r.blocks;var C=r.atime.getTime(),a=r.mtime.getTime(),I=r.ctime.getTime();return H=[Math.floor(C/1e3)>>>0,(Y=Math.floor(C/1e3),+Math.abs(Y)>=1?Y>0?(0|Math.min(+Math.floor(Y/4294967296),4294967295))>>>0:~~+Math.ceil((Y-+(~~Y>>>0))/4294967296)>>>0:0)],B[g+56>>2]=H[0],B[g+60>>2]=H[1],c[g+64>>2]=C%1e3*1e3,H=[Math.floor(a/1e3)>>>0,(Y=Math.floor(a/1e3),+Math.abs(Y)>=1?Y>0?(0|Math.min(+Math.floor(Y/4294967296),4294967295))>>>0:~~+Math.ceil((Y-+(~~Y>>>0))/4294967296)>>>0:0)],B[g+72>>2]=H[0],B[g+76>>2]=H[1],c[g+80>>2]=a%1e3*1e3,H=[Math.floor(I/1e3)>>>0,(Y=Math.floor(I/1e3),+Math.abs(Y)>=1?Y>0?(0|Math.min(+Math.floor(Y/4294967296),4294967295))>>>0:~~+Math.ceil((Y-+(~~Y>>>0))/4294967296)>>>0:0)],B[g+88>>2]=H[0],B[g+92>>2]=H[1],c[g+96>>2]=I%1e3*1e3,H=[r.ino>>>0,(Y=r.ino,+Math.abs(Y)>=1?Y>0?(0|Math.min(+Math.floor(Y/4294967296),4294967295))>>>0:~~+Math.ceil((Y-+(~~Y>>>0))/4294967296)>>>0:0)],B[g+104>>2]=H[0],B[g+108>>2]=H[1],0},doMsync:function(A,e,g,r,C){if(!U.isFile(e.node.mode))throw new U.ErrnoError(43);if(2&r)return 0;var a=k.slice(A,A+g);U.msync(e,a,C,g,r)},varargs:void 0,get:function(){return j.varargs+=4,B[j.varargs-4>>2]},getStr:function(A){return D(A)},getStreamFromFD:function(A){var e=U.getStream(A);if(!e)throw new U.ErrnoError(8);return e}};var S={};function q(){if(!q.strings){var A={USER:"web_user",LOGNAME:"web_user",PATH:"/",PWD:"/",HOME:"/home/web_user",LANG:("object"==typeof navigator&&navigator.languages&&navigator.languages[0]||"C").replace("-","_")+".UTF-8",_:a||"./this.program"};for(var e in S)void 0===S[e]?delete A[e]:A[e]=S[e];var g=[];for(var e in A)g.push(e+"="+A[e]);q.strings=g}return q.strings}var _=function(A,e,g,r){A||(A=this),this.parent=A,this.mount=A.mount,this.mounted=null,this.id=U.nextInode++,this.name=e,this.mode=g,this.node_ops={},this.stream_ops={},this.rdev=r};Object.defineProperties(_.prototype,{read:{get:function(){return!(365&~this.mode)},set:function(A){A?this.mode|=365:this.mode&=-366}},write:{get:function(){return!(146&~this.mode)},set:function(A){A?this.mode|=146:this.mode&=-147}},isFolder:{get:function(){return U.isDir(this.mode)}},isDevice:{get:function(){return U.isChrdev(this.mode)}}}),U.FSNode=_,U.staticInit(),A.FS_createPath=U.createPath,A.FS_createDataFile=U.createDataFile,A.FS_unlink=U.unlink,A.FS_createLazyFile=U.createLazyFile,A.FS_createDevice=U.createDevice;var $={g:function(A,e,g,r){z("Assertion failed: "+D(A)+", at: "+[e?D(e):"unknown filename",g,r?D(r):"unknown function"])},u:function(A,e,g){throw new W(A).init(e,g),A},d:function(A,e,g){j.varargs=g;try{var r=j.getStreamFromFD(A);switch(e){case 0:return(C=j.get())<0?-28:U.createStream(r,C).fd;case 1:case 2:case 6:case 7:return 0;case 3:return r.flags;case 4:var C=j.get();return r.flags|=C,0;case 5:C=j.get();return o[C+0>>1]=2,0;case 16:case 8:default:return-28;case 9:return a=28,B[_A()>>2]=a,-1}}catch(A){if(void 0===U||!(A instanceof U.ErrnoError))throw A;return-A.errno}var a},q:function(A,e,g){try{var r=j.getStreamFromFD(A);r.getdents||(r.getdents=U.readdir(r.path));for(var C=280,a=0,I=U.llseek(r,0,1),f=Math.floor(I/C);f>>0,(Y=i,+Math.abs(Y)>=1?Y>0?(0|Math.min(+Math.floor(Y/4294967296),4294967295))>>>0:~~+Math.ceil((Y-+(~~Y>>>0))/4294967296)>>>0:0)],B[e+a>>2]=H[0],B[e+a+4>>2]=H[1],H=[(f+1)*C>>>0,(Y=(f+1)*C,+Math.abs(Y)>=1?Y>0?(0|Math.min(+Math.floor(Y/4294967296),4294967295))>>>0:~~+Math.ceil((Y-+(~~Y>>>0))/4294967296)>>>0:0)],B[e+a+8>>2]=H[0],B[e+a+12>>2]=H[1],o[e+a+16>>1]=280,n[e+a+18|0]=b,u(s,k,e+a+19,256),a+=C,f+=1}return U.llseek(r,f*C,0),a}catch(A){if(void 0===U||!(A instanceof U.ErrnoError))throw A;return-A.errno}},h:function(A,e,g){j.varargs=g;try{var r=j.getStreamFromFD(A);switch(e){case 21509:case 21505:case 21510:case 21511:case 21512:case 21506:case 21507:case 21508:case 21523:case 21524:return r.tty?0:-59;case 21519:if(!r.tty)return-59;var C=j.get();return B[C>>2]=0,0;case 21520:return r.tty?-28:-59;case 21531:C=j.get();return U.ioctl(r,e,C);default:return-28}}catch(A){if(void 0===U||!(A instanceof U.ErrnoError))throw A;return-A.errno}},e:function(A,e,g,r){j.varargs=r;try{e=j.getStr(e),e=j.calculateAt(A,e);var C=r?j.get():0;return U.open(e,g,C).fd}catch(A){if(void 0===U||!(A instanceof U.ErrnoError))throw A;return-A.errno}},o:function(A){try{return A=j.getStr(A),U.rmdir(A),0}catch(A){if(void 0===U||!(A instanceof U.ErrnoError))throw A;return-A.errno}},n:function(A,e){try{return A=j.getStr(A),j.doStat(U.stat,A,e)}catch(A){if(void 0===U||!(A instanceof U.ErrnoError))throw A;return-A.errno}},p:function(A,e,g){try{return e=j.getStr(e),e=j.calculateAt(A,e),0===g?U.unlink(e):512===g?U.rmdir(e):z("Invalid flags passed to unlinkat"),0}catch(A){if(void 0===U||!(A instanceof U.ErrnoError))throw A;return-A.errno}},i:function(){return true},l:function(){z("")},f:function(){return Date.now()},j:function(A,e,g){k.copyWithin(A,e,e+g)},m:function(A){k.length,z("OOM")},r:function(A,e){var g=0;return q().forEach((function(r,C){var a=e+g;c[A+4*C>>2]=a,function(A,e){for(var g=0;g>2]=g.length;var r=0;return g.forEach((function(A){r+=A.length+1})),c[e>>2]=r,0},b:function(A){try{var e=j.getStreamFromFD(A);return U.close(e),0}catch(A){if(void 0===U||!(A instanceof U.ErrnoError))throw A;return A.errno}},t:function(A,e,g,r){try{var C=function(A,e,g,r){for(var C=0,a=0;a>2],f=c[e+4>>2];e+=8;var i=U.read(A,n,I,f,r);if(i<0)return-1;if(C+=i,i>2]=C,0}catch(A){if(void 0===U||!(A instanceof U.ErrnoError))throw A;return A.errno}},k:function(A,e,g,r,C){try{var a=(i=g)+2097152>>>0<4194305-!!(f=e)?(f>>>0)+4294967296*i:NaN;if(isNaN(a))return 61;var I=j.getStreamFromFD(A);return U.llseek(I,a,r),H=[I.position>>>0,(Y=I.position,+Math.abs(Y)>=1?Y>0?(0|Math.min(+Math.floor(Y/4294967296),4294967295))>>>0:~~+Math.ceil((Y-+(~~Y>>>0))/4294967296)>>>0:0)],B[C>>2]=H[0],B[C+4>>2]=H[1],I.getdents&&0===a&&0===r&&(I.getdents=null),0}catch(A){if(void 0===U||!(A instanceof U.ErrnoError))throw A;return A.errno}var f,i},c:function(A,e,g,r){try{var C=function(A,e,g,r){for(var C=0,a=0;a>2],f=c[e+4>>2];e+=8;var i=U.write(A,n,I,f,r);if(i<0)return-1;C+=i}return C}(j.getStreamFromFD(A),e,g);return c[r>>2]=C,0}catch(A){if(void 0===U||!(A instanceof U.ErrnoError))throw A;return A.errno}},a:i};!function(){var e,g={a:$};function r(e,g){var r=e.exports;A.asm=r,A.asm.ua,p(A.asm.v),y()}function C(A){r(A.instance)}if(F(),A.instantiateWasm)try{return A.instantiateWasm(g,r)}catch(A){return f("Module.instantiateWasm callback failed with error: "+A),!1}e=C,async function(){return[]}().then((function(A){return b.instantiate(A,g)})).then((function(A){return A})).then(e,(function(A){f("failed to asynchronously prepare wasm: "+A),z(A)}))}(),A.___wasm_call_ctors=function(){return(A.___wasm_call_ctors=A.asm.v).apply(null,arguments)};var AA=A._emscripten_bind_VoidPtr___destroy___0=function(){return(AA=A._emscripten_bind_VoidPtr___destroy___0=A.asm.w).apply(null,arguments)},eA=A._emscripten_bind_espeak_VOICE_get_name_0=function(){return(eA=A._emscripten_bind_espeak_VOICE_get_name_0=A.asm.x).apply(null,arguments)},gA=A._emscripten_bind_espeak_VOICE_set_name_1=function(){return(gA=A._emscripten_bind_espeak_VOICE_set_name_1=A.asm.y).apply(null,arguments)},rA=A._emscripten_bind_espeak_VOICE_get_languages_1=function(){return(rA=A._emscripten_bind_espeak_VOICE_get_languages_1=A.asm.z).apply(null,arguments)},CA=A._emscripten_bind_espeak_VOICE_get_identifier_0=function(){return(CA=A._emscripten_bind_espeak_VOICE_get_identifier_0=A.asm.A).apply(null,arguments)},aA=A._emscripten_bind_espeak_VOICE_set_identifier_1=function(){return(aA=A._emscripten_bind_espeak_VOICE_set_identifier_1=A.asm.B).apply(null,arguments)},IA=A._emscripten_bind_espeak_VOICE_get_gender_0=function(){return(IA=A._emscripten_bind_espeak_VOICE_get_gender_0=A.asm.C).apply(null,arguments)},fA=A._emscripten_bind_espeak_VOICE_set_gender_1=function(){return(fA=A._emscripten_bind_espeak_VOICE_set_gender_1=A.asm.D).apply(null,arguments)},iA=A._emscripten_bind_espeak_VOICE_get_age_0=function(){return(iA=A._emscripten_bind_espeak_VOICE_get_age_0=A.asm.E).apply(null,arguments)},bA=A._emscripten_bind_espeak_VOICE_set_age_1=function(){return(bA=A._emscripten_bind_espeak_VOICE_set_age_1=A.asm.F).apply(null,arguments)},sA=A._emscripten_bind_espeak_VOICE_get_variant_0=function(){return(sA=A._emscripten_bind_espeak_VOICE_get_variant_0=A.asm.G).apply(null,arguments)},tA=A._emscripten_bind_espeak_VOICE_set_variant_1=function(){return(tA=A._emscripten_bind_espeak_VOICE_set_variant_1=A.asm.H).apply(null,arguments)},nA=A._emscripten_bind_espeak_VOICE_get_xx1_0=function(){return(nA=A._emscripten_bind_espeak_VOICE_get_xx1_0=A.asm.I).apply(null,arguments)},kA=A._emscripten_bind_espeak_VOICE_set_xx1_1=function(){return(kA=A._emscripten_bind_espeak_VOICE_set_xx1_1=A.asm.J).apply(null,arguments)},oA=A._emscripten_bind_espeak_VOICE_get_score_0=function(){return(oA=A._emscripten_bind_espeak_VOICE_get_score_0=A.asm.K).apply(null,arguments)},BA=A._emscripten_bind_espeak_VOICE_set_score_1=function(){return(BA=A._emscripten_bind_espeak_VOICE_set_score_1=A.asm.L).apply(null,arguments)},cA=A._emscripten_bind_espeak_VOICE_get_spare_0=function(){return(cA=A._emscripten_bind_espeak_VOICE_get_spare_0=A.asm.M).apply(null,arguments)},QA=A._emscripten_bind_espeak_VOICE_set_spare_1=function(){return(QA=A._emscripten_bind_espeak_VOICE_set_spare_1=A.asm.N).apply(null,arguments)},GA=A._emscripten_bind_espeak_VOICE___destroy___0=function(){return(GA=A._emscripten_bind_espeak_VOICE___destroy___0=A.asm.O).apply(null,arguments)},wA=A._emscripten_bind_espeak_EVENT_get_type_0=function(){return(wA=A._emscripten_bind_espeak_EVENT_get_type_0=A.asm.P).apply(null,arguments)},EA=A._emscripten_bind_espeak_EVENT_get_unique_identifier_0=function(){return(EA=A._emscripten_bind_espeak_EVENT_get_unique_identifier_0=A.asm.Q).apply(null,arguments)},DA=A._emscripten_bind_espeak_EVENT_get_text_position_0=function(){return(DA=A._emscripten_bind_espeak_EVENT_get_text_position_0=A.asm.R).apply(null,arguments)},uA=A._emscripten_bind_espeak_EVENT_get_length_0=function(){return(uA=A._emscripten_bind_espeak_EVENT_get_length_0=A.asm.S).apply(null,arguments)},lA=A._emscripten_bind_espeak_EVENT_get_audio_position_0=function(){return(lA=A._emscripten_bind_espeak_EVENT_get_audio_position_0=A.asm.T).apply(null,arguments)},xA=A._emscripten_bind_espeak_EVENT_get_sample_0=function(){return(xA=A._emscripten_bind_espeak_EVENT_get_sample_0=A.asm.U).apply(null,arguments)},dA=A._emscripten_bind_espeak_EVENT_get_user_data_0=function(){return(dA=A._emscripten_bind_espeak_EVENT_get_user_data_0=A.asm.V).apply(null,arguments)},mA=A._emscripten_bind_espeak_EVENT___destroy___0=function(){return(mA=A._emscripten_bind_espeak_EVENT___destroy___0=A.asm.W).apply(null,arguments)},MA=A._emscripten_bind_eSpeakNGWorker_eSpeakNGWorker_0=function(){return(MA=A._emscripten_bind_eSpeakNGWorker_eSpeakNGWorker_0=A.asm.X).apply(null,arguments)},vA=A._emscripten_bind_eSpeakNGWorker_synth__2=function(){return(vA=A._emscripten_bind_eSpeakNGWorker_synth__2=A.asm.Y).apply(null,arguments)},hA=A._emscripten_bind_eSpeakNGWorker_synth_ipa__2=function(){return(hA=A._emscripten_bind_eSpeakNGWorker_synth_ipa__2=A.asm.Z).apply(null,arguments)},pA=A._emscripten_bind_eSpeakNGWorker_getSizeOfEventStruct__0=function(){return(pA=A._emscripten_bind_eSpeakNGWorker_getSizeOfEventStruct__0=A.asm._).apply(null,arguments)},YA=A._emscripten_bind_eSpeakNGWorker_set_voice_2=function(){return(YA=A._emscripten_bind_eSpeakNGWorker_set_voice_2=A.asm.$).apply(null,arguments)},HA=A._emscripten_bind_eSpeakNGWorker_set_voice_3=function(){return(HA=A._emscripten_bind_eSpeakNGWorker_set_voice_3=A.asm.aa).apply(null,arguments)},NA=A._emscripten_bind_eSpeakNGWorker_set_voice_4=function(){return(NA=A._emscripten_bind_eSpeakNGWorker_set_voice_4=A.asm.ba).apply(null,arguments)},PA=A._emscripten_bind_eSpeakNGWorker_set_voice_5=function(){return(PA=A._emscripten_bind_eSpeakNGWorker_set_voice_5=A.asm.ca).apply(null,arguments)},FA=A._emscripten_bind_eSpeakNGWorker_get_voices_1=function(){return(FA=A._emscripten_bind_eSpeakNGWorker_get_voices_1=A.asm.da).apply(null,arguments)},yA=A._emscripten_bind_eSpeakNGWorker_set_voices_2=function(){return(yA=A._emscripten_bind_eSpeakNGWorker_set_voices_2=A.asm.ea).apply(null,arguments)},zA=A._emscripten_bind_eSpeakNGWorker_get_samplerate_0=function(){return(zA=A._emscripten_bind_eSpeakNGWorker_get_samplerate_0=A.asm.fa).apply(null,arguments)},OA=A._emscripten_bind_eSpeakNGWorker_get_rate_0=function(){return(OA=A._emscripten_bind_eSpeakNGWorker_get_rate_0=A.asm.ga).apply(null,arguments)},ZA=A._emscripten_bind_eSpeakNGWorker_set_rate_1=function(){return(ZA=A._emscripten_bind_eSpeakNGWorker_set_rate_1=A.asm.ha).apply(null,arguments)},KA=A._emscripten_bind_eSpeakNGWorker_get_pitch_0=function(){return(KA=A._emscripten_bind_eSpeakNGWorker_get_pitch_0=A.asm.ia).apply(null,arguments)},WA=A._emscripten_bind_eSpeakNGWorker_set_pitch_1=function(){return(WA=A._emscripten_bind_eSpeakNGWorker_set_pitch_1=A.asm.ja).apply(null,arguments)},XA=A._emscripten_bind_eSpeakNGWorker___destroy___0=function(){return(XA=A._emscripten_bind_eSpeakNGWorker___destroy___0=A.asm.ka).apply(null,arguments)},LA=A._emscripten_enum_espeak_EVENT_TYPE_espeakEVENT_LIST_TERMINATED=function(){return(LA=A._emscripten_enum_espeak_EVENT_TYPE_espeakEVENT_LIST_TERMINATED=A.asm.la).apply(null,arguments)},TA=A._emscripten_enum_espeak_EVENT_TYPE_espeakEVENT_WORD=function(){return(TA=A._emscripten_enum_espeak_EVENT_TYPE_espeakEVENT_WORD=A.asm.ma).apply(null,arguments)},VA=A._emscripten_enum_espeak_EVENT_TYPE_espeakEVENT_SENTENCE=function(){return(VA=A._emscripten_enum_espeak_EVENT_TYPE_espeakEVENT_SENTENCE=A.asm.na).apply(null,arguments)},JA=A._emscripten_enum_espeak_EVENT_TYPE_espeakEVENT_MARK=function(){return(JA=A._emscripten_enum_espeak_EVENT_TYPE_espeakEVENT_MARK=A.asm.oa).apply(null,arguments)},RA=A._emscripten_enum_espeak_EVENT_TYPE_espeakEVENT_PLAY=function(){return(RA=A._emscripten_enum_espeak_EVENT_TYPE_espeakEVENT_PLAY=A.asm.pa).apply(null,arguments)},UA=A._emscripten_enum_espeak_EVENT_TYPE_espeakEVENT_END=function(){return(UA=A._emscripten_enum_espeak_EVENT_TYPE_espeakEVENT_END=A.asm.qa).apply(null,arguments)},jA=A._emscripten_enum_espeak_EVENT_TYPE_espeakEVENT_MSG_TERMINATED=function(){return(jA=A._emscripten_enum_espeak_EVENT_TYPE_espeakEVENT_MSG_TERMINATED=A.asm.ra).apply(null,arguments)},SA=A._emscripten_enum_espeak_EVENT_TYPE_espeakEVENT_PHONEME=function(){return(SA=A._emscripten_enum_espeak_EVENT_TYPE_espeakEVENT_PHONEME=A.asm.sa).apply(null,arguments)},qA=A._emscripten_enum_espeak_EVENT_TYPE_espeakEVENT_SAMPLERATE=function(){return(qA=A._emscripten_enum_espeak_EVENT_TYPE_espeakEVENT_SAMPLERATE=A.asm.ta).apply(null,arguments)},_A=A.___errno_location=function(){return(_A=A.___errno_location=A.asm.va).apply(null,arguments)};A._free=function(){return(A._free=A.asm.wa).apply(null,arguments)},A._malloc=function(){return(A._malloc=A.asm.xa).apply(null,arguments)};var $A,Ae=A.___cxa_is_pointer_type=function(){return(Ae=A.___cxa_is_pointer_type=A.asm.ya).apply(null,arguments)};function ee(e){function g(){$A||($A=!0,A.calledRun=!0,s||(h=!0,A.noFSInit||U.init.initialized||U.init(),U.ignorePermissions=!1,Z(M),A.onRuntimeInitialized&&A.onRuntimeInitialized(),function(){if(A.postRun)for("function"==typeof A.postRun&&(A.postRun=[A.postRun]);A.postRun.length;)e=A.postRun.shift(),v.unshift(e);var e;Z(v)}()))}N>0||(!function(){if(A.preRun)for("function"==typeof A.preRun&&(A.preRun=[A.preRun]);A.preRun.length;)e=A.preRun.shift(),m.unshift(e);var e;Z(m)}(),N>0||(A.setStatus?(A.setStatus("Running..."),setTimeout((function(){setTimeout((function(){A.setStatus("")}),1),g()}),1)):g()))}if(A.___start_em_js=132724,A.___stop_em_js=132822,A.addRunDependency=F,A.removeRunDependency=y,A.FS_createPath=U.createPath,A.FS_createDataFile=U.createDataFile,A.FS_createLazyFile=U.createLazyFile,A.FS_createDevice=U.createDevice,A.FS_unlink=U.unlink,P=function A(){$A||ee(),$A||(P=A)},A.preInit)for("function"==typeof A.preInit&&(A.preInit=[A.preInit]);A.preInit.length>0;)A.preInit.pop()();function ge(){}function re(A){return(A||ge).__cache__}function Ce(A,e){var g=re(e),r=g[A];return r||((r=Object.create((e||ge).prototype)).ptr=A,g[A]=r)}ee(),ge.prototype=Object.create(ge.prototype),ge.prototype.constructor=ge,ge.prototype.__class__=ge,ge.__cache__={},A.WrapperObject=ge,A.getCache=re,A.wrapPointer=Ce,A.castObject=function(A,e){return Ce(A.ptr,e)},A.NULL=Ce(0),A.destroy=function(A){if(!A.__destroy__)throw"Error: Cannot destroy object. (Did you create it yourself?)";A.__destroy__(),delete re(A.__class__)[A.ptr]},A.compare=function(A,e){return A.ptr===e.ptr},A.getPointer=function(A){return A.ptr},A.getClass=function(A){return A.__class__};var ae={buffer:0,size:0,pos:0,temps:[],needed:0,prepare:function(){if(ae.needed){for(var e=0;e=ae.size?(t(a>0),ae.needed+=a,r=A._malloc(a),ae.temps.push(r)):(r=ae.buffer+ae.pos,ae.pos+=a),r},copy:function(A,e,g){switch(g>>>=0,e.BYTES_PER_ELEMENT){case 2:g>>>=1;break;case 4:g>>>=2;break;case 8:g>>>=3}for(var r=0;r{A.calledRun?e(new A.eSpeakNGWorker):A.onRuntimeInitialized=()=>e(new A.eSpeakNGWorker)})),ke=["en"],oe=ne.then((A=>{const e=A.list_voices().map((({name:A,identifier:e,languages:g})=>({name:A,identifier:e,languages:g.filter((A=>ke.includes(A.name.split("-")[0])))}))).filter((A=>A.languages.length>0)),g=new Set;for(const A of e){g.add(A.identifier);for(const e of A.languages)g.add(e.name)}return{voices:e,identifiers:g}})),Be=async A=>{const{voices:e}=await oe;if(!A)return e;const g=A.split("-")[0];return e.filter((A=>A.languages.some((A=>A.name===g||A.name.startsWith(g+"-")))))},ce=async(A,e="en-us")=>{const g=await ne,{identifiers:r}=await oe;if(!r.has(e))throw new Error(`Invalid language identifier: "${e}". Should be one of: ${Array.from(r).toSorted().join(", ")}.`);return g.set_voice(e),g.synthesize_ipa(A).ipa?.split("\n").filter((A=>A.length>0))??[]};export{Be as list_voices,ce as phonemize}; diff --git a/_shared/voice/vendor/kokoro/stub.js b/_shared/voice/vendor/kokoro/stub.js new file mode 100644 index 0000000000000000000000000000000000000000..86c865a4d785155dd57488be98bd2825b789b85b --- /dev/null +++ b/_shared/voice/vendor/kokoro/stub.js @@ -0,0 +1,6 @@ +// browser stub for node built-ins kokoro-js imports but doesn't use client-side. +export const join = (...a) => a.join('/'); +export const resolve = (...a) => a.join('/'); +export const dirname = (p) => String(p).replace(/\/[^/]*$/, ''); +export const readFile = async () => { throw new Error('fs unavailable in browser'); }; +export default {}; diff --git a/_shared/voice/vendor/kokoro/transformers/ort-wasm-simd-threaded.jsep.mjs b/_shared/voice/vendor/kokoro/transformers/ort-wasm-simd-threaded.jsep.mjs new file mode 100644 index 0000000000000000000000000000000000000000..fd68c6339d617918f1d3113c5757301312971353 --- /dev/null +++ b/_shared/voice/vendor/kokoro/transformers/ort-wasm-simd-threaded.jsep.mjs @@ -0,0 +1,125 @@ +var ortWasmThreaded = (() => { + var _scriptName = import.meta.url; + + return ( +async function(moduleArg = {}) { + var moduleRtn; + +var e=moduleArg,aa,ca,da=new Promise((a,b)=>{aa=a;ca=b}),ea="object"==typeof window,k="undefined"!=typeof WorkerGlobalScope,n="object"==typeof process&&"object"==typeof process.versions&&"string"==typeof process.versions.node&&"renderer"!=process.type,q=k&&self.name?.startsWith("em-pthread");if(n){const {createRequire:a}=await import("module");var require=a(import.meta.url),fa=require("worker_threads");global.Worker=fa.Worker;q=(k=!fa.oc)&&"em-pthread"==fa.workerData} +e.mountExternalData=(a,b)=>{a.startsWith("./")&&(a=a.substring(2));(e.Eb||(e.Eb=new Map)).set(a,b)};e.unmountExternalData=()=>{delete e.Eb};var SharedArrayBuffer=globalThis.SharedArrayBuffer??(new WebAssembly.Memory({initial:0,maximum:0,pc:!0})).buffer.constructor; +const ha=a=>async(...b)=>{try{if(e.Fb)throw Error("Session already started");const c=e.Fb={dc:b[0],errors:[]},d=await a(...b);if(e.Fb!==c)throw Error("Session mismatch");e.Jb?.flush();const f=c.errors;if(0h);if(0{if("webgpu"===a){[e.Jb,e.Ub,e.Yb,e.Kb,e.Xb,e.jb,e.Zb,e.ac,e.Vb,e.Wb,e.$b]=b;const c=e.Jb;e.jsepRegisterBuffer=(d,f,g,h)=>c.registerBuffer(d,f,g,h);e.jsepGetBuffer=d=>c.getBuffer(d);e.jsepCreateDownloader=(d,f,g)=>c.createDownloader(d,f,g);e.jsepOnCreateSession=d=>{c.onCreateSession(d)};e.jsepOnReleaseSession=d=>{c.onReleaseSession(d)};e.jsepOnRunStart=d=>c.onRunStart(d);e.bc=(d,f)=>{c.upload(d,f)}}else if("webnn"===a){const c=b[0];[e.nc,e.Nb,e.webnnEnsureTensor,e.Ob,e.webnnDownloadTensor]= +b.slice(1);e.webnnReleaseTensorId=e.Nb;e.webnnUploadTensor=e.Ob;e.webnnOnRunStart=d=>c.onRunStart(d);e.webnnOnRunEnd=c.onRunEnd.bind(c);e.webnnRegisterMLContext=(d,f)=>{c.registerMLContext(d,f)};e.webnnOnReleaseSession=d=>{c.onReleaseSession(d)};e.webnnCreateMLTensorDownloader=(d,f)=>c.createMLTensorDownloader(d,f);e.webnnRegisterMLTensor=(d,f,g,h)=>c.registerMLTensor(d,f,g,h);e.webnnCreateMLContext=d=>c.createMLContext(d);e.webnnRegisterMLConstant=(d,f,g,h,l,m)=>c.registerMLConstant(d,f,g,h,l,e.Eb, +m);e.webnnRegisterGraphInput=c.registerGraphInput.bind(c);e.webnnIsGraphInput=c.isGraphInput.bind(c);e.webnnCreateTemporaryTensor=c.createTemporaryTensor.bind(c);e.webnnIsInt64Supported=c.isInt64Supported.bind(c)}}; +let ja=()=>{const a=(b,c,d)=>(...f)=>{const g=t,h=c?.();f=b(...f);const l=c?.();h!==l&&(b=l,d(h),c=d=null);return t!=g?ia():f};(b=>{for(const c of b)e[c]=a(e[c],()=>e[c],d=>e[c]=d)})(["_OrtAppendExecutionProvider","_OrtCreateSession","_OrtRun","_OrtRunWithBinding","_OrtBindInput"]);"undefined"!==typeof ha&&(e._OrtRun=ha(e._OrtRun),e._OrtRunWithBinding=ha(e._OrtRunWithBinding));ja=void 0};e.asyncInit=()=>{ja?.()};var ka=Object.assign({},e),la="./this.program",ma=(a,b)=>{throw b;},v="",na,oa; +if(n){var fs=require("fs"),pa=require("path");import.meta.url.startsWith("data:")||(v=pa.dirname(require("url").fileURLToPath(import.meta.url))+"/");oa=a=>{a=qa(a)?new URL(a):a;return fs.readFileSync(a)};na=async a=>{a=qa(a)?new URL(a):a;return fs.readFileSync(a,void 0)};!e.thisProgram&&1{process.exitCode=a;throw b;}}else if(ea||k)k?v=self.location.href:"undefined"!=typeof document&& +document.currentScript&&(v=document.currentScript.src),_scriptName&&(v=_scriptName),v.startsWith("blob:")?v="":v=v.slice(0,v.replace(/[?#].*/,"").lastIndexOf("/")+1),n||(k&&(oa=a=>{var b=new XMLHttpRequest;b.open("GET",a,!1);b.responseType="arraybuffer";b.send(null);return new Uint8Array(b.response)}),na=async a=>{if(qa(a))return new Promise((c,d)=>{var f=new XMLHttpRequest;f.open("GET",a,!0);f.responseType="arraybuffer";f.onload=()=>{200==f.status||0==f.status&&f.response?c(f.response):d(f.status)}; +f.onerror=d;f.send(null)});var b=await fetch(a,{credentials:"same-origin"});if(b.ok)return b.arrayBuffer();throw Error(b.status+" : "+b.url);});var ra=console.log.bind(console),sa=console.error.bind(console);n&&(ra=(...a)=>fs.writeSync(1,a.join(" ")+"\n"),sa=(...a)=>fs.writeSync(2,a.join(" ")+"\n"));var ta=ra,x=sa;Object.assign(e,ka);ka=null;var ua=e.wasmBinary,z,va,A=!1,wa,B,xa,ya,za,Aa,Ba,Ca,C,Da,Ea,qa=a=>a.startsWith("file://");function D(){z.buffer!=B.buffer&&E();return B} +function F(){z.buffer!=B.buffer&&E();return xa}function G(){z.buffer!=B.buffer&&E();return ya}function Fa(){z.buffer!=B.buffer&&E();return za}function H(){z.buffer!=B.buffer&&E();return Aa}function I(){z.buffer!=B.buffer&&E();return Ba}function Ga(){z.buffer!=B.buffer&&E();return Ca}function J(){z.buffer!=B.buffer&&E();return Ea} +if(q){var Ha;if(n){var Ia=fa.parentPort;Ia.on("message",b=>onmessage({data:b}));Object.assign(globalThis,{self:global,postMessage:b=>Ia.postMessage(b)})}var Ja=!1;x=function(...b){b=b.join(" ");n?fs.writeSync(2,b+"\n"):console.error(b)};self.alert=function(...b){postMessage({Bb:"alert",text:b.join(" "),ic:Ka()})};self.onunhandledrejection=b=>{throw b.reason||b;};function a(b){try{var c=b.data,d=c.Bb;if("load"===d){let f=[];self.onmessage=g=>f.push(g);self.startWorker=()=>{postMessage({Bb:"loaded"}); +for(let g of f)a(g);self.onmessage=a};for(const g of c.Rb)if(!e[g]||e[g].proxy)e[g]=(...h)=>{postMessage({Bb:"callHandler",Qb:g,args:h})},"print"==g&&(ta=e[g]),"printErr"==g&&(x=e[g]);z=c.kc;E();Ha(c.lc)}else if("run"===d){La(c.Ab);Ma(c.Ab,0,0,1,0,0);Na();Oa(c.Ab);Ja||(Pa(),Ja=!0);try{Qa(c.fc,c.Hb)}catch(f){if("unwind"!=f)throw f;}}else"setimmediate"!==c.target&&("checkMailbox"===d?Ja&&Ra():d&&(x(`worker: received unknown command ${d}`),x(c)))}catch(f){throw Sa(),f;}}self.onmessage=a} +function E(){var a=z.buffer;e.HEAP8=B=new Int8Array(a);e.HEAP16=ya=new Int16Array(a);e.HEAPU8=xa=new Uint8Array(a);e.HEAPU16=za=new Uint16Array(a);e.HEAP32=Aa=new Int32Array(a);e.HEAPU32=Ba=new Uint32Array(a);e.HEAPF32=Ca=new Float32Array(a);e.HEAPF64=Ea=new Float64Array(a);e.HEAP64=C=new BigInt64Array(a);e.HEAPU64=Da=new BigUint64Array(a)}q||(z=new WebAssembly.Memory({initial:256,maximum:65536,shared:!0}),E());function Ta(){q?startWorker(e):K.Ca()}var Ua=0,Va=null; +function Wa(){Ua--;if(0==Ua&&Va){var a=Va;Va=null;a()}}function L(a){a="Aborted("+a+")";x(a);A=!0;a=new WebAssembly.RuntimeError(a+". Build with -sASSERTIONS for more info.");ca(a);throw a;}var Xa;async function Ya(a){if(!ua)try{var b=await na(a);return new Uint8Array(b)}catch{}if(a==Xa&&ua)a=new Uint8Array(ua);else if(oa)a=oa(a);else throw"both async and sync fetching of the wasm failed";return a} +async function Za(a,b){try{var c=await Ya(a);return await WebAssembly.instantiate(c,b)}catch(d){x(`failed to asynchronously prepare wasm: ${d}`),L(d)}}async function $a(a){var b=Xa;if(!ua&&"function"==typeof WebAssembly.instantiateStreaming&&!qa(b)&&!n)try{var c=fetch(b,{credentials:"same-origin"});return await WebAssembly.instantiateStreaming(c,a)}catch(d){x(`wasm streaming compile failed: ${d}`),x("falling back to ArrayBuffer instantiation")}return Za(b,a)} +function ab(){bb={L:cb,Aa:db,b:eb,$:fb,A:gb,pa:hb,X:ib,Z:jb,qa:kb,na:lb,ga:mb,ma:nb,J:ob,Y:pb,V:qb,oa:rb,W:sb,va:tb,E:ub,Q:vb,O:wb,D:xb,u:yb,r:zb,P:Ab,z:Bb,R:Cb,ja:Db,T:Eb,aa:Fb,M:Gb,F:Hb,ia:Oa,sa:Ib,t:Jb,Ba:Kb,w:Lb,o:Mb,l:Nb,c:Ob,n:Pb,j:Qb,v:Rb,p:Sb,f:Tb,s:Ub,m:Vb,e:Wb,k:Xb,i:Yb,g:Zb,d:$b,da:ac,ea:bc,fa:cc,ba:dc,ca:ec,N:fc,xa:gc,ua:hc,h:ic,C:jc,G:kc,ta:lc,x:mc,ra:nc,U:oc,q:pc,y:qc,K:rc,S:sc,za:tc,ya:uc,ka:vc,la:wc,_:xc,B:yc,I:zc,ha:Ac,H:Bc,a:z,wa:Cc};return{a:bb}} +var Dc={829644:(a,b,c,d,f)=>{if("undefined"==typeof e||!e.Eb)return 1;a=M(Number(a>>>0));a.startsWith("./")&&(a=a.substring(2));a=e.Eb.get(a);if(!a)return 2;b=Number(b>>>0);c=Number(c>>>0);d=Number(d>>>0);if(b+c>a.byteLength)return 3;try{const g=a.subarray(b,b+c);switch(f){case 0:F().set(g,d>>>0);break;case 1:e.mc?e.mc(d,g):e.bc(d,g);break;default:return 4}return 0}catch{return 4}},830468:(a,b,c)=>{e.Ob(a,F().subarray(b>>>0,b+c>>>0))},830532:()=>e.nc(),830574:a=>{e.Nb(a)},830611:()=>{e.Vb()},830642:()=> +{e.Wb()},830671:()=>{e.$b()},830696:a=>e.Ub(a),830729:a=>e.Yb(a),830761:(a,b,c)=>{e.Kb(Number(a),Number(b),Number(c),!0)},830824:(a,b,c)=>{e.Kb(Number(a),Number(b),Number(c))},830881:()=>"undefined"!==typeof wasmOffsetConverter,830938:a=>{e.jb("Abs",a,void 0)},830989:a=>{e.jb("Neg",a,void 0)},831040:a=>{e.jb("Floor",a,void 0)},831093:a=>{e.jb("Ceil",a,void 0)},831145:a=>{e.jb("Reciprocal",a,void 0)},831203:a=>{e.jb("Sqrt",a,void 0)},831255:a=>{e.jb("Exp",a,void 0)},831306:a=>{e.jb("Erf",a,void 0)}, +831357:a=>{e.jb("Sigmoid",a,void 0)},831412:(a,b,c)=>{e.jb("HardSigmoid",a,{alpha:b,beta:c})},831491:a=>{e.jb("Log",a,void 0)},831542:a=>{e.jb("Sin",a,void 0)},831593:a=>{e.jb("Cos",a,void 0)},831644:a=>{e.jb("Tan",a,void 0)},831695:a=>{e.jb("Asin",a,void 0)},831747:a=>{e.jb("Acos",a,void 0)},831799:a=>{e.jb("Atan",a,void 0)},831851:a=>{e.jb("Sinh",a,void 0)},831903:a=>{e.jb("Cosh",a,void 0)},831955:a=>{e.jb("Asinh",a,void 0)},832008:a=>{e.jb("Acosh",a,void 0)},832061:a=>{e.jb("Atanh",a,void 0)}, +832114:a=>{e.jb("Tanh",a,void 0)},832166:a=>{e.jb("Not",a,void 0)},832217:(a,b,c)=>{e.jb("Clip",a,{min:b,max:c})},832286:a=>{e.jb("Clip",a,void 0)},832338:(a,b)=>{e.jb("Elu",a,{alpha:b})},832396:a=>{e.jb("Gelu",a,void 0)},832448:a=>{e.jb("Relu",a,void 0)},832500:(a,b)=>{e.jb("LeakyRelu",a,{alpha:b})},832564:(a,b)=>{e.jb("ThresholdedRelu",a,{alpha:b})},832634:(a,b)=>{e.jb("Cast",a,{to:b})},832692:a=>{e.jb("Add",a,void 0)},832743:a=>{e.jb("Sub",a,void 0)},832794:a=>{e.jb("Mul",a,void 0)},832845:a=> +{e.jb("Div",a,void 0)},832896:a=>{e.jb("Pow",a,void 0)},832947:a=>{e.jb("Equal",a,void 0)},833E3:a=>{e.jb("Greater",a,void 0)},833055:a=>{e.jb("GreaterOrEqual",a,void 0)},833117:a=>{e.jb("Less",a,void 0)},833169:a=>{e.jb("LessOrEqual",a,void 0)},833228:(a,b,c,d,f)=>{e.jb("ReduceMean",a,{keepDims:!!b,noopWithEmptyAxes:!!c,axes:d?Array.from(H().subarray(Number(d)>>>0,Number(f)>>>0)):[]})},833403:(a,b,c,d,f)=>{e.jb("ReduceMax",a,{keepDims:!!b,noopWithEmptyAxes:!!c,axes:d?Array.from(H().subarray(Number(d)>>> +0,Number(f)>>>0)):[]})},833577:(a,b,c,d,f)=>{e.jb("ReduceMin",a,{keepDims:!!b,noopWithEmptyAxes:!!c,axes:d?Array.from(H().subarray(Number(d)>>>0,Number(f)>>>0)):[]})},833751:(a,b,c,d,f)=>{e.jb("ReduceProd",a,{keepDims:!!b,noopWithEmptyAxes:!!c,axes:d?Array.from(H().subarray(Number(d)>>>0,Number(f)>>>0)):[]})},833926:(a,b,c,d,f)=>{e.jb("ReduceSum",a,{keepDims:!!b,noopWithEmptyAxes:!!c,axes:d?Array.from(H().subarray(Number(d)>>>0,Number(f)>>>0)):[]})},834100:(a,b,c,d,f)=>{e.jb("ReduceL1",a,{keepDims:!!b, +noopWithEmptyAxes:!!c,axes:d?Array.from(H().subarray(Number(d)>>>0,Number(f)>>>0)):[]})},834273:(a,b,c,d,f)=>{e.jb("ReduceL2",a,{keepDims:!!b,noopWithEmptyAxes:!!c,axes:d?Array.from(H().subarray(Number(d)>>>0,Number(f)>>>0)):[]})},834446:(a,b,c,d,f)=>{e.jb("ReduceLogSum",a,{keepDims:!!b,noopWithEmptyAxes:!!c,axes:d?Array.from(H().subarray(Number(d)>>>0,Number(f)>>>0)):[]})},834623:(a,b,c,d,f)=>{e.jb("ReduceSumSquare",a,{keepDims:!!b,noopWithEmptyAxes:!!c,axes:d?Array.from(H().subarray(Number(d)>>> +0,Number(f)>>>0)):[]})},834803:(a,b,c,d,f)=>{e.jb("ReduceLogSumExp",a,{keepDims:!!b,noopWithEmptyAxes:!!c,axes:d?Array.from(H().subarray(Number(d)>>>0,Number(f)>>>0)):[]})},834983:a=>{e.jb("Where",a,void 0)},835036:(a,b,c)=>{e.jb("Transpose",a,{perm:b?Array.from(H().subarray(Number(b)>>>0,Number(c)>>>0)):[]})},835160:(a,b,c,d)=>{e.jb("DepthToSpace",a,{blocksize:b,mode:M(c),format:d?"NHWC":"NCHW"})},835293:(a,b,c,d)=>{e.jb("DepthToSpace",a,{blocksize:b,mode:M(c),format:d?"NHWC":"NCHW"})},835426:(a, +b,c,d,f,g,h,l,m,p,r,u,w,y,ba)=>{e.jb("ConvTranspose",a,{format:m?"NHWC":"NCHW",autoPad:b,dilations:[c],group:d,kernelShape:[f],pads:[g,h],strides:[l],wIsConst:()=>!!D()[p>>>0],outputPadding:r?Array.from(H().subarray(Number(r)>>>0,Number(u)>>>0)):[],outputShape:w?Array.from(H().subarray(Number(w)>>>0,Number(y)>>>0)):[],activation:M(ba)})},835859:(a,b,c,d,f,g,h,l,m,p,r,u,w,y)=>{e.jb("ConvTranspose",a,{format:l?"NHWC":"NCHW",autoPad:b,dilations:Array.from(H().subarray(Number(c)>>>0,(Number(c)>>>0)+2>>> +0)),group:d,kernelShape:Array.from(H().subarray(Number(f)>>>0,(Number(f)>>>0)+2>>>0)),pads:Array.from(H().subarray(Number(g)>>>0,(Number(g)>>>0)+4>>>0)),strides:Array.from(H().subarray(Number(h)>>>0,(Number(h)>>>0)+2>>>0)),wIsConst:()=>!!D()[m>>>0],outputPadding:p?Array.from(H().subarray(Number(p)>>>0,Number(r)>>>0)):[],outputShape:u?Array.from(H().subarray(Number(u)>>>0,Number(w)>>>0)):[],activation:M(y)})},836520:(a,b,c,d,f,g,h,l,m,p,r,u,w,y,ba)=>{e.jb("ConvTranspose",a,{format:m?"NHWC":"NCHW", +autoPad:b,dilations:[c],group:d,kernelShape:[f],pads:[g,h],strides:[l],wIsConst:()=>!!D()[p>>>0],outputPadding:r?Array.from(H().subarray(Number(r)>>>0,Number(u)>>>0)):[],outputShape:w?Array.from(H().subarray(Number(w)>>>0,Number(y)>>>0)):[],activation:M(ba)})},836953:(a,b,c,d,f,g,h,l,m,p,r,u,w,y)=>{e.jb("ConvTranspose",a,{format:l?"NHWC":"NCHW",autoPad:b,dilations:Array.from(H().subarray(Number(c)>>>0,(Number(c)>>>0)+2>>>0)),group:d,kernelShape:Array.from(H().subarray(Number(f)>>>0,(Number(f)>>>0)+ +2>>>0)),pads:Array.from(H().subarray(Number(g)>>>0,(Number(g)>>>0)+4>>>0)),strides:Array.from(H().subarray(Number(h)>>>0,(Number(h)>>>0)+2>>>0)),wIsConst:()=>!!D()[m>>>0],outputPadding:p?Array.from(H().subarray(Number(p)>>>0,Number(r)>>>0)):[],outputShape:u?Array.from(H().subarray(Number(u)>>>0,Number(w)>>>0)):[],activation:M(y)})},837614:(a,b)=>{e.jb("GlobalAveragePool",a,{format:b?"NHWC":"NCHW"})},837705:(a,b,c,d,f,g,h,l,m,p,r,u,w,y)=>{e.jb("AveragePool",a,{format:y?"NHWC":"NCHW",auto_pad:b,ceil_mode:c, +count_include_pad:d,storage_order:f,dilations:g?Array.from(H().subarray(Number(g)>>>0,Number(h)>>>0)):[],kernel_shape:l?Array.from(H().subarray(Number(l)>>>0,Number(m)>>>0)):[],pads:p?Array.from(H().subarray(Number(p)>>>0,Number(r)>>>0)):[],strides:u?Array.from(H().subarray(Number(u)>>>0,Number(w)>>>0)):[]})},838184:(a,b)=>{e.jb("GlobalAveragePool",a,{format:b?"NHWC":"NCHW"})},838275:(a,b,c,d,f,g,h,l,m,p,r,u,w,y)=>{e.jb("AveragePool",a,{format:y?"NHWC":"NCHW",auto_pad:b,ceil_mode:c,count_include_pad:d, +storage_order:f,dilations:g?Array.from(H().subarray(Number(g)>>>0,Number(h)>>>0)):[],kernel_shape:l?Array.from(H().subarray(Number(l)>>>0,Number(m)>>>0)):[],pads:p?Array.from(H().subarray(Number(p)>>>0,Number(r)>>>0)):[],strides:u?Array.from(H().subarray(Number(u)>>>0,Number(w)>>>0)):[]})},838754:(a,b)=>{e.jb("GlobalMaxPool",a,{format:b?"NHWC":"NCHW"})},838841:(a,b,c,d,f,g,h,l,m,p,r,u,w,y)=>{e.jb("MaxPool",a,{format:y?"NHWC":"NCHW",auto_pad:b,ceil_mode:c,count_include_pad:d,storage_order:f,dilations:g? +Array.from(H().subarray(Number(g)>>>0,Number(h)>>>0)):[],kernel_shape:l?Array.from(H().subarray(Number(l)>>>0,Number(m)>>>0)):[],pads:p?Array.from(H().subarray(Number(p)>>>0,Number(r)>>>0)):[],strides:u?Array.from(H().subarray(Number(u)>>>0,Number(w)>>>0)):[]})},839316:(a,b)=>{e.jb("GlobalMaxPool",a,{format:b?"NHWC":"NCHW"})},839403:(a,b,c,d,f,g,h,l,m,p,r,u,w,y)=>{e.jb("MaxPool",a,{format:y?"NHWC":"NCHW",auto_pad:b,ceil_mode:c,count_include_pad:d,storage_order:f,dilations:g?Array.from(H().subarray(Number(g)>>> +0,Number(h)>>>0)):[],kernel_shape:l?Array.from(H().subarray(Number(l)>>>0,Number(m)>>>0)):[],pads:p?Array.from(H().subarray(Number(p)>>>0,Number(r)>>>0)):[],strides:u?Array.from(H().subarray(Number(u)>>>0,Number(w)>>>0)):[]})},839878:(a,b,c,d,f)=>{e.jb("Gemm",a,{alpha:b,beta:c,transA:d,transB:f})},839982:a=>{e.jb("MatMul",a,void 0)},840036:(a,b,c,d)=>{e.jb("ArgMax",a,{keepDims:!!b,selectLastIndex:!!c,axis:d})},840144:(a,b,c,d)=>{e.jb("ArgMin",a,{keepDims:!!b,selectLastIndex:!!c,axis:d})},840252:(a, +b)=>{e.jb("Softmax",a,{axis:b})},840315:(a,b)=>{e.jb("Concat",a,{axis:b})},840375:(a,b,c,d,f)=>{e.jb("Split",a,{axis:b,numOutputs:c,splitSizes:d?Array.from(H().subarray(Number(d)>>>0,Number(f)>>>0)):[]})},840531:a=>{e.jb("Expand",a,void 0)},840585:(a,b)=>{e.jb("Gather",a,{axis:Number(b)})},840656:(a,b)=>{e.jb("GatherElements",a,{axis:Number(b)})},840735:(a,b)=>{e.jb("GatherND",a,{batch_dims:Number(b)})},840814:(a,b,c,d,f,g,h,l,m,p,r)=>{e.jb("Resize",a,{antialias:b,axes:c?Array.from(H().subarray(Number(c)>>> +0,Number(d)>>>0)):[],coordinateTransformMode:M(f),cubicCoeffA:g,excludeOutside:h,extrapolationValue:l,keepAspectRatioPolicy:M(m),mode:M(p),nearestMode:M(r)})},841176:(a,b,c,d,f,g,h)=>{e.jb("Slice",a,{starts:b?Array.from(H().subarray(Number(b)>>>0,Number(c)>>>0)):[],ends:d?Array.from(H().subarray(Number(d)>>>0,Number(f)>>>0)):[],axes:g?Array.from(H().subarray(Number(g)>>>0,Number(h)>>>0)):[]})},841440:a=>{e.jb("Tile",a,void 0)},841492:(a,b,c)=>{e.jb("InstanceNormalization",a,{epsilon:b,format:c?"NHWC": +"NCHW"})},841606:(a,b,c)=>{e.jb("InstanceNormalization",a,{epsilon:b,format:c?"NHWC":"NCHW"})},841720:a=>{e.jb("Range",a,void 0)},841773:(a,b)=>{e.jb("Einsum",a,{equation:M(b)})},841854:(a,b,c,d,f)=>{e.jb("Pad",a,{mode:b,value:c,pads:d?Array.from(H().subarray(Number(d)>>>0,Number(f)>>>0)):[]})},841997:(a,b,c,d,f,g)=>{e.jb("BatchNormalization",a,{epsilon:b,momentum:c,spatial:!!f,trainingMode:!!d,format:g?"NHWC":"NCHW"})},842166:(a,b,c,d,f,g)=>{e.jb("BatchNormalization",a,{epsilon:b,momentum:c,spatial:!!f, +trainingMode:!!d,format:g?"NHWC":"NCHW"})},842335:(a,b,c)=>{e.jb("CumSum",a,{exclusive:Number(b),reverse:Number(c)})},842432:(a,b,c)=>{e.jb("DequantizeLinear",a,{axis:b,blockSize:c})},842522:(a,b,c,d,f)=>{e.jb("GridSample",a,{align_corners:b,mode:M(c),padding_mode:M(d),format:f?"NHWC":"NCHW"})},842692:(a,b,c,d,f)=>{e.jb("GridSample",a,{align_corners:b,mode:M(c),padding_mode:M(d),format:f?"NHWC":"NCHW"})},842862:(a,b)=>{e.jb("ScatterND",a,{reduction:M(b)})},842947:(a,b,c,d,f,g,h,l,m)=>{e.jb("Attention", +a,{numHeads:b,isUnidirectional:c,maskFilterValue:d,scale:f,doRotary:g,qkvHiddenSizes:h?Array.from(H().subarray(Number(l)>>>0,Number(l)+h>>>0)):[],pastPresentShareBuffer:!!m})},843219:a=>{e.jb("BiasAdd",a,void 0)},843274:a=>{e.jb("BiasSplitGelu",a,void 0)},843335:a=>{e.jb("FastGelu",a,void 0)},843391:(a,b,c,d,f,g,h,l,m,p,r,u,w,y,ba,Vd)=>{e.jb("Conv",a,{format:u?"NHWC":"NCHW",auto_pad:b,dilations:c?Array.from(H().subarray(Number(c)>>>0,Number(d)>>>0)):[],group:f,kernel_shape:g?Array.from(H().subarray(Number(g)>>> +0,Number(h)>>>0)):[],pads:l?Array.from(H().subarray(Number(l)>>>0,Number(m)>>>0)):[],strides:p?Array.from(H().subarray(Number(p)>>>0,Number(r)>>>0)):[],w_is_const:()=>!!D()[Number(w)>>>0],activation:M(y),activation_params:ba?Array.from(Ga().subarray(Number(ba)>>>0,Number(Vd)>>>0)):[]})},843975:a=>{e.jb("Gelu",a,void 0)},844027:(a,b,c,d,f,g,h,l,m)=>{e.jb("GroupQueryAttention",a,{numHeads:b,kvNumHeads:c,scale:d,softcap:f,doRotary:g,rotaryInterleaved:h,smoothSoftmax:l,localWindowSize:m})},844244:(a, +b,c,d)=>{e.jb("LayerNormalization",a,{axis:b,epsilon:c,simplified:!!d})},844355:(a,b,c,d)=>{e.jb("LayerNormalization",a,{axis:b,epsilon:c,simplified:!!d})},844466:(a,b,c,d,f,g)=>{e.jb("MatMulNBits",a,{k:b,n:c,accuracyLevel:d,bits:f,blockSize:g})},844593:(a,b,c,d,f,g)=>{e.jb("MultiHeadAttention",a,{numHeads:b,isUnidirectional:c,maskFilterValue:d,scale:f,doRotary:g})},844752:(a,b)=>{e.jb("QuickGelu",a,{alpha:b})},844816:(a,b,c,d,f)=>{e.jb("RotaryEmbedding",a,{interleaved:!!b,numHeads:c,rotaryEmbeddingDim:d, +scale:f})},844955:(a,b,c)=>{e.jb("SkipLayerNormalization",a,{epsilon:b,simplified:!!c})},845057:(a,b,c)=>{e.jb("SkipLayerNormalization",a,{epsilon:b,simplified:!!c})},845159:(a,b,c,d)=>{e.jb("GatherBlockQuantized",a,{gatherAxis:b,quantizeAxis:c,blockSize:d})},845280:a=>{e.Zb(a)},845314:(a,b)=>e.ac(Number(a),Number(b),e.Fb.dc,e.Fb.errors)};function db(a,b,c){return Ec(async()=>{await e.Xb(Number(a),Number(b),Number(c))})}function cb(){return"undefined"!==typeof wasmOffsetConverter} +class Fc{name="ExitStatus";constructor(a){this.message=`Program terminated with exit(${a})`;this.status=a}} +var Gc=a=>{a.terminate();a.onmessage=()=>{}},Hc=[],Lc=a=>{0==N.length&&(Ic(),Jc(N[0]));var b=N.pop();if(!b)return 6;Kc.push(b);O[a.Ab]=b;b.Ab=a.Ab;var c={Bb:"run",fc:a.ec,Hb:a.Hb,Ab:a.Ab};n&&b.unref();b.postMessage(c,a.Mb);return 0},P=0,Q=(a,b,...c)=>{for(var d=2*c.length,f=Mc(),g=Nc(8*d),h=g>>>3,l=0;l>>0]=m)}a=Oc(a,0,d,g,b);Pc(f);return a}; +function Cc(a){if(q)return Q(0,1,a);wa=a;if(!(0{wa=a;if(q)throw Qc(a),"unwind";Cc(a)},N=[],Kc=[],Rc=[],O={};function Sc(){for(var a=e.numThreads-1;a--;)Ic();Hc.unshift(()=>{Ua++;Tc(()=>Wa())})}var Vc=a=>{var b=a.Ab;delete O[b];N.push(a);Kc.splice(Kc.indexOf(a),1);a.Ab=0;Uc(b)};function Na(){Rc.forEach(a=>a())} +var Jc=a=>new Promise(b=>{a.onmessage=g=>{g=g.data;var h=g.Bb;if(g.Gb&&g.Gb!=Ka()){var l=O[g.Gb];l?l.postMessage(g,g.Mb):x(`Internal error! Worker sent a message "${h}" to target pthread ${g.Gb}, but that thread no longer exists!`)}else if("checkMailbox"===h)Ra();else if("spawnThread"===h)Lc(g);else if("cleanupThread"===h)Vc(O[g.hc]);else if("loaded"===h)a.loaded=!0,n&&!a.Ab&&a.unref(),b(a);else if("alert"===h)alert(`Thread ${g.ic}: ${g.text}`);else if("setimmediate"===g.target)a.postMessage(g);else if("callHandler"=== +h)e[g.Qb](...g.args);else h&&x(`worker sent an unknown command ${h}`)};a.onerror=g=>{x(`${"worker sent an error!"} ${g.filename}:${g.lineno}: ${g.message}`);throw g;};n&&(a.on("message",g=>a.onmessage({data:g})),a.on("error",g=>a.onerror(g)));var c=[],d=[],f;for(f of d)e.propertyIsEnumerable(f)&&c.push(f);a.postMessage({Bb:"load",Rb:c,kc:z,lc:va})});function Tc(a){q?a():Promise.all(N.map(Jc)).then(a)} +function Ic(){var a=new Worker(new URL(import.meta.url),{type:"module",workerData:"em-pthread",name:"em-pthread"});N.push(a)}var La=a=>{E();var b=I()[a+52>>>2>>>0];a=I()[a+56>>>2>>>0];Wc(b,b-a);Pc(b)},Qa=(a,b)=>{P=0;a=Xc(a,b);0>>=0;var d=new Zc(a);b>>>=0;c>>>=0;I()[d.Ib+16>>>2>>>0]=0;I()[d.Ib+4>>>2>>>0]=b;I()[d.Ib+8>>>2>>>0]=c;$c=a;ad++;throw $c;} +function bd(a,b,c,d){return q?Q(2,1,a,b,c,d):fb(a,b,c,d)}function fb(a,b,c,d){a>>>=0;b>>>=0;c>>>=0;d>>>=0;if("undefined"==typeof SharedArrayBuffer)return 6;var f=[];if(q&&0===f.length)return bd(a,b,c,d);a={ec:c,Ab:a,Hb:d,Mb:f};return q?(a.Bb="spawnThread",postMessage(a,f),0):Lc(a)} +var cd="undefined"!=typeof TextDecoder?new TextDecoder:void 0,dd=(a,b=0,c=NaN)=>{b>>>=0;var d=b+c;for(c=b;a[c]&&!(c>=d);)++c;if(16f?d+=String.fromCharCode(f):(f-=65536,d+=String.fromCharCode(55296|f>>10,56320| +f&1023))}}else d+=String.fromCharCode(f)}return d},M=(a,b)=>(a>>>=0)?dd(F(),a,b):"";function gb(a,b,c){return q?Q(3,1,a,b,c):0}function hb(a,b){if(q)return Q(4,1,a,b)} +var ed=a=>{for(var b=0,c=0;c=d?b++:2047>=d?b+=2:55296<=d&&57343>=d?(b+=4,++c):b+=3}return b},fd=(a,b,c)=>{var d=F();b>>>=0;if(0=h){var l=a.charCodeAt(++g);h=65536+((h&1023)<<10)|l&1023}if(127>=h){if(b>=c)break;d[b++>>>0]=h}else{if(2047>=h){if(b+1>=c)break;d[b++>>>0]=192|h>>6}else{if(65535>=h){if(b+2>=c)break;d[b++>>>0]=224|h>>12}else{if(b+3>=c)break;d[b++>>>0]=240|h>>18; +d[b++>>>0]=128|h>>12&63}d[b++>>>0]=128|h>>6&63}d[b++>>>0]=128|h&63}}d[b>>>0]=0;a=b-f}else a=0;return a};function ib(a,b){if(q)return Q(5,1,a,b)}function jb(a,b,c){if(q)return Q(6,1,a,b,c)}function kb(a,b,c){return q?Q(7,1,a,b,c):0}function lb(a,b){if(q)return Q(8,1,a,b)}function mb(a,b,c){if(q)return Q(9,1,a,b,c)}function nb(a,b,c,d){if(q)return Q(10,1,a,b,c,d)}function ob(a,b,c,d){if(q)return Q(11,1,a,b,c,d)}function pb(a,b,c,d){if(q)return Q(12,1,a,b,c,d)}function qb(a){if(q)return Q(13,1,a)} +function rb(a,b){if(q)return Q(14,1,a,b)}function sb(a,b,c){if(q)return Q(15,1,a,b,c)}var tb=()=>L(""),gd,R=a=>{for(var b="";F()[a>>>0];)b+=gd[F()[a++>>>0]];return b},hd={},jd={},kd={},S;function ld(a,b,c={}){var d=b.name;if(!a)throw new S(`type "${d}" must have a positive integer typeid pointer`);if(jd.hasOwnProperty(a)){if(c.Sb)return;throw new S(`Cannot register type '${d}' twice`);}jd[a]=b;delete kd[a];hd.hasOwnProperty(a)&&(b=hd[a],delete hd[a],b.forEach(f=>f()))} +function T(a,b,c={}){return ld(a,b,c)}var md=(a,b,c)=>{switch(b){case 1:return c?d=>D()[d>>>0]:d=>F()[d>>>0];case 2:return c?d=>G()[d>>>1>>>0]:d=>Fa()[d>>>1>>>0];case 4:return c?d=>H()[d>>>2>>>0]:d=>I()[d>>>2>>>0];case 8:return c?d=>C[d>>>3]:d=>Da[d>>>3];default:throw new TypeError(`invalid integer width (${b}): ${a}`);}}; +function ub(a,b,c){a>>>=0;c>>>=0;b=R(b>>>0);T(a,{name:b,fromWireType:d=>d,toWireType:function(d,f){if("bigint"!=typeof f&&"number"!=typeof f)throw null===f?f="null":(d=typeof f,f="object"===d||"array"===d||"function"===d?f.toString():""+f),new TypeError(`Cannot convert "${f}" to ${this.name}`);"number"==typeof f&&(f=BigInt(f));return f},Cb:U,readValueFromPointer:md(b,c,-1==b.indexOf("u")),Db:null})}var U=8; +function vb(a,b,c,d){a>>>=0;b=R(b>>>0);T(a,{name:b,fromWireType:function(f){return!!f},toWireType:function(f,g){return g?c:d},Cb:U,readValueFromPointer:function(f){return this.fromWireType(F()[f>>>0])},Db:null})}var nd=[],V=[];function Ob(a){a>>>=0;9{if(!a)throw new S("Cannot use deleted val. handle = "+a);return V[a]},X=a=>{switch(a){case void 0:return 2;case null:return 4;case !0:return 6;case !1:return 8;default:const b=nd.pop()||V.length;V[b]=a;V[b+1]=1;return b}};function od(a){return this.fromWireType(I()[a>>>2>>>0])}var pd={name:"emscripten::val",fromWireType:a=>{var b=W(a);Ob(a);return b},toWireType:(a,b)=>X(b),Cb:U,readValueFromPointer:od,Db:null};function wb(a){return T(a>>>0,pd)} +var qd=(a,b)=>{switch(b){case 4:return function(c){return this.fromWireType(Ga()[c>>>2>>>0])};case 8:return function(c){return this.fromWireType(J()[c>>>3>>>0])};default:throw new TypeError(`invalid float width (${b}): ${a}`);}};function xb(a,b,c){a>>>=0;c>>>=0;b=R(b>>>0);T(a,{name:b,fromWireType:d=>d,toWireType:(d,f)=>f,Cb:U,readValueFromPointer:qd(b,c),Db:null})} +function yb(a,b,c,d,f){a>>>=0;c>>>=0;b=R(b>>>0);-1===f&&(f=4294967295);f=l=>l;if(0===d){var g=32-8*c;f=l=>l<>>g}var h=b.includes("unsigned")?function(l,m){return m>>>0}:function(l,m){return m};T(a,{name:b,fromWireType:f,toWireType:h,Cb:U,readValueFromPointer:md(b,c,0!==d),Db:null})} +function zb(a,b,c){function d(g){var h=I()[g>>>2>>>0];g=I()[g+4>>>2>>>0];return new f(D().buffer,g,h)}a>>>=0;var f=[Int8Array,Uint8Array,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array,BigInt64Array,BigUint64Array][b];c=R(c>>>0);T(a,{name:c,fromWireType:d,Cb:U,readValueFromPointer:d},{Sb:!0})} +function Ab(a,b){a>>>=0;b=R(b>>>0);T(a,{name:b,fromWireType:function(c){for(var d=I()[c>>>2>>>0],f=c+4,g,h=f,l=0;l<=d;++l){var m=f+l;if(l==d||0==F()[m>>>0])h=M(h,m-h),void 0===g?g=h:(g+=String.fromCharCode(0),g+=h),h=m+1}Y(c);return g},toWireType:function(c,d){d instanceof ArrayBuffer&&(d=new Uint8Array(d));var f="string"==typeof d;if(!(f||d instanceof Uint8Array||d instanceof Uint8ClampedArray||d instanceof Int8Array))throw new S("Cannot pass non-string to std::string");var g=f?ed(d):d.length;var h= +rd(4+g+1),l=h+4;I()[h>>>2>>>0]=g;if(f)fd(d,l,g+1);else if(f)for(f=0;f>>0]=m}else for(f=0;f>>0]=d[f];null!==c&&c.push(Y,h);return h},Cb:U,readValueFromPointer:od,Db(c){Y(c)}})} +var sd="undefined"!=typeof TextDecoder?new TextDecoder("utf-16le"):void 0,td=(a,b)=>{var c=a>>1;for(var d=c+b/2;!(c>=d)&&Fa()[c>>>0];)++c;c<<=1;if(32=b/2);++d){var f=G()[a+2*d>>>1>>>0];if(0==f)break;c+=String.fromCharCode(f)}return c},ud=(a,b,c)=>{c??=2147483647;if(2>c)return 0;c-=2;var d=b;c=c<2*a.length?c/2:a.length;for(var f=0;f>>1>>>0]=g;b+=2}G()[b>>>1>>>0]=0;return b-d},vd=a=>2*a.length,wd=(a,b)=>{for(var c= +0,d="";!(c>=b/4);){var f=H()[a+4*c>>>2>>>0];if(0==f)break;++c;65536<=f?(f-=65536,d+=String.fromCharCode(55296|f>>10,56320|f&1023)):d+=String.fromCharCode(f)}return d},xd=(a,b,c)=>{b>>>=0;c??=2147483647;if(4>c)return 0;var d=b;c=d+c-4;for(var f=0;f=g){var h=a.charCodeAt(++f);g=65536+((g&1023)<<10)|h&1023}H()[b>>>2>>>0]=g;b+=4;if(b+4>c)break}H()[b>>>2>>>0]=0;return b-d},yd=a=>{for(var b=0,c=0;c= +d&&++c;b+=4}return b}; +function Bb(a,b,c){a>>>=0;b>>>=0;c>>>=0;c=R(c);if(2===b){var d=td;var f=ud;var g=vd;var h=l=>Fa()[l>>>1>>>0]}else 4===b&&(d=wd,f=xd,g=yd,h=l=>I()[l>>>2>>>0]);T(a,{name:c,fromWireType:l=>{for(var m=I()[l>>>2>>>0],p,r=l+4,u=0;u<=m;++u){var w=l+4+u*b;if(u==m||0==h(w))r=d(r,w-r),void 0===p?p=r:(p+=String.fromCharCode(0),p+=r),r=w+b}Y(l);return p},toWireType:(l,m)=>{if("string"!=typeof m)throw new S(`Cannot pass non-string to C++ string type ${c}`);var p=g(m),r=rd(4+p+b);I()[r>>>2>>>0]=p/b;f(m,r+4,p+b); +null!==l&&l.push(Y,r);return r},Cb:U,readValueFromPointer:od,Db(l){Y(l)}})}function Cb(a,b){a>>>=0;b=R(b>>>0);T(a,{Tb:!0,name:b,Cb:0,fromWireType:()=>{},toWireType:()=>{}})}function Db(a){Ma(a>>>0,!k,1,!ea,131072,!1);Na()}var zd=a=>{if(!A)try{if(a(),!(0>>=0;"function"===typeof Atomics.jc&&(Atomics.jc(H(),a>>>2,a).value.then(Ra),a+=128,Atomics.store(H(),a>>>2,1))}var Ra=()=>{var a=Ka();a&&(Oa(a),zd(Ad))};function Eb(a,b){a>>>=0;a==b>>>0?setTimeout(Ra):q?postMessage({Gb:a,Bb:"checkMailbox"}):(a=O[a])&&a.postMessage({Bb:"checkMailbox"})}var Bd=[];function Fb(a,b,c,d,f){b>>>=0;d/=2;Bd.length=d;c=f>>>0>>>3;for(f=0;f>>0];return(b?Dc[b]:Cd[a])(...Bd)}var Gb=()=>{P=0}; +function Hb(a){a>>>=0;q?postMessage({Bb:"cleanupThread",hc:a}):Vc(O[a])}function Ib(a){n&&O[a>>>0].ref()}var Ed=(a,b)=>{var c=jd[a];if(void 0===c)throw a=Dd(a),c=R(a),Y(a),new S(`${b} has unknown type ${c}`);return c},Fd=(a,b,c)=>{var d=[];a=a.toWireType(d,c);d.length&&(I()[b>>>2>>>0]=X(d));return a};function Jb(a,b,c){b>>>=0;c>>>=0;a=W(a>>>0);b=Ed(b,"emval::as");return Fd(b,c,a)}function Kb(a,b){b>>>=0;a=W(a>>>0);b=Ed(b,"emval::as");return b.toWireType(null,a)}var Gd=a=>{try{a()}catch(b){L(b)}}; +function Hd(){var a=K,b={};for(let [c,d]of Object.entries(a))b[c]="function"==typeof d?(...f)=>{Id.push(c);try{return d(...f)}finally{A||(Id.pop(),t&&1===Z&&0===Id.length&&(Z=0,P+=1,Gd(Jd),"undefined"!=typeof Fibers&&Fibers.rc()))}}:d;return b}var Z=0,t=null,Kd=0,Id=[],Ld={},Md={},Nd=0,Od=null,Pd=[];function ia(){return new Promise((a,b)=>{Od={resolve:a,reject:b}})} +function Qd(){var a=rd(65548),b=a+12;I()[a>>>2>>>0]=b;I()[a+4>>>2>>>0]=b+65536;b=Id[0];var c=Ld[b];void 0===c&&(c=Nd++,Ld[b]=c,Md[c]=b);b=c;H()[a+8>>>2>>>0]=b;return a}function Rd(){var a=H()[t+8>>>2>>>0];a=K[Md[a]];--P;return a()} +function Sd(a){if(!A){if(0===Z){var b=!1,c=!1;a((d=0)=>{if(!A&&(Kd=d,b=!0,c)){Z=2;Gd(()=>Td(t));"undefined"!=typeof MainLoop&&MainLoop.Pb&&MainLoop.resume();d=!1;try{var f=Rd()}catch(l){f=l,d=!0}var g=!1;if(!t){var h=Od;h&&(Od=null,(d?h.reject:h.resolve)(f),g=!0)}if(d&&!g)throw f;}});c=!0;b||(Z=1,t=Qd(),"undefined"!=typeof MainLoop&&MainLoop.Pb&&MainLoop.pause(),Gd(()=>Ud(t)))}else 2===Z?(Z=0,Gd(Wd),Y(t),t=null,Pd.forEach(zd)):L(`invalid state: ${Z}`);return Kd}} +function Ec(a){return Sd(b=>{a().then(b)})}function Lb(a){a>>>=0;return Ec(async()=>{var b=await W(a);return X(b)})}var Xd=[];function Mb(a,b,c,d){c>>>=0;d>>>=0;a=Xd[a>>>0];b=W(b>>>0);return a(null,b,c,d)}var Yd={},Zd=a=>{var b=Yd[a];return void 0===b?R(a):b};function Nb(a,b,c,d,f){c>>>=0;d>>>=0;f>>>=0;a=Xd[a>>>0];b=W(b>>>0);c=Zd(c);return a(b,b[c],d,f)}var $d=()=>"object"==typeof globalThis?globalThis:Function("return this")(); +function Pb(a){a>>>=0;if(0===a)return X($d());a=Zd(a);return X($d()[a])}var ae=a=>{var b=Xd.length;Xd.push(a);return b},be=(a,b)=>{for(var c=Array(a),d=0;d>>2>>>0],"parameter "+d);return c},ce=(a,b)=>Object.defineProperty(b,"name",{value:a}); +function de(a){var b=Function;if(!(b instanceof Function))throw new TypeError(`new_ called with constructor type ${typeof b} which is not a function`);var c=ce(b.name||"unknownFunctionName",function(){});c.prototype=b.prototype;c=new c;a=b.apply(c,a);return a instanceof Object?a:c} +function Qb(a,b,c){b=be(a,b>>>0);var d=b.shift();a--;var f="return function (obj, func, destructorsRef, args) {\n",g=0,h=[];0===c&&h.push("obj");for(var l=["retType"],m=[d],p=0;pr.name).join(", ")}) => ${d.name}>`;return ae(ce(c,a))}function Rb(a){a=Zd(a>>>0);return X(e[a])}function Sb(a,b){b>>>=0;a=W(a>>>0);b=W(b);return X(a[b])}function Tb(a){a>>>=0;9>>0);for(var b=Array(a.length),c=0;c>>0))}function Xb(){return X({})} +function Yb(a){a>>>=0;for(var b=W(a);b.length;){var c=b.pop();b.pop()(c)}Ob(a)}function Zb(a,b,c){b>>>=0;c>>>=0;a=W(a>>>0);b=W(b);c=W(c);a[b]=c}function $b(a,b){b>>>=0;a=Ed(a>>>0,"_emval_take_value");a=a.readValueFromPointer(b);return X(a)} +function ac(a,b){a=-9007199254740992>a||9007199254740992>>=0;a=new Date(1E3*a);H()[b>>>2>>>0]=a.getUTCSeconds();H()[b+4>>>2>>>0]=a.getUTCMinutes();H()[b+8>>>2>>>0]=a.getUTCHours();H()[b+12>>>2>>>0]=a.getUTCDate();H()[b+16>>>2>>>0]=a.getUTCMonth();H()[b+20>>>2>>>0]=a.getUTCFullYear()-1900;H()[b+24>>>2>>>0]=a.getUTCDay();a=(a.getTime()-Date.UTC(a.getUTCFullYear(),0,1,0,0,0,0))/864E5|0;H()[b+28>>>2>>>0]=a} +var ee=a=>0===a%4&&(0!==a%100||0===a%400),fe=[0,31,60,91,121,152,182,213,244,274,305,335],ge=[0,31,59,90,120,151,181,212,243,273,304,334]; +function bc(a,b){a=-9007199254740992>a||9007199254740992>>=0;a=new Date(1E3*a);H()[b>>>2>>>0]=a.getSeconds();H()[b+4>>>2>>>0]=a.getMinutes();H()[b+8>>>2>>>0]=a.getHours();H()[b+12>>>2>>>0]=a.getDate();H()[b+16>>>2>>>0]=a.getMonth();H()[b+20>>>2>>>0]=a.getFullYear()-1900;H()[b+24>>>2>>>0]=a.getDay();var c=(ee(a.getFullYear())?fe:ge)[a.getMonth()]+a.getDate()-1|0;H()[b+28>>>2>>>0]=c;H()[b+36>>>2>>>0]=-(60*a.getTimezoneOffset());c=(new Date(a.getFullYear(),6,1)).getTimezoneOffset(); +var d=(new Date(a.getFullYear(),0,1)).getTimezoneOffset();a=(c!=d&&a.getTimezoneOffset()==Math.min(d,c))|0;H()[b+32>>>2>>>0]=a} +function cc(a){a>>>=0;var b=new Date(H()[a+20>>>2>>>0]+1900,H()[a+16>>>2>>>0],H()[a+12>>>2>>>0],H()[a+8>>>2>>>0],H()[a+4>>>2>>>0],H()[a>>>2>>>0],0),c=H()[a+32>>>2>>>0],d=b.getTimezoneOffset(),f=(new Date(b.getFullYear(),6,1)).getTimezoneOffset(),g=(new Date(b.getFullYear(),0,1)).getTimezoneOffset(),h=Math.min(g,f);0>c?H()[a+32>>>2>>>0]=Number(f!=g&&h==d):0>>2>>>0]=b.getDay();c=(ee(b.getFullYear())?fe:ge)[b.getMonth()]+ +b.getDate()-1|0;H()[a+28>>>2>>>0]=c;H()[a>>>2>>>0]=b.getSeconds();H()[a+4>>>2>>>0]=b.getMinutes();H()[a+8>>>2>>>0]=b.getHours();H()[a+12>>>2>>>0]=b.getDate();H()[a+16>>>2>>>0]=b.getMonth();H()[a+20>>>2>>>0]=b.getYear();a=b.getTime();return BigInt(isNaN(a)?-1:a/1E3)}function dc(a,b,c,d,f,g,h){return q?Q(16,1,a,b,c,d,f,g,h):-52}function ec(a,b,c,d,f,g){if(q)return Q(17,1,a,b,c,d,f,g)}var he={},pc=()=>performance.timeOrigin+performance.now(); +function fc(a,b){if(q)return Q(18,1,a,b);he[a]&&(clearTimeout(he[a].id),delete he[a]);if(!b)return 0;var c=setTimeout(()=>{delete he[a];zd(()=>ie(a,performance.timeOrigin+performance.now()))},b);he[a]={id:c,qc:b};return 0} +function gc(a,b,c,d){a>>>=0;b>>>=0;c>>>=0;d>>>=0;var f=(new Date).getFullYear(),g=(new Date(f,0,1)).getTimezoneOffset();f=(new Date(f,6,1)).getTimezoneOffset();var h=Math.max(g,f);I()[a>>>2>>>0]=60*h;H()[b>>>2>>>0]=Number(g!=f);b=l=>{var m=Math.abs(l);return`UTC${0<=l?"-":"+"}${String(Math.floor(m/60)).padStart(2,"0")}${String(m%60).padStart(2,"0")}`};a=b(g);b=b(f);fDate.now(),je=1; +function hc(a,b,c){if(!(0<=a&&3>=a))return 28;if(0===a)a=Date.now();else if(je)a=performance.timeOrigin+performance.now();else return 52;C[c>>>0>>>3]=BigInt(Math.round(1E6*a));return 0}var ke=[],le=(a,b)=>{ke.length=0;for(var c;c=F()[a++>>>0];){var d=105!=c;d&=112!=c;b+=d&&b%8?4:0;ke.push(112==c?I()[b>>>2>>>0]:106==c?C[b>>>3]:105==c?H()[b>>>2>>>0]:J()[b>>>3>>>0]);b+=d?8:4}return ke};function ic(a,b,c){a>>>=0;b=le(b>>>0,c>>>0);return Dc[a](...b)} +function jc(a,b,c){a>>>=0;b=le(b>>>0,c>>>0);return Dc[a](...b)}var kc=()=>{};function mc(a,b){return x(M(a>>>0,b>>>0))}var nc=()=>{P+=1;throw"unwind";};function oc(){return 4294901760}var qc=()=>n?require("os").cpus().length:navigator.hardwareConcurrency;function rc(){L("Cannot use emscripten_pc_get_function without -sUSE_OFFSET_CONVERTER");return 0} +function sc(a){a>>>=0;var b=F().length;if(a<=b||4294901760=c;c*=2){var d=b*(1+.2/c);d=Math.min(d,a+100663296);a:{d=(Math.min(4294901760,65536*Math.ceil(Math.max(a,d)/65536))-z.buffer.byteLength+65535)/65536|0;try{z.grow(d);E();var f=1;break a}catch(g){}f=void 0}if(f)return!0}return!1}var me=()=>{L("Cannot use convertFrameToPC (needed by __builtin_return_address) without -sUSE_OFFSET_CONVERTER");return 0},ne={},oe=a=>{a.forEach(b=>{var c=me();c&&(ne[c]=b)})}; +function tc(){var a=Error().stack.toString().split("\n");"Error"==a[0]&&a.shift();oe(a);ne.Lb=me();ne.cc=a;return ne.Lb}function uc(a,b,c){a>>>=0;b>>>=0;if(ne.Lb==a)var d=ne.cc;else d=Error().stack.toString().split("\n"),"Error"==d[0]&&d.shift(),oe(d);for(var f=3;d[f]&&me()!=a;)++f;for(a=0;a>>2>>>0]=me();return a} +var pe={},re=()=>{if(!qe){var a={USER:"web_user",LOGNAME:"web_user",PATH:"/",PWD:"/",HOME:"/home/web_user",LANG:("object"==typeof navigator&&navigator.languages&&navigator.languages[0]||"C").replace("-","_")+".UTF-8",_:la||"./this.program"},b;for(b in pe)void 0===pe[b]?delete a[b]:a[b]=pe[b];var c=[];for(b in a)c.push(`${b}=${a[b]}`);qe=c}return qe},qe; +function vc(a,b){if(q)return Q(19,1,a,b);a>>>=0;b>>>=0;var c=0;re().forEach((d,f)=>{var g=b+c;f=I()[a+4*f>>>2>>>0]=g;for(g=0;g>>0]=d.charCodeAt(g);D()[f>>>0]=0;c+=d.length+1});return 0}function wc(a,b){if(q)return Q(20,1,a,b);a>>>=0;b>>>=0;var c=re();I()[a>>>2>>>0]=c.length;var d=0;c.forEach(f=>d+=f.length+1);I()[b>>>2>>>0]=d;return 0}function yc(a){return q?Q(21,1,a):52}function zc(a,b,c,d){return q?Q(22,1,a,b,c,d):52}function Ac(a,b,c,d){return q?Q(23,1,a,b,c,d):70} +var se=[null,[],[]];function Bc(a,b,c,d){if(q)return Q(24,1,a,b,c,d);b>>>=0;c>>>=0;d>>>=0;for(var f=0,g=0;g>>2>>>0],l=I()[b+4>>>2>>>0];b+=8;for(var m=0;m>>0],r=se[a];0===p||10===p?((1===a?ta:x)(dd(r)),r.length=0):r.push(p)}f+=l}I()[d>>>2>>>0]=f;return 0}q||Sc();for(var te=Array(256),ue=0;256>ue;++ue)te[ue]=String.fromCharCode(ue);gd=te;S=e.BindingError=class extends Error{constructor(a){super(a);this.name="BindingError"}}; +e.InternalError=class extends Error{constructor(a){super(a);this.name="InternalError"}};V.push(0,1,void 0,1,null,1,!0,1,!1,1);e.count_emval_handles=()=>V.length/2-5-nd.length;var Cd=[Cc,Qc,bd,gb,hb,ib,jb,kb,lb,mb,nb,ob,pb,qb,rb,sb,dc,ec,fc,vc,wc,yc,zc,Ac,Bc],bb,K; +(async function(){function a(d,f){K=d.exports;K=Hd();K=ve();Rc.push(K.ib);va=f;Wa();return K}Ua++;var b=ab();if(e.instantiateWasm)return new Promise(d=>{e.instantiateWasm(b,(f,g)=>{a(f,g);d(f.exports)})});if(q)return new Promise(d=>{Ha=f=>{var g=new WebAssembly.Instance(f,ab());d(a(g,f))}});Xa??=e.locateFile?e.locateFile?e.locateFile("ort-wasm-simd-threaded.jsep.wasm",v):v+"ort-wasm-simd-threaded.jsep.wasm":(new URL("ort-wasm-simd-threaded.jsep.wasm",import.meta.url)).href;try{var c=await $a(b); +return a(c.instance,c.module)}catch(d){return ca(d),Promise.reject(d)}})();var Dd=a=>(Dd=K.Da)(a),Pa=()=>(Pa=K.Ea)();e._OrtInit=(a,b)=>(e._OrtInit=K.Fa)(a,b);e._OrtGetLastError=(a,b)=>(e._OrtGetLastError=K.Ga)(a,b);e._OrtCreateSessionOptions=(a,b,c,d,f,g,h,l,m,p)=>(e._OrtCreateSessionOptions=K.Ha)(a,b,c,d,f,g,h,l,m,p);e._OrtAppendExecutionProvider=(a,b,c,d,f)=>(e._OrtAppendExecutionProvider=K.Ia)(a,b,c,d,f);e._OrtAddFreeDimensionOverride=(a,b,c)=>(e._OrtAddFreeDimensionOverride=K.Ja)(a,b,c); +e._OrtAddSessionConfigEntry=(a,b,c)=>(e._OrtAddSessionConfigEntry=K.Ka)(a,b,c);e._OrtReleaseSessionOptions=a=>(e._OrtReleaseSessionOptions=K.La)(a);e._OrtCreateSession=(a,b,c)=>(e._OrtCreateSession=K.Ma)(a,b,c);e._OrtReleaseSession=a=>(e._OrtReleaseSession=K.Na)(a);e._OrtGetInputOutputCount=(a,b,c)=>(e._OrtGetInputOutputCount=K.Oa)(a,b,c);e._OrtGetInputOutputMetadata=(a,b,c,d)=>(e._OrtGetInputOutputMetadata=K.Pa)(a,b,c,d);e._OrtFree=a=>(e._OrtFree=K.Qa)(a); +e._OrtCreateTensor=(a,b,c,d,f,g)=>(e._OrtCreateTensor=K.Ra)(a,b,c,d,f,g);e._OrtGetTensorData=(a,b,c,d,f)=>(e._OrtGetTensorData=K.Sa)(a,b,c,d,f);e._OrtReleaseTensor=a=>(e._OrtReleaseTensor=K.Ta)(a);e._OrtCreateRunOptions=(a,b,c,d)=>(e._OrtCreateRunOptions=K.Ua)(a,b,c,d);e._OrtAddRunConfigEntry=(a,b,c)=>(e._OrtAddRunConfigEntry=K.Va)(a,b,c);e._OrtReleaseRunOptions=a=>(e._OrtReleaseRunOptions=K.Wa)(a);e._OrtCreateBinding=a=>(e._OrtCreateBinding=K.Xa)(a); +e._OrtBindInput=(a,b,c)=>(e._OrtBindInput=K.Ya)(a,b,c);e._OrtBindOutput=(a,b,c,d)=>(e._OrtBindOutput=K.Za)(a,b,c,d);e._OrtClearBoundOutputs=a=>(e._OrtClearBoundOutputs=K._a)(a);e._OrtReleaseBinding=a=>(e._OrtReleaseBinding=K.$a)(a);e._OrtRunWithBinding=(a,b,c,d,f)=>(e._OrtRunWithBinding=K.ab)(a,b,c,d,f);e._OrtRun=(a,b,c,d,f,g,h,l)=>(e._OrtRun=K.bb)(a,b,c,d,f,g,h,l);e._OrtEndProfiling=a=>(e._OrtEndProfiling=K.cb)(a);e._JsepOutput=(a,b,c)=>(e._JsepOutput=K.db)(a,b,c); +e._JsepGetNodeName=a=>(e._JsepGetNodeName=K.eb)(a); +var Ka=()=>(Ka=K.fb)(),Y=e._free=a=>(Y=e._free=K.gb)(a),rd=e._malloc=a=>(rd=e._malloc=K.hb)(a),Ma=(a,b,c,d,f,g)=>(Ma=K.kb)(a,b,c,d,f,g),Sa=()=>(Sa=K.lb)(),Oc=(a,b,c,d,f)=>(Oc=K.mb)(a,b,c,d,f),Uc=a=>(Uc=K.nb)(a),Yc=a=>(Yc=K.ob)(a),ie=(a,b)=>(ie=K.pb)(a,b),Ad=()=>(Ad=K.qb)(),Wc=(a,b)=>(Wc=K.rb)(a,b),Pc=a=>(Pc=K.sb)(a),Nc=a=>(Nc=K.tb)(a),Mc=()=>(Mc=K.ub)(),Xc=e.dynCall_ii=(a,b)=>(Xc=e.dynCall_ii=K.vb)(a,b),Ud=a=>(Ud=K.wb)(a),Jd=()=>(Jd=K.xb)(),Td=a=>(Td=K.yb)(a),Wd=()=>(Wd=K.zb)(); +function ve(){var a=K;a=Object.assign({},a);var b=d=>f=>d(f)>>>0,c=d=>()=>d()>>>0;a.Da=b(a.Da);a.fb=c(a.fb);a.hb=b(a.hb);a.tb=b(a.tb);a.ub=c(a.ub);a.__cxa_get_exception_ptr=b(a.__cxa_get_exception_ptr);return a}e.stackSave=()=>Mc();e.stackRestore=a=>Pc(a);e.stackAlloc=a=>Nc(a); +e.setValue=function(a,b,c="i8"){c.endsWith("*")&&(c="*");switch(c){case "i1":D()[a>>>0]=b;break;case "i8":D()[a>>>0]=b;break;case "i16":G()[a>>>1>>>0]=b;break;case "i32":H()[a>>>2>>>0]=b;break;case "i64":C[a>>>3]=BigInt(b);break;case "float":Ga()[a>>>2>>>0]=b;break;case "double":J()[a>>>3>>>0]=b;break;case "*":I()[a>>>2>>>0]=b;break;default:L(`invalid type for setValue: ${c}`)}}; +e.getValue=function(a,b="i8"){b.endsWith("*")&&(b="*");switch(b){case "i1":return D()[a>>>0];case "i8":return D()[a>>>0];case "i16":return G()[a>>>1>>>0];case "i32":return H()[a>>>2>>>0];case "i64":return C[a>>>3];case "float":return Ga()[a>>>2>>>0];case "double":return J()[a>>>3>>>0];case "*":return I()[a>>>2>>>0];default:L(`invalid type for getValue: ${b}`)}};e.UTF8ToString=M;e.stringToUTF8=fd;e.lengthBytesUTF8=ed; +function we(){if(0 { + +module.exports = __webpack_require__.p + "ort-wasm-simd-threaded.jsep.wasm"; + +/***/ }), + +/***/ "./node_modules/onnxruntime-web/dist/ort.bundle.min.mjs?46eb": +/*!**************************************************************!*\ + !*** ./node_modules/onnxruntime-web/dist/ort.bundle.min.mjs ***! + \**************************************************************/ +/***/ ((module, __unused_webpack_exports, __webpack_require__) => { + +module.exports = __webpack_require__.p + "ort.bundle.min.mjs"; + +/***/ }), + +/***/ "?2ce3": +/*!**********************************!*\ + !*** onnxruntime-node (ignored) ***! + \**********************************/ +/***/ (() => { + +/* (ignored) */ + +/***/ }), + +/***/ "?7a2c": +/*!********************!*\ + !*** fs (ignored) ***! + \********************/ +/***/ (() => { + +/* (ignored) */ + +/***/ }), + +/***/ "?a42a": +/*!**********************!*\ + !*** path (ignored) ***! + \**********************/ +/***/ (() => { + +/* (ignored) */ + +/***/ }), + +/***/ "?2b25": +/*!***********************!*\ + !*** sharp (ignored) ***! + \***********************/ +/***/ (() => { + +/* (ignored) */ + +/***/ }), + +/***/ "?569f": +/*!********************!*\ + !*** fs (ignored) ***! + \********************/ +/***/ (() => { + +/* (ignored) */ + +/***/ }), + +/***/ "?3f59": +/*!**********************!*\ + !*** path (ignored) ***! + \**********************/ +/***/ (() => { + +/* (ignored) */ + +/***/ }), + +/***/ "?154a": +/*!*********************!*\ + !*** url (ignored) ***! + \*********************/ +/***/ (() => { + +/* (ignored) */ + +/***/ }), + +/***/ "./node_modules/@huggingface/jinja/dist/index.js": +/*!*******************************************************!*\ + !*** ./node_modules/@huggingface/jinja/dist/index.js ***! + \*******************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ Environment: () => (/* binding */ Environment), +/* harmony export */ Interpreter: () => (/* binding */ Interpreter), +/* harmony export */ Template: () => (/* binding */ Template), +/* harmony export */ parse: () => (/* binding */ parse), +/* harmony export */ tokenize: () => (/* binding */ tokenize) +/* harmony export */ }); +// src/lexer.ts +var TOKEN_TYPES = Object.freeze({ + Text: "Text", + // The text between Jinja statements or expressions + NumericLiteral: "NumericLiteral", + // e.g., 123 + BooleanLiteral: "BooleanLiteral", + // true or false + NullLiteral: "NullLiteral", + // none + StringLiteral: "StringLiteral", + // 'string' + Identifier: "Identifier", + // Variables, functions, etc. + Equals: "Equals", + // = + OpenParen: "OpenParen", + // ( + CloseParen: "CloseParen", + // ) + OpenStatement: "OpenStatement", + // {% + CloseStatement: "CloseStatement", + // %} + OpenExpression: "OpenExpression", + // {{ + CloseExpression: "CloseExpression", + // }} + OpenSquareBracket: "OpenSquareBracket", + // [ + CloseSquareBracket: "CloseSquareBracket", + // ] + OpenCurlyBracket: "OpenCurlyBracket", + // { + CloseCurlyBracket: "CloseCurlyBracket", + // } + Comma: "Comma", + // , + Dot: "Dot", + // . + Colon: "Colon", + // : + Pipe: "Pipe", + // | + CallOperator: "CallOperator", + // () + AdditiveBinaryOperator: "AdditiveBinaryOperator", + // + - + MultiplicativeBinaryOperator: "MultiplicativeBinaryOperator", + // * / % + ComparisonBinaryOperator: "ComparisonBinaryOperator", + // < > <= >= == != + UnaryOperator: "UnaryOperator", + // ! - + + // Keywords + Set: "Set", + If: "If", + For: "For", + In: "In", + Is: "Is", + NotIn: "NotIn", + Else: "Else", + EndSet: "EndSet", + EndIf: "EndIf", + ElseIf: "ElseIf", + EndFor: "EndFor", + And: "And", + Or: "Or", + Not: "UnaryOperator", + Macro: "Macro", + EndMacro: "EndMacro", + Break: "Break", + Continue: "Continue" +}); +var KEYWORDS = Object.freeze({ + set: TOKEN_TYPES.Set, + for: TOKEN_TYPES.For, + in: TOKEN_TYPES.In, + is: TOKEN_TYPES.Is, + if: TOKEN_TYPES.If, + else: TOKEN_TYPES.Else, + endset: TOKEN_TYPES.EndSet, + endif: TOKEN_TYPES.EndIf, + elif: TOKEN_TYPES.ElseIf, + endfor: TOKEN_TYPES.EndFor, + and: TOKEN_TYPES.And, + or: TOKEN_TYPES.Or, + not: TOKEN_TYPES.Not, + "not in": TOKEN_TYPES.NotIn, + macro: TOKEN_TYPES.Macro, + endmacro: TOKEN_TYPES.EndMacro, + break: TOKEN_TYPES.Break, + continue: TOKEN_TYPES.Continue, + // Literals + true: TOKEN_TYPES.BooleanLiteral, + false: TOKEN_TYPES.BooleanLiteral, + none: TOKEN_TYPES.NullLiteral, + // NOTE: According to the Jinja docs: The special constants true, false, and none are indeed lowercase. + // Because that caused confusion in the past, (True used to expand to an undefined variable that was considered false), + // all three can now also be written in title case (True, False, and None). However, for consistency, (all Jinja identifiers are lowercase) + // you should use the lowercase versions. + True: TOKEN_TYPES.BooleanLiteral, + False: TOKEN_TYPES.BooleanLiteral, + None: TOKEN_TYPES.NullLiteral +}); +var Token = class { + /** + * Constructs a new Token. + * @param {string} value The raw value as seen inside the source code. + * @param {TokenType} type The type of token. + */ + constructor(value, type) { + this.value = value; + this.type = type; + } +}; +function isWord(char) { + return /\w/.test(char); +} +function isInteger(char) { + return /[0-9]/.test(char); +} +var ORDERED_MAPPING_TABLE = [ + // Control sequences + ["{%", TOKEN_TYPES.OpenStatement], + ["%}", TOKEN_TYPES.CloseStatement], + ["{{", TOKEN_TYPES.OpenExpression], + ["}}", TOKEN_TYPES.CloseExpression], + // Single character tokens + ["(", TOKEN_TYPES.OpenParen], + [")", TOKEN_TYPES.CloseParen], + ["{", TOKEN_TYPES.OpenCurlyBracket], + ["}", TOKEN_TYPES.CloseCurlyBracket], + ["[", TOKEN_TYPES.OpenSquareBracket], + ["]", TOKEN_TYPES.CloseSquareBracket], + [",", TOKEN_TYPES.Comma], + [".", TOKEN_TYPES.Dot], + [":", TOKEN_TYPES.Colon], + ["|", TOKEN_TYPES.Pipe], + // Comparison operators + ["<=", TOKEN_TYPES.ComparisonBinaryOperator], + [">=", TOKEN_TYPES.ComparisonBinaryOperator], + ["==", TOKEN_TYPES.ComparisonBinaryOperator], + ["!=", TOKEN_TYPES.ComparisonBinaryOperator], + ["<", TOKEN_TYPES.ComparisonBinaryOperator], + [">", TOKEN_TYPES.ComparisonBinaryOperator], + // Arithmetic operators + ["+", TOKEN_TYPES.AdditiveBinaryOperator], + ["-", TOKEN_TYPES.AdditiveBinaryOperator], + ["*", TOKEN_TYPES.MultiplicativeBinaryOperator], + ["/", TOKEN_TYPES.MultiplicativeBinaryOperator], + ["%", TOKEN_TYPES.MultiplicativeBinaryOperator], + // Assignment operator + ["=", TOKEN_TYPES.Equals] +]; +var ESCAPE_CHARACTERS = /* @__PURE__ */ new Map([ + ["n", "\n"], + // New line + ["t", " "], + // Horizontal tab + ["r", "\r"], + // Carriage return + ["b", "\b"], + // Backspace + ["f", "\f"], + // Form feed + ["v", "\v"], + // Vertical tab + ["'", "'"], + // Single quote + ['"', '"'], + // Double quote + ["\\", "\\"] + // Backslash +]); +function preprocess(template, options = {}) { + if (template.endsWith("\n")) { + template = template.slice(0, -1); + } + template = template.replace(/{#.*?#}/gs, "{##}"); + if (options.lstrip_blocks) { + template = template.replace(/^[ \t]*({[#%])/gm, "$1"); + } + if (options.trim_blocks) { + template = template.replace(/([#%]})\n/g, "$1"); + } + return template.replace(/{##}/g, "").replace(/-%}\s*/g, "%}").replace(/\s*{%-/g, "{%").replace(/-}}\s*/g, "}}").replace(/\s*{{-/g, "{{"); +} +function tokenize(source, options = {}) { + const tokens = []; + const src = preprocess(source, options); + let cursorPosition = 0; + const consumeWhile = (predicate) => { + let str = ""; + while (predicate(src[cursorPosition])) { + if (src[cursorPosition] === "\\") { + ++cursorPosition; + if (cursorPosition >= src.length) + throw new SyntaxError("Unexpected end of input"); + const escaped = src[cursorPosition++]; + const unescaped = ESCAPE_CHARACTERS.get(escaped); + if (unescaped === void 0) { + throw new SyntaxError(`Unexpected escaped character: ${escaped}`); + } + str += unescaped; + continue; + } + str += src[cursorPosition++]; + if (cursorPosition >= src.length) + throw new SyntaxError("Unexpected end of input"); + } + return str; + }; + main: + while (cursorPosition < src.length) { + const lastTokenType = tokens.at(-1)?.type; + if (lastTokenType === void 0 || lastTokenType === TOKEN_TYPES.CloseStatement || lastTokenType === TOKEN_TYPES.CloseExpression) { + let text = ""; + while (cursorPosition < src.length && // Keep going until we hit the next Jinja statement or expression + !(src[cursorPosition] === "{" && (src[cursorPosition + 1] === "%" || src[cursorPosition + 1] === "{"))) { + text += src[cursorPosition++]; + } + if (text.length > 0) { + tokens.push(new Token(text, TOKEN_TYPES.Text)); + continue; + } + } + consumeWhile((char2) => /\s/.test(char2)); + const char = src[cursorPosition]; + if (char === "-" || char === "+") { + const lastTokenType2 = tokens.at(-1)?.type; + if (lastTokenType2 === TOKEN_TYPES.Text || lastTokenType2 === void 0) { + throw new SyntaxError(`Unexpected character: ${char}`); + } + switch (lastTokenType2) { + case TOKEN_TYPES.Identifier: + case TOKEN_TYPES.NumericLiteral: + case TOKEN_TYPES.BooleanLiteral: + case TOKEN_TYPES.NullLiteral: + case TOKEN_TYPES.StringLiteral: + case TOKEN_TYPES.CloseParen: + case TOKEN_TYPES.CloseSquareBracket: + break; + default: { + ++cursorPosition; + const num = consumeWhile(isInteger); + tokens.push( + new Token(`${char}${num}`, num.length > 0 ? TOKEN_TYPES.NumericLiteral : TOKEN_TYPES.UnaryOperator) + ); + continue; + } + } + } + for (const [char2, token] of ORDERED_MAPPING_TABLE) { + const slice2 = src.slice(cursorPosition, cursorPosition + char2.length); + if (slice2 === char2) { + tokens.push(new Token(char2, token)); + cursorPosition += char2.length; + continue main; + } + } + if (char === "'" || char === '"') { + ++cursorPosition; + const str = consumeWhile((c) => c !== char); + tokens.push(new Token(str, TOKEN_TYPES.StringLiteral)); + ++cursorPosition; + continue; + } + if (isInteger(char)) { + const num = consumeWhile(isInteger); + tokens.push(new Token(num, TOKEN_TYPES.NumericLiteral)); + continue; + } + if (isWord(char)) { + const word = consumeWhile(isWord); + const type = Object.hasOwn(KEYWORDS, word) ? KEYWORDS[word] : TOKEN_TYPES.Identifier; + if (type === TOKEN_TYPES.In && tokens.at(-1)?.type === TOKEN_TYPES.Not) { + tokens.pop(); + tokens.push(new Token("not in", TOKEN_TYPES.NotIn)); + } else { + tokens.push(new Token(word, type)); + } + continue; + } + throw new SyntaxError(`Unexpected character: ${char}`); + } + return tokens; +} + +// src/ast.ts +var Statement = class { + type = "Statement"; +}; +var Program = class extends Statement { + constructor(body) { + super(); + this.body = body; + } + type = "Program"; +}; +var If = class extends Statement { + constructor(test, body, alternate) { + super(); + this.test = test; + this.body = body; + this.alternate = alternate; + } + type = "If"; +}; +var For = class extends Statement { + constructor(loopvar, iterable, body, defaultBlock) { + super(); + this.loopvar = loopvar; + this.iterable = iterable; + this.body = body; + this.defaultBlock = defaultBlock; + } + type = "For"; +}; +var Break = class extends Statement { + type = "Break"; +}; +var Continue = class extends Statement { + type = "Continue"; +}; +var SetStatement = class extends Statement { + constructor(assignee, value, body) { + super(); + this.assignee = assignee; + this.value = value; + this.body = body; + } + type = "Set"; +}; +var Macro = class extends Statement { + constructor(name, args, body) { + super(); + this.name = name; + this.args = args; + this.body = body; + } + type = "Macro"; +}; +var Expression = class extends Statement { + type = "Expression"; +}; +var MemberExpression = class extends Expression { + constructor(object, property, computed) { + super(); + this.object = object; + this.property = property; + this.computed = computed; + } + type = "MemberExpression"; +}; +var CallExpression = class extends Expression { + constructor(callee, args) { + super(); + this.callee = callee; + this.args = args; + } + type = "CallExpression"; +}; +var Identifier = class extends Expression { + /** + * @param {string} value The name of the identifier + */ + constructor(value) { + super(); + this.value = value; + } + type = "Identifier"; +}; +var Literal = class extends Expression { + constructor(value) { + super(); + this.value = value; + } + type = "Literal"; +}; +var NumericLiteral = class extends Literal { + type = "NumericLiteral"; +}; +var StringLiteral = class extends Literal { + type = "StringLiteral"; +}; +var BooleanLiteral = class extends Literal { + type = "BooleanLiteral"; +}; +var NullLiteral = class extends Literal { + type = "NullLiteral"; +}; +var ArrayLiteral = class extends Literal { + type = "ArrayLiteral"; +}; +var TupleLiteral = class extends Literal { + type = "TupleLiteral"; +}; +var ObjectLiteral = class extends Literal { + type = "ObjectLiteral"; +}; +var BinaryExpression = class extends Expression { + constructor(operator, left, right) { + super(); + this.operator = operator; + this.left = left; + this.right = right; + } + type = "BinaryExpression"; +}; +var FilterExpression = class extends Expression { + constructor(operand, filter) { + super(); + this.operand = operand; + this.filter = filter; + } + type = "FilterExpression"; +}; +var SelectExpression = class extends Expression { + constructor(iterable, test) { + super(); + this.iterable = iterable; + this.test = test; + } + type = "SelectExpression"; +}; +var TestExpression = class extends Expression { + constructor(operand, negate, test) { + super(); + this.operand = operand; + this.negate = negate; + this.test = test; + } + type = "TestExpression"; +}; +var UnaryExpression = class extends Expression { + constructor(operator, argument) { + super(); + this.operator = operator; + this.argument = argument; + } + type = "UnaryExpression"; +}; +var SliceExpression = class extends Expression { + constructor(start = void 0, stop = void 0, step = void 0) { + super(); + this.start = start; + this.stop = stop; + this.step = step; + } + type = "SliceExpression"; +}; +var KeywordArgumentExpression = class extends Expression { + constructor(key, value) { + super(); + this.key = key; + this.value = value; + } + type = "KeywordArgumentExpression"; +}; + +// src/parser.ts +function parse(tokens) { + const program = new Program([]); + let current = 0; + function expect(type, error) { + const prev = tokens[current++]; + if (!prev || prev.type !== type) { + throw new Error(`Parser Error: ${error}. ${prev.type} !== ${type}.`); + } + return prev; + } + function parseAny() { + switch (tokens[current].type) { + case TOKEN_TYPES.Text: + return parseText(); + case TOKEN_TYPES.OpenStatement: + return parseJinjaStatement(); + case TOKEN_TYPES.OpenExpression: + return parseJinjaExpression(); + default: + throw new SyntaxError(`Unexpected token type: ${tokens[current].type}`); + } + } + function not(...types) { + return current + types.length <= tokens.length && types.some((type, i) => type !== tokens[current + i].type); + } + function is(...types) { + return current + types.length <= tokens.length && types.every((type, i) => type === tokens[current + i].type); + } + function parseText() { + return new StringLiteral(expect(TOKEN_TYPES.Text, "Expected text token").value); + } + function parseJinjaStatement() { + expect(TOKEN_TYPES.OpenStatement, "Expected opening statement token"); + let result; + switch (tokens[current].type) { + case TOKEN_TYPES.Set: + ++current; + result = parseSetStatement(); + expect(TOKEN_TYPES.CloseStatement, "Expected closing statement token"); + break; + case TOKEN_TYPES.If: + ++current; + result = parseIfStatement(); + expect(TOKEN_TYPES.OpenStatement, "Expected {% token"); + expect(TOKEN_TYPES.EndIf, "Expected endif token"); + expect(TOKEN_TYPES.CloseStatement, "Expected %} token"); + break; + case TOKEN_TYPES.Macro: + ++current; + result = parseMacroStatement(); + expect(TOKEN_TYPES.OpenStatement, "Expected {% token"); + expect(TOKEN_TYPES.EndMacro, "Expected endmacro token"); + expect(TOKEN_TYPES.CloseStatement, "Expected %} token"); + break; + case TOKEN_TYPES.For: + ++current; + result = parseForStatement(); + expect(TOKEN_TYPES.OpenStatement, "Expected {% token"); + expect(TOKEN_TYPES.EndFor, "Expected endfor token"); + expect(TOKEN_TYPES.CloseStatement, "Expected %} token"); + break; + case TOKEN_TYPES.Break: + ++current; + expect(TOKEN_TYPES.CloseStatement, "Expected closing statement token"); + result = new Break(); + break; + case TOKEN_TYPES.Continue: + ++current; + expect(TOKEN_TYPES.CloseStatement, "Expected closing statement token"); + result = new Continue(); + break; + default: + throw new SyntaxError(`Unknown statement type: ${tokens[current].type}`); + } + return result; + } + function parseJinjaExpression() { + expect(TOKEN_TYPES.OpenExpression, "Expected opening expression token"); + const result = parseExpression(); + expect(TOKEN_TYPES.CloseExpression, "Expected closing expression token"); + return result; + } + function parseSetStatement() { + const left = parseExpression(); + if (is(TOKEN_TYPES.Equals)) { + ++current; + const value = parseExpression(); + return new SetStatement(left, value, []); + } else { + const body = []; + expect(TOKEN_TYPES.CloseStatement, "Expected %} token"); + while (!(tokens[current]?.type === TOKEN_TYPES.OpenStatement && tokens[current + 1]?.type === TOKEN_TYPES.EndSet)) { + const another = parseAny(); + body.push(another); + } + expect(TOKEN_TYPES.OpenStatement, "Expected {% token"); + expect(TOKEN_TYPES.EndSet, "Expected endset token"); + return new SetStatement(left, null, body); + } + } + function parseIfStatement() { + const test = parseExpression(); + expect(TOKEN_TYPES.CloseStatement, "Expected closing statement token"); + const body = []; + const alternate = []; + while (!(tokens[current]?.type === TOKEN_TYPES.OpenStatement && (tokens[current + 1]?.type === TOKEN_TYPES.ElseIf || tokens[current + 1]?.type === TOKEN_TYPES.Else || tokens[current + 1]?.type === TOKEN_TYPES.EndIf))) { + body.push(parseAny()); + } + if (tokens[current]?.type === TOKEN_TYPES.OpenStatement && tokens[current + 1]?.type !== TOKEN_TYPES.EndIf) { + ++current; + if (is(TOKEN_TYPES.ElseIf)) { + expect(TOKEN_TYPES.ElseIf, "Expected elseif token"); + alternate.push(parseIfStatement()); + } else { + expect(TOKEN_TYPES.Else, "Expected else token"); + expect(TOKEN_TYPES.CloseStatement, "Expected closing statement token"); + while (!(tokens[current]?.type === TOKEN_TYPES.OpenStatement && tokens[current + 1]?.type === TOKEN_TYPES.EndIf)) { + alternate.push(parseAny()); + } + } + } + return new If(test, body, alternate); + } + function parseMacroStatement() { + const name = parsePrimaryExpression(); + if (name.type !== "Identifier") { + throw new SyntaxError(`Expected identifier following macro statement`); + } + const args = parseArgs(); + expect(TOKEN_TYPES.CloseStatement, "Expected closing statement token"); + const body = []; + while (not(TOKEN_TYPES.OpenStatement, TOKEN_TYPES.EndMacro)) { + body.push(parseAny()); + } + return new Macro(name, args, body); + } + function parseExpressionSequence(primary = false) { + const fn = primary ? parsePrimaryExpression : parseExpression; + const expressions = [fn()]; + const isTuple = is(TOKEN_TYPES.Comma); + while (isTuple) { + ++current; + expressions.push(fn()); + if (!is(TOKEN_TYPES.Comma)) { + break; + } + } + return isTuple ? new TupleLiteral(expressions) : expressions[0]; + } + function parseForStatement() { + const loopVariable = parseExpressionSequence(true); + if (!(loopVariable instanceof Identifier || loopVariable instanceof TupleLiteral)) { + throw new SyntaxError(`Expected identifier/tuple for the loop variable, got ${loopVariable.type} instead`); + } + expect(TOKEN_TYPES.In, "Expected `in` keyword following loop variable"); + const iterable = parseExpression(); + expect(TOKEN_TYPES.CloseStatement, "Expected closing statement token"); + const body = []; + while (not(TOKEN_TYPES.OpenStatement, TOKEN_TYPES.EndFor) && not(TOKEN_TYPES.OpenStatement, TOKEN_TYPES.Else)) { + body.push(parseAny()); + } + const alternative = []; + if (is(TOKEN_TYPES.OpenStatement, TOKEN_TYPES.Else)) { + ++current; + ++current; + expect(TOKEN_TYPES.CloseStatement, "Expected closing statement token"); + while (not(TOKEN_TYPES.OpenStatement, TOKEN_TYPES.EndFor)) { + alternative.push(parseAny()); + } + } + return new For(loopVariable, iterable, body, alternative); + } + function parseExpression() { + return parseIfExpression(); + } + function parseIfExpression() { + const a = parseLogicalOrExpression(); + if (is(TOKEN_TYPES.If)) { + ++current; + const predicate = parseLogicalOrExpression(); + if (is(TOKEN_TYPES.Else)) { + ++current; + const b = parseLogicalOrExpression(); + return new If(predicate, [a], [b]); + } else { + return new SelectExpression(a, predicate); + } + } + return a; + } + function parseLogicalOrExpression() { + let left = parseLogicalAndExpression(); + while (is(TOKEN_TYPES.Or)) { + const operator = tokens[current]; + ++current; + const right = parseLogicalAndExpression(); + left = new BinaryExpression(operator, left, right); + } + return left; + } + function parseLogicalAndExpression() { + let left = parseLogicalNegationExpression(); + while (is(TOKEN_TYPES.And)) { + const operator = tokens[current]; + ++current; + const right = parseLogicalNegationExpression(); + left = new BinaryExpression(operator, left, right); + } + return left; + } + function parseLogicalNegationExpression() { + let right; + while (is(TOKEN_TYPES.Not)) { + const operator = tokens[current]; + ++current; + const arg = parseLogicalNegationExpression(); + right = new UnaryExpression(operator, arg); + } + return right ?? parseComparisonExpression(); + } + function parseComparisonExpression() { + let left = parseAdditiveExpression(); + while (is(TOKEN_TYPES.ComparisonBinaryOperator) || is(TOKEN_TYPES.In) || is(TOKEN_TYPES.NotIn)) { + const operator = tokens[current]; + ++current; + const right = parseAdditiveExpression(); + left = new BinaryExpression(operator, left, right); + } + return left; + } + function parseAdditiveExpression() { + let left = parseMultiplicativeExpression(); + while (is(TOKEN_TYPES.AdditiveBinaryOperator)) { + const operator = tokens[current]; + ++current; + const right = parseMultiplicativeExpression(); + left = new BinaryExpression(operator, left, right); + } + return left; + } + function parseCallMemberExpression() { + const member = parseMemberExpression(parsePrimaryExpression()); + if (is(TOKEN_TYPES.OpenParen)) { + return parseCallExpression(member); + } + return member; + } + function parseCallExpression(callee) { + let expression = new CallExpression(callee, parseArgs()); + expression = parseMemberExpression(expression); + if (is(TOKEN_TYPES.OpenParen)) { + expression = parseCallExpression(expression); + } + return expression; + } + function parseArgs() { + expect(TOKEN_TYPES.OpenParen, "Expected opening parenthesis for arguments list"); + const args = parseArgumentsList(); + expect(TOKEN_TYPES.CloseParen, "Expected closing parenthesis for arguments list"); + return args; + } + function parseArgumentsList() { + const args = []; + while (!is(TOKEN_TYPES.CloseParen)) { + let argument = parseExpression(); + if (is(TOKEN_TYPES.Equals)) { + ++current; + if (!(argument instanceof Identifier)) { + throw new SyntaxError(`Expected identifier for keyword argument`); + } + const value = parseExpression(); + argument = new KeywordArgumentExpression(argument, value); + } + args.push(argument); + if (is(TOKEN_TYPES.Comma)) { + ++current; + } + } + return args; + } + function parseMemberExpressionArgumentsList() { + const slices = []; + let isSlice = false; + while (!is(TOKEN_TYPES.CloseSquareBracket)) { + if (is(TOKEN_TYPES.Colon)) { + slices.push(void 0); + ++current; + isSlice = true; + } else { + slices.push(parseExpression()); + if (is(TOKEN_TYPES.Colon)) { + ++current; + isSlice = true; + } + } + } + if (slices.length === 0) { + throw new SyntaxError(`Expected at least one argument for member/slice expression`); + } + if (isSlice) { + if (slices.length > 3) { + throw new SyntaxError(`Expected 0-3 arguments for slice expression`); + } + return new SliceExpression(...slices); + } + return slices[0]; + } + function parseMemberExpression(object) { + while (is(TOKEN_TYPES.Dot) || is(TOKEN_TYPES.OpenSquareBracket)) { + const operator = tokens[current]; + ++current; + let property; + const computed = operator.type !== TOKEN_TYPES.Dot; + if (computed) { + property = parseMemberExpressionArgumentsList(); + expect(TOKEN_TYPES.CloseSquareBracket, "Expected closing square bracket"); + } else { + property = parsePrimaryExpression(); + if (property.type !== "Identifier") { + throw new SyntaxError(`Expected identifier following dot operator`); + } + } + object = new MemberExpression(object, property, computed); + } + return object; + } + function parseMultiplicativeExpression() { + let left = parseTestExpression(); + while (is(TOKEN_TYPES.MultiplicativeBinaryOperator)) { + const operator = tokens[current]; + ++current; + const right = parseTestExpression(); + left = new BinaryExpression(operator, left, right); + } + return left; + } + function parseTestExpression() { + let operand = parseFilterExpression(); + while (is(TOKEN_TYPES.Is)) { + ++current; + const negate = is(TOKEN_TYPES.Not); + if (negate) { + ++current; + } + let filter = parsePrimaryExpression(); + if (filter instanceof BooleanLiteral) { + filter = new Identifier(filter.value.toString()); + } else if (filter instanceof NullLiteral) { + filter = new Identifier("none"); + } + if (!(filter instanceof Identifier)) { + throw new SyntaxError(`Expected identifier for the test`); + } + operand = new TestExpression(operand, negate, filter); + } + return operand; + } + function parseFilterExpression() { + let operand = parseCallMemberExpression(); + while (is(TOKEN_TYPES.Pipe)) { + ++current; + let filter = parsePrimaryExpression(); + if (!(filter instanceof Identifier)) { + throw new SyntaxError(`Expected identifier for the filter`); + } + if (is(TOKEN_TYPES.OpenParen)) { + filter = parseCallExpression(filter); + } + operand = new FilterExpression(operand, filter); + } + return operand; + } + function parsePrimaryExpression() { + const token = tokens[current]; + switch (token.type) { + case TOKEN_TYPES.NumericLiteral: + ++current; + return new NumericLiteral(Number(token.value)); + case TOKEN_TYPES.StringLiteral: + ++current; + return new StringLiteral(token.value); + case TOKEN_TYPES.BooleanLiteral: + ++current; + return new BooleanLiteral(token.value.toLowerCase() === "true"); + case TOKEN_TYPES.NullLiteral: + ++current; + return new NullLiteral(null); + case TOKEN_TYPES.Identifier: + ++current; + return new Identifier(token.value); + case TOKEN_TYPES.OpenParen: { + ++current; + const expression = parseExpressionSequence(); + if (tokens[current].type !== TOKEN_TYPES.CloseParen) { + throw new SyntaxError(`Expected closing parenthesis, got ${tokens[current].type} instead`); + } + ++current; + return expression; + } + case TOKEN_TYPES.OpenSquareBracket: { + ++current; + const values = []; + while (!is(TOKEN_TYPES.CloseSquareBracket)) { + values.push(parseExpression()); + if (is(TOKEN_TYPES.Comma)) { + ++current; + } + } + ++current; + return new ArrayLiteral(values); + } + case TOKEN_TYPES.OpenCurlyBracket: { + ++current; + const values = /* @__PURE__ */ new Map(); + while (!is(TOKEN_TYPES.CloseCurlyBracket)) { + const key = parseExpression(); + expect(TOKEN_TYPES.Colon, "Expected colon between key and value in object literal"); + const value = parseExpression(); + values.set(key, value); + if (is(TOKEN_TYPES.Comma)) { + ++current; + } + } + ++current; + return new ObjectLiteral(values); + } + default: + throw new SyntaxError(`Unexpected token: ${token.type}`); + } + } + while (current < tokens.length) { + program.body.push(parseAny()); + } + return program; +} + +// src/utils.ts +function range(start, stop, step = 1) { + if (stop === void 0) { + stop = start; + start = 0; + } + const result = []; + for (let i = start; i < stop; i += step) { + result.push(i); + } + return result; +} +function slice(array, start, stop, step = 1) { + const direction = Math.sign(step); + if (direction >= 0) { + start = (start ??= 0) < 0 ? Math.max(array.length + start, 0) : Math.min(start, array.length); + stop = (stop ??= array.length) < 0 ? Math.max(array.length + stop, 0) : Math.min(stop, array.length); + } else { + start = (start ??= array.length - 1) < 0 ? Math.max(array.length + start, -1) : Math.min(start, array.length - 1); + stop = (stop ??= -1) < -1 ? Math.max(array.length + stop, -1) : Math.min(stop, array.length - 1); + } + const result = []; + for (let i = start; direction * i < direction * stop; i += step) { + result.push(array[i]); + } + return result; +} +function titleCase(value) { + return value.replace(/\b\w/g, (c) => c.toUpperCase()); +} + +// src/runtime.ts +var BreakControl = class extends Error { +}; +var ContinueControl = class extends Error { +}; +var RuntimeValue = class { + type = "RuntimeValue"; + value; + /** + * A collection of built-in functions for this type. + */ + builtins = /* @__PURE__ */ new Map(); + /** + * Creates a new RuntimeValue. + */ + constructor(value = void 0) { + this.value = value; + } + /** + * Determines truthiness or falsiness of the runtime value. + * This function should be overridden by subclasses if it has custom truthiness criteria. + * @returns {BooleanValue} BooleanValue(true) if the value is truthy, BooleanValue(false) otherwise. + */ + __bool__() { + return new BooleanValue(!!this.value); + } +}; +var NumericValue = class extends RuntimeValue { + type = "NumericValue"; +}; +var StringValue = class extends RuntimeValue { + type = "StringValue"; + builtins = /* @__PURE__ */ new Map([ + [ + "upper", + new FunctionValue(() => { + return new StringValue(this.value.toUpperCase()); + }) + ], + [ + "lower", + new FunctionValue(() => { + return new StringValue(this.value.toLowerCase()); + }) + ], + [ + "strip", + new FunctionValue(() => { + return new StringValue(this.value.trim()); + }) + ], + [ + "title", + new FunctionValue(() => { + return new StringValue(titleCase(this.value)); + }) + ], + ["length", new NumericValue(this.value.length)], + [ + "rstrip", + new FunctionValue(() => { + return new StringValue(this.value.trimEnd()); + }) + ], + [ + "lstrip", + new FunctionValue(() => { + return new StringValue(this.value.trimStart()); + }) + ], + [ + "startswith", + new FunctionValue((args) => { + if (args.length === 0) { + throw new Error("startswith() requires at least one argument"); + } + const prefix = args[0]; + if (!(prefix instanceof StringValue)) { + throw new Error("startswith() argument must be a string"); + } + return new BooleanValue(this.value.startsWith(prefix.value)); + }) + ], + [ + "endswith", + new FunctionValue((args) => { + if (args.length === 0) { + throw new Error("endswith() requires at least one argument"); + } + const suffix = args[0]; + if (!(suffix instanceof StringValue)) { + throw new Error("endswith() argument must be a string"); + } + return new BooleanValue(this.value.endsWith(suffix.value)); + }) + ], + [ + "split", + // follows Python's `str.split(sep=None, maxsplit=-1)` function behavior + // https://docs.python.org/3.13/library/stdtypes.html#str.split + new FunctionValue((args) => { + const sep = args[0] ?? new NullValue(); + if (!(sep instanceof StringValue || sep instanceof NullValue)) { + throw new Error("sep argument must be a string or null"); + } + const maxsplit = args[1] ?? new NumericValue(-1); + if (!(maxsplit instanceof NumericValue)) { + throw new Error("maxsplit argument must be a number"); + } + let result = []; + if (sep instanceof NullValue) { + const text = this.value.trimStart(); + for (const { 0: match, index } of text.matchAll(/\S+/g)) { + if (maxsplit.value !== -1 && result.length >= maxsplit.value && index !== void 0) { + result.push(match + text.slice(index + match.length)); + break; + } + result.push(match); + } + } else { + if (sep.value === "") { + throw new Error("empty separator"); + } + result = this.value.split(sep.value); + if (maxsplit.value !== -1 && result.length > maxsplit.value) { + result.push(result.splice(maxsplit.value).join(sep.value)); + } + } + return new ArrayValue(result.map((part) => new StringValue(part))); + }) + ] + ]); +}; +var BooleanValue = class extends RuntimeValue { + type = "BooleanValue"; +}; +var ObjectValue = class extends RuntimeValue { + type = "ObjectValue"; + /** + * NOTE: necessary to override since all JavaScript arrays are considered truthy, + * while only non-empty Python arrays are consider truthy. + * + * e.g., + * - JavaScript: {} && 5 -> 5 + * - Python: {} and 5 -> {} + */ + __bool__() { + return new BooleanValue(this.value.size > 0); + } + builtins = /* @__PURE__ */ new Map([ + [ + "get", + new FunctionValue(([key, defaultValue]) => { + if (!(key instanceof StringValue)) { + throw new Error(`Object key must be a string: got ${key.type}`); + } + return this.value.get(key.value) ?? defaultValue ?? new NullValue(); + }) + ], + [ + "items", + new FunctionValue(() => { + return new ArrayValue( + Array.from(this.value.entries()).map(([key, value]) => new ArrayValue([new StringValue(key), value])) + ); + }) + ] + ]); +}; +var KeywordArgumentsValue = class extends ObjectValue { + type = "KeywordArgumentsValue"; +}; +var ArrayValue = class extends RuntimeValue { + type = "ArrayValue"; + builtins = /* @__PURE__ */ new Map([["length", new NumericValue(this.value.length)]]); + /** + * NOTE: necessary to override since all JavaScript arrays are considered truthy, + * while only non-empty Python arrays are consider truthy. + * + * e.g., + * - JavaScript: [] && 5 -> 5 + * - Python: [] and 5 -> [] + */ + __bool__() { + return new BooleanValue(this.value.length > 0); + } +}; +var TupleValue = class extends ArrayValue { + type = "TupleValue"; +}; +var FunctionValue = class extends RuntimeValue { + type = "FunctionValue"; +}; +var NullValue = class extends RuntimeValue { + type = "NullValue"; +}; +var UndefinedValue = class extends RuntimeValue { + type = "UndefinedValue"; +}; +var Environment = class { + constructor(parent) { + this.parent = parent; + } + /** + * The variables declared in this environment. + */ + variables = /* @__PURE__ */ new Map([ + [ + "namespace", + new FunctionValue((args) => { + if (args.length === 0) { + return new ObjectValue(/* @__PURE__ */ new Map()); + } + if (args.length !== 1 || !(args[0] instanceof ObjectValue)) { + throw new Error("`namespace` expects either zero arguments or a single object argument"); + } + return args[0]; + }) + ] + ]); + /** + * The tests available in this environment. + */ + tests = /* @__PURE__ */ new Map([ + ["boolean", (operand) => operand.type === "BooleanValue"], + ["callable", (operand) => operand instanceof FunctionValue], + [ + "odd", + (operand) => { + if (operand.type !== "NumericValue") { + throw new Error(`Cannot apply test "odd" to type: ${operand.type}`); + } + return operand.value % 2 !== 0; + } + ], + [ + "even", + (operand) => { + if (operand.type !== "NumericValue") { + throw new Error(`Cannot apply test "even" to type: ${operand.type}`); + } + return operand.value % 2 === 0; + } + ], + ["false", (operand) => operand.type === "BooleanValue" && !operand.value], + ["true", (operand) => operand.type === "BooleanValue" && operand.value], + ["none", (operand) => operand.type === "NullValue"], + ["string", (operand) => operand.type === "StringValue"], + ["number", (operand) => operand.type === "NumericValue"], + ["integer", (operand) => operand.type === "NumericValue" && Number.isInteger(operand.value)], + ["iterable", (operand) => operand.type === "ArrayValue" || operand.type === "StringValue"], + ["mapping", (operand) => operand.type === "ObjectValue"], + [ + "lower", + (operand) => { + const str = operand.value; + return operand.type === "StringValue" && str === str.toLowerCase(); + } + ], + [ + "upper", + (operand) => { + const str = operand.value; + return operand.type === "StringValue" && str === str.toUpperCase(); + } + ], + ["none", (operand) => operand.type === "NullValue"], + ["defined", (operand) => operand.type !== "UndefinedValue"], + ["undefined", (operand) => operand.type === "UndefinedValue"], + ["equalto", (a, b) => a.value === b.value], + ["eq", (a, b) => a.value === b.value] + ]); + /** + * Set the value of a variable in the current environment. + */ + set(name, value) { + return this.declareVariable(name, convertToRuntimeValues(value)); + } + declareVariable(name, value) { + if (this.variables.has(name)) { + throw new SyntaxError(`Variable already declared: ${name}`); + } + this.variables.set(name, value); + return value; + } + // private assignVariable(name: string, value: AnyRuntimeValue): AnyRuntimeValue { + // const env = this.resolve(name); + // env.variables.set(name, value); + // return value; + // } + /** + * Set variable in the current scope. + * See https://jinja.palletsprojects.com/en/3.0.x/templates/#assignments for more information. + */ + setVariable(name, value) { + this.variables.set(name, value); + return value; + } + /** + * Resolve the environment in which the variable is declared. + * @param {string} name The name of the variable. + * @returns {Environment} The environment in which the variable is declared. + */ + resolve(name) { + if (this.variables.has(name)) { + return this; + } + if (this.parent) { + return this.parent.resolve(name); + } + throw new Error(`Unknown variable: ${name}`); + } + lookupVariable(name) { + try { + return this.resolve(name).variables.get(name) ?? new UndefinedValue(); + } catch { + return new UndefinedValue(); + } + } +}; +var Interpreter = class { + global; + constructor(env) { + this.global = env ?? new Environment(); + } + /** + * Run the program. + */ + run(program) { + return this.evaluate(program, this.global); + } + /** + * Evaluates expressions following the binary operation type. + */ + evaluateBinaryExpression(node, environment) { + const left = this.evaluate(node.left, environment); + switch (node.operator.value) { + case "and": + return left.__bool__().value ? this.evaluate(node.right, environment) : left; + case "or": + return left.__bool__().value ? left : this.evaluate(node.right, environment); + } + const right = this.evaluate(node.right, environment); + switch (node.operator.value) { + case "==": + return new BooleanValue(left.value == right.value); + case "!=": + return new BooleanValue(left.value != right.value); + } + if (left instanceof UndefinedValue || right instanceof UndefinedValue) { + throw new Error("Cannot perform operation on undefined values"); + } else if (left instanceof NullValue || right instanceof NullValue) { + throw new Error("Cannot perform operation on null values"); + } else if (left instanceof NumericValue && right instanceof NumericValue) { + switch (node.operator.value) { + case "+": + return new NumericValue(left.value + right.value); + case "-": + return new NumericValue(left.value - right.value); + case "*": + return new NumericValue(left.value * right.value); + case "/": + return new NumericValue(left.value / right.value); + case "%": + return new NumericValue(left.value % right.value); + case "<": + return new BooleanValue(left.value < right.value); + case ">": + return new BooleanValue(left.value > right.value); + case ">=": + return new BooleanValue(left.value >= right.value); + case "<=": + return new BooleanValue(left.value <= right.value); + } + } else if (left instanceof ArrayValue && right instanceof ArrayValue) { + switch (node.operator.value) { + case "+": + return new ArrayValue(left.value.concat(right.value)); + } + } else if (right instanceof ArrayValue) { + const member = right.value.find((x) => x.value === left.value) !== void 0; + switch (node.operator.value) { + case "in": + return new BooleanValue(member); + case "not in": + return new BooleanValue(!member); + } + } + if (left instanceof StringValue || right instanceof StringValue) { + switch (node.operator.value) { + case "+": + return new StringValue(left.value.toString() + right.value.toString()); + } + } + if (left instanceof StringValue && right instanceof StringValue) { + switch (node.operator.value) { + case "in": + return new BooleanValue(right.value.includes(left.value)); + case "not in": + return new BooleanValue(!right.value.includes(left.value)); + } + } + if (left instanceof StringValue && right instanceof ObjectValue) { + switch (node.operator.value) { + case "in": + return new BooleanValue(right.value.has(left.value)); + case "not in": + return new BooleanValue(!right.value.has(left.value)); + } + } + throw new SyntaxError(`Unknown operator "${node.operator.value}" between ${left.type} and ${right.type}`); + } + evaluateArguments(args, environment) { + const positionalArguments = []; + const keywordArguments = /* @__PURE__ */ new Map(); + for (const argument of args) { + if (argument.type === "KeywordArgumentExpression") { + const kwarg = argument; + keywordArguments.set(kwarg.key.value, this.evaluate(kwarg.value, environment)); + } else { + if (keywordArguments.size > 0) { + throw new Error("Positional arguments must come before keyword arguments"); + } + positionalArguments.push(this.evaluate(argument, environment)); + } + } + return [positionalArguments, keywordArguments]; + } + /** + * Evaluates expressions following the filter operation type. + */ + evaluateFilterExpression(node, environment) { + const operand = this.evaluate(node.operand, environment); + if (node.filter.type === "Identifier") { + const filter = node.filter; + if (filter.value === "tojson") { + return new StringValue(toJSON(operand)); + } + if (operand instanceof ArrayValue) { + switch (filter.value) { + case "list": + return operand; + case "first": + return operand.value[0]; + case "last": + return operand.value[operand.value.length - 1]; + case "length": + return new NumericValue(operand.value.length); + case "reverse": + return new ArrayValue(operand.value.reverse()); + case "sort": + return new ArrayValue( + operand.value.sort((a, b) => { + if (a.type !== b.type) { + throw new Error(`Cannot compare different types: ${a.type} and ${b.type}`); + } + switch (a.type) { + case "NumericValue": + return a.value - b.value; + case "StringValue": + return a.value.localeCompare(b.value); + default: + throw new Error(`Cannot compare type: ${a.type}`); + } + }) + ); + case "join": + return new StringValue(operand.value.map((x) => x.value).join("")); + case "string": + return new StringValue(toJSON(operand)); + default: + throw new Error(`Unknown ArrayValue filter: ${filter.value}`); + } + } else if (operand instanceof StringValue) { + switch (filter.value) { + case "length": + return new NumericValue(operand.value.length); + case "upper": + return new StringValue(operand.value.toUpperCase()); + case "lower": + return new StringValue(operand.value.toLowerCase()); + case "title": + return new StringValue(titleCase(operand.value)); + case "capitalize": + return new StringValue(operand.value.charAt(0).toUpperCase() + operand.value.slice(1)); + case "trim": + return new StringValue(operand.value.trim()); + case "indent": + return new StringValue( + operand.value.split("\n").map( + (x, i) => ( + // By default, don't indent the first line or empty lines + i === 0 || x.length === 0 ? x : " " + x + ) + ).join("\n") + ); + case "join": + case "string": + return operand; + default: + throw new Error(`Unknown StringValue filter: ${filter.value}`); + } + } else if (operand instanceof NumericValue) { + switch (filter.value) { + case "abs": + return new NumericValue(Math.abs(operand.value)); + default: + throw new Error(`Unknown NumericValue filter: ${filter.value}`); + } + } else if (operand instanceof ObjectValue) { + switch (filter.value) { + case "items": + return new ArrayValue( + Array.from(operand.value.entries()).map(([key, value]) => new ArrayValue([new StringValue(key), value])) + ); + case "length": + return new NumericValue(operand.value.size); + default: + throw new Error(`Unknown ObjectValue filter: ${filter.value}`); + } + } + throw new Error(`Cannot apply filter "${filter.value}" to type: ${operand.type}`); + } else if (node.filter.type === "CallExpression") { + const filter = node.filter; + if (filter.callee.type !== "Identifier") { + throw new Error(`Unknown filter: ${filter.callee.type}`); + } + const filterName = filter.callee.value; + if (filterName === "tojson") { + const [, kwargs] = this.evaluateArguments(filter.args, environment); + const indent = kwargs.get("indent") ?? new NullValue(); + if (!(indent instanceof NumericValue || indent instanceof NullValue)) { + throw new Error("If set, indent must be a number"); + } + return new StringValue(toJSON(operand, indent.value)); + } else if (filterName === "join") { + let value; + if (operand instanceof StringValue) { + value = Array.from(operand.value); + } else if (operand instanceof ArrayValue) { + value = operand.value.map((x) => x.value); + } else { + throw new Error(`Cannot apply filter "${filterName}" to type: ${operand.type}`); + } + const [args, kwargs] = this.evaluateArguments(filter.args, environment); + const separator = args.at(0) ?? kwargs.get("separator") ?? new StringValue(""); + if (!(separator instanceof StringValue)) { + throw new Error("separator must be a string"); + } + return new StringValue(value.join(separator.value)); + } + if (operand instanceof ArrayValue) { + switch (filterName) { + case "selectattr": + case "rejectattr": { + const select = filterName === "selectattr"; + if (operand.value.some((x) => !(x instanceof ObjectValue))) { + throw new Error(`\`${filterName}\` can only be applied to array of objects`); + } + if (filter.args.some((x) => x.type !== "StringLiteral")) { + throw new Error(`arguments of \`${filterName}\` must be strings`); + } + const [attr, testName, value] = filter.args.map((x) => this.evaluate(x, environment)); + let testFunction; + if (testName) { + const test = environment.tests.get(testName.value); + if (!test) { + throw new Error(`Unknown test: ${testName.value}`); + } + testFunction = test; + } else { + testFunction = (...x) => x[0].__bool__().value; + } + const filtered = operand.value.filter((item) => { + const a = item.value.get(attr.value); + const result = a ? testFunction(a, value) : false; + return select ? result : !result; + }); + return new ArrayValue(filtered); + } + case "map": { + const [, kwargs] = this.evaluateArguments(filter.args, environment); + if (kwargs.has("attribute")) { + const attr = kwargs.get("attribute"); + if (!(attr instanceof StringValue)) { + throw new Error("attribute must be a string"); + } + const defaultValue = kwargs.get("default"); + const mapped = operand.value.map((item) => { + if (!(item instanceof ObjectValue)) { + throw new Error("items in map must be an object"); + } + return item.value.get(attr.value) ?? defaultValue ?? new UndefinedValue(); + }); + return new ArrayValue(mapped); + } else { + throw new Error("`map` expressions without `attribute` set are not currently supported."); + } + } + } + throw new Error(`Unknown ArrayValue filter: ${filterName}`); + } else if (operand instanceof StringValue) { + switch (filterName) { + case "indent": { + const [args, kwargs] = this.evaluateArguments(filter.args, environment); + const width = args.at(0) ?? kwargs.get("width") ?? new NumericValue(4); + if (!(width instanceof NumericValue)) { + throw new Error("width must be a number"); + } + const first = args.at(1) ?? kwargs.get("first") ?? new BooleanValue(false); + const blank = args.at(2) ?? kwargs.get("blank") ?? new BooleanValue(false); + const lines = operand.value.split("\n"); + const indent = " ".repeat(width.value); + const indented = lines.map( + (x, i) => !first.value && i === 0 || !blank.value && x.length === 0 ? x : indent + x + ); + return new StringValue(indented.join("\n")); + } + } + throw new Error(`Unknown StringValue filter: ${filterName}`); + } else { + throw new Error(`Cannot apply filter "${filterName}" to type: ${operand.type}`); + } + } + throw new Error(`Unknown filter: ${node.filter.type}`); + } + /** + * Evaluates expressions following the test operation type. + */ + evaluateTestExpression(node, environment) { + const operand = this.evaluate(node.operand, environment); + const test = environment.tests.get(node.test.value); + if (!test) { + throw new Error(`Unknown test: ${node.test.value}`); + } + const result = test(operand); + return new BooleanValue(node.negate ? !result : result); + } + /** + * Evaluates expressions following the unary operation type. + */ + evaluateUnaryExpression(node, environment) { + const argument = this.evaluate(node.argument, environment); + switch (node.operator.value) { + case "not": + return new BooleanValue(!argument.value); + default: + throw new SyntaxError(`Unknown operator: ${node.operator.value}`); + } + } + evalProgram(program, environment) { + return this.evaluateBlock(program.body, environment); + } + evaluateBlock(statements, environment) { + let result = ""; + for (const statement of statements) { + const lastEvaluated = this.evaluate(statement, environment); + if (lastEvaluated.type !== "NullValue" && lastEvaluated.type !== "UndefinedValue") { + result += lastEvaluated.value; + } + } + return new StringValue(result); + } + evaluateIdentifier(node, environment) { + return environment.lookupVariable(node.value); + } + evaluateCallExpression(expr, environment) { + const [args, kwargs] = this.evaluateArguments(expr.args, environment); + if (kwargs.size > 0) { + args.push(new KeywordArgumentsValue(kwargs)); + } + const fn = this.evaluate(expr.callee, environment); + if (fn.type !== "FunctionValue") { + throw new Error(`Cannot call something that is not a function: got ${fn.type}`); + } + return fn.value(args, environment); + } + evaluateSliceExpression(object, expr, environment) { + if (!(object instanceof ArrayValue || object instanceof StringValue)) { + throw new Error("Slice object must be an array or string"); + } + const start = this.evaluate(expr.start, environment); + const stop = this.evaluate(expr.stop, environment); + const step = this.evaluate(expr.step, environment); + if (!(start instanceof NumericValue || start instanceof UndefinedValue)) { + throw new Error("Slice start must be numeric or undefined"); + } + if (!(stop instanceof NumericValue || stop instanceof UndefinedValue)) { + throw new Error("Slice stop must be numeric or undefined"); + } + if (!(step instanceof NumericValue || step instanceof UndefinedValue)) { + throw new Error("Slice step must be numeric or undefined"); + } + if (object instanceof ArrayValue) { + return new ArrayValue(slice(object.value, start.value, stop.value, step.value)); + } else { + return new StringValue(slice(Array.from(object.value), start.value, stop.value, step.value).join("")); + } + } + evaluateMemberExpression(expr, environment) { + const object = this.evaluate(expr.object, environment); + let property; + if (expr.computed) { + if (expr.property.type === "SliceExpression") { + return this.evaluateSliceExpression(object, expr.property, environment); + } else { + property = this.evaluate(expr.property, environment); + } + } else { + property = new StringValue(expr.property.value); + } + let value; + if (object instanceof ObjectValue) { + if (!(property instanceof StringValue)) { + throw new Error(`Cannot access property with non-string: got ${property.type}`); + } + value = object.value.get(property.value) ?? object.builtins.get(property.value); + } else if (object instanceof ArrayValue || object instanceof StringValue) { + if (property instanceof NumericValue) { + value = object.value.at(property.value); + if (object instanceof StringValue) { + value = new StringValue(object.value.at(property.value)); + } + } else if (property instanceof StringValue) { + value = object.builtins.get(property.value); + } else { + throw new Error(`Cannot access property with non-string/non-number: got ${property.type}`); + } + } else { + if (!(property instanceof StringValue)) { + throw new Error(`Cannot access property with non-string: got ${property.type}`); + } + value = object.builtins.get(property.value); + } + return value instanceof RuntimeValue ? value : new UndefinedValue(); + } + evaluateSet(node, environment) { + const rhs = node.value ? this.evaluate(node.value, environment) : this.evaluateBlock(node.body, environment); + if (node.assignee.type === "Identifier") { + const variableName = node.assignee.value; + environment.setVariable(variableName, rhs); + } else if (node.assignee.type === "MemberExpression") { + const member = node.assignee; + const object = this.evaluate(member.object, environment); + if (!(object instanceof ObjectValue)) { + throw new Error("Cannot assign to member of non-object"); + } + if (member.property.type !== "Identifier") { + throw new Error("Cannot assign to member with non-identifier property"); + } + object.value.set(member.property.value, rhs); + } else { + throw new Error(`Invalid LHS inside assignment expression: ${JSON.stringify(node.assignee)}`); + } + return new NullValue(); + } + evaluateIf(node, environment) { + const test = this.evaluate(node.test, environment); + return this.evaluateBlock(test.__bool__().value ? node.body : node.alternate, environment); + } + evaluateFor(node, environment) { + const scope = new Environment(environment); + let test, iterable; + if (node.iterable.type === "SelectExpression") { + const select = node.iterable; + iterable = this.evaluate(select.iterable, scope); + test = select.test; + } else { + iterable = this.evaluate(node.iterable, scope); + } + if (!(iterable instanceof ArrayValue)) { + throw new Error(`Expected iterable type in for loop: got ${iterable.type}`); + } + const items = []; + const scopeUpdateFunctions = []; + for (let i = 0; i < iterable.value.length; ++i) { + const loopScope = new Environment(scope); + const current = iterable.value[i]; + let scopeUpdateFunction; + if (node.loopvar.type === "Identifier") { + scopeUpdateFunction = (scope2) => scope2.setVariable(node.loopvar.value, current); + } else if (node.loopvar.type === "TupleLiteral") { + const loopvar = node.loopvar; + if (current.type !== "ArrayValue") { + throw new Error(`Cannot unpack non-iterable type: ${current.type}`); + } + const c = current; + if (loopvar.value.length !== c.value.length) { + throw new Error(`Too ${loopvar.value.length > c.value.length ? "few" : "many"} items to unpack`); + } + scopeUpdateFunction = (scope2) => { + for (let j = 0; j < loopvar.value.length; ++j) { + if (loopvar.value[j].type !== "Identifier") { + throw new Error(`Cannot unpack non-identifier type: ${loopvar.value[j].type}`); + } + scope2.setVariable(loopvar.value[j].value, c.value[j]); + } + }; + } else { + throw new Error(`Invalid loop variable(s): ${node.loopvar.type}`); + } + if (test) { + scopeUpdateFunction(loopScope); + const testValue = this.evaluate(test, loopScope); + if (!testValue.__bool__().value) { + continue; + } + } + items.push(current); + scopeUpdateFunctions.push(scopeUpdateFunction); + } + let result = ""; + let noIteration = true; + for (let i = 0; i < items.length; ++i) { + const loop = /* @__PURE__ */ new Map([ + ["index", new NumericValue(i + 1)], + ["index0", new NumericValue(i)], + ["revindex", new NumericValue(items.length - i)], + ["revindex0", new NumericValue(items.length - i - 1)], + ["first", new BooleanValue(i === 0)], + ["last", new BooleanValue(i === items.length - 1)], + ["length", new NumericValue(items.length)], + ["previtem", i > 0 ? items[i - 1] : new UndefinedValue()], + ["nextitem", i < items.length - 1 ? items[i + 1] : new UndefinedValue()] + ]); + scope.setVariable("loop", new ObjectValue(loop)); + scopeUpdateFunctions[i](scope); + try { + const evaluated = this.evaluateBlock(node.body, scope); + result += evaluated.value; + } catch (err) { + if (err instanceof ContinueControl) { + continue; + } + if (err instanceof BreakControl) { + break; + } + throw err; + } + noIteration = false; + } + if (noIteration) { + const defaultEvaluated = this.evaluateBlock(node.defaultBlock, scope); + result += defaultEvaluated.value; + } + return new StringValue(result); + } + /** + * See https://jinja.palletsprojects.com/en/3.1.x/templates/#macros for more information. + */ + evaluateMacro(node, environment) { + environment.setVariable( + node.name.value, + new FunctionValue((args, scope) => { + const macroScope = new Environment(scope); + args = args.slice(); + let kwargs; + if (args.at(-1)?.type === "KeywordArgumentsValue") { + kwargs = args.pop(); + } + for (let i = 0; i < node.args.length; ++i) { + const nodeArg = node.args[i]; + const passedArg = args[i]; + if (nodeArg.type === "Identifier") { + const identifier = nodeArg; + if (!passedArg) { + throw new Error(`Missing positional argument: ${identifier.value}`); + } + macroScope.setVariable(identifier.value, passedArg); + } else if (nodeArg.type === "KeywordArgumentExpression") { + const kwarg = nodeArg; + const value = passedArg ?? // Try positional arguments first + kwargs?.value.get(kwarg.key.value) ?? // Look in user-passed kwargs + this.evaluate(kwarg.value, macroScope); + macroScope.setVariable(kwarg.key.value, value); + } else { + throw new Error(`Unknown argument type: ${nodeArg.type}`); + } + } + return this.evaluateBlock(node.body, macroScope); + }) + ); + return new NullValue(); + } + evaluate(statement, environment) { + if (statement === void 0) + return new UndefinedValue(); + switch (statement.type) { + case "Program": + return this.evalProgram(statement, environment); + case "Set": + return this.evaluateSet(statement, environment); + case "If": + return this.evaluateIf(statement, environment); + case "For": + return this.evaluateFor(statement, environment); + case "Macro": + return this.evaluateMacro(statement, environment); + case "Break": + throw new BreakControl(); + case "Continue": + throw new ContinueControl(); + case "NumericLiteral": + return new NumericValue(Number(statement.value)); + case "StringLiteral": + return new StringValue(statement.value); + case "BooleanLiteral": + return new BooleanValue(statement.value); + case "NullLiteral": + return new NullValue(statement.value); + case "ArrayLiteral": + return new ArrayValue(statement.value.map((x) => this.evaluate(x, environment))); + case "TupleLiteral": + return new TupleValue(statement.value.map((x) => this.evaluate(x, environment))); + case "ObjectLiteral": { + const mapping = /* @__PURE__ */ new Map(); + for (const [key, value] of statement.value) { + const evaluatedKey = this.evaluate(key, environment); + if (!(evaluatedKey instanceof StringValue)) { + throw new Error(`Object keys must be strings: got ${evaluatedKey.type}`); + } + mapping.set(evaluatedKey.value, this.evaluate(value, environment)); + } + return new ObjectValue(mapping); + } + case "Identifier": + return this.evaluateIdentifier(statement, environment); + case "CallExpression": + return this.evaluateCallExpression(statement, environment); + case "MemberExpression": + return this.evaluateMemberExpression(statement, environment); + case "UnaryExpression": + return this.evaluateUnaryExpression(statement, environment); + case "BinaryExpression": + return this.evaluateBinaryExpression(statement, environment); + case "FilterExpression": + return this.evaluateFilterExpression(statement, environment); + case "TestExpression": + return this.evaluateTestExpression(statement, environment); + default: + throw new SyntaxError(`Unknown node type: ${statement.type}`); + } + } +}; +function convertToRuntimeValues(input) { + switch (typeof input) { + case "number": + return new NumericValue(input); + case "string": + return new StringValue(input); + case "boolean": + return new BooleanValue(input); + case "undefined": + return new UndefinedValue(); + case "object": + if (input === null) { + return new NullValue(); + } else if (Array.isArray(input)) { + return new ArrayValue(input.map(convertToRuntimeValues)); + } else { + return new ObjectValue( + new Map(Object.entries(input).map(([key, value]) => [key, convertToRuntimeValues(value)])) + ); + } + case "function": + return new FunctionValue((args, _scope) => { + const result = input(...args.map((x) => x.value)) ?? null; + return convertToRuntimeValues(result); + }); + default: + throw new Error(`Cannot convert to runtime value: ${input}`); + } +} +function toJSON(input, indent, depth) { + const currentDepth = depth ?? 0; + switch (input.type) { + case "NullValue": + case "UndefinedValue": + return "null"; + case "NumericValue": + case "StringValue": + case "BooleanValue": + return JSON.stringify(input.value); + case "ArrayValue": + case "ObjectValue": { + const indentValue = indent ? " ".repeat(indent) : ""; + const basePadding = "\n" + indentValue.repeat(currentDepth); + const childrenPadding = basePadding + indentValue; + if (input.type === "ArrayValue") { + const core = input.value.map((x) => toJSON(x, indent, currentDepth + 1)); + return indent ? `[${childrenPadding}${core.join(`,${childrenPadding}`)}${basePadding}]` : `[${core.join(", ")}]`; + } else { + const core = Array.from(input.value.entries()).map(([key, value]) => { + const v = `"${key}": ${toJSON(value, indent, currentDepth + 1)}`; + return indent ? `${childrenPadding}${v}` : v; + }); + return indent ? `{${core.join(",")}${basePadding}}` : `{${core.join(", ")}}`; + } + } + default: + throw new Error(`Cannot convert to JSON: ${input.type}`); + } +} + +// src/format.ts +var NEWLINE = "\n"; +var OPEN_STATEMENT = "{%- "; +var CLOSE_STATEMENT = " -%}"; +var OPERATOR_PRECEDENCE = { + MultiplicativeBinaryOperator: 2, + AdditiveBinaryOperator: 1, + ComparisonBinaryOperator: 0 +}; +function format(program, indent = " ") { + const indentStr = typeof indent === "number" ? " ".repeat(indent) : indent; + const body = formatStatements(program.body, 0, indentStr); + return body.replace(/\n$/, ""); +} +function createStatement(...text) { + return OPEN_STATEMENT + text.join(" ") + CLOSE_STATEMENT; +} +function formatStatements(stmts, depth, indentStr) { + return stmts.map((stmt) => formatStatement(stmt, depth, indentStr)).join(NEWLINE); +} +function formatStatement(node, depth, indentStr) { + const pad = indentStr.repeat(depth); + switch (node.type) { + case "Program": + return formatStatements(node.body, depth, indentStr); + case "If": + return formatIf(node, depth, indentStr); + case "For": + return formatFor(node, depth, indentStr); + case "Set": + return formatSet(node, depth, indentStr); + case "Macro": + return formatMacro(node, depth, indentStr); + case "Break": + return pad + createStatement("break"); + case "Continue": + return pad + createStatement("continue"); + default: + return pad + "{{- " + formatExpression(node) + " -}}"; + } +} +function formatIf(node, depth, indentStr) { + const pad = indentStr.repeat(depth); + const clauses = []; + let current = node; + while (current) { + clauses.push({ test: current.test, body: current.body }); + if (current.alternate.length === 1 && current.alternate[0].type === "If") { + current = current.alternate[0]; + } else { + break; + } + } + let out = pad + createStatement("if", formatExpression(clauses[0].test)) + NEWLINE + formatStatements(clauses[0].body, depth + 1, indentStr); + for (let i = 1; i < clauses.length; i++) { + out += NEWLINE + pad + createStatement("elif", formatExpression(clauses[i].test)) + NEWLINE + formatStatements(clauses[i].body, depth + 1, indentStr); + } + if (current && current.alternate.length > 0) { + out += NEWLINE + pad + createStatement("else") + NEWLINE + formatStatements(current.alternate, depth + 1, indentStr); + } + out += NEWLINE + pad + createStatement("endif"); + return out; +} +function formatFor(node, depth, indentStr) { + const pad = indentStr.repeat(depth); + let formattedIterable = ""; + if (node.iterable.type === "SelectExpression") { + const n = node.iterable; + formattedIterable = `${formatExpression(n.iterable)} if ${formatExpression(n.test)}`; + } else { + formattedIterable = formatExpression(node.iterable); + } + let out = pad + createStatement("for", formatExpression(node.loopvar), "in", formattedIterable) + NEWLINE + formatStatements(node.body, depth + 1, indentStr); + if (node.defaultBlock.length > 0) { + out += NEWLINE + pad + createStatement("else") + NEWLINE + formatStatements(node.defaultBlock, depth + 1, indentStr); + } + out += NEWLINE + pad + createStatement("endfor"); + return out; +} +function formatSet(node, depth, indentStr) { + const pad = indentStr.repeat(depth); + const left = formatExpression(node.assignee); + const right = node.value ? formatExpression(node.value) : ""; + const value = pad + createStatement("set", `${left}${node.value ? " = " + right : ""}`); + if (node.body.length === 0) { + return value; + } + return value + NEWLINE + formatStatements(node.body, depth + 1, indentStr) + NEWLINE + pad + createStatement("endset"); +} +function formatMacro(node, depth, indentStr) { + const pad = indentStr.repeat(depth); + const args = node.args.map(formatExpression).join(", "); + return pad + createStatement("macro", `${node.name.value}(${args})`) + NEWLINE + formatStatements(node.body, depth + 1, indentStr) + NEWLINE + pad + createStatement("endmacro"); +} +function formatExpression(node, parentPrec = -1) { + switch (node.type) { + case "Identifier": + return node.value; + case "NullLiteral": + return "none"; + case "NumericLiteral": + case "BooleanLiteral": + return `${node.value}`; + case "StringLiteral": + return JSON.stringify(node.value); + case "BinaryExpression": { + const n = node; + const thisPrecedence = OPERATOR_PRECEDENCE[n.operator.type] ?? 0; + const left = formatExpression(n.left, thisPrecedence); + const right = formatExpression(n.right, thisPrecedence + 1); + const expr = `${left} ${n.operator.value} ${right}`; + return thisPrecedence < parentPrec ? `(${expr})` : expr; + } + case "UnaryExpression": { + const n = node; + const val = n.operator.value + (n.operator.value === "not" ? " " : "") + formatExpression(n.argument, Infinity); + return val; + } + case "LogicalNegationExpression": + return `not ${formatExpression(node.argument, Infinity)}`; + case "CallExpression": { + const n = node; + const args = n.args.map((a) => formatExpression(a, -1)).join(", "); + return `${formatExpression(n.callee, -1)}(${args})`; + } + case "MemberExpression": { + const n = node; + let obj = formatExpression(n.object, -1); + if (n.object.type !== "Identifier") { + obj = `(${obj})`; + } + let prop = formatExpression(n.property, -1); + if (!n.computed && n.property.type !== "Identifier") { + prop = `(${prop})`; + } + return n.computed ? `${obj}[${prop}]` : `${obj}.${prop}`; + } + case "FilterExpression": { + const n = node; + const operand = formatExpression(n.operand, Infinity); + if (n.filter.type === "CallExpression") { + return `${operand} | ${formatExpression(n.filter, -1)}`; + } + return `${operand} | ${n.filter.value}`; + } + case "SelectExpression": { + const n = node; + return `${formatExpression(n.iterable, -1)} | select(${formatExpression(n.test, -1)})`; + } + case "TestExpression": { + const n = node; + return `${formatExpression(n.operand, -1)} is${n.negate ? " not" : ""} ${n.test.value}`; + } + case "ArrayLiteral": + case "TupleLiteral": { + const elems = node.value.map((e) => formatExpression(e, -1)); + const brackets = node.type === "ArrayLiteral" ? "[]" : "()"; + return `${brackets[0]}${elems.join(", ")}${brackets[1]}`; + } + case "ObjectLiteral": { + const entries = Array.from(node.value.entries()).map( + ([k, v]) => `${formatExpression(k, -1)}: ${formatExpression(v, -1)}` + ); + return `{ ${entries.join(", ")} }`; + } + case "SliceExpression": { + const n = node; + const s = n.start ? formatExpression(n.start, -1) : ""; + const t = n.stop ? formatExpression(n.stop, -1) : ""; + const st = n.step ? `:${formatExpression(n.step, -1)}` : ""; + return `${s}:${t}${st}`; + } + case "KeywordArgumentExpression": { + const n = node; + return `${n.key.value}=${formatExpression(n.value, -1)}`; + } + case "If": { + const n = node; + const test = formatExpression(n.test, -1); + const body = formatExpression(n.body[0], 0); + const alternate = formatExpression(n.alternate[0], -1); + return `${body} if ${test} else ${alternate}`; + } + default: + throw new Error(`Unknown expression type: ${node.type}`); + } +} + +// src/index.ts +var Template = class { + parsed; + /** + * @param {string} template The template string + */ + constructor(template) { + const tokens = tokenize(template, { + lstrip_blocks: true, + trim_blocks: true + }); + this.parsed = parse(tokens); + } + render(items) { + const env = new Environment(); + env.set("false", false); + env.set("true", true); + env.set("raise_exception", (args) => { + throw new Error(args); + }); + env.set("range", range); + if (items) { + for (const [key, value] of Object.entries(items)) { + env.set(key, value); + } + } + const interpreter = new Interpreter(env); + const result = interpreter.run(this.parsed); + return result.value; + } + format(options) { + return format(this.parsed, options?.indent || " "); + } +}; + + + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/backend-impl.js": +/*!******************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/backend-impl.js ***! + \******************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ registerBackend: () => (/* binding */ registerBackend), +/* harmony export */ resolveBackendAndExecutionProviders: () => (/* binding */ resolveBackendAndExecutionProviders) +/* harmony export */ }); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +const backends = new Map(); +const backendsSortedByPriority = []; +/** + * Register a backend. + * + * @param name - the name as a key to lookup as an execution provider. + * @param backend - the backend object. + * @param priority - an integer indicating the priority of the backend. Higher number means higher priority. if priority + * < 0, it will be considered as a 'beta' version and will not be used as a fallback backend by default. + * + * @ignore + */ +const registerBackend = (name, backend, priority) => { + if (backend && typeof backend.init === 'function' && typeof backend.createInferenceSessionHandler === 'function') { + const currentBackend = backends.get(name); + if (currentBackend === undefined) { + backends.set(name, { backend, priority }); + } + else if (currentBackend.priority > priority) { + // same name is already registered with a higher priority. skip registeration. + return; + } + else if (currentBackend.priority === priority) { + if (currentBackend.backend !== backend) { + throw new Error(`cannot register backend "${name}" using priority ${priority}`); + } + } + if (priority >= 0) { + const i = backendsSortedByPriority.indexOf(name); + if (i !== -1) { + backendsSortedByPriority.splice(i, 1); + } + for (let i = 0; i < backendsSortedByPriority.length; i++) { + if (backends.get(backendsSortedByPriority[i]).priority <= priority) { + backendsSortedByPriority.splice(i, 0, name); + return; + } + } + backendsSortedByPriority.push(name); + } + return; + } + throw new TypeError('not a valid backend'); +}; +/** + * Try to resolve and initialize a backend. + * + * @param backendName - the name of the backend. + * @returns the backend instance if resolved and initialized successfully, or an error message if failed. + */ +const tryResolveAndInitializeBackend = async (backendName) => { + const backendInfo = backends.get(backendName); + if (!backendInfo) { + return 'backend not found.'; + } + if (backendInfo.initialized) { + return backendInfo.backend; + } + else if (backendInfo.aborted) { + return backendInfo.error; + } + else { + const isInitializing = !!backendInfo.initPromise; + try { + if (!isInitializing) { + backendInfo.initPromise = backendInfo.backend.init(backendName); + } + await backendInfo.initPromise; + backendInfo.initialized = true; + return backendInfo.backend; + } + catch (e) { + if (!isInitializing) { + backendInfo.error = `${e}`; + backendInfo.aborted = true; + } + return backendInfo.error; + } + finally { + delete backendInfo.initPromise; + } + } +}; +/** + * Resolve execution providers from the specific session options. + * + * @param options - the session options object. + * @returns a promise that resolves to a tuple of an initialized backend instance and a session options object with + * filtered EP list. + * + * @ignore + */ +const resolveBackendAndExecutionProviders = async (options) => { + // extract backend hints from session options + const eps = options.executionProviders || []; + const backendHints = eps.map((i) => (typeof i === 'string' ? i : i.name)); + const backendNames = backendHints.length === 0 ? backendsSortedByPriority : backendHints; + // try to resolve and initialize all requested backends + let backend; + const errors = []; + const availableBackendNames = new Set(); + for (const backendName of backendNames) { + const resolveResult = await tryResolveAndInitializeBackend(backendName); + if (typeof resolveResult === 'string') { + errors.push({ name: backendName, err: resolveResult }); + } + else { + if (!backend) { + backend = resolveResult; + } + if (backend === resolveResult) { + availableBackendNames.add(backendName); + } + } + } + // if no backend is available, throw error. + if (!backend) { + throw new Error(`no available backend found. ERR: ${errors.map((e) => `[${e.name}] ${e.err}`).join(', ')}`); + } + // for each explicitly requested backend, if it's not available, output warning message. + for (const { name, err } of errors) { + if (backendHints.includes(name)) { + // eslint-disable-next-line no-console + console.warn(`removing requested execution provider "${name}" from session options because it is not available: ${err}`); + } + } + const filteredEps = eps.filter((i) => availableBackendNames.has(typeof i === 'string' ? i : i.name)); + return [ + backend, + new Proxy(options, { + get: (target, prop) => { + if (prop === 'executionProviders') { + return filteredEps; + } + return Reflect.get(target, prop); + }, + }), + ]; +}; +//# sourceMappingURL=backend-impl.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/backend.js": +/*!*************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/backend.js ***! + \*************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ registerBackend: () => (/* reexport safe */ _backend_impl_js__WEBPACK_IMPORTED_MODULE_0__.registerBackend) +/* harmony export */ }); +/* harmony import */ var _backend_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./backend-impl.js */ "./node_modules/onnxruntime-common/dist/esm/backend-impl.js"); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +//# sourceMappingURL=backend.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/env-impl.js": +/*!**************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/env-impl.js ***! + \**************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ env: () => (/* binding */ env) +/* harmony export */ }); +/* harmony import */ var _version_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./version.js */ "./node_modules/onnxruntime-common/dist/esm/version.js"); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +let logLevelValue = 'warning'; +const env = { + wasm: {}, + webgl: {}, + webgpu: {}, + versions: { common: _version_js__WEBPACK_IMPORTED_MODULE_0__.version }, + set logLevel(value) { + if (value === undefined) { + return; + } + if (typeof value !== 'string' || ['verbose', 'info', 'warning', 'error', 'fatal'].indexOf(value) === -1) { + throw new Error(`Unsupported logging level: ${value}`); + } + logLevelValue = value; + }, + get logLevel() { + return logLevelValue; + }, +}; +// set property 'logLevel' so that they can be correctly transferred to worker by `postMessage()`. +Object.defineProperty(env, 'logLevel', { enumerable: true }); +//# sourceMappingURL=env-impl.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/env.js": +/*!*********************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/env.js ***! + \*********************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ env: () => (/* binding */ env) +/* harmony export */ }); +/* harmony import */ var _env_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./env-impl.js */ "./node_modules/onnxruntime-common/dist/esm/env-impl.js"); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +/** + * Represent a set of flags as a global singleton. + */ +const env = _env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env; +//# sourceMappingURL=env.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/index.js": +/*!***********************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/index.js ***! + \***********************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ InferenceSession: () => (/* reexport safe */ _inference_session_js__WEBPACK_IMPORTED_MODULE_2__.InferenceSession), +/* harmony export */ TRACE: () => (/* reexport safe */ _trace_js__WEBPACK_IMPORTED_MODULE_6__.TRACE), +/* harmony export */ TRACE_FUNC_BEGIN: () => (/* reexport safe */ _trace_js__WEBPACK_IMPORTED_MODULE_6__.TRACE_FUNC_BEGIN), +/* harmony export */ TRACE_FUNC_END: () => (/* reexport safe */ _trace_js__WEBPACK_IMPORTED_MODULE_6__.TRACE_FUNC_END), +/* harmony export */ Tensor: () => (/* reexport safe */ _tensor_js__WEBPACK_IMPORTED_MODULE_3__.Tensor), +/* harmony export */ env: () => (/* reexport safe */ _env_js__WEBPACK_IMPORTED_MODULE_1__.env), +/* harmony export */ registerBackend: () => (/* reexport safe */ _backend_js__WEBPACK_IMPORTED_MODULE_0__.registerBackend) +/* harmony export */ }); +/* harmony import */ var _backend_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./backend.js */ "./node_modules/onnxruntime-common/dist/esm/backend.js"); +/* harmony import */ var _env_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./env.js */ "./node_modules/onnxruntime-common/dist/esm/env.js"); +/* harmony import */ var _inference_session_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./inference-session.js */ "./node_modules/onnxruntime-common/dist/esm/inference-session.js"); +/* harmony import */ var _tensor_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./tensor.js */ "./node_modules/onnxruntime-common/dist/esm/tensor.js"); +/* harmony import */ var _tensor_conversion_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ./tensor-conversion.js */ "./node_modules/onnxruntime-common/dist/esm/tensor-conversion.js"); +/* harmony import */ var _tensor_factory_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ./tensor-factory.js */ "./node_modules/onnxruntime-common/dist/esm/tensor-factory.js"); +/* harmony import */ var _trace_js__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! ./trace.js */ "./node_modules/onnxruntime-common/dist/esm/trace.js"); +/* harmony import */ var _onnx_model_js__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__(/*! ./onnx-model.js */ "./node_modules/onnxruntime-common/dist/esm/onnx-model.js"); +/* harmony import */ var _onnx_value_js__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__(/*! ./onnx-value.js */ "./node_modules/onnxruntime-common/dist/esm/onnx-value.js"); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +/** + * # ONNX Runtime JavaScript API + * + * ONNX Runtime JavaScript API is a unified API for all JavaScript usages, including the following NPM packages: + * + * - [onnxruntime-node](https://www.npmjs.com/package/onnxruntime-node) + * - [onnxruntime-web](https://www.npmjs.com/package/onnxruntime-web) + * - [onnxruntime-react-native](https://www.npmjs.com/package/onnxruntime-react-native) + * + * See also: + * - [Get Started](https://onnxruntime.ai/docs/get-started/with-javascript/) + * - [Inference examples](https://github.com/microsoft/onnxruntime-inference-examples/tree/main/js) + * + * @packageDocumentation + */ + + + + + + + + + +//# sourceMappingURL=index.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/inference-session-impl.js": +/*!****************************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/inference-session-impl.js ***! + \****************************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ InferenceSession: () => (/* binding */ InferenceSession) +/* harmony export */ }); +/* harmony import */ var _backend_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./backend-impl.js */ "./node_modules/onnxruntime-common/dist/esm/backend-impl.js"); +/* harmony import */ var _tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./tensor.js */ "./node_modules/onnxruntime-common/dist/esm/tensor.js"); +/* harmony import */ var _trace_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./trace.js */ "./node_modules/onnxruntime-common/dist/esm/trace.js"); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + + + +class InferenceSession { + constructor(handler) { + this.handler = handler; + } + async run(feeds, arg1, arg2) { + (0,_trace_js__WEBPACK_IMPORTED_MODULE_2__.TRACE_FUNC_BEGIN)(); + const fetches = {}; + let options = {}; + // check inputs + if (typeof feeds !== 'object' || feeds === null || feeds instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor || Array.isArray(feeds)) { + throw new TypeError("'feeds' must be an object that use input names as keys and OnnxValue as corresponding values."); + } + let isFetchesEmpty = true; + // determine which override is being used + if (typeof arg1 === 'object') { + if (arg1 === null) { + throw new TypeError('Unexpected argument[1]: cannot be null.'); + } + if (arg1 instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor) { + throw new TypeError("'fetches' cannot be a Tensor"); + } + if (Array.isArray(arg1)) { + if (arg1.length === 0) { + throw new TypeError("'fetches' cannot be an empty array."); + } + isFetchesEmpty = false; + // output names + for (const name of arg1) { + if (typeof name !== 'string') { + throw new TypeError("'fetches' must be a string array or an object."); + } + if (this.outputNames.indexOf(name) === -1) { + throw new RangeError(`'fetches' contains invalid output name: ${name}.`); + } + fetches[name] = null; + } + if (typeof arg2 === 'object' && arg2 !== null) { + options = arg2; + } + else if (typeof arg2 !== 'undefined') { + throw new TypeError("'options' must be an object."); + } + } + else { + // decide whether arg1 is fetches or options + // if any output name is present and its value is valid OnnxValue, we consider it fetches + let isFetches = false; + const arg1Keys = Object.getOwnPropertyNames(arg1); + for (const name of this.outputNames) { + if (arg1Keys.indexOf(name) !== -1) { + const v = arg1[name]; + if (v === null || v instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor) { + isFetches = true; + isFetchesEmpty = false; + fetches[name] = v; + } + } + } + if (isFetches) { + if (typeof arg2 === 'object' && arg2 !== null) { + options = arg2; + } + else if (typeof arg2 !== 'undefined') { + throw new TypeError("'options' must be an object."); + } + } + else { + options = arg1; + } + } + } + else if (typeof arg1 !== 'undefined') { + throw new TypeError("Unexpected argument[1]: must be 'fetches' or 'options'."); + } + // check if all inputs are in feed + for (const name of this.inputNames) { + if (typeof feeds[name] === 'undefined') { + throw new Error(`input '${name}' is missing in 'feeds'.`); + } + } + // if no fetches is specified, we use the full output names list + if (isFetchesEmpty) { + for (const name of this.outputNames) { + fetches[name] = null; + } + } + // feeds, fetches and options are prepared + const results = await this.handler.run(feeds, fetches, options); + const returnValue = {}; + for (const key in results) { + if (Object.hasOwnProperty.call(results, key)) { + const result = results[key]; + if (result instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor) { + returnValue[key] = result; + } + else { + returnValue[key] = new _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor(result.type, result.data, result.dims); + } + } + } + (0,_trace_js__WEBPACK_IMPORTED_MODULE_2__.TRACE_FUNC_END)(); + return returnValue; + } + async release() { + return this.handler.dispose(); + } + static async create(arg0, arg1, arg2, arg3) { + (0,_trace_js__WEBPACK_IMPORTED_MODULE_2__.TRACE_FUNC_BEGIN)(); + // either load from a file or buffer + let filePathOrUint8Array; + let options = {}; + if (typeof arg0 === 'string') { + filePathOrUint8Array = arg0; + if (typeof arg1 === 'object' && arg1 !== null) { + options = arg1; + } + else if (typeof arg1 !== 'undefined') { + throw new TypeError("'options' must be an object."); + } + } + else if (arg0 instanceof Uint8Array) { + filePathOrUint8Array = arg0; + if (typeof arg1 === 'object' && arg1 !== null) { + options = arg1; + } + else if (typeof arg1 !== 'undefined') { + throw new TypeError("'options' must be an object."); + } + } + else if (arg0 instanceof ArrayBuffer || + (typeof SharedArrayBuffer !== 'undefined' && arg0 instanceof SharedArrayBuffer)) { + const buffer = arg0; + let byteOffset = 0; + let byteLength = arg0.byteLength; + if (typeof arg1 === 'object' && arg1 !== null) { + options = arg1; + } + else if (typeof arg1 === 'number') { + byteOffset = arg1; + if (!Number.isSafeInteger(byteOffset)) { + throw new RangeError("'byteOffset' must be an integer."); + } + if (byteOffset < 0 || byteOffset >= buffer.byteLength) { + throw new RangeError(`'byteOffset' is out of range [0, ${buffer.byteLength}).`); + } + byteLength = arg0.byteLength - byteOffset; + if (typeof arg2 === 'number') { + byteLength = arg2; + if (!Number.isSafeInteger(byteLength)) { + throw new RangeError("'byteLength' must be an integer."); + } + if (byteLength <= 0 || byteOffset + byteLength > buffer.byteLength) { + throw new RangeError(`'byteLength' is out of range (0, ${buffer.byteLength - byteOffset}].`); + } + if (typeof arg3 === 'object' && arg3 !== null) { + options = arg3; + } + else if (typeof arg3 !== 'undefined') { + throw new TypeError("'options' must be an object."); + } + } + else if (typeof arg2 !== 'undefined') { + throw new TypeError("'byteLength' must be a number."); + } + } + else if (typeof arg1 !== 'undefined') { + throw new TypeError("'options' must be an object."); + } + filePathOrUint8Array = new Uint8Array(buffer, byteOffset, byteLength); + } + else { + throw new TypeError("Unexpected argument[0]: must be 'path' or 'buffer'."); + } + // resolve backend, update session options with validated EPs, and create session handler + const [backend, optionsWithValidatedEPs] = await (0,_backend_impl_js__WEBPACK_IMPORTED_MODULE_0__.resolveBackendAndExecutionProviders)(options); + const handler = await backend.createInferenceSessionHandler(filePathOrUint8Array, optionsWithValidatedEPs); + (0,_trace_js__WEBPACK_IMPORTED_MODULE_2__.TRACE_FUNC_END)(); + return new InferenceSession(handler); + } + startProfiling() { + this.handler.startProfiling(); + } + endProfiling() { + this.handler.endProfiling(); + } + get inputNames() { + return this.handler.inputNames; + } + get outputNames() { + return this.handler.outputNames; + } +} +//# sourceMappingURL=inference-session-impl.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/inference-session.js": +/*!***********************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/inference-session.js ***! + \***********************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ InferenceSession: () => (/* binding */ InferenceSession) +/* harmony export */ }); +/* harmony import */ var _inference_session_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./inference-session-impl.js */ "./node_modules/onnxruntime-common/dist/esm/inference-session-impl.js"); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// eslint-disable-next-line @typescript-eslint/naming-convention +const InferenceSession = _inference_session_impl_js__WEBPACK_IMPORTED_MODULE_0__.InferenceSession; +//# sourceMappingURL=inference-session.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/onnx-model.js": +/*!****************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/onnx-model.js ***! + \****************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +//# sourceMappingURL=onnx-model.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/onnx-value.js": +/*!****************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/onnx-value.js ***! + \****************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +//# sourceMappingURL=onnx-value.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/tensor-conversion-impl.js": +/*!****************************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/tensor-conversion-impl.js ***! + \****************************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ tensorToDataURL: () => (/* binding */ tensorToDataURL), +/* harmony export */ tensorToImageData: () => (/* binding */ tensorToImageData) +/* harmony export */ }); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +/** + * implementation of Tensor.toDataURL() + */ +const tensorToDataURL = (tensor, options) => { + const canvas = typeof document !== 'undefined' ? document.createElement('canvas') : new OffscreenCanvas(1, 1); + canvas.width = tensor.dims[3]; + canvas.height = tensor.dims[2]; + const pixels2DContext = canvas.getContext('2d'); + if (pixels2DContext != null) { + // Default values for height and width & format + let width; + let height; + if (options?.tensorLayout !== undefined && options.tensorLayout === 'NHWC') { + width = tensor.dims[2]; + height = tensor.dims[3]; + } + else { + // Default layout is NCWH + width = tensor.dims[3]; + height = tensor.dims[2]; + } + const inputformat = options?.format !== undefined ? options.format : 'RGB'; + const norm = options?.norm; + let normMean; + let normBias; + if (norm === undefined || norm.mean === undefined) { + normMean = [255, 255, 255, 255]; + } + else { + if (typeof norm.mean === 'number') { + normMean = [norm.mean, norm.mean, norm.mean, norm.mean]; + } + else { + normMean = [norm.mean[0], norm.mean[1], norm.mean[2], 0]; + if (norm.mean[3] !== undefined) { + normMean[3] = norm.mean[3]; + } + } + } + if (norm === undefined || norm.bias === undefined) { + normBias = [0, 0, 0, 0]; + } + else { + if (typeof norm.bias === 'number') { + normBias = [norm.bias, norm.bias, norm.bias, norm.bias]; + } + else { + normBias = [norm.bias[0], norm.bias[1], norm.bias[2], 0]; + if (norm.bias[3] !== undefined) { + normBias[3] = norm.bias[3]; + } + } + } + const stride = height * width; + // Default pointer assignments + let rTensorPointer = 0, gTensorPointer = stride, bTensorPointer = stride * 2, aTensorPointer = -1; + // Updating the pointer assignments based on the input image format + if (inputformat === 'RGBA') { + rTensorPointer = 0; + gTensorPointer = stride; + bTensorPointer = stride * 2; + aTensorPointer = stride * 3; + } + else if (inputformat === 'RGB') { + rTensorPointer = 0; + gTensorPointer = stride; + bTensorPointer = stride * 2; + } + else if (inputformat === 'RBG') { + rTensorPointer = 0; + bTensorPointer = stride; + gTensorPointer = stride * 2; + } + for (let i = 0; i < height; i++) { + for (let j = 0; j < width; j++) { + const R = (tensor.data[rTensorPointer++] - normBias[0]) * normMean[0]; // R value + const G = (tensor.data[gTensorPointer++] - normBias[1]) * normMean[1]; // G value + const B = (tensor.data[bTensorPointer++] - normBias[2]) * normMean[2]; // B value + const A = aTensorPointer === -1 ? 255 : (tensor.data[aTensorPointer++] - normBias[3]) * normMean[3]; // A value + // eslint-disable-next-line @typescript-eslint/restrict-plus-operands + pixels2DContext.fillStyle = 'rgba(' + R + ',' + G + ',' + B + ',' + A + ')'; + pixels2DContext.fillRect(j, i, 1, 1); + } + } + if ('toDataURL' in canvas) { + return canvas.toDataURL(); + } + else { + throw new Error('toDataURL is not supported'); + } + } + else { + throw new Error('Can not access image data'); + } +}; +/** + * implementation of Tensor.toImageData() + */ +const tensorToImageData = (tensor, options) => { + const pixels2DContext = typeof document !== 'undefined' + ? document.createElement('canvas').getContext('2d') + : new OffscreenCanvas(1, 1).getContext('2d'); + let image; + if (pixels2DContext != null) { + // Default values for height and width & format + let width; + let height; + let channels; + if (options?.tensorLayout !== undefined && options.tensorLayout === 'NHWC') { + width = tensor.dims[2]; + height = tensor.dims[1]; + channels = tensor.dims[3]; + } + else { + // Default layout is NCWH + width = tensor.dims[3]; + height = tensor.dims[2]; + channels = tensor.dims[1]; + } + const inputformat = options !== undefined ? (options.format !== undefined ? options.format : 'RGB') : 'RGB'; + const norm = options?.norm; + let normMean; + let normBias; + if (norm === undefined || norm.mean === undefined) { + normMean = [255, 255, 255, 255]; + } + else { + if (typeof norm.mean === 'number') { + normMean = [norm.mean, norm.mean, norm.mean, norm.mean]; + } + else { + normMean = [norm.mean[0], norm.mean[1], norm.mean[2], 255]; + if (norm.mean[3] !== undefined) { + normMean[3] = norm.mean[3]; + } + } + } + if (norm === undefined || norm.bias === undefined) { + normBias = [0, 0, 0, 0]; + } + else { + if (typeof norm.bias === 'number') { + normBias = [norm.bias, norm.bias, norm.bias, norm.bias]; + } + else { + normBias = [norm.bias[0], norm.bias[1], norm.bias[2], 0]; + if (norm.bias[3] !== undefined) { + normBias[3] = norm.bias[3]; + } + } + } + const stride = height * width; + if (options !== undefined) { + if ((options.format !== undefined && channels === 4 && options.format !== 'RGBA') || + (channels === 3 && options.format !== 'RGB' && options.format !== 'BGR')) { + throw new Error("Tensor format doesn't match input tensor dims"); + } + } + // Default pointer assignments + const step = 4; + let rImagePointer = 0, gImagePointer = 1, bImagePointer = 2, aImagePointer = 3; + let rTensorPointer = 0, gTensorPointer = stride, bTensorPointer = stride * 2, aTensorPointer = -1; + // Updating the pointer assignments based on the input image format + if (inputformat === 'RGBA') { + rTensorPointer = 0; + gTensorPointer = stride; + bTensorPointer = stride * 2; + aTensorPointer = stride * 3; + } + else if (inputformat === 'RGB') { + rTensorPointer = 0; + gTensorPointer = stride; + bTensorPointer = stride * 2; + } + else if (inputformat === 'RBG') { + rTensorPointer = 0; + bTensorPointer = stride; + gTensorPointer = stride * 2; + } + image = pixels2DContext.createImageData(width, height); + for (let i = 0; i < height * width; rImagePointer += step, gImagePointer += step, bImagePointer += step, aImagePointer += step, i++) { + image.data[rImagePointer] = (tensor.data[rTensorPointer++] - normBias[0]) * normMean[0]; // R value + image.data[gImagePointer] = (tensor.data[gTensorPointer++] - normBias[1]) * normMean[1]; // G value + image.data[bImagePointer] = (tensor.data[bTensorPointer++] - normBias[2]) * normMean[2]; // B value + image.data[aImagePointer] = + aTensorPointer === -1 ? 255 : (tensor.data[aTensorPointer++] - normBias[3]) * normMean[3]; // A value + } + } + else { + throw new Error('Can not access image data'); + } + return image; +}; +//# sourceMappingURL=tensor-conversion-impl.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/tensor-conversion.js": +/*!***********************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/tensor-conversion.js ***! + \***********************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +//# sourceMappingURL=tensor-conversion.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/tensor-factory-impl.js": +/*!*************************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/tensor-factory-impl.js ***! + \*************************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ bufferToTensor: () => (/* binding */ bufferToTensor), +/* harmony export */ tensorFromGpuBuffer: () => (/* binding */ tensorFromGpuBuffer), +/* harmony export */ tensorFromImage: () => (/* binding */ tensorFromImage), +/* harmony export */ tensorFromMLTensor: () => (/* binding */ tensorFromMLTensor), +/* harmony export */ tensorFromPinnedBuffer: () => (/* binding */ tensorFromPinnedBuffer), +/* harmony export */ tensorFromTexture: () => (/* binding */ tensorFromTexture) +/* harmony export */ }); +/* harmony import */ var _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./tensor-impl.js */ "./node_modules/onnxruntime-common/dist/esm/tensor-impl.js"); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +/** + * Create a new tensor object from image object + * + * @param buffer - Extracted image buffer data - assuming RGBA format + * @param imageFormat - input image configuration - required configurations height, width, format + * @param tensorFormat - output tensor configuration - Default is RGB format + */ +const bufferToTensor = (buffer, options) => { + if (buffer === undefined) { + throw new Error('Image buffer must be defined'); + } + if (options.height === undefined || options.width === undefined) { + throw new Error('Image height and width must be defined'); + } + if (options.tensorLayout === 'NHWC') { + throw new Error('NHWC Tensor layout is not supported yet'); + } + const { height, width } = options; + const norm = options.norm ?? { mean: 255, bias: 0 }; + let normMean; + let normBias; + if (typeof norm.mean === 'number') { + normMean = [norm.mean, norm.mean, norm.mean, norm.mean]; + } + else { + normMean = [norm.mean[0], norm.mean[1], norm.mean[2], norm.mean[3] ?? 255]; + } + if (typeof norm.bias === 'number') { + normBias = [norm.bias, norm.bias, norm.bias, norm.bias]; + } + else { + normBias = [norm.bias[0], norm.bias[1], norm.bias[2], norm.bias[3] ?? 0]; + } + const inputformat = options.format !== undefined ? options.format : 'RGBA'; + // default value is RGBA since imagedata and HTMLImageElement uses it + const outputformat = options.tensorFormat !== undefined ? (options.tensorFormat !== undefined ? options.tensorFormat : 'RGB') : 'RGB'; + const stride = height * width; + const float32Data = outputformat === 'RGBA' ? new Float32Array(stride * 4) : new Float32Array(stride * 3); + // Default pointer assignments + let step = 4, rImagePointer = 0, gImagePointer = 1, bImagePointer = 2, aImagePointer = 3; + let rTensorPointer = 0, gTensorPointer = stride, bTensorPointer = stride * 2, aTensorPointer = -1; + // Updating the pointer assignments based on the input image format + if (inputformat === 'RGB') { + step = 3; + rImagePointer = 0; + gImagePointer = 1; + bImagePointer = 2; + aImagePointer = -1; + } + // Updating the pointer assignments based on the output tensor format + if (outputformat === 'RGBA') { + aTensorPointer = stride * 3; + } + else if (outputformat === 'RBG') { + rTensorPointer = 0; + bTensorPointer = stride; + gTensorPointer = stride * 2; + } + else if (outputformat === 'BGR') { + bTensorPointer = 0; + gTensorPointer = stride; + rTensorPointer = stride * 2; + } + for (let i = 0; i < stride; i++, rImagePointer += step, bImagePointer += step, gImagePointer += step, aImagePointer += step) { + float32Data[rTensorPointer++] = (buffer[rImagePointer] + normBias[0]) / normMean[0]; + float32Data[gTensorPointer++] = (buffer[gImagePointer] + normBias[1]) / normMean[1]; + float32Data[bTensorPointer++] = (buffer[bImagePointer] + normBias[2]) / normMean[2]; + if (aTensorPointer !== -1 && aImagePointer !== -1) { + float32Data[aTensorPointer++] = (buffer[aImagePointer] + normBias[3]) / normMean[3]; + } + } + // Float32Array -> ort.Tensor + const outputTensor = outputformat === 'RGBA' + ? new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor('float32', float32Data, [1, 4, height, width]) + : new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor('float32', float32Data, [1, 3, height, width]); + return outputTensor; +}; +/** + * implementation of Tensor.fromImage(). + */ +const tensorFromImage = async (image, options) => { + // checking the type of image object + const isHTMLImageEle = typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement; + const isImageDataEle = typeof ImageData !== 'undefined' && image instanceof ImageData; + const isImageBitmap = typeof ImageBitmap !== 'undefined' && image instanceof ImageBitmap; + const isString = typeof image === 'string'; + let data; + let bufferToTensorOptions = options ?? {}; + const createCanvas = () => { + if (typeof document !== 'undefined') { + return document.createElement('canvas'); + } + else if (typeof OffscreenCanvas !== 'undefined') { + return new OffscreenCanvas(1, 1); + } + else { + throw new Error('Canvas is not supported'); + } + }; + const createCanvasContext = (canvas) => { + if (typeof HTMLCanvasElement !== 'undefined' && canvas instanceof HTMLCanvasElement) { + return canvas.getContext('2d'); + } + else if (canvas instanceof OffscreenCanvas) { + return canvas.getContext('2d'); + } + else { + return null; + } + }; + // filling and checking image configuration options + if (isHTMLImageEle) { + // HTMLImageElement - image object - format is RGBA by default + const canvas = createCanvas(); + canvas.width = image.width; + canvas.height = image.height; + const pixels2DContext = createCanvasContext(canvas); + if (pixels2DContext != null) { + let height = image.height; + let width = image.width; + if (options !== undefined && options.resizedHeight !== undefined && options.resizedWidth !== undefined) { + height = options.resizedHeight; + width = options.resizedWidth; + } + if (options !== undefined) { + bufferToTensorOptions = options; + if (options.tensorFormat !== undefined) { + throw new Error('Image input config format must be RGBA for HTMLImageElement'); + } + else { + bufferToTensorOptions.tensorFormat = 'RGBA'; + } + bufferToTensorOptions.height = height; + bufferToTensorOptions.width = width; + } + else { + bufferToTensorOptions.tensorFormat = 'RGBA'; + bufferToTensorOptions.height = height; + bufferToTensorOptions.width = width; + } + pixels2DContext.drawImage(image, 0, 0); + data = pixels2DContext.getImageData(0, 0, width, height).data; + } + else { + throw new Error('Can not access image data'); + } + } + else if (isImageDataEle) { + let height; + let width; + if (options !== undefined && options.resizedWidth !== undefined && options.resizedHeight !== undefined) { + height = options.resizedHeight; + width = options.resizedWidth; + } + else { + height = image.height; + width = image.width; + } + if (options !== undefined) { + bufferToTensorOptions = options; + } + bufferToTensorOptions.format = 'RGBA'; + bufferToTensorOptions.height = height; + bufferToTensorOptions.width = width; + if (options !== undefined) { + const tempCanvas = createCanvas(); + tempCanvas.width = width; + tempCanvas.height = height; + const pixels2DContext = createCanvasContext(tempCanvas); + if (pixels2DContext != null) { + pixels2DContext.putImageData(image, 0, 0); + data = pixels2DContext.getImageData(0, 0, width, height).data; + } + else { + throw new Error('Can not access image data'); + } + } + else { + data = image.data; + } + } + else if (isImageBitmap) { + // ImageBitmap - image object - format must be provided by user + if (options === undefined) { + throw new Error('Please provide image config with format for Imagebitmap'); + } + const canvas = createCanvas(); + canvas.width = image.width; + canvas.height = image.height; + const pixels2DContext = createCanvasContext(canvas); + if (pixels2DContext != null) { + const height = image.height; + const width = image.width; + pixels2DContext.drawImage(image, 0, 0, width, height); + data = pixels2DContext.getImageData(0, 0, width, height).data; + bufferToTensorOptions.height = height; + bufferToTensorOptions.width = width; + return bufferToTensor(data, bufferToTensorOptions); + } + else { + throw new Error('Can not access image data'); + } + } + else if (isString) { + return new Promise((resolve, reject) => { + const canvas = createCanvas(); + const context = createCanvasContext(canvas); + if (!image || !context) { + return reject(); + } + const newImage = new Image(); + newImage.crossOrigin = 'Anonymous'; + newImage.src = image; + newImage.onload = () => { + canvas.width = newImage.width; + canvas.height = newImage.height; + context.drawImage(newImage, 0, 0, canvas.width, canvas.height); + const img = context.getImageData(0, 0, canvas.width, canvas.height); + bufferToTensorOptions.height = canvas.height; + bufferToTensorOptions.width = canvas.width; + resolve(bufferToTensor(img.data, bufferToTensorOptions)); + }; + }); + } + else { + throw new Error('Input data provided is not supported - aborted tensor creation'); + } + if (data !== undefined) { + return bufferToTensor(data, bufferToTensorOptions); + } + else { + throw new Error('Input data provided is not supported - aborted tensor creation'); + } +}; +/** + * implementation of Tensor.fromTexture(). + */ +const tensorFromTexture = (texture, options) => { + const { width, height, download, dispose } = options; + // Always assume RGBAF32. TODO: support different texture format + const dims = [1, height, width, 4]; + return new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor({ location: 'texture', type: 'float32', texture, dims, download, dispose }); +}; +/** + * implementation of Tensor.fromGpuBuffer(). + */ +const tensorFromGpuBuffer = (gpuBuffer, options) => { + const { dataType, dims, download, dispose } = options; + return new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor({ location: 'gpu-buffer', type: dataType ?? 'float32', gpuBuffer, dims, download, dispose }); +}; +/** + * implementation of Tensor.fromMLTensor(). + */ +const tensorFromMLTensor = (mlTensor, options) => { + const { dataType, dims, download, dispose } = options; + return new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor({ location: 'ml-tensor', type: dataType ?? 'float32', mlTensor, dims, download, dispose }); +}; +/** + * implementation of Tensor.fromPinnedBuffer(). + */ +const tensorFromPinnedBuffer = (type, buffer, dims) => new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor({ location: 'cpu-pinned', type, data: buffer, dims: dims ?? [buffer.length] }); +//# sourceMappingURL=tensor-factory-impl.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/tensor-factory.js": +/*!********************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/tensor-factory.js ***! + \********************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +//# sourceMappingURL=tensor-factory.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/tensor-impl-type-mapping.js": +/*!******************************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/tensor-impl-type-mapping.js ***! + \******************************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP: () => (/* binding */ NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP), +/* harmony export */ NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP: () => (/* binding */ NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP), +/* harmony export */ checkTypedArray: () => (/* binding */ checkTypedArray) +/* harmony export */ }); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// a runtime map that maps type string to TypedArray constructor. Should match Tensor.DataTypeMap. +const NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP = new Map([ + ['float32', Float32Array], + ['uint8', Uint8Array], + ['int8', Int8Array], + ['uint16', Uint16Array], + ['int16', Int16Array], + ['int32', Int32Array], + ['bool', Uint8Array], + ['float64', Float64Array], + ['uint32', Uint32Array], + ['int4', Uint8Array], + ['uint4', Uint8Array], +]); +// a runtime map that maps type string to TypedArray constructor. Should match Tensor.DataTypeMap. +const NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP = new Map([ + [Float32Array, 'float32'], + [Uint8Array, 'uint8'], + [Int8Array, 'int8'], + [Uint16Array, 'uint16'], + [Int16Array, 'int16'], + [Int32Array, 'int32'], + [Float64Array, 'float64'], + [Uint32Array, 'uint32'], +]); +// the following code allows delaying execution of BigInt/Float16Array checking. This allows lazy initialization for +// NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP and NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP, which allows BigInt/Float16Array +// polyfill if available. +let isTypedArrayChecked = false; +const checkTypedArray = () => { + if (!isTypedArrayChecked) { + isTypedArrayChecked = true; + const isBigInt64ArrayAvailable = typeof BigInt64Array !== 'undefined' && BigInt64Array.from; + const isBigUint64ArrayAvailable = typeof BigUint64Array !== 'undefined' && BigUint64Array.from; + // eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-explicit-any + const Float16Array = globalThis.Float16Array; + const isFloat16ArrayAvailable = typeof Float16Array !== 'undefined' && Float16Array.from; + if (isBigInt64ArrayAvailable) { + NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.set('int64', BigInt64Array); + NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.set(BigInt64Array, 'int64'); + } + if (isBigUint64ArrayAvailable) { + NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.set('uint64', BigUint64Array); + NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.set(BigUint64Array, 'uint64'); + } + if (isFloat16ArrayAvailable) { + NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.set('float16', Float16Array); + NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.set(Float16Array, 'float16'); + } + else { + // if Float16Array is not available, use 'Uint16Array' to store the data. + NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.set('float16', Uint16Array); + } + } +}; +//# sourceMappingURL=tensor-impl-type-mapping.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/tensor-impl.js": +/*!*****************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/tensor-impl.js ***! + \*****************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ Tensor: () => (/* binding */ Tensor) +/* harmony export */ }); +/* harmony import */ var _tensor_conversion_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./tensor-conversion-impl.js */ "./node_modules/onnxruntime-common/dist/esm/tensor-conversion-impl.js"); +/* harmony import */ var _tensor_factory_impl_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./tensor-factory-impl.js */ "./node_modules/onnxruntime-common/dist/esm/tensor-factory-impl.js"); +/* harmony import */ var _tensor_impl_type_mapping_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./tensor-impl-type-mapping.js */ "./node_modules/onnxruntime-common/dist/esm/tensor-impl-type-mapping.js"); +/* harmony import */ var _tensor_utils_impl_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./tensor-utils-impl.js */ "./node_modules/onnxruntime-common/dist/esm/tensor-utils-impl.js"); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + + + + +/** + * the implementation of Tensor interface. + * + * @ignore + */ +class Tensor { + /** + * implementation. + */ + constructor(arg0, arg1, arg2) { + // perform one-time check for BigInt/Float16Array support + (0,_tensor_impl_type_mapping_js__WEBPACK_IMPORTED_MODULE_2__.checkTypedArray)(); + let type; + let dims; + if (typeof arg0 === 'object' && 'location' in arg0) { + // + // constructing tensor from specific location + // + this.dataLocation = arg0.location; + type = arg0.type; + dims = arg0.dims; + switch (arg0.location) { + case 'cpu-pinned': { + const expectedTypedArrayConstructor = _tensor_impl_type_mapping_js__WEBPACK_IMPORTED_MODULE_2__.NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.get(type); + if (!expectedTypedArrayConstructor) { + throw new TypeError(`unsupported type "${type}" to create tensor from pinned buffer`); + } + if (!(arg0.data instanceof expectedTypedArrayConstructor)) { + throw new TypeError(`buffer should be of type ${expectedTypedArrayConstructor.name}`); + } + this.cpuData = arg0.data; + break; + } + case 'texture': { + if (type !== 'float32') { + throw new TypeError(`unsupported type "${type}" to create tensor from texture`); + } + this.gpuTextureData = arg0.texture; + this.downloader = arg0.download; + this.disposer = arg0.dispose; + break; + } + case 'gpu-buffer': { + if (type !== 'float32' && + type !== 'float16' && + type !== 'int32' && + type !== 'int64' && + type !== 'uint32' && + type !== 'uint8' && + type !== 'bool' && + type !== 'uint4' && + type !== 'int4') { + throw new TypeError(`unsupported type "${type}" to create tensor from gpu buffer`); + } + this.gpuBufferData = arg0.gpuBuffer; + this.downloader = arg0.download; + this.disposer = arg0.dispose; + break; + } + case 'ml-tensor': { + if (type !== 'float32' && + type !== 'float16' && + type !== 'int32' && + type !== 'int64' && + type !== 'uint32' && + type !== 'uint64' && + type !== 'int8' && + type !== 'uint8' && + type !== 'bool' && + type !== 'uint4' && + type !== 'int4') { + throw new TypeError(`unsupported type "${type}" to create tensor from MLTensor`); + } + this.mlTensorData = arg0.mlTensor; + this.downloader = arg0.download; + this.disposer = arg0.dispose; + break; + } + default: + throw new Error(`Tensor constructor: unsupported location '${this.dataLocation}'`); + } + } + else { + // + // constructing tensor of location 'cpu' + // + let data; + let maybeDims; + // check whether arg0 is type or data + if (typeof arg0 === 'string') { + // + // Override: constructor(type, data, ...) + // + type = arg0; + maybeDims = arg2; + if (arg0 === 'string') { + // string tensor + if (!Array.isArray(arg1)) { + throw new TypeError("A string tensor's data must be a string array."); + } + // we don't check whether every element in the array is string; this is too slow. we assume it's correct and + // error will be populated at inference + data = arg1; + } + else { + // numeric tensor + const typedArrayConstructor = _tensor_impl_type_mapping_js__WEBPACK_IMPORTED_MODULE_2__.NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.get(arg0); + if (typedArrayConstructor === undefined) { + throw new TypeError(`Unsupported tensor type: ${arg0}.`); + } + if (Array.isArray(arg1)) { + if ((arg0 === 'float16' && typedArrayConstructor === Uint16Array) || arg0 === 'uint4' || arg0 === 'int4') { + // - 'float16': + // When no Float16Array polyfill is used, we cannot create 'float16' tensor from number array. + // + // Throw error here because when user try to use number array as data, + // e.g. new Tensor('float16', [1, 2, 3, 4], dims)), it will actually call + // Uint16Array.from(arg1) which generates wrong data. + // + // - 'uint4' and 'int4': + // Uint8Array.from(arg1) will generate wrong data for 'uint4' and 'int4' tensor. + // + throw new TypeError(`Creating a ${arg0} tensor from number array is not supported. Please use ${typedArrayConstructor.name} as data.`); + } + else if (arg0 === 'uint64' || arg0 === 'int64') { + // use 'as any' here because: + // 1. TypeScript's check on type of 'Array.isArray()' does not work with readonly arrays. + // see https://github.com/microsoft/TypeScript/issues/17002 + // 2. TypeScript's check on union type of '(BigInt64ArrayConstructor|BigUint64ArrayConstructor).from()' + // does not accept parameter mapFn. + // 3. parameters of 'SupportedTypedArrayConstructors.from()' does not match the requirement of the union + // type. + // assume 'arg1' is of type "readonly number[]|readonly bigint[]" here. + // eslint-disable-next-line @typescript-eslint/no-explicit-any + data = typedArrayConstructor.from(arg1, BigInt); + } + else { + // assume 'arg1' is of type "readonly number[]" here. + // eslint-disable-next-line @typescript-eslint/no-explicit-any + data = typedArrayConstructor.from(arg1); + } + } + else if (arg1 instanceof typedArrayConstructor) { + data = arg1; + } + else if (arg1 instanceof Uint8ClampedArray) { + if (arg0 === 'uint8') { + data = Uint8Array.from(arg1); + } + else { + throw new TypeError(`A Uint8ClampedArray tensor's data must be type of uint8`); + } + } + else if (arg0 === 'float16' && arg1 instanceof Uint16Array && typedArrayConstructor !== Uint16Array) { + // when Float16Array is available and data is of type Uint16Array. + // We allow Uint16Array to be passed in as data for 'float16' tensor until Float16Array is generally + // supported in JavaScript environment. + // eslint-disable-next-line @typescript-eslint/no-explicit-any + data = new globalThis.Float16Array(arg1.buffer, arg1.byteOffset, arg1.length); + } + else { + throw new TypeError(`A ${type} tensor's data must be type of ${typedArrayConstructor}`); + } + } + } + else { + // + // Override: constructor(data, ...) + // + maybeDims = arg1; + if (Array.isArray(arg0)) { + // only boolean[] and string[] is supported + if (arg0.length === 0) { + throw new TypeError('Tensor type cannot be inferred from an empty array.'); + } + const firstElementType = typeof arg0[0]; + if (firstElementType === 'string') { + type = 'string'; + data = arg0; + } + else if (firstElementType === 'boolean') { + type = 'bool'; + // 'arg0' is of type 'boolean[]'. Uint8Array.from(boolean[]) actually works, but typescript thinks this is + // wrong type. We use 'as any' to make it happy. + // eslint-disable-next-line @typescript-eslint/no-explicit-any + data = Uint8Array.from(arg0); + } + else { + throw new TypeError(`Invalid element type of data array: ${firstElementType}.`); + } + } + else if (arg0 instanceof Uint8ClampedArray) { + type = 'uint8'; + data = Uint8Array.from(arg0); + } + else { + // get tensor type from TypedArray + const mappedType = _tensor_impl_type_mapping_js__WEBPACK_IMPORTED_MODULE_2__.NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.get(arg0.constructor); + if (mappedType === undefined) { + throw new TypeError(`Unsupported type for tensor data: ${arg0.constructor}.`); + } + type = mappedType; + data = arg0; + } + } + // type and data is processed, now processing dims + if (maybeDims === undefined) { + // assume 1-D tensor if dims omitted + maybeDims = [data.length]; + } + else if (!Array.isArray(maybeDims)) { + throw new TypeError("A tensor's dims must be a number array"); + } + dims = maybeDims; + this.cpuData = data; + this.dataLocation = 'cpu'; + } + // perform check on dims + const size = (0,_tensor_utils_impl_js__WEBPACK_IMPORTED_MODULE_3__.calculateSize)(dims); + // if data is on CPU, check whether data length matches tensor size + if (this.cpuData && size !== this.cpuData.length) { + if ((type === 'uint4' || type === 'int4') && Math.ceil(size / 2) === this.cpuData.length) { + // for (u)int4, the data length is half of the tensor size. So we check this special case when size is odd. + } + else { + throw new Error(`Tensor's size(${size}) does not match data length(${this.cpuData.length}).`); + } + } + this.type = type; + this.dims = dims; + this.size = size; + } + // #endregion + // #region factory + static async fromImage(image, options) { + return (0,_tensor_factory_impl_js__WEBPACK_IMPORTED_MODULE_1__.tensorFromImage)(image, options); + } + static fromTexture(texture, options) { + return (0,_tensor_factory_impl_js__WEBPACK_IMPORTED_MODULE_1__.tensorFromTexture)(texture, options); + } + static fromGpuBuffer(gpuBuffer, options) { + return (0,_tensor_factory_impl_js__WEBPACK_IMPORTED_MODULE_1__.tensorFromGpuBuffer)(gpuBuffer, options); + } + static fromMLTensor(mlTensor, options) { + return (0,_tensor_factory_impl_js__WEBPACK_IMPORTED_MODULE_1__.tensorFromMLTensor)(mlTensor, options); + } + static fromPinnedBuffer(type, buffer, dims) { + return (0,_tensor_factory_impl_js__WEBPACK_IMPORTED_MODULE_1__.tensorFromPinnedBuffer)(type, buffer, dims); + } + // #endregion + // #region conversions + toDataURL(options) { + return (0,_tensor_conversion_impl_js__WEBPACK_IMPORTED_MODULE_0__.tensorToDataURL)(this, options); + } + toImageData(options) { + return (0,_tensor_conversion_impl_js__WEBPACK_IMPORTED_MODULE_0__.tensorToImageData)(this, options); + } + // #endregion + // #region properties + get data() { + this.ensureValid(); + if (!this.cpuData) { + throw new Error('The data is not on CPU. Use `getData()` to download GPU data to CPU, ' + + 'or use `texture` or `gpuBuffer` property to access the GPU data directly.'); + } + return this.cpuData; + } + get location() { + return this.dataLocation; + } + get texture() { + this.ensureValid(); + if (!this.gpuTextureData) { + throw new Error('The data is not stored as a WebGL texture.'); + } + return this.gpuTextureData; + } + get gpuBuffer() { + this.ensureValid(); + if (!this.gpuBufferData) { + throw new Error('The data is not stored as a WebGPU buffer.'); + } + return this.gpuBufferData; + } + get mlTensor() { + this.ensureValid(); + if (!this.mlTensorData) { + throw new Error('The data is not stored as a WebNN MLTensor.'); + } + return this.mlTensorData; + } + // #endregion + // #region methods + async getData(releaseData) { + this.ensureValid(); + switch (this.dataLocation) { + case 'cpu': + case 'cpu-pinned': + return this.data; + case 'texture': + case 'gpu-buffer': + case 'ml-tensor': { + if (!this.downloader) { + throw new Error('The current tensor is not created with a specified data downloader.'); + } + if (this.isDownloading) { + throw new Error('The current tensor is being downloaded.'); + } + try { + this.isDownloading = true; + const data = await this.downloader(); + this.downloader = undefined; + this.dataLocation = 'cpu'; + this.cpuData = data; + if (releaseData && this.disposer) { + this.disposer(); + this.disposer = undefined; + } + return data; + } + finally { + this.isDownloading = false; + } + } + default: + throw new Error(`cannot get data from location: ${this.dataLocation}`); + } + } + dispose() { + if (this.isDownloading) { + throw new Error('The current tensor is being downloaded.'); + } + if (this.disposer) { + this.disposer(); + this.disposer = undefined; + } + this.cpuData = undefined; + this.gpuTextureData = undefined; + this.gpuBufferData = undefined; + this.mlTensorData = undefined; + this.downloader = undefined; + this.isDownloading = undefined; + this.dataLocation = 'none'; + } + // #endregion + // #region tensor utilities + ensureValid() { + if (this.dataLocation === 'none') { + throw new Error('The tensor is disposed.'); + } + } + reshape(dims) { + this.ensureValid(); + if (this.downloader || this.disposer) { + throw new Error('Cannot reshape a tensor that owns GPU resource.'); + } + return (0,_tensor_utils_impl_js__WEBPACK_IMPORTED_MODULE_3__.tensorReshape)(this, dims); + } +} +//# sourceMappingURL=tensor-impl.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/tensor-utils-impl.js": +/*!***********************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/tensor-utils-impl.js ***! + \***********************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ calculateSize: () => (/* binding */ calculateSize), +/* harmony export */ tensorReshape: () => (/* binding */ tensorReshape) +/* harmony export */ }); +/* harmony import */ var _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./tensor-impl.js */ "./node_modules/onnxruntime-common/dist/esm/tensor-impl.js"); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +/** + * calculate size from dims. + * + * @param dims the dims array. May be an illegal input. + */ +const calculateSize = (dims) => { + let size = 1; + for (let i = 0; i < dims.length; i++) { + const dim = dims[i]; + if (typeof dim !== 'number' || !Number.isSafeInteger(dim)) { + throw new TypeError(`dims[${i}] must be an integer, got: ${dim}`); + } + if (dim < 0) { + throw new RangeError(`dims[${i}] must be a non-negative integer, got: ${dim}`); + } + size *= dim; + } + return size; +}; +/** + * implementation of Tensor.reshape() + */ +const tensorReshape = (tensor, dims) => { + switch (tensor.location) { + case 'cpu': + return new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor(tensor.type, tensor.data, dims); + case 'cpu-pinned': + return new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor({ + location: 'cpu-pinned', + data: tensor.data, + type: tensor.type, + dims, + }); + case 'texture': + return new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor({ + location: 'texture', + texture: tensor.texture, + type: tensor.type, + dims, + }); + case 'gpu-buffer': + return new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor({ + location: 'gpu-buffer', + gpuBuffer: tensor.gpuBuffer, + type: tensor.type, + dims, + }); + case 'ml-tensor': + return new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor({ + location: 'ml-tensor', + mlTensor: tensor.mlTensor, + type: tensor.type, + dims, + }); + default: + throw new Error(`tensorReshape: tensor location ${tensor.location} is not supported`); + } +}; +//# sourceMappingURL=tensor-utils-impl.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/tensor.js": +/*!************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/tensor.js ***! + \************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ Tensor: () => (/* binding */ Tensor) +/* harmony export */ }); +/* harmony import */ var _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./tensor-impl.js */ "./node_modules/onnxruntime-common/dist/esm/tensor-impl.js"); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// eslint-disable-next-line @typescript-eslint/naming-convention +const Tensor = _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor; +//# sourceMappingURL=tensor.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/trace.js": +/*!***********************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/trace.js ***! + \***********************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ TRACE: () => (/* binding */ TRACE), +/* harmony export */ TRACE_FUNC_BEGIN: () => (/* binding */ TRACE_FUNC_BEGIN), +/* harmony export */ TRACE_FUNC_END: () => (/* binding */ TRACE_FUNC_END) +/* harmony export */ }); +/* harmony import */ var _env_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./env-impl.js */ "./node_modules/onnxruntime-common/dist/esm/env-impl.js"); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +/** + * @ignore + */ +const TRACE = (deviceType, label) => { + if (typeof _env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.trace === 'undefined' ? !_env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.wasm.trace : !_env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.trace) { + return; + } + // eslint-disable-next-line no-console + console.timeStamp(`${deviceType}::ORT::${label}`); +}; +const TRACE_FUNC = (msg, extraMsg) => { + const stack = new Error().stack?.split(/\r\n|\r|\n/g) || []; + let hasTraceFunc = false; + for (let i = 0; i < stack.length; i++) { + if (hasTraceFunc && !stack[i].includes('TRACE_FUNC')) { + let label = `FUNC_${msg}::${stack[i].trim().split(' ')[1]}`; + if (extraMsg) { + label += `::${extraMsg}`; + } + TRACE('CPU', label); + return; + } + if (stack[i].includes('TRACE_FUNC')) { + hasTraceFunc = true; + } + } +}; +/** + * @ignore + */ +const TRACE_FUNC_BEGIN = (extraMsg) => { + if (typeof _env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.trace === 'undefined' ? !_env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.wasm.trace : !_env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.trace) { + return; + } + TRACE_FUNC('BEGIN', extraMsg); +}; +/** + * @ignore + */ +const TRACE_FUNC_END = (extraMsg) => { + if (typeof _env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.trace === 'undefined' ? !_env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.wasm.trace : !_env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.trace) { + return; + } + TRACE_FUNC('END', extraMsg); +}; +//# sourceMappingURL=trace.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/version.js": +/*!*************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/version.js ***! + \*************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ version: () => (/* binding */ version) +/* harmony export */ }); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// This file is generated by /js/scripts/update-version.ts +// Do not modify file content manually. +const version = '1.21.0'; +//# sourceMappingURL=version.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-web/dist/ort.bundle.min.mjs?3a96": +/*!**************************************************************!*\ + !*** ./node_modules/onnxruntime-web/dist/ort.bundle.min.mjs ***! + \**************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ InferenceSession: () => (/* binding */ Gp), +/* harmony export */ TRACE: () => (/* binding */ gr), +/* harmony export */ TRACE_FUNC_BEGIN: () => (/* binding */ Re), +/* harmony export */ TRACE_FUNC_END: () => (/* binding */ Oe), +/* harmony export */ Tensor: () => (/* binding */ Ge), +/* harmony export */ "default": () => (/* binding */ IS), +/* harmony export */ env: () => (/* binding */ ge), +/* harmony export */ registerBackend: () => (/* binding */ $t) +/* harmony export */ }); +/*! + * ONNX Runtime Web v1.22.0-dev.20250409-89f8206ba4 + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. + */ +var zn=Object.defineProperty;var Up=Object.getOwnPropertyDescriptor;var Np=Object.getOwnPropertyNames;var Vp=Object.prototype.hasOwnProperty;var On=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,r)=>(typeof require<"u"?require:t)[r]}):e)(function(e){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+e+'" is not supported')});var U=(e,t)=>()=>(e&&(t=e(e=0)),t);var Dt=(e,t)=>{for(var r in t)zn(e,r,{get:t[r],enumerable:!0})},Wp=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of Np(t))!Vp.call(e,o)&&o!==r&&zn(e,o,{get:()=>t[o],enumerable:!(n=Up(t,o))||n.enumerable});return e};var Ft=e=>Wp(zn({},"__esModule",{value:!0}),e);var fr,vt,$t,Lp,Fi,Bn=U(()=>{"use strict";fr=new Map,vt=[],$t=(e,t,r)=>{if(t&&typeof t.init=="function"&&typeof t.createInferenceSessionHandler=="function"){let n=fr.get(e);if(n===void 0)fr.set(e,{backend:t,priority:r});else{if(n.priority>r)return;if(n.priority===r&&n.backend!==t)throw new Error(`cannot register backend "${e}" using priority ${r}`)}if(r>=0){let o=vt.indexOf(e);o!==-1&&vt.splice(o,1);for(let i=0;i{let t=fr.get(e);if(!t)return"backend not found.";if(t.initialized)return t.backend;if(t.aborted)return t.error;{let r=!!t.initPromise;try{return r||(t.initPromise=t.backend.init(e)),await t.initPromise,t.initialized=!0,t.backend}catch(n){return r||(t.error=`${n}`,t.aborted=!0),t.error}finally{delete t.initPromise}}},Fi=async e=>{let t=e.executionProviders||[],r=t.map(d=>typeof d=="string"?d:d.name),n=r.length===0?vt:r,o,i=[],a=new Set;for(let d of n){let c=await Lp(d);typeof c=="string"?i.push({name:d,err:c}):(o||(o=c),o===c&&a.add(d))}if(!o)throw new Error(`no available backend found. ERR: ${i.map(d=>`[${d.name}] ${d.err}`).join(", ")}`);for(let{name:d,err:c}of i)r.includes(d)&&console.warn(`removing requested execution provider "${d}" from session options because it is not available: ${c}`);let u=t.filter(d=>a.has(typeof d=="string"?d:d.name));return[o,new Proxy(e,{get:(d,c)=>c==="executionProviders"?u:Reflect.get(d,c)})]}});var qi=U(()=>{"use strict";Bn()});var ji,Ki=U(()=>{"use strict";ji="1.22.0-dev.20250409-89f8206ba4"});var Zi,Me,Dn=U(()=>{"use strict";Ki();Zi="warning",Me={wasm:{},webgl:{},webgpu:{},versions:{common:ji},set logLevel(e){if(e!==void 0){if(typeof e!="string"||["verbose","info","warning","error","fatal"].indexOf(e)===-1)throw new Error(`Unsupported logging level: ${e}`);Zi=e}},get logLevel(){return Zi}};Object.defineProperty(Me,"logLevel",{enumerable:!0})});var ge,Qi=U(()=>{"use strict";Dn();ge=Me});var Yi,Xi,Ji=U(()=>{"use strict";Yi=(e,t)=>{let r=typeof document<"u"?document.createElement("canvas"):new OffscreenCanvas(1,1);r.width=e.dims[3],r.height=e.dims[2];let n=r.getContext("2d");if(n!=null){let o,i;t?.tensorLayout!==void 0&&t.tensorLayout==="NHWC"?(o=e.dims[2],i=e.dims[3]):(o=e.dims[3],i=e.dims[2]);let a=t?.format!==void 0?t.format:"RGB",u=t?.norm,d,c;u===void 0||u.mean===void 0?d=[255,255,255,255]:typeof u.mean=="number"?d=[u.mean,u.mean,u.mean,u.mean]:(d=[u.mean[0],u.mean[1],u.mean[2],0],u.mean[3]!==void 0&&(d[3]=u.mean[3])),u===void 0||u.bias===void 0?c=[0,0,0,0]:typeof u.bias=="number"?c=[u.bias,u.bias,u.bias,u.bias]:(c=[u.bias[0],u.bias[1],u.bias[2],0],u.bias[3]!==void 0&&(c[3]=u.bias[3]));let p=i*o,m=0,f=p,b=p*2,g=-1;a==="RGBA"?(m=0,f=p,b=p*2,g=p*3):a==="RGB"?(m=0,f=p,b=p*2):a==="RBG"&&(m=0,b=p,f=p*2);for(let _=0;_{let r=typeof document<"u"?document.createElement("canvas").getContext("2d"):new OffscreenCanvas(1,1).getContext("2d"),n;if(r!=null){let o,i,a;t?.tensorLayout!==void 0&&t.tensorLayout==="NHWC"?(o=e.dims[2],i=e.dims[1],a=e.dims[3]):(o=e.dims[3],i=e.dims[2],a=e.dims[1]);let u=t!==void 0&&t.format!==void 0?t.format:"RGB",d=t?.norm,c,p;d===void 0||d.mean===void 0?c=[255,255,255,255]:typeof d.mean=="number"?c=[d.mean,d.mean,d.mean,d.mean]:(c=[d.mean[0],d.mean[1],d.mean[2],255],d.mean[3]!==void 0&&(c[3]=d.mean[3])),d===void 0||d.bias===void 0?p=[0,0,0,0]:typeof d.bias=="number"?p=[d.bias,d.bias,d.bias,d.bias]:(p=[d.bias[0],d.bias[1],d.bias[2],0],d.bias[3]!==void 0&&(p[3]=d.bias[3]));let m=i*o;if(t!==void 0&&(t.format!==void 0&&a===4&&t.format!=="RGBA"||a===3&&t.format!=="RGB"&&t.format!=="BGR"))throw new Error("Tensor format doesn't match input tensor dims");let f=4,b=0,g=1,_=2,S=3,$=0,v=m,x=m*2,T=-1;u==="RGBA"?($=0,v=m,x=m*2,T=m*3):u==="RGB"?($=0,v=m,x=m*2):u==="RBG"&&($=0,x=m,v=m*2),n=r.createImageData(o,i);for(let E=0;E{"use strict";hr();Mn=(e,t)=>{if(e===void 0)throw new Error("Image buffer must be defined");if(t.height===void 0||t.width===void 0)throw new Error("Image height and width must be defined");if(t.tensorLayout==="NHWC")throw new Error("NHWC Tensor layout is not supported yet");let{height:r,width:n}=t,o=t.norm??{mean:255,bias:0},i,a;typeof o.mean=="number"?i=[o.mean,o.mean,o.mean,o.mean]:i=[o.mean[0],o.mean[1],o.mean[2],o.mean[3]??255],typeof o.bias=="number"?a=[o.bias,o.bias,o.bias,o.bias]:a=[o.bias[0],o.bias[1],o.bias[2],o.bias[3]??0];let u=t.format!==void 0?t.format:"RGBA",d=t.tensorFormat!==void 0&&t.tensorFormat!==void 0?t.tensorFormat:"RGB",c=r*n,p=d==="RGBA"?new Float32Array(c*4):new Float32Array(c*3),m=4,f=0,b=1,g=2,_=3,S=0,$=c,v=c*2,x=-1;u==="RGB"&&(m=3,f=0,b=1,g=2,_=-1),d==="RGBA"?x=c*3:d==="RBG"?(S=0,v=c,$=c*2):d==="BGR"&&(v=0,$=c,S=c*2);for(let E=0;E{let r=typeof HTMLImageElement<"u"&&e instanceof HTMLImageElement,n=typeof ImageData<"u"&&e instanceof ImageData,o=typeof ImageBitmap<"u"&&e instanceof ImageBitmap,i=typeof e=="string",a,u=t??{},d=()=>{if(typeof document<"u")return document.createElement("canvas");if(typeof OffscreenCanvas<"u")return new OffscreenCanvas(1,1);throw new Error("Canvas is not supported")},c=p=>typeof HTMLCanvasElement<"u"&&p instanceof HTMLCanvasElement||p instanceof OffscreenCanvas?p.getContext("2d"):null;if(r){let p=d();p.width=e.width,p.height=e.height;let m=c(p);if(m!=null){let f=e.height,b=e.width;if(t!==void 0&&t.resizedHeight!==void 0&&t.resizedWidth!==void 0&&(f=t.resizedHeight,b=t.resizedWidth),t!==void 0){if(u=t,t.tensorFormat!==void 0)throw new Error("Image input config format must be RGBA for HTMLImageElement");u.tensorFormat="RGBA",u.height=f,u.width=b}else u.tensorFormat="RGBA",u.height=f,u.width=b;m.drawImage(e,0,0),a=m.getImageData(0,0,b,f).data}else throw new Error("Can not access image data")}else if(n){let p,m;if(t!==void 0&&t.resizedWidth!==void 0&&t.resizedHeight!==void 0?(p=t.resizedHeight,m=t.resizedWidth):(p=e.height,m=e.width),t!==void 0&&(u=t),u.format="RGBA",u.height=p,u.width=m,t!==void 0){let f=d();f.width=m,f.height=p;let b=c(f);if(b!=null)b.putImageData(e,0,0),a=b.getImageData(0,0,m,p).data;else throw new Error("Can not access image data")}else a=e.data}else if(o){if(t===void 0)throw new Error("Please provide image config with format for Imagebitmap");let p=d();p.width=e.width,p.height=e.height;let m=c(p);if(m!=null){let f=e.height,b=e.width;return m.drawImage(e,0,0,b,f),a=m.getImageData(0,0,b,f).data,u.height=f,u.width=b,Mn(a,u)}else throw new Error("Can not access image data")}else{if(i)return new Promise((p,m)=>{let f=d(),b=c(f);if(!e||!b)return m();let g=new Image;g.crossOrigin="Anonymous",g.src=e,g.onload=()=>{f.width=g.width,f.height=g.height,b.drawImage(g,0,0,f.width,f.height);let _=b.getImageData(0,0,f.width,f.height);u.height=f.height,u.width=f.width,p(Mn(_.data,u))}});throw new Error("Input data provided is not supported - aborted tensor creation")}if(a!==void 0)return Mn(a,u);throw new Error("Input data provided is not supported - aborted tensor creation")},ta=(e,t)=>{let{width:r,height:n,download:o,dispose:i}=t,a=[1,n,r,4];return new Pe({location:"texture",type:"float32",texture:e,dims:a,download:o,dispose:i})},ra=(e,t)=>{let{dataType:r,dims:n,download:o,dispose:i}=t;return new Pe({location:"gpu-buffer",type:r??"float32",gpuBuffer:e,dims:n,download:o,dispose:i})},na=(e,t)=>{let{dataType:r,dims:n,download:o,dispose:i}=t;return new Pe({location:"ml-tensor",type:r??"float32",mlTensor:e,dims:n,download:o,dispose:i})},oa=(e,t,r)=>new Pe({location:"cpu-pinned",type:e,data:t,dims:r??[t.length]})});var xt,qt,aa,sa,ua=U(()=>{"use strict";xt=new Map([["float32",Float32Array],["uint8",Uint8Array],["int8",Int8Array],["uint16",Uint16Array],["int16",Int16Array],["int32",Int32Array],["bool",Uint8Array],["float64",Float64Array],["uint32",Uint32Array],["int4",Uint8Array],["uint4",Uint8Array]]),qt=new Map([[Float32Array,"float32"],[Uint8Array,"uint8"],[Int8Array,"int8"],[Uint16Array,"uint16"],[Int16Array,"int16"],[Int32Array,"int32"],[Float64Array,"float64"],[Uint32Array,"uint32"]]),aa=!1,sa=()=>{if(!aa){aa=!0;let e=typeof BigInt64Array<"u"&&BigInt64Array.from,t=typeof BigUint64Array<"u"&&BigUint64Array.from,r=globalThis.Float16Array,n=typeof r<"u"&&r.from;e&&(xt.set("int64",BigInt64Array),qt.set(BigInt64Array,"int64")),t&&(xt.set("uint64",BigUint64Array),qt.set(BigUint64Array,"uint64")),n?(xt.set("float16",r),qt.set(r,"float16")):xt.set("float16",Uint16Array)}}});var da,la,ca=U(()=>{"use strict";hr();da=e=>{let t=1;for(let r=0;r{switch(e.location){case"cpu":return new Pe(e.type,e.data,t);case"cpu-pinned":return new Pe({location:"cpu-pinned",data:e.data,type:e.type,dims:t});case"texture":return new Pe({location:"texture",texture:e.texture,type:e.type,dims:t});case"gpu-buffer":return new Pe({location:"gpu-buffer",gpuBuffer:e.gpuBuffer,type:e.type,dims:t});case"ml-tensor":return new Pe({location:"ml-tensor",mlTensor:e.mlTensor,type:e.type,dims:t});default:throw new Error(`tensorReshape: tensor location ${e.location} is not supported`)}}});var Pe,hr=U(()=>{"use strict";Ji();ia();ua();ca();Pe=class{constructor(t,r,n){sa();let o,i;if(typeof t=="object"&&"location"in t)switch(this.dataLocation=t.location,o=t.type,i=t.dims,t.location){case"cpu-pinned":{let u=xt.get(o);if(!u)throw new TypeError(`unsupported type "${o}" to create tensor from pinned buffer`);if(!(t.data instanceof u))throw new TypeError(`buffer should be of type ${u.name}`);this.cpuData=t.data;break}case"texture":{if(o!=="float32")throw new TypeError(`unsupported type "${o}" to create tensor from texture`);this.gpuTextureData=t.texture,this.downloader=t.download,this.disposer=t.dispose;break}case"gpu-buffer":{if(o!=="float32"&&o!=="float16"&&o!=="int32"&&o!=="int64"&&o!=="uint32"&&o!=="uint8"&&o!=="bool"&&o!=="uint4"&&o!=="int4")throw new TypeError(`unsupported type "${o}" to create tensor from gpu buffer`);this.gpuBufferData=t.gpuBuffer,this.downloader=t.download,this.disposer=t.dispose;break}case"ml-tensor":{if(o!=="float32"&&o!=="float16"&&o!=="int32"&&o!=="int64"&&o!=="uint32"&&o!=="uint64"&&o!=="int8"&&o!=="uint8"&&o!=="bool"&&o!=="uint4"&&o!=="int4")throw new TypeError(`unsupported type "${o}" to create tensor from MLTensor`);this.mlTensorData=t.mlTensor,this.downloader=t.download,this.disposer=t.dispose;break}default:throw new Error(`Tensor constructor: unsupported location '${this.dataLocation}'`)}else{let u,d;if(typeof t=="string")if(o=t,d=n,t==="string"){if(!Array.isArray(r))throw new TypeError("A string tensor's data must be a string array.");u=r}else{let c=xt.get(t);if(c===void 0)throw new TypeError(`Unsupported tensor type: ${t}.`);if(Array.isArray(r)){if(t==="float16"&&c===Uint16Array||t==="uint4"||t==="int4")throw new TypeError(`Creating a ${t} tensor from number array is not supported. Please use ${c.name} as data.`);t==="uint64"||t==="int64"?u=c.from(r,BigInt):u=c.from(r)}else if(r instanceof c)u=r;else if(r instanceof Uint8ClampedArray)if(t==="uint8")u=Uint8Array.from(r);else throw new TypeError("A Uint8ClampedArray tensor's data must be type of uint8");else if(t==="float16"&&r instanceof Uint16Array&&c!==Uint16Array)u=new globalThis.Float16Array(r.buffer,r.byteOffset,r.length);else throw new TypeError(`A ${o} tensor's data must be type of ${c}`)}else if(d=r,Array.isArray(t)){if(t.length===0)throw new TypeError("Tensor type cannot be inferred from an empty array.");let c=typeof t[0];if(c==="string")o="string",u=t;else if(c==="boolean")o="bool",u=Uint8Array.from(t);else throw new TypeError(`Invalid element type of data array: ${c}.`)}else if(t instanceof Uint8ClampedArray)o="uint8",u=Uint8Array.from(t);else{let c=qt.get(t.constructor);if(c===void 0)throw new TypeError(`Unsupported type for tensor data: ${t.constructor}.`);o=c,u=t}if(d===void 0)d=[u.length];else if(!Array.isArray(d))throw new TypeError("A tensor's dims must be a number array");i=d,this.cpuData=u,this.dataLocation="cpu"}let a=da(i);if(this.cpuData&&a!==this.cpuData.length&&!((o==="uint4"||o==="int4")&&Math.ceil(a/2)===this.cpuData.length))throw new Error(`Tensor's size(${a}) does not match data length(${this.cpuData.length}).`);this.type=o,this.dims=i,this.size=a}static async fromImage(t,r){return ea(t,r)}static fromTexture(t,r){return ta(t,r)}static fromGpuBuffer(t,r){return ra(t,r)}static fromMLTensor(t,r){return na(t,r)}static fromPinnedBuffer(t,r,n){return oa(t,r,n)}toDataURL(t){return Yi(this,t)}toImageData(t){return Xi(this,t)}get data(){if(this.ensureValid(),!this.cpuData)throw new Error("The data is not on CPU. Use `getData()` to download GPU data to CPU, or use `texture` or `gpuBuffer` property to access the GPU data directly.");return this.cpuData}get location(){return this.dataLocation}get texture(){if(this.ensureValid(),!this.gpuTextureData)throw new Error("The data is not stored as a WebGL texture.");return this.gpuTextureData}get gpuBuffer(){if(this.ensureValid(),!this.gpuBufferData)throw new Error("The data is not stored as a WebGPU buffer.");return this.gpuBufferData}get mlTensor(){if(this.ensureValid(),!this.mlTensorData)throw new Error("The data is not stored as a WebNN MLTensor.");return this.mlTensorData}async getData(t){switch(this.ensureValid(),this.dataLocation){case"cpu":case"cpu-pinned":return this.data;case"texture":case"gpu-buffer":case"ml-tensor":{if(!this.downloader)throw new Error("The current tensor is not created with a specified data downloader.");if(this.isDownloading)throw new Error("The current tensor is being downloaded.");try{this.isDownloading=!0;let r=await this.downloader();return this.downloader=void 0,this.dataLocation="cpu",this.cpuData=r,t&&this.disposer&&(this.disposer(),this.disposer=void 0),r}finally{this.isDownloading=!1}}default:throw new Error(`cannot get data from location: ${this.dataLocation}`)}}dispose(){if(this.isDownloading)throw new Error("The current tensor is being downloaded.");this.disposer&&(this.disposer(),this.disposer=void 0),this.cpuData=void 0,this.gpuTextureData=void 0,this.gpuBufferData=void 0,this.mlTensorData=void 0,this.downloader=void 0,this.isDownloading=void 0,this.dataLocation="none"}ensureValid(){if(this.dataLocation==="none")throw new Error("The tensor is disposed.")}reshape(t){if(this.ensureValid(),this.downloader||this.disposer)throw new Error("Cannot reshape a tensor that owns GPU resource.");return la(this,t)}}});var Ge,Rn=U(()=>{"use strict";hr();Ge=Pe});var gr,pa,Re,Oe,Un=U(()=>{"use strict";Dn();gr=(e,t)=>{(typeof Me.trace>"u"?!Me.wasm.trace:!Me.trace)||console.timeStamp(`${e}::ORT::${t}`)},pa=(e,t)=>{let r=new Error().stack?.split(/\r\n|\r|\n/g)||[],n=!1;for(let o=0;o{(typeof Me.trace>"u"?!Me.wasm.trace:!Me.trace)||pa("BEGIN",e)},Oe=e=>{(typeof Me.trace>"u"?!Me.wasm.trace:!Me.trace)||pa("END",e)}});var br,ma=U(()=>{"use strict";Bn();Rn();Un();br=class e{constructor(t){this.handler=t}async run(t,r,n){Re();let o={},i={};if(typeof t!="object"||t===null||t instanceof Ge||Array.isArray(t))throw new TypeError("'feeds' must be an object that use input names as keys and OnnxValue as corresponding values.");let a=!0;if(typeof r=="object"){if(r===null)throw new TypeError("Unexpected argument[1]: cannot be null.");if(r instanceof Ge)throw new TypeError("'fetches' cannot be a Tensor");if(Array.isArray(r)){if(r.length===0)throw new TypeError("'fetches' cannot be an empty array.");a=!1;for(let c of r){if(typeof c!="string")throw new TypeError("'fetches' must be a string array or an object.");if(this.outputNames.indexOf(c)===-1)throw new RangeError(`'fetches' contains invalid output name: ${c}.`);o[c]=null}if(typeof n=="object"&&n!==null)i=n;else if(typeof n<"u")throw new TypeError("'options' must be an object.")}else{let c=!1,p=Object.getOwnPropertyNames(r);for(let m of this.outputNames)if(p.indexOf(m)!==-1){let f=r[m];(f===null||f instanceof Ge)&&(c=!0,a=!1,o[m]=f)}if(c){if(typeof n=="object"&&n!==null)i=n;else if(typeof n<"u")throw new TypeError("'options' must be an object.")}else i=r}}else if(typeof r<"u")throw new TypeError("Unexpected argument[1]: must be 'fetches' or 'options'.");for(let c of this.inputNames)if(typeof t[c]>"u")throw new Error(`input '${c}' is missing in 'feeds'.`);if(a)for(let c of this.outputNames)o[c]=null;let u=await this.handler.run(t,o,i),d={};for(let c in u)if(Object.hasOwnProperty.call(u,c)){let p=u[c];p instanceof Ge?d[c]=p:d[c]=new Ge(p.type,p.data,p.dims)}return Oe(),d}async release(){return this.handler.dispose()}static async create(t,r,n,o){Re();let i,a={};if(typeof t=="string"){if(i=t,typeof r=="object"&&r!==null)a=r;else if(typeof r<"u")throw new TypeError("'options' must be an object.")}else if(t instanceof Uint8Array){if(i=t,typeof r=="object"&&r!==null)a=r;else if(typeof r<"u")throw new TypeError("'options' must be an object.")}else if(t instanceof ArrayBuffer||typeof SharedArrayBuffer<"u"&&t instanceof SharedArrayBuffer){let p=t,m=0,f=t.byteLength;if(typeof r=="object"&&r!==null)a=r;else if(typeof r=="number"){if(m=r,!Number.isSafeInteger(m))throw new RangeError("'byteOffset' must be an integer.");if(m<0||m>=p.byteLength)throw new RangeError(`'byteOffset' is out of range [0, ${p.byteLength}).`);if(f=t.byteLength-m,typeof n=="number"){if(f=n,!Number.isSafeInteger(f))throw new RangeError("'byteLength' must be an integer.");if(f<=0||m+f>p.byteLength)throw new RangeError(`'byteLength' is out of range (0, ${p.byteLength-m}].`);if(typeof o=="object"&&o!==null)a=o;else if(typeof o<"u")throw new TypeError("'options' must be an object.")}else if(typeof n<"u")throw new TypeError("'byteLength' must be a number.")}else if(typeof r<"u")throw new TypeError("'options' must be an object.");i=new Uint8Array(p,m,f)}else throw new TypeError("Unexpected argument[0]: must be 'path' or 'buffer'.");let[u,d]=await Fi(a),c=await u.createInferenceSessionHandler(i,d);return Oe(),new e(c)}startProfiling(){this.handler.startProfiling()}endProfiling(){this.handler.endProfiling()}get inputNames(){return this.handler.inputNames}get outputNames(){return this.handler.outputNames}get inputMetadata(){return this.handler.inputMetadata}get outputMetadata(){return this.handler.outputMetadata}}});var Gp,fa=U(()=>{"use strict";ma();Gp=br});var ha=U(()=>{"use strict"});var ga=U(()=>{"use strict"});var ba=U(()=>{"use strict"});var ya=U(()=>{"use strict"});var Nn={};Dt(Nn,{InferenceSession:()=>Gp,TRACE:()=>gr,TRACE_FUNC_BEGIN:()=>Re,TRACE_FUNC_END:()=>Oe,Tensor:()=>Ge,env:()=>ge,registerBackend:()=>$t});var We=U(()=>{"use strict";qi();Qi();fa();Rn();ha();ga();Un();ba();ya()});var yr=U(()=>{"use strict"});var $a={};Dt($a,{default:()=>Hp});var wa,va,Hp,xa=U(()=>{"use strict";Vn();ht();_r();wa="ort-wasm-proxy-worker",va=globalThis.self?.name===wa;va&&(self.onmessage=e=>{let{type:t,in:r}=e.data;try{switch(t){case"init-wasm":wr(r.wasm).then(()=>{vr(r).then(()=>{postMessage({type:t})},n=>{postMessage({type:t,err:n})})},n=>{postMessage({type:t,err:n})});break;case"init-ep":{let{epName:n,env:o}=r;$r(o,n).then(()=>{postMessage({type:t})},i=>{postMessage({type:t,err:i})});break}case"copy-from":{let{buffer:n}=r,o=jt(n);postMessage({type:t,out:o});break}case"create":{let{model:n,options:o}=r;xr(n,o).then(i=>{postMessage({type:t,out:i})},i=>{postMessage({type:t,err:i})});break}case"release":Sr(r),postMessage({type:t});break;case"run":{let{sessionId:n,inputIndices:o,inputs:i,outputIndices:a,options:u}=r;Tr(n,o,i,a,new Array(a.length).fill(null),u).then(d=>{d.some(c=>c[3]!=="cpu")?postMessage({type:t,err:"Proxy does not support non-cpu tensor location."}):postMessage({type:t,out:d},Cr([...i,...d]))},d=>{postMessage({type:t,err:d})});break}case"end-profiling":Ir(r),postMessage({type:t});break;default:}}catch(n){postMessage({type:t,err:n})}});Hp=va?null:e=>new Worker(e??Ue,{type:"module",name:wa})});var Ta={};Dt(Ta,{default:()=>Fp});var Wn,Sa,Fp,qp,Ia=U(()=>{"use strict";Sa=(Wn=import.meta.url,async function(e={}){var t,r,n=e,o=new Promise((s,l)=>{t=s,r=l}),i=typeof window=="object",a=typeof WorkerGlobalScope<"u",u=a&&self.name?.startsWith("em-pthread");n.mountExternalData=(s,l)=>{s.startsWith("./")&&(s=s.substring(2)),(n.Eb||(n.Eb=new Map)).set(s,l)},n.unmountExternalData=()=>{delete n.Eb};var d=globalThis.SharedArrayBuffer??new WebAssembly.Memory({initial:0,maximum:0,pc:!0}).buffer.constructor;let c=s=>async(...l)=>{try{if(n.Fb)throw Error("Session already started");let h=n.Fb={dc:l[0],errors:[]},y=await s(...l);if(n.Fb!==h)throw Error("Session mismatch");n.Jb?.flush();let w=h.errors;if(0B),0{if(s==="webgpu"){[n.Jb,n.Ub,n.Yb,n.Kb,n.Xb,n.jb,n.Zb,n.ac,n.Vb,n.Wb,n.$b]=l;let h=n.Jb;n.jsepRegisterBuffer=(y,w,A,B)=>h.registerBuffer(y,w,A,B),n.jsepGetBuffer=y=>h.getBuffer(y),n.jsepCreateDownloader=(y,w,A)=>h.createDownloader(y,w,A),n.jsepOnCreateSession=y=>{h.onCreateSession(y)},n.jsepOnReleaseSession=y=>{h.onReleaseSession(y)},n.jsepOnRunStart=y=>h.onRunStart(y),n.bc=(y,w)=>{h.upload(y,w)}}else if(s==="webnn"){let h=l[0];[n.nc,n.Nb,n.webnnEnsureTensor,n.Ob,n.webnnDownloadTensor]=l.slice(1),n.webnnReleaseTensorId=n.Nb,n.webnnUploadTensor=n.Ob,n.webnnOnRunStart=y=>h.onRunStart(y),n.webnnOnRunEnd=h.onRunEnd.bind(h),n.webnnRegisterMLContext=(y,w)=>{h.registerMLContext(y,w)},n.webnnOnReleaseSession=y=>{h.onReleaseSession(y)},n.webnnCreateMLTensorDownloader=(y,w)=>h.createMLTensorDownloader(y,w),n.webnnRegisterMLTensor=(y,w,A,B)=>h.registerMLTensor(y,w,A,B),n.webnnCreateMLContext=y=>h.createMLContext(y),n.webnnRegisterMLConstant=(y,w,A,B,R,G)=>h.registerMLConstant(y,w,A,B,R,n.Eb,G),n.webnnRegisterGraphInput=h.registerGraphInput.bind(h),n.webnnIsGraphInput=h.isGraphInput.bind(h),n.webnnCreateTemporaryTensor=h.createTemporaryTensor.bind(h),n.webnnIsInt64Supported=h.isInt64Supported.bind(h)}};let p=()=>{let s=(l,h,y)=>(...w)=>{let A=Ze,B=h?.();w=l(...w);let R=h?.();return B!==R&&(l=R,y(B),h=y=null),Ze!=A?new Promise((G,K)=>{Sn={resolve:G,reject:K}}):w};(()=>{for(let l of["_OrtAppendExecutionProvider","_OrtCreateSession","_OrtRun","_OrtRunWithBinding","_OrtBindInput"])n[l]=s(n[l],()=>n[l],h=>n[l]=h)})(),c!==void 0&&(n._OrtRun=c(n._OrtRun),n._OrtRunWithBinding=c(n._OrtRunWithBinding)),p=void 0};n.asyncInit=()=>{p?.()};var m,f,b=Object.assign({},n),g=(s,l)=>{throw l},_="";(i||a)&&(a?_=self.location.href:typeof document<"u"&&document.currentScript&&(_=document.currentScript.src),Wn&&(_=Wn),_=_.startsWith("blob:")?"":_.slice(0,_.replace(/[?#].*/,"").lastIndexOf("/")+1),a&&(f=s=>{var l=new XMLHttpRequest;return l.open("GET",s,!1),l.responseType="arraybuffer",l.send(null),new Uint8Array(l.response)}),m=async s=>{if(X(s))return new Promise((h,y)=>{var w=new XMLHttpRequest;w.open("GET",s,!0),w.responseType="arraybuffer",w.onload=()=>{w.status==200||w.status==0&&w.response?h(w.response):y(w.status)},w.onerror=y,w.send(null)});var l=await fetch(s,{credentials:"same-origin"});if(l.ok)return l.arrayBuffer();throw Error(l.status+" : "+l.url)});var S=console.log.bind(console),$=console.error.bind(console),v=S,x=$;Object.assign(n,b),b=null;var T,E,I,z,O,D,L,q,Q,W,Z,we,H,j=n.wasmBinary,te=!1,X=s=>s.startsWith("file://");function ue(){return T.buffer!=z.buffer&&Ce(),z}function he(){return T.buffer!=z.buffer&&Ce(),O}function ye(){return T.buffer!=z.buffer&&Ce(),D}function re(){return T.buffer!=z.buffer&&Ce(),L}function C(){return T.buffer!=z.buffer&&Ce(),q}function V(){return T.buffer!=z.buffer&&Ce(),Q}function de(){return T.buffer!=z.buffer&&Ce(),W}function ze(){return T.buffer!=z.buffer&&Ce(),H}if(u){let s=function(l){try{var h=l.data,y=h.Bb;if(y==="load"){let w=[];self.onmessage=A=>w.push(A),self.startWorker=()=>{postMessage({Bb:"loaded"});for(let A of w)s(A);self.onmessage=s};for(let A of h.Rb)n[A]&&!n[A].proxy||(n[A]=(...B)=>{postMessage({Bb:"callHandler",Qb:A,args:B})},A=="print"&&(v=n[A]),A=="printErr"&&(x=n[A]));T=h.kc,Ce(),ve(h.lc)}else if(y==="run"){_c(h.Ab),An(h.Ab,0,0,1,0,0),No(),$n(h.Ab),$e||(Oi(),$e=!0);try{wc(h.fc,h.Hb)}catch(w){if(w!="unwind")throw w}}else h.target!=="setimmediate"&&(y==="checkMailbox"?$e&&nr():y&&(x(`worker: received unknown command ${y}`),x(h)))}catch(w){throw Bi(),w}};var wg=s,ve,$e=!1;x=function(...l){l=l.join(" "),console.error(l)},self.alert=function(...l){postMessage({Bb:"alert",text:l.join(" "),ic:cr()})},self.onunhandledrejection=l=>{throw l.reason||l},self.onmessage=s}function Ce(){var s=T.buffer;n.HEAP8=z=new Int8Array(s),n.HEAP16=D=new Int16Array(s),n.HEAPU8=O=new Uint8Array(s),n.HEAPU16=L=new Uint16Array(s),n.HEAP32=q=new Int32Array(s),n.HEAPU32=Q=new Uint32Array(s),n.HEAPF32=W=new Float32Array(s),n.HEAPF64=H=new Float64Array(s),n.HEAP64=Z=new BigInt64Array(s),n.HEAPU64=we=new BigUint64Array(s)}function _t(){u?startWorker(n):Y.Ca()}u||(T=new WebAssembly.Memory({initial:256,maximum:65536,shared:!0}),Ce());var kt,Pt=0,Lt=null;function zo(){if(--Pt==0&&Lt){var s=Lt;Lt=null,s()}}function dt(s){throw x(s="Aborted("+s+")"),te=!0,s=new WebAssembly.RuntimeError(s+". Build with -sASSERTIONS for more info."),r(s),s}function Oo(){return{a:{L:yc,Aa:bc,b:$c,$:Go,A:qo,pa:jo,X:Zo,Z:Qo,qa:Yo,na:Xo,ga:Jo,ma:ei,J:ti,Y:ri,V:ni,oa:oi,W:ii,va:xc,E:Tc,Q:Ic,O:Ac,D:kc,u:Pc,r:zc,P:Oc,z:Vc,R:Wc,ja:Lc,T:Gc,aa:Hc,M:Fc,F:qc,ia:$n,sa:jc,t:Kc,Ba:Zc,w:Xc,o:Jc,l:tp,c:_n,n:rp,j:ip,v:ap,p:sp,f:up,s:dp,m:lp,e:cp,k:pp,i:mp,g:fp,d:hp,da:gp,ea:bp,fa:yp,ba:_i,ca:wi,N:vi,xa:wp,ua:xp,h:Sp,C:Tp,G:Ip,ta:vp,x:Cp,ra:Ap,U:Ep,q:_p,y:kp,K:Pp,S:zp,za:Op,ya:Bp,ka:Ti,la:Ii,_:hn,B:Ci,I:Ai,ha:Ei,H:ki,a:T,wa:fn}}}var cn={829644:(s,l,h,y,w)=>{if(n===void 0||!n.Eb)return 1;if((s=Te(Number(s>>>0))).startsWith("./")&&(s=s.substring(2)),!(s=n.Eb.get(s)))return 2;if(l=Number(l>>>0),h=Number(h>>>0),y=Number(y>>>0),l+h>s.byteLength)return 3;try{let A=s.subarray(l,l+h);switch(w){case 0:he().set(A,y>>>0);break;case 1:n.mc?n.mc(y,A):n.bc(y,A);break;default:return 4}return 0}catch{return 4}},830468:(s,l,h)=>{n.Ob(s,he().subarray(l>>>0,l+h>>>0))},830532:()=>n.nc(),830574:s=>{n.Nb(s)},830611:()=>{n.Vb()},830642:()=>{n.Wb()},830671:()=>{n.$b()},830696:s=>n.Ub(s),830729:s=>n.Yb(s),830761:(s,l,h)=>{n.Kb(Number(s),Number(l),Number(h),!0)},830824:(s,l,h)=>{n.Kb(Number(s),Number(l),Number(h))},830881:()=>typeof wasmOffsetConverter<"u",830938:s=>{n.jb("Abs",s,void 0)},830989:s=>{n.jb("Neg",s,void 0)},831040:s=>{n.jb("Floor",s,void 0)},831093:s=>{n.jb("Ceil",s,void 0)},831145:s=>{n.jb("Reciprocal",s,void 0)},831203:s=>{n.jb("Sqrt",s,void 0)},831255:s=>{n.jb("Exp",s,void 0)},831306:s=>{n.jb("Erf",s,void 0)},831357:s=>{n.jb("Sigmoid",s,void 0)},831412:(s,l,h)=>{n.jb("HardSigmoid",s,{alpha:l,beta:h})},831491:s=>{n.jb("Log",s,void 0)},831542:s=>{n.jb("Sin",s,void 0)},831593:s=>{n.jb("Cos",s,void 0)},831644:s=>{n.jb("Tan",s,void 0)},831695:s=>{n.jb("Asin",s,void 0)},831747:s=>{n.jb("Acos",s,void 0)},831799:s=>{n.jb("Atan",s,void 0)},831851:s=>{n.jb("Sinh",s,void 0)},831903:s=>{n.jb("Cosh",s,void 0)},831955:s=>{n.jb("Asinh",s,void 0)},832008:s=>{n.jb("Acosh",s,void 0)},832061:s=>{n.jb("Atanh",s,void 0)},832114:s=>{n.jb("Tanh",s,void 0)},832166:s=>{n.jb("Not",s,void 0)},832217:(s,l,h)=>{n.jb("Clip",s,{min:l,max:h})},832286:s=>{n.jb("Clip",s,void 0)},832338:(s,l)=>{n.jb("Elu",s,{alpha:l})},832396:s=>{n.jb("Gelu",s,void 0)},832448:s=>{n.jb("Relu",s,void 0)},832500:(s,l)=>{n.jb("LeakyRelu",s,{alpha:l})},832564:(s,l)=>{n.jb("ThresholdedRelu",s,{alpha:l})},832634:(s,l)=>{n.jb("Cast",s,{to:l})},832692:s=>{n.jb("Add",s,void 0)},832743:s=>{n.jb("Sub",s,void 0)},832794:s=>{n.jb("Mul",s,void 0)},832845:s=>{n.jb("Div",s,void 0)},832896:s=>{n.jb("Pow",s,void 0)},832947:s=>{n.jb("Equal",s,void 0)},833e3:s=>{n.jb("Greater",s,void 0)},833055:s=>{n.jb("GreaterOrEqual",s,void 0)},833117:s=>{n.jb("Less",s,void 0)},833169:s=>{n.jb("LessOrEqual",s,void 0)},833228:(s,l,h,y,w)=>{n.jb("ReduceMean",s,{keepDims:!!l,noopWithEmptyAxes:!!h,axes:y?Array.from(C().subarray(Number(y)>>>0,Number(w)>>>0)):[]})},833403:(s,l,h,y,w)=>{n.jb("ReduceMax",s,{keepDims:!!l,noopWithEmptyAxes:!!h,axes:y?Array.from(C().subarray(Number(y)>>>0,Number(w)>>>0)):[]})},833577:(s,l,h,y,w)=>{n.jb("ReduceMin",s,{keepDims:!!l,noopWithEmptyAxes:!!h,axes:y?Array.from(C().subarray(Number(y)>>>0,Number(w)>>>0)):[]})},833751:(s,l,h,y,w)=>{n.jb("ReduceProd",s,{keepDims:!!l,noopWithEmptyAxes:!!h,axes:y?Array.from(C().subarray(Number(y)>>>0,Number(w)>>>0)):[]})},833926:(s,l,h,y,w)=>{n.jb("ReduceSum",s,{keepDims:!!l,noopWithEmptyAxes:!!h,axes:y?Array.from(C().subarray(Number(y)>>>0,Number(w)>>>0)):[]})},834100:(s,l,h,y,w)=>{n.jb("ReduceL1",s,{keepDims:!!l,noopWithEmptyAxes:!!h,axes:y?Array.from(C().subarray(Number(y)>>>0,Number(w)>>>0)):[]})},834273:(s,l,h,y,w)=>{n.jb("ReduceL2",s,{keepDims:!!l,noopWithEmptyAxes:!!h,axes:y?Array.from(C().subarray(Number(y)>>>0,Number(w)>>>0)):[]})},834446:(s,l,h,y,w)=>{n.jb("ReduceLogSum",s,{keepDims:!!l,noopWithEmptyAxes:!!h,axes:y?Array.from(C().subarray(Number(y)>>>0,Number(w)>>>0)):[]})},834623:(s,l,h,y,w)=>{n.jb("ReduceSumSquare",s,{keepDims:!!l,noopWithEmptyAxes:!!h,axes:y?Array.from(C().subarray(Number(y)>>>0,Number(w)>>>0)):[]})},834803:(s,l,h,y,w)=>{n.jb("ReduceLogSumExp",s,{keepDims:!!l,noopWithEmptyAxes:!!h,axes:y?Array.from(C().subarray(Number(y)>>>0,Number(w)>>>0)):[]})},834983:s=>{n.jb("Where",s,void 0)},835036:(s,l,h)=>{n.jb("Transpose",s,{perm:l?Array.from(C().subarray(Number(l)>>>0,Number(h)>>>0)):[]})},835160:(s,l,h,y)=>{n.jb("DepthToSpace",s,{blocksize:l,mode:Te(h),format:y?"NHWC":"NCHW"})},835293:(s,l,h,y)=>{n.jb("DepthToSpace",s,{blocksize:l,mode:Te(h),format:y?"NHWC":"NCHW"})},835426:(s,l,h,y,w,A,B,R,G,K,ae,le,_e,ke,Bt)=>{n.jb("ConvTranspose",s,{format:G?"NHWC":"NCHW",autoPad:l,dilations:[h],group:y,kernelShape:[w],pads:[A,B],strides:[R],wIsConst:()=>!!ue()[K>>>0],outputPadding:ae?Array.from(C().subarray(Number(ae)>>>0,Number(le)>>>0)):[],outputShape:_e?Array.from(C().subarray(Number(_e)>>>0,Number(ke)>>>0)):[],activation:Te(Bt)})},835859:(s,l,h,y,w,A,B,R,G,K,ae,le,_e,ke)=>{n.jb("ConvTranspose",s,{format:R?"NHWC":"NCHW",autoPad:l,dilations:Array.from(C().subarray(Number(h)>>>0,2+(Number(h)>>>0)>>>0)),group:y,kernelShape:Array.from(C().subarray(Number(w)>>>0,2+(Number(w)>>>0)>>>0)),pads:Array.from(C().subarray(Number(A)>>>0,4+(Number(A)>>>0)>>>0)),strides:Array.from(C().subarray(Number(B)>>>0,2+(Number(B)>>>0)>>>0)),wIsConst:()=>!!ue()[G>>>0],outputPadding:K?Array.from(C().subarray(Number(K)>>>0,Number(ae)>>>0)):[],outputShape:le?Array.from(C().subarray(Number(le)>>>0,Number(_e)>>>0)):[],activation:Te(ke)})},836520:(s,l,h,y,w,A,B,R,G,K,ae,le,_e,ke,Bt)=>{n.jb("ConvTranspose",s,{format:G?"NHWC":"NCHW",autoPad:l,dilations:[h],group:y,kernelShape:[w],pads:[A,B],strides:[R],wIsConst:()=>!!ue()[K>>>0],outputPadding:ae?Array.from(C().subarray(Number(ae)>>>0,Number(le)>>>0)):[],outputShape:_e?Array.from(C().subarray(Number(_e)>>>0,Number(ke)>>>0)):[],activation:Te(Bt)})},836953:(s,l,h,y,w,A,B,R,G,K,ae,le,_e,ke)=>{n.jb("ConvTranspose",s,{format:R?"NHWC":"NCHW",autoPad:l,dilations:Array.from(C().subarray(Number(h)>>>0,2+(Number(h)>>>0)>>>0)),group:y,kernelShape:Array.from(C().subarray(Number(w)>>>0,2+(Number(w)>>>0)>>>0)),pads:Array.from(C().subarray(Number(A)>>>0,4+(Number(A)>>>0)>>>0)),strides:Array.from(C().subarray(Number(B)>>>0,2+(Number(B)>>>0)>>>0)),wIsConst:()=>!!ue()[G>>>0],outputPadding:K?Array.from(C().subarray(Number(K)>>>0,Number(ae)>>>0)):[],outputShape:le?Array.from(C().subarray(Number(le)>>>0,Number(_e)>>>0)):[],activation:Te(ke)})},837614:(s,l)=>{n.jb("GlobalAveragePool",s,{format:l?"NHWC":"NCHW"})},837705:(s,l,h,y,w,A,B,R,G,K,ae,le,_e,ke)=>{n.jb("AveragePool",s,{format:ke?"NHWC":"NCHW",auto_pad:l,ceil_mode:h,count_include_pad:y,storage_order:w,dilations:A?Array.from(C().subarray(Number(A)>>>0,Number(B)>>>0)):[],kernel_shape:R?Array.from(C().subarray(Number(R)>>>0,Number(G)>>>0)):[],pads:K?Array.from(C().subarray(Number(K)>>>0,Number(ae)>>>0)):[],strides:le?Array.from(C().subarray(Number(le)>>>0,Number(_e)>>>0)):[]})},838184:(s,l)=>{n.jb("GlobalAveragePool",s,{format:l?"NHWC":"NCHW"})},838275:(s,l,h,y,w,A,B,R,G,K,ae,le,_e,ke)=>{n.jb("AveragePool",s,{format:ke?"NHWC":"NCHW",auto_pad:l,ceil_mode:h,count_include_pad:y,storage_order:w,dilations:A?Array.from(C().subarray(Number(A)>>>0,Number(B)>>>0)):[],kernel_shape:R?Array.from(C().subarray(Number(R)>>>0,Number(G)>>>0)):[],pads:K?Array.from(C().subarray(Number(K)>>>0,Number(ae)>>>0)):[],strides:le?Array.from(C().subarray(Number(le)>>>0,Number(_e)>>>0)):[]})},838754:(s,l)=>{n.jb("GlobalMaxPool",s,{format:l?"NHWC":"NCHW"})},838841:(s,l,h,y,w,A,B,R,G,K,ae,le,_e,ke)=>{n.jb("MaxPool",s,{format:ke?"NHWC":"NCHW",auto_pad:l,ceil_mode:h,count_include_pad:y,storage_order:w,dilations:A?Array.from(C().subarray(Number(A)>>>0,Number(B)>>>0)):[],kernel_shape:R?Array.from(C().subarray(Number(R)>>>0,Number(G)>>>0)):[],pads:K?Array.from(C().subarray(Number(K)>>>0,Number(ae)>>>0)):[],strides:le?Array.from(C().subarray(Number(le)>>>0,Number(_e)>>>0)):[]})},839316:(s,l)=>{n.jb("GlobalMaxPool",s,{format:l?"NHWC":"NCHW"})},839403:(s,l,h,y,w,A,B,R,G,K,ae,le,_e,ke)=>{n.jb("MaxPool",s,{format:ke?"NHWC":"NCHW",auto_pad:l,ceil_mode:h,count_include_pad:y,storage_order:w,dilations:A?Array.from(C().subarray(Number(A)>>>0,Number(B)>>>0)):[],kernel_shape:R?Array.from(C().subarray(Number(R)>>>0,Number(G)>>>0)):[],pads:K?Array.from(C().subarray(Number(K)>>>0,Number(ae)>>>0)):[],strides:le?Array.from(C().subarray(Number(le)>>>0,Number(_e)>>>0)):[]})},839878:(s,l,h,y,w)=>{n.jb("Gemm",s,{alpha:l,beta:h,transA:y,transB:w})},839982:s=>{n.jb("MatMul",s,void 0)},840036:(s,l,h,y)=>{n.jb("ArgMax",s,{keepDims:!!l,selectLastIndex:!!h,axis:y})},840144:(s,l,h,y)=>{n.jb("ArgMin",s,{keepDims:!!l,selectLastIndex:!!h,axis:y})},840252:(s,l)=>{n.jb("Softmax",s,{axis:l})},840315:(s,l)=>{n.jb("Concat",s,{axis:l})},840375:(s,l,h,y,w)=>{n.jb("Split",s,{axis:l,numOutputs:h,splitSizes:y?Array.from(C().subarray(Number(y)>>>0,Number(w)>>>0)):[]})},840531:s=>{n.jb("Expand",s,void 0)},840585:(s,l)=>{n.jb("Gather",s,{axis:Number(l)})},840656:(s,l)=>{n.jb("GatherElements",s,{axis:Number(l)})},840735:(s,l)=>{n.jb("GatherND",s,{batch_dims:Number(l)})},840814:(s,l,h,y,w,A,B,R,G,K,ae)=>{n.jb("Resize",s,{antialias:l,axes:h?Array.from(C().subarray(Number(h)>>>0,Number(y)>>>0)):[],coordinateTransformMode:Te(w),cubicCoeffA:A,excludeOutside:B,extrapolationValue:R,keepAspectRatioPolicy:Te(G),mode:Te(K),nearestMode:Te(ae)})},841176:(s,l,h,y,w,A,B)=>{n.jb("Slice",s,{starts:l?Array.from(C().subarray(Number(l)>>>0,Number(h)>>>0)):[],ends:y?Array.from(C().subarray(Number(y)>>>0,Number(w)>>>0)):[],axes:A?Array.from(C().subarray(Number(A)>>>0,Number(B)>>>0)):[]})},841440:s=>{n.jb("Tile",s,void 0)},841492:(s,l,h)=>{n.jb("InstanceNormalization",s,{epsilon:l,format:h?"NHWC":"NCHW"})},841606:(s,l,h)=>{n.jb("InstanceNormalization",s,{epsilon:l,format:h?"NHWC":"NCHW"})},841720:s=>{n.jb("Range",s,void 0)},841773:(s,l)=>{n.jb("Einsum",s,{equation:Te(l)})},841854:(s,l,h,y,w)=>{n.jb("Pad",s,{mode:l,value:h,pads:y?Array.from(C().subarray(Number(y)>>>0,Number(w)>>>0)):[]})},841997:(s,l,h,y,w,A)=>{n.jb("BatchNormalization",s,{epsilon:l,momentum:h,spatial:!!w,trainingMode:!!y,format:A?"NHWC":"NCHW"})},842166:(s,l,h,y,w,A)=>{n.jb("BatchNormalization",s,{epsilon:l,momentum:h,spatial:!!w,trainingMode:!!y,format:A?"NHWC":"NCHW"})},842335:(s,l,h)=>{n.jb("CumSum",s,{exclusive:Number(l),reverse:Number(h)})},842432:(s,l,h)=>{n.jb("DequantizeLinear",s,{axis:l,blockSize:h})},842522:(s,l,h,y,w)=>{n.jb("GridSample",s,{align_corners:l,mode:Te(h),padding_mode:Te(y),format:w?"NHWC":"NCHW"})},842692:(s,l,h,y,w)=>{n.jb("GridSample",s,{align_corners:l,mode:Te(h),padding_mode:Te(y),format:w?"NHWC":"NCHW"})},842862:(s,l)=>{n.jb("ScatterND",s,{reduction:Te(l)})},842947:(s,l,h,y,w,A,B,R,G)=>{n.jb("Attention",s,{numHeads:l,isUnidirectional:h,maskFilterValue:y,scale:w,doRotary:A,qkvHiddenSizes:B?Array.from(C().subarray(Number(R)>>>0,Number(R)+B>>>0)):[],pastPresentShareBuffer:!!G})},843219:s=>{n.jb("BiasAdd",s,void 0)},843274:s=>{n.jb("BiasSplitGelu",s,void 0)},843335:s=>{n.jb("FastGelu",s,void 0)},843391:(s,l,h,y,w,A,B,R,G,K,ae,le,_e,ke,Bt,Rp)=>{n.jb("Conv",s,{format:le?"NHWC":"NCHW",auto_pad:l,dilations:h?Array.from(C().subarray(Number(h)>>>0,Number(y)>>>0)):[],group:w,kernel_shape:A?Array.from(C().subarray(Number(A)>>>0,Number(B)>>>0)):[],pads:R?Array.from(C().subarray(Number(R)>>>0,Number(G)>>>0)):[],strides:K?Array.from(C().subarray(Number(K)>>>0,Number(ae)>>>0)):[],w_is_const:()=>!!ue()[Number(_e)>>>0],activation:Te(ke),activation_params:Bt?Array.from(de().subarray(Number(Bt)>>>0,Number(Rp)>>>0)):[]})},843975:s=>{n.jb("Gelu",s,void 0)},844027:(s,l,h,y,w,A,B,R,G)=>{n.jb("GroupQueryAttention",s,{numHeads:l,kvNumHeads:h,scale:y,softcap:w,doRotary:A,rotaryInterleaved:B,smoothSoftmax:R,localWindowSize:G})},844244:(s,l,h,y)=>{n.jb("LayerNormalization",s,{axis:l,epsilon:h,simplified:!!y})},844355:(s,l,h,y)=>{n.jb("LayerNormalization",s,{axis:l,epsilon:h,simplified:!!y})},844466:(s,l,h,y,w,A)=>{n.jb("MatMulNBits",s,{k:l,n:h,accuracyLevel:y,bits:w,blockSize:A})},844593:(s,l,h,y,w,A)=>{n.jb("MultiHeadAttention",s,{numHeads:l,isUnidirectional:h,maskFilterValue:y,scale:w,doRotary:A})},844752:(s,l)=>{n.jb("QuickGelu",s,{alpha:l})},844816:(s,l,h,y,w)=>{n.jb("RotaryEmbedding",s,{interleaved:!!l,numHeads:h,rotaryEmbeddingDim:y,scale:w})},844955:(s,l,h)=>{n.jb("SkipLayerNormalization",s,{epsilon:l,simplified:!!h})},845057:(s,l,h)=>{n.jb("SkipLayerNormalization",s,{epsilon:l,simplified:!!h})},845159:(s,l,h,y)=>{n.jb("GatherBlockQuantized",s,{gatherAxis:l,quantizeAxis:h,blockSize:y})},845280:s=>{n.Zb(s)},845314:(s,l)=>n.ac(Number(s),Number(l),n.Fb.dc,n.Fb.errors)};function bc(s,l,h){return mi(async()=>{await n.Xb(Number(s),Number(l),Number(h))})}function yc(){return typeof wasmOffsetConverter<"u"}class pn{name="ExitStatus";constructor(l){this.message=`Program terminated with exit(${l})`,this.status=l}}var Bo=s=>{s.terminate(),s.onmessage=()=>{}},mn=[],Do=s=>{ct.length==0&&(Wo(),Vo(ct[0]));var l=ct.pop();if(!l)return 6;Gt.push(l),wt[s.Ab]=l,l.Ab=s.Ab;var h={Bb:"run",fc:s.ec,Hb:s.Hb,Ab:s.Ab};return l.postMessage(h,s.Mb),0},lt=0,xe=(s,l,...h)=>{for(var y=2*h.length,w=Pn(),A=kn(8*y),B=A>>>3,R=0;R>>0]=G)}return s=Di(s,0,y,A,l),mr(w),s};function fn(s){if(u)return xe(0,1,s);if(I=s,!(0{if(I=s,u)throw Mo(s),"unwind";fn(s)},ct=[],Gt=[],Ro=[],wt={},Uo=s=>{var l=s.Ab;delete wt[l],ct.push(s),Gt.splice(Gt.indexOf(s),1),s.Ab=0,Mi(l)};function No(){Ro.forEach(s=>s())}var Vo=s=>new Promise(l=>{s.onmessage=w=>{var A=(w=w.data).Bb;if(w.Gb&&w.Gb!=cr()){var B=wt[w.Gb];B?B.postMessage(w,w.Mb):x(`Internal error! Worker sent a message "${A}" to target pthread ${w.Gb}, but that thread no longer exists!`)}else A==="checkMailbox"?nr():A==="spawnThread"?Do(w):A==="cleanupThread"?Uo(wt[w.hc]):A==="loaded"?(s.loaded=!0,l(s)):A==="alert"?alert(`Thread ${w.ic}: ${w.text}`):w.target==="setimmediate"?s.postMessage(w):A==="callHandler"?n[w.Qb](...w.args):A&&x(`worker sent an unknown command ${A}`)},s.onerror=w=>{throw x(`worker sent an error! ${w.filename}:${w.lineno}: ${w.message}`),w};var h,y=[];for(h of[])n.propertyIsEnumerable(h)&&y.push(h);s.postMessage({Bb:"load",Rb:y,kc:T,lc:E})});function Wo(){var s=new Worker((()=>{let l=URL;return import.meta.url>"file:"&&import.meta.url<"file;"?new l(/* asset import */ __webpack_require__(/*! ort.bundle.min.mjs */ "./node_modules/onnxruntime-web/dist/ort.bundle.min.mjs?46eb"), __webpack_require__.b):new URL(import.meta.url)})(),{type:"module",workerData:"em-pthread",name:"em-pthread"});ct.push(s)}var _c=s=>{Ce();var l=V()[s+52>>>2>>>0];s=V()[s+56>>>2>>>0],Ni(l,l-s),mr(l)},wc=(s,l)=>{lt=0,s=Vi(s,l),0>>=0);throw l>>>=0,h>>>=0,V()[y.Ib+16>>>2>>>0]=0,V()[y.Ib+4>>>2>>>0]=l,V()[y.Ib+8>>>2>>>0]=h,s}function Lo(s,l,h,y){return u?xe(2,1,s,l,h,y):Go(s,l,h,y)}function Go(s,l,h,y){if(s>>>=0,h>>>=0,y>>>=0,d===void 0)return 6;var w=[];return u&&w.length===0?Lo(s,l>>>=0,h,y):(s={ec:h,Ab:s,Hb:y,Mb:w},u?(s.Bb="spawnThread",postMessage(s,w),0):Do(s))}var Ho=typeof TextDecoder<"u"?new TextDecoder:void 0,Fo=(s,l=0,h=NaN)=>{var y=(l>>>=0)+h;for(h=l;s[h]&&!(h>=y);)++h;if(16(w=(240&w)==224?(15&w)<<12|A<<6|B:(7&w)<<18|A<<12|B<<6|63&s[l++])?y+=String.fromCharCode(w):(w-=65536,y+=String.fromCharCode(55296|w>>10,56320|1023&w))}}else y+=String.fromCharCode(w)}return y},Te=(s,l)=>(s>>>=0)?Fo(he(),s,l):"";function qo(s,l,h){return u?xe(3,1,s,l,h):0}function jo(s,l){if(u)return xe(4,1,s,l)}var Ko=s=>{for(var l=0,h=0;h=y?l++:2047>=y?l+=2:55296<=y&&57343>=y?(l+=4,++h):l+=3}return l},zt=(s,l,h)=>{var y=he();if(l>>>=0,0=B&&(B=65536+((1023&B)<<10)|1023&s.charCodeAt(++A)),127>=B){if(l>=h)break;y[l++>>>0]=B}else{if(2047>=B){if(l+1>=h)break;y[l++>>>0]=192|B>>6}else{if(65535>=B){if(l+2>=h)break;y[l++>>>0]=224|B>>12}else{if(l+3>=h)break;y[l++>>>0]=240|B>>18,y[l++>>>0]=128|B>>12&63}y[l++>>>0]=128|B>>6&63}y[l++>>>0]=128|63&B}}y[l>>>0]=0,s=l-w}else s=0;return s};function Zo(s,l){if(u)return xe(5,1,s,l)}function Qo(s,l,h){if(u)return xe(6,1,s,l,h)}function Yo(s,l,h){return u?xe(7,1,s,l,h):0}function Xo(s,l){if(u)return xe(8,1,s,l)}function Jo(s,l,h){if(u)return xe(9,1,s,l,h)}function ei(s,l,h,y){if(u)return xe(10,1,s,l,h,y)}function ti(s,l,h,y){if(u)return xe(11,1,s,l,h,y)}function ri(s,l,h,y){if(u)return xe(12,1,s,l,h,y)}function ni(s){if(u)return xe(13,1,s)}function oi(s,l){if(u)return xe(14,1,s,l)}function ii(s,l,h){if(u)return xe(15,1,s,l,h)}var ai,pt,xc=()=>dt(""),Ke=s=>{for(var l="";he()[s>>>0];)l+=ai[he()[s++>>>0]];return l},gn={},bn={},Sc={};function it(s,l,h={}){return function(y,w,A={}){var B=w.name;if(!y)throw new pt(`type "${B}" must have a positive integer typeid pointer`);if(bn.hasOwnProperty(y)){if(A.Sb)return;throw new pt(`Cannot register type '${B}' twice`)}bn[y]=w,delete Sc[y],gn.hasOwnProperty(y)&&(w=gn[y],delete gn[y],w.forEach(R=>R()))}(s,l,h)}var si=(s,l,h)=>{switch(l){case 1:return h?y=>ue()[y>>>0]:y=>he()[y>>>0];case 2:return h?y=>ye()[y>>>1>>>0]:y=>re()[y>>>1>>>0];case 4:return h?y=>C()[y>>>2>>>0]:y=>V()[y>>>2>>>0];case 8:return h?y=>Z[y>>>3]:y=>we[y>>>3];default:throw new TypeError(`invalid integer width (${l}): ${s}`)}};function Tc(s,l,h){h>>>=0,it(s>>>=0,{name:l=Ke(l>>>0),fromWireType:y=>y,toWireType:function(y,w){if(typeof w!="bigint"&&typeof w!="number")throw w=w===null?"null":(y=typeof w)=="object"||y==="array"||y==="function"?w.toString():""+w,new TypeError(`Cannot convert "${w}" to ${this.name}`);return typeof w=="number"&&(w=BigInt(w)),w},Cb:mt,readValueFromPointer:si(l,h,l.indexOf("u")==-1),Db:null})}var mt=8;function Ic(s,l,h,y){it(s>>>=0,{name:l=Ke(l>>>0),fromWireType:function(w){return!!w},toWireType:function(w,A){return A?h:y},Cb:mt,readValueFromPointer:function(w){return this.fromWireType(he()[w>>>0])},Db:null})}var yn=[],at=[];function _n(s){9<(s>>>=0)&&--at[s+1]==0&&(at[s]=void 0,yn.push(s))}var De=s=>{if(!s)throw new pt("Cannot use deleted val. handle = "+s);return at[s]},Ve=s=>{switch(s){case void 0:return 2;case null:return 4;case!0:return 6;case!1:return 8;default:let l=yn.pop()||at.length;return at[l]=s,at[l+1]=1,l}};function wn(s){return this.fromWireType(V()[s>>>2>>>0])}var Cc={name:"emscripten::val",fromWireType:s=>{var l=De(s);return _n(s),l},toWireType:(s,l)=>Ve(l),Cb:mt,readValueFromPointer:wn,Db:null};function Ac(s){return it(s>>>0,Cc)}var Ec=(s,l)=>{switch(l){case 4:return function(h){return this.fromWireType(de()[h>>>2>>>0])};case 8:return function(h){return this.fromWireType(ze()[h>>>3>>>0])};default:throw new TypeError(`invalid float width (${l}): ${s}`)}};function kc(s,l,h){h>>>=0,it(s>>>=0,{name:l=Ke(l>>>0),fromWireType:y=>y,toWireType:(y,w)=>w,Cb:mt,readValueFromPointer:Ec(l,h),Db:null})}function Pc(s,l,h,y,w){if(s>>>=0,h>>>=0,l=Ke(l>>>0),w===-1&&(w=4294967295),w=R=>R,y===0){var A=32-8*h;w=R=>R<>>A}var B=l.includes("unsigned")?function(R,G){return G>>>0}:function(R,G){return G};it(s,{name:l,fromWireType:w,toWireType:B,Cb:mt,readValueFromPointer:si(l,h,y!==0),Db:null})}function zc(s,l,h){function y(A){var B=V()[A>>>2>>>0];return A=V()[A+4>>>2>>>0],new w(ue().buffer,A,B)}var w=[Int8Array,Uint8Array,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array,BigInt64Array,BigUint64Array][l];it(s>>>=0,{name:h=Ke(h>>>0),fromWireType:y,Cb:mt,readValueFromPointer:y},{Sb:!0})}function Oc(s,l){it(s>>>=0,{name:l=Ke(l>>>0),fromWireType:function(h){for(var y,w=V()[h>>>2>>>0],A=h+4,B=A,R=0;R<=w;++R){var G=A+R;R!=w&&he()[G>>>0]!=0||(B=Te(B,G-B),y===void 0?y=B:(y+="\0",y+=B),B=G+1)}return Qe(h),y},toWireType:function(h,y){y instanceof ArrayBuffer&&(y=new Uint8Array(y));var w=typeof y=="string";if(!(w||y instanceof Uint8Array||y instanceof Uint8ClampedArray||y instanceof Int8Array))throw new pt("Cannot pass non-string to std::string");var A=w?Ko(y):y.length,B=pr(4+A+1),R=B+4;if(V()[B>>>2>>>0]=A,w)zt(y,R,A+1);else if(w)for(w=0;w>>0]=G}else for(w=0;w>>0]=y[w];return h!==null&&h.push(Qe,B),B},Cb:mt,readValueFromPointer:wn,Db(h){Qe(h)}})}var ui=typeof TextDecoder<"u"?new TextDecoder("utf-16le"):void 0,Bc=(s,l)=>{for(var h=s>>1,y=h+l/2;!(h>=y)&&re()[h>>>0];)++h;if(32<(h<<=1)-s&&ui)return ui.decode(he().slice(s,h));for(h="",y=0;!(y>=l/2);++y){var w=ye()[s+2*y>>>1>>>0];if(w==0)break;h+=String.fromCharCode(w)}return h},Dc=(s,l,h)=>{if(h??=2147483647,2>h)return 0;var y=l;h=(h-=2)<2*s.length?h/2:s.length;for(var w=0;w>>1>>>0]=A,l+=2}return ye()[l>>>1>>>0]=0,l-y},Mc=s=>2*s.length,Rc=(s,l)=>{for(var h=0,y="";!(h>=l/4);){var w=C()[s+4*h>>>2>>>0];if(w==0)break;++h,65536<=w?(w-=65536,y+=String.fromCharCode(55296|w>>10,56320|1023&w)):y+=String.fromCharCode(w)}return y},Uc=(s,l,h)=>{if(l>>>=0,h??=2147483647,4>h)return 0;var y=l;h=y+h-4;for(var w=0;w=A&&(A=65536+((1023&A)<<10)|1023&s.charCodeAt(++w)),C()[l>>>2>>>0]=A,(l+=4)+4>h)break}return C()[l>>>2>>>0]=0,l-y},Nc=s=>{for(var l=0,h=0;h=y&&++h,l+=4}return l};function Vc(s,l,h){if(s>>>=0,l>>>=0,h=Ke(h>>>=0),l===2)var y=Bc,w=Dc,A=Mc,B=R=>re()[R>>>1>>>0];else l===4&&(y=Rc,w=Uc,A=Nc,B=R=>V()[R>>>2>>>0]);it(s,{name:h,fromWireType:R=>{for(var G,K=V()[R>>>2>>>0],ae=R+4,le=0;le<=K;++le){var _e=R+4+le*l;le!=K&&B(_e)!=0||(ae=y(ae,_e-ae),G===void 0?G=ae:(G+="\0",G+=ae),ae=_e+l)}return Qe(R),G},toWireType:(R,G)=>{if(typeof G!="string")throw new pt(`Cannot pass non-string to C++ string type ${h}`);var K=A(G),ae=pr(4+K+l);return V()[ae>>>2>>>0]=K/l,w(G,ae+4,K+l),R!==null&&R.push(Qe,ae),ae},Cb:mt,readValueFromPointer:wn,Db(R){Qe(R)}})}function Wc(s,l){it(s>>>=0,{Tb:!0,name:l=Ke(l>>>0),Cb:0,fromWireType:()=>{},toWireType:()=>{}})}function Lc(s){An(s>>>0,!a,1,!i,131072,!1),No()}var vn=s=>{if(!te)try{if(s(),!(0>>=0,typeof Atomics.jc=="function"&&(Atomics.jc(C(),s>>>2,s).value.then(nr),s+=128,Atomics.store(C(),s>>>2,1))}var nr=()=>{var s=cr();s&&($n(s),vn(Ui))};function Gc(s,l){(s>>>=0)==l>>>0?setTimeout(nr):u?postMessage({Gb:s,Bb:"checkMailbox"}):(s=wt[s])&&s.postMessage({Bb:"checkMailbox"})}var xn=[];function Hc(s,l,h,y,w){for(l>>>=0,y/=2,xn.length=y,h=w>>>0>>>3,w=0;w>>0];return(l?cn[l]:Mp[s])(...xn)}var Fc=()=>{lt=0};function qc(s){s>>>=0,u?postMessage({Bb:"cleanupThread",hc:s}):Uo(wt[s])}function jc(s){}var or=(s,l)=>{var h=bn[s];if(h===void 0)throw s=zi(s),h=Ke(s),Qe(s),new pt(`${l} has unknown type ${h}`);return h},di=(s,l,h)=>{var y=[];return s=s.toWireType(y,h),y.length&&(V()[l>>>2>>>0]=Ve(y)),s};function Kc(s,l,h){return l>>>=0,h>>>=0,s=De(s>>>0),l=or(l,"emval::as"),di(l,h,s)}function Zc(s,l){return l>>>=0,s=De(s>>>0),(l=or(l,"emval::as")).toWireType(null,s)}var ir=s=>{try{s()}catch(l){dt(l)}},ft=0,Ze=null,li=0,ar=[],ci={},pi={},Qc=0,Sn=null,Yc=[];function mi(s){return function(l){if(!te){if(ft===0){var h=!1,y=!1;l((w=0)=>{if(!te&&(li=w,h=!0,y)){ft=2,ir(()=>Gi(Ze)),typeof MainLoop<"u"&&MainLoop.Pb&&MainLoop.resume(),w=!1;try{var A=function(){var G=C()[Ze+8>>>2>>>0];return G=Y[pi[G]],--lt,G()}()}catch(G){A=G,w=!0}var B=!1;if(!Ze){var R=Sn;R&&(Sn=null,(w?R.reject:R.resolve)(A),B=!0)}if(w&&!B)throw A}}),y=!0,h||(ft=1,Ze=function(){var w=pr(65548),A=w+12;V()[w>>>2>>>0]=A,V()[w+4>>>2>>>0]=A+65536,A=ar[0];var B=ci[A];return B===void 0&&(B=Qc++,ci[A]=B,pi[B]=A),A=B,C()[w+8>>>2>>>0]=A,w}(),typeof MainLoop<"u"&&MainLoop.Pb&&MainLoop.pause(),ir(()=>Wi(Ze)))}else ft===2?(ft=0,ir(Hi),Qe(Ze),Ze=null,Yc.forEach(vn)):dt(`invalid state: ${ft}`);return li}}(l=>{s().then(l)})}function Xc(s){return s>>>=0,mi(async()=>{var l=await De(s);return Ve(l)})}var sr=[];function Jc(s,l,h,y){return h>>>=0,y>>>=0,(s=sr[s>>>0])(null,l=De(l>>>0),h,y)}var ep={},ur=s=>{var l=ep[s];return l===void 0?Ke(s):l};function tp(s,l,h,y,w){return h>>>=0,y>>>=0,w>>>=0,(s=sr[s>>>0])(l=De(l>>>0),l[h=ur(h)],y,w)}var fi=()=>typeof globalThis=="object"?globalThis:Function("return this")();function rp(s){return(s>>>=0)==0?Ve(fi()):(s=ur(s),Ve(fi()[s]))}var np=s=>{var l=sr.length;return sr.push(s),l},op=(s,l)=>{for(var h=Array(s),y=0;y>>2>>>0],"parameter "+y);return h},hi=(s,l)=>Object.defineProperty(l,"name",{value:s});function ip(s,l,h){var y=(l=op(s,l>>>0)).shift();s--;var w=`return function (obj, func, destructorsRef, args) { +`,A=0,B=[];h===0&&B.push("obj");for(var R=["retType"],G=[y],K=0;Kae.name).join(", ")}) => ${y.name}>`,np(hi(h,s))}function ap(s){return s=ur(s>>>0),Ve(n[s])}function sp(s,l){return l>>>=0,s=De(s>>>0),l=De(l),Ve(s[l])}function up(s){9<(s>>>=0)&&(at[s+1]+=1)}function dp(){return Ve([])}function lp(s){s=De(s>>>0);for(var l=Array(s.length),h=0;h>>0))}function pp(){return Ve({})}function mp(s){for(var l=De(s>>>=0);l.length;){var h=l.pop();l.pop()(h)}_n(s)}function fp(s,l,h){l>>>=0,h>>>=0,s=De(s>>>0),l=De(l),h=De(h),s[l]=h}function hp(s,l){return l>>>=0,s=(s=or(s>>>0,"_emval_take_value")).readValueFromPointer(l),Ve(s)}function gp(s,l){s=-9007199254740992>s||9007199254740992>>=0,s=new Date(1e3*s),C()[l>>>2>>>0]=s.getUTCSeconds(),C()[l+4>>>2>>>0]=s.getUTCMinutes(),C()[l+8>>>2>>>0]=s.getUTCHours(),C()[l+12>>>2>>>0]=s.getUTCDate(),C()[l+16>>>2>>>0]=s.getUTCMonth(),C()[l+20>>>2>>>0]=s.getUTCFullYear()-1900,C()[l+24>>>2>>>0]=s.getUTCDay(),s=(s.getTime()-Date.UTC(s.getUTCFullYear(),0,1,0,0,0,0))/864e5|0,C()[l+28>>>2>>>0]=s}var gi=s=>s%4==0&&(s%100!=0||s%400==0),bi=[0,31,60,91,121,152,182,213,244,274,305,335],yi=[0,31,59,90,120,151,181,212,243,273,304,334];function bp(s,l){s=-9007199254740992>s||9007199254740992>>=0,s=new Date(1e3*s),C()[l>>>2>>>0]=s.getSeconds(),C()[l+4>>>2>>>0]=s.getMinutes(),C()[l+8>>>2>>>0]=s.getHours(),C()[l+12>>>2>>>0]=s.getDate(),C()[l+16>>>2>>>0]=s.getMonth(),C()[l+20>>>2>>>0]=s.getFullYear()-1900,C()[l+24>>>2>>>0]=s.getDay();var h=(gi(s.getFullYear())?bi:yi)[s.getMonth()]+s.getDate()-1|0;C()[l+28>>>2>>>0]=h,C()[l+36>>>2>>>0]=-60*s.getTimezoneOffset(),h=new Date(s.getFullYear(),6,1).getTimezoneOffset();var y=new Date(s.getFullYear(),0,1).getTimezoneOffset();s=0|(h!=y&&s.getTimezoneOffset()==Math.min(y,h)),C()[l+32>>>2>>>0]=s}function yp(s){s>>>=0;var l=new Date(C()[s+20>>>2>>>0]+1900,C()[s+16>>>2>>>0],C()[s+12>>>2>>>0],C()[s+8>>>2>>>0],C()[s+4>>>2>>>0],C()[s>>>2>>>0],0),h=C()[s+32>>>2>>>0],y=l.getTimezoneOffset(),w=new Date(l.getFullYear(),6,1).getTimezoneOffset(),A=new Date(l.getFullYear(),0,1).getTimezoneOffset(),B=Math.min(A,w);return 0>h?C()[s+32>>>2>>>0]=+(w!=A&&B==y):0>>2>>>0]=l.getDay(),h=(gi(l.getFullYear())?bi:yi)[l.getMonth()]+l.getDate()-1|0,C()[s+28>>>2>>>0]=h,C()[s>>>2>>>0]=l.getSeconds(),C()[s+4>>>2>>>0]=l.getMinutes(),C()[s+8>>>2>>>0]=l.getHours(),C()[s+12>>>2>>>0]=l.getDate(),C()[s+16>>>2>>>0]=l.getMonth(),C()[s+20>>>2>>>0]=l.getYear(),s=l.getTime(),BigInt(isNaN(s)?-1:s/1e3)}function _i(s,l,h,y,w,A,B){return u?xe(16,1,s,l,h,y,w,A,B):-52}function wi(s,l,h,y,w,A){if(u)return xe(17,1,s,l,h,y,w,A)}var Ht={},_p=()=>performance.timeOrigin+performance.now();function vi(s,l){if(u)return xe(18,1,s,l);if(Ht[s]&&(clearTimeout(Ht[s].id),delete Ht[s]),!l)return 0;var h=setTimeout(()=>{delete Ht[s],vn(()=>Ri(s,performance.timeOrigin+performance.now()))},l);return Ht[s]={id:h,qc:l},0}function wp(s,l,h,y){s>>>=0,l>>>=0,h>>>=0,y>>>=0;var w=new Date().getFullYear(),A=new Date(w,0,1).getTimezoneOffset();w=new Date(w,6,1).getTimezoneOffset();var B=Math.max(A,w);V()[s>>>2>>>0]=60*B,C()[l>>>2>>>0]=+(A!=w),s=(l=R=>{var G=Math.abs(R);return`UTC${0<=R?"-":"+"}${String(Math.floor(G/60)).padStart(2,"0")}${String(G%60).padStart(2,"0")}`})(A),l=l(w),wDate.now(),$p=1;function xp(s,l,h){if(!(0<=s&&3>=s))return 28;if(s===0)s=Date.now();else{if(!$p)return 52;s=performance.timeOrigin+performance.now()}return Z[h>>>0>>>3]=BigInt(Math.round(1e6*s)),0}var Tn=[],$i=(s,l)=>{Tn.length=0;for(var h;h=he()[s++>>>0];){var y=h!=105;l+=(y&=h!=112)&&l%8?4:0,Tn.push(h==112?V()[l>>>2>>>0]:h==106?Z[l>>>3]:h==105?C()[l>>>2>>>0]:ze()[l>>>3>>>0]),l+=y?8:4}return Tn};function Sp(s,l,h){return s>>>=0,l=$i(l>>>0,h>>>0),cn[s](...l)}function Tp(s,l,h){return s>>>=0,l=$i(l>>>0,h>>>0),cn[s](...l)}var Ip=()=>{};function Cp(s,l){return x(Te(s>>>0,l>>>0))}var Ap=()=>{throw lt+=1,"unwind"};function Ep(){return 4294901760}var kp=()=>navigator.hardwareConcurrency;function Pp(){return dt("Cannot use emscripten_pc_get_function without -sUSE_OFFSET_CONVERTER"),0}function zp(s){s>>>=0;var l=he().length;if(s<=l||4294901760=h;h*=2){var y=l*(1+.2/h);y=Math.min(y,s+100663296);e:{y=(Math.min(4294901760,65536*Math.ceil(Math.max(s,y)/65536))-T.buffer.byteLength+65535)/65536|0;try{T.grow(y),Ce();var w=1;break e}catch{}w=void 0}if(w)return!0}return!1}var dr=()=>(dt("Cannot use convertFrameToPC (needed by __builtin_return_address) without -sUSE_OFFSET_CONVERTER"),0),Ot={},xi=s=>{s.forEach(l=>{var h=dr();h&&(Ot[h]=l)})};function Op(){var s=Error().stack.toString().split(` +`);return s[0]=="Error"&&s.shift(),xi(s),Ot.Lb=dr(),Ot.cc=s,Ot.Lb}function Bp(s,l,h){if(s>>>=0,l>>>=0,Ot.Lb==s)var y=Ot.cc;else(y=Error().stack.toString().split(` +`))[0]=="Error"&&y.shift(),xi(y);for(var w=3;y[w]&&dr()!=s;)++w;for(s=0;s>>2>>>0]=dr();return s}var In,Cn={},Si=()=>{if(!In){var s,l={USER:"web_user",LOGNAME:"web_user",PATH:"/",PWD:"/",HOME:"/home/web_user",LANG:(typeof navigator=="object"&&navigator.languages&&navigator.languages[0]||"C").replace("-","_")+".UTF-8",_:"./this.program"};for(s in Cn)Cn[s]===void 0?delete l[s]:l[s]=Cn[s];var h=[];for(s in l)h.push(`${s}=${l[s]}`);In=h}return In};function Ti(s,l){if(u)return xe(19,1,s,l);s>>>=0,l>>>=0;var h=0;return Si().forEach((y,w)=>{var A=l+h;for(w=V()[s+4*w>>>2>>>0]=A,A=0;A>>0]=y.charCodeAt(A);ue()[w>>>0]=0,h+=y.length+1}),0}function Ii(s,l){if(u)return xe(20,1,s,l);s>>>=0,l>>>=0;var h=Si();V()[s>>>2>>>0]=h.length;var y=0;return h.forEach(w=>y+=w.length+1),V()[l>>>2>>>0]=y,0}function Ci(s){return u?xe(21,1,s):52}function Ai(s,l,h,y){return u?xe(22,1,s,l,h,y):52}function Ei(s,l,h,y){return u?xe(23,1,s,l,h,y):70}var Dp=[null,[],[]];function ki(s,l,h,y){if(u)return xe(24,1,s,l,h,y);l>>>=0,h>>>=0,y>>>=0;for(var w=0,A=0;A>>2>>>0],R=V()[l+4>>>2>>>0];l+=8;for(var G=0;G>>0],ae=Dp[s];K===0||K===10?((s===1?v:x)(Fo(ae)),ae.length=0):ae.push(K)}w+=R}return V()[y>>>2>>>0]=w,0}u||function(){for(var s=n.numThreads-1;s--;)Wo();mn.unshift(()=>{Pt++,function(l){u?l():Promise.all(ct.map(Vo)).then(l)}(()=>zo())})}();for(var Pi=Array(256),lr=0;256>lr;++lr)Pi[lr]=String.fromCharCode(lr);ai=Pi,pt=n.BindingError=class extends Error{constructor(s){super(s),this.name="BindingError"}},n.InternalError=class extends Error{constructor(s){super(s),this.name="InternalError"}},at.push(0,1,void 0,1,null,1,!0,1,!1,1),n.count_emval_handles=()=>at.length/2-5-yn.length;var Y,Mp=[fn,Mo,Lo,qo,jo,Zo,Qo,Yo,Xo,Jo,ei,ti,ri,ni,oi,ii,_i,wi,vi,Ti,Ii,Ci,Ai,Ei,ki];(async function(){function s(y,w){return Y=y.exports,Y=function(){var A=Y,B={};for(let[R,G]of Object.entries(A))B[R]=typeof G=="function"?(...K)=>{ar.push(R);try{return G(...K)}finally{te||(ar.pop(),Ze&&ft===1&&ar.length===0&&(ft=0,lt+=1,ir(Li),typeof Fibers<"u"&&Fibers.rc()))}}:G;return B}(),Y=function(){var A=Y,B=G=>K=>G(K)>>>0,R=G=>()=>G()>>>0;return(A=Object.assign({},A)).Da=B(A.Da),A.fb=R(A.fb),A.hb=B(A.hb),A.tb=B(A.tb),A.ub=R(A.ub),A.__cxa_get_exception_ptr=B(A.__cxa_get_exception_ptr),A}(),Ro.push(Y.ib),E=w,zo(),Y}Pt++;var l=Oo();if(n.instantiateWasm)return new Promise(y=>{n.instantiateWasm(l,(w,A)=>{s(w,A),y(w.exports)})});if(u)return new Promise(y=>{ve=w=>{var A=new WebAssembly.Instance(w,Oo());y(s(A,w))}});kt??=n.locateFile?n.locateFile?n.locateFile("ort-wasm-simd-threaded.jsep.wasm",_):_+"ort-wasm-simd-threaded.jsep.wasm":new URL(/* asset import */ __webpack_require__(/*! ort-wasm-simd-threaded.jsep.wasm */ "./node_modules/onnxruntime-web/dist/ort-wasm-simd-threaded.jsep.wasm"), __webpack_require__.b).href;try{var h=await async function(y){var w=kt;if(!j&&typeof WebAssembly.instantiateStreaming=="function"&&!X(w))try{var A=fetch(w,{credentials:"same-origin"});return await WebAssembly.instantiateStreaming(A,y)}catch(B){x(`wasm streaming compile failed: ${B}`),x("falling back to ArrayBuffer instantiation")}return async function(B,R){try{var G=await async function(K){if(!j)try{var ae=await m(K);return new Uint8Array(ae)}catch{}if(K==kt&&j)K=new Uint8Array(j);else{if(!f)throw"both async and sync fetching of the wasm failed";K=f(K)}return K}(B);return await WebAssembly.instantiate(G,R)}catch(K){x(`failed to asynchronously prepare wasm: ${K}`),dt(K)}}(w,y)}(l);return s(h.instance,h.module)}catch(y){return r(y),Promise.reject(y)}})();var zi=s=>(zi=Y.Da)(s),Oi=()=>(Oi=Y.Ea)();n._OrtInit=(s,l)=>(n._OrtInit=Y.Fa)(s,l),n._OrtGetLastError=(s,l)=>(n._OrtGetLastError=Y.Ga)(s,l),n._OrtCreateSessionOptions=(s,l,h,y,w,A,B,R,G,K)=>(n._OrtCreateSessionOptions=Y.Ha)(s,l,h,y,w,A,B,R,G,K),n._OrtAppendExecutionProvider=(s,l,h,y,w)=>(n._OrtAppendExecutionProvider=Y.Ia)(s,l,h,y,w),n._OrtAddFreeDimensionOverride=(s,l,h)=>(n._OrtAddFreeDimensionOverride=Y.Ja)(s,l,h),n._OrtAddSessionConfigEntry=(s,l,h)=>(n._OrtAddSessionConfigEntry=Y.Ka)(s,l,h),n._OrtReleaseSessionOptions=s=>(n._OrtReleaseSessionOptions=Y.La)(s),n._OrtCreateSession=(s,l,h)=>(n._OrtCreateSession=Y.Ma)(s,l,h),n._OrtReleaseSession=s=>(n._OrtReleaseSession=Y.Na)(s),n._OrtGetInputOutputCount=(s,l,h)=>(n._OrtGetInputOutputCount=Y.Oa)(s,l,h),n._OrtGetInputOutputMetadata=(s,l,h,y)=>(n._OrtGetInputOutputMetadata=Y.Pa)(s,l,h,y),n._OrtFree=s=>(n._OrtFree=Y.Qa)(s),n._OrtCreateTensor=(s,l,h,y,w,A)=>(n._OrtCreateTensor=Y.Ra)(s,l,h,y,w,A),n._OrtGetTensorData=(s,l,h,y,w)=>(n._OrtGetTensorData=Y.Sa)(s,l,h,y,w),n._OrtReleaseTensor=s=>(n._OrtReleaseTensor=Y.Ta)(s),n._OrtCreateRunOptions=(s,l,h,y)=>(n._OrtCreateRunOptions=Y.Ua)(s,l,h,y),n._OrtAddRunConfigEntry=(s,l,h)=>(n._OrtAddRunConfigEntry=Y.Va)(s,l,h),n._OrtReleaseRunOptions=s=>(n._OrtReleaseRunOptions=Y.Wa)(s),n._OrtCreateBinding=s=>(n._OrtCreateBinding=Y.Xa)(s),n._OrtBindInput=(s,l,h)=>(n._OrtBindInput=Y.Ya)(s,l,h),n._OrtBindOutput=(s,l,h,y)=>(n._OrtBindOutput=Y.Za)(s,l,h,y),n._OrtClearBoundOutputs=s=>(n._OrtClearBoundOutputs=Y._a)(s),n._OrtReleaseBinding=s=>(n._OrtReleaseBinding=Y.$a)(s),n._OrtRunWithBinding=(s,l,h,y,w)=>(n._OrtRunWithBinding=Y.ab)(s,l,h,y,w),n._OrtRun=(s,l,h,y,w,A,B,R)=>(n._OrtRun=Y.bb)(s,l,h,y,w,A,B,R),n._OrtEndProfiling=s=>(n._OrtEndProfiling=Y.cb)(s),n._JsepOutput=(s,l,h)=>(n._JsepOutput=Y.db)(s,l,h),n._JsepGetNodeName=s=>(n._JsepGetNodeName=Y.eb)(s);var cr=()=>(cr=Y.fb)(),Qe=n._free=s=>(Qe=n._free=Y.gb)(s),pr=n._malloc=s=>(pr=n._malloc=Y.hb)(s),An=(s,l,h,y,w,A)=>(An=Y.kb)(s,l,h,y,w,A),Bi=()=>(Bi=Y.lb)(),Di=(s,l,h,y,w)=>(Di=Y.mb)(s,l,h,y,w),Mi=s=>(Mi=Y.nb)(s),En=s=>(En=Y.ob)(s),Ri=(s,l)=>(Ri=Y.pb)(s,l),Ui=()=>(Ui=Y.qb)(),Ni=(s,l)=>(Ni=Y.rb)(s,l),mr=s=>(mr=Y.sb)(s),kn=s=>(kn=Y.tb)(s),Pn=()=>(Pn=Y.ub)(),Vi=n.dynCall_ii=(s,l)=>(Vi=n.dynCall_ii=Y.vb)(s,l),Wi=s=>(Wi=Y.wb)(s),Li=()=>(Li=Y.xb)(),Gi=s=>(Gi=Y.yb)(s),Hi=()=>(Hi=Y.zb)();return n.stackSave=()=>Pn(),n.stackRestore=s=>mr(s),n.stackAlloc=s=>kn(s),n.setValue=function(s,l,h="i8"){switch(h.endsWith("*")&&(h="*"),h){case"i1":case"i8":ue()[s>>>0]=l;break;case"i16":ye()[s>>>1>>>0]=l;break;case"i32":C()[s>>>2>>>0]=l;break;case"i64":Z[s>>>3]=BigInt(l);break;case"float":de()[s>>>2>>>0]=l;break;case"double":ze()[s>>>3>>>0]=l;break;case"*":V()[s>>>2>>>0]=l;break;default:dt(`invalid type for setValue: ${h}`)}},n.getValue=function(s,l="i8"){switch(l.endsWith("*")&&(l="*"),l){case"i1":case"i8":return ue()[s>>>0];case"i16":return ye()[s>>>1>>>0];case"i32":return C()[s>>>2>>>0];case"i64":return Z[s>>>3];case"float":return de()[s>>>2>>>0];case"double":return ze()[s>>>3>>>0];case"*":return V()[s>>>2>>>0];default:dt(`invalid type for getValue: ${l}`)}},n.UTF8ToString=Te,n.stringToUTF8=zt,n.lengthBytesUTF8=Ko,function s(){if(0{"use strict";yr();Ea=typeof location>"u"?void 0:location.origin,Gn=import.meta.url>"file:"&&import.meta.url<"file;",jp=()=>{if(true){if(Gn){let e=URL;return new URL(new e(/* asset import */ __webpack_require__(/*! ort.bundle.min.mjs */ "./node_modules/onnxruntime-web/dist/ort.bundle.min.mjs?46eb"), __webpack_require__.b).href,Ea).href}return import.meta.url}},Ue=jp(),ka=()=>{if(Ue&&!Ue.startsWith("blob:"))return Ue.substring(0,Ue.lastIndexOf("/")+1)},Ln=(e,t)=>{try{let r=t??Ue;return(r?new URL(e,r):new URL(e)).origin===Ea}catch{return!1}},Kp=(e,t)=>{let r=t??Ue;try{return(r?new URL(e,r):new URL(e)).href}catch{return}},Zp=(e,t)=>`${t??"./"}${e}`,Pa=async e=>{let r=await(await fetch(e,{credentials:"same-origin"})).blob();return URL.createObjectURL(r)},Qp=async e=>(await import(/*webpackIgnore:true*/e)).default,Ca=(xa(),Ft($a)).default,za=async()=>{if(!Ue)throw new Error("Failed to load proxy worker: cannot determine the script source URL.");if(Ln(Ue))return[void 0,Ca()];let e=await Pa(Ue);return[e,Ca(e)]},Aa=(Ia(),Ft(Ta)).default,Oa=async(e,t,r)=>{if(!e&&!t&&Aa&&Ue&&Ln(Ue))return[void 0,Aa];{let n="ort-wasm-simd-threaded.jsep.mjs",o=e??Kp(n,t),i= true&&r&&o&&!Ln(o,t),a=i?await Pa(o):o??Zp(n,t);return[i?a:void 0,await Qp(a)]}}});var Hn,Fn,Ar,Ba,Yp,Xp,Jp,wr,fe,ht=U(()=>{"use strict";_r();Fn=!1,Ar=!1,Ba=!1,Yp=()=>{if(typeof SharedArrayBuffer>"u")return!1;try{return typeof MessageChannel<"u"&&new MessageChannel().port1.postMessage(new SharedArrayBuffer(1)),WebAssembly.validate(new Uint8Array([0,97,115,109,1,0,0,0,1,4,1,96,0,0,3,2,1,0,5,4,1,3,1,1,10,11,1,9,0,65,0,254,16,2,0,26,11]))}catch{return!1}},Xp=()=>{try{return WebAssembly.validate(new Uint8Array([0,97,115,109,1,0,0,0,1,4,1,96,0,0,3,2,1,0,10,30,1,28,0,65,0,253,15,253,12,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,253,186,1,26,11]))}catch{return!1}},Jp=()=>{try{return WebAssembly.validate(new Uint8Array([0,97,115,109,1,0,0,0,1,5,1,96,0,1,123,3,2,1,0,10,19,1,17,0,65,1,253,15,65,2,253,15,65,3,253,15,253,147,2,11]))}catch{return!1}},wr=async e=>{if(Fn)return Promise.resolve();if(Ar)throw new Error("multiple calls to 'initializeWebAssembly()' detected.");if(Ba)throw new Error("previous call to 'initializeWebAssembly()' failed.");Ar=!0;let t=e.initTimeout,r=e.numThreads;if(e.simd!==!1){if(e.simd==="relaxed"){if(!Jp())throw new Error("Relaxed WebAssembly SIMD is not supported in the current environment.")}else if(!Xp())throw new Error("WebAssembly SIMD is not supported in the current environment.")}let n=Yp();r>1&&!n&&(typeof self<"u"&&!self.crossOriginIsolated&&console.warn("env.wasm.numThreads is set to "+r+", but this will not work unless you enable crossOriginIsolated mode. See https://web.dev/cross-origin-isolation-guide/ for more info."),console.warn("WebAssembly multi-threading is not supported in the current environment. Falling back to single-threading."),e.numThreads=r=1);let o=e.wasmPaths,i=typeof o=="string"?o:void 0,a=o?.mjs,u=a?.href??a,d=o?.wasm,c=d?.href??d,p=e.wasmBinary,[m,f]=await Oa(u,i,r>1),b=!1,g=[];if(t>0&&g.push(new Promise(_=>{setTimeout(()=>{b=!0,_()},t)})),g.push(new Promise((_,S)=>{let $={numThreads:r};if(p)$.wasmBinary=p;else if(c||i)$.locateFile=v=>c??i+v;else if(u&&u.indexOf("blob:")!==0)$.locateFile=v=>new URL(v,u).href;else if(m){let v=ka();v&&($.locateFile=x=>v+x)}f($).then(v=>{Ar=!1,Fn=!0,Hn=v,_(),m&&URL.revokeObjectURL(m)},v=>{Ar=!1,Ba=!0,S(v)})})),await Promise.race(g),b)throw new Error(`WebAssembly backend initializing failed due to timeout: ${t}ms`)},fe=()=>{if(Fn&&Hn)return Hn;throw new Error("WebAssembly is not initialized yet.")}});var Ne,Kt,pe,Er=U(()=>{"use strict";ht();Ne=(e,t)=>{let r=fe(),n=r.lengthBytesUTF8(e)+1,o=r._malloc(n);return r.stringToUTF8(e,o,n),t.push(o),o},Kt=(e,t,r,n)=>{if(typeof e=="object"&&e!==null){if(r.has(e))throw new Error("Circular reference in options");r.add(e)}Object.entries(e).forEach(([o,i])=>{let a=t?t+o:o;if(typeof i=="object")Kt(i,a+".",r,n);else if(typeof i=="string"||typeof i=="number")n(a,i.toString());else if(typeof i=="boolean")n(a,i?"1":"0");else throw new Error(`Can't handle extra config type: ${typeof i}`)})},pe=e=>{let t=fe(),r=t.stackSave();try{let n=t.PTR_SIZE,o=t.stackAlloc(2*n);t._OrtGetLastError(o,o+n);let i=Number(t.getValue(o,n===4?"i32":"i64")),a=t.getValue(o+n,"*"),u=a?t.UTF8ToString(a):"";throw new Error(`${e} ERROR_CODE: ${i}, ERROR_MESSAGE: ${u}`)}finally{t.stackRestore(r)}}});var Da,Ma=U(()=>{"use strict";ht();Er();Da=e=>{let t=fe(),r=0,n=[],o=e||{};try{if(e?.logSeverityLevel===void 0)o.logSeverityLevel=2;else if(typeof e.logSeverityLevel!="number"||!Number.isInteger(e.logSeverityLevel)||e.logSeverityLevel<0||e.logSeverityLevel>4)throw new Error(`log serverity level is not valid: ${e.logSeverityLevel}`);if(e?.logVerbosityLevel===void 0)o.logVerbosityLevel=0;else if(typeof e.logVerbosityLevel!="number"||!Number.isInteger(e.logVerbosityLevel))throw new Error(`log verbosity level is not valid: ${e.logVerbosityLevel}`);e?.terminate===void 0&&(o.terminate=!1);let i=0;return e?.tag!==void 0&&(i=Ne(e.tag,n)),r=t._OrtCreateRunOptions(o.logSeverityLevel,o.logVerbosityLevel,!!o.terminate,i),r===0&&pe("Can't create run options."),e?.extra!==void 0&&Kt(e.extra,"",new WeakSet,(a,u)=>{let d=Ne(a,n),c=Ne(u,n);t._OrtAddRunConfigEntry(r,d,c)!==0&&pe(`Can't set a run config entry: ${a} - ${u}.`)}),[r,n]}catch(i){throw r!==0&&t._OrtReleaseRunOptions(r),n.forEach(a=>t._free(a)),i}}});var em,tm,rm,kr,nm,Ra,Ua=U(()=>{"use strict";ht();Er();em=e=>{switch(e){case"disabled":return 0;case"basic":return 1;case"extended":return 2;case"all":return 99;default:throw new Error(`unsupported graph optimization level: ${e}`)}},tm=e=>{switch(e){case"sequential":return 0;case"parallel":return 1;default:throw new Error(`unsupported execution mode: ${e}`)}},rm=e=>{e.extra||(e.extra={}),e.extra.session||(e.extra.session={});let t=e.extra.session;t.use_ort_model_bytes_directly||(t.use_ort_model_bytes_directly="1"),e.executionProviders&&e.executionProviders.some(r=>(typeof r=="string"?r:r.name)==="webgpu")&&(e.enableMemPattern=!1)},kr=(e,t,r,n)=>{let o=Ne(t,n),i=Ne(r,n);fe()._OrtAddSessionConfigEntry(e,o,i)!==0&&pe(`Can't set a session config entry: ${t} - ${r}.`)},nm=async(e,t,r)=>{for(let n of t){let o=typeof n=="string"?n:n.name,i=[];switch(o){case"webnn":if(o="WEBNN",typeof n!="string"){let m=n?.deviceType;m&&kr(e,"deviceType",m,r)}break;case"webgpu":if(o="JS",typeof n!="string"){let p=n;if(p?.preferredLayout){if(p.preferredLayout!=="NCHW"&&p.preferredLayout!=="NHWC")throw new Error(`preferredLayout must be either 'NCHW' or 'NHWC': ${p.preferredLayout}`);kr(e,"preferredLayout",p.preferredLayout,r)}}break;case"wasm":case"cpu":continue;default:throw new Error(`not supported execution provider: ${o}`)}let a=Ne(o,r),u=i.length,d=0,c=0;if(u>0){d=fe()._malloc(u*fe().PTR_SIZE),r.push(d),c=fe()._malloc(u*fe().PTR_SIZE),r.push(c);for(let p=0;p{let t=fe(),r=0,n=[],o=e||{};rm(o);try{let i=em(o.graphOptimizationLevel??"all"),a=tm(o.executionMode??"sequential"),u=typeof o.logId=="string"?Ne(o.logId,n):0,d=o.logSeverityLevel??2;if(!Number.isInteger(d)||d<0||d>4)throw new Error(`log serverity level is not valid: ${d}`);let c=o.logVerbosityLevel??0;if(!Number.isInteger(c)||c<0||c>4)throw new Error(`log verbosity level is not valid: ${c}`);let p=typeof o.optimizedModelFilePath=="string"?Ne(o.optimizedModelFilePath,n):0;if(r=t._OrtCreateSessionOptions(i,!!o.enableCpuMemArena,!!o.enableMemPattern,a,!!o.enableProfiling,0,u,d,c,p),r===0&&pe("Can't create session options."),o.executionProviders&&await nm(r,o.executionProviders,n),o.enableGraphCapture!==void 0){if(typeof o.enableGraphCapture!="boolean")throw new Error(`enableGraphCapture must be a boolean value: ${o.enableGraphCapture}`);kr(r,"enableGraphCapture",o.enableGraphCapture.toString(),n)}if(o.freeDimensionOverrides)for(let[m,f]of Object.entries(o.freeDimensionOverrides)){if(typeof m!="string")throw new Error(`free dimension override name must be a string: ${m}`);if(typeof f!="number"||!Number.isInteger(f)||f<0)throw new Error(`free dimension override value must be a non-negative integer: ${f}`);let b=Ne(m,n);t._OrtAddFreeDimensionOverride(r,b,f)!==0&&pe(`Can't set a free dimension override: ${m} - ${f}.`)}return o.extra!==void 0&&Kt(o.extra,"",new WeakSet,(m,f)=>{kr(r,m,f,n)}),[r,n]}catch(i){throw r!==0&&t._OrtReleaseSessionOptions(r)!==0&&pe("Can't release session options."),n.forEach(a=>t._free(a)),i}}});var Mt,Ye,gt,Pr,Zt,zr,Or,qn,ee=U(()=>{"use strict";Mt=e=>{switch(e){case"int8":return 3;case"uint8":return 2;case"bool":return 9;case"int16":return 5;case"uint16":return 4;case"int32":return 6;case"uint32":return 12;case"float16":return 10;case"float32":return 1;case"float64":return 11;case"string":return 8;case"int64":return 7;case"uint64":return 13;case"int4":return 22;case"uint4":return 21;default:throw new Error(`unsupported data type: ${e}`)}},Ye=e=>{switch(e){case 3:return"int8";case 2:return"uint8";case 9:return"bool";case 5:return"int16";case 4:return"uint16";case 6:return"int32";case 12:return"uint32";case 10:return"float16";case 1:return"float32";case 11:return"float64";case 8:return"string";case 7:return"int64";case 13:return"uint64";case 22:return"int4";case 21:return"uint4";default:throw new Error(`unsupported data type: ${e}`)}},gt=(e,t)=>{let r=[-1,4,1,1,2,2,4,8,-1,1,2,8,4,8,-1,-1,-1,-1,-1,-1,-1,.5,.5][e],n=typeof t=="number"?t:t.reduce((o,i)=>o*i,1);return r>0?Math.ceil(n*r):void 0},Pr=e=>{switch(e){case"float16":return typeof Float16Array<"u"&&Float16Array.from?Float16Array:Uint16Array;case"float32":return Float32Array;case"uint8":return Uint8Array;case"int8":return Int8Array;case"uint16":return Uint16Array;case"int16":return Int16Array;case"int32":return Int32Array;case"bool":return Uint8Array;case"float64":return Float64Array;case"uint32":return Uint32Array;case"int64":return BigInt64Array;case"uint64":return BigUint64Array;default:throw new Error(`unsupported type: ${e}`)}},Zt=e=>{switch(e){case"verbose":return 0;case"info":return 1;case"warning":return 2;case"error":return 3;case"fatal":return 4;default:throw new Error(`unsupported logging level: ${e}`)}},zr=e=>e==="float32"||e==="float16"||e==="int32"||e==="int64"||e==="uint32"||e==="uint8"||e==="bool"||e==="uint4"||e==="int4",Or=e=>e==="float32"||e==="float16"||e==="int32"||e==="int64"||e==="uint32"||e==="uint64"||e==="int8"||e==="uint8"||e==="bool"||e==="uint4"||e==="int4",qn=e=>{switch(e){case"none":return 0;case"cpu":return 1;case"cpu-pinned":return 2;case"texture":return 3;case"gpu-buffer":return 4;case"ml-tensor":return 5;default:throw new Error(`unsupported data location: ${e}`)}}});var Qt,jn=U(()=>{"use strict";yr();Qt=async e=>{if(typeof e=="string")if(false){}else{let t=await fetch(e);if(!t.ok)throw new Error(`failed to load external data file: ${e}`);let r=t.headers.get("Content-Length"),n=r?parseInt(r,10):0;if(n<1073741824)return new Uint8Array(await t.arrayBuffer());{if(!t.body)throw new Error(`failed to load external data file: ${e}, no response body.`);let o=t.body.getReader(),i;try{i=new ArrayBuffer(n)}catch(u){if(u instanceof RangeError){let d=Math.ceil(n/65536);i=new WebAssembly.Memory({initial:d,maximum:d}).buffer}else throw u}let a=0;for(;;){let{done:u,value:d}=await o.read();if(u)break;let c=d.byteLength;new Uint8Array(i,a,c).set(d),a+=c}return new Uint8Array(i,0,n)}}else return e instanceof Blob?new Uint8Array(await e.arrayBuffer()):e instanceof Uint8Array?e:new Uint8Array(e)}});var om,im,Na,Va,Br,am,se,Xe=U(()=>{"use strict";ee();om=["V","I","W","E","F"],im=(e,t)=>{console.log(`[${om[e]},${new Date().toISOString()}]${t}`)},Br=(e,t)=>{Na=e,Va=t},am=(e,t)=>{let r=Zt(e),n=Zt(Na);r>=n&&im(r,typeof t=="function"?t():t)},se=(...e)=>{Va&&am(...e)}});var Kn,Je,k,Tt,Dr,Wa,La,ne=U(()=>{"use strict";Kn=class{static calcMatMulShape(t,r){return t[1]!==r[0]?void 0:[t[0],r[1]]}},Je=class{static calcShape(t,r,n=!1){let o=t.length,i=r.length;if(o===0)return r;if(i===0)return t;let a=Math.max(t.length,r.length),u=new Array(a);if(n){if(o<2||i<2)return;let d=Kn.calcMatMulShape([t[o-2],t[o-1]],[r[i-2],r[i-1]]);if(d===void 0)return;[u[a-2],u[a-1]]=d}for(let d=n?3:1;d<=a;d++){let c=o-d<0?1:t[o-d],p=i-d<0?1:r[i-d];if(c!==p&&c>1&&p>1)return;let m=Math.max(c,p);if(c&&p)u[a-d]=Math.max(c,p);else{if(m>1)return;u[a-d]=0}}return u}static isValidBroadcast(t,r){let n=t.length,o=r.length;if(n>o)return!1;for(let i=1;i<=n;i++)if(t[n-i]!==1&&t[n-i]!==r[o-i])return!1;return!0}},k=class e{static size(t){return e.getSizeFromDimensionRange(t,0,t.length)}static convertShape(t,r=4){let n=t.length;if(n===0)return[];let o=new Array(n),i=n-1;for(;i>=0;){if(t[i]%r===0){o[i]=t[i]/r;break}if(r%t[i]!==0)throw new Error("cannot convert shape");o[i]=1,r/=t[i],i--}for(i--;i>=0;i--)o[i]=t[i];return o}static sizeFromDimension(t,r){if(r<0||r>t.length)throw new Error(`invalid dimension of ${r} for sizeFromDimension as Tensor has ${t.length} dimensions.`);return e.getSizeFromDimensionRange(t,r,t.length)}static sizeToDimension(t,r){if(r<0||r>t.length)throw new Error(`invalid dimension of ${r} for sizeToDimension as Tensor has ${t.length} dimensions.`);return e.getSizeFromDimensionRange(t,0,r)}static getSizeFromDimensionRange(t,r,n){let o=1;for(let i=r;i=0;--o)n[o]=n[o+1]*t[o+1];return n}static normalizeAxis(t,r){if(t<-r&&t>=r)throw new Error("unsupported axis for this operation.");return t<0?t+r:t}static normalizeAxes(t,r){return t.map(n=>this.normalizeAxis(n,r??t.length))}static sortBasedOnPerm(t,r){return r?r.map(n=>t[n]):t.slice().reverse()}static padShape(t,r){let n=t.length;return t.map((o,i)=>o+r[i]+r[i+n])}static areEqual(t,r){return t.length!==r.length?!1:t.every((n,o)=>n===r[o])}},Tt=class e{static adjustPoolAttributes(t,r,n,o,i,a){if(!t&&n.length!==r.length-2)throw new Error("length of specified kernel shapes should be 2 less than length of input dimensions");if(t)for(let u=0;u=n.length?n.push(r[u+2]):n[u]=r[u+2];for(let u=0;u=n[u]||a[u+n.length]>=n[u])throw new Error("pads should be smaller than kernel")}}static adjustPadsBasedOnAutoPad(t,r,n,o,i,a,u){if(u){if(i.length!==2*(t.length-2))throw new Error("length of pads should be twice the length of data dimensions");if(r.length!==t.length-2)throw new Error("length of strides should be the length of data dimensions");if(o.length!==t.length-2)throw new Error("length of kernel shapes should be the length of data dimensions");for(let d=0;d{"use strict";ee();Mr=(e,t)=>new(Pr(t))(e)});var Yn,Ha,sm,Ga,um,Fa,Rr,Ur,Qn,qa,ja=U(()=>{"use strict";Xe();Yn=(e,t=!0)=>{if(e.byteLength%8!==0)throw new Error("Invalid Uint8Array length - must be a multiple of 8 (BigInt).");let r=e.byteLength/8,n=new BigInt64Array(e.buffer,e.byteOffset,r),o=new Int32Array(r);for(let i=0;i2147483647n||a<-2147483648n)throw new Error(`Overflow occurred when converting BigInt to Int32 at index ${i}: ${a}`);o[i]=Number(a)}return t?new Uint8Array(o.buffer):o},Ha=(e,t=!0)=>{if(e.byteLength%4!==0)throw new Error("Invalid Uint8Array length - must be a multiple of 4 (Int32).");let r=e.byteLength/4,n=new Int32Array(e.buffer,e.byteOffset,r),o=BigInt64Array.from(n,BigInt);return t?new Uint8Array(o.buffer):o},sm=1,Ga=()=>sm++,um=new Map([["float32",32],["float16",16],["int32",32],["uint32",32],["int64",64],["uint64",64],["int8",8],["uint8",8],["int4",4],["uint4",4]]),Fa=(e,t)=>{let r=um.get(e);if(!r)throw new Error("Unsupported data type.");return t.length>0?Math.ceil(t.reduce((n,o)=>n*o)*r/8):0},Rr=class{constructor(t){this.shouldConvertInt64toInt32=!1;this.isInt64ToInt32Converted=!1;let{sessionId:r,context:n,tensor:o,dataType:i,shape:a,shouldConvertInt64toInt32:u=!1}=t;this.sessionId=r,this.mlContext=n,this.mlTensor=o,this.dataType=i,this.tensorShape=a,this.shouldConvertInt64toInt32=u}get tensor(){return this.mlTensor}get type(){return this.dataType}get shape(){return this.tensorShape}get byteLength(){return Fa(this.dataType,this.tensorShape)}destroy(){se("verbose",()=>"[WebNN] TensorWrapper.destroy"),this.mlTensor.destroy()}write(t){this.mlContext.writeTensor(this.mlTensor,t)}async read(t,r){if(t){let n=await this.mlContext.readTensor(this.mlTensor),o=Ha(new Uint8Array(n));if(r){(r instanceof ArrayBuffer?new Uint8Array(r):new Uint8Array(r.buffer,r.byteOffset,r.byteLength)).set(o);return}else return o.buffer}else return r?this.mlContext.readTensor(this.mlTensor,r):this.mlContext.readTensor(this.mlTensor)}canReuseTensor(t,r,n){return this.mlContext===t&&this.dataType===r&&this.tensorShape.length===n.length&&this.tensorShape.every((o,i)=>o===n[i])}setIsInt64ToInt32Converted(t){this.isInt64ToInt32Converted=t}},Ur=class{constructor(t,r){this.tensorManager=t;this.wrapper=r}get tensorWrapper(){return this.wrapper}releaseTensor(){this.tensorWrapper&&(this.tensorManager.releaseTensor(this.tensorWrapper),this.wrapper=void 0)}async ensureTensor(t,r,n,o){let i=r,a=this.tensorManager.getMLContext(t),u=i==="int64"&&!a.opSupportLimits().input.dataTypes.includes("int64");if(u&&(i="int32",se("verbose",()=>"[WebNN] TensorIdTracker.ensureTensor: convert dataType from int64 to int32")),this.wrapper){if(this.wrapper.canReuseTensor(a,i,n))return this.wrapper.tensor;if(o){if(this.wrapper.byteLength!==Fa(i,n))throw new Error("Unable to copy data to tensor with different size.");this.activeUpload=new Uint8Array(await this.wrapper.read())}this.tensorManager.releaseTensor(this.wrapper)}let d=typeof MLTensorUsage>"u"?void 0:MLTensorUsage.READ|MLTensorUsage.WRITE;return this.wrapper=await this.tensorManager.getCachedTensor(t,i,n,d,!0,!0,u),o&&this.activeUpload&&(this.wrapper.write(this.activeUpload),this.activeUpload=void 0),this.wrapper.tensor}upload(t){let r=t;if(this.wrapper)if(this.wrapper.shouldConvertInt64toInt32&&(r=Yn(t,!0),this.wrapper.setIsInt64ToInt32Converted(!0)),r.byteLength===this.wrapper.byteLength){this.wrapper.write(r);return}else se("verbose",()=>"Data size does not match tensor size. Releasing tensor."),this.releaseTensor();this.activeUpload?this.activeUpload.set(r):this.activeUpload=new Uint8Array(r)}async download(t){if(this.activeUpload){let r=this.wrapper?.isInt64ToInt32Converted?Ha(this.activeUpload):this.activeUpload;if(t){t instanceof ArrayBuffer?new Uint8Array(t).set(r):new Uint8Array(t.buffer,t.byteOffset,t.byteLength).set(r);return}else return r.buffer}if(!this.wrapper)throw new Error("Tensor has not been created.");return t?this.wrapper.read(this.wrapper?.shouldConvertInt64toInt32,t):this.wrapper.read(this.wrapper?.shouldConvertInt64toInt32)}},Qn=class{constructor(t){this.backend=t;this.tensorTrackersById=new Map;this.freeTensors=[];this.externalTensors=new Set}getMLContext(t){let r=this.backend.getMLContext(t);if(!r)throw new Error("MLContext not found for session.");return r}reserveTensorId(){let t=Ga();return this.tensorTrackersById.set(t,new Ur(this)),t}releaseTensorId(t){let r=this.tensorTrackersById.get(t);r&&(this.tensorTrackersById.delete(t),r.tensorWrapper&&this.releaseTensor(r.tensorWrapper))}async ensureTensor(t,r,n,o,i){se("verbose",()=>`[WebNN] TensorManager.ensureTensor {tensorId: ${r}, dataType: ${n}, shape: ${o}, copyOld: ${i}}`);let a=this.tensorTrackersById.get(r);if(!a)throw new Error("Tensor not found.");return a.ensureTensor(t,n,o,i)}upload(t,r){let n=this.tensorTrackersById.get(t);if(!n)throw new Error("Tensor not found.");n.upload(r)}async download(t,r){se("verbose",()=>`[WebNN] TensorManager.download {tensorId: ${t}, dstBuffer: ${r?.byteLength}}`);let n=this.tensorTrackersById.get(t);if(!n)throw new Error("Tensor not found.");return n.download(r)}releaseTensorsForSession(t){for(let r of this.freeTensors)r.sessionId===t&&r.destroy();this.freeTensors=this.freeTensors.filter(r=>r.sessionId!==t)}registerTensor(t,r,n,o){let i=this.getMLContext(t),a=Ga(),u=new Rr({sessionId:t,context:i,tensor:r,dataType:n,shape:o});return this.tensorTrackersById.set(a,new Ur(this,u)),this.externalTensors.add(u),a}async getCachedTensor(t,r,n,o,i,a,u=!1){let d=this.getMLContext(t);for(let[p,m]of this.freeTensors.entries())if(m.canReuseTensor(d,r,n)){se("verbose",()=>`[WebNN] Reusing tensor {dataType: ${r}, shape: ${n}}`);let f=this.freeTensors.splice(p,1)[0];return f.sessionId=t,f}se("verbose",()=>`[WebNN] MLContext.createTensor {dataType: ${r}, shape: ${n}}`);let c=await d.createTensor({dataType:r,shape:n,dimensions:n,usage:o,writable:i,readable:a});return new Rr({sessionId:t,context:d,tensor:c,dataType:r,shape:n,shouldConvertInt64toInt32:u})}releaseTensor(t){this.externalTensors.has(t)&&this.externalTensors.delete(t),this.freeTensors.push(t)}},qa=(...e)=>new Qn(...e)});var Xn,dm,Nr,Ka=U(()=>{"use strict";ee();ht();Zn();ja();Xe();Xn=new Map([[1,"float32"],[10,"float16"],[6,"int32"],[12,"uint32"],[7,"int64"],[13,"uint64"],[22,"int4"],[21,"uint4"],[3,"int8"],[2,"uint8"],[9,"uint8"]]),dm=(e,t)=>{if(e===t)return!0;if(e===void 0||t===void 0)return!1;let r=Object.keys(e).sort(),n=Object.keys(t).sort();return r.length===n.length&&r.every((o,i)=>o===n[i]&&e[o]===t[o])},Nr=class{constructor(t){this.tensorManager=qa(this);this.mlContextBySessionId=new Map;this.sessionIdsByMLContext=new Map;this.mlContextCache=[];this.sessionGraphInputs=new Map;this.temporaryGraphInputs=[];this.temporarySessionTensorIds=new Map;Br(t.logLevel,!!t.debug)}get currentSessionId(){if(this.activeSessionId===void 0)throw new Error("No active session");return this.activeSessionId}onRunStart(t){se("verbose",()=>`[WebNN] onRunStart {sessionId: ${t}}`),this.activeSessionId=t}onRunEnd(t){se("verbose",()=>`[WebNN] onRunEnd {sessionId: ${t}}`);let r=this.temporarySessionTensorIds.get(t);if(r){for(let n of r)se("verbose",()=>`[WebNN] releasing temporary tensor {tensorId: ${n}}`),this.tensorManager.releaseTensorId(n);this.temporarySessionTensorIds.delete(t),this.activeSessionId=void 0}}async createMLContext(t){if(t instanceof GPUDevice){let n=this.mlContextCache.findIndex(o=>o.gpuDevice===t);if(n!==-1)return this.mlContextCache[n].mlContext;{let o=await navigator.ml.createContext(t);return this.mlContextCache.push({gpuDevice:t,mlContext:o}),o}}else if(t===void 0){let n=this.mlContextCache.findIndex(o=>o.options===void 0&&o.gpuDevice===void 0);if(n!==-1)return this.mlContextCache[n].mlContext;{let o=await navigator.ml.createContext();return this.mlContextCache.push({mlContext:o}),o}}let r=this.mlContextCache.findIndex(n=>dm(n.options,t));if(r!==-1)return this.mlContextCache[r].mlContext;{let n=await navigator.ml.createContext(t);return this.mlContextCache.push({options:t,mlContext:n}),n}}registerMLContext(t,r){this.mlContextBySessionId.set(t,r);let n=this.sessionIdsByMLContext.get(r);n||(n=new Set,this.sessionIdsByMLContext.set(r,n)),n.add(t),this.temporaryGraphInputs.length>0&&(this.sessionGraphInputs.set(t,this.temporaryGraphInputs),this.temporaryGraphInputs=[])}onReleaseSession(t){this.sessionGraphInputs.delete(t);let r=this.mlContextBySessionId.get(t);if(!r)return;this.tensorManager.releaseTensorsForSession(t),this.mlContextBySessionId.delete(t);let n=this.sessionIdsByMLContext.get(r);if(n.delete(t),n.size===0){this.sessionIdsByMLContext.delete(r);let o=this.mlContextCache.findIndex(i=>i.mlContext===r);o!==-1&&this.mlContextCache.splice(o,1)}}getMLContext(t){return this.mlContextBySessionId.get(t)}reserveTensorId(){return this.tensorManager.reserveTensorId()}releaseTensorId(t){se("verbose",()=>`[WebNN] releaseTensorId {tensorId: ${t}}`),this.tensorManager.releaseTensorId(t)}async ensureTensor(t,r,n,o,i){let a=Xn.get(n);if(!a)throw new Error(`Unsupported ONNX data type: ${n}`);return this.tensorManager.ensureTensor(t??this.currentSessionId,r,a,o,i)}async createTemporaryTensor(t,r,n){se("verbose",()=>`[WebNN] createTemporaryTensor {onnxDataType: ${r}, shape: ${n}}`);let o=Xn.get(r);if(!o)throw new Error(`Unsupported ONNX data type: ${r}`);let i=this.tensorManager.reserveTensorId();await this.tensorManager.ensureTensor(t,i,o,n,!1);let a=this.temporarySessionTensorIds.get(t);return a?a.push(i):this.temporarySessionTensorIds.set(t,[i]),i}uploadTensor(t,r){if(!fe().shouldTransferToMLTensor)throw new Error("Trying to upload to a MLTensor while shouldTransferToMLTensor is false");se("verbose",()=>`[WebNN] uploadTensor {tensorId: ${t}, data: ${r.byteLength}}`),this.tensorManager.upload(t,r)}async downloadTensor(t,r){return this.tensorManager.download(t,r)}createMLTensorDownloader(t,r){return async()=>{let n=await this.tensorManager.download(t);return Mr(n,r)}}registerMLTensor(t,r,n,o){let i=Xn.get(n);if(!i)throw new Error(`Unsupported ONNX data type: ${n}`);let a=this.tensorManager.registerTensor(t,r,i,o);return se("verbose",()=>`[WebNN] registerMLTensor {tensor: ${r}, dataType: ${i}, dimensions: ${o}} -> {tensorId: ${a}}`),a}registerMLConstant(t,r,n,o,i,a,u=!1){if(!a)throw new Error("External mounted files are not available.");let d=t;t.startsWith("./")&&(d=t.substring(2));let c=a.get(d);if(!c)throw new Error(`File with name ${d} not found in preloaded files.`);if(r+n>c.byteLength)throw new Error("Out of bounds: data offset and length exceed the external file data size.");let p=c.slice(r,r+n).buffer,m;switch(i.dataType){case"float32":m=new Float32Array(p);break;case"float16":m=typeof Float16Array<"u"&&Float16Array.from?new Float16Array(p):new Uint16Array(p);break;case"int32":m=new Int32Array(p);break;case"uint32":m=new Uint32Array(p);break;case"int64":u?(m=Yn(new Uint8Array(p),!1),i.dataType="int32"):m=new BigInt64Array(p);break;case"uint64":m=new BigUint64Array(p);break;case"int8":m=new Int8Array(p);break;case"int4":case"uint4":case"uint8":m=new Uint8Array(p);break;default:throw new Error(`Unsupported data type: ${i.dataType} in creating WebNN Constant from external data.`)}return se("verbose",()=>`[WebNN] registerMLConstant {dataType: ${i.dataType}, shape: ${i.shape}}} ${u?"(Note: it was int64 data type and registered to int32 as workaround)":""}`),o.constant(i,m)}registerGraphInput(t){this.temporaryGraphInputs.push(t)}isGraphInput(t,r){let n=this.sessionGraphInputs.get(t);return n?n.includes(r):!1}isInt64Supported(t){return!!this.mlContextBySessionId.get(t)?.opSupportLimits().input.dataTypes.includes("int64")}flush(){}}});var Vr=U(()=>{"use strict"});var Za,Jn,eo,lm,cm,Qa,ro,to,Xa,Ja=U(()=>{"use strict";Xe();Vr();Za=new Map([[64,250],[128,200],[256,200],[512,200],[2048,230],[4096,200],[8192,50],[16384,50],[32768,50],[65536,50],[131072,50],[262144,50],[524288,50],[1048576,50],[2097152,30],[4194304,20],[8388608,10],[12582912,10],[16777216,10],[26214400,15],[33554432,22],[44236800,2],[58982400,6],[67108864,6],[134217728,6],[167772160,6]]),Jn=[],eo=e=>Math.ceil(Number(e)/16)*16,lm=e=>{for(let t=0;tcm++,ro=async(e,t,r,n)=>{let o=eo(r),i=e.device.createBuffer({size:o,usage:GPUBufferUsage.COPY_DST|GPUBufferUsage.MAP_READ});try{let a=e.getCommandEncoder();e.endComputePass(),a.copyBufferToBuffer(t,0,i,0,o),e.flush(),await i.mapAsync(GPUMapMode.READ);let u=i.getMappedRange();if(n){let d=n();return d.set(new Uint8Array(u,0,r)),d}else return new Uint8Array(u.slice(0,r))}finally{i.destroy()}},to=class{constructor(t){this.backend=t;this.storageCache=new Map,this.freeBuffers=new Map,this.freeUniformBuffers=new Map,this.buffersPending=[],this.capturedPendingBuffers=new Map;for(let[r]of Za)Jn.push(r),this.freeBuffers.set(r,[]),this.freeUniformBuffers.set(r,[]);this.sessionCount=0}upload(t,r){let n=r.buffer,o=r.byteOffset,i=r.byteLength,a=eo(i),u=this.storageCache.get(t);if(!u)throw new Error("gpu data for uploading does not exist");if(Number(u.originalSize)!==i)throw new Error(`inconsistent data size. gpu data size=${u.originalSize}, data size=${i}`);let d=this.backend.device.createBuffer({mappedAtCreation:!0,size:a,usage:GPUBufferUsage.MAP_WRITE|GPUBufferUsage.COPY_SRC}),c=d.getMappedRange();new Uint8Array(c).set(new Uint8Array(n,o,i)),d.unmap();let p=this.backend.device.createCommandEncoder();p.copyBufferToBuffer(d,0,u.gpuData.buffer,0,a),this.backend.device.queue.submit([p.finish()]),d.destroy(),se("verbose",()=>`[WebGPU] GpuDataManager.upload(id=${t})`)}memcpy(t,r){let n=this.storageCache.get(t);if(!n)throw new Error("source gpu data for memcpy does not exist");let o=this.storageCache.get(r);if(!o)throw new Error("destination gpu data for memcpy does not exist");if(n.originalSize!==o.originalSize)throw new Error("inconsistent source and destination gpu data size");let i=eo(n.originalSize),a=this.backend.getCommandEncoder();this.backend.endComputePass(),a.copyBufferToBuffer(n.gpuData.buffer,0,o.gpuData.buffer,0,i)}registerExternalBuffer(t,r,n){let o;if(n){if(o=n[0],t===n[1])return se("verbose",()=>`[WebGPU] GpuDataManager.registerExternalBuffer(size=${r}) => id=${o}, buffer is the same, skip.`),o;if(this.backend.capturedCommandList.has(this.backend.currentSessionId))throw new Error(`Registering a different external buffer under graph capture mode is not supported yet. + Please use the previous external buffer!`)}else o=Qa();return this.storageCache.set(o,{gpuData:{id:o,type:0,buffer:t},originalSize:r}),se("verbose",()=>`[WebGPU] GpuDataManager.registerExternalBuffer(size=${r}) => id=${o}, registered.`),o}unregisterExternalBuffer(t){t!==void 0&&(this.storageCache.delete(t),se("verbose",()=>`[WebGPU] GpuDataManager.unregisterExternalBuffer() => id=${t}`))}create(t,r=GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST){let n=lm(t),o,i=(r&GPUBufferUsage.STORAGE)===GPUBufferUsage.STORAGE,a=(r&GPUBufferUsage.UNIFORM)===GPUBufferUsage.UNIFORM;if(i||a){let c=(i?this.freeBuffers:this.freeUniformBuffers).get(n);c?c.length>0?o=c.pop():o=this.backend.device.createBuffer({size:n,usage:r}):o=this.backend.device.createBuffer({size:n,usage:r})}else o=this.backend.device.createBuffer({size:n,usage:r});let u={id:Qa(),type:0,buffer:o};return this.storageCache.set(u.id,{gpuData:u,originalSize:Number(t)}),se("verbose",()=>`[WebGPU] GpuDataManager.create(size=${t}) => id=${u.id}`),u}get(t){return this.storageCache.get(t)?.gpuData}release(t){let r=typeof t=="bigint"?Number(t):t,n=this.storageCache.get(r);if(!n){if(this.storageCache.size===0)return 0;throw new Error("releasing data does not exist")}return se("verbose",()=>`[WebGPU] GpuDataManager.release(id=${r}), gpuDataId=${n.gpuData.id}`),this.storageCache.delete(r),this.buffersPending.push(n.gpuData.buffer),n.originalSize}async download(t,r){let n=this.storageCache.get(Number(t));if(!n)throw new Error("data does not exist");await ro(this.backend,n.gpuData.buffer,n.originalSize,r)}refreshPendingBuffers(){if(this.buffersPending.length!==0)if(this.backend.sessionStatus==="default"){for(let t of this.buffersPending){let r=Za.get(t.size);if((t.usage&GPUBufferUsage.STORAGE)===GPUBufferUsage.STORAGE){let n=this.freeBuffers.get(t.size)||[];r===void 0||n.length>=r?t.destroy():n.push(t)}else if((t.usage&GPUBufferUsage.UNIFORM)===GPUBufferUsage.UNIFORM){let n=this.freeUniformBuffers.get(t.size)||[];r===void 0||n.length>=r?t.destroy():n.push(t)}else t.destroy()}this.buffersPending=[]}else{let t=this.capturedPendingBuffers.get(this.backend.currentSessionId);t||(t=[],this.capturedPendingBuffers.set(this.backend.currentSessionId,t));for(let r of this.buffersPending)t.push(r);this.buffersPending=[]}}dispose(){this.freeBuffers.forEach(t=>{t.forEach(r=>{r.destroy()})}),this.freeUniformBuffers.forEach(t=>{t.forEach(r=>{r.destroy()})}),this.storageCache.forEach(t=>{t.gpuData.buffer.destroy()}),this.capturedPendingBuffers.forEach(t=>{t.forEach(r=>{r.destroy()})}),this.storageCache=new Map,this.freeBuffers=new Map,this.freeUniformBuffers=new Map,this.capturedPendingBuffers=new Map}onCreateSession(){this.sessionCount+=1}onReleaseSession(t){let r=this.capturedPendingBuffers.get(t);r&&(r.forEach(n=>{n.destroy()}),this.capturedPendingBuffers.delete(t)),this.sessionCount-=1,this.sessionCount===0&&(se("warning",()=>"[WebGPU] Clearing webgpu buffer cache"),this.storageCache.forEach(n=>{n.gpuData.buffer.destroy()}),this.storageCache=new Map)}},Xa=(...e)=>new to(...e)});var no,J,Se=U(()=>{"use strict";no=class{constructor(t){Object.assign(this,t)}get cacheKey(){return this.key||(this.key=Object.getOwnPropertyNames(this).sort().map(t=>`${this[t]}`).join(";")),this.key}},J=e=>new no(e)});var It,io,be,Ae,N,ce,ao,Ct,He,F,Wr,P,M,es,Lr,oo,ts,ie=U(()=>{"use strict";ee();ne();It=64,io=(e,t)=>{if(t===3)throw new Error("vec3 has same alignment as vec4, use vec4 instead");switch(Number(e)){case 10:return t>1?`vec${t}`:"f16";case 1:return t>1?`vec${t}`:"f32";case 6:return t>1?`vec${t}`:"i32";case 12:return t>1?`vec${t}`:"u32";case 7:if(t>1)throw new Error("currently not supported vecX of uint64 yet");return["vec2","i32"];case 13:if(t>1)throw new Error("currently not supported vecX of uint64 yet");return["vec2","u32"];case 9:if(t!==4)throw new Error("bool must be vec4");return["u32","vec4"];case 22:return"i32";case 21:return"u32";default:throw new Error(`Unknown data type: ${e}`)}},be=(e,t=1)=>{let r=io(e,t);return typeof r=="string"?r:r[0]},Ae=(e,t=1)=>{let r=io(e,t);return typeof r=="string"?r:r[1]},N=(...e)=>{let t=[];return e.forEach(r=>{r.length!==0&&t.push({type:12,data:r},{type:12,data:k.computeStrides(r)})}),t},ce=e=>e%4===0?4:e%2===0?2:1,ao=(e="f32",t,r="0")=>!t||t===1?`${e}(${r})`:`vec${t}<${e}>(${r})`,Ct=(e,t,r)=>e==="f32"?r:t===1?`f32(${r})`:`vec${t}(${r})`,He=(e,t)=>t===4?`(${e}.x + ${e}.y + ${e}.z + ${e}.w)`:t===2?`(${e}.x + ${e}.y)`:t===3?`(${e}.x + ${e}.y + ${e}.z)`:e,F=(e,t,r,n)=>e.startsWith("uniforms.")&&r>4?typeof t=="string"?n==="f16"?`${e}[(${t}) / 8][(${t}) % 8 / 4][(${t}) % 8 % 4]`:`${e}[(${t}) / 4][(${t}) % 4]`:n==="f16"?`${e}[${Math.floor(t/8)}][${Math.floor(t%8/4)}][${t%8%4}]`:`${e}[${Math.floor(t/4)}][${t%4}]`:r>1?`${e}[${t}]`:e,Wr=(e,t,r,n,o)=>{let i=typeof r=="number",a=i?r:r.length,u=[...new Array(a).keys()],d=a<2?"u32":a<=4?`vec${a}`:`array`,c=io(t,o),p=typeof c=="string"?c:c[1],m=typeof c=="string"?c:c[0],f={indices:d,value:p,storage:m,tensor:t},b=C=>typeof C=="string"?C:`${C}u`,g={offsetToIndices:!1,indicesToOffset:!1,broadcastedIndicesToOffset:!1,set:!1,setByIndices:!1,get:!1,getByIndices:!1},_=i?"uniforms.":"",S=`${_}${e}_shape`,$=`${_}${e}_strides`,v="";for(let C=0;C ${f.indices} { + var indices: ${f.indices}; + var current = offset; + ${v} + return indices; + }`,T=C=>(g.offsetToIndices=!0,a<2?C:`o2i_${e}(${C})`),E=[];if(a>=2)for(let C=a-1;C>=0;C--)E.push(`${F($,C,a)} * (indices[${C}])`);let I=a<2?"":` + fn i2o_${e}(indices: ${f.indices}) -> u32 { + return ${E.join("+")}; + }`,z=C=>(g.indicesToOffset=!0,a<2?C:`i2o_${e}(${C})`),O=(...C)=>a===0?"0u":`${f.indices}(${C.map(b).join(",")})`,D=(C,V)=>a<2?`${C}`:`${F(C,V,a)}`,L=(C,V,de)=>a<2?`${C}=${de};`:`${F(C,V,a)}=${de};`,q={},Q=(C,V)=>{g.broadcastedIndicesToOffset=!0;let de=`${V.name}broadcastedIndicesTo${e}Offset`;if(de in q)return`${de}(${C})`;let ze=[];for(let ve=a-1;ve>=0;ve--){let $e=V.indicesGet("outputIndices",ve+V.rank-a);ze.push(`${D($,ve)} * (${$e} % ${D(S,ve)})`)}return q[de]=`fn ${de}(outputIndices: ${V.type.indices}) -> u32 { + return ${ze.length>0?ze.join("+"):"0u"}; + }`,`${de}(${C})`},W=(C,V)=>(()=>{if(f.storage===f.value)return`${e}[${C}]=${V};`;if(f.storage==="vec2"&&f.value==="i32")return`${e}[${C}]=vec2(u32(${V}), select(0u, 0xFFFFFFFFu, ${V} < 0));`;if(f.storage==="vec2"&&f.value==="u32")return`${e}[${C}]=vec2(u32(${V}), 0u);`;if(f.storage==="u32"&&f.value==="vec4")return`${e}[${C}]=dot(vec4(0x1, 0x100, 0x10000, 0x1000000), vec4(${V}));`;throw new Error(`not supported combination of storage type ${f.storage} and value type ${f.value} yet`)})(),Z=C=>(()=>{if(f.storage===f.value)return`${e}[${C}]`;if(f.storage==="vec2"&&f.value==="i32")return`i32(${e}[${C}].x)`;if(f.storage==="vec2"&&f.value==="u32")return`u32(${e}[${C}].x)`;if(f.storage==="u32"&&f.value==="vec4")return`vec4(bool(${e}[${C}] & 0xFFu), bool(${e}[${C}] & 0xFF00u), bool(${e}[${C}] & 0xFF0000u), bool(${e}[${C}] & 0xFF000000u))`;throw new Error(`not supported combination of storage type ${f.storage} and value type ${f.value} yet`)})(),we=a<2?"":` + fn get_${e}ByIndices(indices: ${f.indices}) -> ${p} { + return ${Z(`i2o_${e}(indices)`)}; + }`,H=a<2?"":(()=>{let C=u.map(de=>`d${de}: u32`).join(", "),V=u.map(de=>`d${de}`).join(", ");return` + fn get_${e}(${C}) -> ${p} { + return get_${e}ByIndices(${O(V)}); + }`})(),j=(...C)=>{if(C.length!==a)throw new Error(`indices length must be ${a}`);let V=C.map(b).join(",");return a===0?Z("0u"):a===1?Z(V[0]):(g.get=!0,g.getByIndices=!0,g.indicesToOffset=!0,`get_${e}(${V})`)},te=C=>a<2?Z(C):(g.getByIndices=!0,g.indicesToOffset=!0,`get_${e}ByIndices(${C})`),X=a<2?"":` + fn set_${e}ByIndices(indices: ${f.indices}, value: ${p}) { + ${W(`i2o_${e}(indices)`,"value")} + }`,ue=a<2?"":(()=>{let C=u.map(de=>`d${de}: u32`).join(", "),V=u.map(de=>`d${de}`).join(", ");return` + fn set_${e}(${C}, value: ${p}) { + set_${e}ByIndices(${O(V)}, value); + }`})();return{impl:()=>{let C=[],V=!1;return g.offsetToIndices&&(C.push(x),V=!0),g.indicesToOffset&&(C.push(I),V=!0),g.broadcastedIndicesToOffset&&(Object.values(q).forEach(de=>C.push(de)),V=!0),g.set&&(C.push(ue),V=!0),g.setByIndices&&(C.push(X),V=!0),g.get&&(C.push(H),V=!0),g.getByIndices&&(C.push(we),V=!0),!i&&V&&C.unshift(`const ${S} = ${f.indices}(${r.join(",")});`,`const ${$} = ${f.indices}(${k.computeStrides(r).join(",")});`),C.join(` +`)},type:f,offsetToIndices:T,indicesToOffset:z,broadcastedIndicesToOffset:Q,indices:O,indicesGet:D,indicesSet:L,set:(...C)=>{if(C.length!==a+1)throw new Error(`indices length must be ${a}`);let V=C[a];if(typeof V!="string")throw new Error("value must be string");let de=C.slice(0,a).map(b).join(",");return a===0?W("0u",V):a===1?W(de[0],V):(g.set=!0,g.setByIndices=!0,g.indicesToOffset=!0,`set_${e}(${de}, ${V})`)},setByOffset:W,setByIndices:(C,V)=>a<2?W(C,V):(g.setByIndices=!0,g.indicesToOffset=!0,`set_${e}ByIndices(${C}, ${V});`),get:j,getByOffset:Z,getByIndices:te,usage:n,name:e,strides:$,shape:S,rank:a}},P=(e,t,r,n=1)=>Wr(e,t,r,"input",n),M=(e,t,r,n=1)=>Wr(e,t,r,"output",n),es=(e,t,r)=>Wr(e,t,r,"atomicOutput",1),Lr=(e,t,r,n=1)=>Wr(e,t,r,"internal",n),oo=class{constructor(t,r){this.normalizedDispatchGroup=t;this.limits=r;this.internalVariables=[];this.variables=[];this.uniforms=[];this.variableIndex=0}guardAgainstOutOfBoundsWorkgroupSizes(t){return`if (global_idx >= ${typeof t=="number"?`${t}u`:t}) { return; }`}mainStart(t=It){let r=typeof t=="number"?t:t[0],n=typeof t=="number"?1:t[1],o=typeof t=="number"?1:t[2];if(r>this.limits.maxComputeWorkgroupSizeX||n>this.limits.maxComputeWorkgroupSizeY||o>this.limits.maxComputeWorkgroupSizeZ)throw new Error(`workgroup size [${r}, ${n}, ${o}] exceeds the maximum workgroup size [${this.limits.maxComputeWorkgroupSizeX}, ${this.limits.maxComputeWorkgroupSizeY}, ${this.limits.maxComputeWorkgroupSizeZ}].`);if(r*n*o>this.limits.maxComputeInvocationsPerWorkgroup)throw new Error(`workgroup size [${r}, ${n}, ${o}] exceeds the maximum workgroup invocations ${this.limits.maxComputeInvocationsPerWorkgroup}.`);let i=this.normalizedDispatchGroup[1]===1&&this.normalizedDispatchGroup[2]===1,a=i?`@builtin(global_invocation_id) global_id : vec3, + @builtin(workgroup_id) workgroup_id : vec3, + @builtin(local_invocation_index) local_idx : u32, + @builtin(local_invocation_id) local_id : vec3`:`@builtin(global_invocation_id) global_id : vec3, + @builtin(local_invocation_id) local_id : vec3, + @builtin(local_invocation_index) local_idx : u32, + @builtin(workgroup_id) workgroup_id : vec3, + @builtin(num_workgroups) num_workgroups : vec3`,u=i?`let global_idx = global_id.x; + let workgroup_index = workgroup_id.x;`:`let workgroup_index = workgroup_id.z * num_workgroups[0] * num_workgroups[1] + + workgroup_id.y * num_workgroups[0] + workgroup_id.x; + let global_idx = workgroup_index * ${r*n*o}u + local_idx;`;return`@compute @workgroup_size(${r}, ${n}, ${o}) + fn main(${a}) { + ${u} + `}appendVariableUniforms(t){t.rank!==0&&(t.shape.startsWith("uniforms.")&&this.uniforms.push({name:t.shape.replace("uniforms.",""),type:"u32",length:t.rank}),t.strides.startsWith("uniforms.")&&this.uniforms.push({name:t.strides.replace("uniforms.",""),type:"u32",length:t.rank}))}declareVariable(t,r){if(t.usage==="internal")throw new Error("cannot use internal variable with declareVariable(). use registerInternalVariables() instead.");this.variables.push(t),this.appendVariableUniforms(t);let n=t.usage==="input"?"read":"read_write",o=t.usage==="atomicOutput"?"atomic":t.type.storage;return`@group(0) @binding(${r}) var ${t.name}: array<${o}>;`}declareVariables(...t){return t.map(r=>this.declareVariable(r,this.variableIndex++)).join(` +`)}registerInternalVariable(t){if(t.usage!=="internal")throw new Error("cannot use input or output variable with registerInternalVariable(). use declareVariables() instead.");this.internalVariables.push(t),this.appendVariableUniforms(t)}registerInternalVariables(...t){return t.forEach(r=>this.registerInternalVariable(r)),this}registerUniform(t,r,n=1){return this.uniforms.push({name:t,type:r,length:n}),this}registerUniforms(t){return this.uniforms=this.uniforms.concat(t),this}uniformDeclaration(){if(this.uniforms.length===0)return"";let t=[];for(let{name:r,type:n,length:o}of this.uniforms)if(o&&o>4)n==="f16"?t.push(`@align(16) ${r}:array, ${Math.ceil(o/8)}>`):t.push(`${r}:array, ${Math.ceil(o/4)}>`);else{let i=o==null||o===1?n:`vec${o}<${n}>`;t.push(`${r}:${i}`)}return` + struct Uniforms { ${t.join(", ")} }; + @group(0) @binding(${this.variableIndex}) var uniforms: Uniforms;`}get additionalImplementations(){return this.uniformDeclaration()+this.variables.map(t=>t.impl()).join(` +`)+this.internalVariables.map(t=>t.impl()).join(` +`)}get variablesInfo(){if(this.uniforms.length===0)return;let t=r=>[12,10,1,6][["u32","f16","f32","i32"].indexOf(r)];return this.uniforms.map(r=>[t(r.type),r.length??1])}},ts=(e,t)=>new oo(e,t)});var pm,rs,mm,fm,hm,gm,Ee,ns,os,st=U(()=>{"use strict";ee();ne();Se();ie();pm=(e,t)=>{if(!e||e.length!==1)throw new Error("Transpose requires 1 input.");if(t.length!==0&&t.length!==e[0].dims.length)throw new Error(`perm size ${t.length} does not match input rank ${e[0].dims.length}`)},rs=(e,t)=>t.length!==0?t:[...new Array(e).keys()].reverse(),mm=(e,t)=>k.sortBasedOnPerm(e,rs(e.length,t)),fm=(e,t,r,n)=>{let o=`fn perm(i: ${n.type.indices}) -> ${r.type.indices} { + var a: ${r.type.indices};`;for(let i=0;i{let r=[],n=[];for(let o=0;o{let r=0;for(let n=0;n{let r=e.dataType,n=e.dims.length,o=rs(n,t),i=mm(e.dims,o),a=e.dims,u=i,d=n<2||gm(o,e.dims),c;if(d)return c=_=>{let S=P("input",r,a,4),$=M("output",r,u,4);return` + ${_.registerUniform("output_size","u32").declareVariables(S,$)} + ${_.mainStart()} + ${_.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + output[global_idx] = input[global_idx]; + }`},{name:"TransposeCopy",shaderCache:{inputDependencies:["type"]},getRunData:()=>{let _=k.size(i);return{outputs:[{dims:i,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(_/64/4)},programUniforms:[{type:12,data:Math.ceil(_/4)}]}},getShaderSource:c};let{newShape:p,newPerm:m}=hm(e.dims,o),f=k.areEqual(m,[2,3,1]),b=k.areEqual(m,[3,1,2]);if(p.length===2||f||b){a=f?[p[0],p[1]*p[2]]:b?[p[0]*p[1],p[2]]:p,u=[a[1],a[0]];let _=16;return c=S=>{let $=P("a",r,a.length),v=M("output",r,u.length);return` + ${S.registerUniform("output_size","u32").declareVariables($,v)} + var tile : array, ${_}>; + ${S.mainStart([_,_,1])} + let stride = (uniforms.output_shape[1] - 1) / ${_} + 1; + let workgroup_id_x = workgroup_index % stride; + let workgroup_id_y = workgroup_index / stride; + let input_col = workgroup_id_y * ${_}u + local_id.x; + let input_row = workgroup_id_x * ${_}u + local_id.y; + if (input_row < uniforms.a_shape[0] && input_col < uniforms.a_shape[1]) { + tile[local_id.y][local_id.x] = ${$.getByIndices(`${$.type.indices}(input_row, input_col)`)}; + } + workgroupBarrier(); + + let output_col = workgroup_id_x * ${_}u + local_id.x; + let output_row = workgroup_id_y * ${_}u + local_id.y; + if (output_row < uniforms.output_shape[0] && output_col < uniforms.output_shape[1]) { + ${v.setByIndices(`${v.type.indices}(output_row, output_col)`,"tile[local_id.x][local_id.y]")} + } + }`},{name:"TransposeShared",shaderCache:{inputDependencies:["type"]},getRunData:()=>{let S=k.size(i);return{outputs:[{dims:i,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(u[1]/_),y:Math.ceil(u[0]/_)},programUniforms:[{type:12,data:S},...N(a,u)]}},getShaderSource:c}}return c=_=>{let S=P("a",r,a.length),$=M("output",r,u.length);return` + ${_.registerUniform("output_size","u32").declareVariables(S,$)} + + ${fm(o,n,S,$)} + + ${_.mainStart()} + ${_.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + + let indices = ${$.offsetToIndices("global_idx")}; + let aIndices = perm(indices); + + ${$.setByOffset("global_idx",S.getByIndices("aIndices"))} + }`},{name:"Transpose",shaderCache:{hint:`${t}`,inputDependencies:["rank"]},getRunData:()=>{let _=k.size(i);return{outputs:[{dims:i,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(_/64)},programUniforms:[{type:12,data:_},...N(a,u)]}},getShaderSource:c}},ns=(e,t)=>{pm(e.inputs,t.perm),e.compute(Ee(e.inputs[0],t.perm))},os=e=>J({perm:e.perm})});var bm,ym,_m,wm,vm,$m,xm,Sm,Tm,Im,et,is,as,ss,us,ds,ls,cs,ps,ms,fs,hs=U(()=>{"use strict";ee();ne();ie();Gr();st();bm={max:"select(bestValue, candidate, candidate > bestValue)",min:"select(bestValue, candidate, candidate < bestValue)",mean:"bestValue + candidate",sum:"bestValue + candidate",prod:"bestValue * candidate",sumSquare:"bestValue + candidate * candidate",logSumExp:"bestValue + exp(candidate)",l1:"bestValue + abs(candidate)",l2:"bestValue + candidate * candidate",logSum:"bestValue + candidate"},ym={max:"select(bestValue, candidate, candidate > bestValue)",min:"select(bestValue, candidate, candidate < bestValue)",mean:"bestValue + candidate",sum:"bestValue + candidate",prod:"bestValue * candidate",sumSquare:"bestValue + candidate",logSumExp:"bestValue + candidate",l1:"bestValue + candidate",l2:"bestValue + candidate",logSum:"bestValue + candidate"},_m={max:"_A[offset]",min:"_A[offset]",mean:"0",sum:"0",prod:"1",sumSquare:"0",logSumExp:"0",l1:"0",l2:"0",logSum:"0"},wm={max:"bestValue",min:"bestValue",sum:"bestValue",prod:"bestValue",sumSquare:"bestValue",logSumExp:"log(bestValue)",l1:"bestValue",l2:"sqrt(bestValue)",logSum:"log(bestValue)"},vm=(e,t)=>{let r=[];for(let n=t-e;n{let r=[],n=e.length;for(let i=0;ie[i]);return[r,o]},xm=(e,t)=>{let r=e.length+t.length,n=[],o=0;for(let i=0;i{for(let r=0;r{let r=[];if(!Sm(e,t)){for(let n=0;nr.push(n))}return r},Im=(e,t,r,n,o,i,a)=>{let u=r[0].dims,d=k.size(i),c=k.size(a),p=P("_A",r[0].dataType,u),m=M("output",o,i),f=64;d===1&&(f=256);let b=` + var aBestValues : array; + `,g=_=>` + ${_.registerUniform("reduceSize","u32").declareVariables(p,m)} + ${b} + fn DIV_CEIL(a : u32, b : u32) -> u32 { + return ((a - 1u) / b + 1u); + } + ${_.mainStart(f)} + + let outputIndex = global_idx / ${f}; + let offset = outputIndex * uniforms.reduceSize; + + var bestValue = f32(${_m[n]}); + let Length = uniforms.reduceSize; + for (var k = local_idx; k < Length; k = k + ${f}) { + let candidate = f32(${p.getByOffset("offset + k")}); + bestValue = ${bm[n]}; + } + aBestValues[local_idx] = bestValue; + workgroupBarrier(); + + var reduceSize = min(Length, ${f}u); + for (var currentSize = reduceSize / 2u; reduceSize > 1u; + currentSize = reduceSize / 2u) { + let interval = DIV_CEIL(reduceSize, 2u); + if (local_idx < currentSize) { + let candidate = aBestValues[local_idx + interval]; + bestValue = ${ym[n]}; + aBestValues[local_idx] = bestValue; + } + reduceSize = interval; + workgroupBarrier(); + } + + if (local_idx == 0u) { + ${m.setByOffset("outputIndex",`${n==="mean"?`${m.type.storage}(bestValue / f32(uniforms.reduceSize))`:`${m.type.storage}(${wm[n]})`}`)}; + } + }`;return{name:e,shaderCache:{hint:`${t};${f}`,inputDependencies:["type"]},getShaderSource:g,getRunData:()=>({outputs:[{dims:i,dataType:o}],dispatchGroup:{x:d},programUniforms:[{type:12,data:c}]})}},et=(e,t,r,n)=>{let o=e.inputs.length===1?r:so(e.inputs,r),i=o.axes;i.length===0&&!o.noopWithEmptyAxes&&(i=e.inputs[0].dims.map((b,g)=>g));let a=k.normalizeAxes(i,e.inputs[0].dims.length),u=a,d=e.inputs[0],c=Tm(u,e.inputs[0].dims.length);c.length>0&&(d=e.compute(Ee(e.inputs[0],c),{inputs:[0],outputs:[-1]})[0],u=vm(u.length,d.dims.length));let[p,m]=$m(d.dims,u),f=p;o.keepDims&&(f=xm(p,a)),e.compute(Im(t,o.cacheKey,[d],n,e.inputs[0].dataType,f,m),{inputs:[d]})},is=(e,t)=>{et(e,"ReduceMeanShared",t,"mean")},as=(e,t)=>{et(e,"ReduceL1Shared",t,"l1")},ss=(e,t)=>{et(e,"ReduceL2Shared",t,"l2")},us=(e,t)=>{et(e,"ReduceLogSumExpShared",t,"logSumExp")},ds=(e,t)=>{et(e,"ReduceMaxShared",t,"max")},ls=(e,t)=>{et(e,"ReduceMinShared",t,"min")},cs=(e,t)=>{et(e,"ReduceProdShared",t,"prod")},ps=(e,t)=>{et(e,"ReduceSumShared",t,"sum")},ms=(e,t)=>{et(e,"ReduceSumSquareShared",t,"sumSquare")},fs=(e,t)=>{et(e,"ReduceLogSumShared",t,"logSum")}});var tt,Cm,Hr,so,rt,Am,Em,km,Pm,zm,Om,Bm,Dm,Mm,Rm,nt,gs,bs,ys,_s,ws,vs,$s,xs,Ss,Ts,Gr=U(()=>{"use strict";ee();ne();Se();ie();hs();tt=e=>{if(!e||e.length===0||e.length>2)throw new Error("Reduce op requires 1 or 2 inputs.");if(e.length===2&&e[1].dims.length!==1)throw new Error("Invalid axes input dims.")},Cm=e=>["","",`var value = ${e.getByIndices("input_indices")};`,""],Hr=(e,t,r,n,o,i,a=!1,u=!1)=>{let d=[],c=r[0].dims,p=c.length,m=k.normalizeAxes(o,p),f=!u&&m.length===0;c.forEach((S,$)=>{f||m.indexOf($)>=0?a&&d.push(1):d.push(S)});let b=d.length,g=k.size(d);return{name:e,shaderCache:t,getShaderSource:S=>{let $=[],v=P("_A",r[0].dataType,p),x=M("output",i,b),T=n(v,x,m),E=T[2];for(let I=0,z=0;I=0?(a&&z++,E=`for(var j${I}: u32 = 0; j${I} < ${c[I]}; j${I}++) { + ${T[2].includes("last_index")?`let last_index = j${I};`:""} + ${v.indicesSet("input_indices",I,`j${I}`)} + ${E} + }`):($.push(`${v.indicesSet("input_indices",I,x.indicesGet("output_indices",z))};`),z++);return` + + ${S.registerUniform("output_size","u32").declareVariables(v,x)} + + ${S.mainStart()} + ${S.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + var input_indices: ${v.type.indices}; + let output_indices = ${x.offsetToIndices("global_idx")}; + + ${$.join(` +`)} + ${T[0]} // init ops for reduce max/min + ${T[1]} + ${E} + ${T[3]} + ${T.length===4?x.setByOffset("global_idx","value"):T.slice(4).join(` +`)} + }`},getRunData:()=>({outputs:[{dims:d,dataType:i}],dispatchGroup:{x:Math.ceil(g/64)},programUniforms:[{type:12,data:g},...N(c,d)]})}},so=(e,t)=>{let r=[];return e[1].dims[0]>0&&e[1].getBigInt64Array().forEach(n=>r.push(Number(n))),J({axes:r,keepDims:t.keepDims,noopWithEmptyAxes:t.noopWithEmptyAxes})},rt=(e,t,r,n)=>{let o=e.inputs,i=o.length===1?r:so(o,r);e.compute(Hr(t,{hint:i.cacheKey,inputDependencies:["rank"]},[o[0]],i.noopWithEmptyAxes&&i.axes.length===0?Cm:n,i.axes,o[0].dataType,i.keepDims,i.noopWithEmptyAxes),{inputs:[0]})},Am=(e,t)=>{tt(e.inputs),rt(e,"ReduceLogSum",t,(n,o)=>[`var value = ${o.type.storage}(0);`,"",`value += ${n.getByIndices("input_indices")};`,"value = log(value);"])},Em=(e,t)=>{tt(e.inputs),rt(e,"ReduceL1",t,(n,o)=>[`var value = ${o.type.storage}(0);`,"",`value += abs(${n.getByIndices("input_indices")});`,""])},km=(e,t)=>{tt(e.inputs),rt(e,"ReduceL2",t,(n,o)=>[`var t = ${o.type.value}(0); var value = ${o.type.value}(0);`,"",`t = ${n.getByIndices("input_indices")}; value += (t * t);`,"value = sqrt(value);"])},Pm=(e,t)=>{tt(e.inputs),rt(e,"ReduceLogSumExp",t,(n,o)=>[`var value = ${o.type.storage}(0);`,"",`value += exp(${n.getByIndices("input_indices")});`,"value = log(value);"])},zm=(e,t)=>{tt(e.inputs),rt(e,"ReduceMax",t,(n,o,i)=>{let a=[];for(let u=0;u=0||i.length===0)&&a.push(n.indicesSet("input_indices",u,0));return[`${a.join(` +`)}`,`var value = ${n.getByIndices("input_indices")};`,`value = max(value, ${n.getByIndices("input_indices")});`,""]})},Om=(e,t)=>{tt(e.inputs),rt(e,"ReduceMean",t,(n,o,i)=>{let a=1;for(let u=0;u=0||i.length===0)&&(a*=e.inputs[0].dims[u]);return["var sum = f32(0);","",`sum += f32(${n.getByIndices("input_indices")});`,`let value = ${o.type.value}(sum / ${a});`]})},Bm=(e,t)=>{tt(e.inputs),rt(e,"ReduceMin",t,(n,o,i)=>{let a=[];for(let u=0;u=0||i.length===0)&&a.push(`input_indices[${u}] = 0;`);return[`${a.join(` +`)}`,`var value = ${n.getByIndices("input_indices")};`,`value = min(value, ${n.getByIndices("input_indices")});`,""]})},Dm=(e,t)=>{tt(e.inputs),rt(e,"ReduceProd",t,(n,o)=>[`var value = ${o.type.storage}(1);`,"",`value *= ${n.getByIndices("input_indices")};`,""])},Mm=(e,t)=>{tt(e.inputs),rt(e,"ReduceSum",t,(n,o)=>[`var value = ${o.type.storage}(0);`,"",`value += ${n.getByIndices("input_indices")};`,""])},Rm=(e,t)=>{tt(e.inputs),rt(e,"ReduceSumSquare",t,(n,o)=>[`var t = ${o.type.value}(0); var value = ${o.type.value}(0);`,"",`t = ${n.getByIndices("input_indices")}; value += t * t;`,""])},nt=(e,t,r)=>{if(t.length===0)return r;let n=1,o=1;for(let i=0;i1024},gs=(e,t)=>{nt(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Om(e,t):is(e,t)},bs=(e,t)=>{nt(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Em(e,t):as(e,t)},ys=(e,t)=>{nt(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?km(e,t):ss(e,t)},_s=(e,t)=>{nt(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Pm(e,t):us(e,t)},ws=(e,t)=>{nt(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?zm(e,t):ds(e,t)},vs=(e,t)=>{nt(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Bm(e,t):ls(e,t)},$s=(e,t)=>{nt(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Dm(e,t):cs(e,t)},xs=(e,t)=>{nt(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Mm(e,t):ps(e,t)},Ss=(e,t)=>{nt(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Rm(e,t):ms(e,t)},Ts=(e,t)=>{nt(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?Am(e,t):fs(e,t)}});var Is,Cs,As,uo,Es=U(()=>{"use strict";ee();Se();Gr();Is=e=>{if(!e||e.length===0||e.length>2)throw new Error("ArgMinMaxOp op requires 1 or 2 inputs.");if(e[0].dataType!==1)throw new Error("Invalid input type.")},Cs=(e,t)=>{Is(e.inputs);let r=(n,o,i)=>{let a=[];for(let u=0;u=0||i.length===0)&&a.push(`input_indices[${u}] = 0;`);return[`${a.join(` +`)}`,`var value = ${n.getByIndices("input_indices")}; +var best_index : i32 = 0;`,`if (${n.getByIndices("input_indices")} ${t.selectLastIndex>0?"<=":"<"} value) { + value = ${n.getByIndices("input_indices")}; + best_index = i32(last_index); + }`,"",o.setByOffset("global_idx","best_index")]};e.compute(Hr("ArgMin",{hint:t.cacheKey,inputDependencies:["rank"]},[e.inputs[0]],r,[t.axis],7,t.keepDims),{inputs:[0]})},As=(e,t)=>{Is(e.inputs);let r=(n,o,i)=>{let a=[];for(let u=0;u=0||i.length===0)&&a.push(`input_indices[${u}] = 0;`);return[`${a.join(` +`)}`,`var value = ${n.getByIndices("input_indices")}; +var best_index : i32 = 0;`,`if (${n.getByIndices("input_indices")} ${t.selectLastIndex>0?">=":">"} value) { + value = ${n.getByIndices("input_indices")}; + best_index = i32(last_index); + }`,"",o.setByOffset("global_idx","best_index")]};e.compute(Hr("argMax",{hint:t.cacheKey,inputDependencies:["rank"]},[e.inputs[0]],r,[t.axis],7,t.keepDims),{inputs:[0]})},uo=e=>J(e)});var Um,lo,Nm,Vm,Wm,Rt,Lm,ks,Fr=U(()=>{"use strict";ee();ne();Vr();ie();Um=(e,t)=>{let r=e[0],n=e[1],o=e[2],i=e[3],a=e[4],u=e[5];if(a&&u)throw new Error("Attention cannot have both past and attention_bias");if(r.dims.length!==3)throw new Error('Input "input" must have 3 dimensions');let d=r.dims[0],c=r.dims[1],p=r.dims[2];if(o.dims.length!==1)throw new Error('Input "bias" is expected to have 1 dimensions');if(n.dims.length!==2)throw new Error('Input "weights" is expected to have 2 dimensions');if(n.dims[0]!==p)throw new Error("Input 1 dimension 0 should have same length as dimension 2 of input 0");if(o.dims[0]!==n.dims[1])throw new Error('Input "bias" dimension 0 should have same length as dimension 1 of input "weights"');let m=o.dims[0]/3,f=m,b=f;if(t.qkvHiddenSizes.length>0){if(t.qkvHiddenSizes.length!==3)throw new Error("qkv_hidden_sizes attribute should have 3 elements");for(let x of t.qkvHiddenSizes)if(x%t.numHeads!==0)throw new Error("qkv_hidden_sizes should be divisible by num_heads");m=t.qkvHiddenSizes[0],f=t.qkvHiddenSizes[1],b=t.qkvHiddenSizes[2]}let g=c;if(m!==f)throw new Error("qkv_hidden_sizes first element should be same as the second");if(o.dims[0]!==m+f+b)throw new Error('Input "bias" dimension 0 should have same length as sum of Q/K/V hidden sizes');let _=0;if(a){if(f!==b)throw new Error('Input "past" expect k_hidden_size == v_hidden_size');if(a.dims.length!==5)throw new Error('Input "past" must have 5 dimensions');if(a.dims[0]!==2)throw new Error('Input "past" first dimension must be 2');if(a.dims[1]!==d)throw new Error('Input "past" second dimension must be batch_size');if(a.dims[2]!==t.numHeads)throw new Error('Input "past" third dimension must be num_heads');if(a.dims[4]!==f/t.numHeads)throw new Error('Input "past" fifth dimension must be k_hidden_size / num_heads');t.pastPresentShareBuffer||(_=a.dims[3])}let S=g+_,$=-1,v=0;if(i)throw new Error("Mask not supported");if(a)throw new Error("past is not supported");if(u){if(u.dims.length!==4)throw new Error('Input "attention_bias" must have 4 dimensions');if(u.dims[0]!==d||u.dims[1]!==t.numHeads||u.dims[2]!==c||u.dims[3]!==S)throw new Error('Expect "attention_bias" shape (batch_size, num_heads, sequence_length, total_sequence_length)')}return{batchSize:d,sequenceLength:c,pastSequenceLength:_,kvSequenceLength:g,totalSequenceLength:S,maxSequenceLength:$,inputHiddenSize:p,hiddenSize:m,vHiddenSize:b,headSize:Math.floor(m/t.numHeads),vHeadSize:Math.floor(b/t.numHeads),numHeads:t.numHeads,isUnidirectional:!1,pastPresentShareBuffer:!1,maskFilterValue:t.maskFilterValue,maskType:v,scale:t.scale,broadcastResPosBias:!1,passPastInKv:!1,qkvFormat:1}},lo=(e,t,r)=>t&&e?` + let total_sequence_length_input = u32(${t.getByOffset("0")}); + let present_sequence_length = max(total_sequence_length_input, uniforms.past_sequence_length); + let is_subsequent_prompt: bool = sequence_length > 1 && sequence_length != total_sequence_length_input; + let is_first_prompt: bool = is_subsequent_prompt == false && sequence_length == total_sequence_length_input; + total_sequence_length = u32(${e?.getByOffset("batchIdx")}) + 1; + var past_sequence_length: u32 = 0; + if (is_first_prompt == false) { + past_sequence_length = total_sequence_length - sequence_length; + } + `:` + ${r?"let past_sequence_length = uniforms.past_sequence_length":""}; + let present_sequence_length = total_sequence_length; + `,Nm=(e,t,r,n,o,i,a,u)=>{let d=ce(a?1:i),c=64,p=i/d;p{let v=M("x",e.dataType,e.dims,d),x=[v],T=a?P("seq_lens",a.dataType,a.dims):void 0;T&&x.push(T);let E=u?P("total_sequence_length_input",u.dataType,u.dims):void 0;E&&x.push(E);let I=Ae(e.dataType),z=[{name:"batch_size",type:"u32"},{name:"num_heads",type:"u32"},{name:"past_sequence_length",type:"u32"},{name:"sequence_length",type:"u32"},{name:"total_sequence_length",type:"u32"},{name:"elements_per_thread",type:"u32"}];return` + var thread_max: array; + var thread_sum: array; + ${$.registerUniforms(z).declareVariables(...x)} + ${$.mainStart([c,1,1])} + let batchIdx = workgroup_id.z / uniforms.num_heads; + let headIdx = workgroup_id.z % uniforms.num_heads; + let sequence_length = uniforms.sequence_length; + var total_sequence_length = uniforms.total_sequence_length; + ${lo(T,E,!1)} + let local_offset = local_idx * uniforms.elements_per_thread; + let offset = (global_idx / ${c}) * uniforms.total_sequence_length + local_offset; + let seq_causal_length = ${a?"u32(past_sequence_length + workgroup_id.y + 1)":"total_sequence_length"}; + var thread_max_vector = ${g}(-3.402823e+38f); + for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < seq_causal_length; i++) { + thread_max_vector = max(${g}(x[offset + i]), thread_max_vector); + } + thread_max[local_idx] = ${(()=>{switch(d){case 1:return"thread_max_vector";case 2:return"max(thread_max_vector.x, thread_max_vector.y)";case 4:return"max(max(thread_max_vector.x, thread_max_vector.y), max(thread_max_vector.z, thread_max_vector.w))";default:throw new Error(`Unsupported components: ${d}`)}})()}; + workgroupBarrier(); + + var max_value = f32(-3.402823e+38f); + for (var i = 0u; i < ${c}; i++) { + max_value = max(thread_max[i], max_value); + } + + var sum_vector = ${g}(0); + for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < seq_causal_length; i++) { + sum_vector += exp(${g}(x[offset + i]) - max_value); + } + thread_sum[local_idx] = ${(()=>{switch(d){case 1:return"sum_vector";case 2:return"sum_vector.x + sum_vector.y";case 4:return"sum_vector.x + sum_vector.y + sum_vector.z + sum_vector.w";default:throw new Error(`Unsupported components: ${d}`)}})()}; + workgroupBarrier(); + + var sum: f32 = 0; + for (var i = 0u; i < ${c}; i++) { + sum += thread_sum[i]; + } + + if (sum == 0) { + for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < seq_causal_length; i++) { + x[offset + i] = ${v.type.value}(${I}(1.0) / ${I}(seq_causal_length)); + } + } else { + for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < seq_causal_length; i++) { + var f32input = ${g}(x[offset + i]); + x[offset + i] = ${v.type.value}(exp(f32input - max_value) / sum); + } + } + ${a?` + for (var total_seq_id: u32 = seq_causal_length; total_seq_id + local_offset < uniforms.total_sequence_length; total_seq_id++) { + x[offset + total_seq_id] = ${v.type.value}(${I}(0)); + }`:""}; + }`};return{name:"AttentionProbsSoftmax",shaderCache:{hint:`${c};${b};${d}`,inputDependencies:_},getShaderSource:S,getRunData:()=>({outputs:[],dispatchGroup:{x:1,y:o,z:t*r},programUniforms:f})}},Vm=(e,t,r,n,o,i,a,u,d)=>{let c=a+i.kvSequenceLength,p=[i.batchSize,i.numHeads,i.sequenceLength,c],m=e>1&&n,f=i.kvNumHeads?i.kvNumHeads:i.numHeads,b=m?[i.batchSize,f,c,i.headSize]:void 0,g=i.nReps?i.nReps:1,_=i.scale===0?1/Math.sqrt(i.headSize):i.scale,S=ce(i.headSize),$=i.headSize/S,v=12,x={x:Math.ceil(c/v),y:Math.ceil(i.sequenceLength/v),z:i.batchSize*i.numHeads},T=[{type:12,data:i.sequenceLength},{type:12,data:$},{type:12,data:c},{type:12,data:i.numHeads},{type:12,data:i.headSize},{type:1,data:_},{type:12,data:a},{type:12,data:i.kvSequenceLength},{type:12,data:g}],E=m&&n&&k.size(n.dims)>0,I=["type","type"];E&&I.push("type"),o&&I.push("type"),u&&I.push("type"),d&&I.push("type");let z=[{dims:p,dataType:t.dataType,gpuDataType:0}];m&&z.push({dims:b,dataType:t.dataType,gpuDataType:0});let O=D=>{let L=P("q",t.dataType,t.dims,S),q=P("key",r.dataType,r.dims,S),Q=[L,q];if(E){let X=P("past_key",n.dataType,n.dims,S);Q.push(X)}o&&Q.push(P("attention_bias",o.dataType,o.dims));let W=u?P("seq_lens",u.dataType,u.dims):void 0;W&&Q.push(W);let Z=d?P("total_sequence_length_input",d.dataType,d.dims):void 0;Z&&Q.push(Z);let we=M("output",t.dataType,p),H=[we];m&&H.push(M("present_key",t.dataType,b,S));let j=Ae(1,S),te=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"head_size",type:"u32"},{name:"alpha",type:"f32"},{name:"past_sequence_length",type:"u32"},{name:"kv_sequence_length",type:"u32"},{name:"n_reps",type:"u32"}];return` + const TILE_SIZE = ${v}u; + + var tileQ: array<${L.type.storage}, ${v*v}>; + var tileK: array<${L.type.storage}, ${v*v}>; + ${D.registerUniforms(te).declareVariables(...Q,...H)} + ${D.mainStart([v,v,1])} + // x holds the N and y holds the M + let headIdx = workgroup_id.z % uniforms.num_heads; + let kvHeadIdx = ${g===1?"headIdx":"headIdx / uniforms.n_reps"}; + let kv_num_heads = ${g===1?"uniforms.num_heads":"uniforms.num_heads / uniforms.n_reps"}; + let batchIdx = workgroup_id.z / uniforms.num_heads; + let m = workgroup_id.y * TILE_SIZE; + let n = workgroup_id.x * TILE_SIZE; + let sequence_length = uniforms.M; + var total_sequence_length = uniforms.N; + ${lo(W,Z,!0)} + let absKvHeadIdx = batchIdx * kv_num_heads + kvHeadIdx; + let qOffset = workgroup_id.z * uniforms.M * uniforms.K + m * uniforms.K; + ${E&&m?"let pastKeyOffset = absKvHeadIdx * uniforms.past_sequence_length * uniforms.K;":""}; + let kOffset = absKvHeadIdx * uniforms.kv_sequence_length * uniforms.K; + ${m?"let presentKeyOffset = absKvHeadIdx * uniforms.N * uniforms.K;":""} + var value = ${j}(0); + for (var w: u32 = 0u; w < uniforms.K; w += TILE_SIZE) { + if (global_id.y < uniforms.M && w + local_id.x < uniforms.K) { + tileQ[TILE_SIZE * local_id.y + local_id.x] = q[qOffset + local_id.y * uniforms.K + w + local_id.x]; + } + if (n + local_id.y < uniforms.N && w + local_id.x < uniforms.K) { + var idx = TILE_SIZE * local_id.y + local_id.x; + ${E&&m?` + if (n + local_id.y < past_sequence_length) { + tileK[idx] = past_key[pastKeyOffset + (n + local_id.y) * uniforms.K + w + local_id.x]; + } else if (n + local_id.y - past_sequence_length < uniforms.kv_sequence_length) { + tileK[idx] = key[kOffset + (n + local_id.y - past_sequence_length) * uniforms.K + w + local_id.x]; + }`:` + if (n + local_id.y < uniforms.kv_sequence_length) { + tileK[idx] = key[kOffset + (n + local_id.y) * uniforms.K + w + local_id.x]; + }`} + ${m?`if (n + local_id.y < present_sequence_length) { + present_key[presentKeyOffset + (n + local_id.y) * uniforms.K + w + local_id.x] = tileK[idx]; + }`:""} + } + workgroupBarrier(); + + for (var k: u32 = 0u; k < TILE_SIZE && w+k < uniforms.K; k++) { + value += ${j}(tileQ[TILE_SIZE * local_id.y + k] * tileK[TILE_SIZE * local_id.x + k]); + } + + workgroupBarrier(); + } + + if (global_id.y < uniforms.M && global_id.x < total_sequence_length) { + let headOffset = workgroup_id.z * uniforms.M * uniforms.N; + let outputIdx = headOffset + global_id.y * uniforms.N + global_id.x; + var sum: f32 = ${(()=>{switch(S){case 1:return"value";case 2:return"value.x + value.y";case 4:return"value.x + value.y + value.z + value.w";default:throw new Error(`Unsupported components: ${S}`)}})()}; + output[outputIdx] = ${we.type.value} (sum * uniforms.alpha) + ${o?"attention_bias[outputIdx]":"0.0"}; + } + }`};return{name:"AttentionProbs",shaderCache:{hint:`${S};${o!==void 0};${n!==void 0};${e}`,inputDependencies:I},getRunData:()=>({outputs:z,dispatchGroup:x,programUniforms:T}),getShaderSource:O}},Wm=(e,t,r,n,o,i,a=void 0,u=void 0)=>{let d=i+o.kvSequenceLength,c=o.nReps?o.nReps:1,p=o.vHiddenSize*c,m=e>1&&n,f=o.kvNumHeads?o.kvNumHeads:o.numHeads,b=m?[o.batchSize,f,d,o.headSize]:void 0,g=[o.batchSize,o.sequenceLength,p],_=12,S={x:Math.ceil(o.vHeadSize/_),y:Math.ceil(o.sequenceLength/_),z:o.batchSize*o.numHeads},$=[{type:12,data:o.sequenceLength},{type:12,data:d},{type:12,data:o.vHeadSize},{type:12,data:o.numHeads},{type:12,data:o.headSize},{type:12,data:p},{type:12,data:i},{type:12,data:o.kvSequenceLength},{type:12,data:c}],v=m&&n&&k.size(n.dims)>0,x=["type","type"];v&&x.push("type"),a&&x.push("type"),u&&x.push("type");let T=[{dims:g,dataType:t.dataType,gpuDataType:0}];m&&T.push({dims:b,dataType:t.dataType,gpuDataType:0});let E=I=>{let z=P("probs",t.dataType,t.dims),O=P("v",r.dataType,r.dims),D=[z,O];v&&D.push(P("past_value",n.dataType,n.dims));let L=a?P("seq_lens",a.dataType,a.dims):void 0;a&&D.push(L);let q=u?P("total_sequence_length_input",u.dataType,u.dims):void 0;u&&D.push(q);let W=[M("output",t.dataType,g)];m&&W.push(M("present_value",t.dataType,b));let Z=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"head_size",type:"u32"},{name:"v_hidden_size",type:"u32"},{name:"past_sequence_length",type:"u32"},{name:"kv_sequence_length",type:"u32"},{name:"n_reps",type:"u32"}];return` + const TILE_SIZE = ${_}u; + var tileQ: array<${z.type.value}, ${_*_}>; + var tileV: array<${z.type.value}, ${_*_}>; + ${I.registerUniforms(Z).declareVariables(...D,...W)} + ${I.mainStart([_,_,1])} + let headIdx = workgroup_id.z % uniforms.num_heads; + let batchIdx = workgroup_id.z / uniforms.num_heads; + let kvHeadIdx = ${c===1?"headIdx":"headIdx / uniforms.n_reps"}; + let kv_num_heads = ${c===1?"uniforms.num_heads":"uniforms.num_heads / uniforms.n_reps"}; + let m = global_id.y; + let n = global_id.x; + let sequence_length = uniforms.M; + var total_sequence_length = uniforms.K; + ${lo(L,q,!0)} + let offsetA = workgroup_id.z * uniforms.M * uniforms.K + m * uniforms.K; + let absKvHeadIdx = batchIdx * kv_num_heads + kvHeadIdx; // kvHeadIdx is relative to the batch + ${v&&m?"let pastValueOffset = absKvHeadIdx * uniforms.N * uniforms.past_sequence_length + n;":""}; + let vOffset = absKvHeadIdx * uniforms.N * uniforms.kv_sequence_length + n; + ${m?"let presentValueOffset = absKvHeadIdx * uniforms.N * uniforms.K + n;":""} + var value = ${z.type.storage}(0); + for (var w: u32 = 0u; w < uniforms.K; w += TILE_SIZE) { + if (m < uniforms.M && w + local_id.x < uniforms.K) { + tileQ[TILE_SIZE * local_id.y + local_id.x] = probs[offsetA + w + local_id.x]; + } + if (n < uniforms.N && w + local_id.y < uniforms.K) { + var idx = TILE_SIZE * local_id.y + local_id.x; + ${v&&m?` + if (w + local_id.y < past_sequence_length) { + tileV[idx] = past_value[pastValueOffset + (w + local_id.y) * uniforms.N]; + } else if (w + local_id.y - past_sequence_length < uniforms.kv_sequence_length) { + tileV[idx] = v[vOffset + (w + local_id.y - past_sequence_length) * uniforms.N]; + } + `:` + if (w + local_id.y < uniforms.kv_sequence_length) { + tileV[idx] = v[vOffset + (w + local_id.y) * uniforms.N]; + }`} + ${m?` + if (w + local_id.y < present_sequence_length) { + present_value[presentValueOffset + (w + local_id.y) * uniforms.N] = tileV[idx]; + }`:""} + } + workgroupBarrier(); + for (var k: u32 = 0u; k < TILE_SIZE && w+k < total_sequence_length; k++) { + value += tileQ[TILE_SIZE * local_id.y + k] * tileV[TILE_SIZE * k + local_id.x]; + } + workgroupBarrier(); + } + + // we need to transpose output from BNSH_v to BSND_v + if (m < uniforms.M && n < uniforms.N) { + let outputIdx = batchIdx * uniforms.M * uniforms.v_hidden_size + m * uniforms.v_hidden_size + + headIdx * uniforms.N + n; + output[outputIdx] = value; + } + }`};return{name:"AttentionScore",shaderCache:{hint:`${n!==void 0};${e}`,inputDependencies:x},getRunData:()=>({outputs:T,dispatchGroup:S,programUniforms:$}),getShaderSource:E}},Rt=(e,t,r,n,o,i,a,u,d,c,p=void 0,m=void 0)=>{let f=Math.min(e.outputCount,1+(a?1:0)+(u?1:0)),b=f>1?c.pastSequenceLength:0,g=b+c.kvSequenceLength,_=d&&k.size(d.dims)>0?d:void 0,S=[t,r];f>1&&a&&k.size(a.dims)>0&&S.push(a),_&&S.push(_),p&&S.push(p),m&&S.push(m);let $=e.compute(Vm(f,t,r,a,_,c,b,p,m),{inputs:S,outputs:f>1?[-1,1]:[-1]})[0];e.compute(Nm($,c.batchSize,c.numHeads,b,c.sequenceLength,g,p,m),{inputs:p&&m?[$,p,m]:[$],outputs:[]});let v=[$,n];f>1&&u&&k.size(u.dims)>0&&v.push(u),p&&v.push(p),m&&v.push(m),e.compute(Wm(f,$,n,u,c,b,p,m),{inputs:v,outputs:f>1?[0,2]:[0]})},Lm=(e,t)=>{let r=[t.batchSize,t.numHeads,t.sequenceLength,t.headSize],n=t.sequenceLength,o=t.inputHiddenSize,i=t.headSize,a=12,u={x:Math.ceil(t.headSize/a),y:Math.ceil(t.sequenceLength/a),z:t.batchSize*t.numHeads},d=[e.inputs[0],e.inputs[1],e.inputs[2]],c=[{type:12,data:n},{type:12,data:o},{type:12,data:i},{type:12,data:t.numHeads},{type:12,data:t.headSize},{type:12,data:t.hiddenSize},{type:12,data:t.hiddenSize+t.hiddenSize+t.vHiddenSize}],p=m=>{let f=M("output_q",d[0].dataType,r),b=M("output_k",d[0].dataType,r),g=M("output_v",d[0].dataType,r),_=P("input",d[0].dataType,d[0].dims),S=P("weight",d[1].dataType,d[1].dims),$=P("bias",d[2].dataType,d[2].dims),v=_.type.storage,x=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"head_size",type:"u32"},{name:"hidden_size",type:"u32"},{name:"ldb",type:"u32"}];return` + const TILE_SIZE = ${a}u; + var tileInput: array<${v}, ${a*a}>; + var tileWeightQ: array<${v}, ${a*a}>; + var tileWeightK: array<${v}, ${a*a}>; + var tileWeightV: array<${v}, ${a*a}>; + ${m.registerUniforms(x).declareVariables(_,S,$,f,b,g)} + ${m.mainStart([a,a,1])} + let batchIndex = workgroup_id.z / uniforms.num_heads; + let headNumber = workgroup_id.z % uniforms.num_heads; + let m = global_id.y; + let n = global_id.x; + + let inputOffset = batchIndex * (uniforms.M * uniforms.K) + m * uniforms.K; + let biasOffsetQ = headNumber * uniforms.head_size; + let biasOffsetK = uniforms.hidden_size + biasOffsetQ; + let biasOffsetV = uniforms.hidden_size + biasOffsetK; + + var valueQ = ${v}(0); + var valueK = ${v}(0); + var valueV = ${v}(0); + for (var w: u32 = 0u; w < uniforms.K; w += TILE_SIZE) { + if (m < uniforms.M && w + local_id.x < uniforms.K) { + tileInput[TILE_SIZE * local_id.y + local_id.x] = input[inputOffset + w + local_id.x]; + } + if (n < uniforms.N && w + local_id.y < uniforms.K) { + let offset = n + (w + local_id.y) * uniforms.ldb; + tileWeightQ[TILE_SIZE * local_id.y + local_id.x] = weight[biasOffsetQ + offset]; + tileWeightK[TILE_SIZE * local_id.y + local_id.x] = weight[biasOffsetK + offset]; + tileWeightV[TILE_SIZE * local_id.y + local_id.x] = weight[biasOffsetV + offset]; + } + workgroupBarrier(); + for (var k: u32 = 0u; k({outputs:[{dims:r,dataType:e.inputs[0].dataType,gpuDataType:0},{dims:r,dataType:e.inputs[0].dataType,gpuDataType:0},{dims:r,dataType:e.inputs[0].dataType,gpuDataType:0}],dispatchGroup:u,programUniforms:c}),getShaderSource:p},{inputs:d,outputs:[-1,-1,-1]})},ks=(e,t)=>{let r=Um(e.inputs,t),[n,o,i]=Lm(e,r);return Rt(e,n,o,i,e.inputs[4],void 0,void 0,void 0,e.inputs[5],r)}});var Gm,Hm,Fm,Ps,zs=U(()=>{"use strict";We();ee();ne();Se();ie();Gm=(e,t)=>{if(!e||e.length!==5)throw new Error("BatchNormalization requires 5 inputs");let r=(n,o,i)=>{let a=o.length;if(a!==n.length)throw new Error(`${i}: num dimensions != ${a}`);o.forEach((u,d)=>{if(u!==n[d])throw new Error(`${i}: dim[${d}] do not match`)})};if(e[0].dims.length>1){let n=t.format==="NHWC"?t.spatial?e[0].dims.slice(-1):e[0].dims.slice(-1).concat(e[0].dims.slice(1,e[0].dims.length-1)):e[0].dims.slice(1,t.spatial?2:void 0);r(e[1].dims,n,"Invalid input scale"),r(e[2].dims,n,"Invalid input B"),r(e[3].dims,n,"Invalid input mean"),r(e[4].dims,n,"Invalid input var")}else r(e[1].dims,[1],"Invalid input scale"),r(e[2].dims,[1],"Invalid input B"),r(e[3].dims,[1],"Invalid input mean"),r(e[4].dims,[1],"Invalid input var")},Hm=(e,t)=>{let{epsilon:r,spatial:n,format:o}=t,i=e[0].dims,a=n?ce(i[i.length-1]):1,u=o==="NHWC"&&i.length>1?a:1,d=k.size(i)/a,c=n,p=c?i.length:i,m=P("x",e[0].dataType,e[0].dims,a),f=P("scale",e[1].dataType,e[1].dims,u),b=P("bias",e[2].dataType,e[2].dims,u),g=P("inputMean",e[3].dataType,e[3].dims,u),_=P("inputVar",e[4].dataType,e[4].dims,u),S=M("y",e[0].dataType,p,a),$=()=>{let x="";if(n)x=`let cOffset = ${i.length===1?"0u":o==="NHWC"?`outputIndices[${i.length-1}] / ${a}`:"outputIndices[1]"};`;else if(o==="NCHW")x=` + ${S.indicesSet("outputIndices","0","0")} + let cOffset = ${S.indicesToOffset("outputIndices")};`;else{x=`var cIndices = ${f.type.indices}(0); + cIndices[0] = outputIndices[${i.length-1}];`;for(let T=1;T` + const epsilon = ${r}; + ${x.registerUniform("outputSize","u32").declareVariables(m,f,b,g,_,S)} + ${x.mainStart()} + ${x.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + var outputIndices = ${S.offsetToIndices(`global_idx * ${a}`)}; + ${$()} + let scale = ${f.getByOffset("cOffset")}; + let bias = ${b.getByOffset("cOffset")}; + let inputMean = ${g.getByOffset("cOffset")}; + let inputVar = ${_.getByOffset("cOffset")}; + let x = ${m.getByOffset("global_idx")}; + let value = (x - inputMean) * inverseSqrt(inputVar + epsilon) * scale + bias; + ${S.setByOffset("global_idx","value")} + }`;return{name:"BatchNormalization",shaderCache:{hint:`${t.epsilon}_${t.format}_${n}_${a}`,inputDependencies:c?["rank","type","type","type","type"]:void 0},getShaderSource:v,getRunData:()=>({outputs:[{dims:e[0].dims,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(d/64)},programUniforms:c?[{type:12,data:d},...N(i)]:[{type:12,data:d}]})}},Fm=e=>J(e),Ps=(e,t)=>{let{inputs:r,outputCount:n}=e,o=Fm({...t,outputCount:n});if(ge.webgpu.validateInputContent&&Gm(r,o),t.trainingMode)throw new Error("BatchNormalization trainingMode is not supported yet.");e.compute(Hm(r,o))}});var qm,jm,Os,Bs=U(()=>{"use strict";ne();ie();qm=e=>{if(e[0].dims.length!==3)throw new Error("input should have 3 dimensions");if(![320,640,1280].includes(e[0].dims[2]))throw new Error("number of channels should be 320, 640 or 1280");if(e[1].dims.length!==1)throw new Error("bias is expected to have 1 dimensions");if(e[0].dims[2]!==e[1].dims[0])throw new Error("last dimension of input and bias are not the same")},jm=e=>{let t=e[0].dims,r=e[0].dims[2],n=k.size(t)/4,o=e[0].dataType,i=P("input",o,t,4),a=P("bias",o,[r],4),u=P("residual",o,t,4),d=M("output",o,t,4);return{name:"BiasAdd",getRunData:()=>({outputs:[{dims:t,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(n/64)}}),getShaderSource:p=>` + const channels = ${r}u / 4; + ${p.declareVariables(i,a,u,d)} + + ${p.mainStart()} + ${p.guardAgainstOutOfBoundsWorkgroupSizes(n)} + let value = ${i.getByOffset("global_idx")} + + ${a.getByOffset("global_idx % channels")} + ${u.getByOffset("global_idx")}; + ${d.setByOffset("global_idx","value")} + }`}},Os=e=>{qm(e.inputs),e.compute(jm(e.inputs))}});var Km,me,Ds,Ms,Rs,Us,Ns,Vs,Ws,Ls,Gs,Zm,Hs,Fs,qs,js,Yt,Ks,qr,Zs,Qs,Ys,Xs,Js,eu,tu,ru,nu,ou,iu,au,su,uu,du,lu,cu,pu,co,po,mu,fu,hu,Qm,Ym,gu,jr=U(()=>{"use strict";ee();ne();Se();ie();Km=(e,t,r,n,o,i,a)=>{let u=Math.ceil(t/4),d="";typeof o=="string"?d=`${o}(a)`:d=o("a");let c=P("inputData",r,[u],4),p=M("outputData",n,[u],4),m=[{name:"vec_size",type:"u32"}];return a&&m.push(...a),` + ${e.registerUniforms(m).declareVariables(c,p)} + + ${i??""} + + ${e.mainStart()} + ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")} + + let a = ${c.getByOffset("global_idx")}; + ${p.setByOffset("global_idx",d)} + }`},me=(e,t,r,n,o,i=e.dataType,a,u)=>{let d=[{type:12,data:Math.ceil(k.size(e.dims)/4)}];return a&&d.push(...a),{name:t,shaderCache:{hint:o,inputDependencies:["type"]},getShaderSource:c=>Km(c,k.size(e.dims),e.dataType,i,r,n,u),getRunData:c=>({outputs:[{dims:e.dims,dataType:i}],dispatchGroup:{x:Math.ceil(k.size(c[0].dims)/64/4)},programUniforms:d})}},Ds=e=>{e.compute(me(e.inputs[0],"Abs","abs"))},Ms=e=>{e.compute(me(e.inputs[0],"Acos","acos"))},Rs=e=>{e.compute(me(e.inputs[0],"Acosh","acosh"))},Us=e=>{e.compute(me(e.inputs[0],"Asin","asin"))},Ns=e=>{e.compute(me(e.inputs[0],"Asinh","asinh"))},Vs=e=>{e.compute(me(e.inputs[0],"Atan","atan"))},Ws=e=>{e.compute(me(e.inputs[0],"Atanh","atanh"))},Ls=e=>J(e),Gs=(e,t)=>{let r;switch(t.to){case 10:r="vec4";break;case 1:r="vec4";break;case 12:r="vec4";break;case 6:r="vec4";break;case 9:r="vec4";break;default:throw new RangeError(`not supported type (specified in attribute 'to' from 'Cast' operator): ${t.to}`)}e.compute(me(e.inputs[0],"Cast",r,void 0,t.cacheKey,t.to))},Zm=e=>{let t,r,n=e.length>=2&&e[1].data!==0,o=e.length>=3&&e[2].data!==0;switch(e[0].dataType){case 1:t=n?e[1].getFloat32Array()[0]:-34028234663852886e22,r=o?e[2].getFloat32Array()[0]:34028234663852886e22;break;case 10:t=n?e[1].getUint16Array()[0]:64511,r=o?e[2].getUint16Array()[0]:31743;break;default:throw new Error("Unsupport data type")}return J({min:t,max:r})},Hs=(e,t)=>{let r=t||Zm(e.inputs),n=Ae(e.inputs[0].dataType);e.compute(me(e.inputs[0],"Clip",o=>`clamp(${o}, vec4<${n}>(uniforms.min), vec4<${n}>(uniforms.max))`,void 0,r.cacheKey,void 0,[{type:e.inputs[0].dataType,data:r.min},{type:e.inputs[0].dataType,data:r.max}],[{name:"min",type:n},{name:"max",type:n}]),{inputs:[0]})},Fs=e=>{e.compute(me(e.inputs[0],"Ceil","ceil"))},qs=e=>{e.compute(me(e.inputs[0],"Cos","cos"))},js=e=>{e.compute(me(e.inputs[0],"Cosh","cosh"))},Yt=e=>J(e),Ks=(e,t)=>{let r=Ae(e.inputs[0].dataType);e.compute(me(e.inputs[0],"Elu",n=>`elu_vf32(${n})`,` + const elu_alpha_ = ${r}(${t.alpha}); + + fn elu_f32(a: ${r}) -> ${r} { + return select((exp(a) - 1.0) * elu_alpha_, a, a >= 0.0); + } + + fn elu_vf32(v: vec4<${r}>) -> vec4<${r}> { + return vec4(elu_f32(v.x), elu_f32(v.y), elu_f32(v.z), elu_f32(v.w)); + }`,t.cacheKey))},qr=(e="f32")=>` +const r0: ${e} = 0.3275911; +const r1: ${e} = 0.254829592; +const r2: ${e} = -0.284496736; +const r3: ${e} = 1.421413741; +const r4: ${e} = -1.453152027; +const r5: ${e} = 1.061405429; + +fn erf_vf32(v: vec4<${e}>) -> vec4<${e}> { + let absv = abs(v); + let x = 1.0 / (1.0 + r0 * absv); + return sign(v) * (1.0 - ((((r5 * x + r4) * x + r3) * x + r2) * x + r1) * x * exp(-absv * absv)); +}`,Zs=e=>{let t=Ae(e.inputs[0].dataType);e.compute(me(e.inputs[0],"Erf",r=>`erf_vf32(${r})`,qr(t)))},Qs=e=>{e.compute(me(e.inputs[0],"Exp","exp"))},Ys=e=>{e.compute(me(e.inputs[0],"Floor","floor"))},Xs=e=>{let t=Ae(e.inputs[0].dataType);e.compute(me(e.inputs[0],"Gelu",r=>`0.5 * ${r} * (1.0 + erf_vf32(${r} * 0.7071067811865475))`,qr(t)))},Js=(e,t)=>{let r=Ae(e.inputs[0].dataType);e.compute(me(e.inputs[0],"LeakyRelu",n=>`select(leaky_relu_alpha_ * ${n}, ${n}, ${n} >= vec4<${r}>(0.0))`,`const leaky_relu_alpha_ = ${r}(${t.alpha});`,t.cacheKey))},eu=e=>{e.compute(me(e.inputs[0],"Not",t=>`!${t}`))},tu=e=>{e.compute(me(e.inputs[0],"Neg",t=>`-${t}`))},ru=e=>{e.compute(me(e.inputs[0],"Reciprocal",t=>`1.0/${t}`))},nu=e=>{let t=Ae(e.inputs[0].dataType);e.compute(me(e.inputs[0],"Relu",r=>`select(vec4<${t}>(0.0), ${r}, ${r} > vec4<${t}>(0.0))`))},ou=e=>{e.compute(me(e.inputs[0],"Sigmoid",t=>`(1.0 / (1.0 + exp(-${t})))`))},iu=e=>J(e),au=(e,t)=>{let r=Ae(e.inputs[0].dataType);e.compute(me(e.inputs[0],"HardSigmoid",n=>`max(vec4<${r}>(0.0), min(vec4<${r}>(1.0), ${t.alpha} * ${n} + vec4<${r}>(${t.beta})))`,void 0,t.cacheKey))},su=e=>{e.compute(me(e.inputs[0],"Sin","sin"))},uu=e=>{e.compute(me(e.inputs[0],"Sinh","sinh"))},du=e=>{e.compute(me(e.inputs[0],"Sqrt","sqrt"))},lu=e=>{e.compute(me(e.inputs[0],"Tan","tan"))},cu=e=>`sign(${e}) * (1 - exp(-2 * abs(${e}))) / (1 + exp(-2 * abs(${e})))`,pu=e=>{e.compute(me(e.inputs[0],"Tanh",cu))},co=(e="f32")=>` +const fast_gelu_a: ${e} = 0.5; +const fast_gelu_b: ${e} = 0.7978845608028654; +const fast_gelu_c: ${e} = 0.035677408136300125; + +fn tanh_v(v: vec4<${e}>) -> vec4<${e}> { + return ${cu("v")}; +} +`,po=e=>`(fast_gelu_a + fast_gelu_a * tanh_v(${e} * (fast_gelu_c * ${e} * ${e} + fast_gelu_b))) * ${e}`,mu=e=>{let t=Ae(e.inputs[0].dataType);e.compute(me(e.inputs[0],"FastGelu",po,co(t),void 0,e.inputs[0].dataType))},fu=(e,t)=>{let r=Ae(e.inputs[0].dataType);return e.compute(me(e.inputs[0],"ThresholdedRelu",n=>`select(vec4<${r}>(0.0), ${n}, ${n} > thresholded_relu_alpha_)`,`const thresholded_relu_alpha_ = vec4<${r}>(${t.alpha});`,t.cacheKey)),0},hu=e=>{e.compute(me(e.inputs[0],"Log","log"))},Qm=(e,t)=>` +const alpha = vec4<${e}>(${t}); +const one = ${e}(1.0); +const zero = ${e}(0.0); + +fn quick_gelu_impl(x: vec4<${e}>) -> vec4<${e}> { + let v = x *alpha; + var x1 : vec4<${e}>; + for (var i = 0; i < 4; i = i + 1) { + if (v[i] >= zero) { + x1[i] = one / (one + exp(-v[i])); + } else { + x1[i] = one - one / (one + exp(v[i])); + } + } + return x * x1; +} +`,Ym=e=>`quick_gelu_impl(${e})`,gu=(e,t)=>{let r=Ae(e.inputs[0].dataType);e.compute(me(e.inputs[0],"QuickGelu",Ym,Qm(r,t.alpha),t.cacheKey,e.inputs[0].dataType))}});var Xm,Jm,yu,_u=U(()=>{"use strict";ne();ie();jr();Xm=e=>{if(e[0].dims.length!==3)throw new Error("input should have 3 dimensions");if(![2560,5120,10240].includes(e[0].dims[2]))throw new Error("hidden state should be 2560, 5120 or 10240");if(e[1].dims.length!==1)throw new Error("bias is expected to have 1 dimensions");if(e[0].dims[2]!==e[1].dims[0])throw new Error("last dimension of input and bias are not the same")},Jm=e=>{let t=e[0].dims.slice();t[2]=t[2]/2;let r=P("input",e[0].dataType,e[0].dims,4),n=P("bias",e[0].dataType,[e[0].dims[2]],4),o=M("output",e[0].dataType,t,4),i=k.size(t)/4,a=be(e[0].dataType);return{name:"BiasSplitGelu",getRunData:()=>({outputs:[{dims:t,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(i/64)}}),getShaderSource:d=>` + const M_SQRT2 = sqrt(2.0); + const halfChannels = ${e[0].dims[2]/4/2}u; + + ${d.declareVariables(r,n,o)} + + ${qr(a)} + + ${d.mainStart()} + ${d.guardAgainstOutOfBoundsWorkgroupSizes(i)} + let biasIdx = global_idx % halfChannels; + let batchIndex = global_idx / halfChannels; + let inputOffset = biasIdx + batchIndex * halfChannels * 2; + let valueLeft = input[inputOffset] + bias[biasIdx]; + let valueRight = input[inputOffset + halfChannels] + bias[biasIdx + halfChannels]; + let geluRight = valueRight * 0.5 * (erf_vf32(valueRight / M_SQRT2) + 1); + + ${o.setByOffset("global_idx","valueLeft * geluRight")} + }`}},yu=e=>{Xm(e.inputs),e.compute(Jm(e.inputs))}});var ef,tf,ot,wu,vu,$u,xu,Su,Tu,Iu,Cu,Au,Eu,ku=U(()=>{"use strict";ee();ne();ie();ef=(e,t,r,n,o,i,a,u,d,c,p,m)=>{let f,b;typeof u=="string"?f=b=(v,x)=>`${u}((${v}),(${x}))`:typeof u=="function"?f=b=u:(f=u.scalar,b=u.vector);let g=M("outputData",p,n.length,4),_=P("aData",d,t.length,4),S=P("bData",c,r.length,4),$;if(o)if(i){let v=k.size(t)===1,x=k.size(r)===1,T=t.length>0&&t[t.length-1]%4===0,E=r.length>0&&r[r.length-1]%4===0;v||x?$=g.setByOffset("global_idx",b(v?`${_.type.value}(${_.getByOffset("0")}.x)`:_.getByOffset("global_idx"),x?`${S.type.value}(${S.getByOffset("0")}.x)`:S.getByOffset("global_idx"))):$=` + let outputIndices = ${g.offsetToIndices("global_idx * 4u")}; + let offsetA = ${_.broadcastedIndicesToOffset("outputIndices",g)}; + let offsetB = ${S.broadcastedIndicesToOffset("outputIndices",g)}; + ${g.setByOffset("global_idx",b(a||T?_.getByOffset("offsetA / 4u"):`${_.type.value}(${_.getByOffset("offsetA / 4u")}[offsetA % 4u])`,a||E?S.getByOffset("offsetB / 4u"):`${S.type.value}(${S.getByOffset("offsetB / 4u")}[offsetB % 4u])`))} + `}else $=g.setByOffset("global_idx",b(_.getByOffset("global_idx"),S.getByOffset("global_idx")));else{if(!i)throw new Error("no necessary to use scalar implementation for element-wise binary op implementation.");let v=(x,T,E="")=>{let I=`aData[indexA${T}][componentA${T}]`,z=`bData[indexB${T}][componentB${T}]`;return` + let outputIndices${T} = ${g.offsetToIndices(`global_idx * 4u + ${T}u`)}; + let offsetA${T} = ${_.broadcastedIndicesToOffset(`outputIndices${T}`,g)}; + let offsetB${T} = ${S.broadcastedIndicesToOffset(`outputIndices${T}`,g)}; + let indexA${T} = offsetA${T} / 4u; + let indexB${T} = offsetB${T} / 4u; + let componentA${T} = offsetA${T} % 4u; + let componentB${T} = offsetB${T} % 4u; + ${x}[${T}] = ${E}(${f(I,z)}); + `};p===9?$=` + var data = vec4(0); + ${v("data",0,"u32")} + ${v("data",1,"u32")} + ${v("data",2,"u32")} + ${v("data",3,"u32")} + outputData[global_idx] = dot(vec4(0x1, 0x100, 0x10000, 0x1000000), vec4(data));`:$=` + ${v("outputData[global_idx]",0)} + ${v("outputData[global_idx]",1)} + ${v("outputData[global_idx]",2)} + ${v("outputData[global_idx]",3)} + `}return` + ${e.registerUniform("vec_size","u32").declareVariables(_,S,g)} + + ${m??""} + + ${e.mainStart()} + ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")} + ${$} + }`},tf=(e,t,r,n,o,i,a=r.dataType)=>{let u=r.dims.map(_=>Number(_)??1),d=n.dims.map(_=>Number(_)??1),c=!k.areEqual(u,d),p=u,m=k.size(u),f=!1,b=!1,g=[c];if(c){let _=Je.calcShape(u,d,!1);if(!_)throw new Error("Can't perform binary op on the given tensors");p=_.slice(),m=k.size(p);let S=k.size(u)===1,$=k.size(d)===1,v=u.length>0&&u[u.length-1]%4===0,x=d.length>0&&d[d.length-1]%4===0;g.push(S),g.push($),g.push(v),g.push(x);let T=1;for(let E=1;E_.toString()).join("_"),inputDependencies:["rank","rank"]},getShaderSource:_=>ef(_,u,d,p,f,c,b,o,r.dataType,n.dataType,a,i),getRunData:()=>({outputs:[{dims:p,dataType:a}],dispatchGroup:{x:Math.ceil(m/64/4)},programUniforms:[{type:12,data:Math.ceil(k.size(p)/4)},...N(u,d,p)]})}},ot=(e,t,r,n,o,i)=>{e.compute(tf(t,o??"",e.inputs[0],e.inputs[1],r,n,i))},wu=e=>{ot(e,"Add",(t,r)=>`${t}+${r}`)},vu=e=>{ot(e,"Div",(t,r)=>`${t}/${r}`)},$u=e=>{ot(e,"Equal",{scalar:(t,r)=>`u32(${t}==${r})`,vector:(t,r)=>`vec4(${t}==${r})`},void 0,void 0,9)},xu=e=>{ot(e,"Mul",(t,r)=>`${t}*${r}`)},Su=e=>{let t=P("input",e.inputs[0].dataType,e.inputs[0].dims).type.value;ot(e,"Pow",{scalar:(n,o)=>`pow_custom(${n},${o})`,vector:(n,o)=>`pow_vector_custom(${n},${o})`},` + fn pow_custom(a : ${t}, b : ${t}) -> ${t} { + if (b == ${t}(0.0)) { + return ${t}(1.0); + } else if (a < ${t}(0.0) && f32(b) != floor(f32(b))) { + return ${t}(pow(f32(a), f32(b))); // NaN + } + return select(sign(a), ${t}(1.0), round(f32(abs(b) % ${t}(2.0))) != 1.0) * ${t}(${t==="i32"?"round":""}(pow(f32(abs(a)), f32(b)))); + } + fn pow_vector_custom(a : vec4<${t}>, b : vec4<${t}>) -> vec4<${t}> { + // TODO: implement vectorized pow + return vec4<${t}>(pow_custom(a.x, b.x), pow_custom(a.y, b.y), pow_custom(a.z, b.z), pow_custom(a.w, b.w)); + } + `)},Tu=e=>{ot(e,"Sub",(t,r)=>`${t}-${r}`)},Iu=e=>{ot(e,"Greater",{scalar:(t,r)=>`u32(${t}>${r})`,vector:(t,r)=>`vec4(${t}>${r})`},void 0,void 0,9)},Cu=e=>{ot(e,"Less",{scalar:(t,r)=>`u32(${t}<${r})`,vector:(t,r)=>`vec4(${t}<${r})`},void 0,void 0,9)},Au=e=>{ot(e,"GreaterOrEqual",{scalar:(t,r)=>`u32(${t}>=${r})`,vector:(t,r)=>`vec4(${t}>=${r})`},void 0,void 0,9)},Eu=e=>{ot(e,"LessOrEqual",{scalar:(t,r)=>`u32(${t}<=${r})`,vector:(t,r)=>`vec4(${t}<=${r})`},void 0,void 0,9)}});var nf,of,af,sf,Pu,zu,Ou=U(()=>{"use strict";ee();ne();Se();ie();nf=(e,t)=>{if(!e||e.length<1)throw new Error("too few inputs");let r=0,n=e[r],o=n.dataType,i=n.dims.length;e.forEach((a,u)=>{if(u!==r){if(a.dataType!==o)throw new Error("input tensors should be one type");if(a.dims.length!==i)throw new Error("input tensors should have the same shape");a.dims.forEach((d,c)=>{if(c!==t&&d!==n.dims[c])throw new Error("non concat dimensions must match")})}})},of=(e,t)=>` + fn calculateInputIndex(index: u32) -> u32 { + let sizeInConcatAxis = array(${t}); + for (var i: u32 = 0u; i < ${e}; i += 1u ) { + if (index < sizeInConcatAxis[i]) { + return i; + } + } + return ${e}u; + }`,af=(e,t)=>{let r=e.length,n=[];for(let o=0;o{let o=k.size(r),i=new Array(e.length),a=new Array(e.length),u=0,d=[],c=[],p=[{type:12,data:o}];for(let _=0;_`uniforms.sizeInConcatAxis${_}`).join(","),g=_=>` + + ${(()=>{_.registerUniform("outputSize","u32");for(let S=0;S(${b}); + ${f} -= sizeInConcatAxis[inputIndex - 1u]; + } + + ${af(a,m)} + }`;return{name:"Concat",shaderCache:{hint:`${t}`,inputDependencies:d},getRunData:()=>({outputs:[{dims:r,dataType:n}],dispatchGroup:{x:Math.ceil(o/64)},programUniforms:p}),getShaderSource:g}},Pu=(e,t)=>{let r=e.inputs,n=r[0].dims,o=k.normalizeAxis(t.axis,n.length);nf(r,o);let i=n.slice();i[o]=r.reduce((u,d)=>u+(d.dims.length>o?d.dims[o]:0),0);let a=r.filter(u=>k.size(u.dims)>0);e.compute(sf(a,o,i,r[0].dataType),{inputs:a})},zu=e=>J({axis:e.axis})});var Fe,qe,je,Kr,bt=U(()=>{"use strict";ee();ne();Fe=(e,t,r="f32")=>{switch(e.activation){case"Relu":return`value = max(value, ${t}(0.0));`;case"Sigmoid":return`value = (${t}(1.0) / (${t}(1.0) + exp(-value)));`;case"Clip":return`value = clamp(value, ${t}(${r}(uniforms.clip_min)), ${t}(${r}(uniforms.clip_max)));`;case"HardSigmoid":return`value = max(${t}(0.0), min(${t}(1.0), ${r}(uniforms.alpha) * value + ${r}(uniforms.beta)));`;case"LeakyRelu":return`value = select(${r}(uniforms.alpha) * value, value, value >= ${t}(0.0));`;case"Tanh":return`let e2x = exp(-2.0 * abs(value)); + value = sign(value) * (1.0 - e2x) / (1.0 + e2x); + `;case"":return"";default:throw new Error(`Unsupported activation ${e.activation}`)}},qe=(e,t)=>{e.activation==="Clip"?t.push({type:1,data:e.clipMax},{type:1,data:e.clipMin}):e.activation==="HardSigmoid"?t.push({type:1,data:e.alpha},{type:1,data:e.beta}):e.activation==="LeakyRelu"&&t.push({type:1,data:e.alpha})},je=(e,t)=>{e.activation==="Clip"?t.push({name:"clip_max",type:"f32"},{name:"clip_min",type:"f32"}):e.activation==="HardSigmoid"?t.push({name:"alpha",type:"f32"},{name:"beta",type:"f32"}):e.activation==="LeakyRelu"&&t.push({name:"alpha",type:"f32"})},Kr=e=>{let t=e?.activation||"";if(t==="HardSigmoid"){let[r,n]=e?.activation_params||[.2,.5];return{activation:t,alpha:r,beta:n}}else if(t==="Clip"){let[r,n]=e?.activation_params||[Wa,La];return{activation:t,clipMax:n,clipMin:r}}else if(t==="LeakyRelu"){let[r]=e?.activation_params||[.01];return{activation:t,alpha:r}}return{activation:t}}});var Ie,Bu,Zr=U(()=>{"use strict";Ie=(e,t)=>{switch(e){case 1:return t;case 2:return`vec2<${t}>`;case 3:return`vec3<${t}>`;case 4:return`vec4<${t}>`;default:throw new Error(`${e}-component is not supported.`)}},Bu=e=>` + ${e?"value = value + getBiasByOutputCoords(coords);":""} + `});var Du,Mu=U(()=>{"use strict";Du=e=>` +fn getIndexFromCoords4D(coords : vec4, shape : vec4) -> i32 { + return dot(coords, vec4( + shape.y * shape.z * shape.w, shape.z * shape.w, shape.w, 1)); +} +fn getOutputIndexFromCoords(coords : vec4) -> i32 { + return dot(coords, vec4( + i32(${e}.x), i32(${e}.y), i32(${e}.z), 1)); +} +`});var Xt,Qr,Yr=U(()=>{"use strict";ee();ne();ie();bt();Xt=(e,t,r,n,o)=>{let i=n-r;return` + ${Array.from({length:r}).map((a,u)=>` + if (${F(t.shape,u,t.rank)} != 1) { + ${t.indicesSet(e,u,F(o,u+i,n))} + } else { + ${t.indicesSet(e,u,0)} + }`).join("")} +`},Qr=(e,t,r,n,o=!1,i)=>{let a=e[0].dims,u=e[1].dims,d=a[a.length-2],c=u[u.length-1],p=a[a.length-1],m=ce(c),f=ce(p),b=ce(d),g=k.size(r)/m/b,_=e.length>2,S=n?n.slice(0,-2):r.slice(0,-2),v=[k.size(S),d,c],x=[{type:12,data:g},{type:12,data:d},{type:12,data:c},{type:12,data:p}];qe(t,x),x.push(...N(S,a,u)),_&&x.push(...N(e[2].dims)),x.push(...N(v));let T=E=>{let I=Lr("batch_dims",e[0].dataType,S.length),z=P("a",e[0].dataType,a.length,f),O=P("b",e[1].dataType,u.length,m),D=M("output",e[0].dataType,v.length,m),L=be(D.type.tensor),q=Fe(t,D.type.value,L),Q=[z,O],W="";if(_){let H=o?m:1;Q.push(P("bias",e[2].dataType,e[2].dims.length,H)),W=`${o?`value += bias[col / ${H}];`:`value += ${D.type.value}(bias[row + i]);`}`}let Z=[{name:"output_size",type:"u32"},{name:"M",type:"u32"},{name:"N",type:"u32"},{name:"K",type:"u32"}];je(t,Z);let we=()=>{let H=`var a_data: ${z.type.value};`;for(let j=0;j; + for (var k: u32 = 0u; k < uniforms.K; k = k + ${f}) { + ${we()} + } + for (var i = 0u; i < ${b}u; i++) { + var value = values[i]; + ${W} + ${q} + let cur_indices = ${D.type.indices}(batch, row + i, col); + let offset = ${D.indicesToOffset("cur_indices")}; + ${D.setByOffset(`offset / ${m}`,"value")}; + } + } + `};return{name:"MatMulNaive",shaderCache:{hint:`${t.activation};${m};${f};${b};${o}`,inputDependencies:_?["rank","rank","rank"]:["rank","rank"]},getRunData:()=>({outputs:[{dims:i?i(r):r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(g/64)},programUniforms:x}),getShaderSource:T}}});var uf,df,mo,Ru,lf,fo,cf,Jt,Xr=U(()=>{"use strict";ee();ne();ie();bt();Yr();Zr();uf=(e,t)=>e?` + mm_Asub[inputRow][inputCol] = mm_readA(batch, + kStart + inputRow, + globalRowStart / innerElementSize + inputCol${t?", batchIndices":""}); + `:` + mm_Asub[inputRow][inputCol] = mm_readA(batch, + globalRow + innerRow, + kStart / innerElementSize + inputCol${t?", batchIndices":""}); + `,df=(e,t)=>e?` + let ACached0 = mm_Asub[k * innerElementSize][localRow]; + let ACached1 = mm_Asub[k * innerElementSize + 1][localRow]; + let ACached2 = mm_Asub[k * innerElementSize + 2][localRow]; + ${t===3?"":"let ACached3 = mm_Asub[k * innerElementSize + 3][localRow];"} + for (var i = 0; i < rowPerThread; i = i + 1) { + acc[i] = BCached0 * ACached0[i] + acc[i]; + acc[i] = BCached1 * ACached1[i] + acc[i]; + acc[i] = BCached2 * ACached2[i] + acc[i]; + ${t===3?"":"acc[i] = BCached3 * ACached3[i] + acc[i];"} + }`:` + for (var i = 0; i < rowPerThread; i = i + 1) { + let ACached = mm_Asub[tileRow + i][k]; + acc[i] = BCached0 * ACached.x + acc[i]; + acc[i] = BCached1 * ACached.y + acc[i]; + acc[i] = BCached2 * ACached.z + acc[i]; + ${t===3?"":"acc[i] = BCached3 * ACached.w + acc[i];"} + }`,mo=(e,t,r="f32",n,o=!1,i=32,a=!1,u=32)=>{let d=t[1]*e[1],c=t[0]*e[0],p=o?d:i,m=o?i:d,f=p/t[0],b=i/t[1];if(!((o&&f===4&&e[1]===4||!o&&(f===3||f===4))&&p%t[0]===0&&i%t[1]===0&&e[0]===4))throw new Error(`If transposeA ${o} is true, innerElementSize ${f} and workPerThread[1] ${e[1]} must be 4. + Otherwise, innerElementSize ${f} must be 3 or 4. + tileAWidth ${p} must be divisible by workgroupSize[0]${t[0]}. tileInner ${i} must be divisible by workgroupSize[1] ${t[1]}. colPerThread ${e[0]} must be 4.`);return` +var mm_Asub: array, ${p/f}>, ${m}>; +var mm_Bsub: array, ${c/e[0]}>, ${i}>; + +const rowPerThread = ${e[1]}; +const colPerThread = ${e[0]}; +const innerElementSize = ${f}; +const tileInner = ${i}; + +@compute @workgroup_size(${t[0]}, ${t[1]}, ${t[2]}) +fn main(@builtin(local_invocation_id) localId : vec3, + @builtin(global_invocation_id) globalId : vec3, + @builtin(workgroup_id) workgroupId : vec3) { + let localRow = i32(localId.y); + let tileRow = localRow * rowPerThread; + let tileCol = i32(localId.x); + + let globalRow =i32(globalId.y) * rowPerThread; + let globalCol = i32(globalId.x); + let batch = ${a?"0":"i32(globalId.z)"}; + ${n?`let batchIndices = ${n.offsetToIndices("u32(batch)")};`:""} + let globalRowStart = i32(workgroupId.y) * ${d}; + + let num_tiles = ${a?`${Math.ceil(u/i)}`:"(uniforms.dim_inner - 1) / tileInner + 1"}; + var kStart = ${a?`i32(globalId.z) * ${u}`:"0"}; + + var acc: array, rowPerThread>; + + // Loop over shared dimension. + let tileRowB = localRow * ${b}; + for (var t = 0; t < num_tiles; t = t + 1) { + // Load one tile of A into local memory. + for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { + let inputRow = tileRow + innerRow; + let inputCol = tileCol; + ${uf(o,n)} + } + + // Load one tile of B into local memory. + for (var innerRow = 0; innerRow < ${b}; innerRow = innerRow + 1) { + let inputRow = tileRowB + innerRow; + let inputCol = tileCol; + mm_Bsub[inputRow][inputCol] = mm_readB(batch, kStart + inputRow, globalCol${n?", batchIndices":""}); + } + kStart = kStart + tileInner; + workgroupBarrier(); + + // Compute acc values for a single thread. + for (var k = 0; k < tileInner / innerElementSize; k = k + 1) { + let BCached0 = mm_Bsub[k * innerElementSize][tileCol]; + let BCached1 = mm_Bsub[k * innerElementSize + 1][tileCol]; + let BCached2 = mm_Bsub[k * innerElementSize + 2][tileCol]; + ${f===3?"":"let BCached3 = mm_Bsub[k * innerElementSize + 3][tileCol];"} + + ${df(o,f)} + } + + workgroupBarrier(); + } + + for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { + mm_write(batch, globalRow + innerRow, globalCol, acc[innerRow]); + } +}`},Ru=(e,t)=>e?` + mm_Asub[inputRow][inputCol] = mm_readA(batch, + kStart + inputRow, + globalRowStart + inputCol${t?", batchIndices":""}); + `:` + mm_Asub[inputRow][inputCol] = mm_readA(batch, + globalRowStart + inputRow, + kStart + inputCol${t?", batchIndices":""}); + `,lf=e=>e?"let ACached = mm_Asub[k][tileRow + innerRow];":"let ACached = mm_Asub[tileRow + innerRow][k];",fo=(e,t,r="f32",n,o=!1,i=32,a=!1,u=32,d=!1)=>{let c=e[1]*t[1],p=e[0]*t[0],m=o?c:i,f=o?i:c;if(!(f%t[1]===0&&m%t[0]===0&&i%t[1]===0))throw new Error(`tileAHight ${f} must be divisible by workgroupSize[1]${t[1]}, tileAWidth ${m} must be divisible by workgroupSize[0]${t[0]}, tileInner ${i} must be divisible by workgroupSize[1]${t[1]}`);let b=f/t[1],g=m/t[0],_=i/t[1],S=d?` + let localRow = i32(localId.y); + let localCol = i32(localId.x); + let globalRowStart = i32(workgroupId.y) * ${c}; + let globalColStart = i32(workgroupId.x) * ${p}; + + // Loop over shared dimension. + for (var t = 0; t < num_tiles; t = t + 1) { + // Load one tile of A into local memory. + for (var inputRow = localRow; inputRow < ${f}; inputRow = inputRow + ${t[1]}) { + for (var inputCol = localCol; inputCol < ${m}; inputCol = inputCol + ${t[0]}) { + ${Ru(o,n)} + } + } + // Load one tile of B into local memory. + for (var inputRow = localRow; inputRow < ${i}; inputRow = inputRow + ${t[1]}) { + for (var inputCol = localCol; inputCol < ${p}; inputCol = inputCol + ${t[0]}) { + mm_Bsub[inputRow][inputCol] = mm_readB(batch, + kStart + inputRow, + globalColStart + inputCol${n?", batchIndices":""}); + } + } + kStart = kStart + tileInner; + workgroupBarrier(); + + // Compute acc values for a single thread. + var BCached : array<${r}, colPerThread>; + for (var k = 0; k < tileInner; k = k + 1) { + for (var inner = 0; inner < colPerThread; inner = inner + 1) { + BCached[inner] = mm_Bsub[k][localCol + inner * ${t[0]}]; + } + for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { + let ACached = ${o?`mm_Asub[k][localRow + innerRow * ${t[1]}];`:`mm_Asub[localRow + innerRow * ${t[1]}][k];`} + for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) { + acc[innerRow][innerCol] = acc[innerRow][innerCol] + + ACached * BCached[innerCol]; + } + } + } + workgroupBarrier(); + } + for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { + let gRow = globalRowStart + localRow + innerRow * ${t[1]}; + for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) { + let gCol = globalColStart + localCol + innerCol * ${t[0]}; + mm_write(batch, gRow, gCol, acc[innerRow][innerCol]); + } + } + `:` +let tileRow = i32(localId.y) * rowPerThread; +let tileCol = i32(localId.x) * colPerThread; + +let globalRow = i32(globalId.y) * rowPerThread; +let globalCol = i32(globalId.x) * colPerThread; +let globalRowStart = i32(workgroupId.y) * ${c}; + +let tileRowA = i32(localId.y) * ${b}; +let tileColA = i32(localId.x) * ${g}; +let tileRowB = i32(localId.y) * ${_}; +// Loop over shared dimension. +for (var t = 0; t < num_tiles; t = t + 1) { + // Load one tile of A into local memory. + for (var innerRow = 0; innerRow < ${b}; innerRow = innerRow + 1) { + for (var innerCol = 0; innerCol < ${g}; innerCol = innerCol + 1) { + let inputRow = tileRowA + innerRow; + let inputCol = tileColA + innerCol; + ${Ru(o,n)} + } + } + + // Load one tile of B into local memory. + for (var innerRow = 0; innerRow < ${_}; innerRow = innerRow + 1) { + for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) { + let inputRow = tileRowB + innerRow; + let inputCol = tileCol + innerCol; + mm_Bsub[inputRow][inputCol] = mm_readB(batch, + kStart + inputRow, + globalCol + innerCol${n?", batchIndices":""}); + } + } + kStart = kStart + tileInner; + workgroupBarrier(); + + // Compute acc values for a single thread. + var BCached : array<${r}, colPerThread>; + for (var k = 0; k < tileInner; k = k + 1) { + for (var inner = 0; inner < colPerThread; inner = inner + 1) { + BCached[inner] = mm_Bsub[k][tileCol + inner]; + } + + for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { + ${lf(o)} + for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) { + acc[innerRow][innerCol] = acc[innerRow][innerCol] + ACached * BCached[innerCol]; + } + } + } + + workgroupBarrier(); +} + +for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { + for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) { + mm_write(batch, globalRow + innerRow, globalCol + innerCol, + acc[innerRow][innerCol]); + } +} +`;return` + var mm_Asub : array, ${f}>; + var mm_Bsub : array, ${i}>; + const rowPerThread = ${e[1]}; + const colPerThread = ${e[0]}; + const tileInner = ${i}; + +@compute @workgroup_size(${t[0]}, ${t[1]}, ${t[2]}) +fn main(@builtin(local_invocation_id) localId : vec3, + @builtin(global_invocation_id) globalId : vec3, + @builtin(workgroup_id) workgroupId : vec3) { + let batch = ${a?"0":"i32(globalId.z)"}; + ${n?`let batchIndices = ${n.offsetToIndices("u32(batch)")};`:""} + let num_tiles = ${a?`${Math.ceil(u/i)}`:"(uniforms.dim_inner - 1) / tileInner + 1"}; + var kStart = ${a?`i32(globalId.z) * ${u}`:"0"}; + + var acc : array, rowPerThread>; + ${S} + } +`},cf=(e,t,r,n,o=!1)=>{let[i,a,u,d]=n,c=be(n[0].type.tensor);return` + fn mm_readA(batch: i32, row: i32, colIn: i32, batchIndices: ${i.type.indices}) -> ${Ie(e,c)} { + var value = ${Ie(e,c)}(0.0); + let col = colIn * ${e}; + if(row < uniforms.dim_a_outer && col < uniforms.dim_inner) + { + var aIndices: ${a.type.indices}; + ${Xt("aIndices",a,a.rank-2,i.rank,"batchIndices")} + ${a.indicesSet("aIndices",a.rank-2,"u32(row)")} + ${a.indicesSet("aIndices",a.rank-1,"u32(colIn)")} + value = ${a.getByIndices("aIndices")}; + } + return value; + } + + fn mm_readB(batch: i32, row: i32, colIn: i32, batchIndices: ${i.type.indices}) -> ${Ie(e,c)} { + var value = ${Ie(e,c)}(0.0); + let col = colIn * ${e}; + if(row < uniforms.dim_inner && col < uniforms.dim_b_outer) + { + var bIndices: ${u.type.indices}; + ${Xt("bIndices",u,u.rank-2,i.rank,"batchIndices")} + ${u.indicesSet("bIndices",u.rank-2,"u32(row)")} + ${u.indicesSet("bIndices",u.rank-1,"u32(colIn)")} + value = ${u.getByIndices("bIndices")}; + } + return value; + } + + fn mm_write(batch: i32, row: i32, colIn: i32, valueIn: ${Ie(e,c)}) { + let col = colIn * ${e}; + if (row < uniforms.dim_a_outer && col < uniforms.dim_b_outer) { + var value = valueIn; + let coords = vec3(batch, row, colIn); + ${t?`value = value + ${o?"bias[colIn]":`${Ie(e,c)}(bias[row])`};`:""} + ${r} + ${d.setByIndices("vec3(coords)","value")} + } + } + `},Jt=(e,t,r,n,o=!1,i)=>{let a=e[0].dims,u=e[1].dims,d=a.slice(0,-2),c=u.slice(0,-2),p=n?n.slice(0,-2):r.slice(0,-2),m=k.size(p),f=a[a.length-2],b=a[a.length-1],g=u[u.length-1],_=b%4===0&&g%4===0,S=f<=8?[4,1,1]:[4,4,1],$=[8,8,1],v=[Math.ceil(g/$[0]/S[0]),Math.ceil(f/$[1]/S[1]),Math.ceil(m/$[2]/S[2])],x=_?4:1,T=[...d,f,b/x],E=T.length,I=[...c,b,g/x],z=I.length,O=[m,f,g/x],D=[{type:6,data:f},{type:6,data:g},{type:6,data:b}];qe(t,D),D.push(...N(p,T,I));let L=["rank","rank"],q=e.length>2;q&&(D.push(...N(e[2].dims)),L.push("rank")),D.push(...N(O));let Q=W=>{let Z=p.length,we=Lr("batchDims",e[0].dataType,Z,1),H=be(e[0].dataType),j=P("a",e[0].dataType,E,x),te=P("b",e[1].dataType,z,x),X=M("result",e[0].dataType,O.length,x),ue=[j,te];if(q){let V=o?x:1;ue.push(P("bias",e[2].dataType,e[2].dims.length,V))}let he=[{name:"dim_a_outer",type:"i32"},{name:"dim_b_outer",type:"i32"},{name:"dim_inner",type:"i32"}];je(t,he);let ye=be(X.type.tensor),re=Fe(t,X.type.value,ye),C=cf(x,q,re,[we,j,te,X],o);return` + ${W.registerUniforms(he).registerInternalVariables(we).declareVariables(...ue,X)} + ${C} + ${_?mo(S,$,H,we):fo(S,$,H,we)} + `};return{name:"MatMul",shaderCache:{hint:`${S};${t.activation};${_};${o}`,inputDependencies:L},getRunData:()=>({outputs:[{dims:i?i(r):r,dataType:e[0].dataType}],dispatchGroup:{x:v[0],y:v[1],z:v[2]},programUniforms:D}),getShaderSource:Q}}});var pf,Uu,Nu=U(()=>{"use strict";ee();Xe();ie();bt();Zr();Mu();Xr();pf=(e,t,r,n,o=!1,i,a=4,u=4,d=4,c="f32")=>{let p=L=>{switch(L){case 1:return"resData = x[xIndex];";case 3:return`resData = vec3<${c}>(x[xIndex], x[xIndex + 1], x[xIndex + 2]);`;case 4:return"resData = x[xIndex / 4];";default:throw new Error(`innerElementSize ${L} is not supported.`)}},m=L=>{switch(L){case 1:return"return w[row * i32(uniforms.w_shape[3]) + colIn];";case 4:return"return w[row * i32(uniforms.w_shape[3]) / 4 + colIn];";default:throw new Error(`innerElementSize ${L} is not supported.`)}},f=e?` + let coord = vec4(batch, xRow, xCol, xCh); + `:` + let coord = vec4(batch, xCh, xRow, xCol); + `,b=e?` + let coords = vec4( + batch, + row / outWidth, + row % outWidth, + col); + `:` + let coords = vec4( + batch, + row, + col / outWidth, + col % outWidth); + `,g=e?"i32(uniforms.x_shape[1])":"i32(uniforms.x_shape[2])",_=e?"i32(uniforms.x_shape[2])":"i32(uniforms.x_shape[3])",S=e?"row":"col",$=e?"col":"row",v=` + let inChannels = i32(uniforms.w_shape[2]); + let outWidth = ${e?"i32(uniforms.result_shape[2])":"i32(uniforms.result_shape[3])"}; + let outRow = ${S} / outWidth; + let outCol = ${S} % outWidth; + + let WRow = ${$} / (i32(uniforms.w_shape[1]) * inChannels); + let WCol = ${$} / inChannels % i32(uniforms.w_shape[1]); + let xRow = outRow * uniforms.stride[0] + uniforms.dilation[0] * WRow - uniforms.pad[0]; + let xCol = outCol * uniforms.stride[1] + uniforms.dilation[1] * WCol - uniforms.pad[1]; + let xCh = ${$} % inChannels; + var resData = ${Ie(a,c)}(0.0); + // The bounds checking is always needed since we use it to pad zero for + // the 'same' padding type. + if (xRow >= 0 && xRow < ${g} && xCol >= 0 && xCol < ${_}) { + ${f} + let xIndex = getIndexFromCoords4D(coord, vec4(uniforms.x_shape)); + ${p(a)} + } + return resData;`,x=e?t&&n?` + let col = colIn * ${a}; + ${v}`:` + let col = colIn * ${a}; + if (row < uniforms.dim_a_outer && col < uniforms.dim_inner) { + ${v} + } + return ${Ie(a,c)}(0.0);`:n&&r?` + let col = colIn * ${a}; + ${v}`:` + let col = colIn * ${a}; + if (row < uniforms.dim_inner && col < uniforms.dim_b_outer) { + ${v} + } + return ${Ie(a,c)}(0.0);`,T=e?n&&r?m(u):` + let col = colIn * ${u}; + if (row < uniforms.dim_inner && col < uniforms.dim_b_outer) { + ${m(u)} + } + return ${Ie(u,c)}(0.0);`:` + let col = colIn * ${u}; + if (row < uniforms.dim_inner && col < uniforms.dim_a_outer) { + ${m(u)} + } + return ${Ie(u,c)}(0.0);`,E=Ie(d,c),I=e?Ie(a,c):Ie(u,c),z=e?Ie(u,c):Ie(a,c),O=Fe(i,E,c);return` + fn mm_readA(batch: i32, row : i32, colIn : i32) -> ${I} { + ${e?x:T} + } + + fn mm_readB(batch: i32, row : i32, colIn : i32) -> ${z} { + ${e?T:x} + } + + fn mm_write(batch: i32, row : i32, colIn : i32, valueIn : ${E}) { + let col = colIn * ${d}; + if (row < uniforms.dim_a_outer && col < uniforms.dim_b_outer) + { + var value = valueIn; + let outWidth = ${e?"i32(uniforms.result_shape[2])":"i32(uniforms.result_shape[3])"}; + ${b} + ${Bu(o)} + ${O} + setOutputAtCoords(coords[0], coords[1], coords[2], coords[3], value); + } + }`},Uu=(e,t,r,n,o,i,a,u,d)=>{let c=t.format==="NHWC",p=c?e[0].dims[3]:e[0].dims[1],m=r[0],f=c?r[2]:r[3],b=c?r[1]:r[2],g=c?r[3]:r[1],_=c&&(p%4===0||p%3===0)&&g%4===0,S=c?g:f*b,$=c?f*b:g,v=[8,8,1],x=n<=8?[4,1,1]:[4,4,1],T=[Math.ceil(S/v[0]/x[0]),Math.ceil($/v[1]/x[1]),Math.ceil(m/v[2]/x[2])];se("verbose",()=>`[conv2d_mm_webgpu] dispatch = ${T}`);let E=_?c&&p%4!==0?3:4:1,I=v[1]*x[1],z=v[0]*x[0],O=Math.max(v[0]*E,v[1]),D=n%I===0,L=o%z===0,q=i%O===0,Q=_?[E,4,4]:[1,1,1],W=[{type:6,data:n},{type:6,data:o},{type:6,data:i},{type:6,data:[t.pads[0],t.pads[1]]},{type:6,data:t.strides},{type:6,data:t.dilations}];qe(t,W),W.push(...N(e[0].dims,e[1].dims));let Z=["rank","rank"];a&&(W.push(...N(e[2].dims)),Z.push("rank")),W.push(...N(r));let we=H=>{let j=[{name:"dim_a_outer",type:"i32"},{name:"dim_b_outer",type:"i32"},{name:"dim_inner",type:"i32"},{name:"pad",type:"i32",length:2},{name:"stride",type:"i32",length:2},{name:"dilation",type:"i32",length:2}];je(t,j);let te=_?4:1,X=be(e[0].dataType),ue=` + fn setOutputAtIndex(flatIndex : i32, value : ${_?`vec4<${X}>`:X}) { + result[flatIndex] = ${_?`vec4<${X}>`:X}(value); + } + fn setOutputAtCoords(d0 : i32, d1 : i32, d2 : i32, d3 : i32, value : ${_?`vec4<${X}>`:X}) { + let flatIndex = getOutputIndexFromCoords(vec4(d0, d1, d2, d3)); + setOutputAtIndex(flatIndex ${_?"/ 4":""}, value); + }`,he=P("x",e[0].dataType,e[0].dims.length,E===3?1:E),ye=P("w",e[1].dataType,e[1].dims.length,te),re=[he,ye],C=M("result",e[0].dataType,r.length,te);if(a){let V=P("bias",e[2].dataType,e[2].dims.length,te);re.push(V),ue+=` + fn getBiasByOutputCoords(coords : vec4) -> ${_?`vec4<${X}>`:X} { + return bias[coords.${c?"w":"y"}${_?"/ 4":""}]; + }`}return` + ${Du("uniforms.result_strides")} + //struct Uniforms { xShape : vec4, wShape : vec4, outShape : vec4, + // outShapeStrides: vec3, filterDims : vec2, pad : vec2, stride : vec2, + // dilation : vec2, dimAOuter : i32, dimBOuter : i32, dimInner : i32 }; + ${H.registerUniforms(j).declareVariables(...re,C)} + ${ue} + ${pf(c,D,L,q,a,t,Q[0],Q[1],Q[2],X)} + ${_?mo(x,v,X,void 0,!c,O):fo(x,v,X,void 0,!c,O,!1,void 0,u)}`};return{name:"Conv2DMatMul",shaderCache:{hint:`${t.cacheKey};${E};${_};${D};${L};${q};${I};${z};${O}`,inputDependencies:Z},getRunData:()=>({outputs:[{dims:d?d(r):r,dataType:e[0].dataType}],dispatchGroup:{x:T[0],y:T[1],z:T[2]},programUniforms:W}),getShaderSource:we}}});var mf,Vu,Jr,ff,Wu,hf,Lu,Gu,Hu=U(()=>{"use strict";ee();Xe();ne();ie();bt();Zr();mf=e=>{let t=1;for(let r=0;rtypeof e=="number"?[e,e,e]:e,Jr=(e,t)=>t<=1?e:e+(e-1)*(t-1),ff=(e,t,r,n=1)=>{let o=Jr(t,n);return Math.floor((e[0]*(r-1)-r+o)/2)},Wu=(e,t,r,n,o)=>{o==null&&(o=ff(e,t[0],n[0]));let i=[0,0,0,r];for(let a=0;a<3;a++)e[a]+2*o>=t[a]&&(i[a]=Math.trunc((e[a]-t[a]+2*o)/n[a]+1));return i},hf=(e,t,r,n,o,i,a,u,d,c)=>{let p,m,f,b;if(e==="VALID"&&(e=0),typeof e=="number"){p={top:e,bottom:e,left:e,right:e,front:e,back:e};let g=Wu([t,r,n,1],[u,d,c],1,[o,i,a],e);m=g[0],f=g[1],b=g[2]}else if(Array.isArray(e)){if(!e.every((_,S,$)=>_===$[0]))throw Error(`Unsupported padding parameter: ${e}`);p={top:e[0],bottom:e[1],left:e[2],right:e[3],front:e[4],back:e[5]};let g=Wu([t,r,n,1],[u,d,c],1,[o,i,a],e[0]);m=g[0],f=g[1],b=g[2]}else if(e==="SAME_UPPER"){m=Math.ceil(t/o),f=Math.ceil(r/i),b=Math.ceil(n/a);let g=(m-1)*o+u-t,_=(f-1)*i+d-r,S=(b-1)*a+c-n,$=Math.floor(g/2),v=g-$,x=Math.floor(_/2),T=_-x,E=Math.floor(S/2),I=S-E;p={top:x,bottom:T,left:E,right:I,front:$,back:v}}else throw Error(`Unknown padding parameter: ${e}`);return{padInfo:p,outDepth:m,outHeight:f,outWidth:b}},Lu=(e,t,r,n,o,i=!1,a="channelsLast")=>{let u,d,c,p,m;if(a==="channelsLast")[u,d,c,p,m]=e;else if(a==="channelsFirst")[u,m,d,c,p]=e;else throw new Error(`Unknown dataFormat ${a}`);let[f,,b,g,_]=t,[S,$,v]=Vu(r),[x,T,E]=Vu(n),I=Jr(b,x),z=Jr(g,T),O=Jr(_,E),{padInfo:D,outDepth:L,outHeight:q,outWidth:Q}=hf(o,d,c,p,S,$,v,I,z,O),W=i?f*m:f,Z=[0,0,0,0,0];return a==="channelsFirst"?Z=[u,W,L,q,Q]:a==="channelsLast"&&(Z=[u,L,q,Q,W]),{batchSize:u,dataFormat:a,inDepth:d,inHeight:c,inWidth:p,inChannels:m,outDepth:L,outHeight:q,outWidth:Q,outChannels:W,padInfo:D,strideDepth:S,strideHeight:$,strideWidth:v,filterDepth:b,filterHeight:g,filterWidth:_,effectiveFilterDepth:I,effectiveFilterHeight:z,effectiveFilterWidth:O,dilationDepth:x,dilationHeight:T,dilationWidth:E,inShape:e,outShape:Z,filterShape:t}},Gu=(e,t,r,n,o,i)=>{let a=i==="channelsLast",u=a?e[0].dims[3]:e[0].dims[1],d=!1,c=[64,1,1],p={x:r.map((v,x)=>x)},m=[Math.ceil(mf(p.x.map(v=>r[v]))/c[0]),1,1];se("verbose",()=>`[conv3d_naive_webgpu] dispatch = ${m}`);let f=d?a&&u%4!==0?3:4:1,b=k.size(r),g=[{type:12,data:b},{type:12,data:n},{type:12,data:o},{type:12,data:t.strides},{type:12,data:t.dilations}];qe(t,g),g.push(...N(e[0].dims,e[1].dims));let _=["rank","rank"],S=e.length===3;S&&(g.push(...N(e[2].dims)),_.push("rank")),g.push(...N(r));let $=v=>{let x=[{name:"output_size",type:"u32"},{name:"filter_dims",type:"u32",length:n.length},{name:"pads",type:"u32",length:o.length},{name:"strides",type:"u32",length:t.strides.length},{name:"dilations",type:"u32",length:t.dilations.length}];je(t,x);let T=d?4:1,E=be(e[0].dataType),I=P("x",e[0].dataType,e[0].dims.length,f===3?1:f),z=P("W",e[1].dataType,e[1].dims.length,T),O=[I,z],D=M("result",e[0].dataType,r.length,T),L="";if(S){let W=P("bias",e[2].dataType,e[2].dims.length,T);O.push(W),L+=` + fn getBiasByOutputCoords(coords : array) -> ${d?`vec4<${E}>`:E} { + return bias[${a?F("coords",4,5):F("coords",1,5)}${d?"/ 4":""}]; + }`}let q=Ie(f,E),Q=Fe(t,q,E);return` + ${L} + fn getX(d0 : u32, d1 : u32, d2 : u32, d3 : u32, d4 : u32) -> f32 { + let aIndices = array(d0, d1, d2, d3, d4); + return ${I.getByIndices("aIndices")}; + } + fn getW(d0 : u32, d1 : u32, d2 : u32, d3 : u32, d4 : u32) -> f32 { + let aIndices = array(d0, d1, d2, d3, d4); + return ${z.getByIndices("aIndices")}; + } + ${v.registerUniforms(x).declareVariables(...O,D)} + ${v.mainStart()} + ${v.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + let coords = ${D.offsetToIndices("global_idx")}; + let batch = ${F("coords",0,I.rank)}; + let d2 = ${a?F("coords",I.rank-1,I.rank):F("coords",1,I.rank)}; + let xFRCCorner = vec3(${a?F("coords",1,I.rank):F("coords",2,I.rank)}, + ${a?F("coords",2,I.rank):F("coords",3,I.rank)}, + ${a?F("coords",3,I.rank):F("coords",4,I.rank)}) * uniforms.strides - uniforms.pads; + let xFCorner = xFRCCorner.x; + let xRCorner = xFRCCorner.y; + let xCCorner = xFRCCorner.z; + let xShapeY = ${a?F("uniforms.x_shape",1,I.rank):F("uniforms.x_shape",2,I.rank)}; + let xShapeZ = ${a?F("uniforms.x_shape",2,I.rank):F("uniforms.x_shape",3,I.rank)}; + let xShapeW = ${a?F("uniforms.x_shape",3,I.rank):F("uniforms.x_shape",4,I.rank)}; + let xShapeU = ${a?F("uniforms.x_shape",4,I.rank):F("uniforms.x_shape",1,I.rank)}; + let inputDepthNearestVec4 = (xShapeU / 4) * 4; + let inputDepthVec4Remainder = xShapeU % 4; + + var value = 0.0; + for (var wF = 0u; wF < uniforms.filter_dims[0]; wF++) { + let xF = xFCorner + wF * uniforms.dilations[0]; + if (xF < 0 || xF >= xShapeY) { + continue; + } + + for (var wR = 0u; wR < uniforms.filter_dims[1]; wR++) { + let xR = xRCorner + wR * uniforms.dilations[1]; + if (xR < 0 || xR >= xShapeZ) { + continue; + } + + for (var wC = 0u; wC < uniforms.filter_dims[2]; wC++) { + let xC = xCCorner + wC * uniforms.dilations[2]; + if (xC < 0 || xC >= xShapeW) { + continue; + } + + for (var d1 = 0u; d1 < inputDepthNearestVec4; d1 += 4) { + ${a?`let xValues = vec4( + getX(batch, xF, xR, xC, d1), + getX(batch, xF, xR, xC, d1 + 1), + getX(batch, xF, xR, xC, d1 + 2), + getX(batch, xF, xR, xC, d1 + 3)); + `:`let xValues = vec4( + getX(batch, d1, xF, xR, xC), + getX(batch, d1 + 1, xF, xR, xC), + getX(batch, d1 + 2, xF, xR, xC), + getX(batch, d1 + 3, xF, xR, xC)); + `} + let wValues = vec4( + getW(d2, d1, wF, wR, wC), + getW(d2, d1 + 1, wF, wR, wC), + getW(d2, d1 + 2, wF, wR, wC), + getW(d2, d1 + 3, wF, wR, wC)); + value += dot(xValues, wValues); + } + if (inputDepthVec4Remainder == 1) { + ${a?`value += getX(batch, xF, xR, xC, inputDepthNearestVec4) + * getW(d2, inputDepthNearestVec4, wF, wR, wC);`:`value += getX(batch, inputDepthNearestVec4, xF, xR, xC) + * getW(d2, inputDepthNearestVec4, wF, wR, wC);`} + } else if (inputDepthVec4Remainder == 2) { + ${a?`let xValues = vec2( + getX(batch, xF, xR, xC, inputDepthNearestVec4), + getX(batch, xF, xR, xC, inputDepthNearestVec4 + 1)); + `:`let xValues = vec2( + getX(batch, inputDepthNearestVec4, xF, xR, xC), + getX(batch, inputDepthNearestVec4 + 1, xF, xR, xC)); + `} + let wValues = vec2( + getW(d2, inputDepthNearestVec4, wF, wR, wC), + getW(d2, inputDepthNearestVec4 + 1, wF, wR, wC)); + value += dot(xValues, wValues); + } else if (inputDepthVec4Remainder == 3) { + ${a?`let xValues = vec3( + getX(batch, xF, xR, xC, inputDepthNearestVec4), + getX(batch, xF, xR, xC, inputDepthNearestVec4 + 1), + getX(batch, xF, xR, xC, inputDepthNearestVec4 + 2)); + `:`let xValues = vec3( + getX(batch, inputDepthNearestVec4, xF, xR, xC), + getX(batch, inputDepthNearestVec4 + 1, xF, xR, xC), + getX(batch, inputDepthNearestVec4 + 2, xF, xR, xC)); + `} + let wValues = vec3( + getW(d2, inputDepthNearestVec4, wF, wR, wC), + getW(d2, inputDepthNearestVec4 + 1, wF, wR, wC), + getW(d2, inputDepthNearestVec4 + 2, wF, wR, wC)); + value += dot(xValues, wValues); + } + } + } + } + ${S?"value = value + getBiasByOutputCoords(coords)":""}; + ${Q} + result[global_idx] = f32(value); + }`};return{name:"Conv3DNaive",shaderCache:{hint:`${t.cacheKey};${a};${f};${S}`,inputDependencies:_},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:m[0],y:m[1],z:m[2]},programUniforms:g}),getShaderSource:$}}});var Fu,qu,ju=U(()=>{"use strict";ee();ne();ie();bt();Fu=(e,t,r,n)=>{let o=e.length>2,i=o?"value += b[output_channel];":"",a=e[0].dims,u=e[1].dims,d=t.format==="NHWC",c=d?r[3]:r[1],p=c/t.group,m=d&&p>=4?ce(c):1,f=k.size(r)/m,b=[{type:12,data:f},{type:12,data:t.dilations},{type:12,data:[t.strides[0],t.strides[1]]},{type:12,data:[t.pads[0],t.pads[1]]},{type:12,data:p}];qe(t,b),b.push(...N(a,[u[0],u[1],u[2],u[3]/m]));let g=o?["rank","rank","rank"]:["rank","rank"];b.push(...N([r[0],r[1],r[2],r[3]/m]));let _=S=>{let $=M("output",e[0].dataType,r.length,m),v=be($.type.tensor),x=Fe(t,$.type.value,v),T=P("x",e[0].dataType,a.length),E=P("w",e[1].dataType,u.length,m),I=[T,E];o&&I.push(P("b",e[2].dataType,e[2].dims,m));let z=[{name:"output_size",type:"u32"},{name:"dilations",type:"u32",length:t.dilations.length},{name:"strides",type:"u32",length:2},{name:"pads",type:"u32",length:2},{name:"output_channels_per_group",type:"u32"}];je(t,z);let O=d?` + for (var wHeight: u32 = 0u; wHeight < uniforms.w_shape[0]; wHeight++) { + let xHeight = xRCCorner.x + wHeight * uniforms.dilations[0]; + + if (xHeight < 0u || xHeight >= uniforms.x_shape[1]) { + continue; + } + + for (var wWidth: u32 = 0u; wWidth < uniforms.w_shape[1]; wWidth++) { + let xWidth = xRCCorner.y + wWidth * uniforms.dilations[1]; + if (xWidth < 0u || xWidth >= uniforms.x_shape[2]) { + continue; + } + + for (var wInChannel: u32 = 0u; wInChannel < uniforms.w_shape[2]; wInChannel++) { + let input_channel = in_channel_offset + wInChannel; + let xVal = ${T.get("batch","xHeight","xWidth","input_channel")}; + let wVal = ${E.get("wHeight","wWidth","wInChannel","output_channel")}; + value += xVal * wVal; + } + } + } + `:` + for (var wInChannel: u32 = 0u; wInChannel < uniforms.w_shape[1]; wInChannel++) { + let input_channel = in_channel_offset + wInChannel; + for (var wHeight: u32 = 0u; wHeight < uniforms.w_shape[2]; wHeight++) { + let xHeight = xRCCorner.x + wHeight * uniforms.dilations[0]; + + if (xHeight < 0u || xHeight >= uniforms.x_shape[2]) { + continue; + } + + for (var wWidth: u32 = 0u; wWidth < uniforms.w_shape[3]; wWidth++) { + let xWidth = xRCCorner.y + wWidth * uniforms.dilations[1]; + if (xWidth < 0u || xWidth >= uniforms.x_shape[3]) { + continue; + } + + let xVal = ${T.get("batch","input_channel","xHeight","xWidth")}; + let wVal = ${E.get("output_channel","wInChannel","wHeight","wWidth")}; + value += xVal * wVal; + } + } + } + `;return` + ${S.registerUniforms(z).declareVariables(...I,$)} + + ${S.mainStart()} + ${S.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + + let outputIndices = ${$.offsetToIndices("global_idx")}; + let batch: u32 = outputIndices[0]; + let output_channel: u32 = outputIndices[${d?3:1}]; + let xRCCorner: vec2 = vec2(outputIndices[${d?1:2}], outputIndices[${d?2:3}]) * uniforms.strides - uniforms.pads; + let group_id: u32 = output_channel * ${m} / uniforms.output_channels_per_group; + var in_channel_offset = group_id * uniforms.w_shape[${d?2:1}]; + + var value: ${$.type.value} = ${$.type.value}(0); + ${O} + ${i} + ${x} + ${$.setByOffset("global_idx","value")} + }`};return{name:"GroupedConv",shaderCache:{hint:`${t.cacheKey}_${m}`,inputDependencies:g},getRunData:()=>({outputs:[{dims:n?n(r):r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(f/64)},programUniforms:b}),getShaderSource:_}},qu=(e,t,r,n)=>{let o=e.length>2,i=ce(r[3]),a=ce(r[2]),u=k.size(r)/i/a,d=[e[0].dims[0],e[0].dims[1],e[0].dims[2],e[0].dims[3]/i],c=[e[1].dims[0],e[1].dims[1],e[1].dims[2],e[1].dims[3]/i],p=[r[0],r[1],r[2],r[3]/i],m=[{type:12,data:u},{type:6,data:[t.strides[0],t.strides[1]]},{type:6,data:[t.pads[0],t.pads[1]]}];qe(t,m),m.push(...N(d,c,p));let f=(a-1)*t.strides[1]+c[1],b=g=>{let _=M("output",e[0].dataType,p.length,i),S=be(_.type.tensor),$=Fe(t,_.type.value,S),v=P("x",e[0].dataType,d.length,i),x=P("w",e[1].dataType,c.length,i),T=[v,x];o&&T.push(P("b",e[2].dataType,e[2].dims,i));let E=o?"value += b[output_channel];":"",I=[{name:"output_size",type:"u32"},{name:"strides",type:"i32",length:2},{name:"pads",type:"i32",length:2}];return je(t,I),` + ${g.registerUniforms(I).declareVariables(...T,_)} + ${g.mainStart()} + ${g.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + let width0 = uniforms.output_shape[3]; + let output_channel = global_idx % width0; + var index1 = global_idx / width0; + let width1 = uniforms.output_shape[2] / ${a}u; + let col = (index1 % width1) * ${a}u; + index1 = index1 / width1; + let row = index1 % uniforms.output_shape[1]; + let batch = index1 / uniforms.output_shape[1]; + + let x_corner = vec2(i32(row), i32(col)) * uniforms.strides - uniforms.pads; + + var x_vals: array<${v.type.value}, ${f}>; + var values: array<${_.type.value}, ${a}>; + let input_channel = output_channel; + // Use constant instead of uniform can give better performance for w's height/width. + for (var w_height: u32 = 0u; w_height < ${c[0]}; w_height++) { + let x_height = x_corner.x + i32(w_height); + if (x_height >= 0 && u32(x_height) < uniforms.x_shape[1]) { + for (var i = 0; i < ${f}; i++) { + let x_width = x_corner.y + i; + if (x_width >= 0 && u32(x_width) < uniforms.x_shape[2]) { + x_vals[i] = ${v.get("batch","u32(x_height)","u32(x_width)","input_channel")}; + } else { + x_vals[i] = ${v.type.value}(0); + } + } + for (var w_width: u32 = 0u; w_width < ${c[1]}; w_width++) { + let w_val = ${x.get("w_height","w_width","0","output_channel")}; + for (var i = 0u; i < ${a}u; i++) { + values[i] = fma(x_vals[i * u32(uniforms.strides[1]) + w_width], w_val, values[i]); + } + } + } + } + + for (var i = 0u; i < ${a}u; i++) { + var value = values[i]; + ${E} + ${$} + ${_.set("batch","row","col + i","output_channel","value")}; + } + }`};return{name:"GroupedConv-Vectorize",shaderCache:{hint:`${t.cacheKey};${i};${a};${f};${c[0]};${c[1]}`,inputDependencies:o?["rank","rank","type"]:["rank","rank"]},getRunData:()=>({outputs:[{dims:n?n(r):r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(u/64)},programUniforms:m}),getShaderSource:b}}});var gf,ho,bf,go,bo,Ku,yf,_f,yo,Zu=U(()=>{"use strict";ne();Nu();Hu();Xr();ju();bt();Yr();st();gf=(e,t,r,n,o,i)=>{let a=e[0],u=e.slice(i?1:2,i?3:4),d=u.length,c=t[0],m=t.slice(2).map((g,_)=>g+(g-1)*(r[_]-1)),b=u.map((g,_)=>g+n[_]+n[_+d]).map((g,_)=>Math.floor((g-m[_]+o[_])/o[_]));return b.splice(0,0,a),b.splice(i?3:1,0,c),b},ho=[2,3,1,0],bf=(e,t)=>{if(!e||e.length!==2&&e.length!==3)throw new Error("Conv requires 2 or 3 inputs");if(e[0].dims.length>5)throw new Error("greater than 5D is not supported");if(e[0].dims.length!==e[1].dims.length)throw new Error("filter does not have same dimension as input");let r=e[0].dims[t.format==="NHWC"?e[0].dims.length-1:1],n=e[1].dims[1]*t.group;if(r!==n)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");if(e.length===3&&(e[2].dims.length!==1||e[1].dims[0]!==e[2].dims[0]))throw new Error("invalid bias");let o=e[0].dims.length-2;if(t.dilations.length!==o)throw new Error(`dilations should be ${o}D`);if(t.strides.length!==o)throw new Error(`strides should be ${o}D`);if(t.pads.length!==o*2)throw new Error(`pads should be ${o*2}D`);if(t.kernelShape.length!==0&&t.kernelShape.length!==e[1].dims.length-2)throw new Error("invalid kernel shape")},go=(e,t)=>{let r=e.kernelShape.slice();r.length{let t=Kr(e),r=e.format,n=["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][e.auto_pad],o=e.dilations,i=e.group,a=e.kernel_shape,u=e.pads,d=e.strides,c=e.w_is_const();return{autoPad:n,format:r,dilations:o,group:i,kernelShape:a,pads:u,strides:d,wIsConst:c,...t,cacheKey:`${e.format};${t.activation};`}},Ku=(e,t,r,n)=>{let o=r.format==="NHWC",i=gf(t[0].dims,t[1].dims,r.dilations,r.pads,r.strides,o);if(r.group!==1){let I=[t[0]];if(o){let O=e.kernelCustomData.wT??e.compute(Ee(t[1],ho),{inputs:[1],outputs:[r.wIsConst?-2:-1]})[0];r.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=O),I.push(O)}else I.push(t[1]);t.length===3&&I.push(t[2]),!e.adapterInfo.isArchitecture("ampere")&&o&&t[1].dims[0]===r.group&&t[1].dims[1]===1&&r.dilations[0]===1&&r.dilations[1]===1?e.compute(qu(I,r,i,n),{inputs:I}):e.compute(Fu(I,r,i,n),{inputs:I});return}let a=t.length===3,u=t[0].dims[o?1:2],d=t[0].dims[o?2:3],c=t[0].dims[o?3:1],p=t[1].dims[2],m=t[1].dims[3],f=i[o?1:2],b=i[o?2:3],g=i[o?3:1],_=o&&p===u&&m===d&&r.pads[0]===0&&r.pads[1]===0;if(_||p===1&&m===1&&r.dilations[0]===1&&r.dilations[1]===1&&r.strides[0]===1&&r.strides[1]===1&&r.pads[0]===0&&r.pads[1]===0){let I=i[0],z,O,D,L=[];if(o){let W=e.kernelCustomData.wT??e.compute(Ee(t[1],ho),{inputs:[1],outputs:[r.wIsConst?-2:-1]})[0];if(r.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=W),_){let Z=u*d*c;z=t[0].reshape([1,I,Z]),O=W.reshape([1,Z,g]),D=[1,I,g]}else z=t[0].reshape([I,u*d,c]),O=W.reshape([1,c,g]),D=[I,f*b,g];L.push(z),L.push(O)}else z=t[0].reshape([I,c,u*d]),O=t[1].reshape([1,g,c]),D=[I,g,f*b],L.push(O),L.push(z);a&&L.push(t[2]);let q=D[2],Q=L[0].dims[L[0].dims.length-1];q<8&&Q<8?e.compute(Qr(L,r,i,D,o,n),{inputs:L}):e.compute(Jt(L,r,i,D,o,n),{inputs:L});return}let S=!0,$=e.kernelCustomData.wT??e.compute(Ee(t[1],ho),{inputs:[1],outputs:[r.wIsConst?-2:-1]})[0];r.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=$);let v=[t[0],$];a&&v.push(t[2]);let x=o?f*b:g,T=o?g:f*b,E=p*m*c;e.compute(Uu(v,r,i,x,T,E,a,S,n),{inputs:v})},yf=(e,t)=>{let r=t.format==="NHWC",n=[e.inputs[0].reshape(r?[e.inputs[0].dims[0],1,e.inputs[0].dims[1],e.inputs[0].dims[2]]:[e.inputs[0].dims[0],e.inputs[0].dims[1],1,e.inputs[0].dims[2]]),e.inputs[1].reshape([e.inputs[1].dims[0],e.inputs[1].dims[1],1,e.inputs[1].dims[2]])];e.inputs.length===3&&n.push(e.inputs[2]);let o=[0,t.pads[0],0,t.pads[1]],i=[1].concat(t.strides),a=[1].concat(t.dilations),u=[1].concat(t.kernelShape),d=go({...t,pads:o,strides:i,dilations:a,kernelShape:u},n);Ku(e,n,d,c=>r?[c[0],c[2],c[3]]:[c[0],c[1],c[3]])},_f=(e,t,r)=>{let n=r.format==="NHWC"?"channelsLast":"channelsFirst",o=go(r,t),i=r.autoPad==="NOTSET"?r.pads:r.autoPad,a=Lu(t[0].dims,t[1].dims,r.strides,r.dilations,i,!1,n);e.compute(Gu(t,o,a.outShape,[a.filterDepth,a.filterHeight,a.filterWidth],[a.padInfo.front,a.padInfo.top,a.padInfo.left],n))},yo=(e,t)=>{if(bf(e.inputs,t),e.inputs[0].dims.length===3)yf(e,t);else if(e.inputs[0].dims.length===5)_f(e,e.inputs,t);else{let r=go(t,e.inputs);Ku(e,e.inputs,r)}}});var Qu,Yu=U(()=>{"use strict";ee();Xe();ne();ie();Qu=(e,t,r)=>{let n=e.length>2,o=t.outputShape,i=t.format==="NHWC",a=t.group,u=e[1].dims,d=u[2]/a,c=u[3],p=i?ce(d):1,m=i&&c===1&&d>=4,f=m?Math.floor(d/4)*4:Math.floor(d/p)*p,b=d-f,g=i?ce(c):1,_=i?c===1?p:g:1,S=k.size(o)/g,$=[Math.ceil(S/64),1,1];se("verbose",()=>`[conv2d_backprop_webgpu] dispatch = ${$}`);let v=["rank","rank"],x=[t.strides[0],t.strides[1]],T=[t.kernelShape[i?1:2],t.kernelShape[i?2:3]],E=[t.dilations[0],t.dilations[1]],I=[T[0]+(t.dilations[0]<=1?0:(t.kernelShape[i?1:2]-1)*(t.dilations[0]-1)),T[1]+(t.dilations[1]<=1?0:(t.kernelShape[i?2:3]-1)*(t.dilations[1]-1))],z=[I[0]-1-Math.floor((t.pads[0]+t.pads[2])/2),I[1]-1-Math.floor((t.pads[1]+t.pads[3])/2)],O=[{type:12,data:S},{type:12,data:x},{type:12,data:T},{type:12,data:E},{type:12,data:I},{type:6,data:z},{type:12,data:f},{type:12,data:d},{type:12,data:c},...N(e[0].dims,e[1].dims)];n&&(O.push(...N(e[2].dims)),v.push("rank")),O.push(...N(o));let D=L=>{let q=[{name:"output_size",type:"u32"},{name:"strides",type:"u32",length:x.length},{name:"filter_dims",type:"u32",length:T.length},{name:"dilations",type:"u32",length:T.length},{name:"effective_filter_dims",type:"u32",length:I.length},{name:"pads",type:"i32",length:z.length},{name:"input_channels_per_group_int",type:"u32"},{name:"input_channels_per_group",type:"u32"},{name:"output_channels_per_group",type:"u32"}],Q=be(e[0].dataType),W=i?1:2,Z=i?2:3,we=i?3:1,H=P("W",e[1].dataType,e[1].dims.length,_),j=P("Dy",e[0].dataType,e[0].dims.length,p),te=[j,H];n&&te.push(P("bias",e[2].dataType,[o[we]].length,g));let X=M("result",e[0].dataType,o.length,g),ue=()=>{let re="";if(m)p===4?re+=` + let xValue = ${j.getByOffset("x_offset")}; + let wValue = ${H.getByOffset("w_offset")}; + dotProd = dotProd + dot(xValue, wValue); + x_offset += 1u; + w_offset += 1u;`:p===2?re+=` + dotProd = dotProd + dot(vec4<${Q}>(${j.getByOffset("x_offset")}, ${j.getByOffset("x_offset + 1u")}), vec4<${Q}>(${H.getByOffset("w_offset")}, ${H.getByOffset("w_offset + 1u")})); + x_offset += 2u; + w_offset += 2u;`:p===1&&(re+=` + dotProd = dotProd + dot(vec4<${Q}>(${j.getByOffset("x_offset")}, ${j.getByOffset("x_offset + 1u")}, ${j.getByOffset("x_offset + 2u")}, ${j.getByOffset("x_offset + 3u")}), vec4<${Q}>(${H.getByOffset("w_offset")}, ${H.getByOffset("w_offset + 1u")}, ${H.getByOffset("w_offset + 2u")}, ${H.getByOffset("w_offset + 3u")})); + x_offset += 4u; + w_offset += 4u;`);else if(re+=` + let xValue = ${i?j.getByOffset(`${j.indicesToOffset(`${j.type.indices}(batch, idyR, idyC, inputChannel)`)} / ${p}`):j.get("batch","inputChannel","idyR","idyC")}; + `,p===1)re+=` + let w_offset = ${H.indicesToOffset(`${H.type.indices}(u32(wRPerm), u32(wCPerm), inputChannel, wOutChannel)`)}; + let wValue = ${H.getByOffset(`w_offset / ${_}`)}; + dotProd = dotProd + xValue * wValue;`;else for(let C=0;C{if(b===0)return"";if(!m)throw new Error(`packInputAs4 ${m} is not true.`);let re="";if(p===1){re+="dotProd = dotProd";for(let C=0;C(i32(r), i32(c)) - uniforms.pads; + let dyRCorner = dyCorner.x; + let dyCCorner = dyCorner.y; + let groupId = d1 / uniforms.output_channels_per_group; + let wOutChannel = d1 - groupId * uniforms.output_channels_per_group; + // Convolve dy(?, ?, d2) with w(:, :, d1, d2) to compute dx(xR, xC, d1). + // ? = to be determined. : = across all values in that axis. + var dotProd = ${X.type.value}(0.0); + var wR: u32 = 0; + if (uniforms.dilations.x == 1) { + // Minimum wR >= 0 that satisfies (dyRCorner + wR) % (uniforms.strides.x) == 0 + wR = u32(((dyRCorner + i32(uniforms.strides.x) - 1) / i32(uniforms.strides.x)) * i32(uniforms.strides.x) - dyRCorner); + } + for (; wR < uniforms.effective_filter_dims.x; wR = wR + 1) { + if (wR % uniforms.dilations.x != 0) { + continue; + } + let dyR = (${Q}(dyRCorner) + ${Q}(wR)) / ${Q}(uniforms.strides[0]); + let wRPerm = uniforms.filter_dims.x - 1 - wR / uniforms.dilations.x; + if (dyR < 0.0 || dyR >= ${Q}(uniforms.Dy_shape[${W}]) || fract(dyR) > 0.0 || + wRPerm < 0) { + continue; + } + let idyR: u32 = u32(dyR); + var wC: u32 = 0; + if (uniforms.dilations.y == 1) { + // Minimum wC >= 0 that satisfies (dyCCorner + wC) % (uniforms.strides.y) == 0 + wC = u32(((dyCCorner + i32(uniforms.strides.y) - 1) / i32(uniforms.strides.y)) * i32(uniforms.strides.y) - dyCCorner); + } + for (; wC < uniforms.effective_filter_dims.y; wC = wC + 1) { + if (wC % uniforms.dilations.y != 0) { + continue; + } + let dyC = (${Q}(dyCCorner) + ${Q}(wC)) / ${Q}(uniforms.strides.y); + let wCPerm = uniforms.filter_dims.y - 1 - wC / uniforms.dilations.y; + if (dyC < 0.0 || dyC >= ${Q}(uniforms.Dy_shape[${Z}]) || + fract(dyC) > 0.0 || wCPerm < 0) { + continue; + } + let idyC: u32 = u32(dyC); + var inputChannel = groupId * uniforms.input_channels_per_group; + ${m?` + var x_offset = ${j.indicesToOffset(`${j.type.indices}(batch, idyR, idyC, inputChannel)`)} / ${p}; + var w_offset = ${H.indicesToOffset(`${H.type.indices}(wRPerm, wCPerm, inputChannel, wOutChannel)`)} / ${_}; + `:""} + for (var d2: u32 = 0; d2 < uniforms.input_channels_per_group_int; d2 = d2 + ${m?4:p}) { + ${ue()} + inputChannel = inputChannel + ${m?4:p}; + } + ${he()} + wC = wC + uniforms.strides.y - 1; + } + wR = wR + uniforms.strides[0] - 1; + } + let value = dotProd${n?` + bias[d1 / ${g}]`:""}; + ${X.setByOffset("global_idx","value")}; + `;return` + ${L.registerUniforms(q).declareVariables(...te,X)} + ${L.mainStart()} + ${L.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}; + ${ye}}`};return{name:"ConvTranspose2D",shaderCache:{hint:`${t.cacheKey};${p}${_}${g}${m}${b}`,inputDependencies:v},getRunData:()=>({dispatchGroup:{x:$[0],y:$[1],z:$[2]},outputs:[{dims:r?r(o):o,dataType:e[0].dataType}],programUniforms:O}),getShaderSource:D}}});var wf,vf,$f,Xu,Ju,xf,ed,Sf,td,rd=U(()=>{"use strict";Yu();bt();st();wf=(e,t,r,n,o,i)=>(e-1)*t+r+(n-1)*o+1-i,vf=(e,t,r,n,o)=>{let i=Math.floor(e/2);t==="SAME_UPPER"?(r[n]=i,r[o]=e-i):t==="SAME_LOWER"&&(r[n]=e-i,r[o]=i)},$f=(e,t,r,n,o,i,a,u,d,c)=>{let p=e.length-2,m=c.length===0;d.length{let r=e.kernelShape.slice();if(e.kernelShape.length===0||e.kernelShape.reduce((m,f)=>m*f,1)===0){r.length=0;for(let m=2;mm+f,0)===0){let m=t[0].dims.length-2;d=new Array(m).fill(1)}let c=e.strides.slice();if(c.reduce((m,f)=>m+f,0)===0){let m=t[0].dims.length-2;c=new Array(m).fill(1)}$f(u,r,d,e.autoPad,e.group,o,c,n,a,i);let p=Object.assign({},e);return Object.assign(p,{kernelShape:r,pads:o,outputPadding:a,outputShape:i,dilations:d,strides:c}),p},Ju=e=>{let t=Kr(e),r=e.format,n=["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][typeof e.autoPad>"u"?0:e.autoPad],o=e.dilations,i=e.group,a=e.kernelShape,u=e.pads,d=e.strides,c=e.wIsConst(),p=e.outputPadding,m=e.outputShape;return{autoPad:n,format:r,dilations:o,group:i,kernelShape:a,outputPadding:p,outputShape:m,pads:u,strides:d,wIsConst:c,...t,cacheKey:`${e.format};${t.activation};`}},xf=(e,t)=>{if(!e||e.length!==2&&e.length!==3)throw new Error("Conv requires 2 or 3 inputs");if(e[0].dims.length!==4&&e[0].dims.length!==3)throw new Error("currently only support 2-dimensional conv");if(e[0].dims.length!==e[1].dims.length)throw new Error("filter does not have same dimension as input");let r=e[0].dims[t.format==="NHWC"?e[0].dims.length-1:1],n=e[1].dims[0];if(r!==n)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");let o=e[1].dims[1]*t.group;if(e.length===3&&(e[2].dims.length!==1||e[2].dims[0]!==o))throw new Error("invalid bias");let i=e[0].dims.length-2;if(t.dilations.reduce((p,m)=>p+m,0)>0&&t.dilations.length!==i)throw new Error(`dilations should be ${i}D`);if(t.strides.reduce((p,m)=>p+m,0)>0&&t.strides.length!==i)throw new Error(`strides should be ${i}D`);if(t.pads.reduce((p,m)=>p+m,0)>0&&t.pads.length!==i*2)throw new Error(`pads should be ${i*2}D`);if(t.outputPadding.length!==i&&t.outputPadding.length!==0)throw new Error(`output_padding should be ${i}D`);if(t.kernelShape.reduce((p,m)=>p+m,0)>0&&t.kernelShape.length!==0&&t.kernelShape.length!==e[1].dims.length-2)throw new Error("invalid kernel shape");if(t.outputShape.length!==0&&t.outputShape.length!==e[0].dims.length-2)throw new Error("invalid output shape")},ed=(e,t,r,n)=>{let o=e.kernelCustomData.wT??e.compute(Ee(t[1],[2,3,0,1]),{inputs:[1],outputs:[r.wIsConst?-2:-1]})[0];r.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=o);let i=[t[0],o];t.length===3&&i.push(t[2]),e.compute(Qu(i,r,n),{inputs:i})},Sf=(e,t)=>{let r=t.format==="NHWC",n=[e.inputs[0].reshape(r?[e.inputs[0].dims[0],1,e.inputs[0].dims[1],e.inputs[0].dims[2]]:[e.inputs[0].dims[0],e.inputs[0].dims[1],1,e.inputs[0].dims[2]]),e.inputs[1].reshape([e.inputs[1].dims[0],e.inputs[1].dims[1],1,e.inputs[1].dims[2]])];e.inputs.length===3&&n.push(e.inputs[2]);let o=t.kernelShape;(o.length===0||o[0]===0)&&(o=[e.inputs[1].dims[2]]);let i=t.dilations;(i.length===0||i[0]===0)&&(i=[1]);let a=t.strides;(a.length===0||a[0]===0)&&(a=[1]);let u=t.pads;u.length===0&&(u=[0,0]),u=[0,u[0],0,u[1]],a=[1].concat(a),i=[1].concat(i),o=[1].concat(o);let d=t.outputPadding;d=[0].concat(d);let c=Xu({...t,pads:u,strides:a,dilations:i,kernelShape:o,outputPadding:d},n);ed(e,n,c,p=>r?[p[0],p[2],p[3]]:[p[0],p[1],p[3]])},td=(e,t)=>{if(xf(e.inputs,t),e.inputs[0].dims.length===3)Sf(e,t);else{let r=Xu(t,e.inputs);ed(e,e.inputs,r)}}});var Tf,nd,od,id=U(()=>{"use strict";ee();ne();Se();ie();Tf=(e,t,r,n)=>{let o=k.size(t),i=t.length,a=P("input",e,i),u=M("output",e,i),d=r.dataType===6?r.getInt32Array()[0]:Number(r.getBigInt64Array()[0]),c=k.normalizeAxis(d,i),p=m=>{let f=` i32(${a.indicesGet("inputIndices","uniforms.axis")}) `,b=F("uniforms.input_shape","uniforms.axis",i),g=n.reverse?f+(n.exclusive?" + 1":""):"0",_=n.reverse?b:f+(n.exclusive?"":" + 1");return` + ${m.registerUniform("outputSize","u32").registerUniform("axis","u32").declareVariables(a,u)} + ${m.mainStart()} + ${m.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + var inputIndices = ${u.offsetToIndices("global_idx")}; + var sum = ${u.type.value}(0); + let first : i32 = ${g}; + let last : i32 = ${_}; + for (var i : i32 = first; i < last; i++) { + ${a.indicesSet("inputIndices","uniforms.axis","u32(i)")}; + sum = sum + ${a.getByIndices("inputIndices")}; + } + ${u.setByOffset("global_idx","sum")}; + }`};return{name:"CumSum",shaderCache:{hint:n.cacheKey,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:t,dataType:e}],dispatchGroup:{x:Math.ceil(o/64)},programUniforms:[{type:12,data:o},{type:12,data:c},...N(t,t)]}),getShaderSource:p}},nd=(e,t)=>{let r=e.inputs[0].dims,n=e.inputs[0].dataType,o=e.inputs[1];e.compute(Tf(n,r,o,t),{inputs:[0]})},od=e=>{let t=e.exclusive===1,r=e.reverse===1;return J({exclusive:t,reverse:r})}});var If,Cf,Af,ad,sd,ud=U(()=>{"use strict";ee();ne();Se();ie();If=e=>{if(!e||e.length!==1)throw new Error("DepthToSpace requires 1 input.");if(e[0].dims.length!==4)throw new Error("DepthToSpace requires 4D input.")},Cf=(e,t,r,n)=>{let o=[];o.push(`fn perm(i: ${n.type.indices}) -> ${r.type.indices} { + var a: ${r.type.indices};`);for(let i=0;i{let r,n,o,i,a,u,d=t.format==="NHWC",c=t.blocksize,p=t.mode==="DCR";d?([r,n,o,i]=e.dims,a=p?[r,n,o,c,c,i/c**2]:[r,n,o,i/c**2,c,c],u=p?[0,1,3,2,4,5]:[0,1,4,2,5,3]):([r,n,o,i]=[e.dims[0],e.dims[2],e.dims[3],e.dims[1]],a=p?[r,c,c,i/c**2,n,o]:[r,i/c**2,c,c,n,o],u=p?[0,3,4,1,5,2]:[0,1,4,2,5,3]);let m=e.reshape(a),f=m.dims.length,b=e.dataType,g=P("a",b,f),_=M("output",b,f),S=$=>` + ${$.registerUniform("output_size","u32").declareVariables(g,_)} + + ${Cf(u,f,g,_)} + + ${$.mainStart()} + ${$.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + + let indices = ${_.offsetToIndices("global_idx")}; + let aIndices = perm(indices); + + ${_.setByOffset("global_idx",g.getByIndices("aIndices"))} + }`;return{name:"DepthToSpace",shaderCache:{hint:`${e.dims};${t.blocksize};${t.mode}`,inputDependencies:["rank"]},getRunData:$=>{let v=d?[r,n*c,o*c,i/c**2]:[r,i/c**2,n*c,o*c],x=k.size(v),T=m.dims,E=k.sortBasedOnPerm(T,u);return{outputs:[{dims:v,dataType:$[0].dataType}],dispatchGroup:{x:Math.ceil(x/64)},programUniforms:[{type:12,data:x},...N(T,E)]}},getShaderSource:S}},ad=(e,t)=>{If(e.inputs),e.compute(Af(e.inputs[0],t))},sd=e=>J({blocksize:e.blocksize,mode:e.mode,format:e.format})});var _o,en,dd,Ef,kf,wo,vo,ld,Pf,cd,pd,md=U(()=>{"use strict";ee();ne();Se();ie();_o="[a-zA-Z]|\\.\\.\\.",en="("+_o+")+",dd="^"+en+"$",Ef="("+en+",)*"+en,kf="^"+Ef+"$",wo=class{constructor(t=-1){this.symbolToIndices=new Map,this.inputIndex=t}addSymbol(t,r){let n=this.symbolToIndices.get(t);n===void 0?n=[r]:n.push(r),this.symbolToIndices.set(t,n)}},vo=class{constructor(t,r){this.equation=r;this.hasEllipsis=!1,this.symbolToInfo=new Map,this.lhs=new Array,this.outputDims=[];let[n,o]=r.includes("->")?r.split("->",2):[r,""];if(!n.match(RegExp(kf)))throw new Error("Invalid LHS term");if(n.split(",").forEach((u,d)=>{let c=t[d].dims.slice();if(!u.match(RegExp(dd)))throw new Error("Invalid LHS term");let p=this.processTerm(u,!0,c,d);this.lhs.push(p)}),o==="")o+=[...this.symbolToInfo.entries()].filter(([u,d])=>d.count===1||u==="...").map(([u])=>u).join("");else if(!o.match(RegExp(en)))throw new Error("Invalid RHS");o.match(RegExp(_o,"g"))?.forEach(u=>{if(u==="...")this.outputDims=this.outputDims.concat(this.ellipsisDims);else{let d=this.symbolToInfo.get(u);if(d===void 0)throw new Error("Invalid RHS symbol");this.outputDims.push(d.dimValue)}}),this.rhs=this.processTerm(o,!1,this.outputDims)}addSymbol(t,r,n){let o=this.symbolToInfo.get(t);if(o!==void 0){if(o.dimValue!==r&&o.count!==1)throw new Error("Dimension mismatch");o.count++,o.inputIndices.push(n)}else o={count:1,dimValue:r,inputIndices:[n]};this.symbolToInfo.set(t,o)}processTerm(t,r,n,o=-1){let i=n.length,a=!1,u=[],d=0;if(!t.match(RegExp(dd))&&!r&&t!=="")throw new Error("Invalid LHS term");let c=t.match(RegExp(_o,"g")),p=new wo(o);return c?.forEach((m,f)=>{if(m==="..."){if(a)throw new Error("Only one ellipsis is allowed per input term");a=!0;let b=i-c.length+1;if(b<0)throw new Error("Ellipsis out of bounds");if(u=n.slice(d,d+b),this.hasEllipsis){if(this.ellipsisDims.length!==u.length||this.ellipsisDims.toString()!==u.toString())throw new Error("Ellipsis dimensions mismatch")}else if(r)this.hasEllipsis=!0,this.ellipsisDims=u;else throw new Error("Ellipsis must be specified in the LHS");for(let g=0;ge+"_max",Pf=(e,t,r,n)=>{let i=e.map(p=>p.length).map((p,m)=>P(`input${m}`,t,p)),a=k.size(n),u=M("output",t,n.length),d=[...r.symbolToInfo.keys()].filter(p=>!r.rhs.symbolToIndices.has(p)),c=p=>{let m=[],f="var prod = 1.0;",b="var sum = 0.0;",g="sum += prod;",_=[],S=[],$=[],v=[],x=r.symbolToInfo.size===r.rhs.symbolToIndices.size;r.symbolToInfo.forEach((E,I)=>{if(r.rhs.symbolToIndices.has(I)){let z=r.rhs.symbolToIndices.get(I)?.[0];z!==void 0&&r.lhs.forEach((O,D)=>{if(E.inputIndices.includes(D)){let L=O.symbolToIndices.get(I);if(L===void 0)throw new Error("Invalid symbol error");L.forEach(q=>{m.push(`${i[D].indicesSet(`input${D}Indices`,q,u.indicesGet("outputIndices",z))}`)})}})}else r.lhs.forEach((z,O)=>{if(E.inputIndices.includes(O)){let D=z.symbolToIndices.get(I);if(D===void 0)throw new Error("Invalid symbol error");D.forEach(L=>{_.push(`${i[O].indicesSet(`input${O}Indices`,L,`${I}`)}`)}),v.push(`prod *= ${i[O].getByIndices(`input${O}Indices`)};`)}}),S.push(`for(var ${I}: u32 = 0; ${I} < uniforms.${ld(I)}; ${I}++) {`),$.push("}")});let T=x?[...m,`let sum = ${i.map((E,I)=>E.getByIndices(`input${I}Indices`)).join(" * ")};`]:[...m,b,...S,..._,f,...v,g,...$];return` + ${p.registerUniforms(d.map(E=>({name:`${ld(E)}`,type:"u32"}))).registerUniform("outputSize","u32").declareVariables(...i,u)} + + ${p.mainStart()} + ${p.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + var outputIndices = ${u.offsetToIndices("global_idx")}; + ${i.map((E,I)=>`var input${I}Indices: ${i[I].type.indices};`).join(` +`)} + ${T.join(` +`)}; + ${u.setByOffset("global_idx","sum")}; + }`};return{name:"Einsum",shaderCache:{hint:r.equation,inputDependencies:e.map(()=>"rank")},getRunData:()=>{let p=d.filter(f=>r.symbolToInfo.has(f)).map(f=>({type:12,data:r.symbolToInfo.get(f)?.dimValue||0}));p.push({type:12,data:a});let m=e.map((f,b)=>[...N(f)]).reduce((f,b)=>f.concat(b),p);return m.push(...N(n)),{outputs:[{dims:n,dataType:t}],dispatchGroup:{x:Math.ceil(a/64)},programUniforms:m}},getShaderSource:c}},cd=(e,t)=>{let r=new vo(e.inputs,t.equation),n=r.outputDims,o=e.inputs.map((i,a)=>i.dims);e.compute(Pf(o,e.inputs[0].dataType,r,n))},pd=e=>{let t=e.equation.replace(/\s+/g,"");return J({equation:t})}});var zf,fd,Of,Bf,hd,gd=U(()=>{"use strict";ee();ne();ie();zf=e=>{if(!e||e.length!==2)throw new Error("Expand requires 2 input.");let t=e[0].dims,r=Array.from(e[1].getBigInt64Array(),Number),n=r.length{let r=e.length-t.length,n=[];for(let o=0;oe.length>t.length?fd(e,t):fd(t,e),Bf=e=>{let t=e[0].dims,r=Array.from(e[1].getBigInt64Array(),Number),n=Of(t,r),o=e[0].dataType,i=o===9||k.size(t)===1,a=o===9||t.length>0&&t[t.length-1]%4===0?4:1,u=i||n.length>0&&n[n.length-1]%4===0?4:1,d=Math.ceil(k.size(n)/u),c=m=>{let f=P("input",o,t.length,a),b=M("output",o,n.length,u),g;if(o===9){let _=(S,$,v="")=>` + let outputIndices${$} = ${b.offsetToIndices(`outputOffset + ${$}u`)}; + let offset${$} = ${f.broadcastedIndicesToOffset(`outputIndices${$}`,b)}; + let index${$} = offset${$} / 4u; + let component${$} = offset${$} % 4u; + ${S}[${$}] = ${v}(${f.getByOffset(`index${$}`)}[component${$}]); + `;g=` + let outputOffset = global_idx * ${u}; + var data = vec4(0); + ${_("data",0,"u32")} + ${_("data",1,"u32")} + ${_("data",2,"u32")} + ${_("data",3,"u32")} + ${b.setByOffset("global_idx","data")} + }`}else g=` + let outputIndices = ${b.offsetToIndices(`global_idx * ${u}`)}; + let inputOffset = ${f.broadcastedIndicesToOffset("outputIndices",b)}; + let data = ${b.type.value}(${f.getByOffset(`inputOffset / ${a}`)}); + ${b.setByOffset("global_idx","data")} + }`;return` + ${m.registerUniform("vec_size","u32").declareVariables(f,b)} + ${m.mainStart()} + ${m.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")} + ${g}`},p=[{type:12,data:d},...N(t,n)];return{name:"Expand",shaderCache:{hint:`${n.length};${a}${u}`,inputDependencies:["rank"]},getShaderSource:c,getRunData:()=>({outputs:[{dims:n,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(d/64)},programUniforms:p})}},hd=e=>{zf(e.inputs),e.compute(Bf(e.inputs),{inputs:[0]})}});var Df,bd,yd=U(()=>{"use strict";ee();ne();ie();jr();Df=e=>{let t=e[0].dataType,r=k.size(e[0].dims),n=k.size(e[1].dims),o=n%4===0,i=a=>{let u=P("x",t,[1],4),d=P("bias",t,[1],4),c=M("y",t,[1],4),p=[{name:"output_vec_size",type:"u32"},{name:"bias_size",type:"u32"}],m=b=>` + let bias${b}_offset: u32 = (global_idx * 4 + ${b}) % uniforms.bias_size; + let bias${b} = ${d.getByOffset(`bias${b}_offset / 4`)}[bias${b}_offset % 4];`,f=o?` + let bias = ${d.getByOffset("global_idx % (uniforms.bias_size / 4)")};`:`${m(0)}${m(1)}${m(2)}${m(3)} + let bias = ${u.type.value}(bias0, bias1, bias2, bias3);`;return`${a.registerUniforms(p).declareVariables(u,d,c)} + + ${co(Ae(t))} + + ${a.mainStart(It)} + ${a.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_vec_size")} + + let x = ${u.getByOffset("global_idx")}; + ${f} + let x_in = x + bias; + ${c.setByOffset("global_idx",po("x_in"))} + }`};return{name:"FastGeluWithBias",shaderCache:{hint:`${o}`,inputDependencies:["type","type"]},getShaderSource:i,getRunData:a=>({outputs:[{dims:a[0].dims,dataType:a[0].dataType}],programUniforms:[{type:12,data:Math.ceil(r/4)},{type:12,data:n}],dispatchGroup:{x:Math.ceil(r/It/4)}})}},bd=e=>{e.inputs.length<2||k.size(e.inputs[1].dims)===0?mu(e):e.compute(Df(e.inputs))}});var Mf,Rf,_d,wd,vd=U(()=>{"use strict";ee();ne();Se();ie();Mf=e=>{if(!e||e.length!==2)throw new Error("Gather requires 2 inputs.")},Rf=(e,t)=>{let r=e[0].dims,n=e[1].dims,o=r.length,i=k.normalizeAxis(t.axis,o),a=r.slice(0);a.splice(i,1,...n);let u=r[i],d=e[0].dataType===9?4:1,c=Math.ceil(k.size(a)/d),p=[{type:12,data:c},{type:6,data:u},{type:12,data:i},...N(e[0].dims,e[1].dims,a)],m=f=>{let b=P("data",e[0].dataType,e[0].dims.length,d),g=P("inputIndices",e[1].dataType,e[1].dims.length),_=M("output",e[0].dataType,a.length,d),S=v=>{let x=n.length,T=`var indicesIndices${v} = ${g.type.indices}(0);`;for(let E=0;E1?`indicesIndices${v}[${E}]`:`indicesIndices${v}`} = ${a.length>1?`outputIndices${v}[uniforms.axis + ${E}]`:`outputIndices${v}`};`;T+=` + var idx${v} = ${g.getByIndices(`indicesIndices${v}`)}; + if (idx${v} < 0) { + idx${v} = idx${v} + uniforms.axisDimLimit; + } + var dataIndices${v} : ${b.type.indices}; + `;for(let E=0,I=0;E1?`dataIndices${v}[${E}]`:`dataIndices${v}`} = u32(idx${v});`,I+=x):(T+=`${o>1?`dataIndices${v}[${E}]`:`dataIndices${v}`} = ${a.length>1?`outputIndices${v}[${I}]`:`outputIndices${v}`};`,I++);return T},$;if(e[0].dataType===9){let v=(x,T,E="")=>` + let outputIndices${T} = ${_.offsetToIndices(`outputOffset + ${T}u`)}; + ${S(T)}; + let offset${T} = ${b.indicesToOffset(`dataIndices${T}`)}; + let index${T} = offset${T} / 4u; + let component${T} = offset${T} % 4u; + ${x}[${T}] = ${E}(${b.getByOffset(`index${T}`)}[component${T}]); + `;$=` + let outputOffset = global_idx * ${d}; + var value = vec4(0); + ${v("value",0,"u32")} + ${v("value",1,"u32")} + ${v("value",2,"u32")} + ${v("value",3,"u32")} + ${_.setByOffset("global_idx","value")} + `}else $=` + let outputIndices = ${_.offsetToIndices("global_idx")}; + ${S("")}; + let value = ${b.getByIndices("dataIndices")}; + ${_.setByOffset("global_idx","value")}; + `;return` + ${f.registerUniform("outputSize","u32").registerUniform("axisDimLimit","i32").registerUniform("axis","u32").declareVariables(b,g,_)} + ${f.mainStart()} + ${f.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + ${$} + }`};return{name:"Gather",shaderCache:{hint:t.cacheKey,inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:a,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(c/64)},programUniforms:p}),getShaderSource:m}},_d=e=>J({axis:e.axis}),wd=(e,t)=>{let r=e.inputs;Mf(r),e.compute(Rf(e.inputs,t))}});var Uf,$d,xd,Sd=U(()=>{"use strict";ee();ne();ie();Uf=(e,t,r,n,o,i,a,u,d)=>{let c=[{type:12,data:i},{type:12,data:n},{type:12,data:o},{type:12,data:r},{type:12,data:a},{type:12,data:u},{type:12,data:d}],p=[i];c.push(...N(t.dims,p));let m=f=>{let b=P("indices_data",t.dataType,t.dims.length),g=M("input_slice_offsets_data",12,1,1),_=[b,g],S=[{name:"output_size",type:"u32"},{name:"batch_dims",type:"u32"},{name:"input_dims",type:"u32",length:o.length},{name:"sizes_from_slice_dims_data",type:"u32",length:r.length},{name:"num_slices_per_batch",type:"u32"},{name:"input_batch_stride",type:"u32"},{name:"num_slice_dims",type:"u32"}];return` + ${f.registerUniforms(S).declareVariables(..._)} + ${f.mainStart()} + ${f.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + let batch_idx = global_idx / uniforms.num_slices_per_batch; + let base_offset = batch_idx * uniforms.input_batch_stride; + + let slice_indices_base_offset = global_idx * uniforms.num_slice_dims; + var relative_slice_offset = 0; + for (var dim_idx = 0u; dim_idx < uniforms.num_slice_dims; dim_idx ++) { + var index = i32(indices_data[dim_idx + slice_indices_base_offset].x); + let input_dim_idx = uniforms.batch_dims + dim_idx; + if (index < 0) { + ${o.length===1?"index += i32(uniforms.input_dims);":"index += i32(uniforms.input_dims[input_dim_idx]);"} + } + ${r.length===1?"relative_slice_offset += index * i32(uniforms.sizes_from_slice_dims_data);":"relative_slice_offset += index * i32(uniforms.sizes_from_slice_dims_data[dim_idx]);"} + } + + input_slice_offsets_data[global_idx] = base_offset + u32(relative_slice_offset); + }`};return e.compute({name:"computeSliceOffsets",shaderCache:{hint:`${o.length}_${r.length}`,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:p,dataType:e.inputs[1].dataType}],dispatchGroup:{x:Math.ceil(i/64)},programUniforms:c}),getShaderSource:m},{inputs:[t],outputs:[-1]})[0]},$d=(e,t)=>{let r=e.inputs,n=r[0].dims,o=r[0].dataType,i=r[1].dims,a=i[i.length-1],u=k.sizeToDimension(i,i.length-1),d=k.sizeFromDimension(n,t.batchDims+a),c=k.sizeToDimension(n,t.batchDims),p=k.sizeFromDimension(n,t.batchDims),m=u/c,f=new Array(a),b=d;for(let T=0;Tn.length)throw new Error("last dimension of indices must not be larger than rank of input tensor");let S=i.slice(0,-1).concat(n.slice(_)),$=k.size(S),v=[{type:12,data:$},{type:12,data:d},...N(r[0].dims,g.dims,S)],x=T=>{let E=P("data",r[0].dataType,r[0].dims.length),I=P("slice_offsets",12,g.dims.length),z=M("output",r[0].dataType,S.length);return` + ${T.registerUniform("output_size","u32").registerUniform("slice_size","u32").declareVariables(E,I,z)} + ${T.mainStart()} + ${T.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + let slice_offset = slice_offsets[global_idx / uniforms.slice_size]; + output[global_idx] = data[u32(slice_offset) + global_idx % uniforms.slice_size]; + }`};e.compute({name:"GatherND",shaderCache:{hint:t.cacheKey,inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:S,dataType:o}],dispatchGroup:{x:Math.ceil($/64)},programUniforms:v}),getShaderSource:x},{inputs:[r[0],g]})},xd=e=>({batchDims:e.batch_dims,cacheKey:""})});var Nf,Vf,Td,Id,Cd=U(()=>{"use strict";ee();ne();Se();ie();Nf=(e,t)=>{if(e.length<3||e.length>4)throw new Error("GatherBlockQuantized requires 3 or 4 inputs.");let r=k.normalizeAxis(t.quantizeAxis,e[0].dims.length),n=t.blockSize,o=e[0],i=e[2],a=e.length===4?e[3]:void 0;if(i.dims.length!==o.dims.length||!o.dims.map((u,d)=>d===r?Math.ceil(u/n)===i.dims[d]:u===i.dims[d]).reduce((u,d)=>u&&d,!0))throw new Error("Scales must have the same rank as the input tensor and the dims should match except on gatherAxis.");if(a){if(a.dataType!==o.dataType)throw new Error("Zero point must have the same data type as the input tensor.");if(a.dims.length!==i.dims.length||!a.dims.map((u,d)=>u===i.dims[d]).reduce((u,d)=>u&&d,!0))throw new Error("Zero point must have the same rank as the input tensor and the dims should match except on quantizeAxis.")}},Vf=(e,t)=>{let r=e[0].dims,n=e[1].dims,o=r.length,i=k.normalizeAxis(t.gatherAxis,o),a=k.normalizeAxis(t.quantizeAxis,o),u=r.slice(0);u.splice(i,1,...n);let d=k.size(u),c=e[2].dataType,m=e[0].dataType===22,f=[{type:12,data:d},{type:12,data:a},{type:12,data:i},{type:12,data:t.blockSize},...N(...e.map((g,_)=>g.dims),u)],b=g=>{let _=P("data",e[0].dataType,e[0].dims.length),S=P("inputIndices",e[1].dataType,e[1].dims.length),$=P("scales",e[2].dataType,e[2].dims.length),v=e.length>3?P("zeroPoint",e[3].dataType,e[3].dims.length):void 0,x=M("output",c,u.length),T=[_,S,$];v&&T.push(v);let E=[{name:"output_size",type:"u32"},{name:"quantize_axis",type:"u32"},{name:"gather_axis",type:"u32"},{name:"block_size",type:"u32"}];return` + ${g.registerUniforms(E).declareVariables(...T,x)} + ${g.mainStart()} + let output_indices = ${x.offsetToIndices("global_idx")}; + var indices_indices = ${S.type.indices}(0); + ${n.length>1?` + for (var i: u32 = 0; i < ${n.length}; i++) { + let index = ${x.indicesGet("output_indices","uniforms.gather_axis + i")}; + ${S.indicesSet("indices_indices","i","index")}; + }`:`indices_indices = ${x.indicesGet("output_indices","uniforms.gather_axis")};`}; + var data_indices = ${_.type.indices}(0); + for (var i: u32 = 0; i < uniforms.gather_axis; i++) { + let index = ${x.indicesGet("output_indices","i")}; + ${_.indicesSet("data_indices","i","index")}; + } + var index_from_indices = ${S.getByIndices("indices_indices")}; + if (index_from_indices < 0) { + index_from_indices += ${r[i]}; + } + ${_.indicesSet("data_indices","uniforms.gather_axis","u32(index_from_indices)")}; + for (var i = uniforms.gather_axis + 1; i < ${u.length}; i++) { + let index = ${x.indicesGet("output_indices",`i + ${n.length} - 1`)}; + ${_.indicesSet("data_indices","i","index")}; + } + let data_offset = ${_.indicesToOffset("data_indices")}; + let data_index = data_offset % 8; + // Convert 4-bit packed data to 8-bit packed data. + let packed_4bit_quantized_data = ${_.getByOffset("data_offset / 8")}; + let packed_8bit_quantized_data = (packed_4bit_quantized_data >> (4 * (data_index % 2))) & 0x0f0f0f0f; + let quantized_data_vec = ${m?"unpack4xI8":"unpack4xU8"}(u32(packed_8bit_quantized_data)); + let quantized_data = quantized_data_vec[data_index / 2]; + var scale_indices = data_indices; + let quantize_axis_index = ${$.indicesGet("data_indices","uniforms.quantize_axis")} / uniforms.block_size; + ${$.indicesSet("scale_indices","uniforms.quantize_axis","quantize_axis_index")}; + var scale = ${$.getByIndices("scale_indices")}; + ${v?` + let zero_point_indices = scale_indices; + let zero_point_offset = ${v.indicesToOffset("zero_point_indices")}; + let zero_point_index = zero_point_offset % 8; + let packed_4bit_zero_points = ${v.getByOffset("zero_point_offset / 8")}; + let packed_8bit_zero_points = (packed_4bit_zero_points >> (4 * (zero_point_index % 2))) & 0x0f0f0f0f; + let zero_point_vec = ${m?"unpack4xI8":"unpack4xU8"}(u32(packed_8bit_zero_points)); + let zero_point = zero_point_vec[zero_point_index / 2];`:"var zero_point = 0"}; + let dequantized_data = ${Ae(c)}(quantized_data - zero_point) * scale; + ${x.setByOffset("global_idx","dequantized_data")}; + }`};return{name:"GatherBlockQuantized",shaderCache:{hint:`${t.cacheKey};${e.filter((g,_)=>_!==1).map(g=>g.dims.join("_")).join(";")}`,inputDependencies:Array.from({length:e.length},(g,_)=>"rank")},getRunData:()=>({outputs:[{dims:u,dataType:c}],dispatchGroup:{x:Math.ceil(d/64)},programUniforms:f}),getShaderSource:b}},Td=(e,t)=>{let r=e.inputs;Nf(r,t),e.compute(Vf(e.inputs,t))},Id=e=>J({blockSize:e.blockSize,gatherAxis:e.gatherAxis,quantizeAxis:e.quantizeAxis})});var Wf,Lf,Ad,Ed,kd=U(()=>{"use strict";ee();ne();Se();ie();Wf=e=>{if(!e||e.length!==2)throw new Error("GatherElements requires 2 inputs.");if(e[0].dims.length<1)throw new Error("GatherElements requires that the data input be rank >= 1.");if(e[0].dims.length!==e[1].dims.length)throw new Error(`GatherElements requires that the data input and + indices input tensors be of same rank.`)},Lf=(e,t)=>{let r=e[0].dims,n=e[0].dataType,o=r.length,i=e[1].dims,a=e[1].dataType,u=k.normalizeAxis(t.axis,o),d=r[u],c=i.slice(0),p=k.size(c),m=P("input",n,o),f=P("indicesInput",a,i.length),b=M("output",n,c.length),g=[{type:12,data:p},{type:6,data:d},{type:12,data:u}];return g.push(...N(r,i,c)),{name:"GatherElements",shaderCache:{inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:c,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(p/64)},programUniforms:g}),getShaderSource:$=>` + ${$.registerUniform("outputSize","u32").registerUniform("axisDimLimit","i32").registerUniform("axis","u32").declareVariables(m,f,b)} + ${$.mainStart()} + ${$.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + + let outputIndices = ${b.offsetToIndices("global_idx")}; + + var idx = ${f.getByOffset("global_idx")}; + if (idx < 0) { + idx = idx + uniforms.axisDimLimit; + } + var inputIndices = ${m.type.indices}(outputIndices); + ${m.indicesSet("inputIndices","uniforms.axis","u32(idx)")}; + let value = ${m.getByIndices("inputIndices")}; + + ${b.setByOffset("global_idx","value")}; + }`}},Ad=e=>J({axis:e.axis}),Ed=(e,t)=>{let r=e.inputs;Wf(r),e.compute(Lf(e.inputs,t))}});var Gf,Hf,Pd,zd,Od=U(()=>{"use strict";ee();ne();ie();Gf=e=>{if(!e)throw new Error("Input is missing");if(e.length<2||e.length>3)throw new Error("Invaid input number.");if(e.length===3&&e[2].dims.length>2)throw new Error("Invalid input shape of C");if(e[0].dataType!==e[1].dataType||e.length===3&&e[0].dataType!==e[2].dataType)throw new Error("Input types are mismatched")},Hf=(e,t)=>{let r=e[0].dims.slice(),n=e[1].dims.slice(),[o,i,a]=Dr.getShapeOfGemmResult(r,t.transA,n,t.transB,e.length===3?e[2].dims:void 0),u=[o,i];if(!u)throw new Error("Can't use gemm on the given tensors");let d=16,c=Math.ceil(i/d),p=Math.ceil(o/d),m=!0,f=k.size(u),b=[{type:12,data:m?c:f},{type:12,data:o},{type:12,data:i},{type:12,data:a},{type:1,data:t.alpha},{type:1,data:t.beta}],g=["type","type"];e.length===3&&(b.push(...N(e[2].dims)),g.push("rank")),b.push(...N(u));let _=$=>{let v="";t.transA&&t.transB?v="value += a[k * uniforms.M + m] * b[n * uniforms.K + k];":t.transA&&!t.transB?v="value += a[k * uniforms.M + m] * b[k * uniforms.N + n];":!t.transA&&t.transB?v="value += a[m * uniforms.K + k] * b[n * uniforms.K + k];":!t.transA&&!t.transB&&(v="value += a[m * uniforms.K + k] * b[k * uniforms.N + n];");let x=t.alpha===1?"":"value *= uniforms.alpha;",T=P("a",e[0].dataType,e[0].dims),E=P("b",e[1].dataType,e[1].dims),I=T.type.value,z=null,O=[T,E];e.length===3&&(z=P("c",e[2].dataType,e[2].dims.length),O.push(z));let D=M("output",e[0].dataType,u.length);O.push(D);let L=[{name:"output_size",type:"u32"},{name:"M",type:"u32"},{name:"N",type:"u32"},{name:"K",type:"u32"},{name:"alpha",type:"f32"},{name:"beta",type:"f32"}];return` + ${$.registerUniforms(L).declareVariables(...O)} + + ${$.mainStart()} + ${$.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + + let m = global_idx / uniforms.N; + let n = global_idx % uniforms.N; + + var value = ${I}(0); + for (var k: u32 = 0u; k < uniforms.K; k++) { + ${v} + } + + ${x} + ${z!=null?`let cOffset = ${z.broadcastedIndicesToOffset("vec2(m, n)",D)}; value += ${I}(uniforms.beta) * ${z.getByOffset("cOffset")};`:""} + output[global_idx] = value; + }`},S=$=>{let v=P("a",e[0].dataType,e[0].dims),x=P("b",e[1].dataType,e[1].dims),T=null,E=[v,x];e.length===3&&(T=P("c",e[2].dataType,e[2].dims.length),E.push(T));let I=M("output",e[0].dataType,u.length);E.push(I);let z=[{name:"num_tile_n",type:"u32"},{name:"M",type:"u32"},{name:"N",type:"u32"},{name:"K",type:"u32"},{name:"alpha",type:"f32"},{name:"beta",type:"f32"}],O="",D="";t.transA&&t.transB?(D=` + var col = tile_row_start + local_id.x; + var row = k_start + local_id.y; + if (col < uniforms.M && row < uniforms.K) { + tile_a[local_id.y][local_id.x] = a[row * uniforms.M + col]; + } else { + tile_a[local_id.y][local_id.x] = ${v.type.value}(0); + } + + col = k_start + local_id.x; + row = tile_col_start + local_id.y; + if (col < uniforms.K && row < uniforms.N) { + tile_b[local_id.y][local_id.x] = b[row * uniforms.K + col]; + } else { + tile_b[local_id.y][local_id.x] = ${x.type.value}(0); + } + `,O="value += tile_a[k][local_id.y] * tile_b[local_id.x][k];"):t.transA&&!t.transB?(D=` + var col = tile_row_start + local_id.x; + var row = k_start + local_id.y; + if (col < uniforms.M && row < uniforms.K) { + tile_a[local_id.y][local_id.x] = a[row * uniforms.M + col]; + } else { + tile_a[local_id.y][local_id.x] = ${v.type.value}(0); + } + + col = tile_col_start + local_id.x; + row = k_start + local_id.y; + if (col < uniforms.N && row < uniforms.K) { + tile_b[local_id.y][local_id.x] = b[row * uniforms.N + col]; + } else { + tile_b[local_id.y][local_id.x] = ${x.type.value}(0); + } + `,O="value += tile_a[k][local_id.y] * tile_b[k][local_id.x];"):!t.transA&&t.transB?(D=` + var col = k_start + local_id.x; + var row = tile_row_start + local_id.y; + if (col < uniforms.K && row < uniforms.M) { + tile_a[local_id.y][local_id.x] = a[row * uniforms.K + col]; + } else { + tile_a[local_id.y][local_id.x] = ${v.type.value}(0); + } + + col = k_start + local_id.x; + row = tile_col_start + local_id.y; + if (col < uniforms.K && row < uniforms.N) { + tile_b[local_id.y][local_id.x] = b[row * uniforms.K + col]; + } else { + tile_b[local_id.y][local_id.x] = ${x.type.value}(0); + } + `,O="value += tile_a[local_id.y][k] * tile_b[local_id.x][k];"):!t.transA&&!t.transB&&(D=` + var col = k_start + local_id.x; + var row = tile_row_start + local_id.y; + if (col < uniforms.K && row < uniforms.M) { + tile_a[local_id.y][local_id.x] = a[row * uniforms.K + col]; + } else { + tile_a[local_id.y][local_id.x] = ${v.type.value}(0); + } + + col = tile_col_start + local_id.x; + row = k_start + local_id.y; + if (col < uniforms.N && row < uniforms.K) { + tile_b[local_id.y][local_id.x] = b[row * uniforms.N + col]; + } else { + tile_b[local_id.y][local_id.x] = ${x.type.value}(0); + } + `,O="value += tile_a[local_id.y][k] * tile_b[k][local_id.x];");let L=t.alpha===1?"":"value *= uniforms.alpha;";return` + ${$.registerUniforms(z).declareVariables(...E)} + var tile_a: array, ${d}>; + var tile_b: array, ${d}>; + ${$.mainStart([d,d,1])} + let tile_col_start = (workgroup_index % uniforms.num_tile_n) * ${d}; + let tile_row_start = (workgroup_index / uniforms.num_tile_n) * ${d}; + let num_tiles = (uniforms.K - 1) / ${d} + 1; + var k_start = 0u; + var value = ${I.type.value}(0); + for (var t: u32 = 0u; t < num_tiles; t++) { + ${D} + k_start = k_start + ${d}; + workgroupBarrier(); + + for (var k: u32 = 0u; k < ${d}; k++) { + ${O} + } + workgroupBarrier(); + } + + ${L} + let m = tile_row_start + local_id.y; + let n = tile_col_start + local_id.x; + ${T!=null?`let cOffset = ${T.broadcastedIndicesToOffset("vec2(m, n)",I)}; value += ${I.type.value}(uniforms.beta) * ${T.getByOffset("cOffset")};`:""} + if (m < uniforms.M && n < uniforms.N) { + output[m * uniforms.N + n] = value; + } + }`};return m?{name:"GemmShared",shaderCache:{hint:`${t.cacheKey}`,inputDependencies:g},getRunData:()=>({outputs:[{dims:u,dataType:e[0].dataType}],dispatchGroup:{x:c*p},programUniforms:b}),getShaderSource:S}:{name:"Gemm",shaderCache:{hint:`${t.cacheKey}`,inputDependencies:g},getRunData:()=>({outputs:[{dims:u,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(f/64)},programUniforms:b}),getShaderSource:_}},Pd=e=>{let t=e.transA,r=e.transB,n=e.alpha,o=e.beta;return{transA:t,transB:r,alpha:n,beta:o,cacheKey:`${e.transA};${e.transB};${e.alpha===1}`}},zd=(e,t)=>{Gf(e.inputs),e.compute(Hf(e.inputs,t))}});var ut,yt,Ut,Nt,Ff,qf,jf,Kf,Zf,Qf,Yf,Xf,Bd,Dd,Md=U(()=>{"use strict";ee();ne();Se();ie();[ut,yt,Ut,Nt]=[0,1,2,3],Ff=e=>{if(e[0].dims.length!==4)throw new Error("only 4-D tensor is supported.");if(e[0].dims.length!==e[1].dims.length)throw new Error("input dimensions must be equal to grid dimensions");if(e[0].dims.length-2!==e[1].dims[e[1].dims.length-1])throw new Error(`last dimension of grid must be equal to ${e[0].dims.length-2}`);if(e[0].dims[0]!==e[1].dims[0])throw new Error("grid batch size must match input batch size")},qf=` + fn gs_get_cubic_coeffs(x: f32) -> vec4 { + let cubic_alpha = -0.75f; + let x_abs = abs(x); + var coeffs: vec4; + coeffs[0] = (((cubic_alpha * (x_abs + 1) - 5 * cubic_alpha) * (x_abs + 1) + 8 * cubic_alpha) * (x_abs + 1) - 4 * cubic_alpha); + coeffs[1] = (((cubic_alpha + 2) * x_abs - (cubic_alpha + 3)) * x_abs * x_abs + 1); + coeffs[2] = (((cubic_alpha + 2) * (1 - x_abs) - (cubic_alpha + 3)) * (1 - x_abs) * (1 - x_abs) + 1); + coeffs[3] = (((cubic_alpha * (2 - x_abs) - 5 * cubic_alpha) * (2 - x_abs) + 8 * cubic_alpha) * (2 - x_abs) - 4 * cubic_alpha); + return coeffs; + } +`,jf=e=>` + fn gs_bicubic_interpolate(p: mat4x4<${e}>, x: f32, y: f32) -> ${e} { + var v: vec4; + var coeffs = gs_get_cubic_coeffs(x); + for (var i = 0; i < 4; i++) { + v[i] = coeffs[0] * p[i][0] + coeffs[1] * p[i][1] + coeffs[2] * p[i][2] + coeffs[3] * p[i][3]; + } + coeffs = gs_get_cubic_coeffs(y); + let pixel = ${e}(coeffs[0] * v[0] + coeffs[1] * v[1] + coeffs[2] * v[2] + coeffs[3] * v[3]); + return pixel; + } +`,Kf=e=>` + fn gs_denormalize(n: f32, length: i32) -> f32 { + ${e.alignCorners===0?` + // alignCorners: false => [-1, 1] to [-0.5, length - 0.5] + return ((n + 1.0) * f32(length) - 1.0) / 2.0; + `:` + // alignCorners: true => [-1, 1] to [0, length - 1] + return (n + 1.0) / 2.0 * (f32(length - 1)); + `} + } +`,Zf=e=>` + ${e.paddingMode==="reflection"?` + fn gs_reflect(x: i32, x_min: f32, x_max: f32) -> u32 { + var dx = 0.0; + var fx = f32(x); + let range = x_max - x_min; + if (fx < x_min) { + dx = x_min - fx; + let n = u32(dx / range); + let r = dx - f32(n) * range; + if (n % 2 == 0) { + fx = x_min + r; + } else { + fx = x_max - r; + } + } else if (fx > x_max) { + dx = fx - x_max; + let n = u32(dx / range); + let r = dx - f32(n) * range; + if (n % 2 == 0) { + fx = x_max - r; + } else { + fx = x_min + r; + } + } + return u32(fx); + }`:""} +`,Qf=(e,t,r)=>` + fn pixel_at_grid(r: i32, c: i32, H: i32, W: i32, batch: u32, channel: u32, border: vec4) -> ${t} { + var pixel = ${t}(0); + var indices = vec4(0); + indices[${ut}] = batch; + indices[${yt}] = channel;`+(()=>{switch(r.paddingMode){case"zeros":return` + if (r >= 0 && r < H && c >=0 && c < W) { + indices[${Ut}] = u32(r); + indices[${Nt}] = u32(c); + } else { + return ${t}(0); + } + `;case"border":return` + indices[${Ut}] = u32(clamp(r, 0, H - 1)); + indices[${Nt}] = u32(clamp(c, 0, W - 1)); + `;case"reflection":return` + indices[${Ut}] = gs_reflect(r, border[1], border[3]); + indices[${Nt}] = gs_reflect(c, border[0], border[2]); + `;default:throw new Error(`padding mode ${r.paddingMode} is not supported`)}})()+` + return ${e.getByIndices("indices")}; + } +`,Yf=(e,t,r)=>(()=>{switch(r.mode){case"nearest":return` + let result = pixel_at_grid(i32(round(y)), i32(round(x)), H_in, W_in, indices[${ut}], indices[${yt}], border); + `;case"bilinear":return` + let x1 = i32(floor(x)); + let y1 = i32(floor(y)); + let x2 = x1 + 1; + let y2 = y1 + 1; + + let p11 = pixel_at_grid(y1, x1, H_in, W_in, indices[${ut}], indices[${yt}], border); + let p12 = pixel_at_grid(y1, x2, H_in, W_in, indices[${ut}], indices[${yt}], border); + let p21 = pixel_at_grid(y2, x1, H_in, W_in, indices[${ut}], indices[${yt}], border); + let p22 = pixel_at_grid(y2, x2, H_in, W_in, indices[${ut}], indices[${yt}], border); + + let dx2 = ${t}(f32(x2) - x); + let dx1 = ${t}(x - f32(x1)); + let dy2 = ${t}(f32(y2) - y); + let dy1 = ${t}(y - f32(y1)); + let result = dy2 * (dx2 * p11 + dx1 * p12) + dy1 * (dx2 * p21 + dx1 * p22); + `;case"bicubic":return` + let x0 = i32(floor(x)) - 1; + let y0 = i32(floor(y)) - 1; + var p: mat4x4<${t}>; + for (var h = 0; h < 4; h++) { + for (var w = 0; w < 4; w++) { + p[h][w] = pixel_at_grid(h + y0, w + x0, H_in, W_in, indices[${ut}], indices[${yt}], border); + } + } + + let dx = x - f32(x0 + 1); + let dy = y - f32(y0 + 1); + let result = gs_bicubic_interpolate(p, dx, dy); + `;default:throw new Error(`mode ${r.mode} is not supported`)}})()+`${e.setByOffset("global_idx","result")}`,Xf=(e,t)=>{let r=P("x",e[0].dataType,e[0].dims.length),n=[e[1].dims[0],e[1].dims[1],e[1].dims[2]],o=P("grid",e[1].dataType,n.length,2),i=[e[0].dims[0],e[0].dims[1],e[1].dims[1],e[1].dims[2]];t.format==="NHWC"&&(i=[e[0].dims[0],e[1].dims[1],e[1].dims[2],e[0].dims[3]],[ut,yt,Ut,Nt]=[0,3,1,2]);let a=M("output",e[0].dataType,i.length),u=r.type.value,d=k.size(i),c=[{type:12,data:d},...N(e[0].dims,n,i)],p=m=>` + ${m.registerUniform("output_size","u32").declareVariables(r,o,a)} + ${qf} + ${jf(u)} + ${Kf(t)} + ${Zf(t)} + ${Qf(r,u,t)} + + ${m.mainStart()} + ${m.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + let H_in = i32(uniforms.x_shape[${Ut}]); + let W_in = i32(uniforms.x_shape[${Nt}]); + + ${t.alignCorners===0?` + let x_min = -0.5; + let x_max = f32(W_in) - 0.5; + let y_min = -0.5; + let y_max = f32(H_in) - 0.5; + `:` + let x_min = 0.0; + let x_max = f32(W_in) - 1.0; + let y_min = 0.0; + let y_max = f32(H_in) - 1.0; + `}; + let border = vec4(x_min, y_min, x_max, y_max); + + let indices = ${a.offsetToIndices("global_idx")}; + var grid_indices = vec3(indices[${ut}], indices[${Ut}], indices[${Nt}]); + let nxy = ${o.getByIndices("grid_indices")}; + var x = gs_denormalize(f32(nxy[0]), W_in); + var y = gs_denormalize(f32(nxy[1]), H_in); + + ${Yf(a,u,t)} + }`;return{name:"GridSample",shaderCache:{hint:`${t.cacheKey}`,inputDependencies:["type","type"]},getRunData:m=>{let f=k.size(i);return{outputs:[{dims:i,dataType:m[0].dataType}],dispatchGroup:{x:Math.ceil(f/64)},programUniforms:c}},getShaderSource:p}},Bd=(e,t)=>{Ff(e.inputs),e.compute(Xf(e.inputs,t))},Dd=e=>J({alignCorners:e.align_corners,mode:e.mode,paddingMode:e.padding_mode,format:e.format})});var Be,th,Ud,Rd,rh,er,Nd,$o=U(()=>{"use strict";ee();ne();Se();Vr();Fr();ie();st();Be=(e,t)=>e.length>t&&e[t].dims.length>0?e[t]:void 0,th=(e,t)=>{let r=e[0],n=Be(e,1),o=Be(e,2),i=Be(e,3),a=Be(e,4),u=Be(e,5),d=Be(e,6),c=Be(e,7);if(r.dims.length!==3&&r.dims.length!==5)throw new Error("Input query is expected to have 3 or 5 dimensions");let p=r.dims[0],m=r.dims[1],f=r.dims.length===3?r.dims[2]:t.numHeads*r.dims[4],b=m,g=0,_=0,S=Math.floor(f/t.numHeads);if(d&&c&&k.size(d.dims)&&k.size(c.dims)){if(d.dims.length!==4)throw new Error('Input "past_key" is expected to have 4 dimensions');if(d.dims[0]!==p||d.dims[1]!==t.numHeads||d.dims[3]!==S)throw new Error('Input "past_key" shape (batch_size, num_heads, past_sequence_length, head_size)');if(c.dims[0]!==p||c.dims[1]!==t.numHeads||c.dims[3]!==S)throw new Error('Input "past_value" shape (batch_size, num_heads, past_sequence_length, head_size)');if(d.dims[2]!==c.dims[2])throw new Error('Input "past_key" and "past_value" shall have same dim 2 (past_sequence_length)');if(c.dims.length!==4)throw new Error('Input "past_value" is expected to have 4 dimensions');g=d.dims[2],_=d.dims[2]}else if(d&&k.size(d.dims)||c&&k.size(c.dims))throw new Error('Input "past_key" and "past_value" shall be both present or both absent');let $;if(n&&k.size(n.dims)>0){if(r.dims.length!==3)throw new Error('Input "query" is expected to have 3 dimensions when key is given');if(n.dims.length<3||n.dims.length>5)throw new Error('Input "key" is expected to have 3, 4, or 5 dimensions');if(r.dims[0]!==n.dims[0])throw new Error('Input "query" and "key" shall have same dim 0 (batch size)');if(n.dims.length===3){if(n.dims[2]!==r.dims[2])throw new Error('Input "query" and "key" shall have same dim 2 (hidden_size)');$=2,b=n.dims[1]}else if(n.dims.length===5){if(n.dims[2]!==t.numHeads||n.dims[3]!==2||n.dims[4]!==S)throw new Error('Expect "key" shape (batch_size, kv_sequence_length, num_heads, 2, head_size) for packed kv');if(o)throw new Error('Expect "value" be none when "key" has packed kv format.');$=5,b=n.dims[1]}else{if(n.dims[1]!==t.numHeads||n.dims[3]!==S)throw new Error('Expect "key" shape (batch_size, num_heads, kv_sequence_length, head_size) for past_key');$=0,b=n.dims[2]}}else{if(r.dims.length!==5)throw new Error('Input "query" is expected to have 5 dimensions when key is empty');if(r.dims[2]!==t.numHeads||r.dims[3]!==3)throw new Error('Expect "query" shape (batch_size, kv_sequence_length, num_heads, 3, head_size) for packed kv');$=3}if(i&&k.size(i.dims)>0){if(i.dims.length!==1)throw new Error('Input "bias" is expected to have 1 dimension');if(n&&n.dims.length===5&&n.dims[3]===2)throw new Error("bias is not allowed for packed kv.")}let v=g+b,x=0;if(a&&k.size(a.dims)>0){x=8;let z=a.dims;throw z.length===1?z[0]===p?x=1:z[0]===3*p+2&&(x=3):z.length===2&&z[0]===p&&z[1]===v&&(x=5),x===8?new Error('Input "key_padding_mask" shape shall be (batch_size) or (batch_size, total_sequence_length)'):new Error("Mask not supported")}let T=!1,E=f;if(o&&k.size(o.dims)>0){if(o.dims.length!==3&&o.dims.length!==4)throw new Error('Input "value" is expected to have 3 or 4 dimensions');if(r.dims[0]!==o.dims[0])throw new Error('Input "query" and "value" shall have same dim 0 (batch_size)');if(o.dims.length===3){if(b!==o.dims[1])throw new Error('Input "key" and "value" shall have the same dim 1 (kv_sequence_length)');E=o.dims[2]}else{if(b!==o.dims[2])throw new Error('Input "key" and "value" shall have the same dim 2 (kv_sequence_length)');E=o.dims[1]*o.dims[3],T=!0}}let I=!1;if(a&&k.size(a.dims)>0)throw new Error("Key padding mask is not supported");if(u&&k.size(u.dims)>0){if(u.dims.length!==4)throw new Error('Input "attention_bias" is expected to have 4 dimensions');if(u.dims[0]!==p||u.dims[1]!==t.numHeads||u.dims[2]!==m||u.dims[3]!==v)throw new Error('Expect "attention_bias" shape (batch_size, num_heads, sequence_length, total_sequence_length)')}return{batchSize:p,sequenceLength:m,pastSequenceLength:g,kvSequenceLength:b,totalSequenceLength:v,maxSequenceLength:_,inputHiddenSize:0,hiddenSize:f,vHiddenSize:E,headSize:S,vHeadSize:Math.floor(E/t.numHeads),numHeads:t.numHeads,isUnidirectional:!1,pastPresentShareBuffer:!1,maskFilterValue:t.maskFilterValue,maskType:x,scale:t.scale,broadcastResPosBias:I,passPastInKv:T,qkvFormat:$}},Ud=e=>J({...e}),Rd=J({perm:[0,2,1,3]}),rh=(e,t,r,n,o,i,a)=>{let u=[n,o,i],d=k.size(u),c=[{type:12,data:d},{type:12,data:a},{type:12,data:i}],p=m=>{let f=M("qkv_with_bias",t.dataType,u),b=P("qkv",t.dataType,u),g=P("bias",r.dataType,u),_=[{name:"output_size",type:"u32"},{name:"bias_offset",type:"u32"},{name:"hidden_size",type:"u32"}];return` + ${m.registerUniforms(_).declareVariables(b,g,f)} + ${m.mainStart()} + ${m.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + let bias_offset_idx = (global_idx % uniforms.hidden_size) + uniforms.bias_offset; + + qkv_with_bias[global_idx] = qkv[global_idx] + bias[bias_offset_idx]; + }`};return e.compute({name:"MultiHeadAttentionAddBias",shaderCache:{inputDependencies:["type","type"]},getRunData:()=>({outputs:[{dims:u,dataType:t.dataType,gpuDataType:0}],dispatchGroup:{x:Math.ceil(d/64)},programUniforms:c}),getShaderSource:p},{inputs:[t,r],outputs:[-1]})[0]},er=(e,t,r,n,o,i,a,u)=>{let d=i;if(a&&k.size(a.dims)>0){if(n===1)throw new Error("AddBiasReshape is not implemented. Please export your model with packed QKV or KV");return d=rh(e,i,a,t,n,r*o,u),d=d.reshape([t,n,r,o]),r===1||n===1?d:e.compute(Ee(d,Rd.perm),{inputs:[d],outputs:[-1]})[0]}else return i.dims.length===3&&(d=i.reshape([t,n,r,o])),r===1||n===1?d:e.compute(Ee(d,Rd.perm),{inputs:[d],outputs:[-1]})[0]},Nd=(e,t)=>{let r=th(e.inputs,t),n=e.inputs[0],o=Be(e.inputs,1),i=Be(e.inputs,2),a=Be(e.inputs,3),u=Be(e.inputs,4),d=Be(e.inputs,5),c=Be(e.inputs,6),p=Be(e.inputs,7);if(n.dims.length===5)throw new Error("Packed QKV is not implemented");if(o?.dims.length===5)throw new Error("Packed KV is not implemented");let m=o&&i&&o.dims.length===4&&i.dims.length===4,f=er(e,r.batchSize,r.numHeads,r.sequenceLength,r.headSize,n,a,0);if(m)return Rt(e,f,o,i,u,void 0,c,p,d,r);if(!o||!i)throw new Error("key and value must be provided");let b=er(e,r.batchSize,r.numHeads,r.kvSequenceLength,r.headSize,o,a,r.hiddenSize),g=er(e,r.batchSize,r.numHeads,r.kvSequenceLength,r.vHeadSize,i,a,2*r.hiddenSize);Rt(e,f,b,g,u,void 0,c,p,d,r)}});var nh,oh,ih,ah,xo,Vd,Wd,So=U(()=>{"use strict";ee();ne();Se();ie();nh=e=>{if(!e||e.length<1)throw new Error("too few inputs")},oh=(e,t)=>{let r=[],n=t.numOutputs;return e[1].dims[0]>0&&(e[1].getBigInt64Array().forEach(o=>r.push(Number(o))),n=r.length),J({numOutputs:n,axis:t.axis,splitSizes:r})},ih=e=>` +fn calculateOutputIndex(index: u32) -> u32 { + for (var i: u32 = 0u; i < ${e}u; i += 1u ) { + if (index < ${F("uniforms.size_in_split_axis","i",e)}) { + return i; + } + } + return ${e}u; +}`,ah=e=>{let t=e.length,r=[];for(let n=0;n{let r=e[0].dims,n=k.size(r),o=e[0].dataType,i=k.normalizeAxis(t.axis,r.length),a=new Array(t.numOutputs),u=P("input",o,r.length),d=new Array(t.numOutputs),c=[],p=[],m=0,f=[{type:12,data:n}];for(let g=0;g` + ${g.registerUniform("input_size","u32").registerUniform("size_in_split_axis","u32",d.length).declareVariables(u,...a)} + ${ih(d.length)} + ${ah(a)} + + ${g.mainStart()} + ${g.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.input_size")} + + var indices = ${u.offsetToIndices("global_idx")}; + var index = ${u.indicesGet("indices",i)}; + let output_number = calculateOutputIndex(index); + if (output_number != 0) { + index -= ${F("uniforms.size_in_split_axis","output_number - 1u",d.length)}; + ${u.indicesSet("indices",i,"index")}; + } + writeBufferData(output_number, indices, global_idx); + }`;return{name:"Split",shaderCache:{hint:t.cacheKey,inputDependencies:["rank"]},getShaderSource:b,getRunData:()=>({outputs:c,dispatchGroup:{x:Math.ceil(n/64)},programUniforms:f})}},Vd=(e,t)=>{nh(e.inputs);let r=e.inputs.length===1?t:oh(e.inputs,t);e.compute(xo(e.inputs,r),{inputs:[0]})},Wd=e=>{let t=e.axis,r=e.splitSizes,n=e.numOutputs<0?r.length:e.numOutputs;if(n!==r.length)throw new Error("numOutputs and splitSizes lengh must be equal");return J({axis:t,numOutputs:n,splitSizes:r})}});var sh,tn,Ld,To=U(()=>{"use strict";ee();ne();Se();ie();sh=(e,t)=>{let[r,n,o,i]=e,{numHeads:a,rotaryEmbeddingDim:u}=t;if(r.dims.length!==3&&r.dims.length!==4)throw new Error(`Input 'x' is expected to have 3 or 4 dimensions, got ${r.dims.length}`);if(!k.areEqual(n.dims,[])&&!k.areEqual(n.dims,[1])&&n.dims.length!==2)throw new Error(`Input 'position_ids' is expected to have 0, 1, or 2 dimensions, got ${n.dims.length}`);if(o.dims.length!==2)throw new Error(`Input 'cos_cache' is expected to have 2 dimensions, got ${o.dims.length}`);if(i.dims.length!==2)throw new Error(`Input 'sin_cache' is expected to have 2 dimensions, got ${i.dims.length}`);if(!k.areEqual(o.dims,i.dims))throw new Error("Inputs 'cos_cache' and 'sin_cache' are expected to have the same shape");if(u>0&&a===0)throw new Error("num_heads must be provided if rotary_embedding_dim is specified");let d=r.dims[0],c=r.dims[r.dims.length-2],p=o.dims[0],m=k.sizeFromDimension(r.dims,1)/c,f=u===0?o.dims[1]*2:m/a;if(u>f)throw new Error("rotary_embedding_dim must be less than or equal to head_size");if(n.dims.length===2){if(d!==n.dims[0])throw new Error(`Input 'position_ids' dimension 0 should be of size batch_size, got ${n.dims[0]}`);if(c!==n.dims[1])throw new Error(`Input 'position_ids' dimension 1 should be of size sequence_length, got ${n.dims[1]}`)}if(f/2!==o.dims[1]&&u/2!==o.dims[1])throw new Error(`Input 'cos_cache' dimension 1 should be same as head_size / 2 or rotary_embedding_dim / 2, got ${o.dims[1]}`);if(c>p)throw new Error("Updating cos_cache and sin_cache in RotaryEmbedding is not currently supported")},tn=(e,t)=>{let{interleaved:r,numHeads:n,rotaryEmbeddingDim:o,scale:i}=t,a=e[0].dims[0],u=k.sizeFromDimension(e[0].dims,1),d=e[0].dims[e[0].dims.length-2],c=u/d,p=e[2].dims[1],m=o===0?p*2:c/n,f=new Array(a,d,c/m,m-p),b=k.computeStrides(f),g=[{type:1,data:i},{type:12,data:f},{type:12,data:b},...e[0].dims.length===3?new Array({type:12,data:[u,c,m,1]}):[],...e[0].dims.length===4?new Array({type:12,data:[u,m,d*m,1]}):[],...N(e[0].dims,e[1].dims,e[2].dims,e[3].dims,e[0].dims)],_=S=>{let $=P("input",e[0].dataType,e[0].dims.length),v=P("position_ids",e[1].dataType,e[1].dims.length),x=P("cos_cache",e[2].dataType,e[2].dims.length),T=P("sin_cache",e[3].dataType,e[3].dims.length),E=M("output",e[0].dataType,e[0].dims.length);return S.registerUniforms([{name:"scale",type:"f32"},{name:"global_shape",type:"u32",length:f.length},{name:"global_strides",type:"u32",length:b.length},{name:"input_output_strides",type:"u32",length:b.length}]),` + ${S.declareVariables($,v,x,T,E)} + + ${S.mainStart(It)} + let half_rotary_emb_dim = uniforms.${x.name}_shape[1]; + let bsnh = global_idx / uniforms.global_strides % uniforms.global_shape; + let size = uniforms.global_shape[0] * uniforms.global_strides[0]; + ${S.guardAgainstOutOfBoundsWorkgroupSizes("size")} + + if (bsnh[3] < half_rotary_emb_dim) { + let position_ids_idx = + ${v.broadcastedIndicesToOffset("bsnh.xy",M("",v.type.tensor,2))}; + let position_id = + u32(${v.getByOffset("position_ids_idx")}) + select(0, bsnh[1], position_ids_idx == 0); + let i = dot(bsnh, uniforms.input_output_strides) + select(0, bsnh[3], ${r}); + let j = i + select(half_rotary_emb_dim, 1, ${r}); + let re = ${$.getByOffset("i")} * ${x.get("position_id","bsnh[3]")} - + ${$.getByOffset("j")} * ${T.get("position_id","bsnh[3]")}; + ${E.setByOffset("i","re")} + let im = ${$.getByOffset("i")} * ${T.get("position_id","bsnh[3]")} + + ${$.getByOffset("j")} * ${x.get("position_id","bsnh[3]")}; + ${E.setByOffset("j","im")} + } else { + let k = dot(bsnh, uniforms.input_output_strides) + half_rotary_emb_dim; + ${E.setByOffset("k",$.getByOffset("k"))} + } + }`};return{name:"RotaryEmbedding",shaderCache:{hint:J({interleaved:r}).cacheKey,inputDependencies:["rank","rank","rank","rank"]},getShaderSource:_,getRunData:()=>({outputs:[{dims:e[0].dims,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(k.size(f)/It)},programUniforms:g})}},Ld=(e,t)=>{sh(e.inputs,t),e.compute(tn(e.inputs,t))}});var uh,dh,Gd,lh,Hd,Fd=U(()=>{"use strict";Se();ee();Fr();$o();So();st();To();ie();uh=(e,t)=>{if(t.doRotary&&e.length<=7)throw new Error("cos_cache and sin_cache inputs are required if do_rotary is specified");let r=e[0],n=e[1],o=e[2],i=e[3],a=e[4];if(t.doRotary!==0&&e.length<=7)throw new Error("cos_cast and sin_cache are expected if do_rotary attribute is non-zero");if(t.localWindowSize!==-1)throw new Error("Local attention is not supported");if(t.softcap!==0)throw new Error("Softcap is not supported");if(t.rotaryInterleaved!==0)throw new Error("Rotary interleaved is not supported");if(t.smoothSoftmax)throw new Error("Smooth softmax is not supported");if(r.dims.length!==3&&r.dims.length!==5)throw new Error("Input query is expected to have 3 or 5 dimensions");let u=!1,d=r.dims[0],c=r.dims[1],p=r.dims.length===3?u?r.dims[2]/3:r.dims[2]:t.numHeads*r.dims[4],m=c,f=0,b=!n||n.dims.length===0,g=Math.floor(b?p/(t.numHeads+2*t.kvNumHeads):p/t.numHeads);b&&(p=g*t.numHeads);let _=i&&i.dims.length!==0,S=a&&a.dims.length!==0;if(_&&i.dims.length===4&&i.dims[0]===d&&i.dims[1]!==t.kvNumHeads&&i.dims[2]===t.kvNumHeads&&i.dims[3]===g)throw new Error("BSNH pastKey/pastValue is not supported");if(_&&S){if(i.dims.length!==4)throw new Error('Input "past_key" is expected to have 4 dimensions');if(a.dims.length!==4)throw new Error('Input "past_value" is expected to have 4 dimensions');f=i.dims[2]}else if(_||S)throw new Error('Input "past_key" and "past_value" shall be both present or both absent');let v=1;if(n&&n.dims.length>0){if(r.dims.length!==3)throw new Error('Input "query" is expected to have 3 dimensions when key is given');if(n.dims.length<3||n.dims.length>5)throw new Error('Input "key" is expected to have 3, 4, or 5 dimensions');if(r.dims[0]!==n.dims[0])throw new Error('Input "query" and "key" shall have same dim 0 (batch size)');if(n.dims.length===3){if(r.dims[2]%n.dims[2]!==0)throw new Error('Dimension 2 of "query" should be a multiple of "key"');m=n.dims[1]}else if(n.dims.length===5){if(n.dims[2]!==t.numHeads||n.dims[3]!==2||n.dims[4]!==g)throw new Error('Expect "key" shape (batch_size, kv_sequence_length, num_heads, 2, head_size) for packed kv');if(o)throw new Error('Expect "value" be none when "key" has packed kv format.');m=n.dims[1]}else{if(n.dims[1]!==t.numHeads||n.dims[3]!==g)throw new Error('Expect "key" shape (batch_size, num_heads, kv_sequence_length, head_size) for past_key');m=n.dims[2]}}else{if(r.dims.length!==3&&r.dims.length!==5)throw new Error('Input "query" is expected to have 3 or 5 dimensions when key is empty');if(r.dims.length===5&&(r.dims[2]!==t.numHeads||r.dims[3]!==3))throw new Error('Expect "query" shape (batch_size, kv_sequence_length, num_heads, 3, head_size) for packed kv');v=3}let x=0,T=!1,E=t.kvNumHeads?g*t.kvNumHeads:p;if(o&&o.dims.length>0){if(o.dims.length!==3&&o.dims.length!==4)throw new Error('Input "value" is expected to have 3 or 4 dimensions');if(r.dims[0]!==o.dims[0])throw new Error('Input "query" and "value" shall have same dim 0 (batch_size)');if(o.dims.length===3){if(m!==o.dims[1])throw new Error('Input "key" and "value" shall have the same dim 1 (kv_sequence_length)');E=o.dims[2]}else{if(m!==o.dims[2])throw new Error('Input "past_key" and "past_value" shall have the same dim 2 (kv_sequence_length)');E=o.dims[1]*o.dims[3],T=!0}}let I=e.length>4?e[5]:void 0;if(I&&I.dims.length!==1&&I.dims[0]!==d)throw new Error('Input "seqlens" is expected to have 1 dimension and the same dim 0 as batch_size');return{batchSize:d,sequenceLength:c,pastSequenceLength:f,kvSequenceLength:m,totalSequenceLength:-1,maxSequenceLength:-1,inputHiddenSize:0,hiddenSize:p,vHiddenSize:E,headSize:g,vHeadSize:Math.floor(E/t.kvNumHeads),numHeads:t.numHeads,kvNumHeads:t.kvNumHeads,nReps:t.numHeads/t.kvNumHeads,pastPresentShareBuffer:!1,maskType:x,scale:t.scale,broadcastResPosBias:!1,passPastInKv:T,qkvFormat:v}},dh=J({perm:[0,2,1,3]}),Gd=(e,t,r)=>{let n=t,o=r.kvNumHeads;return t.dims.length===3&&r.kvSequenceLength!==0&&(n=t.reshape([r.batchSize,r.kvSequenceLength,o,r.headSize]),n=e.compute(Ee(n,dh.perm),{inputs:[n],outputs:[-1]})[0]),n},lh=(e,t,r,n)=>{let o=7,i=["type","type"],a=[e*t],u=e*t,d=[{type:12,data:u},{type:12,data:t},{type:12,data:e}],c=p=>{let m=P("seq_lens",r.dataType,r.dims),f=P("total_seq_lens",n.dataType,n.dims),b=M("pos_ids",o,a),g=[{name:"output_size",type:"u32"},{name:"sequence_length",type:"u32"},{name:"batch_size",type:"u32"}];return` + ${p.registerUniforms(g).declareVariables(m,f,b)} + ${p.mainStart()} + ${p.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + let total_sequence_length = u32(${f.getByOffset("0")}); + let is_subsequent_prompt = uniforms.sequence_length > 1 && uniforms.sequence_length != total_sequence_length; + let is_first_prompt = !is_subsequent_prompt && uniforms.sequence_length == total_sequence_length; + let batch_idx = global_idx / uniforms.sequence_length; + let sequence_idx = i32(global_idx % uniforms.sequence_length); + var pos_id: i32 = 0; + let seqlen = ${m.getByOffset("batch_idx")}; + let total_seqlen = seqlen + 1; + if (is_first_prompt) { + if (sequence_idx < total_seqlen) { + pos_id = sequence_idx; + } else { + pos_id = 1; + } + ${b.setByOffset("global_idx","pos_id")} + } else if (is_subsequent_prompt) { + let past_seqlen = total_seqlen - i32(uniforms.sequence_length); + if (past_seqlen + sequence_idx < total_seqlen) { + pos_id = past_seqlen + sequence_idx; + } else { + pos_id = 1; + } + ${b.setByOffset("global_idx","pos_id")} + } else if (global_idx < uniforms.batch_size) { + ${b.setByOffset("global_idx","seqlen")} + }; + } + `};return{name:"GeneratePositionIds",shaderCache:{hint:`${e};${t}`,inputDependencies:i},getRunData:()=>({outputs:[{dims:a,dataType:o}],dispatchGroup:{x:Math.ceil(u/64)},programUniforms:d}),getShaderSource:c}},Hd=(e,t)=>{let r=uh(e.inputs,t);if(e.inputs[0].dims.length===5)throw new Error("Packed QKV is not implemented");if(e.inputs[1]?.dims.length===5)throw new Error("Packed KV is not implemented");let n=e.inputs[0],o=e.inputs[1]&&e.inputs[1].dims.length>0?e.inputs[1]:void 0,i=e.inputs[2]&&e.inputs[2].dims.length>0?e.inputs[2]:void 0,a=e.inputs[3]&&e.inputs[3].dims.length!==0?e.inputs[3]:void 0,u=e.inputs[4]&&e.inputs[4].dims.length!==0?e.inputs[4]:void 0,d=e.inputs.length>4?e.inputs[5]:void 0,c=e.inputs.length>5?e.inputs[6]:void 0,p=r.kvNumHeads?r.kvNumHeads:r.numHeads,m=J({axis:2,numOutputs:3,splitSizes:[r.numHeads*r.headSize,p*r.headSize,p*r.headSize]}),[f,b,g]=!o&&!i?e.compute(xo([n],m),{inputs:[n],outputs:[-1,-1,-1]}):[n,o,i],_,S;if(t.doRotary){let T=e.compute(lh(r.batchSize,r.sequenceLength,d,c),{inputs:[d,c],outputs:[-1]})[0],E=e.inputs[7],I=e.inputs[8],z=J({interleaved:t.rotaryInterleaved!==0,numHeads:r.numHeads,rotaryEmbeddingDim:0,scale:t.scale}),O=[f,T,E,I],D=[-1];_=e.compute(tn(O,z),{inputs:O,outputs:D})[0],O.splice(0,1,b);let L=J({interleaved:t.rotaryInterleaved!==0,numHeads:r.kvNumHeads,rotaryEmbeddingDim:0,scale:t.scale});S=e.compute(tn(O,L),{inputs:O,outputs:D})[0]}let $=er(e,r.batchSize,r.numHeads,r.sequenceLength,r.headSize,t.doRotary?_:f,void 0,0),v=Gd(e,t.doRotary?S:b,r),x=Gd(e,g,r);Rt(e,$,v,x,void 0,void 0,a,u,void 0,r,d,c)}});var qd,ch,ph,jd,Kd=U(()=>{"use strict";ee();ne();st();ie();qd=(e,t,r,n,o,i,a,u)=>{let d=ce(i),c=d===1?"f32":`vec${d}f`,p=d===1?"vec2f":`mat2x${d}f`,m=o*a,f=64;m===1&&(f=256);let b=[o,a,i/d],g=[o,a,2],_=["rank","type","type"],S=[];S.push(...N(b,g));let $=v=>{let x=P("x",t.dataType,3,d),T=P("scale",r.dataType,r.dims),E=P("bias",n.dataType,n.dims),I=M("output",1,3,2),z=[x,T,E,I];return` + var workgroup_shared : array<${p}, ${f}>; + const workgroup_size = ${f}u; + ${v.declareVariables(...z)} + ${v.mainStart(f)} + let batch = workgroup_index / uniforms.x_shape[1]; + let channel = workgroup_index % uniforms.x_shape[1]; + let hight = uniforms.x_shape[2]; + // initialize workgroup memory + var sum = ${c}(0); + var squared_sum = ${c}(0); + for (var h = local_idx; h < hight; h += workgroup_size) { + let value = ${c}(${x.get("batch","channel","h")}); + sum += value; + squared_sum += value * value; + } + workgroup_shared[local_idx] = ${p}(sum, squared_sum); + workgroupBarrier(); + + for (var currSize = workgroup_size >> 1; currSize > 0; currSize = currSize >> 1) { + if (local_idx < currSize) { + workgroup_shared[local_idx] = workgroup_shared[local_idx] + workgroup_shared[local_idx + currSize]; + } + workgroupBarrier(); + } + if (local_idx == 0) { + let sum_final = ${He("workgroup_shared[0][0]",d)} / f32(hight * ${d}); + let squared_sum_final = ${He("workgroup_shared[0][1]",d)} / f32(hight * ${d}); + + let inv_std_dev = inverseSqrt(squared_sum_final - sum_final * sum_final + f32(${u})); + let channel_scale = inv_std_dev * f32(scale[channel]); + let channel_shift = f32(bias[channel]) - sum_final * channel_scale; + output[workgroup_index] = vec2f(channel_scale, channel_shift); + } + }`};return e.compute({name:"InstanceNormComputeChannelScaleShift",shaderCache:{hint:`${d};${u};${f}`,inputDependencies:_},getRunData:()=>({outputs:[{dims:g,dataType:1}],dispatchGroup:{x:m},programUniforms:S}),getShaderSource:$},{inputs:[t,r,n],outputs:[-1]})[0]},ch=(e,t,r)=>{let n=t[0].dims,o=n,i=2,a=n[0],u=n[1],d=k.sizeFromDimension(n,i),c=ce(d),p=k.size(o)/c,m=qd(e,t[0],t[1],t[2],a,d,u,r.epsilon),f=[a,u,d/c],b=[a,u],g=["type","none"],_=S=>{let $=P("x",t[0].dataType,f.length,c),v=P("scale_shift",1,b.length,2),x=M("output",t[0].dataType,f.length,c),T=[$,v,x];return` + ${S.registerUniform("output_size","u32").declareVariables(...T)} + ${S.mainStart()} + ${S.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + let outputIndices = ${x.offsetToIndices("global_idx")}; + let batch = outputIndices[0]; + let channel = outputIndices[1]; + let scale_shift = ${v.getByIndices("vec2(batch, channel)")}; + let value = ${$.getByOffset("global_idx")} * ${x.type.value}(scale_shift.x) + ${x.type.value}(scale_shift.y); + ${x.setByOffset("global_idx","value")}; + }`};e.compute({name:"InstanceNormalization",shaderCache:{hint:`${c}`,inputDependencies:g},getRunData:()=>({outputs:[{dims:o,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(p/64)},programUniforms:[{type:12,data:p},...N(f,b,f)]}),getShaderSource:_},{inputs:[t[0],m]})},ph=(e,t,r)=>{let n=t[0].dims,o=n,i=n[0],a=n[n.length-1],u=k.sizeFromDimension(n,1)/a,d=ce(a),c=k.size(o)/d,p=[{type:12,data:u},{type:12,data:Math.floor(a/d)}],m=["type","type"],f=!1,b=[0,n.length-1];for(let $=0;$n[b[v]])),_=qd(e,g,t[1],t[2],i,u,a,r.epsilon),S=$=>{let v=be(t[0].dataType),x=d===1?"vec2f":`mat${d}x2f`,T=z=>{let O=z===0?"x":"y",D=d===1?"f32":`vec${d}f`;switch(d){case 1:return`${v}(${D}(scale.${O}))`;case 2:return`vec2<${v}>(${D}(scale[0].${O}, scale[1].${O}))`;case 4:return`vec4<${v}>(${D}(scale[0].${O}, scale[1].${O}, scale[2].${O}, scale[3].${O}))`;default:throw new Error(`Not supported compoents ${d}`)}},E=P("input",t[0].dataType,t[0].dims,d),I=M("output",t[0].dataType,o,d);return` + @group(0) @binding(0) var input : array<${E.type.storage}>; + @group(0) @binding(1) var scale_input : array<${x}>; + @group(0) @binding(2) var output : array<${I.type.storage}>; + struct Uniforms {H: u32, C : u32}; + @group(0) @binding(3) var uniforms: Uniforms; + + ${$.mainStart()} + let current_image_number = global_idx / (uniforms.C * uniforms.H); + let current_channel_number = global_idx % uniforms.C; + + let scale_offset = current_image_number * uniforms.C + current_channel_number; + let scale = scale_input[scale_offset]; + output[global_idx] = fma(input[global_idx], ${T(0)}, ${T(1)}); + }`};e.compute({name:"InstanceNormalizationNHWC",shaderCache:{hint:`${d}`,inputDependencies:m},getRunData:()=>({outputs:[{dims:o,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(c/64)},programUniforms:p}),getShaderSource:S},{inputs:[t[0],_]})},jd=(e,t)=>{t.format==="NHWC"?ph(e,e.inputs,t):ch(e,e.inputs,t)}});var mh,fh,Zd,Qd=U(()=>{"use strict";ee();ne();ie();mh=e=>{if(!e||e.length<2)throw new Error("layerNorm requires at least 2 inputs.")},fh=(e,t,r)=>{let n=t.simplified,o=e[0].dims,i=e[1],a=!n&&e[2],u=o,d=k.normalizeAxis(t.axis,o.length),c=k.sizeToDimension(o,d),p=k.sizeFromDimension(o,d),m=k.size(i.dims),f=a?k.size(a.dims):0;if(m!==p||a&&f!==p)throw new Error(`Size of X.shape()[axis:] == ${p}. + Size of scale and bias (if provided) must match this. + Got scale size of ${m} and bias size of ${f}`);let b=[];for(let E=0;E1,v=r>2,x=E=>{let I=be(e[0].dataType),z=[P("x",e[0].dataType,e[0].dims,g),P("scale",i.dataType,i.dims,g)];a&&z.push(P("bias",a.dataType,a.dims,g)),z.push(M("output",e[0].dataType,u,g)),$&&z.push(M("mean_data_output",1,b)),v&&z.push(M("inv_std_output",1,b));let O=[{name:"norm_count",type:"u32"},{name:"norm_size",type:"f32"},{name:"norm_size_vectorized",type:"u32"},{name:"epsilon",type:"f32"}];return` + ${E.registerUniforms(O).declareVariables(...z)} + ${E.mainStart()} + ${E.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.norm_count")} + let offset = global_idx * uniforms.norm_size_vectorized; + var mean_vector = ${ao("f32",g)}; + var mean_square_vector = ${ao("f32",g)}; + + for (var h: u32 = 0u; h < uniforms.norm_size_vectorized; h++) { + let value = ${Ct(I,g,"x[h + offset]")}; + mean_vector += value; + mean_square_vector += value * value; + } + let mean = ${He("mean_vector",g)} / uniforms.norm_size; + let inv_std_dev = inverseSqrt(${He("mean_square_vector",g)} / uniforms.norm_size ${n?"":"- mean * mean"} + uniforms.epsilon); + + for (var j: u32 = 0; j < uniforms.norm_size_vectorized; j++) { + let f32input = ${Ct(I,g,"x[j + offset]")}; + let f32scale = ${Ct(I,g,"scale[j]")}; + output[j + offset] = ${z[0].type.value}((f32input ${n?"":"- mean"}) * inv_std_dev * f32scale + ${a?`+ ${Ct(I,g,"bias[j]")}`:""} + ); + } + + ${$?"mean_data_output[global_idx] = mean":""}; + ${v?"inv_std_output[global_idx] = inv_std_dev":""}; + }`},T=[{dims:u,dataType:e[0].dataType}];return $&&T.push({dims:b,dataType:1}),v&&T.push({dims:b,dataType:1}),{name:"LayerNormalization",shaderCache:{hint:`${g};${r};${n}`,inputDependencies:_},getRunData:()=>({outputs:T,dispatchGroup:{x:Math.ceil(c/64)},programUniforms:S}),getShaderSource:x}},Zd=(e,t)=>{mh(e.inputs),e.compute(fh(e.inputs,t,e.outputCount))}});var hh,Yd,Xd=U(()=>{"use strict";ne();Yr();Xr();hh=e=>{if(!e||e.length!==2)throw new Error("MatMul requires 2 inputs.");if(e[0].dims[e[0].dims.length-1]!==e[1].dims[e[1].dims.length-2])throw new Error("shared dimension does not match.")},Yd=e=>{hh(e.inputs);let t=Je.calcShape(e.inputs[0].dims,e.inputs[1].dims,!0);if(!t)throw new Error("Can't use matmul on the given tensors");let r=t[t.length-1],n=e.inputs[0].dims[e.inputs[0].dims.length-1];if(r<8&&n<8)e.compute(Qr(e.inputs,{activation:""},t));else{let o=t[t.length-2],i=k.size(e.inputs[0].dims.slice(0,-2)),a=k.size(e.inputs[1].dims.slice(0,-2));if(i!==1&&o===1&&a===1){let u=e.inputs[0].reshape([1,i,n]),d=e.inputs[1].reshape([1,n,r]),c=[1,i,r],p=[u,d];e.compute(Jt(p,{activation:""},t,c),{inputs:p})}else e.compute(Jt(e.inputs,{activation:""},t))}}});var gh,bh,yh,Jd,el,tl=U(()=>{"use strict";ee();ne();Se();ie();gh=(e,t)=>{if(e.length<3||e.length>4)throw new Error("MatMulNBits requires 3 or 4 inputs");let r=e[0],n=r.dims.length;if(r.dims[n-1]!==t.k)throw new Error("The last dim of input shape does not match the k value");let o=Math.floor((t.k+t.blockSize-1)/t.blockSize),i=t.blockSize/8*t.bits,a=e[1];if(!k.areEqual(a.dims,[t.n,o,i]))throw new Error("The second inputs must be 3D tensor with shape N X nBlocksPerCol X blobSize");let d=e[2].dims;if(k.size(d)!==t.n*o)throw new Error("scales input size error.");if(e.length===4){let p=e[3].dims,m=t.bits>4?t.n*o:t.n*Math.floor((o+1)/2);if(k.size(p)!==m)throw new Error("zeroPoints input size error.")}},bh=(e,t)=>{let r=e[0].dims,n=r.length,o=r[n-2],i=t.k,a=t.n,u=r.slice(0,n-2),d=k.size(u),p=e[1].dims[2]/4,m=e[0].dataType,f=ce(t.k),b=ce(p),g=ce(a),_=u.concat([o,a]),S=o>1&&a/g%2===0?2:1,$=k.size(_)/g/S,v=64,x=[],T=[d,o,i/f],E=k.convertShape(e[1].dims).slice();E.splice(-1,1,p/b),x.push(...N(T)),x.push(...N(E)),x.push(...N(e[2].dims)),e.length===4&&x.push(...N(k.convertShape(e[3].dims)));let I=[d,o,a/g];x.push(...N(I));let z=O=>{let D=T.length,L=P("a",e[0].dataType,D,f),q=P("b",12,E.length,b),Q=P("scales",e[2].dataType,e[2].dims.length),W=[L,q,Q],Z=e.length===4?P("zero_points",12,e[3].dims.length):void 0;Z&&W.push(Z);let we=I.length,H=M("output",e[0].dataType,we,g),j=be(e[0].dataType),te=(()=>{switch(f){case 1:return`array<${j}, 8>`;case 2:return`mat4x2<${j}>`;case 4:return`mat2x4<${j}>`;default:throw new Error(`${f}-component is not supported.`)}})(),X=()=>{let ye=` + // reuse a data + var input_offset = ${L.indicesToOffset(`${L.type.indices}(batch, row, word_offset)`)}; + var a_data: ${te}; + for (var j: u32 = 0; j < ${8/f}; j++) { + a_data[j] = ${L.getByOffset("input_offset")}; + input_offset++; + } + `;for(let re=0;re> 4) & b_mask); + b_quantized_values = ${te}(${Array.from({length:4},(C,V)=>`${j}(b_value_lower[${V}]), ${j}(b_value_upper[${V}])`).join(", ")}); + b_dequantized_values = ${f===1?`${te}(${Array.from({length:8},(C,V)=>`(b_quantized_values[${V}] - ${Z?`zero_point${re}`:"zero_point"}) * scale${re}`).join(", ")});`:`(b_quantized_values - ${te}(${Array(8).fill(`${Z?`zero_point${re}`:"zero_point"}`).join(",")})) * scale${re};`}; + workgroup_shared[local_id.x * ${S} + ${Math.floor(re/g)}]${g>1?`[${re%g}]`:""} += ${Array.from({length:8/f},(C,V)=>`${f===1?`a_data[${V}] * b_dequantized_values[${V}]`:`dot(a_data[${V}], b_dequantized_values[${V}])`}`).join(" + ")}; + `;return ye},ue=()=>{let ye=` + var col_index = col * ${g}; + ${Z?` + let zero_point_bytes_per_col = (nBlocksPerCol + 1) / 2; + var zero_point_byte_count: u32; + var zero_point_word_index: u32; + var zero_point_byte_offset: u32; + let zero_point_nibble_offset: u32 = block & 0x1u; + var zero_point_bits_offset: u32; + var zero_point_word: u32;`:` + // The default zero point is 8 for unsigned 4-bit quantization. + let zero_point = ${j}(8);`} + `;for(let re=0;re> 0x1u); + zero_point_word_index = zero_point_byte_count >> 0x2u; + zero_point_byte_offset = zero_point_byte_count & 0x3u; + zero_point_bits_offset = (zero_point_byte_offset << 3) + (zero_point_nibble_offset << 2); + zero_point_word = ${Z.getByOffset("zero_point_word_index")} >> zero_point_bits_offset; + let zero_point${re} = ${j}((zero_point_word) & 0xFu);`:""} + col_index += 1;`;return ye},he=()=>{let ye=`col_index = col * ${g};`;for(let re=0;re; + var b_value_upper: vec4; + var b_quantized_values: ${te}; + var b_dequantized_values: ${te};`,ye};return` + var workgroup_shared: array<${H.type.value}, ${S*v}>; + ${O.declareVariables(...W,H)} + ${O.mainStart([v,1,1])} + let output_indices = ${H.offsetToIndices(`(global_idx / ${v}) * ${S}`)}; + let col = output_indices[2]; + let row = output_indices[1]; + let batch = output_indices[0]; + let nBlocksPerCol = uniforms.b_shape[1]; + + for (var block = local_id.x; block < nBlocksPerCol; block += ${v}) { + //process one block + var word_offset: u32 = block * ${t.blockSize/f}; + ${ue()} + for (var word: u32 = 0; word < ${p}; word += ${b}) { + ${he()} + for (var i: u32 = 0; i < ${b}; i++) { + ${X()} + word_offset += ${8/f}; + } + } + } + workgroupBarrier(); + + if (local_id.x < ${S}) { + var output_value: ${H.type.value} = ${H.type.value}(0); + var workgroup_shared_offset: u32 = local_id.x; + for (var b: u32 = 0u; b < ${v}u; b++) { + output_value += workgroup_shared[workgroup_shared_offset]; + workgroup_shared_offset += ${S}; + } + ${H.setByIndices(`${H.type.indices}(batch, row, col + local_id.x)`,"output_value")}; + } + }`};return{name:"MatMulNBits",shaderCache:{hint:`${t.blockSize};${t.bits};${f};${b};${g};${S};${v}`,inputDependencies:Array(e.length).fill("rank")},getRunData:()=>({outputs:[{dims:_,dataType:m}],dispatchGroup:{x:$},programUniforms:x}),getShaderSource:z}},yh=(e,t)=>{let r=e[0].dims,n=r.length,o=r[n-2],i=t.k,a=t.n,u=r.slice(0,n-2),d=k.size(u),p=e[1].dims[2]/4,m=e[0].dataType,f=ce(t.k),b=ce(p),g=u.concat([o,a]),_=128,S=a%8===0?8:a%4===0?4:1,$=_/S,v=$*b*8,x=v/f,T=v/t.blockSize,E=k.size(g)/S,I=[],z=[d,o,i/f],O=k.convertShape(e[1].dims).slice();O.splice(-1,1,p/b),I.push(...N(z)),I.push(...N(O)),I.push(...N(e[2].dims)),e.length===4&&I.push(...N(k.convertShape(e[3].dims)));let D=[d,o,a];I.push(...N(D));let L=q=>{let Q=z.length,W=P("a",e[0].dataType,Q,f),Z=P("b",12,O.length,b),we=P("scales",e[2].dataType,e[2].dims.length),H=[W,Z,we],j=e.length===4?P("zero_points",12,e[3].dims.length):void 0;j&&H.push(j);let te=D.length,X=M("output",e[0].dataType,te),ue=be(e[0].dataType),he=()=>{switch(f){case 1:return` + let a_data0 = vec4<${ue}>(sub_a[word_offset], sub_a[word_offset + 1], sub_a[word_offset + 2], sub_a[word_offset + 3]); + let a_data1 = vec4<${ue}>(sub_a[word_offset + 4], sub_a[word_offset + 5], sub_a[word_offset + 6], sub_a[word_offset + 7]);`;case 2:return` + let a_data0 = vec4<${ue}>(sub_a[word_offset], sub_a[word_offset + 1]); + let a_data1 = vec4<${ue}>(sub_a[word_offset + 2], sub_a[word_offset + 3]);`;case 4:return` + let a_data0 = sub_a[word_offset]; + let a_data1 = sub_a[word_offset + 1];`;default:throw new Error(`${f}-component is not supported.`)}};return` + var sub_a: array<${W.type.value}, ${x}>; + var inter_results: array, ${S}>; + ${q.declareVariables(...H,X)} + ${q.mainStart([$,S,1])} + let output_indices = ${X.offsetToIndices(`workgroup_index * ${S}`)}; + let col = output_indices[2]; + let row = output_indices[1]; + let batch = output_indices[0]; + let n_blocks_per_col = uniforms.b_shape[1]; + let num_tiles = (n_blocks_per_col - 1) / ${T} + 1; + + // Loop over shared dimension. + for (var tile: u32 = 0; tile < num_tiles; tile += 1) { + let a_col_start = tile * ${x}; + // load one tile A data into shared memory. + for (var a_offset = local_idx; a_offset < ${x}; a_offset += ${_}) + { + let a_col = a_col_start + a_offset; + if (a_col < uniforms.a_shape[2]) + { + sub_a[a_offset] = ${W.getByIndices(`${W.type.indices}(batch, row, a_col)`)}; + } else { + sub_a[a_offset] = ${W.type.value}(0); + } + } + workgroupBarrier(); + + // each thread process one block + let b_row = col + local_id.y; + let block = tile * ${T} + local_id.x; + ${j?` + let zero_point_bytes_per_col = (n_blocks_per_col + 1) / 2; + let zero_point_byte_count = b_row * zero_point_bytes_per_col + (block >> 0x1u); + let zero_point_word_index = zero_point_byte_count >> 0x2u; + let zero_point_byte_offset = zero_point_byte_count & 0x3u; + let zero_point_nibble_offset: u32 = block & 0x1u; + let zero_point_bits_offset = (zero_point_byte_offset << 3) + (zero_point_nibble_offset << 2); + let zero_point_word = ${j.getByOffset("zero_point_word_index")} >> zero_point_bits_offset; + let zero_point = ${ue}((zero_point_word) & 0xFu);`:` + // The default zero point is 8 for unsigned 4-bit quantization. + let zero_point = ${ue}(8);`} + let scale = ${we.getByOffset("b_row * n_blocks_per_col + block")}; + let b_data = ${Z.getByIndices(`${Z.type.indices}(b_row, block, 0)`)}; + var word_offset = local_id.x * ${t.blockSize/f}; + for (var i: u32 = 0; i < ${b}; i++) { + ${he()} + let b_value = ${b===1?"b_data":"b_data[i]"}; + let b_value_lower = unpack4xU8(b_value & 0x0F0F0F0Fu); + let b_value_upper = unpack4xU8((b_value >> 4) & 0x0F0F0F0Fu); + let b_quantized_values = mat2x4<${ue}>(${Array.from({length:4},(ye,re)=>`${ue}(b_value_lower[${re}]), ${ue}(b_value_upper[${re}])`).join(", ")}); + let b_dequantized_values = (b_quantized_values - mat2x4<${ue}>(${Array(8).fill("zero_point").join(",")})) * scale; + inter_results[local_id.y][local_id.x] += ${Array.from({length:2},(ye,re)=>`${`dot(a_data${re}, b_dequantized_values[${re}])`}`).join(" + ")}; + word_offset += ${8/f}; + } + workgroupBarrier(); + } + + if (local_idx < ${S}) { + var output_value: ${X.type.value} = ${X.type.value}(0); + for (var b = 0u; b < ${$}; b++) { + output_value += inter_results[local_idx][b]; + } + if (col + local_idx < uniforms.output_shape[2]) + { + ${X.setByIndices(`${X.type.indices}(batch, row, col + local_idx)`,"output_value")} + } + } + }`};return{name:"BlockwiseMatMulNBits32",shaderCache:{hint:`${t.blockSize};${f};${b};${$};${S}`,inputDependencies:Array(e.length).fill("rank")},getRunData:()=>({outputs:[{dims:g,dataType:m}],dispatchGroup:{x:E},programUniforms:I}),getShaderSource:L}},Jd=(e,t)=>{gh(e.inputs,t),t.blockSize===32&&e.adapterInfo.isVendor("intel")&&e.adapterInfo.isArchitecture("gen-12lp")?e.compute(yh(e.inputs,t)):e.compute(bh(e.inputs,t))},el=e=>J(e)});var _h,wh,vh,$h,xh,Sh,Th,Ih,rl,nl=U(()=>{"use strict";ee();ne();ie();_h=e=>{if(!e||e.length<1)throw new Error("Too few inputs");if(e[0].dataType!==1&&e[0].dataType!==10)throw new Error("Input type must be float or float16.");if(e.length>=2){let t=e[0].dims.length*2===e[1].dims[0];if(e.length===4&&(t=e[3].dims[0]*2===e[1].dims[0]),!t)throw new Error("The pads should be a 1D tensor of shape [2 * input_rank] or [2 * num_axes].")}},wh=(e,t,r)=>{let n="";for(let o=t-1;o>=0;--o)n+=` + k = i32(${e.indicesGet("indices",o)}) - ${F("uniforms.pads",o,r)}; + if (k < 0) { + break; + } + if (k >= i32(${F("uniforms.x_shape",o,t)})) { + break; + } + offset += k * i32(${F("uniforms.x_strides",o,t)}); + `;return` + value = ${e.type.value}(uniforms.constant_value); + for (var i = 0; i < 1; i++) { + var offset = 0; + var k = 0; + ${n} + value = x[offset]; + } + `},vh=(e,t,r)=>{let n="";for(let o=t-1;o>=0;--o)n+=` + k = i32(${e.indicesGet("indices",o)}) - ${F("uniforms.pads",o,r)}; + if (k < 0) { + k = -k; + } + { + let _2n_1 = 2 * (i32(${F("uniforms.x_shape",o,t)}) - 1); + k = k % _2n_1; + if(k >= i32(${F("uniforms.x_shape",o,t)})) { + k = _2n_1 - k; + } + } + offset += k * i32(${F("uniforms.x_strides",o,t)}); + `;return` + var offset = 0; + var k = 0; + ${n} + value = x[offset]; + `},$h=(e,t,r)=>{let n="";for(let o=t-1;o>=0;--o)n+=` + k = i32(${e.indicesGet("indices",o)}) - ${F("uniforms.pads",o,r)}; + if (k < 0) { + k = 0; + } + if (k >= i32(${F("uniforms.x_shape",o,t)})) { + k = i32(${F("uniforms.x_shape",o,t)}) - 1; + } + offset += k * i32(${F("uniforms.x_strides",o,t)}); + `;return` + var offset = 0; + var k = 0; + ${n} + value = x[offset]; + `},xh=(e,t,r)=>{let n="";for(let o=t-1;o>=0;--o)n+=` + k = i32(${e.indicesGet("indices",o)}) - ${F("uniforms.pads",o,r)}; + if (k < 0) { + k += i32(${F("uniforms.x_shape",o,t)}]); + } + if (k >= i32(${F("uniforms.x_shape",o,t)})) { + k -= i32(${F("uniforms.x_shape",o,t)}); + } + offset += k * i32(${F("uniforms.x_strides",o,t)}); + `;return` + var offset = 0; + var k = 0; + ${n} + value = x[offset]; + `},Sh=(e,t,r)=>{switch(r.mode){case 0:return wh(e,t,r.pads.length);case 1:return vh(e,t,r.pads.length);case 2:return $h(e,t,r.pads.length);case 3:return xh(e,t,r.pads.length);default:throw new Error("Invalid mode")}},Th=(e,t)=>{let r=k.padShape(e[0].dims.slice(),t.pads),n=e[0].dims,o=k.size(r),i=[{type:12,data:o},{type:6,data:t.pads}],a=e.length>=3&&e[2].data;t.mode===0&&i.push({type:a?e[2].dataType:1,data:t.value}),i.push(...N(e[0].dims,r));let u=["rank"],d=c=>{let p=M("output",e[0].dataType,r.length),m=P("x",e[0].dataType,n.length),f=m.type.value,b=Sh(p,n.length,t),g=[{name:"output_size",type:"u32"},{name:"pads",type:"i32",length:t.pads.length}];return t.mode===0&&g.push({name:"constant_value",type:a?f:"f32"}),` + ${c.registerUniforms(g).declareVariables(m,p)} + ${c.mainStart()} + ${c.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + + let indices = ${p.offsetToIndices("global_idx")}; + + var value = ${f}(0); + ${b} + output[global_idx] = value; + }`};return{name:"Pad",shaderCache:{hint:`${t.mode}${a}`,inputDependencies:u},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(k.size(r)/64)},programUniforms:i}),getShaderSource:d}},Ih=(e,t)=>{if(e.length>1){let r=e[1].getBigInt64Array(),n=e.length>=3&&e[2].data?e[2].dataType===10?e[2].getUint16Array()[0]:e[2].getFloat32Array()[0]:0,o=e[0].dims.length,i=new Int32Array(2*o).fill(0);if(e.length>=4){let u=e[3].getBigInt64Array();for(let d=0;di[Number(d)]=Number(u));let a=[];return i.forEach(u=>a.push(u)),{mode:t.mode,value:n,pads:a}}else return t},rl=(e,t)=>{_h(e.inputs);let r=Ih(e.inputs,t);e.compute(Th(e.inputs,r),{inputs:[0]})}});var rn,ol,il,al,sl,Ch,Ah,ul,dl,ll,cl,pl,ml,fl,hl,gl,bl,yl,_l,wl=U(()=>{"use strict";We();ee();ne();ie();rn=e=>{if(ge.webgpu.validateInputContent&&(!e||e.length!==1))throw new Error("Pool ops requires 1 input.")},ol=(e,t,r)=>{let n=t.format==="NHWC",o=e.dims.slice();n&&o.splice(1,0,o.pop());let i=Object.hasOwnProperty.call(t,"dilations"),a=t.kernelShape.slice(),u=t.strides.slice(),d=i?t.dilations.slice():[],c=t.pads.slice();Tt.adjustPoolAttributes(r,o,a,u,d,c);let p=Tt.computePoolOutputShape(r,o,u,d,a,c,t.autoPad),m=Object.assign({},t);i?Object.assign(m,{kernelShape:a,strides:u,pads:c,dilations:d,cacheKey:t.cacheKey}):Object.assign(m,{kernelShape:a,strides:u,pads:c,cacheKey:t.cacheKey});let f=p.slice();return f.push(f.splice(1,1)[0]),[m,n?f:p]},il=(e,t)=>{let r=t.format==="NHWC",n=k.size(e),o=k.size(t.kernelShape),i=[{type:12,data:n},{type:12,data:o}],a=[{name:"outputSize",type:"u32"},{name:"kernelSize",type:"u32"}];if(t.kernelShape.length<=2){let u=t.kernelShape[t.kernelShape.length-1],d=t.strides[t.strides.length-1],c=t.pads[t.pads.length/2-1],p=t.pads[t.pads.length-1],m=!!(c+p);i.push({type:12,data:u},{type:12,data:d},{type:12,data:c},{type:12,data:p}),a.push({name:"kw",type:"u32"},{name:"sw",type:"u32"},{name:"pwStart",type:"u32"},{name:"pwEnd",type:"u32"});let f=!1;if(t.kernelShape.length===2){let b=t.kernelShape[t.kernelShape.length-2],g=t.strides[t.strides.length-2],_=t.pads[t.pads.length/2-2],S=t.pads[t.pads.length-2];f=!!(_+S),i.push({type:12,data:b},{type:12,data:g},{type:12,data:_},{type:12,data:S}),a.push({name:"kh",type:"u32"},{name:"sh",type:"u32"},{name:"phStart",type:"u32"},{name:"phEnd",type:"u32"})}return[i,a,!0,m,f]}else{if(r)throw new Error("Pooling with kernelShape.length > 2 is not supported for NHWC format.");let u=k.computeStrides(t.kernelShape);i.push({type:12,data:u},{type:12,data:t.pads},{type:12,data:t.strides}),a.push({name:"kernelStrides",type:"u32",length:u.length},{name:"pads",type:"u32",length:t.pads.length},{name:"strides",type:"u32",length:t.strides.length});let d=t.pads.reduce((c,p)=>c+p);return[i,a,!!d,!1,!1]}},al=(e,t,r,n,o,i,a,u,d,c,p,m)=>{let f=o.format==="NHWC",b=t.type.value,g=M("output",t.type.tensor,n);if(o.kernelShape.length<=2){let _="",S="",$="",v=r-(f?2:1);if(p?_=` + for (var i: u32 = 0u; i < uniforms.kw; i++) { + xIndices[${v}] = indices[${v}] * uniforms.sw - uniforms.pwStart + i; + if (xIndices[${v}] < 0 || xIndices[${v}] + >= uniforms.x_shape[${v}]) { + pad++; + continue; + } + let x_val = x[${t.indicesToOffset("xIndices")}]; + ${i} + }`:_=` + for (var i: u32 = 0u; i < uniforms.kw; i++) { + xIndices[${v}] = indices[${v}] * uniforms.sw - uniforms.pwStart + i; + let x_val = x[${t.indicesToOffset("xIndices")}]; + ${i} + }`,o.kernelShape.length===2){let T=r-(f?3:2);m?S=` + for (var j: u32 = 0u; j < uniforms.kh; j++) { + xIndices[${T}] = indices[${T}] * uniforms.sh - uniforms.phStart + j; + if (xIndices[${T}] < 0 || xIndices[${T}] >= uniforms.x_shape[${T}]) { + pad += i32(uniforms.kw); + continue; + } + `:S=` + for (var j: u32 = 0u; j < uniforms.kh; j++) { + xIndices[${T}] = indices[${T}] * uniforms.sh - uniforms.phStart + j; + `,$=` + } + `}return` + ${e.registerUniforms(d).declareVariables(t,g)} + + ${e.mainStart()} + ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + + let indices = ${g.offsetToIndices("global_idx")}; + var xIndices = ${g.offsetToIndices("global_idx")}; + + var value = ${b}(${u}); + var pad = 0; + ${S} + ${_} + ${$} + ${a} + + output[global_idx] = value; + }`}else{if(f)throw new Error("Pooling with kernelShape.length > 2 is not supported for NHWC format.");let _=o.kernelShape.length,S=o.pads.length,$="";return c?$=` + if (xIndices[j] >= uniforms.x_shape[j]) { + pad++; + isPad = true; + break; + } + } + if (!isPad) { + let x_val = x[${t.indicesToOffset("xIndices")}]; + ${i} + }`:$=` + } + let x_val = x[${t.indicesToOffset("xIndices")}]; + ${i} + `,` + ${e.registerUniforms(d).declareVariables(t,g)} + + ${e.mainStart()} + ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + let indices = ${g.offsetToIndices("global_idx")}; + var xIndices = ${g.offsetToIndices("global_idx")}; + + var offsets: array; + + var value = ${b}(${u}); + var pad = 0; + var isPad = false; + + for (var i: u32 = 0u; i < uniforms.kernelSize; i++) { + var offset = i; + for (var j = 0u; j < ${_-1}u; j++) { + offsets[j] = offset / ${F("uniforms.kernelStrides","j",_)}; + offset -= offsets[j] * ${F("uniforms.kernelStrides","j",_)}; + } + offsets[${_-1}] = offset; + + isPad = false; + for (var j = ${r-_}u; j < ${r}u; j++) { + xIndices[j] = indices[j] * ${F("uniforms.strides",`j - ${r-_}u`,_)} + + offsets[j - ${r-_}u] - ${F("uniforms.pads","j - 2u",S)}; + ${$} + } + ${a} + + output[global_idx] = value; + }`}},sl=e=>`${e.format};${e.ceilMode};${e.autoPad};${e.kernelShape.length}`,Ch=e=>`${sl(e)};${e.countIncludePad}`,Ah=e=>`${sl(e)};${e.storageOrder};${e.dilations}`,ul=e=>({format:e.format,autoPad:["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][e.auto_pad],ceilMode:e.ceil_mode,kernelShape:e.kernel_shape,strides:e.strides,pads:e.pads}),dl=(e,t,r,n)=>{let[o,i]=ol(t,n,r),a=P("x",t.dataType,t.dims.length),u=a.type.value,d="value += x_val;",c="";o.countIncludePad?c+=`value /= ${u}(uniforms.kernelSize);`:c+=`value /= ${u}(i32(uniforms.kernelSize) - pad);`;let[p,m,f,b,g]=il(i,o);p.push(...N(t.dims,i));let _=["rank"];return{name:e,shaderCache:{hint:`${n.cacheKey};${f};${b};${g}`,inputDependencies:_},getRunData:()=>({outputs:[{dims:i,dataType:t.dataType}],dispatchGroup:{x:Math.ceil(k.size(i)/64)},programUniforms:p}),getShaderSource:S=>al(S,a,t.dims.length,i.length,o,d,c,0,m,f,b,g)}},ll=e=>{let t=e.count_include_pad!==0,r=ul(e);if(r.ceilMode!==0)throw new Error("using ceil() in shape computation is not yet supported for AveragePool");let n={countIncludePad:t,...r,cacheKey:""};return{...n,cacheKey:Ch(n)}},cl=(e,t)=>{rn(e.inputs),e.compute(dl("AveragePool",e.inputs[0],!1,t))},pl={autoPad:"",ceilMode:0,countIncludePad:!1,kernelShape:[],strides:[],pads:[],storageOrder:0,dilations:[]},ml=e=>{let t=e.format;return{format:t,...pl,cacheKey:t}},fl=(e,t)=>{rn(e.inputs),e.compute(dl("GlobalAveragePool",e.inputs[0],!0,t))},hl=(e,t,r,n)=>{let[o,i]=ol(t,n,r),a=` + value = max(x_val, value); + `,u="",d=P("x",t.dataType,t.dims.length),c=["rank"],[p,m,f,b,g]=il(i,o);return p.push(...N(t.dims,i)),{name:e,shaderCache:{hint:`${n.cacheKey};${f};${b};${g}`,inputDependencies:c},getRunData:()=>({outputs:[{dims:i,dataType:t.dataType}],dispatchGroup:{x:Math.ceil(k.size(i)/64)},programUniforms:p}),getShaderSource:_=>al(_,d,t.dims.length,i.length,o,a,u,t.dataType===10?-65504:-1e5,m,f,b,g)}},gl=(e,t)=>{rn(e.inputs),e.compute(hl("MaxPool",e.inputs[0],!1,t))},bl=e=>{let t=e.storage_order,r=e.dilations,n=ul(e);if(t!==0)throw new Error("column major storage order is not yet supported for MaxPool");if(n.ceilMode!==0)throw new Error("using ceil() in shape computation is not yet supported for MaxPool");let o={storageOrder:t,dilations:r,...n,cacheKey:""};return{...o,cacheKey:Ah(o)}},yl=e=>{let t=e.format;return{format:t,...pl,cacheKey:t}},_l=(e,t)=>{rn(e.inputs),e.compute(hl("GlobalMaxPool",e.inputs[0],!0,t))}});var kh,Ph,vl,$l,xl=U(()=>{"use strict";ee();ne();Se();ie();kh=(e,t)=>{if(e.length<2||e.length>3)throw new Error("DequantizeLinear requires 2 or 3 inputs.");if(e.length===3&&e[1].dims===e[2].dims)throw new Error("x-scale and x-zero-point must have the same shape.");if(e.length===3&&e[0].dataType!==e[2].dataType)throw new Error("x and x-zero-point must have the same data type.");if(e[0].dataType===6&&e.length>2)throw new Error("In the case of dequantizing int32 there is no zero point.");if(e[1].dims.length!==0&&e[1].dims.length!==1&&e[1].dims.length!==e[0].dims.length)throw new Error("scale input must be a scalar, a 1D tensor, or have the same rank as the input tensor.");if(e.length>2){if(e[0].dataType!==e[2].dataType)throw new Error("x and x-zero-point must have the same data type.");if(e[1].dims.length!==e[2].dims.length)throw new Error("scale and zero-point inputs must have the same rank.");if(!e[1].dims.map((r,n)=>r===e[2].dims[n]).reduce((r,n)=>r&&n,!0))throw new Error("scale and zero-point inputs must have the same shape.")}if(t.blockSize>0){if(e[1].dims.length===0||e[1].dims.length===1&&e[1].dims[0]===1)throw new Error("blockSize must be set only for block quantization.");if(!e[1].dims.map((o,i)=>i===t.axis||o===e[0].dims[i]).reduce((o,i)=>o&&i,!0))throw new Error("For block qunatization, scale input shape to match the input shape except for the axis");if(e[1].dims.length!==e[0].dims.length)throw new Error("For block qunatization the scale input rank must be the same as the x rank.");let r=e[0].dims[t.axis],n=e[1].dims[t.axis];if(t.blockSizeMath.ceil(r/(n-1)-1))throw new Error("blockSize must be with in the range [ceil(dI / Si), ceil(dI / (Si - 1) - 1)].")}},Ph=(e,t)=>{let r=k.normalizeAxis(t.axis,e[0].dims.length),n=e[0].dataType,o=n===3,i=e[0].dims,a=e[1].dataType,u=k.size(i),d=n===3||n===2,c=d?[Math.ceil(k.size(e[0].dims)/4)]:e[0].dims,p=e[1].dims,m=e.length>2?e[2]:void 0,f=m?d?[Math.ceil(k.size(m.dims)/4)]:m.dims:void 0,b=p.length===0||p.length===1&&p[0]===1,g=b===!1&&p.length===1,_=ce(u),S=b&&(!d||_===4),$=S?_:1,v=S&&!d?_:1,x=P("input",d?12:n,c.length,v),T=P("scale",a,p.length),E=m?P("zero_point",d?12:n,f.length):void 0,I=M("output",a,i.length,$),z=[x,T];E&&z.push(E);let O=[c,p];m&&O.push(f);let D=[{type:12,data:u/$},{type:12,data:r},{type:12,data:t.blockSize},...N(...O,i)],L=q=>{let Q=[{name:"output_size",type:"u32"},{name:"axis",type:"u32"},{name:"block_size",type:"u32"}];return` + ${q.registerUniforms(Q).declareVariables(...z,I)} + ${q.mainStart()} + ${q.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + let output_indices = ${I.offsetToIndices("global_idx")}; + + // Set input x + ${d?` + let input = ${x.getByOffset("global_idx / 4")}; + let x_vec = ${o?"unpack4xI8(input)":"unpack4xU8(input)"}; + let x_value = ${$===1?"x_vec[global_idx % 4]":"x_vec"};`:`let x_value = ${x.getByOffset("global_idx")};`}; + + // Set scale input + ${b?`let scale_value= ${T.getByOffset("0")}`:g?` + let scale_index = ${I.indicesGet("output_indices","uniforms.axis")}; + let scale_value= ${T.getByOffset("scale_index")};`:` + var scale_indices: ${T.type.indices} = output_indices; + let index = ${T.indicesGet("scale_indices","uniforms.axis")} / uniforms.block_size; + ${T.indicesSet("scale_indices","uniforms.axis","index")}; + let scale_value= ${T.getByIndices("scale_indices")};`}; + + // Set zero-point input + ${E?b?d?` + let zero_point_input = ${E.getByOffset("0")}; + let zero_point_vec = ${o?"unpack4xI8(zero_point_input)":"unpack4xU8(zero_point_input)"}; + let zero_point_value= zero_point_vec[0]`:`let zero_point_value = ${E.getByOffset("0")}`:g?d?` + let zero_point_index = ${I.indicesGet("output_indices","uniforms.axis")}; + let zero_point_input = ${E.getByOffset("zero_point_index / 4")}; + let zero_point_vec = ${o?"unpack4xI8(zero_point_input)":"unpack4xU8(zero_point_input)"}; + let zero_point_value = zero_point_vec[zero_point_index % 4]`:` + let zero_point_index = ${I.indicesGet("output_indices","uniforms.axis")}; + let zero_point_value = ${E.getByOffset("zero_point_index")};`:d?` + let zero_point_offset = ${T.indicesToOffset("scale_indices")}; + let zero_point_input = ${E.getByOffset("zero_point_offset / 4")}; + let zero_point_vec = ${o?"unpack4xI8(zero_point_input)":"unpack4xU8(zero_point_input)"}; + let zero_point_value = zero_point_vec[zero_point_offset % 4];`:`let zero_point_value = ${E.getByIndices("scale_indices")};`:`let zero_point_value = ${d?o?"i32":"u32":x.type.value}(0);`}; + // Compute and write output + ${I.setByOffset("global_idx",`${I.type.value}(x_value - zero_point_value) * scale_value`)}; + }`};return{name:"DequantizeLinear",shaderCache:{hint:t.cacheKey,inputDependencies:E?["rank","rank","rank"]:["rank","rank"]},getShaderSource:L,getRunData:()=>({outputs:[{dims:i,dataType:a}],dispatchGroup:{x:Math.ceil(u/$/64),y:1,z:1},programUniforms:D})}},vl=(e,t)=>{kh(e.inputs,t),e.compute(Ph(e.inputs,t))},$l=e=>J({axis:e.axis,blockSize:e.blockSize})});var zh,Oh,Sl,Tl=U(()=>{"use strict";We();ee();ie();zh=(e,t,r)=>{let n=e===t,o=et&&r>0;if(n||o||i)throw new Error("Range these inputs' contents are invalid.")},Oh=(e,t,r,n)=>{let o=Math.abs(Math.ceil((t-e)/r)),i=[o],a=o,u=[{type:12,data:a},{type:n,data:e},{type:n,data:r},...N(i)],d=c=>{let p=M("output",n,i.length),m=p.type.value,f=[{name:"outputSize",type:"u32"},{name:"start",type:m},{name:"delta",type:m}];return` + ${c.registerUniforms(f).declareVariables(p)} + ${c.mainStart()} + ${c.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + output[global_idx] = uniforms.start + ${m}(global_idx) * uniforms.delta; + }`};return{name:"Range",shaderCache:{hint:`${n}`},getShaderSource:d,getRunData:()=>({outputs:[{dims:i,dataType:n}],dispatchGroup:{x:Math.ceil(a/64)},programUniforms:u})}},Sl=e=>{let t=0,r=0,n=0;e.inputs[0].dataType===6?(t=e.inputs[0].getInt32Array()[0],r=e.inputs[1].getInt32Array()[0],n=e.inputs[2].getInt32Array()[0]):e.inputs[0].dataType===1&&(t=e.inputs[0].getFloat32Array()[0],r=e.inputs[1].getFloat32Array()[0],n=e.inputs[2].getFloat32Array()[0]),ge.webgpu.validateInputContent&&zh(t,r,n),e.compute(Oh(t,r,n,e.inputs[0].dataType),{inputs:[]})}});var Bh,Il,Cl,Dh,Al,El,kl=U(()=>{"use strict";ee();ne();Se();ie();Bh=(e,t,r,n)=>{if(e!=="none"&&n!=="i32"&&n!=="u32"&&n!=="f32")throw new Error(`Input ${n} is not supported with reduction ${e}.`);let o=`{ + var oldValue = 0; + loop { + let newValueF32 =`,i=`; + let newValue = bitcast(newValueF32); + let res = atomicCompareExchangeWeak(&${t}, oldValue, newValue); + if res.exchanged { + break; + } + oldValue = res.old_value; + } + }`;switch(e){case"none":return`${t}=${r};`;case"add":return n==="i32"||n==="u32"?`atomicAdd(&${t}, bitcast<${n}>(${r}));`:` + ${o}bitcast<${n}>(oldValue) + (${r})${i}`;case"max":return n==="i32"||n==="u32"?`atomicMax(&${t}, bitcast<${n}>(${r}));`:` + ${o}max(bitcast(oldValue), (${r}))${i}`;case"min":return n==="i32"||n==="u32"?`atomicMin(&${t}, bitcast<${n}>(${r}));`:`${o}min(bitcast<${n}>(oldValue), (${r}))${i}`;case"mul":return`${o}(bitcast<${n}>(oldValue) * (${r}))${i}`;default:throw new Error(`Reduction ${e} is not supported.`)}},Il=(e,t)=>`${e===1?` + let element_count_dim = uniforms.output_strides; + let dim_value = uniforms.output_shape;`:` + let element_count_dim = uniforms.output_strides[${t?"i - indices_start":"i"}]; + let dim_value = uniforms.output_shape[${t?"i - indices_start":"i"} + uniforms.last_index_dimension];`} + + if (index >= 0) { + if (index >= i32(dim_value)) { + index = i32(dim_value - 1); + } + } else { + if (index < -i32(dim_value)) { + index = 0; + } else { + index += i32(dim_value); + } + } + data_offset += u32((u32(index) * element_count_dim));`,Cl=(e,t,r)=>`for (var i = 0u; i < uniforms.num_updates_elements; i++) { + let value = updates[uniforms.num_updates_elements * ${r?"global_idx":"idx"} + i]; + ${Bh(e.reduction,"output[data_offset + i]","value",t)} + }`,Dh=(e,t)=>{let r=e[0].dims,n=e[1].dims,o=r,i=1,a=Math.ceil(k.size(n)/i),u=n[n.length-1],d=k.sizeFromDimension(r,u),c=k.sizeFromDimension(n,0)/u,p=[{type:12,data:a},{type:12,data:u},{type:12,data:d},...N(e[1].dims,e[2].dims,o)],m=f=>{let b=P("indices",e[1].dataType,e[1].dims.length),g=P("updates",e[2].dataType,e[2].dims.length,i),_=t.reduction!=="none"&&t.reduction!==""?es("output",e[0].dataType,o.length):M("output",e[0].dataType,o.length,i);return` + ${f.registerUniform("output_size","u32").registerUniform("last_index_dimension","u32").registerUniform("num_updates_elements","u32").declareVariables(b,g,_)} + ${f.mainStart()} + ${f.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + var hasDuplicates = false; + if (${t.reduction==="none"}) { + for (var i = 0; i < ${c}; i = i + 1) { + for (var j = i + 1; j < ${c}; j = j + 1) { + var index_i = i32(indices[i].x); + var index_j = i32(indices[j].x); + if (index_i == index_j) { + hasDuplicates = true; + break; + } + } + if (hasDuplicates) { + break; + } + } + } + + if (${t.reduction==="none"} && hasDuplicates) { + if (global_idx != 0u) { + return; + } + // Process each index-update pair individually when duplicates exist + for (var idx = 0u; idx < ${c}u; idx++) { + var data_offset = 0u; + for (var i = 0u; i < uniforms.last_index_dimension; i++) { + var index = i32(indices[idx * uniforms.last_index_dimension + i].x); + ${Il(r.length,!1)} + } + ${Cl(t,_.type.value,!1)} + } + return; + } + + var data_offset = 0u; + var indices_start = uniforms.last_index_dimension * global_idx; + var indices_end = indices_start + uniforms.last_index_dimension; + for (var i = indices_start; i < indices_end; i++) { + var index = i32(indices[i].x); + ${Il(r.length,!0)} + } + ${Cl(t,_.type.value,!0)} + }`};return{name:"ScatterND",shaderCache:{hint:`${t.cacheKey}_${t.reduction}`,inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:o,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(a/64)},programUniforms:p}),getShaderSource:m}},Al=e=>J({reduction:e.reduction}),El=(e,t)=>{e.compute(Dh(e.inputs,t),{inputs:[e.inputs[1],e.inputs[2]],outputs:[]})}});var Mh,Rh,Uh,Pl,Nh,Vh,Wh,Lh,Gh,Hh,Fh,qh,zl,jh,Kh,Zh,Qh,Yh,Ol,Bl,Dl=U(()=>{"use strict";ee();ne();Se();ie();Mh=(e,t)=>{if(e.every(r=>r>0||(()=>{throw new Error("Resize requires scales input values to be positive")})),e.length>0){if(t.mode==="linear"){if(!(e.length===2||e.length===3||e.length===4&&e[0]===1&&e[1]===1||e.length===4&&e[0]===1&&e[3]===1||e.length===5&&e[0]===1&&e[1]===1))throw new Error(`For linear mode, Resize requires scales to be 2D, 3D, 4D with either two outermost or one innermost and + one outermost scale values equal to 1, or 5D with two outermost scale values equal to 1`)}else if(t.mode==="cubic"&&!(e.length===2||e.length===4&&e[0]===1&&e[1]===1||e.length===4&&e[0]===1&&e[3]===1))throw new Error("Resize requires scales input size to be 2 or 4 for cubic mode")}},Rh=(e,t,r)=>{t.every(o=>o>=0&&o{throw new Error("Resize requires axes input values to be positive and less than rank")}));let n=new Array(r).fill(1);return t.forEach((o,i)=>n[o]=e[i]),n},Uh=(e,t,r,n,o,i)=>{let[a,u,d]=r>10?[1,2,3]:[-1,e.length>1?1:-1,-1],c=e[0].dims.length;if(a>0&&e.length>a&&e[a].dims.length>0)e[a].getFloat32Array().forEach(p=>i.push(p));else if(t.coordinateTransformMode==="tf_crop_and_resize")throw new Error("Resize requires RoI input to be specified when coordinateTransformMode is tfCropAndResize");if(u>0&&e.length>u&&e[u].dims.length===1&&e[u].dims[0]>0){if(e[u].getFloat32Array().forEach(p=>n.push(p)),n.length!==0&&n.length!==c&&r>=18&&n.length!==t.axes.length)throw new Error("Resize requires scales input size to be same as input rank or axes size for opset 18 and up");Mh(n,t),t.axes.length>0&&Rh(n,t.axes,c).forEach((p,m)=>n[m]=p)}if(d>0&&e.length>d&&e[d].dims.length===1&&e[d].dims[0]>0&&(e[d].getBigInt64Array().forEach(p=>o.push(Number(p))),o.length!==0&&o.length!==c&&r>=18&&o.length!==t.axes.length))throw new Error("Resize requires sizes input size to be same as input rank or axes size for opset 18 and up");if(t.axes.length>0){if(n.length!==0&&n.length!==t.axes.length)throw new Error('Resize requires "scales" input size to be of axes rank when axes attributes is specified');if(o.length!==0&&o.length!==t.axes.length)throw new Error('Resize requires "sizes" input size to be of rank axes rank when axes attributes is specified')}if(typeof n<"u"&&typeof o<"u"&&n.length>0&&o.length>c)throw new Error("Resize requires only of scales or sizes to be specified")},Pl=(e,t,r,n)=>` + // The whole part and the fractional part are calculated separately due to inaccuracy of floating + // point division. As an example, f32(21) / f32(7) may evaluate to 2.99... instead of 3, causing an + // offset-by-one error later in floor(). + let big = (${e}) * (${t}); + let whole = ${n}(big / (${r})); + let fract = ${n}(big % (${r})) / ${n}(${r}); + return whole + fract; +`,Nh=(e,t)=>`fn getOriginalCoordinateFromResizedCoordinate(xResized: u32, xScale: f32, lengthResized: u32, + lengthOriginal: u32, roiStart: f32, roiEnd: f32) -> ${t} { `+(()=>{switch(e){case"asymmetric":return` + if (xScale < 1.0 || floor(xScale) != xScale) { + return ${t}(xResized) / ${t}(xScale); + } else { + ${Pl("xResized","lengthOriginal","lengthResized",t)} + } + `;case"pytorch_half_pixel":return`if (lengthResized > 1) { + return (${t}(xResized) + 0.5) / ${t}(xScale) - 0.5; + } else { + return 0.0; + }`;case"tf_half_pixel_for_nn":return`return (${t}(xResized) + 0.5) / ${t}(xScale);`;case"align_corners":return`if (lengthResized == 1) { + return 0.0; + } else { + ${Pl("xResized","lengthOriginal - 1","lengthResized - 1",t)} + }`;case"tf_crop_and_resize":return`if (lengthResized > 1) { + return ${t}(roiStart) * ${t}(lengthOriginal - 1) + + (${t}(xResized) * ${t}(roiEnd - roiStart) * ${t}(lengthOriginal - 1)) / + ${t}(lengthResized - 1); + } else { + return 0.5 * ${t}(roiStart + roiEnd) * ${t}(lengthOriginal - 1); + }`;case"half_pixel_symmetric":return`const outputWidth = ${t}xScale * ${t}(lengthResized); + const adjustment = ${t}(lengthResized) / outputWidth; + const center = ${t}(lengthOriginal) / 2; + const offset = center * (1 - adjustment); + return offset + ((${t}(xResized) + 0.5) / ${t}(xScale)) - 0.5;`;case"half_pixel":return`return ((${t}(xResized) + 0.5) / ${t}(xScale)) - 0.5;`;default:throw new Error(`Coordinate transform mode ${e} is not supported`)}})()+"}",Vh=(e,t,r)=>`fn getNearestPixelFromOriginal(xOriginal: ${r}, isDownSample: bool) -> ${r} {`+(()=>{switch(e){case"round_prefer_ceil":return"if (fract(xOriginal) == 0.5) { return ceil(xOriginal); } else { return round(xOriginal); }";case"floor":return"return floor(xOriginal);";case"ceil":return"return ceil(xOriginal);";case"round_prefer_floor":return"if (fract(xOriginal) == 0.5) { return floor(xOriginal); } else { return round(xOriginal); }";case"simple":default:if(t<11)return"if (isDownSample) { return ceil(xOriginal); } else { return xOriginal; }";throw new Error(`Nearest mode ${e} is not supported`)}})()+"}",Wh=(e,t,r)=>{let n=new Array(r).fill(0).concat(new Array(r).fill(1)),o=e.length===0?n:e.slice();return t.length>0?(t.forEach((i,a)=>{n[i]=o[a],n[a+r]=o[t.length+a]}),n):o},Lh=(e,t,r,n)=>{let o=[];if(r.length>0)if(n.length>0){if(e.forEach(i=>o.push(i)),Math.max(...n)>e.length)throw new Error("axes is out of bound");n.forEach((i,a)=>o[i]=r[a])}else r.forEach(i=>o.push(i));else{if(t.length===0)throw new Error("Resize requires either scales or sizes.");o=e.map((i,a)=>Math.round(i*t[a]))}return o},Gh=(e,t,r)=>{let n=(()=>{switch(r.keepAspectRatioPolicy){case"not_larger":return r.axes.length>0?Math.min(...r.axes.map(i=>t[i]),Number.MAX_VALUE):Math.min(...t,Number.MAX_VALUE);case"not_smaller":return r.axes.length>0?Math.max(...r.axes.map(i=>t[i]),Number.MIN_VALUE):Math.max(...t,Number.MIN_VALUE);default:throw new Error(`Keep aspect ratio policy ${r.keepAspectRatioPolicy} is not supported`)}})();t.fill(1,0,t.length);let o=e.slice();return r.axes.length>0?(r.axes.forEach(i=>t[i]=n),r.axes.forEach(i=>o[i]=Math.round(e[i]*t[i]))):(t.fill(n,0,t.length),o.forEach((i,a)=>o[a]=Math.round(i*t[a]))),o},Hh=(e,t,r,n,o)=>` + fn calculateOriginalIndicesFromOutputIndices(output_indices: ${e.type.indices}) -> array<${e.type.value}, ${r.length}> { + var original_indices: array<${e.type.value}, ${r.length}>; + for (var i:u32 = 0; i < ${r.length}; i++) { + var output_index = ${e.indicesGet("output_indices","i")}; + var scale = ${F("uniforms.scales","i",n)}; + var roi_low = ${F("uniforms.roi","i",o)}; + var roi_hi = ${F("uniforms.roi",`i + ${t.length}`,o)}; + if (scale == 1.0) { + original_indices[i] = ${e.type.value}(output_index); + } else { + var input_shape_i = ${F("uniforms.input_shape","i",t.length)}; + var output_shape_i = ${F("uniforms.output_shape","i",r.length)}; + original_indices[i] = getOriginalCoordinateFromResizedCoordinate(output_index, scale, output_shape_i, + input_shape_i, roi_low, roi_hi); + } + } + return original_indices; + }`,Fh=(e,t,r,n,o,i,a)=>` + fn calculateInputIndicesFromOutputIndices(output_indices: ${t.type.indices}) -> ${e.type.indices} { + var input_indices: ${e.type.indices}; + for (var i:u32 = 0; i < ${n.length}; i++) { + var output_index = ${t.indicesGet("output_indices","i")}; + var input_index: u32; + var scale = ${F("uniforms.scales","i",o)}; + if (scale == 1.0) { + input_index = output_index; + } else { + var roi_low = ${F("uniforms.roi","i",i)}; + var roi_hi = ${F("uniforms.roi",`i + ${r.length}`,i)}; + var input_shape_i = ${F("uniforms.input_shape","i",r.length)}; + var output_shape_i = ${F("uniforms.output_shape","i",n.length)}; + var original_idx = getOriginalCoordinateFromResizedCoordinate(output_index, scale, output_shape_i, + input_shape_i, roi_low, roi_hi); + if (!${a} || (original_idx >= 0 && original_idx < ${t.type.value}(input_shape_i))) { + if (original_idx < 0) { + input_index = 0; + } else if (original_idx > ${t.type.value}(input_shape_i - 1)) { + input_index = input_shape_i - 1; + } else { + input_index = u32(getNearestPixelFromOriginal(original_idx, scale < 1)); + } + } else { + input_index = u32(original_idx); + } + } + ${e.indicesSet("input_indices","i","input_index")} + } + return input_indices; + }`,qh=(e,t)=>` + fn checkInputIndices(input_indices: ${e.type.indices}) -> bool { + for (var i:u32 = 0; i < ${t.length}; i++) { + var input_index = ${e.indicesGet("input_indices","i")}; + if (input_index < 0 || input_index >= ${F("uniforms.input_shape","i",t.length)}) { + return false; + } + } + return true; + }`,zl=(e,t,r,n)=>e.rank>n?` + ${e.indicesSet("input_indices",t,"channel")}; + ${e.indicesSet("input_indices",r,"batch")}; +`:"",jh=(e,t,r,n,o)=>{let[a,u,d,c]=r.length===2?[-1,0,1,-1]:[0,2,3,1],p=e.type.value;return` + fn getInputValue(batch: u32, channel: u32, row: u32, col: u32) -> ${p} { + var input_indices: ${e.type.indices}; + ${e.indicesSet("input_indices",u,`max(0, min(row, ${r[u]} - 1))`)}; + ${e.indicesSet("input_indices",d,`max(0, min(col, ${r[d]} - 1))`)}; + ${zl(e,c,a,2)} + return ${e.getByIndices("input_indices")}; + } + + fn bilinearInterpolation(output_indices: ${t.type.indices}) -> ${p} { + var originalIndices = calculateOriginalIndicesFromOutputIndices(output_indices); + var row:${p} = originalIndices[${u}]; + var col:${p} = originalIndices[${d}]; + ${n?`if (row < 0 || row > (${r[u]} - 1) || col < 0 || col > (${r[d]} - 1)) { + return ${o}; + }`:""}; + row = max(0, min(row, ${r[u]} - 1)); + col = max(0, min(col, ${r[d]} - 1)); + var row1: u32 = u32(row); + var col1: u32 = u32(col); + var row2: u32 = u32(row + 1); + var col2: u32 = u32(col + 1); + var channel: u32 = ${r.length>2?`u32(originalIndices[${c}])`:"0"}; + var batch: u32 = ${r.length>2?`u32(originalIndices[${a}])`:"0"}; + var x11: ${p} = getInputValue(batch, channel, row1, col1); + var x12: ${p} = getInputValue(batch, channel, row1, col2); + var x21: ${p} = getInputValue(batch, channel, row2, col1); + var x22: ${p} = getInputValue(batch, channel, row2, col2); + var dx1: ${p} = abs(row - ${p}(row1)); + var dx2: ${p} = abs(${p}(row2) - row); + var dy1: ${p} = abs(col - ${p}(col1)); + var dy2: ${p} = abs(${p}(col2) - col); + if (row1 == row2) { + dx1 = 0.5; + dx2 = 0.5; + } + if (col1 == col2) { + dy1 = 0.5; + dy2 = 0.5; + } + return (x11 * dx2 * dy2 + x12 * dx2 * dy1 + x21 * dx1 * dy2 + x22 * dx1 * dy1); + }`},Kh=(e,t,r,n,o,i,a,u,d,c)=>{let p=r.length===2,m=!0,[f,b]=p?[0,1]:m?[2,3]:[1,2],g=e.type.value,_=S=>{let $=S===f?"row":"col";return` + fn ${$}CubicInterpolation(input_indices: ${e.type.indices}, output_indices: ${t.type.indices}) -> ${g} { + var output_index = ${t.indicesGet("output_indices",S)}; + var originalIdx: ${g} = getOriginalCoordinateFromResizedCoordinate(output_index, ${o[S]}, + ${n[S]}, ${r[S]}, ${i[S]}, ${i[S]} + ${r.length}); + var fractOriginalIdx: ${g} = originalIdx - floor(originalIdx); + var coefs = getCubicInterpolationCoefs(fractOriginalIdx); + + if (${u} && (originalIdx < 0 || originalIdx > (${r[S]} - 1))) { + return ${d}; + } + var data: array<${g}, 4> = array<${g}, 4>(0.0, 0.0, 0.0, 0.0); + for (var i: i32 = -1; i < 3; i++) { + var ${$}: ${g} = originalIdx + ${g}(i); + if (${$} < 0 || ${$} >= ${r[S]}) { + ${c?`coefs[i + 1] = 0.0; + continue;`:u?`return ${d};`:`${$} = max(0, min(${$}, ${r[S]} - 1));`}; + } + var input_indices_copy: ${e.type.indices} = input_indices; + ${e.indicesSet("input_indices_copy",S,`u32(${$})`)}; + data[i + 1] = ${S===f?e.getByIndices("input_indices_copy"):"rowCubicInterpolation(input_indices_copy, output_indices)"}; + } + return cubicInterpolation1D(data, coefs); + }`};return` + ${_(f)}; + ${_(b)}; + fn getCubicInterpolationCoefs(s: ${g}) -> array<${g}, 4> { + var absS = abs(s); + var coeffs: array<${g}, 4> = array<${g}, 4>(0.0, 0.0, 0.0, 0.0); + var oneMinusAbsS: ${g} = 1.0 - absS; + var twoMinusAbsS: ${g} = 2.0 - absS; + var onePlusAbsS: ${g} = 1.0 + absS; + coeffs[0] = ((${a} * onePlusAbsS - 5 * ${a}) * onePlusAbsS + 8 * ${a}) * onePlusAbsS - 4 * ${a}; + coeffs[1] = ((${a} + 2) * absS - (${a} + 3)) * absS * absS + 1; + coeffs[2] = ((${a} + 2) * oneMinusAbsS - (${a} + 3)) * oneMinusAbsS * oneMinusAbsS + 1; + coeffs[3] = ((${a} * twoMinusAbsS - 5 * ${a}) * twoMinusAbsS + 8 * ${a}) * twoMinusAbsS - 4 * ${a}; + return coeffs; + } + + fn cubicInterpolation1D(x: array<${g}, 4>, coefs: array<${g}, 4>) -> ${g} { + var coefsSum: ${g} = coefs[0] + coefs[1] + coefs[2] + coefs[3]; + return (x[0] * coefs[0] + x[1] * coefs[1]+ x[2] * coefs[2]+ x[3] * coefs[3]) / coefsSum; + } + + fn bicubicInterpolation(output_indices: ${t.type.indices}) -> ${g} { + var input_indices: ${e.type.indices} = output_indices; + return colCubicInterpolation(input_indices, output_indices); + } + `},Zh=(e,t,r,n,o)=>{let[a,u,d,c,p]=r.length===3?[-1,0,1,2,-1]:[0,2,3,4,1],m=e.type.value;return` + fn getInputValue(batch: u32, channel: u32, depth:u32, height: u32, width: u32) -> ${m} { + var input_indices: ${e.type.indices}; + ${e.indicesSet("input_indices",u,`max(0, min(depth, ${r[u]} - 1))`)}; + ${e.indicesSet("input_indices",d,`max(0, min(height, ${r[d]} - 1))`)}; + ${e.indicesSet("input_indices",c,`max(0, min(width, ${r[c]} - 1))`)}; + ${zl(e,p,a,3)} + return ${e.getByIndices("input_indices")}; + } + + fn trilinearInterpolation(output_indices: ${t.type.indices}) -> ${m} { + var originalIndices = calculateOriginalIndicesFromOutputIndices(output_indices); + var depth:${m} = originalIndices[${u}]; + var height:${m} = originalIndices[${d}]; + var width:${m} = originalIndices[${c}]; + ${n?`if (depth < 0 || depth > (${r[u]} - 1) || height < 0 || height > (${r[d]} - 1) || width < 0 || (width > ${r[c]} - 1)) { + return ${o}; + }`:""}; + + depth = max(0, min(depth, ${r[u]} - 1)); + height = max(0, min(height, ${r[d]} - 1)); + width = max(0, min(width, ${r[c]} - 1)); + var depth1: u32 = u32(depth); + var height1: u32 = u32(height); + var width1: u32 = u32(width); + var depth2: u32 = u32(depth + 1); + var height2: u32 = u32(height + 1); + var width2: u32 = u32(width + 1); + var channel: u32 = ${r.length>3?`u32(originalIndices[${p}])`:"0"}; + var batch: u32 = ${r.length>3?`u32(originalIndices[${a}])`:"0"}; + + var x111: ${m} = getInputValue(batch, channel, depth1, height1, width1); + var x112: ${m} = getInputValue(batch, channel, depth1, height1, width2); + var x121: ${m} = getInputValue(batch, channel, depth1, height2, width1); + var x122: ${m} = getInputValue(batch, channel, depth1, height2, width2); + var x211: ${m} = getInputValue(batch, channel, depth2, height1, width1); + var x212: ${m} = getInputValue(batch, channel, depth2, height1, width2); + var x221: ${m} = getInputValue(batch, channel, depth2, height2, width1); + var x222: ${m} = getInputValue(batch, channel, depth2, height2, width2); + var dx1: ${m} = abs(depth - ${m}(depth1)); + var dx2: ${m} = abs(${m}(depth2) - depth); + var dy1: ${m} = abs(height - ${m}(height1)); + var dy2: ${m} = abs(${m}(height2) - height); + var dz1: ${m} = abs(width - ${m}(width1)); + var dz2: ${m} = abs(${m}(width2) - width); + if (depth1 == depth2) { + dx1 = 0.5; + dx2 = 0.5; + } + if (height1 == height2) { + dy1 = 0.5; + dy2 = 0.5; + } + if (width1 == width2) { + dz1 = 0.5; + dz2 = 0.5; + } + return (x111 * dx2 * dy2 * dz2 + x112 * dx2 * dy2 * dz1 + x121 * dx2 * dy1 *dz2 + x122 * dx2 * dy1 * dz1 + + x211 * dx1 * dy2 * dz2 + x212 * dx1 * dy2 * dz1 + x221 * dx1 * dy1 *dz2 + x222 * dx1 * dy1 * dz1); + }`},Qh=(e,t,r,n,o,i)=>{let a=e.dims,u=Wh(i,t.axes,a.length),d=Lh(a,n,o,t.axes),c=n.slice();n.length===0&&(c=a.map((v,x)=>v===0?1:d[x]/v),t.keepAspectRatioPolicy!=="stretch"&&(d=Gh(a,c,t)));let p=M("output",e.dataType,d.length),m=P("input",e.dataType,a.length),f=k.size(d),b=a.length===d.length&&a.every((v,x)=>v===d[x]),g=t.coordinateTransformMode==="tf_crop_and_resize",_=t.extrapolationValue,S=m.type.value,$=v=>` + ${b?"":` + ${Nh(t.coordinateTransformMode,S)}; + ${(()=>{switch(t.mode){case"nearest":return` + ${qh(m,a)}; + ${Vh(t.nearestMode,r,S)}; + ${Fh(m,p,a,d,c.length,u.length,g)}; + `;case"linear":return` + ${Hh(p,a,d,c.length,u.length)}; + ${(()=>{if(a.length===2||a.length===4)return`${jh(m,p,a,g,_)}`;if(a.length===3||a.length===5)return`${Zh(m,p,a,g,_)}`;throw Error("Linear mode only supports input dims 2, 3, 4 and 5 are supported in linear mode.")})()}; + `;case"cubic":return` + ${(()=>{if(a.length===2||a.length===4)return`${Kh(m,p,a,d,c,u,t.cubicCoeffA,g,t.extrapolationValue,t.excludeOutside)}`;throw Error("Cubic mode only supports input dims 2 and 4 are supported in linear mode.")})()}; + `;default:throw Error("Invalid resize mode")}})()}; + `} + ${v.registerUniform("output_size","u32").registerUniform("scales","f32",c.length).registerUniform("roi","f32",u.length).declareVariables(m,p)} + ${v.mainStart()} + ${v.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + ${b?"output[global_idx] = input[global_idx];":` + let output_indices = ${p.offsetToIndices("global_idx")}; + var input_indices: ${m.type.indices}; + ${(()=>{switch(t.mode){case"nearest":return`input_indices = calculateInputIndicesFromOutputIndices(output_indices); + if (checkInputIndices(input_indices)) { + output[global_idx] = ${m.getByIndices("input_indices")}; + } else { + output[global_idx] = ${t.extrapolationValue}; + }`;case"linear":return`output[global_idx] = ${a.length===2||a.length===4?"bilinearInterpolation":"trilinearInterpolation"}(output_indices);`;case"cubic":return"output[global_idx] = bicubicInterpolation(output_indices);";default:throw Error(`Unsupported resize mode: ${t.mode}`)}})()}; +`} + }`;return{name:"Resize",shaderCache:{hint:`${t.cacheKey}|${r}|${c.length>0?t.mode==="cubic"?c:c.length:""}|${o.length>0?o:""}|${u.length>0?u:""}|${b}|${t.mode==="nearest"?a.length:a}`,inputDependencies:["rank"]},getShaderSource:$,getRunData:()=>({outputs:[{dims:d,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(f/64)},programUniforms:[{type:12,data:f},{type:1,data:c},{type:1,data:u},...N(a,d)]})}},Yh=e=>{let t=e.customDataBuffer;return new Uint32Array(t,t.byteOffset,1)[0]},Ol=(e,t)=>{let r=[],n=[],o=[],i=Yh(e);if(t.antialias!==0)throw Error("Only default value (0) for Antialias attribute is supported");Uh(e.inputs,t,i,r,n,o),e.compute(Qh(e.inputs[0],t,i,r,n,o),{inputs:[0]})},Bl=e=>{let t=e.antialias,r=e.axes,n=e.coordinateTransformMode,o=e.cubicCoeffA,i=e.excludeOutside!==0,a=e.extrapolationValue,u=e.keepAspectRatioPolicy,d=e.mode,c=e.nearestMode===""?"simple":e.nearestMode;return J({antialias:t,axes:r,coordinateTransformMode:n,cubicCoeffA:o,excludeOutside:i,extrapolationValue:a,keepAspectRatioPolicy:u,mode:d,nearestMode:c})}});var Xh,Jh,Ml,Rl=U(()=>{"use strict";ee();ne();ie();Xh=e=>{if(!e||e.length<3)throw new Error("layerNorm requires at least 3 inputs.");let t=e[0],r=e[1],n=e[2];if(t.dataType!==r.dataType||t.dataType!==n.dataType)throw new Error("All inputs must have the same data type");if(t.dims.length!==3&&t.dims.length!==2)throw new Error("Input must be 2D or 3D");if(r.dims.length!==3&&r.dims.length!==2)throw new Error("Skip must be 2D or 3D");let o=t.dims[t.dims.length-1],i=t.dims[t.dims.length-2];if(r.dims[r.dims.length-1]!==o)throw new Error("Skip must have the same hidden size as input");if(r.dims[r.dims.length-2]!==i)throw new Error("Skip must have the same sequence length as input");if(n.dims.length!==1)throw new Error("Gamma must be 1D");if(n.dims[n.dims.length-1]!==o)throw new Error("Gamma must have the same hidden size as input");if(e.length>3){let a=e[3];if(a.dims.length!==1)throw new Error("Beta must be 1D");if(a.dims[a.dims.length-1]!==o)throw new Error("Beta must have the same hidden size as input")}if(e.length>4){let a=e[4];if(a.dims.length!==1)throw new Error("Bias must be 1D");if(a.dims[a.dims.length-1]!==o)throw new Error("Bias must have the same hidden size as input")}},Jh=(e,t,r,n)=>{let o=t.simplified,i=e[0].dims,a=k.size(i),u=i,d=a,c=i.slice(-1)[0],p=n?i.slice(0,-1).concat(1):[],m=!o&&e.length>3,f=e.length>4,b=n&&r>1,g=n&&r>2,_=r>3,S=64,$=ce(c),v=[{type:12,data:d},{type:12,data:$},{type:12,data:c},{type:1,data:t.epsilon}],x=E=>{let I=[{name:"output_size",type:"u32"},{name:"components",type:"u32"},{name:"hidden_size",type:"u32"},{name:"epsilon",type:"f32"}],z=[P("x",e[0].dataType,e[0].dims,$),P("skip",e[1].dataType,e[1].dims,$),P("gamma",e[2].dataType,e[2].dims,$)];m&&z.push(P("beta",e[3].dataType,e[3].dims,$)),f&&z.push(P("bias",e[4].dataType,e[4].dims,$)),z.push(M("output",e[0].dataType,u,$)),b&&z.push(M("mean_output",1,p)),g&&z.push(M("inv_std_output",1,p)),_&&z.push(M("input_skip_bias_sum",e[0].dataType,u,$));let O=be(e[0].dataType),D=be(1,$);return` + + ${E.registerUniforms(I).declareVariables(...z)} + var sum_shared : array<${D}, ${S}>; + var sum_squared_shared : array<${D}, ${S}>; + + ${E.mainStart([S,1,1])} + let ix = local_id.x; + let iy = global_id.x / ${S}; + + let hidden_size_vectorized: u32 = uniforms.hidden_size / uniforms.components; + var stride = hidden_size_vectorized / ${S}; + let offset = ix * stride + iy * hidden_size_vectorized; + let offset1d = stride * ix; + if (ix == ${S-1}) { + stride = hidden_size_vectorized - stride * ix; + } + for (var i: u32 = 0; i < stride; i++) { + let skip_value = skip[offset + i]; + let bias_value = ${f?"bias[offset1d + i]":O+"(0.0)"}; + let input_value = x[offset + i]; + let value = input_value + skip_value + bias_value; + ${_?"input_skip_bias_sum[offset + i] = value;":""} + output[offset + i] = value; + let f32_value = ${Ct(O,$,"value")}; + sum_shared[ix] += f32_value; + sum_squared_shared[ix] += f32_value * f32_value; + } + workgroupBarrier(); + + var reduce_size : u32 = ${S}; + for (var curr_size = reduce_size >> 1; curr_size > 0; curr_size = reduce_size >> 1) { + reduce_size = curr_size + (reduce_size & 1); + if (ix < curr_size) { + sum_shared[ix] += sum_shared[ix + reduce_size]; + sum_squared_shared[ix] += sum_squared_shared[ix + reduce_size]; + } + workgroupBarrier(); + } + + let sum = sum_shared[0]; + let square_sum = sum_squared_shared[0]; + let mean = ${He("sum",$)} / f32(uniforms.hidden_size); + let inv_std_dev = inverseSqrt(${He("square_sum",$)} / f32(uniforms.hidden_size) ${o?"":"- mean * mean"} + uniforms.epsilon); + ${b?"mean_output[global_idx] = mean;":""} + ${g?"inv_std_output[global_idx] = inv_std_dev;":""} + + for (var i: u32 = 0; i < stride; i++) { + output[offset + i] = (output[offset + i] ${o?"":`- ${O}(mean)`}) * + ${O}(inv_std_dev) * gamma[offset1d + i] + ${m?"+ beta[offset1d + i]":""}; + } + }`},T=[{dims:u,dataType:e[0].dataType}];return r>1&&T.push({dims:p,dataType:1}),r>2&&T.push({dims:p,dataType:1}),r>3&&T.push({dims:i,dataType:e[0].dataType}),{name:"SkipLayerNormalization",shaderCache:{hint:`${$};${b};${g};${_}`,inputDependencies:e.map((E,I)=>"type")},getShaderSource:x,getRunData:()=>({outputs:T,dispatchGroup:{x:Math.ceil(d/c)},programUniforms:v})}},Ml=(e,t)=>{Xh(e.inputs);let n=[0];e.outputCount>1&&n.push(-3),e.outputCount>2&&n.push(-3),e.outputCount>3&&n.push(3),e.compute(Jh(e.inputs,t,e.outputCount,!1),{outputs:n})}});var eg,nn,tg,Ul,rg,ng,Nl,Vl,Wl=U(()=>{"use strict";ee();ne();Se();ie();eg=(e,t)=>{if(!e||e.length<1)throw new Error("too few inputs");if(t.axes.length!==0){if(t.axes.length!==t.starts.length||t.axes.length!==t.ends.length)throw new Error("axes, starts and ends must have the same length")}else if(t.starts.length!==t.ends.length)throw new Error("starts and ends must have the same length");e.slice(1).forEach((r,n)=>{if(e[n+1].dataType!==6&&e[n+1].dataType!==7)throw new Error(`Input ${n} must be an array of int32 or int64`)})},nn=(e,t)=>{let r=[];if(e.length>t)if(e[t].dataType===7)e[t].getBigInt64Array().forEach(n=>r.push(Number(n)));else if(e[t].dataType===6)e[t].getInt32Array().forEach(n=>r.push(Number(n)));else throw new Error(`Input ${t} must be an array of int32 or int64`);return r},tg=(e,t)=>{if(e.length>1){let r=nn(e,1),n=nn(e,2),o=nn(e,3);return o.length===0&&(o=[...Array(e[0].dims.length).keys()]),J({starts:r,ends:n,axes:o})}else return t},Ul=(e,t,r,n,o)=>{let i=e;return e<0&&(i+=r[n[t]]),o[t]<0?Math.max(0,Math.min(i,r[n[t]]-1)):Math.max(0,Math.min(i,r[n[t]]))},rg=(e,t,r)=>`fn calculateInputIndices(output_indices: ${t.type.indices}) -> ${e.type.indices} { + var input_indices: ${e.type.indices}; + var carry = 0u; + for (var i = ${r.length}; i >= 0; i--) { + let input_shape_i = ${F("uniforms.input_shape","i",r.length)}; + let steps_i = ${F("uniforms.steps","i",r.length)}; + let signs_i = ${F("uniforms.signs","i",r.length)}; + let starts_i = ${F("uniforms.starts","i",r.length)}; + var output_index = ${t.indicesGet("output_indices","i")}; + var input_index = output_index * steps_i + starts_i + carry; + carry = input_index / input_shape_i; + input_index = input_index % input_shape_i; + if (signs_i < 0) { + input_index = input_shape_i - input_index - 1u + starts_i; + } + ${e.indicesSet("input_indices","i","input_index")}; + } + return input_indices; + }`,ng=(e,t)=>{let r=e[0].dims,n=k.size(r),o=t.axes.length>0?k.normalizeAxes(t.axes,r.length):[...Array(r.length).keys()],i=nn(e,4);i.forEach($=>$!==0||(()=>{throw new Error("step cannot be 0")})),i.length===0&&(i=Array(o.length).fill(1));let a=t.starts.map(($,v)=>Ul($,v,r,o,i)),u=t.ends.map(($,v)=>Ul($,v,r,o,i));if(o.length!==a.length||o.length!==u.length)throw new Error("start, ends and axes should have the same number of elements");if(o.length!==r.length)for(let $=0;$Math.sign($));i.forEach(($,v,x)=>{if($<0){let T=(u[v]-a[v])/$,E=a[v],I=E+T*i[v];a[v]=I,u[v]=E,x[v]=-$}});let c=r.slice(0);o.forEach(($,v)=>{c[$]=Math.ceil((u[$]-a[$])/i[$])});let p={dims:c,dataType:e[0].dataType},m=M("output",e[0].dataType,c.length),f=P("input",e[0].dataType,e[0].dims.length),b=k.size(c),g=[{name:"outputSize",type:"u32"},{name:"starts",type:"u32",length:a.length},{name:"signs",type:"i32",length:d.length},{name:"steps",type:"u32",length:i.length}],_=[{type:12,data:b},{type:12,data:a},{type:6,data:d},{type:12,data:i},...N(e[0].dims,c)],S=$=>` + ${$.registerUniforms(g).declareVariables(f,m)} + ${rg(f,m,r)} + ${$.mainStart()} + ${$.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + let output_indices = ${m.offsetToIndices("global_idx")}; + let input_indices = calculateInputIndices(output_indices); + ${m.setByOffset("global_idx",f.getByIndices("input_indices"))} + }`;return{name:"Slice",shaderCache:{hint:`${d.length}_${a.length}_${i.length}`,inputDependencies:["rank"]},getShaderSource:S,getRunData:()=>({outputs:[p],dispatchGroup:{x:Math.ceil(n/64)},programUniforms:_})}},Nl=(e,t)=>{eg(e.inputs,t);let r=tg(e.inputs,t);e.compute(ng(e.inputs,r),{inputs:[0]})},Vl=e=>{let t=e.starts,r=e.ends,n=e.axes;return J({starts:t,ends:r,axes:n})}});var og,ig,Ll,Gl,Hl=U(()=>{"use strict";ee();ne();Se();st();ie();og=e=>{if(!e||e.length!==1)throw new Error("Softmax op requires 1 input.")},ig=(e,t)=>{let r=e.inputs[0],n=r.dims,o=k.size(n),i=n.length,a=k.normalizeAxis(t.axis,i),u=aO),c[a]=i-1,c[i-1]=a,d=e.compute(Ee(r,c),{inputs:[r],outputs:[-1]})[0]):d=r;let p=d.dims,m=p[i-1],f=o/m,b=ce(m),g=m/b,_=64;f===1&&(_=256);let S=(z,O)=>O===4?`max(max(${z}.x, ${z}.y), max(${z}.z, ${z}.w))`:O===2?`max(${z}.x, ${z}.y)`:O===3?`max(max(${z}.x, ${z}.y), ${z}.z)`:z,$=P("x",d.dataType,d.dims,b),v=M("result",d.dataType,d.dims,b),x=$.type.value,T=be(d.dataType)==="f32"?`var threadMax = ${x}(-3.402823e+38f);`:`var threadMax = ${x}(-65504.0h);`,E=z=>` + var rowMaxShared : ${x}; + var rowSumShared : ${x}; + var threadShared : array<${x}, ${_}>; + + fn getValue(row: i32, col: i32, row_stride: i32) -> ${x} { + let index = row * row_stride + col; + return x[index]; + } + + fn setValue(row: i32, col: i32, row_stride: i32, value: ${x}) { + let index = row * row_stride + col; + result[index] = value; + } + ${z.registerUniform("packedCols","i32").declareVariables($,v)} + ${z.mainStart(_)} + let gindex = i32(global_idx); + let lindex = i32(local_idx); + const wg = ${_}; + let row = gindex / wg; + let cols = uniforms.packedCols; + let row_stride : i32 = uniforms.packedCols; + + // find the rows max + ${T} + for (var col = lindex; col < cols; col += wg) { + let value = getValue(row, col, row_stride); + threadMax = max(threadMax, value); + } + if (lindex < cols) { + threadShared[lindex] = threadMax; + } + workgroupBarrier(); + + var reduceSize = min(cols, wg); + for (var currSize = reduceSize >> 1; currSize > 0; currSize = reduceSize >> 1) { + reduceSize = currSize + (reduceSize & 1); + if (lindex < currSize) { + threadShared[lindex] = max(threadShared[lindex], threadShared[lindex + reduceSize]); + } + workgroupBarrier(); + } + if (lindex == 0) { + rowMaxShared = ${x}(${S("threadShared[0]",b)}); + } + workgroupBarrier(); + + // find the rows sum + var threadSum = ${x}(0.0); + for (var col = lindex; col < cols; col += wg) { + let subExp = exp(getValue(row, col, row_stride) - rowMaxShared); + threadSum += subExp; + } + threadShared[lindex] = threadSum; + workgroupBarrier(); + + for (var currSize = wg >> 1; currSize > 0; currSize = currSize >> 1) { + if (lindex < currSize) { + threadShared[lindex] = threadShared[lindex] + threadShared[lindex + currSize]; + } + workgroupBarrier(); + } + if (lindex == 0) { + rowSumShared = ${x}(${He("threadShared[0]",b)}); + } + workgroupBarrier(); + + // calculate final value for each element in the row + for (var col = lindex; col < cols; col += wg) { + let value = exp(getValue(row, col, row_stride) - rowMaxShared) / rowSumShared; + setValue(row, col, row_stride, value); + } + }`,I=e.compute({name:"Softmax",shaderCache:{hint:`${b};${_}`,inputDependencies:["type"]},getRunData:()=>({outputs:[{dims:p,dataType:d.dataType}],dispatchGroup:{x:f},programUniforms:[{type:6,data:g}]}),getShaderSource:E},{inputs:[d],outputs:[u?-1:0]})[0];u&&e.compute(Ee(I,c),{inputs:[I]})},Ll=(e,t)=>{og(e.inputs),ig(e,t)},Gl=e=>J({axis:e.axis})});var Fl,ag,sg,ug,ql,jl=U(()=>{"use strict";ee();ne();ie();Fl=e=>Array.from(e.getBigInt64Array(),Number),ag=e=>{if(!e||e.length!==2)throw new Error("Tile requires 2 inputs.");if(e[0].dataType!==1&&e[0].dataType!==10&&e[0].dataType!==6&&e[0].dataType!==12)throw new Error("Tile only support float, float16, int32, and uint32 data types");if(e[1].dataType!==7)throw new Error("Tile `repeats` input should be of int64 data type");if(e[1].dims.length!==1)throw new Error("Tile `repeats` input should be 1-D");if(Fl(e[1]).length!==e[0].dims.length)throw new Error("Tile `repeats` input should have same number of elements as rank of input data tensor")},sg=(e,t)=>{let r=[];for(let n=0;n{let r=e[0].dims,n=t??Fl(e[1]),o=sg(r,n),i=k.size(o),a=e[0].dataType,u=P("input",a,r.length),d=M("output",a,o.length),c=p=>` + const inputShape = ${u.indices(...r)}; + ${p.registerUniform("output_size","u32").declareVariables(u,d)} + ${p.mainStart()} + ${p.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + let output_indices = ${d.offsetToIndices("global_idx")}; + var input_indices: ${u.type.indices}; + for (var i = 0; i < ${r.length}; i++) { + let input_dim_i = ${u.indicesGet("uniforms.input_shape","i")}; + let input_dim_value = ${d.indicesGet("output_indices","i")} % input_dim_i; + + ${u.indicesSet("input_indices","i","input_dim_value")} + } + ${d.setByOffset("global_idx",u.getByIndices("input_indices"))} + }`;return{name:"Tile",shaderCache:{hint:`${n}`,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:o,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(i/64)},programUniforms:[{type:12,data:i},...N(e[0].dims,o)]}),getShaderSource:c}},ql=e=>{ag(e.inputs),e.compute(ug(e.inputs),{inputs:[0]})}});var dg,lg,Kl,Zl=U(()=>{"use strict";ee();ne();ie();dg=(e,t,r,n,o)=>{let i=M("output_data",o,r.length,4),a=P("a_data",t[1].dataType,t[1].dims.length,4),u=P("b_data",t[2].dataType,t[2].dims.length,4),d=P("c_data",t[0].dataType,t[0].dims.length,4),c,p=(m,f,b)=>`select(${f}, ${m}, ${b})`;if(!n)c=i.setByOffset("global_idx",p(a.getByOffset("global_idx"),u.getByOffset("global_idx"),d.getByOffset("global_idx")));else{let m=(f,b,g="")=>{let _=`a_data[index_a${b}][component_a${b}]`,S=`b_data[index_b${b}][component_b${b}]`,$=`bool(c_data[index_c${b}] & (0xffu << (component_c${b} * 8)))`;return` + let output_indices${b} = ${i.offsetToIndices(`global_idx * 4u + ${b}u`)}; + let offset_a${b} = ${a.broadcastedIndicesToOffset(`output_indices${b}`,i)}; + let offset_b${b} = ${u.broadcastedIndicesToOffset(`output_indices${b}`,i)}; + let offset_c${b} = ${d.broadcastedIndicesToOffset(`output_indices${b}`,i)}; + let index_a${b} = offset_a${b} / 4u; + let index_b${b} = offset_b${b} / 4u; + let index_c${b} = offset_c${b} / 4u; + let component_a${b} = offset_a${b} % 4u; + let component_b${b} = offset_b${b} % 4u; + let component_c${b} = offset_c${b} % 4u; + ${f}[${b}] = ${g}(${p(_,S,$)}); + `};o===9?c=` + var data = vec4(0); + ${m("data",0,"u32")} + ${m("data",1,"u32")} + ${m("data",2,"u32")} + ${m("data",3,"u32")} + output_data[global_idx] = dot(vec4(0x1, 0x100, 0x10000, 0x1000000), vec4(data));`:c=` + ${m("output_data[global_idx]",0)} + ${m("output_data[global_idx]",1)} + ${m("output_data[global_idx]",2)} + ${m("output_data[global_idx]",3)} + `}return` + ${e.registerUniform("vec_size","u32").declareVariables(d,a,u,i)} + ${e.mainStart()} + ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")} + ${c} + }`},lg=e=>{let t=e[1].dims,r=e[2].dims,n=e[0].dims,o=e[1].dataType,i=!(k.areEqual(t,r)&&k.areEqual(r,n)),a=t,u=k.size(t);if(i){let c=Je.calcShape(Je.calcShape(t,r,!1),n,!1);if(!c)throw new Error("Can't perform where op on the given tensors");a=c,u=k.size(a)}let d=Math.ceil(u/4);return{name:"Where",shaderCache:{inputDependencies:["rank","rank","rank"]},getShaderSource:c=>dg(c,e,a,i,o),getRunData:()=>({outputs:[{dims:a,dataType:o}],dispatchGroup:{x:Math.ceil(u/64/4)},programUniforms:[{type:12,data:d},...N(n,t,r,a)]})}},Kl=e=>{e.compute(lg(e.inputs))}});var Ql,Yl=U(()=>{"use strict";Es();Fr();zs();Bs();_u();ku();Ou();Zu();rd();id();ud();md();gd();yd();vd();Sd();Cd();kd();Od();Md();Fd();Kd();Qd();Xd();tl();$o();nl();wl();xl();Tl();kl();Gr();Dl();To();Rl();Wl();Hl();So();jl();st();jr();Zl();Ql=new Map([["Abs",[Ds]],["Acos",[Ms]],["Acosh",[Rs]],["Add",[wu]],["ArgMax",[As,uo]],["ArgMin",[Cs,uo]],["Asin",[Us]],["Asinh",[Ns]],["Atan",[Vs]],["Atanh",[Ws]],["Attention",[ks]],["AveragePool",[cl,ll]],["BatchNormalization",[Ps]],["BiasAdd",[Os]],["BiasSplitGelu",[yu]],["Cast",[Gs,Ls]],["Ceil",[Fs]],["Clip",[Hs]],["Concat",[Pu,zu]],["Conv",[yo,bo]],["ConvTranspose",[td,Ju]],["Cos",[qs]],["Cosh",[js]],["CumSum",[nd,od]],["DepthToSpace",[ad,sd]],["DequantizeLinear",[vl,$l]],["Div",[vu]],["Einsum",[cd,pd]],["Elu",[Ks,Yt]],["Equal",[$u]],["Erf",[Zs]],["Exp",[Qs]],["Expand",[hd]],["FastGelu",[bd]],["Floor",[Ys]],["FusedConv",[yo,bo]],["Gather",[wd,_d]],["GatherElements",[Ed,Ad]],["GatherBlockQuantized",[Td,Id]],["GatherND",[$d,xd]],["Gelu",[Xs]],["Gemm",[zd,Pd]],["GlobalAveragePool",[fl,ml]],["GlobalMaxPool",[_l,yl]],["Greater",[Iu]],["GreaterOrEqual",[Au]],["GridSample",[Bd,Dd]],["GroupQueryAttention",[Hd]],["HardSigmoid",[au,iu]],["InstanceNormalization",[jd]],["LayerNormalization",[Zd]],["LeakyRelu",[Js,Yt]],["Less",[Cu]],["LessOrEqual",[Eu]],["Log",[hu]],["MatMul",[Yd]],["MatMulNBits",[Jd,el]],["MaxPool",[gl,bl]],["Mul",[xu]],["MultiHeadAttention",[Nd,Ud]],["Neg",[tu]],["Not",[eu]],["Pad",[rl]],["Pow",[Su]],["QuickGelu",[gu,Yt]],["Range",[Sl]],["Reciprocal",[ru]],["ReduceMin",[vs]],["ReduceMean",[gs]],["ReduceMax",[ws]],["ReduceSum",[xs]],["ReduceProd",[$s]],["ReduceL1",[bs]],["ReduceL2",[ys]],["ReduceLogSum",[Ts]],["ReduceLogSumExp",[_s]],["ReduceSumSquare",[Ss]],["Relu",[nu]],["Resize",[Ol,Bl]],["RotaryEmbedding",[Ld]],["ScatterND",[El,Al]],["Sigmoid",[ou]],["Sin",[su]],["Sinh",[uu]],["Slice",[Nl,Vl]],["SkipLayerNormalization",[Ml]],["Split",[Vd,Wd]],["Sqrt",[du]],["Softmax",[Ll,Gl]],["Sub",[Tu]],["Tan",[lu]],["Tanh",[pu]],["ThresholdedRelu",[fu,Yt]],["Tile",[ql]],["Transpose",[ns,os]],["Where",[Kl]]])});var on,Xl=U(()=>{"use strict";We();Xe();ie();on=class{constructor(t){this.backend=t;this.repo=new Map,this.attributesBound=!1}getArtifact(t){return this.repo.get(t)}setArtifact(t,r){this.repo.set(t,r)}run(t,r,n,o,i){Re(t.programInfo.name);let a=this.backend.device,u=this.backend.getComputePassEncoder();this.backend.writeTimestamp(this.backend.pendingDispatchNumber*2);let d=[];for(let p of r)d.push({binding:d.length,resource:{buffer:p.buffer}});for(let p of n)d.push({binding:d.length,resource:{buffer:p.buffer}});i&&d.push({binding:d.length,resource:i});let c=a.createBindGroup({layout:t.computePipeline.getBindGroupLayout(0),entries:d,label:t.programInfo.name});if(this.backend.sessionStatus==="capturing"){let p={kernelId:this.backend.currentKernelId,computePipeline:t.computePipeline,bindGroup:c,dispatchGroup:o};this.backend.capturedCommandList.get(this.backend.currentSessionId).push(p)}u.setPipeline(t.computePipeline),u.setBindGroup(0,c),u.dispatchWorkgroups(...o),this.backend.writeTimestamp(this.backend.pendingDispatchNumber*2+1),this.backend.pendingDispatchNumber++,(this.backend.pendingDispatchNumber>=this.backend.maxDispatchNumber||this.backend.queryType==="at-passes")&&this.backend.endComputePass(),this.backend.pendingDispatchNumber>=this.backend.maxDispatchNumber&&this.backend.flush(),Oe(t.programInfo.name)}dispose(){}build(t,r){Re(t.name);let n=this.backend.device,o=[];[{feature:"shader-f16",extension:"f16"},{feature:"subgroups",extension:"subgroups"}].forEach(m=>{n.features.has(m.feature)&&o.push(`enable ${m.extension};`)});let a=ts(r,this.backend.device.limits),u=t.getShaderSource(a),d=`${o.join(` +`)} +${a.additionalImplementations} +${u}`,c=n.createShaderModule({code:d,label:t.name});se("verbose",()=>`[WebGPU] ${t.name} shader code: ${d}`);let p=n.createComputePipeline({compute:{module:c,entryPoint:"main"},layout:"auto",label:t.name});return Oe(t.name),{programInfo:t,computePipeline:p,uniformVariablesInfo:a.variablesInfo}}normalizeDispatchGroupSize(t){let r=typeof t=="number"?t:t.x,n=typeof t=="number"?1:t.y||1,o=typeof t=="number"?1:t.z||1,i=this.backend.device.limits.maxComputeWorkgroupsPerDimension;if(r<=i&&n<=i&&o<=i)return[r,n,o];let a=r*n*o,u=Math.ceil(Math.sqrt(a));if(u>i){if(u=Math.ceil(Math.cbrt(a)),u>i)throw new Error("Total dispatch size exceeds WebGPU maximum.");return[u,u,u]}else return[u,u,1]}}});var Jl={};Dt(Jl,{WebGpuBackend:()=>Co});var cg,pg,Io,Co,ec=U(()=>{"use strict";We();ee();Xe();Zn();Ja();Yl();Xl();cg=(e,t)=>{if(t.length!==e.length)throw new Error(`inputDependencies length ${t.length} is not equal to inputTensors length ${e.length}.`);let r=[];for(let n=0;n{let n=e.name;return e.shaderCache?.hint&&(n+="["+e.shaderCache.hint+"]"),n+=":"+r+`:${cg(t,e.shaderCache?.inputDependencies??new Array(t.length).fill("dims"))}`,n},Io=class{constructor(t){t&&(this.architecture=t.architecture,this.vendor=t.vendor)}isArchitecture(t){return this.architecture===t}isVendor(t){return this.vendor===t}},Co=class{constructor(){this.currentSessionId=null;this.currentKernelId=null;this.commandEncoder=null;this.computePassEncoder=null;this.maxDispatchNumber=16;this.pendingDispatchNumber=0;this.pendingKernels=[];this.pendingQueries=new Map;this.sessionStatus="default";this.capturedCommandList=new Map;this.capturedPendingKernels=new Map;this.sessionExternalDataMapping=new Map}get currentKernelCustomData(){if(this.currentKernelId===null)throw new Error("currentKernelCustomData(): currentKernelId is null. (should not happen)");let t=this.kernelCustomData.get(this.currentKernelId);return t||(t={},this.kernelCustomData.set(this.currentKernelId,t)),t}async initialize(t,r){this.env=t;let n=[],o={requiredLimits:{maxComputeWorkgroupStorageSize:r.limits.maxComputeWorkgroupStorageSize,maxComputeWorkgroupsPerDimension:r.limits.maxComputeWorkgroupsPerDimension,maxStorageBufferBindingSize:r.limits.maxStorageBufferBindingSize,maxBufferSize:r.limits.maxBufferSize,maxComputeInvocationsPerWorkgroup:r.limits.maxComputeInvocationsPerWorkgroup,maxComputeWorkgroupSizeX:r.limits.maxComputeWorkgroupSizeX,maxComputeWorkgroupSizeY:r.limits.maxComputeWorkgroupSizeY,maxComputeWorkgroupSizeZ:r.limits.maxComputeWorkgroupSizeZ},requiredFeatures:n},i=a=>r.features.has(a)&&n.push(a)&&!0;i("chromium-experimental-timestamp-query-inside-passes")||i("timestamp-query"),i("shader-f16"),i("subgroups"),this.device=await r.requestDevice(o),this.adapterInfo=new Io(r.info||await r.requestAdapterInfo()),this.gpuDataManager=Xa(this),this.programManager=new on(this),this.kernels=new Map,this.kernelPersistentData=new Map,this.kernelCustomData=new Map,Br(t.logLevel,!!t.debug),this.device.onuncapturederror=a=>{a.error instanceof GPUValidationError&&console.error(`An uncaught WebGPU validation error was raised: ${a.error.message}`)},Object.defineProperty(this.env.webgpu,"device",{value:this.device,writable:!1,enumerable:!0,configurable:!1}),Object.defineProperty(this.env.webgpu,"adapter",{value:r,writable:!1,enumerable:!0,configurable:!1}),this.setQueryType()}dispose(){typeof this.querySet<"u"&&this.querySet.destroy(),this.gpuDataManager.dispose()}getCommandEncoder(){return this.commandEncoder||(this.commandEncoder=this.device.createCommandEncoder()),this.commandEncoder}getComputePassEncoder(){if(!this.computePassEncoder){let t=this.getCommandEncoder(),r={};this.queryType==="at-passes"&&(r.timestampWrites={querySet:this.querySet,beginningOfPassWriteIndex:this.pendingDispatchNumber*2,endOfPassWriteIndex:this.pendingDispatchNumber*2+1}),this.computePassEncoder=t.beginComputePass(r)}return this.computePassEncoder}endComputePass(){this.computePassEncoder&&(this.computePassEncoder.end(),this.computePassEncoder=null)}flush(){if(!this.commandEncoder)return;Re(),this.endComputePass();let t;this.queryType!=="none"&&(this.commandEncoder.resolveQuerySet(this.querySet,0,this.pendingDispatchNumber*2,this.queryResolveBuffer,0),t=this.device.createBuffer({size:this.pendingDispatchNumber*2*8,usage:GPUBufferUsage.MAP_READ|GPUBufferUsage.COPY_DST}),this.pendingQueries.set(t,this.pendingKernels),this.pendingKernels=[],this.commandEncoder.copyBufferToBuffer(this.queryResolveBuffer,0,t,0,this.pendingDispatchNumber*2*8)),this.device.queue.submit([this.commandEncoder.finish()]),this.gpuDataManager.refreshPendingBuffers(),this.commandEncoder=null,this.pendingDispatchNumber=0,this.queryType!=="none"&&t.mapAsync(GPUMapMode.READ).then(()=>{let r=new BigUint64Array(t.getMappedRange()),n=this.pendingQueries.get(t);for(let o=0;o"u"&&(this.queryTimeBase=b);let _=Number(b-this.queryTimeBase),S=Number(g-this.queryTimeBase);if(!Number.isSafeInteger(_)||!Number.isSafeInteger(S))throw new RangeError("incorrect timestamp range");if(this.env.webgpu.profiling?.ondata)this.env.webgpu.profiling.ondata({version:1,inputsMetadata:m.map($=>({dims:$.dims,dataType:Ye($.dataType)})),outputsMetadata:f.map($=>({dims:$.dims,dataType:Ye($.dataType)})),kernelId:a,kernelType:d,kernelName:c,programName:p,startTime:_,endTime:S});else{let $="";m.forEach((x,T)=>{$+=`input[${T}]: [${x.dims}] | ${Ye(x.dataType)}, `});let v="";f.forEach((x,T)=>{v+=`output[${T}]: [${x.dims}] | ${Ye(x.dataType)}, `}),console.log(`[profiling] kernel "${a}|${d}|${c}|${p}" ${$}${v}execution time: ${S-_} ns`)}gr("GPU",`${p}::${b}::${g}`)}t.unmap(),this.pendingQueries.delete(t)}),Oe()}run(t,r,n,o,i,a){Re(t.name);let u=[];for(let x=0;xT):n;if(m.length!==d.length)throw new Error(`Output size ${m.length} must be equal to ${d.length}.`);let f=[],b=[];for(let x=0;x=a)throw new Error(`Invalid output index: ${m[x]}`);if(m[x]===-3)continue;let T=m[x]===-1,E=m[x]===-2,I=T||E?i(d[x].dataType,d[x].dims):o(m[x],d[x].dataType,d[x].dims);if(f.push(I),I.data===0)continue;let z=this.gpuDataManager.get(I.data);if(!z)throw new Error(`no GPU data for output: ${I.data}`);if(T&&this.temporaryData.push(z),E){let O=this.kernelPersistentData.get(this.currentKernelId);O||(O=[],this.kernelPersistentData.set(this.currentKernelId,O)),O.push(z)}b.push(z)}if(u.length!==r.length||b.length!==f.length){if(b.length===0)return Oe(t.name),f;throw new Error(`Program ${t.name} has zero-sized tensor(s) in inputs or outputs. This is not supported now.`)}let g;if(p){let x=0,T=[];p.forEach(O=>{let D=typeof O.data=="number"?[O.data]:O.data;if(D.length===0)return;let L=O.type===10?2:4,q,Q;O.type===10?(Q=D.length>4?16:D.length>2?8:D.length*L,q=D.length>4?16:L*D.length):(Q=D.length<=2?D.length*L:16,q=16),x=Math.ceil(x/Q)*Q,T.push(x);let W=O.type===10?8:4;x+=D.length>4?Math.ceil(D.length/W)*q:D.length*L});let E=16;x=Math.ceil(x/E)*E;let I=new ArrayBuffer(x);p.forEach((O,D)=>{let L=T[D],q=typeof O.data=="number"?[O.data]:O.data;if(O.type===6)new Int32Array(I,L,q.length).set(q);else if(O.type===12)new Uint32Array(I,L,q.length).set(q);else if(O.type===10)new Uint16Array(I,L,q.length).set(q);else if(O.type===1)new Float32Array(I,L,q.length).set(q);else throw new Error(`Unsupported uniform type: ${Ye(O.type)}`)});let z=this.gpuDataManager.create(x,GPUBufferUsage.COPY_DST|GPUBufferUsage.UNIFORM);this.device.queue.writeBuffer(z.buffer,0,I,0,x),this.gpuDataManager.release(z.id),g={offset:0,size:x,buffer:z.buffer}}let _=this.programManager.normalizeDispatchGroupSize(c),S=_[1]===1&&_[2]===1,$=pg(t,r,S),v=this.programManager.getArtifact($);if(v||(v=this.programManager.build(t,_),this.programManager.setArtifact($,v),se("info",()=>`[artifact] key: ${$}, programName: ${t.name}`)),p&&v.uniformVariablesInfo){if(p.length!==v.uniformVariablesInfo.length)throw new Error(`Uniform variables count mismatch: expect ${v.uniformVariablesInfo.length}, got ${p.length} in program "${v.programInfo.name}".`);for(let x=0;x`[ProgramManager] run "${t.name}" (key=${$}) with ${_[0]}x${_[1]}x${_[2]}`),this.queryType!=="none"||this.sessionStatus==="capturing"){let x={kernelId:this.currentKernelId,programName:v.programInfo.name,inputTensorViews:r,outputTensorViews:f};this.pendingKernels.push(x),this.sessionStatus==="capturing"&&this.capturedPendingKernels.get(this.currentSessionId).push(x)}return this.programManager.run(v,u,b,_,g),Oe(t.name),f}upload(t,r){this.gpuDataManager.upload(t,r)}memcpy(t,r){this.gpuDataManager.memcpy(t,r)}async download(t,r){await this.gpuDataManager.download(t,r)}alloc(t){return this.gpuDataManager.create(t).id}free(t){return this.gpuDataManager.release(t)}createKernel(t,r,n,o){let i=Ql.get(t);if(!i)throw new Error(`kernel not implemented: ${t}`);let a={kernelType:t,kernelName:o,kernelEntry:i[0],attributes:[i[1],n]};this.kernels.set(r,a)}releaseKernel(t){let r=this.kernelPersistentData.get(t);if(r){for(let n of r)this.gpuDataManager.release(n.id);this.kernelPersistentData.delete(t)}this.kernelCustomData.delete(t),this.kernels.delete(t)}computeKernel(t,r,n){let o=this.kernels.get(t);if(!o)throw new Error(`kernel not created: ${t}`);let i=o.kernelType,a=o.kernelName,u=o.kernelEntry,d=o.attributes;if(this.currentKernelId!==null)throw new Error(`kernel "[${i}] ${a}" is not allowed to be called recursively`);this.currentKernelId=t,d[0]&&(d[1]=d[0](d[1]),d[0]=void 0),se("info",()=>`[WebGPU] Start to run kernel "[${i}] ${a}"...`);let c=this.env.debug;this.temporaryData=[];try{return c&&this.device.pushErrorScope("validation"),u(r,d[1]),0}catch(p){return n.push(Promise.resolve(`[WebGPU] Kernel "[${i}] ${a}" failed. ${p}`)),1}finally{c&&n.push(this.device.popErrorScope().then(p=>p?`GPU validation error for kernel "[${i}] ${a}": ${p.message}`:null));for(let p of this.temporaryData)this.gpuDataManager.release(p.id);this.temporaryData=[],this.currentKernelId=null}}registerBuffer(t,r,n,o){let i=this.sessionExternalDataMapping.get(t);i||(i=new Map,this.sessionExternalDataMapping.set(t,i));let a=i.get(r),u=this.gpuDataManager.registerExternalBuffer(n,o,a);return i.set(r,[u,n]),u}unregisterBuffers(t){let r=this.sessionExternalDataMapping.get(t);r&&(r.forEach(n=>this.gpuDataManager.unregisterExternalBuffer(n[0])),this.sessionExternalDataMapping.delete(t))}getBuffer(t){let r=this.gpuDataManager.get(t);if(!r)throw new Error(`no GPU data for buffer: ${t}`);return r.buffer}createDownloader(t,r,n){return async()=>{let o=await ro(this,t,r);return Mr(o.buffer,n)}}writeTimestamp(t){this.queryType==="inside-passes"&&this.computePassEncoder.writeTimestamp(this.querySet,t)}setQueryType(){this.queryType="none",(this.env.webgpu.profiling?.mode==="default"||(typeof this.env.trace>"u"?this.env.wasm.trace:this.env.trace))&&(this.device.features.has("chromium-experimental-timestamp-query-inside-passes")?this.queryType="inside-passes":this.device.features.has("timestamp-query")&&(this.queryType="at-passes"),this.queryType!=="none"&&typeof this.querySet>"u"&&(this.querySet=this.device.createQuerySet({type:"timestamp",count:this.maxDispatchNumber*2}),this.queryResolveBuffer=this.device.createBuffer({size:this.maxDispatchNumber*2*8,usage:GPUBufferUsage.COPY_SRC|GPUBufferUsage.QUERY_RESOLVE})))}captureBegin(){se("info","captureBegin"),this.capturedCommandList.get(this.currentSessionId)||this.capturedCommandList.set(this.currentSessionId,[]),this.capturedPendingKernels.get(this.currentSessionId)||this.capturedPendingKernels.set(this.currentSessionId,[]),this.flush(),this.sessionStatus="capturing"}captureEnd(){se("info","captureEnd"),this.flush(),this.sessionStatus="default"}replay(){se("info","replay"),this.sessionStatus="replaying";let t=this.capturedCommandList.get(this.currentSessionId),r=this.capturedPendingKernels.get(this.currentSessionId),n=t.length;this.pendingKernels=[];for(let o=0;o=this.maxDispatchNumber||this.queryType==="at-passes")&&this.endComputePass(),this.pendingDispatchNumber>=this.maxDispatchNumber&&this.flush()}this.flush(),this.sessionStatus="default"}onCreateSession(){this.gpuDataManager.onCreateSession()}onReleaseSession(t){this.unregisterBuffers(t),this.capturedCommandList.has(t)&&this.capturedCommandList.delete(t),this.capturedPendingKernels.has(t)&&this.capturedPendingKernels.delete(t),this.gpuDataManager.onReleaseSession(t)}onRunStart(t){this.currentSessionId=t,this.setQueryType()}}});var tc={};Dt(tc,{init:()=>mg});var tr,Ao,mg,rc=U(()=>{"use strict";ee();Xe();ne();Ka();tr=class e{constructor(t,r,n,o){this.module=t;this.dataType=r;this.data=n;this.dims=o}getFloat32Array(){if(this.dataType!==1)throw new Error("Invalid data type");let t=k.size(this.dims);return t===0?new Float32Array:new Float32Array(this.module.HEAP8.buffer,this.data,t)}getBigInt64Array(){if(this.dataType!==7)throw new Error("Invalid data type");let t=k.size(this.dims);return t===0?new BigInt64Array:new BigInt64Array(this.module.HEAP8.buffer,this.data,t)}getInt32Array(){if(this.dataType!==6)throw new Error("Invalid data type");let t=k.size(this.dims);return t===0?new Int32Array:new Int32Array(this.module.HEAP8.buffer,this.data,t)}getUint16Array(){if(this.dataType!==10&&this.dataType!==4)throw new Error("Invalid data type");let t=k.size(this.dims);return t===0?new Uint16Array:new Uint16Array(this.module.HEAP8.buffer,this.data,t)}reshape(t){if(k.size(t)!==k.size(this.dims))throw new Error("Invalid new shape");return new e(this.module,this.dataType,this.data,t)}},Ao=class{constructor(t,r,n){this.module=t;this.backend=r;this.customDataOffset=0;this.customDataSize=0;this.adapterInfo=r.adapterInfo;let o=t.PTR_SIZE,i=n/t.PTR_SIZE,a=o===4?"i32":"i64";this.opKernelContext=Number(t.getValue(o*i++,a));let u=Number(t.getValue(o*i++,a));this.outputCount=Number(t.getValue(o*i++,a)),this.customDataOffset=Number(t.getValue(o*i++,"*")),this.customDataSize=Number(t.getValue(o*i++,a));let d=[];for(let c=0;ctypeof u=="number"?this.inputs[u]:u)??this.inputs,o=r?.outputs??[],i=(u,d,c)=>new tr(this.module,d,this.output(u,c),c),a=(u,d)=>{let c=gt(u,d);if(!c)throw new Error(`Unsupported data type: ${u}`);let p=c>0?this.backend.gpuDataManager.create(c).id:0;return new tr(this.module,u,p,d)};return this.backend.run(t,n,o,i,a,this.outputCount)}output(t,r){let n=this.module.stackSave();try{let o=this.module.PTR_SIZE,i=o===4?"i32":"i64",a=this.module.stackAlloc((1+r.length)*o);this.module.setValue(a,r.length,i);for(let u=0;u{let o=t.jsepInit;if(!o)throw new Error("Failed to initialize JSEP. The WebAssembly module is not built with JSEP support.");if(e==="webgpu"){let i=(ec(),Ft(Jl)).WebGpuBackend,a=new i;await a.initialize(r,n),o("webgpu",[a,u=>a.alloc(Number(u)),u=>a.free(u),(u,d,c,p=!1)=>{if(p)se("verbose",()=>`[WebGPU] jsepCopyGpuToGpu: src=${Number(u)}, dst=${Number(d)}, size=${Number(c)}`),a.memcpy(Number(u),Number(d));else{se("verbose",()=>`[WebGPU] jsepCopyCpuToGpu: dataOffset=${Number(u)}, gpuDataId=${Number(d)}, size=${Number(c)}`);let m=t.HEAPU8.subarray(Number(u>>>0),Number(u>>>0)+Number(c));a.upload(Number(d),m)}},async(u,d,c)=>{se("verbose",()=>`[WebGPU] jsepCopyGpuToCpu: gpuDataId=${u}, dataOffset=${d}, size=${c}`),await a.download(Number(u),()=>t.HEAPU8.subarray(Number(d)>>>0,Number(d+c)>>>0))},(u,d,c)=>a.createKernel(u,Number(d),c,t.UTF8ToString(t._JsepGetNodeName(Number(d)))),u=>a.releaseKernel(u),(u,d,c,p)=>{se("verbose",()=>`[WebGPU] jsepRun: sessionHandle=${c}, kernel=${u}, contextDataOffset=${d}`);let m=new Ao(t,a,Number(d));return a.computeKernel(Number(u),m,p)},()=>a.captureBegin(),()=>a.captureEnd(),()=>a.replay()])}else{let i=new Nr(r);o("webnn",[i,()=>i.reserveTensorId(),a=>i.releaseTensorId(a),async(a,u,d,c,p)=>i.ensureTensor(a,u,d,c,p),(a,u)=>{i.uploadTensor(a,u)},async(a,u)=>i.downloadTensor(a,u)])}}});var fg,vr,$r,At,hg,nc,jt,xr,Sr,oc,Tr,Ir,Cr,Vn=U(()=>{"use strict";Ma();Ua();ee();ht();Er();jn();fg=(e,t)=>{fe()._OrtInit(e,t)!==0&&pe("Can't initialize onnxruntime.")},vr=async e=>{fg(e.wasm.numThreads,Zt(e.logLevel))},$r=async(e,t)=>{fe().asyncInit?.();{let r=(rc(),Ft(tc)).init;if(t==="webgpu"){if(typeof navigator>"u"||!navigator.gpu)throw new Error("WebGPU is not supported in current environment");let n=e.webgpu.adapter;if(n){if(typeof n.limits!="object"||typeof n.features!="object"||typeof n.requestDevice!="function")throw new Error("Invalid GPU adapter set in `env.webgpu.adapter`. It must be a GPUAdapter object.")}else{let o=e.webgpu.powerPreference;if(o!==void 0&&o!=="low-power"&&o!=="high-performance")throw new Error(`Invalid powerPreference setting: "${o}"`);let i=e.webgpu.forceFallbackAdapter;if(i!==void 0&&typeof i!="boolean")throw new Error(`Invalid forceFallbackAdapter setting: "${i}"`);if(n=await navigator.gpu.requestAdapter({powerPreference:o,forceFallbackAdapter:i}),!n)throw new Error('Failed to get GPU adapter. You may need to enable flag "--enable-unsafe-webgpu" if you are using Chrome.')}await r("webgpu",fe(),e,n)}if(t==="webnn"){if(typeof navigator>"u"||!navigator.ml)throw new Error("WebNN is not supported in current environment");await r("webnn",fe(),e)}}},At=new Map,hg=e=>{let t=fe(),r=t.stackSave();try{let n=t.PTR_SIZE,o=t.stackAlloc(2*n);t._OrtGetInputOutputCount(e,o,o+n)!==0&&pe("Can't get session input/output count.");let a=n===4?"i32":"i64";return[Number(t.getValue(o,a)),Number(t.getValue(o+n,a))]}finally{t.stackRestore(r)}},nc=(e,t)=>{let r=fe(),n=r.stackSave(),o=0;try{let i=r.PTR_SIZE,a=r.stackAlloc(2*i);r._OrtGetInputOutputMetadata(e,t,a,a+i)!==0&&pe("Can't get session input/output metadata.");let d=Number(r.getValue(a,"*"));o=Number(r.getValue(a+i,"*"));let c=r.HEAP32[o/4];if(c===0)return[d,0];let p=r.HEAPU32[o/4+1],m=[];for(let f=0;f{let t=fe(),r=t._malloc(e.byteLength);if(r===0)throw new Error(`Can't create a session. failed to allocate a buffer of size ${e.byteLength}.`);return t.HEAPU8.set(e,r),[r,e.byteLength]},xr=async(e,t)=>{let r,n,o=fe();Array.isArray(e)?[r,n]=e:e.buffer===o.HEAPU8.buffer?[r,n]=[e.byteOffset,e.byteLength]:[r,n]=jt(e);let i=0,a=0,u=0,d=[],c=[],p=[];try{if([a,d]=await Ra(t),t?.externalData&&o.mountExternalData){let T=[];for(let E of t.externalData){let I=typeof E=="string"?E:E.path;T.push(Qt(typeof E=="string"?E:E.data).then(z=>{o.mountExternalData(I,z)}))}await Promise.all(T)}for(let T of t?.executionProviders??[])if((typeof T=="string"?T:T.name)==="webnn"){if(o.shouldTransferToMLTensor=!1,typeof T!="string"){let I=T,z=I?.context,O=I?.gpuDevice,D=I?.deviceType,L=I?.powerPreference;z?o.currentContext=z:O?o.currentContext=await o.webnnCreateMLContext(O):o.currentContext=await o.webnnCreateMLContext({deviceType:D,powerPreference:L})}else o.currentContext=await o.webnnCreateMLContext();break}i=await o._OrtCreateSession(r,n,a),o.webgpuOnCreateSession?.(i),i===0&&pe("Can't create a session."),o.jsepOnCreateSession?.(),o.currentContext&&(o.webnnRegisterMLContext(i,o.currentContext),o.currentContext=void 0,o.shouldTransferToMLTensor=!0);let[m,f]=hg(i),b=!!t?.enableGraphCapture,g=[],_=[],S=[],$=[],v=[];for(let T=0;TT==="gpu-buffer"||T==="ml-tensor")&&(u=o._OrtCreateBinding(i),u===0&&pe("Can't create IO binding."),x={handle:u,outputPreferredLocations:v,outputPreferredLocationsEncoded:v.map(T=>qn(T))}),At.set(i,[i,c,p,x,b,!1]),[i,g,_,S,$]}catch(m){throw c.forEach(f=>o._OrtFree(f)),p.forEach(f=>o._OrtFree(f)),u!==0&&o._OrtReleaseBinding(u)!==0&&pe("Can't release IO binding."),i!==0&&o._OrtReleaseSession(i)!==0&&pe("Can't release session."),m}finally{o._free(r),a!==0&&o._OrtReleaseSessionOptions(a)!==0&&pe("Can't release session options."),d.forEach(m=>o._free(m)),o.unmountExternalData?.()}},Sr=e=>{let t=fe(),r=At.get(e);if(!r)throw new Error(`cannot release session. invalid session id: ${e}`);let[n,o,i,a,u]=r;a&&(u&&t._OrtClearBoundOutputs(a.handle)!==0&&pe("Can't clear bound outputs."),t._OrtReleaseBinding(a.handle)!==0&&pe("Can't release IO binding.")),t.jsepOnReleaseSession?.(e),t.webnnOnReleaseSession?.(e),t.webgpuOnReleaseSession?.(e),o.forEach(d=>t._OrtFree(d)),i.forEach(d=>t._OrtFree(d)),t._OrtReleaseSession(n)!==0&&pe("Can't release session."),At.delete(e)},oc=async(e,t,r,n,o,i,a=!1)=>{if(!e){t.push(0);return}let u=fe(),d=u.PTR_SIZE,c=e[0],p=e[1],m=e[3],f=m,b,g;if(c==="string"&&(m==="gpu-buffer"||m==="ml-tensor"))throw new Error("String tensor is not supported on GPU.");if(a&&m!=="gpu-buffer")throw new Error(`External buffer must be provided for input/output index ${i} when enableGraphCapture is true.`);if(m==="gpu-buffer"){let $=e[2].gpuBuffer;g=gt(Mt(c),p);{let v=u.jsepRegisterBuffer;if(!v)throw new Error('Tensor location "gpu-buffer" is not supported without using WebGPU.');b=v(n,i,$,g)}}else if(m==="ml-tensor"){let $=e[2].mlTensor;g=gt(Mt(c),p);let v=u.webnnRegisterMLTensor;if(!v)throw new Error('Tensor location "ml-tensor" is not supported without using WebNN.');b=v(n,$,Mt(c),p)}else{let $=e[2];if(Array.isArray($)){g=d*$.length,b=u._malloc(g),r.push(b);for(let v=0;v<$.length;v++){if(typeof $[v]!="string")throw new TypeError(`tensor data at index ${v} is not a string`);u.setValue(b+v*d,Ne($[v],r),"*")}}else{let v=u.webnnIsGraphInput;if(c!=="string"&&v){let x=u.UTF8ToString(o);if(v(n,x)){let T=Mt(c);g=gt(T,p),f="ml-tensor";let E=u.webnnCreateTemporaryTensor,I=u.webnnUploadTensor;if(!E||!I)throw new Error('Tensor location "ml-tensor" is not supported without using WebNN.');let z=await E(n,T,p);I(z,new Uint8Array($.buffer,$.byteOffset,$.byteLength)),b=z}else g=$.byteLength,b=u._malloc(g),r.push(b),u.HEAPU8.set(new Uint8Array($.buffer,$.byteOffset,g),b)}else g=$.byteLength,b=u._malloc(g),r.push(b),u.HEAPU8.set(new Uint8Array($.buffer,$.byteOffset,g),b)}}let _=u.stackSave(),S=u.stackAlloc(4*p.length);try{p.forEach((v,x)=>u.setValue(S+x*d,v,d===4?"i32":"i64"));let $=u._OrtCreateTensor(Mt(c),b,g,S,p.length,qn(f));$===0&&pe(`Can't create tensor for input/output. session=${n}, index=${i}.`),t.push($)}finally{u.stackRestore(_)}},Tr=async(e,t,r,n,o,i)=>{let a=fe(),u=a.PTR_SIZE,d=At.get(e);if(!d)throw new Error(`cannot run inference. invalid session id: ${e}`);let c=d[0],p=d[1],m=d[2],f=d[3],b=d[4],g=d[5],_=t.length,S=n.length,$=0,v=[],x=[],T=[],E=[],I=a.stackSave(),z=a.stackAlloc(_*u),O=a.stackAlloc(_*u),D=a.stackAlloc(S*u),L=a.stackAlloc(S*u);try{[$,v]=Da(i);for(let W=0;W<_;W++)await oc(r[W],x,E,e,p[t[W]],t[W],b);for(let W=0;Wve*$e,1);te=Ye(ye);let ze=f?.outputPreferredLocations[n[W]];if(te==="string"){if(ze==="gpu-buffer"||ze==="ml-tensor")throw new Error("String tensor is not supported on GPU.");let ve=[];for(let $e=0;$e0){let ve=a.jsepGetBuffer;if(!ve)throw new Error('preferredLocation "gpu-buffer" is not supported without using WebGPU.');let $e=ve(X),Ce=gt(ye,de);if(Ce===void 0||!zr(te))throw new Error(`Unsupported data type: ${te}`);j=!0,Q.push([te,V,{gpuBuffer:$e,download:a.jsepCreateDownloader($e,Ce,te),dispose:()=>{a._OrtReleaseTensor(Z)!==0&&pe("Can't release tensor.")}},"gpu-buffer"])}else if(ze==="ml-tensor"&&de>0){let ve=a.webnnEnsureTensor,$e=a.webnnIsInt64Supported;if(!ve||!$e)throw new Error('preferredLocation "ml-tensor" is not supported without using WebNN.');if(gt(ye,de)===void 0||!Or(te))throw new Error(`Unsupported data type: ${te}`);if(te==="int64"&&!$e(e))throw new Error('preferredLocation "ml-tensor" for int64 output is not supported by current WebNN Context.');let _t=await ve(e,X,ye,V,!1);j=!0,Q.push([te,V,{mlTensor:_t,download:a.webnnCreateMLTensorDownloader(X,te),dispose:()=>{a.webnnReleaseTensorId(X),a._OrtReleaseTensor(Z)}},"ml-tensor"])}else{let ve=Pr(te),$e=new ve(de);new Uint8Array($e.buffer,$e.byteOffset,$e.byteLength).set(a.HEAPU8.subarray(X,X+$e.byteLength)),Q.push([te,V,$e,"cpu"])}}finally{a.stackRestore(we),te==="string"&&X&&a._free(X),j||a._OrtReleaseTensor(Z),a.webnnOnRunEnd?.(c)}}return f&&!b&&(a._OrtClearBoundOutputs(f.handle)!==0&&pe("Can't clear bound outputs."),At.set(e,[c,p,m,f,b,!1])),Q}finally{a.stackRestore(I),x.forEach(q=>a._OrtReleaseTensor(q)),T.forEach(q=>a._OrtReleaseTensor(q)),E.forEach(q=>a._free(q)),$!==0&&a._OrtReleaseRunOptions($),v.forEach(q=>a._free(q))}},Ir=e=>{let t=fe(),r=At.get(e);if(!r)throw new Error("invalid session id");let n=r[0],o=t._OrtEndProfiling(n);o===0&&pe("Can't get an profile file name."),t._OrtFree(o)},Cr=e=>{let t=[];for(let r of e){let n=r[2];!Array.isArray(n)&&"buffer"in n&&t.push(n.buffer)}return t}});var Et,Le,rr,sn,un,an,Eo,ko,Vt,Wt,bg,ic,ac,sc,uc,dc,lc,cc,Po=U(()=>{"use strict";We();Vn();ht();_r();Et=()=>!!ge.wasm.proxy&&typeof document<"u",rr=!1,sn=!1,un=!1,ko=new Map,Vt=(e,t)=>{let r=ko.get(e);r?r.push(t):ko.set(e,[t])},Wt=()=>{if(rr||!sn||un||!Le)throw new Error("worker not ready")},bg=e=>{switch(e.data.type){case"init-wasm":rr=!1,e.data.err?(un=!0,Eo[1](e.data.err)):(sn=!0,Eo[0]()),an&&(URL.revokeObjectURL(an),an=void 0);break;case"init-ep":case"copy-from":case"create":case"release":case"run":case"end-profiling":{let t=ko.get(e.data.type);e.data.err?t.shift()[1](e.data.err):t.shift()[0](e.data.out);break}default:}},ic=async()=>{if(!sn){if(rr)throw new Error("multiple calls to 'initWasm()' detected.");if(un)throw new Error("previous call to 'initWasm()' failed.");if(rr=!0,Et())return new Promise((e,t)=>{Le?.terminate(),za().then(([r,n])=>{try{Le=n,Le.onerror=i=>t(i),Le.onmessage=bg,Eo=[e,t];let o={type:"init-wasm",in:ge};!o.in.wasm.wasmPaths&&(r||Gn)&&(o.in.wasm.wasmPaths={wasm:new URL(/* asset import */ __webpack_require__(/*! ort-wasm-simd-threaded.jsep.wasm */ "./node_modules/onnxruntime-web/dist/ort-wasm-simd-threaded.jsep.wasm"), __webpack_require__.b).href}),Le.postMessage(o),an=r}catch(o){t(o)}},t)});try{await wr(ge.wasm),await vr(ge),sn=!0}catch(e){throw un=!0,e}finally{rr=!1}}},ac=async e=>{if(Et())return Wt(),new Promise((t,r)=>{Vt("init-ep",[t,r]);let n={type:"init-ep",in:{epName:e,env:ge}};Le.postMessage(n)});await $r(ge,e)},sc=async e=>Et()?(Wt(),new Promise((t,r)=>{Vt("copy-from",[t,r]);let n={type:"copy-from",in:{buffer:e}};Le.postMessage(n,[e.buffer])})):jt(e),uc=async(e,t)=>{if(Et()){if(t?.preferredOutputLocation)throw new Error('session option "preferredOutputLocation" is not supported for proxy.');return Wt(),new Promise((r,n)=>{Vt("create",[r,n]);let o={type:"create",in:{model:e,options:{...t}}},i=[];e instanceof Uint8Array&&i.push(e.buffer),Le.postMessage(o,i)})}else return xr(e,t)},dc=async e=>{if(Et())return Wt(),new Promise((t,r)=>{Vt("release",[t,r]);let n={type:"release",in:e};Le.postMessage(n)});Sr(e)},lc=async(e,t,r,n,o,i)=>{if(Et()){if(r.some(a=>a[3]!=="cpu"))throw new Error("input tensor on GPU is not supported for proxy.");if(o.some(a=>a))throw new Error("pre-allocated output tensor is not supported for proxy.");return Wt(),new Promise((a,u)=>{Vt("run",[a,u]);let d=r,c={type:"run",in:{sessionId:e,inputIndices:t,inputs:d,outputIndices:n,options:i}};Le.postMessage(c,Cr(d))})}else return Tr(e,t,r,n,o,i)},cc=async e=>{if(Et())return Wt(),new Promise((t,r)=>{Vt("end-profiling",[t,r]);let n={type:"end-profiling",in:e};Le.postMessage(n)});Ir(e)}});var pc,yg,dn,mc=U(()=>{"use strict";We();Po();ee();yr();jn();pc=(e,t)=>{switch(e.location){case"cpu":return[e.type,e.dims,e.data,"cpu"];case"gpu-buffer":return[e.type,e.dims,{gpuBuffer:e.gpuBuffer},"gpu-buffer"];case"ml-tensor":return[e.type,e.dims,{mlTensor:e.mlTensor},"ml-tensor"];default:throw new Error(`invalid data location: ${e.location} for ${t()}`)}},yg=e=>{switch(e[3]){case"cpu":return new Ge(e[0],e[2],e[1]);case"gpu-buffer":{let t=e[0];if(!zr(t))throw new Error(`not supported data type: ${t} for deserializing GPU tensor`);let{gpuBuffer:r,download:n,dispose:o}=e[2];return Ge.fromGpuBuffer(r,{dataType:t,dims:e[1],download:n,dispose:o})}case"ml-tensor":{let t=e[0];if(!Or(t))throw new Error(`not supported data type: ${t} for deserializing MLTensor tensor`);let{mlTensor:r,download:n,dispose:o}=e[2];return Ge.fromMLTensor(r,{dataType:t,dims:e[1],download:n,dispose:o})}default:throw new Error(`invalid data location: ${e[3]}`)}},dn=class{async fetchModelAndCopyToWasmMemory(t){return sc(await Qt(t))}async loadModel(t,r){Re();let n;typeof t=="string"?n=await this.fetchModelAndCopyToWasmMemory(t):n=t,[this.sessionId,this.inputNames,this.outputNames,this.inputMetadata,this.outputMetadata]=await uc(n,r),Oe()}async dispose(){return dc(this.sessionId)}async run(t,r,n){Re();let o=[],i=[];Object.entries(t).forEach(f=>{let b=f[0],g=f[1],_=this.inputNames.indexOf(b);if(_===-1)throw new Error(`invalid input '${b}'`);o.push(g),i.push(_)});let a=[],u=[];Object.entries(r).forEach(f=>{let b=f[0],g=f[1],_=this.outputNames.indexOf(b);if(_===-1)throw new Error(`invalid output '${b}'`);a.push(g),u.push(_)});let d=o.map((f,b)=>pc(f,()=>`input "${this.inputNames[i[b]]}"`)),c=a.map((f,b)=>f?pc(f,()=>`output "${this.outputNames[u[b]]}"`):null),p=await lc(this.sessionId,i,d,u,c,n),m={};for(let f=0;fln,initializeFlags:()=>fc,wasmBackend:()=>_g});var fc,ln,_g,gc=U(()=>{"use strict";We();Po();mc();fc=()=>{(typeof ge.wasm.initTimeout!="number"||ge.wasm.initTimeout<0)&&(ge.wasm.initTimeout=0);let e=ge.wasm.simd;if(typeof e!="boolean"&&e!==void 0&&e!=="fixed"&&e!=="relaxed"&&(console.warn(`Property "env.wasm.simd" is set to unknown value "${e}". Reset it to \`false\` and ignore SIMD feature checking.`),ge.wasm.simd=!1),typeof ge.wasm.proxy!="boolean"&&(ge.wasm.proxy=!1),typeof ge.wasm.trace!="boolean"&&(ge.wasm.trace=!1),typeof ge.wasm.numThreads!="number"||!Number.isInteger(ge.wasm.numThreads)||ge.wasm.numThreads<=0)if(typeof self<"u"&&!self.crossOriginIsolated)ge.wasm.numThreads=1;else{let t=typeof navigator>"u"?On("node:os").cpus().length:navigator.hardwareConcurrency;ge.wasm.numThreads=Math.min(4,Math.ceil((t||1)/2))}},ln=class{async init(t){fc(),await ic(),await ac(t)}async createInferenceSessionHandler(t,r){let n=new dn;return await n.loadModel(t,r),n}},_g=new ln});We();We();We();var _a="1.22.0-dev.20250409-89f8206ba4";var IS=Nn;{let e=(gc(),Ft(hc)).wasmBackend;$t("webgpu",e,5),$t("webnn",e,5),$t("cpu",e,10),$t("wasm",e,10)}Object.defineProperty(ge.versions,"web",{value:_a,enumerable:!0}); +/** + * @license + * Copyright 2021 Google LLC. All Rights Reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============================================================================= + */ +/** + * @license + * Copyright 2020 Google LLC. All Rights Reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============================================================================= + */ +/** + * @license + * Copyright 2019 Google LLC. All Rights Reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============================================================================= + */ +//# sourceMappingURL=ort.bundle.min.mjs.map + + +/***/ }), + +/***/ "./src/backends/onnx.js": +/*!******************************!*\ + !*** ./src/backends/onnx.js ***! + \******************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +var onnxruntime_node__WEBPACK_IMPORTED_MODULE_1___namespace_cache; +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ Tensor: () => (/* reexport safe */ onnxruntime_common__WEBPACK_IMPORTED_MODULE_3__.Tensor), +/* harmony export */ createInferenceSession: () => (/* binding */ createInferenceSession), +/* harmony export */ deviceToExecutionProviders: () => (/* binding */ deviceToExecutionProviders), +/* harmony export */ isONNXProxy: () => (/* binding */ isONNXProxy), +/* harmony export */ isONNXTensor: () => (/* binding */ isONNXTensor) +/* harmony export */ }); +/* harmony import */ var _env_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../env.js */ "./src/env.js"); +/* harmony import */ var onnxruntime_node__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! onnxruntime-node */ "?2ce3"); +/* harmony import */ var onnxruntime_web__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! onnxruntime-web */ "./node_modules/onnxruntime-web/dist/ort.bundle.min.mjs?3a96"); +/* harmony import */ var onnxruntime_common__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! onnxruntime-common */ "./node_modules/onnxruntime-common/dist/esm/index.js"); +/** + * @file Handler file for choosing the correct version of ONNX Runtime, based on the environment. + * Ideally, we could import the `onnxruntime-web` and `onnxruntime-node` packages only when needed, + * but dynamic imports don't seem to work with the current webpack version and/or configuration. + * This is possibly due to the experimental nature of top-level await statements. + * So, we just import both packages, and use the appropriate one based on the environment: + * - When running in node, we use `onnxruntime-node`. + * - When running in the browser, we use `onnxruntime-web` (`onnxruntime-node` is not bundled). + * + * This module is not directly exported, but can be accessed through the environment variables: + * ```javascript + * import { env } from '@huggingface/transformers'; + * console.log(env.backends.onnx); + * ``` + * + * @module backends/onnx + */ + + + +// NOTE: Import order matters here. We need to import `onnxruntime-node` before `onnxruntime-web`. +// In either case, we select the default export if it exists, otherwise we use the named export. + + + + + +/** + * @typedef {import('onnxruntime-common').InferenceSession.ExecutionProviderConfig} ONNXExecutionProviders + */ + +/** @type {Record} */ +const DEVICE_TO_EXECUTION_PROVIDER_MAPPING = Object.freeze({ + auto: null, // Auto-detect based on device and environment + gpu: null, // Auto-detect GPU + cpu: 'cpu', // CPU + wasm: 'wasm', // WebAssembly + webgpu: 'webgpu', // WebGPU + cuda: 'cuda', // CUDA + dml: 'dml', // DirectML + + webnn: { name: 'webnn', deviceType: 'cpu' }, // WebNN (default) + 'webnn-npu': { name: 'webnn', deviceType: 'npu' }, // WebNN NPU + 'webnn-gpu': { name: 'webnn', deviceType: 'gpu' }, // WebNN GPU + 'webnn-cpu': { name: 'webnn', deviceType: 'cpu' }, // WebNN CPU +}); + +/** + * The list of supported devices, sorted by priority/performance. + * @type {import("../utils/devices.js").DeviceType[]} + */ +const supportedDevices = []; + +/** @type {ONNXExecutionProviders[]} */ +let defaultDevices; +let ONNX; +const ORT_SYMBOL = Symbol.for('onnxruntime'); + +if (ORT_SYMBOL in globalThis) { + // If the JS runtime exposes their own ONNX runtime, use it + ONNX = globalThis[ORT_SYMBOL]; + +} else if (_env_js__WEBPACK_IMPORTED_MODULE_0__.apis.IS_NODE_ENV) { + ONNX = onnxruntime_node__WEBPACK_IMPORTED_MODULE_1__ ?? /*#__PURE__*/ (onnxruntime_node__WEBPACK_IMPORTED_MODULE_1___namespace_cache || (onnxruntime_node__WEBPACK_IMPORTED_MODULE_1___namespace_cache = __webpack_require__.t(onnxruntime_node__WEBPACK_IMPORTED_MODULE_1__, 2))); + + // Updated as of ONNX Runtime 1.20.1 + // The following table lists the supported versions of ONNX Runtime Node.js binding provided with pre-built binaries. + // | EPs/Platforms | Windows x64 | Windows arm64 | Linux x64 | Linux arm64 | MacOS x64 | MacOS arm64 | + // | ------------- | ----------- | ------------- | ----------------- | ----------- | --------- | ----------- | + // | CPU | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | + // | DirectML | ✔️ | ✔️ | ❌ | ❌ | ❌ | ❌ | + // | CUDA | ❌ | ❌ | ✔️ (CUDA v11.8) | ❌ | ❌ | ❌ | + switch (process.platform) { + case 'win32': // Windows x64 and Windows arm64 + supportedDevices.push('dml'); + break; + case 'linux': // Linux x64 and Linux arm64 + if (process.arch === 'x64') { + supportedDevices.push('cuda'); + } + break; + case 'darwin': // MacOS x64 and MacOS arm64 + break; + } + + supportedDevices.push('cpu'); + defaultDevices = ['cpu']; +} else { + ONNX = onnxruntime_web__WEBPACK_IMPORTED_MODULE_2__; + + if (_env_js__WEBPACK_IMPORTED_MODULE_0__.apis.IS_WEBNN_AVAILABLE) { + // TODO: Only push supported providers (depending on available hardware) + supportedDevices.push('webnn-npu', 'webnn-gpu', 'webnn-cpu', 'webnn'); + } + + if (_env_js__WEBPACK_IMPORTED_MODULE_0__.apis.IS_WEBGPU_AVAILABLE) { + supportedDevices.push('webgpu'); + } + + supportedDevices.push('wasm'); + defaultDevices = ['wasm']; +} + +// @ts-ignore +const InferenceSession = ONNX.InferenceSession; + +/** + * Map a device to the execution providers to use for the given device. + * @param {import("../utils/devices.js").DeviceType|"auto"|null} [device=null] (Optional) The device to run the inference on. + * @returns {ONNXExecutionProviders[]} The execution providers to use for the given device. + */ +function deviceToExecutionProviders(device = null) { + // Use the default execution providers if the user hasn't specified anything + if (!device) return defaultDevices; + + // Handle overloaded cases + switch (device) { + case "auto": + return supportedDevices; + case "gpu": + return supportedDevices.filter(x => + ["webgpu", "cuda", "dml", "webnn-gpu"].includes(x), + ); + } + + if (supportedDevices.includes(device)) { + return [DEVICE_TO_EXECUTION_PROVIDER_MAPPING[device] ?? device]; + } + + throw new Error(`Unsupported device: "${device}". Should be one of: ${supportedDevices.join(', ')}.`) +} + + +/** + * To prevent multiple calls to `initWasm()`, we store the first call in a Promise + * that is resolved when the first InferenceSession is created. Subsequent calls + * will wait for this Promise to resolve before creating their own InferenceSession. + * @type {Promise|null} + */ +let wasmInitPromise = null; + +/** + * Create an ONNX inference session. + * @param {Uint8Array|string} buffer_or_path The ONNX model buffer or path. + * @param {import('onnxruntime-common').InferenceSession.SessionOptions} session_options ONNX inference session options. + * @param {Object} session_config ONNX inference session configuration. + * @returns {Promise} The ONNX inference session. + */ +async function createInferenceSession(buffer_or_path, session_options, session_config) { + if (wasmInitPromise) { + // A previous session has already initialized the WASM runtime + // so we wait for it to resolve before creating this new session. + await wasmInitPromise; + } + + const sessionPromise = InferenceSession.create(buffer_or_path, session_options); + wasmInitPromise ??= sessionPromise; + const session = await sessionPromise; + session.config = session_config; + return session; +} + +/** + * Check if an object is an ONNX tensor. + * @param {any} x The object to check + * @returns {boolean} Whether the object is an ONNX tensor. + */ +function isONNXTensor(x) { + return x instanceof ONNX.Tensor; +} + +/** @type {import('onnxruntime-common').Env} */ +// @ts-ignore +const ONNX_ENV = ONNX?.env; +if (ONNX_ENV?.wasm) { + // Initialize wasm backend with suitable default settings. + + // (Optional) Set path to wasm files. This will override the default path search behavior of onnxruntime-web. + // By default, we only do this if we are not in a service worker and the wasmPaths are not already set. + if ( + // @ts-ignore Cannot find name 'ServiceWorkerGlobalScope'.ts(2304) + !(typeof ServiceWorkerGlobalScope !== 'undefined' && self instanceof ServiceWorkerGlobalScope) + && !ONNX_ENV.wasm.wasmPaths + ) { + ONNX_ENV.wasm.wasmPaths = `https://cdn.jsdelivr.net/npm/@huggingface/transformers@${_env_js__WEBPACK_IMPORTED_MODULE_0__.env.version}/dist/`; + } + + // TODO: Add support for loading WASM files from cached buffer when we upgrade to onnxruntime-web@1.19.0 + // https://github.com/microsoft/onnxruntime/pull/21534 + + // Users may wish to proxy the WASM backend to prevent the UI from freezing, + // However, this is not necessary when using WebGPU, so we default to false. + ONNX_ENV.wasm.proxy = false; +} + +if (ONNX_ENV?.webgpu) { + ONNX_ENV.webgpu.powerPreference = 'high-performance'; +} + +/** + * Check if ONNX's WASM backend is being proxied. + * @returns {boolean} Whether ONNX's WASM backend is being proxied. + */ +function isONNXProxy() { + // TODO: Update this when allowing non-WASM backends. + return ONNX_ENV?.wasm?.proxy; +} + +// Expose ONNX environment variables to `env.backends.onnx` +_env_js__WEBPACK_IMPORTED_MODULE_0__.env.backends.onnx = ONNX_ENV; + + +/***/ }), + +/***/ "./src/base/feature_extraction_utils.js": +/*!**********************************************!*\ + !*** ./src/base/feature_extraction_utils.js ***! + \**********************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ FeatureExtractor: () => (/* binding */ FeatureExtractor), +/* harmony export */ validate_audio_inputs: () => (/* binding */ validate_audio_inputs) +/* harmony export */ }); +/* harmony import */ var _utils_constants_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../utils/constants.js */ "./src/utils/constants.js"); +/* harmony import */ var _utils_generic_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../utils/generic.js */ "./src/utils/generic.js"); +/* harmony import */ var _utils_hub_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../utils/hub.js */ "./src/utils/hub.js"); + + + + +/** + * Base class for feature extractors. + */ +class FeatureExtractor extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_1__.Callable { + /** + * Constructs a new FeatureExtractor instance. + * + * @param {Object} config The configuration for the feature extractor. + */ + constructor(config) { + super(); + this.config = config + } + + /** + * Instantiate one of the feature extractor classes of the library from a pretrained model. + * + * The feature extractor class to instantiate is selected based on the `feature_extractor_type` property of + * the config object (either passed as an argument or loaded from `pretrained_model_name_or_path` if possible) + * + * @param {string} pretrained_model_name_or_path The name or path of the pretrained model. Can be either: + * - A string, the *model id* of a pretrained feature_extractor hosted inside a model repo on huggingface.co. + * Valid model ids can be located at the root-level, like `bert-base-uncased`, or namespaced under a + * user or organization name, like `dbmdz/bert-base-german-cased`. + * - A path to a *directory* containing feature_extractor files, e.g., `./my_model_directory/`. + * @param {import('../utils/hub.js').PretrainedOptions} options Additional options for loading the feature_extractor. + * + * @returns {Promise} A new instance of the Feature Extractor class. + */ + static async from_pretrained(pretrained_model_name_or_path, options) { + const config = await (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_2__.getModelJSON)(pretrained_model_name_or_path, _utils_constants_js__WEBPACK_IMPORTED_MODULE_0__.FEATURE_EXTRACTOR_NAME, true, options); + return new this(config); + } +} + + +/** + * Helper function to validate audio inputs. + * @param {any} audio The audio data. + * @param {string} feature_extractor The name of the feature extractor. + * @private + */ +function validate_audio_inputs(audio, feature_extractor) { + if (!(audio instanceof Float32Array || audio instanceof Float64Array)) { + throw new Error( + `${feature_extractor} expects input to be a Float32Array or a Float64Array, but got ${audio?.constructor?.name ?? typeof audio} instead. ` + + `If using the feature extractor directly, remember to use \`read_audio(url, sampling_rate)\` to obtain the raw audio data of the file/url.` + ) + } +} + + +/***/ }), + +/***/ "./src/base/image_processors_utils.js": +/*!********************************************!*\ + !*** ./src/base/image_processors_utils.js ***! + \********************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ ImageProcessor: () => (/* binding */ ImageProcessor), +/* harmony export */ center_to_corners_format: () => (/* binding */ center_to_corners_format), +/* harmony export */ post_process_instance_segmentation: () => (/* binding */ post_process_instance_segmentation), +/* harmony export */ post_process_object_detection: () => (/* binding */ post_process_object_detection), +/* harmony export */ post_process_panoptic_segmentation: () => (/* binding */ post_process_panoptic_segmentation), +/* harmony export */ post_process_semantic_segmentation: () => (/* binding */ post_process_semantic_segmentation) +/* harmony export */ }); +/* harmony import */ var _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../utils/generic.js */ "./src/utils/generic.js"); +/* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../utils/tensor.js */ "./src/utils/tensor.js"); +/* harmony import */ var _utils_maths_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../utils/maths.js */ "./src/utils/maths.js"); +/* harmony import */ var _utils_image_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ../utils/image.js */ "./src/utils/image.js"); +/* harmony import */ var _utils_core_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ../utils/core.js */ "./src/utils/core.js"); +/* harmony import */ var _utils_hub_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ../utils/hub.js */ "./src/utils/hub.js"); +/* harmony import */ var _utils_constants_js__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! ../utils/constants.js */ "./src/utils/constants.js"); + + + + + + + + +/** + * Named tuple to indicate the order we are using is (height x width), + * even though the Graphics' industry standard is (width x height). + * @typedef {[height: number, width: number]} HeightWidth + */ + + +/** + * @typedef {object} ImageProcessorResult + * @property {Tensor} pixel_values The pixel values of the batched preprocessed images. + * @property {HeightWidth[]} original_sizes Array of two-dimensional tuples like [[480, 640]]. + * @property {HeightWidth[]} reshaped_input_sizes Array of two-dimensional tuples like [[1000, 1330]]. + */ + + + +/** + * Helper function to constrain a value to be a multiple of a number. + * @param {number} val The value to constrain. + * @param {number} multiple The number to constrain to. + * @param {number} [minVal=0] The minimum value to constrain to. + * @param {number} [maxVal=null] The maximum value to constrain to. + * @returns {number} The constrained value. + * @private + */ +function constraint_to_multiple_of(val, multiple, minVal = 0, maxVal = null) { + const a = val / multiple; + let x = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_2__.bankers_round)(a) * multiple; + + if (maxVal !== null && x > maxVal) { + x = Math.floor(a) * multiple; + } + + if (x < minVal) { + x = Math.ceil(a) * multiple; + } + + return x; +} + +/** + * Rounds the height and width down to the closest multiple of size_divisibility + * @param {[number, number]} size The size of the image + * @param {number} divisor The divisor to use. + * @returns {[number, number]} The rounded size. + */ +function enforce_size_divisibility([width, height], divisor) { + return [ + Math.max(Math.floor(width / divisor), 1) * divisor, + Math.max(Math.floor(height / divisor), 1) * divisor + ]; +} + + +// Helper functions + +/** + * Converts bounding boxes from center format to corners format. + * + * @param {number[]} arr The coordinate for the center of the box and its width, height dimensions (center_x, center_y, width, height) + * @returns {number[]} The coodinates for the top-left and bottom-right corners of the box (top_left_x, top_left_y, bottom_right_x, bottom_right_y) + */ +function center_to_corners_format([centerX, centerY, width, height]) { + return [ + centerX - width / 2, + centerY - height / 2, + centerX + width / 2, + centerY + height / 2 + ]; +} + +/** + * Post-processes the outputs of the model (for object detection). + * @param {Object} outputs The outputs of the model that must be post-processed + * @param {Tensor} outputs.logits The logits + * @param {Tensor} outputs.pred_boxes The predicted boxes. + * @param {number} [threshold=0.5] The threshold to use for the scores. + * @param {[number, number][]} [target_sizes=null] The sizes of the original images. + * @param {boolean} [is_zero_shot=false] Whether zero-shot object detection was performed. + * @return {Object[]} An array of objects containing the post-processed outputs. + */ +function post_process_object_detection(outputs, threshold = 0.5, target_sizes = null, is_zero_shot = false) { + const out_logits = outputs.logits; + const out_bbox = outputs.pred_boxes; + const [batch_size, num_boxes, num_classes] = out_logits.dims; + + if (target_sizes !== null && target_sizes.length !== batch_size) { + throw Error("Make sure that you pass in as many target sizes as the batch dimension of the logits") + } + let toReturn = []; + for (let i = 0; i < batch_size; ++i) { + let target_size = target_sizes !== null ? target_sizes[i] : null; + let info = { + boxes: [], + classes: [], + scores: [] + } + let logits = out_logits[i]; + let bbox = out_bbox[i]; + + for (let j = 0; j < num_boxes; ++j) { + let logit = logits[j]; + + let indices = []; + let probs; + if (is_zero_shot) { + // Get indices of classes with high enough probability + probs = logit.sigmoid().data; + for (let k = 0; k < probs.length; ++k) { + if (probs[k] > threshold) { + indices.push(k); + } + } + + } else { + // Get most probable class + let maxIndex = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_2__.max)(logit.data)[1]; + + if (maxIndex === num_classes - 1) { + // This is the background class, skip it + continue; + } + // Compute softmax over classes + probs = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_2__.softmax)(logit.data); + + if (probs[maxIndex] < threshold) { + continue; + } + indices.push(maxIndex); + } + + for (const index of indices) { + + // Some class has a high enough probability + /** @type {number[]} */ + let box = bbox[j].data; + + // convert to [x0, y0, x1, y1] format + box = center_to_corners_format(box) + if (target_size !== null) { + box = box.map((x, i) => x * target_size[(i + 1) % 2]) + } + + info.boxes.push(box); + info.classes.push(index); + info.scores.push(probs[index]); + } + } + toReturn.push(info); + } + return toReturn; +} + + +/** + * Post-processes the outputs of the model (for semantic segmentation). + * @param {*} outputs Raw outputs of the model. + * @param {[number, number][]} [target_sizes=null] List of tuples corresponding to the requested final size + * (height, width) of each prediction. If unset, predictions will not be resized. + * @returns {{segmentation: Tensor; labels: number[]}[]} The semantic segmentation maps. + */ +function post_process_semantic_segmentation(outputs, target_sizes = null) { + + const logits = outputs.logits; + const batch_size = logits.dims[0]; + + if (target_sizes !== null && target_sizes.length !== batch_size) { + throw Error("Make sure that you pass in as many target sizes as the batch dimension of the logits") + } + + const toReturn = []; + for (let i = 0; i < batch_size; ++i) { + const target_size = target_sizes !== null ? target_sizes[i] : null; + + let data = logits[i]; + + // 1. If target_size is not null, we need to resize the masks to the target size + if (target_size !== null) { + // resize the masks to the target size + data = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.interpolate)(data, target_size, 'bilinear', false); + } + const [height, width] = target_size ?? data.dims.slice(-2); + + const segmentation = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor( + 'int32', + new Int32Array(height * width), + [height, width] + ); + + // Buffer to store current largest value + const buffer = data[0].data; + const segmentation_data = segmentation.data; + for (let j = 1; j < data.dims[0]; ++j) { + const row = data[j].data; + for (let k = 0; k < row.length; ++k) { + if (row[k] > buffer[k]) { + buffer[k] = row[k]; + segmentation_data[k] = j; + } + } + } + + // Store which objects have labels + // This is much more efficient that creating a set of the final values + const hasLabel = new Array(data.dims[0]); + for (let j = 0; j < segmentation_data.length; ++j) { + const index = segmentation_data[j]; + hasLabel[index] = index; + } + /** @type {number[]} The unique list of labels that were detected */ + const labels = hasLabel.filter(x => x !== undefined); + + toReturn.push({ segmentation, labels }); + } + return toReturn; +} + + +/** + * Binarize the given masks using `object_mask_threshold`, it returns the associated values of `masks`, `scores` and `labels`. + * @param {Tensor} class_logits The class logits. + * @param {Tensor} mask_logits The mask logits. + * @param {number} object_mask_threshold A number between 0 and 1 used to binarize the masks. + * @param {number} num_labels The number of labels. + * @returns {[Tensor[], number[], number[]]} The binarized masks, the scores, and the labels. + * @private + */ +function remove_low_and_no_objects(class_logits, mask_logits, object_mask_threshold, num_labels) { + + const mask_probs_item = []; + const pred_scores_item = []; + const pred_labels_item = []; + + for (let j = 0; j < class_logits.dims[0]; ++j) { + const cls = class_logits[j]; + const mask = mask_logits[j]; + + const pred_label = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_2__.max)(cls.data)[1]; + if (pred_label === num_labels) { + // Is the background, so we ignore it + continue; + } + + const scores = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_2__.softmax)(cls.data); + const pred_score = scores[pred_label]; + if (pred_score > object_mask_threshold) { + mask_probs_item.push(mask); + pred_scores_item.push(pred_score); + pred_labels_item.push(pred_label); + } + } + + return [mask_probs_item, pred_scores_item, pred_labels_item]; +} + +/** + * Checks whether the segment is valid or not. + * @param {Int32Array} mask_labels Labels for each pixel in the mask. + * @param {Tensor[]} mask_probs Probabilities for each pixel in the masks. + * @param {number} k The class id of the segment. + * @param {number} mask_threshold The mask threshold. + * @param {number} overlap_mask_area_threshold The overlap mask area threshold. + * @returns {[boolean, number[]]} Whether the segment is valid or not, and the indices of the valid labels. + * @private + */ +function check_segment_validity( + mask_labels, + mask_probs, + k, + mask_threshold = 0.5, + overlap_mask_area_threshold = 0.8 +) { + // mask_k is a 1D array of indices, indicating where the mask is equal to k + const mask_k = []; + let mask_k_area = 0; + let original_area = 0; + + const mask_probs_k_data = mask_probs[k].data; + + // Compute the area of all the stuff in query k + for (let i = 0; i < mask_labels.length; ++i) { + if (mask_labels[i] === k) { + mask_k.push(i); + ++mask_k_area; + } + + if (mask_probs_k_data[i] >= mask_threshold) { + ++original_area; + } + } + let mask_exists = mask_k_area > 0 && original_area > 0; + + // Eliminate disconnected tiny segments + if (mask_exists) { + // Perform additional check + let area_ratio = mask_k_area / original_area; + mask_exists = area_ratio > overlap_mask_area_threshold; + } + + return [mask_exists, mask_k] +} + +/** + * Computes the segments. + * @param {Tensor[]} mask_probs The mask probabilities. + * @param {number[]} pred_scores The predicted scores. + * @param {number[]} pred_labels The predicted labels. + * @param {number} mask_threshold The mask threshold. + * @param {number} overlap_mask_area_threshold The overlap mask area threshold. + * @param {Set} label_ids_to_fuse The label ids to fuse. + * @param {number[]} target_size The target size of the image. + * @returns {[Tensor, Array<{id: number, label_id: number, score: number}>]} The computed segments. + * @private + */ +function compute_segments( + mask_probs, + pred_scores, + pred_labels, + mask_threshold, + overlap_mask_area_threshold, + label_ids_to_fuse = null, + target_size = null, +) { + const [height, width] = target_size ?? mask_probs[0].dims; + + const segmentation = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor( + 'int32', + new Int32Array(height * width), + [height, width] + ); + const segments = []; + + // 1. If target_size is not null, we need to resize the masks to the target size + if (target_size !== null) { + // resize the masks to the target size + for (let i = 0; i < mask_probs.length; ++i) { + mask_probs[i] = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.interpolate)(mask_probs[i], target_size, 'bilinear', false); + } + } + + // 2. Weigh each mask by its prediction score + // NOTE: `mask_probs` is updated in-place + // + // Temporary storage for the best label/scores for each pixel ([height, width]): + const mask_labels = new Int32Array(mask_probs[0].data.length); + const bestScores = new Float32Array(mask_probs[0].data.length); + + for (let i = 0; i < mask_probs.length; ++i) { + let score = pred_scores[i]; + + const mask_probs_i_data = mask_probs[i].data; + + for (let j = 0; j < mask_probs_i_data.length; ++j) { + mask_probs_i_data[j] *= score + if (mask_probs_i_data[j] > bestScores[j]) { + mask_labels[j] = i; + bestScores[j] = mask_probs_i_data[j]; + } + } + } + + let current_segment_id = 0; + + // let stuff_memory_list = {} + const segmentation_data = segmentation.data; + for (let k = 0; k < pred_labels.length; ++k) { + const pred_class = pred_labels[k]; + + // TODO add `should_fuse` + // let should_fuse = pred_class in label_ids_to_fuse + + // Check if mask exists and large enough to be a segment + const [mask_exists, mask_k] = check_segment_validity( + mask_labels, + mask_probs, + k, + mask_threshold, + overlap_mask_area_threshold + ) + + if (!mask_exists) { + // Nothing to see here + continue; + } + + // TODO + // if (pred_class in stuff_memory_list) { + // current_segment_id = stuff_memory_list[pred_class] + // } else { + // current_segment_id += 1; + // } + ++current_segment_id; + + + // Add current object segment to final segmentation map + for (const index of mask_k) { + segmentation_data[index] = current_segment_id; + } + + segments.push({ + id: current_segment_id, + label_id: pred_class, + // was_fused: should_fuse, TODO + score: pred_scores[k], + }) + + // TODO + // if(should_fuse){ + // stuff_memory_list[pred_class] = current_segment_id + // } + } + + return [segmentation, segments]; +} + +/** + * Rescales the image so that the following conditions are met: + * + * 1. Both dimensions (height and width) are divisible by 'factor'. + * 2. The total number of pixels is within the range ['min_pixels', 'max_pixels']. + * 3. The aspect ratio of the image is maintained as closely as possible. + * + * @param {number} height The height of the image. + * @param {number} width The width of the image. + * @param {number} [factor=28] The factor to use for resizing. + * @param {number} [min_pixels=56*56] The minimum number of pixels. + * @param {number} [max_pixels=14*14*4*1280] The maximum number of pixels. + * @returns {[number, number]} The new height and width of the image. + * @throws {Error} If the height or width is smaller than the factor. + */ +function smart_resize(height, width, factor = 28, min_pixels = 56 * 56, max_pixels = 14 * 14 * 4 * 1280) { + + if (height < factor || width < factor) { + throw new Error(`height:${height} or width:${width} must be larger than factor:${factor}`); + } else if (Math.max(height, width) / Math.min(height, width) > 200) { + throw new Error( + `absolute aspect ratio must be smaller than 200, got ${Math.max(height, width) / Math.min(height, width)}` + ); + } + + let h_bar = Math.round(height / factor) * factor; + let w_bar = Math.round(width / factor) * factor; + + if (h_bar * w_bar > max_pixels) { + const beta = Math.sqrt((height * width) / max_pixels); + h_bar = Math.floor((height / beta) / factor) * factor; + w_bar = Math.floor((width / beta) / factor) * factor; + } else if (h_bar * w_bar < min_pixels) { + const beta = Math.sqrt(min_pixels / (height * width)); + h_bar = Math.ceil((height * beta) / factor) * factor; + w_bar = Math.ceil((width * beta) / factor) * factor; + } + + return [h_bar, w_bar]; +} + + +/** + * Post-process the model output to generate the final panoptic segmentation. + * @param {*} outputs The model output to post process + * @param {number} [threshold=0.5] The probability score threshold to keep predicted instance masks. + * @param {number} [mask_threshold=0.5] Threshold to use when turning the predicted masks into binary values. + * @param {number} [overlap_mask_area_threshold=0.8] The overlap mask area threshold to merge or discard small disconnected parts within each binary instance mask. + * @param {Set} [label_ids_to_fuse=null] The labels in this state will have all their instances be fused together. + * @param {[number, number][]} [target_sizes=null] The target sizes to resize the masks to. + * @returns {Array<{ segmentation: Tensor, segments_info: Array<{id: number, label_id: number, score: number}>}>} + */ +function post_process_panoptic_segmentation( + outputs, + threshold = 0.5, + mask_threshold = 0.5, + overlap_mask_area_threshold = 0.8, + label_ids_to_fuse = null, + target_sizes = null, +) { + if (label_ids_to_fuse === null) { + console.warn("`label_ids_to_fuse` unset. No instance will be fused.") + label_ids_to_fuse = new Set(); + } + + const class_queries_logits = outputs.class_queries_logits ?? outputs.logits; // [batch_size, num_queries, num_classes+1] + const masks_queries_logits = outputs.masks_queries_logits ?? outputs.pred_masks; // [batch_size, num_queries, height, width] + + const mask_probs = masks_queries_logits.sigmoid() // [batch_size, num_queries, height, width] + + let [batch_size, num_queries, num_labels] = class_queries_logits.dims; + num_labels -= 1; // Remove last class (background) + + if (target_sizes !== null && target_sizes.length !== batch_size) { + throw Error("Make sure that you pass in as many target sizes as the batch dimension of the logits") + } + + let toReturn = []; + for (let i = 0; i < batch_size; ++i) { + let target_size = target_sizes !== null ? target_sizes[i] : null; + + let class_logits = class_queries_logits[i]; + let mask_logits = mask_probs[i]; + + let [mask_probs_item, pred_scores_item, pred_labels_item] = remove_low_and_no_objects(class_logits, mask_logits, threshold, num_labels); + + if (pred_labels_item.length === 0) { + // No mask found + let [height, width] = target_size ?? mask_logits.dims.slice(-2); + + let segmentation = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor( + 'int32', + new Int32Array(height * width).fill(-1), + [height, width] + ) + toReturn.push({ + segmentation: segmentation, + segments_info: [] + }); + continue; + } + + + // Get segmentation map and segment information of batch item + let [segmentation, segments] = compute_segments( + mask_probs_item, + pred_scores_item, + pred_labels_item, + mask_threshold, + overlap_mask_area_threshold, + label_ids_to_fuse, + target_size, + ) + + toReturn.push({ + segmentation: segmentation, + segments_info: segments + }) + } + + return toReturn; +} + + +/** + * Post-processes the outputs of the model (for instance segmentation). + * @param {*} outputs Raw outputs of the model. + * @param {number} [threshold=0.5] The probability score threshold to keep predicted instance masks. + * @param {[number, number][]} [target_sizes=null] List of tuples corresponding to the requested final size + * (height, width) of each prediction. If unset, predictions will not be resized. + * @returns {Array<{ segmentation: Tensor, segments_info: Array<{id: number, label_id: number, score: number}>}>} + */ +function post_process_instance_segmentation(outputs, threshold = 0.5, target_sizes = null) { + throw new Error('`post_process_instance_segmentation` is not yet implemented.'); +} + + +/** + * @typedef {Object} ImageProcessorConfig A configuration object used to create an image processor. + * @property {function} [progress_callback=null] If specified, this function will be called during model construction, to provide the user with progress updates. + * @property {number[]} [image_mean] The mean values for image normalization. + * @property {number[]} [image_std] The standard deviation values for image normalization. + * @property {boolean} [do_rescale] Whether to rescale the image pixel values to the [0,1] range. + * @property {number} [rescale_factor] The factor to use for rescaling the image pixel values. + * @property {boolean} [do_normalize] Whether to normalize the image pixel values. + * @property {boolean} [do_resize] Whether to resize the image. + * @property {number} [resample] What method to use for resampling. + * @property {number|Object} [size] The size to resize the image to. + * @property {number|Object} [image_size] The size to resize the image to (same as `size`). + * @property {boolean} [do_flip_channel_order=false] Whether to flip the color channels from RGB to BGR. + * Can be overridden by the `do_flip_channel_order` parameter in the `preprocess` method. + * @property {boolean} [do_center_crop] Whether to center crop the image to the specified `crop_size`. + * Can be overridden by `do_center_crop` in the `preprocess` method. + * @property {boolean} [do_thumbnail] Whether to resize the image using thumbnail method. + * @property {boolean} [keep_aspect_ratio] If `true`, the image is resized to the largest possible size such that the aspect ratio is preserved. + * Can be overidden by `keep_aspect_ratio` in `preprocess`. + * @property {number} [ensure_multiple_of] If `do_resize` is `true`, the image is resized to a size that is a multiple of this value. + * Can be overidden by `ensure_multiple_of` in `preprocess`. + * + * @property {number[]} [mean] The mean values for image normalization (same as `image_mean`). + * @property {number[]} [std] The standard deviation values for image normalization (same as `image_std`). + */ + +class ImageProcessor extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + + /** + * Constructs a new `ImageProcessor`. + * @param {ImageProcessorConfig} config The configuration object. + */ + constructor(config) { + super(); + + this.image_mean = config.image_mean ?? config.mean; + this.image_std = config.image_std ?? config.std; + + this.resample = config.resample ?? 2; // 2 => bilinear + this.do_rescale = config.do_rescale ?? true; + this.rescale_factor = config.rescale_factor ?? (1 / 255); + this.do_normalize = config.do_normalize; + + this.do_thumbnail = config.do_thumbnail; + this.size = config.size ?? config.image_size; + this.do_resize = config.do_resize ?? (this.size !== undefined); + // @ts-expect-error TS2339 + this.size_divisibility = config.size_divisibility ?? config.size_divisor; + + this.do_center_crop = config.do_center_crop; + // @ts-expect-error TS2339 + this.crop_size = config.crop_size; + // @ts-expect-error TS2339 + this.do_convert_rgb = config.do_convert_rgb ?? true; + // @ts-expect-error TS2339 + this.do_crop_margin = config.do_crop_margin; + + // @ts-expect-error TS2339 + this.pad_size = config.pad_size; + // @ts-expect-error TS2339 + this.do_pad = config.do_pad; + // @ts-expect-error TS2339 + this.min_pixels = config.min_pixels; + // @ts-expect-error TS2339 + this.max_pixels = config.max_pixels; + + if (this.do_pad && !this.pad_size && this.size && this.size.width !== undefined && this.size.height !== undefined) { + // Should pad, but no pad size specified + // We infer the pad size from the resize size + this.pad_size = this.size + } + + this.do_flip_channel_order = config.do_flip_channel_order ?? false; + + this.config = config; + } + + /** + * Resize the image to make a thumbnail. The image is resized so that no dimension is larger than any + * corresponding dimension of the specified size. + * @param {RawImage} image The image to be resized. + * @param {{height:number, width:number}} size The size `{"height": h, "width": w}` to resize the image to. + * @param {string | 0 | 1 | 2 | 3 | 4 | 5} [resample=2] The resampling filter to use. + * @returns {Promise} The resized image. + */ + async thumbnail(image, size, resample = 2) { + const input_height = image.height; + const input_width = image.width; + + const output_height = size.height; + const output_width = size.width; + + // We always resize to the smallest of either the input or output size. + let height = Math.min(input_height, output_height) + let width = Math.min(input_width, output_width) + + if (height === input_height && width === input_width) { + return image; + } + if (input_height > input_width) { + width = Math.floor(input_width * height / input_height); + } else if (input_width > input_height) { + height = Math.floor(input_height * width / input_width); + } + return await image.resize(width, height, { resample }); + } + + + /** + * Crops the margin of the image. Gray pixels are considered margin (i.e., pixels with a value below the threshold). + * @param {RawImage} image The image to be cropped. + * @param {number} gray_threshold Value below which pixels are considered to be gray. + * @returns {Promise} The cropped image. + */ + async crop_margin(image, gray_threshold = 200) { + + const gray_image = image.clone().grayscale(); + + const minValue = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_2__.min)(gray_image.data)[0]; + const maxValue = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_2__.max)(gray_image.data)[0]; + const diff = maxValue - minValue; + + if (diff === 0) { + return image; + } + + const threshold = gray_threshold / 255; + + let x_min = gray_image.width, y_min = gray_image.height, x_max = 0, y_max = 0; + const gray_image_data = gray_image.data; + for (let j = 0; j < gray_image.height; ++j) { + const row = j * gray_image.width; + for (let i = 0; i < gray_image.width; ++i) { + if ((gray_image_data[row + i] - minValue) / diff < threshold) { + // We have a non-zero pixel, so we update the min/max values accordingly + x_min = Math.min(x_min, i); + y_min = Math.min(y_min, j); + x_max = Math.max(x_max, i); + y_max = Math.max(y_max, j); + } + } + } + + image = await image.crop([x_min, y_min, x_max, y_max]); + return image; + } + + /** + * Pad the image by a certain amount. + * @param {Float32Array} pixelData The pixel data to pad. + * @param {number[]} imgDims The dimensions of the image (height, width, channels). + * @param {{width:number; height:number}|number|'square'} padSize The dimensions of the padded image. + * @param {Object} options The options for padding. + * @param {'constant'|'symmetric'} [options.mode='constant'] The type of padding to add. + * @param {boolean} [options.center=false] Whether to center the image. + * @param {number|number[]} [options.constant_values=0] The constant value to use for padding. + * @returns {[Float32Array, number[]]} The padded pixel data and image dimensions. + */ + pad_image(pixelData, imgDims, padSize, { + mode = 'constant', + center = false, + constant_values = 0, + } = {}) { + const [imageHeight, imageWidth, imageChannels] = imgDims; + + let paddedImageWidth, paddedImageHeight; + if (typeof padSize === 'number') { + paddedImageWidth = padSize; + paddedImageHeight = padSize; + } else if (padSize === 'square') { + paddedImageWidth = paddedImageHeight = Math.max(imageHeight, imageWidth); + } else { + paddedImageWidth = padSize.width; + paddedImageHeight = padSize.height; + } + + // Only add padding if there is a difference in size + if (paddedImageWidth !== imageWidth || paddedImageHeight !== imageHeight) { + const paddedPixelData = new Float32Array(paddedImageWidth * paddedImageHeight * imageChannels); + if (Array.isArray(constant_values)) { + // Fill with constant values, cycling through the array + for (let i = 0; i < paddedPixelData.length; ++i) { + paddedPixelData[i] = constant_values[i % imageChannels]; + } + } else if (constant_values !== 0) { + paddedPixelData.fill(constant_values); + } + + const [left, top] = center + ? [Math.floor((paddedImageWidth - imageWidth) / 2), Math.floor((paddedImageHeight - imageHeight) / 2)] + : [0, 0]; + + // Copy the original image into the padded image + for (let i = 0; i < imageHeight; ++i) { + const a = (i + top) * paddedImageWidth; + const b = i * imageWidth; + for (let j = 0; j < imageWidth; ++j) { + const c = (a + j + left) * imageChannels; + const d = (b + j) * imageChannels; + for (let k = 0; k < imageChannels; ++k) { + paddedPixelData[c + k] = pixelData[d + k]; + } + } + } + + if (mode === 'symmetric') { + if (center) { + throw new Error('`center` padding is not supported when `mode` is set to `symmetric`.'); + // TODO: Implement this + } + const h1 = imageHeight - 1; + const w1 = imageWidth - 1; + for (let i = 0; i < paddedImageHeight; ++i) { + const a = i * paddedImageWidth; + const b = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.calculateReflectOffset)(i, h1) * imageWidth; + + for (let j = 0; j < paddedImageWidth; ++j) { + if (i < imageHeight && j < imageWidth) continue; // Do not overwrite original image + const c = (a + j) * imageChannels; + const d = (b + (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.calculateReflectOffset)(j, w1)) * imageChannels; + + // Copy channel-wise + for (let k = 0; k < imageChannels; ++k) { + paddedPixelData[c + k] = pixelData[d + k]; + } + } + } + } + + + // Update pixel data and image dimensions + pixelData = paddedPixelData; + imgDims = [paddedImageHeight, paddedImageWidth, imageChannels] + } + return [pixelData, imgDims]; + } + + /** + * Rescale the image' pixel values by `this.rescale_factor`. + * @param {Float32Array} pixelData The pixel data to rescale. + * @returns {void} + */ + rescale(pixelData) { + for (let i = 0; i < pixelData.length; ++i) { + pixelData[i] = this.rescale_factor * pixelData[i]; + } + } + + /** + * Find the target (width, height) dimension of the output image after + * resizing given the input image and the desired size. + * @param {RawImage} image The image to resize. + * @param {any} size The size to use for resizing the image. + * @returns {[number, number]} The target (width, height) dimension of the output image after resizing. + */ + get_resize_output_image_size(image, size) { + // `size` comes in many forms, so we need to handle them all here: + // 1. `size` is an integer, in which case we resize the image to be a square + + const [srcWidth, srcHeight] = image.size; + + let shortest_edge; + let longest_edge; + + if (this.do_thumbnail) { + // NOTE: custom logic for `Donut` models + const { height, width } = size; + shortest_edge = Math.min(height, width) + } + // Support both formats for backwards compatibility + else if (Number.isInteger(size)) { + shortest_edge = size; + // @ts-expect-error TS2339 + longest_edge = this.config.max_size ?? shortest_edge; + + } else if (size !== undefined) { + // Extract known properties from `size` + shortest_edge = size.shortest_edge; + longest_edge = size.longest_edge; + } + + // If `longest_edge` and `shortest_edge` are set, maintain aspect ratio and resize to `shortest_edge` + // while keeping the largest dimension <= `longest_edge` + if (shortest_edge !== undefined || longest_edge !== undefined) { + // http://opensourcehacker.com/2011/12/01/calculate-aspect-ratio-conserving-resize-for-images-in-javascript/ + // Try resize so that shortest edge is `shortest_edge` (target) + const shortResizeFactor = shortest_edge === undefined + ? 1 // If `shortest_edge` is not set, don't upscale + : Math.max(shortest_edge / srcWidth, shortest_edge / srcHeight); + + const newWidth = srcWidth * shortResizeFactor; + const newHeight = srcHeight * shortResizeFactor; + + // The new width and height might be greater than `longest_edge`, so + // we downscale again to ensure the largest dimension is `longest_edge` + const longResizeFactor = longest_edge === undefined + ? 1 // If `longest_edge` is not set, don't downscale + : Math.min(longest_edge / newWidth, longest_edge / newHeight); + + // To avoid certain floating point precision issues, we round to 2 decimal places + let finalWidth = Math.floor(Number((newWidth * longResizeFactor).toFixed(2))); + let finalHeight = Math.floor(Number((newHeight * longResizeFactor).toFixed(2))); + + if (this.size_divisibility !== undefined) { + [finalWidth, finalHeight] = enforce_size_divisibility([finalWidth, finalHeight], this.size_divisibility) + } + return [finalWidth, finalHeight]; + + } else if (size !== undefined && size.width !== undefined && size.height !== undefined) { + // If `width` and `height` are set, resize to those dimensions + + let newWidth = size.width; + let newHeight = size.height; + + // Custom for DPT models + if (this.config.keep_aspect_ratio && this.config.ensure_multiple_of) { + + // determine new height and width + let scale_height = newHeight / srcHeight; + let scale_width = newWidth / srcWidth; + + // scale as little as possible + if (Math.abs(1 - scale_width) < Math.abs(1 - scale_height)) { + // fit width + scale_height = scale_width; + } else { + // fit height + scale_width = scale_height; + } + + newHeight = constraint_to_multiple_of(scale_height * srcHeight, this.config.ensure_multiple_of); + newWidth = constraint_to_multiple_of(scale_width * srcWidth, this.config.ensure_multiple_of); + } + + return [newWidth, newHeight]; + + } else if (this.size_divisibility !== undefined) { + return enforce_size_divisibility([srcWidth, srcHeight], this.size_divisibility); + } else if (this.min_pixels !== undefined && this.max_pixels !== undefined) { + // Custom resize logic for Qwen2-VL models + // @ts-expect-error TS2339 + const factor = this.config.patch_size * this.config.merge_size; + return smart_resize(srcHeight, srcWidth, factor, this.min_pixels, this.max_pixels); + } else { + throw new Error(`Could not resize image due to unsupported \`this.size\` option in config: ${JSON.stringify(size)}`); + } + } + + /** + * Resizes the image. + * @param {RawImage} image The image to resize. + * @returns {Promise} The resized image. + */ + async resize(image) { + const [newWidth, newHeight] = this.get_resize_output_image_size(image, this.size); + return await image.resize(newWidth, newHeight, { + // @ts-expect-error TS2322 + resample: this.resample, + }); + } + + /** + * @typedef {object} PreprocessedImage + * @property {HeightWidth} original_size The original size of the image. + * @property {HeightWidth} reshaped_input_size The reshaped input size of the image. + * @property {Tensor} pixel_values The pixel values of the preprocessed image. + */ + + /** + * Preprocesses the given image. + * + * @param {RawImage} image The image to preprocess. + * @param {Object} overrides The overrides for the preprocessing options. + * @returns {Promise} The preprocessed image. + */ + async preprocess(image, { + do_normalize = null, + do_pad = null, + do_convert_rgb = null, + do_convert_grayscale = null, + do_flip_channel_order = null, + } = {}) { + if (this.do_crop_margin) { + // NOTE: Specific to nougat processors. This is done before resizing, + // and can be interpreted as a pre-preprocessing step. + image = await this.crop_margin(image); + } + + const [srcWidth, srcHeight] = image.size; // original image size + + // Convert image to RGB if specified in config. + if (do_convert_rgb ?? this.do_convert_rgb) { + image = image.rgb(); + } else if (do_convert_grayscale) { + image = image.grayscale(); + } + + // TODO: + // For efficiency reasons, it might be best to merge the resize and center crop operations into one. + + // Resize all images + if (this.do_resize) { + image = await this.resize(image); + } + + // Resize the image using thumbnail method. + if (this.do_thumbnail) { + // @ts-expect-error TS2345 + image = await this.thumbnail(image, this.size, this.resample); + } + + if (this.do_center_crop) { + + let crop_width; + let crop_height; + if (Number.isInteger(this.crop_size)) { + crop_width = this.crop_size; + crop_height = this.crop_size; + } else { + crop_width = this.crop_size.width; + crop_height = this.crop_size.height; + } + + image = await image.center_crop(crop_width, crop_height); + } + + /** @type {HeightWidth} */ + const reshaped_input_size = [image.height, image.width]; + + // NOTE: All pixel-level manipulation (i.e., modifying `pixelData`) + // occurs with data in the hwc format (height, width, channels), + // to emulate the behavior of the original Python code (w/ numpy). + /** @type {Float32Array} */ + let pixelData = Float32Array.from(image.data); + let imgDims = [image.height, image.width, image.channels]; + + if (this.do_rescale) { + this.rescale(pixelData); + } + + if (do_normalize ?? this.do_normalize) { + let image_mean = this.image_mean; + if (!Array.isArray(this.image_mean)) { + image_mean = new Array(image.channels).fill(image_mean); + } + + let image_std = this.image_std; + if (!Array.isArray(this.image_std)) { + image_std = new Array(image.channels).fill(image_mean); + } + + if (image_mean.length !== image.channels || image_std.length !== image.channels) { + throw new Error(`When set to arrays, the length of \`image_mean\` (${image_mean.length}) and \`image_std\` (${image_std.length}) must match the number of channels in the image (${image.channels}).`); + } + + for (let i = 0; i < pixelData.length; i += image.channels) { + for (let j = 0; j < image.channels; ++j) { + pixelData[i + j] = (pixelData[i + j] - image_mean[j]) / image_std[j]; + } + } + } + + // do padding after rescaling/normalizing + if (do_pad ?? this.do_pad) { + if (this.pad_size) { + const padded = this.pad_image(pixelData, [image.height, image.width, image.channels], this.pad_size); + [pixelData, imgDims] = padded; // Update pixel data and image dimensions + } else if (this.size_divisibility) { + const [paddedWidth, paddedHeight] = enforce_size_divisibility([imgDims[1], imgDims[0]], this.size_divisibility); + [pixelData, imgDims] = this.pad_image(pixelData, imgDims, { width: paddedWidth, height: paddedHeight }); + } + } + + if (do_flip_channel_order ?? this.do_flip_channel_order) { + if (imgDims[2] !== 3) { + throw new Error('Flipping channel order is only supported for RGB images.'); + } + // Convert RGB to BGR + for (let i = 0; i < pixelData.length; i += 3) { + const temp = pixelData[i]; + pixelData[i] = pixelData[i + 2]; + pixelData[i + 2] = temp; + } + } + + const pixel_values = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor('float32', pixelData, imgDims) + .permute(2, 0, 1); // convert to channel dimension format (hwc -> chw) + + return { + original_size: [srcHeight, srcWidth], + reshaped_input_size: reshaped_input_size, + pixel_values, + } + } + + /** + * Calls the feature extraction process on an array of images, + * preprocesses each image, and concatenates the resulting + * features into a single Tensor. + * @param {RawImage[]} images The image(s) to extract features from. + * @param {...any} args Additional arguments. + * @returns {Promise} An object containing the concatenated pixel values (and other metadata) of the preprocessed images. + */ + async _call(images, ...args) { + if (!Array.isArray(images)) { + images = [images]; + } + /** @type {PreprocessedImage[]} */ + const imageData = await Promise.all(images.map(x => this.preprocess(x))); + + // Stack pixel values + const pixel_values = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.stack)(imageData.map(x => x.pixel_values), 0); + + return { + pixel_values, + + // Original sizes of images + original_sizes: imageData.map(x => x.original_size), + + // Reshaped sizes of images, before padding or cropping + reshaped_input_sizes: imageData.map(x => x.reshaped_input_size), + } + } + + + /** + * Instantiate one of the processor classes of the library from a pretrained model. + * + * The processor class to instantiate is selected based on the `image_processor_type` (or `feature_extractor_type`; legacy) + * property of the config object (either passed as an argument or loaded from `pretrained_model_name_or_path` if possible) + * + * @param {string} pretrained_model_name_or_path The name or path of the pretrained model. Can be either: + * - A string, the *model id* of a pretrained processor hosted inside a model repo on huggingface.co. + * Valid model ids can be located at the root-level, like `bert-base-uncased`, or namespaced under a + * user or organization name, like `dbmdz/bert-base-german-cased`. + * - A path to a *directory* containing processor files, e.g., `./my_model_directory/`. + * @param {import('../utils/hub.js').PretrainedOptions} options Additional options for loading the processor. + * + * @returns {Promise} A new instance of the Processor class. + */ + static async from_pretrained(pretrained_model_name_or_path, options) { + const preprocessorConfig = await (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_5__.getModelJSON)(pretrained_model_name_or_path, _utils_constants_js__WEBPACK_IMPORTED_MODULE_6__.IMAGE_PROCESSOR_NAME, true, options); + return new this(preprocessorConfig); + } +} + + +/***/ }), + +/***/ "./src/base/processing_utils.js": +/*!**************************************!*\ + !*** ./src/base/processing_utils.js ***! + \**************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ Processor: () => (/* binding */ Processor) +/* harmony export */ }); +/* harmony import */ var _utils_constants_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../utils/constants.js */ "./src/utils/constants.js"); +/* harmony import */ var _utils_generic_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../utils/generic.js */ "./src/utils/generic.js"); +/* harmony import */ var _utils_hub_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../utils/hub.js */ "./src/utils/hub.js"); + +/** + * @file Processors are used to prepare inputs (e.g., text, image or audio) for a model. + * + * **Example:** Using a `WhisperProcessor` to prepare an audio input for a model. + * ```javascript + * import { AutoProcessor, read_audio } from '@huggingface/transformers'; + * + * const processor = await AutoProcessor.from_pretrained('openai/whisper-tiny.en'); + * const audio = await read_audio('https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac', 16000); + * const { input_features } = await processor(audio); + * // Tensor { + * // data: Float32Array(240000) [0.4752984642982483, 0.5597258806228638, 0.56434166431427, ...], + * // dims: [1, 80, 3000], + * // type: 'float32', + * // size: 240000, + * // } + * ``` + * + * @module processors + */ + + + + +/** + * @typedef {Object} ProcessorProperties Additional processor-specific properties. + * @typedef {import('../utils/hub.js').PretrainedOptions & ProcessorProperties} PretrainedProcessorOptions + * @typedef {import('../tokenizers.js').PreTrainedTokenizer} PreTrainedTokenizer + */ + + +/** + * Represents a Processor that extracts features from an input. + */ +class Processor extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_1__.Callable { + static classes = [ + 'image_processor_class', + 'tokenizer_class', + 'feature_extractor_class', + ] + static uses_processor_config = false; + + /** + * Creates a new Processor with the given components + * @param {Object} config + * @param {Record} components + */ + constructor(config, components) { + super(); + this.config = config; + this.components = components; + } + + /** + * @returns {import('./image_processors_utils.js').ImageProcessor|undefined} The image processor of the processor, if it exists. + */ + get image_processor() { + return this.components.image_processor; + } + + /** + * @returns {PreTrainedTokenizer|undefined} The tokenizer of the processor, if it exists. + */ + get tokenizer() { + return this.components.tokenizer; + } + + /** + * @returns {import('./feature_extraction_utils.js').FeatureExtractor|undefined} The feature extractor of the processor, if it exists. + */ + get feature_extractor() { + return this.components.feature_extractor; + } + + /** + * @param {Parameters[0]} messages + * @param {Parameters[1]} options + * @returns {ReturnType} + */ + apply_chat_template(messages, options = {}) { + if (!this.tokenizer) { + throw new Error('Unable to apply chat template without a tokenizer.'); + } + return this.tokenizer.apply_chat_template(messages, { + tokenize: false, // default to false + ...options, + }); + } + + /** + * @param {Parameters} args + * @returns {ReturnType} + */ + batch_decode(...args) { + if (!this.tokenizer) { + throw new Error('Unable to decode without a tokenizer.'); + } + return this.tokenizer.batch_decode(...args); + } + + /** + * @param {Parameters} args + * @returns {ReturnType} + */ + decode(...args) { + if (!this.tokenizer) { + throw new Error('Unable to decode without a tokenizer.'); + } + return this.tokenizer.decode(...args); + } + + + /** + * Calls the feature_extractor function with the given input. + * @param {any} input The input to extract features from. + * @param {...any} args Additional arguments. + * @returns {Promise} A Promise that resolves with the extracted features. + */ + async _call(input, ...args) { + for (const item of [this.image_processor, this.feature_extractor, this.tokenizer]) { + if (item) { + return item(input, ...args); + } + } + throw new Error('No image processor, feature extractor, or tokenizer found.'); + } + + + /** + * Instantiate one of the processor classes of the library from a pretrained model. + * + * The processor class to instantiate is selected based on the `image_processor_type` (or `feature_extractor_type`; legacy) + * property of the config object (either passed as an argument or loaded from `pretrained_model_name_or_path` if possible) + * + * @param {string} pretrained_model_name_or_path The name or path of the pretrained model. Can be either: + * - A string, the *model id* of a pretrained processor hosted inside a model repo on huggingface.co. + * Valid model ids can be located at the root-level, like `bert-base-uncased`, or namespaced under a + * user or organization name, like `dbmdz/bert-base-german-cased`. + * - A path to a *directory* containing processor files, e.g., `./my_model_directory/`. + * @param {PretrainedProcessorOptions} options Additional options for loading the processor. + * + * @returns {Promise} A new instance of the Processor class. + */ + static async from_pretrained(pretrained_model_name_or_path, options) { + + const [config, components] = await Promise.all([ + // TODO: + this.uses_processor_config + ? (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_2__.getModelJSON)(pretrained_model_name_or_path, _utils_constants_js__WEBPACK_IMPORTED_MODULE_0__.PROCESSOR_NAME, true, options) + : {}, + Promise.all( + this.classes + .filter((cls) => cls in this) + .map(async (cls) => { + const component = await this[cls].from_pretrained(pretrained_model_name_or_path, options); + return [cls.replace(/_class$/, ''), component]; + }) + ).then(Object.fromEntries) + ]); + + return new this(config, components); + } +} + + +/***/ }), + +/***/ "./src/configs.js": +/*!************************!*\ + !*** ./src/configs.js ***! + \************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ AutoConfig: () => (/* binding */ AutoConfig), +/* harmony export */ PretrainedConfig: () => (/* binding */ PretrainedConfig), +/* harmony export */ getKeyValueShapes: () => (/* binding */ getKeyValueShapes) +/* harmony export */ }); +/* harmony import */ var _utils_core_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./utils/core.js */ "./src/utils/core.js"); +/* harmony import */ var _utils_hub_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./utils/hub.js */ "./src/utils/hub.js"); + +/** + * @file Helper module for using model configs. For more information, see the corresponding + * [Python documentation](https://huggingface.co/docs/transformers/main/en/model_doc/auto#transformers.AutoConfig). + * + * **Example:** Load an `AutoConfig`. + * + * ```javascript + * import { AutoConfig } from '@huggingface/transformers'; + * const config = await AutoConfig.from_pretrained('bert-base-uncased'); + * console.log(config); + * // PretrainedConfig { + * // "model_type": "bert", + * // "is_encoder_decoder": false, + * // "architectures": [ + * // "BertForMaskedLM" + * // ], + * // "vocab_size": 30522 + * // "num_attention_heads": 12, + * // "num_hidden_layers": 12, + * // "hidden_size": 768, + * // "max_position_embeddings": 512, + * // ... + * // } + * ``` + * + * @module configs + */ + + + + +/** + * @typedef {import('./utils/hub.js').PretrainedOptions} PretrainedOptions + */ + +/** + * @typedef {import('./utils/core.js').ProgressCallback} ProgressCallback + */ + +/** + * @typedef {import('./utils/core.js').ProgressInfo} ProgressInfo + */ + +/** + * Loads a config from the specified path. + * @param {string} pretrained_model_name_or_path The path to the config directory. + * @param {PretrainedOptions} options Additional options for loading the config. + * @returns {Promise} A promise that resolves with information about the loaded config. + */ +async function loadConfig(pretrained_model_name_or_path, options) { + return await (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_1__.getModelJSON)(pretrained_model_name_or_path, 'config.json', true, options); +} + +/** + * + * @param {PretrainedConfig} config + * @returns {Object} The normalized configuration. + */ +function getNormalizedConfig(config) { + const mapping = {}; + + let init_normalized_config = {}; + switch (config.model_type) { + // Sub-configs + case 'llava': + case 'paligemma': + case 'gemma3': + case 'florence2': + case 'llava_onevision': + case 'idefics3': + case 'ultravox': + case 'smolvlm': + // @ts-expect-error TS2339 + init_normalized_config = getNormalizedConfig(config.text_config); + break; + case 'moondream1': + // @ts-expect-error TS2339 + init_normalized_config = getNormalizedConfig(config.phi_config); + break; + case 'musicgen': + // @ts-expect-error TS2339 + init_normalized_config = getNormalizedConfig(config.decoder); + break; + case 'multi_modality': + // @ts-expect-error TS2339 + init_normalized_config = getNormalizedConfig(config.language_config); + break; + + // Decoder-only models + case 'gpt2': + case 'gptj': + case 'jais': + case 'codegen': + case 'gpt_bigcode': + mapping['num_heads'] = 'n_head'; + mapping['num_layers'] = 'n_layer'; + mapping['hidden_size'] = 'n_embd'; + break; + case 'gpt_neox': + case 'stablelm': + case 'opt': + case 'falcon': + mapping['num_heads'] = 'num_attention_heads'; + mapping['num_layers'] = 'num_hidden_layers'; + mapping['hidden_size'] = 'hidden_size'; + break; + case 'llama': + case 'olmo': + case 'olmo2': + case 'mobilellm': + case 'granite': + case 'cohere': + case 'mistral': + case 'starcoder2': + case 'qwen2': + case 'qwen2_vl': + case 'phi': + case 'phi3': + case 'phi3_v': + mapping['num_heads'] = 'num_key_value_heads'; + mapping['num_layers'] = 'num_hidden_layers'; + mapping['hidden_size'] = 'hidden_size'; + mapping['num_attention_heads'] = 'num_attention_heads'; + break; + case 'qwen3': + case 'gemma': + case 'gemma2': + case 'gemma3_text': + case 'glm': + case 'helium': + mapping['num_heads'] = 'num_key_value_heads'; + mapping['num_layers'] = 'num_hidden_layers'; + mapping['dim_kv'] = 'head_dim'; + break; + case 'openelm': + mapping['num_heads'] = 'num_kv_heads'; + mapping['num_layers'] = 'num_transformer_layers'; + mapping['dim_kv'] = 'head_dim'; + break; + case 'gpt_neo': + case 'donut-swin': + mapping['num_heads'] = 'num_heads'; + mapping['num_layers'] = 'num_layers'; + mapping['hidden_size'] = 'hidden_size'; + break; + case 'bloom': + mapping['num_heads'] = 'n_head'; + mapping['num_layers'] = 'n_layer'; + mapping['hidden_size'] = 'hidden_size'; + break; + case 'mpt': + mapping['num_heads'] = 'n_heads'; + mapping['num_layers'] = 'n_layers'; + mapping['hidden_size'] = 'd_model'; + break; + case 'exaone': + mapping['num_heads'] = 'num_key_value_heads'; + mapping['num_layers'] = 'num_layers'; + mapping['dim_kv'] = 'head_dim'; + mapping['num_attention_heads'] = 'num_attention_heads'; + break; + + // Encoder-decoder models + case 't5': + case 'mt5': + case 'longt5': + mapping['num_decoder_layers'] = 'num_decoder_layers'; + mapping['num_decoder_heads'] = 'num_heads'; + mapping['decoder_dim_kv'] = 'd_kv'; + mapping['num_encoder_layers'] = 'num_layers'; + mapping['num_encoder_heads'] = 'num_heads'; + mapping['encoder_dim_kv'] = 'd_kv'; + break; + case 'bart': + case 'mbart': + case 'marian': + case 'whisper': + case 'lite-whisper': + case 'm2m_100': + case 'blenderbot': + case 'blenderbot-small': + case 'florence2_language': + mapping['num_decoder_layers'] = 'decoder_layers'; + mapping['num_decoder_heads'] = 'decoder_attention_heads'; + mapping['decoder_hidden_size'] = 'd_model'; + mapping['num_encoder_layers'] = 'encoder_layers'; + mapping['num_encoder_heads'] = 'encoder_attention_heads'; + mapping['encoder_hidden_size'] = 'd_model'; + break; + case 'speecht5': + mapping['num_decoder_layers'] = 'decoder_layers'; + mapping['num_decoder_heads'] = 'decoder_attention_heads'; + mapping['decoder_hidden_size'] = 'hidden_size'; + mapping['num_encoder_layers'] = 'encoder_layers'; + mapping['num_encoder_heads'] = 'encoder_attention_heads'; + mapping['encoder_hidden_size'] = 'hidden_size'; + break; + case 'trocr': + mapping['num_encoder_layers'] = mapping['num_decoder_layers'] = 'decoder_layers'; + mapping['num_encoder_heads'] = mapping['num_decoder_heads'] = 'decoder_attention_heads'; + mapping['encoder_hidden_size'] = mapping['decoder_hidden_size'] = 'd_model'; + break; + case 'musicgen_decoder': + mapping['num_encoder_layers'] = mapping['num_decoder_layers'] = 'num_hidden_layers'; + mapping['num_encoder_heads'] = mapping['num_decoder_heads'] = 'num_attention_heads'; + mapping['encoder_hidden_size'] = mapping['decoder_hidden_size'] = 'hidden_size'; + break; + case 'moonshine': + mapping['num_decoder_layers'] = 'decoder_num_hidden_layers'; + mapping['num_decoder_heads'] = 'decoder_num_key_value_heads'; + mapping['num_encoder_layers'] = 'encoder_num_hidden_layers'; + mapping['num_encoder_heads'] = 'encoder_num_key_value_heads'; + mapping['encoder_hidden_size'] = mapping['decoder_hidden_size'] = 'hidden_size'; + break; + case 'vision-encoder-decoder': + // @ts-expect-error TS2339 + const decoderConfig = getNormalizedConfig(config.decoder); + + const add_encoder_pkv = 'num_decoder_layers' in decoderConfig; + const result = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_0__.pick)(config, ['model_type', 'is_encoder_decoder']); + if (add_encoder_pkv) { + // Decoder is part of an encoder-decoder model + result.num_decoder_layers = decoderConfig.num_decoder_layers; + result.num_decoder_heads = decoderConfig.num_decoder_heads; + result.decoder_hidden_size = decoderConfig.decoder_hidden_size; + + result.num_encoder_layers = decoderConfig.num_encoder_layers; + result.num_encoder_heads = decoderConfig.num_encoder_heads; + result.encoder_hidden_size = decoderConfig.encoder_hidden_size; + } else { + // Decoder is a decoder-only model + result.num_layers = decoderConfig.num_layers; + result.num_heads = decoderConfig.num_heads; + result.hidden_size = decoderConfig.hidden_size; + } + return result; + + } + + // NOTE: If `num_attention_heads` is not set, it is assumed to be equal to `num_heads` + const normalized_config = { + ...init_normalized_config, + ...(0,_utils_core_js__WEBPACK_IMPORTED_MODULE_0__.pick)(config, ['model_type', 'multi_query', 'is_encoder_decoder']), + }; + for (const key in mapping) { + normalized_config[key] = config[mapping[key]]; + } + return normalized_config; +} + +/** + * + * @param {PretrainedConfig} config + * @returns {Record} + */ +function getKeyValueShapes(config, { + prefix = 'past_key_values', + batch_size=1, +} = {}) { + /** @type {Record} */ + const decoderFeeds = {}; + const normalized_config = config.normalized_config; + + if (normalized_config.is_encoder_decoder && ( + 'num_encoder_heads' in normalized_config && 'num_decoder_heads' in normalized_config + )) { + const encoder_dim_kv = normalized_config.encoder_dim_kv ?? ( + normalized_config.encoder_hidden_size / normalized_config.num_encoder_heads + ); + const decoder_dim_kv = normalized_config.decoder_dim_kv ?? ( + normalized_config.decoder_hidden_size / normalized_config.num_decoder_heads + ); + + const encoder_dims = [batch_size, normalized_config.num_encoder_heads, 0, encoder_dim_kv]; + const decoder_dims = [batch_size, normalized_config.num_decoder_heads, 0, decoder_dim_kv]; + for (let i = 0; i < normalized_config.num_decoder_layers; ++i) { + decoderFeeds[`${prefix}.${i}.encoder.key`] = encoder_dims; + decoderFeeds[`${prefix}.${i}.encoder.value`] = encoder_dims; + decoderFeeds[`${prefix}.${i}.decoder.key`] = decoder_dims; + decoderFeeds[`${prefix}.${i}.decoder.value`] = decoder_dims; + } + } else { // Decoders + const num_heads = normalized_config.num_heads; + const num_layers = normalized_config.num_layers; + const dim_kv = normalized_config.dim_kv ?? ( + normalized_config.hidden_size / + (normalized_config.num_attention_heads ?? num_heads) + ); + + if (normalized_config.model_type === 'falcon') { + // NOTE: Custom implementation for Falcon + const dims = [batch_size * num_heads, 0, dim_kv] + for (let i = 0; i < num_layers; ++i) { + decoderFeeds[`${prefix}.${i}.key`] = dims; + decoderFeeds[`${prefix}.${i}.value`] = dims; + } + } else if (normalized_config.multi_query) { // e.g., for `gpt_bigcode` + const dims = [batch_size * num_heads, 0, 2 * dim_kv] + + for (let i = 0; i < num_layers; ++i) { + decoderFeeds[`${prefix}.${i}.key_value`] = dims; + } + } else if (normalized_config.model_type === 'bloom') { + // NOTE: Custom implementation for Bloom + + const keyDims = [batch_size * num_heads, dim_kv, 0] // [batch_size x num_heads,64,past_sequence_length] + const valueDims = [batch_size * num_heads, 0, dim_kv] // [batch_size x num_heads,past_sequence_length,64] + for (let i = 0; i < num_layers; ++i) { + decoderFeeds[`${prefix}.${i}.key`] = keyDims; + decoderFeeds[`${prefix}.${i}.value`] = valueDims; + } + } else if (normalized_config.model_type === 'openelm') { + for (let i = 0; i < num_layers; ++i) { + const dims = [batch_size, num_heads[i], 0, dim_kv] + + decoderFeeds[`${prefix}.${i}.key`] = dims; + decoderFeeds[`${prefix}.${i}.value`] = dims; + } + } else { // Decoder-only + const dims = [batch_size, num_heads, 0, dim_kv] + for (let i = 0; i < num_layers; ++i) { + decoderFeeds[`${prefix}.${i}.key`] = dims; + decoderFeeds[`${prefix}.${i}.value`] = dims; + } + } + } + + return decoderFeeds; +} +/** + * Base class for all configuration classes. For more information, see the corresponding + * [Python documentation](https://huggingface.co/docs/transformers/main/en/main_classes/configuration#transformers.PretrainedConfig). + */ +class PretrainedConfig { + // NOTE: Typo in original + + /** @type {string|null} */ + model_type = null; + + /** @type {boolean} */ + is_encoder_decoder = false; + + /** @type {number} */ + max_position_embeddings; + + /** @type {TransformersJSConfig} */ + 'transformers.js_config'; + + /** + * Create a new PreTrainedTokenizer instance. + * @param {Object} configJSON The JSON of the config. + */ + constructor(configJSON) { + Object.assign(this, configJSON); + this.normalized_config = getNormalizedConfig(this); + } + + /** + * Loads a pre-trained config from the given `pretrained_model_name_or_path`. + * + * @param {string} pretrained_model_name_or_path The path to the pre-trained config. + * @param {PretrainedOptions} options Additional options for loading the config. + * @throws {Error} Throws an error if the config.json is not found in the `pretrained_model_name_or_path`. + * + * @returns {Promise} A new instance of the `PretrainedConfig` class. + */ + static async from_pretrained(pretrained_model_name_or_path, { + progress_callback = null, + config = null, + cache_dir = null, + local_files_only = false, + revision = 'main', + } = {}) { + if (config && !(config instanceof PretrainedConfig)) { + config = new PretrainedConfig(config); + } + + const data = config ?? await loadConfig(pretrained_model_name_or_path, { + progress_callback, + config, + cache_dir, + local_files_only, + revision, + }) + return new this(data); + } +} + +/** + * Helper class which is used to instantiate pretrained configs with the `from_pretrained` function. + * + * @example + * const config = await AutoConfig.from_pretrained('Xenova/bert-base-uncased'); + */ +class AutoConfig { + /** @type {typeof PretrainedConfig.from_pretrained} */ + static async from_pretrained(...args) { + return PretrainedConfig.from_pretrained(...args); + } +} + +/** + * Transformers.js-specific configuration, possibly present in config.json under the key `transformers.js_config`. + * @typedef {Object} TransformersJSConfig + * @property {Record} [device_config] Device-specific configurations. + * @property {import('./utils/tensor.js').DataType|Record} [kv_cache_dtype] The data type of the key-value cache. + * @property {Record} [free_dimension_overrides] Override the free dimensions of the model. + * See https://onnxruntime.ai/docs/tutorials/web/env-flags-and-session-options.html#freedimensionoverrides + * for more information. + * @property {import('./utils/devices.js').DeviceType} [device] The default device to use for the model. + * @property {import('./utils/dtypes.js').DataType|Record} [dtype] The default data type to use for the model. + * @property {import('./utils/hub.js').ExternalData|Record} [use_external_data_format=false] Whether to load the model using the external data format (used for models >= 2GB in size). + */ + +/** + * Device-specific configuration options. + * @typedef {Omit} DeviceConfig + */ + + +/***/ }), + +/***/ "./src/env.js": +/*!********************!*\ + !*** ./src/env.js ***! + \********************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ apis: () => (/* binding */ apis), +/* harmony export */ env: () => (/* binding */ env) +/* harmony export */ }); +/* harmony import */ var fs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! fs */ "?569f"); +/* harmony import */ var path__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! path */ "?3f59"); +/* harmony import */ var url__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! url */ "?154a"); +/** + * @file Module used to configure Transformers.js. + * + * **Example:** Disable remote models. + * ```javascript + * import { env } from '@huggingface/transformers'; + * env.allowRemoteModels = false; + * ``` + * + * **Example:** Set local model path. + * ```javascript + * import { env } from '@huggingface/transformers'; + * env.localModelPath = '/path/to/local/models/'; + * ``` + * + * **Example:** Set cache directory. + * ```javascript + * import { env } from '@huggingface/transformers'; + * env.cacheDir = '/path/to/cache/directory/'; + * ``` + * + * @module env + */ + + + + + +const VERSION = '3.5.1'; + +// Check if various APIs are available (depends on environment) +const IS_BROWSER_ENV = typeof window !== "undefined" && typeof window.document !== "undefined"; +const IS_WEBWORKER_ENV = typeof self !== "undefined" && self.constructor?.name === 'DedicatedWorkerGlobalScope'; +const IS_WEB_CACHE_AVAILABLE = typeof self !== "undefined" && 'caches' in self; +const IS_WEBGPU_AVAILABLE = typeof navigator !== 'undefined' && 'gpu' in navigator; +const IS_WEBNN_AVAILABLE = typeof navigator !== 'undefined' && 'ml' in navigator; + +const IS_PROCESS_AVAILABLE = typeof process !== 'undefined'; +const IS_NODE_ENV = IS_PROCESS_AVAILABLE && process?.release?.name === 'node'; +const IS_FS_AVAILABLE = !isEmpty(fs__WEBPACK_IMPORTED_MODULE_0__); +const IS_PATH_AVAILABLE = !isEmpty(path__WEBPACK_IMPORTED_MODULE_1__); + +/** + * A read-only object containing information about the APIs available in the current environment. + */ +const apis = Object.freeze({ + /** Whether we are running in a browser environment (and not a web worker) */ + IS_BROWSER_ENV, + + /** Whether we are running in a web worker environment */ + IS_WEBWORKER_ENV, + + /** Whether the Cache API is available */ + IS_WEB_CACHE_AVAILABLE, + + /** Whether the WebGPU API is available */ + IS_WEBGPU_AVAILABLE, + + /** Whether the WebNN API is available */ + IS_WEBNN_AVAILABLE, + + /** Whether the Node.js process API is available */ + IS_PROCESS_AVAILABLE, + + /** Whether we are running in a Node.js environment */ + IS_NODE_ENV, + + /** Whether the filesystem API is available */ + IS_FS_AVAILABLE, + + /** Whether the path API is available */ + IS_PATH_AVAILABLE, +}); + +const RUNNING_LOCALLY = IS_FS_AVAILABLE && IS_PATH_AVAILABLE; + +let dirname__ = './'; +if (RUNNING_LOCALLY) { + // NOTE: We wrap `import.meta` in a call to `Object` to prevent Webpack from trying to bundle it in CommonJS. + // Although we get the warning: "Accessing import.meta directly is unsupported (only property access or destructuring is supported)", + // it is safe to ignore since the bundled value (`{}`) isn't used for CommonJS environments (we use __dirname instead). + const _import_meta_url = Object(import.meta).url; + + if (_import_meta_url) { + dirname__ = path__WEBPACK_IMPORTED_MODULE_1__.dirname(path__WEBPACK_IMPORTED_MODULE_1__.dirname(url__WEBPACK_IMPORTED_MODULE_2__.fileURLToPath(_import_meta_url))) // ESM + } else if (typeof __dirname !== 'undefined') { + dirname__ = path__WEBPACK_IMPORTED_MODULE_1__.dirname(__dirname) // CommonJS + } +} + +// Only used for environments with access to file system +const DEFAULT_CACHE_DIR = RUNNING_LOCALLY + ? path__WEBPACK_IMPORTED_MODULE_1__.join(dirname__, '/.cache/') + : null; + +// Set local model path, based on available APIs +const DEFAULT_LOCAL_MODEL_PATH = '/models/'; +const localModelPath = RUNNING_LOCALLY + ? path__WEBPACK_IMPORTED_MODULE_1__.join(dirname__, DEFAULT_LOCAL_MODEL_PATH) + : DEFAULT_LOCAL_MODEL_PATH; + +/** + * Global variable given visible to users to control execution. This provides users a simple way to configure Transformers.js. + * @typedef {Object} TransformersEnvironment + * @property {string} version This version of Transformers.js. + * @property {{onnx: Partial}} backends Expose environment variables of different backends, + * allowing users to set these variables if they want to. + * @property {boolean} allowRemoteModels Whether to allow loading of remote files, defaults to `true`. + * If set to `false`, it will have the same effect as setting `local_files_only=true` when loading pipelines, models, tokenizers, processors, etc. + * @property {string} remoteHost Host URL to load models from. Defaults to the Hugging Face Hub. + * @property {string} remotePathTemplate Path template to fill in and append to `remoteHost` when loading models. + * @property {boolean} allowLocalModels Whether to allow loading of local files, defaults to `false` if running in-browser, and `true` otherwise. + * If set to `false`, it will skip the local file check and try to load the model from the remote host. + * @property {string} localModelPath Path to load local models from. Defaults to `/models/`. + * @property {boolean} useFS Whether to use the file system to load files. By default, it is `true` if available. + * @property {boolean} useBrowserCache Whether to use Cache API to cache models. By default, it is `true` if available. + * @property {boolean} useFSCache Whether to use the file system to cache files. By default, it is `true` if available. + * @property {string} cacheDir The directory to use for caching files with the file system. By default, it is `./.cache`. + * @property {boolean} useCustomCache Whether to use a custom cache system (defined by `customCache`), defaults to `false`. + * @property {Object} customCache The custom cache to use. Defaults to `null`. Note: this must be an object which + * implements the `match` and `put` functions of the Web Cache API. For more information, see https://developer.mozilla.org/en-US/docs/Web/API/Cache. + * If you wish, you may also return a `Promise` from the `match` function if you'd like to use a file path instead of `Promise`. + */ + +/** @type {TransformersEnvironment} */ +const env = { + version: VERSION, + + /////////////////// Backends settings /////////////////// + // NOTE: These will be populated later by the backends themselves. + backends: { + // onnxruntime-web/onnxruntime-node + onnx: {}, + }, + + /////////////////// Model settings /////////////////// + allowRemoteModels: true, + remoteHost: 'https://huggingface.co/', + remotePathTemplate: '{model}/resolve/{revision}/', + + allowLocalModels: !(IS_BROWSER_ENV || IS_WEBWORKER_ENV), + localModelPath: localModelPath, + useFS: IS_FS_AVAILABLE, + + /////////////////// Cache settings /////////////////// + useBrowserCache: IS_WEB_CACHE_AVAILABLE, + + useFSCache: IS_FS_AVAILABLE, + cacheDir: DEFAULT_CACHE_DIR, + + useCustomCache: false, + customCache: null, + ////////////////////////////////////////////////////// +} + + +/** + * @param {Object} obj + * @private + */ +function isEmpty(obj) { + return Object.keys(obj).length === 0; +} + + +/***/ }), + +/***/ "./src/generation/configuration_utils.js": +/*!***********************************************!*\ + !*** ./src/generation/configuration_utils.js ***! + \***********************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ GenerationConfig: () => (/* binding */ GenerationConfig) +/* harmony export */ }); +/* harmony import */ var _utils_core_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../utils/core.js */ "./src/utils/core.js"); + +/** + * @module generation/configuration_utils + */ + + + +/** + * Class that holds a configuration for a generation task. + */ +class GenerationConfig { + // Parameters that control the length of the output + /** + * The maximum length the generated tokens can have. + * Corresponds to the length of the input prompt + `max_new_tokens`. + * Its effect is overridden by `max_new_tokens`, if also set. + * @type {number} + * @default 20 + */ + max_length = 20; + + /** + * The maximum numbers of tokens to generate, ignoring the number of tokens in the prompt. + * @type {number} + * @default null + */ + max_new_tokens = null; + + /** + * The minimum length of the sequence to be generated. + * Corresponds to the length of the input prompt + `min_new_tokens`. + * Its effect is overridden by `min_new_tokens`, if also set. + * @type {number} + * @default 0 + */ + min_length = 0; + + /** + * The minimum numbers of tokens to generate, ignoring the number of tokens in the prompt. + * @type {number} + * @default null + */ + min_new_tokens = null; + + /** + * Controls the stopping condition for beam-based methods, like beam-search. It accepts the following values: + * - `true`, where the generation stops as soon as there are `num_beams` complete candidates; + * - `false`, where an heuristic is applied and the generation stops when is it very unlikely to find better candidates; + * - `"never"`, where the beam search procedure only stops when there cannot be better candidates (canonical beam search algorithm). + * @type {boolean|"never"} + * @default false + */ + early_stopping = false; + + /** + * The maximum amount of time you allow the computation to run for in seconds. + * Generation will still finish the current pass after allocated time has been passed. + * @type {number} + * @default null + */ + max_time = null; + + // Parameters that control the generation strategy used + /** + * Whether or not to use sampling; use greedy decoding otherwise. + * @type {boolean} + * @default false + */ + do_sample = false; + + /** + * Number of beams for beam search. 1 means no beam search. + * @type {number} + * @default 1 + */ + num_beams = 1; + + /** + * Number of groups to divide `num_beams` into in order to ensure diversity among different groups of beams. + * See [this paper](https://arxiv.org/pdf/1610.02424.pdf) for more details. + * @type {number} + * @default 1 + */ + num_beam_groups = 1; + + /** + * The values balance the model confidence and the degeneration penalty in contrastive search decoding. + * @type {number} + * @default null + */ + penalty_alpha = null; + + /** + * Whether or not the model should use the past last key/values attentions (if applicable to the model) to speed up decoding. + * @type {boolean} + * @default true + */ + use_cache = true; + + // Parameters for manipulation of the model output logits + /** + * The value used to modulate the next token probabilities. + * @type {number} + * @default 1.0 + */ + temperature = 1.0; + + /** + * The number of highest probability vocabulary tokens to keep for top-k-filtering. + * @type {number} + * @default 50 + */ + top_k = 50; + + /** + * If set to float < 1, only the smallest set of most probable tokens with probabilities that add up to `top_p` or higher are kept for generation. + * @type {number} + * @default 1.0 + */ + top_p = 1.0; + + /** + * Local typicality measures how similar the conditional probability of predicting a target token next is to the expected conditional probability of predicting a random token next, given the partial text already generated. + * If set to float < 1, the smallest set of the most locally typical tokens with probabilities that add up to `typical_p` or higher are kept for generation. + * See [this paper](https://arxiv.org/pdf/2202.00666.pdf) for more details. + * @type {number} + * @default 1.0 + */ + typical_p = 1.0; + + /** + * If set to float strictly between 0 and 1, only tokens with a conditional probability greater than `epsilon_cutoff` will be sampled. + * In the paper, suggested values range from 3e-4 to 9e-4, depending on the size of the model. + * See [Truncation Sampling as Language Model Desmoothing](https://arxiv.org/abs/2210.15191) for more details. + * @type {number} + * @default 0.0 + */ + epsilon_cutoff = 0.0; + + /** + * Eta sampling is a hybrid of locally typical sampling and epsilon sampling. + * If set to float strictly between 0 and 1, a token is only considered if it is greater than either `eta_cutoff` or `sqrt(eta_cutoff) * exp(-entropy(softmax(next_token_logits)))`. + * The latter term is intuitively the expected next token probability, scaled by `sqrt(eta_cutoff)`. In the paper, suggested values range from 3e-4 to 2e-3, depending on the size of the model. + * See [Truncation Sampling as Language Model Desmoothing](https://arxiv.org/abs/2210.15191) for more details. + * @type {number} + * @default 0.0 + */ + eta_cutoff = 0.0; + + /** + * This value is subtracted from a beam's score if it generates a token same as any beam from other group at a particular time. + * Note that `diversity_penalty` is only effective if `group beam search` is enabled. + * @type {number} + * @default 0.0 + */ + diversity_penalty = 0.0; + + /** + * The parameter for repetition penalty. 1.0 means no penalty. + * See [this paper](https://arxiv.org/pdf/1909.05858.pdf) for more details. + * @type {number} + * @default 1.0 + */ + repetition_penalty = 1.0; + + /** + * The paramater for encoder_repetition_penalty. + * An exponential penalty on sequences that are not in the original input. + * 1.0 means no penalty. + * @type {number} + * @default 1.0 + */ + encoder_repetition_penalty = 1.0; + + /** + * Exponential penalty to the length that is used with beam-based generation. + * It is applied as an exponent to the sequence length, which in turn is used to divide the score of the sequence. + * Since the score is the log likelihood of the sequence (i.e. negative), `length_penalty` > 0.0 promotes longer sequences, while `length_penalty` < 0.0 encourages shorter sequences. + * @type {number} + * @default 1.0 + */ + length_penalty = 1.0; + + /** + * If set to int > 0, all ngrams of that size can only occur once. + * @type {number} + * @default 0 + */ + no_repeat_ngram_size = 0; + + /** + * List of token ids that are not allowed to be generated. + * In order to get the token ids of the words that should not appear in the generated text, use + * `tokenizer(bad_words, { add_prefix_space: true, add_special_tokens: false }).input_ids`. + * @type {number[][]} + * @default null + */ + bad_words_ids = null; + + /** + * List of token ids that must be generated. + * If given a `number[][]`, this is treated as a simple list of words that must be included, the opposite to `bad_words_ids`. + * If given `number[][][]`, this triggers a [disjunctive constraint](https://github.com/huggingface/transformers/issues/14081), where one can allow different forms of each word. + * @type {number[][]|number[][][]} + * @default null + */ + force_words_ids = null; + + /** + * Whether to renormalize the logits after applying all the logits processors or warpers (including the custom ones). + * It's highly recommended to set this flag to `true` as the search algorithms suppose the score logits are normalized but some logit processors or warpers break the normalization. + * @type {boolean} + * @default false + */ + renormalize_logits = false; + + /** + * Custom constraints that can be added to the generation to ensure that the output will contain the use of certain tokens as defined by `Constraint` objects, in the most sensible way possible. + * @type {Object[]} + * @default null + */ + constraints = null; + + /** + * The id of the token to force as the first generated token after the `decoder_start_token_id`. + * Useful for multilingual models like mBART where the first generated token needs to be the target language token. + * @type {number} + * @default null + */ + forced_bos_token_id = null; + + /** + * The id of the token to force as the last generated token when `max_length` is reached. + * Optionally, use a list to set multiple *end-of-sequence* tokens. + * @type {number|number[]} + * @default null + */ + forced_eos_token_id = null; + + /** + * Whether to remove possible *nan* and *inf* outputs of the model to prevent the generation method to crash. Note that using `remove_invalid_values` can slow down generation. + * @type {boolean} + */ + remove_invalid_values = false; + + /** + * This Tuple adds an exponentially increasing length penalty, after a certain amount of tokens have been generated. + * The tuple shall consist of: `(start_index, decay_factor)` where `start_index` indicates where penalty starts and `decay_factor` represents the factor of exponential decay. + * @type {[number, number]} + * @default null + */ + exponential_decay_length_penalty = null; + + /** + * A list of tokens that will be suppressed at generation. + * The `SuppressTokens` logit processor will set their log probs to `-inf` so that they are not sampled. + * @type {number[]} + * @default null + */ + suppress_tokens = null; + + /** + * A streamer that will be used to stream the generation. + * @type {import('./streamers.js').TextStreamer} + * @default null + */ + streamer = null; + + /** + * A list of tokens that will be suppressed at the beginning of the generation. + * The `SuppressBeginTokens` logit processor will set their log probs to `-inf` so that they are not sampled. + * @type {number[]} + * @default null + */ + begin_suppress_tokens = null; + + /** + * A list of pairs of integers which indicates a mapping from generation indices to token indices that will be forced before sampling. + * For example, `[[1, 123]]` means the second generated token will always be a token of index 123. + * @type {[number, number][]} + * @default null + */ + forced_decoder_ids = null; + + /** + * The guidance scale for classifier free guidance (CFG). CFG is enabled by setting `guidance_scale > 1`. + * Higher guidance scale encourages the model to generate samples that are more closely linked to the input + * prompt, usually at the expense of poorer quality. + * @type {number} + * @default null + */ + guidance_scale = null; + + // Parameters that define the output variables of `generate` + /** + * The number of independently computed returned sequences for each element in the batch. + * @type {number} + * @default 1 + */ + num_return_sequences = 1; + + /** + * Whether or not to return the attentions tensors of all attention layers. + * See `attentions` under returned tensors for more details. + * @type {boolean} + * @default false + */ + output_attentions = false; + + /** + * Whether or not to return the hidden states of all layers. + * See `hidden_states` under returned tensors for more details. + * @type {boolean} + * @default false + */ + output_hidden_states = false; + + /** + * Whether or not to return the prediction scores. + * See `scores` under returned tensors for more details. + * @type {boolean} + * @default false + */ + output_scores = false; + + /** + * Whether or not to return a `ModelOutput` instead of a plain tuple. + * @type {boolean} + * @default false + */ + return_dict_in_generate = false; + + // Special tokens that can be used at generation time + /** + * The id of the *padding* token. + * @type {number} + * @default null + */ + pad_token_id = null; + + /** + * The id of the *beginning-of-sequence* token. + * @type {number} + * @default null + */ + bos_token_id = null; + + /** + * The id of the *end-of-sequence* token. + * Optionally, use a list to set multiple *end-of-sequence* tokens. + * @type {number|number[]} + * @default null + */ + eos_token_id = null; + + // Generation parameters exclusive to encoder-decoder models + /** + * If set to int > 0, all ngrams of that size that occur in the `encoder_input_ids` cannot occur in the `decoder_input_ids`. + * @type {number} + * @default 0 + */ + encoder_no_repeat_ngram_size = 0; + + /** + * If an encoder-decoder model starts decoding with a different token than *bos*, the id of that token. + * @type {number} + * @default null + */ + decoder_start_token_id = null; + + // Wild card + /** + * Additional generation kwargs will be forwarded to the `generate` function of the model. + * Kwargs that are not present in `generate`'s signature will be used in the model forward pass. + * @type {Object} + * @default {} + */ + generation_kwargs = {}; + + /** + * + * @param {GenerationConfig|import('../configs.js').PretrainedConfig} config + */ + constructor(config) { + Object.assign(this, (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_0__.pick)(config, Object.getOwnPropertyNames(this))); + } +} + + + +/***/ }), + +/***/ "./src/generation/logits_process.js": +/*!******************************************!*\ + !*** ./src/generation/logits_process.js ***! + \******************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ ClassifierFreeGuidanceLogitsProcessor: () => (/* binding */ ClassifierFreeGuidanceLogitsProcessor), +/* harmony export */ ForcedBOSTokenLogitsProcessor: () => (/* binding */ ForcedBOSTokenLogitsProcessor), +/* harmony export */ ForcedEOSTokenLogitsProcessor: () => (/* binding */ ForcedEOSTokenLogitsProcessor), +/* harmony export */ LogitsProcessor: () => (/* binding */ LogitsProcessor), +/* harmony export */ LogitsProcessorList: () => (/* binding */ LogitsProcessorList), +/* harmony export */ LogitsWarper: () => (/* binding */ LogitsWarper), +/* harmony export */ MinLengthLogitsProcessor: () => (/* binding */ MinLengthLogitsProcessor), +/* harmony export */ MinNewTokensLengthLogitsProcessor: () => (/* binding */ MinNewTokensLengthLogitsProcessor), +/* harmony export */ NoBadWordsLogitsProcessor: () => (/* binding */ NoBadWordsLogitsProcessor), +/* harmony export */ NoRepeatNGramLogitsProcessor: () => (/* binding */ NoRepeatNGramLogitsProcessor), +/* harmony export */ RepetitionPenaltyLogitsProcessor: () => (/* binding */ RepetitionPenaltyLogitsProcessor), +/* harmony export */ SuppressTokensAtBeginLogitsProcessor: () => (/* binding */ SuppressTokensAtBeginLogitsProcessor), +/* harmony export */ TemperatureLogitsWarper: () => (/* binding */ TemperatureLogitsWarper), +/* harmony export */ TopKLogitsWarper: () => (/* binding */ TopKLogitsWarper), +/* harmony export */ TopPLogitsWarper: () => (/* binding */ TopPLogitsWarper), +/* harmony export */ WhisperTimeStampLogitsProcessor: () => (/* binding */ WhisperTimeStampLogitsProcessor) +/* harmony export */ }); +/* harmony import */ var _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../utils/generic.js */ "./src/utils/generic.js"); +/* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../utils/tensor.js */ "./src/utils/tensor.js"); +/* harmony import */ var _utils_maths_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../utils/maths.js */ "./src/utils/maths.js"); + +/** + * @module generation/logits_process + */ + + + + + + +/** + * Abstract base class for all logit processors that can be applied during generation. + */ +class LogitsProcessor extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * Apply the processor to the input logits. + * + * @abstract + * @param {bigint[][]} input_ids The input ids. + * @param {Tensor} logits The logits to process. + * @throws {Error} Throws an error if `_call` is not implemented in the subclass. + */ + _call(input_ids, logits) { + throw Error("`_call` should be implemented in a subclass") + } +} + + +/** + * Abstract base class for all logit warpers that can be applied during generation with multinomial sampling. + */ +class LogitsWarper extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * Apply the processor to the input logits. + * + * @abstract + * @param {bigint[][]} input_ids The input ids. + * @param {Tensor} logits The logits to process. + * @throws {Error} Throws an error if `_call` is not implemented in the subclass. + */ + _call(input_ids, logits) { + throw Error("`_call` should be implemented in a subclass") + } +} + + +/** + * A class representing a list of logits processors. A logits processor is a function that modifies the logits + * output of a language model. This class provides methods for adding new processors and applying all processors to a + * batch of logits. + */ +class LogitsProcessorList extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * Constructs a new instance of `LogitsProcessorList`. + */ + constructor() { + super(); + this.processors = []; + } + + /** + * Adds a new logits processor to the list. + * + * @param {LogitsProcessor} item The logits processor function to add. + */ + push(item) { + this.processors.push(item); + } + + /** + * Adds multiple logits processors to the list. + * + * @param {LogitsProcessor[]} items The logits processor functions to add. + */ + extend(items) { + this.processors.push(...items); + } + + /** + * Applies all logits processors in the list to a batch of logits, modifying them in-place. + * + * @param {bigint[][]} input_ids The input IDs for the language model. + * @param {Tensor} logits + */ + _call(input_ids, logits) { + let toReturn = logits; + // NOTE: Most processors modify logits inplace + for (const processor of this.processors) { + toReturn = processor(input_ids, toReturn); + } + return toReturn; + } + + [Symbol.iterator]() { + return this.processors.values(); + } +} + +// DEPRECATED: https://github.com/huggingface/transformers/pull/29485 +// /** +// * A logits processor that forces a specific token to be generated by the decoder. +// */ +// export class ForceTokensLogitsProcessor extends LogitsProcessor { +// /** +// * Constructs a new instance of `ForceTokensLogitsProcessor`. +// * +// * @param {[number, number][]} forced_decoder_ids The ids of tokens that should be forced. +// */ +// constructor(forced_decoder_ids) { +// super(); +// // TODO: convert to `new Map(forced_decoder_ids)` +// this.force_token_map = Object.fromEntries(forced_decoder_ids ?? []); +// } + +// /** +// * Apply the processor to the input logits. +// * +// * @param {bigint[][]} input_ids The input ids. +// * @param {Tensor} logits The logits to process. +// * @returns {Tensor} The processed logits. +// */ +// _call(input_ids, logits) { +// console.log('this.force_token_map', this.force_token_map) +// console.log('call ForceTokensLogitsProcessor', input_ids, logits) +// console.log('input_ids.length', input_ids.length) +// let map = this.force_token_map[input_ids.length]; +// if (map) { // There exists a mapping +// logits.data.fill(-Infinity) +// logits.data[map] = 0; +// } +// console.log('map', map) +// // throw Error("Not implemented") +// return logits; +// } +// } + +/** + * A LogitsProcessor that forces a BOS token at the beginning of the generated sequence. + */ +class ForcedBOSTokenLogitsProcessor extends LogitsProcessor { + /** + * Create a ForcedBOSTokenLogitsProcessor. + * @param {number} bos_token_id The ID of the beginning-of-sequence token to be forced. + */ + constructor(bos_token_id) { + super(); + this.bos_token_id = bos_token_id; + } + + /** + * Apply the BOS token forcing to the logits. + * @param {bigint[][]} input_ids The input IDs. + * @param {Tensor} logits The logits. + * @returns {Tensor} The logits with BOS token forcing. + */ + _call(input_ids, logits) { + for (let i = 0; i < input_ids.length; ++i) { + if (input_ids[i].length === 1) { + const batch_logits_data = /** @type {Float32Array} */(logits[i].data); + batch_logits_data.fill(-Infinity); + batch_logits_data[this.bos_token_id] = 0; + } + } + return logits; + } +} + +/** + * A logits processor that enforces the specified token as the last generated token when `max_length` is reached. + */ +class ForcedEOSTokenLogitsProcessor extends LogitsProcessor { + /** + * Create a ForcedEOSTokenLogitsProcessor. + * @param {number} max_length The maximum length of the sequence to be generated. + * @param {number|number[]} eos_token_id The id(s) of the *end-of-sequence* token. + */ + constructor(max_length, eos_token_id) { + super(); + this.max_length = max_length; + this.eos_token_id = Array.isArray(eos_token_id) ? eos_token_id : [eos_token_id]; + } + + /** + * Apply the processor to input_ids and logits. + * + * @param {bigint[][]} input_ids The input ids. + * @param {Tensor} logits The logits tensor. + */ + _call(input_ids, logits) { + for (let i = 0; i < input_ids.length; ++i) { + if (input_ids[i].length === this.max_length - 1) { + const batch_logits_data = /** @type {Float32Array} */(logits[i].data); + batch_logits_data.fill(-Infinity); + for (const eos_token of this.eos_token_id) { + batch_logits_data[eos_token] = 0; + } + } + } + return logits; + } +} + +/** + * A LogitsProcessor that suppresses a list of tokens as soon as the `generate` function starts + * generating using `begin_index` tokens. This should ensure that the tokens defined by + * `begin_suppress_tokens` at not sampled at the begining of the generation. + */ +class SuppressTokensAtBeginLogitsProcessor extends LogitsProcessor { + /** + * Create a SuppressTokensAtBeginLogitsProcessor. + * @param {number[]} begin_suppress_tokens The IDs of the tokens to suppress. + * @param {number} begin_index The number of tokens to generate before suppressing tokens. + */ + constructor(begin_suppress_tokens, begin_index) { + super(); + this.begin_suppress_tokens = begin_suppress_tokens; + this.begin_index = begin_index; + } + + /** + * Apply the BOS token forcing to the logits. + * @param {bigint[][]} input_ids The input IDs. + * @param {Tensor} logits The logits. + * @returns {Tensor} The logits with BOS token forcing. + */ + _call(input_ids, logits) { + for (let i = 0; i < input_ids.length; ++i) { + if (input_ids[i].length === this.begin_index) { + const batch_logits_data = /** @type {Float32Array} */(logits[i].data); + for (const token_id of this.begin_suppress_tokens) { + batch_logits_data[token_id] = -Infinity; + } + } + } + return logits; + } +} + +/** + * A LogitsProcessor that handles adding timestamps to generated text. + */ +class WhisperTimeStampLogitsProcessor extends LogitsProcessor { + /** + * Constructs a new WhisperTimeStampLogitsProcessor. + * @param {import('../models/whisper/generation_whisper.js').WhisperGenerationConfig} generate_config The config object passed to the `generate()` method of a transformer model. + * @param {number[]} init_tokens The initial tokens of the input sequence. + */ + constructor(generate_config, init_tokens) { + super(); + this.eos_token_id = + Array.isArray(generate_config.eos_token_id) + ? generate_config.eos_token_id[0] + : generate_config.eos_token_id; + + this.no_timestamps_token_id = generate_config.no_timestamps_token_id; + this.timestamp_begin = this.no_timestamps_token_id + 1; + + this.begin_index = init_tokens.length; + if (init_tokens.at(-1) === this.no_timestamps_token_id) { + this.begin_index -= 1; + } + this.max_initial_timestamp_index = generate_config.max_initial_timestamp_index; + } + + /** + * Modify the logits to handle timestamp tokens. + * @param {bigint[][]} input_ids The input sequence of tokens. + * @param {Tensor} logits The logits output by the model. + * @returns {Tensor} The modified logits. + */ + _call(input_ids, logits) { + for (let i = 0; i < input_ids.length; ++i) { + const batch_logits_data = /** @type {Float32Array} */(logits[i].data); + + // suppress <|notimestamps|> which is handled by without_timestamps + batch_logits_data[this.no_timestamps_token_id] = -Infinity; + + if (input_ids[i].length === this.begin_index - 1) { + batch_logits_data.fill(-Infinity); + batch_logits_data[this.timestamp_begin] = 0; + continue; + } + + // timestamps have to appear in pairs, except directly before eos_token; mask logits accordingly + const seq = input_ids[i].slice(this.begin_index); + const last_was_timestamp = seq.length >= 1 && seq[seq.length - 1] >= this.timestamp_begin; + const penultimate_was_timestamp = seq.length < 2 || seq[seq.length - 2] >= this.timestamp_begin; + + if (last_was_timestamp) { + if (penultimate_was_timestamp) { // has to be non-timestamp + batch_logits_data.subarray(this.timestamp_begin).fill(-Infinity); + } else { // cannot be normal text tokens + batch_logits_data.subarray(0, this.eos_token_id).fill(-Infinity); + } + } + + // apply the `max_initial_timestamp` option + if (input_ids[i].length === this.begin_index && this.max_initial_timestamp_index !== null) { + const last_allowed = this.timestamp_begin + this.max_initial_timestamp_index; + batch_logits_data.subarray(last_allowed + 1).fill(-Infinity); + } + + // if sum of probability over timestamps is above any other token, sample timestamp + const logprobs = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_2__.log_softmax)(batch_logits_data); + const timestamp_logprob = Math.log(logprobs.subarray(this.timestamp_begin).map(Math.exp).reduce((a, b) => a + b)); + const max_text_token_logprob = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_2__.max)(logprobs.subarray(0, this.timestamp_begin))[0]; + + if (timestamp_logprob > max_text_token_logprob) { + batch_logits_data.subarray(0, this.timestamp_begin).fill(-Infinity); + } + } + + return logits; + } +} + +/** + * A logits processor that disallows ngrams of a certain size to be repeated. + */ +class NoRepeatNGramLogitsProcessor extends LogitsProcessor { + /** + * Create a NoRepeatNGramLogitsProcessor. + * @param {number} no_repeat_ngram_size The no-repeat-ngram size. All ngrams of this size can only occur once. + */ + constructor(no_repeat_ngram_size) { + super(); + this.no_repeat_ngram_size = no_repeat_ngram_size; + } + + /** + * Generate n-grams from a sequence of token ids. + * @param {bigint[]} prevInputIds List of previous input ids + * @returns {Map} Map of generated n-grams + */ + getNgrams(prevInputIds) { + const curLen = prevInputIds.length; + + /**@type {number[][]} */ + const ngrams = []; + for (let j = 0; j < curLen + 1 - this.no_repeat_ngram_size; ++j) { + const ngram = []; + for (let k = 0; k < this.no_repeat_ngram_size; ++k) { + ngram.push(prevInputIds[j + k]); + } + ngrams.push(ngram.map(Number)); + } + + /** @type {Map} */ + const generatedNgram = new Map(); + for (const ngram of ngrams) { + const prevNgram = ngram.slice(0, ngram.length - 1); + const prevNgramKey = JSON.stringify(prevNgram); + const prevNgramValue = generatedNgram.get(prevNgramKey) ?? []; + prevNgramValue.push(ngram[ngram.length - 1]); + generatedNgram.set(prevNgramKey, prevNgramValue); + } + return generatedNgram; + } + + /** + * Generate n-grams from a sequence of token ids. + * @param {Map} bannedNgrams Map of banned n-grams + * @param {bigint[]} prevInputIds List of previous input ids + * @returns {number[]} Map of generated n-grams + */ + getGeneratedNgrams(bannedNgrams, prevInputIds) { + const ngramIdx = prevInputIds.slice(prevInputIds.length + 1 - this.no_repeat_ngram_size, prevInputIds.length); + const banned = bannedNgrams.get(JSON.stringify(ngramIdx.map(Number))) ?? []; + return banned; + } + + /** + * Calculate banned n-gram tokens + * @param {bigint[]} prevInputIds List of previous input ids + * @returns {number[]} Map of generated n-grams + */ + calcBannedNgramTokens(prevInputIds) { + const bannedTokens = []; + if (prevInputIds.length + 1 < this.no_repeat_ngram_size) { + // return no banned tokens if we haven't generated no_repeat_ngram_size tokens yet + return bannedTokens; + + } else { + const generatedNgrams = this.getNgrams(prevInputIds); + const bannedTokens = this.getGeneratedNgrams(generatedNgrams, prevInputIds); + return bannedTokens; + } + } + + /** + * Apply the no-repeat-ngram processor to the logits. + * @param {bigint[][]} input_ids The input IDs. + * @param {Tensor} logits The logits. + * @returns {Tensor} The logits with no-repeat-ngram processing. + */ + _call(input_ids, logits) { + for (let i = 0; i < input_ids.length; ++i) { + const batch_logits_data = /** @type {Float32Array} */(logits[i].data); + const bannedTokens = this.calcBannedNgramTokens(input_ids[i]); + for (const token of bannedTokens) { + batch_logits_data[token] = -Infinity; + } + } + return logits; + } +} + +/** + * A logits processor that prevents the repetition of previous tokens through a penalty. + * This penalty is applied at most once per token. Note that, for decoder-only models like most LLMs, + * the considered tokens include the prompt. + * + * In the original [paper](https://arxiv.org/pdf/1909.05858.pdf), the authors suggest the use of a + * penalty of around 1.2 to achieve a good balance between truthful generation and lack of repetition. + * To penalize and reduce repetition, use `penalty` values above 1.0, where a higher value penalizes + * more strongly. To reward and encourage repetition, use `penalty` values between 0.0 and 1.0, where + * a lower value rewards more strongly. + */ +class RepetitionPenaltyLogitsProcessor extends LogitsProcessor { + /** + * Create a RepetitionPenaltyLogitsProcessor. + * @param {number} penalty The parameter for repetition penalty. + * - 1.0 means no penalty. Above 1.0 penalizes previously generated tokens. + * - Between 0.0 and 1.0 rewards previously generated tokens. + */ + constructor(penalty) { + super(); + this.penalty = penalty; + } + + /** + * Apply the repetition penalty to the logits. + * @param {bigint[][]} input_ids The input IDs. + * @param {Tensor} logits The logits. + * @returns {Tensor} The logits with repetition penalty processing. + */ + _call(input_ids, logits) { + for (let i = 0; i < input_ids.length; ++i) { + const batch_logits_data = /** @type {Float32Array} */(logits[i].data); + for (const input_id of new Set(input_ids[i])) { + const token = Number(input_id); + if (batch_logits_data[token] < 0) { + batch_logits_data[token] *= this.penalty; + } else { + batch_logits_data[token] /= this.penalty; + } + } + } + + return logits + } +} + +/** + * A logits processor that enforces a minimum number of tokens. + */ +class MinLengthLogitsProcessor extends LogitsProcessor { + /** + * Create a MinLengthLogitsProcessor. + * @param {number} min_length The minimum length below which the score of `eos_token_id` is set to negative infinity. + * @param {number|number[]} eos_token_id The ID/IDs of the end-of-sequence token. + */ + constructor(min_length, eos_token_id) { + super(); + this.min_length = min_length; + this.eos_token_id = Array.isArray(eos_token_id) ? eos_token_id : [eos_token_id]; + } + + /** + * Apply logit processor. + * @param {bigint[][]} input_ids The input IDs. + * @param {Tensor} logits The logits. + * @returns {Tensor} The processed logits. + */ + _call(input_ids, logits) { + for (let i = 0; i < input_ids.length; ++i) { + if (input_ids[i].length < this.min_length) { + const batch_logits_data = /** @type {Float32Array} */(logits[i].data); + + for (const eos_token of this.eos_token_id) { + batch_logits_data[eos_token] = -Infinity; + } + } + } + + return logits + } +} + +/** + * A logits processor that enforces a minimum number of new tokens. + */ +class MinNewTokensLengthLogitsProcessor extends LogitsProcessor { + /** + * Create a MinNewTokensLengthLogitsProcessor. + * @param {number} prompt_length_to_skip The input tokens length. + * @param {number} min_new_tokens The minimum *new* tokens length below which the score of `eos_token_id` is set to negative infinity. + * @param {number|number[]} eos_token_id The ID/IDs of the end-of-sequence token. + */ + constructor(prompt_length_to_skip, min_new_tokens, eos_token_id) { + super(); + this.prompt_length_to_skip = prompt_length_to_skip; + this.min_new_tokens = min_new_tokens; + this.eos_token_id = Array.isArray(eos_token_id) ? eos_token_id : [eos_token_id]; + } + + /** + * Apply logit processor. + * @param {bigint[][]} input_ids The input IDs. + * @param {Tensor} logits The logits. + * @returns {Tensor} The processed logits. + */ + _call(input_ids, logits) { + for (let i = 0; i < input_ids.length; ++i) { + const new_tokens_length = input_ids[i].length - this.prompt_length_to_skip; + if (new_tokens_length < this.min_new_tokens) { + const batch_logits_data = /** @type {Float32Array} */(logits[i].data); + + for (const eos_token of this.eos_token_id) { + batch_logits_data[eos_token] = -Infinity; + } + } + } + return logits + } +} + +class NoBadWordsLogitsProcessor extends LogitsProcessor { + /** + * Create a `NoBadWordsLogitsProcessor`. + * @param {number[][]} bad_words_ids List of list of token ids that are not allowed to be generated. + * @param {number|number[]} eos_token_id The id of the *end-of-sequence* token. Optionally, use a list to set multiple *end-of-sequence* tokens. + */ + constructor(bad_words_ids, eos_token_id) { + super(); + this.bad_words_ids = bad_words_ids; + this.eos_token_id = Array.isArray(eos_token_id) ? eos_token_id : [eos_token_id]; + } + + /** + * Apply logit processor. + * @param {bigint[][]} input_ids The input IDs. + * @param {Tensor} logits The logits. + * @returns {Tensor} The processed logits. + */ + _call(input_ids, logits) { + for (let i = 0; i < input_ids.length; ++i) { + const batch_logits_data = /** @type {Float32Array} */(logits[i].data); + const ids = input_ids[i]; + for (const bad_word_ids of this.bad_words_ids) { + // There aren't enough tokens to match the banned sequence + if (ids.length < bad_word_ids.length - 1) continue; + + // Whether to modify the logits of the last token in the bad word id sequence + let mark = true; + + // For each bad word in the list, if the current sequence of input ids ends with this sequence (excluding the last), + // then we set the logits of the last bad word id to -Infinity. + for (let j = 1; j <= bad_word_ids.length - 1; ++j) { + // NOTE: We use != instead of !== to compare bigint and number + // @ts-ignore + if (bad_word_ids.at(-j - 1) != ids.at(-j)) { + // We have found a mismatch + mark = false; + break; + } + } + if (mark) { + batch_logits_data[bad_word_ids.at(-1)] = -Infinity; + } + } + } + return logits + } +} + +/** + * [`LogitsProcessor`] for classifier free guidance (CFG). The scores are split over the batch dimension, + * where the first half correspond to the conditional logits (predicted from the input prompt) and the second half + * correspond to the unconditional logits (predicted from an empty or 'null' prompt). The processor computes a + * weighted average across the conditional and unconditional logits, parameterised by the `guidance_scale`. + * + * See [the paper](https://arxiv.org/abs/2306.05284) for more information. + */ +class ClassifierFreeGuidanceLogitsProcessor extends LogitsProcessor { + + /** + * Create a `ClassifierFreeGuidanceLogitsProcessor`. + * @param {number} guidance_scale The guidance scale for classifier free guidance (CFG). CFG is enabled by setting `guidance_scale > 1`. + * Higher guidance scale encourages the model to generate samples that are more closely linked to the input + * prompt, usually at the expense of poorer quality. + */ + constructor(guidance_scale) { + super(); + if (guidance_scale <= 1) { + throw new Error( + `Require guidance scale >1 to use the classifier free guidance processor, got guidance scale ${guidance_scale}.` + ) + } + this.guidance_scale = guidance_scale; + } + + /** + * Apply logit processor. + * @param {bigint[][]} input_ids The input IDs. + * @param {Tensor} logits The logits. + * @returns {Tensor} The processed logits. + */ + _call(input_ids, logits) { + if (logits.dims[0] !== 2 * input_ids.length) { + throw new Error( + `Logits should have twice the batch size of the input ids, the first half of batches corresponding to ` + + `the conditional inputs, and the second half of batches corresponding to the unconditional inputs. Got ` + + `batch size ${logits.dims[0]} for the logits and ${input_ids.length} for the input ids.` + ) + } + + const unguided_bsz = input_ids.length; + const cond_logits = logits.slice([0, unguided_bsz], null); + const uncond_logits = logits.slice([unguided_bsz, logits.dims[0]], null); + + // Merge into uncond_logits (to save memory). This is equivalent to the following: + // scores = uncond_logits + (cond_logits - uncond_logits) * guidance_scale + for (let i = 0; i < uncond_logits.data.length; ++i) { + uncond_logits.data[i] += (cond_logits.data[i] - uncond_logits.data[i]) * this.guidance_scale; + } + + return uncond_logits; + } +} + +/** + * [`LogitsWarper`] for temperature (exponential scaling output probability distribution), which effectively means + * that it can control the randomness of the predicted tokens. Often used together with [`TopPLogitsWarper`] and [`TopKLogitsWarper`]. + */ +class TemperatureLogitsWarper extends LogitsWarper { + /** + * Create a `TemperatureLogitsWarper`. + * @param {number} temperature Strictly positive float value used to modulate the logits distribution. + * A value smaller than `1` decreases randomness (and vice versa), with `0` being equivalent to shifting + * all probability mass to the most likely token. + */ + constructor(temperature) { + super(); + + if (typeof temperature !== 'number' || temperature <= 0) { + let errorMessage = + `\`temperature\` (=${temperature}) must be a strictly positive float, otherwise your next token scores will be invalid.`; + + if (temperature === 0) { + errorMessage += " If you're looking for greedy decoding strategies, set `do_sample=false`." + } + } + this.temperature = temperature; + } + + /** + * Apply logit warper. + * @param {bigint[][]} input_ids The input IDs. + * @param {Tensor} logits The logits. + * @returns {Tensor} The processed logits. + */ + _call(input_ids, logits) { + const batch_logits_data = /** @type {Float32Array} */(logits.data); + for (let i = 0; i < batch_logits_data.length; ++i) { + batch_logits_data[i] /= this.temperature; + } + return logits; + } +} + +/** + * [`LogitsWarper`] that performs top-p, i.e. restricting to top tokens summing to prob_cut_off <= prob_cut_off. + * Often used together with [`TemperatureLogitsWarper`] and [`TopKLogitsWarper`]. + */ +class TopPLogitsWarper extends LogitsWarper { + /** + * Create a `TopPLogitsWarper`. + * @param {number} top_p If set to < 1, only the smallest set of most probable tokens with + * probabilities that add up to `top_p` or higher are kept for generation. + * @param {Object} options Additional options for the top-p sampling. + * @param {number} [options.filter_value=-Infinity] All filtered values will be set to this float value. + * @param {number} [options.min_tokens_to_keep=1] Minimum number of tokens that cannot be filtered. + */ + constructor(top_p, { + filter_value = -Infinity, + min_tokens_to_keep = 1, + } = {}) { + super(); + if (top_p < 0 || top_p > 1.0) { + throw new Error(`\`top_p\` must be a float > 0 and < 1, but is ${top_p}`) + } + if (!Number.isInteger(min_tokens_to_keep) || min_tokens_to_keep < 1) { + throw new Error(`\`min_tokens_to_keep\` must be a positive integer, but is ${min_tokens_to_keep}`) + } + + this.top_p = top_p + this.filter_value = filter_value + this.min_tokens_to_keep = min_tokens_to_keep + } +} + +/** + * [`LogitsWarper`] that performs top-k, i.e. restricting to the k highest probability elements. + * Often used together with [`TemperatureLogitsWarper`] and [`TopPLogitsWarper`]. + */ +class TopKLogitsWarper extends LogitsWarper { + /** + * Create a `TopKLogitsWarper`. + * @param {number} top_k If set to > 0, only the top `top_k` tokens are kept for generation. + * @param {Object} options Additional options for the top-k sampling. + * @param {number} [options.filter_value=-Infinity] All filtered values will be set to this float value. + * @param {number} [options.min_tokens_to_keep=1] Minimum number of tokens that cannot be filtered. + */ + constructor(top_k, { + filter_value = -Infinity, + min_tokens_to_keep = 1, + } = {}) { + super(); + if (!Number.isInteger(top_k) || top_k < 0) { + throw new Error(`\`top_k\` must be a positive integer, but is ${top_k}`) + } + + this.top_k = Math.max(top_k, min_tokens_to_keep) + this.filter_value = filter_value + } +} + +/***/ }), + +/***/ "./src/generation/logits_sampler.js": +/*!******************************************!*\ + !*** ./src/generation/logits_sampler.js ***! + \******************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ LogitsSampler: () => (/* binding */ LogitsSampler) +/* harmony export */ }); +/* harmony import */ var _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../utils/generic.js */ "./src/utils/generic.js"); +/* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../utils/tensor.js */ "./src/utils/tensor.js"); +/* harmony import */ var _utils_maths_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../utils/maths.js */ "./src/utils/maths.js"); +/* harmony import */ var _generation_configuration_utils_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ../generation/configuration_utils.js */ "./src/generation/configuration_utils.js"); + +/** + * @module generation/logits_sampler + */ + + + + + + + +/** + * Sampler is a base class for all sampling methods used for text generation. + */ +class LogitsSampler extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * Creates a new Sampler object with the specified generation config. + * @param {GenerationConfig} generation_config The generation config. + */ + constructor(generation_config) { + super(); + this.generation_config = generation_config; + } + + /** + * Executes the sampler, using the specified logits. + * @param {Tensor} logits + * @returns {Promise<[bigint, number][]>} + */ + async _call(logits) { + // Sample from logits, of dims [batch, sequence_length, vocab_size]. + // If index is specified, sample from [batch, index, vocab_size]. + return this.sample(logits); + } + + /** + * Abstract method for sampling the logits. + * @param {Tensor} logits + * @throws {Error} If not implemented in subclass. + * @returns {Promise<[bigint, number][]>} + */ + async sample(logits) { + throw Error("sample should be implemented in subclasses.") + } + + /** + * Returns the specified logits as an array, with temperature applied. + * @param {Tensor} logits + * @param {number} index + * @returns {Float32Array} + */ + getLogits(logits, index) { + let vocabSize = logits.dims.at(-1); + + let logs = /** @type {Float32Array} */(logits.data); + + if (index === -1) { + logs = logs.slice(-vocabSize); + } else { + let startIndex = index * vocabSize; + logs = logs.slice(startIndex, startIndex + vocabSize); + } + return logs; + } + + /** + * Selects an item randomly based on the specified probabilities. + * @param {import("../transformers.js").DataArray} probabilities An array of probabilities to use for selection. + * @returns {number} The index of the selected item. + */ + randomSelect(probabilities) { + // Return index of chosen item + let sumProbabilities = 0; + for (let i = 0; i < probabilities.length; ++i) { + sumProbabilities += probabilities[i]; + } + + let r = Math.random() * sumProbabilities; + for (let i = 0; i < probabilities.length; ++i) { + r -= probabilities[i]; + if (r <= 0) { + return i; + } + } + return 0; // return first (most probable) as a fallback + } + + /** + * Returns a Sampler object based on the specified options. + * @param {GenerationConfig} generation_config An object containing options for the sampler. + * @returns {LogitsSampler} A Sampler object. + */ + static getSampler(generation_config) { + // - *greedy decoding*: `num_beams=1` and `do_sample=False` + // - *contrastive search*: `penalty_alpha>0` and `top_k>1` + // - *multinomial sampling*: `num_beams=1` and `do_sample=True` + // - *beam-search decoding*: `num_beams>1` and `do_sample=False` + // - *beam-search multinomial sampling*: `num_beams>1` and `do_sample=True` + // - *diverse beam-search decoding*: `num_beams>1` and `num_beam_groups>1` + // - *constrained beam-search decoding*: `constraints!=None` or `force_words_ids!=None` + + // NOTE: beam search is implemented directly into the generation function + if (generation_config.do_sample) { + return new MultinomialSampler(generation_config); + + } else if (generation_config.num_beams > 1) { + return new BeamSearchSampler(generation_config); + + } else { + if (generation_config.num_return_sequences > 1) { + throw Error(`num_return_sequences has to be 1 when doing greedy search, but is ${generation_config.num_return_sequences}.`) + } + return new GreedySampler(generation_config); + } + } +} + +/** + * Class representing a Greedy Sampler. + */ +class GreedySampler extends LogitsSampler { + /** + * Sample the maximum probability of a given logits tensor. + * @param {Tensor} logits + * @returns {Promise<[bigint, number][]>} An array with a single tuple, containing the index of the maximum value and a meaningless score (since this is a greedy search). + */ + async sample(logits) { + // NOTE: no need to do log_softmax here since we only take the maximum + const argmax = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_2__.max)(logits.data)[1]; + + // Note: score is meaningless in this context, since we are performing + // greedy search (p = 1 => log(p) = 0) + return [ + [BigInt(argmax), 0] + ]; + } +} + +/** + * Class representing a MultinomialSampler. + */ +class MultinomialSampler extends LogitsSampler { + + /** + * Sample from the logits. + * @param {Tensor} logits + * @returns {Promise<[bigint, number][]>} + */ + async sample(logits) { + let k = logits.dims.at(-1); // defaults to vocab size + if (this.generation_config.top_k > 0) { + k = Math.min(this.generation_config.top_k, k); + } + + // Get top k tokens + const [v, i] = await (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.topk)(logits, k); + + // Compute softmax over logits + const probabilities = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_2__.softmax)(/** @type {Float32Array} */(v.data)); + + return Array.from({ length: this.generation_config.num_beams }, () => { + const sampledIndex = this.randomSelect(probabilities); + return [ + i.data[sampledIndex], // token id + Math.log(probabilities[sampledIndex]), // score + ]; + }); + } +} + + +/** + * Class representing a BeamSearchSampler. + */ +class BeamSearchSampler extends LogitsSampler { + + /** + * Sample from the logits. + * @param {Tensor} logits + * @returns {Promise<[bigint, number][]>} + */ + async sample(logits) { + let k = logits.dims.at(-1); // defaults to vocab size + if (this.generation_config.top_k > 0) { + k = Math.min(this.generation_config.top_k, k); + } + + // Get top k tokens + const [v, i] = await (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.topk)(logits, k); + + // Compute softmax over logits + const probabilities = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_2__.softmax)(/** @type {Float32Array} */(v.data)); + + return Array.from({ length: this.generation_config.num_beams }, (_, x) => { + return [ + i.data[x], // token id + Math.log(probabilities[x]), // score + ]; + }); + } +} + + +/***/ }), + +/***/ "./src/generation/stopping_criteria.js": +/*!*********************************************!*\ + !*** ./src/generation/stopping_criteria.js ***! + \*********************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ EosTokenCriteria: () => (/* binding */ EosTokenCriteria), +/* harmony export */ InterruptableStoppingCriteria: () => (/* binding */ InterruptableStoppingCriteria), +/* harmony export */ MaxLengthCriteria: () => (/* binding */ MaxLengthCriteria), +/* harmony export */ StoppingCriteria: () => (/* binding */ StoppingCriteria), +/* harmony export */ StoppingCriteriaList: () => (/* binding */ StoppingCriteriaList) +/* harmony export */ }); +/* harmony import */ var _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../utils/generic.js */ "./src/utils/generic.js"); + +/** + * @module generation/stopping_criteria + */ + + + +// NOTE: +// Stopping Criteria returns a list of `batch_size` booleans, indicating whether each sequence in the batch should be stopped. + +/** + * Abstract base class for all stopping criteria that can be applied during generation. + */ +class StoppingCriteria extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * + * @param {number[][]} input_ids (`number[][]` of shape `(batch_size, sequence_length)`): + * Indices of input sequence tokens in the vocabulary. + * @param {number[][]} scores scores (`number[][]` of shape `(batch_size, config.vocab_size)`): + * Prediction scores of a language modeling head. These can be scores for each vocabulary token before SoftMax + * or scores for each vocabulary token after SoftMax. + * @returns {boolean[]} A list of booleans indicating whether each sequence should be stopped. + */ + _call(input_ids, scores) { + throw Error("StoppingCriteria needs to be subclassed"); + } +} +/** + */ +class StoppingCriteriaList extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * Constructs a new instance of `StoppingCriteriaList`. + */ + constructor() { + super(); + this.criteria = []; + } + + /** + * Adds a new stopping criterion to the list. + * + * @param {StoppingCriteria} item The stopping criterion to add. + */ + push(item) { + this.criteria.push(item); + } + + /** + * Adds multiple stopping criteria to the list. + * + * @param {StoppingCriteria|StoppingCriteriaList|StoppingCriteria[]} items The stopping criteria to add. + */ + extend(items) { + if (items instanceof StoppingCriteriaList) { + items = items.criteria; + } else if (items instanceof StoppingCriteria) { + items = [items]; + } + this.criteria.push(...items); + } + + _call(input_ids, scores) { + const is_done = new Array(input_ids.length).fill(false); + for (const criterion of this.criteria) { + const criterion_done = criterion(input_ids, scores); + for (let i = 0; i < is_done.length; ++i) { + is_done[i] ||= criterion_done[i]; + } + } + return is_done; + } + + [Symbol.iterator]() { + return this.criteria.values(); + } +} + +/** + * This class can be used to stop generation whenever the full generated number of tokens exceeds `max_length`. + * Keep in mind for decoder-only type of transformers, this will include the initial prompted tokens. + */ +class MaxLengthCriteria extends StoppingCriteria { + + /** + * + * @param {number} max_length The maximum length that the output sequence can have in number of tokens. + * @param {number} [max_position_embeddings=null] The maximum model length, as defined by the model's `config.max_position_embeddings` attribute. + */ + constructor(max_length, max_position_embeddings = null) { + super(); + this.max_length = max_length; + this.max_position_embeddings = max_position_embeddings; + } + + _call(input_ids) { + return input_ids.map(ids => ids.length >= this.max_length); + } +} + +// TODO: add MaxTimeCriteria + +/** + * This class can be used to stop generation whenever the "end-of-sequence" token is generated. + * By default, it uses the `model.generation_config.eos_token_id`. + */ +class EosTokenCriteria extends StoppingCriteria { + + /** + * + * @param {number|number[]} eos_token_id The id of the *end-of-sequence* token. + * Optionally, use a list to set multiple *end-of-sequence* tokens. + */ + constructor(eos_token_id) { + super(); + if (!Array.isArray(eos_token_id)) { + eos_token_id = [eos_token_id]; + } + this.eos_token_id = eos_token_id; + } + + /** + * + * @param {number[][]} input_ids + * @param {number[][]} scores + * @returns {boolean[]} + */ + _call(input_ids, scores) { + return input_ids.map(ids => { + const last = ids.at(-1); + // NOTE: We use == instead of === to allow for number/bigint comparison + return this.eos_token_id.some(eos_id => last == eos_id); + }); + } +} + +/** + * This class can be used to stop generation whenever the user interrupts the process. + */ +class InterruptableStoppingCriteria extends StoppingCriteria { + constructor() { + super(); + this.interrupted = false; + } + + interrupt() { + this.interrupted = true; + } + + reset() { + this.interrupted = false; + } + + _call(input_ids, scores) { + return new Array(input_ids.length).fill(this.interrupted); + } +} + + +/***/ }), + +/***/ "./src/generation/streamers.js": +/*!*************************************!*\ + !*** ./src/generation/streamers.js ***! + \*************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ BaseStreamer: () => (/* binding */ BaseStreamer), +/* harmony export */ TextStreamer: () => (/* binding */ TextStreamer), +/* harmony export */ WhisperTextStreamer: () => (/* binding */ WhisperTextStreamer) +/* harmony export */ }); +/* harmony import */ var _utils_core_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../utils/core.js */ "./src/utils/core.js"); +/* harmony import */ var _tokenizers_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../tokenizers.js */ "./src/tokenizers.js"); +/* harmony import */ var _env_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../env.js */ "./src/env.js"); + +/** + * @module generation/streamers + */ + + + + + +class BaseStreamer { + /** + * Function that is called by `.generate()` to push new tokens + * @param {bigint[][]} value + */ + put(value) { + throw Error('Not implemented'); + } + + /** + * Function that is called by `.generate()` to signal the end of generation + */ + end() { + throw Error('Not implemented'); + } +} + +const stdout_write = _env_js__WEBPACK_IMPORTED_MODULE_2__.apis.IS_PROCESS_AVAILABLE + ? x => process.stdout.write(x) + : x => console.log(x); + +/** + * Simple text streamer that prints the token(s) to stdout as soon as entire words are formed. + */ +class TextStreamer extends BaseStreamer { + /** + * + * @param {import('../tokenizers.js').PreTrainedTokenizer} tokenizer + * @param {Object} options + * @param {boolean} [options.skip_prompt=false] Whether to skip the prompt tokens + * @param {boolean} [options.skip_special_tokens=true] Whether to skip special tokens when decoding + * @param {function(string): void} [options.callback_function=null] Function to call when a piece of text is ready to display + * @param {function(bigint[]): void} [options.token_callback_function=null] Function to call when a new token is generated + * @param {Object} [options.decode_kwargs={}] Additional keyword arguments to pass to the tokenizer's decode method + */ + constructor(tokenizer, { + skip_prompt = false, + callback_function = null, + token_callback_function = null, + skip_special_tokens = true, + decode_kwargs = {}, + ...kwargs + } = {}) { + super(); + this.tokenizer = tokenizer; + this.skip_prompt = skip_prompt; + this.callback_function = callback_function ?? stdout_write; + this.token_callback_function = token_callback_function; + this.decode_kwargs = { skip_special_tokens, ...decode_kwargs, ...kwargs }; + + // variables used in the streaming process + this.token_cache = []; + this.print_len = 0; + this.next_tokens_are_prompt = true; + } + + /** + * Receives tokens, decodes them, and prints them to stdout as soon as they form entire words. + * @param {bigint[][]} value + */ + put(value) { + if (value.length > 1) { + throw Error('TextStreamer only supports batch size of 1'); + } + + const is_prompt = this.next_tokens_are_prompt; + if (is_prompt) { + this.next_tokens_are_prompt = false; + if (this.skip_prompt) return; + } + + const tokens = value[0]; + this.token_callback_function?.(tokens) + + // Add the new token to the cache and decodes the entire thing. + this.token_cache = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_0__.mergeArrays)(this.token_cache, tokens); + const text = this.tokenizer.decode(this.token_cache, this.decode_kwargs); + + let printable_text; + if (is_prompt || text.endsWith('\n')) { + // After the symbol for a new line, we flush the cache. + printable_text = text.slice(this.print_len); + this.token_cache = []; + this.print_len = 0; + } else if (text.length > 0 && (0,_tokenizers_js__WEBPACK_IMPORTED_MODULE_1__.is_chinese_char)(text.charCodeAt(text.length - 1))) { + // If the last token is a CJK character, we print the characters. + printable_text = text.slice(this.print_len); + this.print_len += printable_text.length; + } else { + // Otherwise, prints until the last space char (simple heuristic to avoid printing incomplete words, + // which may change with the subsequent token -- there are probably smarter ways to do this!) + printable_text = text.slice(this.print_len, text.lastIndexOf(' ') + 1); + this.print_len += printable_text.length; + } + + this.on_finalized_text(printable_text, false); + } + + /** + * Flushes any remaining cache and prints a newline to stdout. + */ + end() { + let printable_text; + if (this.token_cache.length > 0) { + const text = this.tokenizer.decode(this.token_cache, this.decode_kwargs); + printable_text = text.slice(this.print_len); + this.token_cache = []; + this.print_len = 0; + } else { + printable_text = ''; + } + this.next_tokens_are_prompt = true; + this.on_finalized_text(printable_text, true); + } + + /** + * Prints the new text to stdout. If the stream is ending, also prints a newline. + * @param {string} text + * @param {boolean} stream_end + */ + on_finalized_text(text, stream_end) { + if (text.length > 0) { + this.callback_function?.(text); + } + if (stream_end && this.callback_function === stdout_write && _env_js__WEBPACK_IMPORTED_MODULE_2__.apis.IS_PROCESS_AVAILABLE) { + this.callback_function?.('\n'); + } + } +} + +/** + * Utility class to handle streaming of tokens generated by whisper speech-to-text models. + * Callback functions are invoked when each of the following events occur: + * - A new chunk starts (on_chunk_start) + * - A new token is generated (callback_function) + * - A chunk ends (on_chunk_end) + * - The stream is finalized (on_finalize) + */ +class WhisperTextStreamer extends TextStreamer { + /** + * @param {import('../tokenizers.js').WhisperTokenizer} tokenizer + * @param {Object} options + * @param {boolean} [options.skip_prompt=false] Whether to skip the prompt tokens + * @param {function(string): void} [options.callback_function=null] Function to call when a piece of text is ready to display + * @param {function(bigint[]): void} [options.token_callback_function=null] Function to call when a new token is generated + * @param {function(number): void} [options.on_chunk_start=null] Function to call when a new chunk starts + * @param {function(number): void} [options.on_chunk_end=null] Function to call when a chunk ends + * @param {function(): void} [options.on_finalize=null] Function to call when the stream is finalized + * @param {number} [options.time_precision=0.02] Precision of the timestamps + * @param {boolean} [options.skip_special_tokens=true] Whether to skip special tokens when decoding + * @param {Object} [options.decode_kwargs={}] Additional keyword arguments to pass to the tokenizer's decode method + */ + constructor(tokenizer, { + skip_prompt = false, + callback_function = null, + token_callback_function = null, + on_chunk_start = null, + on_chunk_end = null, + on_finalize = null, + time_precision = 0.02, + skip_special_tokens = true, + decode_kwargs = {}, + } = {}) { + super(tokenizer, { + skip_prompt, + skip_special_tokens, + callback_function, + token_callback_function, + decode_kwargs, + }); + this.timestamp_begin = tokenizer.timestamp_begin; + + this.on_chunk_start = on_chunk_start; + this.on_chunk_end = on_chunk_end; + this.on_finalize = on_finalize; + + this.time_precision = time_precision; + + this.waiting_for_timestamp = false; + } + + /** + * @param {bigint[][]} value + */ + put(value) { + if (value.length > 1) { + throw Error('WhisperTextStreamer only supports batch size of 1'); + } + const tokens = value[0]; + + // Check if the token is a timestamp + if (tokens.length === 1) { + const offset = Number(tokens[0]) - this.timestamp_begin; + if (offset >= 0) { + const time = offset * this.time_precision; + if (this.waiting_for_timestamp) { + this.on_chunk_end?.(time); + } else { + this.on_chunk_start?.(time); + } + this.waiting_for_timestamp = !this.waiting_for_timestamp; // Toggle + value = [[]]; // Skip timestamp + } + } + return super.put(value); + } + + end() { + super.end(); + this.on_finalize?.(); + } +} + + +/***/ }), + +/***/ "./src/models.js": +/*!***********************!*\ + !*** ./src/models.js ***! + \***********************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ ASTForAudioClassification: () => (/* binding */ ASTForAudioClassification), +/* harmony export */ ASTModel: () => (/* binding */ ASTModel), +/* harmony export */ ASTPreTrainedModel: () => (/* binding */ ASTPreTrainedModel), +/* harmony export */ AlbertForMaskedLM: () => (/* binding */ AlbertForMaskedLM), +/* harmony export */ AlbertForQuestionAnswering: () => (/* binding */ AlbertForQuestionAnswering), +/* harmony export */ AlbertForSequenceClassification: () => (/* binding */ AlbertForSequenceClassification), +/* harmony export */ AlbertModel: () => (/* binding */ AlbertModel), +/* harmony export */ AlbertPreTrainedModel: () => (/* binding */ AlbertPreTrainedModel), +/* harmony export */ AutoModel: () => (/* binding */ AutoModel), +/* harmony export */ AutoModelForAudioClassification: () => (/* binding */ AutoModelForAudioClassification), +/* harmony export */ AutoModelForAudioFrameClassification: () => (/* binding */ AutoModelForAudioFrameClassification), +/* harmony export */ AutoModelForAudioTextToText: () => (/* binding */ AutoModelForAudioTextToText), +/* harmony export */ AutoModelForCTC: () => (/* binding */ AutoModelForCTC), +/* harmony export */ AutoModelForCausalLM: () => (/* binding */ AutoModelForCausalLM), +/* harmony export */ AutoModelForDepthEstimation: () => (/* binding */ AutoModelForDepthEstimation), +/* harmony export */ AutoModelForDocumentQuestionAnswering: () => (/* binding */ AutoModelForDocumentQuestionAnswering), +/* harmony export */ AutoModelForImageClassification: () => (/* binding */ AutoModelForImageClassification), +/* harmony export */ AutoModelForImageFeatureExtraction: () => (/* binding */ AutoModelForImageFeatureExtraction), +/* harmony export */ AutoModelForImageMatting: () => (/* binding */ AutoModelForImageMatting), +/* harmony export */ AutoModelForImageSegmentation: () => (/* binding */ AutoModelForImageSegmentation), +/* harmony export */ AutoModelForImageTextToText: () => (/* binding */ AutoModelForImageTextToText), +/* harmony export */ AutoModelForImageToImage: () => (/* binding */ AutoModelForImageToImage), +/* harmony export */ AutoModelForMaskGeneration: () => (/* binding */ AutoModelForMaskGeneration), +/* harmony export */ AutoModelForMaskedLM: () => (/* binding */ AutoModelForMaskedLM), +/* harmony export */ AutoModelForNormalEstimation: () => (/* binding */ AutoModelForNormalEstimation), +/* harmony export */ AutoModelForObjectDetection: () => (/* binding */ AutoModelForObjectDetection), +/* harmony export */ AutoModelForPoseEstimation: () => (/* binding */ AutoModelForPoseEstimation), +/* harmony export */ AutoModelForQuestionAnswering: () => (/* binding */ AutoModelForQuestionAnswering), +/* harmony export */ AutoModelForSemanticSegmentation: () => (/* binding */ AutoModelForSemanticSegmentation), +/* harmony export */ AutoModelForSeq2SeqLM: () => (/* binding */ AutoModelForSeq2SeqLM), +/* harmony export */ AutoModelForSequenceClassification: () => (/* binding */ AutoModelForSequenceClassification), +/* harmony export */ AutoModelForSpeechSeq2Seq: () => (/* binding */ AutoModelForSpeechSeq2Seq), +/* harmony export */ AutoModelForTextToSpectrogram: () => (/* binding */ AutoModelForTextToSpectrogram), +/* harmony export */ AutoModelForTextToWaveform: () => (/* binding */ AutoModelForTextToWaveform), +/* harmony export */ AutoModelForTokenClassification: () => (/* binding */ AutoModelForTokenClassification), +/* harmony export */ AutoModelForUniversalSegmentation: () => (/* binding */ AutoModelForUniversalSegmentation), +/* harmony export */ AutoModelForVision2Seq: () => (/* binding */ AutoModelForVision2Seq), +/* harmony export */ AutoModelForXVector: () => (/* binding */ AutoModelForXVector), +/* harmony export */ AutoModelForZeroShotObjectDetection: () => (/* binding */ AutoModelForZeroShotObjectDetection), +/* harmony export */ BartForConditionalGeneration: () => (/* binding */ BartForConditionalGeneration), +/* harmony export */ BartForSequenceClassification: () => (/* binding */ BartForSequenceClassification), +/* harmony export */ BartModel: () => (/* binding */ BartModel), +/* harmony export */ BartPretrainedModel: () => (/* binding */ BartPretrainedModel), +/* harmony export */ BaseModelOutput: () => (/* binding */ BaseModelOutput), +/* harmony export */ BeitForImageClassification: () => (/* binding */ BeitForImageClassification), +/* harmony export */ BeitModel: () => (/* binding */ BeitModel), +/* harmony export */ BeitPreTrainedModel: () => (/* binding */ BeitPreTrainedModel), +/* harmony export */ BertForMaskedLM: () => (/* binding */ BertForMaskedLM), +/* harmony export */ BertForQuestionAnswering: () => (/* binding */ BertForQuestionAnswering), +/* harmony export */ BertForSequenceClassification: () => (/* binding */ BertForSequenceClassification), +/* harmony export */ BertForTokenClassification: () => (/* binding */ BertForTokenClassification), +/* harmony export */ BertModel: () => (/* binding */ BertModel), +/* harmony export */ BertPreTrainedModel: () => (/* binding */ BertPreTrainedModel), +/* harmony export */ BlenderbotForConditionalGeneration: () => (/* binding */ BlenderbotForConditionalGeneration), +/* harmony export */ BlenderbotModel: () => (/* binding */ BlenderbotModel), +/* harmony export */ BlenderbotPreTrainedModel: () => (/* binding */ BlenderbotPreTrainedModel), +/* harmony export */ BlenderbotSmallForConditionalGeneration: () => (/* binding */ BlenderbotSmallForConditionalGeneration), +/* harmony export */ BlenderbotSmallModel: () => (/* binding */ BlenderbotSmallModel), +/* harmony export */ BlenderbotSmallPreTrainedModel: () => (/* binding */ BlenderbotSmallPreTrainedModel), +/* harmony export */ BloomForCausalLM: () => (/* binding */ BloomForCausalLM), +/* harmony export */ BloomModel: () => (/* binding */ BloomModel), +/* harmony export */ BloomPreTrainedModel: () => (/* binding */ BloomPreTrainedModel), +/* harmony export */ CLIPModel: () => (/* binding */ CLIPModel), +/* harmony export */ CLIPPreTrainedModel: () => (/* binding */ CLIPPreTrainedModel), +/* harmony export */ CLIPSegForImageSegmentation: () => (/* binding */ CLIPSegForImageSegmentation), +/* harmony export */ CLIPSegModel: () => (/* binding */ CLIPSegModel), +/* harmony export */ CLIPSegPreTrainedModel: () => (/* binding */ CLIPSegPreTrainedModel), +/* harmony export */ CLIPTextModel: () => (/* binding */ CLIPTextModel), +/* harmony export */ CLIPTextModelWithProjection: () => (/* binding */ CLIPTextModelWithProjection), +/* harmony export */ CLIPVisionModel: () => (/* binding */ CLIPVisionModel), +/* harmony export */ CLIPVisionModelWithProjection: () => (/* binding */ CLIPVisionModelWithProjection), +/* harmony export */ CamembertForMaskedLM: () => (/* binding */ CamembertForMaskedLM), +/* harmony export */ CamembertForQuestionAnswering: () => (/* binding */ CamembertForQuestionAnswering), +/* harmony export */ CamembertForSequenceClassification: () => (/* binding */ CamembertForSequenceClassification), +/* harmony export */ CamembertForTokenClassification: () => (/* binding */ CamembertForTokenClassification), +/* harmony export */ CamembertModel: () => (/* binding */ CamembertModel), +/* harmony export */ CamembertPreTrainedModel: () => (/* binding */ CamembertPreTrainedModel), +/* harmony export */ CausalLMOutput: () => (/* binding */ CausalLMOutput), +/* harmony export */ CausalLMOutputWithPast: () => (/* binding */ CausalLMOutputWithPast), +/* harmony export */ ChineseCLIPModel: () => (/* binding */ ChineseCLIPModel), +/* harmony export */ ChineseCLIPPreTrainedModel: () => (/* binding */ ChineseCLIPPreTrainedModel), +/* harmony export */ ClapAudioModelWithProjection: () => (/* binding */ ClapAudioModelWithProjection), +/* harmony export */ ClapModel: () => (/* binding */ ClapModel), +/* harmony export */ ClapPreTrainedModel: () => (/* binding */ ClapPreTrainedModel), +/* harmony export */ ClapTextModelWithProjection: () => (/* binding */ ClapTextModelWithProjection), +/* harmony export */ CodeGenForCausalLM: () => (/* binding */ CodeGenForCausalLM), +/* harmony export */ CodeGenModel: () => (/* binding */ CodeGenModel), +/* harmony export */ CodeGenPreTrainedModel: () => (/* binding */ CodeGenPreTrainedModel), +/* harmony export */ CohereForCausalLM: () => (/* binding */ CohereForCausalLM), +/* harmony export */ CohereModel: () => (/* binding */ CohereModel), +/* harmony export */ CoherePreTrainedModel: () => (/* binding */ CoherePreTrainedModel), +/* harmony export */ ConvBertForMaskedLM: () => (/* binding */ ConvBertForMaskedLM), +/* harmony export */ ConvBertForQuestionAnswering: () => (/* binding */ ConvBertForQuestionAnswering), +/* harmony export */ ConvBertForSequenceClassification: () => (/* binding */ ConvBertForSequenceClassification), +/* harmony export */ ConvBertForTokenClassification: () => (/* binding */ ConvBertForTokenClassification), +/* harmony export */ ConvBertModel: () => (/* binding */ ConvBertModel), +/* harmony export */ ConvBertPreTrainedModel: () => (/* binding */ ConvBertPreTrainedModel), +/* harmony export */ ConvNextForImageClassification: () => (/* binding */ ConvNextForImageClassification), +/* harmony export */ ConvNextModel: () => (/* binding */ ConvNextModel), +/* harmony export */ ConvNextPreTrainedModel: () => (/* binding */ ConvNextPreTrainedModel), +/* harmony export */ ConvNextV2ForImageClassification: () => (/* binding */ ConvNextV2ForImageClassification), +/* harmony export */ ConvNextV2Model: () => (/* binding */ ConvNextV2Model), +/* harmony export */ ConvNextV2PreTrainedModel: () => (/* binding */ ConvNextV2PreTrainedModel), +/* harmony export */ DFineForObjectDetection: () => (/* binding */ DFineForObjectDetection), +/* harmony export */ DFineModel: () => (/* binding */ DFineModel), +/* harmony export */ DFinePreTrainedModel: () => (/* binding */ DFinePreTrainedModel), +/* harmony export */ DPTForDepthEstimation: () => (/* binding */ DPTForDepthEstimation), +/* harmony export */ DPTModel: () => (/* binding */ DPTModel), +/* harmony export */ DPTPreTrainedModel: () => (/* binding */ DPTPreTrainedModel), +/* harmony export */ DacDecoderModel: () => (/* binding */ DacDecoderModel), +/* harmony export */ DacDecoderOutput: () => (/* binding */ DacDecoderOutput), +/* harmony export */ DacEncoderModel: () => (/* binding */ DacEncoderModel), +/* harmony export */ DacEncoderOutput: () => (/* binding */ DacEncoderOutput), +/* harmony export */ DacModel: () => (/* binding */ DacModel), +/* harmony export */ DacPreTrainedModel: () => (/* binding */ DacPreTrainedModel), +/* harmony export */ DebertaForMaskedLM: () => (/* binding */ DebertaForMaskedLM), +/* harmony export */ DebertaForQuestionAnswering: () => (/* binding */ DebertaForQuestionAnswering), +/* harmony export */ DebertaForSequenceClassification: () => (/* binding */ DebertaForSequenceClassification), +/* harmony export */ DebertaForTokenClassification: () => (/* binding */ DebertaForTokenClassification), +/* harmony export */ DebertaModel: () => (/* binding */ DebertaModel), +/* harmony export */ DebertaPreTrainedModel: () => (/* binding */ DebertaPreTrainedModel), +/* harmony export */ DebertaV2ForMaskedLM: () => (/* binding */ DebertaV2ForMaskedLM), +/* harmony export */ DebertaV2ForQuestionAnswering: () => (/* binding */ DebertaV2ForQuestionAnswering), +/* harmony export */ DebertaV2ForSequenceClassification: () => (/* binding */ DebertaV2ForSequenceClassification), +/* harmony export */ DebertaV2ForTokenClassification: () => (/* binding */ DebertaV2ForTokenClassification), +/* harmony export */ DebertaV2Model: () => (/* binding */ DebertaV2Model), +/* harmony export */ DebertaV2PreTrainedModel: () => (/* binding */ DebertaV2PreTrainedModel), +/* harmony export */ DecisionTransformerModel: () => (/* binding */ DecisionTransformerModel), +/* harmony export */ DecisionTransformerPreTrainedModel: () => (/* binding */ DecisionTransformerPreTrainedModel), +/* harmony export */ DeiTForImageClassification: () => (/* binding */ DeiTForImageClassification), +/* harmony export */ DeiTModel: () => (/* binding */ DeiTModel), +/* harmony export */ DeiTPreTrainedModel: () => (/* binding */ DeiTPreTrainedModel), +/* harmony export */ DepthAnythingForDepthEstimation: () => (/* binding */ DepthAnythingForDepthEstimation), +/* harmony export */ DepthAnythingPreTrainedModel: () => (/* binding */ DepthAnythingPreTrainedModel), +/* harmony export */ DepthProForDepthEstimation: () => (/* binding */ DepthProForDepthEstimation), +/* harmony export */ DepthProPreTrainedModel: () => (/* binding */ DepthProPreTrainedModel), +/* harmony export */ DetrForObjectDetection: () => (/* binding */ DetrForObjectDetection), +/* harmony export */ DetrForSegmentation: () => (/* binding */ DetrForSegmentation), +/* harmony export */ DetrModel: () => (/* binding */ DetrModel), +/* harmony export */ DetrObjectDetectionOutput: () => (/* binding */ DetrObjectDetectionOutput), +/* harmony export */ DetrPreTrainedModel: () => (/* binding */ DetrPreTrainedModel), +/* harmony export */ DetrSegmentationOutput: () => (/* binding */ DetrSegmentationOutput), +/* harmony export */ Dinov2ForImageClassification: () => (/* binding */ Dinov2ForImageClassification), +/* harmony export */ Dinov2Model: () => (/* binding */ Dinov2Model), +/* harmony export */ Dinov2PreTrainedModel: () => (/* binding */ Dinov2PreTrainedModel), +/* harmony export */ Dinov2WithRegistersForImageClassification: () => (/* binding */ Dinov2WithRegistersForImageClassification), +/* harmony export */ Dinov2WithRegistersModel: () => (/* binding */ Dinov2WithRegistersModel), +/* harmony export */ Dinov2WithRegistersPreTrainedModel: () => (/* binding */ Dinov2WithRegistersPreTrainedModel), +/* harmony export */ DistilBertForMaskedLM: () => (/* binding */ DistilBertForMaskedLM), +/* harmony export */ DistilBertForQuestionAnswering: () => (/* binding */ DistilBertForQuestionAnswering), +/* harmony export */ DistilBertForSequenceClassification: () => (/* binding */ DistilBertForSequenceClassification), +/* harmony export */ DistilBertForTokenClassification: () => (/* binding */ DistilBertForTokenClassification), +/* harmony export */ DistilBertModel: () => (/* binding */ DistilBertModel), +/* harmony export */ DistilBertPreTrainedModel: () => (/* binding */ DistilBertPreTrainedModel), +/* harmony export */ DonutSwinModel: () => (/* binding */ DonutSwinModel), +/* harmony export */ DonutSwinPreTrainedModel: () => (/* binding */ DonutSwinPreTrainedModel), +/* harmony export */ EfficientNetForImageClassification: () => (/* binding */ EfficientNetForImageClassification), +/* harmony export */ EfficientNetModel: () => (/* binding */ EfficientNetModel), +/* harmony export */ EfficientNetPreTrainedModel: () => (/* binding */ EfficientNetPreTrainedModel), +/* harmony export */ ElectraForMaskedLM: () => (/* binding */ ElectraForMaskedLM), +/* harmony export */ ElectraForQuestionAnswering: () => (/* binding */ ElectraForQuestionAnswering), +/* harmony export */ ElectraForSequenceClassification: () => (/* binding */ ElectraForSequenceClassification), +/* harmony export */ ElectraForTokenClassification: () => (/* binding */ ElectraForTokenClassification), +/* harmony export */ ElectraModel: () => (/* binding */ ElectraModel), +/* harmony export */ ElectraPreTrainedModel: () => (/* binding */ ElectraPreTrainedModel), +/* harmony export */ EsmForMaskedLM: () => (/* binding */ EsmForMaskedLM), +/* harmony export */ EsmForSequenceClassification: () => (/* binding */ EsmForSequenceClassification), +/* harmony export */ EsmForTokenClassification: () => (/* binding */ EsmForTokenClassification), +/* harmony export */ EsmModel: () => (/* binding */ EsmModel), +/* harmony export */ EsmPreTrainedModel: () => (/* binding */ EsmPreTrainedModel), +/* harmony export */ ExaoneForCausalLM: () => (/* binding */ ExaoneForCausalLM), +/* harmony export */ ExaoneModel: () => (/* binding */ ExaoneModel), +/* harmony export */ ExaonePreTrainedModel: () => (/* binding */ ExaonePreTrainedModel), +/* harmony export */ FalconForCausalLM: () => (/* binding */ FalconForCausalLM), +/* harmony export */ FalconModel: () => (/* binding */ FalconModel), +/* harmony export */ FalconPreTrainedModel: () => (/* binding */ FalconPreTrainedModel), +/* harmony export */ FastViTForImageClassification: () => (/* binding */ FastViTForImageClassification), +/* harmony export */ FastViTModel: () => (/* binding */ FastViTModel), +/* harmony export */ FastViTPreTrainedModel: () => (/* binding */ FastViTPreTrainedModel), +/* harmony export */ Florence2ForConditionalGeneration: () => (/* binding */ Florence2ForConditionalGeneration), +/* harmony export */ Florence2PreTrainedModel: () => (/* binding */ Florence2PreTrainedModel), +/* harmony export */ GLPNForDepthEstimation: () => (/* binding */ GLPNForDepthEstimation), +/* harmony export */ GLPNModel: () => (/* binding */ GLPNModel), +/* harmony export */ GLPNPreTrainedModel: () => (/* binding */ GLPNPreTrainedModel), +/* harmony export */ GPT2LMHeadModel: () => (/* binding */ GPT2LMHeadModel), +/* harmony export */ GPT2Model: () => (/* binding */ GPT2Model), +/* harmony export */ GPT2PreTrainedModel: () => (/* binding */ GPT2PreTrainedModel), +/* harmony export */ GPTBigCodeForCausalLM: () => (/* binding */ GPTBigCodeForCausalLM), +/* harmony export */ GPTBigCodeModel: () => (/* binding */ GPTBigCodeModel), +/* harmony export */ GPTBigCodePreTrainedModel: () => (/* binding */ GPTBigCodePreTrainedModel), +/* harmony export */ GPTJForCausalLM: () => (/* binding */ GPTJForCausalLM), +/* harmony export */ GPTJModel: () => (/* binding */ GPTJModel), +/* harmony export */ GPTJPreTrainedModel: () => (/* binding */ GPTJPreTrainedModel), +/* harmony export */ GPTNeoForCausalLM: () => (/* binding */ GPTNeoForCausalLM), +/* harmony export */ GPTNeoModel: () => (/* binding */ GPTNeoModel), +/* harmony export */ GPTNeoPreTrainedModel: () => (/* binding */ GPTNeoPreTrainedModel), +/* harmony export */ GPTNeoXForCausalLM: () => (/* binding */ GPTNeoXForCausalLM), +/* harmony export */ GPTNeoXModel: () => (/* binding */ GPTNeoXModel), +/* harmony export */ GPTNeoXPreTrainedModel: () => (/* binding */ GPTNeoXPreTrainedModel), +/* harmony export */ Gemma2ForCausalLM: () => (/* binding */ Gemma2ForCausalLM), +/* harmony export */ Gemma2Model: () => (/* binding */ Gemma2Model), +/* harmony export */ Gemma2PreTrainedModel: () => (/* binding */ Gemma2PreTrainedModel), +/* harmony export */ Gemma3ForCausalLM: () => (/* binding */ Gemma3ForCausalLM), +/* harmony export */ Gemma3Model: () => (/* binding */ Gemma3Model), +/* harmony export */ Gemma3PreTrainedModel: () => (/* binding */ Gemma3PreTrainedModel), +/* harmony export */ GemmaForCausalLM: () => (/* binding */ GemmaForCausalLM), +/* harmony export */ GemmaModel: () => (/* binding */ GemmaModel), +/* harmony export */ GemmaPreTrainedModel: () => (/* binding */ GemmaPreTrainedModel), +/* harmony export */ GlmForCausalLM: () => (/* binding */ GlmForCausalLM), +/* harmony export */ GlmModel: () => (/* binding */ GlmModel), +/* harmony export */ GlmPreTrainedModel: () => (/* binding */ GlmPreTrainedModel), +/* harmony export */ GraniteForCausalLM: () => (/* binding */ GraniteForCausalLM), +/* harmony export */ GraniteModel: () => (/* binding */ GraniteModel), +/* harmony export */ GranitePreTrainedModel: () => (/* binding */ GranitePreTrainedModel), +/* harmony export */ GroundingDinoForObjectDetection: () => (/* binding */ GroundingDinoForObjectDetection), +/* harmony export */ GroundingDinoPreTrainedModel: () => (/* binding */ GroundingDinoPreTrainedModel), +/* harmony export */ GroupViTModel: () => (/* binding */ GroupViTModel), +/* harmony export */ GroupViTPreTrainedModel: () => (/* binding */ GroupViTPreTrainedModel), +/* harmony export */ HeliumForCausalLM: () => (/* binding */ HeliumForCausalLM), +/* harmony export */ HeliumModel: () => (/* binding */ HeliumModel), +/* harmony export */ HeliumPreTrainedModel: () => (/* binding */ HeliumPreTrainedModel), +/* harmony export */ HieraForImageClassification: () => (/* binding */ HieraForImageClassification), +/* harmony export */ HieraModel: () => (/* binding */ HieraModel), +/* harmony export */ HieraPreTrainedModel: () => (/* binding */ HieraPreTrainedModel), +/* harmony export */ HubertForCTC: () => (/* binding */ HubertForCTC), +/* harmony export */ HubertForSequenceClassification: () => (/* binding */ HubertForSequenceClassification), +/* harmony export */ HubertModel: () => (/* binding */ HubertModel), +/* harmony export */ HubertPreTrainedModel: () => (/* binding */ HubertPreTrainedModel), +/* harmony export */ IJepaForImageClassification: () => (/* binding */ IJepaForImageClassification), +/* harmony export */ IJepaModel: () => (/* binding */ IJepaModel), +/* harmony export */ IJepaPreTrainedModel: () => (/* binding */ IJepaPreTrainedModel), +/* harmony export */ Idefics3ForConditionalGeneration: () => (/* binding */ Idefics3ForConditionalGeneration), +/* harmony export */ Idefics3PreTrainedModel: () => (/* binding */ Idefics3PreTrainedModel), +/* harmony export */ ImageMattingOutput: () => (/* binding */ ImageMattingOutput), +/* harmony export */ JAISLMHeadModel: () => (/* binding */ JAISLMHeadModel), +/* harmony export */ JAISModel: () => (/* binding */ JAISModel), +/* harmony export */ JAISPreTrainedModel: () => (/* binding */ JAISPreTrainedModel), +/* harmony export */ JinaCLIPModel: () => (/* binding */ JinaCLIPModel), +/* harmony export */ JinaCLIPPreTrainedModel: () => (/* binding */ JinaCLIPPreTrainedModel), +/* harmony export */ JinaCLIPTextModel: () => (/* binding */ JinaCLIPTextModel), +/* harmony export */ JinaCLIPVisionModel: () => (/* binding */ JinaCLIPVisionModel), +/* harmony export */ LiteWhisperForConditionalGeneration: () => (/* binding */ LiteWhisperForConditionalGeneration), +/* harmony export */ LlamaForCausalLM: () => (/* binding */ LlamaForCausalLM), +/* harmony export */ LlamaModel: () => (/* binding */ LlamaModel), +/* harmony export */ LlamaPreTrainedModel: () => (/* binding */ LlamaPreTrainedModel), +/* harmony export */ LlavaForConditionalGeneration: () => (/* binding */ LlavaForConditionalGeneration), +/* harmony export */ LlavaOnevisionForConditionalGeneration: () => (/* binding */ LlavaOnevisionForConditionalGeneration), +/* harmony export */ LlavaPreTrainedModel: () => (/* binding */ LlavaPreTrainedModel), +/* harmony export */ LongT5ForConditionalGeneration: () => (/* binding */ LongT5ForConditionalGeneration), +/* harmony export */ LongT5Model: () => (/* binding */ LongT5Model), +/* harmony export */ LongT5PreTrainedModel: () => (/* binding */ LongT5PreTrainedModel), +/* harmony export */ M2M100ForConditionalGeneration: () => (/* binding */ M2M100ForConditionalGeneration), +/* harmony export */ M2M100Model: () => (/* binding */ M2M100Model), +/* harmony export */ M2M100PreTrainedModel: () => (/* binding */ M2M100PreTrainedModel), +/* harmony export */ MBartForCausalLM: () => (/* binding */ MBartForCausalLM), +/* harmony export */ MBartForConditionalGeneration: () => (/* binding */ MBartForConditionalGeneration), +/* harmony export */ MBartForSequenceClassification: () => (/* binding */ MBartForSequenceClassification), +/* harmony export */ MBartModel: () => (/* binding */ MBartModel), +/* harmony export */ MBartPreTrainedModel: () => (/* binding */ MBartPreTrainedModel), +/* harmony export */ MPNetForMaskedLM: () => (/* binding */ MPNetForMaskedLM), +/* harmony export */ MPNetForQuestionAnswering: () => (/* binding */ MPNetForQuestionAnswering), +/* harmony export */ MPNetForSequenceClassification: () => (/* binding */ MPNetForSequenceClassification), +/* harmony export */ MPNetForTokenClassification: () => (/* binding */ MPNetForTokenClassification), +/* harmony export */ MPNetModel: () => (/* binding */ MPNetModel), +/* harmony export */ MPNetPreTrainedModel: () => (/* binding */ MPNetPreTrainedModel), +/* harmony export */ MT5ForConditionalGeneration: () => (/* binding */ MT5ForConditionalGeneration), +/* harmony export */ MT5Model: () => (/* binding */ MT5Model), +/* harmony export */ MT5PreTrainedModel: () => (/* binding */ MT5PreTrainedModel), +/* harmony export */ MarianMTModel: () => (/* binding */ MarianMTModel), +/* harmony export */ MarianModel: () => (/* binding */ MarianModel), +/* harmony export */ MarianPreTrainedModel: () => (/* binding */ MarianPreTrainedModel), +/* harmony export */ MaskFormerForInstanceSegmentation: () => (/* binding */ MaskFormerForInstanceSegmentation), +/* harmony export */ MaskFormerModel: () => (/* binding */ MaskFormerModel), +/* harmony export */ MaskFormerPreTrainedModel: () => (/* binding */ MaskFormerPreTrainedModel), +/* harmony export */ MaskedLMOutput: () => (/* binding */ MaskedLMOutput), +/* harmony export */ Metric3DForDepthEstimation: () => (/* binding */ Metric3DForDepthEstimation), +/* harmony export */ Metric3DPreTrainedModel: () => (/* binding */ Metric3DPreTrainedModel), +/* harmony export */ Metric3Dv2ForDepthEstimation: () => (/* binding */ Metric3Dv2ForDepthEstimation), +/* harmony export */ Metric3Dv2PreTrainedModel: () => (/* binding */ Metric3Dv2PreTrainedModel), +/* harmony export */ MgpstrForSceneTextRecognition: () => (/* binding */ MgpstrForSceneTextRecognition), +/* harmony export */ MgpstrModelOutput: () => (/* binding */ MgpstrModelOutput), +/* harmony export */ MgpstrPreTrainedModel: () => (/* binding */ MgpstrPreTrainedModel), +/* harmony export */ MimiDecoderModel: () => (/* binding */ MimiDecoderModel), +/* harmony export */ MimiDecoderOutput: () => (/* binding */ MimiDecoderOutput), +/* harmony export */ MimiEncoderModel: () => (/* binding */ MimiEncoderModel), +/* harmony export */ MimiEncoderOutput: () => (/* binding */ MimiEncoderOutput), +/* harmony export */ MimiModel: () => (/* binding */ MimiModel), +/* harmony export */ MimiPreTrainedModel: () => (/* binding */ MimiPreTrainedModel), +/* harmony export */ MistralForCausalLM: () => (/* binding */ MistralForCausalLM), +/* harmony export */ MistralModel: () => (/* binding */ MistralModel), +/* harmony export */ MistralPreTrainedModel: () => (/* binding */ MistralPreTrainedModel), +/* harmony export */ MobileBertForMaskedLM: () => (/* binding */ MobileBertForMaskedLM), +/* harmony export */ MobileBertForQuestionAnswering: () => (/* binding */ MobileBertForQuestionAnswering), +/* harmony export */ MobileBertForSequenceClassification: () => (/* binding */ MobileBertForSequenceClassification), +/* harmony export */ MobileBertModel: () => (/* binding */ MobileBertModel), +/* harmony export */ MobileBertPreTrainedModel: () => (/* binding */ MobileBertPreTrainedModel), +/* harmony export */ MobileLLMForCausalLM: () => (/* binding */ MobileLLMForCausalLM), +/* harmony export */ MobileLLMModel: () => (/* binding */ MobileLLMModel), +/* harmony export */ MobileLLMPreTrainedModel: () => (/* binding */ MobileLLMPreTrainedModel), +/* harmony export */ MobileNetV1ForImageClassification: () => (/* binding */ MobileNetV1ForImageClassification), +/* harmony export */ MobileNetV1ForSemanticSegmentation: () => (/* binding */ MobileNetV1ForSemanticSegmentation), +/* harmony export */ MobileNetV1Model: () => (/* binding */ MobileNetV1Model), +/* harmony export */ MobileNetV1PreTrainedModel: () => (/* binding */ MobileNetV1PreTrainedModel), +/* harmony export */ MobileNetV2ForImageClassification: () => (/* binding */ MobileNetV2ForImageClassification), +/* harmony export */ MobileNetV2ForSemanticSegmentation: () => (/* binding */ MobileNetV2ForSemanticSegmentation), +/* harmony export */ MobileNetV2Model: () => (/* binding */ MobileNetV2Model), +/* harmony export */ MobileNetV2PreTrainedModel: () => (/* binding */ MobileNetV2PreTrainedModel), +/* harmony export */ MobileNetV3ForImageClassification: () => (/* binding */ MobileNetV3ForImageClassification), +/* harmony export */ MobileNetV3ForSemanticSegmentation: () => (/* binding */ MobileNetV3ForSemanticSegmentation), +/* harmony export */ MobileNetV3Model: () => (/* binding */ MobileNetV3Model), +/* harmony export */ MobileNetV3PreTrainedModel: () => (/* binding */ MobileNetV3PreTrainedModel), +/* harmony export */ MobileNetV4ForImageClassification: () => (/* binding */ MobileNetV4ForImageClassification), +/* harmony export */ MobileNetV4ForSemanticSegmentation: () => (/* binding */ MobileNetV4ForSemanticSegmentation), +/* harmony export */ MobileNetV4Model: () => (/* binding */ MobileNetV4Model), +/* harmony export */ MobileNetV4PreTrainedModel: () => (/* binding */ MobileNetV4PreTrainedModel), +/* harmony export */ MobileViTForImageClassification: () => (/* binding */ MobileViTForImageClassification), +/* harmony export */ MobileViTModel: () => (/* binding */ MobileViTModel), +/* harmony export */ MobileViTPreTrainedModel: () => (/* binding */ MobileViTPreTrainedModel), +/* harmony export */ MobileViTV2ForImageClassification: () => (/* binding */ MobileViTV2ForImageClassification), +/* harmony export */ MobileViTV2Model: () => (/* binding */ MobileViTV2Model), +/* harmony export */ MobileViTV2PreTrainedModel: () => (/* binding */ MobileViTV2PreTrainedModel), +/* harmony export */ ModelOutput: () => (/* binding */ ModelOutput), +/* harmony export */ ModernBertForMaskedLM: () => (/* binding */ ModernBertForMaskedLM), +/* harmony export */ ModernBertForSequenceClassification: () => (/* binding */ ModernBertForSequenceClassification), +/* harmony export */ ModernBertForTokenClassification: () => (/* binding */ ModernBertForTokenClassification), +/* harmony export */ ModernBertModel: () => (/* binding */ ModernBertModel), +/* harmony export */ ModernBertPreTrainedModel: () => (/* binding */ ModernBertPreTrainedModel), +/* harmony export */ Moondream1ForConditionalGeneration: () => (/* binding */ Moondream1ForConditionalGeneration), +/* harmony export */ MoonshineForConditionalGeneration: () => (/* binding */ MoonshineForConditionalGeneration), +/* harmony export */ MoonshineModel: () => (/* binding */ MoonshineModel), +/* harmony export */ MoonshinePreTrainedModel: () => (/* binding */ MoonshinePreTrainedModel), +/* harmony export */ MptForCausalLM: () => (/* binding */ MptForCausalLM), +/* harmony export */ MptModel: () => (/* binding */ MptModel), +/* harmony export */ MptPreTrainedModel: () => (/* binding */ MptPreTrainedModel), +/* harmony export */ MultiModalityCausalLM: () => (/* binding */ MultiModalityCausalLM), +/* harmony export */ MultiModalityPreTrainedModel: () => (/* binding */ MultiModalityPreTrainedModel), +/* harmony export */ MusicgenForCausalLM: () => (/* binding */ MusicgenForCausalLM), +/* harmony export */ MusicgenForConditionalGeneration: () => (/* binding */ MusicgenForConditionalGeneration), +/* harmony export */ MusicgenModel: () => (/* binding */ MusicgenModel), +/* harmony export */ MusicgenPreTrainedModel: () => (/* binding */ MusicgenPreTrainedModel), +/* harmony export */ NomicBertModel: () => (/* binding */ NomicBertModel), +/* harmony export */ NomicBertPreTrainedModel: () => (/* binding */ NomicBertPreTrainedModel), +/* harmony export */ OPTForCausalLM: () => (/* binding */ OPTForCausalLM), +/* harmony export */ OPTModel: () => (/* binding */ OPTModel), +/* harmony export */ OPTPreTrainedModel: () => (/* binding */ OPTPreTrainedModel), +/* harmony export */ Olmo2ForCausalLM: () => (/* binding */ Olmo2ForCausalLM), +/* harmony export */ Olmo2Model: () => (/* binding */ Olmo2Model), +/* harmony export */ Olmo2PreTrainedModel: () => (/* binding */ Olmo2PreTrainedModel), +/* harmony export */ OlmoForCausalLM: () => (/* binding */ OlmoForCausalLM), +/* harmony export */ OlmoModel: () => (/* binding */ OlmoModel), +/* harmony export */ OlmoPreTrainedModel: () => (/* binding */ OlmoPreTrainedModel), +/* harmony export */ OpenELMForCausalLM: () => (/* binding */ OpenELMForCausalLM), +/* harmony export */ OpenELMModel: () => (/* binding */ OpenELMModel), +/* harmony export */ OpenELMPreTrainedModel: () => (/* binding */ OpenELMPreTrainedModel), +/* harmony export */ OwlViTForObjectDetection: () => (/* binding */ OwlViTForObjectDetection), +/* harmony export */ OwlViTModel: () => (/* binding */ OwlViTModel), +/* harmony export */ OwlViTPreTrainedModel: () => (/* binding */ OwlViTPreTrainedModel), +/* harmony export */ Owlv2ForObjectDetection: () => (/* binding */ Owlv2ForObjectDetection), +/* harmony export */ Owlv2Model: () => (/* binding */ Owlv2Model), +/* harmony export */ Owlv2PreTrainedModel: () => (/* binding */ Owlv2PreTrainedModel), +/* harmony export */ PaliGemmaForConditionalGeneration: () => (/* binding */ PaliGemmaForConditionalGeneration), +/* harmony export */ PaliGemmaPreTrainedModel: () => (/* binding */ PaliGemmaPreTrainedModel), +/* harmony export */ PatchTSMixerForPrediction: () => (/* binding */ PatchTSMixerForPrediction), +/* harmony export */ PatchTSMixerModel: () => (/* binding */ PatchTSMixerModel), +/* harmony export */ PatchTSMixerPreTrainedModel: () => (/* binding */ PatchTSMixerPreTrainedModel), +/* harmony export */ PatchTSTForPrediction: () => (/* binding */ PatchTSTForPrediction), +/* harmony export */ PatchTSTModel: () => (/* binding */ PatchTSTModel), +/* harmony export */ PatchTSTPreTrainedModel: () => (/* binding */ PatchTSTPreTrainedModel), +/* harmony export */ Phi3ForCausalLM: () => (/* binding */ Phi3ForCausalLM), +/* harmony export */ Phi3Model: () => (/* binding */ Phi3Model), +/* harmony export */ Phi3PreTrainedModel: () => (/* binding */ Phi3PreTrainedModel), +/* harmony export */ Phi3VForCausalLM: () => (/* binding */ Phi3VForCausalLM), +/* harmony export */ Phi3VPreTrainedModel: () => (/* binding */ Phi3VPreTrainedModel), +/* harmony export */ PhiForCausalLM: () => (/* binding */ PhiForCausalLM), +/* harmony export */ PhiModel: () => (/* binding */ PhiModel), +/* harmony export */ PhiPreTrainedModel: () => (/* binding */ PhiPreTrainedModel), +/* harmony export */ PreTrainedModel: () => (/* binding */ PreTrainedModel), +/* harmony export */ PretrainedMixin: () => (/* binding */ PretrainedMixin), +/* harmony export */ PvtForImageClassification: () => (/* binding */ PvtForImageClassification), +/* harmony export */ PvtModel: () => (/* binding */ PvtModel), +/* harmony export */ PvtPreTrainedModel: () => (/* binding */ PvtPreTrainedModel), +/* harmony export */ PyAnnoteForAudioFrameClassification: () => (/* binding */ PyAnnoteForAudioFrameClassification), +/* harmony export */ PyAnnoteModel: () => (/* binding */ PyAnnoteModel), +/* harmony export */ PyAnnotePreTrainedModel: () => (/* binding */ PyAnnotePreTrainedModel), +/* harmony export */ QuestionAnsweringModelOutput: () => (/* binding */ QuestionAnsweringModelOutput), +/* harmony export */ Qwen2ForCausalLM: () => (/* binding */ Qwen2ForCausalLM), +/* harmony export */ Qwen2Model: () => (/* binding */ Qwen2Model), +/* harmony export */ Qwen2PreTrainedModel: () => (/* binding */ Qwen2PreTrainedModel), +/* harmony export */ Qwen2VLForConditionalGeneration: () => (/* binding */ Qwen2VLForConditionalGeneration), +/* harmony export */ Qwen2VLPreTrainedModel: () => (/* binding */ Qwen2VLPreTrainedModel), +/* harmony export */ Qwen3ForCausalLM: () => (/* binding */ Qwen3ForCausalLM), +/* harmony export */ Qwen3Model: () => (/* binding */ Qwen3Model), +/* harmony export */ Qwen3PreTrainedModel: () => (/* binding */ Qwen3PreTrainedModel), +/* harmony export */ RFDetrForObjectDetection: () => (/* binding */ RFDetrForObjectDetection), +/* harmony export */ RFDetrModel: () => (/* binding */ RFDetrModel), +/* harmony export */ RFDetrObjectDetectionOutput: () => (/* binding */ RFDetrObjectDetectionOutput), +/* harmony export */ RFDetrPreTrainedModel: () => (/* binding */ RFDetrPreTrainedModel), +/* harmony export */ RTDetrForObjectDetection: () => (/* binding */ RTDetrForObjectDetection), +/* harmony export */ RTDetrModel: () => (/* binding */ RTDetrModel), +/* harmony export */ RTDetrObjectDetectionOutput: () => (/* binding */ RTDetrObjectDetectionOutput), +/* harmony export */ RTDetrPreTrainedModel: () => (/* binding */ RTDetrPreTrainedModel), +/* harmony export */ RTDetrV2ForObjectDetection: () => (/* binding */ RTDetrV2ForObjectDetection), +/* harmony export */ RTDetrV2Model: () => (/* binding */ RTDetrV2Model), +/* harmony export */ RTDetrV2ObjectDetectionOutput: () => (/* binding */ RTDetrV2ObjectDetectionOutput), +/* harmony export */ RTDetrV2PreTrainedModel: () => (/* binding */ RTDetrV2PreTrainedModel), +/* harmony export */ ResNetForImageClassification: () => (/* binding */ ResNetForImageClassification), +/* harmony export */ ResNetModel: () => (/* binding */ ResNetModel), +/* harmony export */ ResNetPreTrainedModel: () => (/* binding */ ResNetPreTrainedModel), +/* harmony export */ RoFormerForMaskedLM: () => (/* binding */ RoFormerForMaskedLM), +/* harmony export */ RoFormerForQuestionAnswering: () => (/* binding */ RoFormerForQuestionAnswering), +/* harmony export */ RoFormerForSequenceClassification: () => (/* binding */ RoFormerForSequenceClassification), +/* harmony export */ RoFormerForTokenClassification: () => (/* binding */ RoFormerForTokenClassification), +/* harmony export */ RoFormerModel: () => (/* binding */ RoFormerModel), +/* harmony export */ RoFormerPreTrainedModel: () => (/* binding */ RoFormerPreTrainedModel), +/* harmony export */ RobertaForMaskedLM: () => (/* binding */ RobertaForMaskedLM), +/* harmony export */ RobertaForQuestionAnswering: () => (/* binding */ RobertaForQuestionAnswering), +/* harmony export */ RobertaForSequenceClassification: () => (/* binding */ RobertaForSequenceClassification), +/* harmony export */ RobertaForTokenClassification: () => (/* binding */ RobertaForTokenClassification), +/* harmony export */ RobertaModel: () => (/* binding */ RobertaModel), +/* harmony export */ RobertaPreTrainedModel: () => (/* binding */ RobertaPreTrainedModel), +/* harmony export */ SamImageSegmentationOutput: () => (/* binding */ SamImageSegmentationOutput), +/* harmony export */ SamModel: () => (/* binding */ SamModel), +/* harmony export */ SamPreTrainedModel: () => (/* binding */ SamPreTrainedModel), +/* harmony export */ SapiensForDepthEstimation: () => (/* binding */ SapiensForDepthEstimation), +/* harmony export */ SapiensForNormalEstimation: () => (/* binding */ SapiensForNormalEstimation), +/* harmony export */ SapiensForSemanticSegmentation: () => (/* binding */ SapiensForSemanticSegmentation), +/* harmony export */ SapiensPreTrainedModel: () => (/* binding */ SapiensPreTrainedModel), +/* harmony export */ SegformerForImageClassification: () => (/* binding */ SegformerForImageClassification), +/* harmony export */ SegformerForSemanticSegmentation: () => (/* binding */ SegformerForSemanticSegmentation), +/* harmony export */ SegformerModel: () => (/* binding */ SegformerModel), +/* harmony export */ SegformerPreTrainedModel: () => (/* binding */ SegformerPreTrainedModel), +/* harmony export */ Seq2SeqLMOutput: () => (/* binding */ Seq2SeqLMOutput), +/* harmony export */ SequenceClassifierOutput: () => (/* binding */ SequenceClassifierOutput), +/* harmony export */ SiglipModel: () => (/* binding */ SiglipModel), +/* harmony export */ SiglipPreTrainedModel: () => (/* binding */ SiglipPreTrainedModel), +/* harmony export */ SiglipTextModel: () => (/* binding */ SiglipTextModel), +/* harmony export */ SiglipVisionModel: () => (/* binding */ SiglipVisionModel), +/* harmony export */ SmolVLMForConditionalGeneration: () => (/* binding */ SmolVLMForConditionalGeneration), +/* harmony export */ SnacDecoderModel: () => (/* binding */ SnacDecoderModel), +/* harmony export */ SnacEncoderModel: () => (/* binding */ SnacEncoderModel), +/* harmony export */ SnacModel: () => (/* binding */ SnacModel), +/* harmony export */ SnacPreTrainedModel: () => (/* binding */ SnacPreTrainedModel), +/* harmony export */ SpeechT5ForSpeechToText: () => (/* binding */ SpeechT5ForSpeechToText), +/* harmony export */ SpeechT5ForTextToSpeech: () => (/* binding */ SpeechT5ForTextToSpeech), +/* harmony export */ SpeechT5HifiGan: () => (/* binding */ SpeechT5HifiGan), +/* harmony export */ SpeechT5Model: () => (/* binding */ SpeechT5Model), +/* harmony export */ SpeechT5PreTrainedModel: () => (/* binding */ SpeechT5PreTrainedModel), +/* harmony export */ SqueezeBertForMaskedLM: () => (/* binding */ SqueezeBertForMaskedLM), +/* harmony export */ SqueezeBertForQuestionAnswering: () => (/* binding */ SqueezeBertForQuestionAnswering), +/* harmony export */ SqueezeBertForSequenceClassification: () => (/* binding */ SqueezeBertForSequenceClassification), +/* harmony export */ SqueezeBertModel: () => (/* binding */ SqueezeBertModel), +/* harmony export */ SqueezeBertPreTrainedModel: () => (/* binding */ SqueezeBertPreTrainedModel), +/* harmony export */ StableLmForCausalLM: () => (/* binding */ StableLmForCausalLM), +/* harmony export */ StableLmModel: () => (/* binding */ StableLmModel), +/* harmony export */ StableLmPreTrainedModel: () => (/* binding */ StableLmPreTrainedModel), +/* harmony export */ Starcoder2ForCausalLM: () => (/* binding */ Starcoder2ForCausalLM), +/* harmony export */ Starcoder2Model: () => (/* binding */ Starcoder2Model), +/* harmony export */ Starcoder2PreTrainedModel: () => (/* binding */ Starcoder2PreTrainedModel), +/* harmony export */ StyleTextToSpeech2Model: () => (/* binding */ StyleTextToSpeech2Model), +/* harmony export */ StyleTextToSpeech2PreTrainedModel: () => (/* binding */ StyleTextToSpeech2PreTrainedModel), +/* harmony export */ Swin2SRForImageSuperResolution: () => (/* binding */ Swin2SRForImageSuperResolution), +/* harmony export */ Swin2SRModel: () => (/* binding */ Swin2SRModel), +/* harmony export */ Swin2SRPreTrainedModel: () => (/* binding */ Swin2SRPreTrainedModel), +/* harmony export */ SwinForImageClassification: () => (/* binding */ SwinForImageClassification), +/* harmony export */ SwinForSemanticSegmentation: () => (/* binding */ SwinForSemanticSegmentation), +/* harmony export */ SwinModel: () => (/* binding */ SwinModel), +/* harmony export */ SwinPreTrainedModel: () => (/* binding */ SwinPreTrainedModel), +/* harmony export */ T5ForConditionalGeneration: () => (/* binding */ T5ForConditionalGeneration), +/* harmony export */ T5Model: () => (/* binding */ T5Model), +/* harmony export */ T5PreTrainedModel: () => (/* binding */ T5PreTrainedModel), +/* harmony export */ TableTransformerForObjectDetection: () => (/* binding */ TableTransformerForObjectDetection), +/* harmony export */ TableTransformerModel: () => (/* binding */ TableTransformerModel), +/* harmony export */ TableTransformerObjectDetectionOutput: () => (/* binding */ TableTransformerObjectDetectionOutput), +/* harmony export */ TableTransformerPreTrainedModel: () => (/* binding */ TableTransformerPreTrainedModel), +/* harmony export */ TokenClassifierOutput: () => (/* binding */ TokenClassifierOutput), +/* harmony export */ TrOCRForCausalLM: () => (/* binding */ TrOCRForCausalLM), +/* harmony export */ TrOCRPreTrainedModel: () => (/* binding */ TrOCRPreTrainedModel), +/* harmony export */ UltravoxModel: () => (/* binding */ UltravoxModel), +/* harmony export */ UltravoxPreTrainedModel: () => (/* binding */ UltravoxPreTrainedModel), +/* harmony export */ UniSpeechForCTC: () => (/* binding */ UniSpeechForCTC), +/* harmony export */ UniSpeechForSequenceClassification: () => (/* binding */ UniSpeechForSequenceClassification), +/* harmony export */ UniSpeechModel: () => (/* binding */ UniSpeechModel), +/* harmony export */ UniSpeechPreTrainedModel: () => (/* binding */ UniSpeechPreTrainedModel), +/* harmony export */ UniSpeechSatForAudioFrameClassification: () => (/* binding */ UniSpeechSatForAudioFrameClassification), +/* harmony export */ UniSpeechSatForCTC: () => (/* binding */ UniSpeechSatForCTC), +/* harmony export */ UniSpeechSatForSequenceClassification: () => (/* binding */ UniSpeechSatForSequenceClassification), +/* harmony export */ UniSpeechSatModel: () => (/* binding */ UniSpeechSatModel), +/* harmony export */ UniSpeechSatPreTrainedModel: () => (/* binding */ UniSpeechSatPreTrainedModel), +/* harmony export */ ViTForImageClassification: () => (/* binding */ ViTForImageClassification), +/* harmony export */ ViTMAEModel: () => (/* binding */ ViTMAEModel), +/* harmony export */ ViTMAEPreTrainedModel: () => (/* binding */ ViTMAEPreTrainedModel), +/* harmony export */ ViTMSNForImageClassification: () => (/* binding */ ViTMSNForImageClassification), +/* harmony export */ ViTMSNModel: () => (/* binding */ ViTMSNModel), +/* harmony export */ ViTMSNPreTrainedModel: () => (/* binding */ ViTMSNPreTrainedModel), +/* harmony export */ ViTModel: () => (/* binding */ ViTModel), +/* harmony export */ ViTPreTrainedModel: () => (/* binding */ ViTPreTrainedModel), +/* harmony export */ VisionEncoderDecoderModel: () => (/* binding */ VisionEncoderDecoderModel), +/* harmony export */ VitMatteForImageMatting: () => (/* binding */ VitMatteForImageMatting), +/* harmony export */ VitMattePreTrainedModel: () => (/* binding */ VitMattePreTrainedModel), +/* harmony export */ VitPoseForPoseEstimation: () => (/* binding */ VitPoseForPoseEstimation), +/* harmony export */ VitPosePreTrainedModel: () => (/* binding */ VitPosePreTrainedModel), +/* harmony export */ VitsModel: () => (/* binding */ VitsModel), +/* harmony export */ VitsModelOutput: () => (/* binding */ VitsModelOutput), +/* harmony export */ VitsPreTrainedModel: () => (/* binding */ VitsPreTrainedModel), +/* harmony export */ Wav2Vec2BertForCTC: () => (/* binding */ Wav2Vec2BertForCTC), +/* harmony export */ Wav2Vec2BertForSequenceClassification: () => (/* binding */ Wav2Vec2BertForSequenceClassification), +/* harmony export */ Wav2Vec2BertModel: () => (/* binding */ Wav2Vec2BertModel), +/* harmony export */ Wav2Vec2BertPreTrainedModel: () => (/* binding */ Wav2Vec2BertPreTrainedModel), +/* harmony export */ Wav2Vec2ForAudioFrameClassification: () => (/* binding */ Wav2Vec2ForAudioFrameClassification), +/* harmony export */ Wav2Vec2ForCTC: () => (/* binding */ Wav2Vec2ForCTC), +/* harmony export */ Wav2Vec2ForSequenceClassification: () => (/* binding */ Wav2Vec2ForSequenceClassification), +/* harmony export */ Wav2Vec2Model: () => (/* binding */ Wav2Vec2Model), +/* harmony export */ Wav2Vec2PreTrainedModel: () => (/* binding */ Wav2Vec2PreTrainedModel), +/* harmony export */ WavLMForAudioFrameClassification: () => (/* binding */ WavLMForAudioFrameClassification), +/* harmony export */ WavLMForCTC: () => (/* binding */ WavLMForCTC), +/* harmony export */ WavLMForSequenceClassification: () => (/* binding */ WavLMForSequenceClassification), +/* harmony export */ WavLMForXVector: () => (/* binding */ WavLMForXVector), +/* harmony export */ WavLMModel: () => (/* binding */ WavLMModel), +/* harmony export */ WavLMPreTrainedModel: () => (/* binding */ WavLMPreTrainedModel), +/* harmony export */ WeSpeakerResNetModel: () => (/* binding */ WeSpeakerResNetModel), +/* harmony export */ WeSpeakerResNetPreTrainedModel: () => (/* binding */ WeSpeakerResNetPreTrainedModel), +/* harmony export */ WhisperForConditionalGeneration: () => (/* binding */ WhisperForConditionalGeneration), +/* harmony export */ WhisperModel: () => (/* binding */ WhisperModel), +/* harmony export */ WhisperPreTrainedModel: () => (/* binding */ WhisperPreTrainedModel), +/* harmony export */ XLMForQuestionAnswering: () => (/* binding */ XLMForQuestionAnswering), +/* harmony export */ XLMForSequenceClassification: () => (/* binding */ XLMForSequenceClassification), +/* harmony export */ XLMForTokenClassification: () => (/* binding */ XLMForTokenClassification), +/* harmony export */ XLMModel: () => (/* binding */ XLMModel), +/* harmony export */ XLMPreTrainedModel: () => (/* binding */ XLMPreTrainedModel), +/* harmony export */ XLMRobertaForMaskedLM: () => (/* binding */ XLMRobertaForMaskedLM), +/* harmony export */ XLMRobertaForQuestionAnswering: () => (/* binding */ XLMRobertaForQuestionAnswering), +/* harmony export */ XLMRobertaForSequenceClassification: () => (/* binding */ XLMRobertaForSequenceClassification), +/* harmony export */ XLMRobertaForTokenClassification: () => (/* binding */ XLMRobertaForTokenClassification), +/* harmony export */ XLMRobertaModel: () => (/* binding */ XLMRobertaModel), +/* harmony export */ XLMRobertaPreTrainedModel: () => (/* binding */ XLMRobertaPreTrainedModel), +/* harmony export */ XLMWithLMHeadModel: () => (/* binding */ XLMWithLMHeadModel), +/* harmony export */ XVectorOutput: () => (/* binding */ XVectorOutput), +/* harmony export */ YolosForObjectDetection: () => (/* binding */ YolosForObjectDetection), +/* harmony export */ YolosModel: () => (/* binding */ YolosModel), +/* harmony export */ YolosObjectDetectionOutput: () => (/* binding */ YolosObjectDetectionOutput), +/* harmony export */ YolosPreTrainedModel: () => (/* binding */ YolosPreTrainedModel) +/* harmony export */ }); +/* harmony import */ var _configs_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./configs.js */ "./src/configs.js"); +/* harmony import */ var _backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./backends/onnx.js */ "./src/backends/onnx.js"); +/* harmony import */ var _utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./utils/dtypes.js */ "./src/utils/dtypes.js"); +/* harmony import */ var _utils_generic_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./utils/generic.js */ "./src/utils/generic.js"); +/* harmony import */ var _utils_core_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ./utils/core.js */ "./src/utils/core.js"); +/* harmony import */ var _utils_hub_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ./utils/hub.js */ "./src/utils/hub.js"); +/* harmony import */ var _utils_constants_js__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! ./utils/constants.js */ "./src/utils/constants.js"); +/* harmony import */ var _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__(/*! ./generation/logits_process.js */ "./src/generation/logits_process.js"); +/* harmony import */ var _generation_configuration_utils_js__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__(/*! ./generation/configuration_utils.js */ "./src/generation/configuration_utils.js"); +/* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__ = __webpack_require__(/*! ./utils/tensor.js */ "./src/utils/tensor.js"); +/* harmony import */ var _utils_image_js__WEBPACK_IMPORTED_MODULE_10__ = __webpack_require__(/*! ./utils/image.js */ "./src/utils/image.js"); +/* harmony import */ var _utils_maths_js__WEBPACK_IMPORTED_MODULE_11__ = __webpack_require__(/*! ./utils/maths.js */ "./src/utils/maths.js"); +/* harmony import */ var _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_12__ = __webpack_require__(/*! ./generation/stopping_criteria.js */ "./src/generation/stopping_criteria.js"); +/* harmony import */ var _generation_logits_sampler_js__WEBPACK_IMPORTED_MODULE_13__ = __webpack_require__(/*! ./generation/logits_sampler.js */ "./src/generation/logits_sampler.js"); +/* harmony import */ var _env_js__WEBPACK_IMPORTED_MODULE_14__ = __webpack_require__(/*! ./env.js */ "./src/env.js"); +/* harmony import */ var _models_whisper_generation_whisper_js__WEBPACK_IMPORTED_MODULE_15__ = __webpack_require__(/*! ./models/whisper/generation_whisper.js */ "./src/models/whisper/generation_whisper.js"); +/* harmony import */ var _models_whisper_common_whisper_js__WEBPACK_IMPORTED_MODULE_16__ = __webpack_require__(/*! ./models/whisper/common_whisper.js */ "./src/models/whisper/common_whisper.js"); + +/** + * @file Definitions of all models available in Transformers.js. + * + * **Example:** Load and run an `AutoModel`. + * + * ```javascript + * import { AutoModel, AutoTokenizer } from '@huggingface/transformers'; + * + * let tokenizer = await AutoTokenizer.from_pretrained('Xenova/bert-base-uncased'); + * let model = await AutoModel.from_pretrained('Xenova/bert-base-uncased'); + * + * let inputs = await tokenizer('I love transformers!'); + * let { logits } = await model(inputs); + * // Tensor { + * // data: Float32Array(183132) [-7.117443084716797, -7.107812881469727, -7.092104911804199, ...] + * // dims: (3) [1, 6, 30522], + * // type: "float32", + * // size: 183132, + * // } + * ``` + * + * We also provide other `AutoModel`s (listed below), which you can use in the same way as the Python library. For example: + * + * **Example:** Load and run an `AutoModelForSeq2SeqLM`. + * ```javascript + * import { AutoModelForSeq2SeqLM, AutoTokenizer } from '@huggingface/transformers'; + * + * let tokenizer = await AutoTokenizer.from_pretrained('Xenova/t5-small'); + * let model = await AutoModelForSeq2SeqLM.from_pretrained('Xenova/t5-small'); + * + * let { input_ids } = await tokenizer('translate English to German: I love transformers!'); + * let outputs = await model.generate(input_ids); + * let decoded = tokenizer.decode(outputs[0], { skip_special_tokens: true }); + * // 'Ich liebe Transformatoren!' + * ``` + * + * @module models + */ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +////////////////////////////////////////////////// +// Model types: used internally +const MODEL_TYPES = { + EncoderOnly: 0, + EncoderDecoder: 1, + Seq2Seq: 2, + Vision2Seq: 3, + DecoderOnly: 4, + MaskGeneration: 5, + ImageTextToText: 6, + Musicgen: 7, + MultiModality: 8, + Phi3V: 9, + AudioTextToText: 10, + AutoEncoder: 11, +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Helper functions + +// NOTE: These will be populated fully later +const MODEL_TYPE_MAPPING = new Map(); +const MODEL_NAME_TO_CLASS_MAPPING = new Map(); +const MODEL_CLASS_TO_NAME_MAPPING = new Map(); + + +/** + * Constructs an InferenceSession using a model file located at the specified path. + * @param {string} pretrained_model_name_or_path The path to the directory containing the model file. + * @param {string} fileName The name of the model file. + * @param {import('./utils/hub.js').PretrainedModelOptions} options Additional options for loading the model. + * @returns {Promise<{buffer_or_path: Uint8Array|string, session_options: Object, session_config: Object}>} A Promise that resolves to the data needed to create an InferenceSession object. + * @private + */ +async function getSession(pretrained_model_name_or_path, fileName, options) { + let custom_config = options.config?.['transformers.js_config'] ?? {}; + + let device = options.device ?? custom_config.device; + if (device && typeof device !== 'string') { + if (device.hasOwnProperty(fileName)) { + device = device[fileName]; + } else { + console.warn(`device not specified for "${fileName}". Using the default device.`); + device = null; + } + } + + // If the device is not specified, we use the default (supported) execution providers. + const selectedDevice = /** @type {import("./utils/devices.js").DeviceType} */( + device ?? (_env_js__WEBPACK_IMPORTED_MODULE_14__.apis.IS_NODE_ENV ? 'cpu' : 'wasm') + ); + + const executionProviders = (0,_backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.deviceToExecutionProviders)(selectedDevice); + + // Update custom config with the selected device's config, if it exists + const device_config = custom_config.device_config ?? {}; + if (device_config.hasOwnProperty(selectedDevice)) { + custom_config = { + ...custom_config, + ...device_config[selectedDevice], + }; + } + + // If options.dtype is specified, we use it to choose the suffix for the model file. + // Otherwise, we use the default dtype for the device. + let dtype = options.dtype ?? custom_config.dtype; + if (typeof dtype !== 'string') { + if (dtype && dtype.hasOwnProperty(fileName)) { + dtype = dtype[fileName]; + } else { + dtype = _utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DEFAULT_DEVICE_DTYPE_MAPPING[selectedDevice] ?? _utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DATA_TYPES.fp32; + console.warn(`dtype not specified for "${fileName}". Using the default dtype (${dtype}) for this device (${selectedDevice}).`); + } + } + + if (dtype === _utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DATA_TYPES.auto) { + // Try to choose the auto dtype based on the custom config + let config_dtype = custom_config.dtype; + if (typeof config_dtype !== 'string') { + config_dtype = config_dtype?.[fileName]; + } + + if (config_dtype && config_dtype !== _utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DATA_TYPES.auto && _utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DATA_TYPES.hasOwnProperty(config_dtype)) { + // Defined by the config, and is not "auto" + dtype = config_dtype; + } else { + // Choose default dtype based on device, falling back to fp32 + dtype = _utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DEFAULT_DEVICE_DTYPE_MAPPING[selectedDevice] ?? _utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DATA_TYPES.fp32; + } + } + + const selectedDtype = /** @type {import("./utils/dtypes.js").DataType} */(dtype); + + if (!_utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DEFAULT_DTYPE_SUFFIX_MAPPING.hasOwnProperty(selectedDtype)) { + throw new Error(`Invalid dtype: ${selectedDtype}. Should be one of: ${Object.keys(_utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DATA_TYPES).join(', ')}`); + } else if (selectedDtype === _utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DATA_TYPES.fp16 && selectedDevice === 'webgpu' && !(await (0,_utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.isWebGpuFp16Supported)())) { + throw new Error(`The device (${selectedDevice}) does not support fp16.`); + } + + // Only valid for models with a decoder + const kv_cache_dtype_config = custom_config.kv_cache_dtype; + const kv_cache_dtype = kv_cache_dtype_config + ? (typeof kv_cache_dtype_config === 'string' + ? kv_cache_dtype_config + : kv_cache_dtype_config[selectedDtype] ?? 'float32') + : undefined; + + if (kv_cache_dtype && !['float32', 'float16'].includes(kv_cache_dtype)) { + throw new Error(`Invalid kv_cache_dtype: ${kv_cache_dtype}. Should be one of: float32, float16`); + } + + const session_config = { + dtype: selectedDtype, + kv_cache_dtype, + device: selectedDevice, + } + + // Construct the model file name + const suffix = _utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DEFAULT_DTYPE_SUFFIX_MAPPING[selectedDtype]; + const baseName = `${fileName}${suffix}.onnx`; + const modelFileName = `${options.subfolder ?? ''}/${baseName}`; + + const session_options = { ...options.session_options }; + + // Overwrite `executionProviders` if not specified + session_options.executionProviders ??= executionProviders; + + // Overwrite `freeDimensionOverrides` if specified in config and not set in session options + const free_dimension_overrides = custom_config.free_dimension_overrides; + if (free_dimension_overrides) { + session_options.freeDimensionOverrides ??= free_dimension_overrides; + } else if (selectedDevice.startsWith('webnn') && !session_options.freeDimensionOverrides) { + console.warn( + `WebNN does not currently support dynamic shapes and requires 'free_dimension_overrides' to be set in config.json, preferably as a field within config["transformers.js_config"]["device_config"]["${selectedDevice}"]. ` + + `When 'free_dimension_overrides' is not set, you may experience significant performance degradation.` + ); + } + + const return_path = _env_js__WEBPACK_IMPORTED_MODULE_14__.apis.IS_NODE_ENV && _env_js__WEBPACK_IMPORTED_MODULE_14__.env.useFSCache; + const bufferOrPathPromise = (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_5__.getModelFile)(pretrained_model_name_or_path, modelFileName, true, options, return_path); + + // Handle onnx external data files + const use_external_data_format = options.use_external_data_format ?? custom_config.use_external_data_format; + /** @type {Promise[]} */ + let externalDataPromises = []; + if (use_external_data_format) { + let external_data_format; + if (typeof use_external_data_format === 'object') { + if (use_external_data_format.hasOwnProperty(baseName)) { + external_data_format = use_external_data_format[baseName]; + } else if (use_external_data_format.hasOwnProperty(fileName)) { + external_data_format = use_external_data_format[fileName]; + } else { + external_data_format = false; + } + } else { + external_data_format = use_external_data_format; + } + + const num_chunks = +external_data_format; // (false=0, true=1, number remains the same) + if (num_chunks > _utils_hub_js__WEBPACK_IMPORTED_MODULE_5__.MAX_EXTERNAL_DATA_CHUNKS) { + throw new Error(`The number of external data chunks (${num_chunks}) exceeds the maximum allowed value (${_utils_hub_js__WEBPACK_IMPORTED_MODULE_5__.MAX_EXTERNAL_DATA_CHUNKS}).`); + } + for (let i = 0; i < num_chunks; ++i) { + const path = `${baseName}_data${i === 0 ? '' : '_' + i}`; + const fullPath = `${options.subfolder ?? ''}/${path}`; + externalDataPromises.push(new Promise(async (resolve, reject) => { + const data = await (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_5__.getModelFile)(pretrained_model_name_or_path, fullPath, true, options, return_path); + resolve(data instanceof Uint8Array ? { path, data } : path); + })); + } + + } else if (session_options.externalData !== undefined) { + externalDataPromises = session_options.externalData.map(async (ext) => { + // if the external data is a string, fetch the file and replace the string with its content + // @ts-expect-error TS2339 + if (typeof ext.data === "string") { + // @ts-expect-error TS2339 + const ext_buffer = await (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_5__.getModelFile)(pretrained_model_name_or_path, ext.data, true, options); + // @ts-expect-error TS2698 + return { ...ext, data: ext_buffer }; + } + return ext; + }); + } + + if (externalDataPromises.length > 0) { + const externalData = await Promise.all(externalDataPromises); + if (!_env_js__WEBPACK_IMPORTED_MODULE_14__.apis.IS_NODE_ENV) { + session_options.externalData = externalData; + } + } + + if (selectedDevice === 'webgpu') { + const shapes = (0,_configs_js__WEBPACK_IMPORTED_MODULE_0__.getKeyValueShapes)(options.config, { + prefix: 'present', + }); + if (Object.keys(shapes).length > 0 && !(0,_backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.isONNXProxy)()) { + // Only set preferredOutputLocation if shapes are present and we aren't proxying ONNX + /** @type {Record} */ + const preferredOutputLocation = {}; + for (const key in shapes) { + preferredOutputLocation[key] = 'gpu-buffer'; + } + session_options.preferredOutputLocation = preferredOutputLocation; + } + } + + const buffer_or_path = await bufferOrPathPromise; + + return { buffer_or_path, session_options, session_config }; +} + +/** + * Helper function to create multiple InferenceSession objects. + * + * @param {string} pretrained_model_name_or_path The path to the directory containing the model file. + * @param {Record} names The names of the model files to load. + * @param {import('./utils/hub.js').PretrainedModelOptions} options Additional options for loading the model. + * @returns {Promise>} A Promise that resolves to a dictionary of InferenceSession objects. + * @private + */ +async function constructSessions(pretrained_model_name_or_path, names, options) { + return Object.fromEntries(await Promise.all( + Object.keys(names).map(async (name) => { + const { buffer_or_path, session_options, session_config } = await getSession(pretrained_model_name_or_path, names[name], options); + const session = await (0,_backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.createInferenceSession)(buffer_or_path, session_options, session_config); + return [name, session]; + }) + )); +} + +/** + * Helper function to load multiple optional configuration files + * @param {string} pretrained_model_name_or_path The path to the directory containing the config file. + * @param {Record} names The names of the config files to load. + * @param {import('./utils/hub.js').PretrainedModelOptions} options Additional options for loading the configs. + * @returns {Promise>} A Promise that resolves to a dictionary of configuration objects. + * @private + */ +async function getOptionalConfigs(pretrained_model_name_or_path, names, options) { + return Object.fromEntries(await Promise.all( + Object.keys(names).map(async (name) => { + const config = await (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_5__.getModelJSON)(pretrained_model_name_or_path, names[name], false, options); + return [name, config]; + }) + )); +} + +/** + * Validate model inputs + * @param {Object} session The InferenceSession object that will be run. + * @param {Object} inputs The inputs to check. + * @returns {Record} The checked inputs. + * @throws {Error} If any inputs are missing. + * @private + */ +function validateInputs(session, inputs) { + /** + * NOTE: Create either a shallow or deep copy based on `onnx.wasm.proxy` + * @type {Record} + */ + const checkedInputs = Object.create(null); + const missingInputs = []; + for (const inputName of session.inputNames) { + const tensor = inputs[inputName]; + // Rare case where one of the model's input names corresponds to a built-in + // object name (e.g., toString), which would cause a simple (!tensor) check to fail, + // because it's not undefined but a function. + if (!(tensor instanceof _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor)) { + missingInputs.push(inputName); + continue; + } + // NOTE: When `env.wasm.proxy is true` the tensor is moved across the Worker + // boundary, transferring ownership to the worker and invalidating the tensor. + // So, in this case, we simply sacrifice a clone for it. + checkedInputs[inputName] = (0,_backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.isONNXProxy)() ? tensor.clone() : tensor; + } + if (missingInputs.length > 0) { + throw new Error( + `An error occurred during model execution: "Missing the following inputs: ${missingInputs.join(', ')}.`); + } + + const numInputsProvided = Object.keys(inputs).length; + const numInputsNeeded = session.inputNames.length; + if (numInputsProvided > numInputsNeeded) { + // No missing inputs, but too many inputs were provided. + // Warn the user and ignore the extra inputs. + let ignored = Object.keys(inputs).filter(inputName => !session.inputNames.includes(inputName)); + console.warn(`WARNING: Too many inputs were provided (${numInputsProvided} > ${numInputsNeeded}). The following inputs will be ignored: "${ignored.join(', ')}".`); + } + + return checkedInputs; +} + +// Currently, Transformers.js doesn't support simultaneous execution of sessions in WASM/WebGPU. +// For this reason, we need to chain the inference calls (otherwise we get "Error: Session already started"). +let webInferenceChain = Promise.resolve(); + +/** + * Executes an InferenceSession using the specified inputs. + * NOTE: `inputs` must contain at least the input names of the model. + * - If additional inputs are passed, they will be ignored. + * - If inputs are missing, an error will be thrown. + * + * @param {Object} session The InferenceSession object to run. + * @param {Object} inputs An object that maps input names to input tensors. + * @returns {Promise} A Promise that resolves to an object that maps output names to output tensors. + * @private + */ +async function sessionRun(session, inputs) { + const checkedInputs = validateInputs(session, inputs); + try { + // pass the original ort tensor + const ortFeed = Object.fromEntries(Object.entries(checkedInputs).map(([k, v]) => [k, v.ort_tensor])); + const run = () => session.run(ortFeed); + const output = await ((_env_js__WEBPACK_IMPORTED_MODULE_14__.apis.IS_BROWSER_ENV || _env_js__WEBPACK_IMPORTED_MODULE_14__.apis.IS_WEBWORKER_ENV) + ? (webInferenceChain = webInferenceChain.then(run)) + : run()); + return replaceTensors(output); + } catch (e) { + // Error messages can be long (nested) and uninformative. For this reason, + // we apply minor formatting to show the most important information + const formatted = Object.fromEntries(Object.entries(checkedInputs) + .map(([k, tensor]) => { + // Extract these properties from the underlying ORT tensor + const unpacked = { + type: tensor.type, + dims: tensor.dims, + location: tensor.location, + } + if (unpacked.location !== "gpu-buffer") { + // Only return the data if it's not a GPU buffer + unpacked.data = tensor.data; + } + return [k, unpacked]; + })); + + // This usually occurs when the inputs are of the wrong type. + console.error(`An error occurred during model execution: "${e}".`); + console.error('Inputs given to model:', formatted); + throw e; + } +} + +/** + * Replaces ONNX Tensor objects with custom Tensor objects to support additional functions. + * @param {Object} obj The object to replace tensor objects in. + * @returns {Object} The object with tensor objects replaced by custom Tensor objects. + * @private + */ +function replaceTensors(obj) { + for (let prop in obj) { + if ((0,_backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.isONNXTensor)(obj[prop])) { + obj[prop] = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor(obj[prop]); + } else if (typeof obj[prop] === 'object') { + replaceTensors(obj[prop]); + } + } + return obj; +} + + +/** + * Converts an array or Tensor of integers to an int64 Tensor. + * @param {any[]|Tensor} items The input integers to be converted. + * @returns {Tensor} The int64 Tensor with the converted values. + * @throws {Error} If the input array is empty or the input is a batched Tensor and not all sequences have the same length. + * @private + */ +function toI64Tensor(items) { + if (items instanceof _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor) { + return items; + } + // items is an array + if (items.length === 0) { + throw Error("items must be non-empty"); + } + + if (Array.isArray(items[0])) { + // batched + if (items.some(x => x.length !== items[0].length)) { + throw Error("Unable to create tensor, you should probably activate truncation and/or padding with 'padding=True' and/or 'truncation=True' to have batched tensors with the same length.") + } + + return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor('int64', + BigInt64Array.from(items.flat().map(x => BigInt(x))), + [items.length, items[0].length] + ); + } else { + //flat + return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor('int64', + BigInt64Array.from(items.map(x => BigInt(x))), + [1, items.length] + ); + } +} + +/** + * Creates a boolean tensor with a single value. + * @param {boolean} value The value of the tensor. + * @returns {Tensor} The boolean tensor. + * @private + */ +function boolTensor(value) { + return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor('bool', [value], [1]); +} + +// JS doesn't support mixins, so we define some reused functions here, and allow "this" to be passed in +/** + * Perform forward pass on the seq2seq model (both encoder and decoder). + * @param {Object} self The seq2seq model object. + * @param {Object} model_inputs The input object for the model containing encoder and decoder inputs. + * @returns {Promise} Promise that resolves with the output of the seq2seq model. + * @private + */ +async function seq2seqForward(self, model_inputs) { + let { encoder_outputs, input_ids, decoder_input_ids, ...other_decoder_inputs } = model_inputs; + // Encode if needed + if (!encoder_outputs) { + const encoder_inputs = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.pick)(model_inputs, self.sessions['model'].inputNames); + // Encoder outputs are not given, so we must compute them. + encoder_outputs = (await encoderForward(self, encoder_inputs)).last_hidden_state; + } + + other_decoder_inputs.input_ids = decoder_input_ids; + other_decoder_inputs.encoder_hidden_states = encoder_outputs; + + if (self.sessions['decoder_model_merged'].inputNames.includes('encoder_attention_mask')) { + other_decoder_inputs.encoder_attention_mask = model_inputs.attention_mask + } + + const decoderResults = await decoderForward(self, other_decoder_inputs, true); + + return decoderResults; +} + +/** + * Forward pass of an encoder model. + * @param {Object} self The encoder model. + * @param {Object} model_inputs The input data to be used for the forward pass. + * @returns {Promise} The model's outputs. + * @private + */ +async function encoderForward(self, model_inputs) { + const session = self.sessions['model']; + const encoderFeeds = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.pick)(model_inputs, session.inputNames); + + if (session.inputNames.includes('inputs_embeds') && !encoderFeeds.inputs_embeds) { + if (!model_inputs.input_ids) { + throw new Error('Both `input_ids` and `inputs_embeds` are missing in the model inputs.'); + } + encoderFeeds.inputs_embeds = await self.encode_text({ input_ids: model_inputs.input_ids }); + } + if (session.inputNames.includes('token_type_ids') && !encoderFeeds.token_type_ids) { + if (!encoderFeeds.input_ids) { + throw new Error('Both `input_ids` and `token_type_ids` are missing in the model inputs.'); + } + // Assign default `token_type_ids` (all zeroes) to the `encoderFeeds` if the model expects it, + // but they weren't created by the tokenizer. + encoderFeeds.token_type_ids = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.zeros_like)(encoderFeeds.input_ids); + } + if (session.inputNames.includes('pixel_mask') && !encoderFeeds.pixel_mask) { + if (!encoderFeeds.pixel_values) { + throw new Error('Both `pixel_values` and `pixel_mask` are missing in the model inputs.'); + } + // Assign default `pixel_mask` (all ones) to the `encoderFeeds` if the model expects it, + // but they weren't created by the processor. + const dims = encoderFeeds.pixel_values.dims; + encoderFeeds.pixel_mask = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.ones)([dims[0], dims[2], dims[3]]); + } + + return await sessionRun(session, encoderFeeds); +} + +async function autoEncoderForward(self, model_inputs) { + const encoded = await self.encode(model_inputs); + const decoded = await self.decode(encoded); + return decoded; +} + +/** + * Forward pass of a decoder model. + * @param {Object} self The decoder model. + * @param {Object} model_inputs The input data to be used for the forward pass. + * @returns {Promise} The logits and past key values. + * @private + */ +async function decoderForward(self, model_inputs, is_encoder_decoder = false) { + + const session = self.sessions[ + is_encoder_decoder ? 'decoder_model_merged' : 'model' + ] + + const { past_key_values, ...new_model_inputs } = model_inputs; + + if (session.inputNames.includes('use_cache_branch')) { + new_model_inputs.use_cache_branch = boolTensor(!!past_key_values); + } + if (session.inputNames.includes('position_ids') && new_model_inputs.attention_mask && !new_model_inputs.position_ids) { + // NOTE: Handle a special case for paligemma/gemma3 models, where positions are 1-indexed + const start_index = ['paligemma', 'gemma3_text', 'gemma3'].includes(self.config.model_type) ? 1 : 0; + new_model_inputs.position_ids = createPositionIds(new_model_inputs, past_key_values, start_index); + } + + // Unpack the `past_key_values` object into model inputs + self.addPastKeyValues(new_model_inputs, past_key_values); + + // Select only the inputs that are needed for the current session + const fixed = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.pick)(new_model_inputs, session.inputNames); + return await sessionRun(session, fixed); +} + + + +function default_merge_input_ids_with_features({ + modality_token_id, + inputs_embeds, + modality_features, + input_ids, + attention_mask, +}) { + const token_positions = input_ids.tolist().map(ids => + ids.reduce((acc, x, idx) => { + if (x == modality_token_id) acc.push(idx); + return acc; + }, []) + ); + const n_tokens = token_positions.reduce((acc, x) => acc + x.length, 0); + const n_features = modality_features.dims[0]; + if (n_tokens !== n_features) { + throw new Error(`Number of tokens and features do not match: tokens: ${n_tokens}, features ${n_features}`); + } + + // Equivalent to performing a masked_scatter + let img = 0; + for (let i = 0; i < token_positions.length; ++i) { + const tokens = token_positions[i]; + const embeds = inputs_embeds[i]; + for (let j = 0; j < tokens.length; ++j) { + embeds[tokens[j]].data.set(modality_features[img++].data) + } + } + return { inputs_embeds, attention_mask } +} + + +function default_merge_input_ids_with_image_features({ + image_token_id, + inputs_embeds, + image_features, + input_ids, + attention_mask, +}) { + return default_merge_input_ids_with_features({ + modality_token_id: image_token_id, + inputs_embeds, + modality_features: image_features, + input_ids, + attention_mask, + }) +} + +function default_merge_input_ids_with_audio_features({ + audio_token_id, + inputs_embeds, + audio_features, + input_ids, + attention_mask, +}) { + return default_merge_input_ids_with_features({ + modality_token_id: audio_token_id, + inputs_embeds, + modality_features: audio_features, + input_ids, + attention_mask, + }) +} + +/** + * Abstract forward pass function for image-text-to-text or audio-text-to-text models. + * @param {Object} self The model object. + * @param {Object} params Additional parameters. + * @param {Function} [params.encode_function] The function to encode the modality values. + * @param {Function} [params.merge_function] The function to merge the modality features with the input embeddings. + * @param {string} [params.modality_input_name] The modality input name. + * @param {string} [params.modality_output_name] The modality output name. + * @param {Tensor} [params.input_ids=null] + * @param {Tensor} [params.attention_mask=null] + * @param {Tensor} [params.position_ids=null] + * @param {Tensor} [params.inputs_embeds=null] + * @param {Tensor} [params.past_key_values=null] + * @param {Object} [params.generation_config=null] + * @param {Object} [params.logits_processor=null] + * @returns {Promise} The model's output tensor + * @private + */ +async function genericTextToTextForward(self, { + // Generic parameters: + encode_function, + merge_function, + modality_input_name, + modality_output_name, + + // Produced by the tokenizer/processor: + input_ids = null, + attention_mask = null, + + // Used during generation: + position_ids = null, + inputs_embeds = null, + past_key_values = null, + + // Generic generation parameters + generation_config = null, + logits_processor = null, + + // Additional parameters + ...kwargs +}) { + const modality_values = kwargs[modality_input_name]; + if (!inputs_embeds) { + // 1. Extract the text embeddings. + inputs_embeds = await self.encode_text({ input_ids, ...kwargs }); + + // 2. Possibly, merge text and modality values + if (modality_values && input_ids.dims[1] !== 1) { + const modality_features = await encode_function({ + // Pass the modality values under its expected key. + // The caller knows whether this is audio or image. + [modality_input_name]: modality_values, + ...kwargs + }); + ({ inputs_embeds, attention_mask } = merge_function({ + [modality_output_name]: modality_features, + inputs_embeds, + input_ids, + attention_mask, + })); + + } else if (past_key_values && modality_values && input_ids.dims[1] === 1) { + // This branch handles the cache case. + const target_length = input_ids.dims[1]; // always 1 + const past_length = Object.values(past_key_values)[0].dims.at(-2); + + attention_mask = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)([ + (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.ones)([input_ids.dims[0], past_length]), + attention_mask.slice(null, [attention_mask.dims[1] - target_length, attention_mask.dims[1]]), + ], 1); + } + } + + if (!position_ids) { + + if (self.config.model_type === 'qwen2_vl') { + // Special case for qwen2_vl models + // @ts-ignore + const { image_grid_thw, video_grid_thw } = kwargs; + [position_ids] = self.get_rope_index(input_ids, image_grid_thw, video_grid_thw, attention_mask) + } + } + + // 3. Call the decoder forward using the updated inputs. + const outputs = await decoderForward(self, { + inputs_embeds, + past_key_values, + attention_mask, + position_ids, + generation_config, + logits_processor, + }, true); + return outputs; +} + +/** + * Forward pass of an audio-text-to-text model. + * @param {Object} self The audio-text-to-text model. + * @param {Object} params The inputs for the audio-text-to-text forward pass. + * @returns {Promise} The model's output tensor. + * @private + */ +async function audioTextToTextForward(self, params) { + return await genericTextToTextForward(self, { + ...params, + modality_input_name: 'audio_values', + modality_output_name: 'audio_features', + encode_function: self.encode_audio.bind(self), + merge_function: self._merge_input_ids_with_audio_features.bind(self), + }); +} + +/** + * Forward pass of an image-text-to-text model. + * @param {Object} self The image-text-to-text model. + * @param {Object} params The inputs for the image-text-to-text forward pass. + * @returns {Promise} The model's output tensor. + * @private + */ +async function imageTextToTextForward(self, params) { + return await genericTextToTextForward(self, { + ...params, + modality_input_name: 'pixel_values', + modality_output_name: 'image_features', + encode_function: self.encode_image.bind(self), + merge_function: self._merge_input_ids_with_image_features.bind(self), + }); +} + +/** + * Helper function to perform the following: + * ```python + * x = attention_mask.long().cumsum(-1) - 1 + * x.masked_fill_(attention_mask == 0, 1) + * ``` + * @param {Tensor} attention_mask + * @returns {{data: BigInt64Array, dims: number[]}} + */ +function cumsum_masked_fill(attention_mask, start_index = 0) { + const [bz, seq_len] = attention_mask.dims; + const attn_mask_data = attention_mask.data; + + const data = new BigInt64Array(attn_mask_data.length); + for (let i = 0; i < bz; ++i) { + const start = i * seq_len; + let sum = BigInt(start_index); + for (let j = 0; j < seq_len; ++j) { + const index = start + j; + if (attn_mask_data[index] === 0n) { + data[index] = BigInt(1); + } else { // === 1n + data[index] = sum; + sum += attn_mask_data[index]; + } + } + } + return { data, dims: attention_mask.dims }; + +} + +/** + * If the model supports providing position_ids, we create position_ids on the fly for batch generation, + * by computing the cumulative sum of the attention mask along the sequence length dimension. + * + * Equivalent to: + * ```python + * position_ids = attention_mask.long().cumsum(-1) - 1 + * position_ids.masked_fill_(attention_mask == 0, 1) + * if past_key_values: + * position_ids = position_ids[:, -input_ids.shape[1] :] + * ``` + */ +function createPositionIds(model_inputs, past_key_values = null, start_index = 0) { + const { input_ids, inputs_embeds, attention_mask } = model_inputs; + + const { data, dims } = cumsum_masked_fill(attention_mask, start_index); + let position_ids = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor('int64', data, dims); + if (past_key_values) { + const offset = -(input_ids ?? inputs_embeds).dims.at(1); + position_ids = position_ids.slice(null, [offset, null]); + } + return position_ids; +} + +function decoder_prepare_inputs_for_generation(self, input_ids, model_inputs, generation_config) { + if (model_inputs.past_key_values) { + const past_length = Object.values(model_inputs.past_key_values)[0].dims.at(-2); + const { input_ids, attention_mask } = model_inputs; + + // Keep only the unprocessed tokens: + // 1 - If the length of the attention_mask exceeds the length of input_ids, then we are in a setting where + // some of the inputs are exclusively passed as part of the cache (e.g. when passing input_embeds as + // input) + if (attention_mask && attention_mask.dims[1] > input_ids.dims[1]) { + // NOTE: not needed since we only pass the generated tokens to the next forward pass + // const offset = -(attention_mask.dims[1] - past_length); + // model_inputs.input_ids = input_ids.slice(null, [offset, null]); + } + // 2 - If the past_length is smaller than input_ids', then input_ids holds all input tokens. + // We can discard input_ids based on the past_length. + else if (past_length < input_ids.dims[1]) { + // NOTE: Required for phi models. + // See https://github.com/huggingface/transformers/issues/30809#issuecomment-2111918479 for more information. + model_inputs.input_ids = input_ids.slice(null, [past_length, null]); + } + // 3 - Otherwise (past_length >= input_ids.shape[1]), let's assume input_ids only has unprocessed tokens. + else { + if ( + // NOTE: Only used by VLMs (!= so that null matches undefined) + self.config.image_token_index != null && + // Equivalent to `self.config.image_token_index in input_ids` (== so that int matches bigint) + input_ids.data.some(x => x == self.config.image_token_index) + ) { + // TODO: Support multiple image tokens + const num_image_tokens = self.config.num_image_tokens; + if (!num_image_tokens) { + throw new Error('`num_image_tokens` is missing in the model configuration.'); + } + + const num_new_tokens = input_ids.dims[1] - (past_length - num_image_tokens); + model_inputs.input_ids = input_ids.slice(null, [-num_new_tokens, null]); + + // TODO: The attention mask should be formed from the attention mask passed in model_inputs + model_inputs.attention_mask = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.ones)([1, past_length + num_new_tokens]); + } + } + } + + return model_inputs; +} + +function encoder_decoder_prepare_inputs_for_generation(self, input_ids, model_inputs, generation_config) { + if (model_inputs.past_key_values) { + input_ids = input_ids.map(x => [x.at(-1)]); + } + + return { + ...model_inputs, + decoder_input_ids: toI64Tensor(input_ids), + }; +} + +function multimodal_text_to_text_prepare_inputs_for_generation(self, ...args) { + if (self.config.is_encoder_decoder) { + return encoder_decoder_prepare_inputs_for_generation(self, ...args); + } else { + return decoder_prepare_inputs_for_generation(self, ...args); + } +} + +function multimodality_prepare_inputs_for_generation(self, input_ids, model_inputs, generation_config) { + const has_past_key_values = !!model_inputs.past_key_values; + + if (generation_config.guidance_scale !== null && generation_config.guidance_scale > 1) { + if (has_past_key_values) { + model_inputs.input_ids = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)([ + model_inputs.input_ids, + model_inputs.input_ids, + ], 0) + // NOTE: attention_mask handled in generation + } else { + model_inputs.input_ids = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)([ + model_inputs.input_ids, + (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.full_like)(model_inputs.input_ids, BigInt(generation_config.pad_token_id)), + ], 0); + model_inputs.attention_mask = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)([ + model_inputs.attention_mask, + (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.full_like)(model_inputs.attention_mask, 0n), + ], 0); + } + } + + if (has_past_key_values || !model_inputs.pixel_values) { + model_inputs.pixel_values = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.full)([0, 0, 3, 384, 384], 1.0); + } + + if (has_past_key_values) { + const num_img_tokens = 0; + const num_text_tokens = 1; + const has_image = num_img_tokens > 0 ? 1 : 0; + + const batch_size = 1; + model_inputs.images_seq_mask = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor( + 'bool', + new Array(num_img_tokens + num_text_tokens).fill(true).fill(false, 0, num_text_tokens), + [batch_size, num_img_tokens + num_text_tokens], + ); + model_inputs.images_emb_mask = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor( + 'bool', + new Array(num_img_tokens).fill(!!has_image), + [batch_size, 1, num_img_tokens], + ); + } + return model_inputs; +} + +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +/** + * A base class for pre-trained models that provides the model configuration and an ONNX session. + */ +class PreTrainedModel extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_3__.Callable { + main_input_name = 'input_ids'; + forward_params = ['input_ids', 'attention_mask']; + /** + * Creates a new instance of the `PreTrainedModel` class. + * @param {import('./configs.js').PretrainedConfig} config The model configuration. + * @param {Record} sessions The inference sessions for the model. + * @param {Record} configs Additional configuration files (e.g., generation_config.json). + */ + constructor(config, sessions, configs) { + super(); + + this.config = config; + this.sessions = sessions; + this.configs = configs; + + const modelName = MODEL_CLASS_TO_NAME_MAPPING.get(this.constructor); + const modelType = MODEL_TYPE_MAPPING.get(modelName); + + this.can_generate = false; + this._forward = null; + + this._prepare_inputs_for_generation = null; + switch (modelType) { + case MODEL_TYPES.DecoderOnly: + this.can_generate = true; + this._forward = decoderForward; + this._prepare_inputs_for_generation = decoder_prepare_inputs_for_generation; + break; + case MODEL_TYPES.Seq2Seq: + case MODEL_TYPES.Vision2Seq: + case MODEL_TYPES.Musicgen: + this.can_generate = true; + + this._forward = seq2seqForward; + this._prepare_inputs_for_generation = encoder_decoder_prepare_inputs_for_generation; + break; + + case MODEL_TYPES.EncoderDecoder: + this._forward = seq2seqForward; + break; + case MODEL_TYPES.ImageTextToText: + this.can_generate = true; + this._forward = imageTextToTextForward; + this._prepare_inputs_for_generation = multimodal_text_to_text_prepare_inputs_for_generation; + break; + case MODEL_TYPES.AudioTextToText: + this.can_generate = true; + this._forward = audioTextToTextForward; + this._prepare_inputs_for_generation = multimodal_text_to_text_prepare_inputs_for_generation; + break; + case MODEL_TYPES.Phi3V: + this.can_generate = true; + this._prepare_inputs_for_generation = multimodal_text_to_text_prepare_inputs_for_generation; + break; + case MODEL_TYPES.MultiModality: + this.can_generate = true; + this._prepare_inputs_for_generation = multimodality_prepare_inputs_for_generation; + break; + case MODEL_TYPES.AutoEncoder: + this._forward = autoEncoderForward; + break; + default: + // should be MODEL_TYPES.EncoderOnly + this._forward = encoderForward; + break; + } + + if (this.can_generate) { + this.forward_params.push('past_key_values'); + } + + /** @type {import('./configs.js').TransformersJSConfig} */ + this.custom_config = this.config['transformers.js_config'] ?? {}; + } + + /** + * Disposes of all the ONNX sessions that were created during inference. + * @returns {Promise} An array of promises, one for each ONNX session that is being disposed. + * @todo Use https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/FinalizationRegistry + */ + async dispose() { + const promises = []; + for (const session of Object.values(this.sessions)) { + if (session?.handler?.dispose) { + promises.push(session.handler.dispose()) + } + } + return await Promise.all(promises); + } + + /** + * Instantiate one of the model classes of the library from a pretrained model. + * + * The model class to instantiate is selected based on the `model_type` property of the config object + * (either passed as an argument or loaded from `pretrained_model_name_or_path` if possible) + * + * @param {string} pretrained_model_name_or_path The name or path of the pretrained model. Can be either: + * - A string, the *model id* of a pretrained model hosted inside a model repo on huggingface.co. + * Valid model ids can be located at the root-level, like `bert-base-uncased`, or namespaced under a + * user or organization name, like `dbmdz/bert-base-german-cased`. + * - A path to a *directory* containing model weights, e.g., `./my_model_directory/`. + * @param {import('./utils/hub.js').PretrainedModelOptions} options Additional options for loading the model. + * + * @returns {Promise} A new instance of the `PreTrainedModel` class. + */ + static async from_pretrained(pretrained_model_name_or_path, { + progress_callback = null, + config = null, + cache_dir = null, + local_files_only = false, + revision = 'main', + model_file_name = null, + subfolder = 'onnx', + device = null, + dtype = null, + use_external_data_format = null, + session_options = {}, + } = {}) { + + let options = { + progress_callback, + config, + cache_dir, + local_files_only, + revision, + model_file_name, + subfolder, + device, + dtype, + use_external_data_format, + session_options, + } + + const modelName = MODEL_CLASS_TO_NAME_MAPPING.get(this); + const modelType = MODEL_TYPE_MAPPING.get(modelName); + + config = options.config = await _configs_js__WEBPACK_IMPORTED_MODULE_0__.AutoConfig.from_pretrained(pretrained_model_name_or_path, options); + + let info; + if (modelType === MODEL_TYPES.DecoderOnly) { + info = await Promise.all([ + constructSessions(pretrained_model_name_or_path, { + model: options.model_file_name ?? 'model', + }, options), + getOptionalConfigs(pretrained_model_name_or_path, { + generation_config: 'generation_config.json', + }, options), + ]); + + } else if (modelType === MODEL_TYPES.Seq2Seq || modelType === MODEL_TYPES.Vision2Seq) { + info = await Promise.all([ + constructSessions(pretrained_model_name_or_path, { + model: 'encoder_model', + decoder_model_merged: 'decoder_model_merged', + }, options), + getOptionalConfigs(pretrained_model_name_or_path, { + generation_config: 'generation_config.json', + }, options), + ]); + + } else if (modelType === MODEL_TYPES.MaskGeneration) { + info = await Promise.all([ + constructSessions(pretrained_model_name_or_path, { + model: 'vision_encoder', + prompt_encoder_mask_decoder: 'prompt_encoder_mask_decoder', + }, options), + ]); + + } else if (modelType === MODEL_TYPES.EncoderDecoder) { + info = await Promise.all([ + constructSessions(pretrained_model_name_or_path, { + model: 'encoder_model', + decoder_model_merged: 'decoder_model_merged', + }, options), + ]); + + } else if (modelType === MODEL_TYPES.ImageTextToText) { + const sessions = { + embed_tokens: 'embed_tokens', + vision_encoder: 'vision_encoder', + decoder_model_merged: 'decoder_model_merged', + } + if (config.is_encoder_decoder) { + sessions['model'] = 'encoder_model'; + } + info = await Promise.all([ + constructSessions(pretrained_model_name_or_path, sessions, options), + getOptionalConfigs(pretrained_model_name_or_path, { + generation_config: 'generation_config.json', + }, options), + ]); + + } else if (modelType === MODEL_TYPES.AudioTextToText) { + const sessions = { + embed_tokens: 'embed_tokens', + audio_encoder: 'audio_encoder', + decoder_model_merged: 'decoder_model_merged', + } + info = await Promise.all([ + constructSessions(pretrained_model_name_or_path, sessions, options), + getOptionalConfigs(pretrained_model_name_or_path, { + generation_config: 'generation_config.json', + }, options), + ]); + + } else if (modelType === MODEL_TYPES.Musicgen) { + info = await Promise.all([ + constructSessions(pretrained_model_name_or_path, { + model: 'text_encoder', + decoder_model_merged: 'decoder_model_merged', + encodec_decode: 'encodec_decode', + }, options), + getOptionalConfigs(pretrained_model_name_or_path, { + generation_config: 'generation_config.json', + }, options), + ]); + + } else if (modelType === MODEL_TYPES.MultiModality) { + info = await Promise.all([ + constructSessions(pretrained_model_name_or_path, { + prepare_inputs_embeds: 'prepare_inputs_embeds', + model: 'language_model', + lm_head: 'lm_head', + gen_head: 'gen_head', + gen_img_embeds: 'gen_img_embeds', + image_decode: 'image_decode', + }, options), + getOptionalConfigs(pretrained_model_name_or_path, { + generation_config: 'generation_config.json', + }, options), + ]); + + } else if (modelType === MODEL_TYPES.Phi3V) { + info = await Promise.all([ + constructSessions(pretrained_model_name_or_path, { + prepare_inputs_embeds: 'prepare_inputs_embeds', + model: 'model', + vision_encoder: 'vision_encoder', + }, options), + getOptionalConfigs(pretrained_model_name_or_path, { + generation_config: 'generation_config.json', + }, options), + ]); + } else if (modelType === MODEL_TYPES.AutoEncoder) { + info = await Promise.all([ + constructSessions(pretrained_model_name_or_path, { + encoder_model: 'encoder_model', + decoder_model: 'decoder_model', + }, options), + ]); + } else { // should be MODEL_TYPES.EncoderOnly + if (modelType !== MODEL_TYPES.EncoderOnly) { + const type = modelName ?? config?.model_type; + if (type !== 'custom') { + console.warn(`Model type for '${type}' not found, assuming encoder-only architecture. Please report this at ${_utils_constants_js__WEBPACK_IMPORTED_MODULE_6__.GITHUB_ISSUE_URL}.`) + } + } + info = await Promise.all([ + constructSessions(pretrained_model_name_or_path, { + model: options.model_file_name ?? 'model', + }, options), + ]); + } + + // @ts-ignore + return new this(config, ...info); + } + + /** + * Runs the model with the provided inputs + * @param {Object} model_inputs Object containing input tensors + * @returns {Promise} Object containing output tensors + */ + async _call(model_inputs) { + return await this.forward(model_inputs); + } + + /** + * Forward method for a pretrained model. If not overridden by a subclass, the correct forward method + * will be chosen based on the model type. + * @param {Object} model_inputs The input data to the model in the format specified in the ONNX model. + * @returns {Promise} The output data from the model in the format specified in the ONNX model. + * @throws {Error} This method must be implemented in subclasses. + */ + async forward(model_inputs) { + return await this._forward(this, model_inputs); + } + + /** + * Get the model's generation config, if it exists. + * @returns {GenerationConfig|null} The model's generation config if it exists, otherwise `null`. + */ + get generation_config() { + return this.configs?.generation_config ?? null; + } + + /** + * This function returns a [`LogitsProcessorList`] list object that contains all relevant [`LogitsWarper`] + * instances used for multinomial sampling. + * @param {GenerationConfig} generation_config The generation config. + * @returns {LogitsProcessorList} generation_config + */ + _get_logits_warper(generation_config) { + + // instantiate warpers list + const warpers = new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.LogitsProcessorList(); + + if (generation_config.temperature !== null && generation_config.temperature !== 1.0) { + warpers.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.TemperatureLogitsWarper(generation_config.temperature)); + } + if (generation_config.top_k !== null && generation_config.top_k !== 0) { + // TODO: add min_tokens_to_keep + warpers.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.TopKLogitsWarper(generation_config.top_k)); + } + if (generation_config.top_p !== null && generation_config.top_p < 1.0) { + // TODO: add min_tokens_to_keep + warpers.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.TopPLogitsWarper(generation_config.top_p)); + } + + return warpers; + } + + /** + * @param {GenerationConfig} generation_config + * @param {number} input_ids_seq_length The starting sequence length for the input ids. + * @returns {LogitsProcessorList} + * @private + */ + _get_logits_processor( + generation_config, + input_ids_seq_length, + // encoder_input_ids, TODO + // prefix_allowed_tokens_fn, TODO + logits_processor = null + ) { + const processors = new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.LogitsProcessorList(); + + // if (generation_config.diversity_penalty !== null && generation_config.diversity_penalty > 0.0) { + // processors.push(new HammingDiversityLogitsProcessor( + // generation_config.diversity_penalty, + // generation_config.num_beams, + // generation_config.num_beam_groups + // )); + // } + + // if (generation_config.encoder_repetition_penalty !== null && generation_config.encoder_repetition_penalty !== 1.0) { + // processors.push(new EncoderRepetitionPenaltyLogitsProcessor( + // generation_config.encoder_repetition_penalty, + // encoder_input_ids + // )); + // } + + if (generation_config.repetition_penalty !== null && generation_config.repetition_penalty !== 1.0) { + processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.RepetitionPenaltyLogitsProcessor(generation_config.repetition_penalty)); + } + + if (generation_config.no_repeat_ngram_size !== null && generation_config.no_repeat_ngram_size > 0) { + processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.NoRepeatNGramLogitsProcessor(generation_config.no_repeat_ngram_size)); + } + + // if (generation_config.encoder_no_repeat_ngram_size !== null && generation_config.encoder_no_repeat_ngram_size > 0) { + // if (this.config.is_encoder_decoder) { + // processors.push(new EncoderNoRepeatNGramLogitsProcessor( + // generation_config.encoder_no_repeat_ngram_size, + // encoder_input_ids + // )); + // } else { + // throw new Error("It's impossible to use `encoder_no_repeat_ngram_size` with decoder-only architecture"); + // } + // } + + if (generation_config.bad_words_ids !== null) { + processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.NoBadWordsLogitsProcessor(generation_config.bad_words_ids, generation_config.eos_token_id)); + } + + if (generation_config.min_length !== null && generation_config.eos_token_id !== null && generation_config.min_length > 0) { + processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.MinLengthLogitsProcessor(generation_config.min_length, generation_config.eos_token_id)); + } + + if (generation_config.min_new_tokens !== null && generation_config.eos_token_id !== null && generation_config.min_new_tokens > 0) { + processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.MinNewTokensLengthLogitsProcessor( + input_ids_seq_length, + generation_config.min_new_tokens, + generation_config.eos_token_id + )); + } + + // if (prefix_allowed_tokens_fn !== null) { + // processors.push(new PrefixConstrainedLogitsProcessor( + // prefix_allowed_tokens_fn, + // generation_config.num_beams / generation_config.num_beam_groups + // )); + // } + + + if (generation_config.forced_bos_token_id !== null) { + processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.ForcedBOSTokenLogitsProcessor(generation_config.forced_bos_token_id)); + } + + if (generation_config.forced_eos_token_id !== null) { + processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.ForcedEOSTokenLogitsProcessor( + generation_config.max_length, + generation_config.forced_eos_token_id + )); + } + + // if (generation_config.remove_invalid_values === true) { + // processors.push(new InfNanRemoveLogitsProcessor()); + // } + + // if (generation_config.exponential_decay_length_penalty !== null) { + // processors.push(new ExponentialDecayLengthPenalty( + // generation_config.exponential_decay_length_penalty, + // generation_config.eos_token_id, + // input_ids_seq_length + // )); + // } + + // if (generation_config.suppress_tokens !== null) { + // processors.push(new SuppressTokensLogitsProcessor(generation_config.suppress_tokens)); + // } + + if (generation_config.begin_suppress_tokens !== null) { + const begin_index = (input_ids_seq_length > 1 || generation_config.forced_bos_token_id === null) + ? input_ids_seq_length + : input_ids_seq_length + 1; + + processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.SuppressTokensAtBeginLogitsProcessor(generation_config.begin_suppress_tokens, begin_index)); + } + + // DEPRECATED: https://github.com/huggingface/transformers/pull/29485 + // if (generation_config.forced_decoder_ids !== null) { + // processors.push(new ForceTokensLogitsProcessor(generation_config.forced_decoder_ids)); + // } + + + // 8. prepare batched CFG externally + if (generation_config.guidance_scale !== null && generation_config.guidance_scale > 1) { + processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.ClassifierFreeGuidanceLogitsProcessor(generation_config.guidance_scale)); + } + + if (logits_processor !== null) { + processors.extend(logits_processor) + } + + // `LogitNormalization` should always be the last logit processor, when present + // if (generation_config.renormalize_logits === true) { + // processors.push(new LogitNormalization()); + // } + + return processors; + } + + /** + * This function merges multiple generation configs together to form a final generation config to be used by the model for text generation. + * It first creates an empty `GenerationConfig` object, then it applies the model's own `generation_config` property to it. Finally, if a `generation_config` object was passed in the arguments, it overwrites the corresponding properties in the final config with those of the passed config object. + * @param {GenerationConfig|null} generation_config A `GenerationConfig` object containing generation parameters. + * @param {Object} kwargs Additional generation parameters to be used in place of those in the `generation_config` object. + * @returns {GenerationConfig} The final generation config object to be used by the model for text generation. + */ + _prepare_generation_config(generation_config, kwargs, cls = _generation_configuration_utils_js__WEBPACK_IMPORTED_MODULE_8__.GenerationConfig) { + // Create empty generation config (contains defaults) + // We pass `this.config` so that if `eos_token_id` or `bos_token_id` exist in the model's config, we will use them + const config = { ...this.config }; + for (const key of ["decoder", "generator", "text_config"]) { + // Special case: some models have generation attributes set in the decoder. + // Use them if still unset in the generation config. + if (key in config) { + Object.assign(config, config[key]); + } + } + + const gen_config = new cls(config); + + // Apply model's generation config, if it exists + Object.assign(gen_config, this.generation_config ?? {}); + + // Next, use any generation config specified by the user + // when calling `generate` + if (generation_config) { + Object.assign(gen_config, generation_config); + } + + // Finally, if any kwargs were passed, use them to overwrite + if (kwargs) { + Object.assign(gen_config, (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.pick)(kwargs, Object.getOwnPropertyNames(gen_config))); + } + + return gen_config; + } + + /** + * + * @param {GenerationConfig} generation_config + * @param {StoppingCriteriaList} [stopping_criteria=null] + */ + _get_stopping_criteria(generation_config, stopping_criteria = null) { + const criteria = new _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_12__.StoppingCriteriaList(); + + if (generation_config.max_length !== null) { + criteria.push(new _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_12__.MaxLengthCriteria( + generation_config.max_length, + this.config.max_position_embeddings ?? null, + )); + } + // if (generation_config.max_time !== null) { + // criteria.push(new MaxTimeCriteria(generation_config.max_time)); + // } + if (generation_config.eos_token_id !== null) { + criteria.push(new _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_12__.EosTokenCriteria(generation_config.eos_token_id)); + } + + if (stopping_criteria) { + criteria.extend(stopping_criteria); + } + return criteria; + + } + + /** + * Confirms that the model class is compatible with generation. + * If not, raises an exception that points to the right class to use. + */ + _validate_model_class() { + if (!this.can_generate) { + const generate_compatible_mappings = [ + MODEL_FOR_CAUSAL_LM_MAPPING_NAMES, + // MODEL_FOR_CAUSAL_IMAGE_MODELING_MAPPING, // TODO + MODEL_FOR_VISION_2_SEQ_MAPPING_NAMES, + MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES, + MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES, + ]; + + const modelName = MODEL_CLASS_TO_NAME_MAPPING.get(this.constructor); + + const generate_compatible_classes = new Set(); + const modelType = this.config.model_type; + for (const model_mapping of generate_compatible_mappings) { + const supported_models = model_mapping.get(modelType); + if (supported_models) { + generate_compatible_classes.add(supported_models[0]); + } + } + + let errorMessage = `The current model class (${modelName}) is not compatible with \`.generate()\`, as it doesn't have a language model head.` + if (generate_compatible_classes.size > 0) { + errorMessage += ` Please use the following class instead: ${[...generate_compatible_classes].join(', ')}`; + } + throw Error(errorMessage); + } + } + + prepare_inputs_for_generation(...args) { + return this._prepare_inputs_for_generation(this, ...args); + } + + /** + * + * @param {Object} inputs + * @param {bigint[][]} inputs.generated_input_ids + * @param {Object} inputs.outputs + * @param {Object} inputs.model_inputs + * @param {boolean} inputs.is_encoder_decoder + * @returns {Object} The updated model inputs for the next generation iteration. + */ + _update_model_kwargs_for_generation({ generated_input_ids, outputs, model_inputs, is_encoder_decoder }) { + // update past_key_values + model_inputs['past_key_values'] = this.getPastKeyValues(outputs, model_inputs.past_key_values); + + // update inputs for next run + model_inputs['input_ids'] = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor('int64', generated_input_ids.flat(), [generated_input_ids.length, 1]); + + if (!is_encoder_decoder) { + // update attention mask + model_inputs.attention_mask = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)( + [ + model_inputs.attention_mask, + (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.ones)([model_inputs.attention_mask.dims[0], 1]), + ], 1 + ); + } else if ('decoder_attention_mask' in model_inputs) { + // TODO: update decoder attention mask if the model requires it + } + + // force recreate position_ids in next iteration + model_inputs['position_ids'] = null; + + return model_inputs; + } + + /** + * This function extracts the model-specific `inputs` for generation. + * @param {Object} params + * @param {Tensor} [params.inputs=null] + * @param {number} [params.bos_token_id=null] + * @param {Record} [params.model_kwargs] + * @returns {{inputs_tensor: Tensor, model_inputs: Record, model_input_name: string}} The model-specific inputs for generation. + */ + _prepare_model_inputs({ inputs, bos_token_id, model_kwargs }) { + const model_inputs = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.pick)(model_kwargs, this.forward_params); + const input_name = this.main_input_name; + if (input_name in model_inputs) { + if (inputs) { + throw new Error( + "`inputs`: {inputs}` were passed alongside {input_name} which is not allowed. " + + "Make sure to either pass {inputs} or {input_name}=..." + ); + } + } else { + model_inputs[input_name] = inputs; + } + + const inputs_tensor = model_inputs[input_name]; + + return { inputs_tensor, model_inputs, model_input_name: input_name }; + } + + async _prepare_encoder_decoder_kwargs_for_generation({ inputs_tensor, model_inputs, model_input_name, generation_config }) { + if ( + this.sessions['model'].inputNames.includes('inputs_embeds') + && !model_inputs.inputs_embeds + && '_prepare_inputs_embeds' in this + ) { + // Encoder expects `inputs_embeds` instead of `input_ids` + const { input_ids, pixel_values, attention_mask, ...kwargs } = model_inputs; + // @ts-ignore + const prepared_inputs = await this._prepare_inputs_embeds(model_inputs); + model_inputs = { + ...kwargs, + ...(0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.pick)(prepared_inputs, ['inputs_embeds', 'attention_mask']), + }; + } + let { last_hidden_state } = await encoderForward(this, model_inputs); + + // for classifier free guidance we need to add a 'null' input to our encoder hidden states + if (generation_config.guidance_scale !== null && generation_config.guidance_scale > 1) { + + last_hidden_state = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)([ + last_hidden_state, + (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.full_like)(last_hidden_state, 0.0), + ], 0); + + if ('attention_mask' in model_inputs) { + model_inputs['attention_mask'] = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)([ + model_inputs['attention_mask'], + (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.zeros_like)(model_inputs['attention_mask']), + ], 0); + } + + } else if (model_inputs.decoder_input_ids) { + // Ensure that the encoder outputs have the same batch size as the decoder inputs, + // allowing for more efficient batched generation for single inputs + const decoder_input_ids_batch_size = toI64Tensor(model_inputs.decoder_input_ids).dims[0]; + if (decoder_input_ids_batch_size !== last_hidden_state.dims[0]) { + if (last_hidden_state.dims[0] !== 1) { + throw new Error( + `The encoder outputs have a different batch size (${last_hidden_state.dims[0]}) than the decoder inputs (${decoder_input_ids_batch_size}).` + ) + } + last_hidden_state = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)(Array.from({ length: decoder_input_ids_batch_size }, () => last_hidden_state), 0); + } + } + model_inputs['encoder_outputs'] = last_hidden_state; + + return model_inputs; + } + + /** + * Prepares `decoder_input_ids` for generation with encoder-decoder models + * @param {*} param0 + */ + _prepare_decoder_input_ids_for_generation({ batch_size, model_input_name, model_kwargs, decoder_start_token_id, bos_token_id, generation_config }) { + let { decoder_input_ids, ...model_inputs } = model_kwargs; + + // Prepare input ids if the user has not defined `decoder_input_ids` manually. + if (!(decoder_input_ids instanceof _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor)) { + if (!decoder_input_ids) { + decoder_start_token_id ??= bos_token_id; + + if (this.config.model_type === 'musicgen') { + // Custom logic (TODO: move to Musicgen class) + decoder_input_ids = Array.from({ + // @ts-expect-error TS2339 + length: batch_size * this.config.decoder.num_codebooks + }, () => [decoder_start_token_id]); + + } else if (Array.isArray(decoder_start_token_id)) { + if (decoder_start_token_id.length !== batch_size) { + throw new Error( + `\`decoder_start_token_id\` expcted to have length ${batch_size} but got ${decoder_start_token_id.length}` + ) + } + decoder_input_ids = decoder_start_token_id; + } else { + decoder_input_ids = Array.from({ + length: batch_size, + }, () => [decoder_start_token_id]); + } + } else if (!Array.isArray(decoder_input_ids[0])) { + // Correct batch size + decoder_input_ids = Array.from({ + length: batch_size, + }, () => decoder_input_ids); + } + decoder_input_ids = toI64Tensor(decoder_input_ids); + } + + model_kwargs['decoder_attention_mask'] = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.ones_like)(decoder_input_ids); + + return { input_ids: decoder_input_ids, model_inputs }; + } + + /** + * Generates sequences of token ids for models with a language modeling head. + * @param {import('./generation/parameters.js').GenerationFunctionParameters} options + * @returns {Promise} The output of the model, which can contain the generated token ids, attentions, and scores. + */ + async generate({ + inputs = null, + generation_config = null, + logits_processor = null, + stopping_criteria = null, + streamer = null, + + // inputs_attention_mask = null, + ...kwargs + }) { + this._validate_model_class(); + + // Update generation config with defaults and kwargs + generation_config = this._prepare_generation_config(generation_config, kwargs); + + // 3. Define model inputs + let { inputs_tensor, model_inputs, model_input_name } = this._prepare_model_inputs({ + inputs, + model_kwargs: kwargs, + }); + + const is_encoder_decoder = this.config.is_encoder_decoder; + + // 4. Define other model kwargs + if (!is_encoder_decoder) { + // decoder-only models should use left-padding for generation + } else if (!('encoder_outputs' in model_inputs)) { + // if model is encoder decoder encoder_outputs are created + // and added to `model_kwargs` + model_inputs = await this._prepare_encoder_decoder_kwargs_for_generation( + { inputs_tensor, model_inputs, model_input_name, generation_config } + ) + } + + // 5. Prepare `input_ids` which will be used for auto-regressive generation + // TODO: Update to align with HF transformers' implementation + let input_ids; + if (is_encoder_decoder) { + // Generating from the encoder outputs + ({ input_ids, model_inputs } = this._prepare_decoder_input_ids_for_generation({ + batch_size: model_inputs[model_input_name].dims.at(0), + model_input_name, + model_kwargs: model_inputs, + decoder_start_token_id: generation_config.decoder_start_token_id, + bos_token_id: generation_config.bos_token_id, + generation_config, + })); + } else { + input_ids = model_inputs[model_input_name] + } + + // 6. Prepare `max_length` depending on other stopping criteria. + let input_ids_length = input_ids.dims.at(-1); + + if (generation_config.max_new_tokens !== null) { + generation_config.max_length = input_ids_length + generation_config.max_new_tokens; + } + + // input_ids_length = model_inputs[model_input_name].dims.at(1); + // // inputs instanceof Tensor ? : inputs.length; + + // // decoder-only + // if (input_ids_length === 0) { + // throw Error("Must supply a non-empty array of input token ids.") + // } + + // let decoder_input_ids = + // generation_config.decoder_input_ids + // ?? generation_config.decoder_start_token_id + // ?? generation_config.bos_token_id + // ?? generation_config.eos_token_id; + + // Update logits processor + // 8. prepare distribution pre_processing samplers + const prepared_logits_processor = this._get_logits_processor( + generation_config, + input_ids_length, + logits_processor, + ) + + // 9. prepare stopping criteria + const prepared_stopping_criteria = this._get_stopping_criteria( + generation_config, stopping_criteria + ) + + // /** @type {number[]} */ + // let eos_token_ids = generation_config.eos_token_id; + // if (eos_token_ids !== null && !Array.isArray(eos_token_ids)) { + // eos_token_ids = [eos_token_ids]; + // } + + const numInputs = model_inputs[model_input_name].dims.at(0); + + // TODO: + // done is a list of booleans to keep track of which inputs are done + // const done = new Array(numInputs).fill(false); + // For efficiency purposes, we remove completed rows from model_inputs + // when the beam is complete, and we keep track of the row index + // const rowIndexToBatchIndex = new Map(); + + const sampler = _generation_logits_sampler_js__WEBPACK_IMPORTED_MODULE_13__.LogitsSampler.getSampler(generation_config); + + // TODO make > numInputs + const scores = new Array(numInputs).fill(0); + /** @type {bigint[][]} */ + const all_input_ids = input_ids.tolist(); + if (streamer) { + streamer.put(all_input_ids); + } + // const all_generated_input_ids = Array.from({ length: numInputs }, () => []); + + // NOTE: For now, we don't support spawning new beams + // TODO: when we do, we simply copy past key values and accumulate into single large tensor + + //////////////////////////////////////////////////// + // Generic search which handles 4 generation modes: + // - GenerationMode.GREEDY_SEARCH + // - GenerationMode.SAMPLE + // - GenerationMode.BEAM_SEARCH + // - GenerationMode.BEAM_SAMPLE + //////////////////////////////////////////////////// + let outputs; + let attentions = {}; + while (true) { + // prepare model inputs + model_inputs = this.prepare_inputs_for_generation(all_input_ids, model_inputs, generation_config); + outputs = await this.forward(model_inputs); + + if (generation_config.output_attentions && generation_config.return_dict_in_generate) { + // Get attentions if they are present + const token_attentions = this.getAttentions(outputs); + for (const key in token_attentions) { + if (!(key in attentions)) { + attentions[key] = []; + } + attentions[key].push(token_attentions[key]); + } + } + + // Logits are of the form [batch_size, out_seq_length, vocab_size] + // In most cases, this will be [batch_size, 1, vocab_size] + // So, we select the last token's logits: + // (equivalent to `logits = outputs.logits[:, -1, :]`) + const logits = outputs.logits.slice(null, -1, null); + + const next_tokens_scores = prepared_logits_processor(all_input_ids, logits); + + /** @type {[bigint][]} */ + const generated_input_ids = []; + // const new_kv_cache = [];// NOTE: Only used for beam search when concatenating new kv + // Loop over each batch + for (let batch_idx = 0; batch_idx < next_tokens_scores.dims.at(0); ++batch_idx) { + const logs = next_tokens_scores[batch_idx]; + + const sampledTokens = await sampler(logs); + for (const [newTokenId, logProb] of sampledTokens) { + const bigint = BigInt(newTokenId); + // TODO: If branching, use previous beam as a starting point + // update generated ids, model inputs, and length for next step + scores[batch_idx] += logProb; + all_input_ids[batch_idx].push(bigint); + generated_input_ids.push([bigint]); + + // TODO: Support beam search + break; + } + } + if (streamer) { + streamer.put(generated_input_ids); + } + + const stop = prepared_stopping_criteria(all_input_ids); + if (stop.every(x => x)) { + break; + } + + model_inputs = this._update_model_kwargs_for_generation({ + generated_input_ids, outputs, model_inputs, is_encoder_decoder, + }); + } + + if (streamer) { + streamer.end(); + } + + // Retrieve and dispose all final past key values (including encoder attentions) + const past_key_values = this.getPastKeyValues(outputs, model_inputs.past_key_values, true); + + // TODO: ensure all_input_ids is padded correctly... + const sequences = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor('int64', all_input_ids.flat(), [all_input_ids.length, all_input_ids[0].length]); + + if (generation_config.return_dict_in_generate) { + return { + sequences, + past_key_values, + ...attentions, + // TODO: + // scores, + // logits, + } + } else { + // Dispose all remaining tensors + for (const tensor of Object.values(outputs)) { + if (tensor.location === 'gpu-buffer') { + tensor.dispose(); + } + } + return sequences; + } + } + + /** + * Returns an object containing past key values from the given decoder results object. + * + * @param {Object} decoderResults The decoder results object. + * @param {Object} pastKeyValues The previous past key values. + * @returns {Object} An object containing past key values. + */ + getPastKeyValues(decoderResults, pastKeyValues, disposeEncoderPKVs = false) { + const pkvs = Object.create(null); + + for (const name in decoderResults) { + if (name.startsWith('present')) { + const newName = name.replace('present', 'past_key_values'); + const is_encoder_pkv = name.includes('encoder'); + if (is_encoder_pkv && pastKeyValues) { + // Optimization introduced by optimum to reuse past key values. + // So, we just replace the constant outputs (`decoderResults[name]`) with the previous past key values. + // https://github.com/huggingface/optimum/blob/0bf2c05fb7e1182b52d21b703cfc95fd9e4ea3dc/optimum/onnxruntime/base.py#L677-L704 + pkvs[newName] = pastKeyValues[newName]; + } else { // decoder or using first encoder PKVs + pkvs[newName] = decoderResults[name]; + } + + if (pastKeyValues && (!is_encoder_pkv || disposeEncoderPKVs)) { + // - Always dispose decoder PKVs + // - Only dispose encoder past key values when requested (after generation) + const t = pastKeyValues[newName]; + if (t.location === 'gpu-buffer') { + t.dispose(); + } + } + } + } + return pkvs; + } + + /** + * Returns an object containing attentions from the given model output object. + * + * @param {Object} model_output The output of the model. + * @returns {{cross_attentions?: Tensor[]}} An object containing attentions. + */ + getAttentions(model_output) { + const attentions = {}; + + for (const attnName of ['cross_attentions', 'encoder_attentions', 'decoder_attentions']) { + for (const name in model_output) { + if (name.startsWith(attnName)) { + if (!(attnName in attentions)) { + attentions[attnName] = []; + } + attentions[attnName].push(model_output[name]); + } + } + } + return attentions; + } + + /** + * Adds past key values to the decoder feeds object. If pastKeyValues is null, creates new tensors for past key values. + * + * @param {Object} decoderFeeds The decoder feeds object to add past key values to. + * @param {Object} pastKeyValues An object containing past key values. + */ + addPastKeyValues(decoderFeeds, pastKeyValues) { + if (pastKeyValues) { + Object.assign(decoderFeeds, pastKeyValues) + } else { + const session = this.sessions['decoder_model_merged'] ?? this.sessions['model']; + const dtype = session?.config?.kv_cache_dtype ?? 'float32'; + const empty = (dtype === 'float16') ? new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.DataTypeMap.float16() : []; + + const batch_size = (decoderFeeds[this.main_input_name] ?? decoderFeeds.attention_mask)?.dims?.[0] ?? 1; + const shapes = (0,_configs_js__WEBPACK_IMPORTED_MODULE_0__.getKeyValueShapes)(this.config, { batch_size }); + + for (const name in shapes) { + decoderFeeds[name] = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor(dtype, empty, shapes[name]); + } + } + } + + async encode_image({ pixel_values }) { + // image_inputs === { pixel_values } + const features = (await sessionRun(this.sessions['vision_encoder'], { pixel_values })).image_features; + // @ts-expect-error TS2339 + if (!this.config.num_image_tokens) { + console.warn( + 'The number of image tokens was not set in the model configuration. ' + + `Setting it to the number of features detected by the vision encoder (${features.dims[1]}).` + ) + // @ts-expect-error TS2339 + this.config.num_image_tokens = features.dims[1]; + } + return features; + } + + async encode_text({ input_ids }) { + // text_inputs === { input_ids, attention_mask } + return (await sessionRun(this.sessions['embed_tokens'], { input_ids })).inputs_embeds; + } + + async encode_audio({ audio_values }) { + // audio_inputs === { audio_values } + return (await sessionRun(this.sessions['audio_encoder'], { audio_values })).audio_features; + } +} + +////////////////////////////////////////////////// +// Base model output class +class ModelOutput { } + +/** + * Base class for model's outputs, with potential hidden states and attentions. + */ +class BaseModelOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.last_hidden_state Sequence of hidden-states at the output of the last layer of the model. + * @param {Tensor} [output.hidden_states] Hidden-states of the model at the output of each layer plus the optional initial embedding outputs. + * @param {Tensor} [output.attentions] Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads. + */ + constructor({ last_hidden_state, hidden_states = null, attentions = null }) { + super(); + this.last_hidden_state = last_hidden_state; + this.hidden_states = hidden_states; + this.attentions = attentions; + } +} +////////////////////////////////////////////////// +// Bert models +class BertPreTrainedModel extends PreTrainedModel { } +class BertModel extends BertPreTrainedModel { } + +/** + * BertForMaskedLM is a class representing a BERT model for masked language modeling. + */ +class BertForMaskedLM extends BertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for masked language modeling. + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} + +/** + * BertForSequenceClassification is a class representing a BERT model for sequence classification. + */ +class BertForSequenceClassification extends BertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * BertForTokenClassification is a class representing a BERT model for token classification. + */ +class BertForTokenClassification extends BertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * BertForQuestionAnswering is a class representing a BERT model for question answering. + */ +class BertForQuestionAnswering extends BertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for question answering. + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// ModernBert models +class ModernBertPreTrainedModel extends PreTrainedModel { } +class ModernBertModel extends ModernBertPreTrainedModel { } + +class ModernBertForMaskedLM extends ModernBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for masked language modeling. + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} + +class ModernBertForSequenceClassification extends ModernBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +class ModernBertForTokenClassification extends ModernBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// NomicBert models +class NomicBertPreTrainedModel extends PreTrainedModel { } +class NomicBertModel extends NomicBertPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// RoFormer models +class RoFormerPreTrainedModel extends PreTrainedModel { } + +/** + * The bare RoFormer Model transformer outputting raw hidden-states without any specific head on top. + */ +class RoFormerModel extends RoFormerPreTrainedModel { } + +/** + * RoFormer Model with a `language modeling` head on top. + */ +class RoFormerForMaskedLM extends RoFormerPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for masked language modeling. + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} + +/** + * RoFormer Model transformer with a sequence classification/regression head on top (a linear layer on top of the pooled output) + */ +class RoFormerForSequenceClassification extends RoFormerPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * RoFormer Model with a token classification head on top (a linear layer on top of the hidden-states output) + * e.g. for Named-Entity-Recognition (NER) tasks. + */ +class RoFormerForTokenClassification extends RoFormerPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * RoFormer Model with a span classification head on top for extractive question-answering tasks like SQuAD + * (a linear layers on top of the hidden-states output to compute `span start logits` and `span end logits`). + */ +class RoFormerForQuestionAnswering extends RoFormerPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for question answering. + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +// TODO: Add RoFormerForCausalLM and RoFormerForMultipleChoice +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// ConvBert models +class ConvBertPreTrainedModel extends PreTrainedModel { } + +/** + * The bare ConvBERT Model transformer outputting raw hidden-states without any specific head on top. + */ +class ConvBertModel extends ConvBertPreTrainedModel { } + +/** + * ConvBERT Model with a language modeling head on top. + */ +class ConvBertForMaskedLM extends ConvBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for masked language modeling. + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} + +/** + * ConvBERT Model transformer with a sequence classification/regression head on top (a linear layer on top of the pooled output) + */ +class ConvBertForSequenceClassification extends ConvBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * ConvBERT Model with a token classification head on top (a linear layer on top of the hidden-states output) + * e.g. for Named-Entity-Recognition (NER) tasks. + */ +class ConvBertForTokenClassification extends ConvBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * ConvBERT Model with a span classification head on top for extractive question-answering tasks like SQuAD + * (a linear layers on top of the hidden-states output to compute `span start logits` and `span end logits`) + */ +class ConvBertForQuestionAnswering extends ConvBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for question answering. + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Electra models +class ElectraPreTrainedModel extends PreTrainedModel { } + +/** + * The bare Electra Model transformer outputting raw hidden-states without any specific head on top. + * Identical to the BERT model except that it uses an additional linear layer between the embedding + * layer and the encoder if the hidden size and embedding size are different. + */ +class ElectraModel extends ElectraPreTrainedModel { } +// TODO add ElectraForPreTraining +/** + * Electra model with a language modeling head on top. + */ +class ElectraForMaskedLM extends ElectraPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for masked language modeling. + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} + +/** + * ELECTRA Model transformer with a sequence classification/regression head on top (a linear layer on top of the pooled output) + */ +class ElectraForSequenceClassification extends ElectraPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * Electra model with a token classification head on top. + */ +class ElectraForTokenClassification extends ElectraPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * LECTRA Model with a span classification head on top for extractive question-answering tasks like SQuAD + * (a linear layers on top of the hidden-states output to compute `span start logits` and `span end logits`). + */ +class ElectraForQuestionAnswering extends ElectraPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for question answering. + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// CamemBERT models +class CamembertPreTrainedModel extends PreTrainedModel { } + +/** + * The bare CamemBERT Model transformer outputting raw hidden-states without any specific head on top. + */ +class CamembertModel extends CamembertPreTrainedModel { } + +/** + * CamemBERT Model with a `language modeling` head on top. + */ +class CamembertForMaskedLM extends CamembertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for masked language modeling. + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} + +/** + * CamemBERT Model transformer with a sequence classification/regression head on top (a linear layer on top of the pooled output) e.g. for GLUE tasks. + */ +class CamembertForSequenceClassification extends CamembertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * CamemBERT Model with a token classification head on top (a linear layer on top of the hidden-states output) e.g. for Named-Entity-Recognition (NER) tasks. + */ +class CamembertForTokenClassification extends CamembertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * CamemBERT Model with a span classification head on top for extractive question-answering tasks + */ +class CamembertForQuestionAnswering extends CamembertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for question answering. + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// DeBERTa models +class DebertaPreTrainedModel extends PreTrainedModel { } + +/** + * The bare DeBERTa Model transformer outputting raw hidden-states without any specific head on top. + */ +class DebertaModel extends DebertaPreTrainedModel { } + +/** + * DeBERTa Model with a `language modeling` head on top. + */ +class DebertaForMaskedLM extends DebertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for masked language modeling. + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} + +/** + * DeBERTa Model transformer with a sequence classification/regression head on top (a linear layer on top of the pooled output) + */ +class DebertaForSequenceClassification extends DebertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * DeBERTa Model with a token classification head on top (a linear layer on top of the hidden-states output) e.g. for Named-Entity-Recognition (NER) tasks. + */ +class DebertaForTokenClassification extends DebertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * DeBERTa Model with a span classification head on top for extractive question-answering tasks like SQuAD (a linear + * layers on top of the hidden-states output to compute `span start logits` and `span end logits`). + */ +class DebertaForQuestionAnswering extends DebertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for question answering. + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// DeBERTa-v2 models +class DebertaV2PreTrainedModel extends PreTrainedModel { } + +/** + * The bare DeBERTa-V2 Model transformer outputting raw hidden-states without any specific head on top. + */ +class DebertaV2Model extends DebertaV2PreTrainedModel { } + +/** + * DeBERTa-V2 Model with a `language modeling` head on top. + */ +class DebertaV2ForMaskedLM extends DebertaV2PreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for masked language modeling. + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} + +/** + * DeBERTa-V2 Model transformer with a sequence classification/regression head on top (a linear layer on top of the pooled output) + */ +class DebertaV2ForSequenceClassification extends DebertaV2PreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * DeBERTa-V2 Model with a token classification head on top (a linear layer on top of the hidden-states output) e.g. for Named-Entity-Recognition (NER) tasks. + */ +class DebertaV2ForTokenClassification extends DebertaV2PreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * DeBERTa-V2 Model with a span classification head on top for extractive question-answering tasks like SQuAD (a linear + * layers on top of the hidden-states output to compute `span start logits` and `span end logits`). + */ +class DebertaV2ForQuestionAnswering extends DebertaV2PreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for question answering. + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// DistilBert models +class DistilBertPreTrainedModel extends PreTrainedModel { } +class DistilBertModel extends DistilBertPreTrainedModel { } + +/** + * DistilBertForSequenceClassification is a class representing a DistilBERT model for sequence classification. + */ +class DistilBertForSequenceClassification extends DistilBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * DistilBertForTokenClassification is a class representing a DistilBERT model for token classification. + */ +class DistilBertForTokenClassification extends DistilBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} + + +/** + * DistilBertForQuestionAnswering is a class representing a DistilBERT model for question answering. + */ +class DistilBertForQuestionAnswering extends DistilBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for question answering. + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} + +/** + * DistilBertForMaskedLM is a class representing a DistilBERT model for masking task. + */ +class DistilBertForMaskedLM extends DistilBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// ESM models +class EsmPreTrainedModel extends PreTrainedModel { } + +/** + * The bare ESM Model transformer outputting raw hidden-states without any specific head on top. + */ +class EsmModel extends EsmPreTrainedModel { } + +/** + * ESM Model with a `language modeling` head on top. + */ +class EsmForMaskedLM extends EsmPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for masked language modeling. + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} + +/** + * ESM Model transformer with a sequence classification/regression head on top (a linear layer on top of the pooled output) + */ +class EsmForSequenceClassification extends EsmPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * ESM Model with a token classification head on top (a linear layer on top of the hidden-states output) + * e.g. for Named-Entity-Recognition (NER) tasks. + */ +class EsmForTokenClassification extends EsmPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// MobileBert models +class MobileBertPreTrainedModel extends PreTrainedModel { } +class MobileBertModel extends MobileBertPreTrainedModel { } + +/** + * MobileBertForMaskedLM is a class representing a MobileBERT model for masking task. + */ +class MobileBertForMaskedLM extends MobileBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} + +/** + * MobileBert Model transformer with a sequence classification/regression head on top (a linear layer on top of the pooled output) + */ +class MobileBertForSequenceClassification extends MobileBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * MobileBert Model with a span classification head on top for extractive question-answering tasks + */ +class MobileBertForQuestionAnswering extends MobileBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// MPNet models +class MPNetPreTrainedModel extends PreTrainedModel { } + +/** + * The bare MPNet Model transformer outputting raw hidden-states without any specific head on top. + */ +class MPNetModel extends MPNetPreTrainedModel { } + +/** + * MPNetForMaskedLM is a class representing a MPNet model for masked language modeling. + */ +class MPNetForMaskedLM extends MPNetPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for masked language modeling. + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} + +/** + * MPNetForSequenceClassification is a class representing a MPNet model for sequence classification. + */ +class MPNetForSequenceClassification extends MPNetPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * MPNetForTokenClassification is a class representing a MPNet model for token classification. + */ +class MPNetForTokenClassification extends MPNetPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * MPNetForQuestionAnswering is a class representing a MPNet model for question answering. + */ +class MPNetForQuestionAnswering extends MPNetPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for question answering. + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// SqueezeBert models +class SqueezeBertPreTrainedModel extends PreTrainedModel { } +class SqueezeBertModel extends SqueezeBertPreTrainedModel { } +class SqueezeBertForMaskedLM extends SqueezeBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} +class SqueezeBertForSequenceClassification extends SqueezeBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +class SqueezeBertForQuestionAnswering extends SqueezeBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Albert models +class AlbertPreTrainedModel extends PreTrainedModel { } +class AlbertModel extends AlbertPreTrainedModel { } +class AlbertForSequenceClassification extends AlbertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +class AlbertForQuestionAnswering extends AlbertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +class AlbertForMaskedLM extends AlbertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// T5 models +class T5PreTrainedModel extends PreTrainedModel { + forward_params = [ + 'input_ids', + 'attention_mask', + 'encoder_outputs', + 'decoder_input_ids', + 'decoder_attention_mask', + 'past_key_values', + ]; +}; + +class T5Model extends T5PreTrainedModel { } + +/** + * T5Model is a class representing a T5 model for conditional generation. + */ +class T5ForConditionalGeneration extends T5PreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// LONGT5 models +/** + * An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained models. + */ +class LongT5PreTrainedModel extends PreTrainedModel { }; + +/** + * The bare LONGT5 Model transformer outputting raw hidden-states without any specific head on top. + */ +class LongT5Model extends LongT5PreTrainedModel { } + +/** + * LONGT5 Model with a `language modeling` head on top. + */ +class LongT5ForConditionalGeneration extends LongT5PreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// MT5 models +class MT5PreTrainedModel extends PreTrainedModel { }; + +class MT5Model extends MT5PreTrainedModel { } + +/** + * A class representing a conditional sequence-to-sequence model based on the MT5 architecture. + */ +class MT5ForConditionalGeneration extends MT5PreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Bart models +class BartPretrainedModel extends PreTrainedModel { }; + +/** + * The bare BART Model outputting raw hidden-states without any specific head on top. + */ +class BartModel extends BartPretrainedModel { } + +/** + * The BART Model with a language modeling head. Can be used for summarization. + */ +class BartForConditionalGeneration extends BartPretrainedModel { } + +/** + * Bart model with a sequence classification/head on top (a linear layer on top of the pooled output) + */ +class BartForSequenceClassification extends BartPretrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// MBart models +class MBartPreTrainedModel extends PreTrainedModel { }; + +/** + * The bare MBART Model outputting raw hidden-states without any specific head on top. + */ +class MBartModel extends MBartPreTrainedModel { } + +/** + * The MBART Model with a language modeling head. Can be used for summarization, after fine-tuning the pretrained models. + */ +class MBartForConditionalGeneration extends MBartPreTrainedModel { } + +/** + * MBart model with a sequence classification/head on top (a linear layer on top of the pooled output). + */ +class MBartForSequenceClassification extends MBartPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + + +class MBartForCausalLM extends MBartPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Blenderbot models +class BlenderbotPreTrainedModel extends PreTrainedModel { }; + +/** + * The bare Blenderbot Model outputting raw hidden-states without any specific head on top. + */ +class BlenderbotModel extends BlenderbotPreTrainedModel { } + +/** + * The Blenderbot Model with a language modeling head. Can be used for summarization. + */ +class BlenderbotForConditionalGeneration extends BlenderbotPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Blenderbot models +class BlenderbotSmallPreTrainedModel extends PreTrainedModel { }; + +/** + * The bare BlenderbotSmall Model outputting raw hidden-states without any specific head on top. + */ +class BlenderbotSmallModel extends BlenderbotSmallPreTrainedModel { } + +/** + * The BlenderbotSmall Model with a language modeling head. Can be used for summarization. + */ +class BlenderbotSmallForConditionalGeneration extends BlenderbotSmallPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Roberta models +class RobertaPreTrainedModel extends PreTrainedModel { } +class RobertaModel extends RobertaPreTrainedModel { } + +/** + * RobertaForMaskedLM class for performing masked language modeling on Roberta models. + */ +class RobertaForMaskedLM extends RobertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} + +/** + * RobertaForSequenceClassification class for performing sequence classification on Roberta models. + */ +class RobertaForSequenceClassification extends RobertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * RobertaForTokenClassification class for performing token classification on Roberta models. + */ +class RobertaForTokenClassification extends RobertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * RobertaForQuestionAnswering class for performing question answering on Roberta models. + */ +class RobertaForQuestionAnswering extends RobertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// XLM models +/** + * An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained models. + */ +class XLMPreTrainedModel extends PreTrainedModel { } + +/** + * The bare XLM Model transformer outputting raw hidden-states without any specific head on top. + */ +class XLMModel extends XLMPreTrainedModel { } + +/** + * The XLM Model transformer with a language modeling head on top (linear layer with weights tied to the input embeddings). + */ +class XLMWithLMHeadModel extends XLMPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} + +/** + * XLM Model with a sequence classification/regression head on top (a linear layer on top of the pooled output) + */ +class XLMForSequenceClassification extends XLMPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * XLM Model with a token classification head on top (a linear layer on top of the hidden-states output) + */ +class XLMForTokenClassification extends XLMPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * XLM Model with a span classification head on top for extractive question-answering tasks + */ +class XLMForQuestionAnswering extends XLMPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// XLMRoberta models +class XLMRobertaPreTrainedModel extends PreTrainedModel { } +class XLMRobertaModel extends XLMRobertaPreTrainedModel { } + +/** + * XLMRobertaForMaskedLM class for performing masked language modeling on XLMRoberta models. + */ +class XLMRobertaForMaskedLM extends XLMRobertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} + +/** + * XLMRobertaForSequenceClassification class for performing sequence classification on XLMRoberta models. + */ +class XLMRobertaForSequenceClassification extends XLMRobertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * XLMRobertaForTokenClassification class for performing token classification on XLMRoberta models. + */ +class XLMRobertaForTokenClassification extends XLMRobertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * XLMRobertaForQuestionAnswering class for performing question answering on XLMRoberta models. + */ +class XLMRobertaForQuestionAnswering extends XLMRobertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Audio Spectrogram Transformer (AST) models +class ASTPreTrainedModel extends PreTrainedModel { }; + +/** + * The bare AST Model transformer outputting raw hidden-states without any specific head on top. + */ +class ASTModel extends ASTPreTrainedModel { } + +/** + * Audio Spectrogram Transformer model with an audio classification head on top + * (a linear layer on top of the pooled output) e.g. for datasets like AudioSet, Speech Commands v2. + */ +class ASTForAudioClassification extends ASTPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Whisper models +class WhisperPreTrainedModel extends PreTrainedModel { + + requires_attention_mask = false; + main_input_name = 'input_features'; + forward_params = [ + 'input_features', + 'attention_mask', + 'decoder_input_ids', + 'decoder_attention_mask', + 'past_key_values', + ]; +}; + +/** + * WhisperModel class for training Whisper models without a language model head. + */ +class WhisperModel extends WhisperPreTrainedModel { } + + +/** + * WhisperForConditionalGeneration class for generating conditional outputs from Whisper models. + */ +class WhisperForConditionalGeneration extends WhisperPreTrainedModel { + + _prepare_generation_config(generation_config, kwargs) { + return /** @type {WhisperGenerationConfig} */ (super._prepare_generation_config(generation_config, kwargs, _models_whisper_generation_whisper_js__WEBPACK_IMPORTED_MODULE_15__.WhisperGenerationConfig)); + } + + /** + * + * @param {WhisperGenerationConfig} generation_config + */ + _retrieve_init_tokens(generation_config) { + // prefix tokens are of the form: + // - Multilingual: <|startoftranscript|> <|lang_id|> <|task|> [<|notimestamps|>] + // - English-only: <|startoftranscript|> [<|notimestamps|>] + + // 1. Handle <|startoftranscript|> token + const init_tokens = [generation_config.decoder_start_token_id]; + + // 2. Handle <|lang_id|> and <|task> tokens + let language = generation_config.language; + const task = generation_config.task; + if (generation_config.is_multilingual) { + if (!language) { + // TODO: Implement language detection + console.warn('No language specified - defaulting to English (en).'); + language = 'en'; + } + + // Add language token + const language_code = (0,_models_whisper_common_whisper_js__WEBPACK_IMPORTED_MODULE_16__.whisper_language_to_code)(language); + const language_token = `<|${language_code}|>`; + init_tokens.push(generation_config.lang_to_id[language_token]) + + // Add task token + // NOTE: Defaults to 'transcribe' if no task is specified + init_tokens.push(generation_config.task_to_id[task ?? 'transcribe']); + + } else if (language || task) { + throw new Error( + "Cannot specify `task` or `language` for an English-only model. If the model is intended to be multilingual, pass `is_multilingual=true` to generate, or update the generation config." + ) + } + + // 3. Handle <|notimestamps|> token + if ( + !generation_config.return_timestamps + && generation_config.no_timestamps_token_id + && init_tokens.at(-1) !== generation_config.no_timestamps_token_id + ) { + init_tokens.push(generation_config.no_timestamps_token_id); + } else if ( + generation_config.return_timestamps + && + init_tokens.at(-1) === generation_config.no_timestamps_token_id + ) { + console.warn("<|notimestamps|> prompt token is removed from generation_config since `return_timestamps` is set to `true`."); + init_tokens.pop(); + } + + // let's make sure we don't pass `null` tokens as prompt tokens + return init_tokens.filter(token => token != null); + } + + /** + * Transcribes or translates log-mel input features to a sequence of auto-regressively generated token ids. + * @param {import('./models/whisper/generation_whisper.js').WhisperGenerationFunctionParameters} options + * @returns {Promise} The output of the model, which can contain the generated token ids, attentions, and scores. + */ + async generate({ + inputs = null, + generation_config = null, + logits_processor = null, + stopping_criteria = null, + + // Whisper-specific options (passed to kwargs) + // prompt_ids = null, + // language = null, + // task = null, + + ...kwargs + }) { + generation_config = this._prepare_generation_config(generation_config, kwargs); + + const init_tokens = kwargs.decoder_input_ids ?? this._retrieve_init_tokens(generation_config); + + if (generation_config.return_timestamps) { + logits_processor ??= new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.LogitsProcessorList(); + logits_processor.push( + new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.WhisperTimeStampLogitsProcessor(generation_config, init_tokens) + ); + } + + if (generation_config.begin_suppress_tokens) { + logits_processor ??= new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.LogitsProcessorList(); + logits_processor.push( + new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.SuppressTokensAtBeginLogitsProcessor(generation_config.begin_suppress_tokens, init_tokens.length) + ); + } + + if (generation_config.return_token_timestamps) { + if (!generation_config.alignment_heads) { + throw new Error( + "Model generation config has no `alignment_heads`, token-level timestamps not available. " + + "See https://gist.github.com/hollance/42e32852f24243b748ae6bc1f985b13a on how to add this property to the generation config." + ) + } + + if (generation_config.task === 'translate') { + console.warn("Token-level timestamps may not be reliable for task 'translate'.") + } + + generation_config.output_attentions = true; + generation_config.return_dict_in_generate = true; + } + + const outputs = await super.generate({ + inputs, + generation_config, + logits_processor, + decoder_input_ids: init_tokens, + ...kwargs + }); + + if (generation_config.return_token_timestamps) { + outputs["token_timestamps"] = this._extract_token_timestamps( + // @ts-expect-error TS2345 + outputs, + generation_config.alignment_heads, + generation_config.num_frames, + ); + } + + return outputs; + } + + /** + * Calculates token-level timestamps using the encoder-decoder cross-attentions and + * dynamic time-warping (DTW) to map each output token to a position in the input audio. + * If `num_frames` is specified, the encoder-decoder cross-attentions will be cropped before applying DTW. + * @param {Object} generate_outputs Outputs generated by the model + * @param {Tensor[][]} generate_outputs.cross_attentions The cross attentions output by the model + * @param {Tensor} generate_outputs.sequences The sequences output by the model + * @param {number[][]} alignment_heads Alignment heads of the model + * @param {number} [num_frames=null] Number of frames in the input audio. + * @param {number} [time_precision=0.02] Precision of the timestamps in seconds + * @returns {Tensor} tensor containing the timestamps in seconds for each predicted token + */ + _extract_token_timestamps(generate_outputs, alignment_heads, num_frames = null, time_precision = 0.02) { + if (!generate_outputs.cross_attentions) { + throw new Error( + "Model outputs must contain cross attentions to extract timestamps. " + + "This is most likely because the model was not exported with `output_attentions=True`." + ) + } + if (num_frames == null) { + console.warn( + "`num_frames` has not been set, meaning the entire audio will be analyzed. " + + "This may lead to inaccurate token-level timestamps for short audios (< 30 seconds)." + ); + } + + // @ts-expect-error TS2339 + let median_filter_width = this.config.median_filter_width; + if (median_filter_width === undefined) { + console.warn("Model config has no `median_filter_width`, using default value of 7.") + median_filter_width = 7; + } + + // TODO: Improve batch processing + const batch = generate_outputs.cross_attentions; + // Create a list with `decoder_layers` elements, each a tensor of shape + // (batch size, attention_heads, output length, input length). + // @ts-expect-error TS2339 + const cross_attentions = Array.from({ length: this.config.decoder_layers }, + // Concatenate the cross attentions for each layer across sequence length dimension. + (_, i) => (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)(batch.map(x => x[i]), 2) + ); + + const weights = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.stack)(alignment_heads.map(([l, h]) => { + if (l >= cross_attentions.length) { + throw new Error(`Layer index ${l} is out of bounds for cross attentions (length ${cross_attentions.length}).`) + } + return num_frames + ? cross_attentions[l].slice(null, h, null, [0, num_frames]) + : cross_attentions[l].slice(null, h); + })).transpose(1, 0, 2, 3); + + const [std, calculatedMean] = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.std_mean)(weights, -2, 0, true); + + // Normalize and smoothen the weights. + const smoothedWeights = weights.clone(); // [1, 8, seqLength, 1500] + + for (let a = 0; a < smoothedWeights.dims[0]; ++a) { + const aTensor = smoothedWeights[a]; // [8, seqLength, 1500] + + for (let b = 0; b < aTensor.dims[0]; ++b) { + const bTensor = aTensor[b]; // [seqLength, 1500] + + const stdTensorData = std[a][b][0].data; // [1500] + const meanTensorData = calculatedMean[a][b][0].data; // [1500] + + for (let c = 0; c < bTensor.dims[0]; ++c) { + + let cTensorData = bTensor[c].data; // [1500] + for (let d = 0; d < cTensorData.length; ++d) { + cTensorData[d] = (cTensorData[d] - meanTensorData[d]) / stdTensorData[d] + } + + // Apply median filter. + cTensorData.set((0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_11__.medianFilter)(cTensorData, median_filter_width)) + } + } + } + + // Average the different cross-attention heads. + const batchedMatrices = [(0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.mean)(smoothedWeights, 1)]; + + const timestampsShape = generate_outputs.sequences.dims; + + const timestamps = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor( + 'float32', + new Float32Array(timestampsShape[0] * timestampsShape[1]), + timestampsShape + ); + + // Perform dynamic time warping on each element of the batch. + for (let batch_idx = 0; batch_idx < timestampsShape[0]; ++batch_idx) { + // NOTE: Since we run only one batch at a time, we can squeeze to get the same dimensions + // as the python implementation + const matrix = batchedMatrices[batch_idx].neg().squeeze_(0); + const [text_indices, time_indices] = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_11__.dynamic_time_warping)(matrix.tolist()); + + const diffs = Array.from({ length: text_indices.length - 1 }, (v, i) => text_indices[i + 1] - text_indices[i]); + const jumps = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.mergeArrays)([1], diffs).map(x => !!x); // convert to boolean + + const jump_times = []; + for (let i = 0; i < jumps.length; ++i) { + if (jumps[i]) { + // NOTE: No point in rounding here, since we set to Float32Array later + jump_times.push(time_indices[i] * time_precision); + } + } + timestamps[batch_idx].data.set(jump_times, 1) + } + + return timestamps; + } +} +////////////////////////////////////////////////// + +class LiteWhisperForConditionalGeneration extends WhisperForConditionalGeneration { } + +////////////////////////////////////////////////// +// Moonshine models +class MoonshinePreTrainedModel extends PreTrainedModel { + + requires_attention_mask = false; + main_input_name = 'input_values'; + forward_params = [ + 'input_values', + 'decoder_input_ids', + 'past_key_values', + ]; +}; + +/** + * MoonshineModel class for training Moonshine models without a language model head. + */ +class MoonshineModel extends MoonshinePreTrainedModel { } + +class MoonshineForConditionalGeneration extends MoonshinePreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +/** + * Vision Encoder-Decoder model based on OpenAI's GPT architecture for image captioning and other vision tasks + */ +class VisionEncoderDecoderModel extends PreTrainedModel { + main_input_name = 'pixel_values'; + forward_params = [ + // Encoder inputs + 'pixel_values', + + // Decoder inpputs + 'decoder_input_ids', + 'encoder_hidden_states', + 'past_key_values', + ]; +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// LLaVa Models +class LlavaPreTrainedModel extends PreTrainedModel { + forward_params = [ + 'input_ids', + 'attention_mask', + 'pixel_values', + 'position_ids', + 'past_key_values', + ]; +} + +/** + * The LLAVA model which consists of a vision backbone and a language model. + */ +class LlavaForConditionalGeneration extends LlavaPreTrainedModel { + + _merge_input_ids_with_image_features({ + inputs_embeds, + image_features, + input_ids, + attention_mask, + }) { + + // @ts-expect-error TS2339 + const image_token_index = this.config.image_token_index; + + const idsList = input_ids.tolist(); + + // NOTE: we use .findIndex instead of .indexOf to perform weak comparison (==) between BigInt and Number + const indexOfImage = idsList.map(x => x.findIndex(x => x == image_token_index)); + + const noImages = indexOfImage.every(x => x === -1); + const allImages = indexOfImage.every(x => x !== -1); + if (!noImages && !allImages) { + // Check for padding reasons + throw new Error('Every input should contain either 0 or 1 image token.'); + } + + if (noImages) { + return { + inputs_embeds, + attention_mask, + } + } + + const stacked = []; + const stacked_attention_mask = []; + for (let i = 0; i < indexOfImage.length; ++i) { + const index = indexOfImage[i]; + + const e = inputs_embeds[i]; + const im = image_features[i]; + const am = attention_mask[i]; + stacked.push( + (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)([ + e.slice([0, index]), + im, + e.slice([index + 1, e.dims[0]]), + ], 0) + ); + + stacked_attention_mask.push( + (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)([ + am.slice([0, index]), + (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.ones)([im.dims[0]]), + am.slice([index + 1, am.dims[0]]) + ], 0) + ) + } + + return { + inputs_embeds: (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.stack)(stacked, 0), + attention_mask: (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.stack)(stacked_attention_mask, 0), + } + } +} +////////////////////////////////////////////////// + +class LlavaOnevisionForConditionalGeneration extends LlavaForConditionalGeneration { } // NOTE: extends LlavaForConditionalGeneration +class Moondream1ForConditionalGeneration extends LlavaForConditionalGeneration { } // NOTE: extends LlavaForConditionalGeneration + +class Florence2PreTrainedModel extends PreTrainedModel { + forward_params = [ + // Encoder inputs + 'input_ids', + 'inputs_embeds', + 'attention_mask', + 'pixel_values', + + // Decoder inputs + 'encoder_outputs', + 'decoder_input_ids', + 'decoder_inputs_embeds', + 'decoder_attention_mask', + 'past_key_values', + ]; + main_input_name = 'inputs_embeds'; +} + +class Florence2ForConditionalGeneration extends Florence2PreTrainedModel { + + _merge_input_ids_with_image_features({ + inputs_embeds, + image_features, + input_ids, + attention_mask, + }) { + return { + inputs_embeds: (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)([ + image_features, // image embeds + inputs_embeds, // task prefix embeds + ], 1), + attention_mask: (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)([ + (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.ones)(image_features.dims.slice(0, 2)), // image attention mask + attention_mask, // task prefix attention mask + ], 1), + } + } + + async _prepare_inputs_embeds({ input_ids, pixel_values, inputs_embeds, attention_mask }) { + if (!input_ids && !pixel_values) { + throw new Error('Either `input_ids` or `pixel_values` should be provided.'); + } + + // 1. Possibly, extract the input embeddings + let text_features, image_features; + if (input_ids) { + text_features = await this.encode_text({ input_ids }); + } + if (pixel_values) { + image_features = await this.encode_image({ pixel_values }); + } + + // 2. Possibly, merge text and images + if (text_features && image_features) { + ({ inputs_embeds, attention_mask } = this._merge_input_ids_with_image_features({ + inputs_embeds: text_features, + image_features, + input_ids, + attention_mask, + })); + } else { + inputs_embeds = text_features || image_features; + } + + return { inputs_embeds, attention_mask }; + } + + async forward({ + input_ids, + pixel_values, + attention_mask, + decoder_input_ids, + decoder_attention_mask, + encoder_outputs, + past_key_values, + + inputs_embeds, + decoder_inputs_embeds, + }) { + if (!inputs_embeds) { + ({ inputs_embeds, attention_mask } = await this._prepare_inputs_embeds({ input_ids, pixel_values, inputs_embeds, attention_mask })); + } + + if (!encoder_outputs) { + // Must compute encoder outputs + let { last_hidden_state } = await encoderForward(this, { inputs_embeds, attention_mask }); + encoder_outputs = last_hidden_state; + } + + if (!decoder_inputs_embeds) { + if (!decoder_input_ids) { + throw new Error('Either `decoder_input_ids` or `decoder_inputs_embeds` should be provided.'); + } + decoder_inputs_embeds = await this.encode_text({ input_ids: decoder_input_ids }); + } + + const decoderFeeds = { + inputs_embeds: decoder_inputs_embeds, + attention_mask: decoder_attention_mask, + encoder_attention_mask: attention_mask, + encoder_hidden_states: encoder_outputs, + past_key_values, + }; + const decoder_outputs = await decoderForward(this, decoderFeeds, true); + return decoder_outputs; + } +} + +class PaliGemmaPreTrainedModel extends PreTrainedModel { + forward_params = [ + 'input_ids', + // 'inputs_embeds', + 'attention_mask', + 'pixel_values', + 'position_ids', + 'past_key_values', + ]; +} + +class PaliGemmaForConditionalGeneration extends PaliGemmaPreTrainedModel { + _merge_input_ids_with_image_features(kwargs) { + const vision_hidden_size = kwargs.image_features.dims.at(-1); + const reshaped_image_hidden_states = kwargs.image_features.view(-1, vision_hidden_size); + + return default_merge_input_ids_with_image_features({ + // @ts-ignore + image_token_id: this.config.image_token_index, + ...kwargs, + image_features: reshaped_image_hidden_states, + }) + } +} + +////////////////////////////////////////////////// +// Idefics3 Models +class Idefics3PreTrainedModel extends PreTrainedModel { + forward_params = [ + 'input_ids', + 'attention_mask', + 'pixel_values', + 'pixel_attention_mask', + 'position_ids', + 'past_key_values', + ]; +} + +/** + * The Idefics3 model which consists of a vision backbone and a language model. + */ +class Idefics3ForConditionalGeneration extends Idefics3PreTrainedModel { + + async encode_image({ pixel_values, pixel_attention_mask }) { + const features = (await sessionRun(this.sessions['vision_encoder'], { pixel_values, pixel_attention_mask })).image_features; + return features; + } + + _merge_input_ids_with_image_features(kwargs) { + const vision_hidden_size = kwargs.image_features.dims.at(-1); + const reshaped_image_hidden_states = kwargs.image_features.view(-1, vision_hidden_size); + + return default_merge_input_ids_with_image_features({ + // @ts-ignore + image_token_id: this.config.image_token_id, + ...kwargs, + image_features: reshaped_image_hidden_states, + }) + } +} +////////////////////////////////////////////////// + +/** + * The SmolVLM Model with a language modeling head. + * It is made up a SigLIP vision encoder, with a language modeling head on top. + */ +class SmolVLMForConditionalGeneration extends Idefics3ForConditionalGeneration { } + +////////////////////////////////////////////////// +class Phi3VPreTrainedModel extends PreTrainedModel { + forward_params = [ + 'input_ids', + 'inputs_embeds', + 'attention_mask', + 'position_ids', + 'pixel_values', + 'image_sizes', + 'past_key_values', + ]; +} +class Phi3VForCausalLM extends Phi3VPreTrainedModel { + + async forward({ + // Produced by the tokenizer/processor: + input_ids = null, + attention_mask = null, + pixel_values = null, + image_sizes = null, + + // Used during generation: + position_ids = null, + inputs_embeds = null, + past_key_values = null, + + // Generic generation parameters + generation_config = null, + logits_processor = null, + + // TODO: needed? + ...kwargs + }) { + if (!inputs_embeds) { + let image_features; + if (pixel_values && input_ids.dims[1] !== 1) { + if (!image_sizes) { + throw new Error('`image_sizes` must be provided when `pixel_values` is provided.'); + } + + // Encode the image + ({ image_features } = await sessionRun(this.sessions['vision_encoder'], { + pixel_values, + image_sizes, + })); + } else { + const hidden_size = this.config.normalized_config.hidden_size; + image_features = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor( + 'float32', + [], + [0, hidden_size], + ); + } + + ({ inputs_embeds } = await sessionRun(this.sessions['prepare_inputs_embeds'], { + input_ids, + image_features, + })); + } + + const outputs = await decoderForward(this, { + inputs_embeds, + past_key_values, + attention_mask, + position_ids, + generation_config, + logits_processor, + }, false); + return outputs; + } +} + +////////////////////////////////////////////////// +class CLIPPreTrainedModel extends PreTrainedModel { } + +/** + * CLIP Text and Vision Model with a projection layers on top + * + * **Example:** Perform zero-shot image classification with a `CLIPModel`. + * + * ```javascript + * import { AutoTokenizer, AutoProcessor, CLIPModel, RawImage } from '@huggingface/transformers'; + * + * // Load tokenizer, processor, and model + * let tokenizer = await AutoTokenizer.from_pretrained('Xenova/clip-vit-base-patch16'); + * let processor = await AutoProcessor.from_pretrained('Xenova/clip-vit-base-patch16'); + * let model = await CLIPModel.from_pretrained('Xenova/clip-vit-base-patch16'); + * + * // Run tokenization + * let texts = ['a photo of a car', 'a photo of a football match'] + * let text_inputs = tokenizer(texts, { padding: true, truncation: true }); + * + * // Read image and run processor + * let image = await RawImage.read('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/football-match.jpg'); + * let image_inputs = await processor(image); + * + * // Run model with both text and pixel inputs + * let output = await model({ ...text_inputs, ...image_inputs }); + * // { + * // logits_per_image: Tensor { + * // dims: [ 1, 2 ], + * // data: Float32Array(2) [ 18.579734802246094, 24.31830596923828 ], + * // }, + * // logits_per_text: Tensor { + * // dims: [ 2, 1 ], + * // data: Float32Array(2) [ 18.579734802246094, 24.31830596923828 ], + * // }, + * // text_embeds: Tensor { + * // dims: [ 2, 512 ], + * // data: Float32Array(1024) [ ... ], + * // }, + * // image_embeds: Tensor { + * // dims: [ 1, 512 ], + * // data: Float32Array(512) [ ... ], + * // } + * // } + * ``` + */ +class CLIPModel extends CLIPPreTrainedModel { } + +/** + * The text model from CLIP without any head or projection on top. + */ +class CLIPTextModel extends CLIPPreTrainedModel { + /** @type {typeof PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options = {}) { + return super.from_pretrained(pretrained_model_name_or_path, { + ...options, + // Update default model file name if not provided + model_file_name: options.model_file_name ?? 'text_model', + }); + } +} + +/** + * CLIP Text Model with a projection layer on top (a linear layer on top of the pooled output) + * + * **Example:** Compute text embeddings with `CLIPTextModelWithProjection`. + * + * ```javascript + * import { AutoTokenizer, CLIPTextModelWithProjection } from '@huggingface/transformers'; + * + * // Load tokenizer and text model + * const tokenizer = await AutoTokenizer.from_pretrained('Xenova/clip-vit-base-patch16'); + * const text_model = await CLIPTextModelWithProjection.from_pretrained('Xenova/clip-vit-base-patch16'); + * + * // Run tokenization + * let texts = ['a photo of a car', 'a photo of a football match']; + * let text_inputs = tokenizer(texts, { padding: true, truncation: true }); + * + * // Compute embeddings + * const { text_embeds } = await text_model(text_inputs); + * // Tensor { + * // dims: [ 2, 512 ], + * // type: 'float32', + * // data: Float32Array(1024) [ ... ], + * // size: 1024 + * // } + * ``` + */ +class CLIPTextModelWithProjection extends CLIPPreTrainedModel { + /** @type {typeof PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options = {}) { + return super.from_pretrained(pretrained_model_name_or_path, { + ...options, + // Update default model file name if not provided + model_file_name: options.model_file_name ?? 'text_model', + }); + } +} + +/** + * The vision model from CLIP without any head or projection on top. + */ +class CLIPVisionModel extends CLIPPreTrainedModel { + /** @type {typeof PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options = {}) { + return super.from_pretrained(pretrained_model_name_or_path, { + ...options, + // Update default model file name if not provided + model_file_name: options.model_file_name ?? 'vision_model', + }); + } +} + +/** + * CLIP Vision Model with a projection layer on top (a linear layer on top of the pooled output) + * + * **Example:** Compute vision embeddings with `CLIPVisionModelWithProjection`. + * + * ```javascript + * import { AutoProcessor, CLIPVisionModelWithProjection, RawImage} from '@huggingface/transformers'; + * + * // Load processor and vision model + * const processor = await AutoProcessor.from_pretrained('Xenova/clip-vit-base-patch16'); + * const vision_model = await CLIPVisionModelWithProjection.from_pretrained('Xenova/clip-vit-base-patch16'); + * + * // Read image and run processor + * let image = await RawImage.read('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/football-match.jpg'); + * let image_inputs = await processor(image); + * + * // Compute embeddings + * const { image_embeds } = await vision_model(image_inputs); + * // Tensor { + * // dims: [ 1, 512 ], + * // type: 'float32', + * // data: Float32Array(512) [ ... ], + * // size: 512 + * // } + * ``` + */ +class CLIPVisionModelWithProjection extends CLIPPreTrainedModel { + /** @type {typeof PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options = {}) { + return super.from_pretrained(pretrained_model_name_or_path, { + ...options, + // Update default model file name if not provided + model_file_name: options.model_file_name ?? 'vision_model', + }); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// SigLIP models +class SiglipPreTrainedModel extends PreTrainedModel { } + +/** + * SigLIP Text and Vision Model with a projection layers on top + * + * **Example:** Perform zero-shot image classification with a `SiglipModel`. + * + * ```javascript + * import { AutoTokenizer, AutoProcessor, SiglipModel, RawImage } from '@huggingface/transformers'; + * + * // Load tokenizer, processor, and model + * const tokenizer = await AutoTokenizer.from_pretrained('Xenova/siglip-base-patch16-224'); + * const processor = await AutoProcessor.from_pretrained('Xenova/siglip-base-patch16-224'); + * const model = await SiglipModel.from_pretrained('Xenova/siglip-base-patch16-224'); + * + * // Run tokenization + * const texts = ['a photo of 2 cats', 'a photo of 2 dogs']; + * const text_inputs = tokenizer(texts, { padding: 'max_length', truncation: true }); + * + * // Read image and run processor + * const image = await RawImage.read('http://images.cocodataset.org/val2017/000000039769.jpg'); + * const image_inputs = await processor(image); + * + * // Run model with both text and pixel inputs + * const output = await model({ ...text_inputs, ...image_inputs }); + * // { + * // logits_per_image: Tensor { + * // dims: [ 1, 2 ], + * // data: Float32Array(2) [ -1.6019744873046875, -10.720091819763184 ], + * // }, + * // logits_per_text: Tensor { + * // dims: [ 2, 1 ], + * // data: Float32Array(2) [ -1.6019744873046875, -10.720091819763184 ], + * // }, + * // text_embeds: Tensor { + * // dims: [ 2, 768 ], + * // data: Float32Array(1536) [ ... ], + * // }, + * // image_embeds: Tensor { + * // dims: [ 1, 768 ], + * // data: Float32Array(768) [ ... ], + * // } + * // } + * ``` + */ +class SiglipModel extends SiglipPreTrainedModel { } + +/** + * The text model from SigLIP without any head or projection on top. + * + * **Example:** Compute text embeddings with `SiglipTextModel`. + * + * ```javascript + * import { AutoTokenizer, SiglipTextModel } from '@huggingface/transformers'; + * + * // Load tokenizer and text model + * const tokenizer = await AutoTokenizer.from_pretrained('Xenova/siglip-base-patch16-224'); + * const text_model = await SiglipTextModel.from_pretrained('Xenova/siglip-base-patch16-224'); + * + * // Run tokenization + * const texts = ['a photo of 2 cats', 'a photo of 2 dogs']; + * const text_inputs = tokenizer(texts, { padding: 'max_length', truncation: true }); + * + * // Compute embeddings + * const { pooler_output } = await text_model(text_inputs); + * // Tensor { + * // dims: [ 2, 768 ], + * // type: 'float32', + * // data: Float32Array(1536) [ ... ], + * // size: 1536 + * // } + * ``` + */ +class SiglipTextModel extends SiglipPreTrainedModel { + /** @type {typeof PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options = {}) { + return super.from_pretrained(pretrained_model_name_or_path, { + ...options, + // Update default model file name if not provided + model_file_name: options.model_file_name ?? 'text_model', + }); + } +} + +/** + * The vision model from SigLIP without any head or projection on top. + * + * **Example:** Compute vision embeddings with `SiglipVisionModel`. + * + * ```javascript + * import { AutoProcessor, SiglipVisionModel, RawImage} from '@huggingface/transformers'; + * + * // Load processor and vision model + * const processor = await AutoProcessor.from_pretrained('Xenova/siglip-base-patch16-224'); + * const vision_model = await SiglipVisionModel.from_pretrained('Xenova/siglip-base-patch16-224'); + * + * // Read image and run processor + * const image = await RawImage.read('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/football-match.jpg'); + * const image_inputs = await processor(image); + * + * // Compute embeddings + * const { pooler_output } = await vision_model(image_inputs); + * // Tensor { + * // dims: [ 1, 768 ], + * // type: 'float32', + * // data: Float32Array(768) [ ... ], + * // size: 768 + * // } + * ``` + */ +class SiglipVisionModel extends CLIPPreTrainedModel { + /** @type {typeof PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options = {}) { + return super.from_pretrained(pretrained_model_name_or_path, { + ...options, + // Update default model file name if not provided + model_file_name: options.model_file_name ?? 'vision_model', + }); + } +} +////////////////////////////////////////////////// +// ChineseCLIP models +class ChineseCLIPPreTrainedModel extends PreTrainedModel { } + +class ChineseCLIPModel extends ChineseCLIPPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// JinaCLIP models +class JinaCLIPPreTrainedModel extends PreTrainedModel { } + +class JinaCLIPModel extends JinaCLIPPreTrainedModel { + async forward(model_inputs) { + const missing_text_inputs = !model_inputs.input_ids; + const missing_image_inputs = !model_inputs.pixel_values; + + if (missing_text_inputs && missing_image_inputs) { + throw new Error('Either `input_ids` or `pixel_values` should be provided.'); + } + + // If either `input_ids` or `pixel_values` aren't passed, we need to create dummy input since the model requires a value to be specified. + if (missing_text_inputs) { + // NOTE: We cannot pass zero-dimension tensor as input for input_ids. + // Fortunately, the majority of time is spent in the vision encoder, so this shouldn't significantly impact performance. + model_inputs.input_ids = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.ones)([model_inputs.pixel_values.dims[0], 1]); + } + + if (missing_image_inputs) { + // NOTE: Since we create a zero-sized tensor, this does not increase computation time. + // @ts-ignore + const { image_size } = this.config.vision_config; + model_inputs.pixel_values = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.full)([0, 3, image_size, image_size], 0.0); // (pass zero-dimension tensor) + } + + const { text_embeddings, image_embeddings, l2norm_text_embeddings, l2norm_image_embeddings } = await super.forward(model_inputs); + + const result = {}; + if (!missing_text_inputs) { + result.text_embeddings = text_embeddings; + result.l2norm_text_embeddings = l2norm_text_embeddings; + } + if (!missing_image_inputs) { + result.image_embeddings = image_embeddings; + result.l2norm_image_embeddings = l2norm_image_embeddings; + } + return result + } +} + +class JinaCLIPTextModel extends JinaCLIPPreTrainedModel { + /** @type {typeof PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options = {}) { + return super.from_pretrained(pretrained_model_name_or_path, { + ...options, + // Update default model file name if not provided + model_file_name: options.model_file_name ?? 'text_model', + }); + } +} + +class JinaCLIPVisionModel extends JinaCLIPPreTrainedModel { + /** @type {typeof PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options = {}) { + return super.from_pretrained(pretrained_model_name_or_path, { + ...options, + // Update default model file name if not provided + model_file_name: options.model_file_name ?? 'vision_model', + }); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// CLIPSeg models +class CLIPSegPreTrainedModel extends PreTrainedModel { } + +class CLIPSegModel extends CLIPSegPreTrainedModel { } + +/** + * CLIPSeg model with a Transformer-based decoder on top for zero-shot and one-shot image segmentation. + * + * **Example:** Perform zero-shot image segmentation with a `CLIPSegForImageSegmentation` model. + * + * ```javascript + * import { AutoTokenizer, AutoProcessor, CLIPSegForImageSegmentation, RawImage } from '@huggingface/transformers'; + * + * // Load tokenizer, processor, and model + * const tokenizer = await AutoTokenizer.from_pretrained('Xenova/clipseg-rd64-refined'); + * const processor = await AutoProcessor.from_pretrained('Xenova/clipseg-rd64-refined'); + * const model = await CLIPSegForImageSegmentation.from_pretrained('Xenova/clipseg-rd64-refined'); + * + * // Run tokenization + * const texts = ['a glass', 'something to fill', 'wood', 'a jar']; + * const text_inputs = tokenizer(texts, { padding: true, truncation: true }); + * + * // Read image and run processor + * const image = await RawImage.read('https://github.com/timojl/clipseg/blob/master/example_image.jpg?raw=true'); + * const image_inputs = await processor(image); + * + * // Run model with both text and pixel inputs + * const { logits } = await model({ ...text_inputs, ...image_inputs }); + * // logits: Tensor { + * // dims: [4, 352, 352], + * // type: 'float32', + * // data: Float32Array(495616) [ ... ], + * // size: 495616 + * // } + * ``` + * + * You can visualize the predictions as follows: + * ```javascript + * const preds = logits + * .unsqueeze_(1) + * .sigmoid_() + * .mul_(255) + * .round_() + * .to('uint8'); + * + * for (let i = 0; i < preds.dims[0]; ++i) { + * const img = RawImage.fromTensor(preds[i]); + * img.save(`prediction_${i}.png`); + * } + * ``` + */ +class CLIPSegForImageSegmentation extends CLIPSegPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// GPT2 models +class GPT2PreTrainedModel extends PreTrainedModel { } + +class GPT2Model extends GPT2PreTrainedModel { } + +/** + * GPT-2 language model head on top of the GPT-2 base model. This model is suitable for text generation tasks. + */ +class GPT2LMHeadModel extends GPT2PreTrainedModel { } +// export class GPT2ForSequenceClassification extends GPT2PreTrainedModel { +// TODO +// } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// JAIS models +class JAISPreTrainedModel extends PreTrainedModel { } + +/** + * The bare JAIS Model transformer outputting raw hidden-states without any specific head on top. + */ +class JAISModel extends JAISPreTrainedModel { } + +/** + * The JAIS Model transformer with a language modeling head on top (linear layer with weights tied to the input embeddings). + */ +class JAISLMHeadModel extends JAISPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// GPTNeo models +class GPTNeoPreTrainedModel extends PreTrainedModel { } +class GPTNeoModel extends GPTNeoPreTrainedModel { } + +class GPTNeoForCausalLM extends GPTNeoPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// GPTNeoX models +class GPTNeoXPreTrainedModel extends PreTrainedModel { } +class GPTNeoXModel extends GPTNeoXPreTrainedModel { } + +class GPTNeoXForCausalLM extends GPTNeoXPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// GPT-J models +class GPTJPreTrainedModel extends PreTrainedModel { } + +class GPTJModel extends GPTJPreTrainedModel { } + +class GPTJForCausalLM extends GPTJPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// GPTBigCode models +class GPTBigCodePreTrainedModel extends PreTrainedModel { } + +class GPTBigCodeModel extends GPTBigCodePreTrainedModel { } + +class GPTBigCodeForCausalLM extends GPTBigCodePreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// CodeGen models +class CodeGenPreTrainedModel extends PreTrainedModel { } +/** + * CodeGenModel is a class representing a code generation model without a language model head. + */ +class CodeGenModel extends CodeGenPreTrainedModel { } + +/** + * CodeGenForCausalLM is a class that represents a code generation model based on the GPT-2 architecture. It extends the `CodeGenPreTrainedModel` class. + */ +class CodeGenForCausalLM extends CodeGenPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// LLama models + +/** + * The bare LLama Model outputting raw hidden-states without any specific head on top. + */ +class LlamaPreTrainedModel extends PreTrainedModel { } +/** + * The bare LLaMA Model outputting raw hidden-states without any specific head on top. + */ +class LlamaModel extends LlamaPreTrainedModel { } + +class LlamaForCausalLM extends LlamaPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Helium models +class HeliumPreTrainedModel extends PreTrainedModel { } +class HeliumModel extends HeliumPreTrainedModel { } +class HeliumForCausalLM extends HeliumPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Glm models +class GlmPreTrainedModel extends PreTrainedModel { } +class GlmModel extends GlmPreTrainedModel { } +class GlmForCausalLM extends GlmPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// EXAONE models +class ExaonePreTrainedModel extends PreTrainedModel { } +class ExaoneModel extends ExaonePreTrainedModel { } +class ExaoneForCausalLM extends ExaonePreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// MobileLLM models +class MobileLLMPreTrainedModel extends PreTrainedModel { } +class MobileLLMModel extends MobileLLMPreTrainedModel { } +class MobileLLMForCausalLM extends MobileLLMPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// OLMo models +class OlmoPreTrainedModel extends PreTrainedModel { } +class OlmoModel extends OlmoPreTrainedModel { } +class OlmoForCausalLM extends OlmoPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// OLMo2 models +class Olmo2PreTrainedModel extends PreTrainedModel { } +class Olmo2Model extends Olmo2PreTrainedModel { } +class Olmo2ForCausalLM extends Olmo2PreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Granite models +class GranitePreTrainedModel extends PreTrainedModel { } +class GraniteModel extends GranitePreTrainedModel { } +class GraniteForCausalLM extends GranitePreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Cohere models + +/** + * The bare Cohere Model outputting raw hidden-states without any specific head on top. + */ +class CoherePreTrainedModel extends PreTrainedModel { } +class CohereModel extends CoherePreTrainedModel { } + +class CohereForCausalLM extends CoherePreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Gemma models + +/** + * The bare Gemma Model outputting raw hidden-states without any specific head on top. + */ +class GemmaPreTrainedModel extends PreTrainedModel { } +/** + * The bare Gemma Model outputting raw hidden-states without any specific head on top. + */ +class GemmaModel extends GemmaPreTrainedModel { } + +class GemmaForCausalLM extends GemmaPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Gemma2 models + +/** + * The bare Gemma2 Model outputting raw hidden-states without any specific head on top. + */ +class Gemma2PreTrainedModel extends PreTrainedModel { } +/** + * The bare Gemma2 Model outputting raw hidden-states without any specific head on top. + */ +class Gemma2Model extends Gemma2PreTrainedModel { } + +class Gemma2ForCausalLM extends Gemma2PreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Gemma3 models + +/** + * The bare Gemma3 Model outputting raw hidden-states without any specific head on top. + */ +class Gemma3PreTrainedModel extends PreTrainedModel { } +/** + * The bare Gemma3 Model outputting raw hidden-states without any specific head on top. + */ +class Gemma3Model extends Gemma3PreTrainedModel { } + +class Gemma3ForCausalLM extends Gemma3PreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +class OpenELMPreTrainedModel extends PreTrainedModel { } +class OpenELMModel extends OpenELMPreTrainedModel { } + +class OpenELMForCausalLM extends OpenELMPreTrainedModel { } + + +////////////////////////////////////////////////// +// Qwen2 models + +/** + * The bare Qwen2 Model outputting raw hidden-states without any specific head on top. + */ +class Qwen2PreTrainedModel extends PreTrainedModel { } +/** + * The bare Qwen2 Model outputting raw hidden-states without any specific head on top. + */ +class Qwen2Model extends Qwen2PreTrainedModel { } + +class Qwen2ForCausalLM extends Qwen2PreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Qwen3 models + +/** + * The bare Qwen3 Model outputting raw hidden-states without any specific head on top. + */ +class Qwen3PreTrainedModel extends PreTrainedModel { } +/** + * The bare Qwen3 Model outputting raw hidden-states without any specific head on top. + */ +class Qwen3Model extends Qwen3PreTrainedModel { } + +class Qwen3ForCausalLM extends Qwen3PreTrainedModel { } +////////////////////////////////////////////////// + +class Qwen2VLPreTrainedModel extends PreTrainedModel { + forward_params = [ + // Text inputs + 'input_ids', + 'attention_mask', + 'position_ids', + 'past_key_values', + + // Vision inputs + 'pixel_values', + 'image_grid_thw', + ]; +} +class Qwen2VLForConditionalGeneration extends Qwen2VLPreTrainedModel { + + /** + * Calculate the 3D rope index based on image and video's temporal, height and width in LLM. + * + * Explanation: + * Each embedding sequence contains vision embedding and text embedding or just contains text embedding. + * + * For pure text embedding sequence, the rotary position embedding has no difference with mordern LLMs. + * Examples: + * input_ids: [T T T T T], here T is for text. + * temporal position_ids: [0, 1, 2, 3, 4] + * height position_ids: [0, 1, 2, 3, 4] + * width position_ids: [0, 1, 2, 3, 4] + * + * For vision and text embedding sequence, we calculate 3D rotary position embedding for vision part + * and 1D rotary position embeddin for text part. + * Examples: + * Assume we have a video input with 3 temporal patches, 2 height patches and 2 width patches. + * input_ids: [V V V V V V V V V V V V T T T T T], here V is for vision. + * vision temporal position_ids: [0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2] + * vision height position_ids: [0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1] + * vision width position_ids: [0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1] + * text temporal position_ids: [3, 4, 5, 6, 7] + * text height position_ids: [3, 4, 5, 6, 7] + * text width position_ids: [3, 4, 5, 6, 7] + * Here we calculate the text start position_ids as the max vision position_ids plus 1. + * + * @param {Tensor} input_ids Indices of input sequence tokens in the vocabulary. Tensor of shape `(batch_size, sequence_length)`. + * @param {Tensor} image_grid_thw (Optional) The temporal, height and width of feature shape of each image in LLM. Tensor of shape `(num_images, 3)`. + * @param {Tensor} video_grid_thw (Optional) The temporal, height and width of feature shape of each video in LLM. Tensor of shape `(num_videos, 3)`. + * @param {Tensor} attention_mask (Optional) Mask to avoid performing attention on padding token indices. Tensor of shape `(batch_size, sequence_length)`. Mask values selected in `[0, 1]`: + * - 1 for tokens that are **not masked**, + * - 0 for tokens that are **masked**. + * @returns {[Tensor, Tensor]} [position_ids, mrope_position_deltas] with: + * - position_ids: Tensor of shape `(3, batch_size, sequence_length)`. + * - mrope_position_deltas: Tensor of shape `(batch_size)`. + */ + get_rope_index(input_ids, image_grid_thw, video_grid_thw, attention_mask) { + // @ts-ignore + const { vision_config, image_token_id, video_token_id, vision_start_token_id } = this.config; + const spatial_merge_size = vision_config.spatial_merge_size ?? 2; + + const mrope_position_deltas = []; + if (image_grid_thw || video_grid_thw) { + let total_input_ids = input_ids.tolist(); + if (!attention_mask) { + attention_mask = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.ones_like)(input_ids); + } + + const attention_mask_list = attention_mask.tolist(); + const position_ids_list = Array.from({ length: 3 }, _ => Array.from({ length: input_ids.dims[0] }, _ => Array.from({ length: input_ids.dims[1] }, _ => 1))); + + const image_grid_thw_list = image_grid_thw ? image_grid_thw.tolist() : []; + const video_grid_thw_list = video_grid_thw ? video_grid_thw.tolist() : []; + + let image_index = 0; + let video_index = 0; + for (let i = 0; i < total_input_ids.length; ++i) { + const ids = total_input_ids[i].filter((_, j) => attention_mask_list[i][j] == 1); + + const vision_start_indices = ids.reduce((acc, x, idx) => { + if (x == vision_start_token_id) acc.push(idx); + return acc; + }, []); + + const vision_tokens = vision_start_indices.map(x => ids[x + 1]); + const image_nums = vision_tokens.filter(x => x == image_token_id).length; + const video_nums = vision_tokens.filter(x => x == video_token_id).length; + + /** @type {number[][]} */ + let llm_pos_ids_list = []; + let st = 0; + let remain_images = image_nums; + let remain_videos = video_nums; + for (let j = 0; j < vision_tokens.length; ++j) { + const next_image_token = ids.findIndex((x, i) => i > st && x == image_token_id); + const next_video_token = ids.findIndex((x, i) => i > st && x == video_token_id); + + const ed_image = (remain_images > 0 && next_image_token !== -1) + ? next_image_token + : ids.length + 1; + + const ed_video = (remain_videos > 0 && next_video_token !== -1) + ? next_video_token + : ids.length + 1; + + let ed; + let t, h, w; + if (ed_image < ed_video) { + ([t, h, w] = image_grid_thw_list[image_index]); + ++image_index; + --remain_images; + ed = ed_image; + } else { + ([t, h, w] = video_grid_thw_list[video_index]); + ++video_index; + --remain_videos; + ed = ed_video; + } + + const [llm_grid_t, llm_grid_h, llm_grid_w] = [ + Number(t), + Math.floor(Number(h) / spatial_merge_size), + Math.floor(Number(w) / spatial_merge_size) + ] + const text_len = ed - st; + const st_idx = llm_pos_ids_list.length > 0 + ? (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_11__.max)(llm_pos_ids_list.at(-1))[0] + 1 + : 0; + + llm_pos_ids_list.push( + Array.from({ length: 3 * text_len }, (_, i) => st_idx + (i % text_len)) + ) + + const offset = text_len + st_idx; + const grid_size = llm_grid_t * llm_grid_h * llm_grid_w; + const t_index = Array.from({ length: grid_size }, (_, i) => offset + Math.floor(i / (llm_grid_h * llm_grid_w))) + const h_index = Array.from({ length: grid_size }, (_, i) => offset + Math.floor(i / llm_grid_w) % llm_grid_h) + const w_index = Array.from({ length: grid_size }, (_, i) => offset + i % llm_grid_w) + + llm_pos_ids_list.push([t_index, h_index, w_index].flat()) + + st = ed + grid_size; + } + + if (st < ids.length) { + const st_idx = llm_pos_ids_list.length > 0 + ? (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_11__.max)(llm_pos_ids_list.at(-1))[0] + 1 + : 0; + const text_len = ids.length - st; + + llm_pos_ids_list.push( + Array.from({ length: 3 * text_len }, (_, i) => (st_idx + (i % text_len))) + ) + } + + // NOTE: Each item in llm_pos_ids_list is an array of shape (3, text_len), + // meaning to perform concatenation along dim=1, we can do the following: + const num_items = llm_pos_ids_list.reduce((acc, x) => acc + x.length, 0); + /** @type {number[]} */ + const llm_positions = new Array(num_items); + let index = 0; + for (let x = 0; x < 3; ++x) { + for (let y = 0; y < llm_pos_ids_list.length; ++y) { + const val = llm_pos_ids_list[y]; + const text_len = val.length / 3; + for (let z = x * text_len; z < (x + 1) * text_len; ++z) { + llm_positions[index++] = val[z]; + } + } + } + + let count = 0; + const attn_mask = attention_mask_list[i]; + for (let y = 0; y < attn_mask.length; ++y) { + if (attn_mask[y] == 1) { + for (let x = 0; x < 3; ++x) { + position_ids_list[x][i][y] = llm_positions[x * num_items / 3 + count]; + } + ++count; + } + } + + const max_llm_positions = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_11__.max)(llm_positions)[0]; + mrope_position_deltas.push(max_llm_positions + 1 - total_input_ids[i].length); + } + + return [ + new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor('int64', position_ids_list.flat(Infinity), [3, input_ids.dims[0], input_ids.dims[1]]), + new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor('int64', mrope_position_deltas, [mrope_position_deltas.length, 1]), + ]; + + } else { // Text-only + if (attention_mask) { + const { data, dims } = cumsum_masked_fill(attention_mask); + + const position_ids = BigInt64Array.from( + { length: 3 * data.length }, + (_, i) => data[i % data.length] + ); + /** @type {bigint[]} */ + const mrope_position_deltas = Array.from( + { length: dims[0] }, + (_, i) => (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_11__.max)(data.subarray(dims[1] * i, dims[1] * (i + 1)))[0] + 1n + BigInt(dims[1]) + ); + + return [ + new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor('int64', position_ids, [3, ...dims]), + new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor('int64', mrope_position_deltas, [mrope_position_deltas.length, 1]), + ] + } else { + const [batch_size, seq_length] = input_ids.dims; + const position_ids = BigInt64Array.from( + { length: 3 * batch_size * seq_length }, + (_, i) => BigInt(Math.floor(i % seq_length / batch_size)), + ); + + return [ + new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor('int64', position_ids, [3, ...input_ids.dims]), + (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.zeros)([batch_size, 1]), + ] + } + } + } + + async encode_image({ pixel_values, image_grid_thw }) { + const features = (await sessionRun(this.sessions['vision_encoder'], { pixel_values, grid_thw: image_grid_thw })).image_features; + return features; + } + + _merge_input_ids_with_image_features(kwargs) { + return default_merge_input_ids_with_image_features({ + // @ts-ignore + image_token_id: this.config.image_token_id, + ...kwargs + }) + } + + prepare_inputs_for_generation(input_ids, model_inputs, generation_config) { + // Overwritten -- in specific circumstances we don't want to forward image inputs to the model + if (model_inputs.attention_mask && !model_inputs.position_ids) { + // Calculate position_ids and rope_deltas + if (!model_inputs.past_key_values) { + ([model_inputs.position_ids, model_inputs.rope_deltas] = this.get_rope_index( + model_inputs.input_ids, + model_inputs.image_grid_thw, + model_inputs.video_grid_thw, + model_inputs.attention_mask, + )); + + } else { + model_inputs.pixel_values = null; + // model_inputs.pixel_values_videos = null; + + const delta = BigInt(Object.values(model_inputs.past_key_values)[0].dims.at(-2)); + const rope_deltas_list = model_inputs.rope_deltas.map(x => delta + x); + model_inputs.position_ids = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.stack)([rope_deltas_list, rope_deltas_list, rope_deltas_list], 0) + } + } + + return model_inputs; + } +} + + +////////////////////////////////////////////////// +// Phi models +class PhiPreTrainedModel extends PreTrainedModel { } +/** + * The bare Phi Model outputting raw hidden-states without any specific head on top. + */ +class PhiModel extends PhiPreTrainedModel { } + +class PhiForCausalLM extends PhiPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Phi3 models +class Phi3PreTrainedModel extends PreTrainedModel { } + +/** + * The bare Phi3 Model outputting raw hidden-states without any specific head on top. + */ +class Phi3Model extends Phi3PreTrainedModel { } + +class Phi3ForCausalLM extends Phi3PreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Bloom models +/** + * The Bloom Model transformer with a language modeling head on top (linear layer with weights tied to the input embeddings). + */ +class BloomPreTrainedModel extends PreTrainedModel { } + +/** + * The bare Bloom Model transformer outputting raw hidden-states without any specific head on top. + */ +class BloomModel extends BloomPreTrainedModel { } + +/** + * The Bloom Model transformer with a language modeling head on top (linear layer with weights tied to the input embeddings). + */ +class BloomForCausalLM extends BloomPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// MPT models +class MptPreTrainedModel extends PreTrainedModel { } + +/** + * The bare Mpt Model transformer outputting raw hidden-states without any specific head on top. + */ +class MptModel extends MptPreTrainedModel { } + +/** + * The MPT Model transformer with a language modeling head on top (linear layer with weights tied to the input embeddings). + */ +class MptForCausalLM extends MptPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// OPT models +class OPTPreTrainedModel extends PreTrainedModel { } + +/** + * The bare OPT Model outputting raw hidden-states without any specific head on top. + */ +class OPTModel extends OPTPreTrainedModel { } + +/** + * The OPT Model transformer with a language modeling head on top (linear layer with weights tied to the input embeddings). + */ +class OPTForCausalLM extends OPTPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class ViTPreTrainedModel extends PreTrainedModel { } +class ViTModel extends ViTPreTrainedModel { } +class ViTForImageClassification extends ViTPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +class IJepaPreTrainedModel extends PreTrainedModel { } +class IJepaModel extends IJepaPreTrainedModel { } +class IJepaForImageClassification extends IJepaPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +class VitPosePreTrainedModel extends PreTrainedModel { } + +/** + * The VitPose model with a pose estimation head on top. + */ +class VitPoseForPoseEstimation extends VitPosePreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +class PvtPreTrainedModel extends PreTrainedModel { } +class PvtModel extends PvtPreTrainedModel { } +class PvtForImageClassification extends PvtPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class ViTMAEPreTrainedModel extends PreTrainedModel { } +class ViTMAEModel extends ViTMAEPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +class ViTMSNPreTrainedModel extends PreTrainedModel { } +class ViTMSNModel extends ViTMSNPreTrainedModel { } +class ViTMSNForImageClassification extends ViTMSNPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class GroupViTPreTrainedModel extends PreTrainedModel { } +class GroupViTModel extends GroupViTPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +class FastViTPreTrainedModel extends PreTrainedModel { } +class FastViTModel extends FastViTPreTrainedModel { } +class FastViTForImageClassification extends FastViTPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class VitMattePreTrainedModel extends PreTrainedModel { } + +/** + * ViTMatte framework leveraging any vision backbone e.g. for ADE20k, CityScapes. + * + * **Example:** Perform image matting with a `VitMatteForImageMatting` model. + * ```javascript + * import { AutoProcessor, VitMatteForImageMatting, RawImage } from '@huggingface/transformers'; + * + * // Load processor and model + * const processor = await AutoProcessor.from_pretrained('Xenova/vitmatte-small-distinctions-646'); + * const model = await VitMatteForImageMatting.from_pretrained('Xenova/vitmatte-small-distinctions-646'); + * + * // Load image and trimap + * const image = await RawImage.fromURL('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/vitmatte_image.png'); + * const trimap = await RawImage.fromURL('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/vitmatte_trimap.png'); + * + * // Prepare image + trimap for the model + * const inputs = await processor(image, trimap); + * + * // Predict alpha matte + * const { alphas } = await model(inputs); + * // Tensor { + * // dims: [ 1, 1, 640, 960 ], + * // type: 'float32', + * // size: 614400, + * // data: Float32Array(614400) [ 0.9894027709960938, 0.9970508813858032, ... ] + * // } + * ``` + * + * You can visualize the alpha matte as follows: + * ```javascript + * import { Tensor, cat } from '@huggingface/transformers'; + * + * // Visualize predicted alpha matte + * const imageTensor = image.toTensor(); + * + * // Convert float (0-1) alpha matte to uint8 (0-255) + * const alphaChannel = alphas + * .squeeze(0) + * .mul_(255) + * .clamp_(0, 255) + * .round_() + * .to('uint8'); + * + * // Concatenate original image with predicted alpha + * const imageData = cat([imageTensor, alphaChannel], 0); + * + * // Save output image + * const outputImage = RawImage.fromTensor(imageData); + * outputImage.save('output.png'); + * ``` + */ +class VitMatteForImageMatting extends VitMattePreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new ImageMattingOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class MobileViTPreTrainedModel extends PreTrainedModel { } +class MobileViTModel extends MobileViTPreTrainedModel { } +class MobileViTForImageClassification extends MobileViTPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +// TODO: MobileViTForSemanticSegmentation + +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class MobileViTV2PreTrainedModel extends PreTrainedModel { } +class MobileViTV2Model extends MobileViTV2PreTrainedModel { } +class MobileViTV2ForImageClassification extends MobileViTV2PreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +// TODO: MobileViTV2ForSemanticSegmentation + +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class OwlViTPreTrainedModel extends PreTrainedModel { } +class OwlViTModel extends OwlViTPreTrainedModel { } +class OwlViTForObjectDetection extends OwlViTPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class Owlv2PreTrainedModel extends PreTrainedModel { } +class Owlv2Model extends Owlv2PreTrainedModel { } +class Owlv2ForObjectDetection extends Owlv2PreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Beit Models +class BeitPreTrainedModel extends PreTrainedModel { } +class BeitModel extends BeitPreTrainedModel { } +class BeitForImageClassification extends BeitPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +class DetrPreTrainedModel extends PreTrainedModel { } +class DetrModel extends DetrPreTrainedModel { } +class DetrForObjectDetection extends DetrPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new DetrObjectDetectionOutput(await super._call(model_inputs)); + } +} + +class DetrForSegmentation extends DetrPreTrainedModel { + /** + * Runs the model with the provided inputs + * @param {Object} model_inputs Model inputs + * @returns {Promise} Object containing segmentation outputs + */ + async _call(model_inputs) { + return new DetrSegmentationOutput(await super._call(model_inputs)); + } +} + +class DetrObjectDetectionOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits Classification logits (including no-object) for all queries. + * @param {Tensor} output.pred_boxes Normalized boxes coordinates for all queries, represented as (center_x, center_y, width, height). + * These values are normalized in [0, 1], relative to the size of each individual image in the batch (disregarding possible padding). + */ + constructor({ logits, pred_boxes }) { + super(); + this.logits = logits; + this.pred_boxes = pred_boxes; + } +} + +class DetrSegmentationOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits The output logits of the model. + * @param {Tensor} output.pred_boxes Predicted boxes. + * @param {Tensor} output.pred_masks Predicted masks. + */ + constructor({ logits, pred_boxes, pred_masks }) { + super(); + this.logits = logits; + this.pred_boxes = pred_boxes; + this.pred_masks = pred_masks; + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class RTDetrPreTrainedModel extends PreTrainedModel { } +class RTDetrModel extends RTDetrPreTrainedModel { } +class RTDetrForObjectDetection extends RTDetrPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new RTDetrObjectDetectionOutput(await super._call(model_inputs)); + } +} + +class RTDetrObjectDetectionOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits Classification logits (including no-object) for all queries. + * @param {Tensor} output.pred_boxes Normalized boxes coordinates for all queries, represented as (center_x, center_y, width, height). + * These values are normalized in [0, 1], relative to the size of each individual image in the batch (disregarding possible padding). + */ + constructor({ logits, pred_boxes }) { + super(); + this.logits = logits; + this.pred_boxes = pred_boxes; + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +class RTDetrV2PreTrainedModel extends PreTrainedModel { } +class RTDetrV2Model extends RTDetrV2PreTrainedModel { } +class RTDetrV2ForObjectDetection extends RTDetrV2PreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new RTDetrV2ObjectDetectionOutput(await super._call(model_inputs)); + } +} + +class RTDetrV2ObjectDetectionOutput extends RTDetrObjectDetectionOutput { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class RFDetrPreTrainedModel extends PreTrainedModel { } +class RFDetrModel extends RFDetrPreTrainedModel { } +class RFDetrForObjectDetection extends RFDetrPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new RFDetrObjectDetectionOutput(await super._call(model_inputs)); + } +} + +class RFDetrObjectDetectionOutput extends RTDetrObjectDetectionOutput { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class DFinePreTrainedModel extends PreTrainedModel { } +class DFineModel extends DFinePreTrainedModel { } +class DFineForObjectDetection extends DFinePreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new RTDetrObjectDetectionOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class TableTransformerPreTrainedModel extends PreTrainedModel { } + +/** + * The bare Table Transformer Model (consisting of a backbone and encoder-decoder Transformer) + * outputting raw hidden-states without any specific head on top. + */ +class TableTransformerModel extends TableTransformerPreTrainedModel { } + +/** + * Table Transformer Model (consisting of a backbone and encoder-decoder Transformer) + * with object detection heads on top, for tasks such as COCO detection. + */ +class TableTransformerForObjectDetection extends TableTransformerPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new TableTransformerObjectDetectionOutput(await super._call(model_inputs)); + } +} +class TableTransformerObjectDetectionOutput extends DetrObjectDetectionOutput { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +class DeiTPreTrainedModel extends PreTrainedModel { } +class DeiTModel extends DeiTPreTrainedModel { } +class DeiTForImageClassification extends DeiTPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class HieraPreTrainedModel extends PreTrainedModel { } +class HieraModel extends HieraPreTrainedModel { } +class HieraForImageClassification extends HieraPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +/** + * An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained models. + */ +class ResNetPreTrainedModel extends PreTrainedModel { } + +/** + * The bare ResNet model outputting raw features without any specific head on top. + */ +class ResNetModel extends ResNetPreTrainedModel { } + +/** + * ResNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for ImageNet. + */ +class ResNetForImageClassification extends ResNetPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +class SwinPreTrainedModel extends PreTrainedModel { } +class SwinModel extends SwinPreTrainedModel { } +class SwinForImageClassification extends SwinPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +class SwinForSemanticSegmentation extends SwinPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class Swin2SRPreTrainedModel extends PreTrainedModel { } + +/** + * The bare Swin2SR Model transformer outputting raw hidden-states without any specific head on top. + */ +class Swin2SRModel extends Swin2SRPreTrainedModel { } + +/** + * Swin2SR Model transformer with an upsampler head on top for image super resolution and restoration. + * + * **Example:** Super-resolution w/ `Xenova/swin2SR-classical-sr-x2-64`. + * + * ```javascript + * import { AutoProcessor, Swin2SRForImageSuperResolution, RawImage } from '@huggingface/transformers'; + * + * // Load processor and model + * const model_id = 'Xenova/swin2SR-classical-sr-x2-64'; + * const processor = await AutoProcessor.from_pretrained(model_id); + * const model = await Swin2SRForImageSuperResolution.from_pretrained(model_id); + * + * // Prepare model inputs + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/butterfly.jpg'; + * const image = await RawImage.fromURL(url); + * const inputs = await processor(image); + * + * // Run model + * const outputs = await model(inputs); + * + * // Convert Tensor to RawImage + * const output = outputs.reconstruction.squeeze().clamp_(0, 1).mul_(255).round_().to('uint8'); + * const outputImage = RawImage.fromTensor(output); + * // RawImage { + * // data: Uint8Array(786432) [ 41, 31, 24, ... ], + * // width: 512, + * // height: 512, + * // channels: 3 + * // } + * ``` + */ +class Swin2SRForImageSuperResolution extends Swin2SRPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class DPTPreTrainedModel extends PreTrainedModel { } + +/** + * The bare DPT Model transformer outputting raw hidden-states without any specific head on top. + */ +class DPTModel extends DPTPreTrainedModel { } + +/** + * DPT Model with a depth estimation head on top (consisting of 3 convolutional layers) e.g. for KITTI, NYUv2. + * + * **Example:** Depth estimation w/ `Xenova/dpt-hybrid-midas`. + * ```javascript + * import { DPTForDepthEstimation, AutoProcessor, RawImage, interpolate_4d } from '@huggingface/transformers'; + * + * // Load model and processor + * const model_id = 'Xenova/dpt-hybrid-midas'; + * const model = await DPTForDepthEstimation.from_pretrained(model_id); + * const processor = await AutoProcessor.from_pretrained(model_id); + * + * // Load image from URL + * const url = 'http://images.cocodataset.org/val2017/000000039769.jpg'; + * const image = await RawImage.read(url); + * + * // Prepare image for the model + * const inputs = await processor(image); + * + * // Run model + * const { predicted_depth } = await model(inputs); + * + * // Interpolate to original size + * const prediction = (await interpolate_4d(predicted_depth.unsqueeze(1), { + * size: image.size.reverse(), + * mode: 'bilinear', + * })).squeeze(1); + * + * // Visualize the prediction + * const min = prediction.min().item(); + * const max = prediction.max().item(); + * const formatted = prediction.sub_(min).div_(max - min).mul_(255).to('uint8'); + * const depth = RawImage.fromTensor(formatted); + * // RawImage { + * // data: Uint8Array(307200) [ 85, 85, 84, ... ], + * // width: 640, + * // height: 480, + * // channels: 1 + * // } + * ``` + */ +class DPTForDepthEstimation extends DPTPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class DepthAnythingPreTrainedModel extends PreTrainedModel { } + +/** + * Depth Anything Model with a depth estimation head on top (consisting of 3 convolutional layers) e.g. for KITTI, NYUv2. + */ +class DepthAnythingForDepthEstimation extends DepthAnythingPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +class SapiensPreTrainedModel extends PreTrainedModel { } +class SapiensForSemanticSegmentation extends SapiensPreTrainedModel { } +class SapiensForDepthEstimation extends SapiensPreTrainedModel { } +class SapiensForNormalEstimation extends SapiensPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class DepthProPreTrainedModel extends PreTrainedModel { } +class DepthProForDepthEstimation extends DepthProPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class Metric3DPreTrainedModel extends PreTrainedModel { } +class Metric3DForDepthEstimation extends Metric3DPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class Metric3Dv2PreTrainedModel extends PreTrainedModel { } +class Metric3Dv2ForDepthEstimation extends Metric3Dv2PreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class MaskFormerPreTrainedModel extends PreTrainedModel { } +class MaskFormerModel extends MaskFormerPreTrainedModel { } +class MaskFormerForInstanceSegmentation extends MaskFormerPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class GLPNPreTrainedModel extends PreTrainedModel { } + +/** + * The bare GLPN encoder (Mix-Transformer) outputting raw hidden-states without any specific head on top. + */ +class GLPNModel extends GLPNPreTrainedModel { } + +/** + * import { GLPNForDepthEstimation, AutoProcessor, RawImage, interpolate_4d } from '@huggingface/transformers'; + * + * // Load model and processor + * const model_id = 'Xenova/glpn-kitti'; + * const model = await GLPNForDepthEstimation.from_pretrained(model_id); + * const processor = await AutoProcessor.from_pretrained(model_id); + * + * // Load image from URL + * const url = 'http://images.cocodataset.org/val2017/000000039769.jpg'; + * const image = await RawImage.read(url); + * + * // Prepare image for the model + * const inputs = await processor(image); + * + * // Run model + * const { predicted_depth } = await model(inputs); + * + * // Interpolate to original size + * const prediction = (await interpolate_4d(predicted_depth.unsqueeze(1), { + * size: image.size.reverse(), + * mode: 'bilinear', + * })).squeeze(1); + * + * // Visualize the prediction + * const min = prediction.min().item(); + * const max = prediction.max().item(); + * const formatted = prediction.sub_(min).div_(max - min).mul_(255).to('uint8'); + * const depth = RawImage.fromTensor(formatted); + * // RawImage { + * // data: Uint8Array(307200) [ 85, 85, 84, ... ], + * // width: 640, + * // height: 480, + * // channels: 1 + * // } + * ``` + */ +class GLPNForDepthEstimation extends GLPNPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class DonutSwinPreTrainedModel extends PreTrainedModel { } + +/** + * The bare Donut Swin Model transformer outputting raw hidden-states without any specific head on top. + * + * **Example:** Step-by-step Document Parsing. + * + * ```javascript + * import { AutoProcessor, AutoTokenizer, AutoModelForVision2Seq, RawImage } from '@huggingface/transformers'; + * + * // Choose model to use + * const model_id = 'Xenova/donut-base-finetuned-cord-v2'; + * + * // Prepare image inputs + * const processor = await AutoProcessor.from_pretrained(model_id); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/receipt.png'; + * const image = await RawImage.read(url); + * const image_inputs = await processor(image); + * + * // Prepare decoder inputs + * const tokenizer = await AutoTokenizer.from_pretrained(model_id); + * const task_prompt = ''; + * const decoder_input_ids = tokenizer(task_prompt, { + * add_special_tokens: false, + * }).input_ids; + * + * // Create the model + * const model = await AutoModelForVision2Seq.from_pretrained(model_id); + * + * // Run inference + * const output = await model.generate(image_inputs.pixel_values, { + * decoder_input_ids, + * max_length: model.config.decoder.max_position_embeddings, + * }); + * + * // Decode output + * const decoded = tokenizer.batch_decode(output)[0]; + * // CINNAMON SUGAR 17,000 1 x 17,000 17,000 17,000 20,000 3,000 + * ``` + * + * **Example:** Step-by-step Document Visual Question Answering (DocVQA) + * + * ```javascript + * import { AutoProcessor, AutoTokenizer, AutoModelForVision2Seq, RawImage } from '@huggingface/transformers'; + * + * // Choose model to use + * const model_id = 'Xenova/donut-base-finetuned-docvqa'; + * + * // Prepare image inputs + * const processor = await AutoProcessor.from_pretrained(model_id); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/invoice.png'; + * const image = await RawImage.read(url); + * const image_inputs = await processor(image); + * + * // Prepare decoder inputs + * const tokenizer = await AutoTokenizer.from_pretrained(model_id); + * const question = 'What is the invoice number?'; + * const task_prompt = `${question}`; + * const decoder_input_ids = tokenizer(task_prompt, { + * add_special_tokens: false, + * }).input_ids; + * + * // Create the model + * const model = await AutoModelForVision2Seq.from_pretrained(model_id); + * + * // Run inference + * const output = await model.generate(image_inputs.pixel_values, { + * decoder_input_ids, + * max_length: model.config.decoder.max_position_embeddings, + * }); + * + * // Decode output + * const decoded = tokenizer.batch_decode(output)[0]; + * // What is the invoice number? us-001 + * ``` + */ +class DonutSwinModel extends DonutSwinPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +class ConvNextPreTrainedModel extends PreTrainedModel { } + +/** + * The bare ConvNext model outputting raw features without any specific head on top. + */ +class ConvNextModel extends ConvNextPreTrainedModel { } + +/** + * ConvNext Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for ImageNet. + */ +class ConvNextForImageClassification extends ConvNextPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +class ConvNextV2PreTrainedModel extends PreTrainedModel { } + +/** + * The bare ConvNextV2 model outputting raw features without any specific head on top. + */ +class ConvNextV2Model extends ConvNextV2PreTrainedModel { } + +/** + * ConvNextV2 Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for ImageNet. + */ +class ConvNextV2ForImageClassification extends ConvNextV2PreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class Dinov2PreTrainedModel extends PreTrainedModel { } + +/** + * The bare DINOv2 Model transformer outputting raw hidden-states without any specific head on top. + */ +class Dinov2Model extends Dinov2PreTrainedModel { } + +/** + * Dinov2 Model transformer with an image classification head on top (a linear layer on top of the final hidden state of the [CLS] token) e.g. for ImageNet. + */ +class Dinov2ForImageClassification extends Dinov2PreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class Dinov2WithRegistersPreTrainedModel extends PreTrainedModel { } + +/** + * The bare Dinov2WithRegisters Model transformer outputting raw hidden-states without any specific head on top. + */ +class Dinov2WithRegistersModel extends Dinov2WithRegistersPreTrainedModel { } + +/** + * Dinov2WithRegisters Model transformer with an image classification head on top (a linear layer on top of the final hidden state of the [CLS] token) e.g. for ImageNet. + */ +class Dinov2WithRegistersForImageClassification extends Dinov2WithRegistersPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// +class GroundingDinoPreTrainedModel extends PreTrainedModel { } +class GroundingDinoForObjectDetection extends GroundingDinoPreTrainedModel { } + +////////////////////////////////////////////////// +class YolosPreTrainedModel extends PreTrainedModel { } +class YolosModel extends YolosPreTrainedModel { } +class YolosForObjectDetection extends YolosPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new YolosObjectDetectionOutput(await super._call(model_inputs)); + } +} + +class YolosObjectDetectionOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits Classification logits (including no-object) for all queries. + * @param {Tensor} output.pred_boxes Normalized boxes coordinates for all queries, represented as (center_x, center_y, width, height). + * These values are normalized in [0, 1], relative to the size of each individual image in the batch (disregarding possible padding). + */ + constructor({ logits, pred_boxes }) { + super(); + this.logits = logits; + this.pred_boxes = pred_boxes; + } +} +////////////////////////////////////////////////// + + + + +////////////////////////////////////////////////// +class SamPreTrainedModel extends PreTrainedModel { } + +/** + * Segment Anything Model (SAM) for generating segmentation masks, given an input image + * and optional 2D location and bounding boxes. + * + * **Example:** Perform mask generation w/ `Xenova/sam-vit-base`. + * ```javascript + * import { SamModel, AutoProcessor, RawImage } from '@huggingface/transformers'; + * + * const model = await SamModel.from_pretrained('Xenova/sam-vit-base'); + * const processor = await AutoProcessor.from_pretrained('Xenova/sam-vit-base'); + * + * const img_url = 'https://huggingface.co/ybelkada/segment-anything/resolve/main/assets/car.png'; + * const raw_image = await RawImage.read(img_url); + * const input_points = [[[450, 600]]] // 2D localization of a window + * + * const inputs = await processor(raw_image, { input_points }); + * const outputs = await model(inputs); + * + * const masks = await processor.post_process_masks(outputs.pred_masks, inputs.original_sizes, inputs.reshaped_input_sizes); + * // [ + * // Tensor { + * // dims: [ 1, 3, 1764, 2646 ], + * // type: 'bool', + * // data: Uint8Array(14002632) [ ... ], + * // size: 14002632 + * // } + * // ] + * const scores = outputs.iou_scores; + * // Tensor { + * // dims: [ 1, 1, 3 ], + * // type: 'float32', + * // data: Float32Array(3) [ + * // 0.8892380595207214, + * // 0.9311248064041138, + * // 0.983696699142456 + * // ], + * // size: 3 + * // } + * ``` + */ +class SamModel extends SamPreTrainedModel { + + /** + * Compute image embeddings and positional image embeddings, given the pixel values of an image. + * @param {Object} model_inputs Object containing the model inputs. + * @param {Tensor} model_inputs.pixel_values Pixel values obtained using a `SamProcessor`. + * @returns {Promise<{ image_embeddings: Tensor, image_positional_embeddings: Tensor }>} The image embeddings and positional image embeddings. + */ + async get_image_embeddings({ pixel_values }) { + // in: + // - pixel_values: tensor.float32[batch_size,3,1024,1024] + // + // out: + // - image_embeddings: tensor.float32[batch_size,256,64,64] + // - image_positional_embeddings: tensor.float32[batch_size,256,64,64] + return await encoderForward(this, { pixel_values }) + } + + /** + * @typedef {Object} SamModelInputs Object containing the model inputs. + * @property {Tensor} pixel_values Pixel values as a Tensor with shape `(batch_size, num_channels, height, width)`. + * These can be obtained using a `SamProcessor`. + * @property {Tensor} [input_points] Input 2D spatial points with shape `(batch_size, num_points, 2)`. + * This is used by the prompt encoder to encode the prompt. + * @property {Tensor} [input_labels] Input labels for the points, as a Tensor of shape `(batch_size, point_batch_size, num_points)`. + * This is used by the prompt encoder to encode the prompt. There are 4 types of labels: + * - `1`: the point is a point that contains the object of interest + * - `0`: the point is a point that does not contain the object of interest + * - `-1`: the point corresponds to the background + * - `-10`: the point is a padding point, thus should be ignored by the prompt encoder + * @property {Tensor} [input_boxes] Input bounding boxes with shape `(batch_size, num_boxes, 4)`. + * @property {Tensor} [image_embeddings] Image embeddings used by the mask decoder. + * @property {Tensor} [image_positional_embeddings] Image positional embeddings used by the mask decoder. + */ + + /** + * @param {SamModelInputs} model_inputs Object containing the model inputs. + * @returns {Promise} The output of the model. + */ + async forward(model_inputs) { + if (!model_inputs.image_embeddings || !model_inputs.image_positional_embeddings) { + // Compute the image embeddings if they are missing + model_inputs = { + ...model_inputs, + ...(await this.get_image_embeddings(model_inputs)) + } + } + + if (!model_inputs.input_labels && model_inputs.input_points) { + // Set default input labels if they are missing + const shape = model_inputs.input_points.dims.slice(0, -1); + const numElements = shape.reduce((a, b) => a * b, 1); + model_inputs.input_labels = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor( + 'int64', + new BigInt64Array(numElements).fill(1n), + shape + ); + } + + const decoder_inputs = { + image_embeddings: model_inputs.image_embeddings, + image_positional_embeddings: model_inputs.image_positional_embeddings, + }; + if (model_inputs.input_points) { + decoder_inputs.input_points = model_inputs.input_points; + } + if (model_inputs.input_labels) { + decoder_inputs.input_labels = model_inputs.input_labels; + } + if (model_inputs.input_boxes) { + decoder_inputs.input_boxes = model_inputs.input_boxes; + } + + // Returns: + // - iou_scores: tensor.float32[batch_size,point_batch_size,3] + // - pred_masks: tensor.float32[batch_size,point_batch_size,3,256,256] + return await sessionRun(this.sessions['prompt_encoder_mask_decoder'], decoder_inputs); + } + + /** + * Runs the model with the provided inputs + * @param {Object} model_inputs Model inputs + * @returns {Promise} Object containing segmentation outputs + */ + async _call(model_inputs) { + return new SamImageSegmentationOutput(await super._call(model_inputs)); + } +} + + +/** + * Base class for Segment-Anything model's output. + */ +class SamImageSegmentationOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.iou_scores The output logits of the model. + * @param {Tensor} output.pred_masks Predicted boxes. + */ + constructor({ iou_scores, pred_masks }) { + super(); + this.iou_scores = iou_scores; + this.pred_masks = pred_masks; + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// MarianMT models +class MarianPreTrainedModel extends PreTrainedModel { }; + +class MarianModel extends MarianPreTrainedModel { } + +class MarianMTModel extends MarianPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// M2M100 models +class M2M100PreTrainedModel extends PreTrainedModel { }; + +class M2M100Model extends M2M100PreTrainedModel { } + +class M2M100ForConditionalGeneration extends M2M100PreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Wav2Vec2 models +class Wav2Vec2PreTrainedModel extends PreTrainedModel { }; + +/** + * The bare Wav2Vec2 Model transformer outputting raw hidden-states without any specific head on top. + * + * **Example:** Load and run a `Wav2Vec2Model` for feature extraction. + * + * ```javascript + * import { AutoProcessor, AutoModel, read_audio } from '@huggingface/transformers'; + * + * // Read and preprocess audio + * const processor = await AutoProcessor.from_pretrained('Xenova/mms-300m'); + * const audio = await read_audio('https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac', 16000); + * const inputs = await processor(audio); + * + * // Run model with inputs + * const model = await AutoModel.from_pretrained('Xenova/mms-300m'); + * const output = await model(inputs); + * // { + * // last_hidden_state: Tensor { + * // dims: [ 1, 1144, 1024 ], + * // type: 'float32', + * // data: Float32Array(1171456) [ ... ], + * // size: 1171456 + * // } + * // } + * ``` + */ +class Wav2Vec2Model extends Wav2Vec2PreTrainedModel { } + +class Wav2Vec2ForCTC extends Wav2Vec2PreTrainedModel { + /** + * @param {Object} model_inputs + * @param {Tensor} model_inputs.input_values Float values of input raw speech waveform. + * @param {Tensor} model_inputs.attention_mask Mask to avoid performing convolution and attention on padding token indices. Mask values selected in [0, 1] + */ + async _call(model_inputs) { + return new CausalLMOutput(await super._call(model_inputs)); + } +} + +class Wav2Vec2ForSequenceClassification extends Wav2Vec2PreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * Wav2Vec2 Model with a frame classification head on top for tasks like Speaker Diarization. + */ +class Wav2Vec2ForAudioFrameClassification extends Wav2Vec2PreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// PyAnnote models +class PyAnnotePreTrainedModel extends PreTrainedModel { }; + +/** + * The bare PyAnnote Model transformer outputting raw hidden-states without any specific head on top. + */ +class PyAnnoteModel extends PyAnnotePreTrainedModel { } + +/** + * PyAnnote Model with a frame classification head on top for tasks like Speaker Diarization. + * + * **Example:** Load and run a `PyAnnoteForAudioFrameClassification` for speaker diarization. + * + * ```javascript + * import { AutoProcessor, AutoModelForAudioFrameClassification, read_audio } from '@huggingface/transformers'; + * + * // Load model and processor + * const model_id = 'onnx-community/pyannote-segmentation-3.0'; + * const model = await AutoModelForAudioFrameClassification.from_pretrained(model_id); + * const processor = await AutoProcessor.from_pretrained(model_id); + * + * // Read and preprocess audio + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/mlk.wav'; + * const audio = await read_audio(url, processor.feature_extractor.config.sampling_rate); + * const inputs = await processor(audio); + * + * // Run model with inputs + * const { logits } = await model(inputs); + * // { + * // logits: Tensor { + * // dims: [ 1, 767, 7 ], // [batch_size, num_frames, num_classes] + * // type: 'float32', + * // data: Float32Array(5369) [ ... ], + * // size: 5369 + * // } + * // } + * + * const result = processor.post_process_speaker_diarization(logits, audio.length); + * // [ + * // [ + * // { id: 0, start: 0, end: 1.0512535626298245, confidence: 0.8220156481664611 }, + * // { id: 2, start: 1.0512535626298245, end: 2.3398869619825127, confidence: 0.9008811707860472 }, + * // ... + * // ] + * // ] + * + * // Display result + * console.table(result[0], ['start', 'end', 'id', 'confidence']); + * // ┌─────────┬────────────────────┬────────────────────┬────┬─────────────────────┐ + * // │ (index) │ start │ end │ id │ confidence │ + * // ├─────────┼────────────────────┼────────────────────┼────┼─────────────────────┤ + * // │ 0 │ 0 │ 1.0512535626298245 │ 0 │ 0.8220156481664611 │ + * // │ 1 │ 1.0512535626298245 │ 2.3398869619825127 │ 2 │ 0.9008811707860472 │ + * // │ 2 │ 2.3398869619825127 │ 3.5946089560890773 │ 0 │ 0.7521651315796233 │ + * // │ 3 │ 3.5946089560890773 │ 4.578039708226655 │ 2 │ 0.8491978128022479 │ + * // │ 4 │ 4.578039708226655 │ 4.594995410849717 │ 0 │ 0.2935352600416393 │ + * // │ 5 │ 4.594995410849717 │ 6.121008646925269 │ 3 │ 0.6788051309866024 │ + * // │ 6 │ 6.121008646925269 │ 6.256654267909762 │ 0 │ 0.37125512393851134 │ + * // │ 7 │ 6.256654267909762 │ 8.630452635138397 │ 2 │ 0.7467035186353542 │ + * // │ 8 │ 8.630452635138397 │ 10.088643060721703 │ 0 │ 0.7689364814666032 │ + * // │ 9 │ 10.088643060721703 │ 12.58113134631177 │ 2 │ 0.9123324509131324 │ + * // │ 10 │ 12.58113134631177 │ 13.005023911888312 │ 0 │ 0.4828358177572041 │ + * // └─────────┴────────────────────┴────────────────────┴────┴─────────────────────┘ + * ``` + */ +class PyAnnoteForAudioFrameClassification extends PyAnnotePreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// WeSpeakerResNet models +class WeSpeakerResNetPreTrainedModel extends PreTrainedModel { }; +class WeSpeakerResNetModel extends WeSpeakerResNetPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// UniSpeech models +class UniSpeechPreTrainedModel extends PreTrainedModel { }; + +/** + * The bare UniSpeech Model transformer outputting raw hidden-states without any specific head on top. + */ +class UniSpeechModel extends UniSpeechPreTrainedModel { } + +/** + * UniSpeech Model with a `language modeling` head on top for Connectionist Temporal Classification (CTC). + */ +class UniSpeechForCTC extends UniSpeechPreTrainedModel { + /** + * @param {Object} model_inputs + * @param {Tensor} model_inputs.input_values Float values of input raw speech waveform. + * @param {Tensor} model_inputs.attention_mask Mask to avoid performing convolution and attention on padding token indices. Mask values selected in [0, 1] + */ + async _call(model_inputs) { + return new CausalLMOutput(await super._call(model_inputs)); + } +} + +/** + * UniSpeech Model with a sequence classification head on top (a linear layer over the pooled output). + */ +class UniSpeechForSequenceClassification extends UniSpeechPreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// UniSpeechSat models +class UniSpeechSatPreTrainedModel extends PreTrainedModel { }; + +/** + * The bare UniSpeechSat Model transformer outputting raw hidden-states without any specific head on top. + */ +class UniSpeechSatModel extends UniSpeechSatPreTrainedModel { } + +/** + * UniSpeechSat Model with a `language modeling` head on top for Connectionist Temporal Classification (CTC). + */ +class UniSpeechSatForCTC extends UniSpeechSatPreTrainedModel { + /** + * @param {Object} model_inputs + * @param {Tensor} model_inputs.input_values Float values of input raw speech waveform. + * @param {Tensor} model_inputs.attention_mask Mask to avoid performing convolution and attention on padding token indices. Mask values selected in [0, 1] + */ + async _call(model_inputs) { + return new CausalLMOutput(await super._call(model_inputs)); + } +} + +/** + * UniSpeechSat Model with a sequence classification head on top (a linear layer over the pooled output). + */ +class UniSpeechSatForSequenceClassification extends UniSpeechSatPreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * UniSpeechSat Model with a frame classification head on top for tasks like Speaker Diarization. + */ +class UniSpeechSatForAudioFrameClassification extends UniSpeechSatPreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Wav2Vec2Bert models +class Wav2Vec2BertPreTrainedModel extends PreTrainedModel { }; + +/** + * The bare Wav2Vec2Bert Model transformer outputting raw hidden-states without any specific head on top. + */ +class Wav2Vec2BertModel extends Wav2Vec2BertPreTrainedModel { } + +/** + * Wav2Vec2Bert Model with a `language modeling` head on top for Connectionist Temporal Classification (CTC). + */ +class Wav2Vec2BertForCTC extends Wav2Vec2BertPreTrainedModel { + /** + * @param {Object} model_inputs + * @param {Tensor} model_inputs.input_features Float values of input mel-spectrogram. + * @param {Tensor} model_inputs.attention_mask Mask to avoid performing convolution and attention on padding token indices. Mask values selected in [0, 1] + */ + async _call(model_inputs) { + return new CausalLMOutput(await super._call(model_inputs)); + } +} + +/** + * Wav2Vec2Bert Model with a sequence classification head on top (a linear layer over the pooled output). + */ +class Wav2Vec2BertForSequenceClassification extends Wav2Vec2BertPreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Hubert models +class HubertPreTrainedModel extends PreTrainedModel { } + +/** + * The bare Hubert Model transformer outputting raw hidden-states without any specific head on top. + * + * **Example:** Load and run a `HubertModel` for feature extraction. + * + * ```javascript + * import { AutoProcessor, AutoModel, read_audio } from '@huggingface/transformers'; + * + * // Read and preprocess audio + * const processor = await AutoProcessor.from_pretrained('Xenova/hubert-base-ls960'); + * const audio = await read_audio('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav', 16000); + * const inputs = await processor(audio); + * + * // Load and run model with inputs + * const model = await AutoModel.from_pretrained('Xenova/hubert-base-ls960'); + * const output = await model(inputs); + * // { + * // last_hidden_state: Tensor { + * // dims: [ 1, 549, 768 ], + * // type: 'float32', + * // data: Float32Array(421632) [0.0682469978928566, 0.08104046434164047, -0.4975186586380005, ...], + * // size: 421632 + * // } + * // } + * ``` + */ +class HubertModel extends Wav2Vec2PreTrainedModel { } + +/** + * Hubert Model with a `language modeling` head on top for Connectionist Temporal Classification (CTC). + */ +class HubertForCTC extends Wav2Vec2PreTrainedModel { + /** + * @param {Object} model_inputs + * @param {Tensor} model_inputs.input_values Float values of input raw speech waveform. + * @param {Tensor} model_inputs.attention_mask Mask to avoid performing convolution and attention on padding token indices. Mask values selected in [0, 1] + */ + async _call(model_inputs) { + return new CausalLMOutput(await super._call(model_inputs)); + } +} + +/** + * Hubert Model with a sequence classification head on top (a linear layer over the pooled output) for tasks like SUPERB Keyword Spotting. + */ +class HubertForSequenceClassification extends Wav2Vec2PreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// WavLM models +/** + * An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained models. + */ +class WavLMPreTrainedModel extends PreTrainedModel { }; + +/** + * The bare WavLM Model transformer outputting raw hidden-states without any specific head on top. + * + * **Example:** Load and run a `WavLMModel` for feature extraction. + * + * ```javascript + * import { AutoProcessor, AutoModel, read_audio } from '@huggingface/transformers'; + * + * // Read and preprocess audio + * const processor = await AutoProcessor.from_pretrained('Xenova/wavlm-base'); + * const audio = await read_audio('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav', 16000); + * const inputs = await processor(audio); + * + * // Run model with inputs + * const model = await AutoModel.from_pretrained('Xenova/wavlm-base'); + * const output = await model(inputs); + * // { + * // last_hidden_state: Tensor { + * // dims: [ 1, 549, 768 ], + * // type: 'float32', + * // data: Float32Array(421632) [-0.349443256855011, -0.39341306686401367, 0.022836603224277496, ...], + * // size: 421632 + * // } + * // } + * ``` + */ +class WavLMModel extends WavLMPreTrainedModel { } + +/** + * WavLM Model with a `language modeling` head on top for Connectionist Temporal Classification (CTC). + */ +class WavLMForCTC extends WavLMPreTrainedModel { + /** + * @param {Object} model_inputs + * @param {Tensor} model_inputs.input_values Float values of input raw speech waveform. + * @param {Tensor} model_inputs.attention_mask Mask to avoid performing convolution and attention on padding token indices. Mask values selected in [0, 1] + */ + async _call(model_inputs) { + return new CausalLMOutput(await super._call(model_inputs)); + } +} + +/** + * WavLM Model with a sequence classification head on top (a linear layer over the pooled output). + */ +class WavLMForSequenceClassification extends WavLMPreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * WavLM Model with an XVector feature extraction head on top for tasks like Speaker Verification. + * + * **Example:** Extract speaker embeddings with `WavLMForXVector`. + * ```javascript + * import { AutoProcessor, AutoModel, read_audio } from '@huggingface/transformers'; + * + * // Read and preprocess audio + * const processor = await AutoProcessor.from_pretrained('Xenova/wavlm-base-plus-sv'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav'; + * const audio = await read_audio(url, 16000); + * const inputs = await processor(audio); + * + * // Run model with inputs + * const model = await AutoModel.from_pretrained('Xenova/wavlm-base-plus-sv'); + * const outputs = await model(inputs); + * // { + * // logits: Tensor { + * // dims: [ 1, 512 ], + * // type: 'float32', + * // data: Float32Array(512) [0.5847219228744507, ...], + * // size: 512 + * // }, + * // embeddings: Tensor { + * // dims: [ 1, 512 ], + * // type: 'float32', + * // data: Float32Array(512) [-0.09079201519489288, ...], + * // size: 512 + * // } + * // } + * ``` + */ +class WavLMForXVector extends WavLMPreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits and speaker embeddings. + */ + async _call(model_inputs) { + return new XVectorOutput(await super._call(model_inputs)); + } +} + +/** + * WavLM Model with a frame classification head on top for tasks like Speaker Diarization. + * + * **Example:** Perform speaker diarization with `WavLMForAudioFrameClassification`. + * ```javascript + * import { AutoProcessor, AutoModelForAudioFrameClassification, read_audio } from '@huggingface/transformers'; + * + * // Read and preprocess audio + * const processor = await AutoProcessor.from_pretrained('Xenova/wavlm-base-plus-sd'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav'; + * const audio = await read_audio(url, 16000); + * const inputs = await processor(audio); + * + * // Run model with inputs + * const model = await AutoModelForAudioFrameClassification.from_pretrained('Xenova/wavlm-base-plus-sd'); + * const { logits } = await model(inputs); + * // { + * // logits: Tensor { + * // dims: [ 1, 549, 2 ], // [batch_size, num_frames, num_speakers] + * // type: 'float32', + * // data: Float32Array(1098) [-3.5301010608673096, ...], + * // size: 1098 + * // } + * // } + * + * const labels = logits[0].sigmoid().tolist().map( + * frames => frames.map(speaker => speaker > 0.5 ? 1 : 0) + * ); + * console.log(labels); // labels is a one-hot array of shape (num_frames, num_speakers) + * // [ + * // [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], + * // [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], + * // [0, 0], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], + * // ... + * // ] + * ``` + */ +class WavLMForAudioFrameClassification extends WavLMPreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} + +class StyleTextToSpeech2PreTrainedModel extends PreTrainedModel { } +class StyleTextToSpeech2Model extends StyleTextToSpeech2PreTrainedModel { } + +////////////////////////////////////////////////// +// SpeechT5 models +/** + * An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained models. + */ +class SpeechT5PreTrainedModel extends PreTrainedModel { }; + +/** + * The bare SpeechT5 Encoder-Decoder Model outputting raw hidden-states without any specific pre- or post-nets. + */ +class SpeechT5Model extends SpeechT5PreTrainedModel { }; + +/** + * SpeechT5 Model with a speech encoder and a text decoder. + * + * **Example:** Generate speech from text with `SpeechT5ForSpeechToText`. + * ```javascript + * import { AutoTokenizer, AutoProcessor, SpeechT5ForTextToSpeech, SpeechT5HifiGan, Tensor } from '@huggingface/transformers'; + * + * // Load the tokenizer and processor + * const tokenizer = await AutoTokenizer.from_pretrained('Xenova/speecht5_tts'); + * const processor = await AutoProcessor.from_pretrained('Xenova/speecht5_tts'); + * + * // Load the models + * // NOTE: We use the full-precision versions as they are more accurate + * const model = await SpeechT5ForTextToSpeech.from_pretrained('Xenova/speecht5_tts', { dtype: 'fp32' }); + * const vocoder = await SpeechT5HifiGan.from_pretrained('Xenova/speecht5_hifigan', { dtype: 'fp32' }); + * + * // Load speaker embeddings from URL + * const speaker_embeddings_data = new Float32Array( + * await (await fetch('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/speaker_embeddings.bin')).arrayBuffer() + * ); + * const speaker_embeddings = new Tensor( + * 'float32', + * speaker_embeddings_data, + * [1, speaker_embeddings_data.length] + * ) + * + * // Run tokenization + * const { input_ids } = tokenizer('Hello, my dog is cute'); + * + * // Generate waveform + * const { waveform } = await model.generate_speech(input_ids, speaker_embeddings, { vocoder }); + * console.log(waveform) + * // Tensor { + * // dims: [ 26112 ], + * // type: 'float32', + * // size: 26112, + * // data: Float32Array(26112) [ -0.00043630177970044315, -0.00018082228780258447, ... ], + * // } + * ``` + */ +class SpeechT5ForSpeechToText extends SpeechT5PreTrainedModel { } + +/** + * SpeechT5 Model with a text encoder and a speech decoder. + */ +class SpeechT5ForTextToSpeech extends SpeechT5PreTrainedModel { + + /** + * @typedef {Object} SpeechOutput + * @property {Tensor} [spectrogram] The predicted log-mel spectrogram of shape + * `(output_sequence_length, config.num_mel_bins)`. Returned when no `vocoder` is provided + * @property {Tensor} [waveform] The predicted waveform of shape `(num_frames,)`. Returned when a `vocoder` is provided. + * @property {Tensor} [cross_attentions] The outputs of the decoder's cross-attention layers of shape + * `(config.decoder_layers, config.decoder_attention_heads, output_sequence_length, input_sequence_length)`. returned when `output_cross_attentions` is `true`. + */ + + /** + * Converts a sequence of input tokens into a sequence of mel spectrograms, which are subsequently turned into a speech waveform using a vocoder. + * @param {Tensor} input_values Indices of input sequence tokens in the vocabulary. + * @param {Tensor} speaker_embeddings Tensor containing the speaker embeddings. + * @param {Object} options Optional parameters for generating speech. + * @param {number} [options.threshold=0.5] The generated sequence ends when the predicted stop token probability exceeds this value. + * @param {number} [options.minlenratio=0.0] Used to calculate the minimum required length for the output sequence. + * @param {number} [options.maxlenratio=20.0] Used to calculate the maximum allowed length for the output sequence. + * @param {Object} [options.vocoder=null] The vocoder that converts the mel spectrogram into a speech waveform. If `null`, the output is the mel spectrogram. + * @param {boolean} [options.output_cross_attentions=false] Whether or not to return the attentions tensors of the decoder's cross-attention layers. + * @returns {Promise} A promise which resolves to an object containing the spectrogram, waveform, and cross-attention tensors. + */ + async generate_speech(input_values, speaker_embeddings, { + threshold = 0.5, + minlenratio = 0.0, + maxlenratio = 20.0, + vocoder = null, + // output_cross_attentions = false, // TODO add + } = {}) { + + const model_inputs = { + input_ids: input_values + } + + const { encoder_outputs, encoder_attention_mask } = await encoderForward(this, model_inputs); + + // @ts-expect-error TS2339 + const r = encoder_outputs.dims[1] / this.config.reduction_factor; + const maxlen = Math.floor(r * maxlenratio); + const minlen = Math.floor(r * minlenratio); + + // @ts-expect-error TS2339 + const num_mel_bins = this.config.num_mel_bins; + + let spectrogramParts = []; + let past_key_values = null; + let decoder_outputs = null; + let idx = 0; + + while (true) { + ++idx; + + const use_cache_branch = boolTensor(!!decoder_outputs); + let output_sequence; + if (decoder_outputs) { + output_sequence = decoder_outputs.output_sequence_out; + } else { + output_sequence = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor( + 'float32', + new Float32Array(num_mel_bins), + [1, 1, num_mel_bins], + ) + } + let decoderFeeds = { + use_cache_branch, + output_sequence, + encoder_attention_mask: encoder_attention_mask, + speaker_embeddings: speaker_embeddings, + encoder_hidden_states: encoder_outputs, + }; + + this.addPastKeyValues(decoderFeeds, past_key_values); + decoder_outputs = await sessionRun(this.sessions['decoder_model_merged'], decoderFeeds); + past_key_values = this.getPastKeyValues(decoder_outputs, past_key_values); + + const { prob, spectrum } = decoder_outputs; + spectrogramParts.push(spectrum); + + if (idx >= minlen && ( + // Finished when stop token or maximum length is reached. + Array.from(prob.data).filter(p => p >= threshold).length > 0 || idx >= maxlen + )) { + break; + } + } + + const spectrogram = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)(spectrogramParts); + const { waveform } = await sessionRun(vocoder.sessions['model'], { spectrogram }); + + return { + spectrogram, + waveform, + // cross_attentions: null, // TODO add + } + } +} + +/** + * HiFi-GAN vocoder. + * + * See [SpeechT5ForSpeechToText](./models#module_models.SpeechT5ForSpeechToText) for example usage. + */ +class SpeechT5HifiGan extends PreTrainedModel { + main_input_name = 'spectrogram'; +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// TrOCR models +class TrOCRPreTrainedModel extends PreTrainedModel { } + +/** + * The TrOCR Decoder with a language modeling head. + */ +class TrOCRForCausalLM extends TrOCRPreTrainedModel { } + +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Mistral models +/** + * The bare Mistral Model outputting raw hidden-states without any specific head on top. + */ +class MistralPreTrainedModel extends PreTrainedModel { } + +class MistralModel extends MistralPreTrainedModel { } + +class MistralForCausalLM extends MistralPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Starcoder2 models +/** + * The bare Starcoder2 Model outputting raw hidden-states without any specific head on top. + */ +class Starcoder2PreTrainedModel extends PreTrainedModel { } + +class Starcoder2Model extends Starcoder2PreTrainedModel { } + +class Starcoder2ForCausalLM extends Starcoder2PreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Falcon models +/** + * The bare Falcon Model outputting raw hidden-states without any specific head on top. + */ +class FalconPreTrainedModel extends PreTrainedModel { } + +class FalconModel extends FalconPreTrainedModel { } + +class FalconForCausalLM extends FalconPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// CLAP models +class ClapPreTrainedModel extends PreTrainedModel { } + +class ClapModel extends ClapPreTrainedModel { } + +/** + * CLAP Text Model with a projection layer on top (a linear layer on top of the pooled output). + * + * **Example:** Compute text embeddings with `ClapTextModelWithProjection`. + * + * ```javascript + * import { AutoTokenizer, ClapTextModelWithProjection } from '@huggingface/transformers'; + * + * // Load tokenizer and text model + * const tokenizer = await AutoTokenizer.from_pretrained('Xenova/clap-htsat-unfused'); + * const text_model = await ClapTextModelWithProjection.from_pretrained('Xenova/clap-htsat-unfused'); + * + * // Run tokenization + * const texts = ['a sound of a cat', 'a sound of a dog']; + * const text_inputs = tokenizer(texts, { padding: true, truncation: true }); + * + * // Compute embeddings + * const { text_embeds } = await text_model(text_inputs); + * // Tensor { + * // dims: [ 2, 512 ], + * // type: 'float32', + * // data: Float32Array(1024) [ ... ], + * // size: 1024 + * // } + * ``` + */ +class ClapTextModelWithProjection extends ClapPreTrainedModel { + /** @type {typeof PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options = {}) { + return super.from_pretrained(pretrained_model_name_or_path, { + ...options, + // Update default model file name if not provided + model_file_name: options.model_file_name ?? 'text_model', + }); + } +} + +/** + * CLAP Audio Model with a projection layer on top (a linear layer on top of the pooled output). + * + * **Example:** Compute audio embeddings with `ClapAudioModelWithProjection`. + * + * ```javascript + * import { AutoProcessor, ClapAudioModelWithProjection, read_audio } from '@huggingface/transformers'; + * + * // Load processor and audio model + * const processor = await AutoProcessor.from_pretrained('Xenova/clap-htsat-unfused'); + * const audio_model = await ClapAudioModelWithProjection.from_pretrained('Xenova/clap-htsat-unfused'); + * + * // Read audio and run processor + * const audio = await read_audio('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/cat_meow.wav'); + * const audio_inputs = await processor(audio); + * + * // Compute embeddings + * const { audio_embeds } = await audio_model(audio_inputs); + * // Tensor { + * // dims: [ 1, 512 ], + * // type: 'float32', + * // data: Float32Array(512) [ ... ], + * // size: 512 + * // } + * ``` + */ +class ClapAudioModelWithProjection extends ClapPreTrainedModel { + /** @type {typeof PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options = {}) { + return super.from_pretrained(pretrained_model_name_or_path, { + ...options, + // Update default model file name if not provided + model_file_name: options.model_file_name ?? 'audio_model', + }); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// VITS models +class VitsPreTrainedModel extends PreTrainedModel { } + +/** + * The complete VITS model, for text-to-speech synthesis. + * + * **Example:** Generate speech from text with `VitsModel`. + * ```javascript + * import { AutoTokenizer, VitsModel } from '@huggingface/transformers'; + * + * // Load the tokenizer and model + * const tokenizer = await AutoTokenizer.from_pretrained('Xenova/mms-tts-eng'); + * const model = await VitsModel.from_pretrained('Xenova/mms-tts-eng'); + * + * // Run tokenization + * const inputs = tokenizer('I love transformers'); + * + * // Generate waveform + * const { waveform } = await model(inputs); + * // Tensor { + * // dims: [ 1, 35328 ], + * // type: 'float32', + * // data: Float32Array(35328) [ ... ], + * // size: 35328, + * // } + * ``` + */ +class VitsModel extends VitsPreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} The outputs for the VITS model. + */ + async _call(model_inputs) { + return new VitsModelOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Segformer models +class SegformerPreTrainedModel extends PreTrainedModel { } + +/** + * The bare SegFormer encoder (Mix-Transformer) outputting raw hidden-states without any specific head on top. + */ +class SegformerModel extends SegformerPreTrainedModel { } + +/** + * SegFormer Model transformer with an image classification head on top (a linear layer on top of the final hidden states) e.g. for ImageNet. + */ +class SegformerForImageClassification extends SegformerPreTrainedModel { } + +/** + * SegFormer Model transformer with an all-MLP decode head on top e.g. for ADE20k, CityScapes. + */ +class SegformerForSemanticSegmentation extends SegformerPreTrainedModel { } + +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// StableLm models +class StableLmPreTrainedModel extends PreTrainedModel { } + +/** + * The bare StableLm Model transformer outputting raw hidden-states without any specific head on top. + */ +class StableLmModel extends StableLmPreTrainedModel { } + +/** + * StableLm Model with a `language modeling` head on top for Causal Language Modeling (with past). + */ +class StableLmForCausalLM extends StableLmPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +class EfficientNetPreTrainedModel extends PreTrainedModel { } + +/** + * The bare EfficientNet model outputting raw features without any specific head on top. + */ +class EfficientNetModel extends EfficientNetPreTrainedModel { } + +/** + * EfficientNet Model with an image classification head on top (a linear layer on top of the pooled features). + */ +class EfficientNetForImageClassification extends EfficientNetPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Musicgen models +class MusicgenPreTrainedModel extends PreTrainedModel { } + +/** + * The bare Musicgen decoder model outputting raw hidden-states without any specific head on top. + */ +class MusicgenModel extends MusicgenPreTrainedModel { } + +/** + * The MusicGen decoder model with a language modelling head on top. + */ +class MusicgenForCausalLM extends MusicgenPreTrainedModel { } + +/** + * The composite MusicGen model with a text encoder, audio encoder and Musicgen decoder, + * for music generation tasks with one or both of text and audio prompts. + * + * **Example:** Generate music from text with `Xenova/musicgen-small`. + * ```javascript + * import { AutoTokenizer, MusicgenForConditionalGeneration } from '@huggingface/transformers'; + * + * // Load tokenizer and model + * const tokenizer = await AutoTokenizer.from_pretrained('Xenova/musicgen-small'); + * const model = await MusicgenForConditionalGeneration.from_pretrained( + * 'Xenova/musicgen-small', { dtype: 'fp32' } + * ); + * + * // Prepare text input + * const prompt = '80s pop track with bassy drums and synth'; + * const inputs = tokenizer(prompt); + * + * // Generate audio + * const audio_values = await model.generate({ + * ...inputs, + * max_new_tokens: 512, + * do_sample: true, + * guidance_scale: 3, + * }); + * + * // (Optional) Write the output to a WAV file + * import wavefile from 'wavefile'; + * import fs from 'fs'; + * + * const wav = new wavefile.WaveFile(); + * wav.fromScratch(1, model.config.audio_encoder.sampling_rate, '32f', audio_values.data); + * fs.writeFileSync('musicgen_out.wav', wav.toBuffer()); + * ``` + */ +class MusicgenForConditionalGeneration extends PreTrainedModel { // NOTE: not MusicgenPreTrainedModel + forward_params = [ + 'input_ids', + 'attention_mask', + 'encoder_outputs', + 'decoder_input_ids', + 'decoder_attention_mask', + 'past_key_values', + ]; + + /** + * Apply the pattern mask to the final ids, + * then revert the pattern delay mask by filtering the pad token id in a single step. + * @param {Tensor} outputs The output tensor from the model. + * @returns {Tensor} The filtered output tensor. + */ + _apply_and_filter_by_delay_pattern_mask(outputs) { + const [bs_x_codebooks, seqLength] = outputs.dims; + // @ts-expect-error TS2339 + const num_codebooks = this.config.decoder.num_codebooks; + const upperBound = (seqLength - num_codebooks); + + let newDataSize = 0; + for (let i = 0; i < outputs.size; ++i) { + // @ts-expect-error TS2339 + if (outputs.data[i] === this.config.decoder.pad_token_id) { + continue; + } + + const row = (i % seqLength); + const col = Math.floor(i / seqLength) % num_codebooks; + + const diff = row - col; + if (diff > 0 && diff <= upperBound) { + outputs.data[newDataSize++] = outputs.data[i]; + } + } + + const batch_size = Math.floor(bs_x_codebooks / num_codebooks); + const inferred = newDataSize / (batch_size * num_codebooks); + // TODO: assert `inferred` is an integer + return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor( + outputs.type, + outputs.data.slice(0, newDataSize), + [batch_size, num_codebooks, inferred] + ); + } + + + prepare_inputs_for_generation(input_ids, model_inputs, generation_config) { + // apply the delay pattern mask + let clonedInputIds = structuredClone(input_ids); + for (let i = 0; i < clonedInputIds.length; ++i) { + for (let j = 0; j < clonedInputIds[i].length; ++j) { + // @ts-expect-error TS2339 + if ((i % this.config.decoder.num_codebooks) >= j) { + // @ts-expect-error TS2339 + clonedInputIds[i][j] = BigInt(this.config.decoder.pad_token_id); + } + } + } + // for classifier free guidance we need to replicate the decoder args across the batch dim + // (we'll split these before sampling) + if (generation_config.guidance_scale !== null && generation_config.guidance_scale > 1) { + // [batch, seqLength] -> [2 * batch, seqLength] + clonedInputIds = clonedInputIds.concat(clonedInputIds); + } + + const prepped = super.prepare_inputs_for_generation(clonedInputIds, model_inputs, generation_config); + return prepped; + } + + /** + * Generates sequences of token ids for models with a language modeling head. + * @param {import('./generation/parameters.js').GenerationFunctionParameters} options + * @returns {Promise} The output of the model, which can contain the generated token ids, attentions, and scores. + */ + async generate(options) { + + const output_ids = await super.generate(options); + + // apply the pattern mask to the final ids + // tensor: int64[1,batch_size,4,chunk_length] + const audio_codes = this._apply_and_filter_by_delay_pattern_mask( + /** @type {Tensor} */(output_ids) + ).unsqueeze_(0); // append the frame dimension back to the audio codes + + const { audio_values } = await sessionRun(this.sessions['encodec_decode'], { audio_codes }) + + return audio_values; + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// MobileNetV1 models +class MobileNetV1PreTrainedModel extends PreTrainedModel { } + +/** + * The bare MobileNetV1 model outputting raw hidden-states without any specific head on top. + */ +class MobileNetV1Model extends MobileNetV1PreTrainedModel { } + +/** + * MobileNetV1 model with an image classification head on top (a linear layer on top of the pooled features), + * e.g. for ImageNet. + */ +class MobileNetV1ForImageClassification extends MobileNetV1PreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +class MobileNetV1ForSemanticSegmentation extends MobileNetV1PreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// MobileNetV2 models +class MobileNetV2PreTrainedModel extends PreTrainedModel { } + +/** + * The bare MobileNetV2 model outputting raw hidden-states without any specific head on top. + */ +class MobileNetV2Model extends MobileNetV2PreTrainedModel { } + +/** + * MobileNetV2 model with an image classification head on top (a linear layer on top of the pooled features), + * e.g. for ImageNet. + */ +class MobileNetV2ForImageClassification extends MobileNetV2PreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +class MobileNetV2ForSemanticSegmentation extends MobileNetV2PreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// MobileNetV3 models +class MobileNetV3PreTrainedModel extends PreTrainedModel { } + +/** + * The bare MobileNetV3 model outputting raw hidden-states without any specific head on top. + */ +class MobileNetV3Model extends MobileNetV3PreTrainedModel { } + +/** + * MobileNetV3 model with an image classification head on top (a linear layer on top of the pooled features), + * e.g. for ImageNet. + */ +class MobileNetV3ForImageClassification extends MobileNetV3PreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +class MobileNetV3ForSemanticSegmentation extends MobileNetV3PreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// MobileNetV4 models +class MobileNetV4PreTrainedModel extends PreTrainedModel { } + +/** + * The bare MobileNetV4 model outputting raw hidden-states without any specific head on top. + */ +class MobileNetV4Model extends MobileNetV4PreTrainedModel { } + +/** + * MobileNetV4 model with an image classification head on top (a linear layer on top of the pooled features), + * e.g. for ImageNet. + */ +class MobileNetV4ForImageClassification extends MobileNetV4PreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +class MobileNetV4ForSemanticSegmentation extends MobileNetV4PreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Decision Transformer models +class DecisionTransformerPreTrainedModel extends PreTrainedModel { } + +/** + * The model builds upon the GPT2 architecture to perform autoregressive prediction of actions in an offline RL setting. + * Refer to the paper for more details: https://arxiv.org/abs/2106.01345 + */ +class DecisionTransformerModel extends DecisionTransformerPreTrainedModel { } + +////////////////////////////////////////////////// + +class MultiModalityPreTrainedModel extends PreTrainedModel { } +class MultiModalityCausalLM extends MultiModalityPreTrainedModel { + forward_params = [ + // prepare_inputs_embeds + 'input_ids', + 'pixel_values', + 'images_seq_mask', + 'images_emb_mask', + + // language_model + 'attention_mask', + 'position_ids', + 'past_key_values', + ]; + + /** + * @param {ConstructorParameters} args + */ + constructor(...args) { + super(...args); + + // State-based approach to switch out which heads to use during generation + this._generation_mode = 'text'; + } + + async forward(model_inputs) { + const mode = this._generation_mode ?? 'text'; + + // TODO support re-using PKVs for input_ids.dims[1] !== 1 + // if (model_inputs.past_key_values) { + // // && model_inputs.input_ids.dims[1] === 1 + // } + + let output_1; + if (mode === 'text' || !model_inputs.past_key_values) { + const session = this.sessions['prepare_inputs_embeds']; + const prep_inputs = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.pick)(model_inputs, session.inputNames); + output_1 = await sessionRun(session, prep_inputs); + } else { + const session = this.sessions['gen_img_embeds']; + const prep_inputs = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.pick)({ + image_ids: model_inputs.input_ids, + }, session.inputNames); + output_1 = await sessionRun(session, prep_inputs); + } + + const input_2 = { ...model_inputs, ...output_1 } + const output_2 = await decoderForward(this, input_2); + + const head = this.sessions[ + mode === 'text' + ? 'lm_head' + : 'gen_head' + ]; + if (!head) { + throw new Error(`Unable to find "${head}" generation head`); + } + + const output_3 = await sessionRun(head, (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.pick)(output_2, head.inputNames)) + + return { + ...output_1, + ...output_2, + ...output_3, + }; + } + + /** + * @param {import('./generation/parameters.js').GenerationFunctionParameters} options + */ + async generate(options) { + this._generation_mode = 'text'; + return super.generate(options); + } + + /** + * @param {import('./generation/parameters.js').GenerationFunctionParameters} options + */ + async generate_images(options) { + this._generation_mode = 'image'; + + const start_num_tokens = (options.inputs ?? options[this.main_input_name]).dims[1]; + const all_tokens = await super.generate(options); + + const generated_tokens = (/** @type {Tensor} */(all_tokens)).slice(null, [start_num_tokens, null]) + + const image_decode = this.sessions['image_decode']; + const { decoded_image } = await sessionRun(image_decode, { + generated_tokens, + }); + + // Equivalent to `np.clip((dec + 1) / 2 * 255, 0, 255)` + const clamped = decoded_image + .add_(1) + .mul_(255 / 2) + .clamp_(0, 255) + .to('uint8'); + + // Return as a list of images + const images = []; + for (const tensor of clamped) { + const img = _utils_image_js__WEBPACK_IMPORTED_MODULE_10__.RawImage.fromTensor(tensor); + images.push(img); + } + return images; + } +} + +class MgpstrModelOutput extends ModelOutput { + constructor({ char_logits, bpe_logits, wp_logits }) { + super(); + this.char_logits = char_logits; + this.bpe_logits = bpe_logits; + this.wp_logits = wp_logits; + } + + get logits() { + return [this.char_logits, this.bpe_logits, this.wp_logits]; + } +} + +class MgpstrPreTrainedModel extends PreTrainedModel { } + +/** + * MGP-STR Model transformer with three classification heads on top + * (three A^3 modules and three linear layer on top of the transformer encoder output) for scene text recognition (STR). + */ +class MgpstrForSceneTextRecognition extends MgpstrPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new MgpstrModelOutput(await super._call(model_inputs)); + } +} + +////////////////////////////////////////////////// +// PatchTST Transformer models +class PatchTSTPreTrainedModel extends PreTrainedModel { } + +/** + * The bare PatchTST Model outputting raw hidden-states without any specific head. + */ +class PatchTSTModel extends PatchTSTPreTrainedModel { } + +/** + * The PatchTST for prediction model. + */ +class PatchTSTForPrediction extends PatchTSTPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// PatchTSMixer Transformer models +class PatchTSMixerPreTrainedModel extends PreTrainedModel { } + +/** + * The bare PatchTSMixer Model outputting raw hidden-states without any specific head. + */ +class PatchTSMixerModel extends PatchTSMixerPreTrainedModel { } + +/** + * The PatchTSMixer for prediction model. + */ +class PatchTSMixerForPrediction extends PatchTSMixerPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class UltravoxPreTrainedModel extends PreTrainedModel { + forward_params = [ + 'input_ids', + 'attention_mask', + 'position_ids', + 'audio_values', + 'past_key_values', + ]; +} + +class UltravoxModel extends UltravoxPreTrainedModel { + + _merge_input_ids_with_audio_features(kwargs) { + const audio_hidden_size = kwargs.audio_features.dims.at(-1); + const reshaped_audio_features = kwargs.audio_features.view(-1, audio_hidden_size); + + return default_merge_input_ids_with_audio_features({ + // @ts-ignore + audio_token_id: this.config.ignore_index, + ...kwargs, + audio_features: reshaped_audio_features, + }) + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Mimi models +class MimiPreTrainedModel extends PreTrainedModel { + main_input_name = 'input_values'; + forward_params = ['input_values']; +} + +class MimiEncoderOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.audio_codes Discrete code embeddings, of shape `(batch_size, num_quantizers, codes_length)`. + */ + constructor({ audio_codes }) { + super(); + this.audio_codes = audio_codes; + } +} + +class MimiDecoderOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.audio_values Decoded audio values, of shape `(batch_size, num_channels, sequence_length)`. + */ + constructor({ audio_values }) { + super(); + this.audio_values = audio_values; + } +} + +/** + * The Mimi neural audio codec model. + */ +class MimiModel extends MimiPreTrainedModel { + /** + * Encodes the input audio waveform into discrete codes. + * @param {Object} inputs Model inputs + * @param {Tensor} [inputs.input_values] Float values of the input audio waveform, of shape `(batch_size, channels, sequence_length)`). + * @returns {Promise} The output tensor of shape `(batch_size, num_codebooks, sequence_length)`. + */ + async encode(inputs) { + return new MimiEncoderOutput(await sessionRun(this.sessions['encoder_model'], inputs)); + } + + /** + * Decodes the given frames into an output audio waveform. + * @param {MimiEncoderOutput} inputs The encoded audio codes. + * @returns {Promise} The output tensor of shape `(batch_size, num_channels, sequence_length)`. + */ + async decode(inputs) { + return new MimiDecoderOutput(await sessionRun(this.sessions['decoder_model'], inputs)); + } +} + +class MimiEncoderModel extends MimiPreTrainedModel { + /** @type {typeof PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options = {}) { + return super.from_pretrained(pretrained_model_name_or_path, { + ...options, + // Update default model file name if not provided + model_file_name: options.model_file_name ?? 'encoder_model', + }); + } +} +class MimiDecoderModel extends MimiPreTrainedModel { + /** @type {typeof PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options = {}) { + return super.from_pretrained(pretrained_model_name_or_path, { + ...options, + // Update default model file name if not provided + model_file_name: options.model_file_name ?? 'decoder_model', + }); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Dac models +class DacPreTrainedModel extends PreTrainedModel { + main_input_name = 'input_values'; + forward_params = ['input_values']; +} + +class DacEncoderOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.audio_codes Discrete code embeddings, of shape `(batch_size, num_quantizers, codes_length)`. + */ + constructor({ audio_codes }) { + super(); + this.audio_codes = audio_codes; + } +} + +class DacDecoderOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.audio_values Decoded audio values, of shape `(batch_size, num_channels, sequence_length)`. + */ + constructor({ audio_values }) { + super(); + this.audio_values = audio_values; + } +} + +/** + * The DAC (Descript Audio Codec) model. + */ +class DacModel extends DacPreTrainedModel { + /** + * Encodes the input audio waveform into discrete codes. + * @param {Object} inputs Model inputs + * @param {Tensor} [inputs.input_values] Float values of the input audio waveform, of shape `(batch_size, channels, sequence_length)`). + * @returns {Promise} The output tensor of shape `(batch_size, num_codebooks, sequence_length)`. + */ + async encode(inputs) { + return new DacEncoderOutput(await sessionRun(this.sessions['encoder_model'], inputs)); + } + + /** + * Decodes the given frames into an output audio waveform. + * @param {DacEncoderOutput} inputs The encoded audio codes. + * @returns {Promise} The output tensor of shape `(batch_size, num_channels, sequence_length)`. + */ + async decode(inputs) { + return new DacDecoderOutput(await sessionRun(this.sessions['decoder_model'], inputs)); + } +} + +class DacEncoderModel extends DacPreTrainedModel { + /** @type {typeof PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options = {}) { + return super.from_pretrained(pretrained_model_name_or_path, { + ...options, + // Update default model file name if not provided + model_file_name: options.model_file_name ?? 'encoder_model', + }); + } +} +class DacDecoderModel extends DacPreTrainedModel { + /** @type {typeof PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options = {}) { + return super.from_pretrained(pretrained_model_name_or_path, { + ...options, + // Update default model file name if not provided + model_file_name: options.model_file_name ?? 'decoder_model', + }); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Snac models +class SnacPreTrainedModel extends PreTrainedModel { + main_input_name = 'input_values'; + forward_params = ['input_values']; +} + +/** + * The SNAC (Multi-Scale Neural Audio Codec) model. + */ +class SnacModel extends SnacPreTrainedModel { + /** + * Encodes the input audio waveform into discrete codes. + * @param {Object} inputs Model inputs + * @param {Tensor} [inputs.input_values] Float values of the input audio waveform, of shape `(batch_size, channels, sequence_length)`). + * @returns {Promise>} The output tensors of shape `(batch_size, num_codebooks, sequence_length)`. + */ + async encode(inputs) { + return await sessionRun(this.sessions['encoder_model'], inputs); + } + + /** + * Decodes the given frames into an output audio waveform. + * @param {Record} inputs The encoded audio codes. + * @returns {Promise<{audio_values: Tensor}>} The output tensor of shape `(batch_size, num_channels, sequence_length)`. + */ + async decode(inputs) { + return await sessionRun(this.sessions['decoder_model'], inputs); + } +} + +class SnacEncoderModel extends SnacPreTrainedModel { + /** @type {typeof PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options = {}) { + return super.from_pretrained(pretrained_model_name_or_path, { + ...options, + // Update default model file name if not provided + model_file_name: options.model_file_name ?? 'encoder_model', + }); + } +} +class SnacDecoderModel extends SnacPreTrainedModel { + /** @type {typeof PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options = {}) { + return super.from_pretrained(pretrained_model_name_or_path, { + ...options, + // Update default model file name if not provided + model_file_name: options.model_file_name ?? 'decoder_model', + }); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// AutoModels, used to simplify construction of PreTrainedModels +// (uses config to instantiate correct class) + +/** + * Base class of all AutoModels. Contains the `from_pretrained` function + * which is used to instantiate pretrained models. + */ +class PretrainedMixin { + /** + * Mapping from model type to model class. + * @type {Map[]} + */ + static MODEL_CLASS_MAPPINGS = null; + + /** + * Whether to attempt to instantiate the base class (`PretrainedModel`) if + * the model type is not found in the mapping. + */ + static BASE_IF_FAIL = false; + + + /** @type {typeof PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, { + progress_callback = null, + config = null, + cache_dir = null, + local_files_only = false, + revision = 'main', + model_file_name = null, + subfolder = 'onnx', + device = null, + dtype = null, + use_external_data_format = null, + session_options = {}, + } = {}) { + + const options = { + progress_callback, + config, + cache_dir, + local_files_only, + revision, + model_file_name, + subfolder, + device, + dtype, + use_external_data_format, + session_options, + } + options.config = await _configs_js__WEBPACK_IMPORTED_MODULE_0__.AutoConfig.from_pretrained(pretrained_model_name_or_path, options); + + if (!this.MODEL_CLASS_MAPPINGS) { + throw new Error("`MODEL_CLASS_MAPPINGS` not implemented for this type of `AutoClass`: " + this.name); + } + const model_type = options.config.model_type; + for (const MODEL_CLASS_MAPPING of this.MODEL_CLASS_MAPPINGS) { + let modelInfo = MODEL_CLASS_MAPPING.get(model_type); + if (!modelInfo) { + // As a fallback, we check if model_type is specified as the exact class + for (const cls of MODEL_CLASS_MAPPING.values()) { + if (cls[0] === model_type) { + modelInfo = cls; + break; + } + } + if (!modelInfo) continue; // Item not found in this mapping + } + return await modelInfo[1].from_pretrained(pretrained_model_name_or_path, options); + } + + if (this.BASE_IF_FAIL) { + if (!(CUSTOM_ARCHITECTURES.has(model_type))) { + console.warn(`Unknown model class "${model_type}", attempting to construct from base class.`); + } + return await PreTrainedModel.from_pretrained(pretrained_model_name_or_path, options); + } else { + throw Error(`Unsupported model type: ${model_type}`) + } + } +} + +const MODEL_MAPPING_NAMES_ENCODER_ONLY = new Map([ + ['bert', ['BertModel', BertModel]], + ['modernbert', ['ModernBertModel', ModernBertModel]], + ['nomic_bert', ['NomicBertModel', NomicBertModel]], + ['roformer', ['RoFormerModel', RoFormerModel]], + ['electra', ['ElectraModel', ElectraModel]], + ['esm', ['EsmModel', EsmModel]], + ['convbert', ['ConvBertModel', ConvBertModel]], + ['camembert', ['CamembertModel', CamembertModel]], + ['deberta', ['DebertaModel', DebertaModel]], + ['deberta-v2', ['DebertaV2Model', DebertaV2Model]], + ['mpnet', ['MPNetModel', MPNetModel]], + ['albert', ['AlbertModel', AlbertModel]], + ['distilbert', ['DistilBertModel', DistilBertModel]], + ['roberta', ['RobertaModel', RobertaModel]], + ['xlm', ['XLMModel', XLMModel]], + ['xlm-roberta', ['XLMRobertaModel', XLMRobertaModel]], + ['clap', ['ClapModel', ClapModel]], + ['clip', ['CLIPModel', CLIPModel]], + ['clipseg', ['CLIPSegModel', CLIPSegModel]], + ['chinese_clip', ['ChineseCLIPModel', ChineseCLIPModel]], + ['siglip', ['SiglipModel', SiglipModel]], + ['jina_clip', ['JinaCLIPModel', JinaCLIPModel]], + ['mobilebert', ['MobileBertModel', MobileBertModel]], + ['squeezebert', ['SqueezeBertModel', SqueezeBertModel]], + ['wav2vec2', ['Wav2Vec2Model', Wav2Vec2Model]], + ['wav2vec2-bert', ['Wav2Vec2BertModel', Wav2Vec2BertModel]], + ['unispeech', ['UniSpeechModel', UniSpeechModel]], + ['unispeech-sat', ['UniSpeechSatModel', UniSpeechSatModel]], + ['hubert', ['HubertModel', HubertModel]], + ['wavlm', ['WavLMModel', WavLMModel]], + ['audio-spectrogram-transformer', ['ASTModel', ASTModel]], + ['vits', ['VitsModel', VitsModel]], + ['pyannote', ['PyAnnoteModel', PyAnnoteModel]], + ['wespeaker-resnet', ['WeSpeakerResNetModel', WeSpeakerResNetModel]], + + ['detr', ['DetrModel', DetrModel]], + ['rt_detr', ['RTDetrModel', RTDetrModel]], + ['rt_detr_v2', ['RTDetrV2Model', RTDetrV2Model]], + ['rf_detr', ['RFDetrModel', RFDetrModel]], + ['d_fine', ['DFineModel', DFineModel]], + ['table-transformer', ['TableTransformerModel', TableTransformerModel]], + ['vit', ['ViTModel', ViTModel]], + ['ijepa', ['IJepaModel', IJepaModel]], + ['pvt', ['PvtModel', PvtModel]], + ['vit_msn', ['ViTMSNModel', ViTMSNModel]], + ['vit_mae', ['ViTMAEModel', ViTMAEModel]], + ['groupvit', ['GroupViTModel', GroupViTModel]], + ['fastvit', ['FastViTModel', FastViTModel]], + ['mobilevit', ['MobileViTModel', MobileViTModel]], + ['mobilevitv2', ['MobileViTV2Model', MobileViTV2Model]], + ['owlvit', ['OwlViTModel', OwlViTModel]], + ['owlv2', ['Owlv2Model', Owlv2Model]], + ['beit', ['BeitModel', BeitModel]], + ['deit', ['DeiTModel', DeiTModel]], + ['hiera', ['HieraModel', HieraModel]], + ['convnext', ['ConvNextModel', ConvNextModel]], + ['convnextv2', ['ConvNextV2Model', ConvNextV2Model]], + ['dinov2', ['Dinov2Model', Dinov2Model]], + ['dinov2_with_registers', ['Dinov2WithRegistersModel', Dinov2WithRegistersModel]], + ['resnet', ['ResNetModel', ResNetModel]], + ['swin', ['SwinModel', SwinModel]], + ['swin2sr', ['Swin2SRModel', Swin2SRModel]], + ['donut-swin', ['DonutSwinModel', DonutSwinModel]], + ['yolos', ['YolosModel', YolosModel]], + ['dpt', ['DPTModel', DPTModel]], + ['glpn', ['GLPNModel', GLPNModel]], + + ['hifigan', ['SpeechT5HifiGan', SpeechT5HifiGan]], + ['efficientnet', ['EfficientNetModel', EfficientNetModel]], + + ['decision_transformer', ['DecisionTransformerModel', DecisionTransformerModel]], + ['patchtst', ['PatchTSTForPrediction', PatchTSTModel]], + ['patchtsmixer', ['PatchTSMixerForPrediction', PatchTSMixerModel]], + + ['mobilenet_v1', ['MobileNetV1Model', MobileNetV1Model]], + ['mobilenet_v2', ['MobileNetV2Model', MobileNetV2Model]], + ['mobilenet_v3', ['MobileNetV3Model', MobileNetV3Model]], + ['mobilenet_v4', ['MobileNetV4Model', MobileNetV4Model]], + + ['maskformer', ['MaskFormerModel', MaskFormerModel]], + ['mgp-str', ['MgpstrForSceneTextRecognition', MgpstrForSceneTextRecognition]], + + ['style_text_to_speech_2', ['StyleTextToSpeech2Model', StyleTextToSpeech2Model]], +]); + +const MODEL_MAPPING_NAMES_ENCODER_DECODER = new Map([ + ['t5', ['T5Model', T5Model]], + ['longt5', ['LongT5Model', LongT5Model]], + ['mt5', ['MT5Model', MT5Model]], + ['bart', ['BartModel', BartModel]], + ['mbart', ['MBartModel', MBartModel]], + ['marian', ['MarianModel', MarianModel]], + ['whisper', ['WhisperModel', WhisperModel]], + ['m2m_100', ['M2M100Model', M2M100Model]], + ['blenderbot', ['BlenderbotModel', BlenderbotModel]], + ['blenderbot-small', ['BlenderbotSmallModel', BlenderbotSmallModel]], +]); + +const MODEL_MAPPING_NAMES_AUTO_ENCODER = new Map([ + ['mimi', ['MimiModel', MimiModel]], + ['dac', ['DacModel', DacModel]], + ['snac', ['SnacModel', SnacModel]], +]); + +const MODEL_MAPPING_NAMES_DECODER_ONLY = new Map([ + ['bloom', ['BloomModel', BloomModel]], + ['jais', ['JAISModel', JAISModel]], + ['gpt2', ['GPT2Model', GPT2Model]], + ['gptj', ['GPTJModel', GPTJModel]], + ['gpt_bigcode', ['GPTBigCodeModel', GPTBigCodeModel]], + ['gpt_neo', ['GPTNeoModel', GPTNeoModel]], + ['gpt_neox', ['GPTNeoXModel', GPTNeoXModel]], + ['codegen', ['CodeGenModel', CodeGenModel]], + ['llama', ['LlamaModel', LlamaModel]], + ['exaone', ['ExaoneModel', ExaoneModel]], + ['olmo', ['OlmoModel', OlmoModel]], + ['olmo2', ['Olmo2Model', Olmo2Model]], + ['mobilellm', ['MobileLLMModel', MobileLLMModel]], + ['granite', ['GraniteModel', GraniteModel]], + ['cohere', ['CohereModel', CohereModel]], + ['gemma', ['GemmaModel', GemmaModel]], + ['gemma2', ['Gemma2Model', Gemma2Model]], + ['gemma3_text', ['Gemma3Model', Gemma3Model]], + ['helium', ['HeliumModel', HeliumModel]], + ['glm', ['GlmModel', GlmModel]], + ['openelm', ['OpenELMModel', OpenELMModel]], + ['qwen2', ['Qwen2Model', Qwen2Model]], + ['qwen3', ['Qwen3Model', Qwen3Model]], + ['phi', ['PhiModel', PhiModel]], + ['phi3', ['Phi3Model', Phi3Model]], + ['mpt', ['MptModel', MptModel]], + ['opt', ['OPTModel', OPTModel]], + ['mistral', ['MistralModel', MistralModel]], + ['starcoder2', ['Starcoder2Model', Starcoder2Model]], + ['falcon', ['FalconModel', FalconModel]], + ['stablelm', ['StableLmModel', StableLmModel]], +]); + +const MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES = new Map([ + ['speecht5', ['SpeechT5ForSpeechToText', SpeechT5ForSpeechToText]], + ['whisper', ['WhisperForConditionalGeneration', WhisperForConditionalGeneration]], + ['lite-whisper', ['LiteWhisperForConditionalGeneration', LiteWhisperForConditionalGeneration]], + ['moonshine', ['MoonshineForConditionalGeneration', MoonshineForConditionalGeneration]], +]); + +const MODEL_FOR_TEXT_TO_SPECTROGRAM_MAPPING_NAMES = new Map([ + ['speecht5', ['SpeechT5ForTextToSpeech', SpeechT5ForTextToSpeech]], +]); + +const MODEL_FOR_TEXT_TO_WAVEFORM_MAPPING_NAMES = new Map([ + ['vits', ['VitsModel', VitsModel]], + ['musicgen', ['MusicgenForConditionalGeneration', MusicgenForConditionalGeneration]], +]); + +const MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES = new Map([ + ['bert', ['BertForSequenceClassification', BertForSequenceClassification]], + ['modernbert', ['ModernBertForSequenceClassification', ModernBertForSequenceClassification]], + ['roformer', ['RoFormerForSequenceClassification', RoFormerForSequenceClassification]], + ['electra', ['ElectraForSequenceClassification', ElectraForSequenceClassification]], + ['esm', ['EsmForSequenceClassification', EsmForSequenceClassification]], + ['convbert', ['ConvBertForSequenceClassification', ConvBertForSequenceClassification]], + ['camembert', ['CamembertForSequenceClassification', CamembertForSequenceClassification]], + ['deberta', ['DebertaForSequenceClassification', DebertaForSequenceClassification]], + ['deberta-v2', ['DebertaV2ForSequenceClassification', DebertaV2ForSequenceClassification]], + ['mpnet', ['MPNetForSequenceClassification', MPNetForSequenceClassification]], + ['albert', ['AlbertForSequenceClassification', AlbertForSequenceClassification]], + ['distilbert', ['DistilBertForSequenceClassification', DistilBertForSequenceClassification]], + ['roberta', ['RobertaForSequenceClassification', RobertaForSequenceClassification]], + ['xlm', ['XLMForSequenceClassification', XLMForSequenceClassification]], + ['xlm-roberta', ['XLMRobertaForSequenceClassification', XLMRobertaForSequenceClassification]], + ['bart', ['BartForSequenceClassification', BartForSequenceClassification]], + ['mbart', ['MBartForSequenceClassification', MBartForSequenceClassification]], + ['mobilebert', ['MobileBertForSequenceClassification', MobileBertForSequenceClassification]], + ['squeezebert', ['SqueezeBertForSequenceClassification', SqueezeBertForSequenceClassification]], +]); + +const MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES = new Map([ + ['bert', ['BertForTokenClassification', BertForTokenClassification]], + ['modernbert', ['ModernBertForTokenClassification', ModernBertForTokenClassification]], + ['roformer', ['RoFormerForTokenClassification', RoFormerForTokenClassification]], + ['electra', ['ElectraForTokenClassification', ElectraForTokenClassification]], + ['esm', ['EsmForTokenClassification', EsmForTokenClassification]], + ['convbert', ['ConvBertForTokenClassification', ConvBertForTokenClassification]], + ['camembert', ['CamembertForTokenClassification', CamembertForTokenClassification]], + ['deberta', ['DebertaForTokenClassification', DebertaForTokenClassification]], + ['deberta-v2', ['DebertaV2ForTokenClassification', DebertaV2ForTokenClassification]], + ['mpnet', ['MPNetForTokenClassification', MPNetForTokenClassification]], + ['distilbert', ['DistilBertForTokenClassification', DistilBertForTokenClassification]], + ['roberta', ['RobertaForTokenClassification', RobertaForTokenClassification]], + ['xlm', ['XLMForTokenClassification', XLMForTokenClassification]], + ['xlm-roberta', ['XLMRobertaForTokenClassification', XLMRobertaForTokenClassification]], +]); + +const MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES = new Map([ + ['t5', ['T5ForConditionalGeneration', T5ForConditionalGeneration]], + ['longt5', ['LongT5ForConditionalGeneration', LongT5ForConditionalGeneration]], + ['mt5', ['MT5ForConditionalGeneration', MT5ForConditionalGeneration]], + ['bart', ['BartForConditionalGeneration', BartForConditionalGeneration]], + ['mbart', ['MBartForConditionalGeneration', MBartForConditionalGeneration]], + ['marian', ['MarianMTModel', MarianMTModel]], + ['m2m_100', ['M2M100ForConditionalGeneration', M2M100ForConditionalGeneration]], + ['blenderbot', ['BlenderbotForConditionalGeneration', BlenderbotForConditionalGeneration]], + ['blenderbot-small', ['BlenderbotSmallForConditionalGeneration', BlenderbotSmallForConditionalGeneration]], +]); + +const MODEL_FOR_CAUSAL_LM_MAPPING_NAMES = new Map([ + ['bloom', ['BloomForCausalLM', BloomForCausalLM]], + ['gpt2', ['GPT2LMHeadModel', GPT2LMHeadModel]], + ['jais', ['JAISLMHeadModel', JAISLMHeadModel]], + ['gptj', ['GPTJForCausalLM', GPTJForCausalLM]], + ['gpt_bigcode', ['GPTBigCodeForCausalLM', GPTBigCodeForCausalLM]], + ['gpt_neo', ['GPTNeoForCausalLM', GPTNeoForCausalLM]], + ['gpt_neox', ['GPTNeoXForCausalLM', GPTNeoXForCausalLM]], + ['codegen', ['CodeGenForCausalLM', CodeGenForCausalLM]], + ['llama', ['LlamaForCausalLM', LlamaForCausalLM]], + ['exaone', ['ExaoneForCausalLM', ExaoneForCausalLM]], + ['olmo', ['OlmoForCausalLM', OlmoForCausalLM]], + ['olmo2', ['Olmo2ForCausalLM', Olmo2ForCausalLM]], + ['mobilellm', ['MobileLLMForCausalLM', MobileLLMForCausalLM]], + ['granite', ['GraniteForCausalLM', GraniteForCausalLM]], + ['cohere', ['CohereForCausalLM', CohereForCausalLM]], + ['gemma', ['GemmaForCausalLM', GemmaForCausalLM]], + ['gemma2', ['Gemma2ForCausalLM', Gemma2ForCausalLM]], + ['gemma3_text', ['Gemma3ForCausalLM', Gemma3ForCausalLM]], + ['helium', ['HeliumForCausalLM', HeliumForCausalLM]], + ['glm', ['GlmForCausalLM', GlmForCausalLM]], + ['openelm', ['OpenELMForCausalLM', OpenELMForCausalLM]], + ['qwen2', ['Qwen2ForCausalLM', Qwen2ForCausalLM]], + ['qwen3', ['Qwen3ForCausalLM', Qwen3ForCausalLM]], + ['phi', ['PhiForCausalLM', PhiForCausalLM]], + ['phi3', ['Phi3ForCausalLM', Phi3ForCausalLM]], + ['mpt', ['MptForCausalLM', MptForCausalLM]], + ['opt', ['OPTForCausalLM', OPTForCausalLM]], + ['mbart', ['MBartForCausalLM', MBartForCausalLM]], + ['mistral', ['MistralForCausalLM', MistralForCausalLM]], + ['starcoder2', ['Starcoder2ForCausalLM', Starcoder2ForCausalLM]], + ['falcon', ['FalconForCausalLM', FalconForCausalLM]], + ['trocr', ['TrOCRForCausalLM', TrOCRForCausalLM]], + ['stablelm', ['StableLmForCausalLM', StableLmForCausalLM]], + + // Also image-text-to-text + ['phi3_v', ['Phi3VForCausalLM', Phi3VForCausalLM]], +]); + +const MODEL_FOR_MULTIMODALITY_MAPPING_NAMES = new Map([ + ['multi_modality', ['MultiModalityCausalLM', MultiModalityCausalLM]], +]); + + +const MODEL_FOR_MASKED_LM_MAPPING_NAMES = new Map([ + ['bert', ['BertForMaskedLM', BertForMaskedLM]], + ['modernbert', ['ModernBertForMaskedLM', ModernBertForMaskedLM]], + ['roformer', ['RoFormerForMaskedLM', RoFormerForMaskedLM]], + ['electra', ['ElectraForMaskedLM', ElectraForMaskedLM]], + ['esm', ['EsmForMaskedLM', EsmForMaskedLM]], + ['convbert', ['ConvBertForMaskedLM', ConvBertForMaskedLM]], + ['camembert', ['CamembertForMaskedLM', CamembertForMaskedLM]], + ['deberta', ['DebertaForMaskedLM', DebertaForMaskedLM]], + ['deberta-v2', ['DebertaV2ForMaskedLM', DebertaV2ForMaskedLM]], + ['mpnet', ['MPNetForMaskedLM', MPNetForMaskedLM]], + ['albert', ['AlbertForMaskedLM', AlbertForMaskedLM]], + ['distilbert', ['DistilBertForMaskedLM', DistilBertForMaskedLM]], + ['roberta', ['RobertaForMaskedLM', RobertaForMaskedLM]], + ['xlm', ['XLMWithLMHeadModel', XLMWithLMHeadModel]], + ['xlm-roberta', ['XLMRobertaForMaskedLM', XLMRobertaForMaskedLM]], + ['mobilebert', ['MobileBertForMaskedLM', MobileBertForMaskedLM]], + ['squeezebert', ['SqueezeBertForMaskedLM', SqueezeBertForMaskedLM]], +]); + +const MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES = new Map([ + ['bert', ['BertForQuestionAnswering', BertForQuestionAnswering]], + ['roformer', ['RoFormerForQuestionAnswering', RoFormerForQuestionAnswering]], + ['electra', ['ElectraForQuestionAnswering', ElectraForQuestionAnswering]], + ['convbert', ['ConvBertForQuestionAnswering', ConvBertForQuestionAnswering]], + ['camembert', ['CamembertForQuestionAnswering', CamembertForQuestionAnswering]], + ['deberta', ['DebertaForQuestionAnswering', DebertaForQuestionAnswering]], + ['deberta-v2', ['DebertaV2ForQuestionAnswering', DebertaV2ForQuestionAnswering]], + ['mpnet', ['MPNetForQuestionAnswering', MPNetForQuestionAnswering]], + ['albert', ['AlbertForQuestionAnswering', AlbertForQuestionAnswering]], + ['distilbert', ['DistilBertForQuestionAnswering', DistilBertForQuestionAnswering]], + ['roberta', ['RobertaForQuestionAnswering', RobertaForQuestionAnswering]], + ['xlm', ['XLMForQuestionAnswering', XLMForQuestionAnswering]], + ['xlm-roberta', ['XLMRobertaForQuestionAnswering', XLMRobertaForQuestionAnswering]], + ['mobilebert', ['MobileBertForQuestionAnswering', MobileBertForQuestionAnswering]], + ['squeezebert', ['SqueezeBertForQuestionAnswering', SqueezeBertForQuestionAnswering]], +]); + +const MODEL_FOR_VISION_2_SEQ_MAPPING_NAMES = new Map([ + ['vision-encoder-decoder', ['VisionEncoderDecoderModel', VisionEncoderDecoderModel]], + ['idefics3', ['Idefics3ForConditionalGeneration', Idefics3ForConditionalGeneration]], + ['smolvlm', ['SmolVLMForConditionalGeneration', SmolVLMForConditionalGeneration]], +]); + +const MODEL_FOR_IMAGE_TEXT_TO_TEXT_MAPPING_NAMES = new Map([ + ['llava', ['LlavaForConditionalGeneration', LlavaForConditionalGeneration]], + ['llava_onevision', ['LlavaOnevisionForConditionalGeneration', LlavaOnevisionForConditionalGeneration]], + ['moondream1', ['Moondream1ForConditionalGeneration', Moondream1ForConditionalGeneration]], + ['florence2', ['Florence2ForConditionalGeneration', Florence2ForConditionalGeneration]], + ['qwen2-vl', ['Qwen2VLForConditionalGeneration', Qwen2VLForConditionalGeneration]], + ['idefics3', ['Idefics3ForConditionalGeneration', Idefics3ForConditionalGeneration]], + ['smolvlm', ['SmolVLMForConditionalGeneration', SmolVLMForConditionalGeneration]], + ['paligemma', ['PaliGemmaForConditionalGeneration', PaliGemmaForConditionalGeneration]], +]); + +const MODEL_FOR_AUDIO_TEXT_TO_TEXT_MAPPING_NAMES = new Map([ + ['ultravox', ['UltravoxModel', UltravoxModel]], +]); + + +const MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING_NAMES = new Map([ + ['vision-encoder-decoder', ['VisionEncoderDecoderModel', VisionEncoderDecoderModel]], +]); + +const MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES = new Map([ + ['vit', ['ViTForImageClassification', ViTForImageClassification]], + ['ijepa', ['IJepaForImageClassification', IJepaForImageClassification]], + ['pvt', ['PvtForImageClassification', PvtForImageClassification]], + ['vit_msn', ['ViTMSNForImageClassification', ViTMSNForImageClassification]], + ['fastvit', ['FastViTForImageClassification', FastViTForImageClassification]], + ['mobilevit', ['MobileViTForImageClassification', MobileViTForImageClassification]], + ['mobilevitv2', ['MobileViTV2ForImageClassification', MobileViTV2ForImageClassification]], + ['beit', ['BeitForImageClassification', BeitForImageClassification]], + ['deit', ['DeiTForImageClassification', DeiTForImageClassification]], + ['hiera', ['HieraForImageClassification', HieraForImageClassification]], + ['convnext', ['ConvNextForImageClassification', ConvNextForImageClassification]], + ['convnextv2', ['ConvNextV2ForImageClassification', ConvNextV2ForImageClassification]], + ['dinov2', ['Dinov2ForImageClassification', Dinov2ForImageClassification]], + ['dinov2_with_registers', ['Dinov2WithRegistersForImageClassification', Dinov2WithRegistersForImageClassification]], + ['resnet', ['ResNetForImageClassification', ResNetForImageClassification]], + ['swin', ['SwinForImageClassification', SwinForImageClassification]], + ['segformer', ['SegformerForImageClassification', SegformerForImageClassification]], + ['efficientnet', ['EfficientNetForImageClassification', EfficientNetForImageClassification]], + ['mobilenet_v1', ['MobileNetV1ForImageClassification', MobileNetV1ForImageClassification]], + ['mobilenet_v2', ['MobileNetV2ForImageClassification', MobileNetV2ForImageClassification]], + ['mobilenet_v3', ['MobileNetV3ForImageClassification', MobileNetV3ForImageClassification]], + ['mobilenet_v4', ['MobileNetV4ForImageClassification', MobileNetV4ForImageClassification]], +]); + +const MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES = new Map([ + ['detr', ['DetrForObjectDetection', DetrForObjectDetection]], + ['rt_detr', ['RTDetrForObjectDetection', RTDetrForObjectDetection]], + ['rt_detr_v2', ['RTDetrV2ForObjectDetection', RTDetrV2ForObjectDetection]], + ['rf_detr', ['RFDetrForObjectDetection', RFDetrForObjectDetection]], + ['d_fine', ['DFineForObjectDetection', DFineForObjectDetection]], + ['table-transformer', ['TableTransformerForObjectDetection', TableTransformerForObjectDetection]], + ['yolos', ['YolosForObjectDetection', YolosForObjectDetection]], +]); + +const MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING_NAMES = new Map([ + ['owlvit', ['OwlViTForObjectDetection', OwlViTForObjectDetection]], + ['owlv2', ['Owlv2ForObjectDetection', Owlv2ForObjectDetection]], + ['grounding-dino', ['GroundingDinoForObjectDetection', GroundingDinoForObjectDetection]], +]); + +const MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES = new Map([ + // TODO: Do not add new models here + ['detr', ['DetrForSegmentation', DetrForSegmentation]], + ['clipseg', ['CLIPSegForImageSegmentation', CLIPSegForImageSegmentation]], +]); + +const MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING_NAMES = new Map([ + ['segformer', ['SegformerForSemanticSegmentation', SegformerForSemanticSegmentation]], + ['sapiens', ['SapiensForSemanticSegmentation', SapiensForSemanticSegmentation]], + + ['swin', ['SwinForSemanticSegmentation', SwinForSemanticSegmentation]], + ['mobilenet_v1', ['MobileNetV1ForSemanticSegmentation', MobileNetV1ForSemanticSegmentation]], + ['mobilenet_v2', ['MobileNetV2ForSemanticSegmentation', MobileNetV2ForSemanticSegmentation]], + ['mobilenet_v3', ['MobileNetV3ForSemanticSegmentation', MobileNetV3ForSemanticSegmentation]], + ['mobilenet_v4', ['MobileNetV4ForSemanticSegmentation', MobileNetV4ForSemanticSegmentation]], +]); + +const MODEL_FOR_UNIVERSAL_SEGMENTATION_MAPPING_NAMES = new Map([ + ['detr', ['DetrForSegmentation', DetrForSegmentation]], + ['maskformer', ['MaskFormerForInstanceSegmentation', MaskFormerForInstanceSegmentation]], +]); + +const MODEL_FOR_MASK_GENERATION_MAPPING_NAMES = new Map([ + ['sam', ['SamModel', SamModel]], +]); + +const MODEL_FOR_CTC_MAPPING_NAMES = new Map([ + ['wav2vec2', ['Wav2Vec2ForCTC', Wav2Vec2ForCTC]], + ['wav2vec2-bert', ['Wav2Vec2BertForCTC', Wav2Vec2BertForCTC]], + ['unispeech', ['UniSpeechForCTC', UniSpeechForCTC]], + ['unispeech-sat', ['UniSpeechSatForCTC', UniSpeechSatForCTC]], + ['wavlm', ['WavLMForCTC', WavLMForCTC]], + ['hubert', ['HubertForCTC', HubertForCTC]], +]); + +const MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES = new Map([ + ['wav2vec2', ['Wav2Vec2ForSequenceClassification', Wav2Vec2ForSequenceClassification]], + ['wav2vec2-bert', ['Wav2Vec2BertForSequenceClassification', Wav2Vec2BertForSequenceClassification]], + ['unispeech', ['UniSpeechForSequenceClassification', UniSpeechForSequenceClassification]], + ['unispeech-sat', ['UniSpeechSatForSequenceClassification', UniSpeechSatForSequenceClassification]], + ['wavlm', ['WavLMForSequenceClassification', WavLMForSequenceClassification]], + ['hubert', ['HubertForSequenceClassification', HubertForSequenceClassification]], + ['audio-spectrogram-transformer', ['ASTForAudioClassification', ASTForAudioClassification]], +]); + +const MODEL_FOR_AUDIO_XVECTOR_MAPPING_NAMES = new Map([ + ['wavlm', ['WavLMForXVector', WavLMForXVector]], +]); + +const MODEL_FOR_AUDIO_FRAME_CLASSIFICATION_MAPPING_NAMES = new Map([ + ['unispeech-sat', ['UniSpeechSatForAudioFrameClassification', UniSpeechSatForAudioFrameClassification]], + ['wavlm', ['WavLMForAudioFrameClassification', WavLMForAudioFrameClassification]], + ['wav2vec2', ['Wav2Vec2ForAudioFrameClassification', Wav2Vec2ForAudioFrameClassification]], + ['pyannote', ['PyAnnoteForAudioFrameClassification', PyAnnoteForAudioFrameClassification]], +]); + +const MODEL_FOR_IMAGE_MATTING_MAPPING_NAMES = new Map([ + ['vitmatte', ['VitMatteForImageMatting', VitMatteForImageMatting]], +]); + +const MODEL_FOR_TIME_SERIES_PREDICTION_MAPPING_NAMES = new Map([ + ['patchtst', ['PatchTSTForPrediction', PatchTSTForPrediction]], + ['patchtsmixer', ['PatchTSMixerForPrediction', PatchTSMixerForPrediction]], +]) + +const MODEL_FOR_IMAGE_TO_IMAGE_MAPPING_NAMES = new Map([ + ['swin2sr', ['Swin2SRForImageSuperResolution', Swin2SRForImageSuperResolution]], +]) + +const MODEL_FOR_DEPTH_ESTIMATION_MAPPING_NAMES = new Map([ + ['dpt', ['DPTForDepthEstimation', DPTForDepthEstimation]], + ['depth_anything', ['DepthAnythingForDepthEstimation', DepthAnythingForDepthEstimation]], + ['glpn', ['GLPNForDepthEstimation', GLPNForDepthEstimation]], + ['sapiens', ['SapiensForDepthEstimation', SapiensForDepthEstimation]], + ['depth_pro', ['DepthProForDepthEstimation', DepthProForDepthEstimation]], + ['metric3d', ['Metric3DForDepthEstimation', Metric3DForDepthEstimation]], + ['metric3dv2', ['Metric3Dv2ForDepthEstimation', Metric3Dv2ForDepthEstimation]], +]) + +const MODEL_FOR_NORMAL_ESTIMATION_MAPPING_NAMES = new Map([ + ['sapiens', ['SapiensForNormalEstimation', SapiensForNormalEstimation]], +]) + +const MODEL_FOR_POSE_ESTIMATION_MAPPING_NAMES = new Map([ + ['vitpose', ['VitPoseForPoseEstimation', VitPoseForPoseEstimation]], +]) + +// NOTE: This is custom to Transformers.js, and is necessary because certain models +// (e.g., CLIP) are split into vision and text components +const MODEL_FOR_IMAGE_FEATURE_EXTRACTION_MAPPING_NAMES = new Map([ + ['clip', ['CLIPVisionModelWithProjection', CLIPVisionModelWithProjection]], + ['siglip', ['SiglipVisionModel', SiglipVisionModel]], + ['jina_clip', ['JinaCLIPVisionModel', JinaCLIPVisionModel]], +]) + +const MODEL_CLASS_TYPE_MAPPING = [ + // MODEL_MAPPING_NAMES: + [MODEL_MAPPING_NAMES_ENCODER_ONLY, MODEL_TYPES.EncoderOnly], + [MODEL_MAPPING_NAMES_ENCODER_DECODER, MODEL_TYPES.EncoderDecoder], + [MODEL_MAPPING_NAMES_DECODER_ONLY, MODEL_TYPES.DecoderOnly], + [MODEL_MAPPING_NAMES_AUTO_ENCODER, MODEL_TYPES.AutoEncoder], + + [MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES, MODEL_TYPES.Seq2Seq], + [MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES, MODEL_TYPES.Seq2Seq], + [MODEL_FOR_CAUSAL_LM_MAPPING_NAMES, MODEL_TYPES.DecoderOnly], + [MODEL_FOR_MULTIMODALITY_MAPPING_NAMES, MODEL_TYPES.MultiModality], + [MODEL_FOR_MASKED_LM_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_VISION_2_SEQ_MAPPING_NAMES, MODEL_TYPES.Vision2Seq], + [MODEL_FOR_IMAGE_TEXT_TO_TEXT_MAPPING_NAMES, MODEL_TYPES.ImageTextToText], + [MODEL_FOR_AUDIO_TEXT_TO_TEXT_MAPPING_NAMES, MODEL_TYPES.AudioTextToText], + [MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_UNIVERSAL_SEGMENTATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_IMAGE_MATTING_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_TIME_SERIES_PREDICTION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_IMAGE_TO_IMAGE_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_DEPTH_ESTIMATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_NORMAL_ESTIMATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_POSE_ESTIMATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_MASK_GENERATION_MAPPING_NAMES, MODEL_TYPES.MaskGeneration], + [MODEL_FOR_CTC_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_TEXT_TO_SPECTROGRAM_MAPPING_NAMES, MODEL_TYPES.Seq2Seq], + [MODEL_FOR_TEXT_TO_WAVEFORM_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_AUDIO_XVECTOR_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_AUDIO_FRAME_CLASSIFICATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + + // Custom: + [MODEL_FOR_IMAGE_FEATURE_EXTRACTION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], +]; + +for (const [mappings, type] of MODEL_CLASS_TYPE_MAPPING) { + // @ts-ignore + for (const [name, model] of mappings.values()) { + MODEL_TYPE_MAPPING.set(name, type); + MODEL_CLASS_TO_NAME_MAPPING.set(model, name); + MODEL_NAME_TO_CLASS_MAPPING.set(name, model); + } +} + +const CUSTOM_MAPPING = [ + // OVERRIDE: + // TODO: Refactor to allow class to specify model + ['MusicgenForConditionalGeneration', MusicgenForConditionalGeneration, MODEL_TYPES.Musicgen], + ['Phi3VForCausalLM', Phi3VForCausalLM, MODEL_TYPES.Phi3V], + + ['CLIPTextModelWithProjection', CLIPTextModelWithProjection, MODEL_TYPES.EncoderOnly], + ['SiglipTextModel', SiglipTextModel, MODEL_TYPES.EncoderOnly], + ['JinaCLIPTextModel', JinaCLIPTextModel, MODEL_TYPES.EncoderOnly], + ['ClapTextModelWithProjection', ClapTextModelWithProjection, MODEL_TYPES.EncoderOnly], + ['ClapAudioModelWithProjection', ClapAudioModelWithProjection, MODEL_TYPES.EncoderOnly], + + ['DacEncoderModel', DacEncoderModel, MODEL_TYPES.EncoderOnly], + ['DacDecoderModel', DacDecoderModel, MODEL_TYPES.EncoderOnly], + ['MimiEncoderModel', MimiEncoderModel, MODEL_TYPES.EncoderOnly], + ['MimiDecoderModel', MimiDecoderModel, MODEL_TYPES.EncoderOnly], + ['SnacEncoderModel', SnacEncoderModel, MODEL_TYPES.EncoderOnly], + ['SnacDecoderModel', SnacDecoderModel, MODEL_TYPES.EncoderOnly], +] +for (const [name, model, type] of CUSTOM_MAPPING) { + MODEL_TYPE_MAPPING.set(name, type); + MODEL_CLASS_TO_NAME_MAPPING.set(model, name); + MODEL_NAME_TO_CLASS_MAPPING.set(name, model); +} + +const CUSTOM_ARCHITECTURES = new Map([ + ['modnet', MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES], + ['birefnet', MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES], + ['isnet', MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES], + ['ben', MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES], +]); +for (const [name, mapping] of CUSTOM_ARCHITECTURES.entries()) { + mapping.set(name, ['PreTrainedModel', PreTrainedModel]) + MODEL_TYPE_MAPPING.set(name, MODEL_TYPES.EncoderOnly); + MODEL_CLASS_TO_NAME_MAPPING.set(PreTrainedModel, name); + MODEL_NAME_TO_CLASS_MAPPING.set(name, PreTrainedModel); +} + + +/** + * Helper class which is used to instantiate pretrained models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModel.from_pretrained('Xenova/bert-base-uncased'); + */ +class AutoModel extends PretrainedMixin { + /** @type {Map[]} */ + // @ts-ignore + static MODEL_CLASS_MAPPINGS = MODEL_CLASS_TYPE_MAPPING.map(x => x[0]); + static BASE_IF_FAIL = true; +} + +/** + * Helper class which is used to instantiate pretrained sequence classification models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForSequenceClassification.from_pretrained('Xenova/distilbert-base-uncased-finetuned-sst-2-english'); + */ +class AutoModelForSequenceClassification extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained token classification models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForTokenClassification.from_pretrained('Xenova/distilbert-base-multilingual-cased-ner-hrl'); + */ +class AutoModelForTokenClassification extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained sequence-to-sequence models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForSeq2SeqLM.from_pretrained('Xenova/t5-small'); + */ +class AutoModelForSeq2SeqLM extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained sequence-to-sequence speech-to-text models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForSpeechSeq2Seq.from_pretrained('openai/whisper-tiny.en'); + */ +class AutoModelForSpeechSeq2Seq extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained sequence-to-sequence text-to-spectrogram models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForTextToSpectrogram.from_pretrained('microsoft/speecht5_tts'); + */ +class AutoModelForTextToSpectrogram extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_TEXT_TO_SPECTROGRAM_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained text-to-waveform models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForTextToSpectrogram.from_pretrained('facebook/mms-tts-eng'); + */ +class AutoModelForTextToWaveform extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_TEXT_TO_WAVEFORM_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained causal language models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForCausalLM.from_pretrained('Xenova/gpt2'); + */ +class AutoModelForCausalLM extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_CAUSAL_LM_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained masked language models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForMaskedLM.from_pretrained('Xenova/bert-base-uncased'); + */ +class AutoModelForMaskedLM extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_MASKED_LM_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained question answering models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForQuestionAnswering.from_pretrained('Xenova/distilbert-base-cased-distilled-squad'); + */ +class AutoModelForQuestionAnswering extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained vision-to-sequence models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForVision2Seq.from_pretrained('Xenova/vit-gpt2-image-captioning'); + */ +class AutoModelForVision2Seq extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_VISION_2_SEQ_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained image classification models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForImageClassification.from_pretrained('Xenova/vit-base-patch16-224'); + */ +class AutoModelForImageClassification extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained image segmentation models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForImageSegmentation.from_pretrained('Xenova/detr-resnet-50-panoptic'); + */ +class AutoModelForImageSegmentation extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained image segmentation models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForSemanticSegmentation.from_pretrained('nvidia/segformer-b3-finetuned-cityscapes-1024-1024'); + */ +class AutoModelForSemanticSegmentation extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained universal image segmentation models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForUniversalSegmentation.from_pretrained('hf-internal-testing/tiny-random-MaskFormerForInstanceSegmentation'); + */ +class AutoModelForUniversalSegmentation extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_UNIVERSAL_SEGMENTATION_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained object detection models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForObjectDetection.from_pretrained('Xenova/detr-resnet-50'); + */ +class AutoModelForObjectDetection extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES]; +} + +class AutoModelForZeroShotObjectDetection extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING_NAMES]; +} + + +/** + * Helper class which is used to instantiate pretrained mask generation models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForMaskGeneration.from_pretrained('Xenova/sam-vit-base'); + */ +class AutoModelForMaskGeneration extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_MASK_GENERATION_MAPPING_NAMES]; +} + +class AutoModelForCTC extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_CTC_MAPPING_NAMES]; +} + +class AutoModelForAudioClassification extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES]; +} + +class AutoModelForXVector extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_AUDIO_XVECTOR_MAPPING_NAMES]; +} + +class AutoModelForAudioFrameClassification extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_AUDIO_FRAME_CLASSIFICATION_MAPPING_NAMES]; +} + +class AutoModelForDocumentQuestionAnswering extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING_NAMES]; +} + +class AutoModelForImageMatting extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_IMAGE_MATTING_MAPPING_NAMES]; +} + +class AutoModelForImageToImage extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_IMAGE_TO_IMAGE_MAPPING_NAMES]; +} + +class AutoModelForDepthEstimation extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_DEPTH_ESTIMATION_MAPPING_NAMES]; +} + +class AutoModelForNormalEstimation extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_NORMAL_ESTIMATION_MAPPING_NAMES]; +} + +class AutoModelForPoseEstimation extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_POSE_ESTIMATION_MAPPING_NAMES]; +} + +class AutoModelForImageFeatureExtraction extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_IMAGE_FEATURE_EXTRACTION_MAPPING_NAMES]; +} + +class AutoModelForImageTextToText extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_IMAGE_TEXT_TO_TEXT_MAPPING_NAMES]; +} + +class AutoModelForAudioTextToText extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_AUDIO_TEXT_TO_TEXT_MAPPING_NAMES]; +} + +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class Seq2SeqLMOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits The output logits of the model. + * @param {Tensor} output.past_key_values An tensor of key/value pairs that represent the previous state of the model. + * @param {Tensor} output.encoder_outputs The output of the encoder in a sequence-to-sequence model. + * @param {Tensor} [output.decoder_attentions] Attentions weights of the decoder, after the attention softmax, used to compute the weighted average in the self-attention heads. + * @param {Tensor} [output.cross_attentions] Attentions weights of the decoder's cross-attention layer, after the attention softmax, used to compute the weighted average in the cross-attention heads. + */ + constructor({ logits, past_key_values, encoder_outputs, decoder_attentions = null, cross_attentions = null }) { + super(); + this.logits = logits; + this.past_key_values = past_key_values; + this.encoder_outputs = encoder_outputs; + this.decoder_attentions = decoder_attentions; + this.cross_attentions = cross_attentions; + } +} + +/** + * Base class for outputs of sentence classification models. + */ +class SequenceClassifierOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits classification (or regression if config.num_labels==1) scores (before SoftMax). + * @param {Record} [output.attentions] Object of `torch.FloatTensor` (one for each layer) of shape `(batch_size, num_heads, sequence_length, sequence_length)`. + * Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads. + */ + constructor({ logits, ...attentions }) { + super(); + this.logits = logits; + const attentions_list = Object.values(attentions); + if (attentions_list.length > 0) { + // Only set attentions if they are not empty + this.attentions = attentions_list; + } + } +} + +/** + * Base class for outputs of XVector models. + */ +class XVectorOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits Classification hidden states before AMSoftmax, of shape `(batch_size, config.xvector_output_dim)`. + * @param {Tensor} output.embeddings Utterance embeddings used for vector similarity-based retrieval, of shape `(batch_size, config.xvector_output_dim)`. + */ + constructor({ logits, embeddings }) { + super(); + this.logits = logits; + this.embeddings = embeddings; + } +} + +/** + * Base class for outputs of token classification models. + */ +class TokenClassifierOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits Classification scores (before SoftMax). + */ + constructor({ logits }) { + super(); + this.logits = logits; + } +} + +/** + * Base class for masked language models outputs. + */ +class MaskedLMOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax). + */ + constructor({ logits }) { + super(); + this.logits = logits; + } +} + +/** + * Base class for outputs of question answering models. + */ +class QuestionAnsweringModelOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.start_logits Span-start scores (before SoftMax). + * @param {Tensor} output.end_logits Span-end scores (before SoftMax). + */ + constructor({ start_logits, end_logits }) { + super(); + this.start_logits = start_logits; + this.end_logits = end_logits; + } +} + + +/** + * Base class for causal language model (or autoregressive) outputs. + */ +class CausalLMOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits Prediction scores of the language modeling head (scores for each vocabulary token before softmax). + */ + constructor({ logits }) { + super(); + this.logits = logits; + } +} + +/** + * Base class for causal language model (or autoregressive) outputs. + */ +class CausalLMOutputWithPast extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits Prediction scores of the language modeling head (scores for each vocabulary token before softmax). + * @param {Tensor} output.past_key_values Contains pre-computed hidden-states (key and values in the self-attention blocks) + * that can be used (see `past_key_values` input) to speed up sequential decoding. + */ + constructor({ logits, past_key_values }) { + super(); + this.logits = logits; + this.past_key_values = past_key_values; + } +} + +class ImageMattingOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.alphas Estimated alpha values, of shape `(batch_size, num_channels, height, width)`. + */ + constructor({ alphas }) { + super(); + this.alphas = alphas; + } +} + +/** + * Describes the outputs for the VITS model. + */ +class VitsModelOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.waveform The final audio waveform predicted by the model, of shape `(batch_size, sequence_length)`. + * @param {Tensor} output.spectrogram The log-mel spectrogram predicted at the output of the flow model. + * This spectrogram is passed to the Hi-Fi GAN decoder model to obtain the final audio waveform. + */ + constructor({ waveform, spectrogram }) { + super(); + this.waveform = waveform; + this.spectrogram = spectrogram; + } +} + + +/***/ }), + +/***/ "./src/models/audio_spectrogram_transformer/feature_extraction_audio_spectrogram_transformer.js": +/*!******************************************************************************************************!*\ + !*** ./src/models/audio_spectrogram_transformer/feature_extraction_audio_spectrogram_transformer.js ***! + \******************************************************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ ASTFeatureExtractor: () => (/* binding */ ASTFeatureExtractor) +/* harmony export */ }); +/* harmony import */ var _base_feature_extraction_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/feature_extraction_utils.js */ "./src/base/feature_extraction_utils.js"); +/* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../../utils/tensor.js */ "./src/utils/tensor.js"); +/* harmony import */ var _utils_audio_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../../utils/audio.js */ "./src/utils/audio.js"); + + + + + +class ASTFeatureExtractor extends _base_feature_extraction_utils_js__WEBPACK_IMPORTED_MODULE_0__.FeatureExtractor { + + constructor(config) { + super(config); + + const sampling_rate = this.config.sampling_rate; + const mel_filters = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_2__.mel_filter_bank)( + 257, // num_frequency_bins + this.config.num_mel_bins, // num_mel_filters + 20, // min_frequency + Math.floor(sampling_rate / 2), // max_frequency + sampling_rate, // sampling_rate + null, // norm + "kaldi", // mel_scale + true, // triangularize_in_mel_space + ); + this.mel_filters = mel_filters; + + this.window = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_2__.window_function)(400, 'hann', { + periodic: false, + }) + + this.mean = this.config.mean; + this.std = this.config.std; + } + + /** + * Computes the log-Mel spectrogram of the provided audio waveform. + * @param {Float32Array|Float64Array} waveform The audio waveform to process. + * @param {number} max_length The maximum number of frames to return. + * @returns {Promise} An object containing the log-Mel spectrogram data as a Float32Array and its dimensions as an array of numbers. + */ + async _extract_fbank_features(waveform, max_length) { + // NOTE: We don't pad/truncate since that is passed in as `max_num_frames` + return (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_2__.spectrogram)( + waveform, + this.window, // window + 400, // frame_length + 160, // hop_length + { + fft_length: 512, + power: 2.0, + center: false, + preemphasis: 0.97, + mel_filters: this.mel_filters, + log_mel: 'log', + mel_floor: 1.192092955078125e-07, + remove_dc_offset: true, + + // Custom + max_num_frames: max_length, + transpose: true, + } + ) + } + + + /** + * Asynchronously extracts features from a given audio using the provided configuration. + * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. + * @returns {Promise<{ input_values: Tensor }>} A Promise resolving to an object containing the extracted input features as a Tensor. + */ + async _call(audio) { + (0,_base_feature_extraction_utils_js__WEBPACK_IMPORTED_MODULE_0__.validate_audio_inputs)(audio, 'ASTFeatureExtractor'); + + const features = await this._extract_fbank_features(audio, this.config.max_length); + if (this.config.do_normalize) { + // Normalize the input audio spectrogram to have mean=0, std=0.5 + const denom = this.std * 2; + const features_data = features.data; + for (let i = 0; i < features_data.length; ++i) { + features_data[i] = (features_data[i] - this.mean) / denom; + } + } + + return { + input_values: features.unsqueeze_(0) + }; + } +} + + +/***/ }), + +/***/ "./src/models/auto/feature_extraction_auto.js": +/*!****************************************************!*\ + !*** ./src/models/auto/feature_extraction_auto.js ***! + \****************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ AutoFeatureExtractor: () => (/* binding */ AutoFeatureExtractor) +/* harmony export */ }); +/* harmony import */ var _utils_constants_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../utils/constants.js */ "./src/utils/constants.js"); +/* harmony import */ var _utils_hub_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../../utils/hub.js */ "./src/utils/hub.js"); +/* harmony import */ var _base_feature_extraction_utils_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../../base/feature_extraction_utils.js */ "./src/base/feature_extraction_utils.js"); +/* harmony import */ var _feature_extractors_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ../feature_extractors.js */ "./src/models/feature_extractors.js"); + + + + + + +class AutoFeatureExtractor { + + /** @type {typeof FeatureExtractor.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options={}) { + + const preprocessorConfig = await (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_1__.getModelJSON)(pretrained_model_name_or_path, _utils_constants_js__WEBPACK_IMPORTED_MODULE_0__.FEATURE_EXTRACTOR_NAME, true, options); + + // Determine feature extractor class + const key = preprocessorConfig.feature_extractor_type; + const feature_extractor_class = _feature_extractors_js__WEBPACK_IMPORTED_MODULE_3__[key]; + + if (!feature_extractor_class) { + throw new Error(`Unknown feature_extractor_type: '${key}'. Please report this at ${_utils_constants_js__WEBPACK_IMPORTED_MODULE_0__.GITHUB_ISSUE_URL}.`); + } + + // Instantiate feature extractor + return new feature_extractor_class(preprocessorConfig); + } +} + + +/***/ }), + +/***/ "./src/models/auto/image_processing_auto.js": +/*!**************************************************!*\ + !*** ./src/models/auto/image_processing_auto.js ***! + \**************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ AutoImageProcessor: () => (/* binding */ AutoImageProcessor) +/* harmony export */ }); +/* harmony import */ var _utils_constants_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../utils/constants.js */ "./src/utils/constants.js"); +/* harmony import */ var _utils_hub_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../../utils/hub.js */ "./src/utils/hub.js"); +/* harmony import */ var _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../../base/image_processors_utils.js */ "./src/base/image_processors_utils.js"); +/* harmony import */ var _image_processors_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ../image_processors.js */ "./src/models/image_processors.js"); + + + + + + +class AutoImageProcessor { + + /** @type {typeof ImageProcessor.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options={}) { + + const preprocessorConfig = await (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_1__.getModelJSON)(pretrained_model_name_or_path, _utils_constants_js__WEBPACK_IMPORTED_MODULE_0__.IMAGE_PROCESSOR_NAME, true, options); + + // Determine image processor class + const key = preprocessorConfig.image_processor_type ?? preprocessorConfig.feature_extractor_type; + let image_processor_class = _image_processors_js__WEBPACK_IMPORTED_MODULE_3__[key]; + + if (!image_processor_class) { + if (key !== undefined) { + // Only log a warning if the class is not found and the key is set. + console.warn(`Image processor type '${key}' not found, assuming base ImageProcessor. Please report this at ${_utils_constants_js__WEBPACK_IMPORTED_MODULE_0__.GITHUB_ISSUE_URL}.`) + } + image_processor_class = _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_2__.ImageProcessor; + } + + // Instantiate image processor + return new image_processor_class(preprocessorConfig); + } +} + + +/***/ }), + +/***/ "./src/models/auto/processing_auto.js": +/*!********************************************!*\ + !*** ./src/models/auto/processing_auto.js ***! + \********************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ AutoProcessor: () => (/* binding */ AutoProcessor) +/* harmony export */ }); +/* harmony import */ var _utils_constants_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../utils/constants.js */ "./src/utils/constants.js"); +/* harmony import */ var _utils_hub_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../../utils/hub.js */ "./src/utils/hub.js"); +/* harmony import */ var _base_processing_utils_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../../base/processing_utils.js */ "./src/base/processing_utils.js"); +/* harmony import */ var _processors_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ../processors.js */ "./src/models/processors.js"); +/* harmony import */ var _image_processors_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ../image_processors.js */ "./src/models/image_processors.js"); +/* harmony import */ var _feature_extractors_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ../feature_extractors.js */ "./src/models/feature_extractors.js"); + + + + + + + + + + +/** + * Helper class which is used to instantiate pretrained processors with the `from_pretrained` function. + * The chosen processor class is determined by the type specified in the processor config. + * + * **Example:** Load a processor using `from_pretrained`. + * ```javascript + * let processor = await AutoProcessor.from_pretrained('openai/whisper-tiny.en'); + * ``` + * + * **Example:** Run an image through a processor. + * ```javascript + * let processor = await AutoProcessor.from_pretrained('Xenova/clip-vit-base-patch16'); + * let image = await RawImage.read('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/football-match.jpg'); + * let image_inputs = await processor(image); + * // { + * // "pixel_values": { + * // "dims": [ 1, 3, 224, 224 ], + * // "type": "float32", + * // "data": Float32Array [ -1.558687686920166, -1.558687686920166, -1.5440893173217773, ... ], + * // "size": 150528 + * // }, + * // "original_sizes": [ + * // [ 533, 800 ] + * // ], + * // "reshaped_input_sizes": [ + * // [ 224, 224 ] + * // ] + * // } + * ``` + */ +class AutoProcessor { + + /** @type {typeof Processor.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options={}) { + + // TODO: first check for processor.json + const preprocessorConfig = await (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_1__.getModelJSON)(pretrained_model_name_or_path, _utils_constants_js__WEBPACK_IMPORTED_MODULE_0__.IMAGE_PROCESSOR_NAME, true, options); + + const { image_processor_type, feature_extractor_type, processor_class } = preprocessorConfig; + if (processor_class && _processors_js__WEBPACK_IMPORTED_MODULE_3__[processor_class]) { + return _processors_js__WEBPACK_IMPORTED_MODULE_3__[processor_class].from_pretrained(pretrained_model_name_or_path, options); + } + + if (!image_processor_type && !feature_extractor_type) { + throw new Error('No `image_processor_type` or `feature_extractor_type` found in the config.'); + } + + const components = {}; + if (image_processor_type) { + const image_processor_class = _image_processors_js__WEBPACK_IMPORTED_MODULE_4__[image_processor_type]; + if (!image_processor_class) { + throw new Error(`Unknown image_processor_type: '${image_processor_type}'.`); + } + components.image_processor = new image_processor_class(preprocessorConfig); + } + + if (feature_extractor_type) { + const image_processor_class = _image_processors_js__WEBPACK_IMPORTED_MODULE_4__[feature_extractor_type]; + if (image_processor_class) { + // Handle legacy case where image processors were specified as feature extractors + components.image_processor = new image_processor_class(preprocessorConfig); + } else { + const feature_extractor_class = _feature_extractors_js__WEBPACK_IMPORTED_MODULE_5__[feature_extractor_type]; + if (!feature_extractor_class) { + throw new Error(`Unknown feature_extractor_type: '${feature_extractor_type}'.`); + } + components.feature_extractor = new feature_extractor_class(preprocessorConfig); + } + } + + const config = {}; + return new _base_processing_utils_js__WEBPACK_IMPORTED_MODULE_2__.Processor(config, components); + } +} + + +/***/ }), + +/***/ "./src/models/beit/image_processing_beit.js": +/*!**************************************************!*\ + !*** ./src/models/beit/image_processing_beit.js ***! + \**************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ BeitFeatureExtractor: () => (/* binding */ BeitFeatureExtractor) +/* harmony export */ }); +/* harmony import */ var _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/image_processors_utils.js */ "./src/base/image_processors_utils.js"); + + +class BeitFeatureExtractor extends _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.ImageProcessor { } + + +/***/ }), + +/***/ "./src/models/bit/image_processing_bit.js": +/*!************************************************!*\ + !*** ./src/models/bit/image_processing_bit.js ***! + \************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ BitImageProcessor: () => (/* binding */ BitImageProcessor) +/* harmony export */ }); +/* harmony import */ var _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/image_processors_utils.js */ "./src/base/image_processors_utils.js"); + + +class BitImageProcessor extends _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.ImageProcessor { } + + +/***/ }), + +/***/ "./src/models/chinese_clip/image_processing_chinese_clip.js": +/*!******************************************************************!*\ + !*** ./src/models/chinese_clip/image_processing_chinese_clip.js ***! + \******************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ ChineseCLIPFeatureExtractor: () => (/* binding */ ChineseCLIPFeatureExtractor) +/* harmony export */ }); +/* harmony import */ var _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/image_processors_utils.js */ "./src/base/image_processors_utils.js"); + + +class ChineseCLIPFeatureExtractor extends _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.ImageProcessor { } + + +/***/ }), + +/***/ "./src/models/clap/feature_extraction_clap.js": +/*!****************************************************!*\ + !*** ./src/models/clap/feature_extraction_clap.js ***! + \****************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ ClapFeatureExtractor: () => (/* binding */ ClapFeatureExtractor) +/* harmony export */ }); +/* harmony import */ var _base_feature_extraction_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/feature_extraction_utils.js */ "./src/base/feature_extraction_utils.js"); +/* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../../utils/tensor.js */ "./src/utils/tensor.js"); +/* harmony import */ var _utils_audio_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../../utils/audio.js */ "./src/utils/audio.js"); + + + + + +class ClapFeatureExtractor extends _base_feature_extraction_utils_js__WEBPACK_IMPORTED_MODULE_0__.FeatureExtractor { + + constructor(config) { + super(config); + + this.mel_filters = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_2__.mel_filter_bank)( + this.config.nb_frequency_bins, // num_frequency_bins + this.config.feature_size, // num_mel_filters + this.config.frequency_min, // min_frequency + this.config.frequency_max, // max_frequency + this.config.sampling_rate, // sampling_rate + null, // norm + "htk", // mel_scale + ); + + this.mel_filters_slaney = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_2__.mel_filter_bank)( + this.config.nb_frequency_bins, // num_frequency_bins + this.config.feature_size, // num_mel_filters + this.config.frequency_min, // min_frequency + this.config.frequency_max, // max_frequency + this.config.sampling_rate, // sampling_rate + "slaney", // norm + "slaney", // mel_scale + ); + + this.window = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_2__.window_function)(this.config.fft_window_size, 'hann') + + } + + + /** + * Extracts the mel spectrogram and prepares it for the mode based on the `truncation` and `padding` arguments. + * + * Four different path are possible: + * - `truncation="fusion"` and the length of the waveform is greater than the max length: the mel spectrogram + * will be computed on the entire audio. 3 random crops and a dowsampled version of the full mel spectrogram + * are then stacked together. They will later be used for `feature_fusion`. + * - `truncation="rand_trunc"` and the length of the waveform is smaller than the max length: the audio is + * padded based on `padding`. + * - `truncation="fusion"` and the length of the waveform is smaller than the max length: the audio is padded + * based on `padding`, and is repeated `4` times. + * - `truncation="rand_trunc"` and the length of the waveform is greater than the max length: the mel + * spectrogram will be computed on a random crop of the waveform. + * + * @param {Float32Array|Float64Array} waveform The input waveform. + * @param {number} max_length The maximum length of the waveform. + * @param {string} truncation The truncation strategy to use. + * @param {string} padding The padding strategy to use. + * @returns {Promise} An object containing the mel spectrogram data as a Float32Array, its dimensions as an array of numbers, and a boolean indicating whether the waveform was longer than the max length. + * @private + */ + async _get_input_mel(waveform, max_length, truncation, padding) { + + /** @type {Tensor} */ + let input_mel; + let longer = false; + const diff = waveform.length - max_length; + if (diff > 0) { + if (truncation === 'rand_trunc') { + longer = true; + const idx = Math.floor(Math.random() * (diff + 1)); + waveform = waveform.subarray(idx, idx + max_length); + + input_mel = await this._extract_fbank_features(waveform, this.mel_filters_slaney, this.config.nb_max_samples); + } else { + // TODO implement fusion strategy + throw new Error(`Truncation strategy "${truncation}" not implemented`) + } + } else { + if (diff < 0) { + let padded = new Float64Array(max_length); // already padded with zeros + padded.set(waveform); + + if (padding === 'repeat') { + for (let i = waveform.length; i < max_length; i += waveform.length) { + padded.set(waveform.subarray(0, Math.min(waveform.length, max_length - i)), i); + } + } else if (padding === 'repeatpad') { + for (let i = waveform.length; i < -diff; i += waveform.length) { + padded.set(waveform, i); + } + } + waveform = padded; + } + + if (truncation === 'fusion') { + throw new Error(`Truncation strategy "${truncation}" not implemented`) + } + + input_mel = await this._extract_fbank_features(waveform, this.mel_filters_slaney, this.config.nb_max_samples); + } + + return input_mel.unsqueeze_(0); + } + + /** + * Compute the log-mel spectrogram of the provided `waveform` using the Hann window. + * In CLAP, two different filter banks are used depending on the truncation pattern: + * - `self.mel_filters`: they correspond to the default parameters of `torchaudio` which can be obtained from + * calling `torchaudio.transforms.MelSpectrogram().mel_scale.fb`. These filters are used when `truncation` + * is set to `"fusion"`. + * - `self.mel_filteres_slaney` : they correspond to the default parameters of `librosa` which used + * `librosa.filters.mel` when computing the mel spectrogram. These filters were only used in the original + * implementation when the truncation mode is not `"fusion"`. + * + * @param {Float32Array|Float64Array} waveform The audio waveform to process. + * @param {number[][]} mel_filters The mel filters to use. + * @param {number} [max_length=null] The maximum number of frames to return. + * @returns {Promise} An object containing the log-Mel spectrogram data as a Float32Array and its dimensions as an array of numbers. + */ + async _extract_fbank_features(waveform, mel_filters, max_length = null) { + // NOTE: We don't pad/truncate since that is passed in as `max_num_frames` + return (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_2__.spectrogram)( + waveform, + this.window, // window + this.config.fft_window_size, // frame_length + this.config.hop_length, // hop_length + { + power: 2.0, + mel_filters, + log_mel: 'dB', + + // Custom + max_num_frames: max_length, + do_pad: false, + transpose: true, + } + ) + } + + + /** + * Asynchronously extracts features from a given audio using the provided configuration. + * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. + * @returns {Promise<{ input_features: Tensor }>} A Promise resolving to an object containing the extracted input features as a Tensor. + */ + async _call(audio, { + max_length = null, + } = {}) { + (0,_base_feature_extraction_utils_js__WEBPACK_IMPORTED_MODULE_0__.validate_audio_inputs)(audio, 'ClapFeatureExtractor'); + + // convert to mel spectrogram, truncate and pad if needed. + const padded_inputs = await this._get_input_mel( + audio, + max_length ?? this.config.nb_max_samples, + this.config.truncation, + this.config.padding, + ); + + return { + input_features: padded_inputs.unsqueeze_(0), + } + } +} + + +/***/ }), + +/***/ "./src/models/clip/image_processing_clip.js": +/*!**************************************************!*\ + !*** ./src/models/clip/image_processing_clip.js ***! + \**************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ CLIPFeatureExtractor: () => (/* binding */ CLIPFeatureExtractor), +/* harmony export */ CLIPImageProcessor: () => (/* binding */ CLIPImageProcessor) +/* harmony export */ }); +/* harmony import */ var _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/image_processors_utils.js */ "./src/base/image_processors_utils.js"); + + +class CLIPImageProcessor extends _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.ImageProcessor { } +class CLIPFeatureExtractor extends CLIPImageProcessor { } + + +/***/ }), + +/***/ "./src/models/convnext/image_processing_convnext.js": +/*!**********************************************************!*\ + !*** ./src/models/convnext/image_processing_convnext.js ***! + \**********************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ ConvNextFeatureExtractor: () => (/* binding */ ConvNextFeatureExtractor), +/* harmony export */ ConvNextImageProcessor: () => (/* binding */ ConvNextImageProcessor) +/* harmony export */ }); +/* harmony import */ var _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/image_processors_utils.js */ "./src/base/image_processors_utils.js"); + + +class ConvNextImageProcessor extends _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.ImageProcessor { + constructor(config) { + super(config); + + /** + * Percentage of the image to crop. Only has an effect if this.size < 384. + */ + // @ts-expect-error TS2339 + this.crop_pct = this.config.crop_pct ?? (224 / 256); + } + + async resize(image) { + const shortest_edge = this.size?.shortest_edge; + if (shortest_edge === undefined) { + throw new Error(`Size dictionary must contain 'shortest_edge' key.`); + } + + if (shortest_edge < 384) { + // maintain same ratio, resizing shortest edge to shortest_edge/crop_pct + const resize_shortest_edge = Math.floor(shortest_edge / this.crop_pct); + + const [newWidth, newHeight] = this.get_resize_output_image_size(image, { + shortest_edge: resize_shortest_edge, + }); + + image = await image.resize(newWidth, newHeight, { + resample: this.resample, + }); + + // then crop to (shortest_edge, shortest_edge) + image = await image.center_crop(shortest_edge, shortest_edge); + } else { + // warping (no cropping) when evaluated at 384 or larger + image = await image.resize(shortest_edge, shortest_edge, { + resample: this.resample, + }); + } + + return image; + } +} +class ConvNextFeatureExtractor extends ConvNextImageProcessor { } + + +/***/ }), + +/***/ "./src/models/dac/feature_extraction_dac.js": +/*!**************************************************!*\ + !*** ./src/models/dac/feature_extraction_dac.js ***! + \**************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ DacFeatureExtractor: () => (/* binding */ DacFeatureExtractor) +/* harmony export */ }); +/* harmony import */ var _encodec_feature_extraction_encodec_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../encodec/feature_extraction_encodec.js */ "./src/models/encodec/feature_extraction_encodec.js"); + + +class DacFeatureExtractor extends _encodec_feature_extraction_encodec_js__WEBPACK_IMPORTED_MODULE_0__.EncodecFeatureExtractor { } + + +/***/ }), + +/***/ "./src/models/deit/image_processing_deit.js": +/*!**************************************************!*\ + !*** ./src/models/deit/image_processing_deit.js ***! + \**************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ DeiTFeatureExtractor: () => (/* binding */ DeiTFeatureExtractor), +/* harmony export */ DeiTImageProcessor: () => (/* binding */ DeiTImageProcessor) +/* harmony export */ }); +/* harmony import */ var _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/image_processors_utils.js */ "./src/base/image_processors_utils.js"); + + +class DeiTImageProcessor extends _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.ImageProcessor { } +class DeiTFeatureExtractor extends DeiTImageProcessor { } + +/***/ }), + +/***/ "./src/models/detr/image_processing_detr.js": +/*!**************************************************!*\ + !*** ./src/models/detr/image_processing_detr.js ***! + \**************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ DetrFeatureExtractor: () => (/* binding */ DetrFeatureExtractor), +/* harmony export */ DetrImageProcessor: () => (/* binding */ DetrImageProcessor) +/* harmony export */ }); +/* harmony import */ var _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/image_processors_utils.js */ "./src/base/image_processors_utils.js"); +/* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../../utils/tensor.js */ "./src/utils/tensor.js"); + + + + + +/** + * @typedef {object} DetrFeatureExtractorResultProps + * @property {import('../../utils/tensor.js').Tensor} pixel_mask + * @typedef {import('../../base/image_processors_utils.js').ImageProcessorResult & DetrFeatureExtractorResultProps} DetrFeatureExtractorResult + */ + +class DetrImageProcessor extends _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.ImageProcessor { + /** + * Calls the feature extraction process on an array of images, preprocesses + * each image, and concatenates the resulting features into a single Tensor. + * @param {import('../../utils/image.js').RawImage[]} images The image(s) to extract features from. + * @returns {Promise} An object containing the concatenated pixel values of the preprocessed images. + */ + async _call(images) { + const result = await super._call(images); + + // TODO support differently-sized images, for now assume all images are the same size. + // TODO support different mask sizes (not just 64x64) + // Currently, just fill pixel mask with 1s + const maskSize = [result.pixel_values.dims[0], 64, 64]; + const pixel_mask = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.full)(maskSize, 1n); + + return { ...result, pixel_mask }; + } + + /** @type {typeof post_process_object_detection} */ + post_process_object_detection(...args) { + return (0,_base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.post_process_object_detection)(...args); + } + + /** @type {typeof post_process_panoptic_segmentation} */ + post_process_panoptic_segmentation(...args) { + return (0,_base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.post_process_panoptic_segmentation)(...args); + } + + /** @type {typeof post_process_instance_segmentation} */ + post_process_instance_segmentation(...args) { + return (0,_base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.post_process_instance_segmentation)(...args); + } +} + +class DetrFeatureExtractor extends DetrImageProcessor { } // NOTE: extends DetrImageProcessor + + +/***/ }), + +/***/ "./src/models/donut/image_processing_donut.js": +/*!****************************************************!*\ + !*** ./src/models/donut/image_processing_donut.js ***! + \****************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ DonutFeatureExtractor: () => (/* binding */ DonutFeatureExtractor), +/* harmony export */ DonutImageProcessor: () => (/* binding */ DonutImageProcessor) +/* harmony export */ }); +/* harmony import */ var _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/image_processors_utils.js */ "./src/base/image_processors_utils.js"); + + +class DonutImageProcessor extends _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.ImageProcessor { + pad_image(pixelData, imgDims, padSize, options = {}) { + const [imageHeight, imageWidth, imageChannels] = imgDims; + + let image_mean = this.image_mean; + if (!Array.isArray(this.image_mean)) { + image_mean = new Array(imageChannels).fill(image_mean); + } + + let image_std = this.image_std; + if (!Array.isArray(image_std)) { + image_std = new Array(imageChannels).fill(image_mean); + } + + const constant_values = image_mean.map((x, i) => - x / image_std[i]); + + return super.pad_image(pixelData, imgDims, padSize, { + center: true, + + // Since normalization is done after padding, we need to use certain constant values to ensure the same behaviour is observed. + // For more information, see https://github.com/huggingface/transformers/blob/main/src/transformers/models/donut/image_processing_donut.py#L433-L451 + constant_values, + ...options, + }); + } +} +class DonutFeatureExtractor extends DonutImageProcessor { } + + +/***/ }), + +/***/ "./src/models/dpt/image_processing_dpt.js": +/*!************************************************!*\ + !*** ./src/models/dpt/image_processing_dpt.js ***! + \************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ DPTFeatureExtractor: () => (/* binding */ DPTFeatureExtractor), +/* harmony export */ DPTImageProcessor: () => (/* binding */ DPTImageProcessor) +/* harmony export */ }); +/* harmony import */ var _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/image_processors_utils.js */ "./src/base/image_processors_utils.js"); + + +class DPTImageProcessor extends _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.ImageProcessor { } +class DPTFeatureExtractor extends DPTImageProcessor { } // NOTE: extends DPTImageProcessor + + +/***/ }), + +/***/ "./src/models/efficientnet/image_processing_efficientnet.js": +/*!******************************************************************!*\ + !*** ./src/models/efficientnet/image_processing_efficientnet.js ***! + \******************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ EfficientNetImageProcessor: () => (/* binding */ EfficientNetImageProcessor) +/* harmony export */ }); +/* harmony import */ var _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/image_processors_utils.js */ "./src/base/image_processors_utils.js"); + + +class EfficientNetImageProcessor extends _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.ImageProcessor { + constructor(config) { + super(config); + // @ts-expect-error TS2339 + this.include_top = this.config.include_top ?? true; + if (this.include_top) { + this.image_std = this.image_std.map(x => x * x); + } + } +} + + +/***/ }), + +/***/ "./src/models/encodec/feature_extraction_encodec.js": +/*!**********************************************************!*\ + !*** ./src/models/encodec/feature_extraction_encodec.js ***! + \**********************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ EncodecFeatureExtractor: () => (/* binding */ EncodecFeatureExtractor) +/* harmony export */ }); +/* harmony import */ var _base_feature_extraction_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/feature_extraction_utils.js */ "./src/base/feature_extraction_utils.js"); +/* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../../utils/tensor.js */ "./src/utils/tensor.js"); + + + + +class EncodecFeatureExtractor extends _base_feature_extraction_utils_js__WEBPACK_IMPORTED_MODULE_0__.FeatureExtractor { + /** + * Asynchronously extracts input values from a given audio using the provided configuration. + * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. + * @returns {Promise<{ input_values: Tensor; }>} The extracted input values. + */ + async _call(audio) { + (0,_base_feature_extraction_utils_js__WEBPACK_IMPORTED_MODULE_0__.validate_audio_inputs)(audio, 'EncodecFeatureExtractor'); + + if (audio instanceof Float64Array) { + audio = new Float32Array(audio); + } + + const num_channels = this.config.feature_size; + if (audio.length % num_channels !== 0) { + throw new Error(`The length of the audio data must be a multiple of the number of channels (${num_channels}).`); + } + + const shape = [ + 1, /* batch_size */ + num_channels, /* num_channels */ + audio.length / num_channels, /* num_samples */ + ]; + return { + input_values: new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor('float32', audio, shape), + }; + } +} + + +/***/ }), + +/***/ "./src/models/feature_extractors.js": +/*!******************************************!*\ + !*** ./src/models/feature_extractors.js ***! + \******************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ ASTFeatureExtractor: () => (/* reexport safe */ _audio_spectrogram_transformer_feature_extraction_audio_spectrogram_transformer_js__WEBPACK_IMPORTED_MODULE_0__.ASTFeatureExtractor), +/* harmony export */ ClapFeatureExtractor: () => (/* reexport safe */ _clap_feature_extraction_clap_js__WEBPACK_IMPORTED_MODULE_2__.ClapFeatureExtractor), +/* harmony export */ DacFeatureExtractor: () => (/* reexport safe */ _dac_feature_extraction_dac_js__WEBPACK_IMPORTED_MODULE_3__.DacFeatureExtractor), +/* harmony export */ EncodecFeatureExtractor: () => (/* reexport safe */ _encodec_feature_extraction_encodec_js__WEBPACK_IMPORTED_MODULE_1__.EncodecFeatureExtractor), +/* harmony export */ ImageFeatureExtractor: () => (/* reexport safe */ _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_12__.ImageProcessor), +/* harmony export */ MoonshineFeatureExtractor: () => (/* reexport safe */ _moonshine_feature_extraction_moonshine_js__WEBPACK_IMPORTED_MODULE_4__.MoonshineFeatureExtractor), +/* harmony export */ PyAnnoteFeatureExtractor: () => (/* reexport safe */ _pyannote_feature_extraction_pyannote_js__WEBPACK_IMPORTED_MODULE_5__.PyAnnoteFeatureExtractor), +/* harmony export */ SeamlessM4TFeatureExtractor: () => (/* reexport safe */ _seamless_m4t_feature_extraction_seamless_m4t_js__WEBPACK_IMPORTED_MODULE_6__.SeamlessM4TFeatureExtractor), +/* harmony export */ SnacFeatureExtractor: () => (/* reexport safe */ _snac_feature_extraction_snac_js__WEBPACK_IMPORTED_MODULE_7__.SnacFeatureExtractor), +/* harmony export */ SpeechT5FeatureExtractor: () => (/* reexport safe */ _speecht5_feature_extraction_speecht5_js__WEBPACK_IMPORTED_MODULE_8__.SpeechT5FeatureExtractor), +/* harmony export */ Wav2Vec2FeatureExtractor: () => (/* reexport safe */ _wav2vec2_feature_extraction_wav2vec2_js__WEBPACK_IMPORTED_MODULE_9__.Wav2Vec2FeatureExtractor), +/* harmony export */ WeSpeakerFeatureExtractor: () => (/* reexport safe */ _wespeaker_feature_extraction_wespeaker_js__WEBPACK_IMPORTED_MODULE_10__.WeSpeakerFeatureExtractor), +/* harmony export */ WhisperFeatureExtractor: () => (/* reexport safe */ _whisper_feature_extraction_whisper_js__WEBPACK_IMPORTED_MODULE_11__.WhisperFeatureExtractor) +/* harmony export */ }); +/* harmony import */ var _audio_spectrogram_transformer_feature_extraction_audio_spectrogram_transformer_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./audio_spectrogram_transformer/feature_extraction_audio_spectrogram_transformer.js */ "./src/models/audio_spectrogram_transformer/feature_extraction_audio_spectrogram_transformer.js"); +/* harmony import */ var _encodec_feature_extraction_encodec_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./encodec/feature_extraction_encodec.js */ "./src/models/encodec/feature_extraction_encodec.js"); +/* harmony import */ var _clap_feature_extraction_clap_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./clap/feature_extraction_clap.js */ "./src/models/clap/feature_extraction_clap.js"); +/* harmony import */ var _dac_feature_extraction_dac_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./dac/feature_extraction_dac.js */ "./src/models/dac/feature_extraction_dac.js"); +/* harmony import */ var _moonshine_feature_extraction_moonshine_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ./moonshine/feature_extraction_moonshine.js */ "./src/models/moonshine/feature_extraction_moonshine.js"); +/* harmony import */ var _pyannote_feature_extraction_pyannote_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ./pyannote/feature_extraction_pyannote.js */ "./src/models/pyannote/feature_extraction_pyannote.js"); +/* harmony import */ var _seamless_m4t_feature_extraction_seamless_m4t_js__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! ./seamless_m4t/feature_extraction_seamless_m4t.js */ "./src/models/seamless_m4t/feature_extraction_seamless_m4t.js"); +/* harmony import */ var _snac_feature_extraction_snac_js__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__(/*! ./snac/feature_extraction_snac.js */ "./src/models/snac/feature_extraction_snac.js"); +/* harmony import */ var _speecht5_feature_extraction_speecht5_js__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__(/*! ./speecht5/feature_extraction_speecht5.js */ "./src/models/speecht5/feature_extraction_speecht5.js"); +/* harmony import */ var _wav2vec2_feature_extraction_wav2vec2_js__WEBPACK_IMPORTED_MODULE_9__ = __webpack_require__(/*! ./wav2vec2/feature_extraction_wav2vec2.js */ "./src/models/wav2vec2/feature_extraction_wav2vec2.js"); +/* harmony import */ var _wespeaker_feature_extraction_wespeaker_js__WEBPACK_IMPORTED_MODULE_10__ = __webpack_require__(/*! ./wespeaker/feature_extraction_wespeaker.js */ "./src/models/wespeaker/feature_extraction_wespeaker.js"); +/* harmony import */ var _whisper_feature_extraction_whisper_js__WEBPACK_IMPORTED_MODULE_11__ = __webpack_require__(/*! ./whisper/feature_extraction_whisper.js */ "./src/models/whisper/feature_extraction_whisper.js"); +/* harmony import */ var _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_12__ = __webpack_require__(/*! ../base/image_processors_utils.js */ "./src/base/image_processors_utils.js"); + + + + + + + + + + + + + + +// For legacy support, ImageFeatureExtractor is an alias for ImageProcessor + + + +/***/ }), + +/***/ "./src/models/florence2/processing_florence2.js": +/*!******************************************************!*\ + !*** ./src/models/florence2/processing_florence2.js ***! + \******************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ Florence2Processor: () => (/* binding */ Florence2Processor) +/* harmony export */ }); +/* harmony import */ var _base_processing_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/processing_utils.js */ "./src/base/processing_utils.js"); +/* harmony import */ var _auto_image_processing_auto_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../auto/image_processing_auto.js */ "./src/models/auto/image_processing_auto.js"); +/* harmony import */ var _tokenizers_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../../tokenizers.js */ "./src/tokenizers.js"); + + + + +class Florence2Processor extends _base_processing_utils_js__WEBPACK_IMPORTED_MODULE_0__.Processor { + static tokenizer_class = _tokenizers_js__WEBPACK_IMPORTED_MODULE_2__.AutoTokenizer + static image_processor_class = _auto_image_processing_auto_js__WEBPACK_IMPORTED_MODULE_1__.AutoImageProcessor + + constructor(config, components) { + super(config, components); + + const { + // @ts-expect-error TS2339 + tasks_answer_post_processing_type, + // @ts-expect-error TS2339 + task_prompts_without_inputs, + // @ts-expect-error TS2339 + task_prompts_with_input, + } = this.image_processor.config; + + /** @type {Map} */ + this.tasks_answer_post_processing_type = new Map(Object.entries(tasks_answer_post_processing_type ?? {})); + + /** @type {Map} */ + this.task_prompts_without_inputs = new Map(Object.entries(task_prompts_without_inputs ?? {})); + + /** @type {Map} */ + this.task_prompts_with_input = new Map(Object.entries(task_prompts_with_input ?? {})); + + this.regexes = { + quad_boxes: /(.+?)/gm, + bboxes: /([^<]+)?/gm, + } + this.size_per_bin = 1000; + } + + /** + * Helper function to construct prompts from input texts + * @param {string|string[]} text + * @returns {string[]} + */ + construct_prompts(text) { + if (typeof text === 'string') { + text = [text]; + } + + const prompts = []; + for (const t of text) { + // 1. fixed task prompts without additional inputs + if (this.task_prompts_without_inputs.has(t)) { + prompts.push(this.task_prompts_without_inputs.get(t)); + } + // 2. task prompts with additional inputs + else { + for (const [task, prompt] of this.task_prompts_with_input) { + if (t.includes(task)) { + prompts.push(prompt.replaceAll('{input}', t).replaceAll(task, '')); + break; + } + } + + // 3. default prompt + if (prompts.length !== text.length) { + prompts.push(t); + } + } + } + return prompts; + } + + /** + * Post-process the output of the model to each of the task outputs. + * @param {string} text The text to post-process. + * @param {string} task The task to post-process the text for. + * @param {[number, number]} image_size The size of the image. height x width. + */ + post_process_generation(text, task, image_size) { + const task_answer_post_processing_type = this.tasks_answer_post_processing_type.get(task) ?? 'pure_text'; + + // remove the special tokens + text = text.replaceAll('', '').replaceAll('', ''); + + let final_answer; + switch (task_answer_post_processing_type) { + case 'pure_text': + final_answer = text; + break; + + case 'description_with_bboxes': + case 'bboxes': + case 'phrase_grounding': + case 'ocr': + const key = task_answer_post_processing_type === 'ocr' ? 'quad_boxes' : 'bboxes'; + const matches = text.matchAll(this.regexes[key]); + const labels = []; + const items = []; + for (const [_, label, ...locations] of matches) { + // Push new label, or duplicate the last label + labels.push(label ? label.trim() : labels.at(-1) ?? ''); + items.push(locations.map((x, i) => + // NOTE: Add 0.5 to use the center position of the bin as the coordinate. + (Number(x) + 0.5) / this.size_per_bin * image_size[i % 2]) + ); + } + final_answer = { labels, [key]: items }; + break; + + default: + throw new Error(`Task "${task}" (of type "${task_answer_post_processing_type}") not yet implemented.`); + } + + return { [task]: final_answer } + } + + // NOTE: images and text are switched from the python version + // `images` is required, `text` is optional + async _call(images, text=null, kwargs = {}) { + + if (!images && !text){ + throw new Error('Either text or images must be provided'); + } + + const image_inputs = await this.image_processor(images, kwargs); + const text_inputs = text ? this.tokenizer(text, kwargs) : {}; + + return { + ...image_inputs, + ...text_inputs, + } + } +} + + +/***/ }), + +/***/ "./src/models/glpn/image_processing_glpn.js": +/*!**************************************************!*\ + !*** ./src/models/glpn/image_processing_glpn.js ***! + \**************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ GLPNFeatureExtractor: () => (/* binding */ GLPNFeatureExtractor) +/* harmony export */ }); +/* harmony import */ var _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/image_processors_utils.js */ "./src/base/image_processors_utils.js"); + + +class GLPNFeatureExtractor extends _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.ImageProcessor { } + + +/***/ }), + +/***/ "./src/models/grounding_dino/image_processing_grounding_dino.js": +/*!**********************************************************************!*\ + !*** ./src/models/grounding_dino/image_processing_grounding_dino.js ***! + \**********************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ GroundingDinoImageProcessor: () => (/* binding */ GroundingDinoImageProcessor) +/* harmony export */ }); +/* harmony import */ var _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/image_processors_utils.js */ "./src/base/image_processors_utils.js"); +/* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../../utils/tensor.js */ "./src/utils/tensor.js"); + + + + + +/** + * @typedef {object} GroundingDinoFeatureExtractorResultProps + * @property {import('../../utils/tensor.js').Tensor} pixel_mask + * @typedef {import('../../base/image_processors_utils.js').ImageProcessorResult & GroundingDinoFeatureExtractorResultProps} GroundingDinoFeatureExtractorResult + */ + +class GroundingDinoImageProcessor extends _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.ImageProcessor { + /** + * Calls the feature extraction process on an array of images, preprocesses + * each image, and concatenates the resulting features into a single Tensor. + * @param {import('../../utils/image.js').RawImage[]} images The image(s) to extract features from. + * @returns {Promise} An object containing the concatenated pixel values of the preprocessed images. + */ + async _call(images) { + const result = await super._call(images); + + const dims = result.pixel_values.dims; + const pixel_mask = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.ones)([dims[0], dims[2], dims[3]]); + + return { ...result, pixel_mask }; + } +} + + +/***/ }), + +/***/ "./src/models/grounding_dino/processing_grounding_dino.js": +/*!****************************************************************!*\ + !*** ./src/models/grounding_dino/processing_grounding_dino.js ***! + \****************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ GroundingDinoProcessor: () => (/* binding */ GroundingDinoProcessor) +/* harmony export */ }); +/* harmony import */ var _base_processing_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/processing_utils.js */ "./src/base/processing_utils.js"); +/* harmony import */ var _auto_image_processing_auto_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../auto/image_processing_auto.js */ "./src/models/auto/image_processing_auto.js"); +/* harmony import */ var _tokenizers_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../../tokenizers.js */ "./src/tokenizers.js"); +/* harmony import */ var _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ../../base/image_processors_utils.js */ "./src/base/image_processors_utils.js"); + + + + + +/** + * Get token ids of phrases from posmaps and input_ids. + * @param {import('../../utils/tensor.js').Tensor} posmaps A boolean tensor of unbatched text-thresholded logits related to the detected bounding boxes of shape `(hidden_size, )`. + * @param {import('../../utils/tensor.js').Tensor} input_ids A tensor of token ids of shape `(sequence_length, )`. + */ +function get_phrases_from_posmap(posmaps, input_ids) { + + const left_idx = 0; + const right_idx = posmaps.dims.at(-1) - 1; + + const posmaps_list = posmaps.tolist(); + posmaps_list.fill(false, 0, left_idx + 1); + posmaps_list.fill(false, right_idx); + + const input_ids_list = input_ids.tolist(); + return posmaps_list + .map((val, idx) => val ? idx : null) + .filter(idx => idx !== null) + .map(i => input_ids_list[i]); +} + +class GroundingDinoProcessor extends _base_processing_utils_js__WEBPACK_IMPORTED_MODULE_0__.Processor { + static tokenizer_class = _tokenizers_js__WEBPACK_IMPORTED_MODULE_2__.AutoTokenizer + static image_processor_class = _auto_image_processing_auto_js__WEBPACK_IMPORTED_MODULE_1__.AutoImageProcessor + + /** + * @typedef {import('../../utils/image.js').RawImage} RawImage + */ + /** + * + * @param {RawImage|RawImage[]|RawImage[][]} images + * @param {string|string[]} text + * @returns {Promise} + */ + async _call(images, text, options = {}) { + + const image_inputs = images ? await this.image_processor(images, options) : {}; + const text_inputs = text ? this.tokenizer(text, options) : {}; + + return { + ...text_inputs, + ...image_inputs, + } + } + post_process_grounded_object_detection(outputs, input_ids, { + box_threshold = 0.25, + text_threshold = 0.25, + target_sizes = null + } = {}) { + const { logits, pred_boxes } = outputs; + const batch_size = logits.dims[0]; + + if (target_sizes !== null && target_sizes.length !== batch_size) { + throw Error("Make sure that you pass in as many target sizes as the batch dimension of the logits") + } + const num_queries = logits.dims.at(1); + + const probs = logits.sigmoid(); // (batch_size, num_queries, 256) + const scores = probs.max(-1).tolist(); // (batch_size, num_queries) + + // Convert to [x0, y0, x1, y1] format + const boxes = pred_boxes.tolist() // (batch_size, num_queries, 4) + .map(batch => batch.map(box => (0,_base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_3__.center_to_corners_format)(box))); + + const results = []; + for (let i = 0; i < batch_size; ++i) { + const target_size = target_sizes !== null ? target_sizes[i] : null; + + // Convert from relative [0, 1] to absolute [0, height] coordinates + if (target_size !== null) { + boxes[i] = boxes[i].map(box => box.map((x, j) => x * target_size[(j + 1) % 2])); + } + + const batch_scores = scores[i]; + const final_scores = []; + const final_phrases = []; + const final_boxes = []; + for (let j = 0; j < num_queries; ++j) { + const score = batch_scores[j]; + if (score <= box_threshold) { + continue; + } + const box = boxes[i][j]; + const prob = probs[i][j]; + + final_scores.push(score); + final_boxes.push(box); + + const phrases = get_phrases_from_posmap(prob.gt(text_threshold), input_ids[i]); + final_phrases.push(phrases); + } + results.push({ scores: final_scores, boxes: final_boxes, labels: this.batch_decode(final_phrases) }); + } + return results; + } +} + + +/***/ }), + +/***/ "./src/models/idefics3/image_processing_idefics3.js": +/*!**********************************************************!*\ + !*** ./src/models/idefics3/image_processing_idefics3.js ***! + \**********************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ Idefics3ImageProcessor: () => (/* binding */ Idefics3ImageProcessor) +/* harmony export */ }); +/* harmony import */ var _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/image_processors_utils.js */ "./src/base/image_processors_utils.js"); +/* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../../utils/tensor.js */ "./src/utils/tensor.js"); + + + + + +class Idefics3ImageProcessor extends _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.ImageProcessor { + constructor(config) { + super(config); + + this.do_image_splitting = config.do_image_splitting ?? true; + this.max_image_size = config.max_image_size; + } + + /** + * @typedef {import('../../utils/image.js').RawImage} RawImage + * @typedef {import('../../utils/tensor.js').Tensor} Tensor + */ + + /** + * Calculate size to resize images to, to be multiples of `vision_encoder_max_size` while preserving the aspect ratio. + * @param {Tensor} pixel_values Tensor of the image to resize. + * @param {number} vision_encoder_max_size Maximum size of the output image. If the image is larger than this size, + * it will be split into patches of this size, and the original image will be concatenated with the patches, resized to max_size. + */ + get_resize_for_vision_encoder(pixel_values, vision_encoder_max_size) { + let [height, width] = pixel_values.dims.slice(-2); + + const aspect_ratio = width / height; + if (width >= height) { + width = Math.ceil(width / vision_encoder_max_size) * vision_encoder_max_size; + height = Math.floor(width / aspect_ratio); + height = Math.ceil(height / vision_encoder_max_size) * vision_encoder_max_size; + } else { + height = Math.ceil(height / vision_encoder_max_size) * vision_encoder_max_size; + width = Math.floor(height * aspect_ratio); + width = Math.ceil(width / vision_encoder_max_size) * vision_encoder_max_size; + } + return { height, width }; + } + + /** @param {RawImage|RawImage[]|RawImage[][]} images */ + async _call(images, { + do_image_splitting = null, + return_row_col_info = false, + } = {}) { + + /** @type {RawImage[][]} */ + let batched_2d_images; + if (!Array.isArray(images)) { + batched_2d_images = [[images]]; + } else { + if (images.length === 0 || !images[0]) { + throw new Error("No images provided."); + } + if (!Array.isArray(images[0])) { + batched_2d_images = [/** @type {RawImage[]} */(images)]; + } else { + batched_2d_images = /** @type {RawImage[][]} */(images); + } + } + + // List of tensors, each with shape [patches, channels, height, width] + let all_pixel_values = []; + let images_list_rows = []; + let images_list_cols = []; + + const original_sizes = []; + const reshaped_input_sizes = []; + for (const image_batch of batched_2d_images) { + + let images_list = await Promise.all(image_batch.map(x => this.preprocess(x))); + + // Original sizes of images + original_sizes.push(...images_list.map(x => x.original_size)); + + // Reshaped sizes of images, before padding or cropping + reshaped_input_sizes.push(...images_list.map(x => x.reshaped_input_size)); + + // Convert images to 4D tensors for easier processing + images_list.forEach(x => x.pixel_values.unsqueeze_(0)); + + const { longest_edge } = this.max_image_size; + + /** @type {Tensor[]} */ + let images_tensor; + if (do_image_splitting ?? this.do_image_splitting) { + let image_rows = new Array(images_list.length); + let image_cols = new Array(images_list.length); + + // We first resize both height and width of each image to the nearest max_image_size multiple, disregarding the aspect ratio + images_tensor = await Promise.all(images_list.map(async (x, i) => { + const new_size = this.get_resize_for_vision_encoder(x.pixel_values, longest_edge); + + const resized = await (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.interpolate_4d)(x.pixel_values, { + size: [new_size.height, new_size.width], + }); + + const { frames, num_splits_h, num_splits_w } = await this.split_image(resized, this.max_image_size); + image_rows[i] = num_splits_h; + image_cols[i] = num_splits_w; + return (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.cat)(frames, 0); + })); + + images_list_rows.push(image_rows); + images_list_cols.push(image_cols); + + } else { + /** @type {[number, number]} */ + const size = [longest_edge, longest_edge]; + images_tensor = await Promise.all( + images_list.map(x => (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.interpolate_4d)(x.pixel_values, { size })) + ); + + images_list_rows.push(new Array(images_list.length).fill(0)); + images_list_cols.push(new Array(images_list.length).fill(0)); + } + + all_pixel_values.push((0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.cat)(images_tensor, 0)); + } + + const batch_size = all_pixel_values.length; + const [n, c, h, w] = all_pixel_values[0].dims; + + // Stack pixel values + let pixel_values; + let pixel_attention_mask; + if (batch_size === 1) { + pixel_values = all_pixel_values[0].unsqueeze_(0); + pixel_attention_mask = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.full)([batch_size, n, h, w], true); + } else { + // Add padding (if necessary) to images with less patches than the maximum number of patches + const max_num_patches = Math.max(...all_pixel_values.map(x => x.dims.at(0))); + + pixel_attention_mask = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.full)([batch_size, max_num_patches, h, w], true); + const pixel_attention_mask_data = pixel_attention_mask.data; + const pixel_attention_mask_stride = max_num_patches * h * w; + for (let i = 0; i < batch_size; ++i) { + const num_patches = all_pixel_values[i].dims[0]; + if (num_patches < max_num_patches) { + all_pixel_values[i] = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.cat)([ + all_pixel_values[i], + (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.full)([max_num_patches - num_patches, c, h, w], 0), + ], 0); + + const start_offset = i * pixel_attention_mask_stride + num_patches * h * w; + const end_offset = (i + 1) * pixel_attention_mask_stride; + + // @ts-ignore + pixel_attention_mask_data.fill(false, start_offset, end_offset); + } + } + pixel_values = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.stack)(all_pixel_values, 0); + } + + return { + pixel_values, + pixel_attention_mask, + + original_sizes, + reshaped_input_sizes, + ...( + return_row_col_info + ? { rows: images_list_rows, cols: images_list_cols } + : {} + ), + } + } + + async split_image(pixel_values, { longest_edge }) { + const max_height = longest_edge; + const max_width = longest_edge; + + const frames = []; + + const [height, width] = pixel_values.dims.slice(-2); + + let num_splits_h = 0, num_splits_w = 0; + + if (height > max_height || width > max_width) { + // Calculate the number of splits + num_splits_h = Math.ceil(height / max_height); + num_splits_w = Math.ceil(width / max_width); + + // Calculate the optimal width and height for the sub-images + const optimal_height = Math.ceil(height / num_splits_h); + const optimal_width = Math.ceil(width / num_splits_w); + + // Iterate through each row and column + for (let r = 0; r < num_splits_h; ++r) { + for (let c = 0; c < num_splits_w; ++c) { + let start_x, start_y, end_x, end_y; + if (r === num_splits_h - 1) { // At bottom + start_y = height - optimal_height; + end_y = height; + } else { + start_y = r * optimal_height; + end_y = (r + 1) * optimal_height; + } + if (c === num_splits_w - 1) { // At right + start_x = width - optimal_width; + end_x = width; + } else { + start_x = c * optimal_width; + end_x = (c + 1) * optimal_width; + } + + const starts = [start_y, start_x]; + const ends = [end_y, end_x]; + + const patch = await (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.slice)(pixel_values, starts, ends, [2, 3]); + frames.push(patch); + } + } + + // Resize the global image to match max dimensions for memory efficiency + const global_image_height = max_height; + const global_image_width = max_width; + + if (height !== global_image_height || width !== global_image_width) { + pixel_values = await (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.interpolate_4d)(pixel_values, { + size: [global_image_height, global_image_width], + }) + } + } + + frames.push(pixel_values); + + return { frames, num_splits_h, num_splits_w }; + } +} + + +/***/ }), + +/***/ "./src/models/idefics3/processing_idefics3.js": +/*!****************************************************!*\ + !*** ./src/models/idefics3/processing_idefics3.js ***! + \****************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ Idefics3Processor: () => (/* binding */ Idefics3Processor) +/* harmony export */ }); +/* harmony import */ var _base_processing_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/processing_utils.js */ "./src/base/processing_utils.js"); +/* harmony import */ var _auto_image_processing_auto_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../auto/image_processing_auto.js */ "./src/models/auto/image_processing_auto.js"); +/* harmony import */ var _tokenizers_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../../tokenizers.js */ "./src/tokenizers.js"); +/* harmony import */ var _utils_image_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ../../utils/image.js */ "./src/utils/image.js"); +/* harmony import */ var _utils_core_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ../../utils/core.js */ "./src/utils/core.js"); + + + + + + + +/** + * Prompt with expanded image tokens for when the image is split into patches. + * @private + */ +function _prompt_split_image(image_seq_len, image_rows, image_cols, fake_token_around_image, image_token, global_img_token) { + let text_split_images = ""; + for (let n_h = 0; n_h < image_rows; ++n_h) { + for (let n_w = 0; n_w < image_cols; ++n_w) { + text_split_images += ( + fake_token_around_image + + `` + + image_token.repeat(image_seq_len) + ); + } + text_split_images += "\n"; + } + + text_split_images += ( + `\n${fake_token_around_image}` + + `${global_img_token}` + + image_token.repeat(image_seq_len) + + `${fake_token_around_image}` + ); + return text_split_images; +} + +/** + * Prompt with expanded image tokens for a single image. + * @private + */ +function _prompt_single_image(image_seq_len, fake_token_around_image, image_token, global_img_token) { + return ( + `${fake_token_around_image}` + + `${global_img_token}` + + image_token.repeat(image_seq_len) + + `${fake_token_around_image}` + ); +} + +function get_image_prompt_string(image_rows, image_cols, image_seq_len, fake_token_around_image, image_token, global_img_token) { + if (image_rows === 0 && image_cols === 0) { + return _prompt_single_image( + image_seq_len, + fake_token_around_image, + image_token, + global_img_token + ); + } + return _prompt_split_image( + image_seq_len, image_rows, image_cols, fake_token_around_image, image_token, global_img_token + ); +} + + +class Idefics3Processor extends _base_processing_utils_js__WEBPACK_IMPORTED_MODULE_0__.Processor { + static image_processor_class = _auto_image_processing_auto_js__WEBPACK_IMPORTED_MODULE_1__.AutoImageProcessor + static tokenizer_class = _tokenizers_js__WEBPACK_IMPORTED_MODULE_2__.AutoTokenizer + static uses_processor_config = true; + + fake_image_token = ""; + image_token = ""; + global_img_token = ""; + + /** + * + * @param {string|string[]} text + * @param {RawImage|RawImage[]|RawImage[][]} images + * @returns {Promise} + */ + async _call(text, images = null, options = {}) { + options.return_row_col_info ??= true; + + let image_inputs; + + if (images) { + image_inputs = await this.image_processor(images, options); + } + + // NOTE: We assume text is present + if (!Array.isArray(text)) { + text = [text]; + } + + const image_rows = image_inputs.rows ?? [new Array(text.length).fill(0)]; + const image_cols = image_inputs.cols ?? [new Array(text.length).fill(0)]; + + const image_seq_len = this.config.image_seq_len; + const n_images_in_text = [] + const prompt_strings = []; + for (let i = 0; i < text.length; ++i) { + const sample = text[i]; + const sample_rows = image_rows[i]; + const sample_cols = image_cols[i]; + + n_images_in_text.push((0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.count)(sample, this.image_token)); + + // Replace the image token with fake tokens around the expanded image token sequence of length `image_seq_len` + const image_prompt_strings = sample_rows.map( + (n_rows, j) => get_image_prompt_string( + n_rows, + sample_cols[j], + image_seq_len, + this.fake_image_token, + this.image_token, + this.global_img_token, + ) + ); + + const split_sample = sample.split(this.image_token); + if (split_sample.length === 0) { + throw new Error("The image token should be present in the text."); + } + + // Place in the image prompt strings where the image tokens are + let new_sample = split_sample[0]; + for (let j = 0; j < image_prompt_strings.length; ++j) { + new_sample += image_prompt_strings[j] + split_sample[j + 1]; + } + prompt_strings.push(new_sample); + } + + const text_inputs = this.tokenizer(prompt_strings); + + return { + ...text_inputs, + ...image_inputs, + } + } +} + + +/***/ }), + +/***/ "./src/models/image_processors.js": +/*!****************************************!*\ + !*** ./src/models/image_processors.js ***! + \****************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ BeitFeatureExtractor: () => (/* reexport safe */ _beit_image_processing_beit_js__WEBPACK_IMPORTED_MODULE_0__.BeitFeatureExtractor), +/* harmony export */ BitImageProcessor: () => (/* reexport safe */ _bit_image_processing_bit_js__WEBPACK_IMPORTED_MODULE_1__.BitImageProcessor), +/* harmony export */ CLIPFeatureExtractor: () => (/* reexport safe */ _clip_image_processing_clip_js__WEBPACK_IMPORTED_MODULE_3__.CLIPFeatureExtractor), +/* harmony export */ CLIPImageProcessor: () => (/* reexport safe */ _clip_image_processing_clip_js__WEBPACK_IMPORTED_MODULE_3__.CLIPImageProcessor), +/* harmony export */ ChineseCLIPFeatureExtractor: () => (/* reexport safe */ _chinese_clip_image_processing_chinese_clip_js__WEBPACK_IMPORTED_MODULE_2__.ChineseCLIPFeatureExtractor), +/* harmony export */ ConvNextFeatureExtractor: () => (/* reexport safe */ _convnext_image_processing_convnext_js__WEBPACK_IMPORTED_MODULE_4__.ConvNextFeatureExtractor), +/* harmony export */ ConvNextImageProcessor: () => (/* reexport safe */ _convnext_image_processing_convnext_js__WEBPACK_IMPORTED_MODULE_4__.ConvNextImageProcessor), +/* harmony export */ DPTFeatureExtractor: () => (/* reexport safe */ _dpt_image_processing_dpt_js__WEBPACK_IMPORTED_MODULE_8__.DPTFeatureExtractor), +/* harmony export */ DPTImageProcessor: () => (/* reexport safe */ _dpt_image_processing_dpt_js__WEBPACK_IMPORTED_MODULE_8__.DPTImageProcessor), +/* harmony export */ DeiTFeatureExtractor: () => (/* reexport safe */ _deit_image_processing_deit_js__WEBPACK_IMPORTED_MODULE_5__.DeiTFeatureExtractor), +/* harmony export */ DeiTImageProcessor: () => (/* reexport safe */ _deit_image_processing_deit_js__WEBPACK_IMPORTED_MODULE_5__.DeiTImageProcessor), +/* harmony export */ DetrFeatureExtractor: () => (/* reexport safe */ _detr_image_processing_detr_js__WEBPACK_IMPORTED_MODULE_6__.DetrFeatureExtractor), +/* harmony export */ DetrImageProcessor: () => (/* reexport safe */ _detr_image_processing_detr_js__WEBPACK_IMPORTED_MODULE_6__.DetrImageProcessor), +/* harmony export */ DonutFeatureExtractor: () => (/* reexport safe */ _donut_image_processing_donut_js__WEBPACK_IMPORTED_MODULE_7__.DonutFeatureExtractor), +/* harmony export */ DonutImageProcessor: () => (/* reexport safe */ _donut_image_processing_donut_js__WEBPACK_IMPORTED_MODULE_7__.DonutImageProcessor), +/* harmony export */ EfficientNetImageProcessor: () => (/* reexport safe */ _efficientnet_image_processing_efficientnet_js__WEBPACK_IMPORTED_MODULE_9__.EfficientNetImageProcessor), +/* harmony export */ GLPNFeatureExtractor: () => (/* reexport safe */ _glpn_image_processing_glpn_js__WEBPACK_IMPORTED_MODULE_10__.GLPNFeatureExtractor), +/* harmony export */ GroundingDinoImageProcessor: () => (/* reexport safe */ _grounding_dino_image_processing_grounding_dino_js__WEBPACK_IMPORTED_MODULE_11__.GroundingDinoImageProcessor), +/* harmony export */ Idefics3ImageProcessor: () => (/* reexport safe */ _idefics3_image_processing_idefics3_js__WEBPACK_IMPORTED_MODULE_12__.Idefics3ImageProcessor), +/* harmony export */ JinaCLIPImageProcessor: () => (/* reexport safe */ _jina_clip_image_processing_jina_clip_js__WEBPACK_IMPORTED_MODULE_14__.JinaCLIPImageProcessor), +/* harmony export */ LlavaOnevisionImageProcessor: () => (/* reexport safe */ _llava_onevision_image_processing_llava_onevision_js__WEBPACK_IMPORTED_MODULE_15__.LlavaOnevisionImageProcessor), +/* harmony export */ Mask2FormerImageProcessor: () => (/* reexport safe */ _mask2former_image_processing_mask2former_js__WEBPACK_IMPORTED_MODULE_16__.Mask2FormerImageProcessor), +/* harmony export */ MaskFormerFeatureExtractor: () => (/* reexport safe */ _maskformer_image_processing_maskformer_js__WEBPACK_IMPORTED_MODULE_17__.MaskFormerFeatureExtractor), +/* harmony export */ MaskFormerImageProcessor: () => (/* reexport safe */ _maskformer_image_processing_maskformer_js__WEBPACK_IMPORTED_MODULE_17__.MaskFormerImageProcessor), +/* harmony export */ MobileNetV1FeatureExtractor: () => (/* reexport safe */ _mobilenet_v1_image_processing_mobilenet_v1_js__WEBPACK_IMPORTED_MODULE_18__.MobileNetV1FeatureExtractor), +/* harmony export */ MobileNetV1ImageProcessor: () => (/* reexport safe */ _mobilenet_v1_image_processing_mobilenet_v1_js__WEBPACK_IMPORTED_MODULE_18__.MobileNetV1ImageProcessor), +/* harmony export */ MobileNetV2FeatureExtractor: () => (/* reexport safe */ _mobilenet_v2_image_processing_mobilenet_v2_js__WEBPACK_IMPORTED_MODULE_19__.MobileNetV2FeatureExtractor), +/* harmony export */ MobileNetV2ImageProcessor: () => (/* reexport safe */ _mobilenet_v2_image_processing_mobilenet_v2_js__WEBPACK_IMPORTED_MODULE_19__.MobileNetV2ImageProcessor), +/* harmony export */ MobileNetV3FeatureExtractor: () => (/* reexport safe */ _mobilenet_v3_image_processing_mobilenet_v3_js__WEBPACK_IMPORTED_MODULE_20__.MobileNetV3FeatureExtractor), +/* harmony export */ MobileNetV3ImageProcessor: () => (/* reexport safe */ _mobilenet_v3_image_processing_mobilenet_v3_js__WEBPACK_IMPORTED_MODULE_20__.MobileNetV3ImageProcessor), +/* harmony export */ MobileNetV4FeatureExtractor: () => (/* reexport safe */ _mobilenet_v4_image_processing_mobilenet_v4_js__WEBPACK_IMPORTED_MODULE_21__.MobileNetV4FeatureExtractor), +/* harmony export */ MobileNetV4ImageProcessor: () => (/* reexport safe */ _mobilenet_v4_image_processing_mobilenet_v4_js__WEBPACK_IMPORTED_MODULE_21__.MobileNetV4ImageProcessor), +/* harmony export */ MobileViTFeatureExtractor: () => (/* reexport safe */ _mobilevit_image_processing_mobilevit_js__WEBPACK_IMPORTED_MODULE_22__.MobileViTFeatureExtractor), +/* harmony export */ MobileViTImageProcessor: () => (/* reexport safe */ _mobilevit_image_processing_mobilevit_js__WEBPACK_IMPORTED_MODULE_22__.MobileViTImageProcessor), +/* harmony export */ NougatImageProcessor: () => (/* reexport safe */ _nougat_image_processing_nougat_js__WEBPACK_IMPORTED_MODULE_23__.NougatImageProcessor), +/* harmony export */ OwlViTFeatureExtractor: () => (/* reexport safe */ _owlvit_image_processing_owlvit_js__WEBPACK_IMPORTED_MODULE_25__.OwlViTFeatureExtractor), +/* harmony export */ OwlViTImageProcessor: () => (/* reexport safe */ _owlvit_image_processing_owlvit_js__WEBPACK_IMPORTED_MODULE_25__.OwlViTImageProcessor), +/* harmony export */ Owlv2ImageProcessor: () => (/* reexport safe */ _owlv2_image_processing_owlv2_js__WEBPACK_IMPORTED_MODULE_24__.Owlv2ImageProcessor), +/* harmony export */ Phi3VImageProcessor: () => (/* reexport safe */ _phi3_v_image_processing_phi3_v_js__WEBPACK_IMPORTED_MODULE_26__.Phi3VImageProcessor), +/* harmony export */ PvtImageProcessor: () => (/* reexport safe */ _pvt_image_processing_pvt_js__WEBPACK_IMPORTED_MODULE_27__.PvtImageProcessor), +/* harmony export */ Qwen2VLImageProcessor: () => (/* reexport safe */ _qwen2_vl_image_processing_qwen2_vl_js__WEBPACK_IMPORTED_MODULE_28__.Qwen2VLImageProcessor), +/* harmony export */ RTDetrImageProcessor: () => (/* reexport safe */ _rt_detr_image_processing_rt_detr_js__WEBPACK_IMPORTED_MODULE_29__.RTDetrImageProcessor), +/* harmony export */ SamImageProcessor: () => (/* reexport safe */ _sam_image_processing_sam_js__WEBPACK_IMPORTED_MODULE_30__.SamImageProcessor), +/* harmony export */ SegformerFeatureExtractor: () => (/* reexport safe */ _segformer_image_processing_segformer_js__WEBPACK_IMPORTED_MODULE_31__.SegformerFeatureExtractor), +/* harmony export */ SegformerImageProcessor: () => (/* reexport safe */ _segformer_image_processing_segformer_js__WEBPACK_IMPORTED_MODULE_31__.SegformerImageProcessor), +/* harmony export */ SiglipImageProcessor: () => (/* reexport safe */ _siglip_image_processing_siglip_js__WEBPACK_IMPORTED_MODULE_32__.SiglipImageProcessor), +/* harmony export */ SmolVLMImageProcessor: () => (/* reexport safe */ _smolvlm_image_processing_smolvlm_js__WEBPACK_IMPORTED_MODULE_33__.SmolVLMImageProcessor), +/* harmony export */ Swin2SRImageProcessor: () => (/* reexport safe */ _swin2sr_image_processing_swin2sr_js__WEBPACK_IMPORTED_MODULE_34__.Swin2SRImageProcessor), +/* harmony export */ VLMImageProcessor: () => (/* reexport safe */ _janus_image_processing_janus_js__WEBPACK_IMPORTED_MODULE_13__.VLMImageProcessor), +/* harmony export */ ViTFeatureExtractor: () => (/* reexport safe */ _vit_image_processing_vit_js__WEBPACK_IMPORTED_MODULE_35__.ViTFeatureExtractor), +/* harmony export */ ViTImageProcessor: () => (/* reexport safe */ _vit_image_processing_vit_js__WEBPACK_IMPORTED_MODULE_35__.ViTImageProcessor), +/* harmony export */ VitMatteImageProcessor: () => (/* reexport safe */ _vitmatte_image_processing_vitmatte_js__WEBPACK_IMPORTED_MODULE_36__.VitMatteImageProcessor), +/* harmony export */ VitPoseImageProcessor: () => (/* reexport safe */ _vitpose_image_processing_vitpose_js__WEBPACK_IMPORTED_MODULE_37__.VitPoseImageProcessor), +/* harmony export */ YolosFeatureExtractor: () => (/* reexport safe */ _yolos_image_processing_yolos_js__WEBPACK_IMPORTED_MODULE_38__.YolosFeatureExtractor), +/* harmony export */ YolosImageProcessor: () => (/* reexport safe */ _yolos_image_processing_yolos_js__WEBPACK_IMPORTED_MODULE_38__.YolosImageProcessor) +/* harmony export */ }); +/* harmony import */ var _beit_image_processing_beit_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./beit/image_processing_beit.js */ "./src/models/beit/image_processing_beit.js"); +/* harmony import */ var _bit_image_processing_bit_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./bit/image_processing_bit.js */ "./src/models/bit/image_processing_bit.js"); +/* harmony import */ var _chinese_clip_image_processing_chinese_clip_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./chinese_clip/image_processing_chinese_clip.js */ "./src/models/chinese_clip/image_processing_chinese_clip.js"); +/* harmony import */ var _clip_image_processing_clip_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./clip/image_processing_clip.js */ "./src/models/clip/image_processing_clip.js"); +/* harmony import */ var _convnext_image_processing_convnext_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ./convnext/image_processing_convnext.js */ "./src/models/convnext/image_processing_convnext.js"); +/* harmony import */ var _deit_image_processing_deit_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ./deit/image_processing_deit.js */ "./src/models/deit/image_processing_deit.js"); +/* harmony import */ var _detr_image_processing_detr_js__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! ./detr/image_processing_detr.js */ "./src/models/detr/image_processing_detr.js"); +/* harmony import */ var _donut_image_processing_donut_js__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__(/*! ./donut/image_processing_donut.js */ "./src/models/donut/image_processing_donut.js"); +/* harmony import */ var _dpt_image_processing_dpt_js__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__(/*! ./dpt/image_processing_dpt.js */ "./src/models/dpt/image_processing_dpt.js"); +/* harmony import */ var _efficientnet_image_processing_efficientnet_js__WEBPACK_IMPORTED_MODULE_9__ = __webpack_require__(/*! ./efficientnet/image_processing_efficientnet.js */ "./src/models/efficientnet/image_processing_efficientnet.js"); +/* harmony import */ var _glpn_image_processing_glpn_js__WEBPACK_IMPORTED_MODULE_10__ = __webpack_require__(/*! ./glpn/image_processing_glpn.js */ "./src/models/glpn/image_processing_glpn.js"); +/* harmony import */ var _grounding_dino_image_processing_grounding_dino_js__WEBPACK_IMPORTED_MODULE_11__ = __webpack_require__(/*! ./grounding_dino/image_processing_grounding_dino.js */ "./src/models/grounding_dino/image_processing_grounding_dino.js"); +/* harmony import */ var _idefics3_image_processing_idefics3_js__WEBPACK_IMPORTED_MODULE_12__ = __webpack_require__(/*! ./idefics3/image_processing_idefics3.js */ "./src/models/idefics3/image_processing_idefics3.js"); +/* harmony import */ var _janus_image_processing_janus_js__WEBPACK_IMPORTED_MODULE_13__ = __webpack_require__(/*! ./janus/image_processing_janus.js */ "./src/models/janus/image_processing_janus.js"); +/* harmony import */ var _jina_clip_image_processing_jina_clip_js__WEBPACK_IMPORTED_MODULE_14__ = __webpack_require__(/*! ./jina_clip/image_processing_jina_clip.js */ "./src/models/jina_clip/image_processing_jina_clip.js"); +/* harmony import */ var _llava_onevision_image_processing_llava_onevision_js__WEBPACK_IMPORTED_MODULE_15__ = __webpack_require__(/*! ./llava_onevision/image_processing_llava_onevision.js */ "./src/models/llava_onevision/image_processing_llava_onevision.js"); +/* harmony import */ var _mask2former_image_processing_mask2former_js__WEBPACK_IMPORTED_MODULE_16__ = __webpack_require__(/*! ./mask2former/image_processing_mask2former.js */ "./src/models/mask2former/image_processing_mask2former.js"); +/* harmony import */ var _maskformer_image_processing_maskformer_js__WEBPACK_IMPORTED_MODULE_17__ = __webpack_require__(/*! ./maskformer/image_processing_maskformer.js */ "./src/models/maskformer/image_processing_maskformer.js"); +/* harmony import */ var _mobilenet_v1_image_processing_mobilenet_v1_js__WEBPACK_IMPORTED_MODULE_18__ = __webpack_require__(/*! ./mobilenet_v1/image_processing_mobilenet_v1.js */ "./src/models/mobilenet_v1/image_processing_mobilenet_v1.js"); +/* harmony import */ var _mobilenet_v2_image_processing_mobilenet_v2_js__WEBPACK_IMPORTED_MODULE_19__ = __webpack_require__(/*! ./mobilenet_v2/image_processing_mobilenet_v2.js */ "./src/models/mobilenet_v2/image_processing_mobilenet_v2.js"); +/* harmony import */ var _mobilenet_v3_image_processing_mobilenet_v3_js__WEBPACK_IMPORTED_MODULE_20__ = __webpack_require__(/*! ./mobilenet_v3/image_processing_mobilenet_v3.js */ "./src/models/mobilenet_v3/image_processing_mobilenet_v3.js"); +/* harmony import */ var _mobilenet_v4_image_processing_mobilenet_v4_js__WEBPACK_IMPORTED_MODULE_21__ = __webpack_require__(/*! ./mobilenet_v4/image_processing_mobilenet_v4.js */ "./src/models/mobilenet_v4/image_processing_mobilenet_v4.js"); +/* harmony import */ var _mobilevit_image_processing_mobilevit_js__WEBPACK_IMPORTED_MODULE_22__ = __webpack_require__(/*! ./mobilevit/image_processing_mobilevit.js */ "./src/models/mobilevit/image_processing_mobilevit.js"); +/* harmony import */ var _nougat_image_processing_nougat_js__WEBPACK_IMPORTED_MODULE_23__ = __webpack_require__(/*! ./nougat/image_processing_nougat.js */ "./src/models/nougat/image_processing_nougat.js"); +/* harmony import */ var _owlv2_image_processing_owlv2_js__WEBPACK_IMPORTED_MODULE_24__ = __webpack_require__(/*! ./owlv2/image_processing_owlv2.js */ "./src/models/owlv2/image_processing_owlv2.js"); +/* harmony import */ var _owlvit_image_processing_owlvit_js__WEBPACK_IMPORTED_MODULE_25__ = __webpack_require__(/*! ./owlvit/image_processing_owlvit.js */ "./src/models/owlvit/image_processing_owlvit.js"); +/* harmony import */ var _phi3_v_image_processing_phi3_v_js__WEBPACK_IMPORTED_MODULE_26__ = __webpack_require__(/*! ./phi3_v/image_processing_phi3_v.js */ "./src/models/phi3_v/image_processing_phi3_v.js"); +/* harmony import */ var _pvt_image_processing_pvt_js__WEBPACK_IMPORTED_MODULE_27__ = __webpack_require__(/*! ./pvt/image_processing_pvt.js */ "./src/models/pvt/image_processing_pvt.js"); +/* harmony import */ var _qwen2_vl_image_processing_qwen2_vl_js__WEBPACK_IMPORTED_MODULE_28__ = __webpack_require__(/*! ./qwen2_vl/image_processing_qwen2_vl.js */ "./src/models/qwen2_vl/image_processing_qwen2_vl.js"); +/* harmony import */ var _rt_detr_image_processing_rt_detr_js__WEBPACK_IMPORTED_MODULE_29__ = __webpack_require__(/*! ./rt_detr/image_processing_rt_detr.js */ "./src/models/rt_detr/image_processing_rt_detr.js"); +/* harmony import */ var _sam_image_processing_sam_js__WEBPACK_IMPORTED_MODULE_30__ = __webpack_require__(/*! ./sam/image_processing_sam.js */ "./src/models/sam/image_processing_sam.js"); +/* harmony import */ var _segformer_image_processing_segformer_js__WEBPACK_IMPORTED_MODULE_31__ = __webpack_require__(/*! ./segformer/image_processing_segformer.js */ "./src/models/segformer/image_processing_segformer.js"); +/* harmony import */ var _siglip_image_processing_siglip_js__WEBPACK_IMPORTED_MODULE_32__ = __webpack_require__(/*! ./siglip/image_processing_siglip.js */ "./src/models/siglip/image_processing_siglip.js"); +/* harmony import */ var _smolvlm_image_processing_smolvlm_js__WEBPACK_IMPORTED_MODULE_33__ = __webpack_require__(/*! ./smolvlm/image_processing_smolvlm.js */ "./src/models/smolvlm/image_processing_smolvlm.js"); +/* harmony import */ var _swin2sr_image_processing_swin2sr_js__WEBPACK_IMPORTED_MODULE_34__ = __webpack_require__(/*! ./swin2sr/image_processing_swin2sr.js */ "./src/models/swin2sr/image_processing_swin2sr.js"); +/* harmony import */ var _vit_image_processing_vit_js__WEBPACK_IMPORTED_MODULE_35__ = __webpack_require__(/*! ./vit/image_processing_vit.js */ "./src/models/vit/image_processing_vit.js"); +/* harmony import */ var _vitmatte_image_processing_vitmatte_js__WEBPACK_IMPORTED_MODULE_36__ = __webpack_require__(/*! ./vitmatte/image_processing_vitmatte.js */ "./src/models/vitmatte/image_processing_vitmatte.js"); +/* harmony import */ var _vitpose_image_processing_vitpose_js__WEBPACK_IMPORTED_MODULE_37__ = __webpack_require__(/*! ./vitpose/image_processing_vitpose.js */ "./src/models/vitpose/image_processing_vitpose.js"); +/* harmony import */ var _yolos_image_processing_yolos_js__WEBPACK_IMPORTED_MODULE_38__ = __webpack_require__(/*! ./yolos/image_processing_yolos.js */ "./src/models/yolos/image_processing_yolos.js"); + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +/***/ }), + +/***/ "./src/models/janus/image_processing_janus.js": +/*!****************************************************!*\ + !*** ./src/models/janus/image_processing_janus.js ***! + \****************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ VLMImageProcessor: () => (/* binding */ VLMImageProcessor) +/* harmony export */ }); +/* harmony import */ var _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/image_processors_utils.js */ "./src/base/image_processors_utils.js"); + + + +class VLMImageProcessor extends _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.ImageProcessor { + constructor(config) { + super({ + do_pad: true, + pad_size: { + width: config.image_size, + height: config.image_size, + }, + ...config, + }); + // @ts-expect-error TS2339 + this.constant_values = this.config.background_color.map(x => x * this.rescale_factor) + } + + pad_image(pixelData, imgDims, padSize, options) { + return super.pad_image(pixelData, imgDims, padSize, { + constant_values: this.constant_values, + center: true, + ...options, + }); + } +} + + +/***/ }), + +/***/ "./src/models/janus/processing_janus.js": +/*!**********************************************!*\ + !*** ./src/models/janus/processing_janus.js ***! + \**********************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ VLChatProcessor: () => (/* binding */ VLChatProcessor) +/* harmony export */ }); +/* harmony import */ var _base_processing_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/processing_utils.js */ "./src/base/processing_utils.js"); +/* harmony import */ var _auto_image_processing_auto_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../auto/image_processing_auto.js */ "./src/models/auto/image_processing_auto.js"); +/* harmony import */ var _tokenizers_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../../tokenizers.js */ "./src/tokenizers.js"); +/* harmony import */ var _utils_core_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ../../utils/core.js */ "./src/utils/core.js"); +/* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ../../utils/tensor.js */ "./src/utils/tensor.js"); +/* harmony import */ var _utils_image_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ../../utils/image.js */ "./src/utils/image.js"); + + + + + + + + +class VLChatProcessor extends _base_processing_utils_js__WEBPACK_IMPORTED_MODULE_0__.Processor { + static image_processor_class = _auto_image_processing_auto_js__WEBPACK_IMPORTED_MODULE_1__.AutoImageProcessor + static tokenizer_class = _tokenizers_js__WEBPACK_IMPORTED_MODULE_2__.AutoTokenizer + static uses_processor_config = true; + + constructor(config, components) { + super(config, components); + + this.image_tag = this.config.image_tag; + this.image_start_tag = this.config.image_start_tag; + this.image_end_tag = this.config.image_end_tag; + this.num_image_tokens = this.config.num_image_tokens; + } + + /** + * @typedef {Object} MultimodalMessageProperties Additional properties for multimodal messages. + * @property {(RawImage | string | URL)[]} [images] The images in the message. + * @typedef {(import('../../tokenizers.js').Message & MultimodalMessageProperties)[]} MultimodalConversation The conversation possibly containing multimodal inputs. + */ + + /** + * @typedef {Object} VLCChatProcessorResult The processed input. + * @property {Tensor} input_ids The input IDs. + * @property {Tensor} attention_mask The attention mask. + * @property {Tensor} images_seq_mask The image sequence mask. + * @property {Tensor} images_emb_mask The image embedding mask. + */ + + /** + * @param {MultimodalConversation} conversation The chat messages to process. + * @param {Object} options Additional options for processing. + * @param {RawImage|RawImage[]} [options.images] The images to process, if not set in the conversation. + * @param {string} [options.chat_template="default"] The chat template to use. + * @returns {Promise} The processed input. + */ + async _call(conversation, { + images = null, + chat_template = "default", + }={}) { + if (!images) { + images = await Promise.all( + conversation + .filter((msg) => msg.images) + .flatMap((msg) => msg.images) + .map((img) => _utils_image_js__WEBPACK_IMPORTED_MODULE_5__.RawImage.read(img)) + ); + } else if (!Array.isArray(images)) { + images = [images]; + } + + const tokenizer = this.tokenizer; + const result = tokenizer.apply_chat_template(conversation, { + tokenize: false, + add_generation_prompt: true, + chat_template, + }); + + const encode = (text) => tokenizer.encode(text, { add_special_tokens: false }); + const parts = (/** @type {string} */(result)) + .split(this.image_tag); + const num_images = parts.length - 1; + if (images.length !== num_images) { + throw new Error(`Number of images provided (${images.length}) does not match number of "${this.image_tag}" image tags (${num_images})`); + } + + const [ + image_placeholder_tag_id, + image_start_tag_id, + image_end_tag_id, + ] = tokenizer.model.convert_tokens_to_ids([ + this.image_tag, + this.image_start_tag, + this.image_end_tag, + ]); + + let input_ids = encode(parts[0]); + let images_seq_mask = new Array(input_ids.length).fill(false); + for (let i = 1; i < parts.length; ++i) { + const placeholder_image_tokens = new Array(this.num_image_tokens).fill(image_placeholder_tag_id); + const tokens = encode(parts[i]); + input_ids = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_3__.mergeArrays)( + input_ids, + [image_start_tag_id], placeholder_image_tokens, [image_end_tag_id], + tokens, + ); + const image_mask = new Array(this.num_image_tokens).fill(true); + images_seq_mask = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_3__.mergeArrays)( + images_seq_mask, + [false], image_mask, [false], + new Array(tokens.length).fill(false), + ); + } + + const dims = [1, input_ids.length]; + const final = { + input_ids: new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor('int64', input_ids, dims), + attention_mask: new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor('int64', new Array(input_ids.length).fill(1), dims), + images_seq_mask: new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor('bool', images_seq_mask, dims), + images_emb_mask: new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( + 'bool', + new Array(num_images * this.num_image_tokens).fill(true), + [1, num_images, this.num_image_tokens], + ), + } + + if (images && images.length > 0) { + const image_inputs = await this.image_processor(images); + // Set the batch_size dimension to 1 + image_inputs.pixel_values.unsqueeze_(0); + return { ...final, ...image_inputs }; + } + + return final; + } +} + + +/***/ }), + +/***/ "./src/models/jina_clip/image_processing_jina_clip.js": +/*!************************************************************!*\ + !*** ./src/models/jina_clip/image_processing_jina_clip.js ***! + \************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ JinaCLIPImageProcessor: () => (/* binding */ JinaCLIPImageProcessor) +/* harmony export */ }); +/* harmony import */ var _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/image_processors_utils.js */ "./src/base/image_processors_utils.js"); + + +class JinaCLIPImageProcessor extends _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.ImageProcessor { + constructor(config) { + // JinaCLIPImageProcessor uses a custom preprocessor_config.json, so we configure it here + const { resize_mode, fill_color, interpolation, size, ...other } = config; + + const new_size = resize_mode === 'squash' + ? { width: size, height: size } + : resize_mode === 'shortest' + ? { shortest_edge: size } + : { longest_edge: size }; + + const resample = interpolation === 'bicubic' ? 3 : 2; + super({ + ...other, + size: new_size, + resample, + do_center_crop: true, + crop_size: size, + do_normalize: true, + }); + } +} + + +/***/ }), + +/***/ "./src/models/jina_clip/processing_jina_clip.js": +/*!******************************************************!*\ + !*** ./src/models/jina_clip/processing_jina_clip.js ***! + \******************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ JinaCLIPProcessor: () => (/* binding */ JinaCLIPProcessor) +/* harmony export */ }); +/* harmony import */ var _base_processing_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/processing_utils.js */ "./src/base/processing_utils.js"); +/* harmony import */ var _auto_image_processing_auto_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../auto/image_processing_auto.js */ "./src/models/auto/image_processing_auto.js"); +/* harmony import */ var _tokenizers_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../../tokenizers.js */ "./src/tokenizers.js"); + + + + + +class JinaCLIPProcessor extends _base_processing_utils_js__WEBPACK_IMPORTED_MODULE_0__.Processor { + static tokenizer_class = _tokenizers_js__WEBPACK_IMPORTED_MODULE_2__.AutoTokenizer + static image_processor_class = _auto_image_processing_auto_js__WEBPACK_IMPORTED_MODULE_1__.AutoImageProcessor + + async _call(text=null, images=null, kwargs = {}) { + + if (!text && !images){ + throw new Error('Either text or images must be provided'); + } + + const text_inputs = text ? this.tokenizer(text, kwargs) : {}; + const image_inputs = images ? await this.image_processor(images, kwargs) : {}; + + return { + ...text_inputs, + ...image_inputs, + } + } +} + + +/***/ }), + +/***/ "./src/models/llava_onevision/image_processing_llava_onevision.js": +/*!************************************************************************!*\ + !*** ./src/models/llava_onevision/image_processing_llava_onevision.js ***! + \************************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ LlavaOnevisionImageProcessor: () => (/* binding */ LlavaOnevisionImageProcessor) +/* harmony export */ }); +/* harmony import */ var _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/image_processors_utils.js */ "./src/base/image_processors_utils.js"); + + +class LlavaOnevisionImageProcessor extends _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.ImageProcessor {} + + +/***/ }), + +/***/ "./src/models/mask2former/image_processing_mask2former.js": +/*!****************************************************************!*\ + !*** ./src/models/mask2former/image_processing_mask2former.js ***! + \****************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ Mask2FormerImageProcessor: () => (/* binding */ Mask2FormerImageProcessor) +/* harmony export */ }); +/* harmony import */ var _maskformer_image_processing_maskformer_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../maskformer/image_processing_maskformer.js */ "./src/models/maskformer/image_processing_maskformer.js"); + + + +// NOTE: extends MaskFormerImageProcessor +class Mask2FormerImageProcessor extends _maskformer_image_processing_maskformer_js__WEBPACK_IMPORTED_MODULE_0__.MaskFormerImageProcessor { } + + +/***/ }), + +/***/ "./src/models/maskformer/image_processing_maskformer.js": +/*!**************************************************************!*\ + !*** ./src/models/maskformer/image_processing_maskformer.js ***! + \**************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ MaskFormerFeatureExtractor: () => (/* binding */ MaskFormerFeatureExtractor), +/* harmony export */ MaskFormerImageProcessor: () => (/* binding */ MaskFormerImageProcessor) +/* harmony export */ }); +/* harmony import */ var _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/image_processors_utils.js */ "./src/base/image_processors_utils.js"); + + +class MaskFormerImageProcessor extends _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.ImageProcessor { + + /** @type {typeof post_process_panoptic_segmentation} */ + post_process_panoptic_segmentation(...args) { + return (0,_base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.post_process_panoptic_segmentation)(...args); + } + /** @type {typeof post_process_instance_segmentation} */ + post_process_instance_segmentation(...args) { + return (0,_base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.post_process_instance_segmentation)(...args); + } +} +class MaskFormerFeatureExtractor extends MaskFormerImageProcessor { } + + +/***/ }), + +/***/ "./src/models/mgp_str/processing_mgp_str.js": +/*!**************************************************!*\ + !*** ./src/models/mgp_str/processing_mgp_str.js ***! + \**************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ MgpstrProcessor: () => (/* binding */ MgpstrProcessor) +/* harmony export */ }); +/* harmony import */ var _base_processing_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/processing_utils.js */ "./src/base/processing_utils.js"); +/* harmony import */ var _auto_image_processing_auto_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../auto/image_processing_auto.js */ "./src/models/auto/image_processing_auto.js"); +/* harmony import */ var _tokenizers_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../../tokenizers.js */ "./src/tokenizers.js"); +/* harmony import */ var _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ../../utils/maths.js */ "./src/utils/maths.js"); + + + + + +const DECODE_TYPE_MAPPING = { + 'char': ['char_decode', 1], + 'bpe': ['bpe_decode', 2], + 'wp': ['wp_decode', 102], +} +class MgpstrProcessor extends _base_processing_utils_js__WEBPACK_IMPORTED_MODULE_0__.Processor { + static tokenizer_class = _tokenizers_js__WEBPACK_IMPORTED_MODULE_2__.AutoTokenizer + static image_processor_class = _auto_image_processing_auto_js__WEBPACK_IMPORTED_MODULE_1__.AutoImageProcessor + + /** + * @returns {import('../../tokenizers.js').MgpstrTokenizer} The character tokenizer. + */ + get char_tokenizer() { + return this.components.char_tokenizer; + } + + /** + * @returns {import('../../tokenizers.js').GPT2Tokenizer} The BPE tokenizer. + */ + get bpe_tokenizer() { + return this.components.bpe_tokenizer; + } + + /** + * @returns {import('../../tokenizers.js').BertTokenizer} The WordPiece tokenizer. + */ + get wp_tokenizer() { + return this.components.wp_tokenizer; + } + + /** + * Helper function to decode the model prediction logits. + * @param {import('../../utils/tensor.js').Tensor} pred_logits Model prediction logits. + * @param {string} format Type of model prediction. Must be one of ['char', 'bpe', 'wp']. + * @returns {[string[], number[]]} The decoded sentences and their confidence scores. + */ + _decode_helper(pred_logits, format) { + if (!DECODE_TYPE_MAPPING.hasOwnProperty(format)) { + throw new Error(`Format ${format} is not supported.`); + } + + const [decoder_name, eos_token] = DECODE_TYPE_MAPPING[format]; + const decoder = this[decoder_name].bind(this); + + const [batch_size, batch_max_length] = pred_logits.dims; + const conf_scores = []; + const all_ids = []; + + /** @type {number[][][]} */ + const pred_logits_list = pred_logits.tolist(); + for (let i = 0; i < batch_size; ++i) { + const logits = pred_logits_list[i]; + const ids = []; + const scores = []; + + // Start and index=1 to skip the first token + for (let j = 1; j < batch_max_length; ++j) { + // NOTE: == to match bigint and number + const [max_prob, max_prob_index] = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.max)((0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.softmax)(logits[j])); + scores.push(max_prob); + if (max_prob_index == eos_token) { + break; + } + ids.push(max_prob_index); + } + + const confidence_score = scores.length > 0 + ? scores.reduce((a, b) => a * b, 1) + : 0; + + all_ids.push(ids); + conf_scores.push(confidence_score); + } + + const decoded = decoder(all_ids); + return [decoded, conf_scores]; + } + + /** + * Convert a list of lists of char token ids into a list of strings by calling char tokenizer. + * @param {number[][]} sequences List of tokenized input ids. + * @returns {string[]} The list of char decoded sentences. + */ + char_decode(sequences) { + return this.char_tokenizer.batch_decode(sequences).map(str => str.replaceAll(' ', '')); + } + + /** + * Convert a list of lists of BPE token ids into a list of strings by calling BPE tokenizer. + * @param {number[][]} sequences List of tokenized input ids. + * @returns {string[]} The list of BPE decoded sentences. + */ + bpe_decode(sequences) { + return this.bpe_tokenizer.batch_decode(sequences) + } + + /** + * Convert a list of lists of word piece token ids into a list of strings by calling word piece tokenizer. + * @param {number[][]} sequences List of tokenized input ids. + * @returns {string[]} The list of wp decoded sentences. + */ + wp_decode(sequences) { + return this.wp_tokenizer.batch_decode(sequences).map(str => str.replaceAll(' ', '')); + } + + /** + * Convert a list of lists of token ids into a list of strings by calling decode. + * @param {import('../../utils/tensor.js').Tensor[]} sequences List of tokenized input ids. + * @returns {{generated_text: string[], scores: number[], char_preds: string[], bpe_preds: string[], wp_preds: string[]}} + * Dictionary of all the outputs of the decoded results. + * - generated_text: The final results after fusion of char, bpe, and wp. + * - scores: The final scores after fusion of char, bpe, and wp. + * - char_preds: The list of character decoded sentences. + * - bpe_preds: The list of BPE decoded sentences. + * - wp_preds: The list of wp decoded sentences. + */ + // @ts-expect-error The type of this method is not compatible with the one + // in the base class. It might be a good idea to fix this. + batch_decode([char_logits, bpe_logits, wp_logits]) { + const [char_preds, char_scores] = this._decode_helper(char_logits, 'char'); + const [bpe_preds, bpe_scores] = this._decode_helper(bpe_logits, 'bpe'); + const [wp_preds, wp_scores] = this._decode_helper(wp_logits, 'wp'); + + const generated_text = []; + const scores = []; + for (let i = 0; i < char_preds.length; ++i) { + const [max_score, max_score_index] = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.max)([char_scores[i], bpe_scores[i], wp_scores[i]]); + generated_text.push([char_preds[i], bpe_preds[i], wp_preds[i]][max_score_index]); + scores.push(max_score); + } + + return { + generated_text, + scores, + char_preds, + bpe_preds, + wp_preds, + } + } + /** @type {typeof Processor.from_pretrained} */ + static async from_pretrained(...args) { + const base = await super.from_pretrained(...args); + + // Load Transformers.js-compatible versions of the BPE and WordPiece tokenizers + const bpe_tokenizer = await _tokenizers_js__WEBPACK_IMPORTED_MODULE_2__.AutoTokenizer.from_pretrained("Xenova/gpt2") // openai-community/gpt2 + const wp_tokenizer = await _tokenizers_js__WEBPACK_IMPORTED_MODULE_2__.AutoTokenizer.from_pretrained("Xenova/bert-base-uncased") // google-bert/bert-base-uncased + + // Update components + base.components = { + image_processor: base.image_processor, + char_tokenizer: base.tokenizer, + bpe_tokenizer: bpe_tokenizer, + wp_tokenizer: wp_tokenizer, + } + return base; + } + + async _call(images, text = null) { + const result = await this.image_processor(images); + + if (text) { + result.labels = this.tokenizer(text).input_ids + } + + return result; + } +} + + +/***/ }), + +/***/ "./src/models/mobilenet_v1/image_processing_mobilenet_v1.js": +/*!******************************************************************!*\ + !*** ./src/models/mobilenet_v1/image_processing_mobilenet_v1.js ***! + \******************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ MobileNetV1FeatureExtractor: () => (/* binding */ MobileNetV1FeatureExtractor), +/* harmony export */ MobileNetV1ImageProcessor: () => (/* binding */ MobileNetV1ImageProcessor) +/* harmony export */ }); +/* harmony import */ var _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/image_processors_utils.js */ "./src/base/image_processors_utils.js"); + + + +class MobileNetV1ImageProcessor extends _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.ImageProcessor { } +class MobileNetV1FeatureExtractor extends MobileNetV1ImageProcessor { } + + +/***/ }), + +/***/ "./src/models/mobilenet_v2/image_processing_mobilenet_v2.js": +/*!******************************************************************!*\ + !*** ./src/models/mobilenet_v2/image_processing_mobilenet_v2.js ***! + \******************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ MobileNetV2FeatureExtractor: () => (/* binding */ MobileNetV2FeatureExtractor), +/* harmony export */ MobileNetV2ImageProcessor: () => (/* binding */ MobileNetV2ImageProcessor) +/* harmony export */ }); +/* harmony import */ var _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/image_processors_utils.js */ "./src/base/image_processors_utils.js"); + + + +class MobileNetV2ImageProcessor extends _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.ImageProcessor { } +class MobileNetV2FeatureExtractor extends MobileNetV2ImageProcessor { } + + +/***/ }), + +/***/ "./src/models/mobilenet_v3/image_processing_mobilenet_v3.js": +/*!******************************************************************!*\ + !*** ./src/models/mobilenet_v3/image_processing_mobilenet_v3.js ***! + \******************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ MobileNetV3FeatureExtractor: () => (/* binding */ MobileNetV3FeatureExtractor), +/* harmony export */ MobileNetV3ImageProcessor: () => (/* binding */ MobileNetV3ImageProcessor) +/* harmony export */ }); +/* harmony import */ var _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/image_processors_utils.js */ "./src/base/image_processors_utils.js"); + + + +class MobileNetV3ImageProcessor extends _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.ImageProcessor { } +class MobileNetV3FeatureExtractor extends MobileNetV3ImageProcessor { } + + +/***/ }), + +/***/ "./src/models/mobilenet_v4/image_processing_mobilenet_v4.js": +/*!******************************************************************!*\ + !*** ./src/models/mobilenet_v4/image_processing_mobilenet_v4.js ***! + \******************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ MobileNetV4FeatureExtractor: () => (/* binding */ MobileNetV4FeatureExtractor), +/* harmony export */ MobileNetV4ImageProcessor: () => (/* binding */ MobileNetV4ImageProcessor) +/* harmony export */ }); +/* harmony import */ var _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/image_processors_utils.js */ "./src/base/image_processors_utils.js"); + + + +class MobileNetV4ImageProcessor extends _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.ImageProcessor { } +class MobileNetV4FeatureExtractor extends MobileNetV4ImageProcessor { } + + +/***/ }), + +/***/ "./src/models/mobilevit/image_processing_mobilevit.js": +/*!************************************************************!*\ + !*** ./src/models/mobilevit/image_processing_mobilevit.js ***! + \************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ MobileViTFeatureExtractor: () => (/* binding */ MobileViTFeatureExtractor), +/* harmony export */ MobileViTImageProcessor: () => (/* binding */ MobileViTImageProcessor) +/* harmony export */ }); +/* harmony import */ var _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/image_processors_utils.js */ "./src/base/image_processors_utils.js"); + + +class MobileViTImageProcessor extends _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.ImageProcessor { } +class MobileViTFeatureExtractor extends MobileViTImageProcessor { } + + +/***/ }), + +/***/ "./src/models/moonshine/feature_extraction_moonshine.js": +/*!**************************************************************!*\ + !*** ./src/models/moonshine/feature_extraction_moonshine.js ***! + \**************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ MoonshineFeatureExtractor: () => (/* binding */ MoonshineFeatureExtractor) +/* harmony export */ }); +/* harmony import */ var _base_feature_extraction_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/feature_extraction_utils.js */ "./src/base/feature_extraction_utils.js"); +/* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../../utils/tensor.js */ "./src/utils/tensor.js"); + + + + +class MoonshineFeatureExtractor extends _base_feature_extraction_utils_js__WEBPACK_IMPORTED_MODULE_0__.FeatureExtractor { + /** + * Asynchronously extracts input values from a given audio using the provided configuration. + * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. + * @returns {Promise<{ input_values: Tensor; }>} The extracted input values. + */ + async _call(audio) { + (0,_base_feature_extraction_utils_js__WEBPACK_IMPORTED_MODULE_0__.validate_audio_inputs)(audio, 'MoonshineFeatureExtractor'); + + if (audio instanceof Float64Array) { + audio = new Float32Array(audio); + } + + const shape = [ + 1, /* batch_size */ + audio.length, /* num_samples */ + ]; + return { + input_values: new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor('float32', audio, shape), + }; + } +} + + +/***/ }), + +/***/ "./src/models/moonshine/processing_moonshine.js": +/*!******************************************************!*\ + !*** ./src/models/moonshine/processing_moonshine.js ***! + \******************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ MoonshineProcessor: () => (/* binding */ MoonshineProcessor) +/* harmony export */ }); +/* harmony import */ var _auto_feature_extraction_auto_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../auto/feature_extraction_auto.js */ "./src/models/auto/feature_extraction_auto.js"); +/* harmony import */ var _tokenizers_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../../tokenizers.js */ "./src/tokenizers.js"); +/* harmony import */ var _base_processing_utils_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../../base/processing_utils.js */ "./src/base/processing_utils.js"); + + + + +/** + * Represents a MoonshineProcessor that extracts features from an audio input. + */ +class MoonshineProcessor extends _base_processing_utils_js__WEBPACK_IMPORTED_MODULE_2__.Processor { + static tokenizer_class = _tokenizers_js__WEBPACK_IMPORTED_MODULE_1__.AutoTokenizer + static feature_extractor_class = _auto_feature_extraction_auto_js__WEBPACK_IMPORTED_MODULE_0__.AutoFeatureExtractor + + /** + * Calls the feature_extractor function with the given audio input. + * @param {any} audio The audio input to extract features from. + * @returns {Promise} A Promise that resolves with the extracted features. + */ + async _call(audio) { + return await this.feature_extractor(audio); + } +} + + +/***/ }), + +/***/ "./src/models/nougat/image_processing_nougat.js": +/*!******************************************************!*\ + !*** ./src/models/nougat/image_processing_nougat.js ***! + \******************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ NougatImageProcessor: () => (/* binding */ NougatImageProcessor) +/* harmony export */ }); +/* harmony import */ var _donut_image_processing_donut_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../donut/image_processing_donut.js */ "./src/models/donut/image_processing_donut.js"); + + + +// NOTE: extends DonutImageProcessor +class NougatImageProcessor extends _donut_image_processing_donut_js__WEBPACK_IMPORTED_MODULE_0__.DonutImageProcessor { } + + +/***/ }), + +/***/ "./src/models/owlv2/image_processing_owlv2.js": +/*!****************************************************!*\ + !*** ./src/models/owlv2/image_processing_owlv2.js ***! + \****************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ Owlv2ImageProcessor: () => (/* binding */ Owlv2ImageProcessor) +/* harmony export */ }); +/* harmony import */ var _owlvit_image_processing_owlvit_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../owlvit/image_processing_owlvit.js */ "./src/models/owlvit/image_processing_owlvit.js"); + + + +// NOTE: extends OwlViTImageProcessor +class Owlv2ImageProcessor extends _owlvit_image_processing_owlvit_js__WEBPACK_IMPORTED_MODULE_0__.OwlViTImageProcessor { } + + +/***/ }), + +/***/ "./src/models/owlvit/image_processing_owlvit.js": +/*!******************************************************!*\ + !*** ./src/models/owlvit/image_processing_owlvit.js ***! + \******************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ OwlViTFeatureExtractor: () => (/* binding */ OwlViTFeatureExtractor), +/* harmony export */ OwlViTImageProcessor: () => (/* binding */ OwlViTImageProcessor) +/* harmony export */ }); +/* harmony import */ var _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/image_processors_utils.js */ "./src/base/image_processors_utils.js"); + + +class OwlViTImageProcessor extends _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.ImageProcessor { + /** @type {typeof post_process_object_detection} */ + post_process_object_detection(...args) { + return (0,_base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.post_process_object_detection)(...args); + } +} +class OwlViTFeatureExtractor extends OwlViTImageProcessor { } + + +/***/ }), + +/***/ "./src/models/owlvit/processing_owlvit.js": +/*!************************************************!*\ + !*** ./src/models/owlvit/processing_owlvit.js ***! + \************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ OwlViTProcessor: () => (/* binding */ OwlViTProcessor) +/* harmony export */ }); +/* harmony import */ var _base_processing_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/processing_utils.js */ "./src/base/processing_utils.js"); +/* harmony import */ var _auto_image_processing_auto_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../auto/image_processing_auto.js */ "./src/models/auto/image_processing_auto.js"); +/* harmony import */ var _tokenizers_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../../tokenizers.js */ "./src/tokenizers.js"); + + + +class OwlViTProcessor extends _base_processing_utils_js__WEBPACK_IMPORTED_MODULE_0__.Processor { + static tokenizer_class = _tokenizers_js__WEBPACK_IMPORTED_MODULE_2__.AutoTokenizer + static image_processor_class = _auto_image_processing_auto_js__WEBPACK_IMPORTED_MODULE_1__.AutoImageProcessor +} + + +/***/ }), + +/***/ "./src/models/paligemma/processing_paligemma.js": +/*!******************************************************!*\ + !*** ./src/models/paligemma/processing_paligemma.js ***! + \******************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ PaliGemmaProcessor: () => (/* binding */ PaliGemmaProcessor) +/* harmony export */ }); +/* harmony import */ var _base_processing_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/processing_utils.js */ "./src/base/processing_utils.js"); +/* harmony import */ var _auto_image_processing_auto_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../auto/image_processing_auto.js */ "./src/models/auto/image_processing_auto.js"); +/* harmony import */ var _tokenizers_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../../tokenizers.js */ "./src/tokenizers.js"); + + + + +const IMAGE_TOKEN = ""; + +function build_string_from_input( + prompt, + bos_token, + image_seq_len, + image_token, + num_images, +) { + return `${image_token.repeat(image_seq_len * num_images)}${bos_token}${prompt}\n` +} + +class PaliGemmaProcessor extends _base_processing_utils_js__WEBPACK_IMPORTED_MODULE_0__.Processor { + static tokenizer_class = _tokenizers_js__WEBPACK_IMPORTED_MODULE_2__.AutoTokenizer + static image_processor_class = _auto_image_processing_auto_js__WEBPACK_IMPORTED_MODULE_1__.AutoImageProcessor + static uses_processor_config = false; + + /** + * @typedef {import('../../utils/image.js').RawImage} RawImage + */ + + // `images` is required, `text` is optional + async _call(/** @type {RawImage|RawImage[]} */ images, text = null, kwargs = {}) { + if (!text) { + console.warn( + "You are using PaliGemma without a text prefix. It will perform as a picture-captioning model." + ) + text = "" + } + + if (!Array.isArray(images)) { + images = [images] + } + + if (!Array.isArray(text)) { + text = [text] + } + + const bos_token = this.tokenizer.bos_token; + // @ts-expect-error TS2339 + const image_seq_length = this.image_processor.config.image_seq_length; + let input_strings; + if (text.some((t) => t.includes(IMAGE_TOKEN))) { + input_strings = text.map( + sample => { + const expanded_sample = sample.replaceAll(IMAGE_TOKEN, IMAGE_TOKEN.repeat(image_seq_length)); + const bos_rfind_index = expanded_sample.lastIndexOf(IMAGE_TOKEN); + const bos_index = bos_rfind_index === -1 ? 0 : bos_rfind_index + IMAGE_TOKEN.length; + return expanded_sample.slice(0, bos_index) + bos_token + expanded_sample.slice(bos_index) + "\n"; + } + ) + } else { + console.warn( + "You are passing both `text` and `images` to `PaliGemmaProcessor`. The processor expects special " + + "image tokens in the text, as many tokens as there are images per each text. It is recommended to " + + "add `` tokens in the very beginning of your text. For this call, we will infer how many images " + + "each text has and add special tokens." + ) + + input_strings = text.map( + sample => build_string_from_input( + sample, + bos_token, + image_seq_length, + IMAGE_TOKEN, + images.length, + ) + ) + } + + const text_inputs = this.tokenizer(input_strings, kwargs); + const image_inputs = await this.image_processor(images, kwargs); + + return { + ...image_inputs, + ...text_inputs, + } + } +} + + +/***/ }), + +/***/ "./src/models/phi3_v/image_processing_phi3_v.js": +/*!******************************************************!*\ + !*** ./src/models/phi3_v/image_processing_phi3_v.js ***! + \******************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ Phi3VImageProcessor: () => (/* binding */ Phi3VImageProcessor) +/* harmony export */ }); +/* harmony import */ var _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/image_processors_utils.js */ "./src/base/image_processors_utils.js"); +/* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../../utils/tensor.js */ "./src/utils/tensor.js"); + + + +const IMAGE_SIZE = 336; +const SLICE_AXES = [2, 3]; // axes to slice on +const { ceil, floor, sqrt } = Math; + +class Phi3VImageProcessor extends _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.ImageProcessor { + constructor(config) { + super({ + ...config, + do_normalize: true, + do_pad: true, + pad_size: 'custom', + do_convert_rgb: true, + do_resize: true, // Smart resizing "hd_transform" + }); + + this._num_crops = config.num_crops; + } + calc_num_image_tokens_from_image_size(width, height) { + // @ts-expect-error + const { num_img_tokens } = this.config; + return floor(((floor((height / IMAGE_SIZE)) * floor((width / IMAGE_SIZE)) + 1) * num_img_tokens) + 1 + (floor(height / IMAGE_SIZE) + 1) * sqrt(num_img_tokens)); + } + + /** @type {ImageProcessor['get_resize_output_image_size']} */ + get_resize_output_image_size(image, size) { + const hd_num = this._num_crops; + const [width, height] = image.size + + let ratio = width / height; + let scale = 1; + + // Calculate the scaling factor + while (scale * Math.ceil(scale / ratio) <= hd_num) { + scale += 1; + } + scale -= 1; + + // Compute the new dimensions + const new_w = Math.floor(scale * 336); + const new_h = Math.floor(new_w / ratio); + + return [new_w, new_h] + } + + + /** @type {ImageProcessor['pad_image']} */ + pad_image(pixelData, imgDims, padSize, options = {}) { + // Phi3V uses a custom padding strategy: + // - Pad to a multiple of 336 + // - Pad with white pixels + const [imageHeight, imageWidth] = imgDims; + const height = IMAGE_SIZE * ceil(imageHeight / IMAGE_SIZE); + const width = IMAGE_SIZE * ceil(imageWidth / IMAGE_SIZE); + + // NOTE: Since padding is done after normalization, we need to fill with the normalized values + const constant_values = [1, 1, 1].map((x, i) => (x - this.image_mean[i]) / this.image_std[i]); + return super.pad_image(pixelData, imgDims, { width, height }, { + center: true, + constant_values, + ...options, + }); + } + + async _call(images, { + num_crops = null, + } = {}) { + // @ts-expect-error + this._num_crops = num_crops ??= this.config.num_crops; + if (num_crops < 4 || sqrt(num_crops) % 1 !== 0) { + throw new Error("num_crops must be a square number >= 4"); + } + + if (!Array.isArray(images)) { + images = [images]; + } + + const num_images = images.length; + const imageData = await Promise.all(images.map(x => this.preprocess(x))); + + const original_sizes = imageData.map(x => x.original_size); + const reshaped_input_sizes = imageData.map(x => x.reshaped_input_size); + + // Process each image in batch + const all_pixel_values = []; + for (const { pixel_values } of imageData) { + pixel_values.unsqueeze_(0); // Easier processing as 4D tensor + + const [height, width] = pixel_values.dims.slice(-2); + + // Global image (Tensor of shape [num_channels, height, width]) + const batch_pixel_values = await (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.interpolate_4d)(pixel_values, { + size: [IMAGE_SIZE, IMAGE_SIZE], + mode: 'bicubic', + }); + + if (num_crops > 0) { + const patches = []; + const sqrt_patches = sqrt(num_crops); + const patch_width = floor(width / sqrt_patches); + const patch_height = floor(height / sqrt_patches); + for (let y = 0; y < sqrt_patches; ++y) { + for (let x = 0; x < sqrt_patches; ++x) { + let start_x, start_y, end_x, end_y; + if (y === sqrt_patches - 1) { // At bottom + start_y = height - patch_height; + end_y = height; + } else { + start_y = y * patch_height; + end_y = (y + 1) * patch_height; + } + if (x === sqrt_patches - 1) { // At right + start_x = width - patch_width; + end_x = width; + } else { + start_x = x * patch_width; + end_x = (x + 1) * patch_width; + } + + const starts = [start_y, start_x]; + const ends = [end_y, end_x]; + const patch = await (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.slice)(pixel_values, starts, ends, SLICE_AXES); + patches.push(patch); + } + } + + const resized_tensors = await (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.interpolate_4d)((0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.cat)(patches, 0), { + size: [IMAGE_SIZE, IMAGE_SIZE], + mode: 'bicubic', + }); // [num_crops, 3, 336, 336] + + // Concatenate the global image with the patches + all_pixel_values.push((0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.cat)([batch_pixel_values, resized_tensors], 0)); + } else { + // Only use the global image + // NOTE: Not currently supported in modelling code + all_pixel_values.push(batch_pixel_values); + } + } + + // [num_images, 1 + num_crops, num_channels=3, height, width] + const pixel_values = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.stack)(all_pixel_values, 0); + + // Calculate padded image sizes + const sizes = reshaped_input_sizes.map(x => x.map(y => IMAGE_SIZE * ceil(y / IMAGE_SIZE))); + + const image_sizes = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor( + 'int64', + sizes.flat(), + [num_images, 2], + ); + + const num_img_tokens = sizes.map( + ([height, width]) => this.calc_num_image_tokens_from_image_size(width, height), + ); + + return { pixel_values, original_sizes, reshaped_input_sizes, image_sizes, num_img_tokens }; + } +} + + +/***/ }), + +/***/ "./src/models/phi3_v/processing_phi3_v.js": +/*!************************************************!*\ + !*** ./src/models/phi3_v/processing_phi3_v.js ***! + \************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ Phi3VProcessor: () => (/* binding */ Phi3VProcessor) +/* harmony export */ }); +/* harmony import */ var _base_processing_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/processing_utils.js */ "./src/base/processing_utils.js"); +/* harmony import */ var _auto_image_processing_auto_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../auto/image_processing_auto.js */ "./src/models/auto/image_processing_auto.js"); +/* harmony import */ var _tokenizers_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../../tokenizers.js */ "./src/tokenizers.js"); +/* harmony import */ var _utils_image_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ../../utils/image.js */ "./src/utils/image.js"); + + + + + +const IMAGE_TOKEN = "<|image|>"; +const IMAGE_TOKEN_PATTERN = /<\|image_\d+\|>/g; + +class Phi3VProcessor extends _base_processing_utils_js__WEBPACK_IMPORTED_MODULE_0__.Processor { + static image_processor_class = _auto_image_processing_auto_js__WEBPACK_IMPORTED_MODULE_1__.AutoImageProcessor + static tokenizer_class = _tokenizers_js__WEBPACK_IMPORTED_MODULE_2__.AutoTokenizer + + /** + * + * @param {string|string[]} text + * @param {RawImage|RawImage[]} images + * @param { { padding?: boolean, truncation?: boolean, num_crops?: number } | undefined } options + * @returns {Promise} + */ + async _call(text, images = null, { + padding = true, + truncation = true, + num_crops = null, + } = {}) { + + if (!Array.isArray(text)) { + text = [text]; + } + + let text_inputs, image_inputs; + if (images) { + image_inputs = await this.image_processor(images, { num_crops }); + const { num_img_tokens } = image_inputs; + + // The original implementation adds a bos_token before the image tokens + // TODO: Check if this affects performance, since it looks like a bug in the original implementation + const prompt_chunks = text.map((t, i) => t.split(IMAGE_TOKEN_PATTERN).join(IMAGE_TOKEN.repeat(num_img_tokens[i]))); + + text_inputs = this.tokenizer(prompt_chunks, { padding, truncation }); + + // The model expects image tokens to be negative, so we negate the image token ids + const image_token_id = this.tokenizer.model.convert_tokens_to_ids([IMAGE_TOKEN])[0]; + text_inputs.input_ids.map_(id => (id == image_token_id) ? -id : id); + } else { + text_inputs = this.tokenizer(text); + } + + return { + ...text_inputs, + ...image_inputs, + } + } +} + + +/***/ }), + +/***/ "./src/models/processors.js": +/*!**********************************!*\ + !*** ./src/models/processors.js ***! + \**********************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ Florence2Processor: () => (/* reexport safe */ _florence2_processing_florence2_js__WEBPACK_IMPORTED_MODULE_0__.Florence2Processor), +/* harmony export */ GroundingDinoProcessor: () => (/* reexport safe */ _grounding_dino_processing_grounding_dino_js__WEBPACK_IMPORTED_MODULE_1__.GroundingDinoProcessor), +/* harmony export */ Idefics3Processor: () => (/* reexport safe */ _idefics3_processing_idefics3_js__WEBPACK_IMPORTED_MODULE_2__.Idefics3Processor), +/* harmony export */ JinaCLIPProcessor: () => (/* reexport safe */ _jina_clip_processing_jina_clip_js__WEBPACK_IMPORTED_MODULE_4__.JinaCLIPProcessor), +/* harmony export */ MgpstrProcessor: () => (/* reexport safe */ _mgp_str_processing_mgp_str_js__WEBPACK_IMPORTED_MODULE_5__.MgpstrProcessor), +/* harmony export */ MoonshineProcessor: () => (/* reexport safe */ _moonshine_processing_moonshine_js__WEBPACK_IMPORTED_MODULE_6__.MoonshineProcessor), +/* harmony export */ OwlViTProcessor: () => (/* reexport safe */ _owlvit_processing_owlvit_js__WEBPACK_IMPORTED_MODULE_7__.OwlViTProcessor), +/* harmony export */ PaliGemmaProcessor: () => (/* reexport safe */ _paligemma_processing_paligemma_js__WEBPACK_IMPORTED_MODULE_9__.PaliGemmaProcessor), +/* harmony export */ Phi3VProcessor: () => (/* reexport safe */ _phi3_v_processing_phi3_v_js__WEBPACK_IMPORTED_MODULE_8__.Phi3VProcessor), +/* harmony export */ PyAnnoteProcessor: () => (/* reexport safe */ _pyannote_processing_pyannote_js__WEBPACK_IMPORTED_MODULE_10__.PyAnnoteProcessor), +/* harmony export */ Qwen2VLProcessor: () => (/* reexport safe */ _qwen2_vl_processing_qwen2_vl_js__WEBPACK_IMPORTED_MODULE_11__.Qwen2VLProcessor), +/* harmony export */ SamProcessor: () => (/* reexport safe */ _sam_processing_sam_js__WEBPACK_IMPORTED_MODULE_12__.SamProcessor), +/* harmony export */ SmolVLMProcessor: () => (/* reexport safe */ _smolvlm_processing_smolvlm_js__WEBPACK_IMPORTED_MODULE_13__.SmolVLMProcessor), +/* harmony export */ SpeechT5Processor: () => (/* reexport safe */ _speecht5_processing_speecht5_js__WEBPACK_IMPORTED_MODULE_14__.SpeechT5Processor), +/* harmony export */ UltravoxProcessor: () => (/* reexport safe */ _ultravox_processing_ultravox_js__WEBPACK_IMPORTED_MODULE_15__.UltravoxProcessor), +/* harmony export */ VLChatProcessor: () => (/* reexport safe */ _janus_processing_janus_js__WEBPACK_IMPORTED_MODULE_3__.VLChatProcessor), +/* harmony export */ Wav2Vec2Processor: () => (/* reexport safe */ _wav2vec2_processing_wav2vec2_js__WEBPACK_IMPORTED_MODULE_16__.Wav2Vec2Processor), +/* harmony export */ Wav2Vec2ProcessorWithLM: () => (/* reexport safe */ _wav2vec2_with_lm_processing_wav2vec2_with_lm_js__WEBPACK_IMPORTED_MODULE_17__.Wav2Vec2ProcessorWithLM), +/* harmony export */ WhisperProcessor: () => (/* reexport safe */ _whisper_processing_whisper_js__WEBPACK_IMPORTED_MODULE_18__.WhisperProcessor) +/* harmony export */ }); +/* harmony import */ var _florence2_processing_florence2_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./florence2/processing_florence2.js */ "./src/models/florence2/processing_florence2.js"); +/* harmony import */ var _grounding_dino_processing_grounding_dino_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./grounding_dino/processing_grounding_dino.js */ "./src/models/grounding_dino/processing_grounding_dino.js"); +/* harmony import */ var _idefics3_processing_idefics3_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./idefics3/processing_idefics3.js */ "./src/models/idefics3/processing_idefics3.js"); +/* harmony import */ var _janus_processing_janus_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./janus/processing_janus.js */ "./src/models/janus/processing_janus.js"); +/* harmony import */ var _jina_clip_processing_jina_clip_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ./jina_clip/processing_jina_clip.js */ "./src/models/jina_clip/processing_jina_clip.js"); +/* harmony import */ var _mgp_str_processing_mgp_str_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ./mgp_str/processing_mgp_str.js */ "./src/models/mgp_str/processing_mgp_str.js"); +/* harmony import */ var _moonshine_processing_moonshine_js__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! ./moonshine/processing_moonshine.js */ "./src/models/moonshine/processing_moonshine.js"); +/* harmony import */ var _owlvit_processing_owlvit_js__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__(/*! ./owlvit/processing_owlvit.js */ "./src/models/owlvit/processing_owlvit.js"); +/* harmony import */ var _phi3_v_processing_phi3_v_js__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__(/*! ./phi3_v/processing_phi3_v.js */ "./src/models/phi3_v/processing_phi3_v.js"); +/* harmony import */ var _paligemma_processing_paligemma_js__WEBPACK_IMPORTED_MODULE_9__ = __webpack_require__(/*! ./paligemma/processing_paligemma.js */ "./src/models/paligemma/processing_paligemma.js"); +/* harmony import */ var _pyannote_processing_pyannote_js__WEBPACK_IMPORTED_MODULE_10__ = __webpack_require__(/*! ./pyannote/processing_pyannote.js */ "./src/models/pyannote/processing_pyannote.js"); +/* harmony import */ var _qwen2_vl_processing_qwen2_vl_js__WEBPACK_IMPORTED_MODULE_11__ = __webpack_require__(/*! ./qwen2_vl/processing_qwen2_vl.js */ "./src/models/qwen2_vl/processing_qwen2_vl.js"); +/* harmony import */ var _sam_processing_sam_js__WEBPACK_IMPORTED_MODULE_12__ = __webpack_require__(/*! ./sam/processing_sam.js */ "./src/models/sam/processing_sam.js"); +/* harmony import */ var _smolvlm_processing_smolvlm_js__WEBPACK_IMPORTED_MODULE_13__ = __webpack_require__(/*! ./smolvlm/processing_smolvlm.js */ "./src/models/smolvlm/processing_smolvlm.js"); +/* harmony import */ var _speecht5_processing_speecht5_js__WEBPACK_IMPORTED_MODULE_14__ = __webpack_require__(/*! ./speecht5/processing_speecht5.js */ "./src/models/speecht5/processing_speecht5.js"); +/* harmony import */ var _ultravox_processing_ultravox_js__WEBPACK_IMPORTED_MODULE_15__ = __webpack_require__(/*! ./ultravox/processing_ultravox.js */ "./src/models/ultravox/processing_ultravox.js"); +/* harmony import */ var _wav2vec2_processing_wav2vec2_js__WEBPACK_IMPORTED_MODULE_16__ = __webpack_require__(/*! ./wav2vec2/processing_wav2vec2.js */ "./src/models/wav2vec2/processing_wav2vec2.js"); +/* harmony import */ var _wav2vec2_with_lm_processing_wav2vec2_with_lm_js__WEBPACK_IMPORTED_MODULE_17__ = __webpack_require__(/*! ./wav2vec2_with_lm/processing_wav2vec2_with_lm.js */ "./src/models/wav2vec2_with_lm/processing_wav2vec2_with_lm.js"); +/* harmony import */ var _whisper_processing_whisper_js__WEBPACK_IMPORTED_MODULE_18__ = __webpack_require__(/*! ./whisper/processing_whisper.js */ "./src/models/whisper/processing_whisper.js"); + + + + + + + + + + + + + + + + + + + + + +/***/ }), + +/***/ "./src/models/pvt/image_processing_pvt.js": +/*!************************************************!*\ + !*** ./src/models/pvt/image_processing_pvt.js ***! + \************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ PvtImageProcessor: () => (/* binding */ PvtImageProcessor) +/* harmony export */ }); +/* harmony import */ var _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/image_processors_utils.js */ "./src/base/image_processors_utils.js"); + + +class PvtImageProcessor extends _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.ImageProcessor { } + + +/***/ }), + +/***/ "./src/models/pyannote/feature_extraction_pyannote.js": +/*!************************************************************!*\ + !*** ./src/models/pyannote/feature_extraction_pyannote.js ***! + \************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ PyAnnoteFeatureExtractor: () => (/* binding */ PyAnnoteFeatureExtractor) +/* harmony export */ }); +/* harmony import */ var _base_feature_extraction_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/feature_extraction_utils.js */ "./src/base/feature_extraction_utils.js"); +/* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../../utils/tensor.js */ "./src/utils/tensor.js"); +/* harmony import */ var _utils_maths_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../../utils/maths.js */ "./src/utils/maths.js"); + + + + + +class PyAnnoteFeatureExtractor extends _base_feature_extraction_utils_js__WEBPACK_IMPORTED_MODULE_0__.FeatureExtractor { + /** + * Asynchronously extracts features from a given audio using the provided configuration. + * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. + * @returns {Promise<{ input_values: Tensor; }>} The extracted input features. + */ + async _call(audio) { + (0,_base_feature_extraction_utils_js__WEBPACK_IMPORTED_MODULE_0__.validate_audio_inputs)(audio, 'PyAnnoteFeatureExtractor'); + + if (audio instanceof Float64Array) { + audio = new Float32Array(audio); + } + + const shape = [ + 1, /* batch_size */ + 1, /* num_channels */ + audio.length, /* num_samples */ + ]; + return { + input_values: new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor('float32', audio, shape), + }; + } + + /** + * NOTE: Can return fractional values. `Math.ceil` will ensure correct value. + * @param {number} samples The number of frames in the audio. + * @returns {number} The number of frames in the audio. + */ + samples_to_frames(samples) { + return ((samples - this.config.offset) / this.config.step); + } + + /** + * Post-processes the speaker diarization logits output by the model. + * @param {import('../../utils/tensor.js').Tensor} logits The speaker diarization logits output by the model. + * @param {number} num_samples Number of samples in the input audio. + * @returns {Array>} The post-processed speaker diarization results. + */ + post_process_speaker_diarization(logits, num_samples) { + const ratio = ( + num_samples / this.samples_to_frames(num_samples) + ) / this.config.sampling_rate; + + const results = []; + for (const scores of logits.tolist()) { + const accumulated_segments = []; + + let current_speaker = -1; + for (let i = 0; i < scores.length; ++i) { + /** @type {number[]} */ + const probabilities = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_2__.softmax)(scores[i]); + const [score, id] = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_2__.max)(probabilities); + const [start, end] = [i, i + 1]; + + if (id !== current_speaker) { + // Speaker has changed + current_speaker = id; + accumulated_segments.push({ id, start, end, score }); + } else { + // Continue the current segment + accumulated_segments.at(-1).end = end; + accumulated_segments.at(-1).score += score; + } + } + + results.push(accumulated_segments.map( + // Convert frame-space to time-space + // and compute the confidence + ({ id, start, end, score }) => ({ + id, + start: start * ratio, + end: end * ratio, + confidence: score / (end - start), + }) + )); + } + return results; + } + +} + + +/***/ }), + +/***/ "./src/models/pyannote/processing_pyannote.js": +/*!****************************************************!*\ + !*** ./src/models/pyannote/processing_pyannote.js ***! + \****************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ PyAnnoteProcessor: () => (/* binding */ PyAnnoteProcessor) +/* harmony export */ }); +/* harmony import */ var _base_processing_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/processing_utils.js */ "./src/base/processing_utils.js"); +/* harmony import */ var _feature_extraction_pyannote_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./feature_extraction_pyannote.js */ "./src/models/pyannote/feature_extraction_pyannote.js"); + + + +class PyAnnoteProcessor extends _base_processing_utils_js__WEBPACK_IMPORTED_MODULE_0__.Processor { + static feature_extractor_class = _feature_extraction_pyannote_js__WEBPACK_IMPORTED_MODULE_1__.PyAnnoteFeatureExtractor + + /** + * Calls the feature_extractor function with the given audio input. + * @param {any} audio The audio input to extract features from. + * @returns {Promise} A Promise that resolves with the extracted features. + */ + async _call(audio) { + return await this.feature_extractor(audio) + } + + /** @type {PyAnnoteFeatureExtractor['post_process_speaker_diarization']} */ + post_process_speaker_diarization(...args) { + return /** @type {PyAnnoteFeatureExtractor} */(this.feature_extractor).post_process_speaker_diarization(...args); + } + + get sampling_rate() { + return this.feature_extractor.config.sampling_rate; + } +} + + +/***/ }), + +/***/ "./src/models/qwen2_vl/image_processing_qwen2_vl.js": +/*!**********************************************************!*\ + !*** ./src/models/qwen2_vl/image_processing_qwen2_vl.js ***! + \**********************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ Qwen2VLImageProcessor: () => (/* binding */ Qwen2VLImageProcessor) +/* harmony export */ }); +/* harmony import */ var _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/image_processors_utils.js */ "./src/base/image_processors_utils.js"); +/* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../../utils/tensor.js */ "./src/utils/tensor.js"); + + + +class Qwen2VLImageProcessor extends _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.ImageProcessor { + async _call(images, ...args) { + const { pixel_values, original_sizes, reshaped_input_sizes } = await super._call(images, ...args); + + let patches = pixel_values; + + // @ts-ignore + const { temporal_patch_size, merge_size, patch_size } = this.config; + if (patches.dims[0] === 1) { + // Equivalent to np.tile(patches, (self.temporal_patch_size, 1, 1, 1)) + patches = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.cat)(Array.from({ length: temporal_patch_size }, () => patches), 0); + } + + const grid_t = patches.dims[0] / temporal_patch_size; + const channel = patches.dims[1]; + const grid_h = Math.floor(patches.dims[2] / patch_size); + const grid_w = Math.floor(patches.dims[3] / patch_size); + + const flatten_patches = patches + .view( + grid_t, + temporal_patch_size, + channel, + Math.floor(grid_h / merge_size), + merge_size, + patch_size, + Math.floor(grid_w / merge_size), + merge_size, + patch_size, + ) + .permute(0, 3, 6, 4, 7, 2, 1, 5, 8) + .view( + grid_t * grid_h * grid_w, + channel * temporal_patch_size * patch_size * patch_size, + ) + + const image_grid_thw = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor('int64', [grid_t, grid_h, grid_w], [1, 3]); + + return { + pixel_values: flatten_patches, + image_grid_thw, + original_sizes, + reshaped_input_sizes, + } + } +} + + + +/***/ }), + +/***/ "./src/models/qwen2_vl/processing_qwen2_vl.js": +/*!****************************************************!*\ + !*** ./src/models/qwen2_vl/processing_qwen2_vl.js ***! + \****************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ Qwen2VLProcessor: () => (/* binding */ Qwen2VLProcessor) +/* harmony export */ }); +/* harmony import */ var _base_processing_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/processing_utils.js */ "./src/base/processing_utils.js"); +/* harmony import */ var _auto_image_processing_auto_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../auto/image_processing_auto.js */ "./src/models/auto/image_processing_auto.js"); +/* harmony import */ var _tokenizers_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../../tokenizers.js */ "./src/tokenizers.js"); +/* harmony import */ var _utils_image_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ../../utils/image.js */ "./src/utils/image.js"); + + + + + +class Qwen2VLProcessor extends _base_processing_utils_js__WEBPACK_IMPORTED_MODULE_0__.Processor { + static image_processor_class = _auto_image_processing_auto_js__WEBPACK_IMPORTED_MODULE_1__.AutoImageProcessor + static tokenizer_class = _tokenizers_js__WEBPACK_IMPORTED_MODULE_2__.AutoTokenizer + + /** + * + * @param {string|string[]} text + * @param {RawImage|RawImage[]} images + * @param {...any} args + * @returns {Promise} + */ + async _call(text, images = null, ...args) { + + if (!Array.isArray(text)) { + text = [text]; + } + + let image_inputs, image_grid_thw; + + if (images) { + image_inputs = await this.image_processor(images); + image_grid_thw = image_inputs.image_grid_thw; + } + + if (image_grid_thw) { + // @ts-expect-error TS2551 + let merge_length = this.image_processor.config.merge_size ** 2; + let index = 0; + + const image_grid_thw_list = image_grid_thw.tolist(); + text = text.map(t => { + while (t.includes("<|image_pad|>")) { + const prod = Number(image_grid_thw_list[index++].reduce((a, b) => a * b, 1n)); + t = t.replace("<|image_pad|>", "<|placeholder|>".repeat(Math.floor(prod / merge_length))); + } + return t.replaceAll("<|placeholder|>", "<|image_pad|>"); + }); + } + + const text_inputs = this.tokenizer(text); + + return { + ...text_inputs, + ...image_inputs, + // TODO: ...videos_inputs, + } + } +} + + +/***/ }), + +/***/ "./src/models/rt_detr/image_processing_rt_detr.js": +/*!********************************************************!*\ + !*** ./src/models/rt_detr/image_processing_rt_detr.js ***! + \********************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ RTDetrImageProcessor: () => (/* binding */ RTDetrImageProcessor) +/* harmony export */ }); +/* harmony import */ var _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/image_processors_utils.js */ "./src/base/image_processors_utils.js"); + + + +class RTDetrImageProcessor extends _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.ImageProcessor { + /** @type {typeof post_process_object_detection} */ + post_process_object_detection(...args) { + return (0,_base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.post_process_object_detection)(...args); + } +} + + +/***/ }), + +/***/ "./src/models/sam/image_processing_sam.js": +/*!************************************************!*\ + !*** ./src/models/sam/image_processing_sam.js ***! + \************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ SamImageProcessor: () => (/* binding */ SamImageProcessor) +/* harmony export */ }); +/* harmony import */ var _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/image_processors_utils.js */ "./src/base/image_processors_utils.js"); +/* harmony import */ var _utils_core_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../../utils/core.js */ "./src/utils/core.js"); +/* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../../utils/tensor.js */ "./src/utils/tensor.js"); + + + + + + +/** + * @typedef {object} SamImageProcessorResult + * @property {Tensor} pixel_values + * @property {import("../../base/image_processors_utils.js").HeightWidth[]} original_sizes + * @property {import("../../base/image_processors_utils.js").HeightWidth[]} reshaped_input_sizes + * @property {Tensor} [input_points] + * @property {Tensor} [input_labels] + * @property {Tensor} [input_boxes] + */ + +class SamImageProcessor extends _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.ImageProcessor { + + /** + * + * @param {any} input_points + * @param {import("../../base/image_processors_utils.js").HeightWidth[]} original_sizes + * @param {import("../../base/image_processors_utils.js").HeightWidth[]} reshaped_input_sizes + * @returns {Tensor} + */ + reshape_input_points(input_points, original_sizes, reshaped_input_sizes, is_bounding_box = false) { + + // Make deep copy to avoid altering user's input + input_points = structuredClone(input_points); + let shape = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.calculateDimensions)(input_points); + + // TODO: add support for 2D input_points + if (shape.length === 3) { + // Correct user's input + if (!is_bounding_box) { + shape = [1, ...shape]; + } + input_points = [input_points]; + } else if (shape.length !== 4) { + throw Error("The input_points must be a 4D tensor of shape `batch_size`, `point_batch_size`, `nb_points_per_image`, `2`.") + } + + // Reshape input points + for (let i = 0; i < input_points.length; ++i) { // batch_size + let originalImageSize = original_sizes[i]; + let reshapedImageSize = reshaped_input_sizes[i]; + + let resizeFactors = [ + reshapedImageSize[0] / originalImageSize[0], + reshapedImageSize[1] / originalImageSize[1] + ] + + for (let j = 0; j < input_points[i].length; ++j) { // point_batch_size + for (let k = 0; k < input_points[i][j].length; ++k) { // nb_points_per_image + for (let w = 0; w < input_points[i][j][k].length; ++w) { // 2 or 4 + input_points[i][j][k][w] *= resizeFactors[w % 2]; + } + } + } + } + + return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_2__.Tensor( + 'float32', + Float32Array.from(input_points.flat(Infinity)), + shape + ) + + } + + /** + * + * @param {any} input_labels + * @param {Tensor} input_points + * @returns {Tensor} + */ + add_input_labels(input_labels, input_points) { + let shape = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.calculateDimensions)(input_labels); + if (shape.length === 2) { + // Correct user's input + shape = [1, ...shape]; + input_labels = [input_labels]; + } else if (shape.length !== 3) { + throw Error("The input_points must be a 4D tensor of shape `batch_size`, `point_batch_size`, `nb_points_per_image`, `2`.") + } + + if (shape.some((x, i) => x !== input_points.dims[i])) { + throw Error(`The first ${shape.length} dimensions of 'input_points' and 'input_labels' must be the same.`) + } + return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_2__.Tensor( + 'int64', + input_labels.flat(Infinity).map(BigInt), + shape, + ) + } + /** + * @param {any[]} images The URL(s) of the image(s) to extract features from. + * @param {Object} [options] Additional options for the processor. + * @param {any} [options.input_points=null] A 3D or 4D array, representing the input points provided by the user. + * - 3D: `[point_batch_size, nb_points_per_image, 2]`. In this case, `batch_size` is assumed to be 1. + * - 4D: `[batch_size, point_batch_size, nb_points_per_image, 2]`. + * @param {any} [options.input_labels=null] A 2D or 3D array, representing the input labels for the points, used by the prompt encoder to encode the prompt. + * - 2D: `[point_batch_size, nb_points_per_image]`. In this case, `batch_size` is assumed to be 1. + * - 3D: `[batch_size, point_batch_size, nb_points_per_image]`. + * @param {number[][][]} [options.input_boxes=null] A 3D array of shape `(batch_size, num_boxes, 4)`, representing the input boxes provided by the user. + * This is used by the prompt encoder to encode the prompt. Generally yields to much better generated masks. + * The processor will generate a tensor, with each dimension corresponding respectively to the image batch size, + * the number of boxes per image and the coordinates of the top left and botton right point of the box. + * In the order (`x1`, `y1`, `x2`, `y2`): + * - `x1`: the x coordinate of the top left point of the input box + * - `y1`: the y coordinate of the top left point of the input box + * - `x2`: the x coordinate of the bottom right point of the input box + * - `y2`: the y coordinate of the bottom right point of the input box + * @returns {Promise} + */ + async _call(images, { + input_points = null, + input_labels = null, + input_boxes = null + } = {}) { + // TODO allow user to use preprocessed images + /** @type {SamImageProcessorResult} */ + const processed = await super._call(images); + + if (input_points) { + processed.input_points = this.reshape_input_points( + input_points, processed.original_sizes, processed.reshaped_input_sizes + ); + } + + if (input_labels) { + if (!processed.input_points) { + throw Error("`input_points` must be provided if `input_labels` are provided.") + } + processed.input_labels = this.add_input_labels(input_labels, processed.input_points); + } + + if (input_boxes) { + processed.input_boxes = this.reshape_input_points( + input_boxes, processed.original_sizes, processed.reshaped_input_sizes, true, + ); + } + + return processed; + } + + /** + * Remove padding and upscale masks to the original image size. + * @param {Tensor} masks Batched masks from the mask_decoder in (batch_size, num_channels, height, width) format. + * @param {[number, number][]} original_sizes The original sizes of each image before it was resized to the model's expected input shape, in (height, width) format. + * @param {[number, number][]} reshaped_input_sizes The size of each image as it is fed to the model, in (height, width) format. Used to remove padding. + * @param {Object} options Optional parameters for post-processing. + * @param {number} [options.mask_threshold] The threshold to use for binarizing the masks. + * @param {boolean} [options.binarize] Whether to binarize the masks. + * @param {Object} [options.pad_size] The target size the images were padded to before being passed to the model. If `null`, the target size is assumed to be the processor's `pad_size`. + * @param {number} [options.pad_size.height] The height the images were padded to. + * @param {number} [options.pad_size.width] The width the images were padded to. + * @returns {Promise} Batched masks in batch_size, num_channels, height, width) format, where (height, width) is given by original_size. + */ + async post_process_masks(masks, original_sizes, reshaped_input_sizes, { + mask_threshold = 0.0, + binarize = true, + pad_size = null, + } = {}) { + // masks: [1, 1, 3, 256, 256] + + const output_masks = []; + + pad_size = pad_size ?? this.pad_size; + + /** @type {[number, number]} */ + const target_image_size = [pad_size.height, pad_size.width]; + + for (let i = 0; i < original_sizes.length; ++i) { + const original_size = original_sizes[i]; + const reshaped_input_size = reshaped_input_sizes[i]; + + // Upscale mask to padded size + let interpolated_mask = (await (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_2__.interpolate_4d)( + masks[i], + { mode: 'bilinear', size: target_image_size } + )); + + // Crop mask + interpolated_mask = interpolated_mask.slice(null, null, [0, reshaped_input_size[0]], [0, reshaped_input_size[1]]); + + // Downscale mask + interpolated_mask = (await (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_2__.interpolate_4d)( + interpolated_mask, + { mode: 'bilinear', size: original_size } + )); + + if (binarize) { + const data = interpolated_mask.data; + const binarizedMaskData = new Uint8Array(data.length); + for (let i = 0; i < data.length; ++i) { + if (data[i] > mask_threshold) { + binarizedMaskData[i] = 1; + } + } + interpolated_mask = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_2__.Tensor( + 'bool', + binarizedMaskData, + interpolated_mask.dims + ) + } + + output_masks.push(interpolated_mask); + } + + return output_masks; + } + + /** + * Generates a list of crop boxes of different sizes. Each layer has (2**i)**2 boxes for the ith layer. + * @param {import("../../utils/image.js").RawImage} image Input original image + * @param {number} target_size Target size of the resized image + * @param {Object} options Options for generating crop boxes + * @param {number} [options.crop_n_layers] If >0, mask prediction will be run again on crops of the image. + * Sets the number of layers to run, where each layer has 2**i_layer number of image crops. + * @param {number} [options.overlap_ratio] Sets the degree to which crops overlap. In the first crop layer, + * crops will overlap by this fraction of the image length. Later layers with more crops scale down this overlap. + * @param {number} [options.points_per_crop] Number of points to sample from each crop. + * @param {number} [options.crop_n_points_downscale_factor] The number of points-per-side sampled in layer n is + * scaled down by crop_n_points_downscale_factor**n. + * @returns {Object} An object containing the crop boxes, number of points per crop, cropped images, and input labels. + */ + generate_crop_boxes(image, target_size, { + crop_n_layers = 0, + overlap_ratio = 512 / 1500, + points_per_crop = 32, + crop_n_points_downscale_factor = 1, + } = {}) { + // TODO: Implement + // return { crop_boxes, points_per_crop, cropped_images, input_labels } + } +} + + + +/***/ }), + +/***/ "./src/models/sam/processing_sam.js": +/*!******************************************!*\ + !*** ./src/models/sam/processing_sam.js ***! + \******************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ SamProcessor: () => (/* binding */ SamProcessor) +/* harmony export */ }); +/* harmony import */ var _base_processing_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/processing_utils.js */ "./src/base/processing_utils.js"); +/* harmony import */ var _auto_image_processing_auto_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../auto/image_processing_auto.js */ "./src/models/auto/image_processing_auto.js"); + + + +class SamProcessor extends _base_processing_utils_js__WEBPACK_IMPORTED_MODULE_0__.Processor { + static image_processor_class = _auto_image_processing_auto_js__WEBPACK_IMPORTED_MODULE_1__.AutoImageProcessor + + async _call(...args) { + return await this.image_processor(...args); + } + + post_process_masks(...args) { + // @ts-ignore + return this.image_processor.post_process_masks(...args); + } + + reshape_input_points(...args) { + // @ts-ignore + return this.image_processor.reshape_input_points(...args); + } +} + +/***/ }), + +/***/ "./src/models/seamless_m4t/feature_extraction_seamless_m4t.js": +/*!********************************************************************!*\ + !*** ./src/models/seamless_m4t/feature_extraction_seamless_m4t.js ***! + \********************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ SeamlessM4TFeatureExtractor: () => (/* binding */ SeamlessM4TFeatureExtractor) +/* harmony export */ }); +/* harmony import */ var _base_feature_extraction_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/feature_extraction_utils.js */ "./src/base/feature_extraction_utils.js"); +/* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../../utils/tensor.js */ "./src/utils/tensor.js"); +/* harmony import */ var _utils_audio_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../../utils/audio.js */ "./src/utils/audio.js"); + + + + +class SeamlessM4TFeatureExtractor extends _base_feature_extraction_utils_js__WEBPACK_IMPORTED_MODULE_0__.FeatureExtractor { + + constructor(config) { + super(config); + + const sampling_rate = this.config.sampling_rate; + const mel_filters = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_2__.mel_filter_bank)( + 257, // num_frequency_bins + this.config.num_mel_bins, // num_mel_filters + 20, // min_frequency + Math.floor(sampling_rate / 2), // max_frequency + sampling_rate, // sampling_rate + null, // norm + "kaldi", // mel_scale + true, // triangularize_in_mel_space + ); + this.mel_filters = mel_filters; + + this.window = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_2__.window_function)(400, 'povey', { + periodic: false, + }) + } + + /** + * Computes the log-Mel spectrogram of the provided audio waveform. + * @param {Float32Array|Float64Array} waveform The audio waveform to process. + * @param {number} max_length The maximum number of frames to return. + * @returns {Promise} An object containing the log-Mel spectrogram data as a Float32Array and its dimensions as an array of numbers. + */ + async _extract_fbank_features(waveform, max_length) { + // NOTE: We don't pad/truncate since that is passed in as `max_num_frames` + + // Kaldi compliance: 16-bit signed integers + // 32768 == 2 ** 15 + waveform = waveform.map((/** @type {number} */ x) => x * 32768) + + return (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_2__.spectrogram)( + waveform, + this.window, // window + 400, // frame_length + 160, // hop_length + { + fft_length: 512, + power: 2.0, + center: false, + preemphasis: 0.97, + mel_filters: this.mel_filters, + log_mel: 'log', + mel_floor: 1.192092955078125e-07, + remove_dc_offset: true, + + // Custom + max_num_frames: max_length, + transpose: true, + } + ) + } + + /** + * Asynchronously extracts features from a given audio using the provided configuration. + * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. + * @param {Object} options Optional parameters for feature extraction. + * @param {boolean} [options.padding=true] Whether to pad the sequence to a multiple of `pad_to_multiple_of`. + * @param {number} [options.pad_to_multiple_of=2] The number to pad the sequence to a multiple of. + * @param {boolean} [options.do_normalize_per_mel_bins=true] Whether or not to zero-mean unit-variance normalize the input per mel-channel. + * @param {boolean} [options.return_attention_mask=true] Whether to return the attention mask. + * @returns {Promise<{ input_features: Tensor, attention_mask?: Tensor }>} A Promise resolving to an object containing the extracted input features and attention masks as Tensors. + */ + async _call(audio, { + padding = true, + pad_to_multiple_of = 2, + do_normalize_per_mel_bins = true, + return_attention_mask = true, + } = {}) { + (0,_base_feature_extraction_utils_js__WEBPACK_IMPORTED_MODULE_0__.validate_audio_inputs)(audio, 'SeamlessM4TFeatureExtractor'); + + let features = await this._extract_fbank_features(audio, this.config.max_length); + + if (do_normalize_per_mel_bins) { + const [num_features, feature_size] = features.dims; + const data = features.data; + for (let i = 0; i < feature_size; ++i) { + let sum = 0; + for (let j = 0; j < num_features; ++j) { + sum += data[j * feature_size + i]; + } + + const mean = sum / num_features; + + let variance = 0; + for (let j = 0; j < num_features; ++j) { + variance += (data[j * feature_size + i] - mean) ** 2; + } + variance /= num_features - 1; // NOTE: We use ddof=1 + + const std = Math.sqrt(variance + 1e-7); + for (let j = 0; j < num_features; ++j) { + const index = j * feature_size + i; + data[index] = (data[index] - mean) / std; + } + } + } + + let padded_attention_mask; + if (padding) { + const [num_frames, num_channels] = features.dims; + const data = /** @type {Float32Array} */(features.data); + + const pad_size = num_frames % pad_to_multiple_of; + if (pad_size > 0) { + const padded_data = new Float32Array(num_channels * (num_frames + pad_size)); + padded_data.set(data) + padded_data.fill(this.config.padding_value, data.length) + + const numPaddedFrames = num_frames + pad_size; + features = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor( + features.type, + padded_data, + [numPaddedFrames, num_channels], + ) + + if (return_attention_mask) { + padded_attention_mask = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor( + 'int64', + new BigInt64Array(numPaddedFrames), + [1, numPaddedFrames], + ); + /** @type {BigInt64Array} */ (padded_attention_mask.data).fill(1n, 0, num_frames); + } + } + } + + const [num_frames, num_channels] = features.dims; + + const stride = this.config.stride; + const remainder = num_frames % stride; + if (remainder !== 0) { + throw new Error(`The number of frames (${num_frames}) must be a multiple of the stride (${stride}).`) + } + + const input_features = features.view( + 1, + Math.floor(num_frames / stride), + num_channels * stride, + ); + + const result = { input_features } + + if (return_attention_mask) { + const reshapedNumFrames = input_features.dims[1]; + + const attention_mask_data = new BigInt64Array(reshapedNumFrames); + + if (padded_attention_mask) { + const padded_attention_mask_data = padded_attention_mask.data; + for (let i = 1, j = 0; i < num_frames; i += stride, ++j) { + attention_mask_data[j] = padded_attention_mask_data[i]; + } + } else { + attention_mask_data.fill(1n); + } + result.attention_mask = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor( + 'int64', + attention_mask_data, + [1, reshapedNumFrames], + ); + } + + return result; + } +} + + +/***/ }), + +/***/ "./src/models/segformer/image_processing_segformer.js": +/*!************************************************************!*\ + !*** ./src/models/segformer/image_processing_segformer.js ***! + \************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ SegformerFeatureExtractor: () => (/* binding */ SegformerFeatureExtractor), +/* harmony export */ SegformerImageProcessor: () => (/* binding */ SegformerImageProcessor) +/* harmony export */ }); +/* harmony import */ var _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/image_processors_utils.js */ "./src/base/image_processors_utils.js"); + + + +class SegformerImageProcessor extends _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.ImageProcessor { + /** @type {typeof post_process_semantic_segmentation} */ + post_process_semantic_segmentation(...args) { + return (0,_base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.post_process_semantic_segmentation)(...args); + } +} +class SegformerFeatureExtractor extends SegformerImageProcessor { } + + +/***/ }), + +/***/ "./src/models/siglip/image_processing_siglip.js": +/*!******************************************************!*\ + !*** ./src/models/siglip/image_processing_siglip.js ***! + \******************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ SiglipImageProcessor: () => (/* binding */ SiglipImageProcessor) +/* harmony export */ }); +/* harmony import */ var _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/image_processors_utils.js */ "./src/base/image_processors_utils.js"); + + +class SiglipImageProcessor extends _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.ImageProcessor { } + + +/***/ }), + +/***/ "./src/models/smolvlm/image_processing_smolvlm.js": +/*!********************************************************!*\ + !*** ./src/models/smolvlm/image_processing_smolvlm.js ***! + \********************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ SmolVLMImageProcessor: () => (/* reexport safe */ _idefics3_image_processing_idefics3_js__WEBPACK_IMPORTED_MODULE_0__.Idefics3ImageProcessor) +/* harmony export */ }); +/* harmony import */ var _idefics3_image_processing_idefics3_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../idefics3/image_processing_idefics3.js */ "./src/models/idefics3/image_processing_idefics3.js"); + + + + +/***/ }), + +/***/ "./src/models/smolvlm/processing_smolvlm.js": +/*!**************************************************!*\ + !*** ./src/models/smolvlm/processing_smolvlm.js ***! + \**************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ SmolVLMProcessor: () => (/* reexport safe */ _idefics3_processing_idefics3_js__WEBPACK_IMPORTED_MODULE_0__.Idefics3Processor) +/* harmony export */ }); +/* harmony import */ var _idefics3_processing_idefics3_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../idefics3/processing_idefics3.js */ "./src/models/idefics3/processing_idefics3.js"); + + + + +/***/ }), + +/***/ "./src/models/snac/feature_extraction_snac.js": +/*!****************************************************!*\ + !*** ./src/models/snac/feature_extraction_snac.js ***! + \****************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ SnacFeatureExtractor: () => (/* binding */ SnacFeatureExtractor) +/* harmony export */ }); +/* harmony import */ var _dac_feature_extraction_dac_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../dac/feature_extraction_dac.js */ "./src/models/dac/feature_extraction_dac.js"); + + +class SnacFeatureExtractor extends _dac_feature_extraction_dac_js__WEBPACK_IMPORTED_MODULE_0__.DacFeatureExtractor { } + + +/***/ }), + +/***/ "./src/models/speecht5/feature_extraction_speecht5.js": +/*!************************************************************!*\ + !*** ./src/models/speecht5/feature_extraction_speecht5.js ***! + \************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ SpeechT5FeatureExtractor: () => (/* binding */ SpeechT5FeatureExtractor) +/* harmony export */ }); +/* harmony import */ var _base_feature_extraction_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/feature_extraction_utils.js */ "./src/base/feature_extraction_utils.js"); + + + +class SpeechT5FeatureExtractor extends _base_feature_extraction_utils_js__WEBPACK_IMPORTED_MODULE_0__.FeatureExtractor { } + + +/***/ }), + +/***/ "./src/models/speecht5/processing_speecht5.js": +/*!****************************************************!*\ + !*** ./src/models/speecht5/processing_speecht5.js ***! + \****************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ SpeechT5Processor: () => (/* binding */ SpeechT5Processor) +/* harmony export */ }); +/* harmony import */ var _base_processing_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/processing_utils.js */ "./src/base/processing_utils.js"); +/* harmony import */ var _tokenizers_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../../tokenizers.js */ "./src/tokenizers.js"); +/* harmony import */ var _auto_feature_extraction_auto_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../auto/feature_extraction_auto.js */ "./src/models/auto/feature_extraction_auto.js"); + + + + +class SpeechT5Processor extends _base_processing_utils_js__WEBPACK_IMPORTED_MODULE_0__.Processor { + static tokenizer_class = _tokenizers_js__WEBPACK_IMPORTED_MODULE_1__.AutoTokenizer + static feature_extractor_class = _auto_feature_extraction_auto_js__WEBPACK_IMPORTED_MODULE_2__.AutoFeatureExtractor + + /** + * Calls the feature_extractor function with the given input. + * @param {any} input The input to extract features from. + * @returns {Promise} A Promise that resolves with the extracted features. + */ + async _call(input) { + return await this.feature_extractor(input) + } +} + + +/***/ }), + +/***/ "./src/models/swin2sr/image_processing_swin2sr.js": +/*!********************************************************!*\ + !*** ./src/models/swin2sr/image_processing_swin2sr.js ***! + \********************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ Swin2SRImageProcessor: () => (/* binding */ Swin2SRImageProcessor) +/* harmony export */ }); +/* harmony import */ var _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/image_processors_utils.js */ "./src/base/image_processors_utils.js"); + + +class Swin2SRImageProcessor extends _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.ImageProcessor { + pad_image(pixelData, imgDims, padSize, options = {}) { + // NOTE: In this case, `padSize` represents the size of the sliding window for the local attention. + // In other words, the image is padded so that its width and height are multiples of `padSize`. + const [imageHeight, imageWidth, imageChannels] = imgDims; + + return super.pad_image(pixelData, imgDims, { + // NOTE: For Swin2SR models, the original python implementation adds padding even when the image's width/height is already + // a multiple of `pad_size`. However, this is most likely a bug (PR: https://github.com/mv-lab/swin2sr/pull/19). + // For this reason, we only add padding when the image's width/height is not a multiple of `pad_size`. + width: imageWidth + (padSize - imageWidth % padSize) % padSize, + height: imageHeight + (padSize - imageHeight % padSize) % padSize, + }, { + mode: 'symmetric', + center: false, + constant_values: -1, + ...options, + }) + } +} + +/***/ }), + +/***/ "./src/models/ultravox/processing_ultravox.js": +/*!****************************************************!*\ + !*** ./src/models/ultravox/processing_ultravox.js ***! + \****************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ UltravoxProcessor: () => (/* binding */ UltravoxProcessor) +/* harmony export */ }); +/* harmony import */ var _auto_feature_extraction_auto_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../auto/feature_extraction_auto.js */ "./src/models/auto/feature_extraction_auto.js"); +/* harmony import */ var _tokenizers_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../../tokenizers.js */ "./src/tokenizers.js"); +/* harmony import */ var _base_processing_utils_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../../base/processing_utils.js */ "./src/base/processing_utils.js"); + + + + +/** + * Represents a UltravoxProcessor that extracts features from an audio input. + */ +class UltravoxProcessor extends _base_processing_utils_js__WEBPACK_IMPORTED_MODULE_2__.Processor { + static tokenizer_class = _tokenizers_js__WEBPACK_IMPORTED_MODULE_1__.AutoTokenizer + static feature_extractor_class = _auto_feature_extraction_auto_js__WEBPACK_IMPORTED_MODULE_0__.AutoFeatureExtractor + static uses_processor_config = true; + + /** + * @param {string} text The text input to process. + * @param {Float32Array} audio The audio input to process. + */ + async _call(text, audio = null, kwargs = {}) { + // TODO: Support batched inputs + if (Array.isArray(text)) { + throw new Error("Batched inputs are not supported yet."); + } + + let audio_inputs = {}; + if (audio) { + const audio_len = audio.length; + const { input_features } = await this.feature_extractor(audio, { + ...kwargs, + max_length: audio_len, + }); + const nb_encoder_frames = Math.round(audio_len / this.config.encoder_ds_factor + 1e-4); + + // NOTE: The python version appears to have an off-by-one error. + const audio_embed_frames = 1 + Math.ceil(nb_encoder_frames / this.config.stack_factor); + audio_inputs["audio_token_len"] = [audio_embed_frames]; + audio_inputs["audio_values"] = input_features; + + const image_token = this.config.audio_placeholder; + if (!text.includes(image_token)) { + throw new Error(`The input text does not contain the image token ${image_token}.`); + } + text = text.replaceAll(image_token, image_token.repeat(audio_embed_frames)); + } + + const text_inputs = this.tokenizer(text, { + add_special_tokens: false, + ...kwargs, + }); + + return { + ...text_inputs, + ...audio_inputs, + } + } +} + + +/***/ }), + +/***/ "./src/models/vit/image_processing_vit.js": +/*!************************************************!*\ + !*** ./src/models/vit/image_processing_vit.js ***! + \************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ ViTFeatureExtractor: () => (/* binding */ ViTFeatureExtractor), +/* harmony export */ ViTImageProcessor: () => (/* binding */ ViTImageProcessor) +/* harmony export */ }); +/* harmony import */ var _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/image_processors_utils.js */ "./src/base/image_processors_utils.js"); + + +class ViTImageProcessor extends _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.ImageProcessor { } +class ViTFeatureExtractor extends ViTImageProcessor { } + + + +/***/ }), + +/***/ "./src/models/vitmatte/image_processing_vitmatte.js": +/*!**********************************************************!*\ + !*** ./src/models/vitmatte/image_processing_vitmatte.js ***! + \**********************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ VitMatteImageProcessor: () => (/* binding */ VitMatteImageProcessor) +/* harmony export */ }); +/* harmony import */ var _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/image_processors_utils.js */ "./src/base/image_processors_utils.js"); +/* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../../utils/tensor.js */ "./src/utils/tensor.js"); + + + + +class VitMatteImageProcessor extends _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.ImageProcessor { + /** + * Calls the feature extraction process on an array of images, preprocesses + * each image, and concatenates the resulting features into a single Tensor. + * @param {import("../../utils/image.js").RawImage[]} images The image(s) to extract features from. + * @param {import("../../utils/image.js").RawImage[]} trimaps The trimaps(s) to extract features from. + * @returns {Promise} An object containing the concatenated pixel values of the preprocessed images. + */ + async _call(images, trimaps) { + if (!Array.isArray(images)) { + images = [images]; + } + if (!Array.isArray(trimaps)) { + trimaps = [trimaps]; + } + + const imageData = await Promise.all(images.map(x => this.preprocess(x))); + const trimapData = await Promise.all(trimaps.map(x => this.preprocess(x, { + do_normalize: false, + do_convert_rgb: false, + do_convert_grayscale: true, + }))); + + + // Stack pixel values + const pixel_values = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.stack)(imageData.map( + // Concatenate images and trimaps + (x, i) => (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.cat)([x.pixel_values, trimapData[i].pixel_values], 0) + ), 0); + + return { + pixel_values, + + // Original sizes of images + original_sizes: imageData.map(x => x.original_size), + + // Reshaped sizes of images, before padding or cropping + reshaped_input_sizes: imageData.map(x => x.reshaped_input_size), + } + } +} + + +/***/ }), + +/***/ "./src/models/vitpose/image_processing_vitpose.js": +/*!********************************************************!*\ + !*** ./src/models/vitpose/image_processing_vitpose.js ***! + \********************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ VitPoseImageProcessor: () => (/* binding */ VitPoseImageProcessor) +/* harmony export */ }); +/* harmony import */ var _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/image_processors_utils.js */ "./src/base/image_processors_utils.js"); + + +class VitPoseImageProcessor extends _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.ImageProcessor { + + /** + * Transform the heatmaps into keypoint predictions and transform them back to the image. + * NOTE: This is a naive implementation and does not include advanced post-processing techniques, + * so the results may not be as accurate as the original implementation. + * @param {import('../../utils/tensor.js').Tensor} outputs The model outputs. + * @param {[number, number, number, number][][]} boxes List or array of bounding boxes for each image. + * Each box should be a list of 4 floats representing the bounding box coordinates in COCO format (top_left_x, top_left_y, width, height). + * @returns {{ + * bbox: [number, number, number, number], + * scores: number[], + * labels: number[], + * keypoints: [number, number][] + * }[][]} List of keypoints predictions for each image. + */ + post_process_pose_estimation(outputs, boxes, { + threshold = null, + // TODO: + // kernel_size = 11, + // target_sizes = null, + } = {}) { + // NOTE: boxes are 3D (batch_size, num_boxes, 4) + const heatmaps = outputs.tolist(); + const [batch_size, num_classes, height, width] = outputs.dims; + + const results = []; + for (let b = 0; b < batch_size; ++b) { + const heatmap = heatmaps[b]; + const bboxes = boxes[b]; + + const batch_results = []; + for (let n = 0; n < bboxes.length; ++n) { + const bbox = bboxes[n]; + + const keypoints = []; + const scores = []; + const labels = []; + + const xScale = bbox.at(-2) / width; + const yScale = bbox.at(-1) / height; + for (let c = 0; c < heatmap.length; ++c) { + let [xWeightedSum, yWeightedSum] = [0, 0]; + let sum = 0; + let score = -Infinity; + const row = heatmap[c]; + for (let y = 0; y < row.length; ++y) { + const col = row[y]; + for (let x = 0; x < col.length; ++x) { + const value = col[x]; + sum += value; + + score = Math.max(score, value); + + // Get weighted sum of positions + // TODO: Determine best offsets + xWeightedSum += (x + 0.5) * value; + yWeightedSum += (y) * value; + } + } + + // Ignore low scores, if threshold is set + if (threshold != null && score < threshold) continue; + + /** @type {[number, number]} */ + const keypoint = [ + xScale * xWeightedSum / sum, + yScale * yWeightedSum / sum, + ] + keypoints.push(keypoint); + labels.push(c); + scores.push(score); + } + batch_results.push({ + bbox, + scores, + labels, + keypoints, + }); + } + results.push(batch_results); + } + return results; + } +} + + +/***/ }), + +/***/ "./src/models/wav2vec2/feature_extraction_wav2vec2.js": +/*!************************************************************!*\ + !*** ./src/models/wav2vec2/feature_extraction_wav2vec2.js ***! + \************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ Wav2Vec2FeatureExtractor: () => (/* binding */ Wav2Vec2FeatureExtractor) +/* harmony export */ }); +/* harmony import */ var _base_feature_extraction_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/feature_extraction_utils.js */ "./src/base/feature_extraction_utils.js"); +/* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../../utils/tensor.js */ "./src/utils/tensor.js"); + + + +class Wav2Vec2FeatureExtractor extends _base_feature_extraction_utils_js__WEBPACK_IMPORTED_MODULE_0__.FeatureExtractor { + + /** + * @param {Float32Array} input_values + * @returns {Float32Array} + */ + _zero_mean_unit_var_norm(input_values) { + // TODO support batch? + const sum = input_values.reduce((a, b) => a + b, 0); + const mean = sum / input_values.length; + const variance = input_values.reduce((a, b) => a + (b - mean) ** 2, 0) / input_values.length; + return input_values.map(x => (x - mean) / Math.sqrt(variance + 1e-7)); + } + + /** + * Asynchronously extracts features from a given audio using the provided configuration. + * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. + * @returns {Promise<{ input_values: Tensor; attention_mask: Tensor }>} A Promise resolving to an object containing the extracted input features and attention mask as Tensors. + */ + async _call(audio) { + (0,_base_feature_extraction_utils_js__WEBPACK_IMPORTED_MODULE_0__.validate_audio_inputs)(audio, 'Wav2Vec2FeatureExtractor'); + + if (audio instanceof Float64Array) { + audio = new Float32Array(audio); + } + + let input_values = audio; + + // zero-mean and unit-variance normalization + if (this.config.do_normalize) { + input_values = this._zero_mean_unit_var_norm(input_values); + } + + // TODO: allow user to pass in attention mask + const shape = [1, input_values.length]; + return { + input_values: new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor('float32', input_values, shape), + attention_mask: new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor('int64', new BigInt64Array(input_values.length).fill(1n), shape) + }; + } +} + + +/***/ }), + +/***/ "./src/models/wav2vec2/processing_wav2vec2.js": +/*!****************************************************!*\ + !*** ./src/models/wav2vec2/processing_wav2vec2.js ***! + \****************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ Wav2Vec2Processor: () => (/* binding */ Wav2Vec2Processor) +/* harmony export */ }); +/* harmony import */ var _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../tokenizers.js */ "./src/tokenizers.js"); +/* harmony import */ var _auto_feature_extraction_auto_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../auto/feature_extraction_auto.js */ "./src/models/auto/feature_extraction_auto.js"); +/* harmony import */ var _base_processing_utils_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../../base/processing_utils.js */ "./src/base/processing_utils.js"); + + + + +class Wav2Vec2Processor extends _base_processing_utils_js__WEBPACK_IMPORTED_MODULE_2__.Processor { + static tokenizer_class = _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer + static feature_extractor_class = _auto_feature_extraction_auto_js__WEBPACK_IMPORTED_MODULE_1__.AutoFeatureExtractor + + /** + * Calls the feature_extractor function with the given audio input. + * @param {any} audio The audio input to extract features from. + * @returns {Promise} A Promise that resolves with the extracted features. + */ + async _call(audio) { + return await this.feature_extractor(audio) + } +} + + +/***/ }), + +/***/ "./src/models/wav2vec2_with_lm/processing_wav2vec2_with_lm.js": +/*!********************************************************************!*\ + !*** ./src/models/wav2vec2_with_lm/processing_wav2vec2_with_lm.js ***! + \********************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ Wav2Vec2ProcessorWithLM: () => (/* binding */ Wav2Vec2ProcessorWithLM) +/* harmony export */ }); +/* harmony import */ var _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../tokenizers.js */ "./src/tokenizers.js"); +/* harmony import */ var _auto_feature_extraction_auto_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../auto/feature_extraction_auto.js */ "./src/models/auto/feature_extraction_auto.js"); +/* harmony import */ var _base_processing_utils_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../../base/processing_utils.js */ "./src/base/processing_utils.js"); + + + + +class Wav2Vec2ProcessorWithLM extends _base_processing_utils_js__WEBPACK_IMPORTED_MODULE_2__.Processor { + static tokenizer_class = _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer + static feature_extractor_class = _auto_feature_extraction_auto_js__WEBPACK_IMPORTED_MODULE_1__.AutoFeatureExtractor + + /** + * Calls the feature_extractor function with the given audio input. + * @param {any} audio The audio input to extract features from. + * @returns {Promise} A Promise that resolves with the extracted features. + */ + async _call(audio) { + return await this.feature_extractor(audio) + } +} + + +/***/ }), + +/***/ "./src/models/wespeaker/feature_extraction_wespeaker.js": +/*!**************************************************************!*\ + !*** ./src/models/wespeaker/feature_extraction_wespeaker.js ***! + \**************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ WeSpeakerFeatureExtractor: () => (/* binding */ WeSpeakerFeatureExtractor) +/* harmony export */ }); +/* harmony import */ var _base_feature_extraction_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/feature_extraction_utils.js */ "./src/base/feature_extraction_utils.js"); +/* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../../utils/tensor.js */ "./src/utils/tensor.js"); +/* harmony import */ var _utils_audio_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../../utils/audio.js */ "./src/utils/audio.js"); + + + + + +class WeSpeakerFeatureExtractor extends _base_feature_extraction_utils_js__WEBPACK_IMPORTED_MODULE_0__.FeatureExtractor { + + constructor(config) { + super(config); + + const sampling_rate = this.config.sampling_rate; + const mel_filters = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_2__.mel_filter_bank)( + 257, // num_frequency_bins + this.config.num_mel_bins, // num_mel_filters + 20, // min_frequency + Math.floor(sampling_rate / 2), // max_frequency + sampling_rate, // sampling_rate + null, // norm + "kaldi", // mel_scale + true, // triangularize_in_mel_space + ); + this.mel_filters = mel_filters; + + this.window = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_2__.window_function)(400, 'hamming', { + periodic: false, + }) + this.min_num_frames = this.config.min_num_frames; + } + + /** + * Computes the log-Mel spectrogram of the provided audio waveform. + * @param {Float32Array|Float64Array} waveform The audio waveform to process. + * @returns {Promise} An object containing the log-Mel spectrogram data as a Float32Array and its dimensions as an array of numbers. + */ + async _extract_fbank_features(waveform) { + // Kaldi compliance: 16-bit signed integers + // 32768 == 2 ** 15 + waveform = waveform.map((/** @type {number} */ x) => x * 32768) + + return (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_2__.spectrogram)( + waveform, + this.window, // window + 400, // frame_length + 160, // hop_length + { + fft_length: 512, + power: 2.0, + center: false, + preemphasis: 0.97, + mel_filters: this.mel_filters, + log_mel: 'log', + mel_floor: 1.192092955078125e-07, + remove_dc_offset: true, + + // Custom + transpose: true, + min_num_frames: this.min_num_frames, + } + ) + } + + + /** + * Asynchronously extracts features from a given audio using the provided configuration. + * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. + * @returns {Promise<{ input_features: Tensor }>} A Promise resolving to an object containing the extracted input features as a Tensor. + */ + async _call(audio) { + (0,_base_feature_extraction_utils_js__WEBPACK_IMPORTED_MODULE_0__.validate_audio_inputs)(audio, 'WeSpeakerFeatureExtractor'); + + const features = (await this._extract_fbank_features(audio)).unsqueeze_(0); + + if (this.config.fbank_centering_span === null) { + // center features with global average + const meanData = /** @type {Float32Array} */ (features.mean(1).data); + const featuresData = /** @type {Float32Array} */(features.data); + const [batch_size, num_frames, feature_size] = features.dims; + + for (let i = 0; i < batch_size; ++i) { + const offset1 = i * num_frames * feature_size; + const offset2 = i * feature_size; + for (let j = 0; j < num_frames; ++j) { + const offset3 = offset1 + j * feature_size; + for (let k = 0; k < feature_size; ++k) { + featuresData[offset3 + k] -= meanData[offset2 + k]; + } + } + } + } + + return { + input_features: features + }; + } +} + + +/***/ }), + +/***/ "./src/models/whisper/common_whisper.js": +/*!**********************************************!*\ + !*** ./src/models/whisper/common_whisper.js ***! + \**********************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ WHISPER_LANGUAGE_MAPPING: () => (/* binding */ WHISPER_LANGUAGE_MAPPING), +/* harmony export */ WHISPER_TO_LANGUAGE_CODE_MAPPING: () => (/* binding */ WHISPER_TO_LANGUAGE_CODE_MAPPING), +/* harmony export */ whisper_language_to_code: () => (/* binding */ whisper_language_to_code) +/* harmony export */ }); + + +const WHISPER_LANGUAGES = [ + ["en", "english"], + ["zh", "chinese"], + ["de", "german"], + ["es", "spanish"], + ["ru", "russian"], + ["ko", "korean"], + ["fr", "french"], + ["ja", "japanese"], + ["pt", "portuguese"], + ["tr", "turkish"], + ["pl", "polish"], + ["ca", "catalan"], + ["nl", "dutch"], + ["ar", "arabic"], + ["sv", "swedish"], + ["it", "italian"], + ["id", "indonesian"], + ["hi", "hindi"], + ["fi", "finnish"], + ["vi", "vietnamese"], + ["he", "hebrew"], + ["uk", "ukrainian"], + ["el", "greek"], + ["ms", "malay"], + ["cs", "czech"], + ["ro", "romanian"], + ["da", "danish"], + ["hu", "hungarian"], + ["ta", "tamil"], + ["no", "norwegian"], + ["th", "thai"], + ["ur", "urdu"], + ["hr", "croatian"], + ["bg", "bulgarian"], + ["lt", "lithuanian"], + ["la", "latin"], + ["mi", "maori"], + ["ml", "malayalam"], + ["cy", "welsh"], + ["sk", "slovak"], + ["te", "telugu"], + ["fa", "persian"], + ["lv", "latvian"], + ["bn", "bengali"], + ["sr", "serbian"], + ["az", "azerbaijani"], + ["sl", "slovenian"], + ["kn", "kannada"], + ["et", "estonian"], + ["mk", "macedonian"], + ["br", "breton"], + ["eu", "basque"], + ["is", "icelandic"], + ["hy", "armenian"], + ["ne", "nepali"], + ["mn", "mongolian"], + ["bs", "bosnian"], + ["kk", "kazakh"], + ["sq", "albanian"], + ["sw", "swahili"], + ["gl", "galician"], + ["mr", "marathi"], + ["pa", "punjabi"], + ["si", "sinhala"], + ["km", "khmer"], + ["sn", "shona"], + ["yo", "yoruba"], + ["so", "somali"], + ["af", "afrikaans"], + ["oc", "occitan"], + ["ka", "georgian"], + ["be", "belarusian"], + ["tg", "tajik"], + ["sd", "sindhi"], + ["gu", "gujarati"], + ["am", "amharic"], + ["yi", "yiddish"], + ["lo", "lao"], + ["uz", "uzbek"], + ["fo", "faroese"], + ["ht", "haitian creole"], + ["ps", "pashto"], + ["tk", "turkmen"], + ["nn", "nynorsk"], + ["mt", "maltese"], + ["sa", "sanskrit"], + ["lb", "luxembourgish"], + ["my", "myanmar"], + ["bo", "tibetan"], + ["tl", "tagalog"], + ["mg", "malagasy"], + ["as", "assamese"], + ["tt", "tatar"], + ["haw", "hawaiian"], + ["ln", "lingala"], + ["ha", "hausa"], + ["ba", "bashkir"], + ["jw", "javanese"], + ["su", "sundanese"], +] + +// @ts-ignore +const WHISPER_LANGUAGE_MAPPING = new Map(WHISPER_LANGUAGES); +// @ts-ignore +const WHISPER_TO_LANGUAGE_CODE_MAPPING = new Map([ + ...WHISPER_LANGUAGES.map(([k, v]) => [v, k]), + ...[ + ["burmese", "my"], + ["valencian", "ca"], + ["flemish", "nl"], + ["haitian", "ht"], + ["letzeburgesch", "lb"], + ["pushto", "ps"], + ["panjabi", "pa"], + ["moldavian", "ro"], + ["moldovan", "ro"], + ["sinhalese", "si"], + ["castilian", "es"], + ] +]); + +/** + * @param {string} language The language name or code + * @returns {string} The language code + */ +function whisper_language_to_code(language) { + language = language.toLowerCase(); + + // Map to code from user-friendly name (e.g., "english" -> "en") + let language_code = WHISPER_TO_LANGUAGE_CODE_MAPPING.get(language); + + if (language_code === undefined) { + // User provided something that is not a language name + + // Perhaps the user passed the special token itself + const language_special_token = language.match(/^<\|([a-z]{2})\|>$/); + if (language_special_token) { + language = language_special_token[1]; + } + + if (WHISPER_LANGUAGE_MAPPING.has(language)) { + // User provided the language code directly (e.g., "en") + language_code = language; + + } else { + // User provided something that is not a language code or name + const is_language_code = language.length === 2; + const langs = is_language_code ? WHISPER_LANGUAGE_MAPPING.keys() : WHISPER_LANGUAGE_MAPPING.values(); + + throw new Error(`Language "${language}" is not supported. Must be one of: ${JSON.stringify(Array.from(langs))}`); + } + } + return language_code; +} + + +/***/ }), + +/***/ "./src/models/whisper/feature_extraction_whisper.js": +/*!**********************************************************!*\ + !*** ./src/models/whisper/feature_extraction_whisper.js ***! + \**********************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ WhisperFeatureExtractor: () => (/* binding */ WhisperFeatureExtractor) +/* harmony export */ }); +/* harmony import */ var _base_feature_extraction_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/feature_extraction_utils.js */ "./src/base/feature_extraction_utils.js"); +/* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../../utils/tensor.js */ "./src/utils/tensor.js"); +/* harmony import */ var _utils_audio_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../../utils/audio.js */ "./src/utils/audio.js"); +/* harmony import */ var _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ../../utils/maths.js */ "./src/utils/maths.js"); + + + + + +class WhisperFeatureExtractor extends _base_feature_extraction_utils_js__WEBPACK_IMPORTED_MODULE_0__.FeatureExtractor { + + constructor(config) { + super(config); + + // Prefer given `mel_filters` from preprocessor_config.json, or calculate them if they don't exist. + this.config.mel_filters ??= (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_2__.mel_filter_bank)( + Math.floor(1 + this.config.n_fft / 2), // num_frequency_bins + this.config.feature_size, // num_mel_filters + 0.0, // min_frequency + 8000.0, // max_frequency + this.config.sampling_rate, // sampling_rate + "slaney", // norm + "slaney", // mel_scale + ); + + this.window = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_2__.window_function)(this.config.n_fft, 'hann'); + } + + /** + * Computes the log-Mel spectrogram of the provided audio waveform. + * @param {Float32Array|Float64Array} waveform The audio waveform to process. + * @returns {Promise} An object containing the log-Mel spectrogram data as a Float32Array and its dimensions as an array of numbers. + */ + async _extract_fbank_features(waveform) { + const features = await (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_2__.spectrogram)( + waveform, + this.window, // window + this.config.n_fft, // frame_length + this.config.hop_length, // hop_length + { + power: 2.0, + mel_filters: this.config.mel_filters, + log_mel: 'log10', + + // Custom + max_num_frames: Math.min( + Math.floor(waveform.length / this.config.hop_length), + this.config.nb_max_frames, // 3000 + ) + } + ) + + const data = features.data; + const maxValue = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.max)(/** @type {Float32Array} */(data))[0]; + + for (let i = 0; i < data.length; ++i) { + data[i] = (Math.max(data[i], maxValue - 8.0) + 4.0) / 4.0; + } + + return features; + } + + /** + * Asynchronously extracts features from a given audio using the provided configuration. + * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. + * @returns {Promise<{ input_features: Tensor }>} A Promise resolving to an object containing the extracted input features as a Tensor. + */ + async _call(audio, { + max_length = null, + } = {}) { + (0,_base_feature_extraction_utils_js__WEBPACK_IMPORTED_MODULE_0__.validate_audio_inputs)(audio, 'WhisperFeatureExtractor'); + + let waveform; + const length = max_length ?? this.config.n_samples; + if (audio.length > length) { + if (audio.length > this.config.n_samples) { + console.warn( + "Attempting to extract features for audio longer than 30 seconds. " + + "If using a pipeline to extract transcript from a long audio clip, " + + "remember to specify `chunk_length_s` and/or `stride_length_s`." + ); + } + waveform = audio.slice(0, length); + } else { + // pad with zeros + waveform = new Float32Array(length); + waveform.set(audio); + } + + const features = await this._extract_fbank_features(waveform); + + return { + input_features: features.unsqueeze_(0) + }; + } +} + + +/***/ }), + +/***/ "./src/models/whisper/generation_whisper.js": +/*!**************************************************!*\ + !*** ./src/models/whisper/generation_whisper.js ***! + \**************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ WhisperGenerationConfig: () => (/* binding */ WhisperGenerationConfig) +/* harmony export */ }); +/* harmony import */ var _generation_configuration_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../generation/configuration_utils.js */ "./src/generation/configuration_utils.js"); + + +class WhisperGenerationConfig extends _generation_configuration_utils_js__WEBPACK_IMPORTED_MODULE_0__.GenerationConfig { + + /** + * Whether to return the timestamps with the text. This enables the `WhisperTimestampsLogitsProcessor`. + * @type {boolean} + */ + return_timestamps = null; + + /** + * Whether to return token-level timestamps + * with the text. This can be used with or without the `return_timestamps` option. To get word-level + * timestamps, use the tokenizer to group the tokens into words. + * @type {boolean} + */ + return_token_timestamps = null; + + /** + * The number of audio frames available in this chunk. This is only used generating word-level timestamps. + * @type {number} + */ + num_frames = null; + + /** + * Alignment heads to predict word-level timestamps. This is a list of [layer, head] pairs that + * select the cross-attention heads that are highly correlated to word-level timing. + * @type {[number, number][]} + */ + alignment_heads = null; + + /** + * Task to use for generation, either "translate" or "transcribe". + * @type {string} + */ + task = null; + + /** + * Language token to use for generation, can be either in the form of `<|en|>`, `en` or `english`. + * You can find all the possible language tokens in the `model.generation_config.lang_to_id` dictionary. + * @type {string} + */ + language = null; + + /** + * The id of the `"<|notimestamps|>"` token. + * @type {number} + */ + no_timestamps_token_id = null; + + /** + * Rank-1 list of token IDs created by passing text to [`~WhisperProcessor.get_prompt_ids`] that is + * provided as a prompt to each chunk. This can be used to provide or "prompt-engineer" a context for + * transcription, e.g. custom vocabularies or proper nouns to make it more likely to predict those words + * correctly. It cannot be used in conjunction with `decoder_start_token_id` as it overwrites this value. + * @type {number[]} + */ + prompt_ids = null; + + /** + * Whether the model is multilingual or not. + * @type {boolean} + */ + is_multilingual = null; + + /** + * (Optional) A mapping from language tokens to their corresponding IDs. + * Only required if the model is multilingual. + * @type {Record|null} + */ + lang_to_id = null; + + /** + * (Optional) A mapping from task tokens to their corresponding IDs. + * @type {Record|null} + */ + task_to_id = null; + + /** + * Used to set the maximum value of the initial timestamp. This is used to prevent the model from + * predicting timestamps that are too far in the future. + * @type {number} + */ + max_initial_timestamp_index = 1; +} + +/** + * @typedef {import('../../generation/parameters.js').GenerationFunctionParameters & {generation_config: WhisperGenerationConfig} & WhisperGenerationConfig} WhisperGenerationFunctionParameters + */ + + +/***/ }), + +/***/ "./src/models/whisper/processing_whisper.js": +/*!**************************************************!*\ + !*** ./src/models/whisper/processing_whisper.js ***! + \**************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ WhisperProcessor: () => (/* binding */ WhisperProcessor) +/* harmony export */ }); +/* harmony import */ var _auto_feature_extraction_auto_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../auto/feature_extraction_auto.js */ "./src/models/auto/feature_extraction_auto.js"); +/* harmony import */ var _tokenizers_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../../tokenizers.js */ "./src/tokenizers.js"); +/* harmony import */ var _base_processing_utils_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../../base/processing_utils.js */ "./src/base/processing_utils.js"); + + + + +/** + * Represents a WhisperProcessor that extracts features from an audio input. + */ +class WhisperProcessor extends _base_processing_utils_js__WEBPACK_IMPORTED_MODULE_2__.Processor { + static tokenizer_class = _tokenizers_js__WEBPACK_IMPORTED_MODULE_1__.AutoTokenizer + static feature_extractor_class = _auto_feature_extraction_auto_js__WEBPACK_IMPORTED_MODULE_0__.AutoFeatureExtractor + + /** + * Calls the feature_extractor function with the given audio input. + * @param {any} audio The audio input to extract features from. + * @returns {Promise} A Promise that resolves with the extracted features. + */ + async _call(audio) { + return await this.feature_extractor(audio); + } +} + + + +/***/ }), + +/***/ "./src/models/yolos/image_processing_yolos.js": +/*!****************************************************!*\ + !*** ./src/models/yolos/image_processing_yolos.js ***! + \****************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ YolosFeatureExtractor: () => (/* binding */ YolosFeatureExtractor), +/* harmony export */ YolosImageProcessor: () => (/* binding */ YolosImageProcessor) +/* harmony export */ }); +/* harmony import */ var _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../base/image_processors_utils.js */ "./src/base/image_processors_utils.js"); + + +class YolosImageProcessor extends _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.ImageProcessor { + /** @type {typeof post_process_object_detection} */ + post_process_object_detection(...args) { + return (0,_base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_0__.post_process_object_detection)(...args); + } +} +class YolosFeatureExtractor extends YolosImageProcessor { } + + +/***/ }), + +/***/ "./src/ops/registry.js": +/*!*****************************!*\ + !*** ./src/ops/registry.js ***! + \*****************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ TensorOpRegistry: () => (/* binding */ TensorOpRegistry) +/* harmony export */ }); +/* harmony import */ var _backends_onnx_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../backends/onnx.js */ "./src/backends/onnx.js"); +/* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../utils/tensor.js */ "./src/utils/tensor.js"); +/* harmony import */ var _env_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../env.js */ "./src/env.js"); + + + + +const IS_WEB_ENV = _env_js__WEBPACK_IMPORTED_MODULE_2__.apis.IS_BROWSER_ENV || _env_js__WEBPACK_IMPORTED_MODULE_2__.apis.IS_WEBWORKER_ENV; +/** + * Asynchronously creates a wrapper function for running an ONNX inference session. + * + * @param {number[]} session_bytes The session data in bytes. + * @param {import('onnxruntime-common').InferenceSession.SessionOptions} session_options The options for the ONNX session. + * @template {string | [string] | string[]} T + * @param {T} names The name(s) of the output tensor(s). + * + * @returns {Promise): Promise>} + * The wrapper function for running the ONNX inference session. + */ +const wrap = async (session_bytes, session_options, names) => { + const session = await (0,_backends_onnx_js__WEBPACK_IMPORTED_MODULE_0__.createInferenceSession)( + new Uint8Array(session_bytes), session_options, + ); + + /** @type {Promise} */ + let chain = Promise.resolve(); + + return /** @type {any} */(async (/** @type {Record} */ inputs) => { + const proxied = (0,_backends_onnx_js__WEBPACK_IMPORTED_MODULE_0__.isONNXProxy)(); + const ortFeed = Object.fromEntries(Object.entries(inputs).map(([k, v]) => [k, (proxied ? v.clone() : v).ort_tensor])); + + // When running in-browser via WASM, we need to chain calls to session.run to avoid "Error: Session already started" + const outputs = await (chain = IS_WEB_ENV ? chain.then(() => session.run(ortFeed)) : session.run(ortFeed)); + + if (Array.isArray(names)) { + return names.map((n) => new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor(outputs[n])); + } else { + return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor(outputs[/** @type {string} */(names)]); + } + }) +} + +// In-memory registry of initialized ONNX operators +class TensorOpRegistry { + static session_options = { + // TODO: Allow for multiple execution providers + // executionProviders: ['webgpu'], + }; + + static get nearest_interpolate_4d() { + if (!this._nearest_interpolate_4d) { + this._nearest_interpolate_4d = wrap( + [8, 10, 18, 0, 58, 129, 1, 10, 41, 10, 1, 120, 10, 0, 10, 0, 10, 1, 115, 18, 1, 121, 34, 6, 82, 101, 115, 105, 122, 101, 42, 18, 10, 4, 109, 111, 100, 101, 34, 7, 110, 101, 97, 114, 101, 115, 116, 160, 1, 3, 18, 1, 114, 90, 31, 10, 1, 120, 18, 26, 10, 24, 8, 1, 18, 20, 10, 3, 18, 1, 98, 10, 3, 18, 1, 99, 10, 3, 18, 1, 104, 10, 3, 18, 1, 119, 90, 15, 10, 1, 115, 18, 10, 10, 8, 8, 7, 18, 4, 10, 2, 8, 4, 98, 31, 10, 1, 121, 18, 26, 10, 24, 8, 1, 18, 20, 10, 3, 18, 1, 98, 10, 3, 18, 1, 99, 10, 3, 18, 1, 104, 10, 3, 18, 1, 119, 66, 2, 16, 21], + this.session_options, + 'y', + ); + } + return this._nearest_interpolate_4d; + } + static get bilinear_interpolate_4d() { + if (!this._bilinear_interpolate_4d) { + this._bilinear_interpolate_4d = wrap( + [8, 9, 18, 0, 58, 128, 1, 10, 40, 10, 1, 120, 10, 0, 10, 0, 10, 1, 115, 18, 1, 121, 34, 6, 82, 101, 115, 105, 122, 101, 42, 17, 10, 4, 109, 111, 100, 101, 34, 6, 108, 105, 110, 101, 97, 114, 160, 1, 3, 18, 1, 114, 90, 31, 10, 1, 120, 18, 26, 10, 24, 8, 1, 18, 20, 10, 3, 18, 1, 98, 10, 3, 18, 1, 99, 10, 3, 18, 1, 104, 10, 3, 18, 1, 119, 90, 15, 10, 1, 115, 18, 10, 10, 8, 8, 7, 18, 4, 10, 2, 8, 4, 98, 31, 10, 1, 121, 18, 26, 10, 24, 8, 1, 18, 20, 10, 3, 18, 1, 98, 10, 3, 18, 1, 99, 10, 3, 18, 1, 104, 10, 3, 18, 1, 119, 66, 2, 16, 20], + this.session_options, + 'y', + ); + } + return this._bilinear_interpolate_4d; + } + + static get bicubic_interpolate_4d() { + if (!this._bicubic_interpolate_4d) { + this._bicubic_interpolate_4d = wrap( + [8, 9, 18, 0, 58, 127, 10, 39, 10, 1, 120, 10, 0, 10, 0, 10, 1, 115, 18, 1, 121, 34, 6, 82, 101, 115, 105, 122, 101, 42, 16, 10, 4, 109, 111, 100, 101, 34, 5, 99, 117, 98, 105, 99, 160, 1, 3, 18, 1, 114, 90, 31, 10, 1, 120, 18, 26, 10, 24, 8, 1, 18, 20, 10, 3, 18, 1, 98, 10, 3, 18, 1, 99, 10, 3, 18, 1, 104, 10, 3, 18, 1, 119, 90, 15, 10, 1, 115, 18, 10, 10, 8, 8, 7, 18, 4, 10, 2, 8, 4, 98, 31, 10, 1, 121, 18, 26, 10, 24, 8, 1, 18, 20, 10, 3, 18, 1, 98, 10, 3, 18, 1, 99, 10, 3, 18, 1, 104, 10, 3, 18, 1, 119, 66, 2, 16, 20], + this.session_options, + 'y', + ); + } + return this._bicubic_interpolate_4d; + } + + static get matmul() { + if (!this._matmul) { + this._matmul = wrap( + [8, 9, 18, 0, 58, 55, 10, 17, 10, 1, 97, 10, 1, 98, 18, 1, 99, 34, 6, 77, 97, 116, 77, 117, 108, 18, 1, 114, 90, 9, 10, 1, 97, 18, 4, 10, 2, 8, 1, 90, 9, 10, 1, 98, 18, 4, 10, 2, 8, 1, 98, 9, 10, 1, 99, 18, 4, 10, 2, 8, 1, 66, 2, 16, 20], + this.session_options, + 'c', + ); + } + return this._matmul; + } + + static get stft() { + if (!this._stft) { + this._stft = wrap( + [8, 7, 18, 0, 58, 148, 1, 10, 38, 10, 1, 115, 10, 1, 106, 10, 1, 119, 10, 1, 108, 18, 1, 111, 34, 4, 83, 84, 70, 84, 42, 15, 10, 8, 111, 110, 101, 115, 105, 100, 101, 100, 24, 1, 160, 1, 2, 18, 1, 115, 90, 26, 10, 1, 115, 18, 21, 10, 19, 8, 1, 18, 15, 10, 3, 18, 1, 98, 10, 3, 18, 1, 115, 10, 3, 18, 1, 99, 90, 11, 10, 1, 106, 18, 6, 10, 4, 8, 7, 18, 0, 90, 16, 10, 1, 119, 18, 11, 10, 9, 8, 1, 18, 5, 10, 3, 18, 1, 119, 90, 11, 10, 1, 108, 18, 6, 10, 4, 8, 7, 18, 0, 98, 31, 10, 1, 111, 18, 26, 10, 24, 8, 1, 18, 20, 10, 3, 18, 1, 98, 10, 3, 18, 1, 102, 10, 3, 18, 1, 100, 10, 3, 18, 1, 99, 66, 2, 16, 17], + this.session_options, + 'o', + ) + } + return this._stft; + } + + static get rfft() { + if (!this._rfft) { + this._rfft = wrap( + [8, 9, 18, 0, 58, 97, 10, 33, 10, 1, 120, 10, 0, 10, 1, 97, 18, 1, 121, 34, 3, 68, 70, 84, 42, 15, 10, 8, 111, 110, 101, 115, 105, 100, 101, 100, 24, 1, 160, 1, 2, 18, 1, 100, 90, 21, 10, 1, 120, 18, 16, 10, 14, 8, 1, 18, 10, 10, 3, 18, 1, 115, 10, 3, 18, 1, 99, 90, 11, 10, 1, 97, 18, 6, 10, 4, 8, 7, 18, 0, 98, 21, 10, 1, 121, 18, 16, 10, 14, 8, 1, 18, 10, 10, 3, 18, 1, 115, 10, 3, 18, 1, 99, 66, 2, 16, 20], + this.session_options, + 'y', + ) + } + return this._rfft; + } + + static get top_k() { + if (!this._top_k) { + this._top_k = wrap( + [8, 10, 18, 0, 58, 73, 10, 18, 10, 1, 120, 10, 1, 107, 18, 1, 118, 18, 1, 105, 34, 4, 84, 111, 112, 75, 18, 1, 116, 90, 9, 10, 1, 120, 18, 4, 10, 2, 8, 1, 90, 15, 10, 1, 107, 18, 10, 10, 8, 8, 7, 18, 4, 10, 2, 8, 1, 98, 9, 10, 1, 118, 18, 4, 10, 2, 8, 1, 98, 9, 10, 1, 105, 18, 4, 10, 2, 8, 7, 66, 2, 16, 21], + this.session_options, + [ /* Values */ 'v', /* Indices */ 'i'] + ) + } + return this._top_k; + } + + static get slice() { + if (!this._slice) { + this._slice = wrap( + [8, 7, 18, 0, 58, 96, 10, 25, 10, 1, 120, 10, 1, 115, 10, 1, 101, 10, 1, 97, 10, 1, 116, 18, 1, 121, 34, 5, 83, 108, 105, 99, 101, 18, 1, 114, 90, 9, 10, 1, 120, 18, 4, 10, 2, 8, 1, 90, 9, 10, 1, 115, 18, 4, 10, 2, 8, 7, 90, 9, 10, 1, 101, 18, 4, 10, 2, 8, 7, 90, 9, 10, 1, 97, 18, 4, 10, 2, 8, 7, 90, 9, 10, 1, 116, 18, 4, 10, 2, 8, 7, 98, 9, 10, 1, 121, 18, 4, 10, 2, 8, 1, 66, 2, 16, 13], + this.session_options, + 'y', + ) + } + return this._slice; + } +} + + +/***/ }), + +/***/ "./src/pipelines.js": +/*!**************************!*\ + !*** ./src/pipelines.js ***! + \**************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ AudioClassificationPipeline: () => (/* binding */ AudioClassificationPipeline), +/* harmony export */ AutomaticSpeechRecognitionPipeline: () => (/* binding */ AutomaticSpeechRecognitionPipeline), +/* harmony export */ BackgroundRemovalPipeline: () => (/* binding */ BackgroundRemovalPipeline), +/* harmony export */ DepthEstimationPipeline: () => (/* binding */ DepthEstimationPipeline), +/* harmony export */ DocumentQuestionAnsweringPipeline: () => (/* binding */ DocumentQuestionAnsweringPipeline), +/* harmony export */ FeatureExtractionPipeline: () => (/* binding */ FeatureExtractionPipeline), +/* harmony export */ FillMaskPipeline: () => (/* binding */ FillMaskPipeline), +/* harmony export */ ImageClassificationPipeline: () => (/* binding */ ImageClassificationPipeline), +/* harmony export */ ImageFeatureExtractionPipeline: () => (/* binding */ ImageFeatureExtractionPipeline), +/* harmony export */ ImageSegmentationPipeline: () => (/* binding */ ImageSegmentationPipeline), +/* harmony export */ ImageToImagePipeline: () => (/* binding */ ImageToImagePipeline), +/* harmony export */ ImageToTextPipeline: () => (/* binding */ ImageToTextPipeline), +/* harmony export */ ObjectDetectionPipeline: () => (/* binding */ ObjectDetectionPipeline), +/* harmony export */ Pipeline: () => (/* binding */ Pipeline), +/* harmony export */ QuestionAnsweringPipeline: () => (/* binding */ QuestionAnsweringPipeline), +/* harmony export */ SummarizationPipeline: () => (/* binding */ SummarizationPipeline), +/* harmony export */ Text2TextGenerationPipeline: () => (/* binding */ Text2TextGenerationPipeline), +/* harmony export */ TextClassificationPipeline: () => (/* binding */ TextClassificationPipeline), +/* harmony export */ TextGenerationPipeline: () => (/* binding */ TextGenerationPipeline), +/* harmony export */ TextToAudioPipeline: () => (/* binding */ TextToAudioPipeline), +/* harmony export */ TokenClassificationPipeline: () => (/* binding */ TokenClassificationPipeline), +/* harmony export */ TranslationPipeline: () => (/* binding */ TranslationPipeline), +/* harmony export */ ZeroShotAudioClassificationPipeline: () => (/* binding */ ZeroShotAudioClassificationPipeline), +/* harmony export */ ZeroShotClassificationPipeline: () => (/* binding */ ZeroShotClassificationPipeline), +/* harmony export */ ZeroShotImageClassificationPipeline: () => (/* binding */ ZeroShotImageClassificationPipeline), +/* harmony export */ ZeroShotObjectDetectionPipeline: () => (/* binding */ ZeroShotObjectDetectionPipeline), +/* harmony export */ pipeline: () => (/* binding */ pipeline) +/* harmony export */ }); +/* harmony import */ var _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./tokenizers.js */ "./src/tokenizers.js"); +/* harmony import */ var _models_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./models.js */ "./src/models.js"); +/* harmony import */ var _models_auto_processing_auto_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./models/auto/processing_auto.js */ "./src/models/auto/processing_auto.js"); +/* harmony import */ var _base_processing_utils_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./base/processing_utils.js */ "./src/base/processing_utils.js"); +/* harmony import */ var _utils_generic_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ./utils/generic.js */ "./src/utils/generic.js"); +/* harmony import */ var _utils_core_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ./utils/core.js */ "./src/utils/core.js"); +/* harmony import */ var _utils_maths_js__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! ./utils/maths.js */ "./src/utils/maths.js"); +/* harmony import */ var _utils_audio_js__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__(/*! ./utils/audio.js */ "./src/utils/audio.js"); +/* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__(/*! ./utils/tensor.js */ "./src/utils/tensor.js"); +/* harmony import */ var _utils_image_js__WEBPACK_IMPORTED_MODULE_9__ = __webpack_require__(/*! ./utils/image.js */ "./src/utils/image.js"); +/** + * @file Pipelines provide a high-level, easy to use, API for running machine learning models. + * + * **Example:** Instantiate pipeline using the `pipeline` function. + * ```javascript + * import { pipeline } from '@huggingface/transformers'; + * + * const classifier = await pipeline('sentiment-analysis'); + * const output = await classifier('I love transformers!'); + * // [{'label': 'POSITIVE', 'score': 0.999817686}] + * ``` + * + * @module pipelines + */ + + + + + + + + + + + + + + + +/** + * @typedef {string | RawImage | URL | Blob | HTMLCanvasElement | OffscreenCanvas} ImageInput + * @typedef {ImageInput|ImageInput[]} ImagePipelineInputs + */ + +/** + * Prepare images for further tasks. + * @param {ImagePipelineInputs} images images to prepare. + * @returns {Promise} returns processed images. + * @private + */ +async function prepareImages(images) { + if (!Array.isArray(images)) { + images = [images]; + } + + // Possibly convert any non-images to images + return await Promise.all(images.map(x => _utils_image_js__WEBPACK_IMPORTED_MODULE_9__.RawImage.read(x))); +} + +/** + * @typedef {string | URL | Float32Array | Float64Array} AudioInput + * @typedef {AudioInput|AudioInput[]} AudioPipelineInputs + */ + +/** + * Prepare audios for further tasks. + * @param {AudioPipelineInputs} audios audios to prepare. + * @param {number} sampling_rate sampling rate of the audios. + * @returns {Promise} The preprocessed audio data. + * @private + */ +async function prepareAudios(audios, sampling_rate) { + if (!Array.isArray(audios)) { + audios = [audios]; + } + + return await Promise.all(audios.map(x => { + if (typeof x === 'string' || x instanceof URL) { + return (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_7__.read_audio)(x, sampling_rate); + } else if (x instanceof Float64Array) { + return new Float32Array(x); + } + return x; + })); +} + +/** + * @typedef {Object} BoundingBox + * @property {number} xmin The minimum x coordinate of the bounding box. + * @property {number} ymin The minimum y coordinate of the bounding box. + * @property {number} xmax The maximum x coordinate of the bounding box. + * @property {number} ymax The maximum y coordinate of the bounding box. + */ + +/** + * Helper function to convert list [xmin, xmax, ymin, ymax] into object { "xmin": xmin, ... } + * @param {number[]} box The bounding box as a list. + * @param {boolean} asInteger Whether to cast to integers. + * @returns {BoundingBox} The bounding box as an object. + * @private + */ +function get_bounding_box(box, asInteger) { + if (asInteger) { + box = box.map(x => x | 0); + } + const [xmin, ymin, xmax, ymax] = box; + + return { xmin, ymin, xmax, ymax }; +} + + +/** + * @callback DisposeType Disposes the item. + * @returns {Promise} A promise that resolves when the item has been disposed. + * + * @typedef {Object} Disposable + * @property {DisposeType} dispose A promise that resolves when the pipeline has been disposed. + */ + +/** + * The Pipeline class is the class from which all pipelines inherit. + * Refer to this class for methods shared across different pipelines. + */ +class Pipeline extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_4__.Callable { + /** + * Create a new Pipeline. + * @param {Object} options An object containing the following properties: + * @param {string} [options.task] The task of the pipeline. Useful for specifying subtasks. + * @param {PreTrainedModel} [options.model] The model used by the pipeline. + * @param {PreTrainedTokenizer} [options.tokenizer=null] The tokenizer used by the pipeline (if any). + * @param {Processor} [options.processor=null] The processor used by the pipeline (if any). + */ + constructor({ task, model, tokenizer = null, processor = null }) { + super(); + this.task = task; + this.model = model; + this.tokenizer = tokenizer; + this.processor = processor; + } + + /** @type {DisposeType} */ + async dispose() { + await this.model.dispose(); + } +} + +/** + * @typedef {Object} ModelTokenizerConstructorArgs + * @property {string} task The task of the pipeline. Useful for specifying subtasks. + * @property {PreTrainedModel} model The model used by the pipeline. + * @property {PreTrainedTokenizer} tokenizer The tokenizer used by the pipeline. + * + * @typedef {ModelTokenizerConstructorArgs} TextPipelineConstructorArgs An object used to instantiate a text-based pipeline. + */ + +/** + * @typedef {Object} ModelProcessorConstructorArgs + * @property {string} task The task of the pipeline. Useful for specifying subtasks. + * @property {PreTrainedModel} model The model used by the pipeline. + * @property {Processor} processor The processor used by the pipeline. + * + * @typedef {ModelProcessorConstructorArgs} AudioPipelineConstructorArgs An object used to instantiate an audio-based pipeline. + * @typedef {ModelProcessorConstructorArgs} ImagePipelineConstructorArgs An object used to instantiate an image-based pipeline. + */ + + +/** + * @typedef {Object} ModelTokenizerProcessorConstructorArgs + * @property {string} task The task of the pipeline. Useful for specifying subtasks. + * @property {PreTrainedModel} model The model used by the pipeline. + * @property {PreTrainedTokenizer} tokenizer The tokenizer used by the pipeline. + * @property {Processor} processor The processor used by the pipeline. + * + * @typedef {ModelTokenizerProcessorConstructorArgs} TextAudioPipelineConstructorArgs An object used to instantiate a text- and audio-based pipeline. + * @typedef {ModelTokenizerProcessorConstructorArgs} TextImagePipelineConstructorArgs An object used to instantiate a text- and image-based pipeline. + */ + +/** + * @typedef {Object} TextClassificationSingle + * @property {string} label The label predicted. + * @property {number} score The corresponding probability. + * @typedef {TextClassificationSingle[]} TextClassificationOutput + * + * @typedef {Object} TextClassificationPipelineOptions Parameters specific to text classification pipelines. + * @property {number} [top_k=1] The number of top predictions to be returned. + * + * @callback TextClassificationPipelineCallback Classify the text(s) given as inputs. + * @param {string|string[]} texts The input text(s) to be classified. + * @param {TextClassificationPipelineOptions} [options] The options to use for text classification. + * @returns {Promise} An array or object containing the predicted labels and scores. + * + * @typedef {TextPipelineConstructorArgs & TextClassificationPipelineCallback & Disposable} TextClassificationPipelineType + */ + +/** + * Text classification pipeline using any `ModelForSequenceClassification`. + * + * **Example:** Sentiment-analysis w/ `Xenova/distilbert-base-uncased-finetuned-sst-2-english`. + * ```javascript + * const classifier = await pipeline('sentiment-analysis', 'Xenova/distilbert-base-uncased-finetuned-sst-2-english'); + * const output = await classifier('I love transformers!'); + * // [{ label: 'POSITIVE', score: 0.999788761138916 }] + * ``` + * + * **Example:** Multilingual sentiment-analysis w/ `Xenova/bert-base-multilingual-uncased-sentiment` (and return top 5 classes). + * ```javascript + * const classifier = await pipeline('sentiment-analysis', 'Xenova/bert-base-multilingual-uncased-sentiment'); + * const output = await classifier('Le meilleur film de tous les temps.', { top_k: 5 }); + * // [ + * // { label: '5 stars', score: 0.9610759615898132 }, + * // { label: '4 stars', score: 0.03323351591825485 }, + * // { label: '3 stars', score: 0.0036155181005597115 }, + * // { label: '1 star', score: 0.0011325967498123646 }, + * // { label: '2 stars', score: 0.0009423971059732139 } + * // ] + * ``` + * + * **Example:** Toxic comment classification w/ `Xenova/toxic-bert` (and return all classes). + * ```javascript + * const classifier = await pipeline('text-classification', 'Xenova/toxic-bert'); + * const output = await classifier('I hate you!', { top_k: null }); + * // [ + * // { label: 'toxic', score: 0.9593140482902527 }, + * // { label: 'insult', score: 0.16187334060668945 }, + * // { label: 'obscene', score: 0.03452680632472038 }, + * // { label: 'identity_hate', score: 0.0223250575363636 }, + * // { label: 'threat', score: 0.019197041168808937 }, + * // { label: 'severe_toxic', score: 0.005651099607348442 } + * // ] + * ``` + */ +class TextClassificationPipeline extends (/** @type {new (options: TextPipelineConstructorArgs) => TextClassificationPipelineType} */ (Pipeline)) { + + /** + * Create a new TextClassificationPipeline. + * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {TextClassificationPipelineCallback} */ + async _call(texts, { + top_k = 1 + } = {}) { + + // Run tokenization + const model_inputs = this.tokenizer(texts, { + padding: true, + truncation: true, + }); + + // Run model + const outputs = await this.model(model_inputs) + + // TODO: Use softmax tensor function + const function_to_apply = + // @ts-expect-error TS2339 + this.model.config.problem_type === 'multi_label_classification' + ? batch => batch.sigmoid() + : batch => new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.Tensor( + 'float32', + (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_6__.softmax)(batch.data), + batch.dims, + ); // single_label_classification (default) + + // @ts-expect-error TS2339 + const id2label = this.model.config.id2label; + + const toReturn = []; + for (const batch of outputs.logits) { + const output = function_to_apply(batch); + + const scores = await (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.topk)(output, top_k); + + const values = scores[0].tolist(); + const indices = scores[1].tolist(); + const vals = indices.map((x, i) => ({ + label: id2label ? id2label[x] : `LABEL_${x}`, + score: values[i], + })); + if (top_k === 1) { + toReturn.push(...vals); + } else { + toReturn.push(vals); + } + } + + return Array.isArray(texts) || top_k === 1 ? /** @type {TextClassificationOutput} */ (toReturn) : /** @type {TextClassificationOutput[]} */ (toReturn)[0]; + } +} + +/** + * @typedef {Object} TokenClassificationSingle + * @property {string} word The token/word classified. This is obtained by decoding the selected tokens. + * @property {number} score The corresponding probability for `entity`. + * @property {string} entity The entity predicted for that token/word. + * @property {number} index The index of the corresponding token in the sentence. + * @property {number} [start] The index of the start of the corresponding entity in the sentence. + * @property {number} [end] The index of the end of the corresponding entity in the sentence. + * @typedef {TokenClassificationSingle[]} TokenClassificationOutput + * + * @typedef {Object} TokenClassificationPipelineOptions Parameters specific to token classification pipelines. + * @property {string[]} [ignore_labels] A list of labels to ignore. + * + * @callback TokenClassificationPipelineCallback Classify each token of the text(s) given as inputs. + * @param {string|string[]} texts One or several texts (or one list of texts) for token classification. + * @param {TokenClassificationPipelineOptions} [options] The options to use for token classification. + * @returns {Promise} The result. + * + * @typedef {TextPipelineConstructorArgs & TokenClassificationPipelineCallback & Disposable} TokenClassificationPipelineType + */ + +/** + * Named Entity Recognition pipeline using any `ModelForTokenClassification`. + * + * **Example:** Perform named entity recognition with `Xenova/bert-base-NER`. + * ```javascript + * const classifier = await pipeline('token-classification', 'Xenova/bert-base-NER'); + * const output = await classifier('My name is Sarah and I live in London'); + * // [ + * // { entity: 'B-PER', score: 0.9980202913284302, index: 4, word: 'Sarah' }, + * // { entity: 'B-LOC', score: 0.9994474053382874, index: 9, word: 'London' } + * // ] + * ``` + * + * **Example:** Perform named entity recognition with `Xenova/bert-base-NER` (and return all labels). + * ```javascript + * const classifier = await pipeline('token-classification', 'Xenova/bert-base-NER'); + * const output = await classifier('Sarah lives in the United States of America', { ignore_labels: [] }); + * // [ + * // { entity: 'B-PER', score: 0.9966587424278259, index: 1, word: 'Sarah' }, + * // { entity: 'O', score: 0.9987385869026184, index: 2, word: 'lives' }, + * // { entity: 'O', score: 0.9990072846412659, index: 3, word: 'in' }, + * // { entity: 'O', score: 0.9988298416137695, index: 4, word: 'the' }, + * // { entity: 'B-LOC', score: 0.9995510578155518, index: 5, word: 'United' }, + * // { entity: 'I-LOC', score: 0.9990395307540894, index: 6, word: 'States' }, + * // { entity: 'I-LOC', score: 0.9986724853515625, index: 7, word: 'of' }, + * // { entity: 'I-LOC', score: 0.9975294470787048, index: 8, word: 'America' } + * // ] + * ``` + */ +class TokenClassificationPipeline extends (/** @type {new (options: TextPipelineConstructorArgs) => TokenClassificationPipelineType} */ (Pipeline)) { + + /** + * Create a new TokenClassificationPipeline. + * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {TokenClassificationPipelineCallback} */ + async _call(texts, { + ignore_labels = ['O'], + } = {}) { + + const isBatched = Array.isArray(texts); + + // Run tokenization + const model_inputs = this.tokenizer(isBatched ? texts : [texts], { + padding: true, + truncation: true, + }); + + // Run model + const outputs = await this.model(model_inputs) + + const logits = outputs.logits; + // @ts-expect-error TS2339 + const id2label = this.model.config.id2label; + + const toReturn = []; + for (let i = 0; i < logits.dims[0]; ++i) { + const ids = model_inputs.input_ids[i]; + const batch = logits[i]; + + // List of tokens that aren't ignored + const tokens = []; + for (let j = 0; j < batch.dims[0]; ++j) { + const tokenData = batch[j]; + const topScoreIndex = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_6__.max)(tokenData.data)[1]; + + const entity = id2label ? id2label[topScoreIndex] : `LABEL_${topScoreIndex}`; + if (ignore_labels.includes(entity)) { + // We predicted a token that should be ignored. So, we skip it. + continue; + } + + // TODO add option to keep special tokens? + const word = this.tokenizer.decode([ids[j].item()], { skip_special_tokens: true }); + if (word === '') { + // Was a special token. So, we skip it. + continue; + } + + const scores = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_6__.softmax)(tokenData.data); + + tokens.push({ + entity: entity, + score: scores[topScoreIndex], + index: j, + word: word, + + // TODO: Add support for start and end + // start: null, + // end: null, + }); + } + toReturn.push(tokens); + } + return isBatched ? toReturn : toReturn[0]; + } +} + +/** + * @typedef {Object} QuestionAnsweringOutput + * @property {number} score The probability associated to the answer. + * @property {number} [start] The character start index of the answer (in the tokenized version of the input). + * @property {number} [end] The character end index of the answer (in the tokenized version of the input). + * @property {string} answer The answer to the question. + * + * @typedef {Object} QuestionAnsweringPipelineOptions Parameters specific to question answering pipelines. + * @property {number} [top_k=1] The number of top answer predictions to be returned. + * + * @callback QuestionAnsweringPipelineCallback Answer the question(s) given as inputs by using the context(s). + * @param {string|string[]} question One or several question(s) (must be used in conjunction with the `context` argument). + * @param {string|string[]} context One or several context(s) associated with the question(s) (must be used in conjunction with the `question` argument). + * @param {QuestionAnsweringPipelineOptions} [options] The options to use for question answering. + * @returns {Promise} An array or object containing the predicted answers and scores. + * + * @typedef {TextPipelineConstructorArgs & QuestionAnsweringPipelineCallback & Disposable} QuestionAnsweringPipelineType + */ + +/** + * Question Answering pipeline using any `ModelForQuestionAnswering`. + * + * **Example:** Run question answering with `Xenova/distilbert-base-uncased-distilled-squad`. + * ```javascript + * const answerer = await pipeline('question-answering', 'Xenova/distilbert-base-uncased-distilled-squad'); + * const question = 'Who was Jim Henson?'; + * const context = 'Jim Henson was a nice puppet.'; + * const output = await answerer(question, context); + * // { + * // answer: "a nice puppet", + * // score: 0.5768911502526741 + * // } + * ``` + */ +class QuestionAnsweringPipeline extends (/** @type {new (options: TextPipelineConstructorArgs) => QuestionAnsweringPipelineType} */ (Pipeline)) { + + /** + * Create a new QuestionAnsweringPipeline. + * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {QuestionAnsweringPipelineCallback} */ + async _call(question, context, { + top_k = 1 + } = {}) { + + // Run tokenization + const inputs = this.tokenizer(question, { + text_pair: context, + padding: true, + truncation: true, + }); + + const { start_logits, end_logits } = await this.model(inputs); + const input_ids = inputs.input_ids.tolist(); + const attention_mask = inputs.attention_mask.tolist(); + + // TODO: add support for `return_special_tokens_mask` + const special_tokens = this.tokenizer.all_special_ids; + + /** @type {QuestionAnsweringOutput[]} */ + const toReturn = []; + for (let j = 0; j < start_logits.dims[0]; ++j) { + const ids = input_ids[j]; + const sepIndex = ids.findIndex(x => + // We use == to match bigint with number + // @ts-ignore + x == this.tokenizer.sep_token_id + ); + + + const valid_mask = attention_mask[j].map((y, ix) => ( + y == 1 + && ( + ix === 0 // is cls_token + || ( + ix > sepIndex + && special_tokens.findIndex(x => x == ids[ix]) === -1 // token is not a special token (special_tokens_mask == 0) + ) + ) + )); + + const start = start_logits[j].tolist(); + const end = end_logits[j].tolist(); + + // Now, we mask out values that can't be in the answer + // NOTE: We keep the cls_token unmasked (some models use it to indicate unanswerable questions) + for (let i = 1; i < start.length; ++i) { + if ( + attention_mask[j] == 0 // is part of padding + || i <= sepIndex // is before the sep_token + || special_tokens.findIndex(x => x == ids[i]) !== -1 // Is a special token + ) { + // Make sure non-context indexes in the tensor cannot contribute to the softmax + start[i] = -Infinity; + end[i] = -Infinity; + } + } + + // Normalize logits and spans to retrieve the answer + const start_scores = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_6__.softmax)(start).map((x, i) => [x, i]); + const end_scores = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_6__.softmax)(end).map((x, i) => [x, i]); + + // Mask CLS + start_scores[0][0] = 0; + end_scores[0][0] = 0; + + // Generate all valid spans and select best ones + const options = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_5__.product)(start_scores, end_scores) + .filter(x => x[0][1] <= x[1][1]) + .map(x => [x[0][1], x[1][1], x[0][0] * x[1][0]]) + .sort((a, b) => b[2] - a[2]); + + for (let k = 0; k < Math.min(options.length, top_k); ++k) { + const [start, end, score] = options[k]; + + const answer_tokens = ids.slice(start, end + 1) + + const answer = this.tokenizer.decode(answer_tokens, { + skip_special_tokens: true, + }); + + // TODO add start and end? + // NOTE: HF returns character index + toReturn.push({ + answer, score + }); + } + } + + // Mimic HF's return type based on top_k + return (top_k === 1) ? toReturn[0] : toReturn; + } +} + + +/** + * @typedef {Object} FillMaskSingle + * @property {string} sequence The corresponding input with the mask token prediction. + * @property {number} score The corresponding probability. + * @property {number} token The predicted token id (to replace the masked one). + * @property {string} token_str The predicted token (to replace the masked one). + * @typedef {FillMaskSingle[]} FillMaskOutput + * + * @typedef {Object} FillMaskPipelineOptions Parameters specific to fill mask pipelines. + * @property {number} [top_k=5] When passed, overrides the number of predictions to return. + * + * @callback FillMaskPipelineCallback Fill the masked token in the text(s) given as inputs. + * @param {string|string[]} texts One or several texts (or one list of prompts) with masked tokens. + * @param {FillMaskPipelineOptions} [options] The options to use for masked language modelling. + * @returns {Promise} An array of objects containing the score, predicted token, predicted token string, + * and the sequence with the predicted token filled in, or an array of such arrays (one for each input text). + * If only one input text is given, the output will be an array of objects. + * @throws {Error} When the mask token is not found in the input text. + * + * @typedef {TextPipelineConstructorArgs & FillMaskPipelineCallback & Disposable} FillMaskPipelineType + */ + +/** + * Masked language modeling prediction pipeline using any `ModelWithLMHead`. + * + * **Example:** Perform masked language modelling (a.k.a. "fill-mask") with `Xenova/bert-base-uncased`. + * ```javascript + * const unmasker = await pipeline('fill-mask', 'Xenova/bert-base-cased'); + * const output = await unmasker('The goal of life is [MASK].'); + * // [ + * // { token_str: 'survival', score: 0.06137419492006302, token: 8115, sequence: 'The goal of life is survival.' }, + * // { token_str: 'love', score: 0.03902450203895569, token: 1567, sequence: 'The goal of life is love.' }, + * // { token_str: 'happiness', score: 0.03253183513879776, token: 9266, sequence: 'The goal of life is happiness.' }, + * // { token_str: 'freedom', score: 0.018736306577920914, token: 4438, sequence: 'The goal of life is freedom.' }, + * // { token_str: 'life', score: 0.01859794743359089, token: 1297, sequence: 'The goal of life is life.' } + * // ] + * ``` + * + * **Example:** Perform masked language modelling (a.k.a. "fill-mask") with `Xenova/bert-base-cased` (and return top result). + * ```javascript + * const unmasker = await pipeline('fill-mask', 'Xenova/bert-base-cased'); + * const output = await unmasker('The Milky Way is a [MASK] galaxy.', { top_k: 1 }); + * // [{ token_str: 'spiral', score: 0.6299987435340881, token: 14061, sequence: 'The Milky Way is a spiral galaxy.' }] + * ``` + */ +class FillMaskPipeline extends (/** @type {new (options: TextPipelineConstructorArgs) => FillMaskPipelineType} */ (Pipeline)) { + + /** + * Create a new FillMaskPipeline. + * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {FillMaskPipelineCallback} */ + async _call(texts, { + top_k = 5 + } = {}) { + + // Run tokenization + const model_inputs = this.tokenizer(texts, { + padding: true, + truncation: true, + }); + + // Run model + const { logits } = await this.model(model_inputs) + + const toReturn = []; + + /** @type {bigint[][]} */ + const input_ids = model_inputs.input_ids.tolist(); + for (let i = 0; i < input_ids.length; ++i) { + const ids = input_ids[i]; + const mask_token_index = ids.findIndex(x => + // We use == to match bigint with number + // @ts-ignore + x == this.tokenizer.mask_token_id + ); + if (mask_token_index === -1) { + throw Error(`Mask token (${this.tokenizer.mask_token}) not found in text.`) + } + const itemLogits = logits[i][mask_token_index]; + + const scores = await (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.topk)(new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.Tensor( + 'float32', + (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_6__.softmax)(itemLogits.data), + itemLogits.dims, + ), top_k); + const values = scores[0].tolist(); + const indices = scores[1].tolist(); + + toReturn.push(indices.map((x, i) => { + const sequence = ids.slice(); + sequence[mask_token_index] = x; + + return { + score: values[i], + token: Number(x), + token_str: this.tokenizer.decode([x]), + sequence: this.tokenizer.decode(sequence, { skip_special_tokens: true }), + } + })); + } + return Array.isArray(texts) ? toReturn : toReturn[0]; + } +} + + +/** + * @typedef {Object} Text2TextGenerationSingle + * @property {string} generated_text The generated text. + * @typedef {Text2TextGenerationSingle[]} Text2TextGenerationOutput + * + * @callback Text2TextGenerationPipelineCallback Generate the output text(s) using text(s) given as inputs. + * @param {string|string[]} texts Input text for the encoder. + * @param {Partial} [options] Additional keyword arguments to pass along to the generate method of the model. + * @returns {Promise} + * + * @typedef {TextPipelineConstructorArgs & Text2TextGenerationPipelineCallback & Disposable} Text2TextGenerationPipelineType + */ + +/** + * Text2TextGenerationPipeline class for generating text using a model that performs text-to-text generation tasks. + * + * **Example:** Text-to-text generation w/ `Xenova/LaMini-Flan-T5-783M`. + * ```javascript + * const generator = await pipeline('text2text-generation', 'Xenova/LaMini-Flan-T5-783M'); + * const output = await generator('how can I become more healthy?', { + * max_new_tokens: 100, + * }); + * // [{ generated_text: "To become more healthy, you can: 1. Eat a balanced diet with plenty of fruits, vegetables, whole grains, lean proteins, and healthy fats. 2. Stay hydrated by drinking plenty of water. 3. Get enough sleep and manage stress levels. 4. Avoid smoking and excessive alcohol consumption. 5. Regularly exercise and maintain a healthy weight. 6. Practice good hygiene and sanitation. 7. Seek medical attention if you experience any health issues." }] + * ``` + */ +class Text2TextGenerationPipeline extends (/** @type {new (options: TextPipelineConstructorArgs) => Text2TextGenerationPipelineType} */ (Pipeline)) { + /** @type {'generated_text'} */ + _key = 'generated_text'; + + /** + * Create a new Text2TextGenerationPipeline. + * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {Text2TextGenerationPipelineCallback} */ + async _call(texts, generate_kwargs = {}) { + if (!Array.isArray(texts)) { + texts = [texts]; + } + + + // Add global prefix, if present + // @ts-expect-error TS2339 + if (this.model.config.prefix) { + // @ts-expect-error TS2339 + texts = texts.map(x => this.model.config.prefix + x) + } + + // Handle task specific params: + // @ts-expect-error TS2339 + const task_specific_params = this.model.config.task_specific_params + if (task_specific_params && task_specific_params[this.task]) { + // Add prefixes, if present + if (task_specific_params[this.task].prefix) { + texts = texts.map(x => task_specific_params[this.task].prefix + x) + } + + // TODO update generation config + } + + const tokenizer = this.tokenizer; + const tokenizer_options = { + padding: true, + truncation: true, + } + let inputs; + if (this instanceof TranslationPipeline && '_build_translation_inputs' in tokenizer) { + // TODO: move to Translation pipeline? + // Currently put here to avoid code duplication + // @ts-ignore + inputs = tokenizer._build_translation_inputs(texts, tokenizer_options, generate_kwargs); + + } else { + inputs = tokenizer(texts, tokenizer_options); + } + + const outputTokenIds = await this.model.generate({ ...inputs, ...generate_kwargs }); + return tokenizer.batch_decode(/** @type {Tensor} */(outputTokenIds), { + skip_special_tokens: true, + }).map(text => ({ [this._key]: text })); + } +} + + +/** + * @typedef {Object} SummarizationSingle + * @property {string} summary_text The summary text. + * @typedef {SummarizationSingle[]} SummarizationOutput + * + * @callback SummarizationPipelineCallback Summarize the text(s) given as inputs. + * @param {string|string[]} texts One or several articles (or one list of articles) to summarize. + * @param {import('./generation/configuration_utils.js').GenerationConfig} [options] Additional keyword arguments to pass along to the generate method of the model. + * @returns {Promise} + * + * @typedef {TextPipelineConstructorArgs & SummarizationPipelineCallback & Disposable} SummarizationPipelineType + */ + +/** + * A pipeline for summarization tasks, inheriting from Text2TextGenerationPipeline. + * + * **Example:** Summarization w/ `Xenova/distilbart-cnn-6-6`. + * ```javascript + * const generator = await pipeline('summarization', 'Xenova/distilbart-cnn-6-6'); + * const text = 'The tower is 324 metres (1,063 ft) tall, about the same height as an 81-storey building, ' + + * 'and the tallest structure in Paris. Its base is square, measuring 125 metres (410 ft) on each side. ' + + * 'During its construction, the Eiffel Tower surpassed the Washington Monument to become the tallest ' + + * 'man-made structure in the world, a title it held for 41 years until the Chrysler Building in New ' + + * 'York City was finished in 1930. It was the first structure to reach a height of 300 metres. Due to ' + + * 'the addition of a broadcasting aerial at the top of the tower in 1957, it is now taller than the ' + + * 'Chrysler Building by 5.2 metres (17 ft). Excluding transmitters, the Eiffel Tower is the second ' + + * 'tallest free-standing structure in France after the Millau Viaduct.'; + * const output = await generator(text, { + * max_new_tokens: 100, + * }); + * // [{ summary_text: ' The Eiffel Tower is about the same height as an 81-storey building and the tallest structure in Paris. It is the second tallest free-standing structure in France after the Millau Viaduct.' }] + * ``` + */ +class SummarizationPipeline extends (/** @type {new (options: TextPipelineConstructorArgs) => SummarizationPipelineType} */ (/** @type {any} */ (Text2TextGenerationPipeline))) { + /** @type {'summary_text'} */ + _key = 'summary_text'; + + /** + * Create a new SummarizationPipeline. + * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } +} + + +/** + * @typedef {Object} TranslationSingle + * @property {string} translation_text The translated text. + * @typedef {TranslationSingle[]} TranslationOutput + * + * @callback TranslationPipelineCallback Translate the text(s) given as inputs. + * @param {string|string[]} texts Texts to be translated. + * @param {import('./generation/configuration_utils.js').GenerationConfig} [options] Additional keyword arguments to pass along to the generate method of the model. + * @returns {Promise} + * + * @typedef {TextPipelineConstructorArgs & TranslationPipelineCallback & Disposable} TranslationPipelineType + */ + +/** + * Translates text from one language to another. + * + * **Example:** Multilingual translation w/ `Xenova/nllb-200-distilled-600M`. + * + * See [here](https://github.com/facebookresearch/flores/blob/main/flores200/README.md#languages-in-flores-200) + * for the full list of languages and their corresponding codes. + * + * ```javascript + * const translator = await pipeline('translation', 'Xenova/nllb-200-distilled-600M'); + * const output = await translator('जीवन एक चॉकलेट बॉक्स की तरह है।', { + * src_lang: 'hin_Deva', // Hindi + * tgt_lang: 'fra_Latn', // French + * }); + * // [{ translation_text: 'La vie est comme une boîte à chocolat.' }] + * ``` + * + * **Example:** Multilingual translation w/ `Xenova/m2m100_418M`. + * + * See [here](https://huggingface.co/facebook/m2m100_418M#languages-covered) + * for the full list of languages and their corresponding codes. + * + * ```javascript + * const translator = await pipeline('translation', 'Xenova/m2m100_418M'); + * const output = await translator('生活就像一盒巧克力。', { + * src_lang: 'zh', // Chinese + * tgt_lang: 'en', // English + * }); + * // [{ translation_text: 'Life is like a box of chocolate.' }] + * ``` + * + * **Example:** Multilingual translation w/ `Xenova/mbart-large-50-many-to-many-mmt`. + * + * See [here](https://huggingface.co/facebook/mbart-large-50-many-to-many-mmt#languages-covered) + * for the full list of languages and their corresponding codes. + * + * ```javascript + * const translator = await pipeline('translation', 'Xenova/mbart-large-50-many-to-many-mmt'); + * const output = await translator('संयुक्त राष्ट्र के प्रमुख का कहना है कि सीरिया में कोई सैन्य समाधान नहीं है', { + * src_lang: 'hi_IN', // Hindi + * tgt_lang: 'fr_XX', // French + * }); + * // [{ translation_text: 'Le chef des Nations affirme qu 'il n 'y a military solution in Syria.' }] + * ``` + */ +class TranslationPipeline extends (/** @type {new (options: TextPipelineConstructorArgs) => TranslationPipelineType} */ (/** @type {any} */ (Text2TextGenerationPipeline))) { + /** @type {'translation_text'} */ + _key = 'translation_text'; + + /** + * Create a new TranslationPipeline. + * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } +} + +function isChat(x) { + return Array.isArray(x) && x.every(x => 'role' in x && 'content' in x); +} + +/** + * @typedef {import('./tokenizers.js').Message[]} Chat + * + * @typedef {Object} TextGenerationSingle + * @property {string|Chat} generated_text The generated text. + * @typedef {TextGenerationSingle[]} TextGenerationOutput + * + * @typedef {Object} TextGenerationSpecificParams Parameters specific to text-generation pipelines. + * @property {boolean} [add_special_tokens] Whether or not to add special tokens when tokenizing the sequences. + * @property {boolean} [return_full_text=true] If set to `false` only added text is returned, otherwise the full text is returned. + * @typedef {import('./generation/configuration_utils.js').GenerationConfig & TextGenerationSpecificParams} TextGenerationConfig + * + * @callback TextGenerationPipelineCallback Complete the prompt(s) given as inputs. + * @param {string|string[]|Chat|Chat[]} texts One or several prompts (or one list of prompts) to complete. + * @param {Partial} [options] Additional keyword arguments to pass along to the generate method of the model. + * @returns {Promise} An array or object containing the generated texts. + * + * @typedef {TextPipelineConstructorArgs & TextGenerationPipelineCallback & Disposable} TextGenerationPipelineType + */ + +/** + * Language generation pipeline using any `ModelWithLMHead` or `ModelForCausalLM`. + * This pipeline predicts the words that will follow a specified text prompt. + * NOTE: For the full list of generation parameters, see [`GenerationConfig`](./utils/generation#module_utils/generation.GenerationConfig). + * + * **Example:** Text generation with `Xenova/distilgpt2` (default settings). + * ```javascript + * const generator = await pipeline('text-generation', 'Xenova/distilgpt2'); + * const text = 'I enjoy walking with my cute dog,'; + * const output = await generator(text); + * // [{ generated_text: "I enjoy walking with my cute dog, and I love to play with the other dogs." }] + * ``` + * + * **Example:** Text generation with `Xenova/distilgpt2` (custom settings). + * ```javascript + * const generator = await pipeline('text-generation', 'Xenova/distilgpt2'); + * const text = 'Once upon a time, there was'; + * const output = await generator(text, { + * temperature: 2, + * max_new_tokens: 10, + * repetition_penalty: 1.5, + * no_repeat_ngram_size: 2, + * num_beams: 2, + * num_return_sequences: 2, + * }); + * // [{ + * // "generated_text": "Once upon a time, there was an abundance of information about the history and activities that" + * // }, { + * // "generated_text": "Once upon a time, there was an abundance of information about the most important and influential" + * // }] + * ``` + * + * **Example:** Run code generation with `Xenova/codegen-350M-mono`. + * ```javascript + * const generator = await pipeline('text-generation', 'Xenova/codegen-350M-mono'); + * const text = 'def fib(n):'; + * const output = await generator(text, { + * max_new_tokens: 44, + * }); + * // [{ + * // generated_text: 'def fib(n):\n' + + * // ' if n == 0:\n' + + * // ' return 0\n' + + * // ' elif n == 1:\n' + + * // ' return 1\n' + + * // ' else:\n' + + * // ' return fib(n-1) + fib(n-2)\n' + * // }] + * ``` + */ +class TextGenerationPipeline extends (/** @type {new (options: TextPipelineConstructorArgs) => TextGenerationPipelineType} */ (Pipeline)) { + + /** + * Create a new TextGenerationPipeline. + * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {TextGenerationPipelineCallback} */ + async _call(texts, generate_kwargs = {}) { + let isBatched = false; + let isChatInput = false; + + // Normalize inputs + /** @type {string[]} */ + let inputs; + if (typeof texts === 'string') { + inputs = texts = [texts]; + } else if (Array.isArray(texts) && texts.every(x => typeof x === 'string')) { + isBatched = true; + inputs = /** @type {string[]} */(texts); + } else { + if (isChat(texts)) { + texts = [/** @type {Chat} */(texts)]; + } else if (Array.isArray(texts) && texts.every(isChat)) { + isBatched = true; + } else { + throw new Error('Input must be a string, an array of strings, a Chat, or an array of Chats'); + } + isChatInput = true; + + // If the input is a chat, we need to apply the chat template + inputs = /** @type {string[]} */(/** @type {Chat[]} */ (texts).map( + x => this.tokenizer.apply_chat_template(x, { + tokenize: false, + add_generation_prompt: true, + }) + )); + } + + // By default, do not add special tokens + const add_special_tokens = generate_kwargs.add_special_tokens ?? false; + + // By default, return full text + const return_full_text = isChatInput + ? false + : generate_kwargs.return_full_text ?? true; + + this.tokenizer.padding_side = 'left'; + const text_inputs = this.tokenizer(inputs, { + add_special_tokens, + padding: true, + truncation: true, + }); + + const outputTokenIds = /** @type {Tensor} */(await this.model.generate({ + ...text_inputs, + ...generate_kwargs + })); + + const decoded = this.tokenizer.batch_decode(outputTokenIds, { + skip_special_tokens: true, + }); + + let promptLengths; + if (!return_full_text && text_inputs.input_ids.dims.at(-1) > 0) { + promptLengths = this.tokenizer.batch_decode(text_inputs.input_ids, { + skip_special_tokens: true, + }).map(x => x.length); + } + + /** @type {TextGenerationOutput[]} */ + const toReturn = Array.from({ length: texts.length }, _ => []); + for (let i = 0; i < decoded.length; ++i) { + const textIndex = Math.floor(i / outputTokenIds.dims[0] * texts.length); + + if (promptLengths) { + // Trim the decoded text to only include the generated part + decoded[i] = decoded[i].slice(promptLengths[textIndex]); + } + toReturn[textIndex].push({ + generated_text: isChatInput + ? [ + ...((/** @type {Chat[]} */(texts)[textIndex])), + { role: 'assistant', content: decoded[i] }, + ] + : decoded[i] + }); + } + return (!isBatched && toReturn.length === 1) ? toReturn[0] : toReturn; + } +} + +/** + * @typedef {Object} ZeroShotClassificationOutput + * @property {string} sequence The sequence for which this is the output. + * @property {string[]} labels The labels sorted by order of likelihood. + * @property {number[]} scores The probabilities for each of the labels. + * + * @typedef {Object} ZeroShotClassificationPipelineOptions Parameters specific to zero-shot classification pipelines. + * @property {string} [hypothesis_template="This example is {}."] The template used to turn each + * candidate label into an NLI-style hypothesis. The candidate label will replace the {} placeholder. + * @property {boolean} [multi_label=false] Whether or not multiple candidate labels can be true. + * If `false`, the scores are normalized such that the sum of the label likelihoods for each sequence + * is 1. If `true`, the labels are considered independent and probabilities are normalized for each + * candidate by doing a softmax of the entailment score vs. the contradiction score. + * + * @callback ZeroShotClassificationPipelineCallback Classify the sequence(s) given as inputs. + * @param {string|string[]} texts The sequence(s) to classify, will be truncated if the model input is too large. + * @param {string|string[]} candidate_labels The set of possible class labels to classify each sequence into. + * Can be a single label, a string of comma-separated labels, or a list of labels. + * @param {ZeroShotClassificationPipelineOptions} [options] The options to use for zero-shot classification. + * @returns {Promise} An array or object containing the predicted labels and scores. + * + * @typedef {TextPipelineConstructorArgs & ZeroShotClassificationPipelineCallback & Disposable} ZeroShotClassificationPipelineType + */ + +/** + * NLI-based zero-shot classification pipeline using a `ModelForSequenceClassification` + * trained on NLI (natural language inference) tasks. Equivalent of `text-classification` + * pipelines, but these models don't require a hardcoded number of potential classes, they + * can be chosen at runtime. It usually means it's slower but it is **much** more flexible. + * + * **Example:** Zero shot classification with `Xenova/mobilebert-uncased-mnli`. + * ```javascript + * const classifier = await pipeline('zero-shot-classification', 'Xenova/mobilebert-uncased-mnli'); + * const text = 'Last week I upgraded my iOS version and ever since then my phone has been overheating whenever I use your app.'; + * const labels = [ 'mobile', 'billing', 'website', 'account access' ]; + * const output = await classifier(text, labels); + * // { + * // sequence: 'Last week I upgraded my iOS version and ever since then my phone has been overheating whenever I use your app.', + * // labels: [ 'mobile', 'website', 'billing', 'account access' ], + * // scores: [ 0.5562091040482018, 0.1843621307860853, 0.13942646639336376, 0.12000229877234923 ] + * // } + * ``` + * + * **Example:** Zero shot classification with `Xenova/nli-deberta-v3-xsmall` (multi-label). + * ```javascript + * const classifier = await pipeline('zero-shot-classification', 'Xenova/nli-deberta-v3-xsmall'); + * const text = 'I have a problem with my iphone that needs to be resolved asap!'; + * const labels = [ 'urgent', 'not urgent', 'phone', 'tablet', 'computer' ]; + * const output = await classifier(text, labels, { multi_label: true }); + * // { + * // sequence: 'I have a problem with my iphone that needs to be resolved asap!', + * // labels: [ 'urgent', 'phone', 'computer', 'tablet', 'not urgent' ], + * // scores: [ 0.9958870956360275, 0.9923963400697035, 0.002333537946160235, 0.0015134138567598765, 0.0010699384208377163 ] + * // } + * ``` + */ +class ZeroShotClassificationPipeline extends (/** @type {new (options: TextPipelineConstructorArgs) => ZeroShotClassificationPipelineType} */ (Pipeline)) { + /** + * Create a new ZeroShotClassificationPipeline. + * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + + // Use model config to get label2id mapping + this.label2id = Object.fromEntries( + Object.entries((/** @type {any} */(this).model).config.label2id).map( + ([k, v]) => [k.toLowerCase(), v] + ) + ); + + this.entailment_id = this.label2id['entailment']; + if (this.entailment_id === undefined) { + console.warn("Could not find 'entailment' in label2id mapping. Using 2 as entailment_id."); + this.entailment_id = 2; + } + + this.contradiction_id = this.label2id['contradiction'] ?? this.label2id['not_entailment']; + if (this.contradiction_id === undefined) { + console.warn("Could not find 'contradiction' in label2id mapping. Using 0 as contradiction_id."); + this.contradiction_id = 0; + } + } + + /** @type {ZeroShotClassificationPipelineCallback} */ + async _call(texts, candidate_labels, { + hypothesis_template = "This example is {}.", + multi_label = false, + } = {}) { + + const isBatched = Array.isArray(texts); + if (!isBatched) { + texts = [/** @type {string} */ (texts)]; + } + if (!Array.isArray(candidate_labels)) { + candidate_labels = [candidate_labels]; + } + + // Insert labels into hypothesis template + const hypotheses = candidate_labels.map( + x => hypothesis_template.replace('{}', x) + ); + + // How to perform the softmax over the logits: + // - true: softmax over the entailment vs. contradiction dim for each label independently + // - false: softmax the "entailment" logits over all candidate labels + const softmaxEach = multi_label || candidate_labels.length === 1; + + /** @type {ZeroShotClassificationOutput[]} */ + const toReturn = []; + for (const premise of texts) { + const entails_logits = []; + + for (const hypothesis of hypotheses) { + const inputs = this.tokenizer(premise, { + text_pair: hypothesis, + padding: true, + truncation: true, + }) + const outputs = await this.model(inputs) + + if (softmaxEach) { + entails_logits.push([ + outputs.logits.data[this.contradiction_id], + outputs.logits.data[this.entailment_id] + ]) + } else { + entails_logits.push(outputs.logits.data[this.entailment_id]) + } + } + + /** @type {number[]} */ + const scores = softmaxEach + ? entails_logits.map(x => (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_6__.softmax)(x)[1]) + : (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_6__.softmax)(entails_logits); + + // Sort by scores (desc) and return scores with indices + const scores_sorted = scores + .map((x, i) => [x, i]) + .sort((a, b) => (b[0] - a[0])); + + toReturn.push({ + sequence: premise, + labels: scores_sorted.map(x => candidate_labels[x[1]]), + scores: scores_sorted.map(x => x[0]), + }); + } + return isBatched ? toReturn : toReturn[0]; + } +} + +/** + * @typedef {Object} FeatureExtractionPipelineOptions Parameters specific to feature extraction pipelines. + * @property {'none'|'mean'|'cls'} [pooling="none"] The pooling method to use. + * @property {boolean} [normalize=false] Whether or not to normalize the embeddings in the last dimension. + * @property {boolean} [quantize=false] Whether or not to quantize the embeddings. + * @property {'binary'|'ubinary'} [precision='binary'] The precision to use for quantization. + * + * @callback FeatureExtractionPipelineCallback Extract the features of the input(s). + * @param {string|string[]} texts One or several texts (or one list of texts) to get the features of. + * @param {FeatureExtractionPipelineOptions} [options] The options to use for feature extraction. + * @returns {Promise} The features computed by the model. + * + * @typedef {TextPipelineConstructorArgs & FeatureExtractionPipelineCallback & Disposable} FeatureExtractionPipelineType + */ + +/** + * Feature extraction pipeline using no model head. This pipeline extracts the hidden + * states from the base transformer, which can be used as features in downstream tasks. + * + * **Example:** Run feature extraction with `bert-base-uncased` (without pooling/normalization). + * ```javascript + * const extractor = await pipeline('feature-extraction', 'Xenova/bert-base-uncased', { revision: 'default' }); + * const output = await extractor('This is a simple test.'); + * // Tensor { + * // type: 'float32', + * // data: Float32Array [0.05939924716949463, 0.021655935794115067, ...], + * // dims: [1, 8, 768] + * // } + * ``` + * + * **Example:** Run feature extraction with `bert-base-uncased` (with pooling/normalization). + * ```javascript + * const extractor = await pipeline('feature-extraction', 'Xenova/bert-base-uncased', { revision: 'default' }); + * const output = await extractor('This is a simple test.', { pooling: 'mean', normalize: true }); + * // Tensor { + * // type: 'float32', + * // data: Float32Array [0.03373778983950615, -0.010106077417731285, ...], + * // dims: [1, 768] + * // } + * ``` + * + * **Example:** Calculating embeddings with `sentence-transformers` models. + * ```javascript + * const extractor = await pipeline('feature-extraction', 'Xenova/all-MiniLM-L6-v2'); + * const output = await extractor('This is a simple test.', { pooling: 'mean', normalize: true }); + * // Tensor { + * // type: 'float32', + * // data: Float32Array [0.09094982594251633, -0.014774246141314507, ...], + * // dims: [1, 384] + * // } + * ``` + * **Example:** Calculating binary embeddings with `sentence-transformers` models. + * ```javascript + * const extractor = await pipeline('feature-extraction', 'Xenova/all-MiniLM-L6-v2'); + * const output = await extractor('This is a simple test.', { pooling: 'mean', quantize: true, precision: 'binary' }); + * // Tensor { + * // type: 'int8', + * // data: Int8Array [49, 108, 24, ...], + * // dims: [1, 48] + * // } + * ``` + */ +class FeatureExtractionPipeline extends (/** @type {new (options: TextPipelineConstructorArgs) => FeatureExtractionPipelineType} */ (Pipeline)) { + /** + * Create a new FeatureExtractionPipeline. + * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {FeatureExtractionPipelineCallback} */ + async _call(texts, { + pooling = /** @type {'none'} */('none'), + normalize = false, + quantize = false, + precision = /** @type {'binary'} */('binary'), + } = {}) { + + // Run tokenization + const model_inputs = this.tokenizer(texts, { + padding: true, + truncation: true, + }); + + // Run model + const outputs = await this.model(model_inputs) + + // TODO: Provide warning to the user that they might be using model which was not exported + // specifically for feature extraction + // console.log(this.model.config) + // console.log(outputs) + + /** @type {Tensor} */ + let result = outputs.last_hidden_state ?? outputs.logits ?? outputs.token_embeddings; + if (pooling === 'none') { + // Skip pooling + } else if (pooling === 'mean') { + result = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.mean_pooling)(result, model_inputs.attention_mask); + } else if (pooling === 'cls') { + result = result.slice(null, 0); + } else { + throw Error(`Pooling method '${pooling}' not supported.`); + } + + if (normalize) { + result = result.normalize(2, -1); + } + + if (quantize) { + result = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.quantize_embeddings)(result, precision); + } + + return result; + } +} + + +/** + * @typedef {Object} ImageFeatureExtractionPipelineOptions Parameters specific to image feature extraction pipelines. + * @property {boolean} [pool=null] Whether or not to return the pooled output. If set to `false`, the model will return the raw hidden states. + * + * @callback ImageFeatureExtractionPipelineCallback Extract the features of the input(s). + * @param {ImagePipelineInputs} images One or several images (or one list of images) to get the features of. + * @param {ImageFeatureExtractionPipelineOptions} [options] The options to use for image feature extraction. + * @returns {Promise} The image features computed by the model. + * + * @typedef {ImagePipelineConstructorArgs & ImageFeatureExtractionPipelineCallback & Disposable} ImageFeatureExtractionPipelineType + */ + +/** + * Image feature extraction pipeline using no model head. This pipeline extracts the hidden + * states from the base transformer, which can be used as features in downstream tasks. + * + * **Example:** Perform image feature extraction with `Xenova/vit-base-patch16-224-in21k`. + * ```javascript + * const image_feature_extractor = await pipeline('image-feature-extraction', 'Xenova/vit-base-patch16-224-in21k'); + * const url = 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/cats.png'; + * const features = await image_feature_extractor(url); + * // Tensor { + * // dims: [ 1, 197, 768 ], + * // type: 'float32', + * // data: Float32Array(151296) [ ... ], + * // size: 151296 + * // } + * ``` + * + * **Example:** Compute image embeddings with `Xenova/clip-vit-base-patch32`. + * ```javascript + * const image_feature_extractor = await pipeline('image-feature-extraction', 'Xenova/clip-vit-base-patch32'); + * const url = 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/cats.png'; + * const features = await image_feature_extractor(url); + * // Tensor { + * // dims: [ 1, 512 ], + * // type: 'float32', + * // data: Float32Array(512) [ ... ], + * // size: 512 + * // } + * ``` + */ +class ImageFeatureExtractionPipeline extends (/** @type {new (options: ImagePipelineConstructorArgs) => ImageFeatureExtractionPipelineType} */ (Pipeline)) { + /** + * Create a new ImageFeatureExtractionPipeline. + * @param {ImagePipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {ImageFeatureExtractionPipelineCallback} */ + async _call(images, { + pool = null, + } = {}) { + + const preparedImages = await prepareImages(images); + const { pixel_values } = await this.processor(preparedImages); + const outputs = await this.model({ pixel_values }); + + /** @type {Tensor} */ + let result; + if (pool) { + if (!('pooler_output' in outputs)) { + throw Error(`No pooled output was returned. Make sure the model has a 'pooler' layer when using the 'pool' option.`); + } + result = outputs.pooler_output; + + } else { + result = outputs.last_hidden_state ?? outputs.logits ?? outputs.image_embeds; + } + return result; + } +} + +// TODO +// export class SentenceSimilarityPipeline extends Pipeline { +// } + +/** + * @typedef {Object} AudioClassificationSingle + * @property {string} label The label predicted. + * @property {number} score The corresponding probability. + * @typedef {AudioClassificationSingle[]} AudioClassificationOutput + * + * @typedef {Object} AudioClassificationPipelineOptions Parameters specific to audio classification pipelines. + * @property {number} [top_k=5] The number of top labels that will be returned by the pipeline. + * If the provided number is `null` or higher than the number of labels available in the model configuration, + * it will default to the number of labels. + * + * @callback AudioClassificationPipelineCallback Classify the sequence(s) given as inputs. + * @param {AudioPipelineInputs} audio The input audio file(s) to be classified. The input is either: + * - `string` or `URL` that is the filename/URL of the audio file, the file will be read at the processor's sampling rate + * to get the waveform using the [`AudioContext`](https://developer.mozilla.org/en-US/docs/Web/API/AudioContext) API. + * If `AudioContext` is not available, you should pass the raw waveform in as a Float32Array of shape `(n, )`. + * - `Float32Array` or `Float64Array` of shape `(n, )`, representing the raw audio at the correct sampling rate (no further check will be done). + * @param {AudioClassificationPipelineOptions} [options] The options to use for audio classification. + * @returns {Promise} An array or object containing the predicted labels and scores. + * + * @typedef {AudioPipelineConstructorArgs & AudioClassificationPipelineCallback & Disposable} AudioClassificationPipelineType + */ + +/** + * Audio classification pipeline using any `AutoModelForAudioClassification`. + * This pipeline predicts the class of a raw waveform or an audio file. + * + * **Example:** Perform audio classification with `Xenova/wav2vec2-large-xlsr-53-gender-recognition-librispeech`. + * ```javascript + * const classifier = await pipeline('audio-classification', 'Xenova/wav2vec2-large-xlsr-53-gender-recognition-librispeech'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav'; + * const output = await classifier(url); + * // [ + * // { label: 'male', score: 0.9981542229652405 }, + * // { label: 'female', score: 0.001845747814513743 } + * // ] + * ``` + * + * **Example:** Perform audio classification with `Xenova/ast-finetuned-audioset-10-10-0.4593` and return top 4 results. + * ```javascript + * const classifier = await pipeline('audio-classification', 'Xenova/ast-finetuned-audioset-10-10-0.4593'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/cat_meow.wav'; + * const output = await classifier(url, { top_k: 4 }); + * // [ + * // { label: 'Meow', score: 0.5617874264717102 }, + * // { label: 'Cat', score: 0.22365376353263855 }, + * // { label: 'Domestic animals, pets', score: 0.1141069084405899 }, + * // { label: 'Animal', score: 0.08985692262649536 }, + * // ] + * ``` + */ +class AudioClassificationPipeline extends (/** @type {new (options: AudioPipelineConstructorArgs) => AudioClassificationPipelineType} */ (Pipeline)) { + + /** + * Create a new AudioClassificationPipeline. + * @param {AudioPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {AudioClassificationPipelineCallback} */ + async _call(audio, { + top_k = 5 + } = {}) { + + const sampling_rate = this.processor.feature_extractor.config.sampling_rate; + const preparedAudios = await prepareAudios(audio, sampling_rate); + + // @ts-expect-error TS2339 + const id2label = this.model.config.id2label; + + const toReturn = []; + for (const aud of preparedAudios) { + const inputs = await this.processor(aud); + const output = await this.model(inputs); + const logits = output.logits[0]; + + const scores = await (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.topk)(new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.Tensor( + 'float32', + (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_6__.softmax)(logits.data), + logits.dims, + ), top_k); + + const values = scores[0].tolist(); + const indices = scores[1].tolist(); + + const vals = indices.map((x, i) => ({ + label: /** @type {string} */ (id2label ? id2label[x] : `LABEL_${x}`), + score: /** @type {number} */ (values[i]), + })); + + toReturn.push(vals); + }; + return Array.isArray(audio) ? toReturn : toReturn[0]; + } +} + +/** + * @typedef {Object} ZeroShotAudioClassificationOutput + * @property {string} label The label identified by the model. It is one of the suggested `candidate_label`. + * @property {number} score The score attributed by the model for that label (between 0 and 1). + * + * @typedef {Object} ZeroShotAudioClassificationPipelineOptions Parameters specific to zero-shot audio classification pipelines. + * @property {string} [hypothesis_template="This is a sound of {}."] The sentence used in conjunction with `candidate_labels` + * to attempt the audio classification by replacing the placeholder with the candidate_labels. + * Then likelihood is estimated by using `logits_per_audio`. + * + * @callback ZeroShotAudioClassificationPipelineCallback Classify the sequence(s) given as inputs. + * @param {AudioPipelineInputs} audio The input audio file(s) to be classified. The input is either: + * - `string` or `URL` that is the filename/URL of the audio file, the file will be read at the processor's sampling rate + * to get the waveform using the [`AudioContext`](https://developer.mozilla.org/en-US/docs/Web/API/AudioContext) API. + * If `AudioContext` is not available, you should pass the raw waveform in as a Float32Array of shape `(n, )`. + * - `Float32Array` or `Float64Array` of shape `(n, )`, representing the raw audio at the correct sampling rate (no further check will be done). + * @param {string[]} candidate_labels The candidate labels for this audio. + * @param {ZeroShotAudioClassificationPipelineOptions} [options] The options to use for zero-shot audio classification. + * @returns {Promise} An array of objects containing the predicted labels and scores. + * + * @typedef {TextAudioPipelineConstructorArgs & ZeroShotAudioClassificationPipelineCallback & Disposable} ZeroShotAudioClassificationPipelineType + */ + +/** + * Zero shot audio classification pipeline using `ClapModel`. This pipeline predicts the class of an audio when you + * provide an audio and a set of `candidate_labels`. + * + * **Example**: Perform zero-shot audio classification with `Xenova/clap-htsat-unfused`. + * ```javascript + * const classifier = await pipeline('zero-shot-audio-classification', 'Xenova/clap-htsat-unfused'); + * const audio = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/dog_barking.wav'; + * const candidate_labels = ['dog', 'vaccum cleaner']; + * const scores = await classifier(audio, candidate_labels); + * // [ + * // { score: 0.9993992447853088, label: 'dog' }, + * // { score: 0.0006007603369653225, label: 'vaccum cleaner' } + * // ] + * ``` + */ +class ZeroShotAudioClassificationPipeline extends (/** @type {new (options: TextAudioPipelineConstructorArgs) => ZeroShotAudioClassificationPipelineType} */ (Pipeline)) { + + /** + * Create a new ZeroShotAudioClassificationPipeline. + * @param {TextAudioPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {ZeroShotAudioClassificationPipelineCallback} */ + async _call(audio, candidate_labels, { + hypothesis_template = "This is a sound of {}." + } = {}) { + + const single = !Array.isArray(audio); + if (single) { + audio = [/** @type {AudioInput} */ (audio)]; + } + + // Insert label into hypothesis template + const texts = candidate_labels.map( + x => hypothesis_template.replace('{}', x) + ); + + // Run tokenization + const text_inputs = this.tokenizer(texts, { + padding: true, + truncation: true, + }); + + const sampling_rate = this.processor.feature_extractor.config.sampling_rate; + const preparedAudios = await prepareAudios(audio, sampling_rate); + + const toReturn = []; + for (const aud of preparedAudios) { + const audio_inputs = await this.processor(aud); + + // Run model with both text and audio inputs + const output = await this.model({ ...text_inputs, ...audio_inputs }); + + // Compute softmax per audio + const probs = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_6__.softmax)(output.logits_per_audio.data); + + toReturn.push([...probs].map((x, i) => ({ + score: x, + label: candidate_labels[i] + }))); + } + return single ? toReturn[0] : toReturn; + } +} + +/** + * @typedef {Object} Chunk + * @property {[number, number]} timestamp The start and end timestamp of the chunk in seconds. + * @property {string} text The recognized text. + */ + +/** + * @typedef {Object} AutomaticSpeechRecognitionOutput + * @property {string} text The recognized text. + * @property {Chunk[]} [chunks] When using `return_timestamps`, the `chunks` will become a list + * containing all the various text chunks identified by the model. + * + * @typedef {Object} AutomaticSpeechRecognitionSpecificParams Parameters specific to automatic-speech-recognition pipelines. + * @property {boolean|'word'} [return_timestamps] Whether to return timestamps or not. Default is `false`. + * @property {number} [chunk_length_s] The length of audio chunks to process in seconds. Default is 0 (no chunking). + * @property {number} [stride_length_s] The length of overlap between consecutive audio chunks in seconds. If not provided, defaults to `chunk_length_s / 6`. + * @property {boolean} [force_full_sequences] Whether to force outputting full sequences or not. Default is `false`. + * @property {string} [language] The source language. Default is `null`, meaning it should be auto-detected. Use this to potentially improve performance if the source language is known. + * @property {string} [task] The task to perform. Default is `null`, meaning it should be auto-detected. + * @property {number} [num_frames] The number of frames in the input audio. + * @typedef {import('./generation/configuration_utils.js').GenerationConfig & AutomaticSpeechRecognitionSpecificParams} AutomaticSpeechRecognitionConfig + * + * @callback AutomaticSpeechRecognitionPipelineCallback Transcribe the audio sequence(s) given as inputs to text. + * @param {AudioPipelineInputs} audio The input audio file(s) to be transcribed. The input is either: + * - `string` or `URL` that is the filename/URL of the audio file, the file will be read at the processor's sampling rate + * to get the waveform using the [`AudioContext`](https://developer.mozilla.org/en-US/docs/Web/API/AudioContext) API. + * If `AudioContext` is not available, you should pass the raw waveform in as a Float32Array of shape `(n, )`. + * - `Float32Array` or `Float64Array` of shape `(n, )`, representing the raw audio at the correct sampling rate (no further check will be done). + * @param {Partial} [options] Additional keyword arguments to pass along to the generate method of the model. + * @returns {Promise} An object containing the transcription text and optionally timestamps if `return_timestamps` is `true`. + * + * @typedef {TextAudioPipelineConstructorArgs & AutomaticSpeechRecognitionPipelineCallback & Disposable} AutomaticSpeechRecognitionPipelineType + */ + +/** + * Pipeline that aims at extracting spoken text contained within some audio. + * + * **Example:** Transcribe English. + * ```javascript + * const transcriber = await pipeline('automatic-speech-recognition', 'Xenova/whisper-tiny.en'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav'; + * const output = await transcriber(url); + * // { text: " And so my fellow Americans ask not what your country can do for you, ask what you can do for your country." } + * ``` + * + * **Example:** Transcribe English w/ timestamps. + * ```javascript + * const transcriber = await pipeline('automatic-speech-recognition', 'Xenova/whisper-tiny.en'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav'; + * const output = await transcriber(url, { return_timestamps: true }); + * // { + * // text: " And so my fellow Americans ask not what your country can do for you, ask what you can do for your country." + * // chunks: [ + * // { timestamp: [0, 8], text: " And so my fellow Americans ask not what your country can do for you" } + * // { timestamp: [8, 11], text: " ask what you can do for your country." } + * // ] + * // } + * ``` + * + * **Example:** Transcribe English w/ word-level timestamps. + * ```javascript + * const transcriber = await pipeline('automatic-speech-recognition', 'Xenova/whisper-tiny.en'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav'; + * const output = await transcriber(url, { return_timestamps: 'word' }); + * // { + * // "text": " And so my fellow Americans ask not what your country can do for you ask what you can do for your country.", + * // "chunks": [ + * // { "text": " And", "timestamp": [0, 0.78] }, + * // { "text": " so", "timestamp": [0.78, 1.06] }, + * // { "text": " my", "timestamp": [1.06, 1.46] }, + * // ... + * // { "text": " for", "timestamp": [9.72, 9.92] }, + * // { "text": " your", "timestamp": [9.92, 10.22] }, + * // { "text": " country.", "timestamp": [10.22, 13.5] } + * // ] + * // } + * ``` + * + * **Example:** Transcribe French. + * ```javascript + * const transcriber = await pipeline('automatic-speech-recognition', 'Xenova/whisper-small'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/french-audio.mp3'; + * const output = await transcriber(url, { language: 'french', task: 'transcribe' }); + * // { text: " J'adore, j'aime, je n'aime pas, je déteste." } + * ``` + * + * **Example:** Translate French to English. + * ```javascript + * const transcriber = await pipeline('automatic-speech-recognition', 'Xenova/whisper-small'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/french-audio.mp3'; + * const output = await transcriber(url, { language: 'french', task: 'translate' }); + * // { text: " I love, I like, I don't like, I hate." } + * ``` + * + * **Example:** Transcribe/translate audio longer than 30 seconds. + * ```javascript + * const transcriber = await pipeline('automatic-speech-recognition', 'Xenova/whisper-tiny.en'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/ted_60.wav'; + * const output = await transcriber(url, { chunk_length_s: 30, stride_length_s: 5 }); + * // { text: " So in college, I was a government major, which means [...] So I'd start off light and I'd bump it up" } + * ``` + */ +class AutomaticSpeechRecognitionPipeline extends (/** @type {new (options: TextAudioPipelineConstructorArgs) => AutomaticSpeechRecognitionPipelineType} */ (Pipeline)) { + + /** + * Create a new AutomaticSpeechRecognitionPipeline. + * @param {TextAudioPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {AutomaticSpeechRecognitionPipelineCallback} */ + async _call(audio, kwargs = {}) { + switch (this.model.config.model_type) { + case 'whisper': + case 'lite-whisper': + return this._call_whisper(audio, kwargs) + case 'wav2vec2': + case 'wav2vec2-bert': + case 'unispeech': + case 'unispeech-sat': + case 'hubert': + return this._call_wav2vec2(audio, kwargs) + case 'moonshine': + return this._call_moonshine(audio, kwargs) + default: + throw new Error(`AutomaticSpeechRecognitionPipeline does not support model type '${this.model.config.model_type}'.`) + } + } + + /** + * @type {AutomaticSpeechRecognitionPipelineCallback} + * @private + */ + async _call_wav2vec2(audio, kwargs) { + // TODO use kwargs + + if (kwargs.language) { + console.warn('`language` parameter is not yet supported for `wav2vec2` models, defaulting to "English".'); + } + if (kwargs.task) { + console.warn('`task` parameter is not yet supported for `wav2vec2` models, defaulting to "transcribe".'); + } + + const single = !Array.isArray(audio); + if (single) { + audio = [/** @type {AudioInput} */ (audio)]; + } + + const sampling_rate = this.processor.feature_extractor.config.sampling_rate; + const preparedAudios = await prepareAudios(audio, sampling_rate); + + const toReturn = []; + for (const aud of preparedAudios) { + const inputs = await this.processor(aud); + const output = await this.model(inputs); + const logits = output.logits[0]; + + const predicted_ids = []; + for (const item of logits) { + predicted_ids.push((0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_6__.max)(item.data)[1]) + } + const predicted_sentences = this.tokenizer.decode(predicted_ids) + toReturn.push({ text: predicted_sentences }) + } + return single ? toReturn[0] : toReturn; + } + + /** + * @type {AutomaticSpeechRecognitionPipelineCallback} + * @private + */ + async _call_whisper(audio, kwargs) { + const return_timestamps = kwargs.return_timestamps ?? false; + const chunk_length_s = kwargs.chunk_length_s ?? 0; + const force_full_sequences = kwargs.force_full_sequences ?? false; + let stride_length_s = kwargs.stride_length_s ?? null; + + const generation_config = { ...kwargs } + + if (return_timestamps === 'word') { + generation_config['return_token_timestamps'] = true; + generation_config['return_timestamps'] = false; // Do not predict timestamp tokens + } + + const single = !Array.isArray(audio); + if (single) { + audio = [/** @type {AudioInput} */ (audio)]; + } + + // @ts-expect-error TS2339 + const time_precision = this.processor.feature_extractor.config.chunk_length / this.model.config.max_source_positions; + const hop_length = this.processor.feature_extractor.config.hop_length; + + const sampling_rate = this.processor.feature_extractor.config.sampling_rate; + const preparedAudios = await prepareAudios(audio, sampling_rate); + + const toReturn = []; + for (const aud of preparedAudios) { + /** @type {{stride: number[], input_features: Tensor, is_last: boolean, tokens?: bigint[], token_timestamps?: number[]}[]} */ + let chunks = []; + if (chunk_length_s > 0) { + if (stride_length_s === null) { + stride_length_s = chunk_length_s / 6; + } else if (chunk_length_s <= stride_length_s) { + throw Error("`chunk_length_s` must be larger than `stride_length_s`.") + } + + // TODO support different stride_length_s (for left and right) + + const window = sampling_rate * chunk_length_s; + const stride = sampling_rate * stride_length_s; + const jump = window - 2 * stride; + let offset = 0; + + // Create subarrays of audio with overlaps + while (true) { + const offset_end = offset + window; + const subarr = aud.subarray(offset, offset_end); + const feature = await this.processor(subarr); + + const is_first = offset === 0; + const is_last = offset_end >= aud.length; + chunks.push({ + stride: [ + subarr.length, + is_first ? 0 : stride, + is_last ? 0 : stride + ], + input_features: feature.input_features, + is_last, + }) + if (is_last) break; + offset += jump; + } + + } else { + chunks = [{ + stride: [aud.length, 0, 0], + input_features: (await this.processor(aud)).input_features, + is_last: true + }] + } + + // Generate for each set of input features + for (const chunk of chunks) { + generation_config.num_frames = Math.floor(chunk.stride[0] / hop_length); + + // NOTE: doing sequentially for now + const data = await this.model.generate({ + inputs: chunk.input_features, + ...generation_config + }); + + // TODO: Right now we only get top beam + if (return_timestamps === 'word') { + // @ts-expect-error TS2339 + chunk.tokens = data.sequences.tolist()[0]; + // @ts-expect-error TS2339 + chunk.token_timestamps = data.token_timestamps.tolist()[0].map( + (/** @type {number} */ x) => (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_6__.round)(x, 2) + ); + + } else { + chunk.tokens = (/** @type {Tensor} */(data))[0].tolist(); + } + + // convert stride to seconds + chunk.stride = chunk.stride.map(x => x / sampling_rate); + } + + // Merge text chunks + // @ts-ignore + const [full_text, optional] = this.tokenizer._decode_asr(chunks, { + time_precision, return_timestamps, force_full_sequences + }); + + toReturn.push({ text: full_text, ...optional }) + } + return single ? toReturn[0] : toReturn; + } + + /** + * @type {AutomaticSpeechRecognitionPipelineCallback} + * @private + */ + async _call_moonshine(audio, kwargs) { + const single = !Array.isArray(audio); + if (single) { + audio = [/** @type {AudioInput} */ (audio)]; + } + const sampling_rate = this.processor.feature_extractor.config.sampling_rate; + const preparedAudios = await prepareAudios(audio, sampling_rate); + const toReturn = []; + for (const aud of preparedAudios) { + const inputs = await this.processor(aud); + + // According to the [paper](https://arxiv.org/pdf/2410.15608): + // "We use greedy decoding, with a heuristic limit of 6 output tokens + // per second of audio to avoid repeated output sequences." + const max_new_tokens = Math.floor(aud.length / sampling_rate) * 6; + const outputs = await this.model.generate({ max_new_tokens, ...kwargs, ...inputs }); + + const text = this.processor.batch_decode(/** @type {Tensor} */(outputs), { skip_special_tokens: true })[0]; + toReturn.push({ text }); + } + return single ? toReturn[0] : toReturn; + } + +} + +/** + * @typedef {Object} ImageToTextSingle + * @property {string} generated_text The generated text. + * @typedef {ImageToTextSingle[]} ImageToTextOutput + * + * @callback ImageToTextPipelineCallback Assign labels to the image(s) passed as inputs. + * @param {ImagePipelineInputs} texts The images to be captioned. + * @param {Partial} [options] Additional keyword arguments to pass along to the generate method of the model. + * @returns {Promise} An object (or array of objects) containing the generated text(s). + * + * @typedef {TextImagePipelineConstructorArgs & ImageToTextPipelineCallback & Disposable} ImageToTextPipelineType + */ + +/** + * Image To Text pipeline using a `AutoModelForVision2Seq`. This pipeline predicts a caption for a given image. + * + * **Example:** Generate a caption for an image w/ `Xenova/vit-gpt2-image-captioning`. + * ```javascript + * const captioner = await pipeline('image-to-text', 'Xenova/vit-gpt2-image-captioning'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/cats.jpg'; + * const output = await captioner(url); + * // [{ generated_text: 'a cat laying on a couch with another cat' }] + * ``` + * + * **Example:** Optical Character Recognition (OCR) w/ `Xenova/trocr-small-handwritten`. + * ```javascript + * const captioner = await pipeline('image-to-text', 'Xenova/trocr-small-handwritten'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/handwriting.jpg'; + * const output = await captioner(url); + * // [{ generated_text: 'Mr. Brown commented icily.' }] + * ``` + */ +class ImageToTextPipeline extends (/** @type {new (options: TextImagePipelineConstructorArgs) => ImageToTextPipelineType} */ (Pipeline)) { + + /** + * Create a new ImageToTextPipeline. + * @param {TextImagePipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {ImageToTextPipelineCallback} */ + async _call(images, generate_kwargs = {}) { + + const isBatched = Array.isArray(images); + const preparedImages = await prepareImages(images); + + const { pixel_values } = await this.processor(preparedImages); + + const toReturn = []; + for (const batch of pixel_values) { + batch.dims = [1, ...batch.dims] + const output = await this.model.generate({ inputs: batch, ...generate_kwargs }); + const decoded = this.tokenizer.batch_decode(/** @type {Tensor} */(output), { + skip_special_tokens: true, + }).map(x => ({ generated_text: x.trim() })) + toReturn.push(decoded); + } + + return isBatched ? toReturn : toReturn[0]; + } +} + +/** + * @typedef {Object} ImageClassificationSingle + * @property {string} label The label identified by the model. + * @property {number} score The score attributed by the model for that label. + * @typedef {ImageClassificationSingle[]} ImageClassificationOutput + * + * @typedef {Object} ImageClassificationPipelineOptions Parameters specific to image classification pipelines. + * @property {number} [top_k=1] The number of top labels that will be returned by the pipeline. + * + * @callback ImageClassificationPipelineCallback Assign labels to the image(s) passed as inputs. + * @param {ImagePipelineInputs} images The input images(s) to be classified. + * @param {ImageClassificationPipelineOptions} [options] The options to use for image classification. + * @returns {Promise} An array or object containing the predicted labels and scores. + * + * @typedef {ImagePipelineConstructorArgs & ImageClassificationPipelineCallback & Disposable} ImageClassificationPipelineType + */ + +/** + * Image classification pipeline using any `AutoModelForImageClassification`. + * This pipeline predicts the class of an image. + * + * **Example:** Classify an image. + * ```javascript + * const classifier = await pipeline('image-classification', 'Xenova/vit-base-patch16-224'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/tiger.jpg'; + * const output = await classifier(url); + * // [ + * // { label: 'tiger, Panthera tigris', score: 0.632695734500885 }, + * // ] + * ``` + * + * **Example:** Classify an image and return top `n` classes. + * ```javascript + * const classifier = await pipeline('image-classification', 'Xenova/vit-base-patch16-224'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/tiger.jpg'; + * const output = await classifier(url, { top_k: 3 }); + * // [ + * // { label: 'tiger, Panthera tigris', score: 0.632695734500885 }, + * // { label: 'tiger cat', score: 0.3634825646877289 }, + * // { label: 'lion, king of beasts, Panthera leo', score: 0.00045060308184474707 }, + * // ] + * ``` + * + * **Example:** Classify an image and return all classes. + * ```javascript + * const classifier = await pipeline('image-classification', 'Xenova/vit-base-patch16-224'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/tiger.jpg'; + * const output = await classifier(url, { top_k: 0 }); + * // [ + * // { label: 'tiger, Panthera tigris', score: 0.632695734500885 }, + * // { label: 'tiger cat', score: 0.3634825646877289 }, + * // { label: 'lion, king of beasts, Panthera leo', score: 0.00045060308184474707 }, + * // { label: 'jaguar, panther, Panthera onca, Felis onca', score: 0.00035465499968267977 }, + * // ... + * // ] + * ``` + */ +class ImageClassificationPipeline extends (/** @type {new (options: ImagePipelineConstructorArgs) => ImageClassificationPipelineType} */ (Pipeline)) { + + /** + * Create a new ImageClassificationPipeline. + * @param {ImagePipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {ImageClassificationPipelineCallback} */ + async _call(images, { + top_k = 5 + } = {}) { + + const preparedImages = await prepareImages(images); + + const { pixel_values } = await this.processor(preparedImages); + const output = await this.model({ pixel_values }); + + // @ts-expect-error TS2339 + const id2label = this.model.config.id2label; + + /** @type {ImageClassificationOutput[]} */ + const toReturn = []; + for (const batch of output.logits) { + const scores = await (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.topk)(new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.Tensor( + 'float32', + (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_6__.softmax)(batch.data), + batch.dims, + ), top_k); + + const values = scores[0].tolist(); + const indices = scores[1].tolist(); + + const vals = indices.map((x, i) => ({ + label: /** @type {string} */ (id2label ? id2label[x] : `LABEL_${x}`), + score: /** @type {number} */ (values[i]), + })); + toReturn.push(vals); + } + + return Array.isArray(images) ? toReturn : toReturn[0]; + } + +} + +/** + * @typedef {Object} ImageSegmentationPipelineOutput + * @property {string|null} label The label of the segment. + * @property {number|null} score The score of the segment. + * @property {RawImage} mask The mask of the segment. + * + * @typedef {Object} ImageSegmentationPipelineOptions Parameters specific to image segmentation pipelines. + * @property {number} [threshold=0.5] Probability threshold to filter out predicted masks. + * @property {number} [mask_threshold=0.5] Threshold to use when turning the predicted masks into binary values. + * @property {number} [overlap_mask_area_threshold=0.8] Mask overlap threshold to eliminate small, disconnected segments. + * @property {null|string} [subtask=null] Segmentation task to be performed. One of [`panoptic`, `instance`, and `semantic`], + * depending on model capabilities. If not set, the pipeline will attempt to resolve (in that order). + * @property {number[]} [label_ids_to_fuse=null] List of label ids to fuse. If not set, do not fuse any labels. + * @property {number[][]} [target_sizes=null] List of target sizes for the input images. If not set, use the original image sizes. + * + * @callback ImageSegmentationPipelineCallback Segment the input images. + * @param {ImagePipelineInputs} images The input images. + * @param {ImageSegmentationPipelineOptions} [options] The options to use for image segmentation. + * @returns {Promise} The annotated segments. + * + * @typedef {ImagePipelineConstructorArgs & ImageSegmentationPipelineCallback & Disposable} ImageSegmentationPipelineType + */ + +/** + * Image segmentation pipeline using any `AutoModelForXXXSegmentation`. + * This pipeline predicts masks of objects and their classes. + * + * **Example:** Perform image segmentation with `Xenova/detr-resnet-50-panoptic`. + * ```javascript + * const segmenter = await pipeline('image-segmentation', 'Xenova/detr-resnet-50-panoptic'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/cats.jpg'; + * const output = await segmenter(url); + * // [ + * // { label: 'remote', score: 0.9984649419784546, mask: RawImage { ... } }, + * // { label: 'cat', score: 0.9994316101074219, mask: RawImage { ... } } + * // ] + * ``` + */ +class ImageSegmentationPipeline extends (/** @type {new (options: ImagePipelineConstructorArgs) => ImageSegmentationPipelineType} */ (Pipeline)) { + /** + * Create a new ImageSegmentationPipeline. + * @param {ImagePipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + + this.subtasks_mapping = { + // Mapping of subtasks to their corresponding post-processing function names. + panoptic: 'post_process_panoptic_segmentation', + instance: 'post_process_instance_segmentation', + semantic: 'post_process_semantic_segmentation' + } + } + + /** @type {ImageSegmentationPipelineCallback} */ + async _call(images, { + threshold = 0.5, + mask_threshold = 0.5, + overlap_mask_area_threshold = 0.8, + label_ids_to_fuse = null, + target_sizes = null, + subtask = null, + } = {}) { + const isBatched = Array.isArray(images); + + if (isBatched && images.length !== 1) { + throw Error("Image segmentation pipeline currently only supports a batch size of 1."); + } + + const preparedImages = await prepareImages(images); + const imageSizes = preparedImages.map(x => [x.height, x.width]); + + const inputs = await this.processor(preparedImages); + + const { inputNames, outputNames } = this.model.sessions['model']; + if (!inputNames.includes('pixel_values')) { + if (inputNames.length !== 1) { + throw Error(`Expected a single input name, but got ${inputNames.length} inputs: ${inputNames}.`); + } + + const newName = inputNames[0]; + if (newName in inputs) { + throw Error(`Input name ${newName} already exists in the inputs.`); + } + // To ensure compatibility with certain background-removal models, + // we may need to perform a mapping of input to output names + inputs[newName] = inputs.pixel_values; + } + + const output = await this.model(inputs); + + let fn = null; + if (subtask !== null) { + fn = this.subtasks_mapping[subtask]; + } else if (this.processor.image_processor) { + for (const [task, func] of Object.entries(this.subtasks_mapping)) { + if (func in this.processor.image_processor) { + fn = this.processor.image_processor[func].bind(this.processor.image_processor); + subtask = task; + break; + } + } + } + + // @ts-expect-error TS2339 + const id2label = this.model.config.id2label; + + /** @type {ImageSegmentationPipelineOutput[]} */ + const annotation = []; + if (!subtask) { + // We define an epsilon to safeguard against numerical/precision issues when detecting + // the normalization mode of the output (i.e., sigmoid already applied, or not). + // See https://github.com/microsoft/onnxruntime/issues/23943 for more information. + const epsilon = 1e-5; + + // Perform standard image segmentation + const result = output[outputNames[0]]; + for (let i = 0; i < imageSizes.length; ++i) { + const size = imageSizes[i]; + const item = result[i]; + if (item.data.some(x => x < -epsilon || x > 1 + epsilon)) { + item.sigmoid_(); + } + const mask = await _utils_image_js__WEBPACK_IMPORTED_MODULE_9__.RawImage.fromTensor(item.mul_(255).to('uint8')).resize(size[1], size[0]); + annotation.push({ + label: null, + score: null, + mask + }); + } + } else if (subtask === 'panoptic' || subtask === 'instance') { + const processed = fn( + output, + threshold, + mask_threshold, + overlap_mask_area_threshold, + label_ids_to_fuse, + target_sizes ?? imageSizes, // TODO FIX? + )[0]; + + const segmentation = processed.segmentation; + + for (const segment of processed.segments_info) { + const maskData = new Uint8ClampedArray(segmentation.data.length); + for (let i = 0; i < segmentation.data.length; ++i) { + if (segmentation.data[i] === segment.id) { + maskData[i] = 255; + } + } + + const mask = new _utils_image_js__WEBPACK_IMPORTED_MODULE_9__.RawImage(maskData, segmentation.dims[1], segmentation.dims[0], 1) + + annotation.push({ + score: segment.score, + label: id2label[segment.label_id], + mask: mask + }) + } + + } else if (subtask === 'semantic') { + const { segmentation, labels } = fn(output, target_sizes ?? imageSizes)[0]; + + for (const label of labels) { + const maskData = new Uint8ClampedArray(segmentation.data.length); + for (let i = 0; i < segmentation.data.length; ++i) { + if (segmentation.data[i] === label) { + maskData[i] = 255; + } + } + + const mask = new _utils_image_js__WEBPACK_IMPORTED_MODULE_9__.RawImage(maskData, segmentation.dims[1], segmentation.dims[0], 1); + + annotation.push({ + score: null, + label: id2label[label], + mask: mask + }); + } + } else { + throw Error(`Subtask ${subtask} not supported.`); + } + + return annotation; + } +} + + +/** + * @typedef {Object} BackgroundRemovalPipelineOptions Parameters specific to image segmentation pipelines. + * + * @callback BackgroundRemovalPipelineCallback Segment the input images. + * @param {ImagePipelineInputs} images The input images. + * @param {BackgroundRemovalPipelineOptions} [options] The options to use for image segmentation. + * @returns {Promise} The images with the background removed. + * + * @typedef {ImagePipelineConstructorArgs & BackgroundRemovalPipelineCallback & Disposable} BackgroundRemovalPipelineType + */ + +/** + * Background removal pipeline using certain `AutoModelForXXXSegmentation`. + * This pipeline removes the backgrounds of images. + * + * **Example:** Perform background removal with `Xenova/modnet`. + * ```javascript + * const segmenter = await pipeline('background-removal', 'Xenova/modnet'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/portrait-of-woman_small.jpg'; + * const output = await segmenter(url); + * // [ + * // RawImage { data: Uint8ClampedArray(648000) [ ... ], width: 360, height: 450, channels: 4 } + * // ] + * ``` + */ +class BackgroundRemovalPipeline extends (/** @type {new (options: ImagePipelineConstructorArgs) => BackgroundRemovalPipelineType} */ (/** @type {any} */(ImageSegmentationPipeline))) { + /** + * Create a new BackgroundRemovalPipeline. + * @param {ImagePipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {BackgroundRemovalPipelineCallback} */ + async _call(images, options = {}) { + const isBatched = Array.isArray(images); + + if (isBatched && images.length !== 1) { + throw Error("Background removal pipeline currently only supports a batch size of 1."); + } + + const preparedImages = await prepareImages(images); + + // @ts-expect-error TS2339 + const masks = await super._call(images, options); + const result = preparedImages.map((img, i) => { + const cloned = img.clone(); + cloned.putAlpha(masks[i].mask); + return cloned; + }); + + return result; + } +} + +/** + * @typedef {Object} ZeroShotImageClassificationOutput + * @property {string} label The label identified by the model. It is one of the suggested `candidate_label`. + * @property {number} score The score attributed by the model for that label (between 0 and 1). + * + * @typedef {Object} ZeroShotImageClassificationPipelineOptions Parameters specific to zero-shot image classification pipelines. + * @property {string} [hypothesis_template="This is a photo of {}"] The sentence used in conjunction with `candidate_labels` + * to attempt the image classification by replacing the placeholder with the candidate_labels. + * Then likelihood is estimated by using `logits_per_image`. + * + * @callback ZeroShotImageClassificationPipelineCallback Assign labels to the image(s) passed as inputs. + * @param {ImagePipelineInputs} images The input images. + * @param {string[]} candidate_labels The candidate labels for this image. + * @param {ZeroShotImageClassificationPipelineOptions} [options] The options to use for zero-shot image classification. + * @returns {Promise} An array of objects containing the predicted labels and scores. + * + * @typedef {TextImagePipelineConstructorArgs & ZeroShotImageClassificationPipelineCallback & Disposable} ZeroShotImageClassificationPipelineType + */ + +/** + * Zero shot image classification pipeline. This pipeline predicts the class of + * an image when you provide an image and a set of `candidate_labels`. + * + * **Example:** Zero shot image classification w/ `Xenova/clip-vit-base-patch32`. + * ```javascript + * const classifier = await pipeline('zero-shot-image-classification', 'Xenova/clip-vit-base-patch32'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/tiger.jpg'; + * const output = await classifier(url, ['tiger', 'horse', 'dog']); + * // [ + * // { score: 0.9993917942047119, label: 'tiger' }, + * // { score: 0.0003519294841680676, label: 'horse' }, + * // { score: 0.0002562698791734874, label: 'dog' } + * // ] + * ``` + */ +class ZeroShotImageClassificationPipeline extends (/** @type {new (options: TextImagePipelineConstructorArgs) => ZeroShotImageClassificationPipelineType} */ (Pipeline)) { + /** + * Create a new ZeroShotImageClassificationPipeline. + * @param {TextImagePipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {ZeroShotImageClassificationPipelineCallback} */ + async _call(images, candidate_labels, { + hypothesis_template = "This is a photo of {}" + } = {}) { + + const isBatched = Array.isArray(images); + const preparedImages = await prepareImages(images); + + // Insert label into hypothesis template + const texts = candidate_labels.map( + x => hypothesis_template.replace('{}', x) + ); + + // Run tokenization + const text_inputs = this.tokenizer(texts, { + padding: this.model.config.model_type === 'siglip' ? 'max_length' : true, + truncation: true, + }); + + // Run processor + const { pixel_values } = await this.processor(preparedImages); + + // Run model with both text and pixel inputs + const output = await this.model({ ...text_inputs, pixel_values }); + + const function_to_apply = + this.model.config.model_type === 'siglip' + ? batch => batch.sigmoid().data + : batch => (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_6__.softmax)(batch.data); + + // Compare each image with each candidate label + const toReturn = []; + for (const batch of output.logits_per_image) { + // Compute softmax per image + const probs = function_to_apply(batch); + + const result = [...probs].map((x, i) => ({ + score: x, + label: candidate_labels[i] + })); + result.sort((a, b) => b.score - a.score); // sort by score in descending order + toReturn.push(result); + } + + return isBatched ? toReturn : toReturn[0]; + } +} + + +/** + * @typedef {Object} ObjectDetectionPipelineSingle + * @property {string} label The class label identified by the model. + * @property {number} score The score attributed by the model for that label. + * @property {BoundingBox} box The bounding box of detected object in image's original size, or as a percentage if `percentage` is set to true. + * @typedef {ObjectDetectionPipelineSingle[]} ObjectDetectionPipelineOutput + * + * @typedef {Object} ObjectDetectionPipelineOptions Parameters specific to object detection pipelines. + * @property {number} [threshold=0.9] The threshold used to filter boxes by score. + * @property {boolean} [percentage=false] Whether to return the boxes coordinates in percentage (true) or in pixels (false). + * + * @callback ObjectDetectionPipelineCallback Detect objects (bounding boxes & classes) in the image(s) passed as inputs. + * @param {ImagePipelineInputs} images The input images. + * @param {ObjectDetectionPipelineOptions} [options] The options to use for object detection. + * @returns {Promise} A list of objects or a list of list of objects. + * + * @typedef {ImagePipelineConstructorArgs & ObjectDetectionPipelineCallback & Disposable} ObjectDetectionPipelineType + */ + +/** + * Object detection pipeline using any `AutoModelForObjectDetection`. + * This pipeline predicts bounding boxes of objects and their classes. + * + * **Example:** Run object-detection with `Xenova/detr-resnet-50`. + * ```javascript + * const detector = await pipeline('object-detection', 'Xenova/detr-resnet-50'); + * const img = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/cats.jpg'; + * const output = await detector(img, { threshold: 0.9 }); + * // [{ + * // score: 0.9976370930671692, + * // label: "remote", + * // box: { xmin: 31, ymin: 68, xmax: 190, ymax: 118 } + * // }, + * // ... + * // { + * // score: 0.9984092116355896, + * // label: "cat", + * // box: { xmin: 331, ymin: 19, xmax: 649, ymax: 371 } + * // }] + * ``` + */ +class ObjectDetectionPipeline extends (/** @type {new (options: ImagePipelineConstructorArgs) => ObjectDetectionPipelineType} */ (Pipeline)) { + + /** + * Create a new ObjectDetectionPipeline. + * @param {ImagePipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {ObjectDetectionPipelineCallback} */ + async _call(images, { + threshold = 0.9, + percentage = false, + } = {}) { + + const isBatched = Array.isArray(images); + + if (isBatched && images.length !== 1) { + throw Error("Object detection pipeline currently only supports a batch size of 1."); + } + const preparedImages = await prepareImages(images); + + const imageSizes = percentage ? null : preparedImages.map(x => [x.height, x.width]); + + const { pixel_values, pixel_mask } = await this.processor(preparedImages); + const output = await this.model({ pixel_values, pixel_mask }); + + // @ts-ignore + const processed = this.processor.image_processor.post_process_object_detection(output, threshold, imageSizes); + + // Add labels + // @ts-expect-error TS2339 + const id2label = this.model.config.id2label; + + // Format output + /** @type {ObjectDetectionPipelineOutput[]} */ + const result = processed.map(batch => ( + batch.boxes.map((box, i) => ({ + score: batch.scores[i], + label: id2label[batch.classes[i]], + box: get_bounding_box(box, !percentage), + })) + )) + + return isBatched ? result : result[0]; + } +} + + +/** + * @typedef {Object} ZeroShotObjectDetectionOutput + * @property {string} label Text query corresponding to the found object. + * @property {number} score Score corresponding to the object (between 0 and 1). + * @property {BoundingBox} box Bounding box of the detected object in image's original size, or as a percentage if `percentage` is set to true. + * + * @typedef {Object} ZeroShotObjectDetectionPipelineOptions Parameters specific to zero-shot object detection pipelines. + * @property {number} [threshold=0.1] The probability necessary to make a prediction. + * @property {number} [top_k=null] The number of top predictions that will be returned by the pipeline. + * If the provided number is `null` or higher than the number of predictions available, it will default + * to the number of predictions. + * @property {boolean} [percentage=false] Whether to return the boxes coordinates in percentage (true) or in pixels (false). + * + * @callback ZeroShotObjectDetectionPipelineCallback Detect objects (bounding boxes & classes) in the image(s) passed as inputs. + * @param {ImagePipelineInputs} images The input images. + * @param {string[]} candidate_labels What the model should recognize in the image. + * @param {ZeroShotObjectDetectionPipelineOptions} [options] The options to use for zero-shot object detection. + * @returns {Promise} An array of objects containing the predicted labels, scores, and bounding boxes. + * + * @typedef {TextImagePipelineConstructorArgs & ZeroShotObjectDetectionPipelineCallback & Disposable} ZeroShotObjectDetectionPipelineType + */ + +/** + * Zero-shot object detection pipeline. This pipeline predicts bounding boxes of + * objects when you provide an image and a set of `candidate_labels`. + * + * **Example:** Zero-shot object detection w/ `Xenova/owlvit-base-patch32`. + * ```javascript + * const detector = await pipeline('zero-shot-object-detection', 'Xenova/owlvit-base-patch32'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/astronaut.png'; + * const candidate_labels = ['human face', 'rocket', 'helmet', 'american flag']; + * const output = await detector(url, candidate_labels); + * // [ + * // { + * // score: 0.24392342567443848, + * // label: 'human face', + * // box: { xmin: 180, ymin: 67, xmax: 274, ymax: 175 } + * // }, + * // { + * // score: 0.15129457414150238, + * // label: 'american flag', + * // box: { xmin: 0, ymin: 4, xmax: 106, ymax: 513 } + * // }, + * // { + * // score: 0.13649864494800568, + * // label: 'helmet', + * // box: { xmin: 277, ymin: 337, xmax: 511, ymax: 511 } + * // }, + * // { + * // score: 0.10262022167444229, + * // label: 'rocket', + * // box: { xmin: 352, ymin: -1, xmax: 463, ymax: 287 } + * // } + * // ] + * ``` + * + * **Example:** Zero-shot object detection w/ `Xenova/owlvit-base-patch32` (returning top 4 matches and setting a threshold). + * ```javascript + * const detector = await pipeline('zero-shot-object-detection', 'Xenova/owlvit-base-patch32'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/beach.png'; + * const candidate_labels = ['hat', 'book', 'sunglasses', 'camera']; + * const output = await detector(url, candidate_labels, { top_k: 4, threshold: 0.05 }); + * // [ + * // { + * // score: 0.1606510728597641, + * // label: 'sunglasses', + * // box: { xmin: 347, ymin: 229, xmax: 429, ymax: 264 } + * // }, + * // { + * // score: 0.08935828506946564, + * // label: 'hat', + * // box: { xmin: 38, ymin: 174, xmax: 258, ymax: 364 } + * // }, + * // { + * // score: 0.08530698716640472, + * // label: 'camera', + * // box: { xmin: 187, ymin: 350, xmax: 260, ymax: 411 } + * // }, + * // { + * // score: 0.08349756896495819, + * // label: 'book', + * // box: { xmin: 261, ymin: 280, xmax: 494, ymax: 425 } + * // } + * // ] + * ``` + */ +class ZeroShotObjectDetectionPipeline extends (/** @type {new (options: TextImagePipelineConstructorArgs) => ZeroShotObjectDetectionPipelineType} */ (Pipeline)) { + + /** + * Create a new ZeroShotObjectDetectionPipeline. + * @param {TextImagePipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {ZeroShotObjectDetectionPipelineCallback} */ + async _call(images, candidate_labels, { + threshold = 0.1, + top_k = null, + percentage = false, + } = {}) { + + const isBatched = Array.isArray(images); + const preparedImages = await prepareImages(images); + + // Run tokenization + const text_inputs = this.tokenizer(candidate_labels, { + padding: true, + truncation: true, + }); + + // Run processor + const model_inputs = await this.processor(preparedImages); + + // Since non-maximum suppression is performed for exporting, we need to + // process each image separately. For more information, see: + // https://github.com/huggingface/optimum/blob/e3b7efb1257c011db907ef40ab340e795cc5684c/optimum/exporters/onnx/model_configs.py#L1028-L1032 + const toReturn = []; + for (let i = 0; i < preparedImages.length; ++i) { + const image = preparedImages[i]; + const imageSize = percentage ? null : [[image.height, image.width]]; + const pixel_values = model_inputs.pixel_values[i].unsqueeze_(0); + + // Run model with both text and pixel inputs + const output = await this.model({ ...text_inputs, pixel_values }); + + let result; + if ('post_process_grounded_object_detection' in this.processor) { + // @ts-ignore + const processed = this.processor.post_process_grounded_object_detection( + output, + text_inputs.input_ids, + { + // TODO: support separate threshold values + box_threshold: threshold, + text_threshold: threshold, + target_sizes: imageSize, + }, + )[0]; + result = processed.boxes.map((box, i) => ({ + score: processed.scores[i], + label: processed.labels[i], + box: get_bounding_box(box, !percentage), + })) + } else { + // @ts-ignore + const processed = this.processor.image_processor.post_process_object_detection(output, threshold, imageSize, true)[0]; + result = processed.boxes.map((box, i) => ({ + score: processed.scores[i], + label: candidate_labels[processed.classes[i]], + box: get_bounding_box(box, !percentage), + })) + } + result.sort((a, b) => b.score - a.score); + + if (top_k !== null) { + result = result.slice(0, top_k); + } + toReturn.push(result) + } + + return isBatched ? toReturn : toReturn[0]; + } +} + +/** + * @typedef {Object} DocumentQuestionAnsweringSingle + * @property {string} answer The generated text. + * @typedef {DocumentQuestionAnsweringSingle[]} DocumentQuestionAnsweringOutput + * + * @callback DocumentQuestionAnsweringPipelineCallback Answer the question given as input by using the document. + * @param {ImageInput} image The image of the document to use. + * @param {string} question A question to ask of the document. + * @param {Partial} [options] Additional keyword arguments to pass along to the generate method of the model. + * @returns {Promise} An object (or array of objects) containing the answer(s). + * + * @typedef {TextImagePipelineConstructorArgs & DocumentQuestionAnsweringPipelineCallback & Disposable} DocumentQuestionAnsweringPipelineType + */ + +/** + * Document Question Answering pipeline using any `AutoModelForDocumentQuestionAnswering`. + * The inputs/outputs are similar to the (extractive) question answering pipeline; however, + * the pipeline takes an image (and optional OCR'd words/boxes) as input instead of text context. + * + * **Example:** Answer questions about a document with `Xenova/donut-base-finetuned-docvqa`. + * ```javascript + * const qa_pipeline = await pipeline('document-question-answering', 'Xenova/donut-base-finetuned-docvqa'); + * const image = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/invoice.png'; + * const question = 'What is the invoice number?'; + * const output = await qa_pipeline(image, question); + * // [{ answer: 'us-001' }] + * ``` + */ +class DocumentQuestionAnsweringPipeline extends (/** @type {new (options: TextImagePipelineConstructorArgs) => DocumentQuestionAnsweringPipelineType} */ (Pipeline)) { + + /** + * Create a new DocumentQuestionAnsweringPipeline. + * @param {TextImagePipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {DocumentQuestionAnsweringPipelineCallback} */ + async _call(image, question, generate_kwargs = {}) { + + // NOTE: For now, we only support a batch size of 1 + + // Preprocess image + const preparedImage = (await prepareImages(image))[0]; + const { pixel_values } = await this.processor(preparedImage); + + // Run tokenization + const task_prompt = `${question}`; + const decoder_input_ids = this.tokenizer(task_prompt, { + add_special_tokens: false, + padding: true, + truncation: true, + }).input_ids; + + // Run model + const output = await this.model.generate({ + inputs: pixel_values, + // @ts-expect-error TS2339 + max_length: this.model.config.decoder.max_position_embeddings, + decoder_input_ids, + ...generate_kwargs, + }); + + // Decode output + const decoded = this.tokenizer.batch_decode(/** @type {Tensor} */(output))[0]; + + // Parse answer + const match = decoded.match(/(.*?)<\/s_answer>/); + let answer = null; + if (match && match.length >= 2) { + answer = match[1].trim(); + } + return [{ answer }]; + } +} + + +/** + * @typedef {Object} VocoderOptions + * @property {PreTrainedModel} [vocoder] The vocoder used by the pipeline (if the model uses one). If not provided, use the default HifiGan vocoder. + * @typedef {TextAudioPipelineConstructorArgs & VocoderOptions} TextToAudioPipelineConstructorArgs + */ + +/** + * @typedef {Object} TextToAudioOutput + * @property {Float32Array} audio The generated audio waveform. + * @property {number} sampling_rate The sampling rate of the generated audio waveform. + * + * @typedef {Object} TextToAudioPipelineOptions Parameters specific to text-to-audio pipelines. + * @property {Tensor|Float32Array|string|URL} [speaker_embeddings=null] The speaker embeddings (if the model requires it). + * + * @callback TextToAudioPipelineCallback Generates speech/audio from the inputs. + * @param {string|string[]} texts The text(s) to generate. + * @param {TextToAudioPipelineOptions} options Parameters passed to the model generation/forward method. + * @returns {Promise} An object containing the generated audio and sampling rate. + * + * @typedef {TextToAudioPipelineConstructorArgs & TextToAudioPipelineCallback & Disposable} TextToAudioPipelineType + */ + +/** + * Text-to-audio generation pipeline using any `AutoModelForTextToWaveform` or `AutoModelForTextToSpectrogram`. + * This pipeline generates an audio file from an input text and optional other conditional inputs. + * + * **Example:** Generate audio from text with `Xenova/speecht5_tts`. + * ```javascript + * const synthesizer = await pipeline('text-to-speech', 'Xenova/speecht5_tts', { quantized: false }); + * const speaker_embeddings = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/speaker_embeddings.bin'; + * const out = await synthesizer('Hello, my dog is cute', { speaker_embeddings }); + * // RawAudio { + * // audio: Float32Array(26112) [-0.00005657337896991521, 0.00020583874720614403, ...], + * // sampling_rate: 16000 + * // } + * ``` + * + * You can then save the audio to a .wav file with the `wavefile` package: + * ```javascript + * import wavefile from 'wavefile'; + * import fs from 'fs'; + * + * const wav = new wavefile.WaveFile(); + * wav.fromScratch(1, out.sampling_rate, '32f', out.audio); + * fs.writeFileSync('out.wav', wav.toBuffer()); + * ``` + * + * **Example:** Multilingual speech generation with `Xenova/mms-tts-fra`. See [here](https://huggingface.co/models?pipeline_tag=text-to-speech&other=vits&sort=trending) for the full list of available languages (1107). + * ```javascript + * const synthesizer = await pipeline('text-to-speech', 'Xenova/mms-tts-fra'); + * const out = await synthesizer('Bonjour'); + * // RawAudio { + * // audio: Float32Array(23808) [-0.00037693005288019776, 0.0003325853613205254, ...], + * // sampling_rate: 16000 + * // } + * ``` + */ +class TextToAudioPipeline extends (/** @type {new (options: TextToAudioPipelineConstructorArgs) => TextToAudioPipelineType} */ (Pipeline)) { + DEFAULT_VOCODER_ID = "Xenova/speecht5_hifigan" + + /** + * Create a new TextToAudioPipeline. + * @param {TextToAudioPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + + // TODO: Find a better way for `pipeline` to set the default vocoder + this.vocoder = options.vocoder ?? null; + } + + + /** @type {TextToAudioPipelineCallback} */ + async _call(text_inputs, { + speaker_embeddings = null, + } = {}) { + + // If this.processor is not set, we are using a `AutoModelForTextToWaveform` model + if (this.processor) { + return this._call_text_to_spectrogram(text_inputs, { speaker_embeddings }); + } else { + return this._call_text_to_waveform(text_inputs); + } + } + + async _call_text_to_waveform(text_inputs) { + + // Run tokenization + const inputs = this.tokenizer(text_inputs, { + padding: true, + truncation: true, + }); + + // Generate waveform + const { waveform } = await this.model(inputs); + + // @ts-expect-error TS2339 + const sampling_rate = this.model.config.sampling_rate; + return new _utils_audio_js__WEBPACK_IMPORTED_MODULE_7__.RawAudio( + waveform.data, + sampling_rate, + ) + } + + async _call_text_to_spectrogram(text_inputs, { speaker_embeddings }) { + + // Load vocoder, if not provided + if (!this.vocoder) { + console.log('No vocoder specified, using default HifiGan vocoder.'); + this.vocoder = await _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModel.from_pretrained(this.DEFAULT_VOCODER_ID, { dtype: 'fp32' }); + } + + // Load speaker embeddings as Float32Array from path/URL + if (typeof speaker_embeddings === 'string' || speaker_embeddings instanceof URL) { + // Load from URL with fetch + speaker_embeddings = new Float32Array( + await (await fetch(speaker_embeddings)).arrayBuffer() + ); + } + + if (speaker_embeddings instanceof Float32Array) { + speaker_embeddings = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.Tensor( + 'float32', + speaker_embeddings, + [1, speaker_embeddings.length] + ) + } else if (!(speaker_embeddings instanceof _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.Tensor)) { + throw new Error("Speaker embeddings must be a `Tensor`, `Float32Array`, `string`, or `URL`.") + } + + // Run tokenization + const { input_ids } = this.tokenizer(text_inputs, { + padding: true, + truncation: true, + }); + + // NOTE: At this point, we are guaranteed that `speaker_embeddings` is a `Tensor` + // @ts-ignore + const { waveform } = await this.model.generate_speech(input_ids, speaker_embeddings, { vocoder: this.vocoder }); + + const sampling_rate = this.processor.feature_extractor.config.sampling_rate; + return new _utils_audio_js__WEBPACK_IMPORTED_MODULE_7__.RawAudio( + waveform.data, + sampling_rate, + ) + } +} + +/** + * @callback ImageToImagePipelineCallback Transform the image(s) passed as inputs. + * @param {ImagePipelineInputs} images The images to transform. + * @returns {Promise} The transformed image or list of images. + * + * @typedef {ImagePipelineConstructorArgs & ImageToImagePipelineCallback & Disposable} ImageToImagePipelineType + */ + +/** + * Image to Image pipeline using any `AutoModelForImageToImage`. This pipeline generates an image based on a previous image input. + * + * **Example:** Super-resolution w/ `Xenova/swin2SR-classical-sr-x2-64` + * ```javascript + * const upscaler = await pipeline('image-to-image', 'Xenova/swin2SR-classical-sr-x2-64'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/butterfly.jpg'; + * const output = await upscaler(url); + * // RawImage { + * // data: Uint8Array(786432) [ 41, 31, 24, 43, ... ], + * // width: 512, + * // height: 512, + * // channels: 3 + * // } + * ``` + */ +class ImageToImagePipeline extends (/** @type {new (options: ImagePipelineConstructorArgs) => ImageToImagePipelineType} */ (Pipeline)) { + /** + * Create a new ImageToImagePipeline. + * @param {ImagePipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {ImageToImagePipelineCallback} */ + async _call(images) { + + const preparedImages = await prepareImages(images); + const inputs = await this.processor(preparedImages); + const outputs = await this.model(inputs); + + /** @type {RawImage[]} */ + const toReturn = []; + for (const batch of outputs.reconstruction) { + const output = batch.squeeze().clamp_(0, 1).mul_(255).round_().to('uint8'); + toReturn.push(_utils_image_js__WEBPACK_IMPORTED_MODULE_9__.RawImage.fromTensor(output)); + } + + return toReturn.length > 1 ? toReturn : toReturn[0]; + } +} + +/** + * @typedef {Object} DepthEstimationPipelineOutput + * @property {Tensor} predicted_depth The raw depth map predicted by the model. + * @property {RawImage} depth The processed depth map as an image (with the same size as the input image). + * + * @callback DepthEstimationPipelineCallback Predicts the depth for the image(s) passed as inputs. + * @param {ImagePipelineInputs} images The images to compute depth for. + * @returns {Promise} An image or a list of images containing result(s). + * + * @typedef {ImagePipelineConstructorArgs & DepthEstimationPipelineCallback & Disposable} DepthEstimationPipelineType + */ + +/** + * Depth estimation pipeline using any `AutoModelForDepthEstimation`. This pipeline predicts the depth of an image. + * + * **Example:** Depth estimation w/ `Xenova/dpt-hybrid-midas` + * ```javascript + * const depth_estimator = await pipeline('depth-estimation', 'Xenova/dpt-hybrid-midas'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/cats.jpg'; + * const out = await depth_estimator(url); + * // { + * // predicted_depth: Tensor { + * // dims: [ 384, 384 ], + * // type: 'float32', + * // data: Float32Array(147456) [ 542.859130859375, 545.2833862304688, 546.1649169921875, ... ], + * // size: 147456 + * // }, + * // depth: RawImage { + * // data: Uint8Array(307200) [ 86, 86, 86, ... ], + * // width: 640, + * // height: 480, + * // channels: 1 + * // } + * // } + * ``` + */ +class DepthEstimationPipeline extends (/** @type {new (options: ImagePipelineConstructorArgs) => DepthEstimationPipelineType} */ (Pipeline)) { + /** + * Create a new DepthEstimationPipeline. + * @param {ImagePipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {DepthEstimationPipelineCallback} */ + async _call(images) { + + const preparedImages = await prepareImages(images); + + const inputs = await this.processor(preparedImages); + const { predicted_depth } = await this.model(inputs); + + const toReturn = []; + for (let i = 0; i < preparedImages.length; ++i) { + const batch = predicted_depth[i]; + const [height, width] = batch.dims.slice(-2); + const [new_width, new_height] = preparedImages[i].size; + + // Interpolate to original size + const prediction = (await (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.interpolate_4d)(batch.view(1, 1, height, width), { + size: [new_height, new_width], + mode: 'bilinear', + })).view(new_height, new_width); + + const minval = /** @type {number} */(prediction.min().item()); + const maxval = /** @type {number} */(prediction.max().item()); + const formatted = prediction.sub(minval).div_(maxval - minval).mul_(255).to('uint8').unsqueeze(0); + const depth = _utils_image_js__WEBPACK_IMPORTED_MODULE_9__.RawImage.fromTensor(formatted); + toReturn.push({ + predicted_depth: prediction, + depth, + }); + } + + return toReturn.length > 1 ? toReturn : toReturn[0]; + } +} + +const SUPPORTED_TASKS = Object.freeze({ + "text-classification": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": TextClassificationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSequenceClassification, + "default": { + // TODO: replace with original + // "model": "distilbert-base-uncased-finetuned-sst-2-english", + "model": "Xenova/distilbert-base-uncased-finetuned-sst-2-english", + }, + "type": "text", + }, + "token-classification": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": TokenClassificationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForTokenClassification, + "default": { + // TODO: replace with original + // "model": "Davlan/bert-base-multilingual-cased-ner-hrl", + "model": "Xenova/bert-base-multilingual-cased-ner-hrl", + }, + "type": "text", + }, + "question-answering": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": QuestionAnsweringPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForQuestionAnswering, + "default": { + // TODO: replace with original + // "model": "distilbert-base-cased-distilled-squad", + "model": "Xenova/distilbert-base-cased-distilled-squad", + }, + "type": "text", + }, + + "fill-mask": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": FillMaskPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForMaskedLM, + "default": { + // TODO: replace with original + // "model": "bert-base-uncased", + "model": "Xenova/bert-base-uncased", + }, + "type": "text", + }, + "summarization": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": SummarizationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSeq2SeqLM, + "default": { + // TODO: replace with original + // "model": "sshleifer/distilbart-cnn-6-6", + "model": "Xenova/distilbart-cnn-6-6", + }, + "type": "text", + }, + "translation": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": TranslationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSeq2SeqLM, + "default": { + // TODO: replace with original + // "model": "t5-small", + "model": "Xenova/t5-small", + }, + "type": "text", + }, + "text2text-generation": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": Text2TextGenerationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSeq2SeqLM, + "default": { + // TODO: replace with original + // "model": "google/flan-t5-small", + "model": "Xenova/flan-t5-small", + }, + "type": "text", + }, + "text-generation": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": TextGenerationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForCausalLM, + "default": { + // TODO: replace with original + // "model": "gpt2", + "model": "Xenova/gpt2", + }, + "type": "text", + }, + "zero-shot-classification": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": ZeroShotClassificationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSequenceClassification, + "default": { + // TODO: replace with original + // "model": "typeform/distilbert-base-uncased-mnli", + "model": "Xenova/distilbert-base-uncased-mnli", + }, + "type": "text", + }, + "audio-classification": { + "pipeline": AudioClassificationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForAudioClassification, + "processor": _models_auto_processing_auto_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "superb/wav2vec2-base-superb-ks", + "model": "Xenova/wav2vec2-base-superb-ks", + }, + "type": "audio", + }, + "zero-shot-audio-classification": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": ZeroShotAudioClassificationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModel, + "processor": _models_auto_processing_auto_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "laion/clap-htsat-fused", + "model": "Xenova/clap-htsat-unfused", + }, + "type": "multimodal", + }, + "automatic-speech-recognition": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": AutomaticSpeechRecognitionPipeline, + "model": [_models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSpeechSeq2Seq, _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForCTC], + "processor": _models_auto_processing_auto_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "openai/whisper-tiny.en", + "model": "Xenova/whisper-tiny.en", + }, + "type": "multimodal", + }, + "text-to-audio": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": TextToAudioPipeline, + "model": [_models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForTextToWaveform, _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForTextToSpectrogram], + "processor": [_models_auto_processing_auto_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, /* Some don't use a processor */ null], + "default": { + // TODO: replace with original + // "model": "microsoft/speecht5_tts", + "model": "Xenova/speecht5_tts", + }, + "type": "text", + }, + "image-to-text": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": ImageToTextPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForVision2Seq, + "processor": _models_auto_processing_auto_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "nlpconnect/vit-gpt2-image-captioning", + "model": "Xenova/vit-gpt2-image-captioning", + }, + "type": "multimodal", + }, + + "image-classification": { + // no tokenizer + "pipeline": ImageClassificationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForImageClassification, + "processor": _models_auto_processing_auto_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "google/vit-base-patch16-224", + "model": "Xenova/vit-base-patch16-224", + }, + "type": "multimodal", + }, + + "image-segmentation": { + // no tokenizer + "pipeline": ImageSegmentationPipeline, + "model": [_models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForImageSegmentation, _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSemanticSegmentation, _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForUniversalSegmentation], + "processor": _models_auto_processing_auto_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "facebook/detr-resnet-50-panoptic", + "model": "Xenova/detr-resnet-50-panoptic", + }, + "type": "multimodal", + }, + "background-removal": { + // no tokenizer + "pipeline": BackgroundRemovalPipeline, + "model": [_models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForImageSegmentation, _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSemanticSegmentation, _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForUniversalSegmentation], + "processor": _models_auto_processing_auto_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + "model": "Xenova/modnet", + }, + "type": "image", + }, + + "zero-shot-image-classification": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": ZeroShotImageClassificationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModel, + "processor": _models_auto_processing_auto_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "openai/clip-vit-base-patch32", + "model": "Xenova/clip-vit-base-patch32", + }, + "type": "multimodal", + }, + + "object-detection": { + // no tokenizer + "pipeline": ObjectDetectionPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForObjectDetection, + "processor": _models_auto_processing_auto_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "facebook/detr-resnet-50", + "model": "Xenova/detr-resnet-50", + }, + "type": "multimodal", + }, + "zero-shot-object-detection": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": ZeroShotObjectDetectionPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForZeroShotObjectDetection, + "processor": _models_auto_processing_auto_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "google/owlvit-base-patch32", + "model": "Xenova/owlvit-base-patch32", + }, + "type": "multimodal", + }, + "document-question-answering": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": DocumentQuestionAnsweringPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForDocumentQuestionAnswering, + "processor": _models_auto_processing_auto_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "naver-clova-ix/donut-base-finetuned-docvqa", + "model": "Xenova/donut-base-finetuned-docvqa", + }, + "type": "multimodal", + }, + "image-to-image": { + // no tokenizer + "pipeline": ImageToImagePipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForImageToImage, + "processor": _models_auto_processing_auto_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "caidas/swin2SR-classical-sr-x2-64", + "model": "Xenova/swin2SR-classical-sr-x2-64", + }, + "type": "image", + }, + "depth-estimation": { + // no tokenizer + "pipeline": DepthEstimationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForDepthEstimation, + "processor": _models_auto_processing_auto_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "Intel/dpt-large", + "model": "Xenova/dpt-large", + }, + "type": "image", + }, + + // This task serves as a useful interface for dealing with sentence-transformers (https://huggingface.co/sentence-transformers). + "feature-extraction": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": FeatureExtractionPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModel, + "default": { + // TODO: replace with original + // "model": "sentence-transformers/all-MiniLM-L6-v2", + "model": "Xenova/all-MiniLM-L6-v2", + }, + "type": "text", + }, + "image-feature-extraction": { + "processor": _models_auto_processing_auto_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "pipeline": ImageFeatureExtractionPipeline, + "model": [_models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForImageFeatureExtraction, _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModel], + "default": { + // TODO: replace with original + // "model": "google/vit-base-patch16-224", + "model": "Xenova/vit-base-patch16-224-in21k", + }, + "type": "image", + }, +}) + + +// TODO: Add types for TASK_ALIASES +const TASK_ALIASES = Object.freeze({ + "sentiment-analysis": "text-classification", + "ner": "token-classification", + // "vqa": "visual-question-answering", // TODO: Add + "asr": "automatic-speech-recognition", + "text-to-speech": "text-to-audio", + + // Add for backwards compatibility + "embeddings": "feature-extraction", +}); + +/** + * @typedef {keyof typeof SUPPORTED_TASKS} TaskType + * @typedef {keyof typeof TASK_ALIASES} AliasType + * @typedef {TaskType | AliasType} PipelineType All possible pipeline types. + * @typedef {{[K in TaskType]: InstanceType}} SupportedTasks A mapping of pipeline names to their corresponding pipeline classes. + * @typedef {{[K in AliasType]: InstanceType}} AliasTasks A mapping from pipeline aliases to their corresponding pipeline classes. + * @typedef {SupportedTasks & AliasTasks} AllTasks A mapping from all pipeline names and aliases to their corresponding pipeline classes. + */ + +/** + * Utility factory method to build a `Pipeline` object. + * + * @template {PipelineType} T The type of pipeline to return. + * @param {T} task The task defining which pipeline will be returned. Currently accepted tasks are: + * - `"audio-classification"`: will return a `AudioClassificationPipeline`. + * - `"automatic-speech-recognition"`: will return a `AutomaticSpeechRecognitionPipeline`. + * - `"depth-estimation"`: will return a `DepthEstimationPipeline`. + * - `"document-question-answering"`: will return a `DocumentQuestionAnsweringPipeline`. + * - `"feature-extraction"`: will return a `FeatureExtractionPipeline`. + * - `"fill-mask"`: will return a `FillMaskPipeline`. + * - `"image-classification"`: will return a `ImageClassificationPipeline`. + * - `"image-segmentation"`: will return a `ImageSegmentationPipeline`. + * - `"image-to-text"`: will return a `ImageToTextPipeline`. + * - `"object-detection"`: will return a `ObjectDetectionPipeline`. + * - `"question-answering"`: will return a `QuestionAnsweringPipeline`. + * - `"summarization"`: will return a `SummarizationPipeline`. + * - `"text2text-generation"`: will return a `Text2TextGenerationPipeline`. + * - `"text-classification"` (alias "sentiment-analysis" available): will return a `TextClassificationPipeline`. + * - `"text-generation"`: will return a `TextGenerationPipeline`. + * - `"token-classification"` (alias "ner" available): will return a `TokenClassificationPipeline`. + * - `"translation"`: will return a `TranslationPipeline`. + * - `"translation_xx_to_yy"`: will return a `TranslationPipeline`. + * - `"zero-shot-classification"`: will return a `ZeroShotClassificationPipeline`. + * - `"zero-shot-audio-classification"`: will return a `ZeroShotAudioClassificationPipeline`. + * - `"zero-shot-image-classification"`: will return a `ZeroShotImageClassificationPipeline`. + * - `"zero-shot-object-detection"`: will return a `ZeroShotObjectDetectionPipeline`. + * @param {string} [model=null] The name of the pre-trained model to use. If not specified, the default model for the task will be used. + * @param {import('./utils/hub.js').PretrainedModelOptions} [options] Optional parameters for the pipeline. + * @returns {Promise} A Pipeline object for the specified task. + * @throws {Error} If an unsupported pipeline is requested. + */ +async function pipeline( + task, + model = null, + { + progress_callback = null, + config = null, + cache_dir = null, + local_files_only = false, + revision = 'main', + device = null, + dtype = null, + subfolder = 'onnx', + use_external_data_format = null, + model_file_name = null, + session_options = {}, + } = {} +) { + // Helper method to construct pipeline + + // Apply aliases + // @ts-ignore + task = TASK_ALIASES[task] ?? task; + + // Get pipeline info + const pipelineInfo = SUPPORTED_TASKS[task.split('_', 1)[0]]; + if (!pipelineInfo) { + throw Error(`Unsupported pipeline: ${task}. Must be one of [${Object.keys(SUPPORTED_TASKS)}]`) + } + + // Use model if specified, otherwise, use default + if (!model) { + model = pipelineInfo.default.model + console.log(`No model specified. Using default model: "${model}".`); + } + + const pretrainedOptions = { + progress_callback, + config, + cache_dir, + local_files_only, + revision, + device, + dtype, + subfolder, + use_external_data_format, + model_file_name, + session_options, + } + + const classes = new Map([ + ['tokenizer', pipelineInfo.tokenizer], + ['model', pipelineInfo.model], + ['processor', pipelineInfo.processor], + ]); + + // Load model, tokenizer, and processor (if they exist) + const results = await loadItems(classes, model, pretrainedOptions); + results.task = task; + + (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_5__.dispatchCallback)(progress_callback, { + 'status': 'ready', + 'task': task, + 'model': model, + }); + + const pipelineClass = pipelineInfo.pipeline; + return new pipelineClass(results); +} + + +/** + * Helper function to get applicable model, tokenizer, or processor classes for a given model. + * @param {Map} mapping The mapping of names to classes, arrays of classes, or null. + * @param {string} model The name of the model to load. + * @param {import('./utils/hub.js').PretrainedOptions} pretrainedOptions The options to pass to the `from_pretrained` method. + * @private + */ +async function loadItems(mapping, model, pretrainedOptions) { + + const result = Object.create(null); + + /**@type {Promise[]} */ + const promises = []; + for (const [name, cls] of mapping.entries()) { + if (!cls) continue; + + /**@type {Promise} */ + let promise; + if (Array.isArray(cls)) { + promise = new Promise(async (resolve, reject) => { + let e; + for (const c of cls) { + if (c === null) { + // If null, we resolve it immediately, meaning the relevant + // class was not found, but it is optional. + resolve(null); + return; + } + try { + resolve(await c.from_pretrained(model, pretrainedOptions)); + return; + } catch (err) { + if (err.message?.includes('Unsupported model type')) { + // If the error is due to an unsupported model type, we + // save the error and try the next class. + e = err; + } else if (err.message?.includes('Could not locate file')) { + e = err; + } else { + reject(err); + return; + } + + } + } + reject(e); + }) + } else { + promise = cls.from_pretrained(model, pretrainedOptions); + } + + result[name] = promise; + promises.push(promise); + } + + // Wait for all promises to resolve (in parallel) + await Promise.all(promises); + + // Then assign to result + for (const [name, promise] of Object.entries(result)) { + result[name] = await promise; + } + + return result; +} + + +/***/ }), + +/***/ "./src/tokenizers.js": +/*!***************************!*\ + !*** ./src/tokenizers.js ***! + \***************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ AlbertTokenizer: () => (/* binding */ AlbertTokenizer), +/* harmony export */ AutoTokenizer: () => (/* binding */ AutoTokenizer), +/* harmony export */ BartTokenizer: () => (/* binding */ BartTokenizer), +/* harmony export */ BertTokenizer: () => (/* binding */ BertTokenizer), +/* harmony export */ BlenderbotSmallTokenizer: () => (/* binding */ BlenderbotSmallTokenizer), +/* harmony export */ BlenderbotTokenizer: () => (/* binding */ BlenderbotTokenizer), +/* harmony export */ BloomTokenizer: () => (/* binding */ BloomTokenizer), +/* harmony export */ CLIPTokenizer: () => (/* binding */ CLIPTokenizer), +/* harmony export */ CamembertTokenizer: () => (/* binding */ CamembertTokenizer), +/* harmony export */ CodeGenTokenizer: () => (/* binding */ CodeGenTokenizer), +/* harmony export */ CodeLlamaTokenizer: () => (/* binding */ CodeLlamaTokenizer), +/* harmony export */ CohereTokenizer: () => (/* binding */ CohereTokenizer), +/* harmony export */ ConvBertTokenizer: () => (/* binding */ ConvBertTokenizer), +/* harmony export */ DebertaTokenizer: () => (/* binding */ DebertaTokenizer), +/* harmony export */ DebertaV2Tokenizer: () => (/* binding */ DebertaV2Tokenizer), +/* harmony export */ DistilBertTokenizer: () => (/* binding */ DistilBertTokenizer), +/* harmony export */ ElectraTokenizer: () => (/* binding */ ElectraTokenizer), +/* harmony export */ EsmTokenizer: () => (/* binding */ EsmTokenizer), +/* harmony export */ FalconTokenizer: () => (/* binding */ FalconTokenizer), +/* harmony export */ GPT2Tokenizer: () => (/* binding */ GPT2Tokenizer), +/* harmony export */ GPTNeoXTokenizer: () => (/* binding */ GPTNeoXTokenizer), +/* harmony export */ GemmaTokenizer: () => (/* binding */ GemmaTokenizer), +/* harmony export */ Grok1Tokenizer: () => (/* binding */ Grok1Tokenizer), +/* harmony export */ HerbertTokenizer: () => (/* binding */ HerbertTokenizer), +/* harmony export */ LlamaTokenizer: () => (/* binding */ LlamaTokenizer), +/* harmony export */ M2M100Tokenizer: () => (/* binding */ M2M100Tokenizer), +/* harmony export */ MBart50Tokenizer: () => (/* binding */ MBart50Tokenizer), +/* harmony export */ MBartTokenizer: () => (/* binding */ MBartTokenizer), +/* harmony export */ MPNetTokenizer: () => (/* binding */ MPNetTokenizer), +/* harmony export */ MarianTokenizer: () => (/* binding */ MarianTokenizer), +/* harmony export */ MgpstrTokenizer: () => (/* binding */ MgpstrTokenizer), +/* harmony export */ MobileBertTokenizer: () => (/* binding */ MobileBertTokenizer), +/* harmony export */ NllbTokenizer: () => (/* binding */ NllbTokenizer), +/* harmony export */ NougatTokenizer: () => (/* binding */ NougatTokenizer), +/* harmony export */ PreTrainedTokenizer: () => (/* binding */ PreTrainedTokenizer), +/* harmony export */ Qwen2Tokenizer: () => (/* binding */ Qwen2Tokenizer), +/* harmony export */ RoFormerTokenizer: () => (/* binding */ RoFormerTokenizer), +/* harmony export */ RobertaTokenizer: () => (/* binding */ RobertaTokenizer), +/* harmony export */ SiglipTokenizer: () => (/* binding */ SiglipTokenizer), +/* harmony export */ SpeechT5Tokenizer: () => (/* binding */ SpeechT5Tokenizer), +/* harmony export */ SqueezeBertTokenizer: () => (/* binding */ SqueezeBertTokenizer), +/* harmony export */ T5Tokenizer: () => (/* binding */ T5Tokenizer), +/* harmony export */ TokenizerModel: () => (/* binding */ TokenizerModel), +/* harmony export */ VitsTokenizer: () => (/* binding */ VitsTokenizer), +/* harmony export */ Wav2Vec2CTCTokenizer: () => (/* binding */ Wav2Vec2CTCTokenizer), +/* harmony export */ WhisperTokenizer: () => (/* binding */ WhisperTokenizer), +/* harmony export */ XLMRobertaTokenizer: () => (/* binding */ XLMRobertaTokenizer), +/* harmony export */ XLMTokenizer: () => (/* binding */ XLMTokenizer), +/* harmony export */ is_chinese_char: () => (/* binding */ is_chinese_char) +/* harmony export */ }); +/* harmony import */ var _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./utils/generic.js */ "./src/utils/generic.js"); +/* harmony import */ var _utils_core_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./utils/core.js */ "./src/utils/core.js"); +/* harmony import */ var _utils_hub_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./utils/hub.js */ "./src/utils/hub.js"); +/* harmony import */ var _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./utils/maths.js */ "./src/utils/maths.js"); +/* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ./utils/tensor.js */ "./src/utils/tensor.js"); +/* harmony import */ var _utils_data_structures_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ./utils/data-structures.js */ "./src/utils/data-structures.js"); +/* harmony import */ var _huggingface_jinja__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! @huggingface/jinja */ "./node_modules/@huggingface/jinja/dist/index.js"); +/* harmony import */ var _models_whisper_common_whisper_js__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__(/*! ./models/whisper/common_whisper.js */ "./src/models/whisper/common_whisper.js"); + +/** + * @file Tokenizers are used to prepare textual inputs for a model. + * + * **Example:** Create an `AutoTokenizer` and use it to tokenize a sentence. + * This will automatically detect the tokenizer type based on the tokenizer class defined in `tokenizer.json`. + * ```javascript + * import { AutoTokenizer } from '@huggingface/transformers'; + * + * const tokenizer = await AutoTokenizer.from_pretrained('Xenova/bert-base-uncased'); + * const { input_ids } = await tokenizer('I love transformers!'); + * // Tensor { + * // data: BigInt64Array(6) [101n, 1045n, 2293n, 19081n, 999n, 102n], + * // dims: [1, 6], + * // type: 'int64', + * // size: 6, + * // } + * ``` + * + * @module tokenizers + */ + + + + + + + + + + + + + + + +/** + * @typedef {Object} TokenizerProperties Additional tokenizer-specific properties. + * @property {boolean} [legacy=false] Whether or not the `legacy` behavior of the tokenizer should be used. + * @typedef {import('./utils/hub.js').PretrainedOptions & TokenizerProperties} PretrainedTokenizerOptions + */ + +/** + * Loads a tokenizer from the specified path. + * @param {string} pretrained_model_name_or_path The path to the tokenizer directory. + * @param {PretrainedTokenizerOptions} options Additional options for loading the tokenizer. + * @returns {Promise} A promise that resolves with information about the loaded tokenizer. + */ +async function loadTokenizer(pretrained_model_name_or_path, options) { + + const info = await Promise.all([ + (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_2__.getModelJSON)(pretrained_model_name_or_path, 'tokenizer.json', true, options), + (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_2__.getModelJSON)(pretrained_model_name_or_path, 'tokenizer_config.json', true, options), + ]) + + // Override legacy option if `options.legacy` is not null + if (options.legacy !== null) { + info[1].legacy = options.legacy; + } + return info; +} + + +/** + * Helper function to split a string on a regex, but keep the delimiters. + * This is required, because the JavaScript `.split()` method does not keep the delimiters, + * and wrapping in a capturing group causes issues with existing capturing groups (due to nesting). + * @param {string} text The text to split. + * @param {RegExp} regex The regex to split on. + * @returns {string[]} The split string. + */ +function regexSplit(text, regex) { + const result = []; + let prev = 0; + for (const match of text.matchAll(regex)) { + const fullMatch = match[0]; + if (prev < match.index) { + result.push(text.slice(prev, match.index)); + } + if (fullMatch.length > 0) { + result.push(fullMatch); + } + prev = match.index + fullMatch.length; + } + if (prev < text.length) { + result.push(text.slice(prev)); + } + return result; +} + + +/** + * Helper method to construct a pattern from a config object. + * @param {Object} pattern The pattern object. + * @param {boolean} invert Whether to invert the pattern. + * @returns {RegExp|null} The compiled pattern. + */ +function createPattern(pattern, invert = true) { + + if (pattern.Regex !== undefined) { + // In certain cases, the pattern may contain unnecessary escape sequences (e.g., \# or \& or \~). + // i.e., valid in Python (where the patterns are exported from) but invalid in JavaScript (where the patterns are parsed). + // This isn't an issue when creating the regex w/o the 'u' flag, but it is when the 'u' flag is used. + // For this reason, it is necessary to remove these backslashes before creating the regex. + // See https://stackoverflow.com/a/63007777/13989043 for more information + let regex = pattern.Regex.replace(/\\([#&~])/g, '$1'); // TODO: add more characters to this list if necessary + + // We also handle special cases where the regex contains invalid (non-JS compatible) syntax. + for (const [key, value] of PROBLEMATIC_REGEX_MAP) { + regex = regex.replaceAll(key, value); + } + + return new RegExp(regex, 'gu'); + + } else if (pattern.String !== undefined) { + const escaped = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.escapeRegExp)(pattern.String); + // NOTE: if invert is true, we wrap the pattern in a group so that it is kept when performing .split() + return new RegExp(invert ? escaped : `(${escaped})`, 'gu'); + + } else { + console.warn('Unknown pattern type:', pattern) + return null; + } +} + +/** + * Helper function to convert an Object to a Map + * @param {Object} obj The object to convert. + * @returns {Map} The map. + */ +function objectToMap(obj) { + return new Map(Object.entries(obj)); +} + +/** + * Helper function to convert a tensor to a list before decoding. + * @param {Tensor} tensor The tensor to convert. + * @returns {number[]} The tensor as a list. + */ +function prepareTensorForDecode(tensor) { + const dims = tensor.dims; + switch (dims.length) { + case 1: + return tensor.tolist(); + case 2: + if (dims[0] !== 1) { + throw new Error('Unable to decode tensor with `batch size !== 1`. Use `tokenizer.batch_decode(...)` for batched inputs.'); + } + return tensor.tolist()[0]; + default: + throw new Error(`Expected tensor to have 1-2 dimensions, got ${dims.length}.`) + } +} + +/** + * Clean up a list of simple English tokenization artifacts like spaces before punctuations and abbreviated forms + * @param {string} text The text to clean up. + * @returns {string} The cleaned up text. + */ +function clean_up_tokenization(text) { + // Clean up a list of simple English tokenization artifacts + // like spaces before punctuations and abbreviated forms + return text.replace(/ \./g, '.') + .replace(/ \?/g, '?') + .replace(/ \!/g, '!') + .replace(/ ,/g, ',') + .replace(/ \' /g, "'") + .replace(/ n\'t/g, "n't") + .replace(/ \'m/g, "'m") + .replace(/ \'s/g, "'s") + .replace(/ \'ve/g, "'ve") + .replace(/ \'re/g, "'re"); +} + +/** + * Helper function to remove accents from a string. + * @param {string} text The text to remove accents from. + * @returns {string} The text with accents removed. + */ +function remove_accents(text) { + return text.replace(/\p{M}/gu, ''); +} + +/** + * Helper function to lowercase a string and remove accents. + * @param {string} text The text to lowercase and remove accents from. + * @returns {string} The lowercased text with accents removed. + */ +function lowercase_and_remove_accent(text) { + return remove_accents(text.toLowerCase()); +} + + +/** + * Checks whether the given Unicode codepoint represents a CJK (Chinese, Japanese, or Korean) character. + * + * A "chinese character" is defined as anything in the CJK Unicode block: + * https://en.wikipedia.org/wiki/CJK_Unified_Ideographs_(Unicode_block) + * + * Note that the CJK Unicode block is NOT all Japanese and Korean characters, despite its name. + * The modern Korean Hangul alphabet is a different block, as is Japanese Hiragana and Katakana. + * Those alphabets are used to write space-separated words, so they are not treated specially + * and are handled like all other languages. + * + * @param {number|bigint} cp The Unicode codepoint to check. + * @returns {boolean} True if the codepoint represents a CJK character, false otherwise. + */ +function is_chinese_char(cp) { + return ( + (cp >= 0x4E00 && cp <= 0x9FFF) + || (cp >= 0x3400 && cp <= 0x4DBF) + || (cp >= 0x20000 && cp <= 0x2A6DF) + || (cp >= 0x2A700 && cp <= 0x2B73F) + || (cp >= 0x2B740 && cp <= 0x2B81F) + || (cp >= 0x2B820 && cp <= 0x2CEAF) + || (cp >= 0xF900 && cp <= 0xFAFF) + || (cp >= 0x2F800 && cp <= 0x2FA1F) + ) +} + +/** + * Helper function to fuse consecutive unknown tokens. + * @param {string[]} arr The list of input tokens + * @param {Map} tokens_to_ids The mapping from tokens to token ids. + * @param {number} unk_token_id The value to fuse on. + * @private + */ +function fuse_unk(arr, tokens_to_ids, unk_token_id) { + const fused = []; + let i = 0; + while (i < arr.length) { + fused.push(arr[i]) + if ((tokens_to_ids.get(arr[i]) ?? unk_token_id) !== unk_token_id) { + ++i; + continue; + } + + while (++i < arr.length && (tokens_to_ids.get(arr[i]) ?? unk_token_id) === unk_token_id) { + if (tokens_to_ids.get(fused.at(-1)) !== unk_token_id) { + fused[fused.length - 1] += arr[i]; + } + } + } + + return fused; +} + +/** + * Split a string on whitespace. + * @param {string} text The text to split. + * @returns {string[]} The split string. + */ +function whitespace_split(text) { + return text.match(/\S+/g) || []; +} + +const PUNCTUATION_REGEX = '\\p{P}\\u0021-\\u002F\\u003A-\\u0040\\u005B-\\u0060\\u007B-\\u007E'; +const PUNCTUATION_ONLY_REGEX = new RegExp(`^[${PUNCTUATION_REGEX}]+$`, 'gu'); +const BLOOM_SPLIT_CHARS = '.,!?\u2026\u3002\uff0c\u3001\u0964\u06d4\u060c'; + +// A mapping of regex patterns to their equivalent (but possibly longer) JS-compatible versions. +const PROBLEMATIC_REGEX_MAP = new Map([ + // This uses the case insensitive group modifier, which is not supported in JavaScript. + // When parsing the regex, an "Invalid group" error is thrown. + ["(?i:'s|'t|'re|'ve|'m|'ll|'d)", "(?:'([sS]|[tT]|[rR][eE]|[vV][eE]|[mM]|[lL][lL]|[dD]))"], + + // Used to override the default (invalid) regex of the bloom pretokenizer. + // For more information, see https://github.com/huggingface/transformers.js/issues/94 + [` ?[^(\\s|[${BLOOM_SPLIT_CHARS}])]+`, ` ?[^\\s${BLOOM_SPLIT_CHARS}]+`], +]) + + +/** + * Represent a token added by the user on top of the existing Model vocabulary. + * AddedToken can be configured to specify the behavior they should have in various situations like: + * - Whether they should only match single words + * - Whether to include any whitespace on its left or right + */ +class AddedToken { + /** + * Creates a new instance of AddedToken. + * @param {Object} config Added token configuration object. + * @param {string} config.content The content of the added token. + * @param {number} config.id The id of the added token. + * @param {boolean} [config.single_word=false] Whether this token must be a single word or can break words. + * @param {boolean} [config.lstrip=false] Whether this token should strip whitespaces on its left. + * @param {boolean} [config.rstrip=false] Whether this token should strip whitespaces on its right. + * @param {boolean} [config.normalized=false] Whether this token should be normalized. + * @param {boolean} [config.special=false] Whether this token is special. + */ + constructor(config) { + this.content = config.content; + this.id = config.id; + this.single_word = config.single_word ?? false; + this.lstrip = config.lstrip ?? false; + this.rstrip = config.rstrip ?? false; + this.special = config.special ?? false; + this.normalized = config.normalized ?? null; + } +} + +/** + * Abstract base class for tokenizer models. + * + * @extends Callable + */ +class TokenizerModel extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * Creates a new instance of TokenizerModel. + * @param {Object} config The configuration object for the TokenizerModel. + */ + constructor(config) { + super(); + this.config = config; + + /** @type {string[]} */ + this.vocab = []; + + /** + * A mapping of tokens to ids. + * @type {Map} + */ + this.tokens_to_ids = new Map(); + + this.unk_token_id = undefined; + this.unk_token = undefined; + this.end_of_word_suffix = undefined; + + /** @type {boolean} Whether to fuse unknown tokens when encoding. Defaults to false. */ + this.fuse_unk = this.config.fuse_unk ?? false; + } + + /** + * Instantiates a new TokenizerModel instance based on the configuration object provided. + * @param {Object} config The configuration object for the TokenizerModel. + * @param {...*} args Optional arguments to pass to the specific TokenizerModel constructor. + * @returns {TokenizerModel} A new instance of a TokenizerModel. + * @throws Will throw an error if the TokenizerModel type in the config is not recognized. + */ + static fromConfig(config, ...args) { + switch (config.type) { + case 'WordPiece': + return new WordPieceTokenizer(config); + case 'Unigram': + // @ts-ignore + return new Unigram(config, ...args); + case 'BPE': + return new BPE(config); + + default: + // Some older tokenizers, like `google-t5/t5-small` and `distilbert/distilbert-base-uncased`, do not have a `type` field. + // In this case, we can infer the tokenizer type based on the structure of the `vocab` field and other properties. + if (config.vocab) { + if (Array.isArray(config.vocab)) { + // config.vocab is of type `[string, number][]` + // @ts-ignore + return new Unigram(config, ...args); + } else if (typeof config.vocab === 'object' && config.continuing_subword_prefix && config.unk_token) { + return new WordPieceTokenizer(config); + } else { + // @ts-ignore + return new LegacyTokenizerModel(config, ...args); + } + } + throw new Error(`Unknown TokenizerModel type: ${config.type}`); + } + } + + /** + * Internal function to call the TokenizerModel instance. + * @param {string[]} tokens The tokens to encode. + * @returns {string[]} The encoded tokens. + */ + _call(tokens) { + tokens = this.encode(tokens); + if (this.fuse_unk) { + // Fuse unknown tokens + tokens = fuse_unk(tokens, this.tokens_to_ids, this.unk_token_id); + } + return tokens; + } + + /** + * Encodes a list of tokens into a list of token IDs. + * @param {string[]} tokens The tokens to encode. + * @returns {string[]} The encoded tokens. + * @throws Will throw an error if not implemented in a subclass. + */ + encode(tokens) { + throw Error("encode should be implemented in subclass.") + } + + /** + * Converts a list of tokens into a list of token IDs. + * @param {string[]} tokens The tokens to convert. + * @returns {number[]} The converted token IDs. + */ + convert_tokens_to_ids(tokens) { + return tokens.map(t => this.tokens_to_ids.get(t) ?? this.unk_token_id); + } + + /** + * Converts a list of token IDs into a list of tokens. + * @param {number[]|bigint[]} ids The token IDs to convert. + * @returns {string[]} The converted tokens. + */ + convert_ids_to_tokens(ids) { + return ids.map(i => this.vocab[i] ?? this.unk_token); + } +} + +/** + * A subclass of TokenizerModel that uses WordPiece encoding to encode tokens. + * @extends TokenizerModel + */ +class WordPieceTokenizer extends TokenizerModel { + /** + * @param {Object} config The configuration object. + * @param {Object} config.vocab A mapping of tokens to ids. + * @param {string} config.unk_token The unknown token string. + * @param {string} config.continuing_subword_prefix The prefix to use for continuing subwords. + * @param {number} [config.max_input_chars_per_word=100] The maximum number of characters per word. + */ + constructor(config) { + super(config); + /** + * A mapping of tokens to ids. + * @type {Map} + */ + this.tokens_to_ids = objectToMap(config.vocab); + + /** + * The id of the unknown token. + * @type {number} + */ + this.unk_token_id = this.tokens_to_ids.get(config.unk_token); + + /** + * The unknown token string. + * @type {string} + */ + this.unk_token = config.unk_token; + + /** + * The maximum number of characters allowed per word. + * @type {number} + */ + this.max_input_chars_per_word = config.max_input_chars_per_word ?? 100; + + /** + * An array of tokens. + * @type {string[]} + */ + this.vocab = new Array(this.tokens_to_ids.size); + for (const [key, value] of this.tokens_to_ids) { + this.vocab[value] = key; + } + } + + /** + * Encodes an array of tokens using WordPiece encoding. + * @param {string[]} tokens The tokens to encode. + * @returns {string[]} An array of encoded tokens. + */ + encode(tokens) { + const outputTokens = []; + for (const token of tokens) { + const chars = [...token]; + if (chars.length > this.max_input_chars_per_word) { + outputTokens.push(this.unk_token); + continue; + } + + let isUnknown = false; + let start = 0; + const subTokens = []; + + while (start < chars.length) { + let end = chars.length; + let currentSubstring = null; + while (start < end) { + let substr = chars.slice(start, end).join(''); + + if (start > 0) { + substr = this.config.continuing_subword_prefix + substr; + } + if (this.tokens_to_ids.has(substr)) { + currentSubstring = substr; + break; + } + + --end; + } + if (currentSubstring === null) { + isUnknown = true; + break; + } + subTokens.push(currentSubstring); + start = end; + } + if (isUnknown) { + outputTokens.push(this.unk_token); + } else { + outputTokens.push(...subTokens); + } + } + + return outputTokens; + } + +} + +/** + * Class representing a Unigram tokenizer model. + * @extends TokenizerModel + */ +class Unigram extends TokenizerModel { + /** + * Create a new Unigram tokenizer model. + * @param {Object} config The configuration object for the Unigram model. + * @param {number} config.unk_id The ID of the unknown token + * @param {[string, number][]} config.vocab A 2D array representing a mapping of tokens to scores. + * @param {Object} moreConfig Additional configuration object for the Unigram model. + */ + constructor(config, moreConfig) { + super(config); + + const vocabSize = config.vocab.length; + this.vocab = new Array(vocabSize); + /** @type {number[]} */ + this.scores = new Array(vocabSize); + for (let i = 0; i < vocabSize; ++i) { + [this.vocab[i], this.scores[i]] = config.vocab[i]; + } + + this.unk_token_id = config.unk_id; + this.unk_token = this.vocab[config.unk_id]; + + this.tokens_to_ids = new Map(this.vocab.map((x, i) => [x, i])); + this.bos_token = ' '; // beginning of a sentence token + + this.bos_token_id = this.tokens_to_ids.get(this.bos_token); // NOTE: may be undefined + this.eos_token = moreConfig.eos_token; + + this.eos_token_id = this.tokens_to_ids.get(this.eos_token); + this.unk_token = this.vocab[this.unk_token_id]; + + this.minScore = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.min)(this.scores)[0]; + + this.unk_score = this.minScore - 10.0; + this.scores[this.unk_token_id] = this.unk_score; + + this.trie = new _utils_data_structures_js__WEBPACK_IMPORTED_MODULE_5__.CharTrie(); + this.trie.extend(this.vocab); + + // NOTE: `fuse_unk` is hardcoded to true for Unigram models + // See: https://github.com/huggingface/tokenizers/blob/b58227c7f1ccf8b73ee2268354336da56d91e492/tokenizers/src/models/unigram/model.rs#L119 + this.fuse_unk = true; + } + + /** + * Populates lattice nodes. + * @param {TokenLattice} lattice The token lattice to populate with nodes. + */ + populateNodes(lattice) { + const chars = lattice.chars; + const mblen = 1; + let beginPos = 0; + while (beginPos < chars.length) { + let hasSingleNode = false; + + const tokens = []; + const sliced = chars.slice(beginPos).join(''); + const prefixedTokens = this.trie.commonPrefixSearch(sliced); + for (const token of prefixedTokens) { + tokens.push(token); + const tokenId = this.tokens_to_ids.get(token); + const tokenScore = this.scores[tokenId]; + const n = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.len)(token); + lattice.insert(beginPos, n, tokenScore, tokenId); + if (!hasSingleNode && n === mblen) { + hasSingleNode = true; + } + } + if (!hasSingleNode) { + lattice.insert(beginPos, mblen, this.unk_score, this.unk_token_id); + } + beginPos += mblen; + } + } + + /** + * Encodes an array of tokens into an array of subtokens using the unigram model. + * + * @param {string} normalized The normalized string. + * @returns {string[]} An array of subtokens obtained by encoding the input tokens using the unigram model. + */ + tokenize(normalized) { + const lattice = new _utils_data_structures_js__WEBPACK_IMPORTED_MODULE_5__.TokenLattice(normalized, this.bos_token_id, this.eos_token_id); + this.populateNodes(lattice); + return lattice.tokens(); + } + + /** + * Encodes an array of tokens using Unigram encoding. + * @param {string[]} tokens The tokens to encode. + * @returns {string[]} An array of encoded tokens. + */ + encode(tokens) { + const toReturn = []; + for (const token of tokens) { + const tokenized = this.tokenize(token); + toReturn.push(...tokenized); + } + return toReturn; + } + +} + +/** + * Returns list of utf-8 byte and a mapping to unicode strings. + * Specifically avoids mapping to whitespace/control characters the BPE code barfs on. + * @returns {Object} Object with utf-8 byte keys and unicode string values. + */ +const BYTES_TO_UNICODE = (() => { + // Returns list of utf-8 byte and a mapping to unicode strings. + // We specifically avoids mapping to whitespace/control characters + // the bpe code barfs on. + + const bs = [ + ...Array.from({ length: "~".charCodeAt(0) - "!".charCodeAt(0) + 1 }, (_, i) => i + "!".charCodeAt(0)), + ...Array.from({ length: "¬".charCodeAt(0) - "¡".charCodeAt(0) + 1 }, (_, i) => i + "¡".charCodeAt(0)), + ...Array.from({ length: "ÿ".charCodeAt(0) - "®".charCodeAt(0) + 1 }, (_, i) => i + "®".charCodeAt(0)), + ]; + const cs = bs.slice(); + let n = 0; + for (let b = 0; b < 256; ++b) { + if (!bs.includes(b)) { + bs.push(b); + cs.push(256 + n); + n += 1; + } + } + const ccs = cs.map(n => String.fromCharCode(n)); + return Object.fromEntries(bs.map((b, i) => [b, ccs[i]])); +})(); + +const UNICODE_TO_BYTES = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.reverseDictionary)(BYTES_TO_UNICODE); + + +/** + * @typedef {Object} BPENode + * @property {string} token The token associated with the node + * @property {number} bias A positional bias for the node. + * @property {number} [score] The score of the node. + * @property {BPENode} [prev] The previous node in the linked list. + * @property {BPENode} [next] The next node in the linked list. + */ + +/** + * BPE class for encoding text into Byte-Pair-Encoding (BPE) tokens. + * @extends TokenizerModel + */ +class BPE extends TokenizerModel { + /** + * Create a BPE instance. + * @param {Object} config The configuration object for BPE. + * @param {Object} config.vocab A mapping of tokens to ids. + * @param {string[]|[string, string][]} config.merges An array of BPE merges as strings. + * @param {string} config.unk_token The unknown token used for out of vocabulary words. + * @param {string} config.end_of_word_suffix The suffix to place at the end of each word. + * @param {string} [config.continuing_subword_suffix] The suffix to insert between words. + * @param {boolean} [config.byte_fallback=false] Whether to use spm byte-fallback trick (defaults to False) + * @param {boolean} [config.ignore_merges=false] Whether or not to match tokens with the vocab before using merges. + */ + constructor(config) { + super(config); + + /** @type {Map} */ + this.tokens_to_ids = objectToMap(config.vocab); + + this.unk_token_id = this.tokens_to_ids.get(config.unk_token); + this.unk_token = config.unk_token; + + this.vocab = new Array(this.tokens_to_ids.size); + for (const [key, value] of this.tokens_to_ids) { + this.vocab[value] = key; + } + + // Tokenizers >= 0.20.0 serializes BPE merges as a [string, string][] instead of a string[], + // which resolves the ambiguity for merges containing spaces. + const use_new_merge_format = Array.isArray(config.merges[0]); + + /** @type {[string, string][]} */ + this.merges = use_new_merge_format + ? /** @type {[string, string][]} */(config.merges) + : (/** @type {string[]} */(config.merges)).map(x => /** @type {[string, string]} */(x.split(' ', 2))); + this.bpe_ranks = new Map(this.merges.map((x, i) => [JSON.stringify(x), i])); + + this.end_of_word_suffix = config.end_of_word_suffix; + + // NOTE: `continuing_subword_suffix` is custom (to support `BlenderbotSmallTokenizer`) + this.continuing_subword_suffix = config.continuing_subword_suffix ?? null; + + this.byte_fallback = this.config.byte_fallback ?? false; + + if (this.byte_fallback) { + this.text_encoder = new TextEncoder(); + } + + this.ignore_merges = this.config.ignore_merges ?? false; + + /** + * The maximum length we should cache in a model. + * Strings that are too long have minimal chances to cache hit anyway + */ + this.max_length_to_cache = 256; + + /** + * The default capacity for a `BPE`'s internal cache. + */ + this.cache_capacity = 10000; + this.cache = new _utils_data_structures_js__WEBPACK_IMPORTED_MODULE_5__.LRUCache(this.cache_capacity); + } + + /** + * Clears the cache. + */ + clear_cache() { + this.cache.clear(); + } + + /** + * Apply Byte-Pair-Encoding (BPE) to a given token. Efficient heap-based priority + * queue implementation adapted from https://github.com/belladoreai/llama-tokenizer-js. + * @param {string} token The token to encode. + * @returns {string[]} The BPE encoded tokens. + */ + bpe(token) { + if (token.length === 0) { + return []; + } + + const cached = this.cache.get(token); + if (cached !== undefined) { + return cached; + } + + const word = Array.from(token); + if (this.end_of_word_suffix) { + word[word.length - 1] += this.end_of_word_suffix; + } + + let result = []; + if (word.length > 1) { + // Create a priority queue to store the nodes that will be merged. + // The comparator function compares the scores of the nodes. + const queue = new _utils_data_structures_js__WEBPACK_IMPORTED_MODULE_5__.PriorityQueue((a, b) => a.score < b.score); + + // Construct a doubly-linked list of nodes that will be inserted into the priority queue, + // starting with the individual characters. We also populate each node with a positional + // bias to break ties in the priority queue. + let startingNode = { + token: word[0], + bias: 0, + prev: null, + next: null, + } + + let previousNode = startingNode + for (let i = 1; i < word.length; ++i) { + const currentNode = { + bias: i / word.length, // Add fractional component to break ties + token: word[i], + prev: previousNode, + next: null, + } + previousNode.next = currentNode + this._add_node(queue, previousNode) + previousNode = currentNode + } + + while (!queue.isEmpty()) { + // Get the next node with the highest priority + const node = queue.pop(); + + // Check that this merge is still possible + if (node.deleted || !node.next || node.next.deleted) continue; + + // Here, we mark the current node (left side of the merge) and the next node (right side of the merge) as deleted. + // This is because they will both be replaced by a new node representing the merge result. + node.deleted = true; + node.next.deleted = true; + + // Next, we fix the node that comes before the current node (i.e., left side of the merge). + if (node.prev) { + + // Make a shallow copy of the previous node + const newPreviousNode = { ...node.prev }; + + // Mark the old previous node as deleted. This avoids erroneous merges later, + // because there may still be references to this node in the priority queue. + node.prev.deleted = true; + node.prev = newPreviousNode; + + // Update the reference of the previous node, by pointing its previous node to this new previous node. + if (newPreviousNode.prev) { + newPreviousNode.prev.next = newPreviousNode; + } else { + // If the previous of the previous node does not exist, it means that + // `newPreviousNode` must be the new `startingNode`. + startingNode = newPreviousNode; + } + } + + // Create a new node which represents the result of the merge. + const merged = { + token: node.token + node.next.token, + bias: node.bias, + prev: node.prev, + next: node.next.next, + } + + // We now consider where we can add the new merged node to the priority queue: + // 1. prev <-> merged + if (merged.prev) { + merged.prev.next = merged; + this._add_node(queue, merged.prev); + } else { + // If `merged.prev` does not exist, then `merged` must be the new `startingNode`. + startingNode = merged; + } + + // 2. merged <-> next + if (merged.next) { + merged.next.prev = merged; + this._add_node(queue, merged); + } + } + + // Traverse the linked list, starting from the `startingNode`, and collect the tokens. + for (let currentNode = startingNode; currentNode !== null; currentNode = currentNode.next) { + result.push(currentNode.token); + } + } else { + result = word; + } + + // Possibly append suffix + if (this.continuing_subword_suffix) { + // Do not append suffix to the last token + for (let i = 0; i < result.length - 1; ++i) { + result[i] += this.continuing_subword_suffix; + } + } + + if (token.length < this.max_length_to_cache) { + // Save the result to the cache + this.cache.put(token, result); + } + + return result; + } + + + /** + * Helper function to add a node to the priority queue. + * @param {PriorityQueue} queue + * @param {BPENode} node + * @private + */ + _add_node(queue, node) { + // `score` is a measure of the merge priority: lower means higher priority + // We use the BPE rank as a measure of priority (i.e., the local of the merge in the merges list) + // We also add a fractional component to the score to break ties (with the earlier character having higher priority) + const rank = this.bpe_ranks.get(JSON.stringify([node.token, node.next.token])); + if (rank !== undefined) { + node.score = rank + node.bias; + queue.push(node); + } + } + + /** + * Encodes the input sequence of tokens using the BPE algorithm and returns the resulting subword tokens. + * @param {string[]} tokens The input sequence of tokens to encode. + * @returns {string[]} The resulting subword tokens after applying the BPE algorithm to the input sequence of tokens. + */ + encode(tokens) { + const outputTokens = []; + + for (const token of tokens) { + if (this.ignore_merges && this.tokens_to_ids.has(token)) { + outputTokens.push(token); + continue; + } + const bpe_token_list = this.bpe(token); + + for (const t of bpe_token_list) { + if (this.tokens_to_ids.has(t)) { + outputTokens.push(t); + } else if (this.byte_fallback) { + const byteTokens = Array.from(this.text_encoder.encode(t)) + .map(x => `<0x${x.toString(16).toUpperCase().padStart(2, '0')}>`); + if (byteTokens.every(x => this.tokens_to_ids.has(x))) { + // Ensure the byte tokens are actually in the vocabulary, otherwise + // we fall back to the unknown token. For more information, see + // https://github.com/huggingface/transformers/issues/28096. + outputTokens.push(...byteTokens); + } else { + outputTokens.push(this.unk_token); + } + } else { + outputTokens.push(this.unk_token); + } + } + } + + return outputTokens; + } + +} + +/** + * Legacy tokenizer class for tokenizers with only a vocabulary. + */ +class LegacyTokenizerModel extends TokenizerModel { + /** + * Create a LegacyTokenizerModel instance. + * @param {Object} config The configuration object for LegacyTokenizerModel. + * @param {Object} config.vocab A (possibly nested) mapping of tokens to ids. + * @param {Object} moreConfig Additional configuration object for the LegacyTokenizerModel model. + */ + constructor(config, moreConfig) { + super(config); + + /**@type {Map} */ + this.tokens_to_ids = objectToMap( + moreConfig.target_lang + ? config.vocab[moreConfig.target_lang] + : config.vocab + ); + + this.bos_token = moreConfig.bos_token; + this.bos_token_id = this.tokens_to_ids.get(this.bos_token); + + this.eos_token = moreConfig.eos_token; + this.eos_token_id = this.tokens_to_ids.get(this.eos_token); + + this.pad_token = moreConfig.pad_token; + this.pad_token_id = this.tokens_to_ids.get(this.pad_token); + + this.unk_token = moreConfig.unk_token; + this.unk_token_id = this.tokens_to_ids.get(this.unk_token); + + this.vocab = new Array(this.tokens_to_ids.size); + for (const [key, value] of this.tokens_to_ids) { + this.vocab[value] = key; + } + } + + encode(tokens) { + return tokens; + } +} + + +/** + * A base class for text normalization. + * @abstract + */ +class Normalizer extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * @param {Object} config The configuration object for the normalizer. + */ + constructor(config) { + super(); + this.config = config; + } + + /** + * Factory method for creating normalizers from config objects. + * @static + * @param {Object} config The configuration object for the normalizer. + * @returns {Normalizer} A Normalizer object. + * @throws {Error} If an unknown Normalizer type is specified in the config. + */ + static fromConfig(config) { + if (config === null) return null; + switch (config.type) { + case 'BertNormalizer': + return new BertNormalizer(config); + case 'Precompiled': + return new Precompiled(config); + case 'Sequence': + return new NormalizerSequence(config); + case 'Replace': + return new Replace(config); + case 'NFC': + return new NFC(config); + case 'NFD': + return new NFD(config); + case 'NFKC': + return new NFKC(config); + case 'NFKD': + return new NFKD(config); + case 'Strip': + return new StripNormalizer(config); + case 'StripAccents': + return new StripAccents(config); + case 'Lowercase': + return new Lowercase(config); + case 'Prepend': + return new Prepend(config); + default: + throw new Error(`Unknown Normalizer type: ${config.type}`); + } + } + + /** + * Normalize the input text. + * @abstract + * @param {string} text The text to normalize. + * @returns {string} The normalized text. + * @throws {Error} If this method is not implemented in a subclass. + */ + normalize(text) { + throw Error("normalize should be implemented in subclass.") + } + + /** + * Alias for {@link Normalizer#normalize}. + * @param {string} text The text to normalize. + * @returns {string} The normalized text. + */ + _call(text) { + return this.normalize(text); + } + +} + +/** + * Replace normalizer that replaces occurrences of a pattern with a given string or regular expression. + * @extends Normalizer + */ +class Replace extends Normalizer { + /** + * Normalize the input text by replacing the pattern with the content. + * @param {string} text The input text to be normalized. + * @returns {string} The normalized text after replacing the pattern with the content. + */ + normalize(text) { + const pattern = createPattern(this.config.pattern); + return pattern === null + ? text + : text.replaceAll(pattern, this.config.content); + } +} + +/** + * A normalizer that applies Unicode normalization to the input text. + * @extends Normalizer + * @abstract + */ +class UnicodeNormalizer extends Normalizer { + /** + * @type {string} The Unicode normalization form to apply. + * Should be one of: 'NFC', 'NFD', 'NFKC', or 'NFKD'. + */ + form = undefined; + + /** + * Normalize the input text by applying Unicode normalization. + * @param {string} text The input text to be normalized. + * @returns {string} The normalized text. + */ + normalize(text) { + text = text.normalize(this.form) + return text; + } +} + +/** + * A normalizer that applies Unicode normalization form C (NFC) to the input text. + * Canonical Decomposition, followed by Canonical Composition. + * @extends UnicodeNormalizer + */ +class NFC extends UnicodeNormalizer { + form = 'NFC'; +} + +/** + * A normalizer that applies Unicode normalization form D (NFD) to the input text. + * Canonical Decomposition. + * @extends UnicodeNormalizer + */ +class NFD extends UnicodeNormalizer { + form = 'NFD'; +} + +/** + * A normalizer that applies Unicode normalization form KC (NFKC) to the input text. + * Compatibility Decomposition, followed by Canonical Composition. + * @extends UnicodeNormalizer + */ +class NFKC extends UnicodeNormalizer { + form = 'NFKC'; +} + +/** + * A normalizer that applies Unicode normalization form KD (NFKD) to the input text. + * Compatibility Decomposition. + * @extends UnicodeNormalizer + */ +class NFKD extends UnicodeNormalizer { + form = 'NFKD'; +} + +/** + * A normalizer that strips leading and/or trailing whitespace from the input text. + */ +class StripNormalizer extends Normalizer { + /** + * Strip leading and/or trailing whitespace from the input text. + * @param {string} text The input text. + * @returns {string} The normalized text. + */ + normalize(text) { + if (this.config.strip_left && this.config.strip_right) { + // Fast path to avoid an extra trim call + text = text.trim(); + } else { + if (this.config.strip_left) { + text = text.trimStart(); + } + if (this.config.strip_right) { + text = text.trimEnd(); + } + } + return text; + } +} + +/** + * StripAccents normalizer removes all accents from the text. + * @extends Normalizer + */ +class StripAccents extends Normalizer { + /** + * Remove all accents from the text. + * @param {string} text The input text. + * @returns {string} The normalized text without accents. + */ + normalize(text) { + text = remove_accents(text); + return text; + } +} + +/** + * A Normalizer that lowercases the input string. + * @extends Normalizer + */ +class Lowercase extends Normalizer { + /** + * Lowercases the input string. + * @param {string} text The text to normalize. + * @returns {string} The normalized text. + */ + normalize(text) { + text = text.toLowerCase(); + return text; + } +} + +/** + * A Normalizer that prepends a string to the input string. + * @extends Normalizer + */ +class Prepend extends Normalizer { + /** + * Prepends the input string. + * @param {string} text The text to normalize. + * @returns {string} The normalized text. + */ + normalize(text) { + text = this.config.prepend + text; + return text; + } +} + +/** + * A Normalizer that applies a sequence of Normalizers. + * @extends Normalizer + */ +class NormalizerSequence extends Normalizer { + /** + * Create a new instance of NormalizerSequence. + * @param {Object} config The configuration object. + * @param {Object[]} config.normalizers An array of Normalizer configuration objects. + */ + constructor(config) { + super(config); + this.normalizers = config.normalizers.map(x => Normalizer.fromConfig(x)); + } + /** + * Apply a sequence of Normalizers to the input text. + * @param {string} text The text to normalize. + * @returns {string} The normalized text. + */ + normalize(text) { + return this.normalizers.reduce((t, normalizer) => { + return normalizer.normalize(t); + }, text); + } +} + +/** + * A class representing a normalizer used in BERT tokenization. + * @extends Normalizer + */ +class BertNormalizer extends Normalizer { + /** + * Adds whitespace around any CJK (Chinese, Japanese, or Korean) character in the input text. + * + * @param {string} text The input text to tokenize. + * @returns {string} The tokenized text with whitespace added around CJK characters. + */ + _tokenize_chinese_chars(text) { + /* Adds whitespace around any CJK character. */ + const output = []; + for (let i = 0; i < text.length; ++i) { + const char = text[i]; + const cp = char.charCodeAt(0); + if (is_chinese_char(cp)) { + output.push(" "); + output.push(char); + output.push(" "); + } else { + output.push(char); + } + } + return output.join(""); + } + + /** + * Strips accents from the given text. + * @param {string} text The text to strip accents from. + * @returns {string} The text with accents removed. + */ + stripAccents(text) { + // "Mark, Nonspacing" (Mn) + return text.normalize('NFD').replace(/\p{Mn}/gu, ''); + } + + + /** + * Checks whether `char` is a control character. + * @param {string} char The character to check. + * @returns {boolean} Whether `char` is a control character. + * @private + */ + _is_control(char) { + switch (char) { + case '\t': + case '\n': + case '\r': + // These are technically control characters but we count them as whitespace characters. + return false; + + default: + // Check if unicode category starts with C: + // Cc - Control + // Cf - Format + // Co - Private Use + // Cs - Surrogate + return /^\p{Cc}|\p{Cf}|\p{Co}|\p{Cs}$/u.test(char); + } + } + + /** + * Performs invalid character removal and whitespace cleanup on text. + * @param {string} text The text to clean. + * @returns {string} The cleaned text. + * @private + */ + _clean_text(text) { + const output = []; + for (const char of text) { + const cp = char.charCodeAt(0); + if (cp === 0 || cp === 0xFFFD || this._is_control(char)) { + continue; + } + if (/^\s$/.test(char)) { // is whitespace + output.push(" "); + } else { + output.push(char); + } + } + return output.join(""); + } + /** + * Normalizes the given text based on the configuration. + * @param {string} text The text to normalize. + * @returns {string} The normalized text. + */ + normalize(text) { + if (this.config.clean_text) { + text = this._clean_text(text); + } + + if (this.config.handle_chinese_chars) { + text = this._tokenize_chinese_chars(text); + } + + if (this.config.lowercase) { + text = text.toLowerCase(); + + if (this.config.strip_accents !== false) { + text = this.stripAccents(text); + } + } else if (this.config.strip_accents) { + text = this.stripAccents(text); + } + + return text; + } +} + +/** + * A callable class representing a pre-tokenizer used in tokenization. Subclasses + * should implement the `pre_tokenize_text` method to define the specific pre-tokenization logic. + * @extends Callable + */ +class PreTokenizer extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * Factory method that returns an instance of a subclass of `PreTokenizer` based on the provided configuration. + * + * @static + * @param {Object} config A configuration object for the pre-tokenizer. + * @returns {PreTokenizer} An instance of a subclass of `PreTokenizer`. + * @throws {Error} If the provided configuration object does not correspond to any known pre-tokenizer. + */ + static fromConfig(config) { + if (config === null) return null; + + switch (config.type) { + case 'BertPreTokenizer': + return new BertPreTokenizer(config); + case 'Sequence': + return new PreTokenizerSequence(config); + case 'Whitespace': + return new WhitespacePreTokenizer(config); + case 'WhitespaceSplit': + return new WhitespaceSplit(config); + case 'Metaspace': + return new MetaspacePreTokenizer(config); + + case 'ByteLevel': + return new ByteLevelPreTokenizer(config); + case 'Split': + return new SplitPreTokenizer(config); + case 'Punctuation': + return new PunctuationPreTokenizer(config); + case 'Digits': + return new DigitsPreTokenizer(config); + case 'Replace': + return new ReplacePreTokenizer(config); + default: + throw new Error(`Unknown PreTokenizer type: ${config.type}`); + } + } + + /** + * Method that should be implemented by subclasses to define the specific pre-tokenization logic. + * + * @abstract + * @param {string} text The text to pre-tokenize. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} The pre-tokenized text. + * @throws {Error} If the method is not implemented in the subclass. + */ + pre_tokenize_text(text, options) { + throw Error("pre_tokenize_text should be implemented in subclass.") + } + + /** + * Tokenizes the given text into pre-tokens. + * @param {string|string[]} text The text or array of texts to pre-tokenize. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} An array of pre-tokens. + */ + pre_tokenize(text, options) { + return (Array.isArray(text) + ? text.map(x => this.pre_tokenize_text(x, options)) + : this.pre_tokenize_text(text, options) + ).flat(); + } + + /** + * Alias for {@link PreTokenizer#pre_tokenize}. + * @param {string|string[]} text The text or array of texts to pre-tokenize. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} An array of pre-tokens. + */ + _call(text, options) { + return this.pre_tokenize(text, options); + } +} + +/** + * @extends PreTokenizer + */ +class BertPreTokenizer extends PreTokenizer { + /** + * A PreTokenizer that splits text into wordpieces using a basic tokenization scheme + * similar to that used in the original implementation of BERT. + * + * @param {Object} config The configuration object. + */ + constructor(config) { + super(); + // Construct a pattern which matches the rust implementation: + // https://github.com/huggingface/tokenizers/blob/b4fcc9ce6e4ad5806e82826f816acfdfdc4fcc67/tokenizers/src/pre_tokenizers/bert.rs#L11 + // Equivalent to removing whitespace and splitting on punctuation (both \p{P} and other ascii characters) + this.pattern = new RegExp(`[^\\s${PUNCTUATION_REGEX}]+|[${PUNCTUATION_REGEX}]`, 'gu'); + } + /** + * Tokenizes a single text using the BERT pre-tokenization scheme. + * + * @param {string} text The text to tokenize. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} An array of tokens. + */ + pre_tokenize_text(text, options) { + return text.trim().match(this.pattern) || []; + } +} + +/** + * A pre-tokenizer that splits text into Byte-Pair-Encoding (BPE) subwords. + * @extends PreTokenizer + */ +class ByteLevelPreTokenizer extends PreTokenizer { + /** + * Creates a new instance of the `ByteLevelPreTokenizer` class. + * @param {Object} config The configuration object. + */ + constructor(config) { + super(); + this.config = config; + + /** + * @type {boolean} Whether to add a leading space to the first word. + * This allows to treat the leading word just as any other word. + */ + this.add_prefix_space = this.config.add_prefix_space; + + /** + * @type {boolean} Whether the post processing step should trim offsets + * to avoid including whitespaces. + * @todo Use this in the pretokenization step. + */ + this.trim_offsets = this.config.trim_offsets; + + /** + * @type {boolean} Whether to use the standard GPT2 regex for whitespace splitting. + * Set it to False if you want to use your own splitting. Defaults to true. + */ + this.use_regex = this.config.use_regex ?? true; + this.pattern = /'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+/gu; + + this.byte_encoder = BYTES_TO_UNICODE; + this.text_encoder = new TextEncoder(); + } + + /** + * Tokenizes a single piece of text using byte-level tokenization. + * @param {string} text The text to tokenize. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} An array of tokens. + */ + pre_tokenize_text(text, options) { + // Add a leading space if the option is enabled + if (this.add_prefix_space && !text.startsWith(' ')) { + text = ' ' + text; + } + + // Split on whitespace and punctuation + const tokens = this.use_regex ? (text.match(this.pattern) || []) : [text]; + + // Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case) + return tokens.map( + token => Array.from(this.text_encoder.encode(token), byte => this.byte_encoder[byte]).join('') + ); + } +} + +/** + * @typedef {'removed'|'isolated'|'mergedWithPrevious'|'mergedWithNext'|'contiguous'} SplitDelimiterBehavior + */ + +/** + * Splits text using a given pattern. + * @extends PreTokenizer + */ +class SplitPreTokenizer extends PreTokenizer { + /** + * @param {Object} config The configuration options for the pre-tokenizer. + * @param {Object} config.pattern The pattern used to split the text. Can be a string or a regex object. + * @param {string|undefined} config.pattern.String The string to use for splitting. Only defined if the pattern is a string. + * @param {string|undefined} config.pattern.Regex The regex to use for splitting. Only defined if the pattern is a regex. + * @param {SplitDelimiterBehavior} config.behavior The behavior to use when splitting. + * @param {boolean} config.invert Whether to split (invert=false) or match (invert=true) the pattern. + */ + constructor(config) { + super(); + this.config = config; + // TODO support all behaviours (config.behavior) + + this.pattern = createPattern(this.config.pattern, this.config.invert); + } + + /** + * Tokenizes text by splitting it using the given pattern. + * @param {string} text The text to tokenize. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} An array of tokens. + */ + pre_tokenize_text(text, options) { + if (this.pattern === null) { + return []; + } + + if (this.config.invert) { + return text.match(this.pattern) || []; + } else if (this.config.behavior?.toLowerCase() === 'removed') { + return text.split(this.pattern).filter(x => x); + } else { + return regexSplit(text, this.pattern); + } + } +} + +/** + * Splits text based on punctuation. + * @extends PreTokenizer + */ +class PunctuationPreTokenizer extends PreTokenizer { + /** + * @param {Object} config The configuration options for the pre-tokenizer. + * @param {SplitDelimiterBehavior} config.behavior The behavior to use when splitting. + */ + constructor(config) { + super(); + this.config = config; + this.pattern = new RegExp(`[^${PUNCTUATION_REGEX}]+|[${PUNCTUATION_REGEX}]+`, 'gu'); + } + + /** + * Tokenizes text by splitting it using the given pattern. + * @param {string} text The text to tokenize. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} An array of tokens. + */ + pre_tokenize_text(text, options) { + return text.match(this.pattern) || []; + } +} + + +/** + * Splits text based on digits. + * @extends PreTokenizer + */ +class DigitsPreTokenizer extends PreTokenizer { + /** + * @param {Object} config The configuration options for the pre-tokenizer. + * @param {boolean} config.individual_digits Whether to split on individual digits. + */ + constructor(config) { + super(); + this.config = config; + + // Construct a pattern which matches the rust implementation: + const digit_pattern = `[^\\d]+|\\d${this.config.individual_digits ? '' : '+'}`; + this.pattern = new RegExp(digit_pattern, 'gu'); + } + + /** + * Tokenizes text by splitting it using the given pattern. + * @param {string} text The text to tokenize. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} An array of tokens. + */ + pre_tokenize_text(text, options) { + return text.match(this.pattern) || []; + } +} + +/** + * @typedef {Object} PostProcessedOutput + * @property {string[]} tokens List of token produced by the post-processor. + * @property {number[]} [token_type_ids] List of token type ids produced by the post-processor. + */ + + +/** + * @typedef {Object} EncodingSingle + * @property {number[]} input_ids List of token ids to be fed to a model. + * @property {number[]} attention_mask List of token type ids to be fed to a model + * @property {number[]} [token_type_ids] List of indices specifying which tokens should be attended to by the model + */ + + +/** + * @extends Callable + */ +class PostProcessor extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + + /** + * @param {Object} config The configuration for the post-processor. + */ + constructor(config) { + super(); + this.config = config; + } + + /** + * Factory method to create a PostProcessor object from a configuration object. + * + * @param {Object} config Configuration object representing a PostProcessor. + * @returns {PostProcessor} A PostProcessor object created from the given configuration. + * @throws {Error} If an unknown PostProcessor type is encountered. + */ + static fromConfig(config) { + if (config === null) return null; + switch (config.type) { + case 'TemplateProcessing': + return new TemplateProcessing(config); + + case 'ByteLevel': + return new ByteLevelPostProcessor(config); + + case 'RobertaProcessing': + return new RobertaProcessing(config); + case 'BertProcessing': + return new BertProcessing(config); + + case 'Sequence': + return new PostProcessorSequence(config); + default: + throw new Error(`Unknown PostProcessor type: ${config.type}`); + } + } + + /** + * Method to be implemented in subclass to apply post-processing on the given tokens. + * + * @param {Array} tokens The input tokens to be post-processed. + * @param {...*} args Additional arguments required by the post-processing logic. + * @returns {PostProcessedOutput} The post-processed tokens. + * @throws {Error} If the method is not implemented in subclass. + */ + post_process(tokens, ...args) { + throw Error("post_process should be implemented in subclass.") + } + + /** + * Alias for {@link PostProcessor#post_process}. + * @param {Array} tokens The text or array of texts to post-process. + * @param {...*} args Additional arguments required by the post-processing logic. + * @returns {PostProcessedOutput} The post-processed tokens. + */ + _call(tokens, ...args) { + return this.post_process(tokens, ...args); + } +} + +/** + * A post-processor that adds special tokens to the beginning and end of the input. + */ +class BertProcessing extends PostProcessor { + /** + * @param {Object} config The configuration for the post-processor. + * @param {string[]} config.cls The special tokens to add to the beginning of the input. + * @param {string[]} config.sep The special tokens to add to the end of the input. + */ + constructor(config) { + super(config); + // TODO use all of config: add_prefix_space, trim_offsets + + this.cls = config.cls[0]; + this.sep = config.sep[0]; + } + + /** + * Adds the special tokens to the beginning and end of the input. + * @param {string[]} tokens The input tokens. + * @param {string[]} [tokens_pair=null] An optional second set of input tokens. + * @returns {PostProcessedOutput} The post-processed tokens with the special tokens added to the beginning and end. + */ + post_process(tokens, tokens_pair = null, { + add_special_tokens = true, + } = {}) { + if (add_special_tokens) { + tokens = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)([this.cls], tokens, [this.sep]); + } + + let token_type_ids = new Array(tokens.length).fill(0); + if (tokens_pair !== null) { + // NOTE: It is intended to add 2 EOS tokens after the first set of tokens + // https://github.com/huggingface/tokenizers/issues/983 + const middle = (add_special_tokens && this instanceof RobertaProcessing) + ? [this.sep] + : []; + const after = add_special_tokens ? [this.sep] : []; + + tokens = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(tokens, middle, tokens_pair, after); + token_type_ids = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(token_type_ids, new Array(tokens_pair.length + middle.length + after.length).fill(1)); + } + return { tokens, token_type_ids }; + } +} +class RobertaProcessing extends BertProcessing { } // NOTE: extends BertProcessing + +/** + * Post processor that replaces special tokens in a template with actual tokens. + * @extends PostProcessor + */ +class TemplateProcessing extends PostProcessor { + /** + * Creates a new instance of `TemplateProcessing`. + * @param {Object} config The configuration options for the post processor. + * @param {Array} config.single The template for a single sequence of tokens. + * @param {Array} config.pair The template for a pair of sequences of tokens. + */ + constructor(config) { + super(config); + + this.single = config.single; + this.pair = config.pair; + } + + /** + * Replaces special tokens in the template with actual tokens. + * @param {string[]} tokens The list of tokens for the first sequence. + * @param {string[]} [tokens_pair=null] The list of tokens for the second sequence (optional). + * @returns {PostProcessedOutput} An object containing the list of tokens with the special tokens replaced with actual tokens. + */ + post_process(tokens, tokens_pair = null, { + add_special_tokens = true, + } = {}) { + const type = tokens_pair === null ? this.single : this.pair + + let processedTokens = []; + let types = []; + for (const item of type) { + if ('SpecialToken' in item) { + if (add_special_tokens) { + processedTokens.push(item.SpecialToken.id); + types.push(item.SpecialToken.type_id); + } + } else if ('Sequence' in item) { + if (item.Sequence.id === 'A') { + processedTokens = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(processedTokens, tokens); + types = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(types, new Array(tokens.length).fill(item.Sequence.type_id)); + + } else if (item.Sequence.id === 'B') { + processedTokens = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(processedTokens, tokens_pair); + types = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(types, new Array(tokens_pair.length).fill(item.Sequence.type_id)); + } + } + } + return { tokens: processedTokens, token_type_ids: types }; + } +} + +/** + * A PostProcessor that returns the given tokens as is. + * @extends PostProcessor + */ +class ByteLevelPostProcessor extends PostProcessor { + /** + * Post process the given tokens. + * @param {string[]} tokens The list of tokens for the first sequence. + * @param {string[]} [tokens_pair=null] The list of tokens for the second sequence (optional). + * @returns {PostProcessedOutput} An object containing the post-processed tokens. + */ + post_process(tokens, tokens_pair = null) { + if (tokens_pair) { + tokens = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(tokens, tokens_pair); + } + return { tokens }; + } +} + + +/** + * A post-processor that applies multiple post-processors in sequence. + */ +class PostProcessorSequence extends PostProcessor { + + /** + * Creates a new instance of PostProcessorSequence. + * @param {Object} config The configuration object. + * @param {Object[]} config.processors The list of post-processors to apply. + */ + constructor(config) { + super(config); + + this.processors = config.processors.map(x => PostProcessor.fromConfig(x)); + } + + /** + * Post process the given tokens. + * @param {string[]} tokens The list of tokens for the first sequence. + * @param {string[]} [tokens_pair=null] The list of tokens for the second sequence (optional). + * @returns {PostProcessedOutput} An object containing the post-processed tokens. + */ + post_process(tokens, tokens_pair = null, options = {}) { + let token_type_ids; + for (const processor of this.processors) { + if (processor instanceof ByteLevelPostProcessor) { + // Special case where we need to pass the tokens_pair to the post-processor + const output = processor.post_process(tokens); + tokens = output.tokens; + if (tokens_pair) { + const pair_output = processor.post_process(tokens_pair); + tokens_pair = pair_output.tokens; + } + } else { + const output = processor.post_process(tokens, tokens_pair, options); + tokens = output.tokens; + token_type_ids = output.token_type_ids; + } + } + return { tokens, token_type_ids }; + } +} + +/** + * The base class for token decoders. + * @extends Callable + */ +class Decoder extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + + /** + * Creates an instance of `Decoder`. + * + * @param {Object} config The configuration object. + */ + constructor(config) { + super(); + this.config = config; + + /** @type {AddedToken[]} */ + this.added_tokens = []; + this.end_of_word_suffix = null; + this.trim_offsets = config.trim_offsets; + } + + /** + * Creates a decoder instance based on the provided configuration. + * + * @param {Object} config The configuration object. + * @returns {Decoder} A decoder instance. + * @throws {Error} If an unknown decoder type is provided. + */ + static fromConfig(config) { + if (config === null) return null; + switch (config.type) { + case 'WordPiece': + return new WordPieceDecoder(config); + case 'Metaspace': + return new MetaspaceDecoder(config); + case 'ByteLevel': + return new ByteLevelDecoder(config); + + case 'Replace': + return new ReplaceDecoder(config); + case 'ByteFallback': + return new ByteFallback(config); + case 'Fuse': + return new FuseDecoder(config); + case 'Strip': + return new StripDecoder(config); + + case 'Sequence': + return new DecoderSequence(config); + + case 'CTC': + return new CTCDecoder(config); + case 'BPEDecoder': + return new BPEDecoder(config); + default: + throw new Error(`Unknown Decoder type: ${config.type}`); + } + } + + /** + * Calls the `decode` method. + * + * @param {string[]} tokens The list of tokens. + * @returns {string} The decoded string. + */ + _call(tokens) { + return this.decode(tokens); + } + + /** + * Decodes a list of tokens. + * @param {string[]} tokens The list of tokens. + * @returns {string} The decoded string. + */ + decode(tokens) { + return this.decode_chain(tokens).join(''); + } + + /** + * Apply the decoder to a list of tokens. + * + * @param {string[]} tokens The list of tokens. + * @returns {string[]} The decoded list of tokens. + * @throws {Error} If the `decode_chain` method is not implemented in the subclass. + */ + decode_chain(tokens) { + throw Error("`decode_chain` should be implemented in subclass.") + } + +} + +class ReplaceDecoder extends Decoder { + + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + const pattern = createPattern(this.config.pattern); + return pattern === null + ? tokens + : tokens.map(token => token.replaceAll(pattern, this.config.content)) + } +} + + +class ByteFallback extends Decoder { + constructor(config) { + super(config); + + this.text_decoder = new TextDecoder(); + } + + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + + const new_tokens = []; + let previous_byte_tokens = []; + + for (const token of tokens) { + let bytes = null; + if (token.length === 6 && token.startsWith('<0x') && token.endsWith('>')) { + const byte = parseInt(token.slice(3, 5), 16); + if (!isNaN(byte)) { + bytes = byte; + } + } + if (bytes !== null) { + previous_byte_tokens.push(bytes); + } else { + if (previous_byte_tokens.length > 0) { + const string = this.text_decoder.decode(Uint8Array.from(previous_byte_tokens)); + new_tokens.push(string); + previous_byte_tokens = []; + } + new_tokens.push(token); + } + } + if (previous_byte_tokens.length > 0) { + const string = this.text_decoder.decode(Uint8Array.from(previous_byte_tokens)); + new_tokens.push(string); + previous_byte_tokens = []; + } + + return new_tokens; + } +} + +/** + * Fuse simply fuses all tokens into one big string. + * It's usually the last decoding step anyway, but this decoder + * exists incase some decoders need to happen after that step + */ +class FuseDecoder extends Decoder { + + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + return [tokens.join('')]; + } +} + + +class StripDecoder extends Decoder { + constructor(config) { + super(config); + + this.content = this.config.content; + this.start = this.config.start; + this.stop = this.config.stop; + } + + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + return tokens.map(token => { + let start_cut = 0; + for (let i = 0; i < this.start; ++i) { + if (token[i] === this.content) { + start_cut = i + 1; + continue; + } else { + break; + } + } + + let stop_cut = token.length; + for (let i = 0; i < this.stop; ++i) { + const index = token.length - i - 1; + if (token[index] === this.content) { + stop_cut = index; + continue; + } else { + break; + } + } + + return token.slice(start_cut, stop_cut) + }); + } +} + +/** + * A decoder that decodes a list of WordPiece tokens into a single string. + * @extends Decoder + */ +class WordPieceDecoder extends Decoder { + + /** + * Creates a new instance of WordPieceDecoder. + * @param {Object} config The configuration object. + * @param {string} config.prefix The prefix used for WordPiece encoding. + * @param {boolean} config.cleanup Whether to cleanup the decoded string. + */ + constructor(config) { + super(config); + this.cleanup = config.cleanup; + } + + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + return tokens.map((token, i) => { + if (i !== 0) { + if (token.startsWith(this.config.prefix)) { + // NOTE: .replace() is intended; only replace first occurrence + token = token.replace(this.config.prefix, ''); + } else { + token = ' ' + token; + } + } + if (this.cleanup) { + token = clean_up_tokenization(token) + } + + return token; + }); + } +} + +/** + * Byte-level decoder for tokenization output. Inherits from the `Decoder` class. + * @extends Decoder + */ +class ByteLevelDecoder extends Decoder { + + /** + * Create a `ByteLevelDecoder` object. + * @param {Object} config Configuration object. + */ + constructor(config) { + super(config); + + this.byte_decoder = UNICODE_TO_BYTES; + this.text_decoder = new TextDecoder("utf-8", { + fatal: false, + ignoreBOM: true, + }); + + this.end_of_word_suffix = null; + } + + /** + * Convert an array of tokens to string by decoding each byte. + * @param {string[]} tokens Array of tokens to be decoded. + * @returns {string} The decoded string. + */ + convert_tokens_to_string(tokens) { + const text = tokens.join(''); + const byteArray = new Uint8Array([...text].map(c => this.byte_decoder[c])); + const decoded_text = this.text_decoder.decode(byteArray); + return decoded_text; + } + + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + // TODO move to base class (like HF) + // tokens === filtered_tokens + + // To avoid mixing byte-level and unicode for byte-level BPT + // we need to build string separately for added tokens and byte-level tokens + // cf. https://github.com/huggingface/transformers/issues/1133 + const sub_texts = []; + let current_sub_text = []; + for (const token of tokens) { + // tokens sent here are already filtered, so we don't need to do this + // if (skip_special_tokens && this.all_special_ids.includes(token)) { + // continue; + // } + + if (this.added_tokens.find(x => x.content === token) !== undefined) { + if (current_sub_text.length > 0) { + sub_texts.push(this.convert_tokens_to_string(current_sub_text)); + current_sub_text = []; + } + sub_texts.push(token); + } else { + current_sub_text.push(token); + } + } + if (current_sub_text.length > 0) { + sub_texts.push(this.convert_tokens_to_string(current_sub_text)); + } + + // TODO add spaces_between_special_tokens and clean_up_tokenization_spaces options + + return sub_texts; + } +} + +/** + * The CTC (Connectionist Temporal Classification) decoder. + * See https://github.com/huggingface/tokenizers/blob/bb38f390a61883fc2f29d659af696f428d1cda6b/tokenizers/src/decoders/ctc.rs + */ +class CTCDecoder extends Decoder { + + constructor(config) { + super(config); + + this.pad_token = this.config.pad_token; + this.word_delimiter_token = this.config.word_delimiter_token; + this.cleanup = this.config.cleanup; + } + /** + * Converts a connectionist-temporal-classification (CTC) output tokens into a single string. + * @param {string[]} tokens Array of tokens to be decoded. + * @returns {string} The decoded string. + */ + convert_tokens_to_string(tokens) { + if (tokens.length === 0) return ''; + + // group same tokens into non-repeating tokens in CTC style decoding + const grouped_tokens = [tokens[0]]; + for (let i = 1; i < tokens.length; ++i) { + if (tokens[i] !== grouped_tokens.at(-1)) { + grouped_tokens.push(tokens[i]); + } + } + + // filter self.pad_token which is used as CTC-blank token + const filtered_tokens = grouped_tokens.filter(token => token !== this.pad_token); + + let text = filtered_tokens.join(''); + if (this.cleanup) { + // cleanup and replace delimiter token + text = clean_up_tokenization(text) + .replaceAll(this.word_delimiter_token, ' ') + .trim(); + } + return text; + } + + + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + return [this.convert_tokens_to_string(tokens)]; + } +} + +/** + * Apply a sequence of decoders. + * @extends Decoder + */ +class DecoderSequence extends Decoder { + + /** + * Creates a new instance of DecoderSequence. + * @param {Object} config The configuration object. + * @param {Object[]} config.decoders The list of decoders to apply. + */ + constructor(config) { + super(config); + this.decoders = config.decoders.map(x => Decoder.fromConfig(x)); + } + + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + // Use reduce to apply each decoder to the tokens + return this.decoders.reduce((toks, decoder) => { + return decoder.decode_chain(toks); + }, tokens); + } + +} + +class BPEDecoder extends Decoder { + constructor(config) { + super(config); + + this.suffix = this.config.suffix; + } + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + return tokens.map((token, i) => { + return token.replaceAll(this.suffix, (i === tokens.length - 1) ? '' : ' ') + }); + } +} + +// Custom decoder for VITS +class VitsDecoder extends Decoder { + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + let decoded = ''; + for (let i = 1; i < tokens.length; i += 2) { + decoded += tokens[i]; + } + return [decoded]; + } +} + + +/** + * This PreTokenizer replaces spaces with the given replacement character, adds a prefix space if requested, + * and returns a list of tokens. + * @extends PreTokenizer + */ +class MetaspacePreTokenizer extends PreTokenizer { + /** + * @param {Object} config The configuration object for the MetaspacePreTokenizer. + * @param {boolean} config.add_prefix_space Whether to add a prefix space to the first token. + * @param {string} config.replacement The character to replace spaces with. + * @param {string} [config.str_rep=config.replacement] An optional string representation of the replacement character. + * @param {'first'|'never'|'always'} [config.prepend_scheme='always'] The metaspace prepending scheme. + */ + constructor(config) { + super(); + + this.addPrefixSpace = config.add_prefix_space; + this.replacement = config.replacement; + this.strRep = config.str_rep || this.replacement; + this.prepend_scheme = config.prepend_scheme ?? 'always'; + } + + /** + * This method takes a string, replaces spaces with the replacement character, + * adds a prefix space if requested, and returns a new list of tokens. + * @param {string} text The text to pre-tokenize. + * @param {Object} [options] The options for the pre-tokenization. + * @param {number} [options.section_index] The index of the section to pre-tokenize. + * @returns {string[]} A new list of pre-tokenized tokens. + */ + pre_tokenize_text(text, { + section_index = undefined, + } = {}) { + + let normalized = text.replaceAll(' ', this.strRep); + + if ( + // We add a prefix space if: + // (1) The addPrefixSpace option is enabled and the normalized + // token does not already start with the replacement character. + (this.addPrefixSpace && !normalized.startsWith(this.replacement)) + + // and (2) either: + // (a) prepend_scheme is 'always' + // (b) prepend_scheme is 'first' and this is the first section + && ( + this.prepend_scheme === 'always' || + (this.prepend_scheme === 'first' && section_index === 0) + ) + ) { + normalized = this.strRep + normalized; + } + return [normalized]; + } +} + +/** + * MetaspaceDecoder class extends the Decoder class and decodes Metaspace tokenization. + * @extends Decoder + */ +class MetaspaceDecoder extends Decoder { + /** + * Constructs a new MetaspaceDecoder object. + * @param {Object} config The configuration object for the MetaspaceDecoder. + * @param {boolean} config.add_prefix_space Whether to add a prefix space to the decoded string. + * @param {string} config.replacement The string to replace spaces with. + */ + constructor(config) { + super(config); + + this.addPrefixSpace = config.add_prefix_space; + this.replacement = config.replacement; + } + + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + const result = []; + for (let i = 0; i < tokens.length; ++i) { + let normalized = tokens[i].replaceAll(this.replacement, ' '); + if (this.addPrefixSpace && i == 0 && normalized.startsWith(' ')) { + normalized = normalized.substring(1); + } + result.push(normalized); + } + return result; + } +} + +/** + * A normalizer that applies a precompiled charsmap. + * This is useful for applying complex normalizations in C++ and exposing them to JavaScript. + * @extends Normalizer + * @param {Object} config The configuration object for the Precompiled normalizer. + * @param {Object} config.precompiled_charsmap The precompiled charsmap object. + */ +class Precompiled extends Normalizer { + /** + * Create a new instance of Precompiled normalizer. + * @param {Object} config The configuration object. + * @param {any} config.precompiled_charsmap Precompiled chars mapping. + */ + constructor(config) { + super(config); + this.charsmap = config.precompiled_charsmap; + } + + /** + * Normalizes the given text by applying the precompiled charsmap. + * @param {string} text The text to normalize. + * @returns {string} The normalized text. + */ + normalize(text) { + // As stated in the sentencepiece normalization docs (https://github.com/google/sentencepiece/blob/master/doc/normalization.md#use-pre-defined-normalization-rule), + // there are 5 pre-defined normalization rules: + // 1. nmt_nfkc: NFKC normalization with some additional normalization around spaces. (default) + // 2. nfkc: original NFKC normalization. + // 3. nmt_nfkc_cf: nmt_nfkc + Unicode case folding (mostly lower casing) + // 4. nfkc_cf: nfkc + Unicode case folding. + // 5. identity: no normalization + // + // For now, we only implement the default (nmt_nfkc). + // See https://raw.githubusercontent.com/google/sentencepiece/master/data/nmt_nfkc.tsv for the full list of rules. + // TODO: detect when a different `this.charsmap` is used. + + text = text.replace(/[\u0001-\u0008\u000B\u000E-\u001F\u007F\u008F\u009F]/gm, ''); // Remove control characters + text = text.replace(/[\u0009\u000A\u000C\u000D\u00A0\u1680\u2000-\u200F\u2028\u2029\u202F\u205F\u2581\u3000\uFEFF\uFFFD]/gm, '\u0020'); // Replace certain characters with a space + + if (text.includes('\uFF5E')) { + // To match the sentencepiece implementation 100%, we must handle a very strange edge-case. + // For some reason, the "Fullwidth Tilde" character (\uFF5E) should not be converted to the standard Tilde character (\u007E). + // However, NFKC normalization does do this conversion. As a result, we split the string on the Fullwidth Tilde character, + // perform NFKC normalization on each substring, and then join them back together with the Fullwidth Tilde character. + const parts = text.split('\uFF5E'); + text = parts.map(part => part.normalize('NFKC')).join('\uFF5E'); + } else { + text = text.normalize('NFKC'); + } + + return text; + } +} + +/** + * A pre-tokenizer that applies a sequence of pre-tokenizers to the input text. + * @extends PreTokenizer + */ +class PreTokenizerSequence extends PreTokenizer { + /** + * Creates an instance of PreTokenizerSequence. + * @param {Object} config The configuration object for the pre-tokenizer sequence. + * @param {Object[]} config.pretokenizers An array of pre-tokenizer configurations. + */ + constructor(config) { + super(); + this.tokenizers = config.pretokenizers.map(x => PreTokenizer.fromConfig(x)); + } + + /** + * Applies each pre-tokenizer in the sequence to the input text in turn. + * @param {string} text The text to pre-tokenize. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} The pre-tokenized text. + */ + pre_tokenize_text(text, options) { + // Use reduce to apply each tokenizer to the text + return this.tokenizers.reduce((preTokenizedText, tokenizer) => { + return tokenizer.pre_tokenize(preTokenizedText, options); + }, [text]); + } +} + +/** + * Splits on word boundaries (using the following regular expression: `\w+|[^\w\s]+`). + */ +class WhitespacePreTokenizer extends PreTokenizer { + /** + * Creates an instance of WhitespacePreTokenizer. + * @param {Object} config The configuration object for the pre-tokenizer. + */ + constructor(config) { + super(); + } + /** + * Pre-tokenizes the input text by splitting it on word boundaries. + * @param {string} text The text to be pre-tokenized. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} An array of tokens produced by splitting the input text on whitespace. + */ + pre_tokenize_text(text, options) { + return text.match(/\w+|[^\w\s]+/g) || []; + } +} + +/** + * Splits a string of text by whitespace characters into individual tokens. + * @extends PreTokenizer + */ +class WhitespaceSplit extends PreTokenizer { + /** + * Creates an instance of WhitespaceSplit. + * @param {Object} config The configuration object for the pre-tokenizer. + */ + constructor(config) { + super(); + } + /** + * Pre-tokenizes the input text by splitting it on whitespace characters. + * @param {string} text The text to be pre-tokenized. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} An array of tokens produced by splitting the input text on whitespace. + */ + pre_tokenize_text(text, options) { + return whitespace_split(text); + } +} + +// NOTE: `ReplacePreTokenizer` is custom (to support `BlenderbotSmallTokenizer`) +class ReplacePreTokenizer extends PreTokenizer { + /** + * @param {Object} config The configuration options for the pre-tokenizer. + * @param {Object} config.pattern The pattern used to split the text. Can be a string or a regex object. + * @param {string} config.content What to replace the pattern with. + */ + constructor(config) { + super(); + this.config = config; + this.pattern = createPattern(this.config.pattern); + this.content = this.config.content; + } + + /** + * Pre-tokenizes the input text by replacing certain characters. + * @param {string} text The text to be pre-tokenized. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} An array of tokens produced by replacing certain characters. + */ + pre_tokenize_text(text, options) { + if (this.pattern === null) { + return [text]; + } + return [text.replaceAll(this.pattern, this.config.content)]; + } +} + +const SPECIAL_TOKEN_ATTRIBUTES = [ + 'bos_token', + 'eos_token', + 'unk_token', + 'sep_token', + 'pad_token', + 'cls_token', + 'mask_token', + // additional_special_tokens (TODO) +] + +/** + * + * Helper function for padding values of an object, which are each arrays. + * NOTE: No additional checks are made here for validity of arguments. + * @param {Record} item The input object. + * @param {number} length The length to pad to. + * @param {(key: string) => any} value_fn Determine the value to fill the array, based on its key. + * @param {string} side Which side to pad the array. + * @private + */ +function padHelper(item, length, value_fn, side) { + for (const key of Object.keys(item)) { + const diff = length - item[key].length; + const value = value_fn(key); + + const padData = new Array(diff).fill(value); + item[key] = side === 'right' + ? (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(item[key], padData) + : (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(padData, item[key]); + } +} + +/** + * Helper function for truncating values of an object, which are each arrays. + * NOTE: No additional checks are made here for validity of arguments. + * @param {Record} item The input object. + * @param {number} length The length to truncate to. + * @private + */ +function truncateHelper(item, length) { + // Setting .length to a lower value truncates the array in-place: + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length + for (const key of Object.keys(item)) { + item[key].length = length; + } +} + + +/** + * @typedef {Object} Message + * @property {string} role The role of the message (e.g., "user" or "assistant" or "system"). + * @property {string} content The content of the message. + */ + +class PreTrainedTokenizer extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + return_token_type_ids = false; + + padding_side = 'right'; + /** + * Create a new PreTrainedTokenizer instance. + * @param {Object} tokenizerJSON The JSON of the tokenizer. + * @param {Object} tokenizerConfig The config of the tokenizer. + */ + constructor(tokenizerJSON, tokenizerConfig) { + super(); + + this._tokenizer_config = tokenizerConfig; + + // Construct parts of the tokenizer from the JSON + this.normalizer = Normalizer.fromConfig(tokenizerJSON.normalizer); + this.pre_tokenizer = PreTokenizer.fromConfig(tokenizerJSON.pre_tokenizer); + this.model = TokenizerModel.fromConfig(tokenizerJSON.model, tokenizerConfig); + this.post_processor = PostProcessor.fromConfig(tokenizerJSON.post_processor); + this.decoder = Decoder.fromConfig(tokenizerJSON.decoder); + + // Add added_tokens to model + this.special_tokens = []; + this.all_special_ids = []; + + /** @type {AddedToken[]} */ + this.added_tokens = []; + for (const addedToken of tokenizerJSON.added_tokens) { + const token = new AddedToken(addedToken); + this.added_tokens.push(token); + + this.model.tokens_to_ids.set(token.content, token.id); + this.model.vocab[token.id] = token.content; + + if (token.special) { + this.special_tokens.push(token.content); + this.all_special_ids.push(token.id); + } + } + + // Update additional_special_tokens + this.additional_special_tokens = tokenizerConfig.additional_special_tokens ?? []; + this.special_tokens.push(...this.additional_special_tokens); + this.special_tokens = [...new Set(this.special_tokens)]; // Remove duplicates + + if (this.decoder) { + // Slight hack, but it prevents code duplication: + this.decoder.added_tokens = this.added_tokens; + + // Another slight hack to add `end_of_word_suffix` (if present) to the decoder + // This is needed for cases where BPE model and ByteLevel decoder are used + // For more information, see https://github.com/huggingface/transformers.js/issues/74 + // TODO: save this to the decoder when exporting? + this.decoder.end_of_word_suffix = this.model.end_of_word_suffix; + } + + this.added_tokens_splitter = new _utils_data_structures_js__WEBPACK_IMPORTED_MODULE_5__.DictionarySplitter( + this.added_tokens.map(x => x.content), + ); + + /** @type {Map} */ + this.added_tokens_map = new Map(this.added_tokens.map(x => [x.content, x])) + + // Set mask token if present (otherwise will be undefined, which is fine) + this.mask_token = this.getToken('mask_token'); + this.mask_token_id = this.model.tokens_to_ids.get(this.mask_token); + + this.pad_token = this.getToken('pad_token', 'eos_token'); + this.pad_token_id = this.model.tokens_to_ids.get(this.pad_token); + + this.sep_token = this.getToken('sep_token'); + this.sep_token_id = this.model.tokens_to_ids.get(this.sep_token); + + this.unk_token = this.getToken('unk_token'); + this.unk_token_id = this.model.tokens_to_ids.get(this.unk_token); + + this.bos_token = this.getToken('bos_token'); + this.bos_token_id = this.model.tokens_to_ids.get(this.bos_token); + + this.eos_token = this.getToken('eos_token'); + this.eos_token_id = this.model.tokens_to_ids.get(this.eos_token); + + this.model_max_length = tokenizerConfig.model_max_length; + + /** @type {boolean} Whether or not to strip the text when tokenizing (removing excess spaces before and after the string). */ + this.remove_space = tokenizerConfig.remove_space; + + this.clean_up_tokenization_spaces = tokenizerConfig.clean_up_tokenization_spaces ?? true; + this.do_lowercase_and_remove_accent = tokenizerConfig.do_lowercase_and_remove_accent ?? false; + + if (tokenizerConfig.padding_side) { + this.padding_side = tokenizerConfig.padding_side; + } + + this.legacy = false; + + this.chat_template = tokenizerConfig.chat_template ?? null; + if (Array.isArray(this.chat_template)) { + // Chat templates are stored as lists of dicts with fixed key names, + // we reconstruct that into a single dict while loading them. + const chat_template = Object.create(null); + for (const { name, template } of this.chat_template) { + if (typeof name !== 'string' || typeof template !== 'string') { + throw new Error('Chat template must be a list of objects with "name" and "template" properties'); + } + chat_template[name] = template; + } + this.chat_template = chat_template; + } + this._compiled_template_cache = new Map(); + } + + /** + * Returns the value of the first matching key in the tokenizer config object. + * @param {...string} keys One or more keys to search for in the tokenizer config object. + * @returns {string|null} The value associated with the first matching key, or null if no match is found. + * @throws {Error} If an object is found for a matching key and its __type property is not "AddedToken". + * @private + */ + getToken(...keys) { + for (const key of keys) { + const item = this._tokenizer_config[key]; + + if (!item) continue; + + if (typeof item === 'object') { + if (item.__type === 'AddedToken') { + return item.content; + } else { + throw Error(`Unknown token: ${item}`); + } + } else { + return item; + } + } + return null; + } + + /** + * Loads a pre-trained tokenizer from the given `pretrained_model_name_or_path`. + * + * @param {string} pretrained_model_name_or_path The path to the pre-trained tokenizer. + * @param {PretrainedTokenizerOptions} options Additional options for loading the tokenizer. + * + * @throws {Error} Throws an error if the tokenizer.json or tokenizer_config.json files are not found in the `pretrained_model_name_or_path`. + * @returns {Promise} A new instance of the `PreTrainedTokenizer` class. + */ + static async from_pretrained(pretrained_model_name_or_path, { + progress_callback = null, + config = null, + cache_dir = null, + local_files_only = false, + revision = 'main', + legacy = null, + } = {}) { + + const info = await loadTokenizer(pretrained_model_name_or_path, { + progress_callback, + config, + cache_dir, + local_files_only, + revision, + legacy, + }) + + // @ts-ignore + return new this(...info); + } + + /** + * @typedef {number[]|number[][]|Tensor} BatchEncodingItem + * + * @typedef {Object} BatchEncoding Holds the output of the tokenizer's call function. + * @property {BatchEncodingItem} input_ids List of token ids to be fed to a model. + * @property {BatchEncodingItem} attention_mask List of indices specifying which tokens should be attended to by the model. + * @property {BatchEncodingItem} [token_type_ids] List of token type ids to be fed to a model. + */ + + /** + * Encode/tokenize the given text(s). + * @param {string|string[]} text The text to tokenize. + * @param {Object} options An optional object containing the following properties: + * @param {string|string[]} [options.text_pair=null] Optional second sequence to be encoded. If set, must be the same type as text. + * @param {boolean|'max_length'} [options.padding=false] Whether to pad the input sequences. + * @param {boolean} [options.add_special_tokens=true] Whether or not to add the special tokens associated with the corresponding model. + * @param {boolean} [options.truncation=null] Whether to truncate the input sequences. + * @param {number} [options.max_length=null] Maximum length of the returned list and optionally padding length. + * @param {boolean} [options.return_tensor=true] Whether to return the results as Tensors or arrays. + * @param {boolean} [options.return_token_type_ids=null] Whether to return the token type ids. + * @returns {BatchEncoding} Object to be passed to the model. + */ + _call( + // Required positional arguments + text, + + // Optional keyword arguments + { + text_pair = null, + add_special_tokens = true, + padding = false, + truncation = null, + max_length = null, + return_tensor = true, // Different to HF + return_token_type_ids = null, + } = {}, + ) { + + const isBatched = Array.isArray(text); + + /** @type {EncodingSingle[]} */ + let encodedTokens; + + if (isBatched) { + if (text.length === 0) { + throw Error('text array must be non-empty') + } + + if (text_pair !== null) { + if (!Array.isArray(text_pair)) { + throw Error('text_pair must also be an array') + + } else if (text.length !== text_pair.length) { + throw Error('text and text_pair must have the same length') + } + + encodedTokens = text.map( + (t, i) => this._encode_plus(t, { text_pair: text_pair[i], add_special_tokens, return_token_type_ids }) + ) + + } else { + encodedTokens = text.map(x => this._encode_plus(x, { add_special_tokens, return_token_type_ids })); + } + + } else { + if (text === null || text === undefined) { + throw Error('text may not be null or undefined') + } + + if (Array.isArray(text_pair)) { + throw Error('When specifying `text_pair`, since `text` is a string, `text_pair` must also be a string (i.e., not an array).') + } + + // For single input, we just wrap in an array, and then unwrap later. + encodedTokens = [this._encode_plus(text, { text_pair, add_special_tokens, return_token_type_ids })]; + } + // At this point, `encodedTokens` is batched, of shape [batch_size, tokens]. + // However, array may be jagged. So, we may need pad to max_length. + if (max_length === null) { + max_length = this.model_max_length; + } else if (truncation === null) { + if (padding === true) { + console.warn( + "`max_length` is ignored when `padding: true` and there is no truncation strategy. " + + "To pad to max length, use `padding: 'max_length'`." + ) + max_length = this.model_max_length; + } else if (padding === false) { + console.warn("Truncation was not explicitly activated but `max_length` is provided a specific value, please use `truncation: true` to explicitly truncate examples to max length."); + truncation = true; + } + } + + // padding: 'max_length' doesn't require any additional calculation + // but padding: true has to calculate max_length from the sequences + if (padding === true) { + max_length = Math.min((0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.max)(encodedTokens.map(x => x.input_ids.length))[0], max_length ?? Infinity); + } + + // Ensure it is less than model max length + max_length = Math.min(max_length, this.model_max_length ?? Infinity); + + if (padding || truncation) { + + // Perform padding and/or truncation + for (let i = 0; i < encodedTokens.length; ++i) { + if (encodedTokens[i].input_ids.length === max_length) { + continue; + + } else if (encodedTokens[i].input_ids.length > max_length) { + // possibly truncate + if (truncation) { + truncateHelper(encodedTokens[i], max_length); + } + + } else { // t.length < max_length + // possibly pad + if (padding) { + padHelper( + encodedTokens[i], + max_length, + key => key === 'input_ids' ? this.pad_token_id : 0, + this.padding_side + ); + } + } + } + } + + const result = {}; + + if (return_tensor) { + if (!(padding && truncation)) { + // Not, guaranteed that all items have same length, so + // we perform additional check + + if ( + encodedTokens.some(x => { + for (const key of Object.keys(x)) { + if (x[key].length !== encodedTokens[0][key]?.length) { + return true; + } + } + return false; + }) + ) { + throw Error( + "Unable to create tensor, you should probably activate truncation and/or padding " + + "with 'padding=true' and 'truncation=true' to have batched tensors with the same length." + ) + } + } + + // Now we actually convert to tensor + // NOTE: In the same way as the python library, we return a batched tensor, regardless of + // whether we have a single input or multiple inputs. + const dims = [encodedTokens.length, encodedTokens[0].input_ids.length]; + + for (const key of Object.keys(encodedTokens[0])) { + result[key] = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor('int64', + BigInt64Array.from(encodedTokens.flatMap(x => x[key]).map(BigInt)), + dims + ); + } + + } else { + for (const key of Object.keys(encodedTokens[0])) { + result[key] = encodedTokens.map(x => x[key]); + } + + // If not returning a tensor, we match the input type + if (!isBatched) { + // Input was not batched, so we unwrap + for (const key of Object.keys(result)) { + result[key] = result[key][0]; + } + } + } + + return /** @type {BatchEncoding} */(result); + } + + /** + * Encodes a single text using the preprocessor pipeline of the tokenizer. + * + * @param {string|null} text The text to encode. + * @returns {string[]|null} The encoded tokens. + */ + _encode_text(text) { + if (text === null) return null; + + // Actual function which does encoding, for a single text + // First, we take care of special tokens. Needed to avoid issues arising from + // normalization and/or pretokenization (which may not preserve special tokens) + const sections = this.added_tokens_splitter.split(text); + + // Process left/right stripping of added tokens + for (let i = 0; i < sections.length; ++i) { + const addedToken = this.added_tokens_map.get(sections[i]); + if (addedToken) { + if (addedToken.lstrip && i > 0) { + sections[i - 1] = sections[i - 1].trimEnd(); + } + if (addedToken.rstrip && i < sections.length - 1) { + sections[i + 1] = sections[i + 1].trimStart(); + } + } + } + + const tokens = sections.flatMap((x, section_index) => { + if (x.length === 0) return []; + if (this.added_tokens_map.has(x)) return [x]; // Return added tokens unchanged + + if (this.remove_space === true) { + x = x.trim().split(/\s+/).join(' '); + } + if (this.do_lowercase_and_remove_accent) { + x = lowercase_and_remove_accent(x); + } + + if (this.normalizer !== null) { + x = this.normalizer(x); + } + + // If, after normalization, this section is empty (e.g., trimming whitespace), + // we return an empty array + if (x.length === 0) { + return []; + } + + const sectionTokens = (this.pre_tokenizer !== null) ? this.pre_tokenizer(x, { + section_index, + }) : [x]; + + const tokens = this.model(sectionTokens); + + return tokens; + }); + + return tokens; + } + + /** + * Encodes a single text or a pair of texts using the model's tokenizer. + * + * @param {string} text The text to encode. + * @param {Object} options An optional object containing the following properties: + * @param {string} [options.text_pair=null] The optional second text to encode. + * @param {boolean} [options.add_special_tokens=true] Whether or not to add the special tokens associated with the corresponding model. + * @param {boolean} [options.return_token_type_ids=null] Whether to return token_type_ids. + * @returns {EncodingSingle} An object containing the encoded text. + * @private + */ + _encode_plus(text, { + text_pair = null, + add_special_tokens = true, + return_token_type_ids = null, + } = {}) { + + const { tokens, token_type_ids } = this._tokenize_helper(text, { pair: text_pair, add_special_tokens }); + + const input_ids = this.model.convert_tokens_to_ids(tokens); + + const result = { + input_ids, + attention_mask: new Array(input_ids.length).fill(1), + } + if ((return_token_type_ids ?? this.return_token_type_ids) && token_type_ids) { + result.token_type_ids = token_type_ids; + } + return result; + } + + /** + * Internal helper function to tokenize a text, and optionally a pair of texts. + * @param {string} text The text to tokenize. + * @param {Object} options An optional object containing the following properties: + * @param {string} [options.pair=null] The optional second text to tokenize. + * @param {boolean} [options.add_special_tokens=false] Whether or not to add the special tokens associated with the corresponding model. + * @returns {{tokens: string[], token_type_ids?: number[]}} An object containing the tokens and optionally the token type IDs. + */ + _tokenize_helper(text, { + pair = null, + add_special_tokens = false, + } = {}) { + const tokens = this._encode_text(text); + const tokens2 = this._encode_text(pair); + + return this.post_processor + ? this.post_processor(tokens, tokens2, { add_special_tokens }) + : { tokens: (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(tokens ?? [], tokens2 ?? []) }; + } + + /** + * Converts a string into a sequence of tokens. + * @param {string} text The sequence to be encoded. + * @param {Object} options An optional object containing the following properties: + * @param {string} [options.pair] A second sequence to be encoded with the first. + * @param {boolean} [options.add_special_tokens=false] Whether or not to add the special tokens associated with the corresponding model. + * @returns {string[]} The list of tokens. + */ + tokenize(text, { + pair = null, + add_special_tokens = false, + } = {}) { + return this._tokenize_helper(text, { pair, add_special_tokens }).tokens; + } + + /** + * Encodes a single text or a pair of texts using the model's tokenizer. + * + * @param {string} text The text to encode. + * @param {Object} options An optional object containing the following properties: + * @param {string} [options.text_pair=null] The optional second text to encode. + * @param {boolean} [options.add_special_tokens=true] Whether or not to add the special tokens associated with the corresponding model. + * @param {boolean} [options.return_token_type_ids=null] Whether to return token_type_ids. + * @returns {number[]} An array of token IDs representing the encoded text(s). + */ + encode(text, { + text_pair = null, + add_special_tokens = true, + return_token_type_ids = null, + } = {}) { + return this._encode_plus(text, { + text_pair, + add_special_tokens, + return_token_type_ids, + }).input_ids; + } + + /** + * Decode a batch of tokenized sequences. + * @param {number[][]|Tensor} batch List/Tensor of tokenized input sequences. + * @param {Object} decode_args (Optional) Object with decoding arguments. + * @returns {string[]} List of decoded sequences. + */ + batch_decode(batch, decode_args = {}) { + if (batch instanceof _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor) { + batch = batch.tolist(); + } + return batch.map(x => this.decode(x, decode_args)); + } + + /** + * Decodes a sequence of token IDs back to a string. + * + * @param {number[]|bigint[]|Tensor} token_ids List/Tensor of token IDs to decode. + * @param {Object} [decode_args={}] + * @param {boolean} [decode_args.skip_special_tokens=false] If true, special tokens are removed from the output string. + * @param {boolean} [decode_args.clean_up_tokenization_spaces=true] If true, spaces before punctuations and abbreviated forms are removed. + * + * @returns {string} The decoded string. + * @throws {Error} If `token_ids` is not a non-empty array of integers. + */ + decode( + token_ids, + decode_args = {}, + ) { + if (token_ids instanceof _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor) { + token_ids = prepareTensorForDecode(token_ids); + } + + if (!Array.isArray(token_ids) || token_ids.length === 0 || !(0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.isIntegralNumber)(token_ids[0])) { + throw Error("token_ids must be a non-empty array of integers."); + } + + return this.decode_single(token_ids, decode_args) + } + + /** + * Decode a single list of token ids to a string. + * @param {number[]|bigint[]} token_ids List of token ids to decode + * @param {Object} decode_args Optional arguments for decoding + * @param {boolean} [decode_args.skip_special_tokens=false] Whether to skip special tokens during decoding + * @param {boolean} [decode_args.clean_up_tokenization_spaces=null] Whether to clean up tokenization spaces during decoding. + * If null, the value is set to `this.decoder.cleanup` if it exists, falling back to `this.clean_up_tokenization_spaces` if it exists, falling back to `true`. + * @returns {string} The decoded string + */ + decode_single( + token_ids, + { + skip_special_tokens = false, + clean_up_tokenization_spaces = null, + } + ) { + let tokens = this.model.convert_ids_to_tokens(token_ids); + if (skip_special_tokens) { + tokens = tokens.filter(x => !this.special_tokens.includes(x)); + } + + // If `this.decoder` is null, we just join tokens with a space: + // https://github.com/huggingface/tokenizers/blob/8edec536a737cb04494b454805be16c020abb14f/tokenizers/src/tokenizer/mod.rs#L835 + /** @type {string} */ + let decoded = this.decoder ? this.decoder(tokens) : tokens.join(' '); + + // Slight hack, but prevents having to pass `skip_special_tokens` to + // each call to `decode`, which would lead to code duplication. + if (this.decoder && this.decoder.end_of_word_suffix) { + decoded = decoded.replaceAll(this.decoder.end_of_word_suffix, ' '); + if (skip_special_tokens) { + decoded = decoded.trim(); + } + } + + if (clean_up_tokenization_spaces ?? this.clean_up_tokenization_spaces) { + decoded = clean_up_tokenization(decoded); + } + + return decoded; + } + + /** + * Retrieve the chat template string used for tokenizing chat messages. This template is used + * internally by the `apply_chat_template` method and can also be used externally to retrieve the model's chat + * template for better generation tracking. + * + * @param {Object} options An optional object containing the following properties: + * @param {string} [options.chat_template=null] + * A Jinja template or the name of a template to use for this conversion. + * It is usually not necessary to pass anything to this argument, + * as the model's template will be used by default. + * @param {Object[]} [options.tools=null] + * A list of tools (callable functions) that will be accessible to the model. If the template does not + * support function calling, this argument will have no effect. Each tool should be passed as a JSON Schema, + * giving the name, description and argument types for the tool. See our + * [chat templating guide](https://huggingface.co/docs/transformers/main/en/chat_templating#automated-function-conversion-for-tool-use) + * for more information. + * @returns {string} The chat template string. + */ + get_chat_template({ + chat_template = null, + tools = null, + } = {}) { + + // First, handle the cases when the model has a dict of multiple templates + if (this.chat_template && typeof this.chat_template === 'object') { + const template_dict = this.chat_template; + + if (chat_template !== null && Object.hasOwn(template_dict, chat_template)) { + // The user can pass the name of a template to the chat template argument instead of an entire template + chat_template = template_dict[chat_template]; + } else if (chat_template === null) { + if (tools !== null && 'tool_use' in template_dict) { + chat_template = template_dict['tool_use']; + } else if ('default' in template_dict) { + chat_template = template_dict['default']; + } else { + throw Error( + `This model has multiple chat templates with no default specified! Please either pass a chat ` + + `template or the name of the template you wish to use to the 'chat_template' argument. Available ` + + `template names are ${Object.keys(template_dict).sort()}.` + ) + } + } + } else if (chat_template === null) { + // These are the cases when the model has a single template + // priority: `chat_template` argument > `tokenizer.chat_template` + if (this.chat_template) { + chat_template = this.chat_template; + } else { + throw Error( + "Cannot use apply_chat_template() because tokenizer.chat_template is not set and no template " + + "argument was passed! For information about writing templates and setting the " + + "tokenizer.chat_template attribute, please see the documentation at " + + "https://huggingface.co/docs/transformers/main/en/chat_templating" + ) + } + } + return chat_template; + } + + /** + * Converts a list of message objects with `"role"` and `"content"` keys to a list of token + * ids. This method is intended for use with chat models, and will read the tokenizer's chat_template attribute to + * determine the format and control tokens to use when converting. + * + * See [here](https://huggingface.co/docs/transformers/chat_templating) for more information. + * + * **Example:** Applying a chat template to a conversation. + * + * ```javascript + * import { AutoTokenizer } from "@huggingface/transformers"; + * + * const tokenizer = await AutoTokenizer.from_pretrained("Xenova/mistral-tokenizer-v1"); + * + * const chat = [ + * { "role": "user", "content": "Hello, how are you?" }, + * { "role": "assistant", "content": "I'm doing great. How can I help you today?" }, + * { "role": "user", "content": "I'd like to show off how chat templating works!" }, + * ] + * + * const text = tokenizer.apply_chat_template(chat, { tokenize: false }); + * // "[INST] Hello, how are you? [/INST]I'm doing great. How can I help you today? [INST] I'd like to show off how chat templating works! [/INST]" + * + * const input_ids = tokenizer.apply_chat_template(chat, { tokenize: true, return_tensor: false }); + * // [1, 733, 16289, 28793, 22557, 28725, 910, 460, 368, 28804, 733, 28748, 16289, 28793, 28737, 28742, 28719, 2548, 1598, 28723, 1602, 541, 315, 1316, 368, 3154, 28804, 2, 28705, 733, 16289, 28793, 315, 28742, 28715, 737, 298, 1347, 805, 910, 10706, 5752, 1077, 3791, 28808, 733, 28748, 16289, 28793] + * ``` + * + * @param {Message[]} conversation A list of message objects with `"role"` and `"content"` keys, + * representing the chat history so far. + * @param {Object} options An optional object containing the following properties: + * @param {string} [options.chat_template=null] A Jinja template to use for this conversion. If + * this is not passed, the model's chat template will be used instead. + * @param {Object[]} [options.tools=null] + * A list of tools (callable functions) that will be accessible to the model. If the template does not + * support function calling, this argument will have no effect. Each tool should be passed as a JSON Schema, + * giving the name, description and argument types for the tool. See our + * [chat templating guide](https://huggingface.co/docs/transformers/main/en/chat_templating#automated-function-conversion-for-tool-use) + * for more information. + * @param {Record[]} [options.documents=null] + * A list of dicts representing documents that will be accessible to the model if it is performing RAG + * (retrieval-augmented generation). If the template does not support RAG, this argument will have no + * effect. We recommend that each document should be a dict containing "title" and "text" keys. Please + * see the RAG section of the [chat templating guide](https://huggingface.co/docs/transformers/main/en/chat_templating#arguments-for-RAG) + * for examples of passing documents with chat templates. + * @param {boolean} [options.add_generation_prompt=false] Whether to end the prompt with the token(s) that indicate + * the start of an assistant message. This is useful when you want to generate a response from the model. + * Note that this argument will be passed to the chat template, and so it must be supported in the + * template for this argument to have any effect. + * @param {boolean} [options.tokenize=true] Whether to tokenize the output. If false, the output will be a string. + * @param {boolean} [options.padding=false] Whether to pad sequences to the maximum length. Has no effect if tokenize is false. + * @param {boolean} [options.truncation=false] Whether to truncate sequences to the maximum length. Has no effect if tokenize is false. + * @param {number} [options.max_length=null] Maximum length (in tokens) to use for padding or truncation. Has no effect if tokenize is false. + * If not specified, the tokenizer's `max_length` attribute will be used as a default. + * @param {boolean} [options.return_tensor=true] Whether to return the output as a Tensor or an Array. Has no effect if tokenize is false. + * @param {boolean} [options.return_dict=true] Whether to return a dictionary with named outputs. Has no effect if tokenize is false. + * @param {Object} [options.tokenizer_kwargs={}] Additional options to pass to the tokenizer. + * @returns {string | Tensor | number[]| number[][]|BatchEncoding} The tokenized output. + */ + apply_chat_template(conversation, { + tools = null, + documents = null, + chat_template = null, + add_generation_prompt = false, + tokenize = true, + padding = false, + truncation = false, + max_length = null, + return_tensor = true, + return_dict = false, + tokenizer_kwargs = {}, + ...kwargs + } = {}) { + + chat_template = this.get_chat_template({ chat_template, tools }); + + if (typeof chat_template !== 'string') { + throw Error(`chat_template must be a string, but got ${typeof chat_template}`); + } + + // Compilation function uses a cache to avoid recompiling the same template + let compiledTemplate = this._compiled_template_cache.get(chat_template); + if (compiledTemplate === undefined) { + compiledTemplate = new _huggingface_jinja__WEBPACK_IMPORTED_MODULE_6__.Template(chat_template); + this._compiled_template_cache.set(chat_template, compiledTemplate); + } + + const special_tokens_map = Object.create(null); + for (const key of SPECIAL_TOKEN_ATTRIBUTES) { + const value = this.getToken(key); + if (value) { + special_tokens_map[key] = value; + } + } + + const rendered = compiledTemplate.render({ + messages: conversation, + add_generation_prompt, + tools, + documents, + ...special_tokens_map, + ...kwargs, + }); + + if (tokenize) { + const out = this._call(rendered, { + add_special_tokens: false, + padding, + truncation, + max_length, + return_tensor, + ...tokenizer_kwargs, + }); + return return_dict ? out : out.input_ids; + } + + return rendered; + } +} + +/** + * BertTokenizer is a class used to tokenize text for BERT models. + * @extends PreTrainedTokenizer + */ +class BertTokenizer extends PreTrainedTokenizer { + return_token_type_ids = true; +} +/** + * Albert tokenizer + * @extends PreTrainedTokenizer + */ +class AlbertTokenizer extends PreTrainedTokenizer { + return_token_type_ids = true; +} +class MobileBertTokenizer extends PreTrainedTokenizer { + return_token_type_ids = true; +} +class SqueezeBertTokenizer extends PreTrainedTokenizer { + return_token_type_ids = true; +} +class DebertaTokenizer extends PreTrainedTokenizer { + return_token_type_ids = true; +} +class DebertaV2Tokenizer extends PreTrainedTokenizer { + return_token_type_ids = true; +} +class HerbertTokenizer extends PreTrainedTokenizer { + return_token_type_ids = true; +} +class ConvBertTokenizer extends PreTrainedTokenizer { + return_token_type_ids = true; +} +class RoFormerTokenizer extends PreTrainedTokenizer { + return_token_type_ids = true; +} +class DistilBertTokenizer extends PreTrainedTokenizer { } +class CamembertTokenizer extends PreTrainedTokenizer { } +class XLMTokenizer extends PreTrainedTokenizer { + return_token_type_ids = true; + + constructor(tokenizerJSON, tokenizerConfig) { + super(tokenizerJSON, tokenizerConfig); + console.warn('WARNING: `XLMTokenizer` is not yet supported by Hugging Face\'s "fast" tokenizers library. Therefore, you may experience slightly inaccurate results.') + } +} +class ElectraTokenizer extends PreTrainedTokenizer { + return_token_type_ids = true; +} + +class T5Tokenizer extends PreTrainedTokenizer { } +class GPT2Tokenizer extends PreTrainedTokenizer { } +class BartTokenizer extends PreTrainedTokenizer { } +class MBartTokenizer extends PreTrainedTokenizer { + constructor(tokenizerJSON, tokenizerConfig) { + super(tokenizerJSON, tokenizerConfig); + + this.languageRegex = /^[a-z]{2}_[A-Z]{2}$/; + this.language_codes = this.special_tokens.filter(x => this.languageRegex.test(x)); + this.lang_to_token = x => x; // Identity function + } + + /** + * Helper function to build translation inputs for an `MBartTokenizer`. + * @param {string|string[]} raw_inputs The text to tokenize. + * @param {Object} tokenizer_options Options to be sent to the tokenizer + * @param {Object} generate_kwargs Generation options. + * @returns {Object} Object to be passed to the model. + */ + _build_translation_inputs(raw_inputs, tokenizer_options, generate_kwargs) { + return _build_translation_inputs(this, raw_inputs, tokenizer_options, generate_kwargs); + } +} +class MBart50Tokenizer extends MBartTokenizer { } // NOTE: extends MBartTokenizer + +class RobertaTokenizer extends PreTrainedTokenizer { } + +class BloomTokenizer extends PreTrainedTokenizer { } + +const SPIECE_UNDERLINE = "▁"; + +class LlamaTokenizer extends PreTrainedTokenizer { + + padding_side = 'left'; + + constructor(tokenizerJSON, tokenizerConfig) { + super(tokenizerJSON, tokenizerConfig); + + this.legacy = tokenizerConfig.legacy ?? true; + if (!this.legacy) { + // See https://github.com/huggingface/transformers/pull/24565 for more information + this.normalizer = null; + this.pre_tokenizer = new MetaspacePreTokenizer({ + replacement: SPIECE_UNDERLINE, + add_prefix_space: true, + prepend_scheme: "first", + }); + } + } + + /** + * Helper function to handle legacy encoding of SPM tokenizers. + * Adapted from https://github.com/huggingface/transformers/blob/e6dcf8abd6f65bb4b6dfc1831b20d9ba49ce00e2/src/transformers/models/t5/tokenization_t5.py#L374-L387 + * @param {string} text The text to encode. + * @returns {string[]} The encoded tokens. + */ + _encode_text(text) { + if (text === null) return null; + + if (this.legacy || text.length === 0) { + return super._encode_text(text); + } + + let tokens = super._encode_text(SPIECE_UNDERLINE + text.replaceAll(SPIECE_UNDERLINE, " ")); + if (tokens.length > 1 && tokens[0] === SPIECE_UNDERLINE && this.special_tokens.includes(tokens[1])) { + tokens = tokens.slice(1); + } + return tokens; + } +} +class CodeLlamaTokenizer extends PreTrainedTokenizer { } + +class XLMRobertaTokenizer extends PreTrainedTokenizer { } +class MPNetTokenizer extends PreTrainedTokenizer { } + +class FalconTokenizer extends PreTrainedTokenizer { } + +class GPTNeoXTokenizer extends PreTrainedTokenizer { } + +class EsmTokenizer extends PreTrainedTokenizer { } + +class Qwen2Tokenizer extends PreTrainedTokenizer { } + +class GemmaTokenizer extends PreTrainedTokenizer { } + +class Grok1Tokenizer extends PreTrainedTokenizer { } + +/** + * Helper function to build translation inputs for an `NllbTokenizer` or `M2M100Tokenizer`. + * @param {PreTrainedTokenizer} self The tokenizer instance. + * @param {string|string[]} raw_inputs The text to tokenize. + * @param {Object} tokenizer_options Options to be sent to the tokenizer + * @param {Object} generate_kwargs Generation options. + * @returns {Object} Object to be passed to the model. + * @private + */ +function _build_translation_inputs(self, raw_inputs, tokenizer_options, generate_kwargs) { + if (!('language_codes' in self) || !Array.isArray(self.language_codes)) { + throw new Error('Tokenizer must have `language_codes` attribute set and it should be an array of language ids.') + } + if (!('languageRegex' in self) || !(self.languageRegex instanceof RegExp)) { + throw new Error('Tokenizer must have `languageRegex` attribute set and it should be a regular expression.') + } + if (!('lang_to_token' in self) || typeof self.lang_to_token !== 'function') { + throw new Error('Tokenizer must have `lang_to_token` attribute set and it should be a function.') + } + const src_lang_token = generate_kwargs.src_lang; + const tgt_lang_token = generate_kwargs.tgt_lang; + + // Check that the target language is valid: + if (!self.language_codes.includes(tgt_lang_token)) { + throw new Error(`Target language code "${tgt_lang_token}" is not valid. Must be one of: {${self.language_codes.join(', ')}}`); + } + + // Allow `src_lang` to be optional. If not set, we'll use the tokenizer's default. + if (src_lang_token !== undefined) { + // Check that the source language is valid: + if (!self.language_codes.includes(src_lang_token)) { + throw new Error(`Source language code "${src_lang_token}" is not valid. Must be one of: {${self.language_codes.join(', ')}}`); + } + + // In the same way as the Python library, we override the post-processor + // to force the source language to be first: + for (const item of self.post_processor.config.single) { + if ('SpecialToken' in item && self.languageRegex.test(item.SpecialToken.id)) { + item.SpecialToken.id = self.lang_to_token(src_lang_token); + break; + } + } + // TODO: Do the same for pair? + } + + // Override the `forced_bos_token_id` to force the correct language + generate_kwargs.forced_bos_token_id = self.model.convert_tokens_to_ids([self.lang_to_token(tgt_lang_token)])[0]; + + return self._call(raw_inputs, tokenizer_options); +} + +/** + * The NllbTokenizer class is used to tokenize text for NLLB ("No Language Left Behind") models. + * + * No Language Left Behind (NLLB) is a first-of-its-kind, AI breakthrough project + * that open-sources models capable of delivering high-quality translations directly + * between any pair of 200+ languages — including low-resource languages like Asturian, + * Luganda, Urdu and more. It aims to help people communicate with anyone, anywhere, + * regardless of their language preferences. For more information, check out their + * [paper](https://arxiv.org/abs/2207.04672). + * + * For a list of supported languages (along with their language codes), + * @see {@link https://github.com/facebookresearch/flores/blob/main/flores200/README.md#languages-in-flores-200} + */ +class NllbTokenizer extends PreTrainedTokenizer { + + constructor(tokenizerJSON, tokenizerConfig) { + super(tokenizerJSON, tokenizerConfig); + + this.languageRegex = /^[a-z]{3}_[A-Z][a-z]{3}$/; + this.language_codes = this.special_tokens.filter(x => this.languageRegex.test(x)); + this.lang_to_token = x => x; // Identity function + } + + /** + * Helper function to build translation inputs for an `NllbTokenizer`. + * @param {string|string[]} raw_inputs The text to tokenize. + * @param {Object} tokenizer_options Options to be sent to the tokenizer + * @param {Object} generate_kwargs Generation options. + * @returns {Object} Object to be passed to the model. + */ + _build_translation_inputs(raw_inputs, tokenizer_options, generate_kwargs) { + return _build_translation_inputs(this, raw_inputs, tokenizer_options, generate_kwargs); + } +} + +/** + * The M2M100Tokenizer class is used to tokenize text for M2M100 ("Many-to-Many") models. + * + * M2M100 is a multilingual encoder-decoder (seq-to-seq) model trained for Many-to-Many + * multilingual translation. It was introduced in this [paper](https://arxiv.org/abs/2010.11125) + * and first released in [this](https://github.com/pytorch/fairseq/tree/master/examples/m2m_100) repository. + * + * For a list of supported languages (along with their language codes), + * @see {@link https://huggingface.co/facebook/m2m100_418M#languages-covered} + */ +class M2M100Tokenizer extends PreTrainedTokenizer { + constructor(tokenizerJSON, tokenizerConfig) { + super(tokenizerJSON, tokenizerConfig); + + this.languageRegex = /^__[a-z]{2,3}__$/; + this.language_codes = this.special_tokens + .filter(x => this.languageRegex.test(x)) + .map(x => x.slice(2, -2)); + this.lang_to_token = x => `__${x}__`; + } + + /** + * Helper function to build translation inputs for an `M2M100Tokenizer`. + * @param {string|string[]} raw_inputs The text to tokenize. + * @param {Object} tokenizer_options Options to be sent to the tokenizer + * @param {Object} generate_kwargs Generation options. + * @returns {Object} Object to be passed to the model. + */ + _build_translation_inputs(raw_inputs, tokenizer_options, generate_kwargs) { + return _build_translation_inputs(this, raw_inputs, tokenizer_options, generate_kwargs); + } +} + +/** + * WhisperTokenizer tokenizer + * @extends PreTrainedTokenizer + */ +class WhisperTokenizer extends PreTrainedTokenizer { + + get timestamp_begin() { + return this.model.convert_tokens_to_ids(["<|notimestamps|>"])[0] + 1; + } + + /** + * Decodes automatic speech recognition (ASR) sequences. + * @param {Array<{tokens: bigint[], token_timestamps?: number[], stride: number[]}>} sequences The sequences to decode. + * @param {Object} options The options to use for decoding. + * @returns {Array, text: string}>}>} The decoded sequences. + */ + _decode_asr(sequences, { + return_timestamps = false, + return_language = false, + time_precision = null, + force_full_sequences = true + } = {}) { + // Set force_full_sequences=false if you want streaming + // TODO add support for `return_language` + + // Internal method meant to only be used by asr pipeline. + // Handles all the little quirks specific to whisper to handle + // the various options not allowed in other seq2seq models + + // =========== Overview ============ + // - iterate over all outputs + // - all tokens within output + // - Each token can be + // - language token + // - special token + // - timestamp token + // - text token + // - We accumulate the text tokens. + // - We split on end timestamps + // - Lots of complexity comes from stride and timestamps + + if (time_precision === null) { + throw Error("Must specify time_precision") + } + let last_language = null; + + const returnWordTimestamps = return_timestamps === "word"; + + function new_chunk() { + return { "language": last_language, "timestamp": [null, null], "text": "" }; + } + + // Welcome to the state machine! + const chunks = []; + let chunk = new_chunk(); + let time_offset = 0.0; + const timestamp_begin = this.timestamp_begin; + // Whisper timestamp tokens start from 0.00 and go to timestamp 30.00 in 0.02 increments. + // We can calculate the last time stamp token as timestamp_begin plus the number of tokens + // tokens from 0.00 to 30.00 which is 1500. + const total_timestamp_tokens = 1500; // (30.00 - 0.00) / 0.02 + const timestamp_end = timestamp_begin + total_timestamp_tokens; + + let previous_tokens = []; + let previous_token_timestamps = []; + + let skip = false; + let right_stride_start = null; + + + const all_special_ids = new Set(this.all_special_ids); + + for (const output of sequences) { + // NOTE: python version has batches, so it uses [0] + const token_ids = output.tokens; + const token_timestamps = returnWordTimestamps ? output.token_timestamps : null; + + // These keep track of timestamps within strides, which need + // to be skipped and resolve all tokens in a single chunk. + let last_timestamp = null; + let first_timestamp = timestamp_begin; + + if ("stride" in output) { + const [chunk_len, stride_left, stride_right] = output.stride; + + // Offset the timings to account for the other `model_outputs`. + time_offset -= stride_left; + right_stride_start = chunk_len - stride_right; + + // Keeping track of timestamps within strides + // We're going to NOT split on those, and delay until we're + // out of BOTH stride. Otherwise lots of issues occur and + // corner cases + if (stride_left) { + first_timestamp = stride_left / time_precision + timestamp_begin; + } + + if (stride_right) { + for (let i = token_ids.length - 1; i >= 0; --i) { + const token = Number(token_ids[i]); + if (token >= timestamp_begin) { + // There can be several token in the right stride + // But the last one is ALWAYS going to be skipped + if (last_timestamp !== null && (token - timestamp_begin) * time_precision < right_stride_start) { + break; + } + last_timestamp = token; + } + } + } + } + + let current_tokens = []; + let current_token_timestamps = []; + + // - all tokens within output + for (let i = 0; i < token_ids.length; ++i) { + const token = Number(token_ids[i]); + // 4 possible states for each token + // - 1/ Language code + // - 2/ all other special tokens (which we ignore) + // - 3/ Timestamp + // - 4/ Regular text + + if (all_special_ids.has(token)) { + const text = this.decode([token]); + const language = _models_whisper_common_whisper_js__WEBPACK_IMPORTED_MODULE_7__.WHISPER_LANGUAGE_MAPPING.get(text.slice(2, -2)); + + if (language !== undefined) { + // 1/ Indeed some language + // TODO Handle when language is different from the previous + // one, and we cannot use timestamped tokens to create chunks + if (last_language !== null && language !== last_language && !return_timestamps) { + previous_tokens.push(current_tokens); + const resolved_tokens = this.findLongestCommonSequence(previous_tokens)[0]; + const resolved_text = this.decode(resolved_tokens); + chunk.text = resolved_text; + chunks.push(chunk); + + // Flush all our temporary context + previous_tokens = []; + current_tokens = []; + chunk = new_chunk(); + } + + last_language = chunk.language = language; + } else { + // 2/ This is a regular special token, ignoring it + } + } else if (token >= timestamp_begin && token <= timestamp_end) { + // 3/ Timestamp token + const time = (token - timestamp_begin) * time_precision + time_offset; + const rounded_time = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.round)(time, 2); + + if (last_timestamp !== null && token >= last_timestamp) { + // Whisper outputted a timestamp token, but it falls within + // our stride, so we're going to skip it for the time being + // and resolve this later + // Skip is necessary because timestamp tokens always come + // by pair, so we need to skip the next one too (which would mark the start of another chunk). + skip = true; + } else if (skip || (previous_tokens.length > 0 && token < first_timestamp)) { + skip = false; + } else if (chunk.timestamp[0] === null) { + chunk.timestamp[0] = rounded_time; + } else { + // This is the end of the timestamp chunk + if (rounded_time === chunk.timestamp[0]) { + // This is a bug in timestamp token output + // where we're taking the duplicate token + // as a stop where it should be a start. + // This is an issue in the underlying model output + // Let's just skip it so it becomes de-factor a start agin + } else { + chunk.timestamp[1] = rounded_time; + + // Handling merges + previous_tokens.push(current_tokens) + + if (returnWordTimestamps) { + previous_token_timestamps.push(current_token_timestamps); + } + const [resolved_tokens, resolved_token_timestamps] = this.findLongestCommonSequence( + previous_tokens, previous_token_timestamps + ) + + const resolved_text = this.decode(resolved_tokens) + chunk.text = resolved_text + + if (returnWordTimestamps) { + chunk.words = this.collateWordTimestamps( + resolved_tokens, resolved_token_timestamps, last_language, + ) + } + + chunks.push(chunk) + + // Flush all our temporary context + previous_tokens = [] + current_tokens = [] + previous_token_timestamps = [] + current_token_timestamps = [] + chunk = new_chunk() + } + } + + } else { + // 4/ Regular token + // We just append to the list of all tokens so we can handle + // merges later and decode into text. + current_tokens.push(token) + + if (returnWordTimestamps) { + let start_time = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.round)(token_timestamps[i] + time_offset, 2); + + let end_time; + if (i + 1 < token_timestamps.length) { + end_time = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.round)(token_timestamps[i + 1] + time_offset, 2); + + // Do not allow punctuation-only tokens to have a duration. + // This prevents long pauses from messing up the timestamps. + const decoded_text = this.decode([token]); + if (PUNCTUATION_ONLY_REGEX.test(decoded_text)) { + // Add `time_precision` to avoid overlapping timestamps + end_time = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.round)(Math.min(start_time + time_precision, end_time), 2); + } + } else { + // should never happen + end_time = null; + } + current_token_timestamps.push([start_time, end_time]); + } + + } + } + + if ('stride' in output) { + const [chunk_len, stride_left, stride_right] = output.stride; + time_offset += chunk_len - stride_right + } + + // Leftover tokens + if (current_tokens.length > 0) { + previous_tokens.push(current_tokens) + if (returnWordTimestamps) { + previous_token_timestamps.push(current_token_timestamps); + } + } else if (previous_tokens.every(p => p.length === 0)) { + // Flushing previous tokens (END)" + chunk = new_chunk() + previous_tokens = [] + current_tokens = [] + previous_token_timestamps = []; + current_token_timestamps = []; + } + + } + + if (previous_tokens.length > 0) { + if (force_full_sequences && return_timestamps) { + // Last token should always be timestamps, so there shouldn't be + // leftover + throw new Error( + "Whisper did not predict an ending timestamp, which can happen if audio is cut off in the middle of a word. " + + "Also make sure WhisperTimeStampLogitsProcessor was used during generation." + ); + } + + // Happens when we don't use timestamps + const [resolved_tokens, resolved_token_timestamps] = this.findLongestCommonSequence(previous_tokens, previous_token_timestamps); + + // Flushing previous tokens (FINAL) + const resolved_text = this.decode(resolved_tokens); + chunk.text = resolved_text; + if (returnWordTimestamps) { + chunk.words = this.collateWordTimestamps( + resolved_tokens, resolved_token_timestamps, last_language, + ) + } + chunks.push(chunk); + } + + let optional = Object.create(null); + + // Preparing and cleaning up the pipeline output + const full_text = chunks.map(chunk => chunk.text).join(''); + if (return_timestamps || return_language) { + for (let i = 0; i < chunks.length; ++i) { + const chunk = chunks[i]; + if (!return_timestamps) { + delete chunk["timestamp"]; + } + + if (!return_language) { + delete chunk["language"]; + } + } + if (returnWordTimestamps) { + const new_chunks = []; + for (const chunk of chunks) { + for (const word of chunk.words) { + new_chunks.push(word); + } + } + optional = { "chunks": new_chunks }; + } else { + optional = { "chunks": chunks }; + } + } + return [full_text, optional]; + + } + + /** + * Finds the longest common sequence among the provided sequences. + * @param {number[][]} sequences An array of sequences of token ids to compare. + * @returns {number[][]} The longest common sequence found. + * @throws {Error} If there is a bug within the function. + * @private + */ + findLongestCommonSequence(sequences, token_timestamp_sequences = null) { + // It would be much harder to do O(n) because of fault tolerance. + // We actually have a really good property which is that the total sequence + // MUST be those subsequences in order. + // If token_timestamp_sequences is provided, will split those sequences in + // exactly the same way. + let leftSequence = sequences[0]; + let leftLength = leftSequence.length; + let totalSequence = []; + + const use_token_timestamp_sequences = Array.isArray(token_timestamp_sequences) && token_timestamp_sequences.length > 0; + let total_token_timestamp_sequence = use_token_timestamp_sequences ? [] : null; + let left_token_timestamp_sequence = use_token_timestamp_sequences ? token_timestamp_sequences[0] : null; + for (let i = 1; i < sequences.length; ++i) { + const rightSequence = sequences[i]; + let max = 0.0; + let maxIndices = [leftLength, leftLength, 0, 0]; + // Here we're sliding matches + // [a, b, c, d] + // [c, d, f] + // = [c] == [d] + + // [a, b, c, d] + // [c, d, f] + // = [c, d] == [c, d] + + + // [a, b, c, d] + // [c, d, f] + + // = [b, c, d] == [c, d, f] + + // [a, b, c, d] + // [c, d, f] + + // [a, b, c] == [c, d, f] + + // [a, b, c, d] + // [d, f] + + // [a, b] == [d, f] + + // [a, b, c, d] + // [f] + + // [a] == [f] + + const rightLength = rightSequence.length; + for (let j = 1; j < leftLength + rightLength; ++j) { + // Slightly convoluted because we don't want out of bound indices + // This will be necessary for a small conflict resolution optimization + // later + const leftStart = Math.max(0, leftLength - j); + const leftStop = Math.min(leftLength, leftLength + rightLength - j); + const left = leftSequence.slice(leftStart, leftStop); + const rightStart = Math.max(0, j - leftLength); + const rightStop = Math.min(rightLength, j); + const right = rightSequence.slice(rightStart, rightStop); + if (left.length !== right.length) { + throw new Error("There is a bug within whisper `decode_asr` function, please report it. Dropping to prevent bad inference."); + } + + let matches; + if (use_token_timestamp_sequences) { + // Get length of longest subsequence of tokens that match + // and have timestamps that are in order + matches = left.filter((elem, idx) => ( + elem === right[idx] + && left_token_timestamp_sequence[leftStart + idx] <= token_timestamp_sequences[i][rightStart + idx] + )).length; + } else { + matches = left.filter((elem, idx) => elem === right[idx]).length; + } + + // epsilon to favor long perfect matches + const eps = j / 10000.0; + const matching = matches / j + eps; + if (matches > 1 && matching > max) { + max = matching; + maxIndices = [leftStart, leftStop, rightStart, rightStop]; + } + } + const [leftStart, leftStop, rightStart, rightStop] = maxIndices; + const leftMid = Math.floor((leftStop + leftStart) / 2); + const rightMid = Math.floor((rightStop + rightStart) / 2); + totalSequence.push(...leftSequence.slice(0, leftMid)); + leftSequence = rightSequence.slice(rightMid); + leftLength = leftSequence.length; + + if (use_token_timestamp_sequences) { + total_token_timestamp_sequence.push(...left_token_timestamp_sequence.slice(0, leftMid)); + left_token_timestamp_sequence = token_timestamp_sequences[i].slice(rightMid); + } + } + totalSequence.push(...leftSequence); + + if (use_token_timestamp_sequences) { + total_token_timestamp_sequence.push(...left_token_timestamp_sequence); + return [totalSequence, total_token_timestamp_sequence]; + } else { + return [totalSequence, []]; + } + } + + /** @private */ + collateWordTimestamps(tokens, token_timestamps, language) { + + const [words, _, token_indices] = this.combineTokensIntoWords(tokens, language); + + const timings = []; + for (let i = 0; i < words.length; ++i) { + const indices = token_indices[i]; + timings.push({ + text: words[i], + timestamp: [ + token_timestamps[indices.at(0)][0], + token_timestamps[indices.at(-1)][1], + ], + }); + } + return timings; + } + + /** + * Groups tokens by word. Returns a tuple containing a list of strings with the words, + * and a list of `token_id` sequences with the tokens making up each word. + * @param {number[]} tokens + * @param {string} [language] + * @param {string} prepend_punctionations + * @param {string} append_punctuations + * + * @private + */ + combineTokensIntoWords(tokens, language, prepend_punctionations = "\"'“¡¿([{-", append_punctuations = "\"'.。,,!!??::”)]}、") { + language = language ?? 'english'; + + let words, word_tokens, token_indices; + + if (["chinese", "japanese", "thai", "lao", "myanmar"].includes(language)) { + // These languages don't typically use spaces. + [words, word_tokens, token_indices] = this.splitTokensOnUnicode(tokens) + } else { + [words, word_tokens, token_indices] = this.splitTokensOnSpaces(tokens) + } + + return this.mergePunctuations(words, word_tokens, token_indices, prepend_punctionations, append_punctuations); + } + + /** @type {PreTrainedTokenizer['decode']} */ + decode( + token_ids, + decode_args, + ) { + let text; + // @ts-ignore + if (decode_args?.decode_with_timestamps) { + if (token_ids instanceof _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor) { + token_ids = prepareTensorForDecode(token_ids); + } + text = this.decodeWithTimestamps(token_ids, decode_args); + } else { + text = super.decode(token_ids, decode_args); + } + // TODO: implement offsets + // if (decode_args.output_offsets) { + // let offsets = this.computeOffsets + // } + return text; + } + + /** + * @param {number[]|bigint[]} token_ids List of token IDs to decode. + * @param {Object} decode_args Optional arguments for decoding + * @private + */ + decodeWithTimestamps(token_ids, decode_args) { + const time_precision = decode_args?.time_precision ?? 0.02; + + const timestamp_begin = Array.from(this.all_special_ids).at(-1) + 1; + /**@type {Array} */ + let outputs = [[]]; + for (let token of token_ids) { + token = Number(token); + if (token >= timestamp_begin) { + const timestamp = ((token - timestamp_begin) * time_precision).toFixed(2); + outputs.push(`<|${timestamp}|>`); + outputs.push([]); + } else { + outputs[outputs.length - 1].push(token); + } + } + outputs = outputs.map( + s => typeof s === 'string' ? s : super.decode(s, decode_args) + ) + + return outputs.join(''); + } + + /** + * Combine tokens into words by splitting at any position where the tokens are decoded as valid unicode points. + * @param {number[]} tokens + * @returns {*} + * @private + */ + splitTokensOnUnicode(tokens) { + const decoded_full = this.decode(tokens, { + // @ts-ignore + decode_with_timestamps: true, + }); + const replacement_char = '\uFFFD'; + + const words = [] + const word_tokens = [] + const token_indices = [] + let current_tokens = [] + let current_indices = [] + let unicode_offset = 0 + + for (let token_idx = 0; token_idx < tokens.length; ++token_idx) { + const token = tokens[token_idx]; + + current_tokens.push(token); + current_indices.push(token_idx); + + const decoded = this.decode(current_tokens, { + // @ts-ignore + decode_with_timestamps: true, + }); + + if (!decoded.includes(replacement_char) || decoded_full[unicode_offset + decoded.indexOf(replacement_char)] === replacement_char) { + words.push(decoded) + word_tokens.push(current_tokens) + token_indices.push(current_indices) + current_tokens = [] + current_indices = [] + unicode_offset += decoded.length; + } + + } + + return [words, word_tokens, token_indices] + } + + /** + * Combine tokens into words by splitting at whitespace and punctuation tokens. + * @param {number[]} tokens + * @private + */ + splitTokensOnSpaces(tokens) { + + const [subwords, subword_tokens_list, subword_indices_list] = this.splitTokensOnUnicode(tokens); + + const words = [] + const word_tokens = [] + const token_indices = [] + + const punctuationRegex = new RegExp(`^[${PUNCTUATION_REGEX}]$`, 'gu'); + + for (let i = 0; i < subwords.length; ++i) { + + const subword = subwords[i]; + const subword_tokens = subword_tokens_list[i]; + const subword_indices = subword_indices_list[i]; + + // @ts-ignore + const special = subword_tokens[0] >= this.model.tokens_to_ids.get('<|endoftext|>'); + const with_space = subword.startsWith(' '); + const trimmed = subword.trim(); + const punctuation = punctuationRegex.test(trimmed); + + if (special || with_space || punctuation || words.length === 0) { + words.push(subword); + word_tokens.push(subword_tokens); + token_indices.push(subword_indices); + } else { + const ix = words.length - 1; + words[ix] += subword; + word_tokens[ix].push(...subword_tokens); + token_indices[ix].push(...subword_indices); + } + } + + return [words, word_tokens, token_indices]; + + } + + /** + * Merges punctuation tokens with neighboring words. + * @param {string[]} words + * @param {number[][]} tokens + * @param {number[][]} indices + * @param {string} prepended + * @param {string} appended + * @private + */ + mergePunctuations(words, tokens, indices, prepended, appended) { + + const newWords = structuredClone(words); + const newTokens = structuredClone(tokens); + const newIndices = structuredClone(indices); + + + // prepend punctuations + let i = newWords.length - 2; + let j = newWords.length - 1; + + while (i >= 0) { + if (newWords[i].startsWith(' ') && prepended.includes(newWords[i].trim())) { + newWords[j] = newWords[i] + newWords[j]; + newTokens[j] = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(newTokens[i], newTokens[j]); + newIndices[j] = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(newIndices[i], newIndices[j]); + newWords[i] = ''; + newTokens[i] = []; + newIndices[i] = []; + } else { + j = i; + } + --i; + } + + // append punctuations + i = 0; + j = 1; + while (j < newWords.length) { + if (!newWords[i].endsWith(' ') && appended.includes(newWords[j])) { + newWords[i] += newWords[j]; + newTokens[i] = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(newTokens[i], newTokens[j]); + newIndices[i] = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(newIndices[i], newIndices[j]); + newWords[j] = ''; + newTokens[j] = []; + newIndices[j] = []; + } else { + i = j; + } + ++j; + } + + return [ + newWords.filter(x => x), + newTokens.filter(x => x.length > 0), + newIndices.filter(x => x.length > 0), + ] + } +} +class CodeGenTokenizer extends PreTrainedTokenizer { } +class CLIPTokenizer extends PreTrainedTokenizer { } +class SiglipTokenizer extends PreTrainedTokenizer { } + +/** + * @todo This model is not yet supported by Hugging Face's "fast" tokenizers library (https://github.com/huggingface/tokenizers). + * Therefore, this implementation (which is based on fast tokenizers) may produce slightly inaccurate results. + */ +class MarianTokenizer extends PreTrainedTokenizer { + /** + * Create a new MarianTokenizer instance. + * @param {Object} tokenizerJSON The JSON of the tokenizer. + * @param {Object} tokenizerConfig The config of the tokenizer. + */ + constructor(tokenizerJSON, tokenizerConfig) { + super(tokenizerJSON, tokenizerConfig); + + this.languageRegex = /^(>>\w+<<)\s*/g; + + this.supported_language_codes = this.model.vocab.filter( + x => this.languageRegex.test(x) + ); + + console.warn('WARNING: `MarianTokenizer` is not yet supported by Hugging Face\'s "fast" tokenizers library. Therefore, you may experience slightly inaccurate results.') + } + + /** + * Encodes a single text. Overriding this method is necessary since the language codes + * must be removed before encoding with sentencepiece model. + * @see https://github.com/huggingface/transformers/blob/12d51db243a00726a548a43cc333390ebae731e3/src/transformers/models/marian/tokenization_marian.py#L204-L213 + * + * @param {string|null} text The text to encode. + * @returns {Array} The encoded tokens. + */ + _encode_text(text) { + if (text === null) return null; + + // Check if text starts with language code: + const [matchInfo, ...remainder] = text.trim().split(this.languageRegex); + + if (remainder.length === 0) { + // No language code, encode normally + return super._encode_text(matchInfo); + + } else if (remainder.length === 2) { + // Text starts with language code, so we do not encode it with sentencepiece. + const [language, text] = remainder; + + if (!this.supported_language_codes.includes(language)) { + console.warn(`Unsupported language code "${language}" detected, which may lead to unexpected behavior. Should be one of: ${JSON.stringify(this.supported_language_codes)}`) + } + return (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)([language], super._encode_text(text)); + } + } + +} + +class Wav2Vec2CTCTokenizer extends PreTrainedTokenizer { } + +class BlenderbotTokenizer extends PreTrainedTokenizer { } +class BlenderbotSmallTokenizer extends PreTrainedTokenizer { } + +class SpeechT5Tokenizer extends PreTrainedTokenizer { } + +class NougatTokenizer extends PreTrainedTokenizer { } + +class VitsTokenizer extends PreTrainedTokenizer { + + constructor(tokenizerJSON, tokenizerConfig) { + super(tokenizerJSON, tokenizerConfig); + + // Custom decoder function + this.decoder = new VitsDecoder({}); + } +} + +class CohereTokenizer extends PreTrainedTokenizer { } + +class MgpstrTokenizer extends PreTrainedTokenizer { } + +/** + * Helper class which is used to instantiate pretrained tokenizers with the `from_pretrained` function. + * The chosen tokenizer class is determined by the type specified in the tokenizer config. + * + * @example + * const tokenizer = await AutoTokenizer.from_pretrained('Xenova/bert-base-uncased'); + */ +class AutoTokenizer { + static TOKENIZER_CLASS_MAPPING = { + T5Tokenizer, + DistilBertTokenizer, + CamembertTokenizer, + DebertaTokenizer, + DebertaV2Tokenizer, + BertTokenizer, + HerbertTokenizer, + ConvBertTokenizer, + RoFormerTokenizer, + XLMTokenizer, + ElectraTokenizer, + MobileBertTokenizer, + SqueezeBertTokenizer, + AlbertTokenizer, + GPT2Tokenizer, + BartTokenizer, + MBartTokenizer, + MBart50Tokenizer, + RobertaTokenizer, + WhisperTokenizer, + CodeGenTokenizer, + CLIPTokenizer, + SiglipTokenizer, + MarianTokenizer, + BloomTokenizer, + NllbTokenizer, + M2M100Tokenizer, + LlamaTokenizer, + CodeLlamaTokenizer, + XLMRobertaTokenizer, + MPNetTokenizer, + FalconTokenizer, + GPTNeoXTokenizer, + EsmTokenizer, + Wav2Vec2CTCTokenizer, + BlenderbotTokenizer, + BlenderbotSmallTokenizer, + SpeechT5Tokenizer, + NougatTokenizer, + VitsTokenizer, + Qwen2Tokenizer, + GemmaTokenizer, + Grok1Tokenizer, + CohereTokenizer, + MgpstrTokenizer, + + // Base case: + PreTrainedTokenizer, + } + + + /** + * Instantiate one of the tokenizer classes of the library from a pretrained model. + * + * The tokenizer class to instantiate is selected based on the `tokenizer_class` property of the config object + * (either passed as an argument or loaded from `pretrained_model_name_or_path` if possible) + * + * @param {string} pretrained_model_name_or_path The name or path of the pretrained model. Can be either: + * - A string, the *model id* of a pretrained tokenizer hosted inside a model repo on huggingface.co. + * Valid model ids can be located at the root-level, like `bert-base-uncased`, or namespaced under a + * user or organization name, like `dbmdz/bert-base-german-cased`. + * - A path to a *directory* containing tokenizer files, e.g., `./my_model_directory/`. + * @param {PretrainedTokenizerOptions} options Additional options for loading the tokenizer. + * + * @returns {Promise} A new instance of the PreTrainedTokenizer class. + */ + static async from_pretrained(pretrained_model_name_or_path, { + progress_callback = null, + config = null, + cache_dir = null, + local_files_only = false, + revision = 'main', + legacy = null, + } = {}) { + + const [tokenizerJSON, tokenizerConfig] = await loadTokenizer(pretrained_model_name_or_path, { + progress_callback, + config, + cache_dir, + local_files_only, + revision, + legacy, + }) + + // Some tokenizers are saved with the "Fast" suffix, so we remove that if present. + const tokenizerName = tokenizerConfig.tokenizer_class?.replace(/Fast$/, '') ?? 'PreTrainedTokenizer'; + + let cls = this.TOKENIZER_CLASS_MAPPING[tokenizerName]; + if (!cls) { + console.warn(`Unknown tokenizer class "${tokenizerName}", attempting to construct from base class.`); + cls = PreTrainedTokenizer; + } + return new cls(tokenizerJSON, tokenizerConfig); + } +} + + +/***/ }), + +/***/ "./src/utils/audio.js": +/*!****************************!*\ + !*** ./src/utils/audio.js ***! + \****************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ RawAudio: () => (/* binding */ RawAudio), +/* harmony export */ hamming: () => (/* binding */ hamming), +/* harmony export */ hanning: () => (/* binding */ hanning), +/* harmony export */ mel_filter_bank: () => (/* binding */ mel_filter_bank), +/* harmony export */ read_audio: () => (/* binding */ read_audio), +/* harmony export */ spectrogram: () => (/* binding */ spectrogram), +/* harmony export */ window_function: () => (/* binding */ window_function) +/* harmony export */ }); +/* harmony import */ var _hub_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./hub.js */ "./src/utils/hub.js"); +/* harmony import */ var _maths_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./maths.js */ "./src/utils/maths.js"); +/* harmony import */ var _core_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./core.js */ "./src/utils/core.js"); +/* harmony import */ var _env_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ../env.js */ "./src/env.js"); +/* harmony import */ var fs__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! fs */ "?7a2c"); +/* harmony import */ var _tensor_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ./tensor.js */ "./src/utils/tensor.js"); +/** + * @file Helper module for audio processing. + * + * These functions and classes are only used internally, + * meaning an end-user shouldn't need to access anything here. + * + * @module utils/audio + */ + + + + + + + + + +/** + * Helper function to read audio from a path/URL. + * @param {string|URL} url The path/URL to load the audio from. + * @param {number} sampling_rate The sampling rate to use when decoding the audio. + * @returns {Promise} The decoded audio as a `Float32Array`. + */ +async function read_audio(url, sampling_rate) { + if (typeof AudioContext === 'undefined') { + // Running in node or an environment without AudioContext + throw Error( + "Unable to load audio from path/URL since `AudioContext` is not available in your environment. " + + "Instead, audio data should be passed directly to the pipeline/processor. " + + "For more information and some example code, see https://huggingface.co/docs/transformers.js/guides/node-audio-processing." + ) + } + + const response = await (await (0,_hub_js__WEBPACK_IMPORTED_MODULE_0__.getFile)(url)).arrayBuffer(); + const audioCTX = new AudioContext({ sampleRate: sampling_rate }); + if (typeof sampling_rate === 'undefined') { + console.warn(`No sampling rate provided, using default of ${audioCTX.sampleRate}Hz.`) + } + const decoded = await audioCTX.decodeAudioData(response); + + /** @type {Float32Array} */ + let audio; + + // We now replicate HuggingFace's `ffmpeg_read` method: + if (decoded.numberOfChannels === 2) { + // When downmixing a stereo audio file to mono using the -ac 1 option in FFmpeg, + // the audio signal is summed across both channels to create a single mono channel. + // However, if the audio is at full scale (i.e. the highest possible volume level), + // the summing of the two channels can cause the audio signal to clip or distort. + + // To prevent this clipping, FFmpeg applies a scaling factor of 1/sqrt(2) (~ 0.707) + // to the audio signal before summing the two channels. This scaling factor ensures + // that the combined audio signal will not exceed the maximum possible level, even + // if both channels are at full scale. + + // After applying this scaling factor, the audio signal from both channels is summed + // to create a single mono channel. It's worth noting that this scaling factor is + // only applied when downmixing stereo audio to mono using the -ac 1 option in FFmpeg. + // If you're using a different downmixing method, or if you're not downmixing the + // audio at all, this scaling factor may not be needed. + const SCALING_FACTOR = Math.sqrt(2); + + const left = decoded.getChannelData(0); + const right = decoded.getChannelData(1); + + audio = new Float32Array(left.length); + for (let i = 0; i < decoded.length; ++i) { + audio[i] = SCALING_FACTOR * (left[i] + right[i]) / 2; + } + + } else { + // If the audio is not stereo, we can just use the first channel: + audio = decoded.getChannelData(0); + } + + return audio; +} + +/** + * Helper function to generate windows that are special cases of the generalized cosine window. + * See https://www.mathworks.com/help/signal/ug/generalized-cosine-windows.html for more information. + * @param {number} M Number of points in the output window. If zero or less, an empty array is returned. + * @param {number} a_0 Offset for the generalized cosine window. + * @returns {Float64Array} The generated window. + */ +function generalized_cosine_window(M, a_0) { + if (M < 1) { + return new Float64Array(); + } + if (M === 1) { + return new Float64Array([1]); + } + + const a_1 = 1 - a_0; + const factor = 2 * Math.PI / (M - 1); + + const cos_vals = new Float64Array(M); + for (let i = 0; i < M; ++i) { + cos_vals[i] = a_0 - a_1 * Math.cos(i * factor); + } + return cos_vals; +} + +/** + * Generates a Hanning window of length M. + * See https://numpy.org/doc/stable/reference/generated/numpy.hanning.html for more information. + * + * @param {number} M The length of the Hanning window to generate. + * @returns {Float64Array} The generated Hanning window. + */ +function hanning(M) { + return generalized_cosine_window(M, 0.5); +} + + +/** + * Generates a Hamming window of length M. + * See https://numpy.org/doc/stable/reference/generated/numpy.hamming.html for more information. + * + * @param {number} M The length of the Hamming window to generate. + * @returns {Float64Array} The generated Hamming window. + */ +function hamming(M) { + return generalized_cosine_window(M, 0.54); +} + + +const HERTZ_TO_MEL_MAPPING = { + "htk": (/** @type {number} */ freq) => 2595.0 * Math.log10(1.0 + (freq / 700.0)), + "kaldi": (/** @type {number} */ freq) => 1127.0 * Math.log(1.0 + (freq / 700.0)), + "slaney": (/** @type {number} */ freq, min_log_hertz = 1000.0, min_log_mel = 15.0, logstep = 27.0 / Math.log(6.4)) => + freq >= min_log_hertz + ? min_log_mel + Math.log(freq / min_log_hertz) * logstep + : 3.0 * freq / 200.0, +} + +/** + * @template {Float32Array|Float64Array|number} T + * @param {T} freq + * @param {string} [mel_scale] + * @returns {T} + */ +function hertz_to_mel(freq, mel_scale = "htk") { + const fn = HERTZ_TO_MEL_MAPPING[mel_scale]; + if (!fn) { + throw new Error('mel_scale should be one of "htk", "slaney" or "kaldi".'); + } + + // @ts-expect-error ts(2322) + return typeof freq === 'number' ? fn(freq) : freq.map(x => fn(x)); +} + +const MEL_TO_HERTZ_MAPPING = { + "htk": (/** @type {number} */ mels) => 700.0 * (10.0 ** (mels / 2595.0) - 1.0), + "kaldi": (/** @type {number} */ mels) => 700.0 * (Math.exp(mels / 1127.0) - 1.0), + "slaney": (/** @type {number} */ mels, min_log_hertz = 1000.0, min_log_mel = 15.0, logstep = Math.log(6.4) / 27.0) => mels >= min_log_mel + ? min_log_hertz * Math.exp(logstep * (mels - min_log_mel)) + : 200.0 * mels / 3.0, +} + +/** + * @template {Float32Array|Float64Array|number} T + * @param {T} mels + * @param {string} [mel_scale] + * @returns {T} + */ +function mel_to_hertz(mels, mel_scale = "htk") { + const fn = MEL_TO_HERTZ_MAPPING[mel_scale]; + if (!fn) { + throw new Error('mel_scale should be one of "htk", "slaney" or "kaldi".'); + } + + // @ts-expect-error ts(2322) + return typeof mels === 'number' ? fn(mels) : mels.map(x => fn(x)); +} + +/** +* Creates a triangular filter bank. +* +* Adapted from torchaudio and librosa. +* +* @param {Float64Array} fft_freqs Discrete frequencies of the FFT bins in Hz, of shape `(num_frequency_bins,)`. +* @param {Float64Array} filter_freqs Center frequencies of the triangular filters to create, in Hz, of shape `(num_mel_filters,)`. +* @returns {number[][]} of shape `(num_frequency_bins, num_mel_filters)`. +*/ +function _create_triangular_filter_bank(fft_freqs, filter_freqs) { + const filter_diff = Float64Array.from( + { length: filter_freqs.length - 1 }, + (_, i) => filter_freqs[i + 1] - filter_freqs[i] + ); + + const slopes = Array.from({ + length: fft_freqs.length + }, () => new Array(filter_freqs.length)); + + for (let j = 0; j < fft_freqs.length; ++j) { + const slope = slopes[j]; + for (let i = 0; i < filter_freqs.length; ++i) { + slope[i] = filter_freqs[i] - fft_freqs[j]; + } + } + + const numFreqs = filter_freqs.length - 2; + const ret = Array.from({ length: numFreqs }, () => new Array(fft_freqs.length)); + + for (let j = 0; j < fft_freqs.length; ++j) { // 201 + const slope = slopes[j]; + for (let i = 0; i < numFreqs; ++i) { // 80 + const down = -slope[i] / filter_diff[i]; + const up = slope[i + 2] / filter_diff[i + 1]; + ret[i][j] = Math.max(0, Math.min(down, up)); + } + } + return ret; +} + +/** + * Return evenly spaced numbers over a specified interval. + * @param {number} start The starting value of the sequence. + * @param {number} end The end value of the sequence. + * @param {number} num Number of samples to generate. + * @returns `num` evenly spaced samples, calculated over the interval `[start, stop]`. + */ +function linspace(start, end, num) { + const step = (end - start) / (num - 1); + return Float64Array.from({ length: num }, (_, i) => start + step * i); +} + +/** + * Creates a frequency bin conversion matrix used to obtain a mel spectrogram. This is called a *mel filter bank*, and + * various implementation exist, which differ in the number of filters, the shape of the filters, the way the filters + * are spaced, the bandwidth of the filters, and the manner in which the spectrum is warped. The goal of these + * features is to approximate the non-linear human perception of the variation in pitch with respect to the frequency. + * @param {number} num_frequency_bins Number of frequency bins (should be the same as `n_fft // 2 + 1` + * where `n_fft` is the size of the Fourier Transform used to compute the spectrogram). + * @param {number} num_mel_filters Number of mel filters to generate. + * @param {number} min_frequency Lowest frequency of interest in Hz. + * @param {number} max_frequency Highest frequency of interest in Hz. This should not exceed `sampling_rate / 2`. + * @param {number} sampling_rate Sample rate of the audio waveform. + * @param {string} [norm] If `"slaney"`, divide the triangular mel weights by the width of the mel band (area normalization). + * @param {string} [mel_scale] The mel frequency scale to use, `"htk"` or `"slaney"`. + * @param {boolean} [triangularize_in_mel_space] If this option is enabled, the triangular filter is applied in mel space rather than frequency space. + * This should be set to `true` in order to get the same results as `torchaudio` when computing mel filters. + * @returns {number[][]} Triangular filter bank matrix, which is a 2D array of shape (`num_frequency_bins`, `num_mel_filters`). + * This is a projection matrix to go from a spectrogram to a mel spectrogram. + */ +function mel_filter_bank( + num_frequency_bins, + num_mel_filters, + min_frequency, + max_frequency, + sampling_rate, + norm = null, + mel_scale = "htk", + triangularize_in_mel_space = false, +) { + if (norm !== null && norm !== "slaney") { + throw new Error('norm must be one of null or "slaney"'); + } + + if (num_frequency_bins < 2) { + throw new Error(`Require num_frequency_bins: ${num_frequency_bins} >= 2`); + } + + if (min_frequency > max_frequency) { + throw new Error(`Require min_frequency: ${min_frequency} <= max_frequency: ${max_frequency}`); + } + + const mel_min = hertz_to_mel(min_frequency, mel_scale); + const mel_max = hertz_to_mel(max_frequency, mel_scale); + const mel_freqs = linspace(mel_min, mel_max, num_mel_filters + 2); + + let filter_freqs = mel_to_hertz(mel_freqs, mel_scale); + let fft_freqs; // frequencies of FFT bins in Hz + + if (triangularize_in_mel_space) { + const fft_bin_width = sampling_rate / ((num_frequency_bins - 1) * 2); + fft_freqs = hertz_to_mel(Float64Array.from({ length: num_frequency_bins }, (_, i) => i * fft_bin_width), mel_scale); + filter_freqs = mel_freqs; + } else { + fft_freqs = linspace(0, Math.floor(sampling_rate / 2), num_frequency_bins); + } + + const mel_filters = _create_triangular_filter_bank(fft_freqs, filter_freqs); + + if (norm !== null && norm === "slaney") { + // Slaney-style mel is scaled to be approx constant energy per channel + for (let i = 0; i < num_mel_filters; ++i) { + const filter = mel_filters[i]; + const enorm = 2.0 / (filter_freqs[i + 2] - filter_freqs[i]); + for (let j = 0; j < num_frequency_bins; ++j) { + // Apply this enorm to all frequency bins + filter[j] *= enorm; + } + } + } + + // TODO warn if there is a zero row + + return mel_filters; + +} + +/** + * @template {Float32Array|Float64Array} T + * Pads an array with a reflected version of itself on both ends. + * @param {T} array The array to pad. + * @param {number} left The amount of padding to add to the left. + * @param {number} right The amount of padding to add to the right. + * @returns {T} The padded array. + */ +function padReflect(array, left, right) { + // @ts-ignore + const padded = new array.constructor(array.length + left + right); + const w = array.length - 1; + + for (let i = 0; i < array.length; ++i) { + padded[left + i] = array[i]; + } + + for (let i = 1; i <= left; ++i) { + padded[left - i] = array[(0,_core_js__WEBPACK_IMPORTED_MODULE_2__.calculateReflectOffset)(i, w)]; + } + + for (let i = 1; i <= right; ++i) { + padded[w + left + i] = array[(0,_core_js__WEBPACK_IMPORTED_MODULE_2__.calculateReflectOffset)(w - i, w)]; + } + + return padded; +} + +/** + * Helper function to compute `amplitude_to_db` and `power_to_db`. + * @template {Float32Array|Float64Array} T + * @param {T} spectrogram + * @param {number} factor + * @param {number} reference + * @param {number} min_value + * @param {number} db_range + * @returns {T} + */ +function _db_conversion_helper(spectrogram, factor, reference, min_value, db_range) { + if (reference <= 0) { + throw new Error('reference must be greater than zero'); + } + + if (min_value <= 0) { + throw new Error('min_value must be greater than zero'); + } + + reference = Math.max(min_value, reference); + + const logReference = Math.log10(reference); + for (let i = 0; i < spectrogram.length; ++i) { + spectrogram[i] = factor * Math.log10(Math.max(min_value, spectrogram[i]) - logReference) + } + + if (db_range !== null) { + if (db_range <= 0) { + throw new Error('db_range must be greater than zero'); + } + const maxValue = (0,_maths_js__WEBPACK_IMPORTED_MODULE_1__.max)(spectrogram)[0] - db_range; + for (let i = 0; i < spectrogram.length; ++i) { + spectrogram[i] = Math.max(spectrogram[i], maxValue); + } + } + + return spectrogram; +} + +/** + * Converts an amplitude spectrogram to the decibel scale. This computes `20 * log10(spectrogram / reference)`, + * using basic logarithm properties for numerical stability. NOTE: Operates in-place. + * + * The motivation behind applying the log function on the (mel) spectrogram is that humans do not hear loudness on a + * linear scale. Generally to double the perceived volume of a sound we need to put 8 times as much energy into it. + * This means that large variations in energy may not sound all that different if the sound is loud to begin with. + * This compression operation makes the (mel) spectrogram features match more closely what humans actually hear. + * + * @template {Float32Array|Float64Array} T + * @param {T} spectrogram The input amplitude (mel) spectrogram. + * @param {number} [reference=1.0] Sets the input spectrogram value that corresponds to 0 dB. + * For example, use `np.max(spectrogram)` to set the loudest part to 0 dB. Must be greater than zero. + * @param {number} [min_value=1e-5] The spectrogram will be clipped to this minimum value before conversion to decibels, + * to avoid taking `log(0)`. The default of `1e-5` corresponds to a minimum of -100 dB. Must be greater than zero. + * @param {number} [db_range=null] Sets the maximum dynamic range in decibels. For example, if `db_range = 80`, the + * difference between the peak value and the smallest value will never be more than 80 dB. Must be greater than zero. + * @returns {T} The modified spectrogram in decibels. + */ +function amplitude_to_db(spectrogram, reference = 1.0, min_value = 1e-5, db_range = null) { + return _db_conversion_helper(spectrogram, 20.0, reference, min_value, db_range); +} + +/** + * Converts a power spectrogram to the decibel scale. This computes `10 * log10(spectrogram / reference)`, + * using basic logarithm properties for numerical stability. NOTE: Operates in-place. + * + * The motivation behind applying the log function on the (mel) spectrogram is that humans do not hear loudness on a + * linear scale. Generally to double the perceived volume of a sound we need to put 8 times as much energy into it. + * This means that large variations in energy may not sound all that different if the sound is loud to begin with. + * This compression operation makes the (mel) spectrogram features match more closely what humans actually hear. + * + * Based on the implementation of `librosa.power_to_db`. + * + * @template {Float32Array|Float64Array} T + * @param {T} spectrogram The input power (mel) spectrogram. Note that a power spectrogram has the amplitudes squared! + * @param {number} [reference=1.0] Sets the input spectrogram value that corresponds to 0 dB. + * For example, use `np.max(spectrogram)` to set the loudest part to 0 dB. Must be greater than zero. + * @param {number} [min_value=1e-10] The spectrogram will be clipped to this minimum value before conversion to decibels, + * to avoid taking `log(0)`. The default of `1e-10` corresponds to a minimum of -100 dB. Must be greater than zero. + * @param {number} [db_range=null] Sets the maximum dynamic range in decibels. For example, if `db_range = 80`, the + * difference between the peak value and the smallest value will never be more than 80 dB. Must be greater than zero. + * @returns {T} The modified spectrogram in decibels. + */ +function power_to_db(spectrogram, reference = 1.0, min_value = 1e-10, db_range = null) { + return _db_conversion_helper(spectrogram, 10.0, reference, min_value, db_range); +} + +/** + * Calculates a spectrogram over one waveform using the Short-Time Fourier Transform. + * + * This function can create the following kinds of spectrograms: + * - amplitude spectrogram (`power = 1.0`) + * - power spectrogram (`power = 2.0`) + * - complex-valued spectrogram (`power = None`) + * - log spectrogram (use `log_mel` argument) + * - mel spectrogram (provide `mel_filters`) + * - log-mel spectrogram (provide `mel_filters` and `log_mel`) + * + * In this implementation, the window is assumed to be zero-padded to have the same size as the analysis frame. + * A padded window can be obtained from `window_function()`. The FFT input buffer may be larger than the analysis frame, + * typically the next power of two. + * + * @param {Float32Array|Float64Array} waveform The input waveform of shape `(length,)`. This must be a single real-valued, mono waveform. + * @param {Float32Array|Float64Array} window The windowing function to apply of shape `(frame_length,)`, including zero-padding if necessary. The actual window length may be + * shorter than `frame_length`, but we're assuming the array has already been zero-padded. + * @param {number} frame_length The length of the analysis frames in samples (a.k.a., `fft_length`). + * @param {number} hop_length The stride between successive analysis frames in samples. + * @param {Object} options + * @param {number} [options.fft_length=null] The size of the FFT buffer in samples. This determines how many frequency bins the spectrogram will have. + * For optimal speed, this should be a power of two. If `null`, uses `frame_length`. + * @param {number} [options.power=1.0] If 1.0, returns the amplitude spectrogram. If 2.0, returns the power spectrogram. If `null`, returns complex numbers. + * @param {boolean} [options.center=true] Whether to pad the waveform so that frame `t` is centered around time `t * hop_length`. If `false`, frame + * `t` will start at time `t * hop_length`. + * @param {string} [options.pad_mode="reflect"] Padding mode used when `center` is `true`. Possible values are: `"constant"` (pad with zeros), + * `"edge"` (pad with edge values), `"reflect"` (pads with mirrored values). + * @param {boolean} [options.onesided=true] If `true`, only computes the positive frequencies and returns a spectrogram containing `fft_length // 2 + 1` + * frequency bins. If `false`, also computes the negative frequencies and returns `fft_length` frequency bins. + * @param {number} [options.preemphasis=null] Coefficient for a low-pass filter that applies pre-emphasis before the DFT. + * @param {number[][]} [options.mel_filters=null] The mel filter bank of shape `(num_freq_bins, num_mel_filters)`. + * If supplied, applies this filter bank to create a mel spectrogram. + * @param {number} [options.mel_floor=1e-10] Minimum value of mel frequency banks. + * @param {string} [options.log_mel=null] How to convert the spectrogram to log scale. Possible options are: + * `null` (don't convert), `"log"` (take the natural logarithm) `"log10"` (take the base-10 logarithm), `"dB"` (convert to decibels). + * Can only be used when `power` is not `null`. + * @param {number} [options.reference=1.0] Sets the input spectrogram value that corresponds to 0 dB. For example, use `max(spectrogram)[0]` to set + * the loudest part to 0 dB. Must be greater than zero. + * @param {number} [options.min_value=1e-10] The spectrogram will be clipped to this minimum value before conversion to decibels, to avoid taking `log(0)`. + * For a power spectrogram, the default of `1e-10` corresponds to a minimum of -100 dB. For an amplitude spectrogram, the value `1e-5` corresponds to -100 dB. + * Must be greater than zero. + * @param {number} [options.db_range=null] Sets the maximum dynamic range in decibels. For example, if `db_range = 80`, the difference between the + * peak value and the smallest value will never be more than 80 dB. Must be greater than zero. + * @param {boolean} [options.remove_dc_offset=null] Subtract mean from waveform on each frame, applied before pre-emphasis. This should be set to `true` in + * order to get the same results as `torchaudio.compliance.kaldi.fbank` when computing mel filters. + * @param {number} [options.max_num_frames=null] If provided, limits the number of frames to compute to this value. + * @param {number} [options.min_num_frames=null] If provided, ensures the number of frames to compute is at least this value. + * @param {boolean} [options.do_pad=true] If `true`, pads the output spectrogram to have `max_num_frames` frames. + * @param {boolean} [options.transpose=false] If `true`, the returned spectrogram will have shape `(num_frames, num_frequency_bins/num_mel_filters)`. If `false`, the returned spectrogram will have shape `(num_frequency_bins/num_mel_filters, num_frames)`. + * @returns {Promise} Spectrogram of shape `(num_frequency_bins, length)` (regular spectrogram) or shape `(num_mel_filters, length)` (mel spectrogram). + */ +async function spectrogram( + waveform, + window, + frame_length, + hop_length, + { + fft_length = null, + power = 1.0, + center = true, + pad_mode = "reflect", + onesided = true, + preemphasis = null, + mel_filters = null, + mel_floor = 1e-10, + log_mel = null, + reference = 1.0, + min_value = 1e-10, + db_range = null, + remove_dc_offset = null, + + // Custom parameters for efficiency reasons + min_num_frames = null, + max_num_frames = null, + do_pad = true, + transpose = false, + } = {} +) { + const window_length = window.length; + if (fft_length === null) { + fft_length = frame_length; + } + if (frame_length > fft_length) { + throw Error(`frame_length (${frame_length}) may not be larger than fft_length (${fft_length})`) + } + + if (window_length !== frame_length) { + throw new Error(`Length of the window (${window_length}) must equal frame_length (${frame_length})`); + } + + if (hop_length <= 0) { + throw new Error("hop_length must be greater than zero"); + } + + if (power === null && mel_filters !== null) { + throw new Error( + "You have provided `mel_filters` but `power` is `None`. Mel spectrogram computation is not yet supported for complex-valued spectrogram. " + + "Specify `power` to fix this issue." + ); + } + + if (center) { + if (pad_mode !== 'reflect') { + throw new Error(`pad_mode="${pad_mode}" not implemented yet.`) + } + const half_window = Math.floor((fft_length - 1) / 2) + 1; + waveform = padReflect(waveform, half_window, half_window); + } + + // split waveform into frames of frame_length size + let num_frames = Math.floor(1 + Math.floor((waveform.length - frame_length) / hop_length)) + if (min_num_frames !== null && num_frames < min_num_frames) { + num_frames = min_num_frames + } + const num_frequency_bins = onesided ? Math.floor(fft_length / 2) + 1 : fft_length + + let d1 = num_frames; + let d1Max = num_frames; + + // If maximum number of frames is provided, we must either pad or truncate + if (max_num_frames !== null) { + if (max_num_frames > num_frames) { // input is too short, so we pad + if (do_pad) { + d1Max = max_num_frames; + } + } else { // input is too long, so we truncate + d1Max = d1 = max_num_frames; + } + } + + // Preallocate arrays to store output. + const fft = new _maths_js__WEBPACK_IMPORTED_MODULE_1__.FFT(fft_length); + const inputBuffer = new Float64Array(fft_length); + const outputBuffer = new Float64Array(fft.outputBufferSize); + const transposedMagnitudeData = new Float32Array(num_frequency_bins * d1Max); + + for (let i = 0; i < d1; ++i) { + // Populate buffer with waveform data + const offset = i * hop_length; + const buffer_size = Math.min(waveform.length - offset, frame_length); + if (buffer_size !== frame_length) { + // The full buffer is not needed, so we need to reset it (avoid overflow from previous iterations) + // NOTE: We don't need to reset the buffer if it's full since we overwrite the first + // `frame_length` values and the rest (`fft_length - frame_length`) remains zero. + inputBuffer.fill(0, 0, frame_length); + } + + for (let j = 0; j < buffer_size; ++j) { + inputBuffer[j] = waveform[offset + j]; + } + + if (remove_dc_offset) { + let sum = 0; + for (let j = 0; j < buffer_size; ++j) { + sum += inputBuffer[j]; + } + const mean = sum / buffer_size; + for (let j = 0; j < buffer_size; ++j) { + inputBuffer[j] -= mean; + } + } + + if (preemphasis !== null) { + // Done in reverse to avoid copies and distructive modification + for (let j = buffer_size - 1; j >= 1; --j) { + inputBuffer[j] -= preemphasis * inputBuffer[j - 1]; + } + inputBuffer[0] *= 1 - preemphasis; + } + + // Apply window function + for (let j = 0; j < window.length; ++j) { + inputBuffer[j] *= window[j]; + } + + fft.realTransform(outputBuffer, inputBuffer); + + // compute magnitudes + for (let j = 0; j < num_frequency_bins; ++j) { + const j2 = j << 1; + + // NOTE: We transpose the data here to avoid doing it later + transposedMagnitudeData[j * d1Max + i] = outputBuffer[j2] ** 2 + outputBuffer[j2 + 1] ** 2; + } + } + + if (power !== null && power !== 2) { + // slight optimization to not sqrt + const pow = 2 / power; // we use 2 since we already squared + for (let i = 0; i < transposedMagnitudeData.length; ++i) { + transposedMagnitudeData[i] **= pow; + } + } + + // TODO: What if `mel_filters` is null? + const num_mel_filters = mel_filters.length; + + // Perform matrix muliplication: + // mel_spec = mel_filters @ magnitudes.T + // - mel_filters.shape=(80, 201) + // - magnitudes.shape=(3000, 201) => magnitudes.T.shape=(201, 3000) + // - mel_spec.shape=(80, 3000) + let mel_spec = await (0,_tensor_js__WEBPACK_IMPORTED_MODULE_5__.matmul)( + // TODO: Make `mel_filters` a Tensor during initialization + new _tensor_js__WEBPACK_IMPORTED_MODULE_5__.Tensor('float32', mel_filters.flat(), [num_mel_filters, num_frequency_bins]), + new _tensor_js__WEBPACK_IMPORTED_MODULE_5__.Tensor('float32', transposedMagnitudeData, [num_frequency_bins, d1Max]), + ); + if (transpose) { + mel_spec = mel_spec.transpose(1, 0); + } + + const mel_spec_data = /** @type {Float32Array} */(mel_spec.data); + for (let i = 0; i < mel_spec_data.length; ++i) { + mel_spec_data[i] = Math.max(mel_floor, mel_spec_data[i]); + } + + if (power !== null && log_mel !== null) { + const o = Math.min(mel_spec_data.length, d1 * num_mel_filters); + // NOTE: operates in-place + switch (log_mel) { + case 'log': + for (let i = 0; i < o; ++i) { + mel_spec_data[i] = Math.log(mel_spec_data[i]); + } + break; + case 'log10': + for (let i = 0; i < o; ++i) { + mel_spec_data[i] = Math.log10(mel_spec_data[i]); + } + break; + case 'dB': + if (power === 1.0) { + amplitude_to_db(mel_spec_data, reference, min_value, db_range); + } else if (power === 2.0) { + power_to_db(mel_spec_data, reference, min_value, db_range); + } else { + throw new Error(`Cannot use log_mel option '${log_mel}' with power ${power}`) + } + break; + default: + throw new Error(`log_mel must be one of null, 'log', 'log10' or 'dB'. Got '${log_mel}'`); + } + } + + return mel_spec; +} + +/** + * Returns an array containing the specified window. + * @param {number} window_length The length of the window in samples. + * @param {string} name The name of the window function. + * @param {Object} options Additional options. + * @param {boolean} [options.periodic=true] Whether the window is periodic or symmetric. + * @param {number} [options.frame_length=null] The length of the analysis frames in samples. + * Provide a value for `frame_length` if the window is smaller than the frame length, so that it will be zero-padded. + * @param {boolean} [options.center=true] Whether to center the window inside the FFT buffer. Only used when `frame_length` is provided. + * @returns {Float64Array} The window of shape `(window_length,)` or `(frame_length,)`. + */ +function window_function(window_length, name, { + periodic = true, + frame_length = null, + center = true, +} = {}) { + const length = periodic ? window_length + 1 : window_length; + let window; + switch (name) { + case 'boxcar': + window = new Float64Array(length).fill(1.0); + break; + case 'hann': + case 'hann_window': + window = hanning(length); + break; + case 'hamming': + window = hamming(length); + break; + case 'povey': + window = hanning(length).map(x => Math.pow(x, 0.85)); + break; + default: + throw new Error(`Unknown window type ${name}.`); + } + if (periodic) { + window = window.subarray(0, window_length); + } + if (frame_length === null) { + return window; + } + if (window_length > frame_length) { + throw new Error(`Length of the window (${window_length}) may not be larger than frame_length (${frame_length})`); + } + + return window; +} + +/** + * Encode audio data to a WAV file. + * WAV file specs : https://en.wikipedia.org/wiki/WAV#WAV_File_header + * + * Adapted from https://www.npmjs.com/package/audiobuffer-to-wav + * @param {Float32Array} samples The audio samples. + * @param {number} rate The sample rate. + * @returns {ArrayBuffer} The WAV audio buffer. + */ +function encodeWAV(samples, rate) { + let offset = 44; + const buffer = new ArrayBuffer(offset + samples.length * 4); + const view = new DataView(buffer); + + /* RIFF identifier */ + writeString(view, 0, "RIFF"); + /* RIFF chunk length */ + view.setUint32(4, 36 + samples.length * 4, true); + /* RIFF type */ + writeString(view, 8, "WAVE"); + /* format chunk identifier */ + writeString(view, 12, "fmt "); + /* format chunk length */ + view.setUint32(16, 16, true); + /* sample format (raw) */ + view.setUint16(20, 3, true); + /* channel count */ + view.setUint16(22, 1, true); + /* sample rate */ + view.setUint32(24, rate, true); + /* byte rate (sample rate * block align) */ + view.setUint32(28, rate * 4, true); + /* block align (channel count * bytes per sample) */ + view.setUint16(32, 4, true); + /* bits per sample */ + view.setUint16(34, 32, true); + /* data chunk identifier */ + writeString(view, 36, "data"); + /* data chunk length */ + view.setUint32(40, samples.length * 4, true); + + for (let i = 0; i < samples.length; ++i, offset += 4) { + view.setFloat32(offset, samples[i], true); + } + + return buffer; +} + +function writeString(view, offset, string) { + for (let i = 0; i < string.length; ++i) { + view.setUint8(offset + i, string.charCodeAt(i)); + } +} + + +class RawAudio { + + /** + * Create a new `RawAudio` object. + * @param {Float32Array} audio Audio data + * @param {number} sampling_rate Sampling rate of the audio data + */ + constructor(audio, sampling_rate) { + this.audio = audio + this.sampling_rate = sampling_rate + } + + /** + * Convert the audio to a wav file buffer. + * @returns {ArrayBuffer} The WAV file. + */ + toWav() { + return encodeWAV(this.audio, this.sampling_rate) + } + + /** + * Convert the audio to a blob. + * @returns {Blob} + */ + toBlob() { + const wav = this.toWav(); + const blob = new Blob([wav], { type: 'audio/wav' }); + return blob; + } + + /** + * Save the audio to a wav file. + * @param {string} path + */ + async save(path) { + let fn; + + if (_env_js__WEBPACK_IMPORTED_MODULE_3__.apis.IS_BROWSER_ENV) { + if (_env_js__WEBPACK_IMPORTED_MODULE_3__.apis.IS_WEBWORKER_ENV) { + throw new Error('Unable to save a file from a Web Worker.') + } + fn = _core_js__WEBPACK_IMPORTED_MODULE_2__.saveBlob; + } else if (_env_js__WEBPACK_IMPORTED_MODULE_3__.apis.IS_FS_AVAILABLE) { + fn = async (/** @type {string} */ path, /** @type {Blob} */ blob) => { + let buffer = await blob.arrayBuffer(); + fs__WEBPACK_IMPORTED_MODULE_4__.writeFileSync(path, Buffer.from(buffer)); + } + } else { + throw new Error('Unable to save because filesystem is disabled in this environment.') + } + + await fn(path, this.toBlob()) + } +} + + +/***/ }), + +/***/ "./src/utils/constants.js": +/*!********************************!*\ + !*** ./src/utils/constants.js ***! + \********************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ CHAT_TEMPLATE_NAME: () => (/* binding */ CHAT_TEMPLATE_NAME), +/* harmony export */ CONFIG_NAME: () => (/* binding */ CONFIG_NAME), +/* harmony export */ FEATURE_EXTRACTOR_NAME: () => (/* binding */ FEATURE_EXTRACTOR_NAME), +/* harmony export */ GENERATION_CONFIG_NAME: () => (/* binding */ GENERATION_CONFIG_NAME), +/* harmony export */ GITHUB_ISSUE_URL: () => (/* binding */ GITHUB_ISSUE_URL), +/* harmony export */ IMAGE_PROCESSOR_NAME: () => (/* binding */ IMAGE_PROCESSOR_NAME), +/* harmony export */ PROCESSOR_NAME: () => (/* binding */ PROCESSOR_NAME) +/* harmony export */ }); + +const GITHUB_ISSUE_URL = 'https://github.com/huggingface/transformers.js/issues/new/choose'; + +const CONFIG_NAME = "config.json" +const FEATURE_EXTRACTOR_NAME = "preprocessor_config.json" +const IMAGE_PROCESSOR_NAME = FEATURE_EXTRACTOR_NAME +const PROCESSOR_NAME = "processor_config.json" +const CHAT_TEMPLATE_NAME = "chat_template.json" +const GENERATION_CONFIG_NAME = "generation_config.json" + + +/***/ }), + +/***/ "./src/utils/core.js": +/*!***************************!*\ + !*** ./src/utils/core.js ***! + \***************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ calculateDimensions: () => (/* binding */ calculateDimensions), +/* harmony export */ calculateReflectOffset: () => (/* binding */ calculateReflectOffset), +/* harmony export */ count: () => (/* binding */ count), +/* harmony export */ dispatchCallback: () => (/* binding */ dispatchCallback), +/* harmony export */ escapeRegExp: () => (/* binding */ escapeRegExp), +/* harmony export */ isIntegralNumber: () => (/* binding */ isIntegralNumber), +/* harmony export */ isNullishDimension: () => (/* binding */ isNullishDimension), +/* harmony export */ isTypedArray: () => (/* binding */ isTypedArray), +/* harmony export */ len: () => (/* binding */ len), +/* harmony export */ mergeArrays: () => (/* binding */ mergeArrays), +/* harmony export */ pick: () => (/* binding */ pick), +/* harmony export */ pop: () => (/* binding */ pop), +/* harmony export */ product: () => (/* binding */ product), +/* harmony export */ reverseDictionary: () => (/* binding */ reverseDictionary), +/* harmony export */ saveBlob: () => (/* binding */ saveBlob) +/* harmony export */ }); + +/** + * @file Core utility functions/classes for Transformers.js. + * + * These are only used internally, meaning an end-user shouldn't + * need to access anything here. + * + * @module utils/core + */ + +/** + * @typedef {Object} InitiateProgressInfo + * @property {'initiate'} status + * @property {string} name The model id or directory path. + * @property {string} file The name of the file. + */ + +/** + * @typedef {Object} DownloadProgressInfo + * @property {'download'} status + * @property {string} name The model id or directory path. + * @property {string} file The name of the file. + */ + +/** + * @typedef {Object} ProgressStatusInfo + * @property {'progress'} status + * @property {string} name The model id or directory path. + * @property {string} file The name of the file. + * @property {number} progress A number between 0 and 100. + * @property {number} loaded The number of bytes loaded. + * @property {number} total The total number of bytes to be loaded. + */ + +/** + * @typedef {Object} DoneProgressInfo + * @property {'done'} status + * @property {string} name The model id or directory path. + * @property {string} file The name of the file. + */ + +/** + * @typedef {Object} ReadyProgressInfo + * @property {'ready'} status + * @property {string} task The loaded task. + * @property {string} model The loaded model. + */ + +/** + * @typedef {InitiateProgressInfo | DownloadProgressInfo | ProgressStatusInfo | DoneProgressInfo | ReadyProgressInfo} ProgressInfo + */ + +/** + * A callback function that is called with progress information. + * @callback ProgressCallback + * @param {ProgressInfo} progressInfo + * @returns {void} + */ + +/** + * Helper function to dispatch progress callbacks. + * + * @param {ProgressCallback | null | undefined} progress_callback The progress callback function to dispatch. + * @param {ProgressInfo} data The data to pass to the progress callback function. + * @returns {void} + * @private + */ +function dispatchCallback(progress_callback, data) { + if (progress_callback) progress_callback(data); +} + +/** + * Reverses the keys and values of an object. + * + * @param {Object} data The object to reverse. + * @returns {Object} The reversed object. + * @see https://ultimatecourses.com/blog/reverse-object-keys-and-values-in-javascript + */ +function reverseDictionary(data) { + // https://ultimatecourses.com/blog/reverse-object-keys-and-values-in-javascript + return Object.fromEntries(Object.entries(data).map(([key, value]) => [value, key])); +} + +/** + * Escapes regular expression special characters from a string by replacing them with their escaped counterparts. + * + * @param {string} string The string to escape. + * @returns {string} The escaped string. + */ +function escapeRegExp(string) { + return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string +} + +/** + * Check if a value is a typed array. + * @param {*} val The value to check. + * @returns {boolean} True if the value is a `TypedArray`, false otherwise. + * + * Adapted from https://stackoverflow.com/a/71091338/13989043 + */ +function isTypedArray(val) { + return val?.prototype?.__proto__?.constructor?.name === 'TypedArray'; +} + + +/** + * Check if a value is an integer. + * @param {*} x The value to check. + * @returns {boolean} True if the value is a string, false otherwise. + */ +function isIntegralNumber(x) { + return Number.isInteger(x) || typeof x === 'bigint' +} + +/** + * Determine if a provided width or height is nullish. + * @param {*} x The value to check. + * @returns {boolean} True if the value is `null`, `undefined` or `-1`, false otherwise. + */ +function isNullishDimension(x) { + return x === null || x === undefined || x === -1; +} + +/** + * Calculates the dimensions of a nested array. + * + * @param {any[]} arr The nested array to calculate dimensions for. + * @returns {number[]} An array containing the dimensions of the input array. + */ +function calculateDimensions(arr) { + const dimensions = []; + let current = arr; + while (Array.isArray(current)) { + dimensions.push(current.length); + current = current[0]; + } + return dimensions; +} + +/** + * Replicate python's .pop() method for objects. + * @param {Object} obj The object to pop from. + * @param {string} key The key to pop. + * @param {*} defaultValue The default value to return if the key does not exist. + * @returns {*} The value of the popped key. + * @throws {Error} If the key does not exist and no default value is provided. + */ +function pop(obj, key, defaultValue = undefined) { + const value = obj[key]; + if (value !== undefined) { + delete obj[key]; + return value; + } + if (defaultValue === undefined) { + throw Error(`Key ${key} does not exist in object.`) + } + return defaultValue; +} + +/** + * Efficiently merge arrays, creating a new copy. + * Adapted from https://stackoverflow.com/a/6768642/13989043 + * @param {Array[]} arrs Arrays to merge. + * @returns {Array} The merged array. + */ +function mergeArrays(...arrs) { + return Array.prototype.concat.apply([], arrs); +} + +/** + * Compute the Cartesian product of given arrays + * @param {...Array} a Arrays to compute the product + * @returns {Array} Returns the computed Cartesian product as an array + * @private + */ +function product(...a) { + // Cartesian product of items + // Adapted from https://stackoverflow.com/a/43053803 + return a.reduce((a, b) => a.flatMap(d => b.map(e => [d, e]))); +} + +/** + * Calculates the index offset for a given index and window size. + * @param {number} i The index. + * @param {number} w The window size. + * @returns {number} The index offset. + */ +function calculateReflectOffset(i, w) { + return Math.abs((i + w) % (2 * w) - w); +} + +/** + * Save blob file on the web. + * @param {string} path The path to save the blob to + * @param {Blob} blob The blob to save + */ +function saveBlob(path, blob){ + // Convert the canvas content to a data URL + const dataURL = URL.createObjectURL(blob); + + // Create an anchor element with the data URL as the href attribute + const downloadLink = document.createElement('a'); + downloadLink.href = dataURL; + + // Set the download attribute to specify the desired filename for the downloaded image + downloadLink.download = path; + + // Trigger the download + downloadLink.click(); + + // Clean up: remove the anchor element from the DOM + downloadLink.remove(); + + // Revoke the Object URL to free up memory + URL.revokeObjectURL(dataURL); +} + +/** + * + * @param {Object} o + * @param {string[]} props + * @returns {Object} + */ +function pick(o, props) { + return Object.assign( + {}, + ...props.map((prop) => { + if (o[prop] !== undefined) { + return { [prop]: o[prop] }; + } + }) + ); +} + +/** + * Calculate the length of a string, taking multi-byte characters into account. + * This mimics the behavior of Python's `len` function. + * @param {string} s The string to calculate the length of. + * @returns {number} The length of the string. + */ +function len(s) { + let length = 0; + for (const c of s) ++length; + return length; +} + +/** + * Count the occurrences of a value in an array or string. + * This mimics the behavior of Python's `count` method. + * @param {any[]|string} arr The array or string to search. + * @param {any} value The value to count. + */ +function count(arr, value) { + let count = 0; + for (const v of arr) { + if (v === value) ++count; + } + return count; +} + + +/***/ }), + +/***/ "./src/utils/data-structures.js": +/*!**************************************!*\ + !*** ./src/utils/data-structures.js ***! + \**************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ CharTrie: () => (/* binding */ CharTrie), +/* harmony export */ DictionarySplitter: () => (/* binding */ DictionarySplitter), +/* harmony export */ LRUCache: () => (/* binding */ LRUCache), +/* harmony export */ PriorityQueue: () => (/* binding */ PriorityQueue), +/* harmony export */ TokenLattice: () => (/* binding */ TokenLattice) +/* harmony export */ }); + +/** + * @file Custom data structures. + * + * These are only used internally, meaning an end-user shouldn't + * need to access anything here. + * + * @module utils/data-structures + */ + + +/** + * Efficient Heap-based Implementation of a Priority Queue. + * It uses an array-based binary heap, where the root is at index `0`, and the + * children of node `i` are located at indices `2i + 1` and `2i + 2`, respectively. + * + * Adapted from the following sources: + * - https://stackoverflow.com/a/42919752/13989043 (original) + * - https://github.com/belladoreai/llama-tokenizer-js (minor improvements) + */ +class PriorityQueue { + + /** + * Create a new PriorityQueue. + * @param {function(any, any): boolean} comparator Comparator function to determine priority. Defaults to a MaxHeap. + */ + constructor(comparator = (a, b) => a > b, maxSize = Infinity) { + this._heap = []; + this._comparator = comparator; + this._maxSize = maxSize; + } + + /** + * The size of the queue + */ + get size() { + return this._heap.length; + } + + /** + * Check if the queue is empty. + * @returns {boolean} `true` if the queue is empty, `false` otherwise. + */ + isEmpty() { + return this.size === 0; + } + + /** + * Return the element with the highest priority in the queue. + * @returns {any} The highest priority element in the queue. + */ + peek() { + return this._heap[0]; + } + + /** + * Add one or more elements to the queue. + * @param {...any} values The values to push into the queue. + * @returns {number} The new size of the queue. + */ + push(...values) { + return this.extend(values); + } + + /** + * Add multiple elements to the queue. + * @param {any[]} values The values to push into the queue. + * @returns {number} The new size of the queue. + */ + extend(values) { + for (const value of values) { + if (this.size < this._maxSize) { + this._heap.push(value); + this._siftUp(); + } else { + // Get index of value with the lowest priority + const smallest = this._smallest(); + + // If the new value has higher priority than the smallest value in the heap + // then replace the smallest value with the new value and update the heap + if (this._comparator(value, this._heap[smallest])) { + this._heap[smallest] = value; + this._siftUpFrom(smallest); + } + } + } + return this.size; + } + + /** + * Remove and return the element with the highest priority in the queue. + * @returns {any} The element with the highest priority in the queue. + */ + pop() { + const poppedValue = this.peek(); + const bottom = this.size - 1; + if (bottom > 0) { + this._swap(0, bottom); + } + this._heap.pop(); + this._siftDown(); + return poppedValue; + } + + /** + * Replace the element with the highest priority in the queue with a new value. + * @param {*} value The new value. + * @returns {*} The replaced value. + */ + replace(value) { + const replacedValue = this.peek(); + this._heap[0] = value; + this._siftDown(); + return replacedValue; + } + + /** + * Compute the index for the parent of the node at index `i`. + * @param {number} i The index of the node to get the parent of. + * @returns {number} The index of the parent node. + * @private + */ + _parent(i) { + return ((i + 1) >>> 1) - 1; + } + + /** + * Compute the index for the left child of the node at index `i`. + * @param {number} i The index of the node to get the left child of. + * @returns {number} The index of the left child. + * @private + */ + _left(i) { + return (i << 1) + 1; + } + + /** + * Compute the index for the right child of the node at index `i`. + * @param {number} i The index of the node to get the right child of. + * @returns {number} The index of the right child. + * @private + */ + _right(i) { + return (i + 1) << 1; + } + + /** + * Check if the element at index `i` is greater than the element at index `j`. + * @param {number} i The index of the first element to compare. + * @param {number} j The index of the second element to compare. + * @returns {boolean} `true` if the element at index `i` is greater than the element at index `j`, `false` otherwise. + * @private + */ + _greater(i, j) { + return this._comparator(this._heap[i], this._heap[j]); + } + + /** + * Swap the elements at indices `i` and `j`. + * @param {number} i The index of the first element to swap. + * @param {number} j The index of the second element to swap. + * @private + */ + _swap(i, j) { + const temp = this._heap[i]; + this._heap[i] = this._heap[j]; + this._heap[j] = temp; + } + + /** + * Maintain the heap property by updating positions in the heap, + * starting at the last element and moving up the heap. + * @private + */ + _siftUp() { + this._siftUpFrom(this.size - 1); + } + + /** + * Helper function to sift up from a given node. + * @param {number} node The index of the node to start sifting up from. + */ + _siftUpFrom(node) { + while (node > 0 && this._greater(node, this._parent(node))) { + this._swap(node, this._parent(node)); + node = this._parent(node); + } + } + + /** + * Maintain the heap property by updating positions in the heap, + * starting at the first element and moving down the heap. + * @private + */ + _siftDown() { + let node = 0; + while ( + (this._left(node) < this.size && this._greater(this._left(node), node)) || + (this._right(node) < this.size && this._greater(this._right(node), node)) + ) { + const maxChild = (this._right(node) < this.size && this._greater(this._right(node), this._left(node))) + ? this._right(node) + : this._left(node); + this._swap(node, maxChild); + node = maxChild; + } + } + + /** + * Get the index of the smallest element in the heap. Since we use an array-based heap, + * the index can be computed without needing to traverse the heap. + * @private + */ + _smallest() { + return (2 ** (Math.floor(Math.log2(this.size))) - 1); + } +} + +/** + * A trie structure to efficiently store and search for strings. + */ +class CharTrie { + constructor() { + this.root = CharTrieNode.default(); + } + + /** + * Adds one or more `texts` to the trie. + * @param {string[]} texts The strings to add to the trie. + */ + extend(texts) { + for (const text of texts) { + this.push(text); + } + } + + /** + * Adds text to the trie. + * @param {string} text The string to add to the trie. + */ + push(text) { + let node = this.root; + for (const ch of text) { + let child = node.children.get(ch); + if (child === undefined) { + child = CharTrieNode.default(); + node.children.set(ch, child); + } + node = child; + } + node.isLeaf = true; + } + + /** + * Searches the trie for all strings with a common prefix of `text`. + * @param {string} text The common prefix to search for. + * @yields {string} Each string in the trie that has `text` as a prefix. + */ + *commonPrefixSearch(text) { + let node = this.root; + if (node === undefined) return; + + let prefix = ""; + for (const ch of text) { + prefix += ch; + node = node.children.get(ch); + if (node === undefined) return; + if (node.isLeaf) { + yield prefix; + } + } + } +} + +/** + * Represents a node in a character trie. + */ +class CharTrieNode { + /** + * Create a new CharTrieNode. + * @param {boolean} isLeaf Whether the node is a leaf node or not. + * @param {Map} children A map containing the node's children, where the key is a character and the value is a `CharTrieNode`. + */ + constructor(isLeaf, children) { + this.isLeaf = isLeaf; + this.children = children; + } + + /** + * Returns a new `CharTrieNode` instance with default values. + * @returns {CharTrieNode} A new `CharTrieNode` instance with `isLeaf` set to `false` and an empty `children` map. + */ + static default() { + return new CharTrieNode(false, new Map()); + } +} + +/** + * A lattice data structure to be used for tokenization. + */ +class TokenLattice { + /** + * Creates a new TokenLattice instance. + * + * @param {string} sentence The input sentence to be tokenized. + * @param {number} bosTokenId The beginning-of-sequence token ID. + * @param {number} eosTokenId The end-of-sequence token ID. + */ + constructor(sentence, bosTokenId, eosTokenId) { + this.chars = Array.from(sentence); + this.len = this.chars.length; + this.bosTokenId = bosTokenId; + this.eosTokenId = eosTokenId; + this.nodes = []; + this.beginNodes = Array.from({ length: this.len + 1 }, () => []); + this.endNodes = Array.from({ length: this.len + 1 }, () => []); + + const bos = new TokenLatticeNode(this.bosTokenId, 0, 0, 0, 0.0); + const eos = new TokenLatticeNode(this.eosTokenId, 1, this.len, 0, 0.0); + this.nodes.push(bos.clone()); + this.nodes.push(eos.clone()); + this.beginNodes[this.len].push(eos); + this.endNodes[0].push(bos); + } + + /** + * Inserts a new token node into the token lattice. + * + * @param {number} pos The starting position of the token. + * @param {number} length The length of the token. + * @param {number} score The score of the token. + * @param {number} tokenId The token ID of the token. + */ + insert(pos, length, score, tokenId) { + const nodeId = this.nodes.length; + const node = new TokenLatticeNode(tokenId, nodeId, pos, length, score); + this.beginNodes[pos].push(node); + this.endNodes[pos + length].push(node); + this.nodes.push(node); + } + + /** + * Implements the Viterbi algorithm to compute the most likely sequence of tokens. + * + * @returns {TokenLatticeNode[]} The most likely sequence of tokens. + */ + viterbi() { + const len = this.len; + let pos = 0; + while (pos <= len) { + if (this.beginNodes[pos].length == 0) { + return []; + } + for (let rnode of this.beginNodes[pos]) { + rnode.prev = null; + let bestScore = 0.0; + let bestNode = null; + for (let lnode of this.endNodes[pos]) { + const score = lnode.backtraceScore + rnode.score; + if (bestNode === null || score > bestScore) { + bestNode = lnode.clone(); + bestScore = score; + } + } + + if (bestNode !== null) { + rnode.prev = bestNode; + rnode.backtraceScore = bestScore; + } else { + return []; + } + } + ++pos; + } + + const results = []; + const root = this.beginNodes[len][0]; + const prev = root.prev; + if (prev === null) { + return []; + } + + let node = prev.clone(); + while (node.prev !== null) { + results.push(node.clone()); + const n = node.clone(); + node = n.prev.clone(); + } + + results.reverse(); + return results; + } + + /** + * @param {TokenLatticeNode} node + * @returns {string} The array of nodes representing the most likely sequence of tokens. + */ + piece(node) { + return this.chars.slice(node.pos, node.pos + node.length).join(''); + } + + /** + * @returns {string[]} The most likely sequence of tokens. + */ + tokens() { + const nodes = this.viterbi(); + return nodes.map(x => this.piece(x)); + } + + /** + * @returns {number[]} The most likely sequence of token ids. + */ + tokenIds() { + const nodes = this.viterbi(); + return nodes.map(x => x.tokenId); + } +} +class TokenLatticeNode { + /** + * Represents a node in a token lattice for a given sentence. + * @param {number} tokenId The ID of the token associated with this node. + * @param {number} nodeId The ID of this node. + * @param {number} pos The starting position of the token in the sentence. + * @param {number} length The length of the token. + * @param {number} score The score associated with the token. + */ + constructor(tokenId, nodeId, pos, length, score) { + this.tokenId = tokenId; + this.nodeId = nodeId; + this.pos = pos; + this.length = length; + this.score = score; + this.prev = null; + this.backtraceScore = 0.0; + } + + /** + * Returns a clone of this node. + * @returns {TokenLatticeNode} A clone of this node. + */ + clone() { + const n = new TokenLatticeNode(this.tokenId, this.nodeId, this.pos, this.length, this.score); + n.prev = this.prev; + n.backtraceScore = this.backtraceScore; + return n; + } +} + +/** + * A data structure which uses a trie to split a string into tokens based on a dictionary. + * It can also use a regular expression to preprocess the input text before splitting. + * + * NOTE: To ensure multi-byte characters are handled correctly, we operate at byte-level instead of character-level. + */ +class DictionarySplitter { + /** + * @param {string[]} dictionary The dictionary of words to use for splitting. + */ + constructor(dictionary) { + this.trie = this._buildTrie(dictionary); + } + + /** + * Builds a trie from the given dictionary. + * @param {string[]} dictionary The dictionary of words to build the trie from. + * @returns {Object} The root node of the trie. + * @private + */ + _buildTrie(dictionary) { + const trie = Object.create(null); + for (const word of dictionary) { + let node = trie; + for (let i = 0; i < word.length; ++i) { + node = (node[word[i]] ??= Object.create(null)); + } + node.end = word; + } + return trie; + } + + /** + * Splits the input text into tokens based on the dictionary. + * @param {string} text The input text to split. + * @returns {string[]} An array of tokens. + */ + split(text) { + const result = []; + const n = text.length; + let start = 0; + let i = 0; + + while (i < n) { + let node = this.trie; + let match = null; + let j = i; + + while (j < n && (node = node[text[j]])) { + if (node.end) { + // Always keep the last (i.e., longest) match. + match = node.end; + } + ++j; + } + + if (match) { + if (i > start) { + result.push(text.slice(start, i)); + } + result.push(match); + i += match.length; + start = i; + } else { + ++i; + } + } + if (start < n) { + result.push(text.slice(start)); + } + return result; + } +} + +/** +* A simple Least Recently Used (LRU) cache implementation in JavaScript. +* This cache stores key-value pairs and evicts the least recently used item +* when the capacity is exceeded. +*/ +class LRUCache { + /** + * Creates an LRUCache instance. + * @param {number} capacity The maximum number of items the cache can hold. + */ + constructor(capacity) { + this.capacity = capacity; + this.cache = new Map(); + } + + /** + * Retrieves the value associated with the given key and marks the key as recently used. + * @param {any} key The key to retrieve. + * @returns {any} The value associated with the key, or undefined if the key does not exist. + */ + get(key) { + if (!this.cache.has(key)) return undefined; + const value = this.cache.get(key); + this.cache.delete(key); + this.cache.set(key, value); + return value; + } + + /** + * Inserts or updates the key-value pair in the cache. + * If the key already exists, it is updated and marked as recently used. + * If the cache exceeds its capacity, the least recently used item is evicted. + * @param {any} key The key to add or update. + * @param {any} value The value to associate with the key. + */ + put(key, value) { + if (this.cache.has(key)) { + this.cache.delete(key); + } + this.cache.set(key, value); + if (this.cache.size > this.capacity) { + this.cache.delete(this.cache.keys().next().value); + } + } + + /** + * Clears the cache. + */ + clear() { + this.cache.clear(); + } +} + + +/***/ }), + +/***/ "./src/utils/devices.js": +/*!******************************!*\ + !*** ./src/utils/devices.js ***! + \******************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ DEVICE_TYPES: () => (/* binding */ DEVICE_TYPES) +/* harmony export */ }); + +/** + * The list of devices supported by Transformers.js + */ +const DEVICE_TYPES = Object.freeze({ + auto: 'auto', // Auto-detect based on device and environment + gpu: 'gpu', // Auto-detect GPU + cpu: 'cpu', // CPU + wasm: 'wasm', // WebAssembly + webgpu: 'webgpu', // WebGPU + cuda: 'cuda', // CUDA + dml: 'dml', // DirectML + + webnn: 'webnn', // WebNN (default) + 'webnn-npu': 'webnn-npu', // WebNN NPU + 'webnn-gpu': 'webnn-gpu', // WebNN GPU + 'webnn-cpu': 'webnn-cpu', // WebNN CPU +}); + +/** + * @typedef {keyof typeof DEVICE_TYPES} DeviceType + */ + + +/***/ }), + +/***/ "./src/utils/dtypes.js": +/*!*****************************!*\ + !*** ./src/utils/dtypes.js ***! + \*****************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ DATA_TYPES: () => (/* binding */ DATA_TYPES), +/* harmony export */ DEFAULT_DEVICE_DTYPE_MAPPING: () => (/* binding */ DEFAULT_DEVICE_DTYPE_MAPPING), +/* harmony export */ DEFAULT_DTYPE_SUFFIX_MAPPING: () => (/* binding */ DEFAULT_DTYPE_SUFFIX_MAPPING), +/* harmony export */ isWebGpuFp16Supported: () => (/* binding */ isWebGpuFp16Supported) +/* harmony export */ }); +/* harmony import */ var _env_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../env.js */ "./src/env.js"); +/* harmony import */ var _devices_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./devices.js */ "./src/utils/devices.js"); +/// + + + + + +// TODO: Use the adapter from `env.backends.onnx.webgpu.adapter` to check for `shader-f16` support, +// when available in https://github.com/microsoft/onnxruntime/pull/19940. +// For more information, see https://github.com/microsoft/onnxruntime/pull/19857#issuecomment-1999984753 + +/** + * Checks if WebGPU fp16 support is available in the current environment. + */ +const isWebGpuFp16Supported = (function () { + /** @type {boolean} */ + let cachedResult; + + return async function () { + if (cachedResult === undefined) { + if (!_env_js__WEBPACK_IMPORTED_MODULE_0__.apis.IS_WEBGPU_AVAILABLE) { + cachedResult = false; + } else { + try { + const adapter = await navigator.gpu.requestAdapter(); + cachedResult = adapter.features.has('shader-f16'); + } catch (e) { + cachedResult = false; + } + } + } + return cachedResult; + }; +})(); + +const DATA_TYPES = Object.freeze({ + auto: 'auto', // Auto-detect based on environment + fp32: 'fp32', + fp16: 'fp16', + q8: 'q8', + int8: 'int8', + uint8: 'uint8', + q4: 'q4', + bnb4: 'bnb4', + q4f16: 'q4f16', // fp16 model with int4 block weight quantization +}); +/** @typedef {keyof typeof DATA_TYPES} DataType */ + +const DEFAULT_DEVICE_DTYPE_MAPPING = Object.freeze({ + // NOTE: If not specified, will default to fp32 + [_devices_js__WEBPACK_IMPORTED_MODULE_1__.DEVICE_TYPES.wasm]: DATA_TYPES.q8, +}); + +/** @type {Record, string>} */ +const DEFAULT_DTYPE_SUFFIX_MAPPING = Object.freeze({ + [DATA_TYPES.fp32]: '', + [DATA_TYPES.fp16]: '_fp16', + [DATA_TYPES.int8]: '_int8', + [DATA_TYPES.uint8]: '_uint8', + [DATA_TYPES.q8]: '_quantized', + [DATA_TYPES.q4]: '_q4', + [DATA_TYPES.q4f16]: '_q4f16', + [DATA_TYPES.bnb4]: '_bnb4', +}); + + +/***/ }), + +/***/ "./src/utils/generic.js": +/*!******************************!*\ + !*** ./src/utils/generic.js ***! + \******************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ Callable: () => (/* binding */ Callable) +/* harmony export */ }); + +/** + * A base class for creating callable objects. + * See [here](https://stackoverflow.com/q/76073890) for more information. + * + * @type {new () => {(...args: any[]): any, _call(...args: any[]): any}} + */ +const Callable = /** @type {any} */ (class { + /** + * Creates a new instance of the Callable class. + */ + constructor() { + /** + * Creates a closure that delegates to a private method '_call' with the given arguments. + * @type {any} + * @param {...any} args Zero or more arguments to pass to the '_call' method. + * @returns {*} The result of calling the '_call' method. + */ + let closure = function (...args) { + return closure._call(...args) + } + return Object.setPrototypeOf(closure, new.target.prototype) + } + + /** + * This method should be implemented in subclasses to provide the + * functionality of the callable object. + * + * @param {any[]} args + * @throws {Error} If the subclass does not implement the `_call` method. + */ + _call(...args) { + throw Error('Must implement _call method in subclass') + } +}); + + +/***/ }), + +/***/ "./src/utils/hub.js": +/*!**************************!*\ + !*** ./src/utils/hub.js ***! + \**************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ MAX_EXTERNAL_DATA_CHUNKS: () => (/* binding */ MAX_EXTERNAL_DATA_CHUNKS), +/* harmony export */ getFile: () => (/* binding */ getFile), +/* harmony export */ getModelFile: () => (/* binding */ getModelFile), +/* harmony export */ getModelJSON: () => (/* binding */ getModelJSON) +/* harmony export */ }); +/* harmony import */ var fs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! fs */ "?7a2c"); +/* harmony import */ var path__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! path */ "?a42a"); +/* harmony import */ var _env_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../env.js */ "./src/env.js"); +/* harmony import */ var _core_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./core.js */ "./src/utils/core.js"); + +/** + * @file Utility functions to interact with the Hugging Face Hub (https://huggingface.co/models) + * + * @module utils/hub + */ + + + + + + + +/** + * @typedef {boolean|number} ExternalData Whether to load the model using the external data format (used for models >= 2GB in size). + * If `true`, the model will be loaded using the external data format. + * If a number, this many chunks will be loaded using the external data format (of the form: "model.onnx_data[_{chunk_number}]"). + */ +const MAX_EXTERNAL_DATA_CHUNKS = 100; + +/** + * @typedef {Object} PretrainedOptions Options for loading a pretrained model. + * @property {import('./core.js').ProgressCallback} [progress_callback=null] If specified, this function will be called during model construction, to provide the user with progress updates. + * @property {import('../configs.js').PretrainedConfig} [config=null] Configuration for the model to use instead of an automatically loaded configuration. Configuration can be automatically loaded when: + * - The model is a model provided by the library (loaded with the *model id* string of a pretrained model). + * - The model is loaded by supplying a local directory as `pretrained_model_name_or_path` and a configuration JSON file named *config.json* is found in the directory. + * @property {string} [cache_dir=null] Path to a directory in which a downloaded pretrained model configuration should be cached if the standard cache should not be used. + * @property {boolean} [local_files_only=false] Whether or not to only look at local files (e.g., not try downloading the model). + * @property {string} [revision='main'] The specific model version to use. It can be a branch name, a tag name, or a commit id, + * since we use a git-based system for storing models and other artifacts on huggingface.co, so `revision` can be any identifier allowed by git. + * NOTE: This setting is ignored for local requests. + */ + +/** + * @typedef {Object} ModelSpecificPretrainedOptions Options for loading a pretrained model. + * @property {string} [subfolder='onnx'] In case the relevant files are located inside a subfolder of the model repo on huggingface.co, + * you can specify the folder name here. + * @property {string} [model_file_name=null] If specified, load the model with this name (excluding the .onnx suffix). Currently only valid for encoder- or decoder-only models. + * @property {import("./devices.js").DeviceType|Record} [device=null] The device to run the model on. If not specified, the device will be chosen from the environment settings. + * @property {import("./dtypes.js").DataType|Record} [dtype=null] The data type to use for the model. If not specified, the data type will be chosen from the environment settings. + * @property {ExternalData|Record} [use_external_data_format=false] Whether to load the model using the external data format (used for models >= 2GB in size). + * @property {import('onnxruntime-common').InferenceSession.SessionOptions} [session_options] (Optional) User-specified session options passed to the runtime. If not provided, suitable defaults will be chosen. + */ + +/** + * @typedef {PretrainedOptions & ModelSpecificPretrainedOptions} PretrainedModelOptions Options for loading a pretrained model. + */ + +/** + * Mapping from file extensions to MIME types. + */ +const CONTENT_TYPE_MAP = { + 'txt': 'text/plain', + 'html': 'text/html', + 'css': 'text/css', + 'js': 'text/javascript', + 'json': 'application/json', + 'png': 'image/png', + 'jpg': 'image/jpeg', + 'jpeg': 'image/jpeg', + 'gif': 'image/gif', +} +class FileResponse { + + /** + * Creates a new `FileResponse` object. + * @param {string} filePath + */ + constructor(filePath) { + this.filePath = filePath; + this.headers = new Headers(); + + this.exists = fs__WEBPACK_IMPORTED_MODULE_0__.existsSync(filePath); + if (this.exists) { + this.status = 200; + this.statusText = 'OK'; + + let stats = fs__WEBPACK_IMPORTED_MODULE_0__.statSync(filePath); + this.headers.set('content-length', stats.size.toString()); + + this.updateContentType(); + + const stream = fs__WEBPACK_IMPORTED_MODULE_0__.createReadStream(filePath); + this.body = new ReadableStream({ + start(controller) { + stream.on('data', (chunk) => controller.enqueue(chunk)); + stream.on('end', () => controller.close()); + stream.on('error', (err) => controller.error(err)); + }, + cancel() { + stream.destroy(); + } + }); + } else { + this.status = 404; + this.statusText = 'Not Found'; + this.body = null; + } + } + + /** + * Updates the 'content-type' header property of the response based on the extension of + * the file specified by the filePath property of the current object. + * @returns {void} + */ + updateContentType() { + // Set content-type header based on file extension + const extension = this.filePath.toString().split('.').pop().toLowerCase(); + this.headers.set('content-type', CONTENT_TYPE_MAP[extension] ?? 'application/octet-stream'); + } + + /** + * Clone the current FileResponse object. + * @returns {FileResponse} A new FileResponse object with the same properties as the current object. + */ + clone() { + let response = new FileResponse(this.filePath); + response.exists = this.exists; + response.status = this.status; + response.statusText = this.statusText; + response.headers = new Headers(this.headers); + return response; + } + + /** + * Reads the contents of the file specified by the filePath property and returns a Promise that + * resolves with an ArrayBuffer containing the file's contents. + * @returns {Promise} A Promise that resolves with an ArrayBuffer containing the file's contents. + * @throws {Error} If the file cannot be read. + */ + async arrayBuffer() { + const data = await fs__WEBPACK_IMPORTED_MODULE_0__.promises.readFile(this.filePath); + return /** @type {ArrayBuffer} */ (data.buffer); + } + + /** + * Reads the contents of the file specified by the filePath property and returns a Promise that + * resolves with a Blob containing the file's contents. + * @returns {Promise} A Promise that resolves with a Blob containing the file's contents. + * @throws {Error} If the file cannot be read. + */ + async blob() { + const data = await fs__WEBPACK_IMPORTED_MODULE_0__.promises.readFile(this.filePath); + return new Blob([data], { type: this.headers.get('content-type') }); + } + + /** + * Reads the contents of the file specified by the filePath property and returns a Promise that + * resolves with a string containing the file's contents. + * @returns {Promise} A Promise that resolves with a string containing the file's contents. + * @throws {Error} If the file cannot be read. + */ + async text() { + const data = await fs__WEBPACK_IMPORTED_MODULE_0__.promises.readFile(this.filePath, 'utf8'); + return data; + } + + /** + * Reads the contents of the file specified by the filePath property and returns a Promise that + * resolves with a parsed JavaScript object containing the file's contents. + * + * @returns {Promise} A Promise that resolves with a parsed JavaScript object containing the file's contents. + * @throws {Error} If the file cannot be read. + */ + async json() { + return JSON.parse(await this.text()); + } +} + +/** + * Determines whether the given string is a valid URL. + * @param {string|URL} string The string to test for validity as an URL. + * @param {string[]} [protocols=null] A list of valid protocols. If specified, the protocol must be in this list. + * @param {string[]} [validHosts=null] A list of valid hostnames. If specified, the URL's hostname must be in this list. + * @returns {boolean} True if the string is a valid URL, false otherwise. + */ +function isValidUrl(string, protocols = null, validHosts = null) { + let url; + try { + url = new URL(string); + } catch (_) { + return false; + } + if (protocols && !protocols.includes(url.protocol)) { + return false; + } + if (validHosts && !validHosts.includes(url.hostname)) { + return false; + } + return true; +} + +const REPO_ID_REGEX = /^(\b[\w\-.]+\b\/)?\b[\w\-.]{1,96}\b$/; + +/** + * Tests whether a string is a valid Hugging Face model ID or not. + * Adapted from https://github.com/huggingface/huggingface_hub/blob/6378820ebb03f071988a96c7f3268f5bdf8f9449/src/huggingface_hub/utils/_validators.py#L119-L170 + * + * @param {string} string The string to test + * @returns {boolean} True if the string is a valid model ID, false otherwise. + */ +function isValidHfModelId(string) { + if (!REPO_ID_REGEX.test(string)) return false; + if (string.includes("..") || string.includes("--")) return false; + if (string.endsWith(".git") || string.endsWith(".ipynb")) return false; + return true; +} + +/** + * Helper function to get a file, using either the Fetch API or FileSystem API. + * + * @param {URL|string} urlOrPath The URL/path of the file to get. + * @returns {Promise} A promise that resolves to a FileResponse object (if the file is retrieved using the FileSystem API), or a Response object (if the file is retrieved using the Fetch API). + */ +async function getFile(urlOrPath) { + + if (_env_js__WEBPACK_IMPORTED_MODULE_2__.env.useFS && !isValidUrl(urlOrPath, ["http:", "https:", "blob:"])) { + return new FileResponse( + urlOrPath instanceof URL + ? urlOrPath.protocol === "file:" + ? urlOrPath.pathname + : urlOrPath.toString() + : urlOrPath, + ); + } else if (typeof process !== 'undefined' && process?.release?.name === 'node') { + const IS_CI = !!process.env?.TESTING_REMOTELY; + const version = _env_js__WEBPACK_IMPORTED_MODULE_2__.env.version; + + const headers = new Headers(); + headers.set('User-Agent', `transformers.js/${version}; is_ci/${IS_CI};`); + + // Check whether we are making a request to the Hugging Face Hub. + const isHFURL = isValidUrl(urlOrPath, ['http:', 'https:'], ['huggingface.co', 'hf.co']); + if (isHFURL) { + // If an access token is present in the environment variables, + // we add it to the request headers. + // NOTE: We keep `HF_ACCESS_TOKEN` for backwards compatibility (as a fallback). + const token = process.env?.HF_TOKEN ?? process.env?.HF_ACCESS_TOKEN; + if (token) { + headers.set('Authorization', `Bearer ${token}`); + } + } + return fetch(urlOrPath, { headers }); + } else { + // Running in a browser-environment, so we use default headers + // NOTE: We do not allow passing authorization headers in the browser, + // since this would require exposing the token to the client. + return fetch(urlOrPath); + } +} + +const ERROR_MAPPING = { + // 4xx errors (https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#client_error_responses) + 400: 'Bad request error occurred while trying to load file', + 401: 'Unauthorized access to file', + 403: 'Forbidden access to file', + 404: 'Could not locate file', + 408: 'Request timeout error occurred while trying to load file', + + // 5xx errors (https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#server_error_responses) + 500: 'Internal server error error occurred while trying to load file', + 502: 'Bad gateway error occurred while trying to load file', + 503: 'Service unavailable error occurred while trying to load file', + 504: 'Gateway timeout error occurred while trying to load file', +} +/** + * Helper method to handle fatal errors that occur while trying to load a file from the Hugging Face Hub. + * @param {number} status The HTTP status code of the error. + * @param {string} remoteURL The URL of the file that could not be loaded. + * @param {boolean} fatal Whether to raise an error if the file could not be loaded. + * @returns {null} Returns `null` if `fatal = true`. + * @throws {Error} If `fatal = false`. + */ +function handleError(status, remoteURL, fatal) { + if (!fatal) { + // File was not loaded correctly, but it is optional. + // TODO in future, cache the response? + return null; + } + + const message = ERROR_MAPPING[status] ?? `Error (${status}) occurred while trying to load file`; + throw Error(`${message}: "${remoteURL}".`); +} + +class FileCache { + /** + * Instantiate a `FileCache` object. + * @param {string} path + */ + constructor(path) { + this.path = path; + } + + /** + * Checks whether the given request is in the cache. + * @param {string} request + * @returns {Promise} + */ + async match(request) { + + let filePath = path__WEBPACK_IMPORTED_MODULE_1__.join(this.path, request); + let file = new FileResponse(filePath); + + if (file.exists) { + return file; + } else { + return undefined; + } + } + + /** + * Adds the given response to the cache. + * @param {string} request + * @param {Response} response + * @param {(data: {progress: number, loaded: number, total: number}) => void} [progress_callback] Optional. + * The function to call with progress updates + * @returns {Promise} + */ + async put(request, response, progress_callback = undefined) { + let filePath = path__WEBPACK_IMPORTED_MODULE_1__.join(this.path, request); + + try { + const contentLength = response.headers.get('Content-Length'); + const total = parseInt(contentLength ?? '0'); + let loaded = 0; + + await fs__WEBPACK_IMPORTED_MODULE_0__.promises.mkdir(path__WEBPACK_IMPORTED_MODULE_1__.dirname(filePath), { recursive: true }); + const fileStream = fs__WEBPACK_IMPORTED_MODULE_0__.createWriteStream(filePath); + const reader = response.body.getReader(); + + while (true) { + const { done, value } = await reader.read(); + if (done) { + break; + } + + await new Promise((resolve, reject) => { + fileStream.write(value, (err) => { + if (err) { + reject(err); + return; + } + resolve(); + }); + }); + + loaded += value.length; + const progress = total ? (loaded / total) * 100 : 0; + + progress_callback?.({ progress, loaded, total }); + } + + fileStream.close(); + } catch (error) { + // Clean up the file if an error occurred during download + try { + await fs__WEBPACK_IMPORTED_MODULE_0__.promises.unlink(filePath); + } catch { } + throw error; + } + } + + // TODO add the rest? + // addAll(requests: RequestInfo[]): Promise; + // delete(request: RequestInfo | URL, options?: CacheQueryOptions): Promise; + // keys(request?: RequestInfo | URL, options?: CacheQueryOptions): Promise>; + // match(request: RequestInfo | URL, options?: CacheQueryOptions): Promise; + // matchAll(request?: RequestInfo | URL, options?: CacheQueryOptions): Promise>; +} + +/** + * + * @param {FileCache|Cache} cache The cache to search + * @param {string[]} names The names of the item to search for + * @returns {Promise} The item from the cache, or undefined if not found. + */ +async function tryCache(cache, ...names) { + for (let name of names) { + try { + let result = await cache.match(name); + if (result) return result; + } catch (e) { + continue; + } + } + return undefined; +} + +/** + * Retrieves a file from either a remote URL using the Fetch API or from the local file system using the FileSystem API. + * If the filesystem is available and `env.useCache = true`, the file will be downloaded and cached. + * + * @param {string} path_or_repo_id This can be either: + * - a string, the *model id* of a model repo on huggingface.co. + * - a path to a *directory* potentially containing the file. + * @param {string} filename The name of the file to locate in `path_or_repo`. + * @param {boolean} [fatal=true] Whether to throw an error if the file is not found. + * @param {PretrainedOptions} [options] An object containing optional parameters. + * @param {boolean} [return_path=false] Whether to return the path of the file instead of the file content. + * + * @throws Will throw an error if the file is not found and `fatal` is true. + * @returns {Promise} A Promise that resolves with the file content as a Uint8Array if `return_path` is false, or the file path as a string if `return_path` is true. + */ +async function getModelFile(path_or_repo_id, filename, fatal = true, options = {}, return_path = false) { + + if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.allowLocalModels) { + // User has disabled local models, so we just make sure other settings are correct. + + if (options.local_files_only) { + throw Error("Invalid configuration detected: local models are disabled (`env.allowLocalModels=false`) but you have requested to only use local models (`local_files_only=true`).") + } else if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.allowRemoteModels) { + throw Error("Invalid configuration detected: both local and remote models are disabled. Fix by setting `env.allowLocalModels` or `env.allowRemoteModels` to `true`.") + } + } + + // Initiate file retrieval + (0,_core_js__WEBPACK_IMPORTED_MODULE_3__.dispatchCallback)(options.progress_callback, { + status: 'initiate', + name: path_or_repo_id, + file: filename + }) + + // First, check if the a caching backend is available + // If no caching mechanism available, will download the file every time + let cache; + if (!cache && _env_js__WEBPACK_IMPORTED_MODULE_2__.env.useCustomCache) { + // Allow the user to specify a custom cache system. + if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache) { + throw Error('`env.useCustomCache=true`, but `env.customCache` is not defined.') + } + + // Check that the required methods are defined: + if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache.match || !_env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache.put) { + throw new Error( + "`env.customCache` must be an object which implements the `match` and `put` functions of the Web Cache API. " + + "For more information, see https://developer.mozilla.org/en-US/docs/Web/API/Cache" + ) + } + cache = _env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache; + } + + if (!cache && _env_js__WEBPACK_IMPORTED_MODULE_2__.env.useBrowserCache) { + if (typeof caches === 'undefined') { + throw Error('Browser cache is not available in this environment.') + } + try { + // In some cases, the browser cache may be visible, but not accessible due to security restrictions. + // For example, when running an application in an iframe, if a user attempts to load the page in + // incognito mode, the following error is thrown: `DOMException: Failed to execute 'open' on 'CacheStorage': + // An attempt was made to break through the security policy of the user agent.` + // So, instead of crashing, we just ignore the error and continue without using the cache. + cache = await caches.open('transformers-cache'); + } catch (e) { + console.warn('An error occurred while opening the browser cache:', e); + } + } + + if (!cache && _env_js__WEBPACK_IMPORTED_MODULE_2__.env.useFSCache) { + if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.apis.IS_FS_AVAILABLE) { + throw Error('File System Cache is not available in this environment.'); + } + + // If `cache_dir` is not specified, use the default cache directory + cache = new FileCache(options.cache_dir ?? _env_js__WEBPACK_IMPORTED_MODULE_2__.env.cacheDir); + } + + const revision = options.revision ?? 'main'; + const requestURL = pathJoin(path_or_repo_id, filename); + + const validModelId = isValidHfModelId(path_or_repo_id); + const localPath = validModelId + ? pathJoin(_env_js__WEBPACK_IMPORTED_MODULE_2__.env.localModelPath, requestURL) + : requestURL; + const remoteURL = pathJoin( + _env_js__WEBPACK_IMPORTED_MODULE_2__.env.remoteHost, + _env_js__WEBPACK_IMPORTED_MODULE_2__.env.remotePathTemplate + .replaceAll('{model}', path_or_repo_id) + .replaceAll('{revision}', encodeURIComponent(revision)), + filename + ); + + /** @type {string} */ + let cacheKey; + const proposedCacheKey = cache instanceof FileCache + // Choose cache key for filesystem cache + // When using the main revision (default), we use the request URL as the cache key. + // If a specific revision is requested, we account for this in the cache key. + ? revision === 'main' ? requestURL : pathJoin(path_or_repo_id, revision, filename) + : remoteURL; + + // Whether to cache the final response in the end. + let toCacheResponse = false; + + /** @type {Response|FileResponse|undefined} */ + let response; + + if (cache) { + // A caching system is available, so we try to get the file from it. + // 1. We first try to get from cache using the local path. In some environments (like deno), + // non-URL cache keys are not allowed. In these cases, `response` will be undefined. + // 2. If no response is found, we try to get from cache using the remote URL or file system cache. + response = await tryCache(cache, localPath, proposedCacheKey); + } + + const cacheHit = response !== undefined; + if (response === undefined) { + // Caching not available, or file is not cached, so we perform the request + + if (_env_js__WEBPACK_IMPORTED_MODULE_2__.env.allowLocalModels) { + // Accessing local models is enabled, so we try to get the file locally. + // If request is a valid HTTP URL, we skip the local file check. Otherwise, we try to get the file locally. + const isURL = isValidUrl(requestURL, ['http:', 'https:']); + if (!isURL) { + try { + response = await getFile(localPath); + cacheKey = localPath; // Update the cache key to be the local path + } catch (e) { + // Something went wrong while trying to get the file locally. + // NOTE: error handling is done in the next step (since `response` will be undefined) + console.warn(`Unable to load from local path "${localPath}": "${e}"`); + } + } else if (options.local_files_only) { + throw new Error(`\`local_files_only=true\`, but attempted to load a remote file from: ${requestURL}.`); + } else if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.allowRemoteModels) { + throw new Error(`\`env.allowRemoteModels=false\`, but attempted to load a remote file from: ${requestURL}.`); + } + } + + if (response === undefined || response.status === 404) { + // File not found locally. This means either: + // - The user has disabled local file access (`env.allowLocalModels=false`) + // - the path is a valid HTTP url (`response === undefined`) + // - the path is not a valid HTTP url and the file is not present on the file system or local server (`response.status === 404`) + + if (options.local_files_only || !_env_js__WEBPACK_IMPORTED_MODULE_2__.env.allowRemoteModels) { + // User requested local files only, but the file is not found locally. + if (fatal) { + throw Error(`\`local_files_only=true\` or \`env.allowRemoteModels=false\` and file was not found locally at "${localPath}".`); + } else { + // File not found, but this file is optional. + // TODO in future, cache the response? + return null; + } + } + if (!validModelId) { + // Before making any requests to the remote server, we check if the model ID is valid. + // This prevents unnecessary network requests for invalid model IDs. + throw Error(`Local file missing at "${localPath}" and download aborted due to invalid model ID "${path_or_repo_id}".`); + } + + // File not found locally, so we try to download it from the remote server + response = await getFile(remoteURL); + + if (response.status !== 200) { + return handleError(response.status, remoteURL, fatal); + } + + // Success! We use the proposed cache key from earlier + cacheKey = proposedCacheKey; + } + + // Only cache the response if: + toCacheResponse = + cache // 1. A caching system is available + && typeof Response !== 'undefined' // 2. `Response` is defined (i.e., we are in a browser-like environment) + && response instanceof Response // 3. result is a `Response` object (i.e., not a `FileResponse`) + && response.status === 200 // 4. request was successful (status code 200) + } + + // Start downloading + (0,_core_js__WEBPACK_IMPORTED_MODULE_3__.dispatchCallback)(options.progress_callback, { + status: 'download', + name: path_or_repo_id, + file: filename + }) + + let result; + if (!(_env_js__WEBPACK_IMPORTED_MODULE_2__.apis.IS_NODE_ENV && return_path)) { + /** @type {Uint8Array} */ + let buffer; + + if (!options.progress_callback) { + // If no progress callback is specified, we can use the `.arrayBuffer()` + // method to read the response. + buffer = new Uint8Array(await response.arrayBuffer()); + + } else if ( + cacheHit // The item is being read from the cache + && + typeof navigator !== 'undefined' && /firefox/i.test(navigator.userAgent) // We are in Firefox + ) { + // Due to bug in Firefox, we cannot display progress when loading from cache. + // Fortunately, since this should be instantaneous, this should not impact users too much. + buffer = new Uint8Array(await response.arrayBuffer()); + + // For completeness, we still fire the final progress callback + (0,_core_js__WEBPACK_IMPORTED_MODULE_3__.dispatchCallback)(options.progress_callback, { + status: 'progress', + name: path_or_repo_id, + file: filename, + progress: 100, + loaded: buffer.length, + total: buffer.length, + }) + } else { + buffer = await readResponse(response, data => { + (0,_core_js__WEBPACK_IMPORTED_MODULE_3__.dispatchCallback)(options.progress_callback, { + status: 'progress', + name: path_or_repo_id, + file: filename, + ...data, + }) + }) + } + result = buffer; + } + + if ( + // Only cache web responses + // i.e., do not cache FileResponses (prevents duplication) + toCacheResponse && cacheKey + && + // Check again whether request is in cache. If not, we add the response to the cache + (await cache.match(cacheKey) === undefined) + ) { + if (!result) { + // We haven't yet read the response body, so we need to do so now. + await cache.put(cacheKey, /** @type {Response} */(response), options.progress_callback); + } else { + // NOTE: We use `new Response(buffer, ...)` instead of `response.clone()` to handle LFS files + await cache.put(cacheKey, new Response(result, { + headers: response.headers + })) + .catch(err => { + // Do not crash if unable to add to cache (e.g., QuotaExceededError). + // Rather, log a warning and proceed with execution. + console.warn(`Unable to add response to browser cache: ${err}.`); + }); + } + } + (0,_core_js__WEBPACK_IMPORTED_MODULE_3__.dispatchCallback)(options.progress_callback, { + status: 'done', + name: path_or_repo_id, + file: filename + }); + + if (result) { + if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.apis.IS_NODE_ENV && return_path) { + throw new Error("Cannot return path in a browser environment.") + } + return result; + } + if (response instanceof FileResponse) { + return response.filePath; + } + + // Otherwise, return the cached response (most likely a `FileResponse`). + // NOTE: A custom cache may return a Response, or a string (file path) + const cachedResponse = await cache?.match(cacheKey); + if (cachedResponse instanceof FileResponse) { + return cachedResponse.filePath; + } else if (cachedResponse instanceof Response) { + return new Uint8Array(await cachedResponse.arrayBuffer()); + } else if (typeof cachedResponse === 'string') { + return cachedResponse; + } + + throw new Error("Unable to get model file path or buffer."); +} + +/** + * Fetches a JSON file from a given path and file name. + * + * @param {string} modelPath The path to the directory containing the file. + * @param {string} fileName The name of the file to fetch. + * @param {boolean} [fatal=true] Whether to throw an error if the file is not found. + * @param {PretrainedOptions} [options] An object containing optional parameters. + * @returns {Promise} The JSON data parsed into a JavaScript object. + * @throws Will throw an error if the file is not found and `fatal` is true. + */ +async function getModelJSON(modelPath, fileName, fatal = true, options = {}) { + const buffer = await getModelFile(modelPath, fileName, fatal, options, false); + if (buffer === null) { + // Return empty object + return {} + } + + const decoder = new TextDecoder('utf-8'); + const jsonData = decoder.decode(/** @type {Uint8Array} */(buffer)); + + return JSON.parse(jsonData); +} +/** + * Read and track progress when reading a Response object + * + * @param {Response|FileResponse} response The Response object to read + * @param {(data: {progress: number, loaded: number, total: number}) => void} progress_callback The function to call with progress updates + * @returns {Promise} A Promise that resolves with the Uint8Array buffer + */ +async function readResponse(response, progress_callback) { + + const contentLength = response.headers.get('Content-Length'); + if (contentLength === null) { + console.warn('Unable to determine content-length from response headers. Will expand buffer when needed.') + } + let total = parseInt(contentLength ?? '0'); + let buffer = new Uint8Array(total); + let loaded = 0; + + const reader = response.body.getReader(); + async function read() { + const { done, value } = await reader.read(); + if (done) return; + + const newLoaded = loaded + value.length; + if (newLoaded > total) { + total = newLoaded; + + // Adding the new data will overflow buffer. + // In this case, we extend the buffer + const newBuffer = new Uint8Array(total); + + // copy contents + newBuffer.set(buffer); + + buffer = newBuffer; + } + buffer.set(value, loaded); + loaded = newLoaded; + + const progress = (loaded / total) * 100; + + // Call your function here + progress_callback({ progress, loaded, total }); + + return read(); + } + + // Actually read + await read(); + + return buffer; +} + +/** + * Joins multiple parts of a path into a single path, while handling leading and trailing slashes. + * + * @param {...string} parts Multiple parts of a path. + * @returns {string} A string representing the joined path. + */ +function pathJoin(...parts) { + // https://stackoverflow.com/a/55142565 + parts = parts.map((part, index) => { + if (index) { + part = part.replace(new RegExp('^/'), ''); + } + if (index !== parts.length - 1) { + part = part.replace(new RegExp('/$'), ''); + } + return part; + }) + return parts.join('/'); +} + + +/***/ }), + +/***/ "./src/utils/image.js": +/*!****************************!*\ + !*** ./src/utils/image.js ***! + \****************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ RawImage: () => (/* binding */ RawImage), +/* harmony export */ load_image: () => (/* binding */ load_image) +/* harmony export */ }); +/* harmony import */ var _core_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./core.js */ "./src/utils/core.js"); +/* harmony import */ var _hub_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./hub.js */ "./src/utils/hub.js"); +/* harmony import */ var _env_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../env.js */ "./src/env.js"); +/* harmony import */ var _tensor_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./tensor.js */ "./src/utils/tensor.js"); +/* harmony import */ var sharp__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! sharp */ "?2b25"); + +/** + * @file Helper module for image processing. + * + * These functions and classes are only used internally, + * meaning an end-user shouldn't need to access anything here. + * + * @module utils/image + */ + + + + + + +// Will be empty (or not used) if running in browser or web-worker + + +let createCanvasFunction; +let ImageDataClass; +let loadImageFunction; +const IS_BROWSER_OR_WEBWORKER = _env_js__WEBPACK_IMPORTED_MODULE_2__.apis.IS_BROWSER_ENV || _env_js__WEBPACK_IMPORTED_MODULE_2__.apis.IS_WEBWORKER_ENV; +if (IS_BROWSER_OR_WEBWORKER) { + // Running in browser or web-worker + createCanvasFunction = (/** @type {number} */ width, /** @type {number} */ height) => { + if (!self.OffscreenCanvas) { + throw new Error('OffscreenCanvas not supported by this browser.'); + } + return new self.OffscreenCanvas(width, height) + }; + loadImageFunction = self.createImageBitmap; + ImageDataClass = self.ImageData; + +} else if (sharp__WEBPACK_IMPORTED_MODULE_4__) { + // Running in Node.js, electron, or other non-browser environment + + loadImageFunction = async (/**@type {sharp.Sharp}*/img) => { + const metadata = await img.metadata(); + const rawChannels = metadata.channels; + + const { data, info } = await img.rotate().raw().toBuffer({ resolveWithObject: true }); + + const newImage = new RawImage(new Uint8ClampedArray(data), info.width, info.height, info.channels); + if (rawChannels !== undefined && rawChannels !== info.channels) { + // Make sure the new image has the same number of channels as the input image. + // This is necessary for grayscale images. + newImage.convert(rawChannels); + } + return newImage; + } + +} else { + throw new Error('Unable to load image processing library.'); +} + + +// Defined here: https://github.com/python-pillow/Pillow/blob/a405e8406b83f8bfb8916e93971edc7407b8b1ff/src/libImaging/Imaging.h#L262-L268 +const RESAMPLING_MAPPING = { + 0: 'nearest', + 1: 'lanczos', + 2: 'bilinear', + 3: 'bicubic', + 4: 'box', + 5: 'hamming', +} + +/** + * Mapping from file extensions to MIME types. + */ +const CONTENT_TYPE_MAP = new Map([ + ['png', 'image/png'], + ['jpg', 'image/jpeg'], + ['jpeg', 'image/jpeg'], + ['gif', 'image/gif'], +]); + +class RawImage { + + /** + * Create a new `RawImage` object. + * @param {Uint8ClampedArray|Uint8Array} data The pixel data. + * @param {number} width The width of the image. + * @param {number} height The height of the image. + * @param {1|2|3|4} channels The number of channels. + */ + constructor(data, width, height, channels) { + this.data = data; + this.width = width; + this.height = height; + this.channels = channels; + } + + /** + * Returns the size of the image (width, height). + * @returns {[number, number]} The size of the image (width, height). + */ + get size() { + return [this.width, this.height]; + } + + /** + * Helper method for reading an image from a variety of input types. + * @param {RawImage|string|URL|Blob|HTMLCanvasElement|OffscreenCanvas} input + * @returns The image object. + * + * **Example:** Read image from a URL. + * ```javascript + * let image = await RawImage.read('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/football-match.jpg'); + * // RawImage { + * // "data": Uint8ClampedArray [ 25, 25, 25, 19, 19, 19, ... ], + * // "width": 800, + * // "height": 533, + * // "channels": 3 + * // } + * ``` + */ + static async read(input) { + if (input instanceof RawImage) { + return input; + } else if (typeof input === 'string' || input instanceof URL) { + return await this.fromURL(input); + } else if (input instanceof Blob) { + return await this.fromBlob(input); + } else if ( + (typeof HTMLCanvasElement !== "undefined" && input instanceof HTMLCanvasElement) + || + (typeof OffscreenCanvas !== "undefined" && input instanceof OffscreenCanvas) + ) { + return this.fromCanvas(input); + } else { + throw new Error(`Unsupported input type: ${typeof input}`); + } + } + + /** + * Read an image from a canvas. + * @param {HTMLCanvasElement|OffscreenCanvas} canvas The canvas to read the image from. + * @returns {RawImage} The image object. + */ + static fromCanvas(canvas) { + if (!IS_BROWSER_OR_WEBWORKER) { + throw new Error('fromCanvas() is only supported in browser environments.') + } + + const ctx = canvas.getContext('2d'); + const data = ctx.getImageData(0, 0, canvas.width, canvas.height).data; + return new RawImage(data, canvas.width, canvas.height, 4); + } + + /** + * Read an image from a URL or file path. + * @param {string|URL} url The URL or file path to read the image from. + * @returns {Promise} The image object. + */ + static async fromURL(url) { + const response = await (0,_hub_js__WEBPACK_IMPORTED_MODULE_1__.getFile)(url); + if (response.status !== 200) { + throw new Error(`Unable to read image from "${url}" (${response.status} ${response.statusText})`); + } + const blob = await response.blob(); + return this.fromBlob(blob); + } + + /** + * Helper method to create a new Image from a blob. + * @param {Blob} blob The blob to read the image from. + * @returns {Promise} The image object. + */ + static async fromBlob(blob) { + if (IS_BROWSER_OR_WEBWORKER) { + // Running in environment with canvas + const img = await loadImageFunction(blob); + + const ctx = createCanvasFunction(img.width, img.height).getContext('2d'); + + // Draw image to context + ctx.drawImage(img, 0, 0); + + return new this(ctx.getImageData(0, 0, img.width, img.height).data, img.width, img.height, 4); + + } else { + // Use sharp.js to read (and possible resize) the image. + const img = sharp__WEBPACK_IMPORTED_MODULE_4__(await blob.arrayBuffer()); + + return await loadImageFunction(img); + } + } + + /** + * Helper method to create a new Image from a tensor + * @param {Tensor} tensor + */ + static fromTensor(tensor, channel_format = 'CHW') { + if (tensor.dims.length !== 3) { + throw new Error(`Tensor should have 3 dimensions, but has ${tensor.dims.length} dimensions.`); + } + + if (channel_format === 'CHW') { + tensor = tensor.transpose(1, 2, 0); + } else if (channel_format === 'HWC') { + // Do nothing + } else { + throw new Error(`Unsupported channel format: ${channel_format}`); + } + if (!(tensor.data instanceof Uint8ClampedArray || tensor.data instanceof Uint8Array)) { + throw new Error(`Unsupported tensor type: ${tensor.type}`); + } + switch (tensor.dims[2]) { + case 1: + case 2: + case 3: + case 4: + return new RawImage(tensor.data, tensor.dims[1], tensor.dims[0], tensor.dims[2]); + default: + throw new Error(`Unsupported number of channels: ${tensor.dims[2]}`); + } + } + + /** + * Convert the image to grayscale format. + * @returns {RawImage} `this` to support chaining. + */ + grayscale() { + if (this.channels === 1) { + return this; + } + + const newData = new Uint8ClampedArray(this.width * this.height * 1); + switch (this.channels) { + case 3: // rgb to grayscale + case 4: // rgba to grayscale + for (let i = 0, offset = 0; i < this.data.length; i += this.channels) { + const red = this.data[i]; + const green = this.data[i + 1]; + const blue = this.data[i + 2]; + + newData[offset++] = Math.round(0.2989 * red + 0.5870 * green + 0.1140 * blue); + } + break; + default: + throw new Error(`Conversion failed due to unsupported number of channels: ${this.channels}`); + } + return this._update(newData, this.width, this.height, 1); + } + + /** + * Convert the image to RGB format. + * @returns {RawImage} `this` to support chaining. + */ + rgb() { + if (this.channels === 3) { + return this; + } + + const newData = new Uint8ClampedArray(this.width * this.height * 3); + + switch (this.channels) { + case 1: // grayscale to rgb + for (let i = 0, offset = 0; i < this.data.length; ++i) { + newData[offset++] = this.data[i]; + newData[offset++] = this.data[i]; + newData[offset++] = this.data[i]; + } + break; + case 4: // rgba to rgb + for (let i = 0, offset = 0; i < this.data.length; i += 4) { + newData[offset++] = this.data[i]; + newData[offset++] = this.data[i + 1]; + newData[offset++] = this.data[i + 2]; + } + break; + default: + throw new Error(`Conversion failed due to unsupported number of channels: ${this.channels}`); + } + return this._update(newData, this.width, this.height, 3); + + } + + /** + * Convert the image to RGBA format. + * @returns {RawImage} `this` to support chaining. + */ + rgba() { + if (this.channels === 4) { + return this; + } + + const newData = new Uint8ClampedArray(this.width * this.height * 4); + + switch (this.channels) { + case 1: // grayscale to rgba + for (let i = 0, offset = 0; i < this.data.length; ++i) { + newData[offset++] = this.data[i]; + newData[offset++] = this.data[i]; + newData[offset++] = this.data[i]; + newData[offset++] = 255; + } + break; + case 3: // rgb to rgba + for (let i = 0, offset = 0; i < this.data.length; i += 3) { + newData[offset++] = this.data[i]; + newData[offset++] = this.data[i + 1]; + newData[offset++] = this.data[i + 2]; + newData[offset++] = 255; + } + break; + default: + throw new Error(`Conversion failed due to unsupported number of channels: ${this.channels}`); + } + + return this._update(newData, this.width, this.height, 4); + } + + /** + * Apply an alpha mask to the image. Operates in place. + * @param {RawImage} mask The mask to apply. It should have a single channel. + * @returns {RawImage} The masked image. + * @throws {Error} If the mask is not the same size as the image. + * @throws {Error} If the image does not have 4 channels. + * @throws {Error} If the mask is not a single channel. + */ + putAlpha(mask) { + if (mask.width !== this.width || mask.height !== this.height) { + throw new Error(`Expected mask size to be ${this.width}x${this.height}, but got ${mask.width}x${mask.height}`); + } + if (mask.channels !== 1) { + throw new Error(`Expected mask to have 1 channel, but got ${mask.channels}`); + } + + const this_data = this.data; + const mask_data = mask.data; + const num_pixels = this.width * this.height; + if (this.channels === 3) { + // Convert to RGBA and simultaneously apply mask to alpha channel + const newData = new Uint8ClampedArray(num_pixels * 4); + for (let i = 0, in_offset = 0, out_offset = 0; i < num_pixels; ++i) { + newData[out_offset++] = this_data[in_offset++]; + newData[out_offset++] = this_data[in_offset++]; + newData[out_offset++] = this_data[in_offset++]; + newData[out_offset++] = mask_data[i]; + } + return this._update(newData, this.width, this.height, 4); + + } else if (this.channels === 4) { + // Apply mask to alpha channel in place + for (let i = 0; i < num_pixels; ++i) { + this_data[4 * i + 3] = mask_data[i]; + } + return this; + } + throw new Error(`Expected image to have 3 or 4 channels, but got ${this.channels}`); + } + + /** + * Resize the image to the given dimensions. This method uses the canvas API to perform the resizing. + * @param {number} width The width of the new image. `null` or `-1` will preserve the aspect ratio. + * @param {number} height The height of the new image. `null` or `-1` will preserve the aspect ratio. + * @param {Object} options Additional options for resizing. + * @param {0|1|2|3|4|5|string} [options.resample] The resampling method to use. + * @returns {Promise} `this` to support chaining. + */ + async resize(width, height, { + resample = 2, + } = {}) { + + // Do nothing if the image already has the desired size + if (this.width === width && this.height === height) { + return this; + } + + // Ensure resample method is a string + let resampleMethod = RESAMPLING_MAPPING[resample] ?? resample; + + // Calculate width / height to maintain aspect ratio, in the event that + // the user passed a null value in. + // This allows users to pass in something like `resize(320, null)` to + // resize to 320 width, but maintain aspect ratio. + const nullish_width = (0,_core_js__WEBPACK_IMPORTED_MODULE_0__.isNullishDimension)(width); + const nullish_height = (0,_core_js__WEBPACK_IMPORTED_MODULE_0__.isNullishDimension)(height); + if (nullish_width && nullish_height) { + return this; + } else if (nullish_width) { + width = (height / this.height) * this.width; + } else if (nullish_height) { + height = (width / this.width) * this.height; + } + + if (IS_BROWSER_OR_WEBWORKER) { + // TODO use `resample` in browser environment + + // Store number of channels before resizing + const numChannels = this.channels; + + // Create canvas object for this image + const canvas = this.toCanvas(); + + // Actually perform resizing using the canvas API + const ctx = createCanvasFunction(width, height).getContext('2d'); + + // Draw image to context, resizing in the process + ctx.drawImage(canvas, 0, 0, width, height); + + // Create image from the resized data + const resizedImage = new RawImage(ctx.getImageData(0, 0, width, height).data, width, height, 4); + + // Convert back so that image has the same number of channels as before + return resizedImage.convert(numChannels); + + } else { + // Create sharp image from raw data, and resize + let img = this.toSharp(); + + switch (resampleMethod) { + case 'box': + case 'hamming': + if (resampleMethod === 'box' || resampleMethod === 'hamming') { + console.warn(`Resampling method ${resampleMethod} is not yet supported. Using bilinear instead.`); + resampleMethod = 'bilinear'; + } + + case 'nearest': + case 'bilinear': + case 'bicubic': + // Perform resizing using affine transform. + // This matches how the python Pillow library does it. + img = img.affine([width / this.width, 0, 0, height / this.height], { + interpolator: resampleMethod + }); + break; + + case 'lanczos': + // https://github.com/python-pillow/Pillow/discussions/5519 + // https://github.com/lovell/sharp/blob/main/docs/api-resize.md + img = img.resize({ + width, height, + fit: 'fill', + kernel: 'lanczos3', // PIL Lanczos uses a kernel size of 3 + }); + break; + + default: + throw new Error(`Resampling method ${resampleMethod} is not supported.`); + } + + return await loadImageFunction(img); + } + + } + + async pad([left, right, top, bottom]) { + left = Math.max(left, 0); + right = Math.max(right, 0); + top = Math.max(top, 0); + bottom = Math.max(bottom, 0); + + if (left === 0 && right === 0 && top === 0 && bottom === 0) { + // No padding needed + return this; + } + + if (IS_BROWSER_OR_WEBWORKER) { + // Store number of channels before padding + const numChannels = this.channels; + + // Create canvas object for this image + const canvas = this.toCanvas(); + + const newWidth = this.width + left + right; + const newHeight = this.height + top + bottom; + + // Create a new canvas of the desired size. + const ctx = createCanvasFunction(newWidth, newHeight).getContext('2d'); + + // Draw image to context, padding in the process + ctx.drawImage(canvas, + 0, 0, this.width, this.height, + left, top, this.width, this.height + ); + + // Create image from the padded data + const paddedImage = new RawImage( + ctx.getImageData(0, 0, newWidth, newHeight).data, + newWidth, newHeight, 4 + ); + + // Convert back so that image has the same number of channels as before + return paddedImage.convert(numChannels); + + } else { + const img = this.toSharp().extend({ left, right, top, bottom }); + return await loadImageFunction(img); + } + } + + async crop([x_min, y_min, x_max, y_max]) { + // Ensure crop bounds are within the image + x_min = Math.max(x_min, 0); + y_min = Math.max(y_min, 0); + x_max = Math.min(x_max, this.width - 1); + y_max = Math.min(y_max, this.height - 1); + + // Do nothing if the crop is the entire image + if (x_min === 0 && y_min === 0 && x_max === this.width - 1 && y_max === this.height - 1) { + return this; + } + + const crop_width = x_max - x_min + 1; + const crop_height = y_max - y_min + 1; + + if (IS_BROWSER_OR_WEBWORKER) { + // Store number of channels before resizing + const numChannels = this.channels; + + // Create canvas object for this image + const canvas = this.toCanvas(); + + // Create a new canvas of the desired size. This is needed since if the + // image is too small, we need to pad it with black pixels. + const ctx = createCanvasFunction(crop_width, crop_height).getContext('2d'); + + // Draw image to context, cropping in the process + ctx.drawImage(canvas, + x_min, y_min, crop_width, crop_height, + 0, 0, crop_width, crop_height + ); + + // Create image from the resized data + const resizedImage = new RawImage(ctx.getImageData(0, 0, crop_width, crop_height).data, crop_width, crop_height, 4); + + // Convert back so that image has the same number of channels as before + return resizedImage.convert(numChannels); + + } else { + // Create sharp image from raw data + const img = this.toSharp().extract({ + left: x_min, + top: y_min, + width: crop_width, + height: crop_height, + }); + + return await loadImageFunction(img); + } + + } + + async center_crop(crop_width, crop_height) { + // If the image is already the desired size, return it + if (this.width === crop_width && this.height === crop_height) { + return this; + } + + // Determine bounds of the image in the new canvas + const width_offset = (this.width - crop_width) / 2; + const height_offset = (this.height - crop_height) / 2; + + + if (IS_BROWSER_OR_WEBWORKER) { + // Store number of channels before resizing + const numChannels = this.channels; + + // Create canvas object for this image + const canvas = this.toCanvas(); + + // Create a new canvas of the desired size. This is needed since if the + // image is too small, we need to pad it with black pixels. + const ctx = createCanvasFunction(crop_width, crop_height).getContext('2d'); + + let sourceX = 0; + let sourceY = 0; + let destX = 0; + let destY = 0; + + if (width_offset >= 0) { + sourceX = width_offset; + } else { + destX = -width_offset; + } + + if (height_offset >= 0) { + sourceY = height_offset; + } else { + destY = -height_offset; + } + + // Draw image to context, cropping in the process + ctx.drawImage(canvas, + sourceX, sourceY, crop_width, crop_height, + destX, destY, crop_width, crop_height + ); + + // Create image from the resized data + const resizedImage = new RawImage(ctx.getImageData(0, 0, crop_width, crop_height).data, crop_width, crop_height, 4); + + // Convert back so that image has the same number of channels as before + return resizedImage.convert(numChannels); + + } else { + // Create sharp image from raw data + let img = this.toSharp(); + + if (width_offset >= 0 && height_offset >= 0) { + // Cropped image lies entirely within the original image + img = img.extract({ + left: Math.floor(width_offset), + top: Math.floor(height_offset), + width: crop_width, + height: crop_height, + }) + } else if (width_offset <= 0 && height_offset <= 0) { + // Cropped image lies entirely outside the original image, + // so we add padding + const top = Math.floor(-height_offset); + const left = Math.floor(-width_offset); + img = img.extend({ + top: top, + left: left, + + // Ensures the resulting image has the desired dimensions + right: crop_width - this.width - left, + bottom: crop_height - this.height - top, + }); + } else { + // Cropped image lies partially outside the original image. + // We first pad, then crop. + + let y_padding = [0, 0]; + let y_extract = 0; + if (height_offset < 0) { + y_padding[0] = Math.floor(-height_offset); + y_padding[1] = crop_height - this.height - y_padding[0]; + } else { + y_extract = Math.floor(height_offset); + } + + let x_padding = [0, 0]; + let x_extract = 0; + if (width_offset < 0) { + x_padding[0] = Math.floor(-width_offset); + x_padding[1] = crop_width - this.width - x_padding[0]; + } else { + x_extract = Math.floor(width_offset); + } + + img = img.extend({ + top: y_padding[0], + bottom: y_padding[1], + left: x_padding[0], + right: x_padding[1], + }).extract({ + left: x_extract, + top: y_extract, + width: crop_width, + height: crop_height, + }) + } + + return await loadImageFunction(img); + } + } + + async toBlob(type = 'image/png', quality = 1) { + if (!IS_BROWSER_OR_WEBWORKER) { + throw new Error('toBlob() is only supported in browser environments.') + } + + const canvas = this.toCanvas(); + return await canvas.convertToBlob({ type, quality }); + } + + toTensor(channel_format = 'CHW') { + let tensor = new _tensor_js__WEBPACK_IMPORTED_MODULE_3__.Tensor( + 'uint8', + new Uint8Array(this.data), + [this.height, this.width, this.channels] + ); + + if (channel_format === 'HWC') { + // Do nothing + } else if (channel_format === 'CHW') { // hwc -> chw + tensor = tensor.permute(2, 0, 1); + } else { + throw new Error(`Unsupported channel format: ${channel_format}`); + } + return tensor; + } + + toCanvas() { + if (!IS_BROWSER_OR_WEBWORKER) { + throw new Error('toCanvas() is only supported in browser environments.') + } + + // Clone, and convert data to RGBA before drawing to canvas. + // This is because the canvas API only supports RGBA + const cloned = this.clone().rgba(); + + // Create canvas object for the cloned image + const clonedCanvas = createCanvasFunction(cloned.width, cloned.height); + + // Draw image to context + const data = new ImageDataClass(cloned.data, cloned.width, cloned.height); + clonedCanvas.getContext('2d').putImageData(data, 0, 0); + + return clonedCanvas; + } + + /** + * Split this image into individual bands. This method returns an array of individual image bands from an image. + * For example, splitting an "RGB" image creates three new images each containing a copy of one of the original bands (red, green, blue). + * + * Inspired by PIL's `Image.split()` [function](https://pillow.readthedocs.io/en/latest/reference/Image.html#PIL.Image.Image.split). + * @returns {RawImage[]} An array containing bands. + */ + split() { + const { data, width, height, channels } = this; + + /** @type {typeof Uint8Array | typeof Uint8ClampedArray} */ + const data_type = /** @type {any} */(data.constructor); + const per_channel_length = data.length / channels; + + // Pre-allocate buffers for each channel + const split_data = Array.from( + { length: channels }, + () => new data_type(per_channel_length), + ); + + // Write pixel data + for (let i = 0; i < per_channel_length; ++i) { + const data_offset = channels * i; + for (let j = 0; j < channels; ++j) { + split_data[j][i] = data[data_offset + j]; + } + } + return split_data.map((data) => new RawImage(data, width, height, 1)); + } + + /** + * Helper method to update the image data. + * @param {Uint8ClampedArray} data The new image data. + * @param {number} width The new width of the image. + * @param {number} height The new height of the image. + * @param {1|2|3|4|null} [channels] The new number of channels of the image. + * @private + */ + _update(data, width, height, channels = null) { + this.data = data; + this.width = width; + this.height = height; + if (channels !== null) { + this.channels = channels; + } + return this; + } + + /** + * Clone the image + * @returns {RawImage} The cloned image + */ + clone() { + return new RawImage(this.data.slice(), this.width, this.height, this.channels); + } + + /** + * Helper method for converting image to have a certain number of channels + * @param {number} numChannels The number of channels. Must be 1, 3, or 4. + * @returns {RawImage} `this` to support chaining. + */ + convert(numChannels) { + if (this.channels === numChannels) return this; // Already correct number of channels + + switch (numChannels) { + case 1: + this.grayscale(); + break; + case 3: + this.rgb(); + break; + case 4: + this.rgba(); + break; + default: + throw new Error(`Conversion failed due to unsupported number of channels: ${this.channels}`); + } + return this; + } + + /** + * Save the image to the given path. + * @param {string} path The path to save the image to. + */ + async save(path) { + + if (IS_BROWSER_OR_WEBWORKER) { + if (_env_js__WEBPACK_IMPORTED_MODULE_2__.apis.IS_WEBWORKER_ENV) { + throw new Error('Unable to save an image from a Web Worker.') + } + + const extension = path.split('.').pop().toLowerCase(); + const mime = CONTENT_TYPE_MAP.get(extension) ?? 'image/png'; + + // Convert image to Blob + const blob = await this.toBlob(mime); + + (0,_core_js__WEBPACK_IMPORTED_MODULE_0__.saveBlob)(path, blob) + + } else if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.apis.IS_FS_AVAILABLE) { + throw new Error('Unable to save the image because filesystem is disabled in this environment.') + + } else { + const img = this.toSharp(); + return await img.toFile(path); + } + } + + toSharp() { + if (IS_BROWSER_OR_WEBWORKER) { + throw new Error('toSharp() is only supported in server-side environments.') + } + + return sharp__WEBPACK_IMPORTED_MODULE_4__(this.data, { + raw: { + width: this.width, + height: this.height, + channels: this.channels + } + }); + } +} + +/** + * Helper function to load an image from a URL, path, etc. + */ +const load_image = RawImage.read.bind(RawImage); + + + +/***/ }), + +/***/ "./src/utils/maths.js": +/*!****************************!*\ + !*** ./src/utils/maths.js ***! + \****************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ FFT: () => (/* binding */ FFT), +/* harmony export */ bankers_round: () => (/* binding */ bankers_round), +/* harmony export */ cos_sim: () => (/* binding */ cos_sim), +/* harmony export */ dot: () => (/* binding */ dot), +/* harmony export */ dynamic_time_warping: () => (/* binding */ dynamic_time_warping), +/* harmony export */ interpolate_data: () => (/* binding */ interpolate_data), +/* harmony export */ log_softmax: () => (/* binding */ log_softmax), +/* harmony export */ magnitude: () => (/* binding */ magnitude), +/* harmony export */ max: () => (/* binding */ max), +/* harmony export */ medianFilter: () => (/* binding */ medianFilter), +/* harmony export */ min: () => (/* binding */ min), +/* harmony export */ permute_data: () => (/* binding */ permute_data), +/* harmony export */ round: () => (/* binding */ round), +/* harmony export */ softmax: () => (/* binding */ softmax) +/* harmony export */ }); + +/** + * @file Helper module for mathematical processing. + * + * These functions and classes are only used internally, + * meaning an end-user shouldn't need to access anything here. + * + * @module utils/maths + */ + +/** + * @typedef {Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float16Array | Float32Array | Float64Array} TypedArray + * @typedef {BigInt64Array | BigUint64Array} BigTypedArray + * @typedef {TypedArray | BigTypedArray} AnyTypedArray + */ + +/** + * @param {TypedArray} input + */ +function interpolate_data(input, [in_channels, in_height, in_width], [out_height, out_width], mode = 'bilinear', align_corners = false) { + // TODO use mode and align_corners + + // Output image dimensions + const x_scale = out_width / in_width; + const y_scale = out_height / in_height; + + // Output image + // @ts-ignore + const out_img = new input.constructor(out_height * out_width * in_channels); + + // Pre-calculate strides + const inStride = in_height * in_width; + const outStride = out_height * out_width; + + for (let i = 0; i < out_height; ++i) { + for (let j = 0; j < out_width; ++j) { + // Calculate output offset + const outOffset = i * out_width + j; + + // Calculate input pixel coordinates + const x = (j + 0.5) / x_scale - 0.5; + const y = (i + 0.5) / y_scale - 0.5; + + // Calculate the four nearest input pixels + // We also check if the input pixel coordinates are within the image bounds + let x1 = Math.floor(x); + let y1 = Math.floor(y); + const x2 = Math.min(x1 + 1, in_width - 1); + const y2 = Math.min(y1 + 1, in_height - 1); + + x1 = Math.max(x1, 0); + y1 = Math.max(y1, 0); + + + // Calculate the fractional distances between the input pixel and the four nearest pixels + const s = x - x1; + const t = y - y1; + + // Perform bilinear interpolation + const w1 = (1 - s) * (1 - t); + const w2 = s * (1 - t); + const w3 = (1 - s) * t; + const w4 = s * t; + + // Calculate the four nearest input pixel indices + const yStride = y1 * in_width; + const xStride = y2 * in_width; + const idx1 = yStride + x1; + const idx2 = yStride + x2; + const idx3 = xStride + x1; + const idx4 = xStride + x2; + + for (let k = 0; k < in_channels; ++k) { + // Calculate channel offset + const cOffset = k * inStride; + + out_img[k * outStride + outOffset] = + w1 * input[cOffset + idx1] + + w2 * input[cOffset + idx2] + + w3 * input[cOffset + idx3] + + w4 * input[cOffset + idx4]; + } + } + } + + return out_img; +} + + +/** + * Helper method to permute a `AnyTypedArray` directly + * @template {AnyTypedArray} T + * @param {T} array + * @param {number[]} dims + * @param {number[]} axes + * @returns {[T, number[]]} The permuted array and the new shape. + */ +function permute_data(array, dims, axes) { + // Calculate the new shape of the permuted array + // and the stride of the original array + const shape = new Array(axes.length); + const stride = new Array(axes.length); + + for (let i = axes.length - 1, s = 1; i >= 0; --i) { + stride[i] = s; + shape[i] = dims[axes[i]]; + s *= shape[i]; + } + + // Precompute inverse mapping of stride + const invStride = axes.map((_, i) => stride[axes.indexOf(i)]); + + // Create the permuted array with the new shape + // @ts-ignore + const permutedData = new array.constructor(array.length); + + // Permute the original array to the new array + for (let i = 0; i < array.length; ++i) { + let newIndex = 0; + for (let j = dims.length - 1, k = i; j >= 0; --j) { + newIndex += (k % dims[j]) * invStride[j]; + k = Math.floor(k / dims[j]); + } + permutedData[newIndex] = array[i]; + } + + return [permutedData, shape]; +} + + +/** + * Compute the softmax of an array of numbers. + * @template {TypedArray|number[]} T + * @param {T} arr The array of numbers to compute the softmax of. + * @returns {T} The softmax array. + */ +function softmax(arr) { + // Compute the maximum value in the array + const maxVal = max(arr)[0]; + + // Compute the exponentials of the array values + const exps = arr.map(x => Math.exp(x - maxVal)); + + // Compute the sum of the exponentials + // @ts-ignore + const sumExps = exps.reduce((acc, val) => acc + val, 0); + + // Compute the softmax values + const softmaxArr = exps.map(x => x / sumExps); + + return /** @type {T} */(softmaxArr); +} + +/** + * Calculates the logarithm of the softmax function for the input array. + * @template {TypedArray|number[]} T + * @param {T} arr The input array to calculate the log_softmax function for. + * @returns {T} The resulting log_softmax array. + */ +function log_softmax(arr) { + // Compute the maximum value in the array + const maxVal = max(arr)[0]; + + // Compute the sum of the exponentials + let sumExps = 0; + for(let i = 0; i < arr.length; ++i) { + sumExps += Math.exp(arr[i] - maxVal); + } + + // Compute the log of the sum + const logSum = Math.log(sumExps); + + // Compute the softmax values + const logSoftmaxArr = arr.map(x => x - maxVal - logSum); + + return /** @type {T} */(logSoftmaxArr); +} + +/** + * Calculates the dot product of two arrays. + * @param {number[]} arr1 The first array. + * @param {number[]} arr2 The second array. + * @returns {number} The dot product of arr1 and arr2. + */ +function dot(arr1, arr2) { + let result = 0; + for (let i = 0; i < arr1.length; ++i) { + result += arr1[i] * arr2[i]; + } + return result; +} + +/** + * Computes the cosine similarity between two arrays. + * + * @param {number[]} arr1 The first array. + * @param {number[]} arr2 The second array. + * @returns {number} The cosine similarity between the two arrays. + */ +function cos_sim(arr1, arr2) { + // Calculate dot product of the two arrays + const dotProduct = dot(arr1, arr2); + + // Calculate the magnitude of the first array + const magnitudeA = magnitude(arr1); + + // Calculate the magnitude of the second array + const magnitudeB = magnitude(arr2); + + // Calculate the cosine similarity + const cosineSimilarity = dotProduct / (magnitudeA * magnitudeB); + + return cosineSimilarity; +} + +/** + * Calculates the magnitude of a given array. + * @param {number[]} arr The array to calculate the magnitude of. + * @returns {number} The magnitude of the array. + */ +function magnitude(arr) { + return Math.sqrt(arr.reduce((acc, val) => acc + val * val, 0)); +} + + +/** + * Returns the value and index of the minimum element in an array. + * @template {number[]|bigint[]|AnyTypedArray} T + * @param {T} arr array of numbers. + * @returns {T extends bigint[]|BigTypedArray ? [bigint, number] : [number, number]} the value and index of the minimum element, of the form: [valueOfMin, indexOfMin] + * @throws {Error} If array is empty. + */ +function min(arr) { + if (arr.length === 0) throw Error('Array must not be empty'); + let min = arr[0]; + let indexOfMin = 0; + for (let i = 1; i < arr.length; ++i) { + if (arr[i] < min) { + min = arr[i]; + indexOfMin = i; + } + } + return /** @type {T extends bigint[]|BigTypedArray ? [bigint, number] : [number, number]} */([min, indexOfMin]); +} + + +/** + * Returns the value and index of the maximum element in an array. + * @template {number[]|bigint[]|AnyTypedArray} T + * @param {T} arr array of numbers. + * @returns {T extends bigint[]|BigTypedArray ? [bigint, number] : [number, number]} the value and index of the maximum element, of the form: [valueOfMax, indexOfMax] + * @throws {Error} If array is empty. + */ +function max(arr) { + if (arr.length === 0) throw Error('Array must not be empty'); + let max = arr[0]; + let indexOfMax = 0; + for (let i = 1; i < arr.length; ++i) { + if (arr[i] > max) { + max = arr[i]; + indexOfMax = i; + } + } + return /** @type {T extends bigint[]|BigTypedArray ? [bigint, number] : [number, number]} */([max, indexOfMax]); +} + +function isPowerOfTwo(number) { + // Check if the number is greater than 0 and has only one bit set to 1 + return (number > 0) && ((number & (number - 1)) === 0); +} + +/** + * Implementation of Radix-4 FFT. + * + * P2FFT class provides functionality for performing Fast Fourier Transform on arrays + * which are a power of two in length. + * Code adapted from https://www.npmjs.com/package/fft.js + */ +class P2FFT { + /** + * @param {number} size The size of the input array. Must be a power of two larger than 1. + * @throws {Error} FFT size must be a power of two larger than 1. + */ + constructor(size) { + this.size = size | 0; // convert to a 32-bit signed integer + if (this.size <= 1 || !isPowerOfTwo(this.size)) + throw new Error('FFT size must be a power of two larger than 1'); + + this._csize = size << 1; + + this.table = new Float64Array(this.size * 2); + for (let i = 0; i < this.table.length; i += 2) { + const angle = Math.PI * i / this.size; + this.table[i] = Math.cos(angle); + this.table[i + 1] = -Math.sin(angle); + } + + // Find size's power of two + let power = 0; + for (let t = 1; this.size > t; t <<= 1) + ++power; + + // Calculate initial step's width: + // * If we are full radix-4, it is 2x smaller to give inital len=8 + // * Otherwise it is the same as `power` to give len=4 + this._width = power % 2 === 0 ? power - 1 : power; + + // Pre-compute bit-reversal patterns + this._bitrev = new Int32Array(1 << this._width); + for (let j = 0; j < this._bitrev.length; ++j) { + this._bitrev[j] = 0; + for (let shift = 0; shift < this._width; shift += 2) { + const revShift = this._width - shift - 2; + this._bitrev[j] |= ((j >>> shift) & 3) << revShift; + } + } + } + + /** + * Create a complex number array with size `2 * size` + * + * @returns {Float64Array} A complex number array with size `2 * size` + */ + createComplexArray() { + return new Float64Array(this._csize); + } + + /** + * Converts a complex number representation stored in a Float64Array to an array of real numbers. + * + * @param {Float64Array} complex The complex number representation to be converted. + * @param {number[]} [storage] An optional array to store the result in. + * @returns {number[]} An array of real numbers representing the input complex number representation. + */ + fromComplexArray(complex, storage) { + const res = storage || new Array(complex.length >>> 1); + for (let i = 0; i < complex.length; i += 2) + res[i >>> 1] = complex[i]; + return res; + } + + /** + * Convert a real-valued input array to a complex-valued output array. + * @param {Float64Array} input The real-valued input array. + * @param {Float64Array} [storage] Optional buffer to store the output array. + * @returns {Float64Array} The complex-valued output array. + */ + toComplexArray(input, storage) { + const res = storage || this.createComplexArray(); + for (let i = 0; i < res.length; i += 2) { + res[i] = input[i >>> 1]; + res[i + 1] = 0; + } + return res; + } + + /** + * Performs a Fast Fourier Transform (FFT) on the given input data and stores the result in the output buffer. + * + * @param {Float64Array} out The output buffer to store the result. + * @param {Float64Array} data The input data to transform. + * + * @throws {Error} Input and output buffers must be different. + * + * @returns {void} + */ + transform(out, data) { + if (out === data) + throw new Error('Input and output buffers must be different'); + + this._transform4(out, data, 1 /* DONE */); + } + + /** + * Performs a real-valued forward FFT on the given input buffer and stores the result in the given output buffer. + * The input buffer must contain real values only, while the output buffer will contain complex values. The input and + * output buffers must be different. + * + * @param {Float64Array} out The output buffer. + * @param {Float64Array} data The input buffer containing real values. + * + * @throws {Error} If the input and output buffers are the same. + */ + realTransform(out, data) { + if (out === data) + throw new Error('Input and output buffers must be different'); + + this._realTransform4(out, data, 1 /* DONE */); + } + + /** + * Performs an inverse FFT transformation on the given `data` array, and stores the result in `out`. + * The `out` array must be a different buffer than the `data` array. The `out` array will contain the + * result of the transformation. The `data` array will not be modified. + * + * @param {Float64Array} out The output buffer for the transformed data. + * @param {Float64Array} data The input data to transform. + * @throws {Error} If `out` and `data` refer to the same buffer. + * @returns {void} + */ + inverseTransform(out, data) { + if (out === data) + throw new Error('Input and output buffers must be different'); + + this._transform4(out, data, -1 /* DONE */); + for (let i = 0; i < out.length; ++i) + out[i] /= this.size; + } + + /** + * Performs a radix-4 implementation of a discrete Fourier transform on a given set of data. + * + * @param {Float64Array} out The output buffer for the transformed data. + * @param {Float64Array} data The input buffer of data to be transformed. + * @param {number} inv A scaling factor to apply to the transform. + * @returns {void} + */ + _transform4(out, data, inv) { + // radix-4 implementation + + const size = this._csize; + + // Initial step (permute and transform) + const width = this._width; + let step = 1 << width; + let len = (size / step) << 1; + + let outOff; + let t; + const bitrev = this._bitrev; + if (len === 4) { + for (outOff = 0, t = 0; outOff < size; outOff += len, ++t) { + const off = bitrev[t]; + this._singleTransform2(data, out, outOff, off, step); + } + } else { + // len === 8 + for (outOff = 0, t = 0; outOff < size; outOff += len, ++t) { + const off = bitrev[t]; + this._singleTransform4(data, out, outOff, off, step, inv); + } + } + + // Loop through steps in decreasing order + const table = this.table; + for (step >>= 2; step >= 2; step >>= 2) { + len = (size / step) << 1; + const quarterLen = len >>> 2; + + // Loop through offsets in the data + for (outOff = 0; outOff < size; outOff += len) { + // Full case + const limit = outOff + quarterLen - 1; + for (let i = outOff, k = 0; i < limit; i += 2, k += step) { + const A = i; + const B = A + quarterLen; + const C = B + quarterLen; + const D = C + quarterLen; + + // Original values + const Ar = out[A]; + const Ai = out[A + 1]; + const Br = out[B]; + const Bi = out[B + 1]; + const Cr = out[C]; + const Ci = out[C + 1]; + const Dr = out[D]; + const Di = out[D + 1]; + + const tableBr = table[k]; + const tableBi = inv * table[k + 1]; + const MBr = Br * tableBr - Bi * tableBi; + const MBi = Br * tableBi + Bi * tableBr; + + const tableCr = table[2 * k]; + const tableCi = inv * table[2 * k + 1]; + const MCr = Cr * tableCr - Ci * tableCi; + const MCi = Cr * tableCi + Ci * tableCr; + + const tableDr = table[3 * k]; + const tableDi = inv * table[3 * k + 1]; + const MDr = Dr * tableDr - Di * tableDi; + const MDi = Dr * tableDi + Di * tableDr; + + // Pre-Final values + const T0r = Ar + MCr; + const T0i = Ai + MCi; + const T1r = Ar - MCr; + const T1i = Ai - MCi; + const T2r = MBr + MDr; + const T2i = MBi + MDi; + const T3r = inv * (MBr - MDr); + const T3i = inv * (MBi - MDi); + + // Final values + out[A] = T0r + T2r; + out[A + 1] = T0i + T2i; + out[B] = T1r + T3i; + out[B + 1] = T1i - T3r; + out[C] = T0r - T2r; + out[C + 1] = T0i - T2i; + out[D] = T1r - T3i; + out[D + 1] = T1i + T3r; + } + } + } + } + + /** + * Performs a radix-2 implementation of a discrete Fourier transform on a given set of data. + * + * @param {Float64Array} data The input buffer of data to be transformed. + * @param {Float64Array} out The output buffer for the transformed data. + * @param {number} outOff The offset at which to write the output data. + * @param {number} off The offset at which to begin reading the input data. + * @param {number} step The step size for indexing the input data. + * @returns {void} + */ + _singleTransform2(data, out, outOff, off, step) { + // radix-2 implementation + // NOTE: Only called for len=4 + + const evenR = data[off]; + const evenI = data[off + 1]; + const oddR = data[off + step]; + const oddI = data[off + step + 1]; + + out[outOff] = evenR + oddR; + out[outOff + 1] = evenI + oddI; + out[outOff + 2] = evenR - oddR; + out[outOff + 3] = evenI - oddI; + } + + /** + * Performs radix-4 transformation on input data of length 8 + * + * @param {Float64Array} data Input data array of length 8 + * @param {Float64Array} out Output data array of length 8 + * @param {number} outOff Index of output array to start writing from + * @param {number} off Index of input array to start reading from + * @param {number} step Step size between elements in input array + * @param {number} inv Scaling factor for inverse transform + * + * @returns {void} + */ + _singleTransform4(data, out, outOff, off, step, inv) { + // radix-4 + // NOTE: Only called for len=8 + const step2 = step * 2; + const step3 = step * 3; + + // Original values + const Ar = data[off]; + const Ai = data[off + 1]; + const Br = data[off + step]; + const Bi = data[off + step + 1]; + const Cr = data[off + step2]; + const Ci = data[off + step2 + 1]; + const Dr = data[off + step3]; + const Di = data[off + step3 + 1]; + + // Pre-Final values + const T0r = Ar + Cr; + const T0i = Ai + Ci; + const T1r = Ar - Cr; + const T1i = Ai - Ci; + const T2r = Br + Dr; + const T2i = Bi + Di; + const T3r = inv * (Br - Dr); + const T3i = inv * (Bi - Di); + + // Final values + out[outOff] = T0r + T2r; + out[outOff + 1] = T0i + T2i; + out[outOff + 2] = T1r + T3i; + out[outOff + 3] = T1i - T3r; + out[outOff + 4] = T0r - T2r; + out[outOff + 5] = T0i - T2i; + out[outOff + 6] = T1r - T3i; + out[outOff + 7] = T1i + T3r; + } + + /** + * Real input radix-4 implementation + * @param {Float64Array} out Output array for the transformed data + * @param {Float64Array} data Input array of real data to be transformed + * @param {number} inv The scale factor used to normalize the inverse transform + */ + _realTransform4(out, data, inv) { + // Real input radix-4 implementation + const size = this._csize; + + // Initial step (permute and transform) + const width = this._width; + let step = 1 << width; + let len = (size / step) << 1; + + let outOff; + let t; + const bitrev = this._bitrev; + if (len === 4) { + for (outOff = 0, t = 0; outOff < size; outOff += len, ++t) { + const off = bitrev[t]; + this._singleRealTransform2(data, out, outOff, off >>> 1, step >>> 1); + } + } else { + // len === 8 + for (outOff = 0, t = 0; outOff < size; outOff += len, ++t) { + const off = bitrev[t]; + this._singleRealTransform4(data, out, outOff, off >>> 1, step >>> 1, inv); + } + } + + // Loop through steps in decreasing order + const table = this.table; + for (step >>= 2; step >= 2; step >>= 2) { + len = (size / step) << 1; + const halfLen = len >>> 1; + const quarterLen = halfLen >>> 1; + const hquarterLen = quarterLen >>> 1; + + // Loop through offsets in the data + for (outOff = 0; outOff < size; outOff += len) { + for (let i = 0, k = 0; i <= hquarterLen; i += 2, k += step) { + const A = outOff + i; + const B = A + quarterLen; + const C = B + quarterLen; + const D = C + quarterLen; + + // Original values + const Ar = out[A]; + const Ai = out[A + 1]; + const Br = out[B]; + const Bi = out[B + 1]; + const Cr = out[C]; + const Ci = out[C + 1]; + const Dr = out[D]; + const Di = out[D + 1]; + + // Middle values + const MAr = Ar; + const MAi = Ai; + + const tableBr = table[k]; + const tableBi = inv * table[k + 1]; + const MBr = Br * tableBr - Bi * tableBi; + const MBi = Br * tableBi + Bi * tableBr; + + const tableCr = table[2 * k]; + const tableCi = inv * table[2 * k + 1]; + const MCr = Cr * tableCr - Ci * tableCi; + const MCi = Cr * tableCi + Ci * tableCr; + + const tableDr = table[3 * k]; + const tableDi = inv * table[3 * k + 1]; + const MDr = Dr * tableDr - Di * tableDi; + const MDi = Dr * tableDi + Di * tableDr; + + // Pre-Final values + const T0r = MAr + MCr; + const T0i = MAi + MCi; + const T1r = MAr - MCr; + const T1i = MAi - MCi; + const T2r = MBr + MDr; + const T2i = MBi + MDi; + const T3r = inv * (MBr - MDr); + const T3i = inv * (MBi - MDi); + + // Final values + out[A] = T0r + T2r; + out[A + 1] = T0i + T2i; + out[B] = T1r + T3i; + out[B + 1] = T1i - T3r; + + // Output final middle point + if (i === 0) { + out[C] = T0r - T2r; + out[C + 1] = T0i - T2i; + continue; + } + + // Do not overwrite ourselves + if (i === hquarterLen) + continue; + + const SA = outOff + quarterLen - i; + const SB = outOff + halfLen - i; + + out[SA] = T1r - inv * T3i; + out[SA + 1] = -T1i - inv * T3r; + out[SB] = T0r - inv * T2r; + out[SB + 1] = -T0i + inv * T2i; + } + } + } + + // Complete the spectrum by adding its mirrored negative frequency components. + const half = size >>> 1; + for (let i = 2; i < half; i += 2) { + out[size - i] = out[i]; + out[size - i + 1] = -out[i + 1]; + } + } + + /** + * Performs a single real input radix-2 transformation on the provided data + * + * @param {Float64Array} data The input data array + * @param {Float64Array} out The output data array + * @param {number} outOff The output offset + * @param {number} off The input offset + * @param {number} step The step + * + * @returns {void} + */ + _singleRealTransform2(data, out, outOff, off, step) { + // radix-2 implementation + // NOTE: Only called for len=4 + + const evenR = data[off]; + const oddR = data[off + step]; + + out[outOff] = evenR + oddR; + out[outOff + 1] = 0; + out[outOff + 2] = evenR - oddR; + out[outOff + 3] = 0; + } + + /** + * Computes a single real-valued transform using radix-4 algorithm. + * This method is only called for len=8. + * + * @param {Float64Array} data The input data array. + * @param {Float64Array} out The output data array. + * @param {number} outOff The offset into the output array. + * @param {number} off The offset into the input array. + * @param {number} step The step size for the input array. + * @param {number} inv The value of inverse. + */ + _singleRealTransform4(data, out, outOff, off, step, inv) { + // radix-4 + // NOTE: Only called for len=8 + const step2 = step * 2; + const step3 = step * 3; + + // Original values + const Ar = data[off]; + const Br = data[off + step]; + const Cr = data[off + step2]; + const Dr = data[off + step3]; + + // Pre-Final values + const T0r = Ar + Cr; + const T1r = Ar - Cr; + const T2r = Br + Dr; + const T3r = inv * (Br - Dr); + + // Final values + out[outOff] = T0r + T2r; + out[outOff + 1] = 0; + out[outOff + 2] = T1r; + out[outOff + 3] = -T3r; + out[outOff + 4] = T0r - T2r; + out[outOff + 5] = 0; + out[outOff + 6] = T1r; + out[outOff + 7] = T3r; + } +} + +/** + * NP2FFT class provides functionality for performing Fast Fourier Transform on arrays + * which are not a power of two in length. In such cases, the chirp-z transform is used. + * + * For more information, see: https://math.stackexchange.com/questions/77118/non-power-of-2-ffts/77156#77156 + */ +class NP2FFT { + + /** + * Constructs a new NP2FFT object. + * @param {number} fft_length The length of the FFT + */ + constructor(fft_length) { + // Helper variables + const a = 2 * (fft_length - 1); + const b = 2 * (2 * fft_length - 1); + const nextP2 = 2 ** (Math.ceil(Math.log2(b))) + this.bufferSize = nextP2; + this._a = a; + + // Define buffers + // Compute chirp for transform + const chirp = new Float64Array(b); + const ichirp = new Float64Array(nextP2); + this._chirpBuffer = new Float64Array(nextP2); + this._buffer1 = new Float64Array(nextP2); + this._buffer2 = new Float64Array(nextP2); + this._outBuffer1 = new Float64Array(nextP2); + this._outBuffer2 = new Float64Array(nextP2); + + // Compute complex exponentiation + const theta = -2 * Math.PI / fft_length; + const baseR = Math.cos(theta); + const baseI = Math.sin(theta); + + // Precompute helper for chirp-z transform + for (let i = 0; i < b >> 1; ++i) { + // Compute complex power: + const e = (i + 1 - fft_length) ** 2 / 2.0; + + // Compute the modulus and argument of the result + const result_mod = Math.sqrt(baseR ** 2 + baseI ** 2) ** e; + const result_arg = e * Math.atan2(baseI, baseR); + + // Convert the result back to rectangular form + // and assign to chirp and ichirp + const i2 = 2 * i; + chirp[i2] = result_mod * Math.cos(result_arg); + chirp[i2 + 1] = result_mod * Math.sin(result_arg); + + // conjugate + ichirp[i2] = chirp[i2]; + ichirp[i2 + 1] = - chirp[i2 + 1]; + } + this._slicedChirpBuffer = chirp.subarray(a, b); + + // create object to perform Fast Fourier Transforms + // with `nextP2` complex numbers + this._f = new P2FFT(nextP2 >> 1); + this._f.transform(this._chirpBuffer, ichirp); + } + + _transform(output, input, real) { + const ib1 = this._buffer1; + const ib2 = this._buffer2; + const ob2 = this._outBuffer1; + const ob3 = this._outBuffer2; + const cb = this._chirpBuffer; + const sb = this._slicedChirpBuffer; + const a = this._a; + + if (real) { + // Real multiplication + for (let j = 0; j < sb.length; j += 2) { + const j2 = j + 1 + const j3 = j >> 1; + + const a_real = input[j3]; + ib1[j] = a_real * sb[j]; + ib1[j2] = a_real * sb[j2]; + } + } else { + // Complex multiplication + for (let j = 0; j < sb.length; j += 2) { + const j2 = j + 1 + ib1[j] = input[j] * sb[j] - input[j2] * sb[j2]; + ib1[j2] = input[j] * sb[j2] + input[j2] * sb[j]; + } + } + this._f.transform(ob2, ib1); + + for (let j = 0; j < cb.length; j += 2) { + const j2 = j + 1; + + ib2[j] = ob2[j] * cb[j] - ob2[j2] * cb[j2]; + ib2[j2] = ob2[j] * cb[j2] + ob2[j2] * cb[j]; + } + this._f.inverseTransform(ob3, ib2); + + for (let j = 0; j < ob3.length; j += 2) { + const a_real = ob3[j + a]; + const a_imag = ob3[j + a + 1]; + const b_real = sb[j]; + const b_imag = sb[j + 1]; + + output[j] = a_real * b_real - a_imag * b_imag; + output[j + 1] = a_real * b_imag + a_imag * b_real; + } + } + + transform(output, input) { + this._transform(output, input, false); + } + + realTransform(output, input) { + this._transform(output, input, true); + } +} + +class FFT { + constructor(fft_length) { + this.fft_length = fft_length; + this.isPowerOfTwo = isPowerOfTwo(fft_length); + if (this.isPowerOfTwo) { + this.fft = new P2FFT(fft_length); + this.outputBufferSize = 2 * fft_length; + } else { + this.fft = new NP2FFT(fft_length); + this.outputBufferSize = this.fft.bufferSize; + } + } + + realTransform(out, input) { + this.fft.realTransform(out, input); + } + + transform(out, input) { + this.fft.transform(out, input); + } +} + + +/** + * Performs median filter on the provided data. Padding is done by mirroring the data. + * @param {AnyTypedArray} data The input array + * @param {number} windowSize The window size + */ +function medianFilter(data, windowSize) { + + if (windowSize % 2 === 0 || windowSize <= 0) { + throw new Error('Window size must be a positive odd number'); + } + + // @ts-ignore + const outputArray = new data.constructor(data.length); + + // @ts-ignore + const buffer = new data.constructor(windowSize); // Reusable array for storing values + + const halfWindowSize = Math.floor(windowSize / 2); + + for (let i = 0; i < data.length; ++i) { + let valuesIndex = 0; + + for (let j = -halfWindowSize; j <= halfWindowSize; ++j) { + let index = i + j; + if (index < 0) { + index = Math.abs(index); + } else if (index >= data.length) { + index = 2 * (data.length - 1) - index; + } + + buffer[valuesIndex++] = data[index]; + } + + buffer.sort(); + outputArray[i] = buffer[halfWindowSize]; + } + + return outputArray; +} + +/** + * Helper function to round a number to a given number of decimals + * @param {number} num The number to round + * @param {number} decimals The number of decimals + * @returns {number} The rounded number + */ +function round(num, decimals) { + const pow = Math.pow(10, decimals); + return Math.round(num * pow) / pow; +} + +/** + * Helper function to round a number to the nearest integer, with ties rounded to the nearest even number. + * Also known as "bankers' rounding". This is the default rounding mode in python. For example: + * 1.5 rounds to 2 and 2.5 rounds to 2. + * + * @param {number} x The number to round + * @returns {number} The rounded number + */ +function bankers_round(x) { + const r = Math.round(x); + const br = Math.abs(x) % 1 === 0.5 ? (r % 2 === 0 ? r : r - 1) : r; + return br; +} + + +/** + * Measures similarity between two temporal sequences (e.g., input audio and output tokens + * to generate token-level timestamps). + * @param {number[][]} matrix + * @returns {number[][]} + */ +function dynamic_time_warping(matrix) { + const output_length = matrix.length; + const input_length = matrix[0].length; + + const outputShape = [output_length + 1, input_length + 1]; + + const cost = Array.from( + { length: outputShape[0] }, + () => Array(outputShape[1]).fill(Infinity) + ); + cost[0][0] = 0; + + const trace = Array.from( + { length: outputShape[0] }, + () => Array(outputShape[1]).fill(-1) + ); + + for (let j = 1; j < outputShape[1]; ++j) { + for (let i = 1; i < outputShape[0]; ++i) { + const c0 = cost[i - 1][j - 1]; + const c1 = cost[i - 1][j]; + const c2 = cost[i][j - 1]; + + let c, t; + if (c0 < c1 && c0 < c2) { + c = c0; + t = 0; + } else if (c1 < c0 && c1 < c2) { + c = c1; + t = 1; + } else { + c = c2; + t = 2; + } + cost[i][j] = matrix[i - 1][j - 1] + c; + trace[i][j] = t; + } + } + + for (let i = 0; i < outputShape[1]; ++i) { // trace[0, :] = 2 + trace[0][i] = 2; + } + for (let i = 0; i < outputShape[0]; ++i) { // trace[:, 0] = 1 + trace[i][0] = 1; + } + + // backtrace + let i = output_length; + let j = input_length; + let text_indices = []; + let time_indices = []; + while (i > 0 || j > 0) { + text_indices.push(i - 1); + time_indices.push(j - 1); + + switch (trace[i][j]) { + case 0: + --i; --j; + break; + case 1: + --i; + break; + case 2: + --j; + break; + default: + throw new Error( + `Internal error in dynamic time warping. Unexpected trace[${i}, ${j}]. Please file a bug report.` + ) + } + } + + text_indices.reverse(); + time_indices.reverse(); + + return [text_indices, time_indices]; + +} + + +/***/ }), + +/***/ "./src/utils/tensor.js": +/*!*****************************!*\ + !*** ./src/utils/tensor.js ***! + \*****************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ DataTypeMap: () => (/* binding */ DataTypeMap), +/* harmony export */ Tensor: () => (/* binding */ Tensor), +/* harmony export */ cat: () => (/* binding */ cat), +/* harmony export */ full: () => (/* binding */ full), +/* harmony export */ full_like: () => (/* binding */ full_like), +/* harmony export */ interpolate: () => (/* binding */ interpolate), +/* harmony export */ interpolate_4d: () => (/* binding */ interpolate_4d), +/* harmony export */ layer_norm: () => (/* binding */ layer_norm), +/* harmony export */ matmul: () => (/* binding */ matmul), +/* harmony export */ mean: () => (/* binding */ mean), +/* harmony export */ mean_pooling: () => (/* binding */ mean_pooling), +/* harmony export */ ones: () => (/* binding */ ones), +/* harmony export */ ones_like: () => (/* binding */ ones_like), +/* harmony export */ permute: () => (/* binding */ permute), +/* harmony export */ quantize_embeddings: () => (/* binding */ quantize_embeddings), +/* harmony export */ rand: () => (/* binding */ rand), +/* harmony export */ rfft: () => (/* binding */ rfft), +/* harmony export */ slice: () => (/* binding */ slice), +/* harmony export */ stack: () => (/* binding */ stack), +/* harmony export */ std_mean: () => (/* binding */ std_mean), +/* harmony export */ topk: () => (/* binding */ topk), +/* harmony export */ zeros: () => (/* binding */ zeros), +/* harmony export */ zeros_like: () => (/* binding */ zeros_like) +/* harmony export */ }); +/* harmony import */ var _maths_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./maths.js */ "./src/utils/maths.js"); +/* harmony import */ var _backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../backends/onnx.js */ "./src/backends/onnx.js"); +/* harmony import */ var _ops_registry_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../ops/registry.js */ "./src/ops/registry.js"); +/** + * @file Helper module for `Tensor` processing. + * + * These functions and classes are only used internally, + * meaning an end-user shouldn't need to access anything here. + * + * @module utils/tensor + */ + + + + + + + +const DataTypeMap = Object.freeze({ + float32: Float32Array, + // @ts-ignore ts(2552) Limited availability of Float16Array across browsers: + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float16Array + float16: typeof Float16Array !== "undefined" ? Float16Array: Uint16Array, + float64: Float64Array, + string: Array, // string[] + int8: Int8Array, + uint8: Uint8Array, + int16: Int16Array, + uint16: Uint16Array, + int32: Int32Array, + uint32: Uint32Array, + int64: BigInt64Array, + uint64: BigUint64Array, + bool: Uint8Array, + uint4: Uint8Array, + int4: Int8Array, +}); + +/** + * @typedef {keyof typeof DataTypeMap} DataType + * @typedef {import('./maths.js').AnyTypedArray | any[]} DataArray + */ + + +class Tensor { + /** @type {number[]} Dimensions of the tensor. */ + get dims() { + // @ts-ignore + return this.ort_tensor.dims; + } + set dims(value) { + // FIXME: ONNXTensor declares dims as readonly so one needs to use the constructor() if dims change. + // @ts-ignore + this.ort_tensor.dims = value; + } + + /** @type {DataType} Type of the tensor. */ + get type() { + return this.ort_tensor.type; + }; + + /** @type {DataArray} The data stored in the tensor. */ + get data() { + return this.ort_tensor.data; + } + + /** @type {number} The number of elements in the tensor. */ + get size() { + return this.ort_tensor.size; + }; + + /** @type {string} The location of the tensor data. */ + get location() { + return this.ort_tensor.location; + }; + + ort_tensor; + + /** + * Create a new Tensor or copy an existing Tensor. + * @param {[DataType, DataArray, number[]]|[ONNXTensor]} args + */ + constructor(...args) { + if ((0,_backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.isONNXTensor)(args[0])) { + this.ort_tensor = /** @type {ONNXTensor} */ (args[0]); + } else { + // Create new tensor + this.ort_tensor = new _backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.Tensor( + /** @type {DataType} */(args[0]), + // @ts-expect-error ts(2769) Type 'number' is not assignable to type 'bigint'. + /** @type {Exclude} */(args[1]), + args[2], + ); + } + + return new Proxy(this, { + get: (obj, key) => { + if (typeof key === 'string') { + let index = Number(key); + if (Number.isInteger(index)) { + // key is an integer (i.e., index) + return obj._getitem(index); + } + } + // @ts-ignore + return obj[key]; + }, + set: (obj, key, value) => { + // TODO allow setting of data + + // @ts-ignore + return obj[key] = value; + } + }); + } + + dispose() { + this.ort_tensor.dispose(); + // this.ort_tensor = undefined; + } + + /** + * Returns an iterator object for iterating over the tensor data in row-major order. + * If the tensor has more than one dimension, the iterator will yield subarrays. + * @returns {Iterator} An iterator object for iterating over the tensor data in row-major order. + */ + *[Symbol.iterator]() { + const [iterLength, ...iterDims] = this.dims; + + if (iterDims.length > 0) { + const iterSize = iterDims.reduce((a, b) => a * b); + for (let i = 0; i < iterLength; ++i) { + yield this._subarray(i, iterSize, iterDims); + } + } else { + yield* this.data + } + + } + + /** + * Index into a Tensor object. + * @param {number} index The index to access. + * @returns {Tensor} The data at the specified index. + */ + _getitem(index) { + const [iterLength, ...iterDims] = this.dims; + + index = safeIndex(index, iterLength); + + if (iterDims.length > 0) { + const iterSize = iterDims.reduce((a, b) => a * b); + return this._subarray(index, iterSize, iterDims); + } else { + return new Tensor(this.type, [this.data[index]], iterDims); + } + } + + /** + * @param {number|bigint} item The item to search for in the tensor + * @returns {number} The index of the first occurrence of item in the tensor data. + */ + indexOf(item) { + const this_data = this.data; + for (let index = 0; index < this_data.length; ++index) { + // Note: == instead of === so we can match Ints with BigInts + if (this_data[index] == item) { + return index; + } + } + return -1; + } + + /** + * @param {number} index + * @param {number} iterSize + * @param {any} iterDims + * @returns {Tensor} + */ + _subarray(index, iterSize, iterDims) { + const o1 = index * iterSize; + const o2 = (index + 1) * iterSize; + + // We use subarray if available (typed array), otherwise we use slice (normal array) + const data = + ('subarray' in this.data) + ? this.data.subarray(o1, o2) + : this.data.slice(o1, o2); + return new Tensor(this.type, data, iterDims); + } + + /** + * Returns the value of this tensor as a standard JavaScript Number. This only works + * for tensors with one element. For other cases, see `Tensor.tolist()`. + * @returns {number|bigint} The value of this tensor as a standard JavaScript Number. + * @throws {Error} If the tensor has more than one element. + */ + item() { + const this_data = this.data; + if (this_data.length !== 1) { + throw new Error(`a Tensor with ${this_data.length} elements cannot be converted to Scalar`); + } + return this_data[0]; + } + + /** + * Convert tensor data to a n-dimensional JS list + * @returns {Array} + */ + tolist() { + return reshape(this.data, this.dims) + } + + /** + * Return a new Tensor with the sigmoid function applied to each element. + * @returns {Tensor} The tensor with the sigmoid function applied. + */ + sigmoid() { + return this.clone().sigmoid_(); + } + + /** + * Applies the sigmoid function to the tensor in place. + * @returns {Tensor} Returns `this`. + */ + sigmoid_() { + const this_data = this.data; + for (let i = 0; i < this_data.length; ++i) { + this_data[i] = 1 / (1 + Math.exp(-this_data[i])); + } + return this; + } + + /** + * Return a new Tensor with a callback function applied to each element. + * @param {Function} callback - The function to apply to each element. It should take three arguments: + * the current element, its index, and the tensor's data array. + * @returns {Tensor} A new Tensor with the callback function applied to each element. + */ + map(callback) { + return this.clone().map_(callback); + } + + /** + * Apply a callback function to each element of the tensor in place. + * @param {Function} callback - The function to apply to each element. It should take three arguments: + * the current element, its index, and the tensor's data array. + * @returns {Tensor} Returns `this`. + */ + map_(callback) { + const this_data = this.data; + for (let i = 0; i < this_data.length; ++i) { + this_data[i] = callback(this_data[i], i, this_data); + } + return this; + } + + /** + * Return a new Tensor with every element multiplied by a constant. + * @param {number} val The value to multiply by. + * @returns {Tensor} The new tensor. + */ + mul(val) { + return this.clone().mul_(val); + } + + /** + * Multiply the tensor by a constant in place. + * @param {number} val The value to multiply by. + * @returns {Tensor} Returns `this`. + */ + mul_(val) { + const this_data = this.data; + for (let i = 0; i < this_data.length; ++i) { + this_data[i] *= val; + } + return this; + } + + /** + * Return a new Tensor with every element divided by a constant. + * @param {number} val The value to divide by. + * @returns {Tensor} The new tensor. + */ + div(val) { + return this.clone().div_(val); + } + + /** + * Divide the tensor by a constant in place. + * @param {number} val The value to divide by. + * @returns {Tensor} Returns `this`. + */ + div_(val) { + const this_data = this.data; + for (let i = 0; i < this_data.length; ++i) { + this_data[i] /= val; + } + return this; + } + + /** + * Return a new Tensor with every element added by a constant. + * @param {number} val The value to add by. + * @returns {Tensor} The new tensor. + */ + add(val) { + return this.clone().add_(val); + } + + /** + * Add the tensor by a constant in place. + * @param {number} val The value to add by. + * @returns {Tensor} Returns `this`. + */ + add_(val) { + const this_data = this.data; + for (let i = 0; i < this_data.length; ++i) { + this_data[i] += val; + } + return this; + } + + /** + * Return a new Tensor with every element subtracted by a constant. + * @param {number} val The value to subtract by. + * @returns {Tensor} The new tensor. + */ + sub(val) { + return this.clone().sub_(val); + } + + /** + * Subtract the tensor by a constant in place. + * @param {number} val The value to subtract by. + * @returns {Tensor} Returns `this`. + */ + sub_(val) { + const this_data = this.data; + for (let i = 0; i < this_data.length; ++i) { + this_data[i] -= val; + } + return this; + } + + /** + * Creates a deep copy of the current Tensor. + * @returns {Tensor} A new Tensor with the same type, data, and dimensions as the original. + */ + clone() { + return new Tensor(this.type, this.data.slice(), this.dims.slice()); + } + + /** + * Performs a slice operation on the Tensor along specified dimensions. + * + * Consider a Tensor that has a dimension of [4, 7]: + * ``` + * [ 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] + * ``` + * We can slice against the two dims of row and column, for instance in this + * case we can start at the second element, and return to the second last, + * like this: + * ``` + * tensor.slice([1, -1], [1, -1]); + * ``` + * which would return: + * ``` + * [ 9, 10, 11, 12, 13 ] + * [ 16, 17, 18, 19, 20 ] + * ``` + * + * @param {...(number|number[]|null)} slices The slice specifications for each dimension. + * - If a number is given, then a single element is selected. + * - If an array of two numbers is given, then a range of elements [start, end (exclusive)] is selected. + * - If null is given, then the entire dimension is selected. + * @returns {Tensor} A new Tensor containing the selected elements. + * @throws {Error} If the slice input is invalid. + */ + slice(...slices) { + // This allows for slicing with ranges and numbers + const newTensorDims = []; + const newOffsets = []; + + // slices is an array of numbers or arrays of numbers + // e.g., slices = [0, [1, 3], null, [0, 3]] + for (let sliceIndex = 0; sliceIndex < this.dims.length; ++sliceIndex) { + let slice = slices[sliceIndex]; + + if (slice === null || slice === undefined) { + // null or undefined means take the whole dimension + newOffsets.push([0, this.dims[sliceIndex]]); + newTensorDims.push(this.dims[sliceIndex]); + + } else if (typeof slice === 'number') { + slice = safeIndex(slice, this.dims[sliceIndex], sliceIndex); + + // A number means take a single element + newOffsets.push([slice, slice + 1]); + + } else if (Array.isArray(slice) && slice.length === 2) { + // An array of length 2 means take a range of elements + let [start, end] = slice; + start = start === null + ? 0 + : safeIndex(start, this.dims[sliceIndex], sliceIndex, false); + end = end === null + ? this.dims[sliceIndex] + : safeIndex(end, this.dims[sliceIndex], sliceIndex, false); + + if (start > end) { + throw new Error(`Invalid slice: ${slice}`); + } + + const offsets = [ + Math.max(start, 0), + Math.min(end, this.dims[sliceIndex]) + ]; + + newOffsets.push(offsets); + newTensorDims.push(offsets[1] - offsets[0]); + + } else { + throw new Error(`Invalid slice: ${slice}`); + } + } + + const newDims = newOffsets.map(([start, end]) => end - start); + const newBufferSize = newDims.reduce((a, b) => a * b); + + const this_data = this.data; + // Allocate memory + // @ts-ignore + const data = new this_data.constructor(newBufferSize); + + // Precompute strides + const stride = this.stride(); + + for (let i = 0; i < newBufferSize; ++i) { + let originalIndex = 0; + for (let j = newDims.length - 1, num = i; j >= 0; --j) { + const size = newDims[j]; + originalIndex += ((num % size) + newOffsets[j][0]) * stride[j]; + num = Math.floor(num / size); + } + data[i] = this_data[originalIndex]; + } + return new Tensor(this.type, data, newTensorDims); + } + + /** + * Return a permuted version of this Tensor, according to the provided dimensions. + * @param {...number} dims Dimensions to permute. + * @returns {Tensor} The permuted tensor. + */ + permute(...dims) { + return permute(this, dims); + } + + // TODO: implement transpose. For now (backwards compatibility), it's just an alias for permute() + transpose(...dims) { + return this.permute(...dims); + } + + /** + * Returns the sum of each row of the input tensor in the given dimension dim. + * + * @param {number} [dim=null] The dimension or dimensions to reduce. If `null`, all dimensions are reduced. + * @param {boolean} keepdim Whether the output tensor has `dim` retained or not. + * @returns The summed tensor + */ + sum(dim = null, keepdim = false) { + return this.norm(1, dim, keepdim); + } + + /** + * Returns the matrix norm or vector norm of a given tensor. + * @param {number|string} [p='fro'] The order of norm + * @param {number} [dim=null] Specifies which dimension of the tensor to calculate the norm across. + * If dim is None, the norm will be calculated across all dimensions of input. + * @param {boolean} [keepdim=false] Whether the output tensors have dim retained or not. + * @returns {Tensor} The norm of the tensor. + */ + norm(p = 'fro', dim = null, keepdim = false) { + if (p === 'fro') { + // NOTE: Since we only support integer dims, Frobenius norm produces the same result as p=2. + p = 2; + } else if (typeof p === 'string') { + throw Error(`Unsupported norm: ${p}`); + } + + const this_data = this.data; + const fn = (a, b) => a + (b ** p); + + if (dim === null) { + // @ts-ignore + const val = this_data.reduce(fn, 0) ** (1 / p); + return new Tensor(this.type, [val], []); + } + + const [type, result, resultDims] = reduce_helper(fn, this, dim, keepdim); + + if (p !== 1) { + for (let i = 0; i < result.length; ++i) { + result[i] = result[i] ** (1 / p); + } + } + return new Tensor(type, result, resultDims); + } + + /** + * Performs `L_p` normalization of inputs over specified dimension. Operates in place. + * @param {number} [p=2] The exponent value in the norm formulation + * @param {number} [dim=1] The dimension to reduce + * @returns {Tensor} `this` for operation chaining. + */ + normalize_(p = 2.0, dim = 1) { + dim = safeIndex(dim, this.dims.length); + + const norm = this.norm(p, dim, true); + + const this_data = this.data; + const norm_data = norm.data; + for (let i = 0; i < this_data.length; ++i) { + + // Calculate the index in the resulting array + let resultIndex = 0; + + for (let j = this.dims.length - 1, num = i, resultMultiplier = 1; j >= 0; --j) { + const size = this.dims[j]; + if (j !== dim) { + const index = num % size; + resultIndex += index * resultMultiplier; + resultMultiplier *= this.dims[j]; + } + num = Math.floor(num / size); + } + + // Divide by normalized value + this_data[i] /= norm_data[resultIndex]; + } + + return this; + } + + /** + * Performs `L_p` normalization of inputs over specified dimension. + * @param {number} [p=2] The exponent value in the norm formulation + * @param {number} [dim=1] The dimension to reduce + * @returns {Tensor} The normalized tensor. + */ + normalize(p = 2.0, dim = 1) { + return this.clone().normalize_(p, dim); + } + + /** + * Compute and return the stride of this tensor. + * Stride is the jump necessary to go from one element to the next one in the specified dimension dim. + * @returns {number[]} The stride of this tensor. + */ + stride() { + return dimsToStride(this.dims); + } + + /** + * Returns a tensor with all specified dimensions of input of size 1 removed. + * + * NOTE: The returned tensor shares the storage with the input tensor, so changing the contents of one will change the contents of the other. + * If you would like a copy, use `tensor.clone()` before squeezing. + * + * @param {number|number[]} [dim=null] If given, the input will be squeezed only in the specified dimensions. + * @returns {Tensor} The squeezed tensor + */ + squeeze(dim = null) { + return new Tensor( + this.type, + this.data, + calc_squeeze_dims(this.dims, dim) + ) + } + + /** + * In-place version of @see {@link Tensor.squeeze} + */ + squeeze_(dim = null) { + this.dims = calc_squeeze_dims(this.dims, dim); + return this; + } + + /** + * Returns a new tensor with a dimension of size one inserted at the specified position. + * + * NOTE: The returned tensor shares the same underlying data with this tensor. + * + * @param {number} dim The index at which to insert the singleton dimension + * @returns {Tensor} The unsqueezed tensor + */ + unsqueeze(dim = null) { + return new Tensor( + this.type, + this.data, + calc_unsqueeze_dims(this.dims, dim) + ); + } + + /** + * In-place version of @see {@link Tensor.unsqueeze} + */ + unsqueeze_(dim = null) { + this.dims = calc_unsqueeze_dims(this.dims, dim); + return this; + } + + /** + * In-place version of @see {@link Tensor.flatten} + */ + flatten_(start_dim = 0, end_dim = -1) { + // TODO validate inputs + end_dim = (end_dim + this.dims.length) % this.dims.length; + + let dimsToKeepBefore = this.dims.slice(0, start_dim); + let dimsToFlatten = this.dims.slice(start_dim, end_dim + 1); + let dimsToKeepAfter = this.dims.slice(end_dim + 1); + + this.dims = [...dimsToKeepBefore, dimsToFlatten.reduce((a, b) => a * b, 1), ...dimsToKeepAfter] + return this; + } + + /** + * Flattens input by reshaping it into a one-dimensional tensor. + * If `start_dim` or `end_dim` are passed, only dimensions starting with `start_dim` + * and ending with `end_dim` are flattened. The order of elements in input is unchanged. + * @param {number} start_dim the first dim to flatten + * @param {number} end_dim the last dim to flatten + * @returns {Tensor} The flattened tensor. + */ + flatten(start_dim = 0, end_dim = -1) { + return this.clone().flatten_(start_dim, end_dim); + } + + /** + * Returns a new tensor with the same data as the `self` tensor but of a different `shape`. + * @param {...number} dims the desired size + * @returns {Tensor} The tensor with the same data but different shape + */ + view(...dims) { + // TODO: validate dims + let inferredIndex = -1; + for (let i = 0; i < dims.length; ++i) { + if (dims[i] === -1) { + if (inferredIndex !== -1) { + throw new Error("Only one dimension can be inferred"); + } + inferredIndex = i; + } + } + + const this_data = this.data; + if (inferredIndex !== -1) { + // Some dimension must be inferred + const productOther = dims.reduce((product, curr, index) => { + return index !== inferredIndex ? product * curr : product + }, 1); + + dims[inferredIndex] = this_data.length / productOther; + } + return new Tensor(this.type, this_data, dims); // NOTE: uses same underlying storage + } + + neg_() { + const this_data = this.data; + for (let i = 0; i < this_data.length; ++i) { + this_data[i] = -this_data[i]; + } + return this; + } + neg() { + return this.clone().neg_(); + } + + /** + * Computes input > val element-wise. + * @param {number} val The value to compare with. + * @returns {Tensor} A boolean tensor that is `true` where input is greater than other and `false` elsewhere. + */ + gt(val) { + const mask = new Uint8Array(this.data.length); + const this_data = this.data; + for (let i = 0; i < this_data.length; ++i) { + mask[i] = this_data[i] > val ? 1 : 0; + } + return new Tensor('bool', mask, this.dims); + } + + /** + * Computes input < val element-wise. + * @param {number} val The value to compare with. + * @returns {Tensor} A boolean tensor that is `true` where input is less than other and `false` elsewhere. + */ + lt(val) { + const mask = new Uint8Array(this.data.length); + const this_data = this.data; + for (let i = 0; i < this_data.length; ++i) { + mask[i] = this_data[i] < val ? 1 : 0; + } + return new Tensor('bool', mask, this.dims); + } + + /** + * In-place version of @see {@link Tensor.clamp} + */ + clamp_(min, max) { + const this_data = this.data; + for (let i = 0; i < this_data.length; ++i) { + this_data[i] = Math.min(Math.max(this_data[i], min), max); + } + return this; + } + + /** + * Clamps all elements in input into the range [ min, max ] + * @param {number} min lower-bound of the range to be clamped to + * @param {number} max upper-bound of the range to be clamped to + * @returns {Tensor} the output tensor. + */ + clamp(min, max) { + return this.clone().clamp_(min, max); + } + + /** + * In-place version of @see {@link Tensor.round} + */ + round_() { + const this_data = this.data; + for (let i = 0; i < this_data.length; ++i) { + this_data[i] = Math.round(this_data[i]); + } + return this; + } + + /** + * Rounds elements of input to the nearest integer. + * @returns {Tensor} the output tensor. + */ + round() { + return this.clone().round_(); + } + + mean(dim = null, keepdim = false) { + return mean(this, dim, keepdim); + } + + min(dim = null, keepdim = false) { + if (dim === null) { + // None to reduce over all dimensions. + const val = (0,_maths_js__WEBPACK_IMPORTED_MODULE_0__.min)(this.data)[0]; + return new Tensor(this.type, [val], [/* scalar */]); + } + const [type, result, resultDims] = reduce_helper((a, b) => Math.min(a, b), this, dim, keepdim, Infinity); + return new Tensor(type, result, resultDims); + } + + max(dim = null, keepdim = false) { + if (dim === null) { + // None to reduce over all dimensions. + const val = (0,_maths_js__WEBPACK_IMPORTED_MODULE_0__.max)(this.data)[0]; + return new Tensor(this.type, [val], [/* scalar */]); + } + const [type, result, resultDims] = reduce_helper((a, b) => Math.max(a, b), this, dim, keepdim, -Infinity); + return new Tensor(type, result, resultDims); + } + + argmin(dim = null, keepdim = false) { + if (dim !== null) { + throw new Error("`dim !== null` not yet implemented."); + } + const index = (0,_maths_js__WEBPACK_IMPORTED_MODULE_0__.min)(this.data)[1]; + return new Tensor('int64', [BigInt(index)], []); + } + argmax(dim = null, keepdim = false) { + if (dim !== null) { + throw new Error("`dim !== null` not yet implemented."); + } + const index = (0,_maths_js__WEBPACK_IMPORTED_MODULE_0__.max)(this.data)[1]; + return new Tensor('int64', [BigInt(index)], []); + } + + /** + * Performs Tensor dtype conversion. + * @param {DataType} type The desired data type. + * @returns {Tensor} The converted tensor. + */ + to(type) { + // If the self Tensor already has the correct dtype, then self is returned. + if (this.type === type) return this; + + // Otherwise, the returned tensor is a copy of self with the desired dtype. + if (!DataTypeMap.hasOwnProperty(type)) { + throw new Error(`Unsupported type: ${type}`); + } + + // Handle special cases where a mapping function is needed (e.g., where one type is a bigint and the other is a number) + let map_fn; + const is_source_bigint = ['int64', 'uint64'].includes(this.type); + const is_dest_bigint = ['int64', 'uint64'].includes(type); + if (is_source_bigint && !is_dest_bigint) { + // TypeError: Cannot convert a BigInt value to a number + map_fn = Number; + } else if (!is_source_bigint && is_dest_bigint) { + // TypeError: Cannot convert [x] to a BigInt + map_fn = BigInt; + } + + // @ts-ignore + return new Tensor(type, DataTypeMap[type].from(this.data, map_fn), this.dims); + } +} + +/** + * This creates a nested array of a given type and depth (see examples). + * + * @example + * NestArray; // string[] + * @example + * NestArray; // number[][] + * @example + * NestArray; // string[][][] etc. + * @template T + * @template {number} Depth + * @template {never[]} [Acc=[]] + * @typedef {Acc['length'] extends Depth ? T : NestArray} NestArray + */ + +/** + * Reshapes a 1-dimensional array into an n-dimensional array, according to the provided dimensions. + * + * @example + * reshape([10 ], [1 ]); // Type: number[] Value: [10] + * reshape([1, 2, 3, 4 ], [2, 2 ]); // Type: number[][] Value: [[1, 2], [3, 4]] + * reshape([1, 2, 3, 4, 5, 6, 7, 8], [2, 2, 2]); // Type: number[][][] Value: [[[1, 2], [3, 4]], [[5, 6], [7, 8]]] + * reshape([1, 2, 3, 4, 5, 6, 7, 8], [4, 2 ]); // Type: number[][] Value: [[1, 2], [3, 4], [5, 6], [7, 8]] + * @param {T[]|DataArray} data The input array to reshape. + * @param {DIM} dimensions The target shape/dimensions. + * @template T + * @template {[number]|number[]} DIM + * @returns {NestArray} The reshaped array. + */ +function reshape(data, dimensions) { + + const totalElements = data.length; + const dimensionSize = dimensions.reduce((a, b) => a * b); + + if (totalElements !== dimensionSize) { + throw Error(`cannot reshape array of size ${totalElements} into shape (${dimensions})`); + } + + /** @type {any} */ + let reshapedArray = data; + + for (let i = dimensions.length - 1; i >= 0; i--) { + reshapedArray = reshapedArray.reduce((acc, val) => { + let lastArray = acc[acc.length - 1]; + + if (lastArray.length < dimensions[i]) { + lastArray.push(val); + } else { + acc.push([val]); + } + + return acc; + }, [[]]); + } + + return reshapedArray[0]; +} + +/** + * Permutes a tensor according to the provided axes. + * @param {any} tensor The input tensor to permute. + * @param {Array} axes The axes to permute the tensor along. + * @returns {Tensor} The permuted tensor. + */ +function permute(tensor, axes) { + const [permutedData, shape] = (0,_maths_js__WEBPACK_IMPORTED_MODULE_0__.permute_data)(tensor.data, tensor.dims, axes); + return new Tensor(tensor.type, permutedData, shape); +} + + +/** + * Interpolates an Tensor to the given size. + * @param {Tensor} input The input tensor to interpolate. Data must be channel-first (i.e., [c, h, w]) + * @param {number[]} size The output size of the image + * @param {string} mode The interpolation mode + * @param {boolean} align_corners Whether to align corners. + * @returns {Tensor} The interpolated tensor. + */ +function interpolate(input, [out_height, out_width], mode = 'bilinear', align_corners = false) { + + // Input image dimensions + const in_channels = input.dims.at(-3) ?? 1; + const in_height = input.dims.at(-2); + const in_width = input.dims.at(-1); + + let output = (0,_maths_js__WEBPACK_IMPORTED_MODULE_0__.interpolate_data)( + /** @type {import('./maths.js').TypedArray}*/(input.data), + [in_channels, in_height, in_width], + [out_height, out_width], + mode, + align_corners + ); + return new Tensor(input.type, output, [in_channels, out_height, out_width]); +} + + +/** + * Down/up samples the input. + * Inspired by https://pytorch.org/docs/stable/generated/torch.nn.functional.interpolate.html. + * @param {Tensor} input the input tensor + * @param {Object} options the options for the interpolation + * @param {[number, number]|[number, number, number]|[number, number, number, number]} [options.size=null] output spatial size. + * @param {"nearest"|"bilinear"|"bicubic"} [options.mode='bilinear'] algorithm used for upsampling + * @returns {Promise} The interpolated tensor. + */ +async function interpolate_4d(input, { + size = null, + mode = 'bilinear', +} = {}) { + + // Error checking + if (input.dims.length !== 4) { + throw new Error('`interpolate_4d` currently only supports 4D input.'); + } + if (!size) { + // TODO: support scale_factor + throw new Error('`interpolate_4d` requires a `size` argument.'); + } + + // Fill in missing dimensions + let targetDims; + if (size.length === 2) { + targetDims = [...input.dims.slice(0, 2), ...size]; + } else if (size.length === 3) { + targetDims = [input.dims[0], ...size]; + } else if (size.length === 4) { + targetDims = size; + } else { + throw new Error('`size` must be of length 2, 3, or 4.'); + } + + let op; + if (mode === 'nearest') { + op = await _ops_registry_js__WEBPACK_IMPORTED_MODULE_2__.TensorOpRegistry.nearest_interpolate_4d; + } else if (mode === 'bilinear') { + op = await _ops_registry_js__WEBPACK_IMPORTED_MODULE_2__.TensorOpRegistry.bilinear_interpolate_4d; + } else if (mode === 'bicubic') { + op = await _ops_registry_js__WEBPACK_IMPORTED_MODULE_2__.TensorOpRegistry.bicubic_interpolate_4d; + } else { + throw new Error(`Unsupported mode: ${mode}`); + } + + const sizeTensor = new Tensor('int64', new BigInt64Array(targetDims.map(BigInt)), [targetDims.length]); + return await op({ x: input, s: sizeTensor }); +} + +/** + * Matrix product of two tensors. + * Inspired by https://pytorch.org/docs/stable/generated/torch.matmul.html + * @param {Tensor} a the first tensor to be multiplied + * @param {Tensor} b the second tensor to be multiplied + * @returns {Promise} The matrix product of the two tensors. + */ +async function matmul(a, b) { + const op = await _ops_registry_js__WEBPACK_IMPORTED_MODULE_2__.TensorOpRegistry.matmul; + return await op({ a, b }); +} + +/** + * Computes the one dimensional Fourier transform of real-valued input. + * Inspired by https://pytorch.org/docs/stable/generated/torch.fft.rfft.html + * @param {Tensor} x the real input tensor + * @param {Tensor} a The dimension along which to take the one dimensional real FFT. + * @returns {Promise} the output tensor. + */ +async function rfft(x, a) { + const op = await _ops_registry_js__WEBPACK_IMPORTED_MODULE_2__.TensorOpRegistry.rfft; + return await op({ x, a }); +} + + +/** + * Returns the k largest elements of the given input tensor. + * Inspired by https://pytorch.org/docs/stable/generated/torch.topk.html + * @param {Tensor} x the input tensor + * @param {number} [k] the k in "top-k" + * @returns {Promise<[Tensor, Tensor]>} the output tuple of (Tensor, LongTensor) of top-k elements and their indices. + */ +async function topk(x, k) { + const op = await _ops_registry_js__WEBPACK_IMPORTED_MODULE_2__.TensorOpRegistry.top_k; + + if (k == null) { + k = x.dims.at(-1); + } else { + k = Math.min(k, x.dims.at(-1)); + } + return await op({ + x, + k: new Tensor( + 'int64', + [BigInt(k)], + [1] + ) + }); +} + + +const arrayToIndexTensor = (array) => new Tensor('int64', array, [array.length]); +/** + * Slice a multidimensional float32 tensor. + * @param {Tensor} data: Tensor of data to extract slices from + * @param {number[]} starts: 1-D array of starting indices of corresponding axis in axes + * @param {number[]} ends: 1-D array of ending indices (exclusive) of corresponding axis in axes + * @param {number[]} axes: 1-D array of axes that starts and ends apply to + * @param {number[]} [steps]: 1-D array of slice step of corresponding axis in axes. + * @returns {Promise} Sliced data tensor. + */ +async function slice(data, starts, ends, axes, steps) { + const op = await _ops_registry_js__WEBPACK_IMPORTED_MODULE_2__.TensorOpRegistry.slice; + return await op({ + x: data, + s: arrayToIndexTensor(starts), + e: arrayToIndexTensor(ends), + a: arrayToIndexTensor(axes), + t: arrayToIndexTensor(steps ?? new Array(axes.length).fill(1)), + }); +} + + +/** + * Perform mean pooling of the last hidden state followed by a normalization step. + * @param {Tensor} last_hidden_state Tensor of shape [batchSize, seqLength, embedDim] + * @param {Tensor} attention_mask Tensor of shape [batchSize, seqLength] + * @returns {Tensor} Returns a new Tensor of shape [batchSize, embedDim]. + */ +function mean_pooling(last_hidden_state, attention_mask) { + // last_hidden_state: [batchSize, seqLength, embedDim] + // attention_mask: [batchSize, seqLength] + const lastHiddenStateData = last_hidden_state.data; + const attentionMaskData = attention_mask.data; + + const shape = [last_hidden_state.dims[0], last_hidden_state.dims[2]]; + + // @ts-ignore + const returnedData = new lastHiddenStateData.constructor(shape[0] * shape[1]); + const [batchSize, seqLength, embedDim] = last_hidden_state.dims; + + let outIndex = 0; + for (let i = 0; i < batchSize; ++i) { + const offset = i * embedDim * seqLength; + + for (let k = 0; k < embedDim; ++k) { + let sum = 0; + let count = 0; + + const attnMaskOffset = i * seqLength; + const offset2 = offset + k; + // Pool over all words in sequence + for (let j = 0; j < seqLength; ++j) { + // index into attention mask + const attn = Number(attentionMaskData[attnMaskOffset + j]); + + count += attn; + sum += lastHiddenStateData[offset2 + j * embedDim] * attn; + } + + const avg = sum / count; + returnedData[outIndex++] = avg; + } + } + + return new Tensor( + last_hidden_state.type, + returnedData, + shape + ) +} + +/** + * Apply Layer Normalization for last certain number of dimensions. + * @param {Tensor} input The input tensor + * @param {number[]} normalized_shape input shape from an expected input of size + * @param {Object} options The options for the layer normalization + * @param {number} [options.eps=1e-5] A value added to the denominator for numerical stability. + * @returns {Tensor} The normalized tensor. + */ +function layer_norm(input, normalized_shape, { + eps = 1e-5, +} = {}) { + if (input.dims.length !== 2) { + throw new Error('`layer_norm` currently only supports 2D input.'); + } + + const [batchSize, featureDim] = input.dims; + + if (normalized_shape.length !== 1 && normalized_shape[0] !== featureDim) { + throw new Error('`normalized_shape` must be a 1D array with shape `[input.dims[1]]`.'); + } + + const [std, mean] = std_mean(input, 1, 0, true); + const stdData = /** @type {Float32Array} */(std.data); + const meanData = /** @type {Float32Array} */(mean.data); + + const inputData = /** @type {Float32Array} */(input.data); + + // @ts-ignore + const returnedData = new inputData.constructor(inputData.length); + + for (let i = 0; i < batchSize; ++i) { + const offset = i * featureDim; + for (let j = 0; j < featureDim; ++j) { + const offset2 = offset + j; + returnedData[offset2] = (inputData[offset2] - meanData[i]) / (stdData[i] + eps); + } + } + return new Tensor(input.type, returnedData, input.dims); +} + +/** + * Helper function to calculate new dimensions when performing a squeeze operation. + * @param {number[]} dims The dimensions of the tensor. + * @param {number|number[]|null} dim The dimension(s) to squeeze. + * @returns {number[]} The new dimensions. + * @private + */ +function calc_squeeze_dims(dims, dim) { + dims = dims.slice(); + if (dim === null) { + dims = dims.filter((d) => d !== 1); + } else if (typeof dim === 'number') { + if (dims[dim] === 1) { + dims.splice(dim, 1); + } + } else if (Array.isArray(dim)) { + dims = dims.filter((x, i) => { + return x !== 1 || !dim.includes(i); + }); + } + return dims; +} + +/** + * Helper function to calculate new dimensions when performing an unsqueeze operation. + * @param {number[]} dims The dimensions of the tensor. + * @param {number} dim The dimension to unsqueeze. + * @returns {number[]} The new dimensions. + * @private + */ +function calc_unsqueeze_dims(dims, dim) { + // Dimension out of range (e.g., "expected to be in range of [-4, 3], but got 4") + // + 1 since we allow inserting at the end (i.e. dim = -1) + dim = safeIndex(dim, dims.length + 1); + dims = dims.slice(); + // Insert 1 into specified dimension + dims.splice(dim, 0, 1); + return dims; +} + +/** + * Safely calculate the index for an array of a given size, allowing negative indexing. + * @param {number} index The index that will be used. + * @param {number} size The size of the array. + * @param {number} [dimension=null] The dimension that the index is for (optional). + * @returns {number} The index, guaranteed to be non-negative and less than `arrayLength`. + * + * @throws {Error} If the index is out of range. + * @private + */ +function safeIndex(index, size, dimension = null, boundsCheck = true) { + if (index < -size || index >= size) { + if (boundsCheck) { + throw new Error(`IndexError: index ${index} is out of bounds for dimension${dimension === null ? '' : ' ' + dimension} with size ${size}`); + } else { + return index < -size ? 0 : size; + } + } + + if (index < 0) { + // Negative indexing, ensuring positive index + index = ((index % size) + size) % size; + } + return index; +} + +/** + * Concatenates an array of tensors along a specified dimension. + * @param {Tensor[]} tensors The array of tensors to concatenate. + * @param {number} dim The dimension to concatenate along. + * @returns {Tensor} The concatenated tensor. + */ +function cat(tensors, dim = 0) { + dim = safeIndex(dim, tensors[0].dims.length); + + // TODO do validation of shapes + + const resultDims = tensors[0].dims.slice(); + resultDims[dim] = tensors.reduce((a, b) => a + b.dims[dim], 0); + + // Create a new array to store the accumulated values + const resultSize = resultDims.reduce((a, b) => a * b, 1); + // @ts-ignore + const result = new tensors[0].data.constructor(resultSize); + + // Create output tensor of same type as first + const resultType = tensors[0].type; + + if (dim === 0) { + // Handle special case for performance reasons + + let offset = 0; + for (const tensor of tensors) { + const tensorData = tensor.data; + result.set(tensorData, offset); + offset += tensorData.length; + } + + } else { + + let currentDim = 0; + + for (let t = 0; t < tensors.length; ++t) { + const { data, dims } = tensors[t]; + + // Iterate over the data array + for (let i = 0; i < data.length; ++i) { + // Calculate the index in the resulting array + let resultIndex = 0; + + for (let j = dims.length - 1, num = i, resultMultiplier = 1; j >= 0; --j) { + const size = dims[j]; + let index = num % size; + if (j === dim) { + index += currentDim; + } + resultIndex += index * resultMultiplier; + resultMultiplier *= resultDims[j]; + num = Math.floor(num / size); + } + // Accumulate the value at the current index + result[resultIndex] = data[i]; + } + + currentDim += dims[dim]; + } + } + return new Tensor(resultType, result, resultDims); +} + +/** + * Stack an array of tensors along a specified dimension. + * @param {Tensor[]} tensors The array of tensors to stack. + * @param {number} dim The dimension to stack along. + * @returns {Tensor} The stacked tensor. + */ +function stack(tensors, dim = 0) { + // TODO do validation of shapes + // NOTE: stack expects each tensor to be equal size + return cat(tensors.map(t => t.unsqueeze(dim)), dim); +} + + +/** + * @param {(previousValue: any, currentValue: any, currentIndex?: number, resultIndex?: number) => any} callbackfn + * @param {Tensor} input the input tensor. + * @param {number|null} dim the dimension to reduce. + * @param {boolean} keepdim whether the output tensor has dim retained or not. + * @returns {[DataType, any, number[]]} The reduced tensor data. + */ +function reduce_helper(callbackfn, input, dim = null, keepdim = false, initialValue = null) { + const inputData = input.data; + const inputDims = input.dims; + + // Negative indexing + dim = safeIndex(dim, inputDims.length); + + // Calculate the shape of the resulting array after summation + const resultDims = inputDims.slice(); // Copy the original dimensions + resultDims[dim] = 1; // Remove the specified axis + + // Create a new array to store the accumulated values + // @ts-ignore + const result = new inputData.constructor(inputData.length / inputDims[dim]); + if (initialValue !== null) { + result.fill(initialValue); + } + + // Iterate over the data array + for (let i = 0; i < inputData.length; ++i) { + + // Calculate the index in the resulting array + let resultIndex = 0; + + for (let j = inputDims.length - 1, num = i, resultMultiplier = 1; j >= 0; --j) { + const size = inputDims[j]; + if (j !== dim) { + const index = num % size; + resultIndex += index * resultMultiplier; + resultMultiplier *= resultDims[j]; + } + num = Math.floor(num / size); + } + + // Accumulate the value at the current index + result[resultIndex] = callbackfn(result[resultIndex], inputData[i], i, resultIndex); + } + + if (!keepdim) resultDims.splice(dim, 1); + + return [input.type, result, resultDims]; +} + + +/** + * Calculates the standard deviation and mean over the dimensions specified by dim. dim can be a single dimension or `null` to reduce over all dimensions. + * @param {Tensor} input the input tenso + * @param {number|null} dim the dimension to reduce. If None, all dimensions are reduced. + * @param {number} correction difference between the sample size and sample degrees of freedom. Defaults to Bessel's correction, correction=1. + * @param {boolean} keepdim whether the output tensor has dim retained or not. + * @returns {Tensor[]} A tuple of (std, mean) tensors. + */ +function std_mean(input, dim = null, correction = 1, keepdim = false) { + const inputData = /** @type {Float32Array} */(input.data); + const inputDims = input.dims; + + if (dim === null) { + // None to reduce over all dimensions. + const sum = inputData.reduce((a, b) => a + b, 0); + const mean = sum / inputData.length; + const std = Math.sqrt(inputData.reduce((a, b) => a + (b - mean) ** 2, 0) / (inputData.length - correction)); + + const meanTensor = new Tensor(input.type, [mean], [/* scalar */]); + const stdTensor = new Tensor(input.type, [std], [/* scalar */]); + + return [stdTensor, meanTensor]; + } + dim = safeIndex(dim, inputDims.length); + const meanTensor = mean(input, dim, keepdim); + const meanTensorData = meanTensor.data; + + // Compute squared sum + const [type, result, resultDims] = reduce_helper((a, b, i, j) => a + (b - meanTensorData[j]) ** 2, input, dim, keepdim); + + // Square root of the squared sum + for (let i = 0; i < result.length; ++i) { + result[i] = Math.sqrt(result[i] / (inputDims[dim] - correction)); + } + + const stdTensor = new Tensor(type, result, resultDims); + + return [stdTensor, meanTensor]; +} + +/** + * Returns the mean value of each row of the input tensor in the given dimension dim. + * @param {Tensor} input the input tensor. + * @param {number|null} dim the dimension to reduce. + * @param {boolean} keepdim whether the output tensor has dim retained or not. + * @returns {Tensor} A new tensor with means taken along the specified dimension. + */ +function mean(input, dim = null, keepdim = false) { + const inputDims = input.dims; + const inputData = /** @type {Float32Array} */(input.data); + + if (dim === null) { + // None to reduce over all dimensions. + const val = inputData.reduce((a, b) => a + b, 0); + return new Tensor(input.type, [val / inputData.length], [/* scalar */]); + } + dim = safeIndex(dim, inputDims.length); + + // Compute sum + const [type, result, resultDims] = reduce_helper((a, b) => a + b, input, dim, keepdim); + + // Divide by number of elements in the dimension + if (inputDims[dim] !== 1) { + for (let i = 0; i < result.length; ++i) { + result[i] /= inputDims[dim]; + } + } + + return new Tensor(type, result, resultDims); +} + + +function dimsToStride(dims) { + const stride = new Array(dims.length); + for (let i = dims.length - 1, s2 = 1; i >= 0; --i) { + stride[i] = s2; + s2 *= dims[i]; + } + return stride; +} + +function fullHelper(size, fill_value, dtype, cls) { + const numElements = size.reduce((a, b) => a * b, 1); + return new Tensor( + dtype, + new cls(numElements).fill(fill_value), + size + ) +} + +/** + * Creates a tensor of size size filled with fill_value. The tensor's dtype is inferred from fill_value. + * @param {number[]} size A sequence of integers defining the shape of the output tensor. + * @param {number|bigint|boolean} fill_value The value to fill the output tensor with. + * @returns {Tensor} The filled tensor. + */ +function full(size, fill_value) { + let dtype; + let typedArrayCls; + if (typeof fill_value === 'number') { + dtype = 'float32'; + typedArrayCls = Float32Array; + } else if (typeof fill_value === 'bigint') { + dtype = 'int64'; + typedArrayCls = BigInt64Array; + } else if (typeof fill_value === 'boolean') { + dtype = 'bool'; + typedArrayCls = Uint8Array; + } else { + // TODO: support other dtypes + throw new Error(`Unsupported data type: ${typeof fill_value}`); + } + return fullHelper(size, fill_value, dtype, typedArrayCls); +} + +function full_like(tensor, fill_value) { + return full(tensor.dims, fill_value); +} + +/** + * Returns a tensor filled with the scalar value 1, with the shape defined by the variable argument size. + * @param {number[]} size A sequence of integers defining the shape of the output tensor. + * @returns {Tensor} The ones tensor. + */ +function ones(size) { + return fullHelper(size, 1n, 'int64', BigInt64Array); +} + +/** + * Returns a tensor filled with the scalar value 1, with the same size as input. + * @param {Tensor} tensor The size of input will determine size of the output tensor. + * @returns {Tensor} The ones tensor. + */ +function ones_like(tensor) { + return ones(tensor.dims); +} + +/** + * Returns a tensor filled with the scalar value 0, with the shape defined by the variable argument size. + * @param {number[]} size A sequence of integers defining the shape of the output tensor. + * @returns {Tensor} The zeros tensor. + */ +function zeros(size) { + return fullHelper(size, 0n, 'int64', BigInt64Array); +} + +/** + * Returns a tensor filled with the scalar value 0, with the same size as input. + * @param {Tensor} tensor The size of input will determine size of the output tensor. + * @returns {Tensor} The zeros tensor. + */ +function zeros_like(tensor) { + return zeros(tensor.dims); +} + +/** + * Returns a tensor filled with random numbers from a uniform distribution on the interval [0, 1) + * @param {number[]} size A sequence of integers defining the shape of the output tensor. + * @returns {Tensor} The random tensor. + */ +function rand(size) { + const length = size.reduce((a, b) => a * b, 1); + return new Tensor( + "float32", + Float32Array.from({ length }, () => Math.random()), + size, + ) +} + +/** + * Quantizes the embeddings tensor to binary or unsigned binary precision. + * @param {Tensor} tensor The tensor to quantize. + * @param {'binary'|'ubinary'} precision The precision to use for quantization. + * @returns {Tensor} The quantized tensor. + */ +function quantize_embeddings(tensor, precision) { + if (tensor.dims.length !== 2) { + throw new Error("The tensor must have 2 dimensions"); + } + if (tensor.dims.at(-1) % 8 !== 0) { + throw new Error("The last dimension of the tensor must be a multiple of 8"); + } + if (!['binary', 'ubinary'].includes(precision)) { + throw new Error("The precision must be either 'binary' or 'ubinary'"); + } + + const signed = precision === 'binary'; + const dtype = signed ? 'int8' : 'uint8'; + + // Create a typed array to store the packed bits + const cls = signed ? Int8Array : Uint8Array; + const inputData = tensor.data; + const outputData = new cls(inputData.length / 8); + + // Iterate over each number in the array + for (let i = 0; i < inputData.length; ++i) { + // Determine if the number is greater than 0 + const bit = inputData[i] > 0 ? 1 : 0; + + // Calculate the index in the typed array and the position within the byte + const arrayIndex = Math.floor(i / 8); + const bitPosition = i % 8; + + // Pack the bit into the typed array + outputData[arrayIndex] |= bit << (7 - bitPosition); + if (signed && bitPosition === 0) { + outputData[arrayIndex] -= 128; + } + }; + + return new Tensor(dtype, outputData, [tensor.dims[0], tensor.dims[1] / 8]); +} + + +/***/ }), + +/***/ "./src/utils/video.js": +/*!****************************!*\ + !*** ./src/utils/video.js ***! + \****************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ RawVideo: () => (/* binding */ RawVideo), +/* harmony export */ RawVideoFrame: () => (/* binding */ RawVideoFrame), +/* harmony export */ load_video: () => (/* binding */ load_video) +/* harmony export */ }); +/* harmony import */ var _image_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./image.js */ "./src/utils/image.js"); +/* harmony import */ var _env_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../env.js */ "./src/env.js"); + + + +class RawVideoFrame { + + /** + * @param {RawImage} image + * @param {number} timestamp + */ + constructor(image, timestamp) { + this.image = image; + this.timestamp = timestamp; + } +} + +class RawVideo { + /** + * @param {RawVideoFrame[]|RawImage[]} frames + * @param {number} duration + */ + constructor(frames, duration) { + if (frames.length > 0 && frames[0] instanceof _image_js__WEBPACK_IMPORTED_MODULE_0__.RawImage) { + // Assume uniform timestamps + frames = frames.map((image, i) => new RawVideoFrame(image, (i + 1) / (frames.length + 1) * duration)); + } + this.frames = /** @type {RawVideoFrame[]} */ (frames); + this.duration = duration; + } + + get width() { + return this.frames[0].image.width; + } + get height() { + return this.frames[0].image.height; + } + + get fps() { + return this.frames.length / this.duration; + } +} + + +/** + * Loads a video. + * + * @param {string|Blob|HTMLVideoElement} src The video to process. + * @param {Object} [options] Optional parameters. + * @param {number} [options.num_frames=null] The number of frames to sample uniformly. + * @param {number} [options.fps=null] The number of frames to sample per second. + * + * @returns {Promise} The loaded video. + */ +async function load_video(src, { num_frames = null, fps = null } = {}) { + if (!_env_js__WEBPACK_IMPORTED_MODULE_1__.apis.IS_BROWSER_ENV) { + throw new Error("`load_video` is currently only supported in browser environments."); + } + + // TODO: Support efficiently loading all frames using the WebCodecs API. + // Specfically, https://developer.mozilla.org/en-US/docs/Web/API/VideoDecoder + if (num_frames == null && fps == null) { + throw new Error("Either num_frames or fps must be provided."); + } + + const frames = []; + + const video = document.createElement("video"); + video.crossOrigin = "anonymous"; + video.muted = true; // mute to allow autoplay and seeking + + if (typeof src === 'string') { + video.src = src; + } else if (src instanceof Blob) { + video.src = URL.createObjectURL(src); + } else if (src instanceof HTMLVideoElement) { + video.src = src.src; + } else { + throw new Error("Invalid URL or video element provided."); + } + // Wait for metadata to load to obtain duration + await new Promise((resolve) => video.onloadedmetadata = resolve); + + if (video.seekable.start(0) === video.seekable.end(0)) { + // Fallback: Download entire video if not seekable + const response = await fetch(video.src); + const blob = await response.blob(); + video.src = URL.createObjectURL(blob); + await new Promise((resolve) => video.onloadedmetadata = resolve); + } + + const duration = video.duration; + + let count, step; + if (num_frames != null) { + count = num_frames; + step = num_frames === 1 ? 0 : duration / (num_frames - 1); + } else { + step = 1 / fps; + count = Math.floor(duration / step); + } + + // Build an array of sample times based on num_frames or fps + let sampleTimes = []; + for (let i = 0; i < count; ++i) { + sampleTimes.push(num_frames === 1 ? duration / 2 : i * step); + } + + const canvas = document.createElement("canvas"); + canvas.width = video.videoWidth; + canvas.height = video.videoHeight; + const ctx = canvas.getContext("2d", { willReadFrequently: true }); + for (const t of sampleTimes) { + video.currentTime = t; + await new Promise((resolve) => { + video.onseeked = resolve; + }); + ctx.drawImage(video, 0, 0, canvas.width, canvas.height); + const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height); + const frameData = new _image_js__WEBPACK_IMPORTED_MODULE_0__.RawImage(imageData.data, canvas.width, canvas.height, 4); + + const frame = new RawVideoFrame(frameData, t); + frames.push(frame); + } + + // Clean up video element. + video.remove(); + + return new RawVideo(frames, duration); +} + + +/***/ }) + +/******/ }); +/************************************************************************/ +/******/ // The module cache +/******/ var __webpack_module_cache__ = {}; +/******/ +/******/ // The require function +/******/ function __webpack_require__(moduleId) { +/******/ // Check if module is in cache +/******/ var cachedModule = __webpack_module_cache__[moduleId]; +/******/ if (cachedModule !== undefined) { +/******/ return cachedModule.exports; +/******/ } +/******/ // Create a new module (and put it into the cache) +/******/ var module = __webpack_module_cache__[moduleId] = { +/******/ // no module.id needed +/******/ // no module.loaded needed +/******/ exports: {} +/******/ }; +/******/ +/******/ // Execute the module function +/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__); +/******/ +/******/ // Return the exports of the module +/******/ return module.exports; +/******/ } +/******/ +/************************************************************************/ +/******/ /* webpack/runtime/create fake namespace object */ +/******/ (() => { +/******/ var getProto = Object.getPrototypeOf ? (obj) => (Object.getPrototypeOf(obj)) : (obj) => (obj.__proto__); +/******/ var leafPrototypes; +/******/ // create a fake namespace object +/******/ // mode & 1: value is a module id, require it +/******/ // mode & 2: merge all properties of value into the ns +/******/ // mode & 4: return value when already ns object +/******/ // mode & 16: return value when it's Promise-like +/******/ // mode & 8|1: behave like require +/******/ __webpack_require__.t = function(value, mode) { +/******/ if(mode & 1) value = this(value); +/******/ if(mode & 8) return value; +/******/ if(typeof value === 'object' && value) { +/******/ if((mode & 4) && value.__esModule) return value; +/******/ if((mode & 16) && typeof value.then === 'function') return value; +/******/ } +/******/ var ns = Object.create(null); +/******/ __webpack_require__.r(ns); +/******/ var def = {}; +/******/ leafPrototypes = leafPrototypes || [null, getProto({}), getProto([]), getProto(getProto)]; +/******/ for(var current = mode & 2 && value; typeof current == 'object' && !~leafPrototypes.indexOf(current); current = getProto(current)) { +/******/ Object.getOwnPropertyNames(current).forEach((key) => (def[key] = () => (value[key]))); +/******/ } +/******/ def['default'] = () => (value); +/******/ __webpack_require__.d(ns, def); +/******/ return ns; +/******/ }; +/******/ })(); +/******/ +/******/ /* webpack/runtime/define property getters */ +/******/ (() => { +/******/ // define getter functions for harmony exports +/******/ __webpack_require__.d = (exports, definition) => { +/******/ for(var key in definition) { +/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) { +/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] }); +/******/ } +/******/ } +/******/ }; +/******/ })(); +/******/ +/******/ /* webpack/runtime/hasOwnProperty shorthand */ +/******/ (() => { +/******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop)) +/******/ })(); +/******/ +/******/ /* webpack/runtime/make namespace object */ +/******/ (() => { +/******/ // define __esModule on exports +/******/ __webpack_require__.r = (exports) => { +/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { +/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); +/******/ } +/******/ Object.defineProperty(exports, '__esModule', { value: true }); +/******/ }; +/******/ })(); +/******/ +/******/ /* webpack/runtime/publicPath */ +/******/ (() => { +/******/ var scriptUrl; +/******/ if (typeof import.meta.url === "string") scriptUrl = import.meta.url +/******/ // When supporting browsers where an automatic publicPath is not supported you must specify an output.publicPath manually via configuration +/******/ // or pass an empty string ("") and set the __webpack_public_path__ variable from your code to use your own logic. +/******/ if (!scriptUrl) throw new Error("Automatic publicPath is not supported in this browser"); +/******/ scriptUrl = scriptUrl.replace(/#.*$/, "").replace(/\?.*$/, "").replace(/\/[^\/]+$/, "/"); +/******/ __webpack_require__.p = scriptUrl; +/******/ })(); +/******/ +/******/ /* webpack/runtime/base uri */ +/******/ (() => { +/******/ __webpack_require__.b = undefined; +/******/ })(); +/******/ +/************************************************************************/ +var __webpack_exports__ = {}; +// This entry needs to be wrapped in an IIFE because it needs to be isolated against other modules in the chunk. +(() => { +/*!*****************************!*\ + !*** ./src/transformers.js ***! + \*****************************/ +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ ASTFeatureExtractor: () => (/* reexport safe */ _models_feature_extractors_js__WEBPACK_IMPORTED_MODULE_11__.ASTFeatureExtractor), +/* harmony export */ ASTForAudioClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ASTForAudioClassification), +/* harmony export */ ASTModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ASTModel), +/* harmony export */ ASTPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ASTPreTrainedModel), +/* harmony export */ AlbertForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AlbertForMaskedLM), +/* harmony export */ AlbertForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AlbertForQuestionAnswering), +/* harmony export */ AlbertForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AlbertForSequenceClassification), +/* harmony export */ AlbertModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AlbertModel), +/* harmony export */ AlbertPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AlbertPreTrainedModel), +/* harmony export */ AlbertTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.AlbertTokenizer), +/* harmony export */ AudioClassificationPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.AudioClassificationPipeline), +/* harmony export */ AutoConfig: () => (/* reexport safe */ _configs_js__WEBPACK_IMPORTED_MODULE_4__.AutoConfig), +/* harmony export */ AutoFeatureExtractor: () => (/* reexport safe */ _models_auto_feature_extraction_auto_js__WEBPACK_IMPORTED_MODULE_12__.AutoFeatureExtractor), +/* harmony export */ AutoImageProcessor: () => (/* reexport safe */ _models_auto_image_processing_auto_js__WEBPACK_IMPORTED_MODULE_15__.AutoImageProcessor), +/* harmony export */ AutoModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModel), +/* harmony export */ AutoModelForAudioClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForAudioClassification), +/* harmony export */ AutoModelForAudioFrameClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForAudioFrameClassification), +/* harmony export */ AutoModelForAudioTextToText: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForAudioTextToText), +/* harmony export */ AutoModelForCTC: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForCTC), +/* harmony export */ AutoModelForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForCausalLM), +/* harmony export */ AutoModelForDepthEstimation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForDepthEstimation), +/* harmony export */ AutoModelForDocumentQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForDocumentQuestionAnswering), +/* harmony export */ AutoModelForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForImageClassification), +/* harmony export */ AutoModelForImageFeatureExtraction: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForImageFeatureExtraction), +/* harmony export */ AutoModelForImageMatting: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForImageMatting), +/* harmony export */ AutoModelForImageSegmentation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForImageSegmentation), +/* harmony export */ AutoModelForImageTextToText: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForImageTextToText), +/* harmony export */ AutoModelForImageToImage: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForImageToImage), +/* harmony export */ AutoModelForMaskGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForMaskGeneration), +/* harmony export */ AutoModelForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForMaskedLM), +/* harmony export */ AutoModelForNormalEstimation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForNormalEstimation), +/* harmony export */ AutoModelForObjectDetection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForObjectDetection), +/* harmony export */ AutoModelForPoseEstimation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForPoseEstimation), +/* harmony export */ AutoModelForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForQuestionAnswering), +/* harmony export */ AutoModelForSemanticSegmentation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForSemanticSegmentation), +/* harmony export */ AutoModelForSeq2SeqLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForSeq2SeqLM), +/* harmony export */ AutoModelForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForSequenceClassification), +/* harmony export */ AutoModelForSpeechSeq2Seq: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForSpeechSeq2Seq), +/* harmony export */ AutoModelForTextToSpectrogram: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForTextToSpectrogram), +/* harmony export */ AutoModelForTextToWaveform: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForTextToWaveform), +/* harmony export */ AutoModelForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForTokenClassification), +/* harmony export */ AutoModelForUniversalSegmentation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForUniversalSegmentation), +/* harmony export */ AutoModelForVision2Seq: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForVision2Seq), +/* harmony export */ AutoModelForXVector: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForXVector), +/* harmony export */ AutoModelForZeroShotObjectDetection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForZeroShotObjectDetection), +/* harmony export */ AutoProcessor: () => (/* reexport safe */ _models_auto_processing_auto_js__WEBPACK_IMPORTED_MODULE_18__.AutoProcessor), +/* harmony export */ AutoTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.AutoTokenizer), +/* harmony export */ AutomaticSpeechRecognitionPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.AutomaticSpeechRecognitionPipeline), +/* harmony export */ BackgroundRemovalPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.BackgroundRemovalPipeline), +/* harmony export */ BartForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BartForConditionalGeneration), +/* harmony export */ BartForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BartForSequenceClassification), +/* harmony export */ BartModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BartModel), +/* harmony export */ BartPretrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BartPretrainedModel), +/* harmony export */ BartTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.BartTokenizer), +/* harmony export */ BaseModelOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BaseModelOutput), +/* harmony export */ BaseStreamer: () => (/* reexport safe */ _generation_streamers_js__WEBPACK_IMPORTED_MODULE_19__.BaseStreamer), +/* harmony export */ BeitFeatureExtractor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.BeitFeatureExtractor), +/* harmony export */ BeitForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BeitForImageClassification), +/* harmony export */ BeitModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BeitModel), +/* harmony export */ BeitPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BeitPreTrainedModel), +/* harmony export */ BertForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BertForMaskedLM), +/* harmony export */ BertForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BertForQuestionAnswering), +/* harmony export */ BertForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BertForSequenceClassification), +/* harmony export */ BertForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BertForTokenClassification), +/* harmony export */ BertModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BertModel), +/* harmony export */ BertPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BertPreTrainedModel), +/* harmony export */ BertTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.BertTokenizer), +/* harmony export */ BitImageProcessor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.BitImageProcessor), +/* harmony export */ BlenderbotForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BlenderbotForConditionalGeneration), +/* harmony export */ BlenderbotModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BlenderbotModel), +/* harmony export */ BlenderbotPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BlenderbotPreTrainedModel), +/* harmony export */ BlenderbotSmallForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BlenderbotSmallForConditionalGeneration), +/* harmony export */ BlenderbotSmallModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BlenderbotSmallModel), +/* harmony export */ BlenderbotSmallPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BlenderbotSmallPreTrainedModel), +/* harmony export */ BlenderbotSmallTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.BlenderbotSmallTokenizer), +/* harmony export */ BlenderbotTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.BlenderbotTokenizer), +/* harmony export */ BloomForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BloomForCausalLM), +/* harmony export */ BloomModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BloomModel), +/* harmony export */ BloomPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BloomPreTrainedModel), +/* harmony export */ BloomTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.BloomTokenizer), +/* harmony export */ CLIPFeatureExtractor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.CLIPFeatureExtractor), +/* harmony export */ CLIPImageProcessor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.CLIPImageProcessor), +/* harmony export */ CLIPModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPModel), +/* harmony export */ CLIPPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPPreTrainedModel), +/* harmony export */ CLIPSegForImageSegmentation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPSegForImageSegmentation), +/* harmony export */ CLIPSegModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPSegModel), +/* harmony export */ CLIPSegPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPSegPreTrainedModel), +/* harmony export */ CLIPTextModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPTextModel), +/* harmony export */ CLIPTextModelWithProjection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPTextModelWithProjection), +/* harmony export */ CLIPTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.CLIPTokenizer), +/* harmony export */ CLIPVisionModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPVisionModel), +/* harmony export */ CLIPVisionModelWithProjection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPVisionModelWithProjection), +/* harmony export */ CamembertForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CamembertForMaskedLM), +/* harmony export */ CamembertForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CamembertForQuestionAnswering), +/* harmony export */ CamembertForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CamembertForSequenceClassification), +/* harmony export */ CamembertForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CamembertForTokenClassification), +/* harmony export */ CamembertModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CamembertModel), +/* harmony export */ CamembertPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CamembertPreTrainedModel), +/* harmony export */ CamembertTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.CamembertTokenizer), +/* harmony export */ CausalLMOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CausalLMOutput), +/* harmony export */ CausalLMOutputWithPast: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CausalLMOutputWithPast), +/* harmony export */ ChineseCLIPFeatureExtractor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.ChineseCLIPFeatureExtractor), +/* harmony export */ ChineseCLIPModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ChineseCLIPModel), +/* harmony export */ ChineseCLIPPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ChineseCLIPPreTrainedModel), +/* harmony export */ ClapAudioModelWithProjection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ClapAudioModelWithProjection), +/* harmony export */ ClapFeatureExtractor: () => (/* reexport safe */ _models_feature_extractors_js__WEBPACK_IMPORTED_MODULE_11__.ClapFeatureExtractor), +/* harmony export */ ClapModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ClapModel), +/* harmony export */ ClapPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ClapPreTrainedModel), +/* harmony export */ ClapTextModelWithProjection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ClapTextModelWithProjection), +/* harmony export */ ClassifierFreeGuidanceLogitsProcessor: () => (/* reexport safe */ _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_21__.ClassifierFreeGuidanceLogitsProcessor), +/* harmony export */ CodeGenForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CodeGenForCausalLM), +/* harmony export */ CodeGenModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CodeGenModel), +/* harmony export */ CodeGenPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CodeGenPreTrainedModel), +/* harmony export */ CodeGenTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.CodeGenTokenizer), +/* harmony export */ CodeLlamaTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.CodeLlamaTokenizer), +/* harmony export */ CohereForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CohereForCausalLM), +/* harmony export */ CohereModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CohereModel), +/* harmony export */ CoherePreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CoherePreTrainedModel), +/* harmony export */ CohereTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.CohereTokenizer), +/* harmony export */ ConvBertForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvBertForMaskedLM), +/* harmony export */ ConvBertForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvBertForQuestionAnswering), +/* harmony export */ ConvBertForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvBertForSequenceClassification), +/* harmony export */ ConvBertForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvBertForTokenClassification), +/* harmony export */ ConvBertModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvBertModel), +/* harmony export */ ConvBertPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvBertPreTrainedModel), +/* harmony export */ ConvBertTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.ConvBertTokenizer), +/* harmony export */ ConvNextFeatureExtractor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.ConvNextFeatureExtractor), +/* harmony export */ ConvNextForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvNextForImageClassification), +/* harmony export */ ConvNextImageProcessor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.ConvNextImageProcessor), +/* harmony export */ ConvNextModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvNextModel), +/* harmony export */ ConvNextPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvNextPreTrainedModel), +/* harmony export */ ConvNextV2ForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvNextV2ForImageClassification), +/* harmony export */ ConvNextV2Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvNextV2Model), +/* harmony export */ ConvNextV2PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvNextV2PreTrainedModel), +/* harmony export */ DFineForObjectDetection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DFineForObjectDetection), +/* harmony export */ DFineModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DFineModel), +/* harmony export */ DFinePreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DFinePreTrainedModel), +/* harmony export */ DPTFeatureExtractor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.DPTFeatureExtractor), +/* harmony export */ DPTForDepthEstimation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DPTForDepthEstimation), +/* harmony export */ DPTImageProcessor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.DPTImageProcessor), +/* harmony export */ DPTModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DPTModel), +/* harmony export */ DPTPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DPTPreTrainedModel), +/* harmony export */ DacDecoderModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DacDecoderModel), +/* harmony export */ DacDecoderOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DacDecoderOutput), +/* harmony export */ DacEncoderModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DacEncoderModel), +/* harmony export */ DacEncoderOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DacEncoderOutput), +/* harmony export */ DacFeatureExtractor: () => (/* reexport safe */ _models_feature_extractors_js__WEBPACK_IMPORTED_MODULE_11__.DacFeatureExtractor), +/* harmony export */ DacModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DacModel), +/* harmony export */ DacPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DacPreTrainedModel), +/* harmony export */ DataTypeMap: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.DataTypeMap), +/* harmony export */ DebertaForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaForMaskedLM), +/* harmony export */ DebertaForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaForQuestionAnswering), +/* harmony export */ DebertaForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaForSequenceClassification), +/* harmony export */ DebertaForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaForTokenClassification), +/* harmony export */ DebertaModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaModel), +/* harmony export */ DebertaPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaPreTrainedModel), +/* harmony export */ DebertaTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.DebertaTokenizer), +/* harmony export */ DebertaV2ForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaV2ForMaskedLM), +/* harmony export */ DebertaV2ForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaV2ForQuestionAnswering), +/* harmony export */ DebertaV2ForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaV2ForSequenceClassification), +/* harmony export */ DebertaV2ForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaV2ForTokenClassification), +/* harmony export */ DebertaV2Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaV2Model), +/* harmony export */ DebertaV2PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaV2PreTrainedModel), +/* harmony export */ DebertaV2Tokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.DebertaV2Tokenizer), +/* harmony export */ DecisionTransformerModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DecisionTransformerModel), +/* harmony export */ DecisionTransformerPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DecisionTransformerPreTrainedModel), +/* harmony export */ DeiTFeatureExtractor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.DeiTFeatureExtractor), +/* harmony export */ DeiTForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DeiTForImageClassification), +/* harmony export */ DeiTImageProcessor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.DeiTImageProcessor), +/* harmony export */ DeiTModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DeiTModel), +/* harmony export */ DeiTPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DeiTPreTrainedModel), +/* harmony export */ DepthAnythingForDepthEstimation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DepthAnythingForDepthEstimation), +/* harmony export */ DepthAnythingPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DepthAnythingPreTrainedModel), +/* harmony export */ DepthEstimationPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.DepthEstimationPipeline), +/* harmony export */ DepthProForDepthEstimation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DepthProForDepthEstimation), +/* harmony export */ DepthProPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DepthProPreTrainedModel), +/* harmony export */ DetrFeatureExtractor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.DetrFeatureExtractor), +/* harmony export */ DetrForObjectDetection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DetrForObjectDetection), +/* harmony export */ DetrForSegmentation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DetrForSegmentation), +/* harmony export */ DetrImageProcessor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.DetrImageProcessor), +/* harmony export */ DetrModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DetrModel), +/* harmony export */ DetrObjectDetectionOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DetrObjectDetectionOutput), +/* harmony export */ DetrPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DetrPreTrainedModel), +/* harmony export */ DetrSegmentationOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DetrSegmentationOutput), +/* harmony export */ Dinov2ForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Dinov2ForImageClassification), +/* harmony export */ Dinov2Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Dinov2Model), +/* harmony export */ Dinov2PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Dinov2PreTrainedModel), +/* harmony export */ Dinov2WithRegistersForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Dinov2WithRegistersForImageClassification), +/* harmony export */ Dinov2WithRegistersModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Dinov2WithRegistersModel), +/* harmony export */ Dinov2WithRegistersPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Dinov2WithRegistersPreTrainedModel), +/* harmony export */ DistilBertForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DistilBertForMaskedLM), +/* harmony export */ DistilBertForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DistilBertForQuestionAnswering), +/* harmony export */ DistilBertForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DistilBertForSequenceClassification), +/* harmony export */ DistilBertForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DistilBertForTokenClassification), +/* harmony export */ DistilBertModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DistilBertModel), +/* harmony export */ DistilBertPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DistilBertPreTrainedModel), +/* harmony export */ DistilBertTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.DistilBertTokenizer), +/* harmony export */ DocumentQuestionAnsweringPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.DocumentQuestionAnsweringPipeline), +/* harmony export */ DonutFeatureExtractor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.DonutFeatureExtractor), +/* harmony export */ DonutImageProcessor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.DonutImageProcessor), +/* harmony export */ DonutSwinModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DonutSwinModel), +/* harmony export */ DonutSwinPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DonutSwinPreTrainedModel), +/* harmony export */ EfficientNetForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.EfficientNetForImageClassification), +/* harmony export */ EfficientNetImageProcessor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.EfficientNetImageProcessor), +/* harmony export */ EfficientNetModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.EfficientNetModel), +/* harmony export */ EfficientNetPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.EfficientNetPreTrainedModel), +/* harmony export */ ElectraForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ElectraForMaskedLM), +/* harmony export */ ElectraForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ElectraForQuestionAnswering), +/* harmony export */ ElectraForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ElectraForSequenceClassification), +/* harmony export */ ElectraForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ElectraForTokenClassification), +/* harmony export */ ElectraModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ElectraModel), +/* harmony export */ ElectraPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ElectraPreTrainedModel), +/* harmony export */ ElectraTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.ElectraTokenizer), +/* harmony export */ EncodecFeatureExtractor: () => (/* reexport safe */ _models_feature_extractors_js__WEBPACK_IMPORTED_MODULE_11__.EncodecFeatureExtractor), +/* harmony export */ EosTokenCriteria: () => (/* reexport safe */ _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_20__.EosTokenCriteria), +/* harmony export */ EsmForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.EsmForMaskedLM), +/* harmony export */ EsmForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.EsmForSequenceClassification), +/* harmony export */ EsmForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.EsmForTokenClassification), +/* harmony export */ EsmModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.EsmModel), +/* harmony export */ EsmPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.EsmPreTrainedModel), +/* harmony export */ EsmTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.EsmTokenizer), +/* harmony export */ ExaoneForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ExaoneForCausalLM), +/* harmony export */ ExaoneModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ExaoneModel), +/* harmony export */ ExaonePreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ExaonePreTrainedModel), +/* harmony export */ FFT: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.FFT), +/* harmony export */ FalconForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.FalconForCausalLM), +/* harmony export */ FalconModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.FalconModel), +/* harmony export */ FalconPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.FalconPreTrainedModel), +/* harmony export */ FalconTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.FalconTokenizer), +/* harmony export */ FastViTForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.FastViTForImageClassification), +/* harmony export */ FastViTModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.FastViTModel), +/* harmony export */ FastViTPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.FastViTPreTrainedModel), +/* harmony export */ FeatureExtractionPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.FeatureExtractionPipeline), +/* harmony export */ FeatureExtractor: () => (/* reexport safe */ _base_feature_extraction_utils_js__WEBPACK_IMPORTED_MODULE_10__.FeatureExtractor), +/* harmony export */ FillMaskPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.FillMaskPipeline), +/* harmony export */ Florence2ForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Florence2ForConditionalGeneration), +/* harmony export */ Florence2PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Florence2PreTrainedModel), +/* harmony export */ Florence2Processor: () => (/* reexport safe */ _models_processors_js__WEBPACK_IMPORTED_MODULE_17__.Florence2Processor), +/* harmony export */ ForcedBOSTokenLogitsProcessor: () => (/* reexport safe */ _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_21__.ForcedBOSTokenLogitsProcessor), +/* harmony export */ ForcedEOSTokenLogitsProcessor: () => (/* reexport safe */ _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_21__.ForcedEOSTokenLogitsProcessor), +/* harmony export */ GLPNFeatureExtractor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.GLPNFeatureExtractor), +/* harmony export */ GLPNForDepthEstimation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GLPNForDepthEstimation), +/* harmony export */ GLPNModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GLPNModel), +/* harmony export */ GLPNPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GLPNPreTrainedModel), +/* harmony export */ GPT2LMHeadModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPT2LMHeadModel), +/* harmony export */ GPT2Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPT2Model), +/* harmony export */ GPT2PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPT2PreTrainedModel), +/* harmony export */ GPT2Tokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.GPT2Tokenizer), +/* harmony export */ GPTBigCodeForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTBigCodeForCausalLM), +/* harmony export */ GPTBigCodeModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTBigCodeModel), +/* harmony export */ GPTBigCodePreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTBigCodePreTrainedModel), +/* harmony export */ GPTJForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTJForCausalLM), +/* harmony export */ GPTJModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTJModel), +/* harmony export */ GPTJPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTJPreTrainedModel), +/* harmony export */ GPTNeoForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTNeoForCausalLM), +/* harmony export */ GPTNeoModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTNeoModel), +/* harmony export */ GPTNeoPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTNeoPreTrainedModel), +/* harmony export */ GPTNeoXForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTNeoXForCausalLM), +/* harmony export */ GPTNeoXModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTNeoXModel), +/* harmony export */ GPTNeoXPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTNeoXPreTrainedModel), +/* harmony export */ GPTNeoXTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.GPTNeoXTokenizer), +/* harmony export */ Gemma2ForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Gemma2ForCausalLM), +/* harmony export */ Gemma2Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Gemma2Model), +/* harmony export */ Gemma2PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Gemma2PreTrainedModel), +/* harmony export */ Gemma3ForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Gemma3ForCausalLM), +/* harmony export */ Gemma3Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Gemma3Model), +/* harmony export */ Gemma3PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Gemma3PreTrainedModel), +/* harmony export */ GemmaForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GemmaForCausalLM), +/* harmony export */ GemmaModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GemmaModel), +/* harmony export */ GemmaPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GemmaPreTrainedModel), +/* harmony export */ GemmaTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.GemmaTokenizer), +/* harmony export */ GlmForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GlmForCausalLM), +/* harmony export */ GlmModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GlmModel), +/* harmony export */ GlmPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GlmPreTrainedModel), +/* harmony export */ GraniteForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GraniteForCausalLM), +/* harmony export */ GraniteModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GraniteModel), +/* harmony export */ GranitePreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GranitePreTrainedModel), +/* harmony export */ Grok1Tokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.Grok1Tokenizer), +/* harmony export */ GroundingDinoForObjectDetection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GroundingDinoForObjectDetection), +/* harmony export */ GroundingDinoImageProcessor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.GroundingDinoImageProcessor), +/* harmony export */ GroundingDinoPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GroundingDinoPreTrainedModel), +/* harmony export */ GroundingDinoProcessor: () => (/* reexport safe */ _models_processors_js__WEBPACK_IMPORTED_MODULE_17__.GroundingDinoProcessor), +/* harmony export */ GroupViTModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GroupViTModel), +/* harmony export */ GroupViTPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GroupViTPreTrainedModel), +/* harmony export */ HeliumForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.HeliumForCausalLM), +/* harmony export */ HeliumModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.HeliumModel), +/* harmony export */ HeliumPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.HeliumPreTrainedModel), +/* harmony export */ HerbertTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.HerbertTokenizer), +/* harmony export */ HieraForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.HieraForImageClassification), +/* harmony export */ HieraModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.HieraModel), +/* harmony export */ HieraPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.HieraPreTrainedModel), +/* harmony export */ HubertForCTC: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.HubertForCTC), +/* harmony export */ HubertForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.HubertForSequenceClassification), +/* harmony export */ HubertModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.HubertModel), +/* harmony export */ HubertPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.HubertPreTrainedModel), +/* harmony export */ IJepaForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.IJepaForImageClassification), +/* harmony export */ IJepaModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.IJepaModel), +/* harmony export */ IJepaPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.IJepaPreTrainedModel), +/* harmony export */ Idefics3ForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Idefics3ForConditionalGeneration), +/* harmony export */ Idefics3ImageProcessor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.Idefics3ImageProcessor), +/* harmony export */ Idefics3PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Idefics3PreTrainedModel), +/* harmony export */ Idefics3Processor: () => (/* reexport safe */ _models_processors_js__WEBPACK_IMPORTED_MODULE_17__.Idefics3Processor), +/* harmony export */ ImageClassificationPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ImageClassificationPipeline), +/* harmony export */ ImageFeatureExtractionPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ImageFeatureExtractionPipeline), +/* harmony export */ ImageFeatureExtractor: () => (/* reexport safe */ _models_feature_extractors_js__WEBPACK_IMPORTED_MODULE_11__.ImageFeatureExtractor), +/* harmony export */ ImageMattingOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ImageMattingOutput), +/* harmony export */ ImageProcessor: () => (/* reexport safe */ _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_13__.ImageProcessor), +/* harmony export */ ImageSegmentationPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ImageSegmentationPipeline), +/* harmony export */ ImageToImagePipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ImageToImagePipeline), +/* harmony export */ ImageToTextPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ImageToTextPipeline), +/* harmony export */ InterruptableStoppingCriteria: () => (/* reexport safe */ _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_20__.InterruptableStoppingCriteria), +/* harmony export */ JAISLMHeadModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.JAISLMHeadModel), +/* harmony export */ JAISModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.JAISModel), +/* harmony export */ JAISPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.JAISPreTrainedModel), +/* harmony export */ JinaCLIPImageProcessor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.JinaCLIPImageProcessor), +/* harmony export */ JinaCLIPModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.JinaCLIPModel), +/* harmony export */ JinaCLIPPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.JinaCLIPPreTrainedModel), +/* harmony export */ JinaCLIPProcessor: () => (/* reexport safe */ _models_processors_js__WEBPACK_IMPORTED_MODULE_17__.JinaCLIPProcessor), +/* harmony export */ JinaCLIPTextModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.JinaCLIPTextModel), +/* harmony export */ JinaCLIPVisionModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.JinaCLIPVisionModel), +/* harmony export */ LiteWhisperForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.LiteWhisperForConditionalGeneration), +/* harmony export */ LlamaForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.LlamaForCausalLM), +/* harmony export */ LlamaModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.LlamaModel), +/* harmony export */ LlamaPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.LlamaPreTrainedModel), +/* harmony export */ LlamaTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.LlamaTokenizer), +/* harmony export */ LlavaForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.LlavaForConditionalGeneration), +/* harmony export */ LlavaOnevisionForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.LlavaOnevisionForConditionalGeneration), +/* harmony export */ LlavaOnevisionImageProcessor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.LlavaOnevisionImageProcessor), +/* harmony export */ LlavaPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.LlavaPreTrainedModel), +/* harmony export */ LogitsProcessor: () => (/* reexport safe */ _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_21__.LogitsProcessor), +/* harmony export */ LogitsProcessorList: () => (/* reexport safe */ _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_21__.LogitsProcessorList), +/* harmony export */ LogitsWarper: () => (/* reexport safe */ _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_21__.LogitsWarper), +/* harmony export */ LongT5ForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.LongT5ForConditionalGeneration), +/* harmony export */ LongT5Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.LongT5Model), +/* harmony export */ LongT5PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.LongT5PreTrainedModel), +/* harmony export */ M2M100ForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.M2M100ForConditionalGeneration), +/* harmony export */ M2M100Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.M2M100Model), +/* harmony export */ M2M100PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.M2M100PreTrainedModel), +/* harmony export */ M2M100Tokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.M2M100Tokenizer), +/* harmony export */ MBart50Tokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.MBart50Tokenizer), +/* harmony export */ MBartForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MBartForCausalLM), +/* harmony export */ MBartForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MBartForConditionalGeneration), +/* harmony export */ MBartForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MBartForSequenceClassification), +/* harmony export */ MBartModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MBartModel), +/* harmony export */ MBartPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MBartPreTrainedModel), +/* harmony export */ MBartTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.MBartTokenizer), +/* harmony export */ MPNetForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MPNetForMaskedLM), +/* harmony export */ MPNetForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MPNetForQuestionAnswering), +/* harmony export */ MPNetForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MPNetForSequenceClassification), +/* harmony export */ MPNetForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MPNetForTokenClassification), +/* harmony export */ MPNetModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MPNetModel), +/* harmony export */ MPNetPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MPNetPreTrainedModel), +/* harmony export */ MPNetTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.MPNetTokenizer), +/* harmony export */ MT5ForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MT5ForConditionalGeneration), +/* harmony export */ MT5Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MT5Model), +/* harmony export */ MT5PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MT5PreTrainedModel), +/* harmony export */ MarianMTModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MarianMTModel), +/* harmony export */ MarianModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MarianModel), +/* harmony export */ MarianPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MarianPreTrainedModel), +/* harmony export */ MarianTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.MarianTokenizer), +/* harmony export */ Mask2FormerImageProcessor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.Mask2FormerImageProcessor), +/* harmony export */ MaskFormerFeatureExtractor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.MaskFormerFeatureExtractor), +/* harmony export */ MaskFormerForInstanceSegmentation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MaskFormerForInstanceSegmentation), +/* harmony export */ MaskFormerImageProcessor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.MaskFormerImageProcessor), +/* harmony export */ MaskFormerModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MaskFormerModel), +/* harmony export */ MaskFormerPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MaskFormerPreTrainedModel), +/* harmony export */ MaskedLMOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MaskedLMOutput), +/* harmony export */ MaxLengthCriteria: () => (/* reexport safe */ _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_20__.MaxLengthCriteria), +/* harmony export */ Metric3DForDepthEstimation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Metric3DForDepthEstimation), +/* harmony export */ Metric3DPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Metric3DPreTrainedModel), +/* harmony export */ Metric3Dv2ForDepthEstimation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Metric3Dv2ForDepthEstimation), +/* harmony export */ Metric3Dv2PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Metric3Dv2PreTrainedModel), +/* harmony export */ MgpstrForSceneTextRecognition: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MgpstrForSceneTextRecognition), +/* harmony export */ MgpstrModelOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MgpstrModelOutput), +/* harmony export */ MgpstrPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MgpstrPreTrainedModel), +/* harmony export */ MgpstrProcessor: () => (/* reexport safe */ _models_processors_js__WEBPACK_IMPORTED_MODULE_17__.MgpstrProcessor), +/* harmony export */ MgpstrTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.MgpstrTokenizer), +/* harmony export */ MimiDecoderModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MimiDecoderModel), +/* harmony export */ MimiDecoderOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MimiDecoderOutput), +/* harmony export */ MimiEncoderModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MimiEncoderModel), +/* harmony export */ MimiEncoderOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MimiEncoderOutput), +/* harmony export */ MimiModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MimiModel), +/* harmony export */ MimiPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MimiPreTrainedModel), +/* harmony export */ MinLengthLogitsProcessor: () => (/* reexport safe */ _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_21__.MinLengthLogitsProcessor), +/* harmony export */ MinNewTokensLengthLogitsProcessor: () => (/* reexport safe */ _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_21__.MinNewTokensLengthLogitsProcessor), +/* harmony export */ MistralForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MistralForCausalLM), +/* harmony export */ MistralModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MistralModel), +/* harmony export */ MistralPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MistralPreTrainedModel), +/* harmony export */ MobileBertForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileBertForMaskedLM), +/* harmony export */ MobileBertForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileBertForQuestionAnswering), +/* harmony export */ MobileBertForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileBertForSequenceClassification), +/* harmony export */ MobileBertModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileBertModel), +/* harmony export */ MobileBertPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileBertPreTrainedModel), +/* harmony export */ MobileBertTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.MobileBertTokenizer), +/* harmony export */ MobileLLMForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileLLMForCausalLM), +/* harmony export */ MobileLLMModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileLLMModel), +/* harmony export */ MobileLLMPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileLLMPreTrainedModel), +/* harmony export */ MobileNetV1FeatureExtractor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.MobileNetV1FeatureExtractor), +/* harmony export */ MobileNetV1ForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV1ForImageClassification), +/* harmony export */ MobileNetV1ForSemanticSegmentation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV1ForSemanticSegmentation), +/* harmony export */ MobileNetV1ImageProcessor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.MobileNetV1ImageProcessor), +/* harmony export */ MobileNetV1Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV1Model), +/* harmony export */ MobileNetV1PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV1PreTrainedModel), +/* harmony export */ MobileNetV2FeatureExtractor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.MobileNetV2FeatureExtractor), +/* harmony export */ MobileNetV2ForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV2ForImageClassification), +/* harmony export */ MobileNetV2ForSemanticSegmentation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV2ForSemanticSegmentation), +/* harmony export */ MobileNetV2ImageProcessor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.MobileNetV2ImageProcessor), +/* harmony export */ MobileNetV2Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV2Model), +/* harmony export */ MobileNetV2PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV2PreTrainedModel), +/* harmony export */ MobileNetV3FeatureExtractor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.MobileNetV3FeatureExtractor), +/* harmony export */ MobileNetV3ForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV3ForImageClassification), +/* harmony export */ MobileNetV3ForSemanticSegmentation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV3ForSemanticSegmentation), +/* harmony export */ MobileNetV3ImageProcessor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.MobileNetV3ImageProcessor), +/* harmony export */ MobileNetV3Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV3Model), +/* harmony export */ MobileNetV3PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV3PreTrainedModel), +/* harmony export */ MobileNetV4FeatureExtractor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.MobileNetV4FeatureExtractor), +/* harmony export */ MobileNetV4ForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV4ForImageClassification), +/* harmony export */ MobileNetV4ForSemanticSegmentation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV4ForSemanticSegmentation), +/* harmony export */ MobileNetV4ImageProcessor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.MobileNetV4ImageProcessor), +/* harmony export */ MobileNetV4Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV4Model), +/* harmony export */ MobileNetV4PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV4PreTrainedModel), +/* harmony export */ MobileViTFeatureExtractor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.MobileViTFeatureExtractor), +/* harmony export */ MobileViTForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileViTForImageClassification), +/* harmony export */ MobileViTImageProcessor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.MobileViTImageProcessor), +/* harmony export */ MobileViTModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileViTModel), +/* harmony export */ MobileViTPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileViTPreTrainedModel), +/* harmony export */ MobileViTV2ForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileViTV2ForImageClassification), +/* harmony export */ MobileViTV2Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileViTV2Model), +/* harmony export */ MobileViTV2PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileViTV2PreTrainedModel), +/* harmony export */ ModelOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ModelOutput), +/* harmony export */ ModernBertForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ModernBertForMaskedLM), +/* harmony export */ ModernBertForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ModernBertForSequenceClassification), +/* harmony export */ ModernBertForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ModernBertForTokenClassification), +/* harmony export */ ModernBertModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ModernBertModel), +/* harmony export */ ModernBertPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ModernBertPreTrainedModel), +/* harmony export */ Moondream1ForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Moondream1ForConditionalGeneration), +/* harmony export */ MoonshineFeatureExtractor: () => (/* reexport safe */ _models_feature_extractors_js__WEBPACK_IMPORTED_MODULE_11__.MoonshineFeatureExtractor), +/* harmony export */ MoonshineForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MoonshineForConditionalGeneration), +/* harmony export */ MoonshineModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MoonshineModel), +/* harmony export */ MoonshinePreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MoonshinePreTrainedModel), +/* harmony export */ MoonshineProcessor: () => (/* reexport safe */ _models_processors_js__WEBPACK_IMPORTED_MODULE_17__.MoonshineProcessor), +/* harmony export */ MptForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MptForCausalLM), +/* harmony export */ MptModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MptModel), +/* harmony export */ MptPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MptPreTrainedModel), +/* harmony export */ MultiModalityCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MultiModalityCausalLM), +/* harmony export */ MultiModalityPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MultiModalityPreTrainedModel), +/* harmony export */ MusicgenForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MusicgenForCausalLM), +/* harmony export */ MusicgenForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MusicgenForConditionalGeneration), +/* harmony export */ MusicgenModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MusicgenModel), +/* harmony export */ MusicgenPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MusicgenPreTrainedModel), +/* harmony export */ NllbTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.NllbTokenizer), +/* harmony export */ NoBadWordsLogitsProcessor: () => (/* reexport safe */ _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_21__.NoBadWordsLogitsProcessor), +/* harmony export */ NoRepeatNGramLogitsProcessor: () => (/* reexport safe */ _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_21__.NoRepeatNGramLogitsProcessor), +/* harmony export */ NomicBertModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.NomicBertModel), +/* harmony export */ NomicBertPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.NomicBertPreTrainedModel), +/* harmony export */ NougatImageProcessor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.NougatImageProcessor), +/* harmony export */ NougatTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.NougatTokenizer), +/* harmony export */ OPTForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OPTForCausalLM), +/* harmony export */ OPTModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OPTModel), +/* harmony export */ OPTPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OPTPreTrainedModel), +/* harmony export */ ObjectDetectionPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ObjectDetectionPipeline), +/* harmony export */ Olmo2ForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Olmo2ForCausalLM), +/* harmony export */ Olmo2Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Olmo2Model), +/* harmony export */ Olmo2PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Olmo2PreTrainedModel), +/* harmony export */ OlmoForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OlmoForCausalLM), +/* harmony export */ OlmoModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OlmoModel), +/* harmony export */ OlmoPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OlmoPreTrainedModel), +/* harmony export */ OpenELMForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OpenELMForCausalLM), +/* harmony export */ OpenELMModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OpenELMModel), +/* harmony export */ OpenELMPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OpenELMPreTrainedModel), +/* harmony export */ OwlViTFeatureExtractor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.OwlViTFeatureExtractor), +/* harmony export */ OwlViTForObjectDetection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OwlViTForObjectDetection), +/* harmony export */ OwlViTImageProcessor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.OwlViTImageProcessor), +/* harmony export */ OwlViTModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OwlViTModel), +/* harmony export */ OwlViTPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OwlViTPreTrainedModel), +/* harmony export */ OwlViTProcessor: () => (/* reexport safe */ _models_processors_js__WEBPACK_IMPORTED_MODULE_17__.OwlViTProcessor), +/* harmony export */ Owlv2ForObjectDetection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Owlv2ForObjectDetection), +/* harmony export */ Owlv2ImageProcessor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.Owlv2ImageProcessor), +/* harmony export */ Owlv2Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Owlv2Model), +/* harmony export */ Owlv2PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Owlv2PreTrainedModel), +/* harmony export */ PaliGemmaForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PaliGemmaForConditionalGeneration), +/* harmony export */ PaliGemmaPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PaliGemmaPreTrainedModel), +/* harmony export */ PaliGemmaProcessor: () => (/* reexport safe */ _models_processors_js__WEBPACK_IMPORTED_MODULE_17__.PaliGemmaProcessor), +/* harmony export */ PatchTSMixerForPrediction: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PatchTSMixerForPrediction), +/* harmony export */ PatchTSMixerModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PatchTSMixerModel), +/* harmony export */ PatchTSMixerPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PatchTSMixerPreTrainedModel), +/* harmony export */ PatchTSTForPrediction: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PatchTSTForPrediction), +/* harmony export */ PatchTSTModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PatchTSTModel), +/* harmony export */ PatchTSTPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PatchTSTPreTrainedModel), +/* harmony export */ Phi3ForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Phi3ForCausalLM), +/* harmony export */ Phi3Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Phi3Model), +/* harmony export */ Phi3PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Phi3PreTrainedModel), +/* harmony export */ Phi3VForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Phi3VForCausalLM), +/* harmony export */ Phi3VImageProcessor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.Phi3VImageProcessor), +/* harmony export */ Phi3VPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Phi3VPreTrainedModel), +/* harmony export */ Phi3VProcessor: () => (/* reexport safe */ _models_processors_js__WEBPACK_IMPORTED_MODULE_17__.Phi3VProcessor), +/* harmony export */ PhiForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PhiForCausalLM), +/* harmony export */ PhiModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PhiModel), +/* harmony export */ PhiPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PhiPreTrainedModel), +/* harmony export */ Pipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.Pipeline), +/* harmony export */ PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PreTrainedModel), +/* harmony export */ PreTrainedTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.PreTrainedTokenizer), +/* harmony export */ PretrainedConfig: () => (/* reexport safe */ _configs_js__WEBPACK_IMPORTED_MODULE_4__.PretrainedConfig), +/* harmony export */ PretrainedMixin: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PretrainedMixin), +/* harmony export */ Processor: () => (/* reexport safe */ _base_processing_utils_js__WEBPACK_IMPORTED_MODULE_16__.Processor), +/* harmony export */ PvtForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PvtForImageClassification), +/* harmony export */ PvtImageProcessor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.PvtImageProcessor), +/* harmony export */ PvtModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PvtModel), +/* harmony export */ PvtPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PvtPreTrainedModel), +/* harmony export */ PyAnnoteFeatureExtractor: () => (/* reexport safe */ _models_feature_extractors_js__WEBPACK_IMPORTED_MODULE_11__.PyAnnoteFeatureExtractor), +/* harmony export */ PyAnnoteForAudioFrameClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PyAnnoteForAudioFrameClassification), +/* harmony export */ PyAnnoteModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PyAnnoteModel), +/* harmony export */ PyAnnotePreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PyAnnotePreTrainedModel), +/* harmony export */ PyAnnoteProcessor: () => (/* reexport safe */ _models_processors_js__WEBPACK_IMPORTED_MODULE_17__.PyAnnoteProcessor), +/* harmony export */ QuestionAnsweringModelOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.QuestionAnsweringModelOutput), +/* harmony export */ QuestionAnsweringPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.QuestionAnsweringPipeline), +/* harmony export */ Qwen2ForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Qwen2ForCausalLM), +/* harmony export */ Qwen2Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Qwen2Model), +/* harmony export */ Qwen2PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Qwen2PreTrainedModel), +/* harmony export */ Qwen2Tokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.Qwen2Tokenizer), +/* harmony export */ Qwen2VLForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Qwen2VLForConditionalGeneration), +/* harmony export */ Qwen2VLImageProcessor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.Qwen2VLImageProcessor), +/* harmony export */ Qwen2VLPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Qwen2VLPreTrainedModel), +/* harmony export */ Qwen2VLProcessor: () => (/* reexport safe */ _models_processors_js__WEBPACK_IMPORTED_MODULE_17__.Qwen2VLProcessor), +/* harmony export */ Qwen3ForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Qwen3ForCausalLM), +/* harmony export */ Qwen3Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Qwen3Model), +/* harmony export */ Qwen3PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Qwen3PreTrainedModel), +/* harmony export */ RFDetrForObjectDetection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RFDetrForObjectDetection), +/* harmony export */ RFDetrModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RFDetrModel), +/* harmony export */ RFDetrObjectDetectionOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RFDetrObjectDetectionOutput), +/* harmony export */ RFDetrPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RFDetrPreTrainedModel), +/* harmony export */ RTDetrForObjectDetection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RTDetrForObjectDetection), +/* harmony export */ RTDetrImageProcessor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.RTDetrImageProcessor), +/* harmony export */ RTDetrModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RTDetrModel), +/* harmony export */ RTDetrObjectDetectionOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RTDetrObjectDetectionOutput), +/* harmony export */ RTDetrPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RTDetrPreTrainedModel), +/* harmony export */ RTDetrV2ForObjectDetection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RTDetrV2ForObjectDetection), +/* harmony export */ RTDetrV2Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RTDetrV2Model), +/* harmony export */ RTDetrV2ObjectDetectionOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RTDetrV2ObjectDetectionOutput), +/* harmony export */ RTDetrV2PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RTDetrV2PreTrainedModel), +/* harmony export */ RawAudio: () => (/* reexport safe */ _utils_audio_js__WEBPACK_IMPORTED_MODULE_5__.RawAudio), +/* harmony export */ RawImage: () => (/* reexport safe */ _utils_image_js__WEBPACK_IMPORTED_MODULE_6__.RawImage), +/* harmony export */ RawVideo: () => (/* reexport safe */ _utils_video_js__WEBPACK_IMPORTED_MODULE_7__.RawVideo), +/* harmony export */ RawVideoFrame: () => (/* reexport safe */ _utils_video_js__WEBPACK_IMPORTED_MODULE_7__.RawVideoFrame), +/* harmony export */ RepetitionPenaltyLogitsProcessor: () => (/* reexport safe */ _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_21__.RepetitionPenaltyLogitsProcessor), +/* harmony export */ ResNetForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ResNetForImageClassification), +/* harmony export */ ResNetModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ResNetModel), +/* harmony export */ ResNetPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ResNetPreTrainedModel), +/* harmony export */ RoFormerForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RoFormerForMaskedLM), +/* harmony export */ RoFormerForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RoFormerForQuestionAnswering), +/* harmony export */ RoFormerForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RoFormerForSequenceClassification), +/* harmony export */ RoFormerForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RoFormerForTokenClassification), +/* harmony export */ RoFormerModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RoFormerModel), +/* harmony export */ RoFormerPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RoFormerPreTrainedModel), +/* harmony export */ RoFormerTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.RoFormerTokenizer), +/* harmony export */ RobertaForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RobertaForMaskedLM), +/* harmony export */ RobertaForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RobertaForQuestionAnswering), +/* harmony export */ RobertaForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RobertaForSequenceClassification), +/* harmony export */ RobertaForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RobertaForTokenClassification), +/* harmony export */ RobertaModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RobertaModel), +/* harmony export */ RobertaPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RobertaPreTrainedModel), +/* harmony export */ RobertaTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.RobertaTokenizer), +/* harmony export */ SamImageProcessor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.SamImageProcessor), +/* harmony export */ SamImageSegmentationOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SamImageSegmentationOutput), +/* harmony export */ SamModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SamModel), +/* harmony export */ SamPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SamPreTrainedModel), +/* harmony export */ SamProcessor: () => (/* reexport safe */ _models_processors_js__WEBPACK_IMPORTED_MODULE_17__.SamProcessor), +/* harmony export */ SapiensForDepthEstimation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SapiensForDepthEstimation), +/* harmony export */ SapiensForNormalEstimation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SapiensForNormalEstimation), +/* harmony export */ SapiensForSemanticSegmentation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SapiensForSemanticSegmentation), +/* harmony export */ SapiensPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SapiensPreTrainedModel), +/* harmony export */ SeamlessM4TFeatureExtractor: () => (/* reexport safe */ _models_feature_extractors_js__WEBPACK_IMPORTED_MODULE_11__.SeamlessM4TFeatureExtractor), +/* harmony export */ SegformerFeatureExtractor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.SegformerFeatureExtractor), +/* harmony export */ SegformerForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SegformerForImageClassification), +/* harmony export */ SegformerForSemanticSegmentation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SegformerForSemanticSegmentation), +/* harmony export */ SegformerImageProcessor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.SegformerImageProcessor), +/* harmony export */ SegformerModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SegformerModel), +/* harmony export */ SegformerPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SegformerPreTrainedModel), +/* harmony export */ Seq2SeqLMOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Seq2SeqLMOutput), +/* harmony export */ SequenceClassifierOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SequenceClassifierOutput), +/* harmony export */ SiglipImageProcessor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.SiglipImageProcessor), +/* harmony export */ SiglipModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SiglipModel), +/* harmony export */ SiglipPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SiglipPreTrainedModel), +/* harmony export */ SiglipTextModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SiglipTextModel), +/* harmony export */ SiglipTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.SiglipTokenizer), +/* harmony export */ SiglipVisionModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SiglipVisionModel), +/* harmony export */ SmolVLMForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SmolVLMForConditionalGeneration), +/* harmony export */ SmolVLMImageProcessor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.SmolVLMImageProcessor), +/* harmony export */ SmolVLMProcessor: () => (/* reexport safe */ _models_processors_js__WEBPACK_IMPORTED_MODULE_17__.SmolVLMProcessor), +/* harmony export */ SnacDecoderModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SnacDecoderModel), +/* harmony export */ SnacEncoderModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SnacEncoderModel), +/* harmony export */ SnacFeatureExtractor: () => (/* reexport safe */ _models_feature_extractors_js__WEBPACK_IMPORTED_MODULE_11__.SnacFeatureExtractor), +/* harmony export */ SnacModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SnacModel), +/* harmony export */ SnacPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SnacPreTrainedModel), +/* harmony export */ SpeechT5FeatureExtractor: () => (/* reexport safe */ _models_feature_extractors_js__WEBPACK_IMPORTED_MODULE_11__.SpeechT5FeatureExtractor), +/* harmony export */ SpeechT5ForSpeechToText: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SpeechT5ForSpeechToText), +/* harmony export */ SpeechT5ForTextToSpeech: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SpeechT5ForTextToSpeech), +/* harmony export */ SpeechT5HifiGan: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SpeechT5HifiGan), +/* harmony export */ SpeechT5Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SpeechT5Model), +/* harmony export */ SpeechT5PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SpeechT5PreTrainedModel), +/* harmony export */ SpeechT5Processor: () => (/* reexport safe */ _models_processors_js__WEBPACK_IMPORTED_MODULE_17__.SpeechT5Processor), +/* harmony export */ SpeechT5Tokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.SpeechT5Tokenizer), +/* harmony export */ SqueezeBertForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SqueezeBertForMaskedLM), +/* harmony export */ SqueezeBertForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SqueezeBertForQuestionAnswering), +/* harmony export */ SqueezeBertForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SqueezeBertForSequenceClassification), +/* harmony export */ SqueezeBertModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SqueezeBertModel), +/* harmony export */ SqueezeBertPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SqueezeBertPreTrainedModel), +/* harmony export */ SqueezeBertTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.SqueezeBertTokenizer), +/* harmony export */ StableLmForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.StableLmForCausalLM), +/* harmony export */ StableLmModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.StableLmModel), +/* harmony export */ StableLmPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.StableLmPreTrainedModel), +/* harmony export */ Starcoder2ForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Starcoder2ForCausalLM), +/* harmony export */ Starcoder2Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Starcoder2Model), +/* harmony export */ Starcoder2PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Starcoder2PreTrainedModel), +/* harmony export */ StoppingCriteria: () => (/* reexport safe */ _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_20__.StoppingCriteria), +/* harmony export */ StoppingCriteriaList: () => (/* reexport safe */ _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_20__.StoppingCriteriaList), +/* harmony export */ StyleTextToSpeech2Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.StyleTextToSpeech2Model), +/* harmony export */ StyleTextToSpeech2PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.StyleTextToSpeech2PreTrainedModel), +/* harmony export */ SummarizationPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.SummarizationPipeline), +/* harmony export */ SuppressTokensAtBeginLogitsProcessor: () => (/* reexport safe */ _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_21__.SuppressTokensAtBeginLogitsProcessor), +/* harmony export */ Swin2SRForImageSuperResolution: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Swin2SRForImageSuperResolution), +/* harmony export */ Swin2SRImageProcessor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.Swin2SRImageProcessor), +/* harmony export */ Swin2SRModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Swin2SRModel), +/* harmony export */ Swin2SRPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Swin2SRPreTrainedModel), +/* harmony export */ SwinForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SwinForImageClassification), +/* harmony export */ SwinForSemanticSegmentation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SwinForSemanticSegmentation), +/* harmony export */ SwinModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SwinModel), +/* harmony export */ SwinPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SwinPreTrainedModel), +/* harmony export */ T5ForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.T5ForConditionalGeneration), +/* harmony export */ T5Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.T5Model), +/* harmony export */ T5PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.T5PreTrainedModel), +/* harmony export */ T5Tokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.T5Tokenizer), +/* harmony export */ TableTransformerForObjectDetection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.TableTransformerForObjectDetection), +/* harmony export */ TableTransformerModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.TableTransformerModel), +/* harmony export */ TableTransformerObjectDetectionOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.TableTransformerObjectDetectionOutput), +/* harmony export */ TableTransformerPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.TableTransformerPreTrainedModel), +/* harmony export */ TemperatureLogitsWarper: () => (/* reexport safe */ _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_21__.TemperatureLogitsWarper), +/* harmony export */ Tensor: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.Tensor), +/* harmony export */ Text2TextGenerationPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.Text2TextGenerationPipeline), +/* harmony export */ TextClassificationPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.TextClassificationPipeline), +/* harmony export */ TextGenerationPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.TextGenerationPipeline), +/* harmony export */ TextStreamer: () => (/* reexport safe */ _generation_streamers_js__WEBPACK_IMPORTED_MODULE_19__.TextStreamer), +/* harmony export */ TextToAudioPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.TextToAudioPipeline), +/* harmony export */ TokenClassificationPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.TokenClassificationPipeline), +/* harmony export */ TokenClassifierOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.TokenClassifierOutput), +/* harmony export */ TokenizerModel: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.TokenizerModel), +/* harmony export */ TopKLogitsWarper: () => (/* reexport safe */ _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_21__.TopKLogitsWarper), +/* harmony export */ TopPLogitsWarper: () => (/* reexport safe */ _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_21__.TopPLogitsWarper), +/* harmony export */ TrOCRForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.TrOCRForCausalLM), +/* harmony export */ TrOCRPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.TrOCRPreTrainedModel), +/* harmony export */ TranslationPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.TranslationPipeline), +/* harmony export */ UltravoxModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.UltravoxModel), +/* harmony export */ UltravoxPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.UltravoxPreTrainedModel), +/* harmony export */ UltravoxProcessor: () => (/* reexport safe */ _models_processors_js__WEBPACK_IMPORTED_MODULE_17__.UltravoxProcessor), +/* harmony export */ UniSpeechForCTC: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechForCTC), +/* harmony export */ UniSpeechForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechForSequenceClassification), +/* harmony export */ UniSpeechModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechModel), +/* harmony export */ UniSpeechPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechPreTrainedModel), +/* harmony export */ UniSpeechSatForAudioFrameClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechSatForAudioFrameClassification), +/* harmony export */ UniSpeechSatForCTC: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechSatForCTC), +/* harmony export */ UniSpeechSatForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechSatForSequenceClassification), +/* harmony export */ UniSpeechSatModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechSatModel), +/* harmony export */ UniSpeechSatPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechSatPreTrainedModel), +/* harmony export */ VLChatProcessor: () => (/* reexport safe */ _models_processors_js__WEBPACK_IMPORTED_MODULE_17__.VLChatProcessor), +/* harmony export */ VLMImageProcessor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.VLMImageProcessor), +/* harmony export */ ViTFeatureExtractor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.ViTFeatureExtractor), +/* harmony export */ ViTForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTForImageClassification), +/* harmony export */ ViTImageProcessor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.ViTImageProcessor), +/* harmony export */ ViTMAEModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTMAEModel), +/* harmony export */ ViTMAEPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTMAEPreTrainedModel), +/* harmony export */ ViTMSNForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTMSNForImageClassification), +/* harmony export */ ViTMSNModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTMSNModel), +/* harmony export */ ViTMSNPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTMSNPreTrainedModel), +/* harmony export */ ViTModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTModel), +/* harmony export */ ViTPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTPreTrainedModel), +/* harmony export */ VisionEncoderDecoderModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.VisionEncoderDecoderModel), +/* harmony export */ VitMatteForImageMatting: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.VitMatteForImageMatting), +/* harmony export */ VitMatteImageProcessor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.VitMatteImageProcessor), +/* harmony export */ VitMattePreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.VitMattePreTrainedModel), +/* harmony export */ VitPoseForPoseEstimation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.VitPoseForPoseEstimation), +/* harmony export */ VitPoseImageProcessor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.VitPoseImageProcessor), +/* harmony export */ VitPosePreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.VitPosePreTrainedModel), +/* harmony export */ VitsModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.VitsModel), +/* harmony export */ VitsModelOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.VitsModelOutput), +/* harmony export */ VitsPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.VitsPreTrainedModel), +/* harmony export */ VitsTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.VitsTokenizer), +/* harmony export */ Wav2Vec2BertForCTC: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2BertForCTC), +/* harmony export */ Wav2Vec2BertForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2BertForSequenceClassification), +/* harmony export */ Wav2Vec2BertModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2BertModel), +/* harmony export */ Wav2Vec2BertPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2BertPreTrainedModel), +/* harmony export */ Wav2Vec2CTCTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.Wav2Vec2CTCTokenizer), +/* harmony export */ Wav2Vec2FeatureExtractor: () => (/* reexport safe */ _models_feature_extractors_js__WEBPACK_IMPORTED_MODULE_11__.Wav2Vec2FeatureExtractor), +/* harmony export */ Wav2Vec2ForAudioFrameClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2ForAudioFrameClassification), +/* harmony export */ Wav2Vec2ForCTC: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2ForCTC), +/* harmony export */ Wav2Vec2ForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2ForSequenceClassification), +/* harmony export */ Wav2Vec2Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2Model), +/* harmony export */ Wav2Vec2PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2PreTrainedModel), +/* harmony export */ Wav2Vec2Processor: () => (/* reexport safe */ _models_processors_js__WEBPACK_IMPORTED_MODULE_17__.Wav2Vec2Processor), +/* harmony export */ Wav2Vec2ProcessorWithLM: () => (/* reexport safe */ _models_processors_js__WEBPACK_IMPORTED_MODULE_17__.Wav2Vec2ProcessorWithLM), +/* harmony export */ WavLMForAudioFrameClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WavLMForAudioFrameClassification), +/* harmony export */ WavLMForCTC: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WavLMForCTC), +/* harmony export */ WavLMForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WavLMForSequenceClassification), +/* harmony export */ WavLMForXVector: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WavLMForXVector), +/* harmony export */ WavLMModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WavLMModel), +/* harmony export */ WavLMPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WavLMPreTrainedModel), +/* harmony export */ WeSpeakerFeatureExtractor: () => (/* reexport safe */ _models_feature_extractors_js__WEBPACK_IMPORTED_MODULE_11__.WeSpeakerFeatureExtractor), +/* harmony export */ WeSpeakerResNetModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WeSpeakerResNetModel), +/* harmony export */ WeSpeakerResNetPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WeSpeakerResNetPreTrainedModel), +/* harmony export */ WhisperFeatureExtractor: () => (/* reexport safe */ _models_feature_extractors_js__WEBPACK_IMPORTED_MODULE_11__.WhisperFeatureExtractor), +/* harmony export */ WhisperForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WhisperForConditionalGeneration), +/* harmony export */ WhisperModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WhisperModel), +/* harmony export */ WhisperPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WhisperPreTrainedModel), +/* harmony export */ WhisperProcessor: () => (/* reexport safe */ _models_processors_js__WEBPACK_IMPORTED_MODULE_17__.WhisperProcessor), +/* harmony export */ WhisperTextStreamer: () => (/* reexport safe */ _generation_streamers_js__WEBPACK_IMPORTED_MODULE_19__.WhisperTextStreamer), +/* harmony export */ WhisperTimeStampLogitsProcessor: () => (/* reexport safe */ _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_21__.WhisperTimeStampLogitsProcessor), +/* harmony export */ WhisperTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.WhisperTokenizer), +/* harmony export */ XLMForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMForQuestionAnswering), +/* harmony export */ XLMForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMForSequenceClassification), +/* harmony export */ XLMForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMForTokenClassification), +/* harmony export */ XLMModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMModel), +/* harmony export */ XLMPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMPreTrainedModel), +/* harmony export */ XLMRobertaForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMRobertaForMaskedLM), +/* harmony export */ XLMRobertaForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMRobertaForQuestionAnswering), +/* harmony export */ XLMRobertaForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMRobertaForSequenceClassification), +/* harmony export */ XLMRobertaForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMRobertaForTokenClassification), +/* harmony export */ XLMRobertaModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMRobertaModel), +/* harmony export */ XLMRobertaPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMRobertaPreTrainedModel), +/* harmony export */ XLMRobertaTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.XLMRobertaTokenizer), +/* harmony export */ XLMTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.XLMTokenizer), +/* harmony export */ XLMWithLMHeadModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMWithLMHeadModel), +/* harmony export */ XVectorOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XVectorOutput), +/* harmony export */ YolosFeatureExtractor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.YolosFeatureExtractor), +/* harmony export */ YolosForObjectDetection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.YolosForObjectDetection), +/* harmony export */ YolosImageProcessor: () => (/* reexport safe */ _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__.YolosImageProcessor), +/* harmony export */ YolosModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.YolosModel), +/* harmony export */ YolosObjectDetectionOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.YolosObjectDetectionOutput), +/* harmony export */ YolosPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.YolosPreTrainedModel), +/* harmony export */ ZeroShotAudioClassificationPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ZeroShotAudioClassificationPipeline), +/* harmony export */ ZeroShotClassificationPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ZeroShotClassificationPipeline), +/* harmony export */ ZeroShotImageClassificationPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ZeroShotImageClassificationPipeline), +/* harmony export */ ZeroShotObjectDetectionPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ZeroShotObjectDetectionPipeline), +/* harmony export */ bankers_round: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.bankers_round), +/* harmony export */ cat: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.cat), +/* harmony export */ cos_sim: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.cos_sim), +/* harmony export */ dot: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.dot), +/* harmony export */ dynamic_time_warping: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.dynamic_time_warping), +/* harmony export */ env: () => (/* reexport safe */ _env_js__WEBPACK_IMPORTED_MODULE_0__.env), +/* harmony export */ full: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.full), +/* harmony export */ full_like: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.full_like), +/* harmony export */ getKeyValueShapes: () => (/* reexport safe */ _configs_js__WEBPACK_IMPORTED_MODULE_4__.getKeyValueShapes), +/* harmony export */ hamming: () => (/* reexport safe */ _utils_audio_js__WEBPACK_IMPORTED_MODULE_5__.hamming), +/* harmony export */ hanning: () => (/* reexport safe */ _utils_audio_js__WEBPACK_IMPORTED_MODULE_5__.hanning), +/* harmony export */ interpolate: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.interpolate), +/* harmony export */ interpolate_4d: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.interpolate_4d), +/* harmony export */ interpolate_data: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.interpolate_data), +/* harmony export */ is_chinese_char: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.is_chinese_char), +/* harmony export */ layer_norm: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.layer_norm), +/* harmony export */ load_image: () => (/* reexport safe */ _utils_image_js__WEBPACK_IMPORTED_MODULE_6__.load_image), +/* harmony export */ load_video: () => (/* reexport safe */ _utils_video_js__WEBPACK_IMPORTED_MODULE_7__.load_video), +/* harmony export */ log_softmax: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.log_softmax), +/* harmony export */ magnitude: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.magnitude), +/* harmony export */ matmul: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.matmul), +/* harmony export */ max: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.max), +/* harmony export */ mean: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.mean), +/* harmony export */ mean_pooling: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.mean_pooling), +/* harmony export */ medianFilter: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.medianFilter), +/* harmony export */ mel_filter_bank: () => (/* reexport safe */ _utils_audio_js__WEBPACK_IMPORTED_MODULE_5__.mel_filter_bank), +/* harmony export */ min: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.min), +/* harmony export */ ones: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.ones), +/* harmony export */ ones_like: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.ones_like), +/* harmony export */ permute: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.permute), +/* harmony export */ permute_data: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.permute_data), +/* harmony export */ pipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.pipeline), +/* harmony export */ quantize_embeddings: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.quantize_embeddings), +/* harmony export */ rand: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.rand), +/* harmony export */ read_audio: () => (/* reexport safe */ _utils_audio_js__WEBPACK_IMPORTED_MODULE_5__.read_audio), +/* harmony export */ rfft: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.rfft), +/* harmony export */ round: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.round), +/* harmony export */ slice: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.slice), +/* harmony export */ softmax: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.softmax), +/* harmony export */ spectrogram: () => (/* reexport safe */ _utils_audio_js__WEBPACK_IMPORTED_MODULE_5__.spectrogram), +/* harmony export */ stack: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.stack), +/* harmony export */ std_mean: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.std_mean), +/* harmony export */ topk: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.topk), +/* harmony export */ window_function: () => (/* reexport safe */ _utils_audio_js__WEBPACK_IMPORTED_MODULE_5__.window_function), +/* harmony export */ zeros: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.zeros), +/* harmony export */ zeros_like: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.zeros_like) +/* harmony export */ }); +/* harmony import */ var _env_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./env.js */ "./src/env.js"); +/* harmony import */ var _pipelines_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./pipelines.js */ "./src/pipelines.js"); +/* harmony import */ var _models_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./models.js */ "./src/models.js"); +/* harmony import */ var _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./tokenizers.js */ "./src/tokenizers.js"); +/* harmony import */ var _configs_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ./configs.js */ "./src/configs.js"); +/* harmony import */ var _utils_audio_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ./utils/audio.js */ "./src/utils/audio.js"); +/* harmony import */ var _utils_image_js__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! ./utils/image.js */ "./src/utils/image.js"); +/* harmony import */ var _utils_video_js__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__(/*! ./utils/video.js */ "./src/utils/video.js"); +/* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__(/*! ./utils/tensor.js */ "./src/utils/tensor.js"); +/* harmony import */ var _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__ = __webpack_require__(/*! ./utils/maths.js */ "./src/utils/maths.js"); +/* harmony import */ var _base_feature_extraction_utils_js__WEBPACK_IMPORTED_MODULE_10__ = __webpack_require__(/*! ./base/feature_extraction_utils.js */ "./src/base/feature_extraction_utils.js"); +/* harmony import */ var _models_feature_extractors_js__WEBPACK_IMPORTED_MODULE_11__ = __webpack_require__(/*! ./models/feature_extractors.js */ "./src/models/feature_extractors.js"); +/* harmony import */ var _models_auto_feature_extraction_auto_js__WEBPACK_IMPORTED_MODULE_12__ = __webpack_require__(/*! ./models/auto/feature_extraction_auto.js */ "./src/models/auto/feature_extraction_auto.js"); +/* harmony import */ var _base_image_processors_utils_js__WEBPACK_IMPORTED_MODULE_13__ = __webpack_require__(/*! ./base/image_processors_utils.js */ "./src/base/image_processors_utils.js"); +/* harmony import */ var _models_image_processors_js__WEBPACK_IMPORTED_MODULE_14__ = __webpack_require__(/*! ./models/image_processors.js */ "./src/models/image_processors.js"); +/* harmony import */ var _models_auto_image_processing_auto_js__WEBPACK_IMPORTED_MODULE_15__ = __webpack_require__(/*! ./models/auto/image_processing_auto.js */ "./src/models/auto/image_processing_auto.js"); +/* harmony import */ var _base_processing_utils_js__WEBPACK_IMPORTED_MODULE_16__ = __webpack_require__(/*! ./base/processing_utils.js */ "./src/base/processing_utils.js"); +/* harmony import */ var _models_processors_js__WEBPACK_IMPORTED_MODULE_17__ = __webpack_require__(/*! ./models/processors.js */ "./src/models/processors.js"); +/* harmony import */ var _models_auto_processing_auto_js__WEBPACK_IMPORTED_MODULE_18__ = __webpack_require__(/*! ./models/auto/processing_auto.js */ "./src/models/auto/processing_auto.js"); +/* harmony import */ var _generation_streamers_js__WEBPACK_IMPORTED_MODULE_19__ = __webpack_require__(/*! ./generation/streamers.js */ "./src/generation/streamers.js"); +/* harmony import */ var _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_20__ = __webpack_require__(/*! ./generation/stopping_criteria.js */ "./src/generation/stopping_criteria.js"); +/* harmony import */ var _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_21__ = __webpack_require__(/*! ./generation/logits_process.js */ "./src/generation/logits_process.js"); +/** + * @file Entry point for the Transformers.js library. Only the exports from this file + * are available to the end user, and are grouped as follows: + * + * 1. [Pipelines](./pipelines) + * 2. [Environment variables](./env) + * 3. [Models](./models) + * 4. [Tokenizers](./tokenizers) + * 5. [Processors](./processors) + * + * @module transformers + */ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +})(); + +var __webpack_exports__ASTFeatureExtractor = __webpack_exports__.ASTFeatureExtractor; +var __webpack_exports__ASTForAudioClassification = __webpack_exports__.ASTForAudioClassification; +var __webpack_exports__ASTModel = __webpack_exports__.ASTModel; +var __webpack_exports__ASTPreTrainedModel = __webpack_exports__.ASTPreTrainedModel; +var __webpack_exports__AlbertForMaskedLM = __webpack_exports__.AlbertForMaskedLM; +var __webpack_exports__AlbertForQuestionAnswering = __webpack_exports__.AlbertForQuestionAnswering; +var __webpack_exports__AlbertForSequenceClassification = __webpack_exports__.AlbertForSequenceClassification; +var __webpack_exports__AlbertModel = __webpack_exports__.AlbertModel; +var __webpack_exports__AlbertPreTrainedModel = __webpack_exports__.AlbertPreTrainedModel; +var __webpack_exports__AlbertTokenizer = __webpack_exports__.AlbertTokenizer; +var __webpack_exports__AudioClassificationPipeline = __webpack_exports__.AudioClassificationPipeline; +var __webpack_exports__AutoConfig = __webpack_exports__.AutoConfig; +var __webpack_exports__AutoFeatureExtractor = __webpack_exports__.AutoFeatureExtractor; +var __webpack_exports__AutoImageProcessor = __webpack_exports__.AutoImageProcessor; +var __webpack_exports__AutoModel = __webpack_exports__.AutoModel; +var __webpack_exports__AutoModelForAudioClassification = __webpack_exports__.AutoModelForAudioClassification; +var __webpack_exports__AutoModelForAudioFrameClassification = __webpack_exports__.AutoModelForAudioFrameClassification; +var __webpack_exports__AutoModelForAudioTextToText = __webpack_exports__.AutoModelForAudioTextToText; +var __webpack_exports__AutoModelForCTC = __webpack_exports__.AutoModelForCTC; +var __webpack_exports__AutoModelForCausalLM = __webpack_exports__.AutoModelForCausalLM; +var __webpack_exports__AutoModelForDepthEstimation = __webpack_exports__.AutoModelForDepthEstimation; +var __webpack_exports__AutoModelForDocumentQuestionAnswering = __webpack_exports__.AutoModelForDocumentQuestionAnswering; +var __webpack_exports__AutoModelForImageClassification = __webpack_exports__.AutoModelForImageClassification; +var __webpack_exports__AutoModelForImageFeatureExtraction = __webpack_exports__.AutoModelForImageFeatureExtraction; +var __webpack_exports__AutoModelForImageMatting = __webpack_exports__.AutoModelForImageMatting; +var __webpack_exports__AutoModelForImageSegmentation = __webpack_exports__.AutoModelForImageSegmentation; +var __webpack_exports__AutoModelForImageTextToText = __webpack_exports__.AutoModelForImageTextToText; +var __webpack_exports__AutoModelForImageToImage = __webpack_exports__.AutoModelForImageToImage; +var __webpack_exports__AutoModelForMaskGeneration = __webpack_exports__.AutoModelForMaskGeneration; +var __webpack_exports__AutoModelForMaskedLM = __webpack_exports__.AutoModelForMaskedLM; +var __webpack_exports__AutoModelForNormalEstimation = __webpack_exports__.AutoModelForNormalEstimation; +var __webpack_exports__AutoModelForObjectDetection = __webpack_exports__.AutoModelForObjectDetection; +var __webpack_exports__AutoModelForPoseEstimation = __webpack_exports__.AutoModelForPoseEstimation; +var __webpack_exports__AutoModelForQuestionAnswering = __webpack_exports__.AutoModelForQuestionAnswering; +var __webpack_exports__AutoModelForSemanticSegmentation = __webpack_exports__.AutoModelForSemanticSegmentation; +var __webpack_exports__AutoModelForSeq2SeqLM = __webpack_exports__.AutoModelForSeq2SeqLM; +var __webpack_exports__AutoModelForSequenceClassification = __webpack_exports__.AutoModelForSequenceClassification; +var __webpack_exports__AutoModelForSpeechSeq2Seq = __webpack_exports__.AutoModelForSpeechSeq2Seq; +var __webpack_exports__AutoModelForTextToSpectrogram = __webpack_exports__.AutoModelForTextToSpectrogram; +var __webpack_exports__AutoModelForTextToWaveform = __webpack_exports__.AutoModelForTextToWaveform; +var __webpack_exports__AutoModelForTokenClassification = __webpack_exports__.AutoModelForTokenClassification; +var __webpack_exports__AutoModelForUniversalSegmentation = __webpack_exports__.AutoModelForUniversalSegmentation; +var __webpack_exports__AutoModelForVision2Seq = __webpack_exports__.AutoModelForVision2Seq; +var __webpack_exports__AutoModelForXVector = __webpack_exports__.AutoModelForXVector; +var __webpack_exports__AutoModelForZeroShotObjectDetection = __webpack_exports__.AutoModelForZeroShotObjectDetection; +var __webpack_exports__AutoProcessor = __webpack_exports__.AutoProcessor; +var __webpack_exports__AutoTokenizer = __webpack_exports__.AutoTokenizer; +var __webpack_exports__AutomaticSpeechRecognitionPipeline = __webpack_exports__.AutomaticSpeechRecognitionPipeline; +var __webpack_exports__BackgroundRemovalPipeline = __webpack_exports__.BackgroundRemovalPipeline; +var __webpack_exports__BartForConditionalGeneration = __webpack_exports__.BartForConditionalGeneration; +var __webpack_exports__BartForSequenceClassification = __webpack_exports__.BartForSequenceClassification; +var __webpack_exports__BartModel = __webpack_exports__.BartModel; +var __webpack_exports__BartPretrainedModel = __webpack_exports__.BartPretrainedModel; +var __webpack_exports__BartTokenizer = __webpack_exports__.BartTokenizer; +var __webpack_exports__BaseModelOutput = __webpack_exports__.BaseModelOutput; +var __webpack_exports__BaseStreamer = __webpack_exports__.BaseStreamer; +var __webpack_exports__BeitFeatureExtractor = __webpack_exports__.BeitFeatureExtractor; +var __webpack_exports__BeitForImageClassification = __webpack_exports__.BeitForImageClassification; +var __webpack_exports__BeitModel = __webpack_exports__.BeitModel; +var __webpack_exports__BeitPreTrainedModel = __webpack_exports__.BeitPreTrainedModel; +var __webpack_exports__BertForMaskedLM = __webpack_exports__.BertForMaskedLM; +var __webpack_exports__BertForQuestionAnswering = __webpack_exports__.BertForQuestionAnswering; +var __webpack_exports__BertForSequenceClassification = __webpack_exports__.BertForSequenceClassification; +var __webpack_exports__BertForTokenClassification = __webpack_exports__.BertForTokenClassification; +var __webpack_exports__BertModel = __webpack_exports__.BertModel; +var __webpack_exports__BertPreTrainedModel = __webpack_exports__.BertPreTrainedModel; +var __webpack_exports__BertTokenizer = __webpack_exports__.BertTokenizer; +var __webpack_exports__BitImageProcessor = __webpack_exports__.BitImageProcessor; +var __webpack_exports__BlenderbotForConditionalGeneration = __webpack_exports__.BlenderbotForConditionalGeneration; +var __webpack_exports__BlenderbotModel = __webpack_exports__.BlenderbotModel; +var __webpack_exports__BlenderbotPreTrainedModel = __webpack_exports__.BlenderbotPreTrainedModel; +var __webpack_exports__BlenderbotSmallForConditionalGeneration = __webpack_exports__.BlenderbotSmallForConditionalGeneration; +var __webpack_exports__BlenderbotSmallModel = __webpack_exports__.BlenderbotSmallModel; +var __webpack_exports__BlenderbotSmallPreTrainedModel = __webpack_exports__.BlenderbotSmallPreTrainedModel; +var __webpack_exports__BlenderbotSmallTokenizer = __webpack_exports__.BlenderbotSmallTokenizer; +var __webpack_exports__BlenderbotTokenizer = __webpack_exports__.BlenderbotTokenizer; +var __webpack_exports__BloomForCausalLM = __webpack_exports__.BloomForCausalLM; +var __webpack_exports__BloomModel = __webpack_exports__.BloomModel; +var __webpack_exports__BloomPreTrainedModel = __webpack_exports__.BloomPreTrainedModel; +var __webpack_exports__BloomTokenizer = __webpack_exports__.BloomTokenizer; +var __webpack_exports__CLIPFeatureExtractor = __webpack_exports__.CLIPFeatureExtractor; +var __webpack_exports__CLIPImageProcessor = __webpack_exports__.CLIPImageProcessor; +var __webpack_exports__CLIPModel = __webpack_exports__.CLIPModel; +var __webpack_exports__CLIPPreTrainedModel = __webpack_exports__.CLIPPreTrainedModel; +var __webpack_exports__CLIPSegForImageSegmentation = __webpack_exports__.CLIPSegForImageSegmentation; +var __webpack_exports__CLIPSegModel = __webpack_exports__.CLIPSegModel; +var __webpack_exports__CLIPSegPreTrainedModel = __webpack_exports__.CLIPSegPreTrainedModel; +var __webpack_exports__CLIPTextModel = __webpack_exports__.CLIPTextModel; +var __webpack_exports__CLIPTextModelWithProjection = __webpack_exports__.CLIPTextModelWithProjection; +var __webpack_exports__CLIPTokenizer = __webpack_exports__.CLIPTokenizer; +var __webpack_exports__CLIPVisionModel = __webpack_exports__.CLIPVisionModel; +var __webpack_exports__CLIPVisionModelWithProjection = __webpack_exports__.CLIPVisionModelWithProjection; +var __webpack_exports__CamembertForMaskedLM = __webpack_exports__.CamembertForMaskedLM; +var __webpack_exports__CamembertForQuestionAnswering = __webpack_exports__.CamembertForQuestionAnswering; +var __webpack_exports__CamembertForSequenceClassification = __webpack_exports__.CamembertForSequenceClassification; +var __webpack_exports__CamembertForTokenClassification = __webpack_exports__.CamembertForTokenClassification; +var __webpack_exports__CamembertModel = __webpack_exports__.CamembertModel; +var __webpack_exports__CamembertPreTrainedModel = __webpack_exports__.CamembertPreTrainedModel; +var __webpack_exports__CamembertTokenizer = __webpack_exports__.CamembertTokenizer; +var __webpack_exports__CausalLMOutput = __webpack_exports__.CausalLMOutput; +var __webpack_exports__CausalLMOutputWithPast = __webpack_exports__.CausalLMOutputWithPast; +var __webpack_exports__ChineseCLIPFeatureExtractor = __webpack_exports__.ChineseCLIPFeatureExtractor; +var __webpack_exports__ChineseCLIPModel = __webpack_exports__.ChineseCLIPModel; +var __webpack_exports__ChineseCLIPPreTrainedModel = __webpack_exports__.ChineseCLIPPreTrainedModel; +var __webpack_exports__ClapAudioModelWithProjection = __webpack_exports__.ClapAudioModelWithProjection; +var __webpack_exports__ClapFeatureExtractor = __webpack_exports__.ClapFeatureExtractor; +var __webpack_exports__ClapModel = __webpack_exports__.ClapModel; +var __webpack_exports__ClapPreTrainedModel = __webpack_exports__.ClapPreTrainedModel; +var __webpack_exports__ClapTextModelWithProjection = __webpack_exports__.ClapTextModelWithProjection; +var __webpack_exports__ClassifierFreeGuidanceLogitsProcessor = __webpack_exports__.ClassifierFreeGuidanceLogitsProcessor; +var __webpack_exports__CodeGenForCausalLM = __webpack_exports__.CodeGenForCausalLM; +var __webpack_exports__CodeGenModel = __webpack_exports__.CodeGenModel; +var __webpack_exports__CodeGenPreTrainedModel = __webpack_exports__.CodeGenPreTrainedModel; +var __webpack_exports__CodeGenTokenizer = __webpack_exports__.CodeGenTokenizer; +var __webpack_exports__CodeLlamaTokenizer = __webpack_exports__.CodeLlamaTokenizer; +var __webpack_exports__CohereForCausalLM = __webpack_exports__.CohereForCausalLM; +var __webpack_exports__CohereModel = __webpack_exports__.CohereModel; +var __webpack_exports__CoherePreTrainedModel = __webpack_exports__.CoherePreTrainedModel; +var __webpack_exports__CohereTokenizer = __webpack_exports__.CohereTokenizer; +var __webpack_exports__ConvBertForMaskedLM = __webpack_exports__.ConvBertForMaskedLM; +var __webpack_exports__ConvBertForQuestionAnswering = __webpack_exports__.ConvBertForQuestionAnswering; +var __webpack_exports__ConvBertForSequenceClassification = __webpack_exports__.ConvBertForSequenceClassification; +var __webpack_exports__ConvBertForTokenClassification = __webpack_exports__.ConvBertForTokenClassification; +var __webpack_exports__ConvBertModel = __webpack_exports__.ConvBertModel; +var __webpack_exports__ConvBertPreTrainedModel = __webpack_exports__.ConvBertPreTrainedModel; +var __webpack_exports__ConvBertTokenizer = __webpack_exports__.ConvBertTokenizer; +var __webpack_exports__ConvNextFeatureExtractor = __webpack_exports__.ConvNextFeatureExtractor; +var __webpack_exports__ConvNextForImageClassification = __webpack_exports__.ConvNextForImageClassification; +var __webpack_exports__ConvNextImageProcessor = __webpack_exports__.ConvNextImageProcessor; +var __webpack_exports__ConvNextModel = __webpack_exports__.ConvNextModel; +var __webpack_exports__ConvNextPreTrainedModel = __webpack_exports__.ConvNextPreTrainedModel; +var __webpack_exports__ConvNextV2ForImageClassification = __webpack_exports__.ConvNextV2ForImageClassification; +var __webpack_exports__ConvNextV2Model = __webpack_exports__.ConvNextV2Model; +var __webpack_exports__ConvNextV2PreTrainedModel = __webpack_exports__.ConvNextV2PreTrainedModel; +var __webpack_exports__DFineForObjectDetection = __webpack_exports__.DFineForObjectDetection; +var __webpack_exports__DFineModel = __webpack_exports__.DFineModel; +var __webpack_exports__DFinePreTrainedModel = __webpack_exports__.DFinePreTrainedModel; +var __webpack_exports__DPTFeatureExtractor = __webpack_exports__.DPTFeatureExtractor; +var __webpack_exports__DPTForDepthEstimation = __webpack_exports__.DPTForDepthEstimation; +var __webpack_exports__DPTImageProcessor = __webpack_exports__.DPTImageProcessor; +var __webpack_exports__DPTModel = __webpack_exports__.DPTModel; +var __webpack_exports__DPTPreTrainedModel = __webpack_exports__.DPTPreTrainedModel; +var __webpack_exports__DacDecoderModel = __webpack_exports__.DacDecoderModel; +var __webpack_exports__DacDecoderOutput = __webpack_exports__.DacDecoderOutput; +var __webpack_exports__DacEncoderModel = __webpack_exports__.DacEncoderModel; +var __webpack_exports__DacEncoderOutput = __webpack_exports__.DacEncoderOutput; +var __webpack_exports__DacFeatureExtractor = __webpack_exports__.DacFeatureExtractor; +var __webpack_exports__DacModel = __webpack_exports__.DacModel; +var __webpack_exports__DacPreTrainedModel = __webpack_exports__.DacPreTrainedModel; +var __webpack_exports__DataTypeMap = __webpack_exports__.DataTypeMap; +var __webpack_exports__DebertaForMaskedLM = __webpack_exports__.DebertaForMaskedLM; +var __webpack_exports__DebertaForQuestionAnswering = __webpack_exports__.DebertaForQuestionAnswering; +var __webpack_exports__DebertaForSequenceClassification = __webpack_exports__.DebertaForSequenceClassification; +var __webpack_exports__DebertaForTokenClassification = __webpack_exports__.DebertaForTokenClassification; +var __webpack_exports__DebertaModel = __webpack_exports__.DebertaModel; +var __webpack_exports__DebertaPreTrainedModel = __webpack_exports__.DebertaPreTrainedModel; +var __webpack_exports__DebertaTokenizer = __webpack_exports__.DebertaTokenizer; +var __webpack_exports__DebertaV2ForMaskedLM = __webpack_exports__.DebertaV2ForMaskedLM; +var __webpack_exports__DebertaV2ForQuestionAnswering = __webpack_exports__.DebertaV2ForQuestionAnswering; +var __webpack_exports__DebertaV2ForSequenceClassification = __webpack_exports__.DebertaV2ForSequenceClassification; +var __webpack_exports__DebertaV2ForTokenClassification = __webpack_exports__.DebertaV2ForTokenClassification; +var __webpack_exports__DebertaV2Model = __webpack_exports__.DebertaV2Model; +var __webpack_exports__DebertaV2PreTrainedModel = __webpack_exports__.DebertaV2PreTrainedModel; +var __webpack_exports__DebertaV2Tokenizer = __webpack_exports__.DebertaV2Tokenizer; +var __webpack_exports__DecisionTransformerModel = __webpack_exports__.DecisionTransformerModel; +var __webpack_exports__DecisionTransformerPreTrainedModel = __webpack_exports__.DecisionTransformerPreTrainedModel; +var __webpack_exports__DeiTFeatureExtractor = __webpack_exports__.DeiTFeatureExtractor; +var __webpack_exports__DeiTForImageClassification = __webpack_exports__.DeiTForImageClassification; +var __webpack_exports__DeiTImageProcessor = __webpack_exports__.DeiTImageProcessor; +var __webpack_exports__DeiTModel = __webpack_exports__.DeiTModel; +var __webpack_exports__DeiTPreTrainedModel = __webpack_exports__.DeiTPreTrainedModel; +var __webpack_exports__DepthAnythingForDepthEstimation = __webpack_exports__.DepthAnythingForDepthEstimation; +var __webpack_exports__DepthAnythingPreTrainedModel = __webpack_exports__.DepthAnythingPreTrainedModel; +var __webpack_exports__DepthEstimationPipeline = __webpack_exports__.DepthEstimationPipeline; +var __webpack_exports__DepthProForDepthEstimation = __webpack_exports__.DepthProForDepthEstimation; +var __webpack_exports__DepthProPreTrainedModel = __webpack_exports__.DepthProPreTrainedModel; +var __webpack_exports__DetrFeatureExtractor = __webpack_exports__.DetrFeatureExtractor; +var __webpack_exports__DetrForObjectDetection = __webpack_exports__.DetrForObjectDetection; +var __webpack_exports__DetrForSegmentation = __webpack_exports__.DetrForSegmentation; +var __webpack_exports__DetrImageProcessor = __webpack_exports__.DetrImageProcessor; +var __webpack_exports__DetrModel = __webpack_exports__.DetrModel; +var __webpack_exports__DetrObjectDetectionOutput = __webpack_exports__.DetrObjectDetectionOutput; +var __webpack_exports__DetrPreTrainedModel = __webpack_exports__.DetrPreTrainedModel; +var __webpack_exports__DetrSegmentationOutput = __webpack_exports__.DetrSegmentationOutput; +var __webpack_exports__Dinov2ForImageClassification = __webpack_exports__.Dinov2ForImageClassification; +var __webpack_exports__Dinov2Model = __webpack_exports__.Dinov2Model; +var __webpack_exports__Dinov2PreTrainedModel = __webpack_exports__.Dinov2PreTrainedModel; +var __webpack_exports__Dinov2WithRegistersForImageClassification = __webpack_exports__.Dinov2WithRegistersForImageClassification; +var __webpack_exports__Dinov2WithRegistersModel = __webpack_exports__.Dinov2WithRegistersModel; +var __webpack_exports__Dinov2WithRegistersPreTrainedModel = __webpack_exports__.Dinov2WithRegistersPreTrainedModel; +var __webpack_exports__DistilBertForMaskedLM = __webpack_exports__.DistilBertForMaskedLM; +var __webpack_exports__DistilBertForQuestionAnswering = __webpack_exports__.DistilBertForQuestionAnswering; +var __webpack_exports__DistilBertForSequenceClassification = __webpack_exports__.DistilBertForSequenceClassification; +var __webpack_exports__DistilBertForTokenClassification = __webpack_exports__.DistilBertForTokenClassification; +var __webpack_exports__DistilBertModel = __webpack_exports__.DistilBertModel; +var __webpack_exports__DistilBertPreTrainedModel = __webpack_exports__.DistilBertPreTrainedModel; +var __webpack_exports__DistilBertTokenizer = __webpack_exports__.DistilBertTokenizer; +var __webpack_exports__DocumentQuestionAnsweringPipeline = __webpack_exports__.DocumentQuestionAnsweringPipeline; +var __webpack_exports__DonutFeatureExtractor = __webpack_exports__.DonutFeatureExtractor; +var __webpack_exports__DonutImageProcessor = __webpack_exports__.DonutImageProcessor; +var __webpack_exports__DonutSwinModel = __webpack_exports__.DonutSwinModel; +var __webpack_exports__DonutSwinPreTrainedModel = __webpack_exports__.DonutSwinPreTrainedModel; +var __webpack_exports__EfficientNetForImageClassification = __webpack_exports__.EfficientNetForImageClassification; +var __webpack_exports__EfficientNetImageProcessor = __webpack_exports__.EfficientNetImageProcessor; +var __webpack_exports__EfficientNetModel = __webpack_exports__.EfficientNetModel; +var __webpack_exports__EfficientNetPreTrainedModel = __webpack_exports__.EfficientNetPreTrainedModel; +var __webpack_exports__ElectraForMaskedLM = __webpack_exports__.ElectraForMaskedLM; +var __webpack_exports__ElectraForQuestionAnswering = __webpack_exports__.ElectraForQuestionAnswering; +var __webpack_exports__ElectraForSequenceClassification = __webpack_exports__.ElectraForSequenceClassification; +var __webpack_exports__ElectraForTokenClassification = __webpack_exports__.ElectraForTokenClassification; +var __webpack_exports__ElectraModel = __webpack_exports__.ElectraModel; +var __webpack_exports__ElectraPreTrainedModel = __webpack_exports__.ElectraPreTrainedModel; +var __webpack_exports__ElectraTokenizer = __webpack_exports__.ElectraTokenizer; +var __webpack_exports__EncodecFeatureExtractor = __webpack_exports__.EncodecFeatureExtractor; +var __webpack_exports__EosTokenCriteria = __webpack_exports__.EosTokenCriteria; +var __webpack_exports__EsmForMaskedLM = __webpack_exports__.EsmForMaskedLM; +var __webpack_exports__EsmForSequenceClassification = __webpack_exports__.EsmForSequenceClassification; +var __webpack_exports__EsmForTokenClassification = __webpack_exports__.EsmForTokenClassification; +var __webpack_exports__EsmModel = __webpack_exports__.EsmModel; +var __webpack_exports__EsmPreTrainedModel = __webpack_exports__.EsmPreTrainedModel; +var __webpack_exports__EsmTokenizer = __webpack_exports__.EsmTokenizer; +var __webpack_exports__ExaoneForCausalLM = __webpack_exports__.ExaoneForCausalLM; +var __webpack_exports__ExaoneModel = __webpack_exports__.ExaoneModel; +var __webpack_exports__ExaonePreTrainedModel = __webpack_exports__.ExaonePreTrainedModel; +var __webpack_exports__FFT = __webpack_exports__.FFT; +var __webpack_exports__FalconForCausalLM = __webpack_exports__.FalconForCausalLM; +var __webpack_exports__FalconModel = __webpack_exports__.FalconModel; +var __webpack_exports__FalconPreTrainedModel = __webpack_exports__.FalconPreTrainedModel; +var __webpack_exports__FalconTokenizer = __webpack_exports__.FalconTokenizer; +var __webpack_exports__FastViTForImageClassification = __webpack_exports__.FastViTForImageClassification; +var __webpack_exports__FastViTModel = __webpack_exports__.FastViTModel; +var __webpack_exports__FastViTPreTrainedModel = __webpack_exports__.FastViTPreTrainedModel; +var __webpack_exports__FeatureExtractionPipeline = __webpack_exports__.FeatureExtractionPipeline; +var __webpack_exports__FeatureExtractor = __webpack_exports__.FeatureExtractor; +var __webpack_exports__FillMaskPipeline = __webpack_exports__.FillMaskPipeline; +var __webpack_exports__Florence2ForConditionalGeneration = __webpack_exports__.Florence2ForConditionalGeneration; +var __webpack_exports__Florence2PreTrainedModel = __webpack_exports__.Florence2PreTrainedModel; +var __webpack_exports__Florence2Processor = __webpack_exports__.Florence2Processor; +var __webpack_exports__ForcedBOSTokenLogitsProcessor = __webpack_exports__.ForcedBOSTokenLogitsProcessor; +var __webpack_exports__ForcedEOSTokenLogitsProcessor = __webpack_exports__.ForcedEOSTokenLogitsProcessor; +var __webpack_exports__GLPNFeatureExtractor = __webpack_exports__.GLPNFeatureExtractor; +var __webpack_exports__GLPNForDepthEstimation = __webpack_exports__.GLPNForDepthEstimation; +var __webpack_exports__GLPNModel = __webpack_exports__.GLPNModel; +var __webpack_exports__GLPNPreTrainedModel = __webpack_exports__.GLPNPreTrainedModel; +var __webpack_exports__GPT2LMHeadModel = __webpack_exports__.GPT2LMHeadModel; +var __webpack_exports__GPT2Model = __webpack_exports__.GPT2Model; +var __webpack_exports__GPT2PreTrainedModel = __webpack_exports__.GPT2PreTrainedModel; +var __webpack_exports__GPT2Tokenizer = __webpack_exports__.GPT2Tokenizer; +var __webpack_exports__GPTBigCodeForCausalLM = __webpack_exports__.GPTBigCodeForCausalLM; +var __webpack_exports__GPTBigCodeModel = __webpack_exports__.GPTBigCodeModel; +var __webpack_exports__GPTBigCodePreTrainedModel = __webpack_exports__.GPTBigCodePreTrainedModel; +var __webpack_exports__GPTJForCausalLM = __webpack_exports__.GPTJForCausalLM; +var __webpack_exports__GPTJModel = __webpack_exports__.GPTJModel; +var __webpack_exports__GPTJPreTrainedModel = __webpack_exports__.GPTJPreTrainedModel; +var __webpack_exports__GPTNeoForCausalLM = __webpack_exports__.GPTNeoForCausalLM; +var __webpack_exports__GPTNeoModel = __webpack_exports__.GPTNeoModel; +var __webpack_exports__GPTNeoPreTrainedModel = __webpack_exports__.GPTNeoPreTrainedModel; +var __webpack_exports__GPTNeoXForCausalLM = __webpack_exports__.GPTNeoXForCausalLM; +var __webpack_exports__GPTNeoXModel = __webpack_exports__.GPTNeoXModel; +var __webpack_exports__GPTNeoXPreTrainedModel = __webpack_exports__.GPTNeoXPreTrainedModel; +var __webpack_exports__GPTNeoXTokenizer = __webpack_exports__.GPTNeoXTokenizer; +var __webpack_exports__Gemma2ForCausalLM = __webpack_exports__.Gemma2ForCausalLM; +var __webpack_exports__Gemma2Model = __webpack_exports__.Gemma2Model; +var __webpack_exports__Gemma2PreTrainedModel = __webpack_exports__.Gemma2PreTrainedModel; +var __webpack_exports__Gemma3ForCausalLM = __webpack_exports__.Gemma3ForCausalLM; +var __webpack_exports__Gemma3Model = __webpack_exports__.Gemma3Model; +var __webpack_exports__Gemma3PreTrainedModel = __webpack_exports__.Gemma3PreTrainedModel; +var __webpack_exports__GemmaForCausalLM = __webpack_exports__.GemmaForCausalLM; +var __webpack_exports__GemmaModel = __webpack_exports__.GemmaModel; +var __webpack_exports__GemmaPreTrainedModel = __webpack_exports__.GemmaPreTrainedModel; +var __webpack_exports__GemmaTokenizer = __webpack_exports__.GemmaTokenizer; +var __webpack_exports__GlmForCausalLM = __webpack_exports__.GlmForCausalLM; +var __webpack_exports__GlmModel = __webpack_exports__.GlmModel; +var __webpack_exports__GlmPreTrainedModel = __webpack_exports__.GlmPreTrainedModel; +var __webpack_exports__GraniteForCausalLM = __webpack_exports__.GraniteForCausalLM; +var __webpack_exports__GraniteModel = __webpack_exports__.GraniteModel; +var __webpack_exports__GranitePreTrainedModel = __webpack_exports__.GranitePreTrainedModel; +var __webpack_exports__Grok1Tokenizer = __webpack_exports__.Grok1Tokenizer; +var __webpack_exports__GroundingDinoForObjectDetection = __webpack_exports__.GroundingDinoForObjectDetection; +var __webpack_exports__GroundingDinoImageProcessor = __webpack_exports__.GroundingDinoImageProcessor; +var __webpack_exports__GroundingDinoPreTrainedModel = __webpack_exports__.GroundingDinoPreTrainedModel; +var __webpack_exports__GroundingDinoProcessor = __webpack_exports__.GroundingDinoProcessor; +var __webpack_exports__GroupViTModel = __webpack_exports__.GroupViTModel; +var __webpack_exports__GroupViTPreTrainedModel = __webpack_exports__.GroupViTPreTrainedModel; +var __webpack_exports__HeliumForCausalLM = __webpack_exports__.HeliumForCausalLM; +var __webpack_exports__HeliumModel = __webpack_exports__.HeliumModel; +var __webpack_exports__HeliumPreTrainedModel = __webpack_exports__.HeliumPreTrainedModel; +var __webpack_exports__HerbertTokenizer = __webpack_exports__.HerbertTokenizer; +var __webpack_exports__HieraForImageClassification = __webpack_exports__.HieraForImageClassification; +var __webpack_exports__HieraModel = __webpack_exports__.HieraModel; +var __webpack_exports__HieraPreTrainedModel = __webpack_exports__.HieraPreTrainedModel; +var __webpack_exports__HubertForCTC = __webpack_exports__.HubertForCTC; +var __webpack_exports__HubertForSequenceClassification = __webpack_exports__.HubertForSequenceClassification; +var __webpack_exports__HubertModel = __webpack_exports__.HubertModel; +var __webpack_exports__HubertPreTrainedModel = __webpack_exports__.HubertPreTrainedModel; +var __webpack_exports__IJepaForImageClassification = __webpack_exports__.IJepaForImageClassification; +var __webpack_exports__IJepaModel = __webpack_exports__.IJepaModel; +var __webpack_exports__IJepaPreTrainedModel = __webpack_exports__.IJepaPreTrainedModel; +var __webpack_exports__Idefics3ForConditionalGeneration = __webpack_exports__.Idefics3ForConditionalGeneration; +var __webpack_exports__Idefics3ImageProcessor = __webpack_exports__.Idefics3ImageProcessor; +var __webpack_exports__Idefics3PreTrainedModel = __webpack_exports__.Idefics3PreTrainedModel; +var __webpack_exports__Idefics3Processor = __webpack_exports__.Idefics3Processor; +var __webpack_exports__ImageClassificationPipeline = __webpack_exports__.ImageClassificationPipeline; +var __webpack_exports__ImageFeatureExtractionPipeline = __webpack_exports__.ImageFeatureExtractionPipeline; +var __webpack_exports__ImageFeatureExtractor = __webpack_exports__.ImageFeatureExtractor; +var __webpack_exports__ImageMattingOutput = __webpack_exports__.ImageMattingOutput; +var __webpack_exports__ImageProcessor = __webpack_exports__.ImageProcessor; +var __webpack_exports__ImageSegmentationPipeline = __webpack_exports__.ImageSegmentationPipeline; +var __webpack_exports__ImageToImagePipeline = __webpack_exports__.ImageToImagePipeline; +var __webpack_exports__ImageToTextPipeline = __webpack_exports__.ImageToTextPipeline; +var __webpack_exports__InterruptableStoppingCriteria = __webpack_exports__.InterruptableStoppingCriteria; +var __webpack_exports__JAISLMHeadModel = __webpack_exports__.JAISLMHeadModel; +var __webpack_exports__JAISModel = __webpack_exports__.JAISModel; +var __webpack_exports__JAISPreTrainedModel = __webpack_exports__.JAISPreTrainedModel; +var __webpack_exports__JinaCLIPImageProcessor = __webpack_exports__.JinaCLIPImageProcessor; +var __webpack_exports__JinaCLIPModel = __webpack_exports__.JinaCLIPModel; +var __webpack_exports__JinaCLIPPreTrainedModel = __webpack_exports__.JinaCLIPPreTrainedModel; +var __webpack_exports__JinaCLIPProcessor = __webpack_exports__.JinaCLIPProcessor; +var __webpack_exports__JinaCLIPTextModel = __webpack_exports__.JinaCLIPTextModel; +var __webpack_exports__JinaCLIPVisionModel = __webpack_exports__.JinaCLIPVisionModel; +var __webpack_exports__LiteWhisperForConditionalGeneration = __webpack_exports__.LiteWhisperForConditionalGeneration; +var __webpack_exports__LlamaForCausalLM = __webpack_exports__.LlamaForCausalLM; +var __webpack_exports__LlamaModel = __webpack_exports__.LlamaModel; +var __webpack_exports__LlamaPreTrainedModel = __webpack_exports__.LlamaPreTrainedModel; +var __webpack_exports__LlamaTokenizer = __webpack_exports__.LlamaTokenizer; +var __webpack_exports__LlavaForConditionalGeneration = __webpack_exports__.LlavaForConditionalGeneration; +var __webpack_exports__LlavaOnevisionForConditionalGeneration = __webpack_exports__.LlavaOnevisionForConditionalGeneration; +var __webpack_exports__LlavaOnevisionImageProcessor = __webpack_exports__.LlavaOnevisionImageProcessor; +var __webpack_exports__LlavaPreTrainedModel = __webpack_exports__.LlavaPreTrainedModel; +var __webpack_exports__LogitsProcessor = __webpack_exports__.LogitsProcessor; +var __webpack_exports__LogitsProcessorList = __webpack_exports__.LogitsProcessorList; +var __webpack_exports__LogitsWarper = __webpack_exports__.LogitsWarper; +var __webpack_exports__LongT5ForConditionalGeneration = __webpack_exports__.LongT5ForConditionalGeneration; +var __webpack_exports__LongT5Model = __webpack_exports__.LongT5Model; +var __webpack_exports__LongT5PreTrainedModel = __webpack_exports__.LongT5PreTrainedModel; +var __webpack_exports__M2M100ForConditionalGeneration = __webpack_exports__.M2M100ForConditionalGeneration; +var __webpack_exports__M2M100Model = __webpack_exports__.M2M100Model; +var __webpack_exports__M2M100PreTrainedModel = __webpack_exports__.M2M100PreTrainedModel; +var __webpack_exports__M2M100Tokenizer = __webpack_exports__.M2M100Tokenizer; +var __webpack_exports__MBart50Tokenizer = __webpack_exports__.MBart50Tokenizer; +var __webpack_exports__MBartForCausalLM = __webpack_exports__.MBartForCausalLM; +var __webpack_exports__MBartForConditionalGeneration = __webpack_exports__.MBartForConditionalGeneration; +var __webpack_exports__MBartForSequenceClassification = __webpack_exports__.MBartForSequenceClassification; +var __webpack_exports__MBartModel = __webpack_exports__.MBartModel; +var __webpack_exports__MBartPreTrainedModel = __webpack_exports__.MBartPreTrainedModel; +var __webpack_exports__MBartTokenizer = __webpack_exports__.MBartTokenizer; +var __webpack_exports__MPNetForMaskedLM = __webpack_exports__.MPNetForMaskedLM; +var __webpack_exports__MPNetForQuestionAnswering = __webpack_exports__.MPNetForQuestionAnswering; +var __webpack_exports__MPNetForSequenceClassification = __webpack_exports__.MPNetForSequenceClassification; +var __webpack_exports__MPNetForTokenClassification = __webpack_exports__.MPNetForTokenClassification; +var __webpack_exports__MPNetModel = __webpack_exports__.MPNetModel; +var __webpack_exports__MPNetPreTrainedModel = __webpack_exports__.MPNetPreTrainedModel; +var __webpack_exports__MPNetTokenizer = __webpack_exports__.MPNetTokenizer; +var __webpack_exports__MT5ForConditionalGeneration = __webpack_exports__.MT5ForConditionalGeneration; +var __webpack_exports__MT5Model = __webpack_exports__.MT5Model; +var __webpack_exports__MT5PreTrainedModel = __webpack_exports__.MT5PreTrainedModel; +var __webpack_exports__MarianMTModel = __webpack_exports__.MarianMTModel; +var __webpack_exports__MarianModel = __webpack_exports__.MarianModel; +var __webpack_exports__MarianPreTrainedModel = __webpack_exports__.MarianPreTrainedModel; +var __webpack_exports__MarianTokenizer = __webpack_exports__.MarianTokenizer; +var __webpack_exports__Mask2FormerImageProcessor = __webpack_exports__.Mask2FormerImageProcessor; +var __webpack_exports__MaskFormerFeatureExtractor = __webpack_exports__.MaskFormerFeatureExtractor; +var __webpack_exports__MaskFormerForInstanceSegmentation = __webpack_exports__.MaskFormerForInstanceSegmentation; +var __webpack_exports__MaskFormerImageProcessor = __webpack_exports__.MaskFormerImageProcessor; +var __webpack_exports__MaskFormerModel = __webpack_exports__.MaskFormerModel; +var __webpack_exports__MaskFormerPreTrainedModel = __webpack_exports__.MaskFormerPreTrainedModel; +var __webpack_exports__MaskedLMOutput = __webpack_exports__.MaskedLMOutput; +var __webpack_exports__MaxLengthCriteria = __webpack_exports__.MaxLengthCriteria; +var __webpack_exports__Metric3DForDepthEstimation = __webpack_exports__.Metric3DForDepthEstimation; +var __webpack_exports__Metric3DPreTrainedModel = __webpack_exports__.Metric3DPreTrainedModel; +var __webpack_exports__Metric3Dv2ForDepthEstimation = __webpack_exports__.Metric3Dv2ForDepthEstimation; +var __webpack_exports__Metric3Dv2PreTrainedModel = __webpack_exports__.Metric3Dv2PreTrainedModel; +var __webpack_exports__MgpstrForSceneTextRecognition = __webpack_exports__.MgpstrForSceneTextRecognition; +var __webpack_exports__MgpstrModelOutput = __webpack_exports__.MgpstrModelOutput; +var __webpack_exports__MgpstrPreTrainedModel = __webpack_exports__.MgpstrPreTrainedModel; +var __webpack_exports__MgpstrProcessor = __webpack_exports__.MgpstrProcessor; +var __webpack_exports__MgpstrTokenizer = __webpack_exports__.MgpstrTokenizer; +var __webpack_exports__MimiDecoderModel = __webpack_exports__.MimiDecoderModel; +var __webpack_exports__MimiDecoderOutput = __webpack_exports__.MimiDecoderOutput; +var __webpack_exports__MimiEncoderModel = __webpack_exports__.MimiEncoderModel; +var __webpack_exports__MimiEncoderOutput = __webpack_exports__.MimiEncoderOutput; +var __webpack_exports__MimiModel = __webpack_exports__.MimiModel; +var __webpack_exports__MimiPreTrainedModel = __webpack_exports__.MimiPreTrainedModel; +var __webpack_exports__MinLengthLogitsProcessor = __webpack_exports__.MinLengthLogitsProcessor; +var __webpack_exports__MinNewTokensLengthLogitsProcessor = __webpack_exports__.MinNewTokensLengthLogitsProcessor; +var __webpack_exports__MistralForCausalLM = __webpack_exports__.MistralForCausalLM; +var __webpack_exports__MistralModel = __webpack_exports__.MistralModel; +var __webpack_exports__MistralPreTrainedModel = __webpack_exports__.MistralPreTrainedModel; +var __webpack_exports__MobileBertForMaskedLM = __webpack_exports__.MobileBertForMaskedLM; +var __webpack_exports__MobileBertForQuestionAnswering = __webpack_exports__.MobileBertForQuestionAnswering; +var __webpack_exports__MobileBertForSequenceClassification = __webpack_exports__.MobileBertForSequenceClassification; +var __webpack_exports__MobileBertModel = __webpack_exports__.MobileBertModel; +var __webpack_exports__MobileBertPreTrainedModel = __webpack_exports__.MobileBertPreTrainedModel; +var __webpack_exports__MobileBertTokenizer = __webpack_exports__.MobileBertTokenizer; +var __webpack_exports__MobileLLMForCausalLM = __webpack_exports__.MobileLLMForCausalLM; +var __webpack_exports__MobileLLMModel = __webpack_exports__.MobileLLMModel; +var __webpack_exports__MobileLLMPreTrainedModel = __webpack_exports__.MobileLLMPreTrainedModel; +var __webpack_exports__MobileNetV1FeatureExtractor = __webpack_exports__.MobileNetV1FeatureExtractor; +var __webpack_exports__MobileNetV1ForImageClassification = __webpack_exports__.MobileNetV1ForImageClassification; +var __webpack_exports__MobileNetV1ForSemanticSegmentation = __webpack_exports__.MobileNetV1ForSemanticSegmentation; +var __webpack_exports__MobileNetV1ImageProcessor = __webpack_exports__.MobileNetV1ImageProcessor; +var __webpack_exports__MobileNetV1Model = __webpack_exports__.MobileNetV1Model; +var __webpack_exports__MobileNetV1PreTrainedModel = __webpack_exports__.MobileNetV1PreTrainedModel; +var __webpack_exports__MobileNetV2FeatureExtractor = __webpack_exports__.MobileNetV2FeatureExtractor; +var __webpack_exports__MobileNetV2ForImageClassification = __webpack_exports__.MobileNetV2ForImageClassification; +var __webpack_exports__MobileNetV2ForSemanticSegmentation = __webpack_exports__.MobileNetV2ForSemanticSegmentation; +var __webpack_exports__MobileNetV2ImageProcessor = __webpack_exports__.MobileNetV2ImageProcessor; +var __webpack_exports__MobileNetV2Model = __webpack_exports__.MobileNetV2Model; +var __webpack_exports__MobileNetV2PreTrainedModel = __webpack_exports__.MobileNetV2PreTrainedModel; +var __webpack_exports__MobileNetV3FeatureExtractor = __webpack_exports__.MobileNetV3FeatureExtractor; +var __webpack_exports__MobileNetV3ForImageClassification = __webpack_exports__.MobileNetV3ForImageClassification; +var __webpack_exports__MobileNetV3ForSemanticSegmentation = __webpack_exports__.MobileNetV3ForSemanticSegmentation; +var __webpack_exports__MobileNetV3ImageProcessor = __webpack_exports__.MobileNetV3ImageProcessor; +var __webpack_exports__MobileNetV3Model = __webpack_exports__.MobileNetV3Model; +var __webpack_exports__MobileNetV3PreTrainedModel = __webpack_exports__.MobileNetV3PreTrainedModel; +var __webpack_exports__MobileNetV4FeatureExtractor = __webpack_exports__.MobileNetV4FeatureExtractor; +var __webpack_exports__MobileNetV4ForImageClassification = __webpack_exports__.MobileNetV4ForImageClassification; +var __webpack_exports__MobileNetV4ForSemanticSegmentation = __webpack_exports__.MobileNetV4ForSemanticSegmentation; +var __webpack_exports__MobileNetV4ImageProcessor = __webpack_exports__.MobileNetV4ImageProcessor; +var __webpack_exports__MobileNetV4Model = __webpack_exports__.MobileNetV4Model; +var __webpack_exports__MobileNetV4PreTrainedModel = __webpack_exports__.MobileNetV4PreTrainedModel; +var __webpack_exports__MobileViTFeatureExtractor = __webpack_exports__.MobileViTFeatureExtractor; +var __webpack_exports__MobileViTForImageClassification = __webpack_exports__.MobileViTForImageClassification; +var __webpack_exports__MobileViTImageProcessor = __webpack_exports__.MobileViTImageProcessor; +var __webpack_exports__MobileViTModel = __webpack_exports__.MobileViTModel; +var __webpack_exports__MobileViTPreTrainedModel = __webpack_exports__.MobileViTPreTrainedModel; +var __webpack_exports__MobileViTV2ForImageClassification = __webpack_exports__.MobileViTV2ForImageClassification; +var __webpack_exports__MobileViTV2Model = __webpack_exports__.MobileViTV2Model; +var __webpack_exports__MobileViTV2PreTrainedModel = __webpack_exports__.MobileViTV2PreTrainedModel; +var __webpack_exports__ModelOutput = __webpack_exports__.ModelOutput; +var __webpack_exports__ModernBertForMaskedLM = __webpack_exports__.ModernBertForMaskedLM; +var __webpack_exports__ModernBertForSequenceClassification = __webpack_exports__.ModernBertForSequenceClassification; +var __webpack_exports__ModernBertForTokenClassification = __webpack_exports__.ModernBertForTokenClassification; +var __webpack_exports__ModernBertModel = __webpack_exports__.ModernBertModel; +var __webpack_exports__ModernBertPreTrainedModel = __webpack_exports__.ModernBertPreTrainedModel; +var __webpack_exports__Moondream1ForConditionalGeneration = __webpack_exports__.Moondream1ForConditionalGeneration; +var __webpack_exports__MoonshineFeatureExtractor = __webpack_exports__.MoonshineFeatureExtractor; +var __webpack_exports__MoonshineForConditionalGeneration = __webpack_exports__.MoonshineForConditionalGeneration; +var __webpack_exports__MoonshineModel = __webpack_exports__.MoonshineModel; +var __webpack_exports__MoonshinePreTrainedModel = __webpack_exports__.MoonshinePreTrainedModel; +var __webpack_exports__MoonshineProcessor = __webpack_exports__.MoonshineProcessor; +var __webpack_exports__MptForCausalLM = __webpack_exports__.MptForCausalLM; +var __webpack_exports__MptModel = __webpack_exports__.MptModel; +var __webpack_exports__MptPreTrainedModel = __webpack_exports__.MptPreTrainedModel; +var __webpack_exports__MultiModalityCausalLM = __webpack_exports__.MultiModalityCausalLM; +var __webpack_exports__MultiModalityPreTrainedModel = __webpack_exports__.MultiModalityPreTrainedModel; +var __webpack_exports__MusicgenForCausalLM = __webpack_exports__.MusicgenForCausalLM; +var __webpack_exports__MusicgenForConditionalGeneration = __webpack_exports__.MusicgenForConditionalGeneration; +var __webpack_exports__MusicgenModel = __webpack_exports__.MusicgenModel; +var __webpack_exports__MusicgenPreTrainedModel = __webpack_exports__.MusicgenPreTrainedModel; +var __webpack_exports__NllbTokenizer = __webpack_exports__.NllbTokenizer; +var __webpack_exports__NoBadWordsLogitsProcessor = __webpack_exports__.NoBadWordsLogitsProcessor; +var __webpack_exports__NoRepeatNGramLogitsProcessor = __webpack_exports__.NoRepeatNGramLogitsProcessor; +var __webpack_exports__NomicBertModel = __webpack_exports__.NomicBertModel; +var __webpack_exports__NomicBertPreTrainedModel = __webpack_exports__.NomicBertPreTrainedModel; +var __webpack_exports__NougatImageProcessor = __webpack_exports__.NougatImageProcessor; +var __webpack_exports__NougatTokenizer = __webpack_exports__.NougatTokenizer; +var __webpack_exports__OPTForCausalLM = __webpack_exports__.OPTForCausalLM; +var __webpack_exports__OPTModel = __webpack_exports__.OPTModel; +var __webpack_exports__OPTPreTrainedModel = __webpack_exports__.OPTPreTrainedModel; +var __webpack_exports__ObjectDetectionPipeline = __webpack_exports__.ObjectDetectionPipeline; +var __webpack_exports__Olmo2ForCausalLM = __webpack_exports__.Olmo2ForCausalLM; +var __webpack_exports__Olmo2Model = __webpack_exports__.Olmo2Model; +var __webpack_exports__Olmo2PreTrainedModel = __webpack_exports__.Olmo2PreTrainedModel; +var __webpack_exports__OlmoForCausalLM = __webpack_exports__.OlmoForCausalLM; +var __webpack_exports__OlmoModel = __webpack_exports__.OlmoModel; +var __webpack_exports__OlmoPreTrainedModel = __webpack_exports__.OlmoPreTrainedModel; +var __webpack_exports__OpenELMForCausalLM = __webpack_exports__.OpenELMForCausalLM; +var __webpack_exports__OpenELMModel = __webpack_exports__.OpenELMModel; +var __webpack_exports__OpenELMPreTrainedModel = __webpack_exports__.OpenELMPreTrainedModel; +var __webpack_exports__OwlViTFeatureExtractor = __webpack_exports__.OwlViTFeatureExtractor; +var __webpack_exports__OwlViTForObjectDetection = __webpack_exports__.OwlViTForObjectDetection; +var __webpack_exports__OwlViTImageProcessor = __webpack_exports__.OwlViTImageProcessor; +var __webpack_exports__OwlViTModel = __webpack_exports__.OwlViTModel; +var __webpack_exports__OwlViTPreTrainedModel = __webpack_exports__.OwlViTPreTrainedModel; +var __webpack_exports__OwlViTProcessor = __webpack_exports__.OwlViTProcessor; +var __webpack_exports__Owlv2ForObjectDetection = __webpack_exports__.Owlv2ForObjectDetection; +var __webpack_exports__Owlv2ImageProcessor = __webpack_exports__.Owlv2ImageProcessor; +var __webpack_exports__Owlv2Model = __webpack_exports__.Owlv2Model; +var __webpack_exports__Owlv2PreTrainedModel = __webpack_exports__.Owlv2PreTrainedModel; +var __webpack_exports__PaliGemmaForConditionalGeneration = __webpack_exports__.PaliGemmaForConditionalGeneration; +var __webpack_exports__PaliGemmaPreTrainedModel = __webpack_exports__.PaliGemmaPreTrainedModel; +var __webpack_exports__PaliGemmaProcessor = __webpack_exports__.PaliGemmaProcessor; +var __webpack_exports__PatchTSMixerForPrediction = __webpack_exports__.PatchTSMixerForPrediction; +var __webpack_exports__PatchTSMixerModel = __webpack_exports__.PatchTSMixerModel; +var __webpack_exports__PatchTSMixerPreTrainedModel = __webpack_exports__.PatchTSMixerPreTrainedModel; +var __webpack_exports__PatchTSTForPrediction = __webpack_exports__.PatchTSTForPrediction; +var __webpack_exports__PatchTSTModel = __webpack_exports__.PatchTSTModel; +var __webpack_exports__PatchTSTPreTrainedModel = __webpack_exports__.PatchTSTPreTrainedModel; +var __webpack_exports__Phi3ForCausalLM = __webpack_exports__.Phi3ForCausalLM; +var __webpack_exports__Phi3Model = __webpack_exports__.Phi3Model; +var __webpack_exports__Phi3PreTrainedModel = __webpack_exports__.Phi3PreTrainedModel; +var __webpack_exports__Phi3VForCausalLM = __webpack_exports__.Phi3VForCausalLM; +var __webpack_exports__Phi3VImageProcessor = __webpack_exports__.Phi3VImageProcessor; +var __webpack_exports__Phi3VPreTrainedModel = __webpack_exports__.Phi3VPreTrainedModel; +var __webpack_exports__Phi3VProcessor = __webpack_exports__.Phi3VProcessor; +var __webpack_exports__PhiForCausalLM = __webpack_exports__.PhiForCausalLM; +var __webpack_exports__PhiModel = __webpack_exports__.PhiModel; +var __webpack_exports__PhiPreTrainedModel = __webpack_exports__.PhiPreTrainedModel; +var __webpack_exports__Pipeline = __webpack_exports__.Pipeline; +var __webpack_exports__PreTrainedModel = __webpack_exports__.PreTrainedModel; +var __webpack_exports__PreTrainedTokenizer = __webpack_exports__.PreTrainedTokenizer; +var __webpack_exports__PretrainedConfig = __webpack_exports__.PretrainedConfig; +var __webpack_exports__PretrainedMixin = __webpack_exports__.PretrainedMixin; +var __webpack_exports__Processor = __webpack_exports__.Processor; +var __webpack_exports__PvtForImageClassification = __webpack_exports__.PvtForImageClassification; +var __webpack_exports__PvtImageProcessor = __webpack_exports__.PvtImageProcessor; +var __webpack_exports__PvtModel = __webpack_exports__.PvtModel; +var __webpack_exports__PvtPreTrainedModel = __webpack_exports__.PvtPreTrainedModel; +var __webpack_exports__PyAnnoteFeatureExtractor = __webpack_exports__.PyAnnoteFeatureExtractor; +var __webpack_exports__PyAnnoteForAudioFrameClassification = __webpack_exports__.PyAnnoteForAudioFrameClassification; +var __webpack_exports__PyAnnoteModel = __webpack_exports__.PyAnnoteModel; +var __webpack_exports__PyAnnotePreTrainedModel = __webpack_exports__.PyAnnotePreTrainedModel; +var __webpack_exports__PyAnnoteProcessor = __webpack_exports__.PyAnnoteProcessor; +var __webpack_exports__QuestionAnsweringModelOutput = __webpack_exports__.QuestionAnsweringModelOutput; +var __webpack_exports__QuestionAnsweringPipeline = __webpack_exports__.QuestionAnsweringPipeline; +var __webpack_exports__Qwen2ForCausalLM = __webpack_exports__.Qwen2ForCausalLM; +var __webpack_exports__Qwen2Model = __webpack_exports__.Qwen2Model; +var __webpack_exports__Qwen2PreTrainedModel = __webpack_exports__.Qwen2PreTrainedModel; +var __webpack_exports__Qwen2Tokenizer = __webpack_exports__.Qwen2Tokenizer; +var __webpack_exports__Qwen2VLForConditionalGeneration = __webpack_exports__.Qwen2VLForConditionalGeneration; +var __webpack_exports__Qwen2VLImageProcessor = __webpack_exports__.Qwen2VLImageProcessor; +var __webpack_exports__Qwen2VLPreTrainedModel = __webpack_exports__.Qwen2VLPreTrainedModel; +var __webpack_exports__Qwen2VLProcessor = __webpack_exports__.Qwen2VLProcessor; +var __webpack_exports__Qwen3ForCausalLM = __webpack_exports__.Qwen3ForCausalLM; +var __webpack_exports__Qwen3Model = __webpack_exports__.Qwen3Model; +var __webpack_exports__Qwen3PreTrainedModel = __webpack_exports__.Qwen3PreTrainedModel; +var __webpack_exports__RFDetrForObjectDetection = __webpack_exports__.RFDetrForObjectDetection; +var __webpack_exports__RFDetrModel = __webpack_exports__.RFDetrModel; +var __webpack_exports__RFDetrObjectDetectionOutput = __webpack_exports__.RFDetrObjectDetectionOutput; +var __webpack_exports__RFDetrPreTrainedModel = __webpack_exports__.RFDetrPreTrainedModel; +var __webpack_exports__RTDetrForObjectDetection = __webpack_exports__.RTDetrForObjectDetection; +var __webpack_exports__RTDetrImageProcessor = __webpack_exports__.RTDetrImageProcessor; +var __webpack_exports__RTDetrModel = __webpack_exports__.RTDetrModel; +var __webpack_exports__RTDetrObjectDetectionOutput = __webpack_exports__.RTDetrObjectDetectionOutput; +var __webpack_exports__RTDetrPreTrainedModel = __webpack_exports__.RTDetrPreTrainedModel; +var __webpack_exports__RTDetrV2ForObjectDetection = __webpack_exports__.RTDetrV2ForObjectDetection; +var __webpack_exports__RTDetrV2Model = __webpack_exports__.RTDetrV2Model; +var __webpack_exports__RTDetrV2ObjectDetectionOutput = __webpack_exports__.RTDetrV2ObjectDetectionOutput; +var __webpack_exports__RTDetrV2PreTrainedModel = __webpack_exports__.RTDetrV2PreTrainedModel; +var __webpack_exports__RawAudio = __webpack_exports__.RawAudio; +var __webpack_exports__RawImage = __webpack_exports__.RawImage; +var __webpack_exports__RawVideo = __webpack_exports__.RawVideo; +var __webpack_exports__RawVideoFrame = __webpack_exports__.RawVideoFrame; +var __webpack_exports__RepetitionPenaltyLogitsProcessor = __webpack_exports__.RepetitionPenaltyLogitsProcessor; +var __webpack_exports__ResNetForImageClassification = __webpack_exports__.ResNetForImageClassification; +var __webpack_exports__ResNetModel = __webpack_exports__.ResNetModel; +var __webpack_exports__ResNetPreTrainedModel = __webpack_exports__.ResNetPreTrainedModel; +var __webpack_exports__RoFormerForMaskedLM = __webpack_exports__.RoFormerForMaskedLM; +var __webpack_exports__RoFormerForQuestionAnswering = __webpack_exports__.RoFormerForQuestionAnswering; +var __webpack_exports__RoFormerForSequenceClassification = __webpack_exports__.RoFormerForSequenceClassification; +var __webpack_exports__RoFormerForTokenClassification = __webpack_exports__.RoFormerForTokenClassification; +var __webpack_exports__RoFormerModel = __webpack_exports__.RoFormerModel; +var __webpack_exports__RoFormerPreTrainedModel = __webpack_exports__.RoFormerPreTrainedModel; +var __webpack_exports__RoFormerTokenizer = __webpack_exports__.RoFormerTokenizer; +var __webpack_exports__RobertaForMaskedLM = __webpack_exports__.RobertaForMaskedLM; +var __webpack_exports__RobertaForQuestionAnswering = __webpack_exports__.RobertaForQuestionAnswering; +var __webpack_exports__RobertaForSequenceClassification = __webpack_exports__.RobertaForSequenceClassification; +var __webpack_exports__RobertaForTokenClassification = __webpack_exports__.RobertaForTokenClassification; +var __webpack_exports__RobertaModel = __webpack_exports__.RobertaModel; +var __webpack_exports__RobertaPreTrainedModel = __webpack_exports__.RobertaPreTrainedModel; +var __webpack_exports__RobertaTokenizer = __webpack_exports__.RobertaTokenizer; +var __webpack_exports__SamImageProcessor = __webpack_exports__.SamImageProcessor; +var __webpack_exports__SamImageSegmentationOutput = __webpack_exports__.SamImageSegmentationOutput; +var __webpack_exports__SamModel = __webpack_exports__.SamModel; +var __webpack_exports__SamPreTrainedModel = __webpack_exports__.SamPreTrainedModel; +var __webpack_exports__SamProcessor = __webpack_exports__.SamProcessor; +var __webpack_exports__SapiensForDepthEstimation = __webpack_exports__.SapiensForDepthEstimation; +var __webpack_exports__SapiensForNormalEstimation = __webpack_exports__.SapiensForNormalEstimation; +var __webpack_exports__SapiensForSemanticSegmentation = __webpack_exports__.SapiensForSemanticSegmentation; +var __webpack_exports__SapiensPreTrainedModel = __webpack_exports__.SapiensPreTrainedModel; +var __webpack_exports__SeamlessM4TFeatureExtractor = __webpack_exports__.SeamlessM4TFeatureExtractor; +var __webpack_exports__SegformerFeatureExtractor = __webpack_exports__.SegformerFeatureExtractor; +var __webpack_exports__SegformerForImageClassification = __webpack_exports__.SegformerForImageClassification; +var __webpack_exports__SegformerForSemanticSegmentation = __webpack_exports__.SegformerForSemanticSegmentation; +var __webpack_exports__SegformerImageProcessor = __webpack_exports__.SegformerImageProcessor; +var __webpack_exports__SegformerModel = __webpack_exports__.SegformerModel; +var __webpack_exports__SegformerPreTrainedModel = __webpack_exports__.SegformerPreTrainedModel; +var __webpack_exports__Seq2SeqLMOutput = __webpack_exports__.Seq2SeqLMOutput; +var __webpack_exports__SequenceClassifierOutput = __webpack_exports__.SequenceClassifierOutput; +var __webpack_exports__SiglipImageProcessor = __webpack_exports__.SiglipImageProcessor; +var __webpack_exports__SiglipModel = __webpack_exports__.SiglipModel; +var __webpack_exports__SiglipPreTrainedModel = __webpack_exports__.SiglipPreTrainedModel; +var __webpack_exports__SiglipTextModel = __webpack_exports__.SiglipTextModel; +var __webpack_exports__SiglipTokenizer = __webpack_exports__.SiglipTokenizer; +var __webpack_exports__SiglipVisionModel = __webpack_exports__.SiglipVisionModel; +var __webpack_exports__SmolVLMForConditionalGeneration = __webpack_exports__.SmolVLMForConditionalGeneration; +var __webpack_exports__SmolVLMImageProcessor = __webpack_exports__.SmolVLMImageProcessor; +var __webpack_exports__SmolVLMProcessor = __webpack_exports__.SmolVLMProcessor; +var __webpack_exports__SnacDecoderModel = __webpack_exports__.SnacDecoderModel; +var __webpack_exports__SnacEncoderModel = __webpack_exports__.SnacEncoderModel; +var __webpack_exports__SnacFeatureExtractor = __webpack_exports__.SnacFeatureExtractor; +var __webpack_exports__SnacModel = __webpack_exports__.SnacModel; +var __webpack_exports__SnacPreTrainedModel = __webpack_exports__.SnacPreTrainedModel; +var __webpack_exports__SpeechT5FeatureExtractor = __webpack_exports__.SpeechT5FeatureExtractor; +var __webpack_exports__SpeechT5ForSpeechToText = __webpack_exports__.SpeechT5ForSpeechToText; +var __webpack_exports__SpeechT5ForTextToSpeech = __webpack_exports__.SpeechT5ForTextToSpeech; +var __webpack_exports__SpeechT5HifiGan = __webpack_exports__.SpeechT5HifiGan; +var __webpack_exports__SpeechT5Model = __webpack_exports__.SpeechT5Model; +var __webpack_exports__SpeechT5PreTrainedModel = __webpack_exports__.SpeechT5PreTrainedModel; +var __webpack_exports__SpeechT5Processor = __webpack_exports__.SpeechT5Processor; +var __webpack_exports__SpeechT5Tokenizer = __webpack_exports__.SpeechT5Tokenizer; +var __webpack_exports__SqueezeBertForMaskedLM = __webpack_exports__.SqueezeBertForMaskedLM; +var __webpack_exports__SqueezeBertForQuestionAnswering = __webpack_exports__.SqueezeBertForQuestionAnswering; +var __webpack_exports__SqueezeBertForSequenceClassification = __webpack_exports__.SqueezeBertForSequenceClassification; +var __webpack_exports__SqueezeBertModel = __webpack_exports__.SqueezeBertModel; +var __webpack_exports__SqueezeBertPreTrainedModel = __webpack_exports__.SqueezeBertPreTrainedModel; +var __webpack_exports__SqueezeBertTokenizer = __webpack_exports__.SqueezeBertTokenizer; +var __webpack_exports__StableLmForCausalLM = __webpack_exports__.StableLmForCausalLM; +var __webpack_exports__StableLmModel = __webpack_exports__.StableLmModel; +var __webpack_exports__StableLmPreTrainedModel = __webpack_exports__.StableLmPreTrainedModel; +var __webpack_exports__Starcoder2ForCausalLM = __webpack_exports__.Starcoder2ForCausalLM; +var __webpack_exports__Starcoder2Model = __webpack_exports__.Starcoder2Model; +var __webpack_exports__Starcoder2PreTrainedModel = __webpack_exports__.Starcoder2PreTrainedModel; +var __webpack_exports__StoppingCriteria = __webpack_exports__.StoppingCriteria; +var __webpack_exports__StoppingCriteriaList = __webpack_exports__.StoppingCriteriaList; +var __webpack_exports__StyleTextToSpeech2Model = __webpack_exports__.StyleTextToSpeech2Model; +var __webpack_exports__StyleTextToSpeech2PreTrainedModel = __webpack_exports__.StyleTextToSpeech2PreTrainedModel; +var __webpack_exports__SummarizationPipeline = __webpack_exports__.SummarizationPipeline; +var __webpack_exports__SuppressTokensAtBeginLogitsProcessor = __webpack_exports__.SuppressTokensAtBeginLogitsProcessor; +var __webpack_exports__Swin2SRForImageSuperResolution = __webpack_exports__.Swin2SRForImageSuperResolution; +var __webpack_exports__Swin2SRImageProcessor = __webpack_exports__.Swin2SRImageProcessor; +var __webpack_exports__Swin2SRModel = __webpack_exports__.Swin2SRModel; +var __webpack_exports__Swin2SRPreTrainedModel = __webpack_exports__.Swin2SRPreTrainedModel; +var __webpack_exports__SwinForImageClassification = __webpack_exports__.SwinForImageClassification; +var __webpack_exports__SwinForSemanticSegmentation = __webpack_exports__.SwinForSemanticSegmentation; +var __webpack_exports__SwinModel = __webpack_exports__.SwinModel; +var __webpack_exports__SwinPreTrainedModel = __webpack_exports__.SwinPreTrainedModel; +var __webpack_exports__T5ForConditionalGeneration = __webpack_exports__.T5ForConditionalGeneration; +var __webpack_exports__T5Model = __webpack_exports__.T5Model; +var __webpack_exports__T5PreTrainedModel = __webpack_exports__.T5PreTrainedModel; +var __webpack_exports__T5Tokenizer = __webpack_exports__.T5Tokenizer; +var __webpack_exports__TableTransformerForObjectDetection = __webpack_exports__.TableTransformerForObjectDetection; +var __webpack_exports__TableTransformerModel = __webpack_exports__.TableTransformerModel; +var __webpack_exports__TableTransformerObjectDetectionOutput = __webpack_exports__.TableTransformerObjectDetectionOutput; +var __webpack_exports__TableTransformerPreTrainedModel = __webpack_exports__.TableTransformerPreTrainedModel; +var __webpack_exports__TemperatureLogitsWarper = __webpack_exports__.TemperatureLogitsWarper; +var __webpack_exports__Tensor = __webpack_exports__.Tensor; +var __webpack_exports__Text2TextGenerationPipeline = __webpack_exports__.Text2TextGenerationPipeline; +var __webpack_exports__TextClassificationPipeline = __webpack_exports__.TextClassificationPipeline; +var __webpack_exports__TextGenerationPipeline = __webpack_exports__.TextGenerationPipeline; +var __webpack_exports__TextStreamer = __webpack_exports__.TextStreamer; +var __webpack_exports__TextToAudioPipeline = __webpack_exports__.TextToAudioPipeline; +var __webpack_exports__TokenClassificationPipeline = __webpack_exports__.TokenClassificationPipeline; +var __webpack_exports__TokenClassifierOutput = __webpack_exports__.TokenClassifierOutput; +var __webpack_exports__TokenizerModel = __webpack_exports__.TokenizerModel; +var __webpack_exports__TopKLogitsWarper = __webpack_exports__.TopKLogitsWarper; +var __webpack_exports__TopPLogitsWarper = __webpack_exports__.TopPLogitsWarper; +var __webpack_exports__TrOCRForCausalLM = __webpack_exports__.TrOCRForCausalLM; +var __webpack_exports__TrOCRPreTrainedModel = __webpack_exports__.TrOCRPreTrainedModel; +var __webpack_exports__TranslationPipeline = __webpack_exports__.TranslationPipeline; +var __webpack_exports__UltravoxModel = __webpack_exports__.UltravoxModel; +var __webpack_exports__UltravoxPreTrainedModel = __webpack_exports__.UltravoxPreTrainedModel; +var __webpack_exports__UltravoxProcessor = __webpack_exports__.UltravoxProcessor; +var __webpack_exports__UniSpeechForCTC = __webpack_exports__.UniSpeechForCTC; +var __webpack_exports__UniSpeechForSequenceClassification = __webpack_exports__.UniSpeechForSequenceClassification; +var __webpack_exports__UniSpeechModel = __webpack_exports__.UniSpeechModel; +var __webpack_exports__UniSpeechPreTrainedModel = __webpack_exports__.UniSpeechPreTrainedModel; +var __webpack_exports__UniSpeechSatForAudioFrameClassification = __webpack_exports__.UniSpeechSatForAudioFrameClassification; +var __webpack_exports__UniSpeechSatForCTC = __webpack_exports__.UniSpeechSatForCTC; +var __webpack_exports__UniSpeechSatForSequenceClassification = __webpack_exports__.UniSpeechSatForSequenceClassification; +var __webpack_exports__UniSpeechSatModel = __webpack_exports__.UniSpeechSatModel; +var __webpack_exports__UniSpeechSatPreTrainedModel = __webpack_exports__.UniSpeechSatPreTrainedModel; +var __webpack_exports__VLChatProcessor = __webpack_exports__.VLChatProcessor; +var __webpack_exports__VLMImageProcessor = __webpack_exports__.VLMImageProcessor; +var __webpack_exports__ViTFeatureExtractor = __webpack_exports__.ViTFeatureExtractor; +var __webpack_exports__ViTForImageClassification = __webpack_exports__.ViTForImageClassification; +var __webpack_exports__ViTImageProcessor = __webpack_exports__.ViTImageProcessor; +var __webpack_exports__ViTMAEModel = __webpack_exports__.ViTMAEModel; +var __webpack_exports__ViTMAEPreTrainedModel = __webpack_exports__.ViTMAEPreTrainedModel; +var __webpack_exports__ViTMSNForImageClassification = __webpack_exports__.ViTMSNForImageClassification; +var __webpack_exports__ViTMSNModel = __webpack_exports__.ViTMSNModel; +var __webpack_exports__ViTMSNPreTrainedModel = __webpack_exports__.ViTMSNPreTrainedModel; +var __webpack_exports__ViTModel = __webpack_exports__.ViTModel; +var __webpack_exports__ViTPreTrainedModel = __webpack_exports__.ViTPreTrainedModel; +var __webpack_exports__VisionEncoderDecoderModel = __webpack_exports__.VisionEncoderDecoderModel; +var __webpack_exports__VitMatteForImageMatting = __webpack_exports__.VitMatteForImageMatting; +var __webpack_exports__VitMatteImageProcessor = __webpack_exports__.VitMatteImageProcessor; +var __webpack_exports__VitMattePreTrainedModel = __webpack_exports__.VitMattePreTrainedModel; +var __webpack_exports__VitPoseForPoseEstimation = __webpack_exports__.VitPoseForPoseEstimation; +var __webpack_exports__VitPoseImageProcessor = __webpack_exports__.VitPoseImageProcessor; +var __webpack_exports__VitPosePreTrainedModel = __webpack_exports__.VitPosePreTrainedModel; +var __webpack_exports__VitsModel = __webpack_exports__.VitsModel; +var __webpack_exports__VitsModelOutput = __webpack_exports__.VitsModelOutput; +var __webpack_exports__VitsPreTrainedModel = __webpack_exports__.VitsPreTrainedModel; +var __webpack_exports__VitsTokenizer = __webpack_exports__.VitsTokenizer; +var __webpack_exports__Wav2Vec2BertForCTC = __webpack_exports__.Wav2Vec2BertForCTC; +var __webpack_exports__Wav2Vec2BertForSequenceClassification = __webpack_exports__.Wav2Vec2BertForSequenceClassification; +var __webpack_exports__Wav2Vec2BertModel = __webpack_exports__.Wav2Vec2BertModel; +var __webpack_exports__Wav2Vec2BertPreTrainedModel = __webpack_exports__.Wav2Vec2BertPreTrainedModel; +var __webpack_exports__Wav2Vec2CTCTokenizer = __webpack_exports__.Wav2Vec2CTCTokenizer; +var __webpack_exports__Wav2Vec2FeatureExtractor = __webpack_exports__.Wav2Vec2FeatureExtractor; +var __webpack_exports__Wav2Vec2ForAudioFrameClassification = __webpack_exports__.Wav2Vec2ForAudioFrameClassification; +var __webpack_exports__Wav2Vec2ForCTC = __webpack_exports__.Wav2Vec2ForCTC; +var __webpack_exports__Wav2Vec2ForSequenceClassification = __webpack_exports__.Wav2Vec2ForSequenceClassification; +var __webpack_exports__Wav2Vec2Model = __webpack_exports__.Wav2Vec2Model; +var __webpack_exports__Wav2Vec2PreTrainedModel = __webpack_exports__.Wav2Vec2PreTrainedModel; +var __webpack_exports__Wav2Vec2Processor = __webpack_exports__.Wav2Vec2Processor; +var __webpack_exports__Wav2Vec2ProcessorWithLM = __webpack_exports__.Wav2Vec2ProcessorWithLM; +var __webpack_exports__WavLMForAudioFrameClassification = __webpack_exports__.WavLMForAudioFrameClassification; +var __webpack_exports__WavLMForCTC = __webpack_exports__.WavLMForCTC; +var __webpack_exports__WavLMForSequenceClassification = __webpack_exports__.WavLMForSequenceClassification; +var __webpack_exports__WavLMForXVector = __webpack_exports__.WavLMForXVector; +var __webpack_exports__WavLMModel = __webpack_exports__.WavLMModel; +var __webpack_exports__WavLMPreTrainedModel = __webpack_exports__.WavLMPreTrainedModel; +var __webpack_exports__WeSpeakerFeatureExtractor = __webpack_exports__.WeSpeakerFeatureExtractor; +var __webpack_exports__WeSpeakerResNetModel = __webpack_exports__.WeSpeakerResNetModel; +var __webpack_exports__WeSpeakerResNetPreTrainedModel = __webpack_exports__.WeSpeakerResNetPreTrainedModel; +var __webpack_exports__WhisperFeatureExtractor = __webpack_exports__.WhisperFeatureExtractor; +var __webpack_exports__WhisperForConditionalGeneration = __webpack_exports__.WhisperForConditionalGeneration; +var __webpack_exports__WhisperModel = __webpack_exports__.WhisperModel; +var __webpack_exports__WhisperPreTrainedModel = __webpack_exports__.WhisperPreTrainedModel; +var __webpack_exports__WhisperProcessor = __webpack_exports__.WhisperProcessor; +var __webpack_exports__WhisperTextStreamer = __webpack_exports__.WhisperTextStreamer; +var __webpack_exports__WhisperTimeStampLogitsProcessor = __webpack_exports__.WhisperTimeStampLogitsProcessor; +var __webpack_exports__WhisperTokenizer = __webpack_exports__.WhisperTokenizer; +var __webpack_exports__XLMForQuestionAnswering = __webpack_exports__.XLMForQuestionAnswering; +var __webpack_exports__XLMForSequenceClassification = __webpack_exports__.XLMForSequenceClassification; +var __webpack_exports__XLMForTokenClassification = __webpack_exports__.XLMForTokenClassification; +var __webpack_exports__XLMModel = __webpack_exports__.XLMModel; +var __webpack_exports__XLMPreTrainedModel = __webpack_exports__.XLMPreTrainedModel; +var __webpack_exports__XLMRobertaForMaskedLM = __webpack_exports__.XLMRobertaForMaskedLM; +var __webpack_exports__XLMRobertaForQuestionAnswering = __webpack_exports__.XLMRobertaForQuestionAnswering; +var __webpack_exports__XLMRobertaForSequenceClassification = __webpack_exports__.XLMRobertaForSequenceClassification; +var __webpack_exports__XLMRobertaForTokenClassification = __webpack_exports__.XLMRobertaForTokenClassification; +var __webpack_exports__XLMRobertaModel = __webpack_exports__.XLMRobertaModel; +var __webpack_exports__XLMRobertaPreTrainedModel = __webpack_exports__.XLMRobertaPreTrainedModel; +var __webpack_exports__XLMRobertaTokenizer = __webpack_exports__.XLMRobertaTokenizer; +var __webpack_exports__XLMTokenizer = __webpack_exports__.XLMTokenizer; +var __webpack_exports__XLMWithLMHeadModel = __webpack_exports__.XLMWithLMHeadModel; +var __webpack_exports__XVectorOutput = __webpack_exports__.XVectorOutput; +var __webpack_exports__YolosFeatureExtractor = __webpack_exports__.YolosFeatureExtractor; +var __webpack_exports__YolosForObjectDetection = __webpack_exports__.YolosForObjectDetection; +var __webpack_exports__YolosImageProcessor = __webpack_exports__.YolosImageProcessor; +var __webpack_exports__YolosModel = __webpack_exports__.YolosModel; +var __webpack_exports__YolosObjectDetectionOutput = __webpack_exports__.YolosObjectDetectionOutput; +var __webpack_exports__YolosPreTrainedModel = __webpack_exports__.YolosPreTrainedModel; +var __webpack_exports__ZeroShotAudioClassificationPipeline = __webpack_exports__.ZeroShotAudioClassificationPipeline; +var __webpack_exports__ZeroShotClassificationPipeline = __webpack_exports__.ZeroShotClassificationPipeline; +var __webpack_exports__ZeroShotImageClassificationPipeline = __webpack_exports__.ZeroShotImageClassificationPipeline; +var __webpack_exports__ZeroShotObjectDetectionPipeline = __webpack_exports__.ZeroShotObjectDetectionPipeline; +var __webpack_exports__bankers_round = __webpack_exports__.bankers_round; +var __webpack_exports__cat = __webpack_exports__.cat; +var __webpack_exports__cos_sim = __webpack_exports__.cos_sim; +var __webpack_exports__dot = __webpack_exports__.dot; +var __webpack_exports__dynamic_time_warping = __webpack_exports__.dynamic_time_warping; +var __webpack_exports__env = __webpack_exports__.env; +var __webpack_exports__full = __webpack_exports__.full; +var __webpack_exports__full_like = __webpack_exports__.full_like; +var __webpack_exports__getKeyValueShapes = __webpack_exports__.getKeyValueShapes; +var __webpack_exports__hamming = __webpack_exports__.hamming; +var __webpack_exports__hanning = __webpack_exports__.hanning; +var __webpack_exports__interpolate = __webpack_exports__.interpolate; +var __webpack_exports__interpolate_4d = __webpack_exports__.interpolate_4d; +var __webpack_exports__interpolate_data = __webpack_exports__.interpolate_data; +var __webpack_exports__is_chinese_char = __webpack_exports__.is_chinese_char; +var __webpack_exports__layer_norm = __webpack_exports__.layer_norm; +var __webpack_exports__load_image = __webpack_exports__.load_image; +var __webpack_exports__load_video = __webpack_exports__.load_video; +var __webpack_exports__log_softmax = __webpack_exports__.log_softmax; +var __webpack_exports__magnitude = __webpack_exports__.magnitude; +var __webpack_exports__matmul = __webpack_exports__.matmul; +var __webpack_exports__max = __webpack_exports__.max; +var __webpack_exports__mean = __webpack_exports__.mean; +var __webpack_exports__mean_pooling = __webpack_exports__.mean_pooling; +var __webpack_exports__medianFilter = __webpack_exports__.medianFilter; +var __webpack_exports__mel_filter_bank = __webpack_exports__.mel_filter_bank; +var __webpack_exports__min = __webpack_exports__.min; +var __webpack_exports__ones = __webpack_exports__.ones; +var __webpack_exports__ones_like = __webpack_exports__.ones_like; +var __webpack_exports__permute = __webpack_exports__.permute; +var __webpack_exports__permute_data = __webpack_exports__.permute_data; +var __webpack_exports__pipeline = __webpack_exports__.pipeline; +var __webpack_exports__quantize_embeddings = __webpack_exports__.quantize_embeddings; +var __webpack_exports__rand = __webpack_exports__.rand; +var __webpack_exports__read_audio = __webpack_exports__.read_audio; +var __webpack_exports__rfft = __webpack_exports__.rfft; +var __webpack_exports__round = __webpack_exports__.round; +var __webpack_exports__slice = __webpack_exports__.slice; +var __webpack_exports__softmax = __webpack_exports__.softmax; +var __webpack_exports__spectrogram = __webpack_exports__.spectrogram; +var __webpack_exports__stack = __webpack_exports__.stack; +var __webpack_exports__std_mean = __webpack_exports__.std_mean; +var __webpack_exports__topk = __webpack_exports__.topk; +var __webpack_exports__window_function = __webpack_exports__.window_function; +var __webpack_exports__zeros = __webpack_exports__.zeros; +var __webpack_exports__zeros_like = __webpack_exports__.zeros_like; +export { __webpack_exports__ASTFeatureExtractor as ASTFeatureExtractor, __webpack_exports__ASTForAudioClassification as ASTForAudioClassification, __webpack_exports__ASTModel as ASTModel, __webpack_exports__ASTPreTrainedModel as ASTPreTrainedModel, __webpack_exports__AlbertForMaskedLM as AlbertForMaskedLM, __webpack_exports__AlbertForQuestionAnswering as AlbertForQuestionAnswering, __webpack_exports__AlbertForSequenceClassification as AlbertForSequenceClassification, __webpack_exports__AlbertModel as AlbertModel, __webpack_exports__AlbertPreTrainedModel as AlbertPreTrainedModel, __webpack_exports__AlbertTokenizer as AlbertTokenizer, __webpack_exports__AudioClassificationPipeline as AudioClassificationPipeline, __webpack_exports__AutoConfig as AutoConfig, __webpack_exports__AutoFeatureExtractor as AutoFeatureExtractor, __webpack_exports__AutoImageProcessor as AutoImageProcessor, __webpack_exports__AutoModel as AutoModel, __webpack_exports__AutoModelForAudioClassification as AutoModelForAudioClassification, __webpack_exports__AutoModelForAudioFrameClassification as AutoModelForAudioFrameClassification, __webpack_exports__AutoModelForAudioTextToText as AutoModelForAudioTextToText, __webpack_exports__AutoModelForCTC as AutoModelForCTC, __webpack_exports__AutoModelForCausalLM as AutoModelForCausalLM, __webpack_exports__AutoModelForDepthEstimation as AutoModelForDepthEstimation, __webpack_exports__AutoModelForDocumentQuestionAnswering as AutoModelForDocumentQuestionAnswering, __webpack_exports__AutoModelForImageClassification as AutoModelForImageClassification, __webpack_exports__AutoModelForImageFeatureExtraction as AutoModelForImageFeatureExtraction, __webpack_exports__AutoModelForImageMatting as AutoModelForImageMatting, __webpack_exports__AutoModelForImageSegmentation as AutoModelForImageSegmentation, __webpack_exports__AutoModelForImageTextToText as AutoModelForImageTextToText, __webpack_exports__AutoModelForImageToImage as AutoModelForImageToImage, __webpack_exports__AutoModelForMaskGeneration as AutoModelForMaskGeneration, __webpack_exports__AutoModelForMaskedLM as AutoModelForMaskedLM, __webpack_exports__AutoModelForNormalEstimation as AutoModelForNormalEstimation, __webpack_exports__AutoModelForObjectDetection as AutoModelForObjectDetection, __webpack_exports__AutoModelForPoseEstimation as AutoModelForPoseEstimation, __webpack_exports__AutoModelForQuestionAnswering as AutoModelForQuestionAnswering, __webpack_exports__AutoModelForSemanticSegmentation as AutoModelForSemanticSegmentation, __webpack_exports__AutoModelForSeq2SeqLM as AutoModelForSeq2SeqLM, __webpack_exports__AutoModelForSequenceClassification as AutoModelForSequenceClassification, __webpack_exports__AutoModelForSpeechSeq2Seq as AutoModelForSpeechSeq2Seq, __webpack_exports__AutoModelForTextToSpectrogram as AutoModelForTextToSpectrogram, __webpack_exports__AutoModelForTextToWaveform as AutoModelForTextToWaveform, __webpack_exports__AutoModelForTokenClassification as AutoModelForTokenClassification, __webpack_exports__AutoModelForUniversalSegmentation as AutoModelForUniversalSegmentation, __webpack_exports__AutoModelForVision2Seq as AutoModelForVision2Seq, __webpack_exports__AutoModelForXVector as AutoModelForXVector, __webpack_exports__AutoModelForZeroShotObjectDetection as AutoModelForZeroShotObjectDetection, __webpack_exports__AutoProcessor as AutoProcessor, __webpack_exports__AutoTokenizer as AutoTokenizer, __webpack_exports__AutomaticSpeechRecognitionPipeline as AutomaticSpeechRecognitionPipeline, __webpack_exports__BackgroundRemovalPipeline as BackgroundRemovalPipeline, __webpack_exports__BartForConditionalGeneration as BartForConditionalGeneration, __webpack_exports__BartForSequenceClassification as BartForSequenceClassification, __webpack_exports__BartModel as BartModel, __webpack_exports__BartPretrainedModel as BartPretrainedModel, __webpack_exports__BartTokenizer as BartTokenizer, __webpack_exports__BaseModelOutput as BaseModelOutput, __webpack_exports__BaseStreamer as BaseStreamer, __webpack_exports__BeitFeatureExtractor as BeitFeatureExtractor, __webpack_exports__BeitForImageClassification as BeitForImageClassification, __webpack_exports__BeitModel as BeitModel, __webpack_exports__BeitPreTrainedModel as BeitPreTrainedModel, __webpack_exports__BertForMaskedLM as BertForMaskedLM, __webpack_exports__BertForQuestionAnswering as BertForQuestionAnswering, __webpack_exports__BertForSequenceClassification as BertForSequenceClassification, __webpack_exports__BertForTokenClassification as BertForTokenClassification, __webpack_exports__BertModel as BertModel, __webpack_exports__BertPreTrainedModel as BertPreTrainedModel, __webpack_exports__BertTokenizer as BertTokenizer, __webpack_exports__BitImageProcessor as BitImageProcessor, __webpack_exports__BlenderbotForConditionalGeneration as BlenderbotForConditionalGeneration, __webpack_exports__BlenderbotModel as BlenderbotModel, __webpack_exports__BlenderbotPreTrainedModel as BlenderbotPreTrainedModel, __webpack_exports__BlenderbotSmallForConditionalGeneration as BlenderbotSmallForConditionalGeneration, __webpack_exports__BlenderbotSmallModel as BlenderbotSmallModel, __webpack_exports__BlenderbotSmallPreTrainedModel as BlenderbotSmallPreTrainedModel, __webpack_exports__BlenderbotSmallTokenizer as BlenderbotSmallTokenizer, __webpack_exports__BlenderbotTokenizer as BlenderbotTokenizer, __webpack_exports__BloomForCausalLM as BloomForCausalLM, __webpack_exports__BloomModel as BloomModel, __webpack_exports__BloomPreTrainedModel as BloomPreTrainedModel, __webpack_exports__BloomTokenizer as BloomTokenizer, __webpack_exports__CLIPFeatureExtractor as CLIPFeatureExtractor, __webpack_exports__CLIPImageProcessor as CLIPImageProcessor, __webpack_exports__CLIPModel as CLIPModel, __webpack_exports__CLIPPreTrainedModel as CLIPPreTrainedModel, __webpack_exports__CLIPSegForImageSegmentation as CLIPSegForImageSegmentation, __webpack_exports__CLIPSegModel as CLIPSegModel, __webpack_exports__CLIPSegPreTrainedModel as CLIPSegPreTrainedModel, __webpack_exports__CLIPTextModel as CLIPTextModel, __webpack_exports__CLIPTextModelWithProjection as CLIPTextModelWithProjection, __webpack_exports__CLIPTokenizer as CLIPTokenizer, __webpack_exports__CLIPVisionModel as CLIPVisionModel, __webpack_exports__CLIPVisionModelWithProjection as CLIPVisionModelWithProjection, __webpack_exports__CamembertForMaskedLM as CamembertForMaskedLM, __webpack_exports__CamembertForQuestionAnswering as CamembertForQuestionAnswering, __webpack_exports__CamembertForSequenceClassification as CamembertForSequenceClassification, __webpack_exports__CamembertForTokenClassification as CamembertForTokenClassification, __webpack_exports__CamembertModel as CamembertModel, __webpack_exports__CamembertPreTrainedModel as CamembertPreTrainedModel, __webpack_exports__CamembertTokenizer as CamembertTokenizer, __webpack_exports__CausalLMOutput as CausalLMOutput, __webpack_exports__CausalLMOutputWithPast as CausalLMOutputWithPast, __webpack_exports__ChineseCLIPFeatureExtractor as ChineseCLIPFeatureExtractor, __webpack_exports__ChineseCLIPModel as ChineseCLIPModel, __webpack_exports__ChineseCLIPPreTrainedModel as ChineseCLIPPreTrainedModel, __webpack_exports__ClapAudioModelWithProjection as ClapAudioModelWithProjection, __webpack_exports__ClapFeatureExtractor as ClapFeatureExtractor, __webpack_exports__ClapModel as ClapModel, __webpack_exports__ClapPreTrainedModel as ClapPreTrainedModel, __webpack_exports__ClapTextModelWithProjection as ClapTextModelWithProjection, __webpack_exports__ClassifierFreeGuidanceLogitsProcessor as ClassifierFreeGuidanceLogitsProcessor, __webpack_exports__CodeGenForCausalLM as CodeGenForCausalLM, __webpack_exports__CodeGenModel as CodeGenModel, __webpack_exports__CodeGenPreTrainedModel as CodeGenPreTrainedModel, __webpack_exports__CodeGenTokenizer as CodeGenTokenizer, __webpack_exports__CodeLlamaTokenizer as CodeLlamaTokenizer, __webpack_exports__CohereForCausalLM as CohereForCausalLM, __webpack_exports__CohereModel as CohereModel, __webpack_exports__CoherePreTrainedModel as CoherePreTrainedModel, __webpack_exports__CohereTokenizer as CohereTokenizer, __webpack_exports__ConvBertForMaskedLM as ConvBertForMaskedLM, __webpack_exports__ConvBertForQuestionAnswering as ConvBertForQuestionAnswering, __webpack_exports__ConvBertForSequenceClassification as ConvBertForSequenceClassification, __webpack_exports__ConvBertForTokenClassification as ConvBertForTokenClassification, __webpack_exports__ConvBertModel as ConvBertModel, __webpack_exports__ConvBertPreTrainedModel as ConvBertPreTrainedModel, __webpack_exports__ConvBertTokenizer as ConvBertTokenizer, __webpack_exports__ConvNextFeatureExtractor as ConvNextFeatureExtractor, __webpack_exports__ConvNextForImageClassification as ConvNextForImageClassification, __webpack_exports__ConvNextImageProcessor as ConvNextImageProcessor, __webpack_exports__ConvNextModel as ConvNextModel, __webpack_exports__ConvNextPreTrainedModel as ConvNextPreTrainedModel, __webpack_exports__ConvNextV2ForImageClassification as ConvNextV2ForImageClassification, __webpack_exports__ConvNextV2Model as ConvNextV2Model, __webpack_exports__ConvNextV2PreTrainedModel as ConvNextV2PreTrainedModel, __webpack_exports__DFineForObjectDetection as DFineForObjectDetection, __webpack_exports__DFineModel as DFineModel, __webpack_exports__DFinePreTrainedModel as DFinePreTrainedModel, __webpack_exports__DPTFeatureExtractor as DPTFeatureExtractor, __webpack_exports__DPTForDepthEstimation as DPTForDepthEstimation, __webpack_exports__DPTImageProcessor as DPTImageProcessor, __webpack_exports__DPTModel as DPTModel, __webpack_exports__DPTPreTrainedModel as DPTPreTrainedModel, __webpack_exports__DacDecoderModel as DacDecoderModel, __webpack_exports__DacDecoderOutput as DacDecoderOutput, __webpack_exports__DacEncoderModel as DacEncoderModel, __webpack_exports__DacEncoderOutput as DacEncoderOutput, __webpack_exports__DacFeatureExtractor as DacFeatureExtractor, __webpack_exports__DacModel as DacModel, __webpack_exports__DacPreTrainedModel as DacPreTrainedModel, __webpack_exports__DataTypeMap as DataTypeMap, __webpack_exports__DebertaForMaskedLM as DebertaForMaskedLM, __webpack_exports__DebertaForQuestionAnswering as DebertaForQuestionAnswering, __webpack_exports__DebertaForSequenceClassification as DebertaForSequenceClassification, __webpack_exports__DebertaForTokenClassification as DebertaForTokenClassification, __webpack_exports__DebertaModel as DebertaModel, __webpack_exports__DebertaPreTrainedModel as DebertaPreTrainedModel, __webpack_exports__DebertaTokenizer as DebertaTokenizer, __webpack_exports__DebertaV2ForMaskedLM as DebertaV2ForMaskedLM, __webpack_exports__DebertaV2ForQuestionAnswering as DebertaV2ForQuestionAnswering, __webpack_exports__DebertaV2ForSequenceClassification as DebertaV2ForSequenceClassification, __webpack_exports__DebertaV2ForTokenClassification as DebertaV2ForTokenClassification, __webpack_exports__DebertaV2Model as DebertaV2Model, __webpack_exports__DebertaV2PreTrainedModel as DebertaV2PreTrainedModel, __webpack_exports__DebertaV2Tokenizer as DebertaV2Tokenizer, __webpack_exports__DecisionTransformerModel as DecisionTransformerModel, __webpack_exports__DecisionTransformerPreTrainedModel as DecisionTransformerPreTrainedModel, __webpack_exports__DeiTFeatureExtractor as DeiTFeatureExtractor, __webpack_exports__DeiTForImageClassification as DeiTForImageClassification, __webpack_exports__DeiTImageProcessor as DeiTImageProcessor, __webpack_exports__DeiTModel as DeiTModel, __webpack_exports__DeiTPreTrainedModel as DeiTPreTrainedModel, __webpack_exports__DepthAnythingForDepthEstimation as DepthAnythingForDepthEstimation, __webpack_exports__DepthAnythingPreTrainedModel as DepthAnythingPreTrainedModel, __webpack_exports__DepthEstimationPipeline as DepthEstimationPipeline, __webpack_exports__DepthProForDepthEstimation as DepthProForDepthEstimation, __webpack_exports__DepthProPreTrainedModel as DepthProPreTrainedModel, __webpack_exports__DetrFeatureExtractor as DetrFeatureExtractor, __webpack_exports__DetrForObjectDetection as DetrForObjectDetection, __webpack_exports__DetrForSegmentation as DetrForSegmentation, __webpack_exports__DetrImageProcessor as DetrImageProcessor, __webpack_exports__DetrModel as DetrModel, __webpack_exports__DetrObjectDetectionOutput as DetrObjectDetectionOutput, __webpack_exports__DetrPreTrainedModel as DetrPreTrainedModel, __webpack_exports__DetrSegmentationOutput as DetrSegmentationOutput, __webpack_exports__Dinov2ForImageClassification as Dinov2ForImageClassification, __webpack_exports__Dinov2Model as Dinov2Model, __webpack_exports__Dinov2PreTrainedModel as Dinov2PreTrainedModel, __webpack_exports__Dinov2WithRegistersForImageClassification as Dinov2WithRegistersForImageClassification, __webpack_exports__Dinov2WithRegistersModel as Dinov2WithRegistersModel, __webpack_exports__Dinov2WithRegistersPreTrainedModel as Dinov2WithRegistersPreTrainedModel, __webpack_exports__DistilBertForMaskedLM as DistilBertForMaskedLM, __webpack_exports__DistilBertForQuestionAnswering as DistilBertForQuestionAnswering, __webpack_exports__DistilBertForSequenceClassification as DistilBertForSequenceClassification, __webpack_exports__DistilBertForTokenClassification as DistilBertForTokenClassification, __webpack_exports__DistilBertModel as DistilBertModel, __webpack_exports__DistilBertPreTrainedModel as DistilBertPreTrainedModel, __webpack_exports__DistilBertTokenizer as DistilBertTokenizer, __webpack_exports__DocumentQuestionAnsweringPipeline as DocumentQuestionAnsweringPipeline, __webpack_exports__DonutFeatureExtractor as DonutFeatureExtractor, __webpack_exports__DonutImageProcessor as DonutImageProcessor, __webpack_exports__DonutSwinModel as DonutSwinModel, __webpack_exports__DonutSwinPreTrainedModel as DonutSwinPreTrainedModel, __webpack_exports__EfficientNetForImageClassification as EfficientNetForImageClassification, __webpack_exports__EfficientNetImageProcessor as EfficientNetImageProcessor, __webpack_exports__EfficientNetModel as EfficientNetModel, __webpack_exports__EfficientNetPreTrainedModel as EfficientNetPreTrainedModel, __webpack_exports__ElectraForMaskedLM as ElectraForMaskedLM, __webpack_exports__ElectraForQuestionAnswering as ElectraForQuestionAnswering, __webpack_exports__ElectraForSequenceClassification as ElectraForSequenceClassification, __webpack_exports__ElectraForTokenClassification as ElectraForTokenClassification, __webpack_exports__ElectraModel as ElectraModel, __webpack_exports__ElectraPreTrainedModel as ElectraPreTrainedModel, __webpack_exports__ElectraTokenizer as ElectraTokenizer, __webpack_exports__EncodecFeatureExtractor as EncodecFeatureExtractor, __webpack_exports__EosTokenCriteria as EosTokenCriteria, __webpack_exports__EsmForMaskedLM as EsmForMaskedLM, __webpack_exports__EsmForSequenceClassification as EsmForSequenceClassification, __webpack_exports__EsmForTokenClassification as EsmForTokenClassification, __webpack_exports__EsmModel as EsmModel, __webpack_exports__EsmPreTrainedModel as EsmPreTrainedModel, __webpack_exports__EsmTokenizer as EsmTokenizer, __webpack_exports__ExaoneForCausalLM as ExaoneForCausalLM, __webpack_exports__ExaoneModel as ExaoneModel, __webpack_exports__ExaonePreTrainedModel as ExaonePreTrainedModel, __webpack_exports__FFT as FFT, __webpack_exports__FalconForCausalLM as FalconForCausalLM, __webpack_exports__FalconModel as FalconModel, __webpack_exports__FalconPreTrainedModel as FalconPreTrainedModel, __webpack_exports__FalconTokenizer as FalconTokenizer, __webpack_exports__FastViTForImageClassification as FastViTForImageClassification, __webpack_exports__FastViTModel as FastViTModel, __webpack_exports__FastViTPreTrainedModel as FastViTPreTrainedModel, __webpack_exports__FeatureExtractionPipeline as FeatureExtractionPipeline, __webpack_exports__FeatureExtractor as FeatureExtractor, __webpack_exports__FillMaskPipeline as FillMaskPipeline, __webpack_exports__Florence2ForConditionalGeneration as Florence2ForConditionalGeneration, __webpack_exports__Florence2PreTrainedModel as Florence2PreTrainedModel, __webpack_exports__Florence2Processor as Florence2Processor, __webpack_exports__ForcedBOSTokenLogitsProcessor as ForcedBOSTokenLogitsProcessor, __webpack_exports__ForcedEOSTokenLogitsProcessor as ForcedEOSTokenLogitsProcessor, __webpack_exports__GLPNFeatureExtractor as GLPNFeatureExtractor, __webpack_exports__GLPNForDepthEstimation as GLPNForDepthEstimation, __webpack_exports__GLPNModel as GLPNModel, __webpack_exports__GLPNPreTrainedModel as GLPNPreTrainedModel, __webpack_exports__GPT2LMHeadModel as GPT2LMHeadModel, __webpack_exports__GPT2Model as GPT2Model, __webpack_exports__GPT2PreTrainedModel as GPT2PreTrainedModel, __webpack_exports__GPT2Tokenizer as GPT2Tokenizer, __webpack_exports__GPTBigCodeForCausalLM as GPTBigCodeForCausalLM, __webpack_exports__GPTBigCodeModel as GPTBigCodeModel, __webpack_exports__GPTBigCodePreTrainedModel as GPTBigCodePreTrainedModel, __webpack_exports__GPTJForCausalLM as GPTJForCausalLM, __webpack_exports__GPTJModel as GPTJModel, __webpack_exports__GPTJPreTrainedModel as GPTJPreTrainedModel, __webpack_exports__GPTNeoForCausalLM as GPTNeoForCausalLM, __webpack_exports__GPTNeoModel as GPTNeoModel, __webpack_exports__GPTNeoPreTrainedModel as GPTNeoPreTrainedModel, __webpack_exports__GPTNeoXForCausalLM as GPTNeoXForCausalLM, __webpack_exports__GPTNeoXModel as GPTNeoXModel, __webpack_exports__GPTNeoXPreTrainedModel as GPTNeoXPreTrainedModel, __webpack_exports__GPTNeoXTokenizer as GPTNeoXTokenizer, __webpack_exports__Gemma2ForCausalLM as Gemma2ForCausalLM, __webpack_exports__Gemma2Model as Gemma2Model, __webpack_exports__Gemma2PreTrainedModel as Gemma2PreTrainedModel, __webpack_exports__Gemma3ForCausalLM as Gemma3ForCausalLM, __webpack_exports__Gemma3Model as Gemma3Model, __webpack_exports__Gemma3PreTrainedModel as Gemma3PreTrainedModel, __webpack_exports__GemmaForCausalLM as GemmaForCausalLM, __webpack_exports__GemmaModel as GemmaModel, __webpack_exports__GemmaPreTrainedModel as GemmaPreTrainedModel, __webpack_exports__GemmaTokenizer as GemmaTokenizer, __webpack_exports__GlmForCausalLM as GlmForCausalLM, __webpack_exports__GlmModel as GlmModel, __webpack_exports__GlmPreTrainedModel as GlmPreTrainedModel, __webpack_exports__GraniteForCausalLM as GraniteForCausalLM, __webpack_exports__GraniteModel as GraniteModel, __webpack_exports__GranitePreTrainedModel as GranitePreTrainedModel, __webpack_exports__Grok1Tokenizer as Grok1Tokenizer, __webpack_exports__GroundingDinoForObjectDetection as GroundingDinoForObjectDetection, __webpack_exports__GroundingDinoImageProcessor as GroundingDinoImageProcessor, __webpack_exports__GroundingDinoPreTrainedModel as GroundingDinoPreTrainedModel, __webpack_exports__GroundingDinoProcessor as GroundingDinoProcessor, __webpack_exports__GroupViTModel as GroupViTModel, __webpack_exports__GroupViTPreTrainedModel as GroupViTPreTrainedModel, __webpack_exports__HeliumForCausalLM as HeliumForCausalLM, __webpack_exports__HeliumModel as HeliumModel, __webpack_exports__HeliumPreTrainedModel as HeliumPreTrainedModel, __webpack_exports__HerbertTokenizer as HerbertTokenizer, __webpack_exports__HieraForImageClassification as HieraForImageClassification, __webpack_exports__HieraModel as HieraModel, __webpack_exports__HieraPreTrainedModel as HieraPreTrainedModel, __webpack_exports__HubertForCTC as HubertForCTC, __webpack_exports__HubertForSequenceClassification as HubertForSequenceClassification, __webpack_exports__HubertModel as HubertModel, __webpack_exports__HubertPreTrainedModel as HubertPreTrainedModel, __webpack_exports__IJepaForImageClassification as IJepaForImageClassification, __webpack_exports__IJepaModel as IJepaModel, __webpack_exports__IJepaPreTrainedModel as IJepaPreTrainedModel, __webpack_exports__Idefics3ForConditionalGeneration as Idefics3ForConditionalGeneration, __webpack_exports__Idefics3ImageProcessor as Idefics3ImageProcessor, __webpack_exports__Idefics3PreTrainedModel as Idefics3PreTrainedModel, __webpack_exports__Idefics3Processor as Idefics3Processor, __webpack_exports__ImageClassificationPipeline as ImageClassificationPipeline, __webpack_exports__ImageFeatureExtractionPipeline as ImageFeatureExtractionPipeline, __webpack_exports__ImageFeatureExtractor as ImageFeatureExtractor, __webpack_exports__ImageMattingOutput as ImageMattingOutput, __webpack_exports__ImageProcessor as ImageProcessor, __webpack_exports__ImageSegmentationPipeline as ImageSegmentationPipeline, __webpack_exports__ImageToImagePipeline as ImageToImagePipeline, __webpack_exports__ImageToTextPipeline as ImageToTextPipeline, __webpack_exports__InterruptableStoppingCriteria as InterruptableStoppingCriteria, __webpack_exports__JAISLMHeadModel as JAISLMHeadModel, __webpack_exports__JAISModel as JAISModel, __webpack_exports__JAISPreTrainedModel as JAISPreTrainedModel, __webpack_exports__JinaCLIPImageProcessor as JinaCLIPImageProcessor, __webpack_exports__JinaCLIPModel as JinaCLIPModel, __webpack_exports__JinaCLIPPreTrainedModel as JinaCLIPPreTrainedModel, __webpack_exports__JinaCLIPProcessor as JinaCLIPProcessor, __webpack_exports__JinaCLIPTextModel as JinaCLIPTextModel, __webpack_exports__JinaCLIPVisionModel as JinaCLIPVisionModel, __webpack_exports__LiteWhisperForConditionalGeneration as LiteWhisperForConditionalGeneration, __webpack_exports__LlamaForCausalLM as LlamaForCausalLM, __webpack_exports__LlamaModel as LlamaModel, __webpack_exports__LlamaPreTrainedModel as LlamaPreTrainedModel, __webpack_exports__LlamaTokenizer as LlamaTokenizer, __webpack_exports__LlavaForConditionalGeneration as LlavaForConditionalGeneration, __webpack_exports__LlavaOnevisionForConditionalGeneration as LlavaOnevisionForConditionalGeneration, __webpack_exports__LlavaOnevisionImageProcessor as LlavaOnevisionImageProcessor, __webpack_exports__LlavaPreTrainedModel as LlavaPreTrainedModel, __webpack_exports__LogitsProcessor as LogitsProcessor, __webpack_exports__LogitsProcessorList as LogitsProcessorList, __webpack_exports__LogitsWarper as LogitsWarper, __webpack_exports__LongT5ForConditionalGeneration as LongT5ForConditionalGeneration, __webpack_exports__LongT5Model as LongT5Model, __webpack_exports__LongT5PreTrainedModel as LongT5PreTrainedModel, __webpack_exports__M2M100ForConditionalGeneration as M2M100ForConditionalGeneration, __webpack_exports__M2M100Model as M2M100Model, __webpack_exports__M2M100PreTrainedModel as M2M100PreTrainedModel, __webpack_exports__M2M100Tokenizer as M2M100Tokenizer, __webpack_exports__MBart50Tokenizer as MBart50Tokenizer, __webpack_exports__MBartForCausalLM as MBartForCausalLM, __webpack_exports__MBartForConditionalGeneration as MBartForConditionalGeneration, __webpack_exports__MBartForSequenceClassification as MBartForSequenceClassification, __webpack_exports__MBartModel as MBartModel, __webpack_exports__MBartPreTrainedModel as MBartPreTrainedModel, __webpack_exports__MBartTokenizer as MBartTokenizer, __webpack_exports__MPNetForMaskedLM as MPNetForMaskedLM, __webpack_exports__MPNetForQuestionAnswering as MPNetForQuestionAnswering, __webpack_exports__MPNetForSequenceClassification as MPNetForSequenceClassification, __webpack_exports__MPNetForTokenClassification as MPNetForTokenClassification, __webpack_exports__MPNetModel as MPNetModel, __webpack_exports__MPNetPreTrainedModel as MPNetPreTrainedModel, __webpack_exports__MPNetTokenizer as MPNetTokenizer, __webpack_exports__MT5ForConditionalGeneration as MT5ForConditionalGeneration, __webpack_exports__MT5Model as MT5Model, __webpack_exports__MT5PreTrainedModel as MT5PreTrainedModel, __webpack_exports__MarianMTModel as MarianMTModel, __webpack_exports__MarianModel as MarianModel, __webpack_exports__MarianPreTrainedModel as MarianPreTrainedModel, __webpack_exports__MarianTokenizer as MarianTokenizer, __webpack_exports__Mask2FormerImageProcessor as Mask2FormerImageProcessor, __webpack_exports__MaskFormerFeatureExtractor as MaskFormerFeatureExtractor, __webpack_exports__MaskFormerForInstanceSegmentation as MaskFormerForInstanceSegmentation, __webpack_exports__MaskFormerImageProcessor as MaskFormerImageProcessor, __webpack_exports__MaskFormerModel as MaskFormerModel, __webpack_exports__MaskFormerPreTrainedModel as MaskFormerPreTrainedModel, __webpack_exports__MaskedLMOutput as MaskedLMOutput, __webpack_exports__MaxLengthCriteria as MaxLengthCriteria, __webpack_exports__Metric3DForDepthEstimation as Metric3DForDepthEstimation, __webpack_exports__Metric3DPreTrainedModel as Metric3DPreTrainedModel, __webpack_exports__Metric3Dv2ForDepthEstimation as Metric3Dv2ForDepthEstimation, __webpack_exports__Metric3Dv2PreTrainedModel as Metric3Dv2PreTrainedModel, __webpack_exports__MgpstrForSceneTextRecognition as MgpstrForSceneTextRecognition, __webpack_exports__MgpstrModelOutput as MgpstrModelOutput, __webpack_exports__MgpstrPreTrainedModel as MgpstrPreTrainedModel, __webpack_exports__MgpstrProcessor as MgpstrProcessor, __webpack_exports__MgpstrTokenizer as MgpstrTokenizer, __webpack_exports__MimiDecoderModel as MimiDecoderModel, __webpack_exports__MimiDecoderOutput as MimiDecoderOutput, __webpack_exports__MimiEncoderModel as MimiEncoderModel, __webpack_exports__MimiEncoderOutput as MimiEncoderOutput, __webpack_exports__MimiModel as MimiModel, __webpack_exports__MimiPreTrainedModel as MimiPreTrainedModel, __webpack_exports__MinLengthLogitsProcessor as MinLengthLogitsProcessor, __webpack_exports__MinNewTokensLengthLogitsProcessor as MinNewTokensLengthLogitsProcessor, __webpack_exports__MistralForCausalLM as MistralForCausalLM, __webpack_exports__MistralModel as MistralModel, __webpack_exports__MistralPreTrainedModel as MistralPreTrainedModel, __webpack_exports__MobileBertForMaskedLM as MobileBertForMaskedLM, __webpack_exports__MobileBertForQuestionAnswering as MobileBertForQuestionAnswering, __webpack_exports__MobileBertForSequenceClassification as MobileBertForSequenceClassification, __webpack_exports__MobileBertModel as MobileBertModel, __webpack_exports__MobileBertPreTrainedModel as MobileBertPreTrainedModel, __webpack_exports__MobileBertTokenizer as MobileBertTokenizer, __webpack_exports__MobileLLMForCausalLM as MobileLLMForCausalLM, __webpack_exports__MobileLLMModel as MobileLLMModel, __webpack_exports__MobileLLMPreTrainedModel as MobileLLMPreTrainedModel, __webpack_exports__MobileNetV1FeatureExtractor as MobileNetV1FeatureExtractor, __webpack_exports__MobileNetV1ForImageClassification as MobileNetV1ForImageClassification, __webpack_exports__MobileNetV1ForSemanticSegmentation as MobileNetV1ForSemanticSegmentation, __webpack_exports__MobileNetV1ImageProcessor as MobileNetV1ImageProcessor, __webpack_exports__MobileNetV1Model as MobileNetV1Model, __webpack_exports__MobileNetV1PreTrainedModel as MobileNetV1PreTrainedModel, __webpack_exports__MobileNetV2FeatureExtractor as MobileNetV2FeatureExtractor, __webpack_exports__MobileNetV2ForImageClassification as MobileNetV2ForImageClassification, __webpack_exports__MobileNetV2ForSemanticSegmentation as MobileNetV2ForSemanticSegmentation, __webpack_exports__MobileNetV2ImageProcessor as MobileNetV2ImageProcessor, __webpack_exports__MobileNetV2Model as MobileNetV2Model, __webpack_exports__MobileNetV2PreTrainedModel as MobileNetV2PreTrainedModel, __webpack_exports__MobileNetV3FeatureExtractor as MobileNetV3FeatureExtractor, __webpack_exports__MobileNetV3ForImageClassification as MobileNetV3ForImageClassification, __webpack_exports__MobileNetV3ForSemanticSegmentation as MobileNetV3ForSemanticSegmentation, __webpack_exports__MobileNetV3ImageProcessor as MobileNetV3ImageProcessor, __webpack_exports__MobileNetV3Model as MobileNetV3Model, __webpack_exports__MobileNetV3PreTrainedModel as MobileNetV3PreTrainedModel, __webpack_exports__MobileNetV4FeatureExtractor as MobileNetV4FeatureExtractor, __webpack_exports__MobileNetV4ForImageClassification as MobileNetV4ForImageClassification, __webpack_exports__MobileNetV4ForSemanticSegmentation as MobileNetV4ForSemanticSegmentation, __webpack_exports__MobileNetV4ImageProcessor as MobileNetV4ImageProcessor, __webpack_exports__MobileNetV4Model as MobileNetV4Model, __webpack_exports__MobileNetV4PreTrainedModel as MobileNetV4PreTrainedModel, __webpack_exports__MobileViTFeatureExtractor as MobileViTFeatureExtractor, __webpack_exports__MobileViTForImageClassification as MobileViTForImageClassification, __webpack_exports__MobileViTImageProcessor as MobileViTImageProcessor, __webpack_exports__MobileViTModel as MobileViTModel, __webpack_exports__MobileViTPreTrainedModel as MobileViTPreTrainedModel, __webpack_exports__MobileViTV2ForImageClassification as MobileViTV2ForImageClassification, __webpack_exports__MobileViTV2Model as MobileViTV2Model, __webpack_exports__MobileViTV2PreTrainedModel as MobileViTV2PreTrainedModel, __webpack_exports__ModelOutput as ModelOutput, __webpack_exports__ModernBertForMaskedLM as ModernBertForMaskedLM, __webpack_exports__ModernBertForSequenceClassification as ModernBertForSequenceClassification, __webpack_exports__ModernBertForTokenClassification as ModernBertForTokenClassification, __webpack_exports__ModernBertModel as ModernBertModel, __webpack_exports__ModernBertPreTrainedModel as ModernBertPreTrainedModel, __webpack_exports__Moondream1ForConditionalGeneration as Moondream1ForConditionalGeneration, __webpack_exports__MoonshineFeatureExtractor as MoonshineFeatureExtractor, __webpack_exports__MoonshineForConditionalGeneration as MoonshineForConditionalGeneration, __webpack_exports__MoonshineModel as MoonshineModel, __webpack_exports__MoonshinePreTrainedModel as MoonshinePreTrainedModel, __webpack_exports__MoonshineProcessor as MoonshineProcessor, __webpack_exports__MptForCausalLM as MptForCausalLM, __webpack_exports__MptModel as MptModel, __webpack_exports__MptPreTrainedModel as MptPreTrainedModel, __webpack_exports__MultiModalityCausalLM as MultiModalityCausalLM, __webpack_exports__MultiModalityPreTrainedModel as MultiModalityPreTrainedModel, __webpack_exports__MusicgenForCausalLM as MusicgenForCausalLM, __webpack_exports__MusicgenForConditionalGeneration as MusicgenForConditionalGeneration, __webpack_exports__MusicgenModel as MusicgenModel, __webpack_exports__MusicgenPreTrainedModel as MusicgenPreTrainedModel, __webpack_exports__NllbTokenizer as NllbTokenizer, __webpack_exports__NoBadWordsLogitsProcessor as NoBadWordsLogitsProcessor, __webpack_exports__NoRepeatNGramLogitsProcessor as NoRepeatNGramLogitsProcessor, __webpack_exports__NomicBertModel as NomicBertModel, __webpack_exports__NomicBertPreTrainedModel as NomicBertPreTrainedModel, __webpack_exports__NougatImageProcessor as NougatImageProcessor, __webpack_exports__NougatTokenizer as NougatTokenizer, __webpack_exports__OPTForCausalLM as OPTForCausalLM, __webpack_exports__OPTModel as OPTModel, __webpack_exports__OPTPreTrainedModel as OPTPreTrainedModel, __webpack_exports__ObjectDetectionPipeline as ObjectDetectionPipeline, __webpack_exports__Olmo2ForCausalLM as Olmo2ForCausalLM, __webpack_exports__Olmo2Model as Olmo2Model, __webpack_exports__Olmo2PreTrainedModel as Olmo2PreTrainedModel, __webpack_exports__OlmoForCausalLM as OlmoForCausalLM, __webpack_exports__OlmoModel as OlmoModel, __webpack_exports__OlmoPreTrainedModel as OlmoPreTrainedModel, __webpack_exports__OpenELMForCausalLM as OpenELMForCausalLM, __webpack_exports__OpenELMModel as OpenELMModel, __webpack_exports__OpenELMPreTrainedModel as OpenELMPreTrainedModel, __webpack_exports__OwlViTFeatureExtractor as OwlViTFeatureExtractor, __webpack_exports__OwlViTForObjectDetection as OwlViTForObjectDetection, __webpack_exports__OwlViTImageProcessor as OwlViTImageProcessor, __webpack_exports__OwlViTModel as OwlViTModel, __webpack_exports__OwlViTPreTrainedModel as OwlViTPreTrainedModel, __webpack_exports__OwlViTProcessor as OwlViTProcessor, __webpack_exports__Owlv2ForObjectDetection as Owlv2ForObjectDetection, __webpack_exports__Owlv2ImageProcessor as Owlv2ImageProcessor, __webpack_exports__Owlv2Model as Owlv2Model, __webpack_exports__Owlv2PreTrainedModel as Owlv2PreTrainedModel, __webpack_exports__PaliGemmaForConditionalGeneration as PaliGemmaForConditionalGeneration, __webpack_exports__PaliGemmaPreTrainedModel as PaliGemmaPreTrainedModel, __webpack_exports__PaliGemmaProcessor as PaliGemmaProcessor, __webpack_exports__PatchTSMixerForPrediction as PatchTSMixerForPrediction, __webpack_exports__PatchTSMixerModel as PatchTSMixerModel, __webpack_exports__PatchTSMixerPreTrainedModel as PatchTSMixerPreTrainedModel, __webpack_exports__PatchTSTForPrediction as PatchTSTForPrediction, __webpack_exports__PatchTSTModel as PatchTSTModel, __webpack_exports__PatchTSTPreTrainedModel as PatchTSTPreTrainedModel, __webpack_exports__Phi3ForCausalLM as Phi3ForCausalLM, __webpack_exports__Phi3Model as Phi3Model, __webpack_exports__Phi3PreTrainedModel as Phi3PreTrainedModel, __webpack_exports__Phi3VForCausalLM as Phi3VForCausalLM, __webpack_exports__Phi3VImageProcessor as Phi3VImageProcessor, __webpack_exports__Phi3VPreTrainedModel as Phi3VPreTrainedModel, __webpack_exports__Phi3VProcessor as Phi3VProcessor, __webpack_exports__PhiForCausalLM as PhiForCausalLM, __webpack_exports__PhiModel as PhiModel, __webpack_exports__PhiPreTrainedModel as PhiPreTrainedModel, __webpack_exports__Pipeline as Pipeline, __webpack_exports__PreTrainedModel as PreTrainedModel, __webpack_exports__PreTrainedTokenizer as PreTrainedTokenizer, __webpack_exports__PretrainedConfig as PretrainedConfig, __webpack_exports__PretrainedMixin as PretrainedMixin, __webpack_exports__Processor as Processor, __webpack_exports__PvtForImageClassification as PvtForImageClassification, __webpack_exports__PvtImageProcessor as PvtImageProcessor, __webpack_exports__PvtModel as PvtModel, __webpack_exports__PvtPreTrainedModel as PvtPreTrainedModel, __webpack_exports__PyAnnoteFeatureExtractor as PyAnnoteFeatureExtractor, __webpack_exports__PyAnnoteForAudioFrameClassification as PyAnnoteForAudioFrameClassification, __webpack_exports__PyAnnoteModel as PyAnnoteModel, __webpack_exports__PyAnnotePreTrainedModel as PyAnnotePreTrainedModel, __webpack_exports__PyAnnoteProcessor as PyAnnoteProcessor, __webpack_exports__QuestionAnsweringModelOutput as QuestionAnsweringModelOutput, __webpack_exports__QuestionAnsweringPipeline as QuestionAnsweringPipeline, __webpack_exports__Qwen2ForCausalLM as Qwen2ForCausalLM, __webpack_exports__Qwen2Model as Qwen2Model, __webpack_exports__Qwen2PreTrainedModel as Qwen2PreTrainedModel, __webpack_exports__Qwen2Tokenizer as Qwen2Tokenizer, __webpack_exports__Qwen2VLForConditionalGeneration as Qwen2VLForConditionalGeneration, __webpack_exports__Qwen2VLImageProcessor as Qwen2VLImageProcessor, __webpack_exports__Qwen2VLPreTrainedModel as Qwen2VLPreTrainedModel, __webpack_exports__Qwen2VLProcessor as Qwen2VLProcessor, __webpack_exports__Qwen3ForCausalLM as Qwen3ForCausalLM, __webpack_exports__Qwen3Model as Qwen3Model, __webpack_exports__Qwen3PreTrainedModel as Qwen3PreTrainedModel, __webpack_exports__RFDetrForObjectDetection as RFDetrForObjectDetection, __webpack_exports__RFDetrModel as RFDetrModel, __webpack_exports__RFDetrObjectDetectionOutput as RFDetrObjectDetectionOutput, __webpack_exports__RFDetrPreTrainedModel as RFDetrPreTrainedModel, __webpack_exports__RTDetrForObjectDetection as RTDetrForObjectDetection, __webpack_exports__RTDetrImageProcessor as RTDetrImageProcessor, __webpack_exports__RTDetrModel as RTDetrModel, __webpack_exports__RTDetrObjectDetectionOutput as RTDetrObjectDetectionOutput, __webpack_exports__RTDetrPreTrainedModel as RTDetrPreTrainedModel, __webpack_exports__RTDetrV2ForObjectDetection as RTDetrV2ForObjectDetection, __webpack_exports__RTDetrV2Model as RTDetrV2Model, __webpack_exports__RTDetrV2ObjectDetectionOutput as RTDetrV2ObjectDetectionOutput, __webpack_exports__RTDetrV2PreTrainedModel as RTDetrV2PreTrainedModel, __webpack_exports__RawAudio as RawAudio, __webpack_exports__RawImage as RawImage, __webpack_exports__RawVideo as RawVideo, __webpack_exports__RawVideoFrame as RawVideoFrame, __webpack_exports__RepetitionPenaltyLogitsProcessor as RepetitionPenaltyLogitsProcessor, __webpack_exports__ResNetForImageClassification as ResNetForImageClassification, __webpack_exports__ResNetModel as ResNetModel, __webpack_exports__ResNetPreTrainedModel as ResNetPreTrainedModel, __webpack_exports__RoFormerForMaskedLM as RoFormerForMaskedLM, __webpack_exports__RoFormerForQuestionAnswering as RoFormerForQuestionAnswering, __webpack_exports__RoFormerForSequenceClassification as RoFormerForSequenceClassification, __webpack_exports__RoFormerForTokenClassification as RoFormerForTokenClassification, __webpack_exports__RoFormerModel as RoFormerModel, __webpack_exports__RoFormerPreTrainedModel as RoFormerPreTrainedModel, __webpack_exports__RoFormerTokenizer as RoFormerTokenizer, __webpack_exports__RobertaForMaskedLM as RobertaForMaskedLM, __webpack_exports__RobertaForQuestionAnswering as RobertaForQuestionAnswering, __webpack_exports__RobertaForSequenceClassification as RobertaForSequenceClassification, __webpack_exports__RobertaForTokenClassification as RobertaForTokenClassification, __webpack_exports__RobertaModel as RobertaModel, __webpack_exports__RobertaPreTrainedModel as RobertaPreTrainedModel, __webpack_exports__RobertaTokenizer as RobertaTokenizer, __webpack_exports__SamImageProcessor as SamImageProcessor, __webpack_exports__SamImageSegmentationOutput as SamImageSegmentationOutput, __webpack_exports__SamModel as SamModel, __webpack_exports__SamPreTrainedModel as SamPreTrainedModel, __webpack_exports__SamProcessor as SamProcessor, __webpack_exports__SapiensForDepthEstimation as SapiensForDepthEstimation, __webpack_exports__SapiensForNormalEstimation as SapiensForNormalEstimation, __webpack_exports__SapiensForSemanticSegmentation as SapiensForSemanticSegmentation, __webpack_exports__SapiensPreTrainedModel as SapiensPreTrainedModel, __webpack_exports__SeamlessM4TFeatureExtractor as SeamlessM4TFeatureExtractor, __webpack_exports__SegformerFeatureExtractor as SegformerFeatureExtractor, __webpack_exports__SegformerForImageClassification as SegformerForImageClassification, __webpack_exports__SegformerForSemanticSegmentation as SegformerForSemanticSegmentation, __webpack_exports__SegformerImageProcessor as SegformerImageProcessor, __webpack_exports__SegformerModel as SegformerModel, __webpack_exports__SegformerPreTrainedModel as SegformerPreTrainedModel, __webpack_exports__Seq2SeqLMOutput as Seq2SeqLMOutput, __webpack_exports__SequenceClassifierOutput as SequenceClassifierOutput, __webpack_exports__SiglipImageProcessor as SiglipImageProcessor, __webpack_exports__SiglipModel as SiglipModel, __webpack_exports__SiglipPreTrainedModel as SiglipPreTrainedModel, __webpack_exports__SiglipTextModel as SiglipTextModel, __webpack_exports__SiglipTokenizer as SiglipTokenizer, __webpack_exports__SiglipVisionModel as SiglipVisionModel, __webpack_exports__SmolVLMForConditionalGeneration as SmolVLMForConditionalGeneration, __webpack_exports__SmolVLMImageProcessor as SmolVLMImageProcessor, __webpack_exports__SmolVLMProcessor as SmolVLMProcessor, __webpack_exports__SnacDecoderModel as SnacDecoderModel, __webpack_exports__SnacEncoderModel as SnacEncoderModel, __webpack_exports__SnacFeatureExtractor as SnacFeatureExtractor, __webpack_exports__SnacModel as SnacModel, __webpack_exports__SnacPreTrainedModel as SnacPreTrainedModel, __webpack_exports__SpeechT5FeatureExtractor as SpeechT5FeatureExtractor, __webpack_exports__SpeechT5ForSpeechToText as SpeechT5ForSpeechToText, __webpack_exports__SpeechT5ForTextToSpeech as SpeechT5ForTextToSpeech, __webpack_exports__SpeechT5HifiGan as SpeechT5HifiGan, __webpack_exports__SpeechT5Model as SpeechT5Model, __webpack_exports__SpeechT5PreTrainedModel as SpeechT5PreTrainedModel, __webpack_exports__SpeechT5Processor as SpeechT5Processor, __webpack_exports__SpeechT5Tokenizer as SpeechT5Tokenizer, __webpack_exports__SqueezeBertForMaskedLM as SqueezeBertForMaskedLM, __webpack_exports__SqueezeBertForQuestionAnswering as SqueezeBertForQuestionAnswering, __webpack_exports__SqueezeBertForSequenceClassification as SqueezeBertForSequenceClassification, __webpack_exports__SqueezeBertModel as SqueezeBertModel, __webpack_exports__SqueezeBertPreTrainedModel as SqueezeBertPreTrainedModel, __webpack_exports__SqueezeBertTokenizer as SqueezeBertTokenizer, __webpack_exports__StableLmForCausalLM as StableLmForCausalLM, __webpack_exports__StableLmModel as StableLmModel, __webpack_exports__StableLmPreTrainedModel as StableLmPreTrainedModel, __webpack_exports__Starcoder2ForCausalLM as Starcoder2ForCausalLM, __webpack_exports__Starcoder2Model as Starcoder2Model, __webpack_exports__Starcoder2PreTrainedModel as Starcoder2PreTrainedModel, __webpack_exports__StoppingCriteria as StoppingCriteria, __webpack_exports__StoppingCriteriaList as StoppingCriteriaList, __webpack_exports__StyleTextToSpeech2Model as StyleTextToSpeech2Model, __webpack_exports__StyleTextToSpeech2PreTrainedModel as StyleTextToSpeech2PreTrainedModel, __webpack_exports__SummarizationPipeline as SummarizationPipeline, __webpack_exports__SuppressTokensAtBeginLogitsProcessor as SuppressTokensAtBeginLogitsProcessor, __webpack_exports__Swin2SRForImageSuperResolution as Swin2SRForImageSuperResolution, __webpack_exports__Swin2SRImageProcessor as Swin2SRImageProcessor, __webpack_exports__Swin2SRModel as Swin2SRModel, __webpack_exports__Swin2SRPreTrainedModel as Swin2SRPreTrainedModel, __webpack_exports__SwinForImageClassification as SwinForImageClassification, __webpack_exports__SwinForSemanticSegmentation as SwinForSemanticSegmentation, __webpack_exports__SwinModel as SwinModel, __webpack_exports__SwinPreTrainedModel as SwinPreTrainedModel, __webpack_exports__T5ForConditionalGeneration as T5ForConditionalGeneration, __webpack_exports__T5Model as T5Model, __webpack_exports__T5PreTrainedModel as T5PreTrainedModel, __webpack_exports__T5Tokenizer as T5Tokenizer, __webpack_exports__TableTransformerForObjectDetection as TableTransformerForObjectDetection, __webpack_exports__TableTransformerModel as TableTransformerModel, __webpack_exports__TableTransformerObjectDetectionOutput as TableTransformerObjectDetectionOutput, __webpack_exports__TableTransformerPreTrainedModel as TableTransformerPreTrainedModel, __webpack_exports__TemperatureLogitsWarper as TemperatureLogitsWarper, __webpack_exports__Tensor as Tensor, __webpack_exports__Text2TextGenerationPipeline as Text2TextGenerationPipeline, __webpack_exports__TextClassificationPipeline as TextClassificationPipeline, __webpack_exports__TextGenerationPipeline as TextGenerationPipeline, __webpack_exports__TextStreamer as TextStreamer, __webpack_exports__TextToAudioPipeline as TextToAudioPipeline, __webpack_exports__TokenClassificationPipeline as TokenClassificationPipeline, __webpack_exports__TokenClassifierOutput as TokenClassifierOutput, __webpack_exports__TokenizerModel as TokenizerModel, __webpack_exports__TopKLogitsWarper as TopKLogitsWarper, __webpack_exports__TopPLogitsWarper as TopPLogitsWarper, __webpack_exports__TrOCRForCausalLM as TrOCRForCausalLM, __webpack_exports__TrOCRPreTrainedModel as TrOCRPreTrainedModel, __webpack_exports__TranslationPipeline as TranslationPipeline, __webpack_exports__UltravoxModel as UltravoxModel, __webpack_exports__UltravoxPreTrainedModel as UltravoxPreTrainedModel, __webpack_exports__UltravoxProcessor as UltravoxProcessor, __webpack_exports__UniSpeechForCTC as UniSpeechForCTC, __webpack_exports__UniSpeechForSequenceClassification as UniSpeechForSequenceClassification, __webpack_exports__UniSpeechModel as UniSpeechModel, __webpack_exports__UniSpeechPreTrainedModel as UniSpeechPreTrainedModel, __webpack_exports__UniSpeechSatForAudioFrameClassification as UniSpeechSatForAudioFrameClassification, __webpack_exports__UniSpeechSatForCTC as UniSpeechSatForCTC, __webpack_exports__UniSpeechSatForSequenceClassification as UniSpeechSatForSequenceClassification, __webpack_exports__UniSpeechSatModel as UniSpeechSatModel, __webpack_exports__UniSpeechSatPreTrainedModel as UniSpeechSatPreTrainedModel, __webpack_exports__VLChatProcessor as VLChatProcessor, __webpack_exports__VLMImageProcessor as VLMImageProcessor, __webpack_exports__ViTFeatureExtractor as ViTFeatureExtractor, __webpack_exports__ViTForImageClassification as ViTForImageClassification, __webpack_exports__ViTImageProcessor as ViTImageProcessor, __webpack_exports__ViTMAEModel as ViTMAEModel, __webpack_exports__ViTMAEPreTrainedModel as ViTMAEPreTrainedModel, __webpack_exports__ViTMSNForImageClassification as ViTMSNForImageClassification, __webpack_exports__ViTMSNModel as ViTMSNModel, __webpack_exports__ViTMSNPreTrainedModel as ViTMSNPreTrainedModel, __webpack_exports__ViTModel as ViTModel, __webpack_exports__ViTPreTrainedModel as ViTPreTrainedModel, __webpack_exports__VisionEncoderDecoderModel as VisionEncoderDecoderModel, __webpack_exports__VitMatteForImageMatting as VitMatteForImageMatting, __webpack_exports__VitMatteImageProcessor as VitMatteImageProcessor, __webpack_exports__VitMattePreTrainedModel as VitMattePreTrainedModel, __webpack_exports__VitPoseForPoseEstimation as VitPoseForPoseEstimation, __webpack_exports__VitPoseImageProcessor as VitPoseImageProcessor, __webpack_exports__VitPosePreTrainedModel as VitPosePreTrainedModel, __webpack_exports__VitsModel as VitsModel, __webpack_exports__VitsModelOutput as VitsModelOutput, __webpack_exports__VitsPreTrainedModel as VitsPreTrainedModel, __webpack_exports__VitsTokenizer as VitsTokenizer, __webpack_exports__Wav2Vec2BertForCTC as Wav2Vec2BertForCTC, __webpack_exports__Wav2Vec2BertForSequenceClassification as Wav2Vec2BertForSequenceClassification, __webpack_exports__Wav2Vec2BertModel as Wav2Vec2BertModel, __webpack_exports__Wav2Vec2BertPreTrainedModel as Wav2Vec2BertPreTrainedModel, __webpack_exports__Wav2Vec2CTCTokenizer as Wav2Vec2CTCTokenizer, __webpack_exports__Wav2Vec2FeatureExtractor as Wav2Vec2FeatureExtractor, __webpack_exports__Wav2Vec2ForAudioFrameClassification as Wav2Vec2ForAudioFrameClassification, __webpack_exports__Wav2Vec2ForCTC as Wav2Vec2ForCTC, __webpack_exports__Wav2Vec2ForSequenceClassification as Wav2Vec2ForSequenceClassification, __webpack_exports__Wav2Vec2Model as Wav2Vec2Model, __webpack_exports__Wav2Vec2PreTrainedModel as Wav2Vec2PreTrainedModel, __webpack_exports__Wav2Vec2Processor as Wav2Vec2Processor, __webpack_exports__Wav2Vec2ProcessorWithLM as Wav2Vec2ProcessorWithLM, __webpack_exports__WavLMForAudioFrameClassification as WavLMForAudioFrameClassification, __webpack_exports__WavLMForCTC as WavLMForCTC, __webpack_exports__WavLMForSequenceClassification as WavLMForSequenceClassification, __webpack_exports__WavLMForXVector as WavLMForXVector, __webpack_exports__WavLMModel as WavLMModel, __webpack_exports__WavLMPreTrainedModel as WavLMPreTrainedModel, __webpack_exports__WeSpeakerFeatureExtractor as WeSpeakerFeatureExtractor, __webpack_exports__WeSpeakerResNetModel as WeSpeakerResNetModel, __webpack_exports__WeSpeakerResNetPreTrainedModel as WeSpeakerResNetPreTrainedModel, __webpack_exports__WhisperFeatureExtractor as WhisperFeatureExtractor, __webpack_exports__WhisperForConditionalGeneration as WhisperForConditionalGeneration, __webpack_exports__WhisperModel as WhisperModel, __webpack_exports__WhisperPreTrainedModel as WhisperPreTrainedModel, __webpack_exports__WhisperProcessor as WhisperProcessor, __webpack_exports__WhisperTextStreamer as WhisperTextStreamer, __webpack_exports__WhisperTimeStampLogitsProcessor as WhisperTimeStampLogitsProcessor, __webpack_exports__WhisperTokenizer as WhisperTokenizer, __webpack_exports__XLMForQuestionAnswering as XLMForQuestionAnswering, __webpack_exports__XLMForSequenceClassification as XLMForSequenceClassification, __webpack_exports__XLMForTokenClassification as XLMForTokenClassification, __webpack_exports__XLMModel as XLMModel, __webpack_exports__XLMPreTrainedModel as XLMPreTrainedModel, __webpack_exports__XLMRobertaForMaskedLM as XLMRobertaForMaskedLM, __webpack_exports__XLMRobertaForQuestionAnswering as XLMRobertaForQuestionAnswering, __webpack_exports__XLMRobertaForSequenceClassification as XLMRobertaForSequenceClassification, __webpack_exports__XLMRobertaForTokenClassification as XLMRobertaForTokenClassification, __webpack_exports__XLMRobertaModel as XLMRobertaModel, __webpack_exports__XLMRobertaPreTrainedModel as XLMRobertaPreTrainedModel, __webpack_exports__XLMRobertaTokenizer as XLMRobertaTokenizer, __webpack_exports__XLMTokenizer as XLMTokenizer, __webpack_exports__XLMWithLMHeadModel as XLMWithLMHeadModel, __webpack_exports__XVectorOutput as XVectorOutput, __webpack_exports__YolosFeatureExtractor as YolosFeatureExtractor, __webpack_exports__YolosForObjectDetection as YolosForObjectDetection, __webpack_exports__YolosImageProcessor as YolosImageProcessor, __webpack_exports__YolosModel as YolosModel, __webpack_exports__YolosObjectDetectionOutput as YolosObjectDetectionOutput, __webpack_exports__YolosPreTrainedModel as YolosPreTrainedModel, __webpack_exports__ZeroShotAudioClassificationPipeline as ZeroShotAudioClassificationPipeline, __webpack_exports__ZeroShotClassificationPipeline as ZeroShotClassificationPipeline, __webpack_exports__ZeroShotImageClassificationPipeline as ZeroShotImageClassificationPipeline, __webpack_exports__ZeroShotObjectDetectionPipeline as ZeroShotObjectDetectionPipeline, __webpack_exports__bankers_round as bankers_round, __webpack_exports__cat as cat, __webpack_exports__cos_sim as cos_sim, __webpack_exports__dot as dot, __webpack_exports__dynamic_time_warping as dynamic_time_warping, __webpack_exports__env as env, __webpack_exports__full as full, __webpack_exports__full_like as full_like, __webpack_exports__getKeyValueShapes as getKeyValueShapes, __webpack_exports__hamming as hamming, __webpack_exports__hanning as hanning, __webpack_exports__interpolate as interpolate, __webpack_exports__interpolate_4d as interpolate_4d, __webpack_exports__interpolate_data as interpolate_data, __webpack_exports__is_chinese_char as is_chinese_char, __webpack_exports__layer_norm as layer_norm, __webpack_exports__load_image as load_image, __webpack_exports__load_video as load_video, __webpack_exports__log_softmax as log_softmax, __webpack_exports__magnitude as magnitude, __webpack_exports__matmul as matmul, __webpack_exports__max as max, __webpack_exports__mean as mean, __webpack_exports__mean_pooling as mean_pooling, __webpack_exports__medianFilter as medianFilter, __webpack_exports__mel_filter_bank as mel_filter_bank, __webpack_exports__min as min, __webpack_exports__ones as ones, __webpack_exports__ones_like as ones_like, __webpack_exports__permute as permute, __webpack_exports__permute_data as permute_data, __webpack_exports__pipeline as pipeline, __webpack_exports__quantize_embeddings as quantize_embeddings, __webpack_exports__rand as rand, __webpack_exports__read_audio as read_audio, __webpack_exports__rfft as rfft, __webpack_exports__round as round, __webpack_exports__slice as slice, __webpack_exports__softmax as softmax, __webpack_exports__spectrogram as spectrogram, __webpack_exports__stack as stack, __webpack_exports__std_mean as std_mean, __webpack_exports__topk as topk, __webpack_exports__window_function as window_function, __webpack_exports__zeros as zeros, __webpack_exports__zeros_like as zeros_like }; + +//# sourceMappingURL=transformers.js.map \ No newline at end of file diff --git a/_shared/voice/vendor/models/onnx-community/Kokoro-82M-v1.0-ONNX/config.json b/_shared/voice/vendor/models/onnx-community/Kokoro-82M-v1.0-ONNX/config.json new file mode 100644 index 0000000000000000000000000000000000000000..790faf216e7e3f490e71e8bc80df79ed8941101c --- /dev/null +++ b/_shared/voice/vendor/models/onnx-community/Kokoro-82M-v1.0-ONNX/config.json @@ -0,0 +1,3 @@ +{ + "model_type": "style_text_to_speech_2" +} \ No newline at end of file diff --git a/_shared/voice/vendor/models/onnx-community/Kokoro-82M-v1.0-ONNX/tokenizer.json b/_shared/voice/vendor/models/onnx-community/Kokoro-82M-v1.0-ONNX/tokenizer.json new file mode 100644 index 0000000000000000000000000000000000000000..4280f55fc1c32211bc9bb4d55545759a00054ecd --- /dev/null +++ b/_shared/voice/vendor/models/onnx-community/Kokoro-82M-v1.0-ONNX/tokenizer.json @@ -0,0 +1,175 @@ +{ + "version": "1.0", + "truncation": null, + "padding": null, + "added_tokens": [], + "normalizer": { + "type": "Replace", + "pattern": { + "Regex": "[^$;:,.!?\u2014\u2026\"()\u201c\u201d \u0303\u02a3\u02a5\u02a6\u02a8\u1d5d\uab67AIOQSTWY\u1d4aabcdefhijklmnopqrstuvwxyz\u0251\u0250\u0252\u00e6\u03b2\u0254\u0255\u00e7\u0256\u00f0\u02a4\u0259\u025a\u025b\u025c\u025f\u0261\u0265\u0268\u026a\u029d\u026f\u0270\u014b\u0273\u0272\u0274\u00f8\u0278\u03b8\u0153\u0279\u027e\u027b\u0281\u027d\u0282\u0283\u0288\u02a7\u028a\u028b\u028c\u0263\u0264\u03c7\u028e\u0292\u0294\u02c8\u02cc\u02d0\u02b0\u02b2\u2193\u2192\u2197\u2198\u1d7b]" + }, + "content": "" + }, + "pre_tokenizer": { + "type": "Split", + "pattern": { + "Regex": "" + }, + "behavior": "Isolated", + "invert": false + }, + "post_processor": { + "type": "TemplateProcessing", + "single": [ + { + "SpecialToken": { + "id": "$", + "type_id": 0 + } + }, + { + "Sequence": { + "id": "A", + "type_id": 0 + } + }, + { + "SpecialToken": { + "id": "$", + "type_id": 0 + } + } + ], + "special_tokens": { + "$": { + "id": "$", + "ids": [ + 0 + ], + "tokens": [ + "$" + ] + } + } + }, + "decoder": null, + "model": { + "vocab": { + "$": 0, + ";": 1, + ":": 2, + ",": 3, + ".": 4, + "!": 5, + "?": 6, + "\u2014": 9, + "\u2026": 10, + "\"": 11, + "(": 12, + ")": 13, + "\u201c": 14, + "\u201d": 15, + " ": 16, + "\u0303": 17, + "\u02a3": 18, + "\u02a5": 19, + "\u02a6": 20, + "\u02a8": 21, + "\u1d5d": 22, + "\uab67": 23, + "A": 24, + "I": 25, + "O": 31, + "Q": 33, + "S": 35, + "T": 36, + "W": 39, + "Y": 41, + "\u1d4a": 42, + "a": 43, + "b": 44, + "c": 45, + "d": 46, + "e": 47, + "f": 48, + "h": 50, + "i": 51, + "j": 52, + "k": 53, + "l": 54, + "m": 55, + "n": 56, + "o": 57, + "p": 58, + "q": 59, + "r": 60, + "s": 61, + "t": 62, + "u": 63, + "v": 64, + "w": 65, + "x": 66, + "y": 67, + "z": 68, + "\u0251": 69, + "\u0250": 70, + "\u0252": 71, + "\u00e6": 72, + "\u03b2": 75, + "\u0254": 76, + "\u0255": 77, + "\u00e7": 78, + "\u0256": 80, + "\u00f0": 81, + "\u02a4": 82, + "\u0259": 83, + "\u025a": 85, + "\u025b": 86, + "\u025c": 87, + "\u025f": 90, + "\u0261": 92, + "\u0265": 99, + "\u0268": 101, + "\u026a": 102, + "\u029d": 103, + "\u026f": 110, + "\u0270": 111, + "\u014b": 112, + "\u0273": 113, + "\u0272": 114, + "\u0274": 115, + "\u00f8": 116, + "\u0278": 118, + "\u03b8": 119, + "\u0153": 120, + "\u0279": 123, + "\u027e": 125, + "\u027b": 126, + "\u0281": 128, + "\u027d": 129, + "\u0282": 130, + "\u0283": 131, + "\u0288": 132, + "\u02a7": 133, + "\u028a": 135, + "\u028b": 136, + "\u028c": 138, + "\u0263": 139, + "\u0264": 140, + "\u03c7": 142, + "\u028e": 143, + "\u0292": 147, + "\u0294": 148, + "\u02c8": 156, + "\u02cc": 157, + "\u02d0": 158, + "\u02b0": 162, + "\u02b2": 164, + "\u2193": 169, + "\u2192": 171, + "\u2197": 172, + "\u2198": 173, + "\u1d7b": 177 + } + } +} \ No newline at end of file diff --git a/_shared/voice/vendor/models/onnx-community/Kokoro-82M-v1.0-ONNX/tokenizer_config.json b/_shared/voice/vendor/models/onnx-community/Kokoro-82M-v1.0-ONNX/tokenizer_config.json new file mode 100644 index 0000000000000000000000000000000000000000..5c81e9a3a06db9139900d6ee5b60e8bb701ccb0b --- /dev/null +++ b/_shared/voice/vendor/models/onnx-community/Kokoro-82M-v1.0-ONNX/tokenizer_config.json @@ -0,0 +1,6 @@ +{ + "model_max_length": 512, + "pad_token": "$", + "tokenizer_class": "PreTrainedTokenizer", + "unk_token": "$" +} \ No newline at end of file diff --git a/_shared/voice/vendor/models/onnx-community/Kokoro-82M-v1.0-ONNX/voices/af_bella.bin b/_shared/voice/vendor/models/onnx-community/Kokoro-82M-v1.0-ONNX/voices/af_bella.bin new file mode 100644 index 0000000000000000000000000000000000000000..365633a9291e723da03eef8d4a044bdde938285f --- /dev/null +++ b/_shared/voice/vendor/models/onnx-community/Kokoro-82M-v1.0-ONNX/voices/af_bella.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f69d836209b78eb8c66e75e3cda491e26ea838a3674257e9d4e5703cbaf55c8b +size 522240 diff --git a/_shared/voice/vendor/models/onnx-community/Kokoro-82M-v1.0-ONNX/voices/af_heart.bin b/_shared/voice/vendor/models/onnx-community/Kokoro-82M-v1.0-ONNX/voices/af_heart.bin new file mode 100644 index 0000000000000000000000000000000000000000..e256d26664ef513affbcf7bace1246bbcffea5c3 --- /dev/null +++ b/_shared/voice/vendor/models/onnx-community/Kokoro-82M-v1.0-ONNX/voices/af_heart.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d583ccff3cdca2f7fae535cb998ac07e9fcb90f09737b9a41fa2734ec44a8f0b +size 522240 diff --git a/_shared/voice/vendor/models/onnx-community/Kokoro-82M-v1.0-ONNX/voices/af_nicole.bin b/_shared/voice/vendor/models/onnx-community/Kokoro-82M-v1.0-ONNX/voices/af_nicole.bin new file mode 100644 index 0000000000000000000000000000000000000000..672a558c7b1bb5ba3c3b5bd8b3684fda3fc3af92 --- /dev/null +++ b/_shared/voice/vendor/models/onnx-community/Kokoro-82M-v1.0-ONNX/voices/af_nicole.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cd2191ab31b914ed7b318416b0e4440fdf392ddad9106a060819aa600a64f59a +size 522240 diff --git a/_shared/voice/vendor/models/onnx-community/Kokoro-82M-v1.0-ONNX/voices/am_fenrir.bin b/_shared/voice/vendor/models/onnx-community/Kokoro-82M-v1.0-ONNX/voices/am_fenrir.bin new file mode 100644 index 0000000000000000000000000000000000000000..8f11b76a4bee997e468e1658237134e4bcd84e28 --- /dev/null +++ b/_shared/voice/vendor/models/onnx-community/Kokoro-82M-v1.0-ONNX/voices/am_fenrir.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c27989f741f7ee34d273a39d8a595cc0837d35f5ced9a29b7cc162614616df43 +size 522240 diff --git a/_shared/voice/vendor/models/onnx-community/Kokoro-82M-v1.0-ONNX/voices/am_michael.bin b/_shared/voice/vendor/models/onnx-community/Kokoro-82M-v1.0-ONNX/voices/am_michael.bin new file mode 100644 index 0000000000000000000000000000000000000000..fe0ada256058dc58006d5ba628b37a7e73c85e5f --- /dev/null +++ b/_shared/voice/vendor/models/onnx-community/Kokoro-82M-v1.0-ONNX/voices/am_michael.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1d1f21dd8da39c30705cd4c75d039d265e9bc4a2a93ed09bc9e1b1225eb95ba1 +size 522240 diff --git a/_shared/voice/vendor/models/onnx-community/Kokoro-82M-v1.0-ONNX/voices/am_puck.bin b/_shared/voice/vendor/models/onnx-community/Kokoro-82M-v1.0-ONNX/voices/am_puck.bin new file mode 100644 index 0000000000000000000000000000000000000000..501a49dee17309d44af14812508ba6d957f132fa --- /dev/null +++ b/_shared/voice/vendor/models/onnx-community/Kokoro-82M-v1.0-ONNX/voices/am_puck.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fcf73c989033e9233e0b98713eca600c8c74dcc1614b37009d5450ff4a2274a0 +size 522240 diff --git a/_shared/voice/vendor/models/onnx-community/Kokoro-82M-v1.0-ONNX/voices/bf_emma.bin b/_shared/voice/vendor/models/onnx-community/Kokoro-82M-v1.0-ONNX/voices/bf_emma.bin new file mode 100644 index 0000000000000000000000000000000000000000..436db46f637565323f32e25974a45bedc737d54d --- /dev/null +++ b/_shared/voice/vendor/models/onnx-community/Kokoro-82M-v1.0-ONNX/voices/bf_emma.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:669fe0647f9dd04fcab92f1439a40eeb4c8b4ab1f82e4996fe3d918ce4a63b73 +size 522240 diff --git a/_shared/voice/vendor/models/onnx-community/Kokoro-82M-v1.0-ONNX/voices/bm_george.bin b/_shared/voice/vendor/models/onnx-community/Kokoro-82M-v1.0-ONNX/voices/bm_george.bin new file mode 100644 index 0000000000000000000000000000000000000000..197a97c2421b8be060b5d3408a318010d2ea9fff --- /dev/null +++ b/_shared/voice/vendor/models/onnx-community/Kokoro-82M-v1.0-ONNX/voices/bm_george.bin @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c4b235a4c1f2cd3b939fed08b899ce9385638b763f7b73a59616c4fc9bd6c9bc +size 522240 diff --git a/_shared/voice/vendor/models/onnx-community/silero-vad/onnx/model.onnx b/_shared/voice/vendor/models/onnx-community/silero-vad/onnx/model.onnx new file mode 100644 index 0000000000000000000000000000000000000000..b9ade7cc27d1ac3a73e439a3aa521e0dc107cf00 --- /dev/null +++ b/_shared/voice/vendor/models/onnx-community/silero-vad/onnx/model.onnx @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a4a068cd6cf1ea8355b84327595838ca748ec29a25bc91fc82e6c299ccdc5808 +size 2243022 diff --git a/_shared/voice/vendor/models/onnx-community/silero-vad/onnx/model_quantized.onnx b/_shared/voice/vendor/models/onnx-community/silero-vad/onnx/model_quantized.onnx new file mode 100644 index 0000000000000000000000000000000000000000..98c3fa3ed21cfc12f525f6af02d07131e71a46f5 --- /dev/null +++ b/_shared/voice/vendor/models/onnx-community/silero-vad/onnx/model_quantized.onnx @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:982c96dc518784fb9d19bc7f56cc8252473b020e4f2099f32049e4ad0b3b43e7 +size 639335 diff --git a/_shared/voice/vendor/transformers/ort-wasm-simd-threaded.jsep.wasm b/_shared/voice/vendor/transformers/ort-wasm-simd-threaded.jsep.wasm new file mode 100644 index 0000000000000000000000000000000000000000..e60bf9f9491fa66b0fc71dd0134d1f0ba5b5e719 --- /dev/null +++ b/_shared/voice/vendor/transformers/ort-wasm-simd-threaded.jsep.wasm @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0f6fe5c40378504d1a25a77f766133464bb15705af23e01c994f185719fb080e +size 21643825 diff --git a/_shared/voice/vendor/transformers/transformers.js b/_shared/voice/vendor/transformers/transformers.js new file mode 100644 index 0000000000000000000000000000000000000000..e161a201119bb439bab3d4b94b08f194a54939ef --- /dev/null +++ b/_shared/voice/vendor/transformers/transformers.js @@ -0,0 +1,34120 @@ +/******/ var __webpack_modules__ = ({ + +/***/ "./node_modules/onnxruntime-web/dist/ort-wasm-simd-threaded.jsep.wasm": +/*!****************************************************************************!*\ + !*** ./node_modules/onnxruntime-web/dist/ort-wasm-simd-threaded.jsep.wasm ***! + \****************************************************************************/ +/***/ ((module, __unused_webpack_exports, __webpack_require__) => { + +module.exports = __webpack_require__.p + "ort-wasm-simd-threaded.jsep.wasm"; + +/***/ }), + +/***/ "?2ce3": +/*!**********************************!*\ + !*** onnxruntime-node (ignored) ***! + \**********************************/ +/***/ (() => { + +/* (ignored) */ + +/***/ }), + +/***/ "?7a2c": +/*!********************!*\ + !*** fs (ignored) ***! + \********************/ +/***/ (() => { + +/* (ignored) */ + +/***/ }), + +/***/ "?a42a": +/*!**********************!*\ + !*** path (ignored) ***! + \**********************/ +/***/ (() => { + +/* (ignored) */ + +/***/ }), + +/***/ "?2b25": +/*!***********************!*\ + !*** sharp (ignored) ***! + \***********************/ +/***/ (() => { + +/* (ignored) */ + +/***/ }), + +/***/ "?569f": +/*!********************!*\ + !*** fs (ignored) ***! + \********************/ +/***/ (() => { + +/* (ignored) */ + +/***/ }), + +/***/ "?3f59": +/*!**********************!*\ + !*** path (ignored) ***! + \**********************/ +/***/ (() => { + +/* (ignored) */ + +/***/ }), + +/***/ "?154a": +/*!*********************!*\ + !*** url (ignored) ***! + \*********************/ +/***/ (() => { + +/* (ignored) */ + +/***/ }), + +/***/ "./node_modules/@huggingface/jinja/dist/index.js": +/*!*******************************************************!*\ + !*** ./node_modules/@huggingface/jinja/dist/index.js ***! + \*******************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ Environment: () => (/* binding */ Environment), +/* harmony export */ Interpreter: () => (/* binding */ Interpreter), +/* harmony export */ Template: () => (/* binding */ Template), +/* harmony export */ parse: () => (/* binding */ parse), +/* harmony export */ tokenize: () => (/* binding */ tokenize) +/* harmony export */ }); +// src/lexer.ts +var TOKEN_TYPES = Object.freeze({ + Text: "Text", + // The text between Jinja statements or expressions + NumericLiteral: "NumericLiteral", + // e.g., 123 + BooleanLiteral: "BooleanLiteral", + // true or false + StringLiteral: "StringLiteral", + // 'string' + Identifier: "Identifier", + // Variables, functions, etc. + Equals: "Equals", + // = + OpenParen: "OpenParen", + // ( + CloseParen: "CloseParen", + // ) + OpenStatement: "OpenStatement", + // {% + CloseStatement: "CloseStatement", + // %} + OpenExpression: "OpenExpression", + // {{ + CloseExpression: "CloseExpression", + // }} + OpenSquareBracket: "OpenSquareBracket", + // [ + CloseSquareBracket: "CloseSquareBracket", + // ] + OpenCurlyBracket: "OpenCurlyBracket", + // { + CloseCurlyBracket: "CloseCurlyBracket", + // } + Comma: "Comma", + // , + Dot: "Dot", + // . + Colon: "Colon", + // : + Pipe: "Pipe", + // | + CallOperator: "CallOperator", + // () + AdditiveBinaryOperator: "AdditiveBinaryOperator", + // + - + MultiplicativeBinaryOperator: "MultiplicativeBinaryOperator", + // * / % + ComparisonBinaryOperator: "ComparisonBinaryOperator", + // < > <= >= == != + UnaryOperator: "UnaryOperator", + // ! - + + // Keywords + Set: "Set", + If: "If", + For: "For", + In: "In", + Is: "Is", + NotIn: "NotIn", + Else: "Else", + EndIf: "EndIf", + ElseIf: "ElseIf", + EndFor: "EndFor", + And: "And", + Or: "Or", + Not: "UnaryOperator", + Macro: "Macro", + EndMacro: "EndMacro" +}); +var KEYWORDS = Object.freeze({ + set: TOKEN_TYPES.Set, + for: TOKEN_TYPES.For, + in: TOKEN_TYPES.In, + is: TOKEN_TYPES.Is, + if: TOKEN_TYPES.If, + else: TOKEN_TYPES.Else, + endif: TOKEN_TYPES.EndIf, + elif: TOKEN_TYPES.ElseIf, + endfor: TOKEN_TYPES.EndFor, + and: TOKEN_TYPES.And, + or: TOKEN_TYPES.Or, + not: TOKEN_TYPES.Not, + "not in": TOKEN_TYPES.NotIn, + macro: TOKEN_TYPES.Macro, + endmacro: TOKEN_TYPES.EndMacro, + // Literals + true: TOKEN_TYPES.BooleanLiteral, + false: TOKEN_TYPES.BooleanLiteral, + // NOTE: According to the Jinja docs: The special constants true, false, and none are indeed lowercase. + // Because that caused confusion in the past, (True used to expand to an undefined variable that was considered false), + // all three can now also be written in title case (True, False, and None). However, for consistency, (all Jinja identifiers are lowercase) + // you should use the lowercase versions. + True: TOKEN_TYPES.BooleanLiteral, + False: TOKEN_TYPES.BooleanLiteral +}); +var Token = class { + /** + * Constructs a new Token. + * @param {string} value The raw value as seen inside the source code. + * @param {TokenType} type The type of token. + */ + constructor(value, type) { + this.value = value; + this.type = type; + } +}; +function isWord(char) { + return /\w/.test(char); +} +function isInteger(char) { + return /[0-9]/.test(char); +} +var ORDERED_MAPPING_TABLE = [ + // Control sequences + ["{%", TOKEN_TYPES.OpenStatement], + ["%}", TOKEN_TYPES.CloseStatement], + ["{{", TOKEN_TYPES.OpenExpression], + ["}}", TOKEN_TYPES.CloseExpression], + // Single character tokens + ["(", TOKEN_TYPES.OpenParen], + [")", TOKEN_TYPES.CloseParen], + ["{", TOKEN_TYPES.OpenCurlyBracket], + ["}", TOKEN_TYPES.CloseCurlyBracket], + ["[", TOKEN_TYPES.OpenSquareBracket], + ["]", TOKEN_TYPES.CloseSquareBracket], + [",", TOKEN_TYPES.Comma], + [".", TOKEN_TYPES.Dot], + [":", TOKEN_TYPES.Colon], + ["|", TOKEN_TYPES.Pipe], + // Comparison operators + ["<=", TOKEN_TYPES.ComparisonBinaryOperator], + [">=", TOKEN_TYPES.ComparisonBinaryOperator], + ["==", TOKEN_TYPES.ComparisonBinaryOperator], + ["!=", TOKEN_TYPES.ComparisonBinaryOperator], + ["<", TOKEN_TYPES.ComparisonBinaryOperator], + [">", TOKEN_TYPES.ComparisonBinaryOperator], + // Arithmetic operators + ["+", TOKEN_TYPES.AdditiveBinaryOperator], + ["-", TOKEN_TYPES.AdditiveBinaryOperator], + ["*", TOKEN_TYPES.MultiplicativeBinaryOperator], + ["/", TOKEN_TYPES.MultiplicativeBinaryOperator], + ["%", TOKEN_TYPES.MultiplicativeBinaryOperator], + // Assignment operator + ["=", TOKEN_TYPES.Equals] +]; +var ESCAPE_CHARACTERS = /* @__PURE__ */ new Map([ + ["n", "\n"], + // New line + ["t", " "], + // Horizontal tab + ["r", "\r"], + // Carriage return + ["b", "\b"], + // Backspace + ["f", "\f"], + // Form feed + ["v", "\v"], + // Vertical tab + ["'", "'"], + // Single quote + ['"', '"'], + // Double quote + ["\\", "\\"] + // Backslash +]); +function preprocess(template, options = {}) { + if (template.endsWith("\n")) { + template = template.slice(0, -1); + } + template = template.replace(/{#.*?#}/gs, "{##}"); + if (options.lstrip_blocks) { + template = template.replace(/^[ \t]*({[#%])/gm, "$1"); + } + if (options.trim_blocks) { + template = template.replace(/([#%]})\n/g, "$1"); + } + return template.replace(/{##}/g, "").replace(/-%}\s*/g, "%}").replace(/\s*{%-/g, "{%").replace(/-}}\s*/g, "}}").replace(/\s*{{-/g, "{{"); +} +function tokenize(source, options = {}) { + const tokens = []; + const src = preprocess(source, options); + let cursorPosition = 0; + const consumeWhile = (predicate) => { + let str = ""; + while (predicate(src[cursorPosition])) { + if (src[cursorPosition] === "\\") { + ++cursorPosition; + if (cursorPosition >= src.length) + throw new SyntaxError("Unexpected end of input"); + const escaped = src[cursorPosition++]; + const unescaped = ESCAPE_CHARACTERS.get(escaped); + if (unescaped === void 0) { + throw new SyntaxError(`Unexpected escaped character: ${escaped}`); + } + str += unescaped; + continue; + } + str += src[cursorPosition++]; + if (cursorPosition >= src.length) + throw new SyntaxError("Unexpected end of input"); + } + return str; + }; + main: + while (cursorPosition < src.length) { + const lastTokenType = tokens.at(-1)?.type; + if (lastTokenType === void 0 || lastTokenType === TOKEN_TYPES.CloseStatement || lastTokenType === TOKEN_TYPES.CloseExpression) { + let text = ""; + while (cursorPosition < src.length && // Keep going until we hit the next Jinja statement or expression + !(src[cursorPosition] === "{" && (src[cursorPosition + 1] === "%" || src[cursorPosition + 1] === "{"))) { + text += src[cursorPosition++]; + } + if (text.length > 0) { + tokens.push(new Token(text, TOKEN_TYPES.Text)); + continue; + } + } + consumeWhile((char2) => /\s/.test(char2)); + const char = src[cursorPosition]; + if (char === "-" || char === "+") { + const lastTokenType2 = tokens.at(-1)?.type; + if (lastTokenType2 === TOKEN_TYPES.Text || lastTokenType2 === void 0) { + throw new SyntaxError(`Unexpected character: ${char}`); + } + switch (lastTokenType2) { + case TOKEN_TYPES.Identifier: + case TOKEN_TYPES.NumericLiteral: + case TOKEN_TYPES.BooleanLiteral: + case TOKEN_TYPES.StringLiteral: + case TOKEN_TYPES.CloseParen: + case TOKEN_TYPES.CloseSquareBracket: + break; + default: { + ++cursorPosition; + const num = consumeWhile(isInteger); + tokens.push( + new Token(`${char}${num}`, num.length > 0 ? TOKEN_TYPES.NumericLiteral : TOKEN_TYPES.UnaryOperator) + ); + continue; + } + } + } + for (const [char2, token] of ORDERED_MAPPING_TABLE) { + const slice2 = src.slice(cursorPosition, cursorPosition + char2.length); + if (slice2 === char2) { + tokens.push(new Token(char2, token)); + cursorPosition += char2.length; + continue main; + } + } + if (char === "'" || char === '"') { + ++cursorPosition; + const str = consumeWhile((c) => c !== char); + tokens.push(new Token(str, TOKEN_TYPES.StringLiteral)); + ++cursorPosition; + continue; + } + if (isInteger(char)) { + const num = consumeWhile(isInteger); + tokens.push(new Token(num, TOKEN_TYPES.NumericLiteral)); + continue; + } + if (isWord(char)) { + const word = consumeWhile(isWord); + const type = Object.hasOwn(KEYWORDS, word) ? KEYWORDS[word] : TOKEN_TYPES.Identifier; + if (type === TOKEN_TYPES.In && tokens.at(-1)?.type === TOKEN_TYPES.Not) { + tokens.pop(); + tokens.push(new Token("not in", TOKEN_TYPES.NotIn)); + } else { + tokens.push(new Token(word, type)); + } + continue; + } + throw new SyntaxError(`Unexpected character: ${char}`); + } + return tokens; +} + +// src/ast.ts +var Statement = class { + type = "Statement"; +}; +var Program = class extends Statement { + constructor(body) { + super(); + this.body = body; + } + type = "Program"; +}; +var If = class extends Statement { + constructor(test, body, alternate) { + super(); + this.test = test; + this.body = body; + this.alternate = alternate; + } + type = "If"; +}; +var For = class extends Statement { + constructor(loopvar, iterable, body, defaultBlock) { + super(); + this.loopvar = loopvar; + this.iterable = iterable; + this.body = body; + this.defaultBlock = defaultBlock; + } + type = "For"; +}; +var SetStatement = class extends Statement { + constructor(assignee, value) { + super(); + this.assignee = assignee; + this.value = value; + } + type = "Set"; +}; +var Macro = class extends Statement { + constructor(name, args, body) { + super(); + this.name = name; + this.args = args; + this.body = body; + } + type = "Macro"; +}; +var Expression = class extends Statement { + type = "Expression"; +}; +var MemberExpression = class extends Expression { + constructor(object, property, computed) { + super(); + this.object = object; + this.property = property; + this.computed = computed; + } + type = "MemberExpression"; +}; +var CallExpression = class extends Expression { + constructor(callee, args) { + super(); + this.callee = callee; + this.args = args; + } + type = "CallExpression"; +}; +var Identifier = class extends Expression { + /** + * @param {string} value The name of the identifier + */ + constructor(value) { + super(); + this.value = value; + } + type = "Identifier"; +}; +var Literal = class extends Expression { + constructor(value) { + super(); + this.value = value; + } + type = "Literal"; +}; +var NumericLiteral = class extends Literal { + type = "NumericLiteral"; +}; +var StringLiteral = class extends Literal { + type = "StringLiteral"; +}; +var BooleanLiteral = class extends Literal { + type = "BooleanLiteral"; +}; +var ArrayLiteral = class extends Literal { + type = "ArrayLiteral"; +}; +var TupleLiteral = class extends Literal { + type = "TupleLiteral"; +}; +var ObjectLiteral = class extends Literal { + type = "ObjectLiteral"; +}; +var BinaryExpression = class extends Expression { + constructor(operator, left, right) { + super(); + this.operator = operator; + this.left = left; + this.right = right; + } + type = "BinaryExpression"; +}; +var FilterExpression = class extends Expression { + constructor(operand, filter) { + super(); + this.operand = operand; + this.filter = filter; + } + type = "FilterExpression"; +}; +var SelectExpression = class extends Expression { + constructor(iterable, test) { + super(); + this.iterable = iterable; + this.test = test; + } + type = "SelectExpression"; +}; +var TestExpression = class extends Expression { + constructor(operand, negate, test) { + super(); + this.operand = operand; + this.negate = negate; + this.test = test; + } + type = "TestExpression"; +}; +var UnaryExpression = class extends Expression { + constructor(operator, argument) { + super(); + this.operator = operator; + this.argument = argument; + } + type = "UnaryExpression"; +}; +var SliceExpression = class extends Expression { + constructor(start = void 0, stop = void 0, step = void 0) { + super(); + this.start = start; + this.stop = stop; + this.step = step; + } + type = "SliceExpression"; +}; +var KeywordArgumentExpression = class extends Expression { + constructor(key, value) { + super(); + this.key = key; + this.value = value; + } + type = "KeywordArgumentExpression"; +}; + +// src/parser.ts +function parse(tokens) { + const program = new Program([]); + let current = 0; + function expect(type, error) { + const prev = tokens[current++]; + if (!prev || prev.type !== type) { + throw new Error(`Parser Error: ${error}. ${prev.type} !== ${type}.`); + } + return prev; + } + function parseAny() { + switch (tokens[current].type) { + case TOKEN_TYPES.Text: + return parseText(); + case TOKEN_TYPES.OpenStatement: + return parseJinjaStatement(); + case TOKEN_TYPES.OpenExpression: + return parseJinjaExpression(); + default: + throw new SyntaxError(`Unexpected token type: ${tokens[current].type}`); + } + } + function not(...types) { + return current + types.length <= tokens.length && types.some((type, i) => type !== tokens[current + i].type); + } + function is(...types) { + return current + types.length <= tokens.length && types.every((type, i) => type === tokens[current + i].type); + } + function parseText() { + return new StringLiteral(expect(TOKEN_TYPES.Text, "Expected text token").value); + } + function parseJinjaStatement() { + expect(TOKEN_TYPES.OpenStatement, "Expected opening statement token"); + let result; + switch (tokens[current].type) { + case TOKEN_TYPES.Set: + ++current; + result = parseSetStatement(); + expect(TOKEN_TYPES.CloseStatement, "Expected closing statement token"); + break; + case TOKEN_TYPES.If: + ++current; + result = parseIfStatement(); + expect(TOKEN_TYPES.OpenStatement, "Expected {% token"); + expect(TOKEN_TYPES.EndIf, "Expected endif token"); + expect(TOKEN_TYPES.CloseStatement, "Expected %} token"); + break; + case TOKEN_TYPES.Macro: + ++current; + result = parseMacroStatement(); + expect(TOKEN_TYPES.OpenStatement, "Expected {% token"); + expect(TOKEN_TYPES.EndMacro, "Expected endmacro token"); + expect(TOKEN_TYPES.CloseStatement, "Expected %} token"); + break; + case TOKEN_TYPES.For: + ++current; + result = parseForStatement(); + expect(TOKEN_TYPES.OpenStatement, "Expected {% token"); + expect(TOKEN_TYPES.EndFor, "Expected endfor token"); + expect(TOKEN_TYPES.CloseStatement, "Expected %} token"); + break; + default: + throw new SyntaxError(`Unknown statement type: ${tokens[current].type}`); + } + return result; + } + function parseJinjaExpression() { + expect(TOKEN_TYPES.OpenExpression, "Expected opening expression token"); + const result = parseExpression(); + expect(TOKEN_TYPES.CloseExpression, "Expected closing expression token"); + return result; + } + function parseSetStatement() { + const left = parseExpression(); + if (is(TOKEN_TYPES.Equals)) { + ++current; + const value = parseSetStatement(); + return new SetStatement(left, value); + } + return left; + } + function parseIfStatement() { + const test = parseExpression(); + expect(TOKEN_TYPES.CloseStatement, "Expected closing statement token"); + const body = []; + const alternate = []; + while (!(tokens[current]?.type === TOKEN_TYPES.OpenStatement && (tokens[current + 1]?.type === TOKEN_TYPES.ElseIf || tokens[current + 1]?.type === TOKEN_TYPES.Else || tokens[current + 1]?.type === TOKEN_TYPES.EndIf))) { + body.push(parseAny()); + } + if (tokens[current]?.type === TOKEN_TYPES.OpenStatement && tokens[current + 1]?.type !== TOKEN_TYPES.EndIf) { + ++current; + if (is(TOKEN_TYPES.ElseIf)) { + expect(TOKEN_TYPES.ElseIf, "Expected elseif token"); + alternate.push(parseIfStatement()); + } else { + expect(TOKEN_TYPES.Else, "Expected else token"); + expect(TOKEN_TYPES.CloseStatement, "Expected closing statement token"); + while (!(tokens[current]?.type === TOKEN_TYPES.OpenStatement && tokens[current + 1]?.type === TOKEN_TYPES.EndIf)) { + alternate.push(parseAny()); + } + } + } + return new If(test, body, alternate); + } + function parseMacroStatement() { + const name = parsePrimaryExpression(); + if (name.type !== "Identifier") { + throw new SyntaxError(`Expected identifier following macro statement`); + } + const args = parseArgs(); + expect(TOKEN_TYPES.CloseStatement, "Expected closing statement token"); + const body = []; + while (not(TOKEN_TYPES.OpenStatement, TOKEN_TYPES.EndMacro)) { + body.push(parseAny()); + } + return new Macro(name, args, body); + } + function parseExpressionSequence(primary = false) { + const fn = primary ? parsePrimaryExpression : parseExpression; + const expressions = [fn()]; + const isTuple = is(TOKEN_TYPES.Comma); + while (isTuple) { + ++current; + expressions.push(fn()); + if (!is(TOKEN_TYPES.Comma)) { + break; + } + } + return isTuple ? new TupleLiteral(expressions) : expressions[0]; + } + function parseForStatement() { + const loopVariable = parseExpressionSequence(true); + if (!(loopVariable instanceof Identifier || loopVariable instanceof TupleLiteral)) { + throw new SyntaxError(`Expected identifier/tuple for the loop variable, got ${loopVariable.type} instead`); + } + expect(TOKEN_TYPES.In, "Expected `in` keyword following loop variable"); + const iterable = parseExpression(); + expect(TOKEN_TYPES.CloseStatement, "Expected closing statement token"); + const body = []; + while (not(TOKEN_TYPES.OpenStatement, TOKEN_TYPES.EndFor) && not(TOKEN_TYPES.OpenStatement, TOKEN_TYPES.Else)) { + body.push(parseAny()); + } + const alternative = []; + if (is(TOKEN_TYPES.OpenStatement, TOKEN_TYPES.Else)) { + ++current; + ++current; + expect(TOKEN_TYPES.CloseStatement, "Expected closing statement token"); + while (not(TOKEN_TYPES.OpenStatement, TOKEN_TYPES.EndFor)) { + alternative.push(parseAny()); + } + } + return new For(loopVariable, iterable, body, alternative); + } + function parseExpression() { + return parseIfExpression(); + } + function parseIfExpression() { + const a = parseLogicalOrExpression(); + if (is(TOKEN_TYPES.If)) { + ++current; + const predicate = parseLogicalOrExpression(); + if (is(TOKEN_TYPES.Else)) { + ++current; + const b = parseLogicalOrExpression(); + return new If(predicate, [a], [b]); + } else { + return new SelectExpression(a, predicate); + } + } + return a; + } + function parseLogicalOrExpression() { + let left = parseLogicalAndExpression(); + while (is(TOKEN_TYPES.Or)) { + const operator = tokens[current]; + ++current; + const right = parseLogicalAndExpression(); + left = new BinaryExpression(operator, left, right); + } + return left; + } + function parseLogicalAndExpression() { + let left = parseLogicalNegationExpression(); + while (is(TOKEN_TYPES.And)) { + const operator = tokens[current]; + ++current; + const right = parseLogicalNegationExpression(); + left = new BinaryExpression(operator, left, right); + } + return left; + } + function parseLogicalNegationExpression() { + let right; + while (is(TOKEN_TYPES.Not)) { + const operator = tokens[current]; + ++current; + const arg = parseLogicalNegationExpression(); + right = new UnaryExpression(operator, arg); + } + return right ?? parseComparisonExpression(); + } + function parseComparisonExpression() { + let left = parseAdditiveExpression(); + while (is(TOKEN_TYPES.ComparisonBinaryOperator) || is(TOKEN_TYPES.In) || is(TOKEN_TYPES.NotIn)) { + const operator = tokens[current]; + ++current; + const right = parseAdditiveExpression(); + left = new BinaryExpression(operator, left, right); + } + return left; + } + function parseAdditiveExpression() { + let left = parseMultiplicativeExpression(); + while (is(TOKEN_TYPES.AdditiveBinaryOperator)) { + const operator = tokens[current]; + ++current; + const right = parseMultiplicativeExpression(); + left = new BinaryExpression(operator, left, right); + } + return left; + } + function parseCallMemberExpression() { + const member = parseMemberExpression(); + if (is(TOKEN_TYPES.OpenParen)) { + return parseCallExpression(member); + } + return member; + } + function parseCallExpression(callee) { + let callExpression = new CallExpression(callee, parseArgs()); + if (is(TOKEN_TYPES.OpenParen)) { + callExpression = parseCallExpression(callExpression); + } + return callExpression; + } + function parseArgs() { + expect(TOKEN_TYPES.OpenParen, "Expected opening parenthesis for arguments list"); + const args = parseArgumentsList(); + expect(TOKEN_TYPES.CloseParen, "Expected closing parenthesis for arguments list"); + return args; + } + function parseArgumentsList() { + const args = []; + while (!is(TOKEN_TYPES.CloseParen)) { + let argument = parseExpression(); + if (is(TOKEN_TYPES.Equals)) { + ++current; + if (!(argument instanceof Identifier)) { + throw new SyntaxError(`Expected identifier for keyword argument`); + } + const value = parseExpression(); + argument = new KeywordArgumentExpression(argument, value); + } + args.push(argument); + if (is(TOKEN_TYPES.Comma)) { + ++current; + } + } + return args; + } + function parseMemberExpressionArgumentsList() { + const slices = []; + let isSlice = false; + while (!is(TOKEN_TYPES.CloseSquareBracket)) { + if (is(TOKEN_TYPES.Colon)) { + slices.push(void 0); + ++current; + isSlice = true; + } else { + slices.push(parseExpression()); + if (is(TOKEN_TYPES.Colon)) { + ++current; + isSlice = true; + } + } + } + if (slices.length === 0) { + throw new SyntaxError(`Expected at least one argument for member/slice expression`); + } + if (isSlice) { + if (slices.length > 3) { + throw new SyntaxError(`Expected 0-3 arguments for slice expression`); + } + return new SliceExpression(...slices); + } + return slices[0]; + } + function parseMemberExpression() { + let object = parsePrimaryExpression(); + while (is(TOKEN_TYPES.Dot) || is(TOKEN_TYPES.OpenSquareBracket)) { + const operator = tokens[current]; + ++current; + let property; + const computed = operator.type !== TOKEN_TYPES.Dot; + if (computed) { + property = parseMemberExpressionArgumentsList(); + expect(TOKEN_TYPES.CloseSquareBracket, "Expected closing square bracket"); + } else { + property = parsePrimaryExpression(); + if (property.type !== "Identifier") { + throw new SyntaxError(`Expected identifier following dot operator`); + } + } + object = new MemberExpression(object, property, computed); + } + return object; + } + function parseMultiplicativeExpression() { + let left = parseTestExpression(); + while (is(TOKEN_TYPES.MultiplicativeBinaryOperator)) { + const operator = tokens[current]; + ++current; + const right = parseTestExpression(); + left = new BinaryExpression(operator, left, right); + } + return left; + } + function parseTestExpression() { + let operand = parseFilterExpression(); + while (is(TOKEN_TYPES.Is)) { + ++current; + const negate = is(TOKEN_TYPES.Not); + if (negate) { + ++current; + } + let filter = parsePrimaryExpression(); + if (filter instanceof BooleanLiteral) { + filter = new Identifier(filter.value.toString()); + } + if (!(filter instanceof Identifier)) { + throw new SyntaxError(`Expected identifier for the test`); + } + operand = new TestExpression(operand, negate, filter); + } + return operand; + } + function parseFilterExpression() { + let operand = parseCallMemberExpression(); + while (is(TOKEN_TYPES.Pipe)) { + ++current; + let filter = parsePrimaryExpression(); + if (!(filter instanceof Identifier)) { + throw new SyntaxError(`Expected identifier for the filter`); + } + if (is(TOKEN_TYPES.OpenParen)) { + filter = parseCallExpression(filter); + } + operand = new FilterExpression(operand, filter); + } + return operand; + } + function parsePrimaryExpression() { + const token = tokens[current]; + switch (token.type) { + case TOKEN_TYPES.NumericLiteral: + ++current; + return new NumericLiteral(Number(token.value)); + case TOKEN_TYPES.StringLiteral: + ++current; + return new StringLiteral(token.value); + case TOKEN_TYPES.BooleanLiteral: + ++current; + return new BooleanLiteral(token.value.toLowerCase() === "true"); + case TOKEN_TYPES.Identifier: + ++current; + return new Identifier(token.value); + case TOKEN_TYPES.OpenParen: { + ++current; + const expression = parseExpressionSequence(); + if (tokens[current].type !== TOKEN_TYPES.CloseParen) { + throw new SyntaxError(`Expected closing parenthesis, got ${tokens[current].type} instead`); + } + ++current; + return expression; + } + case TOKEN_TYPES.OpenSquareBracket: { + ++current; + const values = []; + while (!is(TOKEN_TYPES.CloseSquareBracket)) { + values.push(parseExpression()); + if (is(TOKEN_TYPES.Comma)) { + ++current; + } + } + ++current; + return new ArrayLiteral(values); + } + case TOKEN_TYPES.OpenCurlyBracket: { + ++current; + const values = /* @__PURE__ */ new Map(); + while (!is(TOKEN_TYPES.CloseCurlyBracket)) { + const key = parseExpression(); + expect(TOKEN_TYPES.Colon, "Expected colon between key and value in object literal"); + const value = parseExpression(); + values.set(key, value); + if (is(TOKEN_TYPES.Comma)) { + ++current; + } + } + ++current; + return new ObjectLiteral(values); + } + default: + throw new SyntaxError(`Unexpected token: ${token.type}`); + } + } + while (current < tokens.length) { + program.body.push(parseAny()); + } + return program; +} + +// src/utils.ts +function range(start, stop, step = 1) { + if (stop === void 0) { + stop = start; + start = 0; + } + const result = []; + for (let i = start; i < stop; i += step) { + result.push(i); + } + return result; +} +function slice(array, start, stop, step = 1) { + const direction = Math.sign(step); + if (direction >= 0) { + start = (start ??= 0) < 0 ? Math.max(array.length + start, 0) : Math.min(start, array.length); + stop = (stop ??= array.length) < 0 ? Math.max(array.length + stop, 0) : Math.min(stop, array.length); + } else { + start = (start ??= array.length - 1) < 0 ? Math.max(array.length + start, -1) : Math.min(start, array.length - 1); + stop = (stop ??= -1) < -1 ? Math.max(array.length + stop, -1) : Math.min(stop, array.length - 1); + } + const result = []; + for (let i = start; direction * i < direction * stop; i += step) { + result.push(array[i]); + } + return result; +} +function titleCase(value) { + return value.replace(/\b\w/g, (c) => c.toUpperCase()); +} + +// src/runtime.ts +var RuntimeValue = class { + type = "RuntimeValue"; + value; + /** + * A collection of built-in functions for this type. + */ + builtins = /* @__PURE__ */ new Map(); + /** + * Creates a new RuntimeValue. + */ + constructor(value = void 0) { + this.value = value; + } + /** + * Determines truthiness or falsiness of the runtime value. + * This function should be overridden by subclasses if it has custom truthiness criteria. + * @returns {BooleanValue} BooleanValue(true) if the value is truthy, BooleanValue(false) otherwise. + */ + __bool__() { + return new BooleanValue(!!this.value); + } +}; +var NumericValue = class extends RuntimeValue { + type = "NumericValue"; +}; +var StringValue = class extends RuntimeValue { + type = "StringValue"; + builtins = /* @__PURE__ */ new Map([ + [ + "upper", + new FunctionValue(() => { + return new StringValue(this.value.toUpperCase()); + }) + ], + [ + "lower", + new FunctionValue(() => { + return new StringValue(this.value.toLowerCase()); + }) + ], + [ + "strip", + new FunctionValue(() => { + return new StringValue(this.value.trim()); + }) + ], + [ + "title", + new FunctionValue(() => { + return new StringValue(titleCase(this.value)); + }) + ], + ["length", new NumericValue(this.value.length)] + ]); +}; +var BooleanValue = class extends RuntimeValue { + type = "BooleanValue"; +}; +var ObjectValue = class extends RuntimeValue { + type = "ObjectValue"; + /** + * NOTE: necessary to override since all JavaScript arrays are considered truthy, + * while only non-empty Python arrays are consider truthy. + * + * e.g., + * - JavaScript: {} && 5 -> 5 + * - Python: {} and 5 -> {} + */ + __bool__() { + return new BooleanValue(this.value.size > 0); + } + builtins = /* @__PURE__ */ new Map([ + [ + "get", + new FunctionValue(([key, defaultValue]) => { + if (!(key instanceof StringValue)) { + throw new Error(`Object key must be a string: got ${key.type}`); + } + return this.value.get(key.value) ?? defaultValue ?? new NullValue(); + }) + ], + [ + "items", + new FunctionValue(() => { + return new ArrayValue( + Array.from(this.value.entries()).map(([key, value]) => new ArrayValue([new StringValue(key), value])) + ); + }) + ] + ]); +}; +var KeywordArgumentsValue = class extends ObjectValue { + type = "KeywordArgumentsValue"; +}; +var ArrayValue = class extends RuntimeValue { + type = "ArrayValue"; + builtins = /* @__PURE__ */ new Map([["length", new NumericValue(this.value.length)]]); + /** + * NOTE: necessary to override since all JavaScript arrays are considered truthy, + * while only non-empty Python arrays are consider truthy. + * + * e.g., + * - JavaScript: [] && 5 -> 5 + * - Python: [] and 5 -> [] + */ + __bool__() { + return new BooleanValue(this.value.length > 0); + } +}; +var TupleValue = class extends ArrayValue { + type = "TupleValue"; +}; +var FunctionValue = class extends RuntimeValue { + type = "FunctionValue"; +}; +var NullValue = class extends RuntimeValue { + type = "NullValue"; +}; +var UndefinedValue = class extends RuntimeValue { + type = "UndefinedValue"; +}; +var Environment = class { + constructor(parent) { + this.parent = parent; + } + /** + * The variables declared in this environment. + */ + variables = /* @__PURE__ */ new Map([ + [ + "namespace", + new FunctionValue((args) => { + if (args.length === 0) { + return new ObjectValue(/* @__PURE__ */ new Map()); + } + if (args.length !== 1 || !(args[0] instanceof ObjectValue)) { + throw new Error("`namespace` expects either zero arguments or a single object argument"); + } + return args[0]; + }) + ] + ]); + /** + * The tests available in this environment. + */ + tests = /* @__PURE__ */ new Map([ + ["boolean", (operand) => operand.type === "BooleanValue"], + ["callable", (operand) => operand instanceof FunctionValue], + [ + "odd", + (operand) => { + if (operand.type !== "NumericValue") { + throw new Error(`Cannot apply test "odd" to type: ${operand.type}`); + } + return operand.value % 2 !== 0; + } + ], + [ + "even", + (operand) => { + if (operand.type !== "NumericValue") { + throw new Error(`Cannot apply test "even" to type: ${operand.type}`); + } + return operand.value % 2 === 0; + } + ], + ["false", (operand) => operand.type === "BooleanValue" && !operand.value], + ["true", (operand) => operand.type === "BooleanValue" && operand.value], + ["string", (operand) => operand.type === "StringValue"], + ["number", (operand) => operand.type === "NumericValue"], + ["integer", (operand) => operand.type === "NumericValue" && Number.isInteger(operand.value)], + ["iterable", (operand) => operand instanceof ArrayValue || operand instanceof StringValue], + [ + "lower", + (operand) => { + const str = operand.value; + return operand.type === "StringValue" && str === str.toLowerCase(); + } + ], + [ + "upper", + (operand) => { + const str = operand.value; + return operand.type === "StringValue" && str === str.toUpperCase(); + } + ], + ["none", (operand) => operand.type === "NullValue"], + ["defined", (operand) => operand.type !== "UndefinedValue"], + ["undefined", (operand) => operand.type === "UndefinedValue"], + ["equalto", (a, b) => a.value === b.value], + ["eq", (a, b) => a.value === b.value] + ]); + /** + * Set the value of a variable in the current environment. + */ + set(name, value) { + return this.declareVariable(name, convertToRuntimeValues(value)); + } + declareVariable(name, value) { + if (this.variables.has(name)) { + throw new SyntaxError(`Variable already declared: ${name}`); + } + this.variables.set(name, value); + return value; + } + // private assignVariable(name: string, value: AnyRuntimeValue): AnyRuntimeValue { + // const env = this.resolve(name); + // env.variables.set(name, value); + // return value; + // } + /** + * Set variable in the current scope. + * See https://jinja.palletsprojects.com/en/3.0.x/templates/#assignments for more information. + */ + setVariable(name, value) { + this.variables.set(name, value); + return value; + } + /** + * Resolve the environment in which the variable is declared. + * @param {string} name The name of the variable. + * @returns {Environment} The environment in which the variable is declared. + */ + resolve(name) { + if (this.variables.has(name)) { + return this; + } + if (this.parent) { + return this.parent.resolve(name); + } + throw new Error(`Unknown variable: ${name}`); + } + lookupVariable(name) { + try { + return this.resolve(name).variables.get(name) ?? new UndefinedValue(); + } catch { + return new UndefinedValue(); + } + } +}; +var Interpreter = class { + global; + constructor(env) { + this.global = env ?? new Environment(); + } + /** + * Run the program. + */ + run(program) { + return this.evaluate(program, this.global); + } + /** + * Evaluates expressions following the binary operation type. + */ + evaluateBinaryExpression(node, environment) { + const left = this.evaluate(node.left, environment); + switch (node.operator.value) { + case "and": + return left.__bool__().value ? this.evaluate(node.right, environment) : left; + case "or": + return left.__bool__().value ? left : this.evaluate(node.right, environment); + } + const right = this.evaluate(node.right, environment); + switch (node.operator.value) { + case "==": + return new BooleanValue(left.value == right.value); + case "!=": + return new BooleanValue(left.value != right.value); + } + if (left instanceof UndefinedValue || right instanceof UndefinedValue) { + throw new Error("Cannot perform operation on undefined values"); + } else if (left instanceof NullValue || right instanceof NullValue) { + throw new Error("Cannot perform operation on null values"); + } else if (left instanceof NumericValue && right instanceof NumericValue) { + switch (node.operator.value) { + case "+": + return new NumericValue(left.value + right.value); + case "-": + return new NumericValue(left.value - right.value); + case "*": + return new NumericValue(left.value * right.value); + case "/": + return new NumericValue(left.value / right.value); + case "%": + return new NumericValue(left.value % right.value); + case "<": + return new BooleanValue(left.value < right.value); + case ">": + return new BooleanValue(left.value > right.value); + case ">=": + return new BooleanValue(left.value >= right.value); + case "<=": + return new BooleanValue(left.value <= right.value); + } + } else if (left instanceof ArrayValue && right instanceof ArrayValue) { + switch (node.operator.value) { + case "+": + return new ArrayValue(left.value.concat(right.value)); + } + } else if (right instanceof ArrayValue) { + const member = right.value.find((x) => x.value === left.value) !== void 0; + switch (node.operator.value) { + case "in": + return new BooleanValue(member); + case "not in": + return new BooleanValue(!member); + } + } + if (left instanceof StringValue || right instanceof StringValue) { + switch (node.operator.value) { + case "+": + return new StringValue(left.value.toString() + right.value.toString()); + } + } + if (left instanceof StringValue && right instanceof StringValue) { + switch (node.operator.value) { + case "in": + return new BooleanValue(right.value.includes(left.value)); + case "not in": + return new BooleanValue(!right.value.includes(left.value)); + } + } + if (left instanceof StringValue && right instanceof ObjectValue) { + switch (node.operator.value) { + case "in": + return new BooleanValue(right.value.has(left.value)); + case "not in": + return new BooleanValue(!right.value.has(left.value)); + } + } + throw new SyntaxError(`Unknown operator "${node.operator.value}" between ${left.type} and ${right.type}`); + } + evaluateArguments(args, environment) { + const positionalArguments = []; + const keywordArguments = /* @__PURE__ */ new Map(); + for (const argument of args) { + if (argument.type === "KeywordArgumentExpression") { + const kwarg = argument; + keywordArguments.set(kwarg.key.value, this.evaluate(kwarg.value, environment)); + } else { + if (keywordArguments.size > 0) { + throw new Error("Positional arguments must come before keyword arguments"); + } + positionalArguments.push(this.evaluate(argument, environment)); + } + } + return [positionalArguments, keywordArguments]; + } + /** + * Evaluates expressions following the filter operation type. + */ + evaluateFilterExpression(node, environment) { + const operand = this.evaluate(node.operand, environment); + if (node.filter.type === "Identifier") { + const filter = node.filter; + if (filter.value === "tojson") { + return new StringValue(toJSON(operand)); + } + if (operand instanceof ArrayValue) { + switch (filter.value) { + case "list": + return operand; + case "first": + return operand.value[0]; + case "last": + return operand.value[operand.value.length - 1]; + case "length": + return new NumericValue(operand.value.length); + case "reverse": + return new ArrayValue(operand.value.reverse()); + case "sort": + return new ArrayValue( + operand.value.sort((a, b) => { + if (a.type !== b.type) { + throw new Error(`Cannot compare different types: ${a.type} and ${b.type}`); + } + switch (a.type) { + case "NumericValue": + return a.value - b.value; + case "StringValue": + return a.value.localeCompare(b.value); + default: + throw new Error(`Cannot compare type: ${a.type}`); + } + }) + ); + default: + throw new Error(`Unknown ArrayValue filter: ${filter.value}`); + } + } else if (operand instanceof StringValue) { + switch (filter.value) { + case "length": + return new NumericValue(operand.value.length); + case "upper": + return new StringValue(operand.value.toUpperCase()); + case "lower": + return new StringValue(operand.value.toLowerCase()); + case "title": + return new StringValue(titleCase(operand.value)); + case "capitalize": + return new StringValue(operand.value.charAt(0).toUpperCase() + operand.value.slice(1)); + case "trim": + return new StringValue(operand.value.trim()); + case "indent": + return new StringValue( + operand.value.split("\n").map( + (x, i) => ( + // By default, don't indent the first line or empty lines + i === 0 || x.length === 0 ? x : " " + x + ) + ).join("\n") + ); + case "string": + return operand; + default: + throw new Error(`Unknown StringValue filter: ${filter.value}`); + } + } else if (operand instanceof NumericValue) { + switch (filter.value) { + case "abs": + return new NumericValue(Math.abs(operand.value)); + default: + throw new Error(`Unknown NumericValue filter: ${filter.value}`); + } + } else if (operand instanceof ObjectValue) { + switch (filter.value) { + case "items": + return new ArrayValue( + Array.from(operand.value.entries()).map(([key, value]) => new ArrayValue([new StringValue(key), value])) + ); + case "length": + return new NumericValue(operand.value.size); + default: + throw new Error(`Unknown ObjectValue filter: ${filter.value}`); + } + } + throw new Error(`Cannot apply filter "${filter.value}" to type: ${operand.type}`); + } else if (node.filter.type === "CallExpression") { + const filter = node.filter; + if (filter.callee.type !== "Identifier") { + throw new Error(`Unknown filter: ${filter.callee.type}`); + } + const filterName = filter.callee.value; + if (filterName === "tojson") { + const [, kwargs] = this.evaluateArguments(filter.args, environment); + const indent = kwargs.get("indent") ?? new NullValue(); + if (!(indent instanceof NumericValue || indent instanceof NullValue)) { + throw new Error("If set, indent must be a number"); + } + return new StringValue(toJSON(operand, indent.value)); + } + if (operand instanceof ArrayValue) { + switch (filterName) { + case "selectattr": { + if (operand.value.some((x) => !(x instanceof ObjectValue))) { + throw new Error("`selectattr` can only be applied to array of objects"); + } + if (filter.args.some((x) => x.type !== "StringLiteral")) { + throw new Error("arguments of `selectattr` must be strings"); + } + const [attr, testName, value] = filter.args.map((x) => this.evaluate(x, environment)); + let testFunction; + if (testName) { + const test = environment.tests.get(testName.value); + if (!test) { + throw new Error(`Unknown test: ${testName.value}`); + } + testFunction = test; + } else { + testFunction = (...x) => x[0].__bool__().value; + } + const filtered = operand.value.filter((item) => { + const a = item.value.get(attr.value); + if (a) { + return testFunction(a, value); + } + return false; + }); + return new ArrayValue(filtered); + } + case "map": { + const [, kwargs] = this.evaluateArguments(filter.args, environment); + if (kwargs.has("attribute")) { + const attr = kwargs.get("attribute"); + if (!(attr instanceof StringValue)) { + throw new Error("attribute must be a string"); + } + const defaultValue = kwargs.get("default"); + const mapped = operand.value.map((item) => { + if (!(item instanceof ObjectValue)) { + throw new Error("items in map must be an object"); + } + return item.value.get(attr.value) ?? defaultValue ?? new UndefinedValue(); + }); + return new ArrayValue(mapped); + } else { + throw new Error("`map` expressions without `attribute` set are not currently supported."); + } + } + } + throw new Error(`Unknown ArrayValue filter: ${filterName}`); + } else if (operand instanceof StringValue) { + switch (filterName) { + case "indent": { + const [args, kwargs] = this.evaluateArguments(filter.args, environment); + const width = args.at(0) ?? kwargs.get("width") ?? new NumericValue(4); + if (!(width instanceof NumericValue)) { + throw new Error("width must be a number"); + } + const first = args.at(1) ?? kwargs.get("first") ?? new BooleanValue(false); + const blank = args.at(2) ?? kwargs.get("blank") ?? new BooleanValue(false); + const lines = operand.value.split("\n"); + const indent = " ".repeat(width.value); + const indented = lines.map( + (x, i) => !first.value && i === 0 || !blank.value && x.length === 0 ? x : indent + x + ); + return new StringValue(indented.join("\n")); + } + } + throw new Error(`Unknown StringValue filter: ${filterName}`); + } else { + throw new Error(`Cannot apply filter "${filterName}" to type: ${operand.type}`); + } + } + throw new Error(`Unknown filter: ${node.filter.type}`); + } + /** + * Evaluates expressions following the test operation type. + */ + evaluateTestExpression(node, environment) { + const operand = this.evaluate(node.operand, environment); + const test = environment.tests.get(node.test.value); + if (!test) { + throw new Error(`Unknown test: ${node.test.value}`); + } + const result = test(operand); + return new BooleanValue(node.negate ? !result : result); + } + /** + * Evaluates expressions following the unary operation type. + */ + evaluateUnaryExpression(node, environment) { + const argument = this.evaluate(node.argument, environment); + switch (node.operator.value) { + case "not": + return new BooleanValue(!argument.value); + default: + throw new SyntaxError(`Unknown operator: ${node.operator.value}`); + } + } + evalProgram(program, environment) { + return this.evaluateBlock(program.body, environment); + } + evaluateBlock(statements, environment) { + let result = ""; + for (const statement of statements) { + const lastEvaluated = this.evaluate(statement, environment); + if (lastEvaluated.type !== "NullValue" && lastEvaluated.type !== "UndefinedValue") { + result += lastEvaluated.value; + } + } + return new StringValue(result); + } + evaluateIdentifier(node, environment) { + return environment.lookupVariable(node.value); + } + evaluateCallExpression(expr, environment) { + const [args, kwargs] = this.evaluateArguments(expr.args, environment); + if (kwargs.size > 0) { + args.push(new KeywordArgumentsValue(kwargs)); + } + const fn = this.evaluate(expr.callee, environment); + if (fn.type !== "FunctionValue") { + throw new Error(`Cannot call something that is not a function: got ${fn.type}`); + } + return fn.value(args, environment); + } + evaluateSliceExpression(object, expr, environment) { + if (!(object instanceof ArrayValue || object instanceof StringValue)) { + throw new Error("Slice object must be an array or string"); + } + const start = this.evaluate(expr.start, environment); + const stop = this.evaluate(expr.stop, environment); + const step = this.evaluate(expr.step, environment); + if (!(start instanceof NumericValue || start instanceof UndefinedValue)) { + throw new Error("Slice start must be numeric or undefined"); + } + if (!(stop instanceof NumericValue || stop instanceof UndefinedValue)) { + throw new Error("Slice stop must be numeric or undefined"); + } + if (!(step instanceof NumericValue || step instanceof UndefinedValue)) { + throw new Error("Slice step must be numeric or undefined"); + } + if (object instanceof ArrayValue) { + return new ArrayValue(slice(object.value, start.value, stop.value, step.value)); + } else { + return new StringValue(slice(Array.from(object.value), start.value, stop.value, step.value).join("")); + } + } + evaluateMemberExpression(expr, environment) { + const object = this.evaluate(expr.object, environment); + let property; + if (expr.computed) { + if (expr.property.type === "SliceExpression") { + return this.evaluateSliceExpression(object, expr.property, environment); + } else { + property = this.evaluate(expr.property, environment); + } + } else { + property = new StringValue(expr.property.value); + } + let value; + if (object instanceof ObjectValue) { + if (!(property instanceof StringValue)) { + throw new Error(`Cannot access property with non-string: got ${property.type}`); + } + value = object.value.get(property.value) ?? object.builtins.get(property.value); + } else if (object instanceof ArrayValue || object instanceof StringValue) { + if (property instanceof NumericValue) { + value = object.value.at(property.value); + if (object instanceof StringValue) { + value = new StringValue(object.value.at(property.value)); + } + } else if (property instanceof StringValue) { + value = object.builtins.get(property.value); + } else { + throw new Error(`Cannot access property with non-string/non-number: got ${property.type}`); + } + } else { + if (!(property instanceof StringValue)) { + throw new Error(`Cannot access property with non-string: got ${property.type}`); + } + value = object.builtins.get(property.value); + } + return value instanceof RuntimeValue ? value : new UndefinedValue(); + } + evaluateSet(node, environment) { + const rhs = this.evaluate(node.value, environment); + if (node.assignee.type === "Identifier") { + const variableName = node.assignee.value; + environment.setVariable(variableName, rhs); + } else if (node.assignee.type === "MemberExpression") { + const member = node.assignee; + const object = this.evaluate(member.object, environment); + if (!(object instanceof ObjectValue)) { + throw new Error("Cannot assign to member of non-object"); + } + if (member.property.type !== "Identifier") { + throw new Error("Cannot assign to member with non-identifier property"); + } + object.value.set(member.property.value, rhs); + } else { + throw new Error(`Invalid LHS inside assignment expression: ${JSON.stringify(node.assignee)}`); + } + return new NullValue(); + } + evaluateIf(node, environment) { + const test = this.evaluate(node.test, environment); + return this.evaluateBlock(test.__bool__().value ? node.body : node.alternate, environment); + } + evaluateFor(node, environment) { + const scope = new Environment(environment); + let test, iterable; + if (node.iterable.type === "SelectExpression") { + const select = node.iterable; + iterable = this.evaluate(select.iterable, scope); + test = select.test; + } else { + iterable = this.evaluate(node.iterable, scope); + } + if (!(iterable instanceof ArrayValue)) { + throw new Error(`Expected iterable type in for loop: got ${iterable.type}`); + } + const items = []; + const scopeUpdateFunctions = []; + for (let i = 0; i < iterable.value.length; ++i) { + const loopScope = new Environment(scope); + const current = iterable.value[i]; + let scopeUpdateFunction; + if (node.loopvar.type === "Identifier") { + scopeUpdateFunction = (scope2) => scope2.setVariable(node.loopvar.value, current); + } else if (node.loopvar.type === "TupleLiteral") { + const loopvar = node.loopvar; + if (current.type !== "ArrayValue") { + throw new Error(`Cannot unpack non-iterable type: ${current.type}`); + } + const c = current; + if (loopvar.value.length !== c.value.length) { + throw new Error(`Too ${loopvar.value.length > c.value.length ? "few" : "many"} items to unpack`); + } + scopeUpdateFunction = (scope2) => { + for (let j = 0; j < loopvar.value.length; ++j) { + if (loopvar.value[j].type !== "Identifier") { + throw new Error(`Cannot unpack non-identifier type: ${loopvar.value[j].type}`); + } + scope2.setVariable(loopvar.value[j].value, c.value[j]); + } + }; + } else { + throw new Error(`Invalid loop variable(s): ${node.loopvar.type}`); + } + if (test) { + scopeUpdateFunction(loopScope); + const testValue = this.evaluate(test, loopScope); + if (!testValue.__bool__().value) { + continue; + } + } + items.push(current); + scopeUpdateFunctions.push(scopeUpdateFunction); + } + let result = ""; + let noIteration = true; + for (let i = 0; i < items.length; ++i) { + const loop = /* @__PURE__ */ new Map([ + ["index", new NumericValue(i + 1)], + ["index0", new NumericValue(i)], + ["revindex", new NumericValue(items.length - i)], + ["revindex0", new NumericValue(items.length - i - 1)], + ["first", new BooleanValue(i === 0)], + ["last", new BooleanValue(i === items.length - 1)], + ["length", new NumericValue(items.length)], + ["previtem", i > 0 ? items[i - 1] : new UndefinedValue()], + ["nextitem", i < items.length - 1 ? items[i + 1] : new UndefinedValue()] + ]); + scope.setVariable("loop", new ObjectValue(loop)); + scopeUpdateFunctions[i](scope); + const evaluated = this.evaluateBlock(node.body, scope); + result += evaluated.value; + noIteration = false; + } + if (noIteration) { + const defaultEvaluated = this.evaluateBlock(node.defaultBlock, scope); + result += defaultEvaluated.value; + } + return new StringValue(result); + } + /** + * See https://jinja.palletsprojects.com/en/3.1.x/templates/#macros for more information. + */ + evaluateMacro(node, environment) { + environment.setVariable( + node.name.value, + new FunctionValue((args, scope) => { + const macroScope = new Environment(scope); + args = args.slice(); + let kwargs; + if (args.at(-1)?.type === "KeywordArgumentsValue") { + kwargs = args.pop(); + } + for (let i = 0; i < node.args.length; ++i) { + const nodeArg = node.args[i]; + const passedArg = args[i]; + if (nodeArg.type === "Identifier") { + const identifier = nodeArg; + if (!passedArg) { + throw new Error(`Missing positional argument: ${identifier.value}`); + } + macroScope.setVariable(identifier.value, passedArg); + } else if (nodeArg.type === "KeywordArgumentExpression") { + const kwarg = nodeArg; + const value = passedArg ?? // Try positional arguments first + kwargs?.value.get(kwarg.key.value) ?? // Look in user-passed kwargs + this.evaluate(kwarg.value, macroScope); + macroScope.setVariable(kwarg.key.value, value); + } else { + throw new Error(`Unknown argument type: ${nodeArg.type}`); + } + } + return this.evaluateBlock(node.body, macroScope); + }) + ); + return new NullValue(); + } + evaluate(statement, environment) { + if (statement === void 0) + return new UndefinedValue(); + switch (statement.type) { + case "Program": + return this.evalProgram(statement, environment); + case "Set": + return this.evaluateSet(statement, environment); + case "If": + return this.evaluateIf(statement, environment); + case "For": + return this.evaluateFor(statement, environment); + case "Macro": + return this.evaluateMacro(statement, environment); + case "NumericLiteral": + return new NumericValue(Number(statement.value)); + case "StringLiteral": + return new StringValue(statement.value); + case "BooleanLiteral": + return new BooleanValue(statement.value); + case "ArrayLiteral": + return new ArrayValue(statement.value.map((x) => this.evaluate(x, environment))); + case "TupleLiteral": + return new TupleValue(statement.value.map((x) => this.evaluate(x, environment))); + case "ObjectLiteral": { + const mapping = /* @__PURE__ */ new Map(); + for (const [key, value] of statement.value) { + const evaluatedKey = this.evaluate(key, environment); + if (!(evaluatedKey instanceof StringValue)) { + throw new Error(`Object keys must be strings: got ${evaluatedKey.type}`); + } + mapping.set(evaluatedKey.value, this.evaluate(value, environment)); + } + return new ObjectValue(mapping); + } + case "Identifier": + return this.evaluateIdentifier(statement, environment); + case "CallExpression": + return this.evaluateCallExpression(statement, environment); + case "MemberExpression": + return this.evaluateMemberExpression(statement, environment); + case "UnaryExpression": + return this.evaluateUnaryExpression(statement, environment); + case "BinaryExpression": + return this.evaluateBinaryExpression(statement, environment); + case "FilterExpression": + return this.evaluateFilterExpression(statement, environment); + case "TestExpression": + return this.evaluateTestExpression(statement, environment); + default: + throw new SyntaxError(`Unknown node type: ${statement.type}`); + } + } +}; +function convertToRuntimeValues(input) { + switch (typeof input) { + case "number": + return new NumericValue(input); + case "string": + return new StringValue(input); + case "boolean": + return new BooleanValue(input); + case "undefined": + return new UndefinedValue(); + case "object": + if (input === null) { + return new NullValue(); + } else if (Array.isArray(input)) { + return new ArrayValue(input.map(convertToRuntimeValues)); + } else { + return new ObjectValue( + new Map(Object.entries(input).map(([key, value]) => [key, convertToRuntimeValues(value)])) + ); + } + case "function": + return new FunctionValue((args, _scope) => { + const result = input(...args.map((x) => x.value)) ?? null; + return convertToRuntimeValues(result); + }); + default: + throw new Error(`Cannot convert to runtime value: ${input}`); + } +} +function toJSON(input, indent, depth) { + const currentDepth = depth ?? 0; + switch (input.type) { + case "NullValue": + case "UndefinedValue": + return "null"; + case "NumericValue": + case "StringValue": + case "BooleanValue": + return JSON.stringify(input.value); + case "ArrayValue": + case "ObjectValue": { + const indentValue = indent ? " ".repeat(indent) : ""; + const basePadding = "\n" + indentValue.repeat(currentDepth); + const childrenPadding = basePadding + indentValue; + if (input.type === "ArrayValue") { + const core = input.value.map((x) => toJSON(x, indent, currentDepth + 1)); + return indent ? `[${childrenPadding}${core.join(`,${childrenPadding}`)}${basePadding}]` : `[${core.join(", ")}]`; + } else { + const core = Array.from(input.value.entries()).map(([key, value]) => { + const v = `"${key}": ${toJSON(value, indent, currentDepth + 1)}`; + return indent ? `${childrenPadding}${v}` : v; + }); + return indent ? `{${core.join(",")}${basePadding}}` : `{${core.join(", ")}}`; + } + } + default: + throw new Error(`Cannot convert to JSON: ${input.type}`); + } +} + +// src/index.ts +var Template = class { + parsed; + /** + * @param {string} template The template string + */ + constructor(template) { + const tokens = tokenize(template, { + lstrip_blocks: true, + trim_blocks: true + }); + this.parsed = parse(tokens); + } + render(items) { + const env = new Environment(); + env.set("false", false); + env.set("true", true); + env.set("raise_exception", (args) => { + throw new Error(args); + }); + env.set("range", range); + for (const [key, value] of Object.entries(items)) { + env.set(key, value); + } + const interpreter = new Interpreter(env); + const result = interpreter.run(this.parsed); + return result.value; + } +}; + + + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/backend-impl.js": +/*!******************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/backend-impl.js ***! + \******************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ registerBackend: () => (/* binding */ registerBackend), +/* harmony export */ resolveBackendAndExecutionProviders: () => (/* binding */ resolveBackendAndExecutionProviders) +/* harmony export */ }); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +const backends = new Map(); +const backendsSortedByPriority = []; +/** + * Register a backend. + * + * @param name - the name as a key to lookup as an execution provider. + * @param backend - the backend object. + * @param priority - an integer indicating the priority of the backend. Higher number means higher priority. if priority + * < 0, it will be considered as a 'beta' version and will not be used as a fallback backend by default. + * + * @ignore + */ +const registerBackend = (name, backend, priority) => { + if (backend && typeof backend.init === 'function' && typeof backend.createInferenceSessionHandler === 'function') { + const currentBackend = backends.get(name); + if (currentBackend === undefined) { + backends.set(name, { backend, priority }); + } + else if (currentBackend.priority > priority) { + // same name is already registered with a higher priority. skip registeration. + return; + } + else if (currentBackend.priority === priority) { + if (currentBackend.backend !== backend) { + throw new Error(`cannot register backend "${name}" using priority ${priority}`); + } + } + if (priority >= 0) { + const i = backendsSortedByPriority.indexOf(name); + if (i !== -1) { + backendsSortedByPriority.splice(i, 1); + } + for (let i = 0; i < backendsSortedByPriority.length; i++) { + if (backends.get(backendsSortedByPriority[i]).priority <= priority) { + backendsSortedByPriority.splice(i, 0, name); + return; + } + } + backendsSortedByPriority.push(name); + } + return; + } + throw new TypeError('not a valid backend'); +}; +/** + * Try to resolve and initialize a backend. + * + * @param backendName - the name of the backend. + * @returns the backend instance if resolved and initialized successfully, or an error message if failed. + */ +const tryResolveAndInitializeBackend = async (backendName) => { + const backendInfo = backends.get(backendName); + if (!backendInfo) { + return 'backend not found.'; + } + if (backendInfo.initialized) { + return backendInfo.backend; + } + else if (backendInfo.aborted) { + return backendInfo.error; + } + else { + const isInitializing = !!backendInfo.initPromise; + try { + if (!isInitializing) { + backendInfo.initPromise = backendInfo.backend.init(backendName); + } + await backendInfo.initPromise; + backendInfo.initialized = true; + return backendInfo.backend; + } + catch (e) { + if (!isInitializing) { + backendInfo.error = `${e}`; + backendInfo.aborted = true; + } + return backendInfo.error; + } + finally { + delete backendInfo.initPromise; + } + } +}; +/** + * Resolve execution providers from the specific session options. + * + * @param options - the session options object. + * @returns a promise that resolves to a tuple of an initialized backend instance and a session options object with + * filtered EP list. + * + * @ignore + */ +const resolveBackendAndExecutionProviders = async (options) => { + // extract backend hints from session options + const eps = options.executionProviders || []; + const backendHints = eps.map(i => typeof i === 'string' ? i : i.name); + const backendNames = backendHints.length === 0 ? backendsSortedByPriority : backendHints; + // try to resolve and initialize all requested backends + let backend; + const errors = []; + const availableBackendNames = new Set(); + for (const backendName of backendNames) { + const resolveResult = await tryResolveAndInitializeBackend(backendName); + if (typeof resolveResult === 'string') { + errors.push({ name: backendName, err: resolveResult }); + } + else { + if (!backend) { + backend = resolveResult; + } + if (backend === resolveResult) { + availableBackendNames.add(backendName); + } + } + } + // if no backend is available, throw error. + if (!backend) { + throw new Error(`no available backend found. ERR: ${errors.map(e => `[${e.name}] ${e.err}`).join(', ')}`); + } + // for each explicitly requested backend, if it's not available, output warning message. + for (const { name, err } of errors) { + if (backendHints.includes(name)) { + // eslint-disable-next-line no-console + console.warn(`removing requested execution provider "${name}" from session options because it is not available: ${err}`); + } + } + const filteredEps = eps.filter(i => availableBackendNames.has(typeof i === 'string' ? i : i.name)); + return [ + backend, new Proxy(options, { + get: (target, prop) => { + if (prop === 'executionProviders') { + return filteredEps; + } + return Reflect.get(target, prop); + } + }) + ]; +}; +//# sourceMappingURL=backend-impl.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/backend.js": +/*!*************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/backend.js ***! + \*************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ registerBackend: () => (/* reexport safe */ _backend_impl_js__WEBPACK_IMPORTED_MODULE_0__.registerBackend) +/* harmony export */ }); +/* harmony import */ var _backend_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./backend-impl.js */ "./node_modules/onnxruntime-common/dist/esm/backend-impl.js"); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +//# sourceMappingURL=backend.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/env-impl.js": +/*!**************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/env-impl.js ***! + \**************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ env: () => (/* binding */ env) +/* harmony export */ }); +/* harmony import */ var _version_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./version.js */ "./node_modules/onnxruntime-common/dist/esm/version.js"); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +let logLevelValue = 'warning'; +const env = { + wasm: {}, + webgl: {}, + webgpu: {}, + versions: { common: _version_js__WEBPACK_IMPORTED_MODULE_0__.version }, + set logLevel(value) { + if (value === undefined) { + return; + } + if (typeof value !== 'string' || ['verbose', 'info', 'warning', 'error', 'fatal'].indexOf(value) === -1) { + throw new Error(`Unsupported logging level: ${value}`); + } + logLevelValue = value; + }, + get logLevel() { + return logLevelValue; + }, +}; +// set property 'logLevel' so that they can be correctly transferred to worker by `postMessage()`. +Object.defineProperty(env, 'logLevel', { enumerable: true }); +//# sourceMappingURL=env-impl.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/env.js": +/*!*********************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/env.js ***! + \*********************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ env: () => (/* binding */ env) +/* harmony export */ }); +/* harmony import */ var _env_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./env-impl.js */ "./node_modules/onnxruntime-common/dist/esm/env-impl.js"); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +/** + * Represent a set of flags as a global singleton. + */ +const env = _env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env; +//# sourceMappingURL=env.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/index.js": +/*!***********************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/index.js ***! + \***********************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ InferenceSession: () => (/* reexport safe */ _inference_session_js__WEBPACK_IMPORTED_MODULE_2__.InferenceSession), +/* harmony export */ TRACE: () => (/* reexport safe */ _trace_js__WEBPACK_IMPORTED_MODULE_6__.TRACE), +/* harmony export */ TRACE_FUNC_BEGIN: () => (/* reexport safe */ _trace_js__WEBPACK_IMPORTED_MODULE_6__.TRACE_FUNC_BEGIN), +/* harmony export */ TRACE_FUNC_END: () => (/* reexport safe */ _trace_js__WEBPACK_IMPORTED_MODULE_6__.TRACE_FUNC_END), +/* harmony export */ Tensor: () => (/* reexport safe */ _tensor_js__WEBPACK_IMPORTED_MODULE_3__.Tensor), +/* harmony export */ TrainingSession: () => (/* reexport safe */ _training_session_js__WEBPACK_IMPORTED_MODULE_9__.TrainingSession), +/* harmony export */ env: () => (/* reexport safe */ _env_js__WEBPACK_IMPORTED_MODULE_1__.env), +/* harmony export */ registerBackend: () => (/* reexport safe */ _backend_js__WEBPACK_IMPORTED_MODULE_0__.registerBackend) +/* harmony export */ }); +/* harmony import */ var _backend_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./backend.js */ "./node_modules/onnxruntime-common/dist/esm/backend.js"); +/* harmony import */ var _env_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./env.js */ "./node_modules/onnxruntime-common/dist/esm/env.js"); +/* harmony import */ var _inference_session_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./inference-session.js */ "./node_modules/onnxruntime-common/dist/esm/inference-session.js"); +/* harmony import */ var _tensor_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./tensor.js */ "./node_modules/onnxruntime-common/dist/esm/tensor.js"); +/* harmony import */ var _tensor_conversion_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ./tensor-conversion.js */ "./node_modules/onnxruntime-common/dist/esm/tensor-conversion.js"); +/* harmony import */ var _tensor_factory_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ./tensor-factory.js */ "./node_modules/onnxruntime-common/dist/esm/tensor-factory.js"); +/* harmony import */ var _trace_js__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! ./trace.js */ "./node_modules/onnxruntime-common/dist/esm/trace.js"); +/* harmony import */ var _onnx_model_js__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__(/*! ./onnx-model.js */ "./node_modules/onnxruntime-common/dist/esm/onnx-model.js"); +/* harmony import */ var _onnx_value_js__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__(/*! ./onnx-value.js */ "./node_modules/onnxruntime-common/dist/esm/onnx-value.js"); +/* harmony import */ var _training_session_js__WEBPACK_IMPORTED_MODULE_9__ = __webpack_require__(/*! ./training-session.js */ "./node_modules/onnxruntime-common/dist/esm/training-session.js"); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +/** + * # ONNX Runtime JavaScript API + * + * ONNX Runtime JavaScript API is a unified API for all JavaScript usages, including the following NPM packages: + * + * - [onnxruntime-node](https://www.npmjs.com/package/onnxruntime-node) + * - [onnxruntime-web](https://www.npmjs.com/package/onnxruntime-web) + * - [onnxruntime-react-native](https://www.npmjs.com/package/onnxruntime-react-native) + * + * See also: + * - [Get Started](https://onnxruntime.ai/docs/get-started/with-javascript/) + * - [Inference examples](https://github.com/microsoft/onnxruntime-inference-examples/tree/main/js) + * + * @packageDocumentation + */ + + + + + + + + + + +//# sourceMappingURL=index.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/inference-session-impl.js": +/*!****************************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/inference-session-impl.js ***! + \****************************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ InferenceSession: () => (/* binding */ InferenceSession) +/* harmony export */ }); +/* harmony import */ var _backend_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./backend-impl.js */ "./node_modules/onnxruntime-common/dist/esm/backend-impl.js"); +/* harmony import */ var _tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./tensor.js */ "./node_modules/onnxruntime-common/dist/esm/tensor.js"); +/* harmony import */ var _trace_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./trace.js */ "./node_modules/onnxruntime-common/dist/esm/trace.js"); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + + + +class InferenceSession { + constructor(handler) { + this.handler = handler; + } + async run(feeds, arg1, arg2) { + (0,_trace_js__WEBPACK_IMPORTED_MODULE_2__.TRACE_FUNC_BEGIN)(); + const fetches = {}; + let options = {}; + // check inputs + if (typeof feeds !== 'object' || feeds === null || feeds instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor || Array.isArray(feeds)) { + throw new TypeError('\'feeds\' must be an object that use input names as keys and OnnxValue as corresponding values.'); + } + let isFetchesEmpty = true; + // determine which override is being used + if (typeof arg1 === 'object') { + if (arg1 === null) { + throw new TypeError('Unexpected argument[1]: cannot be null.'); + } + if (arg1 instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor) { + throw new TypeError('\'fetches\' cannot be a Tensor'); + } + if (Array.isArray(arg1)) { + if (arg1.length === 0) { + throw new TypeError('\'fetches\' cannot be an empty array.'); + } + isFetchesEmpty = false; + // output names + for (const name of arg1) { + if (typeof name !== 'string') { + throw new TypeError('\'fetches\' must be a string array or an object.'); + } + if (this.outputNames.indexOf(name) === -1) { + throw new RangeError(`'fetches' contains invalid output name: ${name}.`); + } + fetches[name] = null; + } + if (typeof arg2 === 'object' && arg2 !== null) { + options = arg2; + } + else if (typeof arg2 !== 'undefined') { + throw new TypeError('\'options\' must be an object.'); + } + } + else { + // decide whether arg1 is fetches or options + // if any output name is present and its value is valid OnnxValue, we consider it fetches + let isFetches = false; + const arg1Keys = Object.getOwnPropertyNames(arg1); + for (const name of this.outputNames) { + if (arg1Keys.indexOf(name) !== -1) { + const v = arg1[name]; + if (v === null || v instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor) { + isFetches = true; + isFetchesEmpty = false; + fetches[name] = v; + } + } + } + if (isFetches) { + if (typeof arg2 === 'object' && arg2 !== null) { + options = arg2; + } + else if (typeof arg2 !== 'undefined') { + throw new TypeError('\'options\' must be an object.'); + } + } + else { + options = arg1; + } + } + } + else if (typeof arg1 !== 'undefined') { + throw new TypeError('Unexpected argument[1]: must be \'fetches\' or \'options\'.'); + } + // check if all inputs are in feed + for (const name of this.inputNames) { + if (typeof feeds[name] === 'undefined') { + throw new Error(`input '${name}' is missing in 'feeds'.`); + } + } + // if no fetches is specified, we use the full output names list + if (isFetchesEmpty) { + for (const name of this.outputNames) { + fetches[name] = null; + } + } + // feeds, fetches and options are prepared + const results = await this.handler.run(feeds, fetches, options); + const returnValue = {}; + for (const key in results) { + if (Object.hasOwnProperty.call(results, key)) { + const result = results[key]; + if (result instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor) { + returnValue[key] = result; + } + else { + returnValue[key] = new _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor(result.type, result.data, result.dims); + } + } + } + (0,_trace_js__WEBPACK_IMPORTED_MODULE_2__.TRACE_FUNC_END)(); + return returnValue; + } + async release() { + return this.handler.dispose(); + } + static async create(arg0, arg1, arg2, arg3) { + (0,_trace_js__WEBPACK_IMPORTED_MODULE_2__.TRACE_FUNC_BEGIN)(); + // either load from a file or buffer + let filePathOrUint8Array; + let options = {}; + if (typeof arg0 === 'string') { + filePathOrUint8Array = arg0; + if (typeof arg1 === 'object' && arg1 !== null) { + options = arg1; + } + else if (typeof arg1 !== 'undefined') { + throw new TypeError('\'options\' must be an object.'); + } + } + else if (arg0 instanceof Uint8Array) { + filePathOrUint8Array = arg0; + if (typeof arg1 === 'object' && arg1 !== null) { + options = arg1; + } + else if (typeof arg1 !== 'undefined') { + throw new TypeError('\'options\' must be an object.'); + } + } + else if (arg0 instanceof ArrayBuffer || + (typeof SharedArrayBuffer !== 'undefined' && arg0 instanceof SharedArrayBuffer)) { + const buffer = arg0; + let byteOffset = 0; + let byteLength = arg0.byteLength; + if (typeof arg1 === 'object' && arg1 !== null) { + options = arg1; + } + else if (typeof arg1 === 'number') { + byteOffset = arg1; + if (!Number.isSafeInteger(byteOffset)) { + throw new RangeError('\'byteOffset\' must be an integer.'); + } + if (byteOffset < 0 || byteOffset >= buffer.byteLength) { + throw new RangeError(`'byteOffset' is out of range [0, ${buffer.byteLength}).`); + } + byteLength = arg0.byteLength - byteOffset; + if (typeof arg2 === 'number') { + byteLength = arg2; + if (!Number.isSafeInteger(byteLength)) { + throw new RangeError('\'byteLength\' must be an integer.'); + } + if (byteLength <= 0 || byteOffset + byteLength > buffer.byteLength) { + throw new RangeError(`'byteLength' is out of range (0, ${buffer.byteLength - byteOffset}].`); + } + if (typeof arg3 === 'object' && arg3 !== null) { + options = arg3; + } + else if (typeof arg3 !== 'undefined') { + throw new TypeError('\'options\' must be an object.'); + } + } + else if (typeof arg2 !== 'undefined') { + throw new TypeError('\'byteLength\' must be a number.'); + } + } + else if (typeof arg1 !== 'undefined') { + throw new TypeError('\'options\' must be an object.'); + } + filePathOrUint8Array = new Uint8Array(buffer, byteOffset, byteLength); + } + else { + throw new TypeError('Unexpected argument[0]: must be \'path\' or \'buffer\'.'); + } + // resolve backend, update session options with validated EPs, and create session handler + const [backend, optionsWithValidatedEPs] = await (0,_backend_impl_js__WEBPACK_IMPORTED_MODULE_0__.resolveBackendAndExecutionProviders)(options); + const handler = await backend.createInferenceSessionHandler(filePathOrUint8Array, optionsWithValidatedEPs); + (0,_trace_js__WEBPACK_IMPORTED_MODULE_2__.TRACE_FUNC_END)(); + return new InferenceSession(handler); + } + startProfiling() { + this.handler.startProfiling(); + } + endProfiling() { + this.handler.endProfiling(); + } + get inputNames() { + return this.handler.inputNames; + } + get outputNames() { + return this.handler.outputNames; + } +} +//# sourceMappingURL=inference-session-impl.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/inference-session.js": +/*!***********************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/inference-session.js ***! + \***********************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ InferenceSession: () => (/* binding */ InferenceSession) +/* harmony export */ }); +/* harmony import */ var _inference_session_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./inference-session-impl.js */ "./node_modules/onnxruntime-common/dist/esm/inference-session-impl.js"); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// eslint-disable-next-line @typescript-eslint/naming-convention +const InferenceSession = _inference_session_impl_js__WEBPACK_IMPORTED_MODULE_0__.InferenceSession; +//# sourceMappingURL=inference-session.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/onnx-model.js": +/*!****************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/onnx-model.js ***! + \****************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +//# sourceMappingURL=onnx-model.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/onnx-value.js": +/*!****************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/onnx-value.js ***! + \****************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +//# sourceMappingURL=onnx-value.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/tensor-conversion-impl.js": +/*!****************************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/tensor-conversion-impl.js ***! + \****************************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ tensorToDataURL: () => (/* binding */ tensorToDataURL), +/* harmony export */ tensorToImageData: () => (/* binding */ tensorToImageData) +/* harmony export */ }); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +/** + * implementation of Tensor.toDataURL() + */ +const tensorToDataURL = (tensor, options) => { + const canvas = typeof document !== 'undefined' ? document.createElement('canvas') : (new OffscreenCanvas(1, 1)); + canvas.width = tensor.dims[3]; + canvas.height = tensor.dims[2]; + const pixels2DContext = canvas.getContext('2d'); + if (pixels2DContext != null) { + // Default values for height and width & format + let width; + let height; + if (options?.tensorLayout !== undefined && options.tensorLayout === 'NHWC') { + width = tensor.dims[2]; + height = tensor.dims[3]; + } + else { // Default layout is NCWH + width = tensor.dims[3]; + height = tensor.dims[2]; + } + const inputformat = options?.format !== undefined ? options.format : 'RGB'; + const norm = options?.norm; + let normMean; + let normBias; + if (norm === undefined || norm.mean === undefined) { + normMean = [255, 255, 255, 255]; + } + else { + if (typeof (norm.mean) === 'number') { + normMean = [norm.mean, norm.mean, norm.mean, norm.mean]; + } + else { + normMean = [norm.mean[0], norm.mean[1], norm.mean[2], 0]; + if (norm.mean[3] !== undefined) { + normMean[3] = norm.mean[3]; + } + } + } + if (norm === undefined || norm.bias === undefined) { + normBias = [0, 0, 0, 0]; + } + else { + if (typeof (norm.bias) === 'number') { + normBias = [norm.bias, norm.bias, norm.bias, norm.bias]; + } + else { + normBias = [norm.bias[0], norm.bias[1], norm.bias[2], 0]; + if (norm.bias[3] !== undefined) { + normBias[3] = norm.bias[3]; + } + } + } + const stride = height * width; + // Default pointer assignments + let rTensorPointer = 0, gTensorPointer = stride, bTensorPointer = stride * 2, aTensorPointer = -1; + // Updating the pointer assignments based on the input image format + if (inputformat === 'RGBA') { + rTensorPointer = 0; + gTensorPointer = stride; + bTensorPointer = stride * 2; + aTensorPointer = stride * 3; + } + else if (inputformat === 'RGB') { + rTensorPointer = 0; + gTensorPointer = stride; + bTensorPointer = stride * 2; + } + else if (inputformat === 'RBG') { + rTensorPointer = 0; + bTensorPointer = stride; + gTensorPointer = stride * 2; + } + for (let i = 0; i < height; i++) { + for (let j = 0; j < width; j++) { + const R = (tensor.data[rTensorPointer++] - normBias[0]) * normMean[0]; // R value + const G = (tensor.data[gTensorPointer++] - normBias[1]) * normMean[1]; // G value + const B = (tensor.data[bTensorPointer++] - normBias[2]) * normMean[2]; // B value + const A = aTensorPointer === -1 ? + 255 : + (tensor.data[aTensorPointer++] - normBias[3]) * normMean[3]; // A value + // eslint-disable-next-line @typescript-eslint/restrict-plus-operands + pixels2DContext.fillStyle = 'rgba(' + R + ',' + G + ',' + B + ',' + A + ')'; + pixels2DContext.fillRect(j, i, 1, 1); + } + } + if ('toDataURL' in canvas) { + return canvas.toDataURL(); + } + else { + throw new Error('toDataURL is not supported'); + } + } + else { + throw new Error('Can not access image data'); + } +}; +/** + * implementation of Tensor.toImageData() + */ +const tensorToImageData = (tensor, options) => { + const pixels2DContext = typeof document !== 'undefined' ? + document.createElement('canvas').getContext('2d') : + new OffscreenCanvas(1, 1).getContext('2d'); + let image; + if (pixels2DContext != null) { + // Default values for height and width & format + let width; + let height; + let channels; + if (options?.tensorLayout !== undefined && options.tensorLayout === 'NHWC') { + width = tensor.dims[2]; + height = tensor.dims[1]; + channels = tensor.dims[3]; + } + else { // Default layout is NCWH + width = tensor.dims[3]; + height = tensor.dims[2]; + channels = tensor.dims[1]; + } + const inputformat = options !== undefined ? (options.format !== undefined ? options.format : 'RGB') : 'RGB'; + const norm = options?.norm; + let normMean; + let normBias; + if (norm === undefined || norm.mean === undefined) { + normMean = [255, 255, 255, 255]; + } + else { + if (typeof (norm.mean) === 'number') { + normMean = [norm.mean, norm.mean, norm.mean, norm.mean]; + } + else { + normMean = [norm.mean[0], norm.mean[1], norm.mean[2], 255]; + if (norm.mean[3] !== undefined) { + normMean[3] = norm.mean[3]; + } + } + } + if (norm === undefined || norm.bias === undefined) { + normBias = [0, 0, 0, 0]; + } + else { + if (typeof (norm.bias) === 'number') { + normBias = [norm.bias, norm.bias, norm.bias, norm.bias]; + } + else { + normBias = [norm.bias[0], norm.bias[1], norm.bias[2], 0]; + if (norm.bias[3] !== undefined) { + normBias[3] = norm.bias[3]; + } + } + } + const stride = height * width; + if (options !== undefined) { + if (options.format !== undefined && (channels === 4 && options.format !== 'RGBA') || + (channels === 3 && (options.format !== 'RGB' && options.format !== 'BGR'))) { + throw new Error('Tensor format doesn\'t match input tensor dims'); + } + } + // Default pointer assignments + const step = 4; + let rImagePointer = 0, gImagePointer = 1, bImagePointer = 2, aImagePointer = 3; + let rTensorPointer = 0, gTensorPointer = stride, bTensorPointer = stride * 2, aTensorPointer = -1; + // Updating the pointer assignments based on the input image format + if (inputformat === 'RGBA') { + rTensorPointer = 0; + gTensorPointer = stride; + bTensorPointer = stride * 2; + aTensorPointer = stride * 3; + } + else if (inputformat === 'RGB') { + rTensorPointer = 0; + gTensorPointer = stride; + bTensorPointer = stride * 2; + } + else if (inputformat === 'RBG') { + rTensorPointer = 0; + bTensorPointer = stride; + gTensorPointer = stride * 2; + } + image = pixels2DContext.createImageData(width, height); + for (let i = 0; i < height * width; rImagePointer += step, gImagePointer += step, bImagePointer += step, aImagePointer += step, i++) { + image.data[rImagePointer] = (tensor.data[rTensorPointer++] - normBias[0]) * normMean[0]; // R value + image.data[gImagePointer] = (tensor.data[gTensorPointer++] - normBias[1]) * normMean[1]; // G value + image.data[bImagePointer] = (tensor.data[bTensorPointer++] - normBias[2]) * normMean[2]; // B value + image.data[aImagePointer] = aTensorPointer === -1 ? + 255 : + (tensor.data[aTensorPointer++] - normBias[3]) * normMean[3]; // A value + } + } + else { + throw new Error('Can not access image data'); + } + return image; +}; +//# sourceMappingURL=tensor-conversion-impl.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/tensor-conversion.js": +/*!***********************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/tensor-conversion.js ***! + \***********************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +//# sourceMappingURL=tensor-conversion.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/tensor-factory-impl.js": +/*!*************************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/tensor-factory-impl.js ***! + \*************************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ bufferToTensor: () => (/* binding */ bufferToTensor), +/* harmony export */ tensorFromGpuBuffer: () => (/* binding */ tensorFromGpuBuffer), +/* harmony export */ tensorFromImage: () => (/* binding */ tensorFromImage), +/* harmony export */ tensorFromPinnedBuffer: () => (/* binding */ tensorFromPinnedBuffer), +/* harmony export */ tensorFromTexture: () => (/* binding */ tensorFromTexture) +/* harmony export */ }); +/* harmony import */ var _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./tensor-impl.js */ "./node_modules/onnxruntime-common/dist/esm/tensor-impl.js"); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +/** + * Create a new tensor object from image object + * + * @param buffer - Extracted image buffer data - assuming RGBA format + * @param imageFormat - input image configuration - required configurations height, width, format + * @param tensorFormat - output tensor configuration - Default is RGB format + */ +const bufferToTensor = (buffer, options) => { + if (buffer === undefined) { + throw new Error('Image buffer must be defined'); + } + if (options.height === undefined || options.width === undefined) { + throw new Error('Image height and width must be defined'); + } + if (options.tensorLayout === 'NHWC') { + throw new Error('NHWC Tensor layout is not supported yet'); + } + const { height, width } = options; + const norm = options.norm ?? { mean: 255, bias: 0 }; + let normMean; + let normBias; + if (typeof (norm.mean) === 'number') { + normMean = [norm.mean, norm.mean, norm.mean, norm.mean]; + } + else { + normMean = [norm.mean[0], norm.mean[1], norm.mean[2], norm.mean[3] ?? 255]; + } + if (typeof (norm.bias) === 'number') { + normBias = [norm.bias, norm.bias, norm.bias, norm.bias]; + } + else { + normBias = [norm.bias[0], norm.bias[1], norm.bias[2], norm.bias[3] ?? 0]; + } + const inputformat = options.format !== undefined ? options.format : 'RGBA'; + // default value is RGBA since imagedata and HTMLImageElement uses it + const outputformat = options.tensorFormat !== undefined ? (options.tensorFormat !== undefined ? options.tensorFormat : 'RGB') : 'RGB'; + const stride = height * width; + const float32Data = outputformat === 'RGBA' ? new Float32Array(stride * 4) : new Float32Array(stride * 3); + // Default pointer assignments + let step = 4, rImagePointer = 0, gImagePointer = 1, bImagePointer = 2, aImagePointer = 3; + let rTensorPointer = 0, gTensorPointer = stride, bTensorPointer = stride * 2, aTensorPointer = -1; + // Updating the pointer assignments based on the input image format + if (inputformat === 'RGB') { + step = 3; + rImagePointer = 0; + gImagePointer = 1; + bImagePointer = 2; + aImagePointer = -1; + } + // Updating the pointer assignments based on the output tensor format + if (outputformat === 'RGBA') { + aTensorPointer = stride * 3; + } + else if (outputformat === 'RBG') { + rTensorPointer = 0; + bTensorPointer = stride; + gTensorPointer = stride * 2; + } + else if (outputformat === 'BGR') { + bTensorPointer = 0; + gTensorPointer = stride; + rTensorPointer = stride * 2; + } + for (let i = 0; i < stride; i++, rImagePointer += step, bImagePointer += step, gImagePointer += step, aImagePointer += step) { + float32Data[rTensorPointer++] = (buffer[rImagePointer] + normBias[0]) / normMean[0]; + float32Data[gTensorPointer++] = (buffer[gImagePointer] + normBias[1]) / normMean[1]; + float32Data[bTensorPointer++] = (buffer[bImagePointer] + normBias[2]) / normMean[2]; + if (aTensorPointer !== -1 && aImagePointer !== -1) { + float32Data[aTensorPointer++] = (buffer[aImagePointer] + normBias[3]) / normMean[3]; + } + } + // Float32Array -> ort.Tensor + const outputTensor = outputformat === 'RGBA' ? new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor('float32', float32Data, [1, 4, height, width]) : + new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor('float32', float32Data, [1, 3, height, width]); + return outputTensor; +}; +/** + * implementation of Tensor.fromImage(). + */ +const tensorFromImage = async (image, options) => { + // checking the type of image object + const isHTMLImageEle = typeof (HTMLImageElement) !== 'undefined' && image instanceof HTMLImageElement; + const isImageDataEle = typeof (ImageData) !== 'undefined' && image instanceof ImageData; + const isImageBitmap = typeof (ImageBitmap) !== 'undefined' && image instanceof ImageBitmap; + const isString = typeof image === 'string'; + let data; + let bufferToTensorOptions = options ?? {}; + const createCanvas = () => { + if (typeof document !== 'undefined') { + return document.createElement('canvas'); + } + else if (typeof OffscreenCanvas !== 'undefined') { + return new OffscreenCanvas(1, 1); + } + else { + throw new Error('Canvas is not supported'); + } + }; + const createCanvasContext = (canvas) => { + if (canvas instanceof HTMLCanvasElement) { + return canvas.getContext('2d'); + } + else if (canvas instanceof OffscreenCanvas) { + return canvas.getContext('2d'); + } + else { + return null; + } + }; + // filling and checking image configuration options + if (isHTMLImageEle) { + // HTMLImageElement - image object - format is RGBA by default + const canvas = createCanvas(); + canvas.width = image.width; + canvas.height = image.height; + const pixels2DContext = createCanvasContext(canvas); + if (pixels2DContext != null) { + let height = image.height; + let width = image.width; + if (options !== undefined && options.resizedHeight !== undefined && options.resizedWidth !== undefined) { + height = options.resizedHeight; + width = options.resizedWidth; + } + if (options !== undefined) { + bufferToTensorOptions = options; + if (options.tensorFormat !== undefined) { + throw new Error('Image input config format must be RGBA for HTMLImageElement'); + } + else { + bufferToTensorOptions.tensorFormat = 'RGBA'; + } + bufferToTensorOptions.height = height; + bufferToTensorOptions.width = width; + } + else { + bufferToTensorOptions.tensorFormat = 'RGBA'; + bufferToTensorOptions.height = height; + bufferToTensorOptions.width = width; + } + pixels2DContext.drawImage(image, 0, 0); + data = pixels2DContext.getImageData(0, 0, width, height).data; + } + else { + throw new Error('Can not access image data'); + } + } + else if (isImageDataEle) { + let height; + let width; + if (options !== undefined && options.resizedWidth !== undefined && options.resizedHeight !== undefined) { + height = options.resizedHeight; + width = options.resizedWidth; + } + else { + height = image.height; + width = image.width; + } + if (options !== undefined) { + bufferToTensorOptions = options; + } + bufferToTensorOptions.format = 'RGBA'; + bufferToTensorOptions.height = height; + bufferToTensorOptions.width = width; + if (options !== undefined) { + const tempCanvas = createCanvas(); + tempCanvas.width = width; + tempCanvas.height = height; + const pixels2DContext = createCanvasContext(tempCanvas); + if (pixels2DContext != null) { + pixels2DContext.putImageData(image, 0, 0); + data = pixels2DContext.getImageData(0, 0, width, height).data; + } + else { + throw new Error('Can not access image data'); + } + } + else { + data = image.data; + } + } + else if (isImageBitmap) { + // ImageBitmap - image object - format must be provided by user + if (options === undefined) { + throw new Error('Please provide image config with format for Imagebitmap'); + } + const canvas = createCanvas(); + canvas.width = image.width; + canvas.height = image.height; + const pixels2DContext = createCanvasContext(canvas); + if (pixels2DContext != null) { + const height = image.height; + const width = image.width; + pixels2DContext.drawImage(image, 0, 0, width, height); + data = pixels2DContext.getImageData(0, 0, width, height).data; + bufferToTensorOptions.height = height; + bufferToTensorOptions.width = width; + return bufferToTensor(data, bufferToTensorOptions); + } + else { + throw new Error('Can not access image data'); + } + } + else if (isString) { + return new Promise((resolve, reject) => { + const canvas = createCanvas(); + const context = createCanvasContext(canvas); + if (!image || !context) { + return reject(); + } + const newImage = new Image(); + newImage.crossOrigin = 'Anonymous'; + newImage.src = image; + newImage.onload = () => { + canvas.width = newImage.width; + canvas.height = newImage.height; + context.drawImage(newImage, 0, 0, canvas.width, canvas.height); + const img = context.getImageData(0, 0, canvas.width, canvas.height); + bufferToTensorOptions.height = canvas.height; + bufferToTensorOptions.width = canvas.width; + resolve(bufferToTensor(img.data, bufferToTensorOptions)); + }; + }); + } + else { + throw new Error('Input data provided is not supported - aborted tensor creation'); + } + if (data !== undefined) { + return bufferToTensor(data, bufferToTensorOptions); + } + else { + throw new Error('Input data provided is not supported - aborted tensor creation'); + } +}; +/** + * implementation of Tensor.fromTexture(). + */ +const tensorFromTexture = (texture, options) => { + const { width, height, download, dispose } = options; + // Always assume RGBAF32. TODO: support different texture format + const dims = [1, height, width, 4]; + return new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor({ location: 'texture', type: 'float32', texture, dims, download, dispose }); +}; +/** + * implementation of Tensor.fromGpuBuffer(). + */ +const tensorFromGpuBuffer = (gpuBuffer, options) => { + const { dataType, dims, download, dispose } = options; + return new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor({ location: 'gpu-buffer', type: dataType ?? 'float32', gpuBuffer, dims, download, dispose }); +}; +/** + * implementation of Tensor.fromPinnedBuffer(). + */ +const tensorFromPinnedBuffer = (type, buffer, dims) => new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor({ location: 'cpu-pinned', type, data: buffer, dims: dims ?? [buffer.length] }); +//# sourceMappingURL=tensor-factory-impl.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/tensor-factory.js": +/*!********************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/tensor-factory.js ***! + \********************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +//# sourceMappingURL=tensor-factory.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/tensor-impl-type-mapping.js": +/*!******************************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/tensor-impl-type-mapping.js ***! + \******************************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP: () => (/* binding */ NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP), +/* harmony export */ NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP: () => (/* binding */ NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP), +/* harmony export */ checkTypedArray: () => (/* binding */ checkTypedArray) +/* harmony export */ }); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// a runtime map that maps type string to TypedArray constructor. Should match Tensor.DataTypeMap. +const NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP = new Map([ + ['float32', Float32Array], + ['uint8', Uint8Array], + ['int8', Int8Array], + ['uint16', Uint16Array], + ['int16', Int16Array], + ['int32', Int32Array], + ['bool', Uint8Array], + ['float64', Float64Array], + ['uint32', Uint32Array], +]); +// a runtime map that maps type string to TypedArray constructor. Should match Tensor.DataTypeMap. +const NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP = new Map([ + [Float32Array, 'float32'], + [Uint8Array, 'uint8'], + [Int8Array, 'int8'], + [Uint16Array, 'uint16'], + [Int16Array, 'int16'], + [Int32Array, 'int32'], + [Float64Array, 'float64'], + [Uint32Array, 'uint32'], +]); +// the following code allows delaying execution of BigInt/Float16Array checking. This allows lazy initialization for +// NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP and NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP, which allows BigInt/Float16Array +// polyfill if available. +let isTypedArrayChecked = false; +const checkTypedArray = () => { + if (!isTypedArrayChecked) { + isTypedArrayChecked = true; + const isBigInt64ArrayAvailable = typeof BigInt64Array !== 'undefined' && BigInt64Array.from; + const isBigUint64ArrayAvailable = typeof BigUint64Array !== 'undefined' && BigUint64Array.from; + const isFloat16ArrayAvailable = typeof Float16Array !== 'undefined' && Float16Array.from; + if (isBigInt64ArrayAvailable) { + NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.set('int64', BigInt64Array); + NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.set(BigInt64Array, 'int64'); + } + if (isBigUint64ArrayAvailable) { + NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.set('uint64', BigUint64Array); + NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.set(BigUint64Array, 'uint64'); + } + if (isFloat16ArrayAvailable) { + NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.set('float16', Float16Array); + NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.set(Float16Array, 'float16'); + } + else { + // if Float16Array is not available, use 'Uint16Array' to store the data. + NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.set('float16', Uint16Array); + } + } +}; +//# sourceMappingURL=tensor-impl-type-mapping.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/tensor-impl.js": +/*!*****************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/tensor-impl.js ***! + \*****************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ Tensor: () => (/* binding */ Tensor) +/* harmony export */ }); +/* harmony import */ var _tensor_conversion_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./tensor-conversion-impl.js */ "./node_modules/onnxruntime-common/dist/esm/tensor-conversion-impl.js"); +/* harmony import */ var _tensor_factory_impl_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./tensor-factory-impl.js */ "./node_modules/onnxruntime-common/dist/esm/tensor-factory-impl.js"); +/* harmony import */ var _tensor_impl_type_mapping_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./tensor-impl-type-mapping.js */ "./node_modules/onnxruntime-common/dist/esm/tensor-impl-type-mapping.js"); +/* harmony import */ var _tensor_utils_impl_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./tensor-utils-impl.js */ "./node_modules/onnxruntime-common/dist/esm/tensor-utils-impl.js"); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + + + + +/** + * the implementation of Tensor interface. + * + * @ignore + */ +class Tensor { + /** + * implementation. + */ + constructor(arg0, arg1, arg2) { + // perform one-time check for BigInt/Float16Array support + (0,_tensor_impl_type_mapping_js__WEBPACK_IMPORTED_MODULE_2__.checkTypedArray)(); + let type; + let dims; + if (typeof arg0 === 'object' && 'location' in arg0) { + // + // constructing tensor from specific location + // + this.dataLocation = arg0.location; + type = arg0.type; + dims = arg0.dims; + switch (arg0.location) { + case 'cpu-pinned': { + const expectedTypedArrayConstructor = _tensor_impl_type_mapping_js__WEBPACK_IMPORTED_MODULE_2__.NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.get(type); + if (!expectedTypedArrayConstructor) { + throw new TypeError(`unsupported type "${type}" to create tensor from pinned buffer`); + } + if (!(arg0.data instanceof expectedTypedArrayConstructor)) { + throw new TypeError(`buffer should be of type ${expectedTypedArrayConstructor.name}`); + } + this.cpuData = arg0.data; + break; + } + case 'texture': { + if (type !== 'float32') { + throw new TypeError(`unsupported type "${type}" to create tensor from texture`); + } + this.gpuTextureData = arg0.texture; + this.downloader = arg0.download; + this.disposer = arg0.dispose; + break; + } + case 'gpu-buffer': { + if ((type !== 'float32' && type !== 'float16' && type !== 'int32' && type !== 'int64' && type !== 'uint32' && + type !== 'uint8' && type !== 'bool')) { + throw new TypeError(`unsupported type "${type}" to create tensor from gpu buffer`); + } + this.gpuBufferData = arg0.gpuBuffer; + this.downloader = arg0.download; + this.disposer = arg0.dispose; + break; + } + default: + throw new Error(`Tensor constructor: unsupported location '${this.dataLocation}'`); + } + } + else { + // + // constructing tensor of location 'cpu' + // + let data; + let maybeDims; + // check whether arg0 is type or data + if (typeof arg0 === 'string') { + // + // Override: constructor(type, data, ...) + // + type = arg0; + maybeDims = arg2; + if (arg0 === 'string') { + // string tensor + if (!Array.isArray(arg1)) { + throw new TypeError('A string tensor\'s data must be a string array.'); + } + // we don't check whether every element in the array is string; this is too slow. we assume it's correct and + // error will be populated at inference + data = arg1; + } + else { + // numeric tensor + const typedArrayConstructor = _tensor_impl_type_mapping_js__WEBPACK_IMPORTED_MODULE_2__.NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.get(arg0); + if (typedArrayConstructor === undefined) { + throw new TypeError(`Unsupported tensor type: ${arg0}.`); + } + if (Array.isArray(arg1)) { + if (arg0 === 'float16' && typedArrayConstructor === Uint16Array) { + // When no Float16Array polyfill is used, we cannot create 'float16' tensor from number array. + // + // Throw error here because when user try to use number array as data, + // e.g. new Tensor('float16', [1, 2, 3, 4], dims)), it will actually call + // Uint16Array.from(arg1) which generates wrong data. + throw new TypeError('Creating a float16 tensor from number array is not supported. Please use Uint16Array as data.'); + } + else if (arg0 === 'uint64' || arg0 === 'int64') { + // use 'as any' here because: + // 1. TypeScript's check on type of 'Array.isArray()' does not work with readonly arrays. + // see https://github.com/microsoft/TypeScript/issues/17002 + // 2. TypeScript's check on union type of '(BigInt64ArrayConstructor|BigUint64ArrayConstructor).from()' + // does not accept parameter mapFn. + // 3. parameters of 'SupportedTypedArrayConstructors.from()' does not match the requirement of the union + // type. + // assume 'arg1' is of type "readonly number[]|readonly bigint[]" here. + // eslint-disable-next-line @typescript-eslint/no-explicit-any + data = typedArrayConstructor.from(arg1, BigInt); + } + else { + // assume 'arg1' is of type "readonly number[]" here. + // eslint-disable-next-line @typescript-eslint/no-explicit-any + data = typedArrayConstructor.from(arg1); + } + } + else if (arg1 instanceof typedArrayConstructor) { + data = arg1; + } + else { + throw new TypeError(`A ${type} tensor's data must be type of ${typedArrayConstructor}`); + } + } + } + else { + // + // Override: constructor(data, ...) + // + maybeDims = arg1; + if (Array.isArray(arg0)) { + // only boolean[] and string[] is supported + if (arg0.length === 0) { + throw new TypeError('Tensor type cannot be inferred from an empty array.'); + } + const firstElementType = typeof arg0[0]; + if (firstElementType === 'string') { + type = 'string'; + data = arg0; + } + else if (firstElementType === 'boolean') { + type = 'bool'; + // 'arg0' is of type 'boolean[]'. Uint8Array.from(boolean[]) actually works, but typescript thinks this is + // wrong type. We use 'as any' to make it happy. + // eslint-disable-next-line @typescript-eslint/no-explicit-any + data = Uint8Array.from(arg0); + } + else { + throw new TypeError(`Invalid element type of data array: ${firstElementType}.`); + } + } + else { + // get tensor type from TypedArray + const mappedType = _tensor_impl_type_mapping_js__WEBPACK_IMPORTED_MODULE_2__.NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.get(arg0.constructor); + if (mappedType === undefined) { + throw new TypeError(`Unsupported type for tensor data: ${arg0.constructor}.`); + } + type = mappedType; + data = arg0; + } + } + // type and data is processed, now processing dims + if (maybeDims === undefined) { + // assume 1-D tensor if dims omitted + maybeDims = [data.length]; + } + else if (!Array.isArray(maybeDims)) { + throw new TypeError('A tensor\'s dims must be a number array'); + } + dims = maybeDims; + this.cpuData = data; + this.dataLocation = 'cpu'; + } + // perform check on dims + const size = (0,_tensor_utils_impl_js__WEBPACK_IMPORTED_MODULE_3__.calculateSize)(dims); + // if data is on CPU, check whether data length matches tensor size + if (this.cpuData && size !== this.cpuData.length) { + throw new Error(`Tensor's size(${size}) does not match data length(${this.cpuData.length}).`); + } + this.type = type; + this.dims = dims; + this.size = size; + } + // #endregion + // #region factory + static async fromImage(image, options) { + return (0,_tensor_factory_impl_js__WEBPACK_IMPORTED_MODULE_1__.tensorFromImage)(image, options); + } + static fromTexture(texture, options) { + return (0,_tensor_factory_impl_js__WEBPACK_IMPORTED_MODULE_1__.tensorFromTexture)(texture, options); + } + static fromGpuBuffer(gpuBuffer, options) { + return (0,_tensor_factory_impl_js__WEBPACK_IMPORTED_MODULE_1__.tensorFromGpuBuffer)(gpuBuffer, options); + } + static fromPinnedBuffer(type, buffer, dims) { + return (0,_tensor_factory_impl_js__WEBPACK_IMPORTED_MODULE_1__.tensorFromPinnedBuffer)(type, buffer, dims); + } + // #endregion + // #region conversions + toDataURL(options) { + return (0,_tensor_conversion_impl_js__WEBPACK_IMPORTED_MODULE_0__.tensorToDataURL)(this, options); + } + toImageData(options) { + return (0,_tensor_conversion_impl_js__WEBPACK_IMPORTED_MODULE_0__.tensorToImageData)(this, options); + } + // #endregion + // #region properties + get data() { + this.ensureValid(); + if (!this.cpuData) { + throw new Error('The data is not on CPU. Use `getData()` to download GPU data to CPU, ' + + 'or use `texture` or `gpuBuffer` property to access the GPU data directly.'); + } + return this.cpuData; + } + get location() { + return this.dataLocation; + } + get texture() { + this.ensureValid(); + if (!this.gpuTextureData) { + throw new Error('The data is not stored as a WebGL texture.'); + } + return this.gpuTextureData; + } + get gpuBuffer() { + this.ensureValid(); + if (!this.gpuBufferData) { + throw new Error('The data is not stored as a WebGPU buffer.'); + } + return this.gpuBufferData; + } + // #endregion + // #region methods + async getData(releaseData) { + this.ensureValid(); + switch (this.dataLocation) { + case 'cpu': + case 'cpu-pinned': + return this.data; + case 'texture': + case 'gpu-buffer': { + if (!this.downloader) { + throw new Error('The current tensor is not created with a specified data downloader.'); + } + if (this.isDownloading) { + throw new Error('The current tensor is being downloaded.'); + } + try { + this.isDownloading = true; + const data = await this.downloader(); + this.downloader = undefined; + this.dataLocation = 'cpu'; + this.cpuData = data; + if (releaseData && this.disposer) { + this.disposer(); + this.disposer = undefined; + } + return data; + } + finally { + this.isDownloading = false; + } + } + default: + throw new Error(`cannot get data from location: ${this.dataLocation}`); + } + } + dispose() { + if (this.isDownloading) { + throw new Error('The current tensor is being downloaded.'); + } + if (this.disposer) { + this.disposer(); + this.disposer = undefined; + } + this.cpuData = undefined; + this.gpuTextureData = undefined; + this.gpuBufferData = undefined; + this.downloader = undefined; + this.isDownloading = undefined; + this.dataLocation = 'none'; + } + // #endregion + // #region tensor utilities + ensureValid() { + if (this.dataLocation === 'none') { + throw new Error('The tensor is disposed.'); + } + } + reshape(dims) { + this.ensureValid(); + if (this.downloader || this.disposer) { + throw new Error('Cannot reshape a tensor that owns GPU resource.'); + } + return (0,_tensor_utils_impl_js__WEBPACK_IMPORTED_MODULE_3__.tensorReshape)(this, dims); + } +} +//# sourceMappingURL=tensor-impl.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/tensor-utils-impl.js": +/*!***********************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/tensor-utils-impl.js ***! + \***********************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ calculateSize: () => (/* binding */ calculateSize), +/* harmony export */ tensorReshape: () => (/* binding */ tensorReshape) +/* harmony export */ }); +/* harmony import */ var _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./tensor-impl.js */ "./node_modules/onnxruntime-common/dist/esm/tensor-impl.js"); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +/** + * calculate size from dims. + * + * @param dims the dims array. May be an illegal input. + */ +const calculateSize = (dims) => { + let size = 1; + for (let i = 0; i < dims.length; i++) { + const dim = dims[i]; + if (typeof dim !== 'number' || !Number.isSafeInteger(dim)) { + throw new TypeError(`dims[${i}] must be an integer, got: ${dim}`); + } + if (dim < 0) { + throw new RangeError(`dims[${i}] must be a non-negative integer, got: ${dim}`); + } + size *= dim; + } + return size; +}; +/** + * implementation of Tensor.reshape() + */ +const tensorReshape = (tensor, dims) => { + switch (tensor.location) { + case 'cpu': + return new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor(tensor.type, tensor.data, dims); + case 'cpu-pinned': + return new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor({ + location: 'cpu-pinned', + data: tensor.data, + type: tensor.type, + dims, + }); + case 'texture': + return new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor({ + location: 'texture', + texture: tensor.texture, + type: tensor.type, + dims, + }); + case 'gpu-buffer': + return new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor({ + location: 'gpu-buffer', + gpuBuffer: tensor.gpuBuffer, + type: tensor.type, + dims, + }); + default: + throw new Error(`tensorReshape: tensor location ${tensor.location} is not supported`); + } +}; +//# sourceMappingURL=tensor-utils-impl.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/tensor.js": +/*!************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/tensor.js ***! + \************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ Tensor: () => (/* binding */ Tensor) +/* harmony export */ }); +/* harmony import */ var _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./tensor-impl.js */ "./node_modules/onnxruntime-common/dist/esm/tensor-impl.js"); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// eslint-disable-next-line @typescript-eslint/naming-convention +const Tensor = _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor; +//# sourceMappingURL=tensor.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/trace.js": +/*!***********************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/trace.js ***! + \***********************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ TRACE: () => (/* binding */ TRACE), +/* harmony export */ TRACE_FUNC_BEGIN: () => (/* binding */ TRACE_FUNC_BEGIN), +/* harmony export */ TRACE_FUNC_END: () => (/* binding */ TRACE_FUNC_END) +/* harmony export */ }); +/* harmony import */ var _env_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./env-impl.js */ "./node_modules/onnxruntime-common/dist/esm/env-impl.js"); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +/** + * @ignore + */ +const TRACE = (deviceType, label) => { + if (typeof _env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.trace === 'undefined' ? !_env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.wasm.trace : !_env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.trace) { + return; + } + // eslint-disable-next-line no-console + console.timeStamp(`${deviceType}::ORT::${label}`); +}; +const TRACE_FUNC = (msg, extraMsg) => { + const stack = new Error().stack?.split(/\r\n|\r|\n/g) || []; + let hasTraceFunc = false; + for (let i = 0; i < stack.length; i++) { + if (hasTraceFunc && !stack[i].includes('TRACE_FUNC')) { + let label = `FUNC_${msg}::${stack[i].trim().split(' ')[1]}`; + if (extraMsg) { + label += `::${extraMsg}`; + } + TRACE('CPU', label); + return; + } + if (stack[i].includes('TRACE_FUNC')) { + hasTraceFunc = true; + } + } +}; +/** + * @ignore + */ +const TRACE_FUNC_BEGIN = (extraMsg) => { + if (typeof _env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.trace === 'undefined' ? !_env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.wasm.trace : !_env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.trace) { + return; + } + TRACE_FUNC('BEGIN', extraMsg); +}; +/** + * @ignore + */ +const TRACE_FUNC_END = (extraMsg) => { + if (typeof _env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.trace === 'undefined' ? !_env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.wasm.trace : !_env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.trace) { + return; + } + TRACE_FUNC('END', extraMsg); +}; +//# sourceMappingURL=trace.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/training-session-impl.js": +/*!***************************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/training-session-impl.js ***! + \***************************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ TrainingSession: () => (/* binding */ TrainingSession) +/* harmony export */ }); +/* harmony import */ var _backend_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./backend-impl.js */ "./node_modules/onnxruntime-common/dist/esm/backend-impl.js"); +/* harmony import */ var _tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./tensor.js */ "./node_modules/onnxruntime-common/dist/esm/tensor.js"); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + + +const noBackendErrMsg = 'Training backend could not be resolved. ' + + 'Make sure you\'re using the correct configuration & WebAssembly files.'; +class TrainingSession { + constructor(handler, hasOptimizerModel, hasEvalModel) { + this.handler = handler; + this.hasOptimizerModel = hasOptimizerModel; + this.hasEvalModel = hasEvalModel; + } + get trainingInputNames() { + return this.handler.inputNames; + } + get trainingOutputNames() { + return this.handler.outputNames; + } + get evalInputNames() { + if (this.hasEvalModel) { + return this.handler.evalInputNames; + } + else { + throw new Error('This training session has no evalModel loaded.'); + } + } + get evalOutputNames() { + if (this.hasEvalModel) { + return this.handler.evalOutputNames; + } + else { + throw new Error('This training session has no evalModel loaded.'); + } + } + static async create(trainingOptions, sessionOptions) { + const evalModel = trainingOptions.evalModel || ''; + const optimizerModel = trainingOptions.optimizerModel || ''; + const options = sessionOptions || {}; + // resolve backend, update session options with validated EPs, and create session handler + const [backend, optionsWithValidatedEPs] = await (0,_backend_impl_js__WEBPACK_IMPORTED_MODULE_0__.resolveBackendAndExecutionProviders)(options); + if (backend.createTrainingSessionHandler) { + const handler = await backend.createTrainingSessionHandler(trainingOptions.checkpointState, trainingOptions.trainModel, evalModel, optimizerModel, optionsWithValidatedEPs); + return new TrainingSession(handler, !!trainingOptions.optimizerModel, !!trainingOptions.evalModel); + } + else { + throw new Error(noBackendErrMsg); + } + } + /** + * Helper function for runTrainStep and future runStep methods that handles the type-narrowing conversion from + * the given parameters to SessionHandler.FetchesType and RunOptions. + * + * @param inputNames the feeds object is checked that they contain all input names in the provided list of input + * names. + * @param outputNames the fetches object is checked that their keys match up with valid names in the list of output + * names. + * @param feeds the required input + * @param arg1 narrowed & converted into the SessionHandler.FetchesType or RunOptions object + * @param arg2 optional RunOptions object. + * @returns + */ + typeNarrowingForRunStep(inputNames, outputNames, feeds, arg1, arg2) { + const fetches = {}; + let options = {}; + // check inputs + if (typeof feeds !== 'object' || feeds === null || feeds instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor || Array.isArray(feeds)) { + throw new TypeError('\'feeds\' must be an object that use input names as keys and OnnxValue as corresponding values.'); + } + let isFetchesEmpty = true; + // determine which override is being used + if (typeof arg1 === 'object') { + if (arg1 === null) { + throw new TypeError('Unexpected argument[1]: cannot be null.'); + } + if (arg1 instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor) { + throw new TypeError('\'fetches\' cannot be a Tensor'); + } + if (Array.isArray(arg1)) { + if (arg1.length === 0) { + throw new TypeError('\'fetches\' cannot be an empty array.'); + } + isFetchesEmpty = false; + // output names + for (const name of arg1) { + if (typeof name !== 'string') { + throw new TypeError('\'fetches\' must be a string array or an object.'); + } + if (outputNames.indexOf(name) === -1) { + throw new RangeError(`'fetches' contains invalid output name: ${name}.`); + } + fetches[name] = null; + } + if (typeof arg2 === 'object' && arg2 !== null) { + options = arg2; + } + else if (typeof arg2 !== 'undefined') { + throw new TypeError('\'options\' must be an object.'); + } + } + else { + // decide whether arg1 is fetches or options + // if any output name is present and its value is valid OnnxValue, we consider it fetches + let isFetches = false; + const arg1Keys = Object.getOwnPropertyNames(arg1); + for (const name of outputNames) { + if (arg1Keys.indexOf(name) !== -1) { + const v = arg1[name]; + if (v === null || v instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor) { + isFetches = true; + isFetchesEmpty = false; + fetches[name] = v; + } + } + } + if (isFetches) { + if (typeof arg2 === 'object' && arg2 !== null) { + options = arg2; + } + else if (typeof arg2 !== 'undefined') { + throw new TypeError('\'options\' must be an object.'); + } + } + else { + options = arg1; + } + } + } + else if (typeof arg1 !== 'undefined') { + throw new TypeError('Unexpected argument[1]: must be \'fetches\' or \'options\'.'); + } + // check if all inputs are in feed + for (const name of inputNames) { + if (typeof feeds[name] === 'undefined') { + throw new Error(`input '${name}' is missing in 'feeds'.`); + } + } + // if no fetches is specified, we use the full output names list + if (isFetchesEmpty) { + for (const name of outputNames) { + fetches[name] = null; + } + } + return [fetches, options]; + } + /** + * Helper method for runTrainStep and any other runStep methods. Takes the ReturnType result from the SessionHandler + * and changes it into a map of Tensors. + * + * @param results + * @returns + */ + convertHandlerReturnTypeToMapOfTensors(results) { + const returnValue = {}; + for (const key in results) { + if (Object.hasOwnProperty.call(results, key)) { + const result = results[key]; + if (result instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor) { + returnValue[key] = result; + } + else { + returnValue[key] = new _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor(result.type, result.data, result.dims); + } + } + } + return returnValue; + } + async lazyResetGrad() { + await this.handler.lazyResetGrad(); + } + async runTrainStep(feeds, arg1, arg2) { + const [fetches, options] = this.typeNarrowingForRunStep(this.trainingInputNames, this.trainingOutputNames, feeds, arg1, arg2); + const results = await this.handler.runTrainStep(feeds, fetches, options); + return this.convertHandlerReturnTypeToMapOfTensors(results); + } + async runOptimizerStep(options) { + if (this.hasOptimizerModel) { + await this.handler.runOptimizerStep(options || {}); + } + else { + throw new Error('This TrainingSession has no OptimizerModel loaded.'); + } + } + async runEvalStep(feeds, arg1, arg2) { + if (this.hasEvalModel) { + const [fetches, options] = this.typeNarrowingForRunStep(this.evalInputNames, this.evalOutputNames, feeds, arg1, arg2); + const results = await this.handler.runEvalStep(feeds, fetches, options); + return this.convertHandlerReturnTypeToMapOfTensors(results); + } + else { + throw new Error('This TrainingSession has no EvalModel loaded.'); + } + } + async getParametersSize(trainableOnly = true) { + return this.handler.getParametersSize(trainableOnly); + } + async loadParametersBuffer(array, trainableOnly = true) { + const paramsSize = await this.getParametersSize(trainableOnly); + // checking that the size of the Uint8Array is equivalent to the byte length of a Float32Array of the number + // of parameters + if (array.length !== 4 * paramsSize) { + throw new Error('Size of the buffer passed into loadParametersBuffer must match the number of parameters in ' + + 'the model. Please use getParametersSize method to check.'); + } + return this.handler.loadParametersBuffer(array, trainableOnly); + } + async getContiguousParameters(trainableOnly = true) { + return this.handler.getContiguousParameters(trainableOnly); + } + async release() { + return this.handler.dispose(); + } +} +//# sourceMappingURL=training-session-impl.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/training-session.js": +/*!**********************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/training-session.js ***! + \**********************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ TrainingSession: () => (/* binding */ TrainingSession) +/* harmony export */ }); +/* harmony import */ var _training_session_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./training-session-impl.js */ "./node_modules/onnxruntime-common/dist/esm/training-session-impl.js"); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// eslint-disable-next-line @typescript-eslint/naming-convention +const TrainingSession = _training_session_impl_js__WEBPACK_IMPORTED_MODULE_0__.TrainingSession; +//# sourceMappingURL=training-session.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/version.js": +/*!*************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/version.js ***! + \*************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ version: () => (/* binding */ version) +/* harmony export */ }); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// This file is generated by /js/scripts/update-version.ts +// Do not modify file content manually. +const version = '1.19.2'; +//# sourceMappingURL=version.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-web/dist/ort.webgpu.bundle.min.mjs": +/*!*********************************************************************!*\ + !*** ./node_modules/onnxruntime-web/dist/ort.webgpu.bundle.min.mjs ***! + \*********************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ InferenceSession: () => (/* binding */ Ip), +/* harmony export */ TRACE: () => (/* binding */ Sr), +/* harmony export */ TRACE_FUNC_BEGIN: () => (/* binding */ Le), +/* harmony export */ TRACE_FUNC_END: () => (/* binding */ Ve), +/* harmony export */ Tensor: () => (/* binding */ Be), +/* harmony export */ TrainingSession: () => (/* binding */ Ap), +/* harmony export */ "default": () => (/* binding */ Vx), +/* harmony export */ env: () => (/* binding */ _e), +/* harmony export */ registerBackend: () => (/* binding */ St) +/* harmony export */ }); +/*! + * ONNX Runtime Web v1.21.0-dev.20241024-d9ca84ef96 + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. + */ +var Vn=Object.defineProperty;var vp=Object.getOwnPropertyDescriptor;var $p=Object.getOwnPropertyNames;var xp=Object.prototype.hasOwnProperty;var Wn=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,r)=>(typeof require<"u"?require:t)[r]}):e)(function(e){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+e+'" is not supported')});var U=(e,t)=>()=>(e&&(t=e(e=0)),t);var Gt=(e,t)=>{for(var r in t)Vn(e,r,{get:t[r],enumerable:!0})},Sp=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of $p(t))!xp.call(e,o)&&o!==r&&Vn(e,o,{get:()=>t[o],enumerable:!(n=vp(t,o))||n.enumerable});return e};var br=e=>Sp(Vn({},"__esModule",{value:!0}),e);var wr,xt,St,Tp,_r,vr=U(()=>{"use strict";wr=new Map,xt=[],St=(e,t,r)=>{if(t&&typeof t.init=="function"&&typeof t.createInferenceSessionHandler=="function"){let n=wr.get(e);if(n===void 0)wr.set(e,{backend:t,priority:r});else{if(n.priority>r)return;if(n.priority===r&&n.backend!==t)throw new Error(`cannot register backend "${e}" using priority ${r}`)}if(r>=0){let o=xt.indexOf(e);o!==-1&&xt.splice(o,1);for(let i=0;i{let t=wr.get(e);if(!t)return"backend not found.";if(t.initialized)return t.backend;if(t.aborted)return t.error;{let r=!!t.initPromise;try{return r||(t.initPromise=t.backend.init(e)),await t.initPromise,t.initialized=!0,t.backend}catch(n){return r||(t.error=`${n}`,t.aborted=!0),t.error}finally{delete t.initPromise}}},_r=async e=>{let t=e.executionProviders||[],r=t.map(l=>typeof l=="string"?l:l.name),n=r.length===0?xt:r,o,i=[],a=new Set;for(let l of n){let c=await Tp(l);typeof c=="string"?i.push({name:l,err:c}):(o||(o=c),o===c&&a.add(l))}if(!o)throw new Error(`no available backend found. ERR: ${i.map(l=>`[${l.name}] ${l.err}`).join(", ")}`);for(let{name:l,err:c}of i)r.includes(l)&&console.warn(`removing requested execution provider "${l}" from session options because it is not available: ${c}`);let d=t.filter(l=>a.has(typeof l=="string"?l:l.name));return[o,new Proxy(e,{get:(l,c)=>c==="executionProviders"?d:Reflect.get(l,c)})]}});var Ji=U(()=>{"use strict";vr()});var ea,ta=U(()=>{"use strict";ea="1.20.0-dev.20241016-2b8fc5529b"});var ra,Ne,Nn=U(()=>{"use strict";ta();ra="warning",Ne={wasm:{},webgl:{},webgpu:{},versions:{common:ea},set logLevel(e){if(e!==void 0){if(typeof e!="string"||["verbose","info","warning","error","fatal"].indexOf(e)===-1)throw new Error(`Unsupported logging level: ${e}`);ra=e}},get logLevel(){return ra}};Object.defineProperty(Ne,"logLevel",{enumerable:!0})});var _e,na=U(()=>{"use strict";Nn();_e=Ne});var oa,ia,aa=U(()=>{"use strict";oa=(e,t)=>{let r=typeof document<"u"?document.createElement("canvas"):new OffscreenCanvas(1,1);r.width=e.dims[3],r.height=e.dims[2];let n=r.getContext("2d");if(n!=null){let o,i;t?.tensorLayout!==void 0&&t.tensorLayout==="NHWC"?(o=e.dims[2],i=e.dims[3]):(o=e.dims[3],i=e.dims[2]);let a=t?.format!==void 0?t.format:"RGB",d=t?.norm,l,c;d===void 0||d.mean===void 0?l=[255,255,255,255]:typeof d.mean=="number"?l=[d.mean,d.mean,d.mean,d.mean]:(l=[d.mean[0],d.mean[1],d.mean[2],0],d.mean[3]!==void 0&&(l[3]=d.mean[3])),d===void 0||d.bias===void 0?c=[0,0,0,0]:typeof d.bias=="number"?c=[d.bias,d.bias,d.bias,d.bias]:(c=[d.bias[0],d.bias[1],d.bias[2],0],d.bias[3]!==void 0&&(c[3]=d.bias[3]));let m=i*o,u=0,h=m,w=m*2,g=-1;a==="RGBA"?(u=0,h=m,w=m*2,g=m*3):a==="RGB"?(u=0,h=m,w=m*2):a==="RBG"&&(u=0,w=m,h=m*2);for(let y=0;y{let r=typeof document<"u"?document.createElement("canvas").getContext("2d"):new OffscreenCanvas(1,1).getContext("2d"),n;if(r!=null){let o,i,a;t?.tensorLayout!==void 0&&t.tensorLayout==="NHWC"?(o=e.dims[2],i=e.dims[1],a=e.dims[3]):(o=e.dims[3],i=e.dims[2],a=e.dims[1]);let d=t!==void 0&&t.format!==void 0?t.format:"RGB",l=t?.norm,c,m;l===void 0||l.mean===void 0?c=[255,255,255,255]:typeof l.mean=="number"?c=[l.mean,l.mean,l.mean,l.mean]:(c=[l.mean[0],l.mean[1],l.mean[2],255],l.mean[3]!==void 0&&(c[3]=l.mean[3])),l===void 0||l.bias===void 0?m=[0,0,0,0]:typeof l.bias=="number"?m=[l.bias,l.bias,l.bias,l.bias]:(m=[l.bias[0],l.bias[1],l.bias[2],0],l.bias[3]!==void 0&&(m[3]=l.bias[3]));let u=i*o;if(t!==void 0&&(t.format!==void 0&&a===4&&t.format!=="RGBA"||a===3&&t.format!=="RGB"&&t.format!=="BGR"))throw new Error("Tensor format doesn't match input tensor dims");let h=4,w=0,g=1,y=2,S=3,$=0,v=u,x=u*2,T=-1;d==="RGBA"?($=0,v=u,x=u*2,T=u*3):d==="RGB"?($=0,v=u,x=u*2):d==="RBG"&&($=0,x=u,v=u*2),n=r.createImageData(o,i);for(let C=0;C{"use strict";$r();Ln=(e,t)=>{if(e===void 0)throw new Error("Image buffer must be defined");if(t.height===void 0||t.width===void 0)throw new Error("Image height and width must be defined");if(t.tensorLayout==="NHWC")throw new Error("NHWC Tensor layout is not supported yet");let{height:r,width:n}=t,o=t.norm??{mean:255,bias:0},i,a;typeof o.mean=="number"?i=[o.mean,o.mean,o.mean,o.mean]:i=[o.mean[0],o.mean[1],o.mean[2],o.mean[3]??255],typeof o.bias=="number"?a=[o.bias,o.bias,o.bias,o.bias]:a=[o.bias[0],o.bias[1],o.bias[2],o.bias[3]??0];let d=t.format!==void 0?t.format:"RGBA",l=t.tensorFormat!==void 0&&t.tensorFormat!==void 0?t.tensorFormat:"RGB",c=r*n,m=l==="RGBA"?new Float32Array(c*4):new Float32Array(c*3),u=4,h=0,w=1,g=2,y=3,S=0,$=c,v=c*2,x=-1;d==="RGB"&&(u=3,h=0,w=1,g=2,y=-1),l==="RGBA"?x=c*3:l==="RBG"?(S=0,v=c,$=c*2):l==="BGR"&&(v=0,$=c,S=c*2);for(let C=0;C{let r=typeof HTMLImageElement<"u"&&e instanceof HTMLImageElement,n=typeof ImageData<"u"&&e instanceof ImageData,o=typeof ImageBitmap<"u"&&e instanceof ImageBitmap,i=typeof e=="string",a,d=t??{},l=()=>{if(typeof document<"u")return document.createElement("canvas");if(typeof OffscreenCanvas<"u")return new OffscreenCanvas(1,1);throw new Error("Canvas is not supported")},c=m=>typeof HTMLCanvasElement<"u"&&m instanceof HTMLCanvasElement||m instanceof OffscreenCanvas?m.getContext("2d"):null;if(r){let m=l();m.width=e.width,m.height=e.height;let u=c(m);if(u!=null){let h=e.height,w=e.width;if(t!==void 0&&t.resizedHeight!==void 0&&t.resizedWidth!==void 0&&(h=t.resizedHeight,w=t.resizedWidth),t!==void 0){if(d=t,t.tensorFormat!==void 0)throw new Error("Image input config format must be RGBA for HTMLImageElement");d.tensorFormat="RGBA",d.height=h,d.width=w}else d.tensorFormat="RGBA",d.height=h,d.width=w;u.drawImage(e,0,0),a=u.getImageData(0,0,w,h).data}else throw new Error("Can not access image data")}else if(n){let m,u;if(t!==void 0&&t.resizedWidth!==void 0&&t.resizedHeight!==void 0?(m=t.resizedHeight,u=t.resizedWidth):(m=e.height,u=e.width),t!==void 0&&(d=t),d.format="RGBA",d.height=m,d.width=u,t!==void 0){let h=l();h.width=u,h.height=m;let w=c(h);if(w!=null)w.putImageData(e,0,0),a=w.getImageData(0,0,u,m).data;else throw new Error("Can not access image data")}else a=e.data}else if(o){if(t===void 0)throw new Error("Please provide image config with format for Imagebitmap");let m=l();m.width=e.width,m.height=e.height;let u=c(m);if(u!=null){let h=e.height,w=e.width;return u.drawImage(e,0,0,w,h),a=u.getImageData(0,0,w,h).data,d.height=h,d.width=w,Ln(a,d)}else throw new Error("Can not access image data")}else{if(i)return new Promise((m,u)=>{let h=l(),w=c(h);if(!e||!w)return u();let g=new Image;g.crossOrigin="Anonymous",g.src=e,g.onload=()=>{h.width=g.width,h.height=g.height,w.drawImage(g,0,0,h.width,h.height);let y=w.getImageData(0,0,h.width,h.height);d.height=h.height,d.width=h.width,m(Ln(y.data,d))}});throw new Error("Input data provided is not supported - aborted tensor creation")}if(a!==void 0)return Ln(a,d);throw new Error("Input data provided is not supported - aborted tensor creation")},ua=(e,t)=>{let{width:r,height:n,download:o,dispose:i}=t,a=[1,n,r,4];return new De({location:"texture",type:"float32",texture:e,dims:a,download:o,dispose:i})},da=(e,t)=>{let{dataType:r,dims:n,download:o,dispose:i}=t;return new De({location:"gpu-buffer",type:r??"float32",gpuBuffer:e,dims:n,download:o,dispose:i})},la=(e,t)=>{let{dataType:r,dims:n,download:o,dispose:i}=t;return new De({location:"ml-tensor",type:r??"float32",mlTensor:e,dims:n,download:o,dispose:i})},ca=(e,t,r)=>new De({location:"cpu-pinned",type:e,data:t,dims:r??[t.length]})});var Tt,Ft,ma,fa,ha=U(()=>{"use strict";Tt=new Map([["float32",Float32Array],["uint8",Uint8Array],["int8",Int8Array],["uint16",Uint16Array],["int16",Int16Array],["int32",Int32Array],["bool",Uint8Array],["float64",Float64Array],["uint32",Uint32Array],["int4",Uint8Array],["uint4",Uint8Array]]),Ft=new Map([[Float32Array,"float32"],[Uint8Array,"uint8"],[Int8Array,"int8"],[Uint16Array,"uint16"],[Int16Array,"int16"],[Int32Array,"int32"],[Float64Array,"float64"],[Uint32Array,"uint32"]]),ma=!1,fa=()=>{if(!ma){ma=!0;let e=typeof BigInt64Array<"u"&&BigInt64Array.from,t=typeof BigUint64Array<"u"&&BigUint64Array.from,r=typeof Float16Array<"u"&&Float16Array.from;e&&(Tt.set("int64",BigInt64Array),Ft.set(BigInt64Array,"int64")),t&&(Tt.set("uint64",BigUint64Array),Ft.set(BigUint64Array,"uint64")),r?(Tt.set("float16",Float16Array),Ft.set(Float16Array,"float16")):Tt.set("float16",Uint16Array)}}});var ga,ya,ba=U(()=>{"use strict";$r();ga=e=>{let t=1;for(let r=0;r{switch(e.location){case"cpu":return new De(e.type,e.data,t);case"cpu-pinned":return new De({location:"cpu-pinned",data:e.data,type:e.type,dims:t});case"texture":return new De({location:"texture",texture:e.texture,type:e.type,dims:t});case"gpu-buffer":return new De({location:"gpu-buffer",gpuBuffer:e.gpuBuffer,type:e.type,dims:t});case"ml-tensor":return new De({location:"ml-tensor",mlTensor:e.mlTensor,type:e.type,dims:t});default:throw new Error(`tensorReshape: tensor location ${e.location} is not supported`)}}});var De,$r=U(()=>{"use strict";aa();pa();ha();ba();De=class{constructor(t,r,n){fa();let o,i;if(typeof t=="object"&&"location"in t)switch(this.dataLocation=t.location,o=t.type,i=t.dims,t.location){case"cpu-pinned":{let d=Tt.get(o);if(!d)throw new TypeError(`unsupported type "${o}" to create tensor from pinned buffer`);if(!(t.data instanceof d))throw new TypeError(`buffer should be of type ${d.name}`);this.cpuData=t.data;break}case"texture":{if(o!=="float32")throw new TypeError(`unsupported type "${o}" to create tensor from texture`);this.gpuTextureData=t.texture,this.downloader=t.download,this.disposer=t.dispose;break}case"gpu-buffer":{if(o!=="float32"&&o!=="float16"&&o!=="int32"&&o!=="int64"&&o!=="uint32"&&o!=="uint8"&&o!=="bool"&&o!=="uint4"&&o!=="int4")throw new TypeError(`unsupported type "${o}" to create tensor from gpu buffer`);this.gpuBufferData=t.gpuBuffer,this.downloader=t.download,this.disposer=t.dispose;break}case"ml-tensor":{if(o!=="float32"&&o!=="float16"&&o!=="int32"&&o!=="int64"&&o!=="uint32"&&o!=="uint64"&&o!=="int8"&&o!=="uint8"&&o!=="bool")throw new TypeError(`unsupported type "${o}" to create tensor from MLTensor`);this.mlTensorData=t.mlTensor,this.downloader=t.download,this.disposer=t.dispose;break}default:throw new Error(`Tensor constructor: unsupported location '${this.dataLocation}'`)}else{let d,l;if(typeof t=="string")if(o=t,l=n,t==="string"){if(!Array.isArray(r))throw new TypeError("A string tensor's data must be a string array.");d=r}else{let c=Tt.get(t);if(c===void 0)throw new TypeError(`Unsupported tensor type: ${t}.`);if(Array.isArray(r)){if(t==="float16"&&c===Uint16Array||t==="uint4"||t==="int4")throw new TypeError(`Creating a ${t} tensor from number array is not supported. Please use ${c.name} as data.`);t==="uint64"||t==="int64"?d=c.from(r,BigInt):d=c.from(r)}else if(r instanceof c)d=r;else if(r instanceof Uint8ClampedArray)if(t==="uint8")d=Uint8Array.from(r);else throw new TypeError("A Uint8ClampedArray tensor's data must be type of uint8");else throw new TypeError(`A ${o} tensor's data must be type of ${c}`)}else if(l=r,Array.isArray(t)){if(t.length===0)throw new TypeError("Tensor type cannot be inferred from an empty array.");let c=typeof t[0];if(c==="string")o="string",d=t;else if(c==="boolean")o="bool",d=Uint8Array.from(t);else throw new TypeError(`Invalid element type of data array: ${c}.`)}else if(t instanceof Uint8ClampedArray)o="uint8",d=Uint8Array.from(t);else{let c=Ft.get(t.constructor);if(c===void 0)throw new TypeError(`Unsupported type for tensor data: ${t.constructor}.`);o=c,d=t}if(l===void 0)l=[d.length];else if(!Array.isArray(l))throw new TypeError("A tensor's dims must be a number array");i=l,this.cpuData=d,this.dataLocation="cpu"}let a=ga(i);if(this.cpuData&&a!==this.cpuData.length&&!((o==="uint4"||o==="int4")&&Math.ceil(a/2)===this.cpuData.length))throw new Error(`Tensor's size(${a}) does not match data length(${this.cpuData.length}).`);this.type=o,this.dims=i,this.size=a}static async fromImage(t,r){return sa(t,r)}static fromTexture(t,r){return ua(t,r)}static fromGpuBuffer(t,r){return da(t,r)}static fromMLTensor(t,r){return la(t,r)}static fromPinnedBuffer(t,r,n){return ca(t,r,n)}toDataURL(t){return oa(this,t)}toImageData(t){return ia(this,t)}get data(){if(this.ensureValid(),!this.cpuData)throw new Error("The data is not on CPU. Use `getData()` to download GPU data to CPU, or use `texture` or `gpuBuffer` property to access the GPU data directly.");return this.cpuData}get location(){return this.dataLocation}get texture(){if(this.ensureValid(),!this.gpuTextureData)throw new Error("The data is not stored as a WebGL texture.");return this.gpuTextureData}get gpuBuffer(){if(this.ensureValid(),!this.gpuBufferData)throw new Error("The data is not stored as a WebGPU buffer.");return this.gpuBufferData}get mlTensor(){if(this.ensureValid(),!this.mlTensorData)throw new Error("The data is not stored as a WebNN MLTensor.");return this.mlTensorData}async getData(t){switch(this.ensureValid(),this.dataLocation){case"cpu":case"cpu-pinned":return this.data;case"texture":case"gpu-buffer":case"ml-tensor":{if(!this.downloader)throw new Error("The current tensor is not created with a specified data downloader.");if(this.isDownloading)throw new Error("The current tensor is being downloaded.");try{this.isDownloading=!0;let r=await this.downloader();return this.downloader=void 0,this.dataLocation="cpu",this.cpuData=r,t&&this.disposer&&(this.disposer(),this.disposer=void 0),r}finally{this.isDownloading=!1}}default:throw new Error(`cannot get data from location: ${this.dataLocation}`)}}dispose(){if(this.isDownloading)throw new Error("The current tensor is being downloaded.");this.disposer&&(this.disposer(),this.disposer=void 0),this.cpuData=void 0,this.gpuTextureData=void 0,this.gpuBufferData=void 0,this.mlTensorData=void 0,this.downloader=void 0,this.isDownloading=void 0,this.dataLocation="none"}ensureValid(){if(this.dataLocation==="none")throw new Error("The tensor is disposed.")}reshape(t){if(this.ensureValid(),this.downloader||this.disposer)throw new Error("Cannot reshape a tensor that owns GPU resource.");return ya(this,t)}}});var Be,xr=U(()=>{"use strict";$r();Be=De});var Sr,wa,Le,Ve,Hn=U(()=>{"use strict";Nn();Sr=(e,t)=>{(typeof Ne.trace>"u"?!Ne.wasm.trace:!Ne.trace)||console.timeStamp(`${e}::ORT::${t}`)},wa=(e,t)=>{let r=new Error().stack?.split(/\r\n|\r|\n/g)||[],n=!1;for(let o=0;o{(typeof Ne.trace>"u"?!Ne.wasm.trace:!Ne.trace)||wa("BEGIN",e)},Ve=e=>{(typeof Ne.trace>"u"?!Ne.wasm.trace:!Ne.trace)||wa("END",e)}});var Tr,_a=U(()=>{"use strict";vr();xr();Hn();Tr=class e{constructor(t){this.handler=t}async run(t,r,n){Le();let o={},i={};if(typeof t!="object"||t===null||t instanceof Be||Array.isArray(t))throw new TypeError("'feeds' must be an object that use input names as keys and OnnxValue as corresponding values.");let a=!0;if(typeof r=="object"){if(r===null)throw new TypeError("Unexpected argument[1]: cannot be null.");if(r instanceof Be)throw new TypeError("'fetches' cannot be a Tensor");if(Array.isArray(r)){if(r.length===0)throw new TypeError("'fetches' cannot be an empty array.");a=!1;for(let c of r){if(typeof c!="string")throw new TypeError("'fetches' must be a string array or an object.");if(this.outputNames.indexOf(c)===-1)throw new RangeError(`'fetches' contains invalid output name: ${c}.`);o[c]=null}if(typeof n=="object"&&n!==null)i=n;else if(typeof n<"u")throw new TypeError("'options' must be an object.")}else{let c=!1,m=Object.getOwnPropertyNames(r);for(let u of this.outputNames)if(m.indexOf(u)!==-1){let h=r[u];(h===null||h instanceof Be)&&(c=!0,a=!1,o[u]=h)}if(c){if(typeof n=="object"&&n!==null)i=n;else if(typeof n<"u")throw new TypeError("'options' must be an object.")}else i=r}}else if(typeof r<"u")throw new TypeError("Unexpected argument[1]: must be 'fetches' or 'options'.");for(let c of this.inputNames)if(typeof t[c]>"u")throw new Error(`input '${c}' is missing in 'feeds'.`);if(a)for(let c of this.outputNames)o[c]=null;let d=await this.handler.run(t,o,i),l={};for(let c in d)if(Object.hasOwnProperty.call(d,c)){let m=d[c];m instanceof Be?l[c]=m:l[c]=new Be(m.type,m.data,m.dims)}return Ve(),l}async release(){return this.handler.dispose()}static async create(t,r,n,o){Le();let i,a={};if(typeof t=="string"){if(i=t,typeof r=="object"&&r!==null)a=r;else if(typeof r<"u")throw new TypeError("'options' must be an object.")}else if(t instanceof Uint8Array){if(i=t,typeof r=="object"&&r!==null)a=r;else if(typeof r<"u")throw new TypeError("'options' must be an object.")}else if(t instanceof ArrayBuffer||typeof SharedArrayBuffer<"u"&&t instanceof SharedArrayBuffer){let m=t,u=0,h=t.byteLength;if(typeof r=="object"&&r!==null)a=r;else if(typeof r=="number"){if(u=r,!Number.isSafeInteger(u))throw new RangeError("'byteOffset' must be an integer.");if(u<0||u>=m.byteLength)throw new RangeError(`'byteOffset' is out of range [0, ${m.byteLength}).`);if(h=t.byteLength-u,typeof n=="number"){if(h=n,!Number.isSafeInteger(h))throw new RangeError("'byteLength' must be an integer.");if(h<=0||u+h>m.byteLength)throw new RangeError(`'byteLength' is out of range (0, ${m.byteLength-u}].`);if(typeof o=="object"&&o!==null)a=o;else if(typeof o<"u")throw new TypeError("'options' must be an object.")}else if(typeof n<"u")throw new TypeError("'byteLength' must be a number.")}else if(typeof r<"u")throw new TypeError("'options' must be an object.");i=new Uint8Array(m,u,h)}else throw new TypeError("Unexpected argument[0]: must be 'path' or 'buffer'.");let[d,l]=await _r(a),c=await d.createInferenceSessionHandler(i,l);return Ve(),new e(c)}startProfiling(){this.handler.startProfiling()}endProfiling(){this.handler.endProfiling()}get inputNames(){return this.handler.inputNames}get outputNames(){return this.handler.outputNames}}});var Ip,va=U(()=>{"use strict";_a();Ip=Tr});var $a=U(()=>{"use strict"});var xa=U(()=>{"use strict"});var Sa=U(()=>{"use strict"});var Ta=U(()=>{"use strict"});var Cp,Ir,Ia=U(()=>{"use strict";vr();xr();Cp="Training backend could not be resolved. Make sure you're using the correct configuration & WebAssembly files.",Ir=class e{constructor(t,r,n){this.handler=t,this.hasOptimizerModel=r,this.hasEvalModel=n}get trainingInputNames(){return this.handler.inputNames}get trainingOutputNames(){return this.handler.outputNames}get evalInputNames(){if(this.hasEvalModel)return this.handler.evalInputNames;throw new Error("This training session has no evalModel loaded.")}get evalOutputNames(){if(this.hasEvalModel)return this.handler.evalOutputNames;throw new Error("This training session has no evalModel loaded.")}static async create(t,r){let n=t.evalModel||"",o=t.optimizerModel||"",i=r||{},[a,d]=await _r(i);if(a.createTrainingSessionHandler){let l=await a.createTrainingSessionHandler(t.checkpointState,t.trainModel,n,o,d);return new e(l,!!t.optimizerModel,!!t.evalModel)}else throw new Error(Cp)}typeNarrowingForRunStep(t,r,n,o,i){let a={},d={};if(typeof n!="object"||n===null||n instanceof Be||Array.isArray(n))throw new TypeError("'feeds' must be an object that use input names as keys and OnnxValue as corresponding values.");let l=!0;if(typeof o=="object"){if(o===null)throw new TypeError("Unexpected argument[1]: cannot be null.");if(o instanceof Be)throw new TypeError("'fetches' cannot be a Tensor");if(Array.isArray(o)){if(o.length===0)throw new TypeError("'fetches' cannot be an empty array.");l=!1;for(let c of o){if(typeof c!="string")throw new TypeError("'fetches' must be a string array or an object.");if(r.indexOf(c)===-1)throw new RangeError(`'fetches' contains invalid output name: ${c}.`);a[c]=null}if(typeof i=="object"&&i!==null)d=i;else if(typeof i<"u")throw new TypeError("'options' must be an object.")}else{let c=!1,m=Object.getOwnPropertyNames(o);for(let u of r)if(m.indexOf(u)!==-1){let h=o[u];(h===null||h instanceof Be)&&(c=!0,l=!1,a[u]=h)}if(c){if(typeof i=="object"&&i!==null)d=i;else if(typeof i<"u")throw new TypeError("'options' must be an object.")}else d=o}}else if(typeof o<"u")throw new TypeError("Unexpected argument[1]: must be 'fetches' or 'options'.");for(let c of t)if(typeof n[c]>"u")throw new Error(`input '${c}' is missing in 'feeds'.`);if(l)for(let c of r)a[c]=null;return[a,d]}convertHandlerReturnTypeToMapOfTensors(t){let r={};for(let n in t)if(Object.hasOwnProperty.call(t,n)){let o=t[n];o instanceof Be?r[n]=o:r[n]=new Be(o.type,o.data,o.dims)}return r}async lazyResetGrad(){await this.handler.lazyResetGrad()}async runTrainStep(t,r,n){let[o,i]=this.typeNarrowingForRunStep(this.trainingInputNames,this.trainingOutputNames,t,r,n),a=await this.handler.runTrainStep(t,o,i);return this.convertHandlerReturnTypeToMapOfTensors(a)}async runOptimizerStep(t){if(this.hasOptimizerModel)await this.handler.runOptimizerStep(t||{});else throw new Error("This TrainingSession has no OptimizerModel loaded.")}async runEvalStep(t,r,n){if(this.hasEvalModel){let[o,i]=this.typeNarrowingForRunStep(this.evalInputNames,this.evalOutputNames,t,r,n),a=await this.handler.runEvalStep(t,o,i);return this.convertHandlerReturnTypeToMapOfTensors(a)}else throw new Error("This TrainingSession has no EvalModel loaded.")}async getParametersSize(t=!0){return this.handler.getParametersSize(t)}async loadParametersBuffer(t,r=!0){let n=await this.getParametersSize(r);if(t.length!==4*n)throw new Error("Size of the buffer passed into loadParametersBuffer must match the number of parameters in the model. Please use getParametersSize method to check.");return this.handler.loadParametersBuffer(t,r)}async getContiguousParameters(t=!0){return this.handler.getContiguousParameters(t)}async release(){return this.handler.dispose()}}});var Ap,Ca=U(()=>{"use strict";Ia();Ap=Ir});var Gn={};Gt(Gn,{InferenceSession:()=>Ip,TRACE:()=>Sr,TRACE_FUNC_BEGIN:()=>Le,TRACE_FUNC_END:()=>Ve,Tensor:()=>Be,TrainingSession:()=>Ap,env:()=>_e,registerBackend:()=>St});var Ke=U(()=>{"use strict";Ji();na();va();xr();$a();xa();Hn();Sa();Ta();Ca()});var Cr=U(()=>{"use strict"});var Pa={};Gt(Pa,{default:()=>kp});var ka,Ea,kp,za=U(()=>{"use strict";Fn();gt();qt();ka="ort-wasm-proxy-worker",Ea=globalThis.self?.name===ka;Ea&&(self.onmessage=e=>{let{type:t,in:r}=e.data;try{switch(t){case"init-wasm":Ar(r.wasm).then(()=>{kr(r).then(()=>{postMessage({type:t})},n=>{postMessage({type:t,err:n})})},n=>{postMessage({type:t,err:n})});break;case"init-ep":{let{epName:n,env:o}=r;Er(o,n).then(()=>{postMessage({type:t})},i=>{postMessage({type:t,err:i})});break}case"copy-from":{let{buffer:n}=r,o=jt(n);postMessage({type:t,out:o});break}case"create":{let{model:n,options:o}=r;Pr(n,o).then(i=>{postMessage({type:t,out:i})},i=>{postMessage({type:t,err:i})});break}case"release":zr(r),postMessage({type:t});break;case"run":{let{sessionId:n,inputIndices:o,inputs:i,outputIndices:a,options:d}=r;Or(n,o,i,a,new Array(a.length).fill(null),d).then(l=>{l.some(c=>c[3]!=="cpu")?postMessage({type:t,err:"Proxy does not support non-cpu tensor location."}):postMessage({type:t,out:l},Br([...i,...l]))},l=>{postMessage({type:t,err:l})});break}case"end-profiling":Dr(r),postMessage({type:t});break;default:}}catch(n){postMessage({type:t,err:n})}});kp=Ea?null:e=>new Worker(e??Ut,{type:"module",name:ka})});var Da={};Gt(Da,{default:()=>Ep});var qn,Oa,Ep,Ba=U(()=>{"use strict";Oa=(qn=import.meta.url,async function(e={}){function t(){return ue.buffer!=ce.buffer&&Ce(),ce}function r(){return ue.buffer!=ce.buffer&&Ce(),q}function n(){return ue.buffer!=ce.buffer&&Ce(),le}function o(){return ue.buffer!=ce.buffer&&Ce(),re}function i(){return ue.buffer!=ce.buffer&&Ce(),ne}function a(){return ue.buffer!=ce.buffer&&Ce(),oe}function d(){return ue.buffer!=ce.buffer&&Ce(),R}function l(){return ue.buffer!=ce.buffer&&Ce(),Re}var c,m,u=Object.assign({},e),h=new Promise((s,p)=>{c=s,m=p}),w=typeof window=="object",g=typeof importScripts=="function",y=g&&self.name=="em-pthread";u.mountExternalData=(s,p)=>{s.startsWith("./")&&(s=s.substring(2)),(u.Eb||(u.Eb=new Map)).set(s,p)},u.unmountExternalData=()=>{delete u.Eb};var S=globalThis.SharedArrayBuffer??new WebAssembly.Memory({initial:0,maximum:0,shared:!0}).buffer.constructor;let $=()=>{let s=(f,b,_)=>(...I)=>{let O=et,B=b?.();I=f(...I);let L=b?.();return B!==L&&(f=L,_(B),b=_=null),et!=O?new Promise((H,Q)=>{Pn={resolve:H,reject:Q}}):I},p=f=>async(...b)=>{try{if(u.Fb)throw Error("Session already started");let _=u.Fb={fc:b[0],errors:[]},I=await f(...b);if(u.Fb!==_)throw Error("Session mismatch");u.Gb?.flush();let O=_.errors;if(0L),0u._OrtCreateSession,f=>u._OrtCreateSession=f),u._OrtRun=p(s(u._OrtRun,()=>u._OrtRun,f=>u._OrtRun=f)),u._OrtRunWithBinding=p(s(u._OrtRunWithBinding,()=>u._OrtRunWithBinding,f=>u._OrtRunWithBinding=f)),u._OrtBindInput=s(u._OrtBindInput,()=>u._OrtBindInput,f=>u._OrtBindInput=f),$=void 0};u.jsepInit=(s,p)=>{if($?.(),s==="webgpu"){[u.Gb,u.Ub,u.Yb,u.Nb,u.Xb,u.jb,u.Zb,u.bc,u.Vb,u.Wb,u.$b]=p;let f=u.Gb;u.jsepRegisterBuffer=(b,_,I,O)=>f.registerBuffer(b,_,I,O),u.jsepGetBuffer=b=>f.getBuffer(b),u.jsepCreateDownloader=(b,_,I)=>f.createDownloader(b,_,I),u.jsepOnCreateSession=b=>{f.onCreateSession(b)},u.jsepOnReleaseSession=b=>{f.onReleaseSession(b)},u.jsepOnRunStart=b=>f.onRunStart(b),u.cc=(b,_)=>{f.upload(b,_)}}else if(s==="webnn"){[u.Gb,u.ac,u.Ob,u.jsepEnsureTensor,u.dc,u.jsepDownloadTensor]=p,u.jsepReleaseTensorId=u.Ob;let f=u.Gb;u.jsepOnRunStart=b=>f.onRunStart(b),u.jsepRegisterMLContext=(b,_)=>{f.registerMLContext(b,_)},u.jsepOnReleaseSession=b=>{f.onReleaseSession(b)},u.jsepCreateMLTensorDownloader=(b,_)=>f.createMLTensorDownloader(b,_),u.jsepRegisterMLTensor=(b,_,I)=>f.registerMLTensor(b,_,I),u.qc=(b,_,I,O,B)=>f.registerMLConstant(b,_,I,O,B,u.Eb)}};var v,x,T=Object.assign({},u),C="./this.program",A=(s,p)=>{throw p},P="";(w||g)&&(g?P=self.location.href:typeof document<"u"&&document.currentScript&&(P=document.currentScript.src),qn&&(P=qn),P=P.startsWith("blob:")?"":P.substr(0,P.replace(/[?#].*/,"").lastIndexOf("/")+1),g&&(x=s=>{var p=new XMLHttpRequest;return p.open("GET",s,!1),p.responseType="arraybuffer",p.send(null),new Uint8Array(p.response)}),v=(s,p,f)=>{var b=new XMLHttpRequest;b.open("GET",s,!0),b.responseType="arraybuffer",b.onload=()=>{b.status==200||b.status==0&&b.response?p(b.response):f()},b.onerror=f,b.send(null)});var D,W=console.log.bind(console),N=console.error.bind(console),j=W,Y=N;if(Object.assign(u,T),T=null,y){let s=function(p){try{var f=p.data,b=f.cmd;if(b==="load"){let _=[];self.onmessage=I=>_.push(I),self.startWorker=()=>{postMessage({cmd:"loaded"});for(let I of _)s(I);self.onmessage=s};for(let I of f.handlers)u[I]&&!u[I].proxy||(u[I]=(...O)=>{postMessage({Mb:"callHandler",oc:I,args:O})},I=="print"&&(j=u[I]),I=="printErr"&&(Y=u[I]));ue=f.wasmMemory,Ce(),Z(f.wasmModule)}else if(b==="run"){Bn(f.pthread_ptr,0,0,1,0,0),An(f.pthread_ptr),ic(),Go(),te||(Ni(),te=!0);try{ac(f.start_routine,f.arg)}catch(_){if(_!="unwind")throw _}}else b==="cancel"?Rt()&&gr(-1):f.target!=="setimmediate"&&(b==="checkMailbox"?te&&sr():b&&(Y(`worker: received unknown command ${b}`),Y(f)))}catch(_){throw Li(),_}};var jh=s,Z,te=!1;Y=function(...p){p=p.join(" "),console.error(p)},self.alert=function(...p){postMessage({Mb:"alert",text:p.join(" "),rc:Rt()})},u.instantiateWasm=(p,f)=>new Promise(b=>{Z=_=>{_=new WebAssembly.Instance(_,Vo()),f(_),b()}}),self.onunhandledrejection=p=>{throw p.reason||p},self.onmessage=s}u.wasmBinary&&(D=u.wasmBinary);var ue,K,de,ce,q,le,re,ne,oe,R,G,ye,Re,$e=!1;function Ce(){var s=ue.buffer;u.HEAP8=ce=new Int8Array(s),u.HEAP16=le=new Int16Array(s),u.HEAPU8=q=new Uint8Array(s),u.HEAPU16=re=new Uint16Array(s),u.HEAP32=ne=new Int32Array(s),u.HEAPU32=oe=new Uint32Array(s),u.HEAPF32=R=new Float32Array(s),u.HEAPF64=Re=new Float64Array(s),u.HEAP64=G=new BigInt64Array(s),u.HEAPU64=ye=new BigUint64Array(s)}if(!y){if(!((ue=new WebAssembly.Memory({initial:256,maximum:65536,shared:!0})).buffer instanceof S))throw Y("requested a shared WebAssembly.Memory but the returned buffer is not a SharedArrayBuffer, indicating that while the browser has SharedArrayBuffer it does not have WebAssembly threads support - you may need to set a flag"),Error("bad memory");Ce()}var bt=[],Ae=[],Me=[],Ue=0,zt=null,wt=null;function Do(){if(--Ue==0&&(zt!==null&&(clearInterval(zt),zt=null),wt)){var s=wt;wt=null,s()}}function Ot(s){throw Y(s="Aborted("+s+")"),$e=!0,de=1,s=new WebAssembly.RuntimeError(s+". Build with -sASSERTIONS for more info."),m(s),s}var gn,Bo=s=>s.startsWith("data:application/octet-stream;base64,"),Mo=s=>s.startsWith("file://");function Ro(s){if(s==gn&&D)return new Uint8Array(D);if(x)return x(s);throw"both async and sync fetching of the wasm failed"}function Uo(s,p,f){return function(b){if(!D&&(w||g)){if(typeof fetch=="function"&&!Mo(b))return fetch(b,{credentials:"same-origin"}).then(_=>{if(!_.ok)throw`failed to load wasm binary file at '${b}'`;return _.arrayBuffer()}).catch(()=>Ro(b));if(v)return new Promise((_,I)=>{v(b,O=>_(new Uint8Array(O)),I)})}return Promise.resolve().then(()=>Ro(b))}(s).then(b=>WebAssembly.instantiate(b,p)).then(f,b=>{Y(`failed to asynchronously prepare wasm: ${b}`),Ot(b)})}function Vo(){return{a:{O:oc,Aa:nc,b:uc,aa:Ko,B:Qo,qa:Zo,Y:ei,_:ti,ra:ri,oa:ni,ha:oi,na:ii,L:ai,Z:si,W:ui,pa:di,X:li,wa:dc,F:cc,Q:pc,P:fc,E:gc,u:yc,q:bc,G:wc,A:Ic,R:Cc,ua:Ac,ka:kc,U:Ec,ba:Pc,H:zc,ja:An,ta:Oc,t:Dc,x:Rc,n:Uc,l:Wc,c:In,o:Nc,j:Gc,w:Fc,p:qc,g:jc,s:Kc,m:Yc,e:Xc,k:Qc,i:Zc,h:Jc,d:ep,ea:tp,fa:rp,ga:np,ca:Si,da:Ti,T:op,f:ip,D:ap,I:sp,M:up,y:dp,sa:lp,V:cp,v:Ci,z:pp,N:mp,S:fp,za:hp,ya:gp,la:Ei,ma:Pi,$:vn,C:zi,K:Oi,ia:Di,J:Bi,a:ue,xa:_n,va:Ui,r:wp}}}var yn={867364:(s,p,f,b,_)=>{if(u===void 0||!u.Eb)return 1;if((s=ze(s>>>0)).startsWith("./")&&(s=s.substring(2)),!(s=u.Eb.get(s)))return 2;if(b>>>=0,(p>>>=0)+(f>>>=0)>s.byteLength)return 3;try{let I=s.subarray(p,p+f);switch(_){case 0:r().set(I,b>>>0);break;case 1:u.cc(b,I);break;default:return 4}return 0}catch{return 4}},868047:(s,p,f)=>{u.dc(s,r().subarray(p>>>0,p+f>>>0))},868110:()=>u.ac(),868151:s=>{u.Ob(s)},868187:()=>{u.Vb()},868218:()=>{u.Wb()},868247:()=>{u.$b()},868272:s=>u.Ub(s),868305:s=>u.Yb(s),868337:(s,p,f)=>{u.Nb(s,p,f,!0)},868376:(s,p,f)=>{u.Nb(s,p,f)},868409:()=>typeof wasmOffsetConverter<"u",868466:s=>{u.jb("Abs",s,void 0)},868517:s=>{u.jb("Neg",s,void 0)},868568:s=>{u.jb("Floor",s,void 0)},868621:s=>{u.jb("Ceil",s,void 0)},868673:s=>{u.jb("Reciprocal",s,void 0)},868731:s=>{u.jb("Sqrt",s,void 0)},868783:s=>{u.jb("Exp",s,void 0)},868834:s=>{u.jb("Erf",s,void 0)},868885:s=>{u.jb("Sigmoid",s,void 0)},868940:(s,p,f)=>{u.jb("HardSigmoid",s,{alpha:p,beta:f})},869019:s=>{u.jb("Log",s,void 0)},869070:s=>{u.jb("Sin",s,void 0)},869121:s=>{u.jb("Cos",s,void 0)},869172:s=>{u.jb("Tan",s,void 0)},869223:s=>{u.jb("Asin",s,void 0)},869275:s=>{u.jb("Acos",s,void 0)},869327:s=>{u.jb("Atan",s,void 0)},869379:s=>{u.jb("Sinh",s,void 0)},869431:s=>{u.jb("Cosh",s,void 0)},869483:s=>{u.jb("Asinh",s,void 0)},869536:s=>{u.jb("Acosh",s,void 0)},869589:s=>{u.jb("Atanh",s,void 0)},869642:s=>{u.jb("Tanh",s,void 0)},869694:s=>{u.jb("Not",s,void 0)},869745:(s,p,f)=>{u.jb("Clip",s,{min:p,max:f})},869814:s=>{u.jb("Clip",s,void 0)},869866:(s,p)=>{u.jb("Elu",s,{alpha:p})},869924:s=>{u.jb("Gelu",s,void 0)},869976:s=>{u.jb("Relu",s,void 0)},870028:(s,p)=>{u.jb("LeakyRelu",s,{alpha:p})},870092:(s,p)=>{u.jb("ThresholdedRelu",s,{alpha:p})},870162:(s,p)=>{u.jb("Cast",s,{to:p})},870220:s=>{u.jb("Add",s,void 0)},870271:s=>{u.jb("Sub",s,void 0)},870322:s=>{u.jb("Mul",s,void 0)},870373:s=>{u.jb("Div",s,void 0)},870424:s=>{u.jb("Pow",s,void 0)},870475:s=>{u.jb("Equal",s,void 0)},870528:s=>{u.jb("Greater",s,void 0)},870583:s=>{u.jb("GreaterOrEqual",s,void 0)},870645:s=>{u.jb("Less",s,void 0)},870697:s=>{u.jb("LessOrEqual",s,void 0)},870756:(s,p,f,b,_)=>{u.jb("ReduceMean",s,{keepDims:!!p,noopWithEmptyAxes:!!f,axes:b?Array.from(i().subarray(b>>>0,_>>>0)):[]})},870915:(s,p,f,b,_)=>{u.jb("ReduceMax",s,{keepDims:!!p,noopWithEmptyAxes:!!f,axes:b?Array.from(i().subarray(b>>>0,_>>>0)):[]})},871073:(s,p,f,b,_)=>{u.jb("ReduceMin",s,{keepDims:!!p,noopWithEmptyAxes:!!f,axes:b?Array.from(i().subarray(b>>>0,_>>>0)):[]})},871231:(s,p,f,b,_)=>{u.jb("ReduceProd",s,{keepDims:!!p,noopWithEmptyAxes:!!f,axes:b?Array.from(i().subarray(b>>>0,_>>>0)):[]})},871390:(s,p,f,b,_)=>{u.jb("ReduceSum",s,{keepDims:!!p,noopWithEmptyAxes:!!f,axes:b?Array.from(i().subarray(b>>>0,_>>>0)):[]})},871548:(s,p,f,b,_)=>{u.jb("ReduceL1",s,{keepDims:!!p,noopWithEmptyAxes:!!f,axes:b?Array.from(i().subarray(b>>>0,_>>>0)):[]})},871705:(s,p,f,b,_)=>{u.jb("ReduceL2",s,{keepDims:!!p,noopWithEmptyAxes:!!f,axes:b?Array.from(i().subarray(b>>>0,_>>>0)):[]})},871862:(s,p,f,b,_)=>{u.jb("ReduceLogSum",s,{keepDims:!!p,noopWithEmptyAxes:!!f,axes:b?Array.from(i().subarray(b>>>0,_>>>0)):[]})},872023:(s,p,f,b,_)=>{u.jb("ReduceSumSquare",s,{keepDims:!!p,noopWithEmptyAxes:!!f,axes:b?Array.from(i().subarray(b>>>0,_>>>0)):[]})},872187:(s,p,f,b,_)=>{u.jb("ReduceLogSumExp",s,{keepDims:!!p,noopWithEmptyAxes:!!f,axes:b?Array.from(i().subarray(b>>>0,_>>>0)):[]})},872351:s=>{u.jb("Where",s,void 0)},872404:(s,p,f)=>{u.jb("Transpose",s,{perm:p?Array.from(i().subarray(p>>>0,f>>>0)):[]})},872512:(s,p,f,b)=>{u.jb("DepthToSpace",s,{blocksize:p,mode:ze(f),format:b?"NHWC":"NCHW"})},872645:(s,p,f,b)=>{u.jb("DepthToSpace",s,{blocksize:p,mode:ze(f),format:b?"NHWC":"NCHW"})},872778:(s,p,f,b,_,I,O,B,L,H,Q,fe,be,z,me)=>{u.jb("ConvTranspose",s,{format:L?"NHWC":"NCHW",autoPad:p,dilations:[f],group:b,kernelShape:[_],pads:[I,O],strides:[B],wIsConst:()=>!!t()[H>>>0],outputPadding:Q?Array.from(i().subarray(Q>>>0,fe>>>0)):[],outputShape:be?Array.from(i().subarray(be>>>0,z>>>0)):[],activation:ze(me)})},873179:(s,p,f,b,_,I,O,B,L,H,Q,fe,be,z)=>{u.jb("ConvTranspose",s,{format:B?"NHWC":"NCHW",autoPad:p,dilations:Array.from(i().subarray(f>>>0,2+(f>>>0)>>>0)),group:b,kernelShape:Array.from(i().subarray(_>>>0,2+(_>>>0)>>>0)),pads:Array.from(i().subarray(I>>>0,4+(I>>>0)>>>0)),strides:Array.from(i().subarray(O>>>0,2+(O>>>0)>>>0)),wIsConst:()=>!!t()[L>>>0],outputPadding:H?Array.from(i().subarray(H>>>0,Q>>>0)):[],outputShape:fe?Array.from(i().subarray(fe>>>0,be>>>0)):[],activation:ze(z)})},873744:(s,p,f,b,_,I,O,B,L,H,Q,fe,be,z,me)=>{u.jb("ConvTranspose",s,{format:L?"NHWC":"NCHW",autoPad:p,dilations:[f],group:b,kernelShape:[_],pads:[I,O],strides:[B],wIsConst:()=>!!t()[H>>>0],outputPadding:Q?Array.from(i().subarray(Q>>>0,fe>>>0)):[],outputShape:be?Array.from(i().subarray(be>>>0,z>>>0)):[],activation:ze(me)})},874145:(s,p,f,b,_,I,O,B,L,H,Q,fe,be,z)=>{u.jb("ConvTranspose",s,{format:B?"NHWC":"NCHW",autoPad:p,dilations:Array.from(i().subarray(f>>>0,2+(f>>>0)>>>0)),group:b,kernelShape:Array.from(i().subarray(_>>>0,2+(_>>>0)>>>0)),pads:Array.from(i().subarray(I>>>0,4+(I>>>0)>>>0)),strides:Array.from(i().subarray(O>>>0,2+(O>>>0)>>>0)),wIsConst:()=>!!t()[L>>>0],outputPadding:H?Array.from(i().subarray(H>>>0,Q>>>0)):[],outputShape:fe?Array.from(i().subarray(fe>>>0,be>>>0)):[],activation:ze(z)})},874710:(s,p)=>{u.jb("GlobalAveragePool",s,{format:p?"NHWC":"NCHW"})},874801:(s,p,f,b,_,I,O,B,L,H,Q,fe,be,z)=>{u.jb("AveragePool",s,{format:z?"NHWC":"NCHW",auto_pad:p,ceil_mode:f,count_include_pad:b,storage_order:_,dilations:I?Array.from(i().subarray(I>>>0,O>>>0)):[],kernel_shape:B?Array.from(i().subarray(B>>>0,L>>>0)):[],pads:H?Array.from(i().subarray(H>>>0,Q>>>0)):[],strides:fe?Array.from(i().subarray(fe>>>0,be>>>0)):[]})},875216:(s,p)=>{u.jb("GlobalAveragePool",s,{format:p?"NHWC":"NCHW"})},875307:(s,p,f,b,_,I,O,B,L,H,Q,fe,be,z)=>{u.jb("AveragePool",s,{format:z?"NHWC":"NCHW",auto_pad:p,ceil_mode:f,count_include_pad:b,storage_order:_,dilations:I?Array.from(i().subarray(I>>>0,O>>>0)):[],kernel_shape:B?Array.from(i().subarray(B>>>0,L>>>0)):[],pads:H?Array.from(i().subarray(H>>>0,Q>>>0)):[],strides:fe?Array.from(i().subarray(fe>>>0,be>>>0)):[]})},875722:(s,p)=>{u.jb("GlobalMaxPool",s,{format:p?"NHWC":"NCHW"})},875809:(s,p,f,b,_,I,O,B,L,H,Q,fe,be,z)=>{u.jb("MaxPool",s,{format:z?"NHWC":"NCHW",auto_pad:p,ceil_mode:f,count_include_pad:b,storage_order:_,dilations:I?Array.from(i().subarray(I>>>0,O>>>0)):[],kernel_shape:B?Array.from(i().subarray(B>>>0,L>>>0)):[],pads:H?Array.from(i().subarray(H>>>0,Q>>>0)):[],strides:fe?Array.from(i().subarray(fe>>>0,be>>>0)):[]})},876220:(s,p)=>{u.jb("GlobalMaxPool",s,{format:p?"NHWC":"NCHW"})},876307:(s,p,f,b,_,I,O,B,L,H,Q,fe,be,z)=>{u.jb("MaxPool",s,{format:z?"NHWC":"NCHW",auto_pad:p,ceil_mode:f,count_include_pad:b,storage_order:_,dilations:I?Array.from(i().subarray(I>>>0,O>>>0)):[],kernel_shape:B?Array.from(i().subarray(B>>>0,L>>>0)):[],pads:H?Array.from(i().subarray(H>>>0,Q>>>0)):[],strides:fe?Array.from(i().subarray(fe>>>0,be>>>0)):[]})},876718:(s,p,f,b,_)=>{u.jb("Gemm",s,{alpha:p,beta:f,transA:b,transB:_})},876822:s=>{u.jb("MatMul",s,void 0)},876876:(s,p,f,b)=>{u.jb("ArgMax",s,{keepDims:!!p,selectLastIndex:!!f,axis:b})},876984:(s,p,f,b)=>{u.jb("ArgMin",s,{keepDims:!!p,selectLastIndex:!!f,axis:b})},877092:(s,p)=>{u.jb("Softmax",s,{axis:p})},877155:(s,p)=>{u.jb("Concat",s,{axis:p})},877215:(s,p,f,b,_)=>{u.jb("Split",s,{axis:p,numOutputs:f,splitSizes:b?Array.from(i().subarray(b>>>0,_>>>0)):[]})},877355:s=>{u.jb("Expand",s,void 0)},877409:(s,p)=>{u.jb("Gather",s,{axis:Number(p)})},877480:(s,p)=>{u.jb("GatherElements",s,{axis:Number(p)})},877559:(s,p,f,b,_,I,O,B,L,H,Q)=>{u.jb("Resize",s,{antialias:p,axes:f?Array.from(i().subarray(f>>>0,b>>>0)):[],coordinateTransformMode:ze(_),cubicCoeffA:I,excludeOutside:O,extrapolationValue:B,keepAspectRatioPolicy:ze(L),mode:ze(H),nearestMode:ze(Q)})},877905:(s,p,f,b,_,I,O)=>{u.jb("Slice",s,{starts:p?Array.from(i().subarray(p>>>0,f>>>0)):[],ends:b?Array.from(i().subarray(b>>>0,_>>>0)):[],axes:I?Array.from(i().subarray(I>>>0,O>>>0)):[]})},878121:s=>{u.jb("Tile",s,void 0)},878173:(s,p,f)=>{u.jb("InstanceNormalization",s,{epsilon:p,format:f?"NHWC":"NCHW"})},878287:(s,p,f)=>{u.jb("InstanceNormalization",s,{epsilon:p,format:f?"NHWC":"NCHW"})},878401:s=>{u.jb("Range",s,void 0)},878454:(s,p)=>{u.jb("Einsum",s,{equation:ze(p)})},878535:(s,p,f,b,_)=>{u.jb("Pad",s,{mode:p,value:f,pads:b?Array.from(i().subarray(b>>>0,_>>>0)):[]})},878662:(s,p,f,b,_,I)=>{u.jb("BatchNormalization",s,{epsilon:p,momentum:f,spatial:!!_,trainingMode:!!b,format:I?"NHWC":"NCHW"})},878831:(s,p,f,b,_,I)=>{u.jb("BatchNormalization",s,{epsilon:p,momentum:f,spatial:!!_,trainingMode:!!b,format:I?"NHWC":"NCHW"})},879e3:(s,p,f)=>{u.jb("CumSum",s,{exclusive:Number(p),reverse:Number(f)})},879097:(s,p,f)=>{u.jb("DequantizeLinear",s,{axis:p,blockSize:f})},879187:(s,p,f,b,_,I,O,B,L)=>{u.jb("Attention",s,{numHeads:p,isUnidirectional:f,maskFilterValue:b,scale:_,doRotary:I,qkvHiddenSizes:O?Array.from(i().subarray(Number(B)>>>0,Number(B)+O>>>0)):[],pastPresentShareBuffer:!!L})},879459:s=>{u.jb("BiasAdd",s,void 0)},879514:s=>{u.jb("BiasSplitGelu",s,void 0)},879575:s=>{u.jb("FastGelu",s,void 0)},879631:(s,p,f,b,_,I,O,B,L,H,Q,fe,be,z,me,Se)=>{u.jb("Conv",s,{format:fe?"NHWC":"NCHW",auto_pad:p,dilations:f?Array.from(i().subarray(f>>>0,b>>>0)):[],group:_,kernel_shape:I?Array.from(i().subarray(I>>>0,O>>>0)):[],pads:B?Array.from(i().subarray(B>>>0,L>>>0)):[],strides:H?Array.from(i().subarray(H>>>0,Q>>>0)):[],w_is_const:()=>!!t()[be>>>0],activation:ze(z),activation_params:me?Array.from(d().subarray(me>>>0,Se>>>0)):[]})},880127:s=>{u.jb("Gelu",s,void 0)},880179:(s,p,f,b,_,I,O,B,L)=>{u.jb("GroupQueryAttention",s,{numHeads:p,kvNumHeads:f,scale:b,softcap:_,doRotary:I,rotaryInterleaved:O,smoothSoftmax:B,localWindowSize:L})},880396:(s,p,f,b)=>{u.jb("LayerNormalization",s,{axis:p,epsilon:f,simplified:!!b})},880507:(s,p,f,b)=>{u.jb("LayerNormalization",s,{axis:p,epsilon:f,simplified:!!b})},880618:(s,p,f,b,_,I)=>{u.jb("MatMulNBits",s,{k:p,n:f,accuracyLevel:b,bits:_,blockSize:I})},880745:(s,p,f,b,_,I)=>{u.jb("MultiHeadAttention",s,{numHeads:p,isUnidirectional:f,maskFilterValue:b,scale:_,doRotary:I})},880904:(s,p)=>{u.jb("QuickGelu",s,{alpha:p})},880968:(s,p,f,b,_)=>{u.jb("RotaryEmbedding",s,{interleaved:!!p,numHeads:f,rotaryEmbeddingDim:b,scale:_})},881107:(s,p,f)=>{u.jb("SkipLayerNormalization",s,{epsilon:p,simplified:!!f})},881209:(s,p,f)=>{u.jb("SkipLayerNormalization",s,{epsilon:p,simplified:!!f})},881311:(s,p,f,b)=>{u.jb("GatherBlockQuantized",s,{gatherAxis:p,quantizeAxis:f,blockSize:b})},881432:s=>{u.Zb(s)},881466:(s,p)=>u.bc(s,p,u.Fb.fc,u.Fb.errors)};function nc(s,p,f){return wi(async()=>{await u.Xb(s,p,f)})}function oc(){return typeof wasmOffsetConverter<"u"}function bn(s){this.name="ExitStatus",this.message=`Program terminated with exit(${s})`,this.status=s}var wn=s=>{s.terminate(),s.onmessage=()=>{}},Wo=s=>{pt.length==0&&(qo(),Fo(pt[0]));var p=pt.pop();if(!p)return 6;vt.push(p),Ze[s.Ab]=p,p.Ab=s.Ab;var f={cmd:"run",start_routine:s.hc,arg:s.Qb,pthread_ptr:s.Ab};return p.postMessage(f,s.mc),0},_t=0,xe=(s,p,...f)=>{for(var b=2*f.length,_=Un(),I=Rn(8*b),O=I>>>3,B=0;B>>0]=L)}return s=Hi(s,0,b,I,p),yr(_),s};function _n(s){if(y)return xe(0,1,s);if(de=s,!(0<_t)){for(var p of vt)wn(p);for(p of pt)wn(p);pt=[],vt=[],Ze=[],$e=!0}A(s,new bn(s))}function No(s){if(y)return xe(1,0,s);vn(s)}var vn=s=>{if(de=s,y)throw No(s),"unwind";_n(s)},pt=[],vt=[],Lo=[],Ze={},Ho=s=>{var p=s.Ab;delete Ze[p],pt.push(s),vt.splice(vt.indexOf(s),1),s.Ab=0,Mn(p)};function Go(){Lo.forEach(s=>s())}var Fo=s=>new Promise(p=>{s.onmessage=_=>{var I=(_=_.data).cmd;if(_.targetThread&&_.targetThread!=Rt()){var O=Ze[_.targetThread];O?O.postMessage(_,_.transferList):Y(`Internal error! Worker sent a message "${I}" to target pthread ${_.targetThread}, but that thread no longer exists!`)}else I==="checkMailbox"?sr():I==="spawnThread"?Wo(_):I==="cleanupThread"?Ho(Ze[_.thread]):I==="killThread"?(_=_.thread,I=Ze[_],delete Ze[_],wn(I),Mn(_),vt.splice(vt.indexOf(I),1),I.Ab=0):I==="cancelThread"?Ze[_.thread].postMessage({cmd:"cancel"}):I==="loaded"?(s.loaded=!0,p(s)):I==="alert"?alert(`Thread ${_.threadId}: ${_.text}`):_.target==="setimmediate"?s.postMessage(_):I==="callHandler"?u[_.handler](..._.args):I&&Y(`worker sent an unknown command ${I}`)},s.onerror=_=>{throw Y(`worker sent an error! ${_.filename}:${_.lineno}: ${_.message}`),_};var f,b=[];for(f of[])u.hasOwnProperty(f)&&b.push(f);s.postMessage({cmd:"load",handlers:b,wasmMemory:ue,wasmModule:K})});function qo(){var s=new Worker(new URL(import.meta.url),{type:"module",workerData:"em-pthread",name:"em-pthread"});pt.push(s)}var ar=s=>{for(;0{var s=Rt(),p=a()[s+52>>>2>>>0];s=a()[s+56>>>2>>>0],Fi(p,p-s),yr(p)},ac=(s,p)=>{_t=0,s=qi(s,p),0<_t?de=s:gr(s)};class sc{constructor(p){this.Jb=p-24}}function uc(s,p,f){var b=new sc(s>>>=0);throw p>>>=0,f>>>=0,a()[b.Jb+16>>>2>>>0]=0,a()[b.Jb+4>>>2>>>0]=p,a()[b.Jb+8>>>2>>>0]=f,s}function jo(s,p,f,b){return y?xe(2,1,s,p,f,b):Ko(s,p,f,b)}function Ko(s,p,f,b){if(s>>>=0,p>>>=0,f>>>=0,b>>>=0,S===void 0)return Y("Current environment does not support SharedArrayBuffer, pthreads are not available!"),6;var _=[];return y&&_.length===0?jo(s,p,f,b):(s={hc:f,Ab:s,Qb:b,mc:_},y?(s.Mb="spawnThread",postMessage(s,_),0):Wo(s))}var Yo=typeof TextDecoder<"u"?new TextDecoder("utf8"):void 0,Xo=(s,p,f)=>{var b=(p>>>=0)+f;for(f=p;s[f]&&!(f>=b);)++f;if(16(_=(240&_)==224?(15&_)<<12|I<<6|O:(7&_)<<18|I<<12|O<<6|63&s[p++])?b+=String.fromCharCode(_):(_-=65536,b+=String.fromCharCode(55296|_>>10,56320|1023&_))}}else b+=String.fromCharCode(_)}return b},ze=(s,p)=>(s>>>=0)?Xo(r(),s,p):"";function Qo(s,p,f){return y?xe(3,1,s,p,f):0}function Zo(s,p){if(y)return xe(4,1,s,p)}var $n=s=>{for(var p=0,f=0;f=b?p++:2047>=b?p+=2:55296<=b&&57343>=b?(p+=4,++f):p+=3}return p},Jo=(s,p,f,b)=>{if(!(0>>=0;b=f+b-1;for(var I=0;I=O&&(O=65536+((1023&O)<<10)|1023&s.charCodeAt(++I)),127>=O){if(f>=b)break;p[f++>>>0]=O}else{if(2047>=O){if(f+1>=b)break;p[f++>>>0]=192|O>>6}else{if(65535>=O){if(f+2>=b)break;p[f++>>>0]=224|O>>12}else{if(f+3>=b)break;p[f++>>>0]=240|O>>18,p[f++>>>0]=128|O>>12&63}p[f++>>>0]=128|O>>6&63}p[f++>>>0]=128|63&O}}return p[f>>>0]=0,f-_},Dt=(s,p,f)=>Jo(s,r(),p,f);function ei(s,p){if(y)return xe(5,1,s,p)}function ti(s,p,f){if(y)return xe(6,1,s,p,f)}function ri(s,p,f){return y?xe(7,1,s,p,f):0}function ni(s,p){if(y)return xe(8,1,s,p)}function oi(s,p,f){if(y)return xe(9,1,s,p,f)}function ii(s,p,f,b){if(y)return xe(10,1,s,p,f,b)}function ai(s,p,f,b){if(y)return xe(11,1,s,p,f,b)}function si(s,p,f,b){if(y)return xe(12,1,s,p,f,b)}function ui(s){if(y)return xe(13,1,s)}function di(s,p){if(y)return xe(14,1,s,p)}function li(s,p,f){if(y)return xe(15,1,s,p,f)}var ci,mt,dc=()=>{Ot("")},Je=s=>{for(var p="";r()[s>>>0];)p+=ci[r()[s++>>>0]];return p},xn={},Sn={},lc={};function ut(s,p,f={}){if(!("argPackAdvance"in p))throw new TypeError("registerType registeredInstance requires argPackAdvance");return function(b,_,I={}){var O=_.name;if(!b)throw new mt(`type "${O}" must have a positive integer typeid pointer`);if(Sn.hasOwnProperty(b)){if(I.Sb)return;throw new mt(`Cannot register type '${O}' twice`)}Sn[b]=_,delete lc[b],xn.hasOwnProperty(b)&&(_=xn[b],delete xn[b],_.forEach(B=>B()))}(s,p,f)}var pi=(s,p,f)=>{switch(p){case 1:return f?b=>t()[b>>>0]:b=>r()[b>>>0];case 2:return f?b=>n()[b>>>1>>>0]:b=>o()[b>>>1>>>0];case 4:return f?b=>i()[b>>>2>>>0]:b=>a()[b>>>2>>>0];case 8:return f?b=>G[b>>>3]:b=>ye[b>>>3];default:throw new TypeError(`invalid integer width (${p}): ${s}`)}};function cc(s,p,f){f>>>=0,ut(s>>>=0,{name:p=Je(p>>>0),fromWireType:b=>b,toWireType:function(b,_){if(typeof _!="bigint"&&typeof _!="number")throw _=_===null?"null":(b=typeof _)=="object"||b==="array"||b==="function"?_.toString():""+_,new TypeError(`Cannot convert "${_}" to ${this.name}`);return typeof _=="number"&&(_=BigInt(_)),_},argPackAdvance:ft,readValueFromPointer:pi(p,f,p.indexOf("u")==-1),Db:null})}var ft=8;function pc(s,p,f,b){ut(s>>>=0,{name:p=Je(p>>>0),fromWireType:function(_){return!!_},toWireType:function(_,I){return I?f:b},argPackAdvance:ft,readValueFromPointer:function(_){return this.fromWireType(r()[_>>>0])},Db:null})}var Tn=[],dt=[];function In(s){9<(s>>>=0)&&--dt[s+1]==0&&(dt[s]=void 0,Tn.push(s))}var qe=s=>{if(!s)throw new mt("Cannot use deleted val. handle = "+s);return dt[s]},je=s=>{switch(s){case void 0:return 2;case null:return 4;case!0:return 6;case!1:return 8;default:let p=Tn.pop()||dt.length;return dt[p]=s,dt[p+1]=1,p}};function Cn(s){return this.fromWireType(a()[s>>>2>>>0])}var mc={name:"emscripten::val",fromWireType:s=>{var p=qe(s);return In(s),p},toWireType:(s,p)=>je(p),argPackAdvance:ft,readValueFromPointer:Cn,Db:null};function fc(s){return ut(s>>>0,mc)}var hc=(s,p)=>{switch(p){case 4:return function(f){return this.fromWireType(d()[f>>>2>>>0])};case 8:return function(f){return this.fromWireType(l()[f>>>3>>>0])};default:throw new TypeError(`invalid float width (${p}): ${s}`)}};function gc(s,p,f){f>>>=0,ut(s>>>=0,{name:p=Je(p>>>0),fromWireType:b=>b,toWireType:(b,_)=>_,argPackAdvance:ft,readValueFromPointer:hc(p,f),Db:null})}function yc(s,p,f,b,_){if(s>>>=0,f>>>=0,p=Je(p>>>0),_===-1&&(_=4294967295),_=B=>B,b===0){var I=32-8*f;_=B=>B<>>I}var O=p.includes("unsigned")?function(B,L){return L>>>0}:function(B,L){return L};ut(s,{name:p,fromWireType:_,toWireType:O,argPackAdvance:ft,readValueFromPointer:pi(p,f,b!==0),Db:null})}function bc(s,p,f){function b(I){var O=a()[I>>>2>>>0];return I=a()[I+4>>>2>>>0],new _(t().buffer,I,O)}var _=[Int8Array,Uint8Array,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array,BigInt64Array,BigUint64Array][p];ut(s>>>=0,{name:f=Je(f>>>0),fromWireType:b,argPackAdvance:ft,readValueFromPointer:b},{Sb:!0})}function wc(s,p){s>>>=0;var f=(p=Je(p>>>0))==="std::string";ut(s,{name:p,fromWireType:function(b){var _=a()[b>>>2>>>0],I=b+4;if(f)for(var O=I,B=0;B<=_;++B){var L=I+B;if(B==_||r()[L>>>0]==0){if(O=ze(O,L-O),H===void 0)var H=O;else H+=String.fromCharCode(0),H+=O;O=L+1}}else{for(H=Array(_),B=0;B<_;++B)H[B]=String.fromCharCode(r()[I+B>>>0]);H=H.join("")}return tt(b),H},toWireType:function(b,_){_ instanceof ArrayBuffer&&(_=new Uint8Array(_));var I=typeof _=="string";if(!(I||_ instanceof Uint8Array||_ instanceof Uint8ClampedArray||_ instanceof Int8Array))throw new mt("Cannot pass non-string to std::string");var O=f&&I?$n(_):_.length,B=hr(4+O+1),L=B+4;if(a()[B>>>2>>>0]=O,f&&I)Dt(_,L,O+1);else if(I)for(I=0;I>>0]=H}else for(I=0;I>>0]=_[I];return b!==null&&b.push(tt,B),B},argPackAdvance:ft,readValueFromPointer:Cn,Db(b){tt(b)}})}var mi=typeof TextDecoder<"u"?new TextDecoder("utf-16le"):void 0,_c=(s,p)=>{for(var f=s>>1,b=f+p/2;!(f>=b)&&o()[f>>>0];)++f;if(32<(f<<=1)-s&&mi)return mi.decode(r().slice(s,f));for(f="",b=0;!(b>=p/2);++b){var _=n()[s+2*b>>>1>>>0];if(_==0)break;f+=String.fromCharCode(_)}return f},vc=(s,p,f)=>{if(f??=2147483647,2>f)return 0;var b=p;f=(f-=2)<2*s.length?f/2:s.length;for(var _=0;_>>1>>>0]=I,p+=2}return n()[p>>>1>>>0]=0,p-b},$c=s=>2*s.length,xc=(s,p)=>{for(var f=0,b="";!(f>=p/4);){var _=i()[s+4*f>>>2>>>0];if(_==0)break;++f,65536<=_?(_-=65536,b+=String.fromCharCode(55296|_>>10,56320|1023&_)):b+=String.fromCharCode(_)}return b},Sc=(s,p,f)=>{if(p>>>=0,f??=2147483647,4>f)return 0;var b=p;f=b+f-4;for(var _=0;_=I&&(I=65536+((1023&I)<<10)|1023&s.charCodeAt(++_)),i()[p>>>2>>>0]=I,(p+=4)+4>f)break}return i()[p>>>2>>>0]=0,p-b},Tc=s=>{for(var p=0,f=0;f=b&&++f,p+=4}return p};function Ic(s,p,f){if(s>>>=0,p>>>=0,f=Je(f>>>=0),p===2)var b=_c,_=vc,I=$c,O=B=>o()[B>>>1>>>0];else p===4&&(b=xc,_=Sc,I=Tc,O=B=>a()[B>>>2>>>0]);ut(s,{name:f,fromWireType:B=>{for(var L,H=a()[B>>>2>>>0],Q=B+4,fe=0;fe<=H;++fe){var be=B+4+fe*p;fe!=H&&O(be)!=0||(Q=b(Q,be-Q),L===void 0?L=Q:(L+=String.fromCharCode(0),L+=Q),Q=be+p)}return tt(B),L},toWireType:(B,L)=>{if(typeof L!="string")throw new mt(`Cannot pass non-string to C++ string type ${f}`);var H=I(L),Q=hr(4+H+p);return a()[Q>>>2>>>0]=H/p,_(L,Q+4,H+p),B!==null&&B.push(tt,Q),Q},argPackAdvance:ft,readValueFromPointer:Cn,Db(B){tt(B)}})}function Cc(s,p){ut(s>>>=0,{Tb:!0,name:p=Je(p>>>0),argPackAdvance:0,fromWireType:()=>{},toWireType:()=>{}})}var Ac=()=>1;function kc(s){Bn(s>>>0,!g,1,!w,131072,!1),Go()}var fi=s=>{if(!$e)try{if(s(),!(0<_t))try{y?gr(de):vn(de)}catch(p){p instanceof bn||p=="unwind"||A(1,p)}}catch(p){p instanceof bn||p=="unwind"||A(1,p)}};function An(s){s>>>=0,typeof Atomics.nc=="function"&&(Atomics.nc(i(),s>>>2,s).value.then(sr),s+=128,Atomics.store(i(),s>>>2,1))}var sr=()=>{var s=Rt();s&&(An(s),fi(Gi))};function Ec(s,p){(s>>>=0)==p>>>0?setTimeout(sr):y?postMessage({targetThread:s,cmd:"checkMailbox"}):(s=Ze[s])&&s.postMessage({cmd:"checkMailbox"})}var kn=[];function Pc(s,p,f,b,_){for(p>>>=0,b/=2,kn.length=b,f=_>>>0>>>3,_=0;_>>0];return(p?yn[p]:_p[s])(...kn)}function zc(s){s>>>=0,y?postMessage({cmd:"cleanupThread",thread:s}):Ho(Ze[s])}function Oc(s){}var En=(s,p)=>{var f=Sn[s];if(f===void 0)throw s=Wi(s),f=Je(s),tt(s),new mt(`${p} has unknown type ${f}`);return f},hi=(s,p,f)=>{var b=[];return s=s.toWireType(b,f),b.length&&(a()[p>>>2>>>0]=je(b)),s};function Dc(s,p,f){return p>>>=0,f>>>=0,s=qe(s>>>0),p=En(p,"emval::as"),hi(p,f,s)}var ur=s=>{try{s()}catch(p){Ot(p)}},ht=0,et=null,gi=0,dr=[],yi={},bi={},Bc=0,Pn=null,Mc=[];function wi(s){return function(p){if(!$e){if(ht===0){var f=!1,b=!1;p((_=0)=>{if(!$e&&(gi=_,f=!0,b)){ht=2,ur(()=>Yi(et)),typeof Browser<"u"&&Browser.Kb.Rb&&Browser.Kb.resume(),_=!1;try{var I=function(){var L=i()[et+8>>>2>>>0];return L=X[bi[L]],--_t,L()}()}catch(L){I=L,_=!0}var O=!1;if(!et){var B=Pn;B&&(Pn=null,(_?B.reject:B.resolve)(I),O=!0)}if(_&&!O)throw I}}),b=!0,f||(ht=1,et=function(){var _=hr(65548),I=_+12;a()[_>>>2>>>0]=I,a()[_+4>>>2>>>0]=I+65536,I=dr[0];var O=yi[I];return O===void 0&&(O=Bc++,yi[I]=O,bi[O]=I),I=O,i()[_+8>>>2>>>0]=I,_}(),typeof Browser<"u"&&Browser.Kb.Rb&&Browser.Kb.pause(),ur(()=>ji(et)))}else ht===2?(ht=0,ur(Xi),tt(et),et=null,Mc.forEach(fi)):Ot(`invalid state: ${ht}`);return gi}}(p=>{s().then(p)})}function Rc(s){return s>>>=0,wi(()=>(s=qe(s)).then(je))}var lr=[];function Uc(s,p,f,b){return f>>>=0,b>>>=0,(s=lr[s>>>0])(null,p=qe(p>>>0),f,b)}var Vc={},cr=s=>{var p=Vc[s];return p===void 0?Je(s):p};function Wc(s,p,f,b,_){return f>>>=0,b>>>=0,_>>>=0,(s=lr[s>>>0])(p=qe(p>>>0),p[f=cr(f)],b,_)}var _i=()=>typeof globalThis=="object"?globalThis:Function("return this")();function Nc(s){return(s>>>=0)==0?je(_i()):(s=cr(s),je(_i()[s]))}var Lc=s=>{var p=lr.length;return lr.push(s),p},Hc=(s,p)=>{for(var f=Array(s),b=0;b>>2>>>0],"parameter "+b);return f},vi=(s,p)=>Object.defineProperty(p,"name",{value:s});function Gc(s,p,f){var b=(p=Hc(s,p>>>0)).shift();s--;var _=`return function (obj, func, destructorsRef, args) { +`,I=0,O=[];f===0&&O.push("obj");for(var B=["retType"],L=[b],H=0;HQ.name).join(", ")}) => ${b.name}>`,Lc(vi(f,s))}function Fc(s){return s=cr(s>>>0),je(u[s])}function qc(s,p){return p>>>=0,s=qe(s>>>0),p=qe(p),je(s[p])}function jc(s){9<(s>>>=0)&&(dt[s+1]+=1)}function Kc(){return je([])}function Yc(s){s=qe(s>>>0);for(var p=Array(s.length),f=0;f>>0))}function Qc(){return je({})}function Zc(s){for(var p=qe(s>>>=0);p.length;){var f=p.pop();p.pop()(f)}In(s)}function Jc(s,p,f){p>>>=0,f>>>=0,s=qe(s>>>0),p=qe(p),f=qe(f),s[p]=f}function ep(s,p){return p>>>=0,s=(s=En(s>>>0,"_emval_take_value")).readValueFromPointer(p),je(s)}function tp(s,p){s=-9007199254740992>s||9007199254740992>>=0,s=new Date(1e3*s),i()[p>>>2>>>0]=s.getUTCSeconds(),i()[p+4>>>2>>>0]=s.getUTCMinutes(),i()[p+8>>>2>>>0]=s.getUTCHours(),i()[p+12>>>2>>>0]=s.getUTCDate(),i()[p+16>>>2>>>0]=s.getUTCMonth(),i()[p+20>>>2>>>0]=s.getUTCFullYear()-1900,i()[p+24>>>2>>>0]=s.getUTCDay(),s=(s.getTime()-Date.UTC(s.getUTCFullYear(),0,1,0,0,0,0))/864e5|0,i()[p+28>>>2>>>0]=s}var Bt=s=>s%4==0&&(s%100!=0||s%400==0),$i=[0,31,60,91,121,152,182,213,244,274,305,335],xi=[0,31,59,90,120,151,181,212,243,273,304,334];function rp(s,p){s=-9007199254740992>s||9007199254740992>>=0,s=new Date(1e3*s),i()[p>>>2>>>0]=s.getSeconds(),i()[p+4>>>2>>>0]=s.getMinutes(),i()[p+8>>>2>>>0]=s.getHours(),i()[p+12>>>2>>>0]=s.getDate(),i()[p+16>>>2>>>0]=s.getMonth(),i()[p+20>>>2>>>0]=s.getFullYear()-1900,i()[p+24>>>2>>>0]=s.getDay();var f=(Bt(s.getFullYear())?$i:xi)[s.getMonth()]+s.getDate()-1|0;i()[p+28>>>2>>>0]=f,i()[p+36>>>2>>>0]=-60*s.getTimezoneOffset(),f=new Date(s.getFullYear(),6,1).getTimezoneOffset();var b=new Date(s.getFullYear(),0,1).getTimezoneOffset();s=0|(f!=b&&s.getTimezoneOffset()==Math.min(b,f)),i()[p+32>>>2>>>0]=s}function np(s){s>>>=0;var p=new Date(i()[s+20>>>2>>>0]+1900,i()[s+16>>>2>>>0],i()[s+12>>>2>>>0],i()[s+8>>>2>>>0],i()[s+4>>>2>>>0],i()[s>>>2>>>0],0),f=i()[s+32>>>2>>>0],b=p.getTimezoneOffset(),_=new Date(p.getFullYear(),6,1).getTimezoneOffset(),I=new Date(p.getFullYear(),0,1).getTimezoneOffset(),O=Math.min(I,_);return 0>f?i()[s+32>>>2>>>0]=+(_!=I&&O==b):0>>2>>>0]=p.getDay(),f=(Bt(p.getFullYear())?$i:xi)[p.getMonth()]+p.getDate()-1|0,i()[s+28>>>2>>>0]=f,i()[s>>>2>>>0]=p.getSeconds(),i()[s+4>>>2>>>0]=p.getMinutes(),i()[s+8>>>2>>>0]=p.getHours(),i()[s+12>>>2>>>0]=p.getDate(),i()[s+16>>>2>>>0]=p.getMonth(),i()[s+20>>>2>>>0]=p.getYear(),s=p.getTime(),BigInt(isNaN(s)?-1:s/1e3)}function Si(s,p,f,b,_,I,O){return y?xe(16,1,s,p,f,b,_,I,O):-52}function Ti(s,p,f,b,_,I){if(y)return xe(17,1,s,p,f,b,_,I)}function op(s,p,f,b){s>>>=0,p>>>=0,f>>>=0,b>>>=0;var _=new Date().getFullYear(),I=new Date(_,0,1),O=new Date(_,6,1);_=I.getTimezoneOffset();var B=O.getTimezoneOffset(),L=Math.max(_,B);a()[s>>>2>>>0]=60*L,i()[p>>>2>>>0]=+(_!=B),I=(s=H=>H.toLocaleTimeString(void 0,{hour12:!1,timeZoneName:"short"}).split(" ")[1])(I),O=s(O),B<_?(Dt(I,f,17),Dt(O,b,17)):(Dt(I,b,17),Dt(O,f,17))}var zn=[],Ii=(s,p)=>{zn.length=0;for(var f;f=r()[s++>>>0];){var b=f!=105;p+=(b&=f!=112)&&p%8?4:0,zn.push(f==112?a()[p>>>2>>>0]:f==106?G[p>>>3]:f==105?i()[p>>>2>>>0]:l()[p>>>3>>>0]),p+=b?8:4}return zn};function ip(s,p,f){return s>>>=0,p=Ii(p>>>0,f>>>0),yn[s](...p)}function ap(s,p,f){return s>>>=0,p=Ii(p>>>0,f>>>0),yn[s](...p)}var sp=()=>{},up=()=>Date.now();function dp(s,p){return Y(ze(s>>>0,p>>>0))}var Ci,lp=()=>{throw _t+=1,"unwind"};function cp(){return 4294901760}Ci=()=>performance.timeOrigin+performance.now();var pp=()=>navigator.hardwareConcurrency;function mp(){return Ot("Cannot use emscripten_pc_get_function without -sUSE_OFFSET_CONVERTER"),0}function fp(s){s>>>=0;var p=r().length;if(s<=p||4294901760=f;f*=2){var b=p*(1+.2/f);b=Math.min(b,s+100663296);var _=Math;b=Math.max(s,b);e:{_=(_.min.call(_,4294901760,b+(65536-b%65536)%65536)-ue.buffer.byteLength+65535)/65536;try{ue.grow(_),Ce();var I=1;break e}catch{}I=void 0}if(I)return!0}return!1}var pr=()=>(Ot("Cannot use convertFrameToPC (needed by __builtin_return_address) without -sUSE_OFFSET_CONVERTER"),0),Mt={},Ai=s=>{s.forEach(p=>{var f=pr();f&&(Mt[f]=p)})};function hp(){var s=Error().stack.toString().split(` +`);return s[0]=="Error"&&s.shift(),Ai(s),Mt.Pb=pr(),Mt.ec=s,Mt.Pb}function gp(s,p,f){if(s>>>=0,p>>>=0,Mt.Pb==s)var b=Mt.ec;else(b=Error().stack.toString().split(` +`))[0]=="Error"&&b.shift(),Ai(b);for(var _=3;b[_]&&pr()!=s;)++_;for(s=0;s>>2>>>0]=pr();return s}var On,Dn={},ki=()=>{if(!On){var s,p={USER:"web_user",LOGNAME:"web_user",PATH:"/",PWD:"/",HOME:"/home/web_user",LANG:(typeof navigator=="object"&&navigator.languages&&navigator.languages[0]||"C").replace("-","_")+".UTF-8",_:C||"./this.program"};for(s in Dn)Dn[s]===void 0?delete p[s]:p[s]=Dn[s];var f=[];for(s in p)f.push(`${s}=${p[s]}`);On=f}return On};function Ei(s,p){if(y)return xe(18,1,s,p);s>>>=0,p>>>=0;var f=0;return ki().forEach((b,_)=>{var I=p+f;for(_=a()[s+4*_>>>2>>>0]=I,I=0;I>>0]=b.charCodeAt(I);t()[_>>>0]=0,f+=b.length+1}),0}function Pi(s,p){if(y)return xe(19,1,s,p);s>>>=0,p>>>=0;var f=ki();a()[s>>>2>>>0]=f.length;var b=0;return f.forEach(_=>b+=_.length+1),a()[p>>>2>>>0]=b,0}function zi(s){return y?xe(20,1,s):52}function Oi(s,p,f,b){return y?xe(21,1,s,p,f,b):52}function Di(s,p,f,b){return y?xe(22,1,s,p,f,b):70}var yp=[null,[],[]];function Bi(s,p,f,b){if(y)return xe(23,1,s,p,f,b);p>>>=0,f>>>=0,b>>>=0;for(var _=0,I=0;I>>2>>>0],B=a()[p+4>>>2>>>0];p+=8;for(var L=0;L>>0],Q=yp[s];H===0||H===10?((s===1?j:Y)(Xo(Q,0)),Q.length=0):Q.push(H)}_+=B}return a()[b>>>2>>>0]=_,0}var Mi=[31,29,31,30,31,30,31,31,30,31,30,31],Ri=[31,28,31,30,31,30,31,31,30,31,30,31],bp=(s,p)=>{t().set(s,p>>>0)};function Ui(s,p,f,b){function _(z,me,Se){for(z=typeof z=="number"?z.toString():z||"";z.lengthZi?-1:0$t-z.getDate())){z.setDate(z.getDate()+me);break}me-=$t-z.getDate()+1,z.setDate(1),11>Se?z.setMonth(Se+1):(z.setMonth(0),z.setFullYear(z.getFullYear()+1))}return Se=new Date(z.getFullYear()+1,0,4),me=B(new Date(z.getFullYear(),0,4)),Se=B(Se),0>=O(me,z)?0>=O(Se,z)?z.getFullYear()+1:z.getFullYear():z.getFullYear()-1}s>>>=0,p>>>=0,f>>>=0,b>>>=0;var H=a()[b+40>>>2>>>0];for(var Q in b={kc:i()[b>>>2>>>0],jc:i()[b+4>>>2>>>0],Hb:i()[b+8>>>2>>>0],Lb:i()[b+12>>>2>>>0],Ib:i()[b+16>>>2>>>0],Cb:i()[b+20>>>2>>>0],ub:i()[b+24>>>2>>>0],Bb:i()[b+28>>>2>>>0],sc:i()[b+32>>>2>>>0],ic:i()[b+36>>>2>>>0],lc:H?ze(H):""},f=ze(f),H={"%c":"%a %b %d %H:%M:%S %Y","%D":"%m/%d/%y","%F":"%Y-%m-%d","%h":"%b","%r":"%I:%M:%S %p","%R":"%H:%M","%T":"%H:%M:%S","%x":"%m/%d/%y","%X":"%H:%M:%S","%Ec":"%c","%EC":"%C","%Ex":"%m/%d/%y","%EX":"%H:%M:%S","%Ey":"%y","%EY":"%Y","%Od":"%d","%Oe":"%e","%OH":"%H","%OI":"%I","%Om":"%m","%OM":"%M","%OS":"%S","%Ou":"%u","%OU":"%U","%OV":"%V","%Ow":"%w","%OW":"%W","%Oy":"%y"})f=f.replace(new RegExp(Q,"g"),H[Q]);var fe="Sunday Monday Tuesday Wednesday Thursday Friday Saturday".split(" "),be="January February March April May June July August September October November December".split(" ");for(Q in H={"%a":z=>fe[z.ub].substring(0,3),"%A":z=>fe[z.ub],"%b":z=>be[z.Ib].substring(0,3),"%B":z=>be[z.Ib],"%C":z=>I((z.Cb+1900)/100|0,2),"%d":z=>I(z.Lb,2),"%e":z=>_(z.Lb,2," "),"%g":z=>L(z).toString().substring(2),"%G":L,"%H":z=>I(z.Hb,2),"%I":z=>((z=z.Hb)==0?z=12:12{for(var me=0,Se=0;Se<=z.Ib-1;me+=(Bt(z.Cb+1900)?Mi:Ri)[Se++]);return I(z.Lb+me,3)},"%m":z=>I(z.Ib+1,2),"%M":z=>I(z.jc,2),"%n":()=>` +`,"%p":z=>0<=z.Hb&&12>z.Hb?"AM":"PM","%S":z=>I(z.kc,2),"%t":()=>" ","%u":z=>z.ub||7,"%U":z=>I(Math.floor((z.Bb+7-z.ub)/7),2),"%V":z=>{var me=Math.floor((z.Bb+7-(z.ub+6)%7)/7);if(2>=(z.ub+371-z.Bb-2)%7&&me++,me)me==53&&((Se=(z.ub+371-z.Bb)%7)==4||Se==3&&Bt(z.Cb)||(me=1));else{me=52;var Se=(z.ub+7-z.Bb-1)%7;(Se==4||Se==5&&Bt(z.Cb%400-1))&&me++}return I(me,2)},"%w":z=>z.ub,"%W":z=>I(Math.floor((z.Bb+7-(z.ub+6)%7)/7),2),"%y":z=>(z.Cb+1900).toString().substring(2),"%Y":z=>z.Cb+1900,"%z":z=>{var me=0<=(z=z.ic);return z=Math.abs(z)/60,(me?"+":"-")+("0000"+(z/60*100+z%60)).slice(-4)},"%Z":z=>z.lc,"%%":()=>"%"},f=f.replace(/%%/g,"\0\0"),H)f.includes(Q)&&(f=f.replace(new RegExp(Q,"g"),H[Q](b)));return Q=function(z){var me=Array($n(z)+1);return Jo(z,me,0,me.length),me}(f=f.replace(/\0\0/g,"%")),Q.length>p?0:(bp(Q,s),Q.length-1)}function wp(s,p,f,b){return Ui(s>>>0,p>>>0,f>>>0,b>>>0)}y||function(){for(var s=u.numThreads-1;s--;)qo();bt.unshift(()=>{Ue++,function(p){y?p():Promise.all(pt.map(Fo)).then(p)}(()=>Do())})}();for(var Vi=Array(256),mr=0;256>mr;++mr)Vi[mr]=String.fromCharCode(mr);ci=Vi,mt=u.BindingError=class extends Error{constructor(s){super(s),this.name="BindingError"}},u.InternalError=class extends Error{constructor(s){super(s),this.name="InternalError"}},dt.push(0,1,void 0,1,null,1,!0,1,!1,1),u.count_emval_handles=()=>dt.length/2-5-Tn.length;var _p=[_n,No,jo,Qo,Zo,ei,ti,ri,ni,oi,ii,ai,si,ui,di,li,Si,Ti,Ei,Pi,zi,Oi,Di,Bi],X=function(){function s(f,b){return X=f.exports,X=function(){var _=X,I={};for(let[O,B]of Object.entries(_))I[O]=typeof B=="function"?(...L)=>{dr.push(O);try{return B(...L)}finally{$e||(dr.pop(),et&&ht===1&&dr.length===0&&(ht=0,_t+=1,ur(Ki),typeof Fibers<"u"&&Fibers.tc()))}}:B;return I}(),X=function(){var _=X,I=B=>L=>B(L)>>>0,O=B=>()=>B()>>>0;return(_=Object.assign({},_)).Ca=I(_.Ca),_.fb=O(_.fb),_.hb=I(_.hb),_.emscripten_main_runtime_thread_id=O(_.emscripten_main_runtime_thread_id),_.sb=I(_.sb),_.tb=O(_.tb),_}(),Lo.push(X.ib),Ae.unshift(X.Ba),K=b,Do(),X}var p=Vo();if(Ue++,u.instantiateWasm)try{return u.instantiateWasm(p,s)}catch(f){Y(`Module.instantiateWasm callback failed with error: ${f}`),m(f)}return gn||=u.locateFile?Bo("ort-wasm-simd-threaded.jsep.wasm")?"ort-wasm-simd-threaded.jsep.wasm":u.locateFile?u.locateFile("ort-wasm-simd-threaded.jsep.wasm",P):P+"ort-wasm-simd-threaded.jsep.wasm":new URL(/* asset import */ __webpack_require__(/*! ort-wasm-simd-threaded.jsep.wasm */ "./node_modules/onnxruntime-web/dist/ort-wasm-simd-threaded.jsep.wasm"), __webpack_require__.b).href,function(f,b){var _=gn;return D||typeof WebAssembly.instantiateStreaming!="function"||Bo(_)||Mo(_)||typeof fetch!="function"?Uo(_,f,b):fetch(_,{credentials:"same-origin"}).then(I=>WebAssembly.instantiateStreaming(I,f).then(b,function(O){return Y(`wasm streaming compile failed: ${O}`),Y("falling back to ArrayBuffer instantiation"),Uo(_,f,b)}))}(p,function(f){s(f.instance,f.module)}).catch(m),{}}(),Wi=s=>(Wi=X.Ca)(s),Ni=()=>(Ni=X.Da)();u._OrtInit=(s,p)=>(u._OrtInit=X.Ea)(s,p),u._OrtGetLastError=(s,p)=>(u._OrtGetLastError=X.Fa)(s,p),u._OrtCreateSessionOptions=(s,p,f,b,_,I,O,B,L,H)=>(u._OrtCreateSessionOptions=X.Ga)(s,p,f,b,_,I,O,B,L,H),u._OrtAppendExecutionProvider=(s,p)=>(u._OrtAppendExecutionProvider=X.Ha)(s,p),u._OrtAddFreeDimensionOverride=(s,p,f)=>(u._OrtAddFreeDimensionOverride=X.Ia)(s,p,f),u._OrtAddSessionConfigEntry=(s,p,f)=>(u._OrtAddSessionConfigEntry=X.Ja)(s,p,f),u._OrtReleaseSessionOptions=s=>(u._OrtReleaseSessionOptions=X.Ka)(s),u._OrtCreateSession=(s,p,f)=>(u._OrtCreateSession=X.La)(s,p,f),u._OrtReleaseSession=s=>(u._OrtReleaseSession=X.Ma)(s),u._OrtGetInputOutputCount=(s,p,f)=>(u._OrtGetInputOutputCount=X.Na)(s,p,f),u._OrtGetInputName=(s,p)=>(u._OrtGetInputName=X.Oa)(s,p),u._OrtGetOutputName=(s,p)=>(u._OrtGetOutputName=X.Pa)(s,p),u._OrtFree=s=>(u._OrtFree=X.Qa)(s),u._OrtCreateTensor=(s,p,f,b,_,I)=>(u._OrtCreateTensor=X.Ra)(s,p,f,b,_,I),u._OrtGetTensorData=(s,p,f,b,_)=>(u._OrtGetTensorData=X.Sa)(s,p,f,b,_),u._OrtReleaseTensor=s=>(u._OrtReleaseTensor=X.Ta)(s),u._OrtCreateRunOptions=(s,p,f,b)=>(u._OrtCreateRunOptions=X.Ua)(s,p,f,b),u._OrtAddRunConfigEntry=(s,p,f)=>(u._OrtAddRunConfigEntry=X.Va)(s,p,f),u._OrtReleaseRunOptions=s=>(u._OrtReleaseRunOptions=X.Wa)(s),u._OrtCreateBinding=s=>(u._OrtCreateBinding=X.Xa)(s),u._OrtBindInput=(s,p,f)=>(u._OrtBindInput=X.Ya)(s,p,f),u._OrtBindOutput=(s,p,f,b)=>(u._OrtBindOutput=X.Za)(s,p,f,b),u._OrtClearBoundOutputs=s=>(u._OrtClearBoundOutputs=X._a)(s),u._OrtReleaseBinding=s=>(u._OrtReleaseBinding=X.$a)(s),u._OrtRunWithBinding=(s,p,f,b,_)=>(u._OrtRunWithBinding=X.ab)(s,p,f,b,_),u._OrtRun=(s,p,f,b,_,I,O,B)=>(u._OrtRun=X.bb)(s,p,f,b,_,I,O,B),u._OrtEndProfiling=s=>(u._OrtEndProfiling=X.cb)(s),u._JsepOutput=(s,p,f)=>(u._JsepOutput=X.db)(s,p,f),u._JsepGetNodeName=s=>(u._JsepGetNodeName=X.eb)(s);var fr,Rt=()=>(Rt=X.fb)(),tt=u._free=s=>(tt=u._free=X.gb)(s),hr=u._malloc=s=>(hr=u._malloc=X.hb)(s),Bn=(s,p,f,b,_,I)=>(Bn=X.kb)(s,p,f,b,_,I),Li=()=>(Li=X.lb)(),Hi=(s,p,f,b,_)=>(Hi=X.mb)(s,p,f,b,_),Mn=s=>(Mn=X.nb)(s),gr=s=>(gr=X.ob)(s),Gi=()=>(Gi=X.pb)(),Fi=(s,p)=>(Fi=X.qb)(s,p),yr=s=>(yr=X.rb)(s),Rn=s=>(Rn=X.sb)(s),Un=()=>(Un=X.tb)(),qi=u.dynCall_ii=(s,p)=>(qi=u.dynCall_ii=X.vb)(s,p),ji=s=>(ji=X.wb)(s),Ki=()=>(Ki=X.xb)(),Yi=s=>(Yi=X.yb)(s),Xi=()=>(Xi=X.zb)();function Qi(){0Un(),u.stackRestore=s=>yr(s),u.stackAlloc=s=>Rn(s),u.UTF8ToString=ze,u.stringToUTF8=Dt,u.lengthBytesUTF8=$n,wt=function s(){fr||Qi(),fr||(wt=s)},Qi(),h}),Ep=Oa;globalThis.self?.name==="em-pthread"&&Oa()});var Ut,Pp,zp,Op,Ma,Ra,Dp,Ua,qt=U(()=>{"use strict";Cr();Ut= false?0:import.meta.url??(typeof document<"u"?document.currentScript?.src:typeof self<"u"?self.location?.href:void 0),Pp= false||typeof location>"u"?void 0:location.origin,zp=(e,t)=>{try{let r=t??Ut;return(r?new URL(e,r):new URL(e)).origin===Pp}catch{return!1}},Op=async e=>{let r=await(await fetch(e,{credentials:"same-origin"})).blob();return URL.createObjectURL(r)},Ma=(za(),br(Pa)).default,Ra=async()=>{if(!Ut)throw new Error("Failed to load proxy worker: cannot determine the script source URL.");if(zp(Ut))return[void 0,Ma()];let e=await Op(Ut);return[e,Ma(e)]},Dp=(Ba(),br(Da)).default,Ua=async(e,t,r)=>[void 0,Dp]});var jn,Kn,Mr,Va,Bp,Mp,Ar,Te,gt=U(()=>{"use strict";qt();Kn=!1,Mr=!1,Va=!1,Bp=()=>{if(typeof SharedArrayBuffer>"u")return!1;try{return typeof MessageChannel<"u"&&new MessageChannel().port1.postMessage(new SharedArrayBuffer(1)),WebAssembly.validate(new Uint8Array([0,97,115,109,1,0,0,0,1,4,1,96,0,0,3,2,1,0,5,4,1,3,1,1,10,11,1,9,0,65,0,254,16,2,0,26,11]))}catch{return!1}},Mp=()=>{try{return WebAssembly.validate(new Uint8Array([0,97,115,109,1,0,0,0,1,4,1,96,0,0,3,2,1,0,10,30,1,28,0,65,0,253,15,253,12,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,253,186,1,26,11]))}catch{return!1}},Ar=async e=>{if(Kn)return Promise.resolve();if(Mr)throw new Error("multiple calls to 'initializeWebAssembly()' detected.");if(Va)throw new Error("previous call to 'initializeWebAssembly()' failed.");Mr=!0;let t=e.initTimeout,r=e.numThreads;if(!Mp())throw new Error("WebAssembly SIMD is not supported in the current environment.");let n=Bp();r>1&&!n&&(typeof self<"u"&&!self.crossOriginIsolated&&console.warn("env.wasm.numThreads is set to "+r+", but this will not work unless you enable crossOriginIsolated mode. See https://web.dev/cross-origin-isolation-guide/ for more info."),console.warn("WebAssembly multi-threading is not supported in the current environment. Falling back to single-threading."),e.numThreads=r=1);let o=e.wasmPaths,i=typeof o=="string"?o:void 0,a=o?.mjs,d=a?.href??a,l=o?.wasm,c=l?.href??l,m=e.wasmBinary,[u,h]=await Ua(d,i,r>1),w=!1,g=[];if(t>0&&g.push(new Promise(y=>{setTimeout(()=>{w=!0,y()},t)})),g.push(new Promise((y,S)=>{let $={numThreads:r};m?$.wasmBinary=m:(c||i)&&($.locateFile=(v,x)=>c??(i??x)+v),h($).then(v=>{Mr=!1,Kn=!0,jn=v,y(),u&&URL.revokeObjectURL(u)},v=>{Mr=!1,Va=!0,S(v)})})),await Promise.race(g),w)throw new Error(`WebAssembly backend initializing failed due to timeout: ${t}ms`)},Te=()=>{if(Kn&&jn)return jn;throw new Error("WebAssembly is not initialized yet.")}});var ke,Kt,ve,Rr=U(()=>{"use strict";gt();ke=(e,t)=>{let r=Te(),n=r.lengthBytesUTF8(e)+1,o=r._malloc(n);return r.stringToUTF8(e,o,n),t.push(o),o},Kt=(e,t,r,n)=>{if(typeof e=="object"&&e!==null){if(r.has(e))throw new Error("Circular reference in options");r.add(e)}Object.entries(e).forEach(([o,i])=>{let a=t?t+o:o;if(typeof i=="object")Kt(i,a+".",r,n);else if(typeof i=="string"||typeof i=="number")n(a,i.toString());else if(typeof i=="boolean")n(a,i?"1":"0");else throw new Error(`Can't handle extra config type: ${typeof i}`)})},ve=e=>{let t=Te(),r=t.stackSave();try{let n=t.stackAlloc(8);t._OrtGetLastError(n,n+4);let o=t.HEAP32[n/4],i=t.HEAPU32[n/4+1],a=i?t.UTF8ToString(i):"";throw new Error(`${e} ERROR_CODE: ${o}, ERROR_MESSAGE: ${a}`)}finally{t.stackRestore(r)}}});var Wa,Na=U(()=>{"use strict";gt();Rr();Wa=e=>{let t=Te(),r=0,n=[],o=e||{};try{if(e?.logSeverityLevel===void 0)o.logSeverityLevel=2;else if(typeof e.logSeverityLevel!="number"||!Number.isInteger(e.logSeverityLevel)||e.logSeverityLevel<0||e.logSeverityLevel>4)throw new Error(`log serverity level is not valid: ${e.logSeverityLevel}`);if(e?.logVerbosityLevel===void 0)o.logVerbosityLevel=0;else if(typeof e.logVerbosityLevel!="number"||!Number.isInteger(e.logVerbosityLevel))throw new Error(`log verbosity level is not valid: ${e.logVerbosityLevel}`);e?.terminate===void 0&&(o.terminate=!1);let i=0;return e?.tag!==void 0&&(i=ke(e.tag,n)),r=t._OrtCreateRunOptions(o.logSeverityLevel,o.logVerbosityLevel,!!o.terminate,i),r===0&&ve("Can't create run options."),e?.extra!==void 0&&Kt(e.extra,"",new WeakSet,(a,d)=>{let l=ke(a,n),c=ke(d,n);t._OrtAddRunConfigEntry(r,l,c)!==0&&ve(`Can't set a run config entry: ${a} - ${d}.`)}),[r,n]}catch(i){throw r!==0&&t._OrtReleaseRunOptions(r),n.forEach(a=>t._free(a)),i}}});var Rp,Up,Vp,Wp,La,Ha=U(()=>{"use strict";gt();Rr();Rp=e=>{switch(e){case"disabled":return 0;case"basic":return 1;case"extended":return 2;case"all":return 99;default:throw new Error(`unsupported graph optimization level: ${e}`)}},Up=e=>{switch(e){case"sequential":return 0;case"parallel":return 1;default:throw new Error(`unsupported execution mode: ${e}`)}},Vp=e=>{e.extra||(e.extra={}),e.extra.session||(e.extra.session={});let t=e.extra.session;t.use_ort_model_bytes_directly||(t.use_ort_model_bytes_directly="1"),e.executionProviders&&e.executionProviders.some(r=>(typeof r=="string"?r:r.name)==="webgpu")&&(e.enableMemPattern=!1)},Wp=(e,t,r)=>{for(let n of t){let o=typeof n=="string"?n:n.name;switch(o){case"webnn":if(o="WEBNN",typeof n!="string"){let d=n?.deviceType;if(d){let l=ke("deviceType",r),c=ke(d,r);Te()._OrtAddSessionConfigEntry(e,l,c)!==0&&ve(`Can't set a session config entry: 'deviceType' - ${d}.`)}}break;case"webgpu":if(o="JS",typeof n!="string"){let a=n;if(a?.preferredLayout){if(a.preferredLayout!=="NCHW"&&a.preferredLayout!=="NHWC")throw new Error(`preferredLayout must be either 'NCHW' or 'NHWC': ${a.preferredLayout}`);let d=ke("preferredLayout",r),l=ke(a.preferredLayout,r);Te()._OrtAddSessionConfigEntry(e,d,l)!==0&&ve(`Can't set a session config entry: 'preferredLayout' - ${a.preferredLayout}.`)}}break;case"wasm":case"cpu":continue;default:throw new Error(`not supported execution provider: ${o}`)}let i=ke(o,r);Te()._OrtAppendExecutionProvider(e,i)!==0&&ve(`Can't append execution provider: ${o}.`)}},La=e=>{let t=Te(),r=0,n=[],o=e||{};Vp(o);try{let i=Rp(o.graphOptimizationLevel??"all"),a=Up(o.executionMode??"sequential"),d=typeof o.logId=="string"?ke(o.logId,n):0,l=o.logSeverityLevel??2;if(!Number.isInteger(l)||l<0||l>4)throw new Error(`log serverity level is not valid: ${l}`);let c=o.logVerbosityLevel??0;if(!Number.isInteger(c)||c<0||c>4)throw new Error(`log verbosity level is not valid: ${c}`);let m=typeof o.optimizedModelFilePath=="string"?ke(o.optimizedModelFilePath,n):0;if(r=t._OrtCreateSessionOptions(i,!!o.enableCpuMemArena,!!o.enableMemPattern,a,!!o.enableProfiling,0,d,l,c,m),r===0&&ve("Can't create session options."),o.executionProviders&&Wp(r,o.executionProviders,n),o.enableGraphCapture!==void 0){if(typeof o.enableGraphCapture!="boolean")throw new Error(`enableGraphCapture must be a boolean value: ${o.enableGraphCapture}`);let u=ke("enableGraphCapture",n),h=ke(o.enableGraphCapture.toString(),n);t._OrtAddSessionConfigEntry(r,u,h)!==0&&ve(`Can't set a session config entry: 'enableGraphCapture' - ${o.enableGraphCapture}.`)}if(o.freeDimensionOverrides)for(let[u,h]of Object.entries(o.freeDimensionOverrides)){if(typeof u!="string")throw new Error(`free dimension override name must be a string: ${u}`);if(typeof h!="number"||!Number.isInteger(h)||h<0)throw new Error(`free dimension override value must be a non-negative integer: ${h}`);let w=ke(u,n);t._OrtAddFreeDimensionOverride(r,w,h)!==0&&ve(`Can't set a free dimension override: ${u} - ${h}.`)}return o.extra!==void 0&&Kt(o.extra,"",new WeakSet,(u,h)=>{let w=ke(u,n),g=ke(h,n);t._OrtAddSessionConfigEntry(r,w,g)!==0&&ve(`Can't set a session config entry: ${u} - ${h}.`)}),[r,n]}catch(i){throw r!==0&&t._OrtReleaseSessionOptions(r),n.forEach(a=>t._free(a)),i}}});var Yt,yt,It,Ur,Xt,Vr,Wr,Yn,J=U(()=>{"use strict";Yt=e=>{switch(e){case"int8":return 3;case"uint8":return 2;case"bool":return 9;case"int16":return 5;case"uint16":return 4;case"int32":return 6;case"uint32":return 12;case"float16":return 10;case"float32":return 1;case"float64":return 11;case"string":return 8;case"int64":return 7;case"uint64":return 13;case"int4":return 22;case"uint4":return 21;default:throw new Error(`unsupported data type: ${e}`)}},yt=e=>{switch(e){case 3:return"int8";case 2:return"uint8";case 9:return"bool";case 5:return"int16";case 4:return"uint16";case 6:return"int32";case 12:return"uint32";case 10:return"float16";case 1:return"float32";case 11:return"float64";case 8:return"string";case 7:return"int64";case 13:return"uint64";case 22:return"int4";case 21:return"uint4";default:throw new Error(`unsupported data type: ${e}`)}},It=(e,t)=>{let r=[-1,4,1,1,2,2,4,8,-1,1,2,8,4,8,-1,-1,-1,-1,-1,-1,-1,.5,.5][e],n=typeof t=="number"?t:t.reduce((o,i)=>o*i,1);return r>0?Math.ceil(n*r):void 0},Ur=e=>{switch(e){case"float16":return typeof Float16Array<"u"&&Float16Array.from?Float16Array:Uint16Array;case"float32":return Float32Array;case"uint8":return Uint8Array;case"int8":return Int8Array;case"uint16":return Uint16Array;case"int16":return Int16Array;case"int32":return Int32Array;case"bool":return Uint8Array;case"float64":return Float64Array;case"uint32":return Uint32Array;case"int64":return BigInt64Array;case"uint64":return BigUint64Array;default:throw new Error(`unsupported type: ${e}`)}},Xt=e=>{switch(e){case"verbose":return 0;case"info":return 1;case"warning":return 2;case"error":return 3;case"fatal":return 4;default:throw new Error(`unsupported logging level: ${e}`)}},Vr=e=>e==="float32"||e==="float16"||e==="int32"||e==="int64"||e==="uint32"||e==="uint8"||e==="bool"||e==="uint4"||e==="int4",Wr=e=>e==="float32"||e==="float16"||e==="int32"||e==="int64"||e==="uint32"||e==="uint64"||e==="int8"||e==="uint8"||e==="bool",Yn=e=>{switch(e){case"none":return 0;case"cpu":return 1;case"cpu-pinned":return 2;case"texture":return 3;case"gpu-buffer":return 4;case"ml-tensor":return 5;default:throw new Error(`unsupported data location: ${e}`)}}});var Qt,Xn=U(()=>{"use strict";Cr();Qt=async e=>{if(typeof e=="string")if(false){}else{let t=await fetch(e);if(!t.ok)throw new Error(`failed to load external data file: ${e}`);let r=t.headers.get("Content-Length"),n=r?parseInt(r,10):0;if(n<1073741824)return new Uint8Array(await t.arrayBuffer());{if(!t.body)throw new Error(`failed to load external data file: ${e}, no response body.`);let o=t.body.getReader(),i;try{i=new ArrayBuffer(n)}catch(d){if(d instanceof RangeError){let l=Math.ceil(n/65536);i=new WebAssembly.Memory({initial:l,maximum:l}).buffer}else throw d}let a=0;for(;;){let{done:d,value:l}=await o.read();if(d)break;let c=l.byteLength;new Uint8Array(i,a,c).set(l),a+=c}return new Uint8Array(i,0,n)}}else return e instanceof Blob?new Uint8Array(await e.arrayBuffer()):e instanceof Uint8Array?e:new Uint8Array(e)}});var Np,Lp,Ga,Fa,Nr,Hp,pe,Xe=U(()=>{"use strict";J();Np=["V","I","W","E","F"],Lp=(e,t)=>{console.log(`[${Np[e]},${new Date().toISOString()}]${t}`)},Nr=(e,t)=>{Ga=e,Fa=t},Hp=(e,t)=>{let r=Xt(e),n=Xt(Ga);r>=n&&Lp(r,typeof t=="function"?t():t)},pe=(...e)=>{Fa&&Hp(...e)}});var Lr,Qn=U(()=>{"use strict";J();Lr=(e,t)=>new(Ur(t))(e)});var Hr=U(()=>{"use strict"});var qa,Zn,Jn,Gp,Fp,ja,to,eo,Ya,Xa=U(()=>{"use strict";Xe();Hr();qa=new Map([[64,250],[128,200],[256,200],[512,200],[2048,230],[4096,200],[8192,50],[16384,50],[32768,50],[65536,50],[131072,50],[262144,50],[524288,50],[1048576,50],[2097152,30],[4194304,20],[8388608,10],[12582912,10],[16777216,10],[26214400,15],[33554432,22],[44236800,2],[58982400,6],[67108864,6],[134217728,6],[167772160,6]]),Zn=[],Jn=e=>Math.ceil(e/16)*16,Gp=e=>{for(let t=0;tFp++,to=async(e,t,r,n)=>{let o=Jn(r),i=e.device.createBuffer({size:o,usage:GPUBufferUsage.COPY_DST|GPUBufferUsage.MAP_READ});try{let a=e.getCommandEncoder();e.endComputePass(),a.copyBufferToBuffer(t,0,i,0,o),e.flush(),await i.mapAsync(GPUMapMode.READ);let d=i.getMappedRange();if(n){let l=n();return l.set(new Uint8Array(d,0,r)),l}else return new Uint8Array(d.slice(0,r))}finally{i.destroy()}},eo=class{constructor(t){this.backend=t;this.storageCache=new Map,this.freeBuffers=new Map,this.freeUniformBuffers=new Map,this.buffersForUploadingPending=[],this.buffersPending=[],this.capturedPendingBuffers=new Map;for(let[r]of qa)Zn.push(r),this.freeBuffers.set(r,[]),this.freeUniformBuffers.set(r,[]);this.sessionCount=0}upload(t,r){let n=r.buffer,o=r.byteOffset,i=r.byteLength,a=Jn(i),d=this.storageCache.get(t);if(!d)throw new Error("gpu data for uploading does not exist");if(d.originalSize!==i)throw new Error(`inconsistent data size. gpu data size=${d.originalSize}, data size=${i}`);let l=this.backend.device.createBuffer({mappedAtCreation:!0,size:a,usage:GPUBufferUsage.MAP_WRITE|GPUBufferUsage.COPY_SRC}),c=l.getMappedRange();new Uint8Array(c).set(new Uint8Array(n,o,i)),l.unmap();let m=this.backend.getCommandEncoder();this.backend.endComputePass(),m.copyBufferToBuffer(l,0,d.gpuData.buffer,0,a),pe("verbose",()=>`[WebGPU] GpuDataManager.upload(id=${t})`),this.buffersForUploadingPending.push(l)}memcpy(t,r){let n=this.storageCache.get(t);if(!n)throw new Error("source gpu data for memcpy does not exist");let o=this.storageCache.get(r);if(!o)throw new Error("destination gpu data for memcpy does not exist");if(n.originalSize!==o.originalSize)throw new Error("inconsistent source and destination gpu data size");let i=Jn(n.originalSize),a=this.backend.getCommandEncoder();this.backend.endComputePass(),a.copyBufferToBuffer(n.gpuData.buffer,0,o.gpuData.buffer,0,i)}registerExternalBuffer(t,r,n){let o;if(n){if(o=n[0],t===n[1])return pe("verbose",()=>`[WebGPU] GpuDataManager.registerExternalBuffer(size=${r}) => id=${o}, buffer is the same, skip.`),o;if(this.backend.capturedCommandList.has(this.backend.currentSessionId))throw new Error(`Registering a different external buffer under graph capture mode is not supported yet. + Please use the previous external buffer!`)}else o=ja();return this.storageCache.set(o,{gpuData:{id:o,type:0,buffer:t},originalSize:r}),pe("verbose",()=>`[WebGPU] GpuDataManager.registerExternalBuffer(size=${r}) => id=${o}, registered.`),o}unregisterExternalBuffer(t){t!==void 0&&(this.storageCache.delete(t),pe("verbose",()=>`[WebGPU] GpuDataManager.unregisterExternalBuffer() => id=${t}`))}create(t,r=GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST){let n=Gp(t),o,i=(r&GPUBufferUsage.STORAGE)===GPUBufferUsage.STORAGE,a=(r&GPUBufferUsage.UNIFORM)===GPUBufferUsage.UNIFORM;if(i||a){let c=(i?this.freeBuffers:this.freeUniformBuffers).get(n);c?c.length>0?o=c.pop():o=this.backend.device.createBuffer({size:n,usage:r}):o=this.backend.device.createBuffer({size:n,usage:r})}else o=this.backend.device.createBuffer({size:n,usage:r});let d={id:ja(),type:0,buffer:o};return this.storageCache.set(d.id,{gpuData:d,originalSize:t}),pe("verbose",()=>`[WebGPU] GpuDataManager.create(size=${t}) => id=${d.id}`),d}get(t){return this.storageCache.get(t)?.gpuData}release(t){let r=this.storageCache.get(t);if(!r){if(this.storageCache.size===0)return 0;throw new Error("releasing data does not exist")}return pe("verbose",()=>`[WebGPU] GpuDataManager.release(id=${t}), gpuDataId=${r.gpuData.id}`),this.storageCache.delete(t),this.buffersPending.push(r.gpuData.buffer),r.originalSize}async download(t,r){let n=this.storageCache.get(t);if(!n)throw new Error("data does not exist");await to(this.backend,n.gpuData.buffer,n.originalSize,r)}refreshPendingBuffers(){for(let t of this.buffersForUploadingPending)t.destroy();if(this.buffersForUploadingPending=[],this.buffersPending.length!==0)if(this.backend.sessionStatus==="default"){for(let t of this.buffersPending){let r=qa.get(t.size);if((t.usage&GPUBufferUsage.STORAGE)===GPUBufferUsage.STORAGE){let n=this.freeBuffers.get(t.size)||[];r===void 0||n.length>=r?t.destroy():n.push(t)}else if((t.usage&GPUBufferUsage.UNIFORM)===GPUBufferUsage.UNIFORM){let n=this.freeUniformBuffers.get(t.size)||[];r===void 0||n.length>=r?t.destroy():n.push(t)}else t.destroy()}this.buffersPending=[]}else{let t=this.capturedPendingBuffers.get(this.backend.currentSessionId);t||(t=[],this.capturedPendingBuffers.set(this.backend.currentSessionId,t));for(let r of this.buffersPending)t.push(r);this.buffersPending=[]}}dispose(){this.freeBuffers.forEach(t=>{t.forEach(r=>{r.destroy()})}),this.freeUniformBuffers.forEach(t=>{t.forEach(r=>{r.destroy()})}),this.storageCache.forEach(t=>{t.gpuData.buffer.destroy()}),this.capturedPendingBuffers.forEach(t=>{t.forEach(r=>{r.destroy()})}),this.storageCache=new Map,this.freeBuffers=new Map,this.freeUniformBuffers=new Map,this.capturedPendingBuffers=new Map}onCreateSession(){this.sessionCount+=1}onReleaseSession(t){let r=this.capturedPendingBuffers.get(t);r&&(r.forEach(n=>{n.destroy()}),this.capturedPendingBuffers.delete(t)),this.sessionCount-=1,this.sessionCount===0&&(pe("warning",()=>"[WebGPU] Clearing webgpu buffer cache"),this.storageCache.forEach(n=>{n.gpuData.buffer.destroy()}),this.storageCache=new Map)}},Ya=(...e)=>new eo(...e)});var ro,ee,Ie=U(()=>{"use strict";ro=class{constructor(t){Object.assign(this,t)}get cacheKey(){return this.key||(this.key=Object.getOwnPropertyNames(this).sort().map(t=>`${this[t]}`).join(";")),this.key}},ee=e=>new ro(e)});var no,rt,k,Ct,Gr,Qa,Za,ae=U(()=>{"use strict";no=class{static calcMatMulShape(t,r){return t[1]!==r[0]?void 0:[t[0],r[1]]}},rt=class{static calcShape(t,r,n=!1){let o=t.length,i=r.length;if(o===0)return r;if(i===0)return t;let a=Math.max(t.length,r.length),d=new Array(a);if(n){if(o<2||i<2)return;let l=no.calcMatMulShape([t[o-2],t[o-1]],[r[i-2],r[i-1]]);if(l===void 0)return;[d[a-2],d[a-1]]=l}for(let l=n?3:1;l<=a;l++){let c=o-l<0?1:t[o-l],m=i-l<0?1:r[i-l];if(c!==m&&c>1&&m>1)return;let u=Math.max(c,m);if(c&&m)d[a-l]=Math.max(c,m);else{if(u>1)return;d[a-l]=0}}return d}static isValidBroadcast(t,r){let n=t.length,o=r.length;if(n>o)return!1;for(let i=1;i<=n;i++)if(t[n-i]!==1&&t[n-i]!==r[o-i])return!1;return!0}},k=class e{static size(t){return e.getSizeFromDimensionRange(t,0,t.length)}static convertShape(t,r=4){let n=t.length;if(n===0)return[];let o=new Array(n),i=n-1;for(;i>=0;){if(t[i]%r===0){o[i]=t[i]/r;break}if(r%t[i]!==0)throw new Error("cannot convert shape");o[i]=1,r/=t[i],i--}for(i--;i>=0;i--)o[i]=t[i];return o}static sizeFromDimension(t,r){if(r<0||r>t.length)throw new Error(`invalid dimension of ${r} for sizeFromDimension as Tensor has ${t.length} dimensions.`);return e.getSizeFromDimensionRange(t,r,t.length)}static sizeToDimension(t,r){if(r<0||r>t.length)throw new Error(`invalid dimension of ${r} for sizeToDimension as Tensor has ${t.length} dimensions.`);return e.getSizeFromDimensionRange(t,0,r)}static getSizeFromDimensionRange(t,r,n){let o=1;for(let i=r;i=0;--o)n[o]=n[o+1]*t[o+1];return n}static normalizeAxis(t,r){if(t<-r&&t>=r)throw new Error("unsupported axis for this operation.");return t<0?t+r:t}static normalizeAxes(t,r){return t.map(n=>this.normalizeAxis(n,r??t.length))}static sortBasedOnPerm(t,r){return r?r.map(n=>t[n]):t.slice().reverse()}static padShape(t,r){let n=t.length;return t.map((o,i)=>o+r[i]+r[i+n])}static areEqual(t,r){return t.length!==r.length?!1:t.every((n,o)=>n===r[o])}},Ct=class e{static adjustPoolAttributes(t,r,n,o,i,a){if(!t&&n.length!==r.length-2)throw new Error("length of specified kernel shapes should be 2 less than length of input dimensions");if(t)for(let d=0;d=n.length?n.push(r[d+2]):n[d]=r[d+2];for(let d=0;d=n[d]||a[d+n.length]>=n[d])throw new Error("pads should be smaller than kernel")}}static adjustPadsBasedOnAutoPad(t,r,n,o,i,a,d){if(d){if(i.length!==2*(t.length-2))throw new Error("length of pads should be twice the length of data dimensions");if(r.length!==t.length-2)throw new Error("length of strides should be the length of data dimensions");if(o.length!==t.length-2)throw new Error("length of kernel shapes should be the length of data dimensions");for(let l=0;l{"use strict";J();ae();At=64,io=(e,t)=>{if(t===3)throw new Error("vec3 has same alignment as vec4, use vec4 instead");switch(e){case 10:return t>1?`vec${t}`:"f16";case 1:return t>1?`vec${t}`:"f32";case 6:return t>1?`vec${t}`:"i32";case 12:return t>1?`vec${t}`:"u32";case 7:if(t>1)throw new Error("currently not supported vecX of uint64 yet");return["vec2","i32"];case 13:if(t>1)throw new Error("currently not supported vecX of uint64 yet");return["vec2","u32"];case 9:if(t!==4)throw new Error("bool must be vec4");return["u32","vec4"];case 22:return"i32";case 21:return"u32";default:throw new Error(`Unknown data type: ${e}`)}},he=(e,t=1)=>{let r=io(e,t);return typeof r=="string"?r:r[0]},Ee=(e,t=1)=>{let r=io(e,t);return typeof r=="string"?r:r[1]},V=(...e)=>{let t=[];return e.forEach(r=>{r.length!==0&&t.push({type:12,data:r},{type:12,data:k.computeStrides(r)})}),t},we=e=>e%4===0?4:e%2===0?2:1,ao=(e="f32",t,r="0")=>!t||t===1?`${e}(${r})`:`vec${t}<${e}>(${r})`,kt=(e,t,r)=>e==="f32"?r:t===1?`f32(${r})`:`vec${t}(${r})`,Qe=(e,t)=>t===4?`(${e}.x + ${e}.y + ${e}.z + ${e}.w)`:t===2?`(${e}.x + ${e}.y)`:t===3?`(${e}.x + ${e}.y + ${e}.z)`:e,F=(e,t,r,n)=>e.startsWith("uniforms.")&&r>4?typeof t=="string"?n==="f16"?`${e}[(${t}) / 8][(${t}) % 8 / 4][(${t}) % 8 % 4]`:`${e}[(${t}) / 4][(${t}) % 4]`:n==="f16"?`${e}[${Math.floor(t/8)}][${Math.floor(t%8/4)}][${t%8%4}]`:`${e}[${Math.floor(t/4)}][${t%4}]`:r>1?`${e}[${t}]`:e,so=(e,t,r,n,o)=>{let i=typeof r=="number",a=i?r:r.length,d=[...new Array(a).keys()],l=a<2?"u32":a<=4?`vec${a}`:`array`,c=io(t,o),m=typeof c=="string"?c:c[1],u=typeof c=="string"?c:c[0],h={indices:l,value:m,storage:u,tensor:t},w=R=>typeof R=="string"?R:`${R}u`,g={offsetToIndices:!1,indicesToOffset:!1,broadcastedIndicesToOffset:!1,set:!1,setByIndices:!1,get:!1,getByIndices:!1},y=i?"uniforms.":"",S=`${y}${e}_shape`,$=`${y}${e}_strides`,v="";for(let R=0;R ${h.indices} { + var indices: ${h.indices}; + var current = offset; + ${v} + return indices; + }`,T=R=>(g.offsetToIndices=!0,a<2?R:`o2i_${e}(${R})`),C=[];if(a>=2)for(let R=a-1;R>=0;R--)C.push(`${F($,R,a)} * (indices[${R}])`);let A=a<2?"":` + fn i2o_${e}(indices: ${h.indices}) -> u32 { + return ${C.join("+")}; + }`,P=R=>(g.indicesToOffset=!0,a<2?R:`i2o_${e}(${R})`),D=(...R)=>a===0?"0u":`${h.indices}(${R.map(w).join(",")})`,W=(R,G)=>a<2?`${R}`:`${F(R,G,a)}`,N=(R,G,ye)=>a<2?`${R}=${ye};`:`${F(R,G,a)}=${ye};`,j={},Y=(R,G)=>{g.broadcastedIndicesToOffset=!0;let ye=`${G.name}broadcastedIndicesTo${e}Offset`;if(ye in j)return`${ye}(${R})`;let Re=[];for(let $e=a-1;$e>=0;$e--){let Ce=G.indicesGet("outputIndices",$e+G.rank-a);Re.push(`${W($,$e)} * (${Ce} % ${W(S,$e)})`)}return j[ye]=`fn ${ye}(outputIndices: ${G.type.indices}) -> u32 { + return ${Re.length>0?Re.join("+"):"0u"}; + }`,`${ye}(${R})`},Z=(R,G)=>(()=>{if(h.storage===h.value)return`${e}[${R}]=${G};`;if(h.storage==="vec2"&&h.value==="i32")return`${e}[${R}]=vec2(u32(${G}), select(0u, 0xFFFFFFFFu, ${G} < 0));`;if(h.storage==="vec2"&&h.value==="u32")return`${e}[${R}]=vec2(u32(${G}), 0u);`;if(h.storage==="u32"&&h.value==="vec4")return`${e}[${R}]=dot(vec4(0x1, 0x100, 0x10000, 0x1000000), vec4(${G}));`;throw new Error(`not supported combination of storage type ${h.storage} and value type ${h.value} yet`)})(),te=R=>(()=>{if(h.storage===h.value)return`${e}[${R}]`;if(h.storage==="vec2"&&h.value==="i32")return`i32(${e}[${R}].x)`;if(h.storage==="vec2"&&h.value==="u32")return`u32(${e}[${R}].x)`;if(h.storage==="u32"&&h.value==="vec4")return`vec4(bool(${e}[${R}] & 0xFFu), bool(${e}[${R}] & 0xFF00u), bool(${e}[${R}] & 0xFF0000u), bool(${e}[${R}] & 0xFF000000u))`;throw new Error(`not supported combination of storage type ${h.storage} and value type ${h.value} yet`)})(),ue=a<2?"":` + fn get_${e}ByIndices(indices: ${h.indices}) -> ${m} { + return ${te(`i2o_${e}(indices)`)}; + }`,K=a<2?"":(()=>{let R=d.map(ye=>`d${ye}: u32`).join(", "),G=d.map(ye=>`d${ye}`).join(", ");return` + fn get_${e}(${R}) -> ${m} { + return get_${e}ByIndices(${D(G)}); + }`})(),de=(...R)=>{if(R.length!==a)throw new Error(`indices length must be ${a}`);let G=R.map(w).join(",");return a===0?te("0u"):a===1?te(G[0]):(g.get=!0,g.getByIndices=!0,g.indicesToOffset=!0,`get_${e}(${G})`)},ce=R=>a<2?te(R):(g.getByIndices=!0,g.indicesToOffset=!0,`get_${e}ByIndices(${R})`),q=a<2?"":` + fn set_${e}ByIndices(indices: ${h.indices}, value: ${m}) { + ${Z(`i2o_${e}(indices)`,"value")} + }`,le=a<2?"":(()=>{let R=d.map(ye=>`d${ye}: u32`).join(", "),G=d.map(ye=>`d${ye}`).join(", ");return` + fn set_${e}(${R}, value: ${m}) { + set_${e}ByIndices(${D(G)}, value); + }`})();return{impl:()=>{let R=[],G=!1;return g.offsetToIndices&&(R.push(x),G=!0),g.indicesToOffset&&(R.push(A),G=!0),g.broadcastedIndicesToOffset&&(Object.values(j).forEach(ye=>R.push(ye)),G=!0),g.set&&(R.push(le),G=!0),g.setByIndices&&(R.push(q),G=!0),g.get&&(R.push(K),G=!0),g.getByIndices&&(R.push(ue),G=!0),!i&&G&&R.unshift(`const ${S} = ${h.indices}(${r.join(",")});`,`const ${$} = ${h.indices}(${k.computeStrides(r).join(",")});`),R.join(` +`)},type:h,offsetToIndices:T,indicesToOffset:P,broadcastedIndicesToOffset:Y,indices:D,indicesGet:W,indicesSet:N,set:(...R)=>{if(R.length!==a+1)throw new Error(`indices length must be ${a}`);let G=R[a];if(typeof G!="string")throw new Error("value must be string");let ye=R.slice(0,a).map(w).join(",");return a===0?Z("0u",G):a===1?Z(ye[0],G):(g.set=!0,g.setByIndices=!0,g.indicesToOffset=!0,`set_${e}(${ye}, ${G})`)},setByOffset:Z,setByIndices:(R,G)=>a<2?Z(R,G):(g.setByIndices=!0,g.indicesToOffset=!0,`set_${e}ByIndices(${R}, ${G});`),get:de,getByOffset:te,getByIndices:ce,usage:n,name:e,strides:$,shape:S,rank:a}},E=(e,t,r,n=1)=>so(e,t,r,"input",n),M=(e,t,r,n=1)=>so(e,t,r,"output",n),Fr=(e,t,r,n=1)=>so(e,t,r,"internal",n),oo=class{constructor(t,r){this.normalizedDispatchGroup=t;this.limits=r;this.internalVariables=[];this.variables=[];this.uniforms=[];this.variableIndex=0}guardAgainstOutOfBoundsWorkgroupSizes(t){return`if (global_idx >= ${typeof t=="number"?`${t}u`:t}) { return; }`}mainStart(t=At){let r=typeof t=="number"?t:t[0],n=typeof t=="number"?1:t[1],o=typeof t=="number"?1:t[2];if(r>this.limits.maxComputeWorkgroupSizeX||n>this.limits.maxComputeWorkgroupSizeY||o>this.limits.maxComputeWorkgroupSizeZ)throw new Error(`workgroup size [${r}, ${n}, ${o}] exceeds the maximum workgroup size [${this.limits.maxComputeWorkgroupSizeX}, ${this.limits.maxComputeWorkgroupSizeY}, ${this.limits.maxComputeWorkgroupSizeZ}].`);if(r*n*o>this.limits.maxComputeInvocationsPerWorkgroup)throw new Error(`workgroup size [${r}, ${n}, ${o}] exceeds the maximum workgroup invocations ${this.limits.maxComputeInvocationsPerWorkgroup}.`);let i=this.normalizedDispatchGroup[1]===1&&this.normalizedDispatchGroup[2]===1,a=i?`@builtin(global_invocation_id) global_id : vec3, + @builtin(workgroup_id) workgroup_id : vec3, + @builtin(local_invocation_index) local_idx : u32, + @builtin(local_invocation_id) local_id : vec3`:`@builtin(global_invocation_id) global_id : vec3, + @builtin(local_invocation_id) local_id : vec3, + @builtin(local_invocation_index) local_idx : u32, + @builtin(workgroup_id) workgroup_id : vec3, + @builtin(num_workgroups) num_workgroups : vec3`,d=i?`let global_idx = global_id.x; + let workgroup_index = workgroup_id.x;`:`let workgroup_index = workgroup_id.z * num_workgroups[0] * num_workgroups[1] + + workgroup_id.y * num_workgroups[0] + workgroup_id.x; + let global_idx = workgroup_index * ${r*n*o}u + local_idx;`;return`@compute @workgroup_size(${r}, ${n}, ${o}) + fn main(${a}) { + ${d} + `}appendVariableUniforms(t){t.rank!==0&&(t.shape.startsWith("uniforms.")&&this.uniforms.push({name:t.shape.replace("uniforms.",""),type:"u32",length:t.rank}),t.strides.startsWith("uniforms.")&&this.uniforms.push({name:t.strides.replace("uniforms.",""),type:"u32",length:t.rank}))}declareVariable(t,r){if(t.usage==="internal")throw new Error("cannot use internal variable with declareVariable(). use registerInternalVariables() instead.");this.variables.push(t),this.appendVariableUniforms(t);let n=t.usage==="input"?"read":"read_write",o=t.type.storage;return`@group(0) @binding(${r}) var ${t.name}: array<${o}>;`}declareVariables(...t){return t.map(r=>this.declareVariable(r,this.variableIndex++)).join(` +`)}registerInternalVariable(t){if(t.usage!=="internal")throw new Error("cannot use input or output variable with registerInternalVariable(). use declareVariables() instead.");this.internalVariables.push(t),this.appendVariableUniforms(t)}registerInternalVariables(...t){return t.forEach(r=>this.registerInternalVariable(r)),this}registerUniform(t,r,n=1){return this.uniforms.push({name:t,type:r,length:n}),this}registerUniforms(t){return this.uniforms=this.uniforms.concat(t),this}uniformDeclaration(){if(this.uniforms.length===0)return"";let t=[];for(let{name:r,type:n,length:o}of this.uniforms)if(o&&o>4)n==="f16"?t.push(`@align(16) ${r}:array, ${Math.ceil(o/8)}>`):t.push(`${r}:array, ${Math.ceil(o/4)}>`);else{let i=o==null||o===1?n:`vec${o}<${n}>`;t.push(`${r}:${i}`)}return` + struct Uniforms { ${t.join(", ")} }; + @group(0) @binding(${this.variableIndex}) var uniforms: Uniforms;`}get additionalImplementations(){return this.uniformDeclaration()+this.variables.map(t=>t.impl()).join(` +`)+this.internalVariables.map(t=>t.impl()).join(` +`)}get variablesInfo(){if(this.uniforms.length===0)return;let t=r=>[12,10,1,6][["u32","f16","f32","i32"].indexOf(r)];return this.uniforms.map(r=>[t(r.type),r.length??1])}},Ja=(e,t)=>new oo(e,t),Wt=(e,t)=>{let r=e.length,n=[];for(let o=0;o1&&a===1&&n.unshift(i)}return n}});var qp,es,jp,Kp,Yp,Pe,ts,rs,lt=U(()=>{"use strict";J();ae();Ie();se();qp=e=>{if(!e||e.length!==1)throw new Error("Transpose requires 1 input.")},es=(e,t)=>t&&t.length!==e?[...new Array(e).keys()].reverse():t,jp=(e,t)=>k.sortBasedOnPerm(e,es(e.length,t)),Kp=(e,t,r,n)=>{let o=`fn perm(i: ${n.type.indices}) -> ${r.type.indices} { + var a: ${r.type.indices};`;for(let i=0;i{let r=[],n=[];for(let o=0;o{let r=e.dataType,n=e.dims.length,o=es(n,t),i=jp(e.dims,o),{newShape:a,newPerm:d}=Yp(e.dims,o),l=k.areEqual(d,[2,3,1]),c=k.areEqual(d,[3,1,2]),m=a.length===2&&d[0]>d[1]||l||c,u=m?a:e.dims,h=i;m&&(u=l?[a[0],a[1]*a[2]]:c?[a[0]*a[1],a[2]]:a,h=[u[1],u[0]]);let w=E("a",r,u.length),g=M("output",r,h.length),y=16,S;return m?S=$=>` + ${$.registerUniform("output_size","u32").declareVariables(w,g)} + var tile : array, ${y}>; + ${$.mainStart([y,y,1])} + let stride = (uniforms.output_shape[1] - 1) / ${y} + 1; + let workgroup_id_x = workgroup_index % stride; + let workgroup_id_y = workgroup_index / stride; + let input_col = workgroup_id_y * ${y}u + local_id.x; + let input_row = workgroup_id_x * ${y}u + local_id.y; + if (input_row < uniforms.a_shape[0] && input_col < uniforms.a_shape[1]) { + tile[local_id.y][local_id.x] = ${w.getByIndices(`${w.type.indices}(input_row, input_col)`)}; + } + workgroupBarrier(); + + let output_col = workgroup_id_x * ${y}u + local_id.x; + let output_row = workgroup_id_y * ${y}u + local_id.y; + if (output_row < uniforms.output_shape[0] && output_col < uniforms.output_shape[1]) { + ${g.setByIndices(`${g.type.indices}(output_row, output_col)`,"tile[local_id.x][local_id.y]")} + } + }`:S=$=>` + ${$.registerUniform("output_size","u32").declareVariables(w,g)} + + ${Kp(o,n,w,g)} + + ${$.mainStart()} + ${$.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + + let indices = ${g.offsetToIndices("global_idx")}; + let aIndices = perm(indices); + + ${g.setByOffset("global_idx",w.getByIndices("aIndices"))} + }`,{name:m?"TransposeShared":"Transpose",shaderCache:{hint:`${t}`,inputDependencies:["rank"]},getRunData:()=>{let $=k.size(i);return{outputs:[{dims:i,dataType:e.dataType}],dispatchGroup:m?{x:Math.ceil(h[1]/y),y:Math.ceil(h[0]/y)}:{x:Math.ceil($/64)},programUniforms:[{type:12,data:$},...V(u,h)]}},getShaderSource:S}},ts=(e,t)=>{qp(e.inputs),e.compute(Pe(e.inputs[0],t.perm))},rs=e=>ee({perm:e.perm})});var Xp,Qp,Zp,Jp,em,tm,rm,nm,om,im,nt,ns,os,is,as,ss,us,ds,ls,cs,ps,ms=U(()=>{"use strict";J();ae();se();qr();lt();Xp={max:"select(bestValue, candidate, candidate > bestValue)",min:"select(bestValue, candidate, candidate < bestValue)",mean:"bestValue + candidate",sum:"bestValue + candidate",prod:"bestValue * candidate",sumSquare:"bestValue + candidate * candidate",logSumExp:"bestValue + exp(candidate)",l1:"bestValue + abs(candidate)",l2:"bestValue + candidate * candidate",logSum:"bestValue + candidate"},Qp={max:"select(bestValue, candidate, candidate > bestValue)",min:"select(bestValue, candidate, candidate < bestValue)",mean:"bestValue + candidate",sum:"bestValue + candidate",prod:"bestValue * candidate",sumSquare:"bestValue + candidate",logSumExp:"bestValue + candidate",l1:"bestValue + candidate",l2:"bestValue + candidate",logSum:"bestValue + candidate"},Zp={max:"_A[offset]",min:"_A[offset]",mean:"0",sum:"0",prod:"1",sumSquare:"0",logSumExp:"0",l1:"0",l2:"0",logSum:"0"},Jp={max:"bestValue",min:"bestValue",sum:"bestValue",prod:"bestValue",sumSquare:"bestValue",logSumExp:"log(bestValue)",l1:"bestValue",l2:"sqrt(bestValue)",logSum:"log(bestValue)"},em=(e,t)=>{let r=[];for(let n=t-e;n{let r=[],n=e.length;for(let i=0;ie[i]);return[r,o]},rm=(e,t)=>{let r=e.length+t.length,n=[],o=0;for(let i=0;i{for(let r=0;r{let r=[];if(!nm(e,t)){for(let n=0;nr.push(n))}return r},im=(e,t,r,n,o,i,a)=>{let d=r[0].dims,l=k.size(i),c=k.size(a),m=E("_A",r[0].dataType,d),u=M("output",o,i),h=32,w=` + var aBestValues : array; + `;return{name:e,shaderCache:t,getShaderSource:y=>` + ${y.registerUniform("reduceSize","u32").declareVariables(m,u)} + ${w} + fn DIV_CEIL(a : u32, b : u32) -> u32 { + return ((a - 1u) / b + 1u); + } + ${y.mainStart(h)} + + let outputIndex = global_idx / ${h}; + let offset = outputIndex * uniforms.reduceSize; + + var bestValue = f32(${Zp[n]}); + let Length = uniforms.reduceSize; + for (var k = local_idx; k < Length; k = k + ${h}) { + let candidate = f32(${m.getByOffset("offset + k")}); + bestValue = ${Xp[n]}; + } + aBestValues[local_idx] = bestValue; + workgroupBarrier(); + + var reduceSize = min(Length, ${h}u); + for (var currentSize = reduceSize / 2u; reduceSize > 1u; + currentSize = reduceSize / 2u) { + let interval = DIV_CEIL(reduceSize, 2u); + if (local_idx < currentSize) { + let candidate = aBestValues[local_idx + interval]; + bestValue = ${Qp[n]}; + aBestValues[local_idx] = bestValue; + } + reduceSize = interval; + workgroupBarrier(); + } + + if (local_idx == 0u) { + ${u.setByOffset("outputIndex",`${n==="mean"?`${u.type.storage}(bestValue / f32(uniforms.reduceSize))`:`${u.type.storage}(${Jp[n]})`}`)}; + } + }`,getRunData:()=>({outputs:[{dims:i,dataType:o}],dispatchGroup:{x:l},programUniforms:[{type:12,data:c}]})}},nt=(e,t,r,n)=>{let o=e.inputs.length===1?r:uo(e.inputs,r),i=o.axes;i.length===0&&!o.noopWithEmptyAxes&&(i=e.inputs[0].dims.map((w,g)=>g));let a=k.normalizeAxes(i,e.inputs[0].dims.length),d=a,l=e.inputs[0],c=om(d,e.inputs[0].dims.length);c.length>0&&(l=e.compute(Pe(e.inputs[0],c),{inputs:[0],outputs:[-1]})[0],d=em(d.length,l.dims.length));let[m,u]=tm(l.dims,d),h=m;o.keepDims&&(h=rm(m,a)),e.compute(im(t,{hint:o.cacheKey,inputDependencies:["type"]},[l],n,e.inputs[0].dataType,h,u),{inputs:[l]})},ns=(e,t)=>{nt(e,"ReduceMeanShared",t,"mean")},os=(e,t)=>{nt(e,"ReduceL1Shared",t,"l1")},is=(e,t)=>{nt(e,"ReduceL2Shared",t,"l2")},as=(e,t)=>{nt(e,"ReduceLogSumExpShared",t,"logSumExp")},ss=(e,t)=>{nt(e,"ReduceMaxShared",t,"max")},us=(e,t)=>{nt(e,"ReduceMinShared",t,"min")},ds=(e,t)=>{nt(e,"ReduceProdShared",t,"prod")},ls=(e,t)=>{nt(e,"ReduceSumShared",t,"sum")},cs=(e,t)=>{nt(e,"ReduceSumSquareShared",t,"sumSquare")},ps=(e,t)=>{nt(e,"ReduceLogSumShared",t,"logSum")}});var ot,am,jr,uo,it,sm,um,dm,lm,cm,pm,mm,fm,hm,gm,at,fs,hs,gs,ys,bs,ws,_s,vs,$s,xs,qr=U(()=>{"use strict";J();ae();Ie();se();ms();ot=e=>{if(!e||e.length===0||e.length>2)throw new Error("Reduce op requires 1 or 2 inputs.");if(e.length===2&&e[1].dims.length!==1)throw new Error("Invalid axes input dims.")},am=e=>["","",`var value = ${e.getByIndices("input_indices")};`,""],jr=(e,t,r,n,o,i,a=!1,d=!1)=>{let l=[],c=r[0].dims,m=c.length,u=k.normalizeAxes(o,m),h=!d&&u.length===0;c.forEach((S,$)=>{h||u.indexOf($)>=0?a&&l.push(1):l.push(S)});let w=l.length,g=k.size(l);return{name:e,shaderCache:t,getShaderSource:S=>{let $=[],v=E("_A",r[0].dataType,m),x=M("output",i,w),T=n(v,x,u),C=T[2];for(let A=0,P=0;A=0?(a&&P++,C=`for(var j${A}: u32 = 0; j${A} < ${c[A]}; j${A}++) { + ${T[2].includes("last_index")?`let last_index = j${A};`:""} + ${v.indicesSet("input_indices",A,`j${A}`)} + ${C} + }`):($.push(`${v.indicesSet("input_indices",A,x.indicesGet("output_indices",P))};`),P++);return` + + ${S.registerUniform("output_size","u32").declareVariables(v,x)} + + ${S.mainStart()} + ${S.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + var input_indices: ${v.type.indices}; + let output_indices = ${x.offsetToIndices("global_idx")}; + + ${$.join(` +`)} + ${T[0]} // init ops for reduce max/min + ${T[1]} + ${C} + ${T[3]} + ${T.length===4?x.setByOffset("global_idx","value"):T.slice(4).join(` +`)} + }`},getRunData:()=>({outputs:[{dims:l,dataType:i}],dispatchGroup:{x:Math.ceil(g/64)},programUniforms:[{type:12,data:g},...V(c,l)]})}},uo=(e,t)=>{let r=[];return e[1].dims[0]>0&&e[1].getBigInt64Array().forEach(n=>r.push(Number(n))),ee({axes:r,keepDims:t.keepDims,noopWithEmptyAxes:t.noopWithEmptyAxes})},it=(e,t,r,n)=>{let o=e.inputs,i=o.length===1?r:uo(o,r);e.compute(jr(t,{hint:i.cacheKey,inputDependencies:["rank"]},[o[0]],i.noopWithEmptyAxes&&i.axes.length===0?am:n,i.axes,o[0].dataType,i.keepDims,i.noopWithEmptyAxes),{inputs:[0]})},sm=(e,t)=>{ot(e.inputs),it(e,"ReduceLogSum",t,(n,o)=>[`var value = ${o.type.storage}(0);`,"",`value += ${n.getByIndices("input_indices")};`,"value = log(value);"])},um=(e,t)=>{ot(e.inputs),it(e,"ReduceL1",t,(n,o)=>[`var value = ${o.type.storage}(0);`,"",`value += abs(${n.getByIndices("input_indices")});`,""])},dm=(e,t)=>{ot(e.inputs),it(e,"ReduceL2",t,(n,o)=>[`var t = ${o.type.value}(0); var value = ${o.type.value}(0);`,"",`t = ${n.getByIndices("input_indices")}; value += (t * t);`,"value = sqrt(value);"])},lm=(e,t)=>{ot(e.inputs),it(e,"ReduceLogSumExp",t,(n,o)=>[`var value = ${o.type.storage}(0);`,"",`value += exp(${n.getByIndices("input_indices")});`,"value = log(value);"])},cm=(e,t)=>{ot(e.inputs),it(e,"ReduceMax",t,(n,o,i)=>{let a=[];for(let d=0;d=0||i.length===0)&&a.push(n.indicesSet("input_indices",d,0));return[`${a.join(` +`)}`,`var value = ${n.getByIndices("input_indices")};`,`value = max(value, ${n.getByIndices("input_indices")});`,""]})},pm=(e,t)=>{ot(e.inputs),it(e,"ReduceMean",t,(n,o,i)=>{let a=1;for(let d=0;d=0||i.length===0)&&(a*=e.inputs[0].dims[d]);return["var sum = f32(0);","",`sum += f32(${n.getByIndices("input_indices")});`,`let value = ${o.type.value}(sum / ${a});`]})},mm=(e,t)=>{ot(e.inputs),it(e,"ReduceMin",t,(n,o,i)=>{let a=[];for(let d=0;d=0||i.length===0)&&a.push(`input_indices[${d}] = 0;`);return[`${a.join(` +`)}`,`var value = ${n.getByIndices("input_indices")};`,`value = min(value, ${n.getByIndices("input_indices")});`,""]})},fm=(e,t)=>{ot(e.inputs),it(e,"ReduceProd",t,(n,o)=>[`var value = ${o.type.storage}(1);`,"",`value *= ${n.getByIndices("input_indices")};`,""])},hm=(e,t)=>{ot(e.inputs),it(e,"ReduceSum",t,(n,o)=>[`var value = ${o.type.storage}(0);`,"",`value += ${n.getByIndices("input_indices")};`,""])},gm=(e,t)=>{ot(e.inputs),it(e,"ReduceSumSquare",t,(n,o)=>[`var t = ${o.type.value}(0); var value = ${o.type.value}(0);`,"",`t = ${n.getByIndices("input_indices")}; value += t * t;`,""])},at=(e,t,r)=>{if(t.length===0)return r;let n=1,o=1;for(let i=0;i1024},fs=(e,t)=>{at(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?pm(e,t):ns(e,t)},hs=(e,t)=>{at(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?um(e,t):os(e,t)},gs=(e,t)=>{at(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?dm(e,t):is(e,t)},ys=(e,t)=>{at(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?lm(e,t):as(e,t)},bs=(e,t)=>{at(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?cm(e,t):ss(e,t)},ws=(e,t)=>{at(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?mm(e,t):us(e,t)},_s=(e,t)=>{at(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?fm(e,t):ds(e,t)},vs=(e,t)=>{at(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?hm(e,t):ls(e,t)},$s=(e,t)=>{at(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?gm(e,t):cs(e,t)},xs=(e,t)=>{at(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?sm(e,t):ps(e,t)}});var Ss,Ts,Is,lo,Cs=U(()=>{"use strict";J();Ie();qr();Ss=e=>{if(!e||e.length===0||e.length>2)throw new Error("ArgMinMaxOp op requires 1 or 2 inputs.");if(e[0].dataType!==1)throw new Error("Invalid input type.")},Ts=(e,t)=>{Ss(e.inputs);let r=(n,o,i)=>{let a=[];for(let d=0;d=0||i.length===0)&&a.push(`input_indices[${d}] = 0;`);return[`${a.join(` +`)}`,`var value = ${n.getByIndices("input_indices")}; +var best_index : i32 = 0;`,`if (${n.getByIndices("input_indices")} ${t.selectLastIndex>0?"<=":"<"} value) { + value = ${n.getByIndices("input_indices")}; + best_index = i32(last_index); + }`,"",o.setByOffset("global_idx","best_index")]};e.compute(jr("ArgMin",{hint:t.cacheKey,inputDependencies:["rank"]},[e.inputs[0]],r,[t.axis],7,t.keepDims),{inputs:[0]})},Is=(e,t)=>{Ss(e.inputs);let r=(n,o,i)=>{let a=[];for(let d=0;d=0||i.length===0)&&a.push(`input_indices[${d}] = 0;`);return[`${a.join(` +`)}`,`var value = ${n.getByIndices("input_indices")}; +var best_index : i32 = 0;`,`if (${n.getByIndices("input_indices")} ${t.selectLastIndex>0?">=":">"} value) { + value = ${n.getByIndices("input_indices")}; + best_index = i32(last_index); + }`,"",o.setByOffset("global_idx","best_index")]};e.compute(jr("argMax",{hint:t.cacheKey,inputDependencies:["rank"]},[e.inputs[0]],r,[t.axis],7,t.keepDims),{inputs:[0]})},lo=e=>ee(e)});var ym,co,bm,wm,_m,Nt,vm,As,Kr=U(()=>{"use strict";J();ae();Hr();se();ym=(e,t)=>{let r=e[0],n=e[1],o=e[2],i=e[3],a=e[4],d=e[5];if(a&&d)throw new Error("Attention cannot have both past and attention_bias");if(r.dims.length!==3)throw new Error('Input "input" must have 3 dimensions');let l=r.dims[0],c=r.dims[1],m=r.dims[2];if(o.dims.length!==1)throw new Error('Input "bias" is expected to have 1 dimensions');if(n.dims.length!==2)throw new Error('Input "weights" is expected to have 2 dimensions');if(n.dims[0]!==m)throw new Error("Input 1 dimension 0 should have same length as dimension 2 of input 0");if(o.dims[0]!==n.dims[1])throw new Error('Input "bias" dimension 0 should have same length as dimension 1 of input "weights"');let u=o.dims[0]/3,h=u,w=h;if(t.qkvHiddenSizes.length>0){if(t.qkvHiddenSizes.length!==3)throw new Error("qkv_hidden_sizes attribute should have 3 elements");for(let x of t.qkvHiddenSizes)if(x%t.numHeads!==0)throw new Error("qkv_hidden_sizes should be divisible by num_heads");u=t.qkvHiddenSizes[0],h=t.qkvHiddenSizes[1],w=t.qkvHiddenSizes[2]}let g=c;if(u!==h)throw new Error("qkv_hidden_sizes first element should be same as the second");if(o.dims[0]!==u+h+w)throw new Error('Input "bias" dimension 0 should have same length as sum of Q/K/V hidden sizes');let y=0;if(a){if(h!==w)throw new Error('Input "past" expect k_hidden_size == v_hidden_size');if(a.dims.length!==5)throw new Error('Input "past" must have 5 dimensions');if(a.dims[0]!==2)throw new Error('Input "past" first dimension must be 2');if(a.dims[1]!==l)throw new Error('Input "past" second dimension must be batch_size');if(a.dims[2]!==t.numHeads)throw new Error('Input "past" third dimension must be num_heads');if(a.dims[4]!==h/t.numHeads)throw new Error('Input "past" fifth dimension must be k_hidden_size / num_heads');t.pastPresentShareBuffer||(y=a.dims[3])}let S=g+y,$=-1,v=0;if(i)throw new Error("Mask not supported");if(a)throw new Error("past is not supported");if(d){if(d.dims.length!==4)throw new Error('Input "attention_bias" must have 4 dimensions');if(d.dims[0]!==l||d.dims[1]!==t.numHeads||d.dims[2]!==c||d.dims[3]!==S)throw new Error('Expect "attention_bias" shape (batch_size, num_heads, sequence_length, total_sequence_length)')}return{batchSize:l,sequenceLength:c,pastSequenceLength:y,kvSequenceLength:g,totalSequenceLength:S,maxSequenceLength:$,inputHiddenSize:m,hiddenSize:u,vHiddenSize:w,headSize:Math.floor(u/t.numHeads),vHeadSize:Math.floor(w/t.numHeads),numHeads:t.numHeads,isUnidirectional:!1,pastPresentShareBuffer:!1,maskFilterValue:t.maskFilterValue,maskType:v,scale:t.scale,broadcastResPosBias:!1,passPastInKv:!1,qkvFormat:1}},co=(e,t,r)=>t&&e?` + let total_sequence_length_input = u32(${t.getByOffset("0")}); + let present_sequence_length = max(total_sequence_length_input, uniforms.past_sequence_length); + let is_subsequent_prompt: bool = sequence_length > 1 && sequence_length != total_sequence_length_input; + let is_first_prompt: bool = is_subsequent_prompt == false && sequence_length == total_sequence_length_input; + total_sequence_length = u32(${e?.getByOffset("batchIdx")}) + 1; + var past_sequence_length: u32 = 0; + if (is_first_prompt == false) { + past_sequence_length = total_sequence_length - sequence_length; + } + `:` + ${r?"let past_sequence_length = uniforms.past_sequence_length":""}; + let present_sequence_length = total_sequence_length; + `,bm=(e,t,r,n,o,i,a,d)=>{let l=we(a?1:i),c=64,m=i/l;m{let v=M("x",e.dataType,e.dims,l),x=[v],T=a?E("seq_lens",a.dataType,a.dims):void 0;T&&x.push(T);let C=d?E("total_sequence_length_input",d.dataType,d.dims):void 0;C&&x.push(C);let A=Ee(e.dataType),P=[{name:"batch_size",type:"u32"},{name:"num_heads",type:"u32"},{name:"past_sequence_length",type:"u32"},{name:"sequence_length",type:"u32"},{name:"total_sequence_length",type:"u32"},{name:"elements_per_thread",type:"u32"}];return` + var thread_max: array; + var thread_sum: array; + ${$.registerUniforms(P).declareVariables(...x)} + ${$.mainStart([c,1,1])} + let batchIdx = workgroup_id.z / uniforms.num_heads; + let headIdx = workgroup_id.z % uniforms.num_heads; + let sequence_length = uniforms.sequence_length; + var total_sequence_length = uniforms.total_sequence_length; + ${co(T,C,!1)} + let local_offset = local_idx * uniforms.elements_per_thread; + let offset = (global_idx / ${c}) * uniforms.total_sequence_length + local_offset; + let seq_causal_length = ${a?"u32(past_sequence_length + workgroup_id.y + 1)":"total_sequence_length"}; + var thread_max_vector = ${g}(-3.402823e+38f); + for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < seq_causal_length; i++) { + thread_max_vector = max(${g}(x[offset + i]), thread_max_vector); + } + thread_max[local_idx] = ${(()=>{switch(l){case 1:return"thread_max_vector";case 2:return"max(thread_max_vector.x, thread_max_vector.y)";case 4:return"max(max(thread_max_vector.x, thread_max_vector.y), max(thread_max_vector.z, thread_max_vector.w))";default:throw new Error(`Unsupported components: ${l}`)}})()}; + workgroupBarrier(); + + var max_value = f32(-3.402823e+38f); + for (var i = 0u; i < ${c}; i++) { + max_value = max(thread_max[i], max_value); + } + + var sum_vector = ${g}(0); + for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < seq_causal_length; i++) { + sum_vector += exp(${g}(x[offset + i]) - max_value); + } + thread_sum[local_idx] = ${(()=>{switch(l){case 1:return"sum_vector";case 2:return"sum_vector.x + sum_vector.y";case 4:return"sum_vector.x + sum_vector.y + sum_vector.z + sum_vector.w";default:throw new Error(`Unsupported components: ${l}`)}})()}; + workgroupBarrier(); + + var sum: f32 = 0; + for (var i = 0u; i < ${c}; i++) { + sum += thread_sum[i]; + } + + if (sum == 0) { + for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < seq_causal_length; i++) { + x[offset + i] = ${v.type.value}(${A}(1.0) / ${A}(seq_causal_length)); + } + } else { + for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < seq_causal_length; i++) { + var f32input = ${g}(x[offset + i]); + x[offset + i] = ${v.type.value}(exp(f32input - max_value) / sum); + } + } + ${a?` + for (var total_seq_id: u32 = seq_causal_length; total_seq_id + local_offset < uniforms.total_sequence_length; total_seq_id++) { + x[offset + total_seq_id] = ${v.type.value}(${A}(0)); + }`:""}; + }`};return{name:"AttentionProbsSoftmax",shaderCache:{hint:`${c};${w};${l}`,inputDependencies:y},getShaderSource:S,getRunData:()=>({outputs:[],dispatchGroup:{x:Math.ceil(i/c),y:o,z:t*r},programUniforms:h})}},wm=(e,t,r,n,o,i,a,d,l)=>{let c=a+i.kvSequenceLength,m=[i.batchSize,i.numHeads,i.sequenceLength,c],u=e>1&&n,h=i.kvNumHeads?i.kvNumHeads:i.numHeads,w=u?[i.batchSize,h,c,i.headSize]:void 0,g=i.nReps?i.nReps:1,y=i.scale===0?1/Math.sqrt(i.headSize):i.scale,S=we(i.headSize),$=i.headSize/S,v=12,x={x:Math.ceil(c/v),y:Math.ceil(i.sequenceLength/v),z:i.batchSize*i.numHeads},T=[{type:12,data:i.sequenceLength},{type:12,data:$},{type:12,data:c},{type:12,data:i.numHeads},{type:12,data:i.headSize},{type:1,data:y},{type:12,data:a},{type:12,data:i.kvSequenceLength},{type:12,data:g}],C=u&&n&&k.size(n.dims)>0,A=["type","type"];C&&A.push("type"),o&&A.push("type"),d&&A.push("type"),l&&A.push("type");let P=[{dims:m,dataType:t.dataType,gpuDataType:0}];u&&P.push({dims:w,dataType:t.dataType,gpuDataType:0});let D=W=>{let N=E("q",t.dataType,t.dims,S),j=E("key",r.dataType,r.dims,S),Y=[N,j];if(C){let q=E("past_key",n.dataType,n.dims,S);Y.push(q)}o&&Y.push(E("attention_bias",o.dataType,o.dims));let Z=d?E("seq_lens",d.dataType,d.dims):void 0;Z&&Y.push(Z);let te=l?E("total_sequence_length_input",l.dataType,l.dims):void 0;te&&Y.push(te);let ue=M("output",t.dataType,m),K=[ue];u&&K.push(M("present_key",t.dataType,w,S));let de=Ee(1,S),ce=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"head_size",type:"u32"},{name:"alpha",type:"f32"},{name:"past_sequence_length",type:"u32"},{name:"kv_sequence_length",type:"u32"},{name:"n_reps",type:"u32"}];return` + const TILE_SIZE = ${v}u; + + var tileQ: array<${N.type.storage}, ${v*v}>; + var tileK: array<${N.type.storage}, ${v*v}>; + ${W.registerUniforms(ce).declareVariables(...Y,...K)} + ${W.mainStart([v,v,1])} + // x holds the N and y holds the M + let headIdx = workgroup_id.z % uniforms.num_heads; + let kvHeadIdx = ${g===1?"headIdx":"headIdx / uniforms.n_reps"}; + let kv_num_heads = ${g===1?"uniforms.num_heads":"uniforms.num_heads / uniforms.n_reps"}; + let batchIdx = workgroup_id.z / uniforms.num_heads; + let m = workgroup_id.y * TILE_SIZE; + let n = workgroup_id.x * TILE_SIZE; + let sequence_length = uniforms.M; + var total_sequence_length = uniforms.N; + ${co(Z,te,!0)} + let absKvHeadIdx = batchIdx * kv_num_heads + kvHeadIdx; + let qOffset = workgroup_id.z * uniforms.M * uniforms.K + m * uniforms.K; + ${C&&u?"let pastKeyOffset = absKvHeadIdx * uniforms.past_sequence_length * uniforms.K;":""}; + let kOffset = absKvHeadIdx * uniforms.kv_sequence_length * uniforms.K; + ${u?"let presentKeyOffset = absKvHeadIdx * uniforms.N * uniforms.K;":""} + var value = ${de}(0); + for (var w: u32 = 0u; w < uniforms.K; w += TILE_SIZE) { + if (global_id.y < uniforms.M && w + local_id.x < uniforms.K) { + tileQ[TILE_SIZE * local_id.y + local_id.x] = q[qOffset + local_id.y * uniforms.K + w + local_id.x]; + } + if (n + local_id.y < uniforms.N && w + local_id.x < uniforms.K) { + var idx = TILE_SIZE * local_id.y + local_id.x; + ${(()=>C&&u?` + if (n + local_id.y < past_sequence_length) { + tileK[idx] = past_key[pastKeyOffset + (n + local_id.y) * uniforms.K + w + local_id.x]; + } else if (n + local_id.y - past_sequence_length < uniforms.kv_sequence_length) { + tileK[idx] = key[kOffset + (n + local_id.y - past_sequence_length) * uniforms.K + w + local_id.x]; + }`:` + if (n + local_id.y < uniforms.kv_sequence_length) { + tileK[idx] = key[kOffset + (n + local_id.y) * uniforms.K + w + local_id.x]; + }`)()} + ${u?`if (n + local_id.y < present_sequence_length) { + present_key[presentKeyOffset + (n + local_id.y) * uniforms.K + w + local_id.x] = tileK[idx]; + }`:""} + } + workgroupBarrier(); + + for (var k: u32 = 0u; k < TILE_SIZE && w+k < uniforms.K; k++) { + value += ${de}(tileQ[TILE_SIZE * local_id.y + k] * tileK[TILE_SIZE * local_id.x + k]); + } + + workgroupBarrier(); + } + + if (global_id.y < uniforms.M && global_id.x < total_sequence_length) { + let headOffset = workgroup_id.z * uniforms.M * uniforms.N; + let outputIdx = headOffset + global_id.y * uniforms.N + global_id.x; + var sum: f32 = ${(()=>{switch(S){case 1:return"value";case 2:return"value.x + value.y";case 4:return"value.x + value.y + value.z + value.w";default:throw new Error(`Unsupported components: ${S}`)}})()}; + output[outputIdx] = ${ue.type.value} (sum * uniforms.alpha) + ${o?"attention_bias[outputIdx]":"0.0"}; + } + }`};return{name:"AttentionProbs",shaderCache:{hint:`${S};${o!==void 0};${n!==void 0};${e}`,inputDependencies:A},getRunData:()=>({outputs:P,dispatchGroup:x,programUniforms:T}),getShaderSource:D}},_m=(e,t,r,n,o,i,a=void 0,d=void 0)=>{let l=i+o.kvSequenceLength,c=o.nReps?o.nReps:1,m=o.vHiddenSize*c,u=e>1&&n,h=o.kvNumHeads?o.kvNumHeads:o.numHeads,w=u?[o.batchSize,h,l,o.headSize]:void 0,g=[o.batchSize,o.sequenceLength,m],y=12,S={x:Math.ceil(o.vHeadSize/y),y:Math.ceil(o.sequenceLength/y),z:o.batchSize*o.numHeads},$=[{type:12,data:o.sequenceLength},{type:12,data:l},{type:12,data:o.vHeadSize},{type:12,data:o.numHeads},{type:12,data:o.headSize},{type:12,data:m},{type:12,data:i},{type:12,data:o.kvSequenceLength},{type:12,data:c}],v=u&&n&&k.size(n.dims)>0,x=["type","type"];v&&x.push("type"),a&&x.push("type"),d&&x.push("type");let T=[{dims:g,dataType:t.dataType,gpuDataType:0}];u&&T.push({dims:w,dataType:t.dataType,gpuDataType:0});let C=A=>{let P=E("probs",t.dataType,t.dims),D=E("v",r.dataType,r.dims),W=[P,D];v&&W.push(E("past_value",n.dataType,n.dims));let N=a?E("seq_lens",a.dataType,a.dims):void 0;a&&W.push(N);let j=d?E("total_sequence_length_input",d.dataType,d.dims):void 0;d&&W.push(j);let Z=[M("output",t.dataType,g)];u&&Z.push(M("present_value",t.dataType,w));let te=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"head_size",type:"u32"},{name:"v_hidden_size",type:"u32"},{name:"past_sequence_length",type:"u32"},{name:"kv_sequence_length",type:"u32"},{name:"n_reps",type:"u32"}];return` + const TILE_SIZE = ${y}u; + var tileQ: array<${P.type.value}, ${y*y}>; + var tileV: array<${P.type.value}, ${y*y}>; + ${A.registerUniforms(te).declareVariables(...W,...Z)} + ${A.mainStart([y,y,1])} + let headIdx = workgroup_id.z % uniforms.num_heads; + let batchIdx = workgroup_id.z / uniforms.num_heads; + let kvHeadIdx = ${c===1?"headIdx":"headIdx / uniforms.n_reps"}; + let kv_num_heads = ${c===1?"uniforms.num_heads":"uniforms.num_heads / uniforms.n_reps"}; + let m = global_id.y; + let n = global_id.x; + let sequence_length = uniforms.M; + var total_sequence_length = uniforms.K; + ${co(N,j,!0)} + let offsetA = workgroup_id.z * uniforms.M * uniforms.K + m * uniforms.K; + let absKvHeadIdx = batchIdx * kv_num_heads + kvHeadIdx; // kvHeadIdx is relative to the batch + ${v&&u?"let pastValueOffset = absKvHeadIdx * uniforms.N * uniforms.past_sequence_length + n;":""}; + let vOffset = absKvHeadIdx * uniforms.N * uniforms.kv_sequence_length + n; + ${u?"let presentValueOffset = absKvHeadIdx * uniforms.N * uniforms.K + n;":""} + var value = ${P.type.storage}(0); + for (var w: u32 = 0u; w < uniforms.K; w += TILE_SIZE) { + if (m < uniforms.M && w + local_id.x < uniforms.K) { + tileQ[TILE_SIZE * local_id.y + local_id.x] = probs[offsetA + w + local_id.x]; + } + if (n < uniforms.N && w + local_id.y < uniforms.K) { + var idx = TILE_SIZE * local_id.y + local_id.x; + ${(()=>v&&u?` + if (w + local_id.y < past_sequence_length) { + tileV[idx] = past_value[pastValueOffset + (w + local_id.y) * uniforms.N]; + } else if (w + local_id.y - past_sequence_length < uniforms.kv_sequence_length) { + tileV[idx] = v[vOffset + (w + local_id.y - past_sequence_length) * uniforms.N]; + } + `:` + if (w + local_id.y < uniforms.kv_sequence_length) { + tileV[idx] = v[vOffset + (w + local_id.y) * uniforms.N]; + }`)()} + ${u?` + if (w + local_id.y < present_sequence_length) { + present_value[presentValueOffset + (w + local_id.y) * uniforms.N] = tileV[idx]; + }`:""} + } + workgroupBarrier(); + for (var k: u32 = 0u; k < TILE_SIZE && w+k < total_sequence_length; k++) { + value += tileQ[TILE_SIZE * local_id.y + k] * tileV[TILE_SIZE * k + local_id.x]; + } + workgroupBarrier(); + } + + // we need to transpose output from BNSH_v to BSND_v + if (m < uniforms.M && n < uniforms.N) { + let outputIdx = batchIdx * uniforms.M * uniforms.v_hidden_size + m * uniforms.v_hidden_size + + headIdx * uniforms.N + n; + output[outputIdx] = value; + } + }`};return{name:"AttentionScore",shaderCache:{hint:`${n!==void 0};${e}`,inputDependencies:x},getRunData:()=>({outputs:T,dispatchGroup:S,programUniforms:$}),getShaderSource:C}},Nt=(e,t,r,n,o,i,a,d,l,c,m=void 0,u=void 0)=>{let h=Math.min(e.outputCount,1+(a?1:0)+(d?1:0)),w=h>1?c.pastSequenceLength:0,g=w+c.kvSequenceLength,y=l&&k.size(l.dims)>0?l:void 0,S=[t,r];h>1&&a&&k.size(a.dims)>0&&S.push(a),y&&S.push(y),m&&S.push(m),u&&S.push(u);let $=e.compute(wm(h,t,r,a,y,c,w,m,u),{inputs:S,outputs:h>1?[-1,1]:[-1]})[0];e.compute(bm($,c.batchSize,c.numHeads,w,c.sequenceLength,g,m,u),{inputs:m&&u?[$,m,u]:[$],outputs:[]});let v=[$,n];h>1&&d&&k.size(d.dims)>0&&v.push(d),m&&v.push(m),u&&v.push(u),e.compute(_m(h,$,n,d,c,w,m,u),{inputs:v,outputs:h>1?[0,2]:[0]})},vm=(e,t)=>{let r=[t.batchSize,t.numHeads,t.sequenceLength,t.headSize],n=t.sequenceLength,o=t.inputHiddenSize,i=t.headSize,a=12,d={x:Math.ceil(t.headSize/a),y:Math.ceil(t.sequenceLength/a),z:t.batchSize*t.numHeads},l=[e.inputs[0],e.inputs[1],e.inputs[2]],c=[{type:12,data:n},{type:12,data:o},{type:12,data:i},{type:12,data:t.numHeads},{type:12,data:t.headSize},{type:12,data:t.hiddenSize},{type:12,data:t.hiddenSize+t.hiddenSize+t.vHiddenSize}],m=u=>{let h=M("output_q",l[0].dataType,r),w=M("output_k",l[0].dataType,r),g=M("output_v",l[0].dataType,r),y=E("input",l[0].dataType,l[0].dims),S=E("weight",l[1].dataType,l[1].dims),$=E("bias",l[2].dataType,l[2].dims),v=y.type.storage,x=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"head_size",type:"u32"},{name:"hidden_size",type:"u32"},{name:"ldb",type:"u32"}];return` + const TILE_SIZE = ${a}u; + var tileInput: array<${v}, ${a*a}>; + var tileWeightQ: array<${v}, ${a*a}>; + var tileWeightK: array<${v}, ${a*a}>; + var tileWeightV: array<${v}, ${a*a}>; + ${u.registerUniforms(x).declareVariables(y,S,$,h,w,g)} + ${u.mainStart([a,a,1])} + let batchIndex = workgroup_id.z / uniforms.num_heads; + let headNumber = workgroup_id.z % uniforms.num_heads; + let m = global_id.y; + let n = global_id.x; + + let inputOffset = batchIndex * (uniforms.M * uniforms.K) + m * uniforms.K; + let biasOffsetQ = headNumber * uniforms.head_size; + let biasOffsetK = uniforms.hidden_size + biasOffsetQ; + let biasOffsetV = uniforms.hidden_size + biasOffsetK; + + var valueQ = ${v}(0); + var valueK = ${v}(0); + var valueV = ${v}(0); + for (var w: u32 = 0u; w < uniforms.K; w += TILE_SIZE) { + if (m < uniforms.M && w + local_id.x < uniforms.K) { + tileInput[TILE_SIZE * local_id.y + local_id.x] = input[inputOffset + w + local_id.x]; + } + if (n < uniforms.N && w + local_id.y < uniforms.K) { + let offset = n + (w + local_id.y) * uniforms.ldb; + tileWeightQ[TILE_SIZE * local_id.y + local_id.x] = weight[biasOffsetQ + offset]; + tileWeightK[TILE_SIZE * local_id.y + local_id.x] = weight[biasOffsetK + offset]; + tileWeightV[TILE_SIZE * local_id.y + local_id.x] = weight[biasOffsetV + offset]; + } + workgroupBarrier(); + for (var k: u32 = 0u; k({outputs:[{dims:r,dataType:e.inputs[0].dataType,gpuDataType:0},{dims:r,dataType:e.inputs[0].dataType,gpuDataType:0},{dims:r,dataType:e.inputs[0].dataType,gpuDataType:0}],dispatchGroup:d,programUniforms:c}),getShaderSource:m},{inputs:l,outputs:[-1,-1,-1]})},As=(e,t)=>{let r=ym(e.inputs,t),[n,o,i]=vm(e,r);return Nt(e,n,o,i,e.inputs[4],void 0,void 0,void 0,e.inputs[5],r)}});var $m,xm,Sm,ks,Es=U(()=>{"use strict";Ke();J();ae();Ie();se();$m=(e,t)=>{if(!e||e.length!==5)throw new Error("BatchNormalization requires 5 inputs");let r=(n,o,i)=>{let a=o.length;if(a!==n.length)throw new Error(`${i}: num dimensions != ${a}`);o.forEach((d,l)=>{if(d!==n[l])throw new Error(`${i}: dim[${l}] do not match`)})};if(e[0].dims.length>1){let n=t.format==="NHWC"?t.spatial?e[0].dims.slice(-1):e[0].dims.slice(-1).concat(e[0].dims.slice(1,e[0].dims.length-1)):e[0].dims.slice(1,t.spatial?2:void 0);r(e[1].dims,n,"Invalid input scale"),r(e[2].dims,n,"Invalid input B"),r(e[3].dims,n,"Invalid input mean"),r(e[4].dims,n,"Invalid input var")}else r(e[1].dims,[1],"Invalid input scale"),r(e[2].dims,[1],"Invalid input B"),r(e[3].dims,[1],"Invalid input mean"),r(e[4].dims,[1],"Invalid input var")},xm=(e,t)=>{let{epsilon:r,spatial:n,format:o}=t,i=e[0].dims,a=n?we(i[i.length-1]):1,d=o==="NHWC"&&i.length>1?a:1,l=k.size(i)/a,c=n,m=c?i.length:i,u=E("x",e[0].dataType,e[0].dims,a),h=E("scale",e[1].dataType,e[1].dims,d),w=E("bias",e[2].dataType,e[2].dims,d),g=E("inputMean",e[3].dataType,e[3].dims,d),y=E("inputVar",e[4].dataType,e[4].dims,d),S=M("y",e[0].dataType,m,a),$=()=>{let x="";if(n)x=`let cOffset = ${i.length===1?"0u":o==="NHWC"?`outputIndices[${i.length-1}] / ${a}`:"outputIndices[1]"};`;else if(o==="NCHW")x=` + ${S.indicesSet("outputIndices","0","0")} + let cOffset = ${S.indicesToOffset("outputIndices")};`;else{x=`var cIndices = ${h.type.indices}(0); + cIndices[0] = outputIndices[${i.length-1}];`;for(let T=1;T` + const epsilon = ${r}; + ${x.registerUniform("outputSize","u32").declareVariables(u,h,w,g,y,S)} + ${x.mainStart()} + ${x.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + var outputIndices = ${S.offsetToIndices(`global_idx * ${a}`)}; + ${$()} + let scale = ${h.getByOffset("cOffset")}; + let bias = ${w.getByOffset("cOffset")}; + let inputMean = ${g.getByOffset("cOffset")}; + let inputVar = ${y.getByOffset("cOffset")}; + let x = ${u.getByOffset("global_idx")}; + let value = (x - inputMean) * inverseSqrt(inputVar + epsilon) * scale + bias; + ${S.setByOffset("global_idx","value")} + }`;return{name:"BatchNormalization",shaderCache:{hint:`${t.epsilon}_${t.format}_${n}_${a}`,inputDependencies:c?["rank","type","type","type","type"]:void 0},getShaderSource:v,getRunData:()=>({outputs:[{dims:e[0].dims,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(l/64)},programUniforms:c?[{type:12,data:l},...V(i)]:[{type:12,data:l}]})}},Sm=e=>ee(e),ks=(e,t)=>{let{inputs:r,outputCount:n}=e,o=Sm({...t,outputCount:n});if(_e.webgpu.validateInputContent&&$m(r,o),t.trainingMode)throw new Error("BatchNormalization trainingMode is not supported yet.");e.compute(xm(r,o))}});var Tm,Im,Ps,zs=U(()=>{"use strict";ae();se();Tm=e=>{if(e[0].dims.length!==3)throw new Error("input should have 3 dimensions");if(![320,640,1280].includes(e[0].dims[2]))throw new Error("number of channels should be 320, 640 or 1280");if(e[1].dims.length!==1)throw new Error("bias is expected to have 1 dimensions");if(e[0].dims[2]!==e[1].dims[0])throw new Error("last dimension of input and bias are not the same")},Im=e=>{let t=e[0].dims,r=e[0].dims[2],n=k.size(t)/4,o=e[0].dataType,i=E("input",o,t,4),a=E("bias",o,[r],4),d=E("residual",o,t,4),l=M("output",o,t,4);return{name:"BiasAdd",getRunData:()=>({outputs:[{dims:t,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(n/64)}}),getShaderSource:m=>` + const channels = ${r}u / 4; + ${m.declareVariables(i,a,d,l)} + + ${m.mainStart()} + ${m.guardAgainstOutOfBoundsWorkgroupSizes(n)} + let value = ${i.getByOffset("global_idx")} + + ${a.getByOffset("global_idx % channels")} + ${d.getByOffset("global_idx")}; + ${l.setByOffset("global_idx","value")} + }`}},Ps=e=>{Tm(e.inputs),e.compute(Im(e.inputs))}});var Cm,ge,Os,Ds,Bs,Ms,Rs,Us,Vs,Ws,Ns,Am,Ls,Hs,Gs,Fs,Zt,qs,Yr,js,Ks,Ys,Xs,Qs,Zs,Js,eu,tu,ru,nu,ou,iu,au,su,uu,du,lu,po,mo,cu,pu,mu,km,Em,fu,Xr=U(()=>{"use strict";J();ae();Ie();se();Cm=(e,t,r,n,o,i,a)=>{let d=Math.ceil(t/4),l="";typeof o=="string"?l=`${o}(a)`:l=o("a");let c=E("inputData",r,[d],4),m=M("outputData",n,[d],4),u=[{name:"vec_size",type:"u32"}];return a&&u.push(...a),` + ${e.registerUniforms(u).declareVariables(c,m)} + + ${i??""} + + ${e.mainStart()} + ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")} + + let a = ${c.getByOffset("global_idx")}; + ${m.setByOffset("global_idx",l)} + }`},ge=(e,t,r,n,o,i=e.dataType,a,d)=>{let l=[{type:12,data:Math.ceil(k.size(e.dims)/4)}];return a&&l.push(...a),{name:t,shaderCache:{hint:o,inputDependencies:["type"]},getShaderSource:c=>Cm(c,k.size(e.dims),e.dataType,i,r,n,d),getRunData:c=>({outputs:[{dims:e.dims,dataType:i}],dispatchGroup:{x:Math.ceil(k.size(c[0].dims)/64/4)},programUniforms:l})}},Os=e=>{e.compute(ge(e.inputs[0],"Abs","abs"))},Ds=e=>{e.compute(ge(e.inputs[0],"Acos","acos"))},Bs=e=>{e.compute(ge(e.inputs[0],"Acosh","acosh"))},Ms=e=>{e.compute(ge(e.inputs[0],"Asin","asin"))},Rs=e=>{e.compute(ge(e.inputs[0],"Asinh","asinh"))},Us=e=>{e.compute(ge(e.inputs[0],"Atan","atan"))},Vs=e=>{e.compute(ge(e.inputs[0],"Atanh","atanh"))},Ws=e=>ee(e),Ns=(e,t)=>{let r;switch(t.to){case 10:r="vec4";break;case 1:r="vec4";break;case 12:r="vec4";break;case 6:r="vec4";break;case 9:r="vec4";break;default:throw new RangeError(`not supported type (specified in attribute 'to' from 'Cast' operator): ${t.to}`)}e.compute(ge(e.inputs[0],"Cast",r,void 0,t.cacheKey,t.to))},Am=e=>{let t,r,n=e.length>=2&&e[1].data!==0,o=e.length>=3&&e[2].data!==0;switch(e[0].dataType){case 1:t=n?e[1].getFloat32Array()[0]:-34028234663852886e22,r=o?e[2].getFloat32Array()[0]:34028234663852886e22;break;case 10:t=n?e[1].getUint16Array()[0]:64511,r=o?e[2].getUint16Array()[0]:31743;break;default:throw new Error("Unsupport data type")}return ee({min:t,max:r})},Ls=(e,t)=>{let r=t||Am(e.inputs),n=Ee(e.inputs[0].dataType);e.compute(ge(e.inputs[0],"Clip",o=>`clamp(${o}, vec4<${n}>(uniforms.min), vec4<${n}>(uniforms.max))`,void 0,r.cacheKey,void 0,[{type:e.inputs[0].dataType,data:r.min},{type:e.inputs[0].dataType,data:r.max}],[{name:"min",type:n},{name:"max",type:n}]),{inputs:[0]})},Hs=e=>{e.compute(ge(e.inputs[0],"Ceil","ceil"))},Gs=e=>{e.compute(ge(e.inputs[0],"Cos","cos"))},Fs=e=>{e.compute(ge(e.inputs[0],"Cosh","cosh"))},Zt=e=>ee(e),qs=(e,t)=>{let r=Ee(e.inputs[0].dataType);e.compute(ge(e.inputs[0],"Elu",n=>`elu_vf32(${n})`,` + const elu_alpha_ = ${r}(${t.alpha}); + + fn elu_f32(a: ${r}) -> ${r} { + return select((exp(a) - 1.0) * elu_alpha_, a, a >= 0.0); + } + + fn elu_vf32(v: vec4<${r}>) -> vec4<${r}> { + return vec4(elu_f32(v.x), elu_f32(v.y), elu_f32(v.z), elu_f32(v.w)); + }`,t.cacheKey))},Yr=(e="f32")=>` +const r0: ${e} = 0.3275911; +const r1: ${e} = 0.254829592; +const r2: ${e} = -0.284496736; +const r3: ${e} = 1.421413741; +const r4: ${e} = -1.453152027; +const r5: ${e} = 1.061405429; + +fn erf_vf32(v: vec4<${e}>) -> vec4<${e}> { + let absv = abs(v); + let x = 1.0 / (1.0 + r0 * absv); + return sign(v) * (1.0 - ((((r5 * x + r4) * x + r3) * x + r2) * x + r1) * x * exp(-absv * absv)); +}`,js=e=>{let t=Ee(e.inputs[0].dataType);e.compute(ge(e.inputs[0],"Erf",r=>`erf_vf32(${r})`,Yr(t)))},Ks=e=>{e.compute(ge(e.inputs[0],"Exp","exp"))},Ys=e=>{e.compute(ge(e.inputs[0],"Floor","floor"))},Xs=e=>{let t=Ee(e.inputs[0].dataType);e.compute(ge(e.inputs[0],"Gelu",r=>`0.5 * ${r} * (1.0 + erf_vf32(${r} * 0.7071067811865475))`,Yr(t)))},Qs=(e,t)=>{let r=Ee(e.inputs[0].dataType);e.compute(ge(e.inputs[0],"LeakyRelu",n=>`select(leaky_relu_alpha_ * ${n}, ${n}, ${n} >= vec4<${r}>(0.0))`,`const leaky_relu_alpha_ = ${r}(${t.alpha});`,t.cacheKey))},Zs=e=>{e.compute(ge(e.inputs[0],"Not",t=>`!${t}`))},Js=e=>{e.compute(ge(e.inputs[0],"Neg",t=>`-${t}`))},eu=e=>{e.compute(ge(e.inputs[0],"Reciprocal",t=>`1.0/${t}`))},tu=e=>{let t=Ee(e.inputs[0].dataType);e.compute(ge(e.inputs[0],"Relu",r=>`select(vec4<${t}>(0.0), ${r}, ${r} > vec4<${t}>(0.0))`))},ru=e=>{e.compute(ge(e.inputs[0],"Sigmoid",t=>`(1.0 / (1.0 + exp(-${t})))`))},nu=e=>ee(e),ou=(e,t)=>{let r=Ee(e.inputs[0].dataType);e.compute(ge(e.inputs[0],"HardSigmoid",n=>`max(vec4<${r}>(0.0), min(vec4<${r}>(1.0), ${t.alpha} * ${n} + vec4<${r}>(${t.beta})))`,void 0,t.cacheKey))},iu=e=>{e.compute(ge(e.inputs[0],"Sin","sin"))},au=e=>{e.compute(ge(e.inputs[0],"Sinh","sinh"))},su=e=>{e.compute(ge(e.inputs[0],"Sqrt","sqrt"))},uu=e=>{e.compute(ge(e.inputs[0],"Tan","tan"))},du=e=>`sign(${e}) * (1 - exp(-2 * abs(${e}))) / (1 + exp(-2 * abs(${e})))`,lu=e=>{e.compute(ge(e.inputs[0],"Tanh",du))},po=(e="f32")=>` +const fast_gelu_a: ${e} = 0.5; +const fast_gelu_b: ${e} = 0.7978845608028654; +const fast_gelu_c: ${e} = 0.035677408136300125; + +fn tanh_v(v: vec4<${e}>) -> vec4<${e}> { + return ${du("v")}; +} +`,mo=e=>`(fast_gelu_a + fast_gelu_a * tanh_v(${e} * (fast_gelu_c * ${e} * ${e} + fast_gelu_b))) * ${e}`,cu=e=>{let t=Ee(e.inputs[0].dataType);e.compute(ge(e.inputs[0],"FastGelu",mo,po(t),void 0,e.inputs[0].dataType))},pu=(e,t)=>{let r=Ee(e.inputs[0].dataType);return e.compute(ge(e.inputs[0],"ThresholdedRelu",n=>`select(vec4<${r}>(0.0), ${n}, ${n} > thresholded_relu_alpha_)`,`const thresholded_relu_alpha_ = vec4<${r}>(${t.alpha});`,t.cacheKey)),0},mu=e=>{e.compute(ge(e.inputs[0],"Log","log"))},km=(e,t)=>` +const alpha = vec4<${e}>(${t}); +const one = ${e}(1.0); +const zero = ${e}(0.0); + +fn quick_gelu_impl(x: vec4<${e}>) -> vec4<${e}> { + let v = x *alpha; + var x1 : vec4<${e}>; + for (var i = 0; i < 4; i = i + 1) { + if (v[i] >= zero) { + x1[i] = one / (one + exp(-v[i])); + } else { + x1[i] = one - one / (one + exp(v[i])); + } + } + return x * x1; +} +`,Em=e=>`quick_gelu_impl(${e})`,fu=(e,t)=>{let r=Ee(e.inputs[0].dataType);e.compute(ge(e.inputs[0],"QuickGelu",Em,km(r,t.alpha),t.cacheKey,e.inputs[0].dataType))}});var Pm,zm,gu,yu=U(()=>{"use strict";ae();se();Xr();Pm=e=>{if(e[0].dims.length!==3)throw new Error("input should have 3 dimensions");if(![2560,5120,10240].includes(e[0].dims[2]))throw new Error("hidden state should be 2560, 5120 or 10240");if(e[1].dims.length!==1)throw new Error("bias is expected to have 1 dimensions");if(e[0].dims[2]!==e[1].dims[0])throw new Error("last dimension of input and bias are not the same")},zm=e=>{let t=e[0].dims.slice();t[2]=t[2]/2;let r=E("input",e[0].dataType,e[0].dims,4),n=E("bias",e[0].dataType,[e[0].dims[2]],4),o=M("output",e[0].dataType,t,4),i=k.size(t)/4,a=he(e[0].dataType);return{name:"BiasSplitGelu",getRunData:()=>({outputs:[{dims:t,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(i/64)}}),getShaderSource:l=>` + const M_SQRT2 = sqrt(2.0); + const halfChannels = ${e[0].dims[2]/4/2}u; + + ${l.declareVariables(r,n,o)} + + ${Yr(a)} + + ${l.mainStart()} + ${l.guardAgainstOutOfBoundsWorkgroupSizes(i)} + let biasIdx = global_idx % halfChannels; + let batchIndex = global_idx / halfChannels; + let inputOffset = biasIdx + batchIndex * halfChannels * 2; + let valueLeft = input[inputOffset] + bias[biasIdx]; + let valueRight = input[inputOffset + halfChannels] + bias[biasIdx + halfChannels]; + let geluRight = valueRight * 0.5 * (erf_vf32(valueRight / M_SQRT2) + 1); + + ${o.setByOffset("global_idx","valueLeft * geluRight")} + }`}},gu=e=>{Pm(e.inputs),e.compute(zm(e.inputs))}});var Om,Dm,st,bu,wu,_u,vu,$u,xu,Su,Tu,Iu,Cu,Au=U(()=>{"use strict";J();ae();se();Om=(e,t,r,n,o,i,a,d,l,c,m,u)=>{let h,w;typeof d=="string"?h=w=(v,x)=>`${d}((${v}),(${x}))`:typeof d=="function"?h=w=d:(h=d.scalar,w=d.vector);let g=M("outputData",m,n.length,4),y=E("aData",l,t.length,4),S=E("bData",c,r.length,4),$;if(o)if(i){let v=k.size(t)===1,x=k.size(r)===1,T=t.length>0&&t[t.length-1]%4===0,C=r.length>0&&r[r.length-1]%4===0;v||x?$=g.setByOffset("global_idx",w(v?`${y.type.value}(${y.getByOffset("0")}.x)`:y.getByOffset("global_idx"),x?`${S.type.value}(${S.getByOffset("0")}.x)`:S.getByOffset("global_idx"))):$=` + let outputIndices = ${g.offsetToIndices("global_idx * 4u")}; + let offsetA = ${y.broadcastedIndicesToOffset("outputIndices",g)}; + let offsetB = ${S.broadcastedIndicesToOffset("outputIndices",g)}; + ${g.setByOffset("global_idx",w(a||T?y.getByOffset("offsetA / 4u"):`${y.type.value}(${y.getByOffset("offsetA / 4u")}[offsetA % 4u])`,a||C?S.getByOffset("offsetB / 4u"):`${S.type.value}(${S.getByOffset("offsetB / 4u")}[offsetB % 4u])`))} + `}else $=g.setByOffset("global_idx",w(y.getByOffset("global_idx"),S.getByOffset("global_idx")));else{if(!i)throw new Error("no necessary to use scalar implementation for element-wise binary op implementation.");let v=(x,T,C="")=>{let A=`aData[indexA${T}][componentA${T}]`,P=`bData[indexB${T}][componentB${T}]`;return` + let outputIndices${T} = ${g.offsetToIndices(`global_idx * 4u + ${T}u`)}; + let offsetA${T} = ${y.broadcastedIndicesToOffset(`outputIndices${T}`,g)}; + let offsetB${T} = ${S.broadcastedIndicesToOffset(`outputIndices${T}`,g)}; + let indexA${T} = offsetA${T} / 4u; + let indexB${T} = offsetB${T} / 4u; + let componentA${T} = offsetA${T} % 4u; + let componentB${T} = offsetB${T} % 4u; + ${x}[${T}] = ${C}(${h(A,P)}); + `};m===9?$=` + var data = vec4(0); + ${v("data",0,"u32")} + ${v("data",1,"u32")} + ${v("data",2,"u32")} + ${v("data",3,"u32")} + outputData[global_idx] = dot(vec4(0x1, 0x100, 0x10000, 0x1000000), vec4(data));`:$=` + ${v("outputData[global_idx]",0)} + ${v("outputData[global_idx]",1)} + ${v("outputData[global_idx]",2)} + ${v("outputData[global_idx]",3)} + `}return` + ${e.registerUniform("vec_size","u32").declareVariables(y,S,g)} + + ${u??""} + + ${e.mainStart()} + ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")} + ${$} + }`},Dm=(e,t,r,n,o,i,a=r.dataType)=>{let d=!k.areEqual(r.dims,n.dims),l=r.dims,c=k.size(r.dims),m=!1,u=!1,h=[d];if(d){let w=rt.calcShape(r.dims,n.dims,!1);if(!w)throw new Error("Can't perform binary op on the given tensors");l=w,c=k.size(l);let g=k.size(r.dims)===1,y=k.size(n.dims)===1,S=r.dims.length>0&&r.dims[r.dims.length-1]%4===0,$=n.dims.length>0&&n.dims[n.dims.length-1]%4===0;h.push(g),h.push(y),h.push(S),h.push($);let v=1;for(let x=1;xw.toString()).join("_"),inputDependencies:["rank","rank"]},getShaderSource:w=>Om(w,r.dims,n.dims,l,m,d,u,o,r.dataType,n.dataType,a,i),getRunData:()=>({outputs:[{dims:l,dataType:a}],dispatchGroup:{x:Math.ceil(c/64/4)},programUniforms:[{type:12,data:Math.ceil(k.size(l)/4)},...V(r.dims,n.dims,l)]})}},st=(e,t,r,n,o,i)=>{e.compute(Dm(t,o??"",e.inputs[0],e.inputs[1],r,n,i))},bu=e=>{st(e,"Add",(t,r)=>`${t}+${r}`)},wu=e=>{st(e,"Div",(t,r)=>`${t}/${r}`)},_u=e=>{st(e,"Equal",{scalar:(t,r)=>`u32(${t}==${r})`,vector:(t,r)=>`vec4(${t}==${r})`},void 0,void 0,9)},vu=e=>{st(e,"Mul",(t,r)=>`${t}*${r}`)},$u=e=>{let t=E("input",e.inputs[0].dataType,e.inputs[0].dims).type.value;st(e,"Pow",{scalar:(n,o)=>`pow_custom(${n},${o})`,vector:(n,o)=>`pow_vector_custom(${n},${o})`},` + fn pow_custom(a : ${t}, b : ${t}) -> ${t} { + if (b == ${t}(0.0)) { + return ${t}(1.0); + } else if (a < ${t}(0.0) && f32(b) != floor(f32(b))) { + return ${t}(pow(f32(a), f32(b))); // NaN + } + return select(sign(a), ${t}(1.0), round(f32(abs(b) % ${t}(2.0))) != 1.0) * ${t}(${t==="i32"?"round":""}(pow(f32(abs(a)), f32(b)))); + } + fn pow_vector_custom(a : vec4<${t}>, b : vec4<${t}>) -> vec4<${t}> { + // TODO: implement vectorized pow + return vec4<${t}>(pow_custom(a.x, b.x), pow_custom(a.y, b.y), pow_custom(a.z, b.z), pow_custom(a.w, b.w)); + } + `)},xu=e=>{st(e,"Sub",(t,r)=>`${t}-${r}`)},Su=e=>{st(e,"Greater",{scalar:(t,r)=>`u32(${t}>${r})`,vector:(t,r)=>`vec4(${t}>${r})`},void 0,void 0,9)},Tu=e=>{st(e,"Less",{scalar:(t,r)=>`u32(${t}<${r})`,vector:(t,r)=>`vec4(${t}<${r})`},void 0,void 0,9)},Iu=e=>{st(e,"GreaterOrEqual",{scalar:(t,r)=>`u32(${t}>=${r})`,vector:(t,r)=>`vec4(${t}>=${r})`},void 0,void 0,9)},Cu=e=>{st(e,"LessOrEqual",{scalar:(t,r)=>`u32(${t}<=${r})`,vector:(t,r)=>`vec4(${t}<=${r})`},void 0,void 0,9)}});var Mm,Rm,Um,Vm,ku,Eu,Pu=U(()=>{"use strict";J();ae();Ie();se();Mm=(e,t)=>{if(!e||e.length<1)throw new Error("too few inputs");let r=0,n=e[r],o=n.dataType,i=n.dims.length;e.forEach((a,d)=>{if(d!==r){if(a.dataType!==o)throw new Error("input tensors should be one type");if(a.dims.length!==i)throw new Error("input tensors should have the same shape");a.dims.forEach((l,c)=>{if(c!==t&&l!==n.dims[c])throw new Error("non concat dimensions must match")})}})},Rm=(e,t)=>` + fn calculateInputIndex(index: u32) -> u32 { + let sizeInConcatAxis = array(${t}); + for (var i: u32 = 0u; i < ${e}; i += 1u ) { + if (index < sizeInConcatAxis[i]) { + return i; + } + } + return ${e}u; + }`,Um=(e,t)=>{let r=e.length,n=[];for(let o=0;o{let o=k.size(r),i=new Array(e.length),a=new Array(e.length),d=0,l=[],c=[],m=[{type:12,data:o}];for(let y=0;y`uniforms.sizeInConcatAxis${y}`).join(","),g=y=>` + + ${(()=>{y.registerUniform("outputSize","u32");for(let S=0;S(${w}); + ${h} -= sizeInConcatAxis[inputIndex - 1u]; + } + + ${Um(a,u)} + }`;return{name:"Concat",shaderCache:{hint:`${t}`,inputDependencies:l},getRunData:()=>({outputs:[{dims:r,dataType:n}],dispatchGroup:{x:Math.ceil(o/64)},programUniforms:m}),getShaderSource:g}},ku=(e,t)=>{let r=e.inputs,n=r[0].dims,o=k.normalizeAxis(t.axis,n.length);Mm(r,o);let i=n.slice();i[o]=r.reduce((d,l)=>d+(l.dims.length>o?l.dims[o]:0),0);let a=r.filter(d=>k.size(d.dims)>0);e.compute(Vm(a,o,i,r[0].dataType),{inputs:a})},Eu=e=>ee({axis:e.axis})});var He,Ge,Fe,Qr,ct=U(()=>{"use strict";J();ae();He=(e,t,r="f32")=>{switch(e.activation){case"Relu":return`value = max(value, ${t}(0.0));`;case"Sigmoid":return`value = (${t}(1.0) / (${t}(1.0) + exp(-value)));`;case"Clip":return`value = clamp(value, ${t}(${r}(uniforms.clip_min)), ${t}(${r}(uniforms.clip_max)));`;case"HardSigmoid":return`value = max(${t}(0.0), min(${t}(1.0), ${r}(uniforms.alpha) * value + ${r}(uniforms.beta)));`;case"LeakyRelu":return`value = select(${r}(uniforms.alpha) * value, value, value >= ${t}(0.0));`;case"Tanh":return`let e2x = exp(-2.0 * abs(value)); + value = sign(value) * (1.0 - e2x) / (1.0 + e2x); + `;case"":return"";default:throw new Error(`Unsupported activation ${e.activation}`)}},Ge=(e,t)=>{e.activation==="Clip"?t.push({type:1,data:e.clipMax},{type:1,data:e.clipMin}):e.activation==="HardSigmoid"?t.push({type:1,data:e.alpha},{type:1,data:e.beta}):e.activation==="LeakyRelu"&&t.push({type:1,data:e.alpha})},Fe=(e,t)=>{e.activation==="Clip"?t.push({name:"clip_max",type:"f32"},{name:"clip_min",type:"f32"}):e.activation==="HardSigmoid"?t.push({name:"alpha",type:"f32"},{name:"beta",type:"f32"}):e.activation==="LeakyRelu"&&t.push({name:"alpha",type:"f32"})},Qr=e=>{let t=e?.activation||"";if(t==="HardSigmoid"){let[r,n]=e?.activation_params||[.2,.5];return{activation:t,alpha:r,beta:n}}else if(t==="Clip"){let[r,n]=e?.activation_params||[Qa,Za];return{activation:t,clipMax:n,clipMin:r}}else if(t==="LeakyRelu"){let[r]=e?.activation_params||[.01];return{activation:t,alpha:r}}return{activation:t}}});var Oe,Zr,Jt=U(()=>{"use strict";Oe=(e,t)=>{switch(e){case 1:return t;case 2:return`vec2<${t}>`;case 3:return`vec3<${t}>`;case 4:return`vec4<${t}>`;default:throw new Error(`${e}-component is not supported.`)}},Zr=e=>` + ${e?"value = value + getBiasByOutputCoords(coords);":""} + `});var Jr,fo=U(()=>{"use strict";Jr=e=>` +fn getIndexFromCoords4D(coords : vec4, shape : vec4) -> i32 { + return dot(coords, vec4( + shape.y * shape.z * shape.w, shape.z * shape.w, shape.w, 1)); +} +fn getOutputIndexFromCoords(coords : vec4) -> i32 { + return dot(coords, vec4( + i32(${e}.x), i32(${e}.y), i32(${e}.z), 1)); +} +`});var Wm,Nm,er,zu,Lm,tr,Hm,en,rr=U(()=>{"use strict";J();ae();se();ct();Jt();Wm=(e,t)=>e?` + mm_Asub[inputRow][inputCol] = mm_readA(batch, + kStart + inputRow, + globalRowStart / innerElementSize + inputCol${t?", batchIndices":""}); + `:` + mm_Asub[inputRow][inputCol] = mm_readA(batch, + globalRow + innerRow, + kStart / innerElementSize + inputCol${t?", batchIndices":""}); + `,Nm=(e,t)=>e?` + let ACached0 = mm_Asub[k * innerElementSize][localRow]; + let ACached1 = mm_Asub[k * innerElementSize + 1][localRow]; + let ACached2 = mm_Asub[k * innerElementSize + 2][localRow]; + ${t===3?"":"let ACached3 = mm_Asub[k * innerElementSize + 3][localRow];"} + for (var i = 0; i < rowPerThread; i = i + 1) { + acc[i] = BCached0 * ACached0[i] + acc[i]; + acc[i] = BCached1 * ACached1[i] + acc[i]; + acc[i] = BCached2 * ACached2[i] + acc[i]; + ${t===3?"":"acc[i] = BCached3 * ACached3[i] + acc[i];"} + }`:` + for (var i = 0; i < rowPerThread; i = i + 1) { + let ACached = mm_Asub[tileRow + i][k]; + acc[i] = BCached0 * ACached.x + acc[i]; + acc[i] = BCached1 * ACached.y + acc[i]; + acc[i] = BCached2 * ACached.z + acc[i]; + ${t===3?"":"acc[i] = BCached3 * ACached.w + acc[i];"} + }`,er=(e,t,r="f32",n,o=!1,i=32,a=!1,d=32)=>{let l=t[1]*e[1],c=t[0]*e[0],m=o?l:i,u=o?i:l,h=m/t[0],w=i/t[1];if(!((o&&h===4&&e[1]===4||!o&&(h===3||h===4))&&m%t[0]===0&&i%t[1]===0&&e[0]===4))throw new Error(`If transposeA ${o} is true, innerElementSize ${h} and workPerThread[1] ${e[1]} must be 4. + Otherwise, innerElementSize ${h} must be 3 or 4. + tileAWidth ${m} must be divisible by workgroupSize[0]${t[0]}. tileInner ${i} must be divisible by workgroupSize[1] ${t[1]}. colPerThread ${e[0]} must be 4.`);return` +var mm_Asub: array, ${m/h}>, ${u}>; +var mm_Bsub: array, ${c/e[0]}>, ${i}>; + +const rowPerThread = ${e[1]}; +const colPerThread = ${e[0]}; +const innerElementSize = ${h}; +const tileInner = ${i}; + +@compute @workgroup_size(${t[0]}, ${t[1]}, ${t[2]}) +fn main(@builtin(local_invocation_id) localId : vec3, + @builtin(global_invocation_id) globalId : vec3, + @builtin(workgroup_id) workgroupId : vec3) { + let localRow = i32(localId.y); + let tileRow = localRow * rowPerThread; + let tileCol = i32(localId.x); + + let globalRow =i32(globalId.y) * rowPerThread; + let globalCol = i32(globalId.x); + let batch = ${a?"0":"i32(globalId.z)"}; + ${n?`let batchIndices = ${n.offsetToIndices("u32(batch)")};`:""} + let globalRowStart = i32(workgroupId.y) * ${l}; + + let num_tiles = ${a?`${Math.ceil(d/i)}`:"(uniforms.dim_inner - 1) / tileInner + 1"}; + var kStart = ${a?`i32(globalId.z) * ${d}`:"0"}; + + var acc: array, rowPerThread>; + + // Loop over shared dimension. + let tileRowB = localRow * ${w}; + for (var t = 0; t < num_tiles; t = t + 1) { + // Load one tile of A into local memory. + for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { + let inputRow = tileRow + innerRow; + let inputCol = tileCol; + ${Wm(o,n)} + } + + // Load one tile of B into local memory. + for (var innerRow = 0; innerRow < ${w}; innerRow = innerRow + 1) { + let inputRow = tileRowB + innerRow; + let inputCol = tileCol; + mm_Bsub[inputRow][inputCol] = mm_readB(batch, kStart + inputRow, globalCol${n?", batchIndices":""}); + } + kStart = kStart + tileInner; + workgroupBarrier(); + + // Compute acc values for a single thread. + for (var k = 0; k < tileInner / innerElementSize; k = k + 1) { + let BCached0 = mm_Bsub[k * innerElementSize][tileCol]; + let BCached1 = mm_Bsub[k * innerElementSize + 1][tileCol]; + let BCached2 = mm_Bsub[k * innerElementSize + 2][tileCol]; + ${h===3?"":"let BCached3 = mm_Bsub[k * innerElementSize + 3][tileCol];"} + + ${Nm(o,h)} + } + + workgroupBarrier(); + } + + for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { + mm_write(batch, globalRow + innerRow, globalCol, acc[innerRow]); + } +}`},zu=(e,t)=>e?` + mm_Asub[inputRow][inputCol] = mm_readA(batch, + kStart + inputRow, + globalRowStart + inputCol${t?", batchIndices":""}); + `:` + mm_Asub[inputRow][inputCol] = mm_readA(batch, + globalRowStart + inputRow, + kStart + inputCol${t?", batchIndices":""}); + `,Lm=e=>e?"let ACached = mm_Asub[k][tileRow + innerRow];":"let ACached = mm_Asub[tileRow + innerRow][k];",tr=(e,t,r="f32",n,o=!1,i=32,a=!1,d=32,l=!1)=>{let c=e[1]*t[1],m=e[0]*t[0],u=o?c:i,h=o?i:c;if(!(h%t[1]===0&&u%t[0]===0&&i%t[1]===0))throw new Error(`tileAHight ${h} must be divisible by workgroupSize[1]${t[1]}, tileAWidth ${u} must be divisible by workgroupSize[0]${t[0]}, tileInner ${i} must be divisible by workgroupSize[1]${t[1]}`);let w=h/t[1],g=u/t[0],y=i/t[1],S=l?` + let localRow = i32(localId.y); + let localCol = i32(localId.x); + let globalRowStart = i32(workgroupId.y) * ${c}; + let globalColStart = i32(workgroupId.x) * ${m}; + + // Loop over shared dimension. + for (var t = 0; t < num_tiles; t = t + 1) { + // Load one tile of A into local memory. + for (var inputRow = localRow; inputRow < ${h}; inputRow = inputRow + ${t[1]}) { + for (var inputCol = localCol; inputCol < ${u}; inputCol = inputCol + ${t[0]}) { + ${zu(o,n)} + } + } + // Load one tile of B into local memory. + for (var inputRow = localRow; inputRow < ${i}; inputRow = inputRow + ${t[1]}) { + for (var inputCol = localCol; inputCol < ${m}; inputCol = inputCol + ${t[0]}) { + mm_Bsub[inputRow][inputCol] = mm_readB(batch, + kStart + inputRow, + globalColStart + inputCol${n?", batchIndices":""}); + } + } + kStart = kStart + tileInner; + workgroupBarrier(); + + // Compute acc values for a single thread. + var BCached : array<${r}, colPerThread>; + for (var k = 0; k < tileInner; k = k + 1) { + for (var inner = 0; inner < colPerThread; inner = inner + 1) { + BCached[inner] = mm_Bsub[k][localCol + inner * ${t[0]}]; + } + for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { + let ACached = ${o?`mm_Asub[k][localRow + innerRow * ${t[1]}];`:`mm_Asub[localRow + innerRow * ${t[1]}][k];`} + for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) { + acc[innerRow][innerCol] = acc[innerRow][innerCol] + + ACached * BCached[innerCol]; + } + } + } + workgroupBarrier(); + } + for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { + let gRow = globalRowStart + localRow + innerRow * ${t[1]}; + for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) { + let gCol = globalColStart + localCol + innerCol * ${t[0]}; + mm_write(batch, gRow, gCol, acc[innerRow][innerCol]); + } + } + `:` +let tileRow = i32(localId.y) * rowPerThread; +let tileCol = i32(localId.x) * colPerThread; + +let globalRow = i32(globalId.y) * rowPerThread; +let globalCol = i32(globalId.x) * colPerThread; +let globalRowStart = i32(workgroupId.y) * ${c}; + +let tileRowA = i32(localId.y) * ${w}; +let tileColA = i32(localId.x) * ${g}; +let tileRowB = i32(localId.y) * ${y}; +// Loop over shared dimension. +for (var t = 0; t < num_tiles; t = t + 1) { + // Load one tile of A into local memory. + for (var innerRow = 0; innerRow < ${w}; innerRow = innerRow + 1) { + for (var innerCol = 0; innerCol < ${g}; innerCol = innerCol + 1) { + let inputRow = tileRowA + innerRow; + let inputCol = tileColA + innerCol; + ${zu(o,n)} + } + } + + // Load one tile of B into local memory. + for (var innerRow = 0; innerRow < ${y}; innerRow = innerRow + 1) { + for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) { + let inputRow = tileRowB + innerRow; + let inputCol = tileCol + innerCol; + mm_Bsub[inputRow][inputCol] = mm_readB(batch, + kStart + inputRow, + globalCol + innerCol${n?", batchIndices":""}); + } + } + kStart = kStart + tileInner; + workgroupBarrier(); + + // Compute acc values for a single thread. + var BCached : array<${r}, colPerThread>; + for (var k = 0; k < tileInner; k = k + 1) { + for (var inner = 0; inner < colPerThread; inner = inner + 1) { + BCached[inner] = mm_Bsub[k][tileCol + inner]; + } + + for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { + ${Lm(o)} + for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) { + acc[innerRow][innerCol] = acc[innerRow][innerCol] + ACached * BCached[innerCol]; + } + } + } + + workgroupBarrier(); +} + +for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { + for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) { + mm_write(batch, globalRow + innerRow, globalCol + innerCol, + acc[innerRow][innerCol]); + } +} +`;return` + var mm_Asub : array, ${h}>; + var mm_Bsub : array, ${i}>; + const rowPerThread = ${e[1]}; + const colPerThread = ${e[0]}; + const tileInner = ${i}; + +@compute @workgroup_size(${t[0]}, ${t[1]}, ${t[2]}) +fn main(@builtin(local_invocation_id) localId : vec3, + @builtin(global_invocation_id) globalId : vec3, + @builtin(workgroup_id) workgroupId : vec3) { + let batch = ${a?"0":"i32(globalId.z)"}; + ${n?`let batchIndices = ${n.offsetToIndices("u32(batch)")};`:""} + let num_tiles = ${a?`${Math.ceil(d/i)}`:"(uniforms.dim_inner - 1) / tileInner + 1"}; + var kStart = ${a?`i32(globalId.z) * ${d}`:"0"}; + + var acc : array, rowPerThread>; + ${S} + } +`},Hm=(e,t,r,n,o,i=!1)=>{let[a,d,l]=o,[c,m,u,h]=n,w=Wt(a,l),g=Wt(d,l),y=he(n[0].type.tensor),S=()=>{let x=m.rank,T=c.rank,C=`var aIndices: ${m.type.indices};`;for(let A=x-2-1,P=T-1;A>=0;A--,P--)C+=` +aIndices[${A}] = ${T>1?`batchIndices[${P}]`:"batchIndices"};`;return w.forEach(A=>{C+=` +aIndices[${A}] = 0;`}),C+=` +aIndices[${x-2}] = u32(row); + aIndices[${x-1}] = u32(colIn);`,C},$=()=>{let x=u.rank,T=c.rank,C=`var bIndices: ${u.type.indices};`;for(let A=x-2-1,P=T-1;A>=0;A--,P--)C+=` +bIndices[${A}] = ${T>1?`batchIndices[${P}]`:"batchIndices"};`;return g.forEach(A=>{C+=` +bIndices[${A}] = 0;`}),C+=` +bIndices[${x-2}] = u32(row); + bIndices[${x-1}] = u32(colIn);`,C};return` + fn mm_readA(batch: i32, row: i32, colIn: i32, batchIndices: ${c.type.indices}) -> ${Oe(e,y)} { + var value = ${Oe(e,y)}(0.0); + let col = colIn * ${e}; + if(row < uniforms.dim_a_outer && col < uniforms.dim_inner) + { + ${S()} + value = ${m.getByIndices("aIndices")}; + } + return value; + } + + fn mm_readB(batch: i32, row: i32, colIn: i32, batchIndices: ${c.type.indices}) -> ${Oe(e,y)} { + var value = ${Oe(e,y)}(0.0); + let col = colIn * ${e}; + if(row < uniforms.dim_inner && col < uniforms.dim_b_outer) + { + ${$()} + value = ${u.getByIndices("bIndices")}; + } + return value; + } + + fn mm_write(batch: i32, row: i32, colIn: i32, valueIn: ${Oe(e,y)}) { + let col = colIn * ${e}; + if (row < uniforms.dim_a_outer && col < uniforms.dim_b_outer) { + var value = valueIn; + let coords = vec3(batch, row, colIn); + ${t?`value = value + ${i?"bias[colIn]":`${Oe(e,y)}(bias[row])`};`:""} + ${r} + ${h.setByIndices("vec3(coords)","value")} + } + } + `},en=(e,t,r,n,o=!1,i)=>{let a=e[0].dims,d=e[1].dims,l=a.slice(0,-2),c=d.slice(0,-2),m=n?n.slice(0,-2):r.slice(0,-2),u=k.size(m),h=a[a.length-2],w=a[a.length-1],g=d[d.length-1],y=w%4===0&&g%4===0,S=h<=8?[4,1,1]:[4,4,1],$=[8,8,1],v=[Math.ceil(g/$[0]/S[0]),Math.ceil(h/$[1]/S[1]),Math.ceil(u/$[2]/S[2])],x=y?4:1,T=[...l,h,w/x],C=T.length,A=[...c,w,g/x],P=A.length,D=[u,h,g/x],W=[{type:6,data:h},{type:6,data:g},{type:6,data:w}];Ge(t,W),W.push(...V(m,T,A));let N=["rank","rank"],j=e.length>2;j&&(W.push(...V(e[2].dims)),N.push("rank")),W.push(...V(D));let Y=Z=>{let te=m.length,ue=Fr("batchDims",e[0].dataType,te,1),K=he(e[0].dataType),de=E("a",e[0].dataType,C,x),ce=E("b",e[1].dataType,P,x),q=M("result",e[0].dataType,D.length,x),le=[de,ce];if(j){let G=o?x:1;le.push(E("bias",e[2].dataType,e[2].dims.length,G))}let re=[{name:"dim_a_outer",type:"i32"},{name:"dim_b_outer",type:"i32"},{name:"dim_inner",type:"i32"}];Fe(t,re);let ne=he(q.type.tensor),oe=He(t,q.type.value,ne),R=Hm(x,j,oe,[ue,de,ce,q],[l,c,m],o);return` + ${Z.registerUniforms(re).registerInternalVariables(ue).declareVariables(...le,q)} + ${R} + ${y?er(S,$,K,ue):tr(S,$,K,ue)} + `};return{name:"MatMul",shaderCache:{hint:`${S};${t.activation};${y};${o}`,inputDependencies:N},getRunData:()=>({outputs:[{dims:i?i(r):r,dataType:e[0].dataType}],dispatchGroup:{x:v[0],y:v[1],z:v[2]},programUniforms:W}),getShaderSource:Y}}});var Gm,Ou,Du=U(()=>{"use strict";J();Xe();se();ct();Jt();fo();rr();Gm=(e,t,r,n,o=!1,i,a=4,d=4,l=4,c="f32")=>{let m=N=>{switch(N){case 1:return"resData = x[xIndex];";case 3:return`resData = vec3<${c}>(x[xIndex], x[xIndex + 1], x[xIndex + 2]);`;case 4:return"resData = x[xIndex / 4];";default:throw new Error(`innerElementSize ${N} is not supported.`)}},u=N=>{switch(N){case 1:return"return w[row * i32(uniforms.w_shape[3]) + colIn];";case 4:return"return w[row * i32(uniforms.w_shape[3]) / 4 + colIn];";default:throw new Error(`innerElementSize ${N} is not supported.`)}},h=e?` + let coord = vec4(batch, xRow, xCol, xCh); + `:` + let coord = vec4(batch, xCh, xRow, xCol); + `,w=e?` + let coords = vec4( + batch, + row / outWidth, + row % outWidth, + col); + `:` + let coords = vec4( + batch, + row, + col / outWidth, + col % outWidth); + `,g=e?"i32(uniforms.x_shape[1])":"i32(uniforms.x_shape[2])",y=e?"i32(uniforms.x_shape[2])":"i32(uniforms.x_shape[3])",S=e?"row":"col",$=e?"col":"row",v=` + let inChannels = i32(uniforms.w_shape[2]); + let outWidth = ${e?"i32(uniforms.result_shape[2])":"i32(uniforms.result_shape[3])"}; + let outRow = ${S} / outWidth; + let outCol = ${S} % outWidth; + + let WRow = ${$} / (i32(uniforms.w_shape[1]) * inChannels); + let WCol = ${$} / inChannels % i32(uniforms.w_shape[1]); + let xRow = outRow * uniforms.stride[0] + uniforms.dilation[0] * WRow - uniforms.pad[0]; + let xCol = outCol * uniforms.stride[1] + uniforms.dilation[1] * WCol - uniforms.pad[1]; + let xCh = ${$} % inChannels; + var resData = ${Oe(a,c)}(0.0); + // The bounds checking is always needed since we use it to pad zero for + // the 'same' padding type. + if (xRow >= 0 && xRow < ${g} && xCol >= 0 && xCol < ${y}) { + ${h} + let xIndex = getIndexFromCoords4D(coord, vec4(uniforms.x_shape)); + ${m(a)} + } + return resData;`,x=e?t&&n?` + let col = colIn * ${a}; + ${v}`:` + let col = colIn * ${a}; + if (row < uniforms.dim_a_outer && col < uniforms.dim_inner) { + ${v} + } + return ${Oe(a,c)}(0.0);`:n&&r?` + let col = colIn * ${a}; + ${v}`:` + let col = colIn * ${a}; + if (row < uniforms.dim_inner && col < uniforms.dim_b_outer) { + ${v} + } + return ${Oe(a,c)}(0.0);`,T=`${u(d)}`,C=Oe(l,c),A=e?Oe(a,c):Oe(d,c),P=e?Oe(d,c):Oe(a,c),D=He(i,C,c);return` + fn mm_readA(batch: i32, row : i32, colIn : i32) -> ${A} { + ${e?x:T} + } + + fn mm_readB(batch: i32, row : i32, colIn : i32) -> ${P} { + ${e?T:x} + } + + fn mm_write(batch: i32, row : i32, colIn : i32, valueIn : ${C}) { + let col = colIn * ${l}; + if (row < uniforms.dim_a_outer && col < uniforms.dim_b_outer) + { + var value = valueIn; + let outWidth = ${e?"i32(uniforms.result_shape[2])":"i32(uniforms.result_shape[3])"}; + ${w} + ${Zr(o)} + ${D} + setOutputAtCoords(coords[0], coords[1], coords[2], coords[3], value); + } + }`},Ou=(e,t,r,n,o,i,a,d,l)=>{let c=t.format==="NHWC",m=c?e[0].dims[3]:e[0].dims[1],u=r[0],h=c?r[2]:r[3],w=c?r[1]:r[2],g=c?r[3]:r[1],y=c&&(m%4===0||m%3===0)&&g%4===0,S=c?g:h*w,$=c?h*w:g,v=[8,8,1],x=n<=8?[4,1,1]:[4,4,1],T=[Math.ceil(S/v[0]/x[0]),Math.ceil($/v[1]/x[1]),Math.ceil(u/v[2]/x[2])];pe("verbose",()=>`[conv2d_mm_webgpu] dispatch = ${T}`);let C=y?c&&m%4!==0?3:4:1,A=v[1]*x[1],P=v[0]*x[0],D=Math.max(v[0]*C,v[1]),W=n%A===0,N=o%P===0,j=i%D===0,Y=y?[C,4,4]:[1,1,1],Z=[{type:6,data:n},{type:6,data:o},{type:6,data:i},{type:6,data:[t.pads[0],t.pads[1]]},{type:6,data:t.strides},{type:6,data:t.dilations}];Ge(t,Z),Z.push(...V(e[0].dims,e[1].dims));let te=["rank","rank"];a&&(Z.push(...V(e[2].dims)),te.push("rank")),Z.push(...V(r));let ue=K=>{let de=[{name:"dim_a_outer",type:"i32"},{name:"dim_b_outer",type:"i32"},{name:"dim_inner",type:"i32"},{name:"pad",type:"i32",length:2},{name:"stride",type:"i32",length:2},{name:"dilation",type:"i32",length:2}];Fe(t,de);let ce=y?4:1,q=he(e[0].dataType),le=` + fn setOutputAtIndex(flatIndex : i32, value : ${y?`vec4<${q}>`:q}) { + result[flatIndex] = ${y?`vec4<${q}>`:q}(value); + } + fn setOutputAtCoords(d0 : i32, d1 : i32, d2 : i32, d3 : i32, value : ${y?`vec4<${q}>`:q}) { + let flatIndex = getOutputIndexFromCoords(vec4(d0, d1, d2, d3)); + setOutputAtIndex(flatIndex ${y?"/ 4":""}, value); + }`,re=E("x",e[0].dataType,e[0].dims.length,C===3?1:C),ne=E("w",e[1].dataType,e[1].dims.length,ce),oe=[re,ne],R=M("result",e[0].dataType,r.length,ce);if(a){let G=E("bias",e[2].dataType,e[2].dims.length,ce);oe.push(G),le+=` + fn getBiasByOutputCoords(coords : vec4) -> ${y?`vec4<${q}>`:q} { + return bias[coords.${c?"w":"y"}${y?"/ 4":""}]; + }`}return` + ${Jr("uniforms.result_strides")} + //struct Uniforms { xShape : vec4, wShape : vec4, outShape : vec4, + // outShapeStrides: vec3, filterDims : vec2, pad : vec2, stride : vec2, + // dilation : vec2, dimAOuter : i32, dimBOuter : i32, dimInner : i32 }; + ${K.registerUniforms(de).declareVariables(...oe,R)} + ${le} + ${Gm(c,W,N,j,a,t,Y[0],Y[1],Y[2],q)} + ${y?er(x,v,q,void 0,!c,D):tr(x,v,q,void 0,!c,D,!1,void 0,d)}`};return{name:"Conv2DMatMul",shaderCache:{hint:`${t.cacheKey};${C};${y};${W};${N};${j};${A};${P};${D}`,inputDependencies:te},getRunData:()=>({outputs:[{dims:l?l(r):r,dataType:e[0].dataType}],dispatchGroup:{x:T[0],y:T[1],z:T[2]},programUniforms:Z}),getShaderSource:ue}}});var Fm,Bu,tn,qm,Mu,jm,Ru,Uu,Vu=U(()=>{"use strict";J();Xe();ae();se();ct();Jt();Fm=e=>{let t=1;for(let r=0;rtypeof e=="number"?[e,e,e]:e,tn=(e,t)=>t<=1?e:e+(e-1)*(t-1),qm=(e,t,r,n=1)=>{let o=tn(t,n);return Math.floor((e[0]*(r-1)-r+o)/2)},Mu=(e,t,r,n,o)=>{o==null&&(o=qm(e,t[0],n[0]));let i=[0,0,0,r];for(let a=0;a<3;a++)e[a]+2*o>=t[a]&&(i[a]=Math.trunc((e[a]-t[a]+2*o)/n[a]+1));return i},jm=(e,t,r,n,o,i,a,d,l,c)=>{let m,u,h,w;if(e==="VALID"&&(e=0),typeof e=="number"){m={top:e,bottom:e,left:e,right:e,front:e,back:e};let g=Mu([t,r,n,1],[d,l,c],1,[o,i,a],e);u=g[0],h=g[1],w=g[2]}else if(Array.isArray(e)){if(!e.every((y,S,$)=>y===$[0]))throw Error(`Unsupported padding parameter: ${e}`);m={top:e[0],bottom:e[1],left:e[2],right:e[3],front:e[4],back:e[5]};let g=Mu([t,r,n,1],[d,l,c],1,[o,i,a],e[0]);u=g[0],h=g[1],w=g[2]}else if(e==="SAME_UPPER"){u=Math.ceil(t/o),h=Math.ceil(r/i),w=Math.ceil(n/a);let g=(u-1)*o+d-t,y=(h-1)*i+l-r,S=(w-1)*a+c-n,$=Math.floor(g/2),v=g-$,x=Math.floor(y/2),T=y-x,C=Math.floor(S/2),A=S-C;m={top:x,bottom:T,left:C,right:A,front:$,back:v}}else throw Error(`Unknown padding parameter: ${e}`);return{padInfo:m,outDepth:u,outHeight:h,outWidth:w}},Ru=(e,t,r,n,o,i=!1,a="channelsLast")=>{let d,l,c,m,u;if(a==="channelsLast")[d,l,c,m,u]=e;else if(a==="channelsFirst")[d,u,l,c,m]=e;else throw new Error(`Unknown dataFormat ${a}`);let[h,,w,g,y]=t,[S,$,v]=Bu(r),[x,T,C]=Bu(n),A=tn(w,x),P=tn(g,T),D=tn(y,C),{padInfo:W,outDepth:N,outHeight:j,outWidth:Y}=jm(o,l,c,m,S,$,v,A,P,D),Z=i?h*u:h,te=[0,0,0,0,0];return a==="channelsFirst"?te=[d,Z,N,j,Y]:a==="channelsLast"&&(te=[d,N,j,Y,Z]),{batchSize:d,dataFormat:a,inDepth:l,inHeight:c,inWidth:m,inChannels:u,outDepth:N,outHeight:j,outWidth:Y,outChannels:Z,padInfo:W,strideDepth:S,strideHeight:$,strideWidth:v,filterDepth:w,filterHeight:g,filterWidth:y,effectiveFilterDepth:A,effectiveFilterHeight:P,effectiveFilterWidth:D,dilationDepth:x,dilationHeight:T,dilationWidth:C,inShape:e,outShape:te,filterShape:t}},Uu=(e,t,r,n,o,i)=>{let a=i==="channelsLast",d=a?e[0].dims[3]:e[0].dims[1],l=!1,c=[64,1,1],m={x:r.map((v,x)=>x)},u=[Math.ceil(Fm(m.x.map(v=>r[v]))/c[0]),1,1];pe("verbose",()=>`[conv3d_naive_webgpu] dispatch = ${u}`);let h=l?a&&d%4!==0?3:4:1,w=k.size(r),g=[{type:12,data:w},{type:12,data:n},{type:12,data:o},{type:12,data:t.strides},{type:12,data:t.dilations}];Ge(t,g),g.push(...V(e[0].dims,e[1].dims));let y=["rank","rank"],S=e.length===3;S&&(g.push(...V(e[2].dims)),y.push("rank")),g.push(...V(r));let $=v=>{let x=[{name:"output_size",type:"u32"},{name:"filter_dims",type:"u32",length:n.length},{name:"pads",type:"u32",length:o.length},{name:"strides",type:"u32",length:t.strides.length},{name:"dilations",type:"u32",length:t.dilations.length}];Fe(t,x);let T=l?4:1,C=he(e[0].dataType),A=E("x",e[0].dataType,e[0].dims.length,h===3?1:h),P=E("W",e[1].dataType,e[1].dims.length,T),D=[A,P],W=M("result",e[0].dataType,r.length,T),N="";if(S){let Z=E("bias",e[2].dataType,e[2].dims.length,T);D.push(Z),N+=` + fn getBiasByOutputCoords(coords : array) -> ${l?`vec4<${C}>`:C} { + return bias[${a?F("coords",4,5):F("coords",1,5)}${l?"/ 4":""}]; + }`}let j=Oe(h,C),Y=He(t,j,C);return` + ${N} + fn getX(d0 : u32, d1 : u32, d2 : u32, d3 : u32, d4 : u32) -> f32 { + let aIndices = array(d0, d1, d2, d3, d4); + return ${A.getByIndices("aIndices")}; + } + fn getW(d0 : u32, d1 : u32, d2 : u32, d3 : u32, d4 : u32) -> f32 { + let aIndices = array(d0, d1, d2, d3, d4); + return ${P.getByIndices("aIndices")}; + } + ${v.registerUniforms(x).declareVariables(...D,W)} + ${v.mainStart()} + ${v.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + let coords = ${W.offsetToIndices("global_idx")}; + let batch = ${F("coords",0,A.rank)}; + let d2 = ${a?F("coords",A.rank-1,A.rank):F("coords",1,A.rank)}; + let xFRCCorner = vec3(${a?F("coords",1,A.rank):F("coords",2,A.rank)}, + ${a?F("coords",2,A.rank):F("coords",3,A.rank)}, + ${a?F("coords",3,A.rank):F("coords",4,A.rank)}) * uniforms.strides - uniforms.pads; + let xFCorner = xFRCCorner.x; + let xRCorner = xFRCCorner.y; + let xCCorner = xFRCCorner.z; + let xShapeY = ${a?F("uniforms.x_shape",1,A.rank):F("uniforms.x_shape",2,A.rank)}; + let xShapeZ = ${a?F("uniforms.x_shape",2,A.rank):F("uniforms.x_shape",3,A.rank)}; + let xShapeW = ${a?F("uniforms.x_shape",3,A.rank):F("uniforms.x_shape",4,A.rank)}; + let xShapeU = ${a?F("uniforms.x_shape",4,A.rank):F("uniforms.x_shape",1,A.rank)}; + let inputDepthNearestVec4 = (xShapeU / 4) * 4; + let inputDepthVec4Remainder = xShapeU % 4; + + var value = 0.0; + for (var wF = 0u; wF < uniforms.filter_dims[0]; wF++) { + let xF = xFCorner + wF * uniforms.dilations[0]; + if (xF < 0 || xF >= xShapeY) { + continue; + } + + for (var wR = 0u; wR < uniforms.filter_dims[1]; wR++) { + let xR = xRCorner + wR * uniforms.dilations[1]; + if (xR < 0 || xR >= xShapeZ) { + continue; + } + + for (var wC = 0u; wC < uniforms.filter_dims[2]; wC++) { + let xC = xCCorner + wC * uniforms.dilations[2]; + if (xC < 0 || xC >= xShapeW) { + continue; + } + + for (var d1 = 0u; d1 < inputDepthNearestVec4; d1 += 4) { + ${a?`let xValues = vec4( + getX(batch, xF, xR, xC, d1), + getX(batch, xF, xR, xC, d1 + 1), + getX(batch, xF, xR, xC, d1 + 2), + getX(batch, xF, xR, xC, d1 + 3)); + `:`let xValues = vec4( + getX(batch, d1, xF, xR, xC), + getX(batch, d1 + 1, xF, xR, xC), + getX(batch, d1 + 2, xF, xR, xC), + getX(batch, d1 + 3, xF, xR, xC)); + `} + let wValues = vec4( + getW(d2, d1, wF, wR, wC), + getW(d2, d1 + 1, wF, wR, wC), + getW(d2, d1 + 2, wF, wR, wC), + getW(d2, d1 + 3, wF, wR, wC)); + value += dot(xValues, wValues); + } + if (inputDepthVec4Remainder == 1) { + ${a?`value += getX(batch, xF, xR, xC, inputDepthNearestVec4) + * getW(d2, inputDepthNearestVec4, wF, wR, wC);`:`value += getX(batch, inputDepthNearestVec4, xF, xR, xC) + * getW(d2, inputDepthNearestVec4, wF, wR, wC);`} + } else if (inputDepthVec4Remainder == 2) { + ${a?`let xValues = vec2( + getX(batch, xF, xR, xC, inputDepthNearestVec4), + getX(batch, xF, xR, xC, inputDepthNearestVec4 + 1)); + `:`let xValues = vec2( + getX(batch, inputDepthNearestVec4, xF, xR, xC), + getX(batch, inputDepthNearestVec4 + 1, xF, xR, xC)); + `} + let wValues = vec2( + getW(d2, inputDepthNearestVec4, wF, wR, wC), + getW(d2, inputDepthNearestVec4 + 1, wF, wR, wC)); + value += dot(xValues, wValues); + } else if (inputDepthVec4Remainder == 3) { + ${a?`let xValues = vec3( + getX(batch, xF, xR, xC, inputDepthNearestVec4), + getX(batch, xF, xR, xC, inputDepthNearestVec4 + 1), + getX(batch, xF, xR, xC, inputDepthNearestVec4 + 2)); + `:`let xValues = vec3( + getX(batch, inputDepthNearestVec4, xF, xR, xC), + getX(batch, inputDepthNearestVec4 + 1, xF, xR, xC), + getX(batch, inputDepthNearestVec4 + 2, xF, xR, xC)); + `} + let wValues = vec3( + getW(d2, inputDepthNearestVec4, wF, wR, wC), + getW(d2, inputDepthNearestVec4 + 1, wF, wR, wC), + getW(d2, inputDepthNearestVec4 + 2, wF, wR, wC)); + value += dot(xValues, wValues); + } + } + } + } + ${S?"value = value + getBiasByOutputCoords(coords)":""}; + ${Y} + result[global_idx] = f32(value); + }`};return{name:"Conv3DNaive",shaderCache:{hint:`${t.cacheKey};${a};${h};${S}`,inputDependencies:y},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:u[0],y:u[1],z:u[2]},programUniforms:g}),getShaderSource:$}}});var Wu,Nu,Lu=U(()=>{"use strict";J();ae();se();ct();Wu=(e,t,r,n)=>{let o=e.length>2,i=o?"value += b[output_channel];":"",a=e[0].dims,d=e[1].dims,l=t.format==="NHWC",c=l?r[3]:r[1],m=c/t.group,u=l&&m>=4?we(c):1,h=k.size(r)/u,w=[{type:12,data:h},{type:12,data:t.dilations},{type:12,data:[t.strides[0],t.strides[1]]},{type:12,data:[t.pads[0],t.pads[1]]},{type:12,data:m}];Ge(t,w),w.push(...V(a,[d[0],d[1],d[2],d[3]/u]));let g=o?["rank","rank","rank"]:["rank","rank"];w.push(...V([r[0],r[1],r[2],r[3]/u]));let y=S=>{let $=M("output",e[0].dataType,r.length,u),v=he($.type.tensor),x=He(t,$.type.value,v),T=E("x",e[0].dataType,a.length),C=E("w",e[1].dataType,d.length,u),A=[T,C];o&&A.push(E("b",e[2].dataType,e[2].dims,u));let P=[{name:"output_size",type:"u32"},{name:"dilations",type:"u32",length:t.dilations.length},{name:"strides",type:"u32",length:2},{name:"pads",type:"u32",length:2},{name:"output_channels_per_group",type:"u32"}];Fe(t,P);let D=l?` + for (var wHeight: u32 = 0u; wHeight < uniforms.w_shape[0]; wHeight++) { + let xHeight = xRCCorner.x + wHeight * uniforms.dilations[0]; + + if (xHeight < 0u || xHeight >= uniforms.x_shape[1]) { + continue; + } + + for (var wWidth: u32 = 0u; wWidth < uniforms.w_shape[1]; wWidth++) { + let xWidth = xRCCorner.y + wWidth * uniforms.dilations[1]; + if (xWidth < 0u || xWidth >= uniforms.x_shape[2]) { + continue; + } + + for (var wInChannel: u32 = 0u; wInChannel < uniforms.w_shape[2]; wInChannel++) { + let input_channel = in_channel_offset + wInChannel; + let xVal = ${T.get("batch","xHeight","xWidth","input_channel")}; + let wVal = ${C.get("wHeight","wWidth","wInChannel","output_channel")}; + value += xVal * wVal; + } + } + } + `:` + for (var wInChannel: u32 = 0u; wInChannel < uniforms.w_shape[1]; wInChannel++) { + let input_channel = in_channel_offset + wInChannel; + for (var wHeight: u32 = 0u; wHeight < uniforms.w_shape[2]; wHeight++) { + let xHeight = xRCCorner.x + wHeight * uniforms.dilations[0]; + + if (xHeight < 0u || xHeight >= uniforms.x_shape[2]) { + continue; + } + + for (var wWidth: u32 = 0u; wWidth < uniforms.w_shape[3]; wWidth++) { + let xWidth = xRCCorner.y + wWidth * uniforms.dilations[1]; + if (xWidth < 0u || xWidth >= uniforms.x_shape[3]) { + continue; + } + + let xVal = ${T.get("batch","input_channel","xHeight","xWidth")}; + let wVal = ${C.get("output_channel","wInChannel","wHeight","wWidth")}; + value += xVal * wVal; + } + } + } + `;return` + ${S.registerUniforms(P).declareVariables(...A,$)} + + ${S.mainStart()} + ${S.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + + let outputIndices = ${$.offsetToIndices("global_idx")}; + let batch: u32 = outputIndices[0]; + let output_channel: u32 = outputIndices[${l?3:1}]; + let xRCCorner: vec2 = vec2(outputIndices[${l?1:2}], outputIndices[${l?2:3}]) * uniforms.strides - uniforms.pads; + let group_id: u32 = output_channel * ${u} / uniforms.output_channels_per_group; + var in_channel_offset = group_id * uniforms.w_shape[${l?2:1}]; + + var value: ${$.type.value} = ${$.type.value}(0); + ${D} + ${i} + ${x} + ${$.setByOffset("global_idx","value")} + }`};return{name:"GroupedConv",shaderCache:{hint:`${t.cacheKey}_${u}`,inputDependencies:g},getRunData:()=>({outputs:[{dims:n?n(r):r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(h/64)},programUniforms:w}),getShaderSource:y}},Nu=(e,t,r,n)=>{let o=e.length>2,i=we(r[3]),a=we(r[2]),d=k.size(r)/i/a,l=[e[0].dims[0],e[0].dims[1],e[0].dims[2],e[0].dims[3]/i],c=[e[1].dims[0],e[1].dims[1],e[1].dims[2],e[1].dims[3]/i],m=[r[0],r[1],r[2],r[3]/i],u=[{type:12,data:d},{type:6,data:[t.strides[0],t.strides[1]]},{type:6,data:[t.pads[0],t.pads[1]]}];Ge(t,u),u.push(...V(l,c,m));let h=(a-1)*t.strides[1]+c[1],w=g=>{let y=M("output",e[0].dataType,m.length,i),S=he(y.type.tensor),$=He(t,y.type.value,S),v=E("x",e[0].dataType,l.length,i),x=E("w",e[1].dataType,c.length,i),T=[v,x];o&&T.push(E("b",e[2].dataType,e[2].dims,i));let C=o?"value += b[output_channel];":"",A=[{name:"output_size",type:"u32"},{name:"strides",type:"i32",length:2},{name:"pads",type:"i32",length:2}];return Fe(t,A),` + ${g.registerUniforms(A).declareVariables(...T,y)} + ${g.mainStart()} + ${g.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + let width0 = uniforms.output_shape[3]; + let output_channel = global_idx % width0; + var index1 = global_idx / width0; + let width1 = uniforms.output_shape[2] / ${a}u; + let col = (index1 % width1) * ${a}u; + index1 = index1 / width1; + let row = index1 % uniforms.output_shape[1]; + let batch = index1 / uniforms.output_shape[1]; + + let x_corner = vec2(i32(row), i32(col)) * uniforms.strides - uniforms.pads; + + var x_vals: array<${v.type.value}, ${h}>; + var values: array<${y.type.value}, ${a}>; + let input_channel = output_channel; + // Use constant instead of uniform can give better performance for w's height/width. + for (var w_height: u32 = 0u; w_height < ${c[0]}; w_height++) { + let x_height = x_corner.x + i32(w_height); + if (x_height >= 0 && u32(x_height) < uniforms.x_shape[1]) { + for (var i = 0; i < ${h}; i++) { + let x_width = x_corner.y + i; + if (x_width >= 0 && u32(x_width) < uniforms.x_shape[2]) { + x_vals[i] = ${v.get("batch","u32(x_height)","u32(x_width)","input_channel")}; + } else { + x_vals[i] = ${v.type.value}(0); + } + } + for (var w_width: u32 = 0u; w_width < ${c[1]}; w_width++) { + let w_val = ${x.get("w_height","w_width","0","output_channel")}; + for (var i = 0u; i < ${a}u; i++) { + values[i] = fma(x_vals[i * u32(uniforms.strides[1]) + w_width], w_val, values[i]); + } + } + } + } + + for (var i = 0u; i < ${a}u; i++) { + var value = values[i]; + ${C} + ${$} + ${y.set("batch","row","col + i","output_channel","value")}; + } + }`};return{name:"GroupedConv-Vectorize",shaderCache:{hint:`${t.cacheKey};${i};${a};${h};${c[0]};${c[1]}`,inputDependencies:o?["rank","rank","type"]:["rank","rank"]},getRunData:()=>({outputs:[{dims:n?n(r):r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(d/64)},programUniforms:u}),getShaderSource:w}}});var ho,Km,Hu,go=U(()=>{"use strict";J();ae();rr();se();ct();ho=(e,t,r,n,o=!1,i)=>{let a=e[0].dims,d=e[1].dims,l=a[a.length-2],c=d[d.length-1],m=a[a.length-1],u=we(c),h=we(m),w=we(l),g=k.size(r)/u/w,y=e.length>2,S=n?n.slice(0,-2):r.slice(0,-2),v=[k.size(S),l,c],x=[{type:12,data:g},{type:12,data:l},{type:12,data:c},{type:12,data:m}];Ge(t,x),x.push(...V(S,a,d)),y&&x.push(...V(e[2].dims)),x.push(...V(v));let T=C=>{let A=Fr("batch_dims",e[0].dataType,S.length),P=E("a",e[0].dataType,a.length,h),D=E("b",e[1].dataType,d.length,u),W=M("output",e[0].dataType,v.length,u),N=he(W.type.tensor),j=He(t,W.type.value,N),Y=[P,D],Z="";if(y){let re=o?u:1;Y.push(E("bias",e[2].dataType,e[2].dims.length,re)),Z=`${o?`value += bias[col / ${re}];`:`value += ${W.type.value}(bias[row + i]);`}`}let te=a.slice(0,-2),ue=d.slice(0,-2),K=Wt(te,S),de=Wt(ue,S),ce=[{name:"output_size",type:"u32"},{name:"M",type:"u32"},{name:"N",type:"u32"},{name:"K",type:"u32"}];Fe(t,ce);let q=(re,ne)=>{let oe=re.rank,R=re.name;if(oe===2)return`var ${R}_indices = ${re.type.indices}(0u, 0u);`;let G=A.rank,ye=`var ${R}_indices: ${re.type.indices};`;for(let Re=oe-2-1,$e=G-1;Re>=0;Re--,$e--)ye+=` +${R}_indices[${Re}] = ${G>1?`batch_indices[${$e}]`:"batch_indices"};`;return ne.forEach(Re=>{ye+=` +${R}_indices[${Re}] = 0;`}),ye+=`${R}_indices[${oe-2}] = 0u; + ${R}_indices[${oe-1}] = 0u;`,ye},le=()=>{let re=`var a_data: ${P.type.value};`;for(let ne=0;ne; + for (var k: u32 = 0u; k < uniforms.K; k = k + ${h}) { + ${le()} + } + for (var i = 0u; i < ${w}u; i++) { + var value = values[i]; + ${Z} + ${j} + let cur_indices = ${W.type.indices}(batch, row + i, col); + let offset = ${W.indicesToOffset("cur_indices")}; + ${W.setByOffset(`offset / ${u}`,"value")}; + } + } + `};return{name:"MatMulNaive",shaderCache:{hint:`${t.activation};${u};${h};${w};${o}`,inputDependencies:y?["rank","rank","rank"]:["rank","rank"]},getRunData:()=>({outputs:[{dims:i?i(r):r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(g/64)},programUniforms:x}),getShaderSource:T}},Km=e=>{if(!e||e.length!==2)throw new Error("MatMul requires 2 inputs.");if(e[0].dims[e[0].dims.length-1]!==e[1].dims[e[1].dims.length-2])throw new Error("shared dimension does not match.")},Hu=e=>{Km(e.inputs);let t=rt.calcShape(e.inputs[0].dims,e.inputs[1].dims,!0);if(!t)throw new Error("Can't use matmul on the given tensors");let r=t[t.length-1],n=e.inputs[0].dims[e.inputs[0].dims.length-1];r<8&&n<8?e.compute(ho(e.inputs,{activation:""},t)):e.compute(en(e.inputs,{activation:""},t))}});var Ym,yo,Xm,bo,wo,Gu,Qm,Zm,_o,Fu=U(()=>{"use strict";ae();Du();Vu();rr();Lu();ct();go();lt();Ym=(e,t,r,n,o,i)=>{let a=e[0],d=e.slice(i?1:2,i?3:4),l=d.length,c=t[0],u=t.slice(2).map((g,y)=>g+(g-1)*(r[y]-1)),w=d.map((g,y)=>g+n[y]+n[y+l]).map((g,y)=>Math.floor((g-u[y]+o[y])/o[y]));return w.splice(0,0,a),w.splice(i?3:1,0,c),w},yo=[2,3,1,0],Xm=(e,t)=>{if(!e||e.length!==2&&e.length!==3)throw new Error("Conv requires 2 or 3 inputs");if(e[0].dims.length>5)throw new Error("greater than 5D is not supported");if(e[0].dims.length!==e[1].dims.length)throw new Error("filter does not have same dimension as input");let r=e[0].dims[t.format==="NHWC"?e[0].dims.length-1:1],n=e[1].dims[1]*t.group;if(r!==n)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");if(e.length===3&&(e[2].dims.length!==1||e[1].dims[0]!==e[2].dims[0]))throw new Error("invalid bias");let o=e[0].dims.length-2;if(t.dilations.length!==o)throw new Error(`dilations should be ${o}D`);if(t.strides.length!==o)throw new Error(`strides should be ${o}D`);if(t.pads.length!==o*2)throw new Error(`pads should be ${o*2}D`);if(t.kernelShape.length!==0&&t.kernelShape.length!==e[1].dims.length-2)throw new Error("invalid kernel shape")},bo=(e,t)=>{let r=e.kernelShape.slice();r.length{let t=Qr(e),r=e.format,n=["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][e.auto_pad],o=e.dilations,i=e.group,a=e.kernel_shape,d=e.pads,l=e.strides,c=e.w_is_const();return{autoPad:n,format:r,dilations:o,group:i,kernelShape:a,pads:d,strides:l,wIsConst:c,...t,cacheKey:`${e.format};${t.activation};`}},Gu=(e,t,r,n)=>{let o=r.format==="NHWC",i=Ym(t[0].dims,t[1].dims,r.dilations,r.pads,r.strides,o);if(r.group!==1){let A=[t[0]];if(o){let D=e.kernelCustomData.wT??e.compute(Pe(t[1],yo),{inputs:[1],outputs:[r.wIsConst?-2:-1]})[0];r.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=D),A.push(D)}else A.push(t[1]);t.length===3&&A.push(t[2]),!e.adapterInfo.isArchitecture("ampere")&&o&&t[1].dims[0]===r.group&&t[1].dims[1]===1&&r.dilations[0]===1&&r.dilations[1]===1?e.compute(Nu(A,r,i,n),{inputs:A}):e.compute(Wu(A,r,i,n),{inputs:A});return}let a=t.length===3,d=t[0].dims[o?1:2],l=t[0].dims[o?2:3],c=t[0].dims[o?3:1],m=t[1].dims[2],u=t[1].dims[3],h=i[o?1:2],w=i[o?2:3],g=i[o?3:1],y=o&&m===d&&u===l&&r.pads[0]===0&&r.pads[1]===0;if(y||m===1&&u===1&&r.dilations[0]===1&&r.dilations[1]===1&&r.strides[0]===1&&r.strides[1]===1&&r.pads[0]===0&&r.pads[1]===0){let A=i[0],P,D,W,N=[];if(o){let Z=e.kernelCustomData.wT??e.compute(Pe(t[1],yo),{inputs:[1],outputs:[r.wIsConst?-2:-1]})[0];if(r.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=Z),y){let te=d*l*c;P=t[0].reshape([1,A,te]),D=Z.reshape([1,te,g]),W=[1,A,g]}else P=t[0].reshape([A,d*l,c]),D=Z.reshape([1,c,g]),W=[A,h*w,g];N.push(P),N.push(D)}else P=t[0].reshape([A,c,d*l]),D=t[1].reshape([1,g,c]),W=[A,g,h*w],N.push(D),N.push(P);a&&N.push(t[2]);let j=W[2],Y=N[0].dims[N[0].dims.length-1];j<8&&Y<8?e.compute(ho(N,r,i,W,o,n),{inputs:N}):e.compute(en(N,r,i,W,o,n),{inputs:N});return}let S=!0,$=e.kernelCustomData.wT??e.compute(Pe(t[1],yo),{inputs:[1],outputs:[r.wIsConst?-2:-1]})[0];r.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=$);let v=[t[0],$];a&&v.push(t[2]);let x=o?h*w:g,T=o?g:h*w,C=m*u*c;e.compute(Ou(v,r,i,x,T,C,a,S,n),{inputs:v})},Qm=(e,t)=>{let r=t.format==="NHWC",n=[e.inputs[0].reshape(r?[e.inputs[0].dims[0],1,e.inputs[0].dims[1],e.inputs[0].dims[2]]:[e.inputs[0].dims[0],e.inputs[0].dims[1],1,e.inputs[0].dims[2]]),e.inputs[1].reshape([e.inputs[1].dims[0],e.inputs[1].dims[1],1,e.inputs[1].dims[2]])];e.inputs.length===3&&n.push(e.inputs[2]);let o=[0,t.pads[0],0,t.pads[1]],i=[1].concat(t.strides),a=[1].concat(t.dilations),d=[1].concat(t.kernelShape),l=bo({...t,pads:o,strides:i,dilations:a,kernelShape:d},n);Gu(e,n,l,c=>r?[c[0],c[2],c[3]]:[c[0],c[1],c[3]])},Zm=(e,t,r)=>{let n=r.format==="NHWC"?"channelsLast":"channelsFirst",o=bo(r,t),i=r.autoPad==="NOTSET"?r.pads:r.autoPad,a=Ru(t[0].dims,t[1].dims,r.strides,r.dilations,i,!1,n);e.compute(Uu(t,o,a.outShape,[a.filterDepth,a.filterHeight,a.filterWidth],[a.padInfo.front,a.padInfo.top,a.padInfo.left],n))},_o=(e,t)=>{if(Xm(e.inputs,t),e.inputs[0].dims.length===3)Qm(e,t);else if(e.inputs[0].dims.length===5)Zm(e,e.inputs,t);else{let r=bo(t,e.inputs);Gu(e,e.inputs,r)}}});var Jm,qu,ju=U(()=>{"use strict";J();Xe();se();ct();Jt();fo();rr();Jm=(e,t=!1,r,n,o=4)=>{let i=$=>{switch($){case 1:return"return w[getIndexFromCoords4D(coord, vec4(uniforms.w_shape))];";case 4:return` + let coord1 = vec4(coordX, coordY, col + 1, rowInner); + let coord2 = vec4(coordX, coordY, col + 2, rowInner); + let coord3 = vec4(coordX, coordY, col + 3, rowInner); + let v0 = w[getIndexFromCoords4D(coord, vec4(uniforms.w_shape))]; + let v1 = w[getIndexFromCoords4D(coord1, vec4(uniforms.w_shape))]; + let v2 = w[getIndexFromCoords4D(coord2, vec4(uniforms.w_shape))]; + let v3 = w[getIndexFromCoords4D(coord3, vec4(uniforms.w_shape))]; + return ${n}(v0, v1, v2, v3); + `;default:throw new Error(`innerElementSize ${$} is not supported.`)}},a=e?` + let coord = vec4(batch, iXR, iXC, xCh); + `:` + let coord = vec4(batch, xCh, iXR, iXC); + `,d=e?` + let coords = vec4( + batch, + row / outWidth, + row % outWidth, + col); + `:` + let coords = vec4( + batch, + row, + col / outWidth, + col % outWidth); + `,l=e?"i32(uniforms.x_shape[1])":"i32(uniforms.x_shape[2])",c=e?"i32(uniforms.x_shape[2])":"i32(uniforms.x_shape[3])",m=e?"row":"col",u=e?"col":"row",h=` + let inChannels = ${e?"i32(uniforms.x_shape[3])":"i32(uniforms.x_shape[1])"}; + let outWidth = ${e?"i32(uniforms.result_shape[2])":"i32(uniforms.result_shape[3])"}; + let outRow = ${m} / outWidth; + let outCol = ${m} % outWidth; + + let WRow = ${u} / (uniforms.filter_dims[1] * inChannels); + let WCol = ${u} / inChannels % uniforms.filter_dims[1]; + let xR = f32(outRow - uniforms.pads[0] + uniforms.dilations[0] * WRow) / f32(uniforms.strides[0]); + let xC = f32(outCol - uniforms.pads[1] + uniforms.dilations[1] * WCol) / f32(uniforms.strides[1]); + if (xR < 0.0 || xR >= f32(${l}) || fract(xR) > 0.0) { + return ${n}(0.0); + } + if (xC < 0.0 || xC >= f32(${c}) || fract(xC) > 0.0) { + return ${n}(0.0); + } + let iXR = i32(xR); + let iXC = i32(xC); + let xCh = ${u} % inChannels; + ${a} + return x[getIndexFromCoords4D(coord, vec4(uniforms.x_shape))/${o}];`,w=e?` + let col = colIn * ${o}; + if (row < uniforms.dim_a_outer && col < uniforms.dim_inner) { + ${h} + } + return ${n}(0.0);`:` + let col = colIn * ${o}; + if (row < uniforms.dim_inner && col < uniforms.dim_b_outer) { + ${h} + } + return ${n}(0.0);`,g=` + let col = colIn * ${o}; + let inChannels = ${e?"i32(uniforms.x_shape[3])":"i32(uniforms.x_shape[1])"}; + let coordX = uniforms.filter_dims[0] - 1 - row / (uniforms.filter_dims[1] * inChannels); + let coordY = uniforms.filter_dims[1] - 1 - (row / inChannels) % uniforms.filter_dims[1]; + if (${e?"row < uniforms.dim_inner && col < uniforms.dim_b_outer":"row < uniforms.dim_inner && col < uniforms.dim_a_outer"} && coordX >= 0 && coordY >= 0) { + let rowInner = row % inChannels; + let coord = vec4(coordX, coordY, col, rowInner); + ${i(o)} + } + return ${n}(0.0); + `,y=He(r,n);return` + fn mm_readA(batch: i32, row : i32, colIn : i32) -> ${n} { + ${e?w:g} + } + + fn mm_readB(batch: i32, row : i32, colIn : i32) -> ${n} { + ${e?g:w} + } + + fn mm_write(batch: i32, row : i32, colIn : i32, valueInput : ${n}) { + let col = colIn * ${o}; + if (row < uniforms.dim_a_outer && col < uniforms.dim_b_outer) { + var value = valueInput; + let outWidth = ${e?"i32(uniforms.result_shape[2])":"i32(uniforms.result_shape[3])"}; + ${d} + ${Zr(t)} + ${y} + result[getIndexFromCoords4D(coords, vec4(uniforms.result_shape))/${o}] = value; + } + }`},qu=(e,t,r,n,o,i,a,d)=>{let l=t.format==="NHWC",c=l?e[0].dims[3]:e[0].dims[1],m=r[0],u=l?r[2]:r[3],h=l?r[1]:r[2],w=l?r[3]:r[1],g=l&&c%4===0&&c%3&&w%4===0,y=l?w:u*h,S=l?u*h:w,$=[8,8,1],v=n<=8?[4,1,1]:[4,4,1],x=[Math.ceil(y/$[0]/v[0]),Math.ceil(S/$[1]/v[1]),Math.ceil(m/$[2]/v[2])];pe("verbose",()=>`[conv_backprop_mm_webgpu] dispatch = ${x}`);let T=g?4:1,C=Math.max($[0]*T,$[1]),A=g?4:1,P=[t.kernelShape[l?1:2],t.kernelShape[l?2:3]],D=[P[0]+(t.dilations[0]<=1?0:(P[0]-1)*(t.dilations[0]-1)),P[1]+(t.dilations[1]<=1?0:(P[1]-1)*(t.dilations[1]-1))],W=[D[0]-1-Math.floor((t.pads[0]+t.pads[2])/2),D[1]-1-Math.floor((t.pads[1]+t.pads[3])/2)],N=[{type:6,data:n},{type:6,data:o},{type:6,data:i},{type:6,data:t.strides},{type:6,data:t.dilations},{type:6,data:P},{type:6,data:W}];Ge(t,N),N.push(...V(e[0].dims,e[1].dims));let j=["rank","rank"];a&&(N.push(...V(e[2].dims)),j.push("rank")),N.push(...V(r));let Y=Z=>{let te=E("x",e[0].dataType,e[0].dims.length,A),ue=E("w",e[1].dataType,e[1].dims.length,1),K=M("result",e[0].dataType,r.length,A),de=[te,ue],ce="";if(a){let re=E("bias",e[2].dataType,e[2].dims.length,A);de.push(re),ce+=` + fn getBiasByOutputCoords(coords : vec4) -> ${re.type.value} { + return bias[coords.${l?"w":"y"}${g?"/ 4":""}]; + }`}let q=[{name:"dim_a_outer",type:"i32"},{name:"dim_b_outer",type:"i32"},{name:"dim_inner",type:"i32"},{name:"strides",type:"i32",length:2},{name:"dilations",type:"i32",length:2},{name:"filter_dims",type:"i32",length:P.length},{name:"pads",type:"i32",length:W.length}];Fe(t,q);let le=he(e[0].dataType,1);if(le!=="f16"&&le!=="f32")throw new Error(`elemType ${le} is not supported.`);return` + ${Jr("uniforms.result_strides")} + ${Z.registerUniforms(q).declareVariables(...de,K)}; + ${ce} + ${Jm(l,a,t,te.type.value,T)} + ${g?er(v,$,le,void 0,!l,C):tr(v,$,le,void 0,!l,C,!1,void 0,d)}`};return{name:"Conv2DTransposeMatMul",shaderCache:{hint:`${t.cacheKey};${v};${$};${g}`,inputDependencies:j},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:x[0],y:x[1],z:x[2]},programUniforms:N}),getShaderSource:Y}}});var ef,vo,Ku=U(()=>{"use strict";J();Xe();ae();se();ef=(e,t,r,n,o,i=!1,a,d,l=!1)=>{let c=l?1:2,m=l?2:3,u=l?3:1,h=i?2:1,w=` + fn setOutputAtIndex(flatIndex : u32, value : ${i?`vec4<${a}>`:a}) { + result[flatIndex] = ${i?`vec4<${a}>`:a}(value); + }`;n&&(w+=` + fn getBiasByOutputCoords(coords : vec4) -> ${i?`vec4<${a}>`:a} { + return bias[coords.${l?"w":"y"}${i?"/ 4":""}]; + }`);let g=i?4:1,y=E("W",t[1].dataType,t[1].dims.length,g),S=E("Dy",t[0].dataType,t[0].dims.length,g),$=[S,y];n&&$.push(E("bias",t[2].dataType,[r[u]].length,g));let v=M("result",t[0].dataType,r.length,g),x=`{ + let batch: u32 = ${o?"global_id.z":"workgroup_id.z"} / uniforms.result_shape[1]; + let r = ${o?"global_id.z":"workgroup_id.z"} % uniforms.result_shape[1]; + let c = ${o?"global_id.y":"workgroup_id.y"} * ${h}; + let d1: u32 = ${o?"global_id.x":"workgroup_id.x"} * 4; + + let dyCorner = vec2(i32(r), i32(c)) - vec2(uniforms.pads); + + // Convolve dy(?, ?, d2) with w(:, :, d1, d2) to compute dx(xR, xC, d1). + // ? = to be determined. : = across all values in that axis. + var dotProd: array, ${h}>; + for (var i = 0; i < ${h}; i++) { + dotProd[i] = vec4<${a}>(0.0); + } + for (var wR: u32 = 0; wR < uniforms.filter_dims[0]; wR = wR + 1) { + var dyR = (${a}(dyCorner.x) + ${a}(wR)) / ${a}(uniforms.strides.x); + let wRPerm = uniforms.filter_dims[0] - 1 - wR; + if (dyR < 0.0 || dyR >= ${a}(uniforms.Dy_shape[1]) || + fract(dyR) > 0.0 || wRPerm < 0) { + continue; + } + let idyR: u32 = u32(dyR); + + for (var wC: u32 = 0; wC < uniforms.filter_dims[1]; wC = wC + 1) { + let dyC = (${a}(dyCorner.y) + ${a}(wC)) / ${a}(uniforms.strides.y); + let dyC2 = (${a}(dyCorner.y) + 1.0 + ${a}(wC)) / ${a}(uniforms.strides.y); + let wCPerm = uniforms.filter_dims[1] - 1 - wC; + if (wCPerm < 0) { + continue; + } + var bDyCVal = true; + var bDyCVal2 = true; + if (dyC < 0.0 || dyC >= ${a}(uniforms.Dy_shape[2]) || + fract(dyC) > 0.0) { + bDyCVal = false; + } + if (dyC2 < 0.0 || dyC2 >= ${a}(uniforms.Dy_shape[2]) || + fract(dyC2) > 0.0) { + bDyCVal2 = false; + } + + let idyC: u32 = u32(dyC); + let idyC2: u32 = u32(dyC2); + if (bDyCVal && bDyCVal2) { + let d2Length = uniforms.Dy_shape[3]; + for (var d2 :u32 = 0; d2 < d2Length; d2 = d2 + 4) { + let wValue0 = ${y.get("u32(wRPerm)","u32(wCPerm)","d1","d2")}; + let wValue1 = ${y.get("u32(wRPerm)","u32(wCPerm)","d1 + 1","d2")}; + let wValue2 = ${y.get("u32(wRPerm)","u32(wCPerm)","d1 + 2","d2")}; + let wValue3 = ${y.get("u32(wRPerm)","u32(wCPerm)","d1 + 3","d2")}; + + var xValue = ${S.get("batch","idyR","idyC","d2")}; + let tmpval = vec4<${a}>(dot(xValue, wValue0), + dot(xValue, wValue1), + dot(xValue, wValue2), + dot(xValue, wValue3)); + dotProd[0] = dotProd[0] + tmpval; + + xValue = ${S.get("batch","idyR","idyC2","d2")}; + + dotProd[1] = dotProd[1] + vec4<${a}>(dot(xValue, wValue0), + dot(xValue, wValue1), + dot(xValue, wValue2), + dot(xValue, wValue3)); + } + } else if (bDyCVal) { + let d2Length = uniforms.Dy_shape[${u}]; + for (var d2: u32 = 0; d2 < d2Length; d2 = d2 + 4) { + let wValue0 = ${y.get("u32(wRPerm)","u32(wCPerm)","d1","d2")}; + let wValue1 = ${y.get("u32(wRPerm)","u32(wCPerm)","d1 + 1","d2")}; + let wValue2 = ${y.get("u32(wRPerm)","u32(wCPerm)","d1 + 2","d2")}; + let wValue3 = ${y.get("u32(wRPerm)","u32(wCPerm)","d1 + 3","d2")}; + + var xValue = ${S.get("batch","idyR","idyC","d2")}; + let tmpval = vec4<${a}>(dot(xValue, wValue0), + dot(xValue, wValue1), + dot(xValue, wValue2), + dot(xValue, wValue3)); + dotProd[0] = dotProd[0] + tmpval; + } + } else if (bDyCVal2) { + let d2Length = uniforms.Dy_shape[3]; + for (var d2: u32 = 0; d2 < d2Length; d2 = d2 + 4) { + let wValue0 = ${y.get("u32(wRPerm)","u32(wCPerm)","d1","d2")}; + let wValue1 = ${y.get("u32(wRPerm)","u32(wCPerm)","d1 + 1","d2")}; + let wValue2 = ${y.get("u32(wRPerm)","u32(wCPerm)","d1 + 2","d2")}; + let wValue3 = ${y.get("u32(wRPerm)","u32(wCPerm)","d1 + 3","d2")}; + + var xValue = ${S.get("batch","idyR","idyC2","d2")}; + let tmpval = vec4<${a}>(dot(xValue, wValue0), + dot(xValue, wValue1), + dot(xValue, wValue2), + dot(xValue, wValue3)); + dotProd[1] = dotProd[1] + tmpval; + } + } + } + } + + for (var i: u32 = 0; i < ${h}; i = i + 1) { + let value = dotProd[i] + ${n?"bias[c+i]":`vec4<${a}>(0.0)`}; + ${v.set("batch","r","c + i","d1","value")}; + } + }`,T=` + let outputIndices = ${v.offsetToIndices("global_idx")}; + let batch = ${v.indicesGet("outputIndices",0)}; + let d1 = ${v.indicesGet("outputIndices",u)}; + let r = ${v.indicesGet("outputIndices",c)}; + let c = ${v.indicesGet("outputIndices",m)}; + let dyCorner = vec2(i32(r), i32(c)) - uniforms.pads; + let dyRCorner = dyCorner.x; + let dyCCorner = dyCorner.y; + let groupId = d1 / uniforms.output_channels_per_group; + let wOutChannel = d1 - groupId * uniforms.output_channels_per_group; + // Convolve dy(?, ?, d2) with w(:, :, d1, d2) to compute dx(xR, xC, d1). + // ? = to be determined. : = across all values in that axis. + var dotProd = ${a}(0.0); + for (var wR: u32 = 0; wR < uniforms.effective_filter_dims.x; wR = wR + 1) { + if (wR % uniforms.dilations.x != 0) { + continue; + } + let dyR = (${a}(dyRCorner) + ${a}(wR)) / ${a}(uniforms.strides[0]); + let wRPerm = uniforms.filter_dims.x - 1 - wR / uniforms.dilations.x; + if (dyR < 0.0 || dyR >= ${a}(uniforms.Dy_shape[${c}]) || fract(dyR) > 0.0 || + wRPerm < 0) { + continue; + } + let idyR: u32 = u32(dyR); + + for (var wC: u32 = 0; wC < uniforms.effective_filter_dims.y; wC = wC + 1) { + if (wC % uniforms.dilations.y != 0) { + continue; + } + let dyC = (${a}(dyCCorner) + ${a}(wC)) / ${a}(uniforms.strides.y); + let wCPerm = uniforms.filter_dims.y - 1 - wC / uniforms.dilations.y; + if (dyC < 0.0 || dyC >= ${a}(uniforms.Dy_shape[${m}]) || + fract(dyC) > 0.0 || wCPerm < 0) { + continue; + } + let idyC: u32 = u32(dyC); + var inputChannel = groupId * uniforms.input_channels_per_group; + for (var d2: u32 = 0; d2 < uniforms.input_channels_per_group; d2 = d2 + 1) { + let xValue = ${l?S.get("batch","idyR","idyC","inputChannel"):S.get("batch","inputChannel","idyR","idyC")}; + let wValue = ${y.get("inputChannel","wOutChannel","u32(wRPerm)","u32(wCPerm)")}; + dotProd = dotProd + xValue * wValue; + inputChannel = inputChannel + 1; + } + } + } + let value = dotProd + ${n?"bias[d1]":`${a}(0.0)`}; + ${v.setByOffset("global_idx","value")}; + `;return` + ${e.registerUniforms(d).declareVariables(...$,v)} + ${w} + + ${e.mainStart()} + ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}; + ${i?x:T}}`},vo=(e,t,r)=>{let n=e.length>2,o=t.outputShape,i=k.size(o),a=[Math.ceil(i/64),1,1];pe("verbose",()=>`[conv2d_backprop_webgpu] dispatch = ${a}`);let d=t.format==="NHWC",l=["rank","rank"],c=[t.strides[0],t.strides[1]],m=[t.kernelShape[d?1:2],t.kernelShape[d?2:3]],u=[t.dilations[0],t.dilations[1]],h=[m[0]+(t.dilations[0]<=1?0:(t.kernelShape[d?1:2]-1)*(t.dilations[0]-1)),m[1]+(t.dilations[1]<=1?0:(t.kernelShape[d?2:3]-1)*(t.dilations[1]-1))],w=[h[0]-1-Math.floor((t.pads[0]+t.pads[2])/2),h[1]-1-Math.floor(t.pads[1]+t.pads[3])/2],g=!1,y=t.group,S=e[1].dims,$=S[0]/y,v=S[1],x=[{type:12,data:i},{type:12,data:c},{type:12,data:m},{type:12,data:u},{type:12,data:h},{type:6,data:w},{type:12,data:$},{type:12,data:v},...V(e[0].dims,e[1].dims)];n&&(x.push(...V(e[2].dims)),l.push("rank")),x.push(...V(o));let T=a[1]===1&&a[2]===1,C=A=>{let P=[{name:"output_size",type:"u32"},{name:"strides",type:"u32",length:c.length},{name:"filter_dims",type:"u32",length:m.length},{name:"dilations",type:"u32",length:m.length},{name:"effective_filter_dims",type:"u32",length:h.length},{name:"pads",type:"i32",length:w.length},{name:"input_channels_per_group",type:"u32"},{name:"output_channels_per_group",type:"u32"}],D=he(e[0].dataType);return`${ef(A,e,o,n,T,g,D,P,d)}`};return{name:"ConvTranspose2D",shaderCache:{hint:`${t.cacheKey};`,inputDependencies:l},getRunData:()=>({dispatchGroup:{x:a[0],y:a[1],z:a[2]},outputs:[{dims:r?r(o):o,dataType:e[0].dataType}],programUniforms:x}),getShaderSource:C}}});var tf,rf,nf,Yu,Xu,of,af,sf,uf,Qu,Zu=U(()=>{"use strict";ju();Ku();ct();lt();tf=(e,t,r,n,o,i)=>(e-1)*t+r+(n-1)*o+1-i,rf=(e,t,r,n,o)=>{let i=Math.floor(e/2);t==="SAME_UPPER"?(r[n]=i,r[o]=e-i):t==="SAME_LOWER"&&(r[n]=e-i,r[o]=i)},nf=(e,t,r,n,o,i,a,d,l,c)=>{let m=e.length-2,u=c.length===0;l.length{let r=e.kernelShape.slice();if(e.kernelShape.length===0||e.kernelShape.reduce((u,h)=>u*h,1)===0){r.length=0;for(let u=2;uu+h,0)===0){let u=t[0].dims.length-2;l=new Array(u).fill(1)}let c=e.strides.slice();if(c.reduce((u,h)=>u+h,0)===0){let u=t[0].dims.length-2;c=new Array(u).fill(1)}nf(d,r,l,e.autoPad,e.group,o,c,n,a,i);let m=Object.assign({},e);return Object.assign(m,{kernelShape:r,pads:o,outputPadding:a,outputShape:i,dilations:l,strides:c}),m},Xu=e=>{let t=Qr(e),r=e.format,n=["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][typeof e.autoPad>"u"?0:e.autoPad],o=e.dilations,i=e.group,a=e.kernelShape,d=e.pads,l=e.strides,c=e.wIsConst(),m=e.outputPadding,u=e.outputShape;return{autoPad:n,format:r,dilations:o,group:i,kernelShape:a,outputPadding:m,outputShape:u,pads:d,strides:l,wIsConst:c,...t,cacheKey:`${e.format};${t.activation};`}},of=(e,t)=>{if(!e||e.length!==2&&e.length!==3)throw new Error("Conv requires 2 or 3 inputs");if(e[0].dims.length!==4&&e[0].dims.length!==3)throw new Error("currently only support 2-dimensional conv");if(e[0].dims.length!==e[1].dims.length)throw new Error("filter does not have same dimension as input");let r=e[0].dims[t.format==="NHWC"?e[0].dims.length-1:1],n=e[1].dims[0];if(r!==n)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");let o=e[1].dims[1]*t.group;if(e.length===3&&(e[2].dims.length!==1||e[2].dims[0]!==o))throw new Error("invalid bias");let i=e[0].dims.length-2;if(t.dilations.reduce((m,u)=>m+u,0)>0&&t.dilations.length!==i)throw new Error(`dilations should be ${i}D`);if(t.strides.reduce((m,u)=>m+u,0)>0&&t.strides.length!==i)throw new Error(`strides should be ${i}D`);if(t.pads.reduce((m,u)=>m+u,0)>0&&t.pads.length!==i*2)throw new Error(`pads should be ${i*2}D`);if(t.outputPadding.length!==i&&t.outputPadding.length!==0)throw new Error(`output_padding should be ${i}D`);if(t.kernelShape.reduce((m,u)=>m+u,0)>0&&t.kernelShape.length!==0&&t.kernelShape.length!==e[1].dims.length-2)throw new Error("invalid kernel shape");if(t.outputShape.length!==0&&t.outputShape.length!==e[0].dims.length-2)throw new Error("invalid output shape")},af=[2,3,1,0],sf=(e,t,r)=>{let n=Yu(r,t),o=r.format==="NHWC",i=n.outputShape,a=i[o?3:1],d=t[0].dims[o?3:1];if(n.group!==1||a===1&&d===1){e.compute(vo(t,n));return}let l=i[o?1:2],c=i[o?2:3],m=t[1].dims[2],u=t[1].dims[3],h=o?l*c:a,w=o?a:l*c,g=m*u*d,y=!0,S=e.kernelCustomData.wT??e.compute(Pe(t[1],af),{inputs:[1],outputs:[r.wIsConst?-2:-1]})[0];r.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=S);let $=[t[0],S],v=t.length===3;v&&(!o&&t[2].dims.length===1?$.push(t[2].reshape([t[2].dims[0],1,1])):$.push(t[2])),e.compute(qu($,n,i,h,w,g,v,y),{inputs:$})},uf=(e,t)=>{let r=t.format==="NHWC",n=[e.inputs[0].reshape(r?[e.inputs[0].dims[0],1,e.inputs[0].dims[1],e.inputs[0].dims[2]]:[e.inputs[0].dims[0],e.inputs[0].dims[1],1,e.inputs[0].dims[2]]),e.inputs[1].reshape([e.inputs[1].dims[0],e.inputs[1].dims[1],1,e.inputs[1].dims[2]])];e.inputs.length===3&&n.push(e.inputs[2]);let o=t.kernelShape;(o.length===0||o[0]===0)&&(o=[e.inputs[1].dims[2]]);let i=t.dilations;(i.length===0||i[0]===0)&&(i=[1]);let a=t.strides;(a.length===0||a[0]===0)&&(a=[1]);let d=t.pads;d.length===0&&(d=[0,0]),d=[0,d[0],0,d[1]],a=[1].concat(a),i=[1].concat(i),o=[1].concat(o);let l=Yu({...t,pads:d,strides:a,dilations:i,kernelShape:o},n);e.compute(vo(n,l,c=>r?[c[0],c[2],c[3]]:[c[0],c[1],c[3]]))},Qu=(e,t)=>{of(e.inputs,t),e.inputs[0].dims.length===3?uf(e,t):sf(e,e.inputs,t)}});var df,Ju,ed,td=U(()=>{"use strict";J();ae();Ie();se();df=(e,t,r,n)=>{let o=k.size(t),i=t.length,a=E("input",e,i),d=M("output",e,i),l=r.dataType===6?r.getInt32Array()[0]:Number(r.getBigInt64Array()[0]),c=k.normalizeAxis(l,i),m=u=>{let h=` i32(${a.indicesGet("inputIndices","uniforms.axis")}) `,w=F("uniforms.input_shape","uniforms.axis",i),g=n.reverse?h+(n.exclusive?" + 1":""):"0",y=n.reverse?w:h+(n.exclusive?"":" + 1");return` + ${u.registerUniform("outputSize","u32").registerUniform("axis","u32").declareVariables(a,d)} + ${u.mainStart()} + ${u.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + var inputIndices = ${d.offsetToIndices("global_idx")}; + var sum = ${d.type.value}(0); + let first : i32 = ${g}; + let last : i32 = ${y}; + for (var i : i32 = first; i < last; i++) { + ${a.indicesSet("inputIndices","uniforms.axis","u32(i)")}; + sum = sum + ${a.getByIndices("inputIndices")}; + } + ${d.setByOffset("global_idx","sum")}; + }`};return{name:"CumSum",shaderCache:{hint:n.cacheKey,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:t,dataType:e}],dispatchGroup:{x:Math.ceil(o/64)},programUniforms:[{type:12,data:o},{type:12,data:c},...V(t,t)]}),getShaderSource:m}},Ju=(e,t)=>{let r=e.inputs[0].dims,n=e.inputs[0].dataType,o=e.inputs[1];e.compute(df(n,r,o,t),{inputs:[0]})},ed=e=>{let t=e.exclusive===1,r=e.reverse===1;return ee({exclusive:t,reverse:r})}});var lf,cf,pf,rd,nd,od=U(()=>{"use strict";J();ae();Ie();se();lf=e=>{if(!e||e.length!==1)throw new Error("DepthToSpace requires 1 input.");if(e[0].dims.length!==4)throw new Error("DepthToSpace requires 4D input.")},cf=(e,t,r,n)=>{let o=[];o.push(`fn perm(i: ${n.type.indices}) -> ${r.type.indices} { + var a: ${r.type.indices};`);for(let i=0;i{let r,n,o,i,a,d,l=t.format==="NHWC",c=t.blocksize,m=t.mode==="DCR";l?([r,n,o,i]=e.dims,a=m?[r,n,o,c,c,i/c**2]:[r,n,o,i/c**2,c,c],d=m?[0,1,3,2,4,5]:[0,1,4,2,5,3]):([r,n,o,i]=[e.dims[0],e.dims[2],e.dims[3],e.dims[1]],a=m?[r,c,c,i/c**2,n,o]:[r,i/c**2,c,c,n,o],d=m?[0,3,4,1,5,2]:[0,1,4,2,5,3]);let u=e.reshape(a),h=u.dims.length,w=e.dataType,g=E("a",w,h),y=M("output",w,h),S=$=>` + ${$.registerUniform("output_size","u32").declareVariables(g,y)} + + ${cf(d,h,g,y)} + + ${$.mainStart()} + ${$.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + + let indices = ${y.offsetToIndices("global_idx")}; + let aIndices = perm(indices); + + ${y.setByOffset("global_idx",g.getByIndices("aIndices"))} + }`;return{name:"DepthToSpace",shaderCache:{hint:`${e.dims};${t.blocksize};${t.mode}`,inputDependencies:["rank"]},getRunData:$=>{let v=l?[r,n*c,o*c,i/c**2]:[r,i/c**2,n*c,o*c],x=k.size(v),T=u.dims,C=k.sortBasedOnPerm(T,d);return{outputs:[{dims:v,dataType:$[0].dataType}],dispatchGroup:{x:Math.ceil(x/64)},programUniforms:[{type:12,data:x},...V(T,C)]}},getShaderSource:S}},rd=(e,t)=>{lf(e.inputs),e.compute(pf(e.inputs[0],t))},nd=e=>ee({blocksize:e.blocksize,mode:e.mode,format:e.format})});var $o,rn,id,mf,ff,xo,So,ad,hf,sd,ud,dd=U(()=>{"use strict";J();ae();Ie();se();$o="[a-zA-Z]|\\.\\.\\.",rn="("+$o+")+",id="^"+rn+"$",mf="("+rn+",)*"+rn,ff="^"+mf+"$",xo=class{constructor(t=-1){this.symbolToIndices=new Map,this.inputIndex=t}addSymbol(t,r){let n=this.symbolToIndices.get(t);n===void 0?n=[r]:n.push(r),this.symbolToIndices.set(t,n)}},So=class{constructor(t,r){this.equation=r;this.hasEllipsis=!1,this.symbolToInfo=new Map,this.lhs=new Array,this.outputDims=[];let[n,o]=r.includes("->")?r.split("->",2):[r,""];if(!n.match(RegExp(ff)))throw new Error("Invalid LHS term");if(n.split(",").forEach((d,l)=>{let c=t[l].dims.slice();if(!d.match(RegExp(id)))throw new Error("Invalid LHS term");let m=this.processTerm(d,!0,c,l);this.lhs.push(m)}),o==="")o+=[...this.symbolToInfo.entries()].filter(([d,l])=>l.count===1||d==="...").map(([d])=>d).join("");else if(!o.match(RegExp(rn)))throw new Error("Invalid RHS");o.match(RegExp($o,"g"))?.forEach(d=>{if(d==="...")this.outputDims=this.outputDims.concat(this.ellipsisDims);else{let l=this.symbolToInfo.get(d);if(l===void 0)throw new Error("Invalid RHS symbol");this.outputDims.push(l.dimValue)}}),this.rhs=this.processTerm(o,!1,this.outputDims)}addSymbol(t,r,n){let o=this.symbolToInfo.get(t);if(o!==void 0){if(o.dimValue!==r&&o.count!==1)throw new Error("Dimension mismatch");o.count++,o.inputIndices.push(n)}else o={count:1,dimValue:r,inputIndices:[n]};this.symbolToInfo.set(t,o)}processTerm(t,r,n,o=-1){let i=n.length,a=!1,d=[],l=0;if(!t.match(RegExp(id))&&!r&&t!=="")throw new Error("Invalid LHS term");let c=t.match(RegExp($o,"g")),m=new xo(o);return c?.forEach((u,h)=>{if(u==="..."){if(a)throw new Error("Only one ellipsis is allowed per input term");a=!0;let w=i-c.length+1;if(w<0)throw new Error("Ellipsis out of bounds");if(d=n.slice(l,l+w),this.hasEllipsis){if(this.ellipsisDims.length!==d.length||this.ellipsisDims.toString()!==d.toString())throw new Error("Ellipsis dimensions mismatch")}else if(r)this.hasEllipsis=!0,this.ellipsisDims=d;else throw new Error("Ellipsis must be specified in the LHS");for(let g=0;ge+"_max",hf=(e,t,r,n)=>{let i=e.map(m=>m.length).map((m,u)=>E(`input${u}`,t,m)),a=k.size(n),d=M("output",t,n.length),l=[...r.symbolToInfo.keys()].filter(m=>!r.rhs.symbolToIndices.has(m)),c=m=>{let u=[],h="var prod = 1.0;",w="var sum = 0.0;",g="sum += prod;",y=[],S=[],$=[],v=[],x=r.symbolToInfo.size===r.rhs.symbolToIndices.size;r.symbolToInfo.forEach((C,A)=>{if(r.rhs.symbolToIndices.has(A)){let P=r.rhs.symbolToIndices.get(A)?.[0];P!==void 0&&r.lhs.forEach((D,W)=>{if(C.inputIndices.includes(W)){let N=D.symbolToIndices.get(A);if(N===void 0)throw new Error("Invalid symbol error");N.forEach(j=>{u.push(`${i[W].indicesSet(`input${W}Indices`,j,d.indicesGet("outputIndices",P))}`)})}})}else r.lhs.forEach((P,D)=>{if(C.inputIndices.includes(D)){let W=P.symbolToIndices.get(A);if(W===void 0)throw new Error("Invalid symbol error");W.forEach(N=>{y.push(`${i[D].indicesSet(`input${D}Indices`,N,`${A}`)}`)}),v.push(`prod *= ${i[D].getByIndices(`input${D}Indices`)};`)}}),S.push(`for(var ${A}: u32 = 0; ${A} < uniforms.${ad(A)}; ${A}++) {`),$.push("}")});let T=x?[...u,`let sum = ${i.map((C,A)=>C.getByIndices(`input${A}Indices`)).join(" * ")};`]:[...u,w,...S,...y,h,...v,g,...$];return` + ${m.registerUniforms(l.map(C=>({name:`${ad(C)}`,type:"u32"}))).registerUniform("outputSize","u32").declareVariables(...i,d)} + + ${m.mainStart()} + ${m.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + var outputIndices = ${d.offsetToIndices("global_idx")}; + ${i.map((C,A)=>`var input${A}Indices: ${i[A].type.indices};`).join(` +`)} + ${T.join(` +`)}; + ${d.setByOffset("global_idx","sum")}; + }`};return{name:"Einsum",shaderCache:{hint:r.equation,inputDependencies:e.map(()=>"rank")},getRunData:()=>{let m=l.filter(h=>r.symbolToInfo.has(h)).map(h=>({type:12,data:r.symbolToInfo.get(h)?.dimValue||0}));m.push({type:12,data:a});let u=e.map((h,w)=>[...V(h)]).reduce((h,w)=>h.concat(w),m);return u.push(...V(n)),{outputs:[{dims:n,dataType:t}],dispatchGroup:{x:Math.ceil(a/64)},programUniforms:u}},getShaderSource:c}},sd=(e,t)=>{let r=new So(e.inputs,t.equation),n=r.outputDims,o=e.inputs.map((i,a)=>i.dims);e.compute(hf(o,e.inputs[0].dataType,r,n))},ud=e=>{let t=e.equation.replace(/\s+/g,"");return ee({equation:t})}});var gf,ld,yf,bf,cd,pd=U(()=>{"use strict";J();ae();se();gf=e=>{if(!e||e.length!==2)throw new Error("Expand requires 2 input.");let t=e[0].dims,r=Array.from(e[1].getBigInt64Array(),Number),n=r.length{let r=e.length-t.length,n=[];for(let o=0;oe.length>t.length?ld(e,t):ld(t,e),bf=e=>{let t=e[0].dims,r=Array.from(e[1].getBigInt64Array(),Number),n=yf(t,r),o=e[0].dataType,i=o===9?4:1,a=Math.ceil(k.size(n)/i),d=c=>{let m=E("input",o,t.length,i),u=M("output",o,n.length,i),h;if(o===9){let w=(g,y,S="")=>` + let outputIndices${y} = ${u.offsetToIndices(`outputOffset + ${y}u`)}; + let offset${y} = ${m.broadcastedIndicesToOffset(`outputIndices${y}`,u)}; + let index${y} = offset${y} / 4u; + let component${y} = offset${y} % 4u; + ${g}[${y}] = ${S}(${m.getByOffset(`index${y}`)}[component${y}]); + `;h=` + let outputOffset = global_idx * ${i}; + var data = vec4(0); + ${w("data",0,"u32")} + ${w("data",1,"u32")} + ${w("data",2,"u32")} + ${w("data",3,"u32")} + ${u.setByOffset("global_idx","data")} + }`}else h=` + let outputIndices = ${u.offsetToIndices("global_idx")}; + let inputOffset = ${m.broadcastedIndicesToOffset("outputIndices",u)}; + ${u.setByOffset("global_idx",m.getByOffset("inputOffset"))} + }`;return` + ${c.registerUniform("vec_size","u32").declareVariables(m,u)} + ${c.mainStart()} + ${c.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")} + ${h}`},l=[{type:12,data:a},...V(t,n)];return{name:"Expand",shaderCache:{hint:`${n.length}`,inputDependencies:["rank"]},getShaderSource:d,getRunData:()=>({outputs:[{dims:n,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(a/64)},programUniforms:l})}},cd=e=>{gf(e.inputs),e.compute(bf(e.inputs),{inputs:[0]})}});var wf,md,fd=U(()=>{"use strict";J();ae();se();Xr();wf=e=>{let t=e[0].dataType,r=k.size(e[0].dims),n=k.size(e[1].dims),o=n%4===0,i=a=>{let d=E("x",t,[1],4),l=E("bias",t,[1],4),c=M("y",t,[1],4),m=[{name:"output_vec_size",type:"u32"},{name:"bias_size",type:"u32"}],u=w=>` + let bias${w}_offset: u32 = (global_idx * 4 + ${w}) % uniforms.bias_size; + let bias${w} = ${l.getByOffset(`bias${w}_offset / 4`)}[bias${w}_offset % 4];`,h=o?` + let bias = ${l.getByOffset("global_idx % (uniforms.bias_size / 4)")};`:`${u(0)}${u(1)}${u(2)}${u(3)} + let bias = ${d.type.value}(bias0, bias1, bias2, bias3);`;return`${a.registerUniforms(m).declareVariables(d,l,c)} + + ${po(Ee(t))} + + ${a.mainStart(At)} + ${a.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_vec_size")} + + let x = ${d.getByOffset("global_idx")}; + ${h} + let x_in = x + bias; + ${c.setByOffset("global_idx",mo("x_in"))} + }`};return{name:"FastGeluWithBias",shaderCache:{hint:`${o}`,inputDependencies:["type","type"]},getShaderSource:i,getRunData:a=>({outputs:[{dims:a[0].dims,dataType:a[0].dataType}],programUniforms:[{type:12,data:Math.ceil(r/4)},{type:12,data:n}],dispatchGroup:{x:Math.ceil(r/At/4)}})}},md=e=>{e.inputs.length<2||k.size(e.inputs[1].dims)===0?cu(e):e.compute(wf(e.inputs))}});var _f,vf,hd,gd,yd=U(()=>{"use strict";J();ae();Ie();se();_f=e=>{if(!e||e.length!==2)throw new Error("Gather requires 2 inputs.")},vf=(e,t)=>{let r=e[0].dims,n=e[1].dims,o=r.length,i=k.normalizeAxis(t.axis,o),a=r.slice(0);a.splice(i,1,...n);let d=r[i],l=e[0].dataType===9?4:1,c=Math.ceil(k.size(a)/l),m=[{type:12,data:c},{type:6,data:d},{type:12,data:i},...V(e[0].dims,e[1].dims,a)],u=h=>{let w=E("data",e[0].dataType,e[0].dims.length,l),g=E("inputIndices",e[1].dataType,e[1].dims.length),y=M("output",e[0].dataType,a.length,l),S=v=>{let x=n.length,T=`var indicesIndices${v} = ${g.type.indices}(0);`;for(let C=0;C1?`indicesIndices${v}[${C}]`:`indicesIndices${v}`} = ${a.length>1?`outputIndices${v}[uniforms.axis + ${C}]`:`outputIndices${v}`};`;T+=` + var idx${v} = ${g.getByIndices(`indicesIndices${v}`)}; + if (idx${v} < 0) { + idx${v} = idx${v} + uniforms.axisDimLimit; + } + var dataIndices${v} : ${w.type.indices}; + `;for(let C=0,A=0;C1?`dataIndices${v}[${C}]`:`dataIndices${v}`} = u32(idx${v});`,A+=x):(T+=`${o>1?`dataIndices${v}[${C}]`:`dataIndices${v}`} = ${a.length>1?`outputIndices${v}[${A}]`:`outputIndices${v}`};`,A++);return T},$;if(e[0].dataType===9){let v=(x,T,C="")=>` + let outputIndices${T} = ${y.offsetToIndices(`outputOffset + ${T}u`)}; + ${S(T)}; + let offset${T} = ${w.indicesToOffset(`dataIndices${T}`)}; + let index${T} = offset${T} / 4u; + let component${T} = offset${T} % 4u; + ${x}[${T}] = ${C}(${w.getByOffset(`index${T}`)}[component${T}]); + `;$=` + let outputOffset = global_idx * ${l}; + var value = vec4(0); + ${v("value",0,"u32")} + ${v("value",1,"u32")} + ${v("value",2,"u32")} + ${v("value",3,"u32")} + ${y.setByOffset("global_idx","value")} + `}else $=` + let outputIndices = ${y.offsetToIndices("global_idx")}; + ${S("")}; + let value = ${w.getByIndices("dataIndices")}; + ${y.setByOffset("global_idx","value")}; + `;return` + ${h.registerUniform("outputSize","u32").registerUniform("axisDimLimit","i32").registerUniform("axis","u32").declareVariables(w,g,y)} + ${h.mainStart()} + ${h.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + ${$} + }`};return{name:"Gather",shaderCache:{hint:t.cacheKey,inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:a,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(c/64)},programUniforms:m}),getShaderSource:u}},hd=e=>ee({axis:e.axis}),gd=(e,t)=>{let r=e.inputs;_f(r),e.compute(vf(e.inputs,t))}});var $f,xf,bd,wd,_d=U(()=>{"use strict";J();ae();Ie();se();$f=(e,t)=>{if(e.length<3||e.length>4)throw new Error("GatherBlockQuantized requires 3 or 4 inputs.");let r=k.normalizeAxis(t.quantizeAxis,e[0].dims.length),n=t.blockSize,o=e[0],i=e[2],a=e.length===4?e[3]:void 0;if(i.dims.length!==o.dims.length||!o.dims.map((d,l)=>l===r?Math.ceil(d/n)===i.dims[l]:d===i.dims[l]).reduce((d,l)=>d&&l,!0))throw new Error("Scales must have the same rank as the input tensor and the dims should match except on gatherAxis.");if(a){if(a.dataType!==o.dataType)throw new Error("Zero point must have the same data type as the input tensor.");if(a.dims.length!==i.dims.length||!a.dims.map((d,l)=>d===i.dims[l]).reduce((d,l)=>d&&l,!0))throw new Error("Zero point must have the same rank as the input tensor and the dims should match except on quantizeAxis.")}},xf=(e,t)=>{let r=e[0].dims,n=e[1].dims,o=r.length,i=k.normalizeAxis(t.gatherAxis,o),a=k.normalizeAxis(t.quantizeAxis,o),d=r.slice(0);d.splice(i,1,...n);let l=k.size(d),c=e[2].dataType,u=e[0].dataType===22,h=[{type:12,data:l},{type:12,data:a},{type:12,data:i},{type:12,data:t.blockSize},...V(...e.map((g,y)=>g.dims),d)],w=g=>{let y=E("data",e[0].dataType,e[0].dims.length),S=E("inputIndices",e[1].dataType,e[1].dims.length),$=E("scales",e[2].dataType,e[2].dims.length),v=e.length>3?E("zeroPoint",e[3].dataType,e[3].dims.length):void 0,x=M("output",c,d.length),T=[y,S,$];v&&T.push(v);let C=[{name:"output_size",type:"u32"},{name:"quantize_axis",type:"u32"},{name:"gather_axis",type:"u32"},{name:"block_size",type:"u32"}];return` + ${g.registerUniforms(C).declareVariables(...T,x)} + ${g.mainStart()} + let output_indices = ${x.offsetToIndices("global_idx")}; + var indices_indices = ${S.type.indices}(0); + ${(()=>n.length>1?` + for (var i: u32 = 0; i < ${n.length}; i++) { + let index = ${x.indicesGet("output_indices","uniforms.gather_axis + i")}; + ${S.indicesSet("indices_indices","i","index")}; + }`:`indices_indices = ${x.indicesGet("output_indices","uniforms.gather_axis")};`)()}; + var data_indices = ${y.type.indices}(0); + for (var i: u32 = 0; i < uniforms.gather_axis; i++) { + let index = ${x.indicesGet("output_indices","i")}; + ${y.indicesSet("data_indices","i","index")}; + } + var index_from_indices = ${S.getByIndices("indices_indices")}; + if (index_from_indices < 0) { + index_from_indices += ${r[i]}; + } + ${y.indicesSet("data_indices","uniforms.gather_axis","u32(index_from_indices)")}; + for (var i = uniforms.gather_axis + 1; i < ${d.length}; i++) { + let index = ${x.indicesGet("output_indices",`i + ${n.length} - 1`)}; + ${y.indicesSet("data_indices","i","index")}; + } + let data_offset = ${y.indicesToOffset("data_indices")}; + let data_index = data_offset % 8; + // Convert 4-bit packed data to 8-bit packed data. + let packed_4bit_quantized_data = ${y.getByOffset("data_offset / 8")}; + let packed_8bit_quantized_data = (packed_4bit_quantized_data >> (4 * (data_index % 2))) & 0x0f0f0f0f; + let quantized_data_vec = ${u?"unpack4xI8":"unpack4xU8"}(u32(packed_8bit_quantized_data)); + let quantized_data = quantized_data_vec[data_index / 2]; + var scale_indices = data_indices; + let quantize_axis_index = ${$.indicesGet("data_indices","uniforms.quantize_axis")} / uniforms.block_size; + ${$.indicesSet("scale_indices","uniforms.quantize_axis","quantize_axis_index")}; + var scale = ${$.getByIndices("scale_indices")}; + ${(()=>v?` + let zero_point_indices = scale_indices; + let zero_point_offset = ${v.indicesToOffset("zero_point_indices")}; + let zero_point_index = zero_point_offset % 8; + let packed_4bit_zero_points = ${v.getByOffset("zero_point_offset / 8")}; + let packed_8bit_zero_points = (packed_4bit_zero_points >> (4 * (zero_point_index % 2))) & 0x0f0f0f0f; + let zero_point_vec = ${u?"unpack4xI8":"unpack4xU8"}(u32(packed_8bit_zero_points)); + let zero_point = zero_point_vec[zero_point_index / 2];`:"var zero_point = 0")()}; + let dequantized_data = ${Ee(c)}(quantized_data - zero_point) * scale; + ${x.setByOffset("global_idx","dequantized_data")}; + }`};return{name:"GatherBlockQuantized",shaderCache:{hint:`${t.cacheKey};${e.filter((g,y)=>y!==1).map(g=>g.dims.join("_")).join(";")}`,inputDependencies:Array.from({length:e.length},(g,y)=>"rank")},getRunData:()=>({outputs:[{dims:d,dataType:c}],dispatchGroup:{x:Math.ceil(l/64)},programUniforms:h}),getShaderSource:w}},bd=(e,t)=>{let r=e.inputs;$f(r,t),e.compute(xf(e.inputs,t))},wd=e=>ee({blockSize:e.blockSize,gatherAxis:e.gatherAxis,quantizeAxis:e.quantizeAxis})});var Sf,Tf,vd,$d,xd=U(()=>{"use strict";J();ae();Ie();se();Sf=e=>{if(!e||e.length!==2)throw new Error("GatherElements requires 2 inputs.");if(e[0].dims.length<1)throw new Error("GatherElements requires that the data input be rank >= 1.");if(e[0].dims.length!==e[1].dims.length)throw new Error(`GatherElements requires that the data input and + indices input tensors be of same rank.`)},Tf=(e,t)=>{let r=e[0].dims,n=e[0].dataType,o=r.length,i=e[1].dims,a=e[1].dataType,d=k.normalizeAxis(t.axis,o),l=r[d],c=i.slice(0),m=k.size(c),u=E("input",n,o),h=E("indicesInput",a,i.length),w=M("output",n,c.length),g=[{type:12,data:m},{type:6,data:l},{type:12,data:d}];return g.push(...V(r,i,c)),{name:"GatherElements",shaderCache:{inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:c,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(m/64)},programUniforms:g}),getShaderSource:$=>` + ${$.registerUniform("outputSize","u32").registerUniform("axisDimLimit","i32").registerUniform("axis","u32").declareVariables(u,h,w)} + ${$.mainStart()} + ${$.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + + let outputIndices = ${w.offsetToIndices("global_idx")}; + + var idx = ${h.getByOffset("global_idx")}; + if (idx < 0) { + idx = idx + uniforms.axisDimLimit; + } + var inputIndices = ${u.type.indices}(outputIndices); + ${u.indicesSet("inputIndices","uniforms.axis","u32(idx)")}; + let value = ${u.getByIndices("inputIndices")}; + + ${w.setByOffset("global_idx","value")}; + }`}},vd=e=>ee({axis:e.axis}),$d=(e,t)=>{let r=e.inputs;Sf(r),e.compute(Tf(e.inputs,t))}});var If,Cf,Sd,Td,Id=U(()=>{"use strict";J();ae();se();If=e=>{if(!e)throw new Error("Input is missing");if(e.length<2||e.length>3)throw new Error("Invaid input number.");if(e.length===3&&e[2].dims.length>2)throw new Error("Invalid input shape of C");if(e[0].dataType!==e[1].dataType||e.length===3&&e[0].dataType!==e[2].dataType)throw new Error("Input types are mismatched")},Cf=(e,t)=>{let r=e[0].dims.slice(),n=e[1].dims.slice(),[o,i,a]=Gr.getShapeOfGemmResult(r,t.transA,n,t.transB,e.length===3?e[2].dims:void 0),d=[o,i];if(!d)throw new Error("Can't use gemm on the given tensors");let l=k.size(d),c=[{type:12,data:l},{type:12,data:o},{type:12,data:i},{type:12,data:a},{type:1,data:t.alpha},{type:1,data:t.beta}],m=["type","type"];e.length===3&&(c.push(...V(e[2].dims)),m.push("rank")),c.push(...V(d));let u=h=>{let w="";t.transA&&t.transB?w="value += a[k * uniforms.M + m] * b[n * uniforms.K + k];":t.transA&&!t.transB?w="value += a[k * uniforms.M + m] * b[k * uniforms.N + n];":!t.transA&&t.transB?w="value += a[m * uniforms.K + k] * b[n * uniforms.K + k];":!t.transA&&!t.transB&&(w="value += a[m * uniforms.K + k] * b[k * uniforms.N + n];");let g=t.alpha===1?"":"value *= uniforms.alpha;",y=E("a",e[0].dataType,e[0].dims),S=E("b",e[1].dataType,e[1].dims),$=y.type.value,v=null,x=[y,S];e.length===3&&(v=E("c",e[2].dataType,e[2].dims.length),x.push(v));let T=M("output",e[0].dataType,d.length);x.push(T);let C=[{name:"output_size",type:"u32"},{name:"M",type:"u32"},{name:"N",type:"u32"},{name:"K",type:"u32"},{name:"alpha",type:"f32"},{name:"beta",type:"f32"}];return` + ${h.registerUniforms(C).declareVariables(...x)} + + ${h.mainStart()} + ${h.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + + let m = global_idx / uniforms.N; + let n = global_idx % uniforms.N; + + var value = ${$}(0); + for (var k: u32 = 0u; k < uniforms.K; k++) { + ${w} + } + + ${g} + ${(()=>v!=null?`let cOffset = ${v.broadcastedIndicesToOffset("vec2(m, n)",T)}; value += ${$}(uniforms.beta) * ${v.getByOffset("cOffset")};`:"")()} + output[global_idx] = value; + }`};return{name:"Gemm",shaderCache:{hint:`${t.cacheKey}`,inputDependencies:m},getRunData:()=>({outputs:[{dims:d,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(l/64)},programUniforms:c}),getShaderSource:u}},Sd=e=>{let t=e.transA,r=e.transB,n=e.alpha,o=e.beta;return{transA:t,transB:r,alpha:n,beta:o,cacheKey:`${e.transA};${e.transB};${e.alpha===1}`}},Td=(e,t)=>{If(e.inputs),e.compute(Cf(e.inputs,t))}});var We,Ef,Ad,Cd,Pf,nr,kd,To=U(()=>{"use strict";J();ae();Ie();Hr();Kr();se();lt();We=(e,t)=>e.length>t&&e[t].dims.length>0?e[t]:void 0,Ef=(e,t)=>{let r=e[0],n=We(e,1),o=We(e,2),i=We(e,3),a=We(e,4),d=We(e,5),l=We(e,6),c=We(e,7);if(r.dims.length!==3&&r.dims.length!==5)throw new Error("Input query is expected to have 3 or 5 dimensions");let m=r.dims[0],u=r.dims[1],h=r.dims.length===3?r.dims[2]:t.numHeads*r.dims[4],w=u,g=0,y=0,S=Math.floor(h/t.numHeads);if(l&&c&&k.size(l.dims)&&k.size(c.dims)){if(l.dims.length!==4)throw new Error('Input "past_key" is expected to have 4 dimensions');if(l.dims[0]!==m||l.dims[1]!==t.numHeads||l.dims[3]!==S)throw new Error('Input "past_key" shape (batch_size, num_heads, past_sequence_length, head_size)');if(c.dims[0]!==m||c.dims[1]!==t.numHeads||c.dims[3]!==S)throw new Error('Input "past_value" shape (batch_size, num_heads, past_sequence_length, head_size)');if(l.dims[2]!==c.dims[2])throw new Error('Input "past_key" and "past_value" shall have same dim 2 (past_sequence_length)');if(c.dims.length!==4)throw new Error('Input "past_value" is expected to have 4 dimensions');g=l.dims[2],y=l.dims[2]}else if(l&&k.size(l.dims)||c&&k.size(c.dims))throw new Error('Input "past_key" and "past_value" shall be both present or both absent');let $;if(n&&k.size(n.dims)>0){if(r.dims.length!==3)throw new Error('Input "query" is expected to have 3 dimensions when key is given');if(n.dims.length<3||n.dims.length>5)throw new Error('Input "key" is expected to have 3, 4, or 5 dimensions');if(r.dims[0]!==n.dims[0])throw new Error('Input "query" and "key" shall have same dim 0 (batch size)');if(n.dims.length===3){if(n.dims[2]!==r.dims[2])throw new Error('Input "query" and "key" shall have same dim 2 (hidden_size)');$=2,w=n.dims[1]}else if(n.dims.length===5){if(n.dims[2]!==t.numHeads||n.dims[3]!==2||n.dims[4]!==S)throw new Error('Expect "key" shape (batch_size, kv_sequence_length, num_heads, 2, head_size) for packed kv');if(o)throw new Error('Expect "value" be none when "key" has packed kv format.');$=5,w=n.dims[1]}else{if(n.dims[1]!==t.numHeads||n.dims[3]!==S)throw new Error('Expect "key" shape (batch_size, num_heads, kv_sequence_length, head_size) for past_key');$=0,w=n.dims[2]}}else{if(r.dims.length!==5)throw new Error('Input "query" is expected to have 5 dimensions when key is empty');if(r.dims[2]!==t.numHeads||r.dims[3]!==3)throw new Error('Expect "query" shape (batch_size, kv_sequence_length, num_heads, 3, head_size) for packed kv');$=3}if(i&&k.size(i.dims)>0){if(i.dims.length!==1)throw new Error('Input "bias" is expected to have 1 dimension');if(n&&n.dims.length===5&&n.dims[3]===2)throw new Error("bias is not allowed for packed kv.")}let v=g+w,x=0;if(a&&k.size(a.dims)>0){x=8;let P=a.dims;throw P.length===1?P[0]===m?x=1:P[0]===3*m+2&&(x=3):P.length===2&&P[0]===m&&P[1]===v&&(x=5),x===8?new Error('Input "key_padding_mask" shape shall be (batch_size) or (batch_size, total_sequence_length)'):new Error("Mask not supported")}let T=!1,C=h;if(o&&k.size(o.dims)>0){if(o.dims.length!==3&&o.dims.length!==4)throw new Error('Input "value" is expected to have 3 or 4 dimensions');if(r.dims[0]!==o.dims[0])throw new Error('Input "query" and "value" shall have same dim 0 (batch_size)');if(o.dims.length===3){if(w!==o.dims[1])throw new Error('Input "key" and "value" shall have the same dim 1 (kv_sequence_length)');C=o.dims[2]}else{if(w!==o.dims[2])throw new Error('Input "key" and "value" shall have the same dim 2 (kv_sequence_length)');C=o.dims[1]*o.dims[3],T=!0}}let A=!1;if(a&&k.size(a.dims)>0)throw new Error("Key padding mask is not supported");if(d&&k.size(d.dims)>0){if(d.dims.length!==4)throw new Error('Input "attention_bias" is expected to have 4 dimensions');if(d.dims[0]!==m||d.dims[1]!==t.numHeads||d.dims[2]!==u||d.dims[3]!==v)throw new Error('Expect "attention_bias" shape (batch_size, num_heads, sequence_length, total_sequence_length)')}return{batchSize:m,sequenceLength:u,pastSequenceLength:g,kvSequenceLength:w,totalSequenceLength:v,maxSequenceLength:y,inputHiddenSize:0,hiddenSize:h,vHiddenSize:C,headSize:S,vHeadSize:Math.floor(C/t.numHeads),numHeads:t.numHeads,isUnidirectional:!1,pastPresentShareBuffer:!1,maskFilterValue:t.maskFilterValue,maskType:x,scale:t.scale,broadcastResPosBias:A,passPastInKv:T,qkvFormat:$}},Ad=e=>ee({...e}),Cd=ee({perm:[0,2,1,3]}),Pf=(e,t,r,n,o,i,a)=>{let d=[n,o,i],l=k.size(d),c=[{type:12,data:l},{type:12,data:a},{type:12,data:i}],m=u=>{let h=M("qkv_with_bias",t.dataType,d),w=E("qkv",t.dataType,d),g=E("bias",r.dataType,d),y=[{name:"output_size",type:"u32"},{name:"bias_offset",type:"u32"},{name:"hidden_size",type:"u32"}];return` + ${u.registerUniforms(y).declareVariables(w,g,h)} + ${u.mainStart()} + ${u.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + let bias_offset_idx = (global_idx % uniforms.hidden_size) + uniforms.bias_offset; + + qkv_with_bias[global_idx] = qkv[global_idx] + bias[bias_offset_idx]; + }`};return e.compute({name:"MultiHeadAttentionAddBias",shaderCache:{inputDependencies:["type","type"]},getRunData:()=>({outputs:[{dims:d,dataType:t.dataType,gpuDataType:0}],dispatchGroup:{x:Math.ceil(l/64)},programUniforms:c}),getShaderSource:m},{inputs:[t,r],outputs:[-1]})[0]},nr=(e,t,r,n,o,i,a,d)=>{let l=i;if(a&&k.size(a.dims)>0){if(n===1)throw new Error("AddBiasReshape is not implemented. Please export your model with packed QKV or KV");return l=Pf(e,i,a,t,n,r*o,d),l=l.reshape([t,n,r,o]),r===1||n===1?l:e.compute(Pe(l,Cd.perm),{inputs:[l],outputs:[-1]})[0]}else return i.dims.length===3&&(l=i.reshape([t,n,r,o])),r===1||n===1?l:e.compute(Pe(l,Cd.perm),{inputs:[l],outputs:[-1]})[0]},kd=(e,t)=>{let r=Ef(e.inputs,t),n=e.inputs[0],o=We(e.inputs,1),i=We(e.inputs,2),a=We(e.inputs,3),d=We(e.inputs,4),l=We(e.inputs,5),c=We(e.inputs,6),m=We(e.inputs,7);if(n.dims.length===5)throw new Error("Packed QKV is not implemented");if(o?.dims.length===5)throw new Error("Packed KV is not implemented");let u=o&&i&&o.dims.length===4&&i.dims.length===4,h=nr(e,r.batchSize,r.numHeads,r.sequenceLength,r.headSize,n,a,0);if(u)return Nt(e,h,o,i,d,void 0,c,m,l,r);if(!o||!i)throw new Error("key and value must be provided");let w=nr(e,r.batchSize,r.numHeads,r.kvSequenceLength,r.headSize,o,a,r.hiddenSize),g=nr(e,r.batchSize,r.numHeads,r.kvSequenceLength,r.vHeadSize,i,a,2*r.hiddenSize);Nt(e,h,w,g,d,void 0,c,m,l,r)}});var zf,Of,Df,Bf,Io,Ed,Pd,Co=U(()=>{"use strict";J();ae();Ie();se();zf=e=>{if(!e||e.length<1)throw new Error("too few inputs")},Of=(e,t)=>{let r=[],n=t.numOutputs;return e[1].dims[0]>0&&(e[1].getBigInt64Array().forEach(o=>r.push(Number(o))),n=r.length),ee({numOutputs:n,axis:t.axis,splitSizes:r})},Df=e=>` +fn calculateOutputIndex(index: u32) -> u32 { + for (var i: u32 = 0u; i < ${e}u; i += 1u ) { + if (index < ${F("uniforms.size_in_split_axis","i",e)}) { + return i; + } + } + return ${e}u; +}`,Bf=e=>{let t=e.length,r=[];for(let n=0;n{let r=e[0].dims,n=k.size(r),o=e[0].dataType,i=k.normalizeAxis(t.axis,r.length),a=new Array(t.numOutputs),d=E("input",o,r.length),l=new Array(t.numOutputs),c=[],m=[],u=0,h=[{type:12,data:n}];for(let g=0;g` + ${g.registerUniform("input_size","u32").registerUniform("size_in_split_axis","u32",l.length).declareVariables(d,...a)} + ${Df(l.length)} + ${Bf(a)} + + ${g.mainStart()} + ${g.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.input_size")} + + var indices = ${d.offsetToIndices("global_idx")}; + var index = ${d.indicesGet("indices",i)}; + let output_number = calculateOutputIndex(index); + if (output_number != 0) { + index -= ${F("uniforms.size_in_split_axis","output_number - 1u",l.length)}; + ${d.indicesSet("indices",i,"index")}; + } + writeBufferData(output_number, indices, global_idx); + }`;return{name:"Split",shaderCache:{hint:t.cacheKey,inputDependencies:["rank"]},getShaderSource:w,getRunData:()=>({outputs:c,dispatchGroup:{x:Math.ceil(n/64)},programUniforms:h})}},Ed=(e,t)=>{zf(e.inputs);let r=e.inputs.length===1?t:Of(e.inputs,t);e.compute(Io(e.inputs,r),{inputs:[0]})},Pd=e=>{let t=e.axis,r=e.splitSizes,n=e.numOutputs<0?r.length:e.numOutputs;if(n!==r.length)throw new Error("numOutputs and splitSizes lengh must be equal");return ee({axis:t,numOutputs:n,splitSizes:r})}});var Mf,Rf,zd,Od,Dd=U(()=>{"use strict";Ie();Kr();To();Co();lt();Mf=(e,t)=>{if(t.doRotary&&e.length<=7)throw new Error("cos_cache and sin_cache inputs are required if do_rotary is specified");let r=e[0],n=e[1],o=e[2],i=e[3],a=e[4];if(t.localWindowSize!==-1)throw new Error("Local attention is not supported");if(t.softcap!==0)throw new Error("Softcap is not supported");if(t.rotaryInterleaved!==0)throw new Error("Rotary interleaved is not supported");if(t.smoothSoftmax)throw new Error("Smooth softmax is not supported");if(r.dims.length!==3&&r.dims.length!==5)throw new Error("Input query is expected to have 3 or 5 dimensions");let d=!1,l=r.dims[0],c=r.dims[1],m=r.dims.length===3?d?r.dims[2]/3:r.dims[2]:t.numHeads*r.dims[4],u=c,h=0,w=!n||n.dims.length===0,g=Math.floor(w?m/(t.numHeads+2*t.kvNumHeads):m/t.numHeads);w&&(m=g*t.numHeads);let y=i&&i.dims.length!==0,S=a&&a.dims.length!==0;if(y&&i.dims.length===4&&i.dims[0]===l&&i.dims[1]!==t.kvNumHeads&&i.dims[2]===t.kvNumHeads&&i.dims[3]===g)throw new Error("BSNH pastKey/pastValue is not supported");if(y&&S){if(i.dims.length!==4)throw new Error('Input "past_key" is expected to have 4 dimensions');if(a.dims.length!==4)throw new Error('Input "past_value" is expected to have 4 dimensions');h=i.dims[2]}else if(y||S)throw new Error('Input "past_key" and "past_value" shall be both present or both absent');let v=1;if(n&&n.dims.length>0){if(r.dims.length!==3)throw new Error('Input "query" is expected to have 3 dimensions when key is given');if(n.dims.length<3||n.dims.length>5)throw new Error('Input "key" is expected to have 3, 4, or 5 dimensions');if(r.dims[0]!==n.dims[0])throw new Error('Input "query" and "key" shall have same dim 0 (batch size)');if(n.dims.length===3){if(r.dims[2]%n.dims[2]!==0)throw new Error('Dimension 2 of "query" should be a multiple of "key"');u=n.dims[1]}else if(n.dims.length===5){if(n.dims[2]!==t.numHeads||n.dims[3]!==2||n.dims[4]!==g)throw new Error('Expect "key" shape (batch_size, kv_sequence_length, num_heads, 2, head_size) for packed kv');if(o)throw new Error('Expect "value" be none when "key" has packed kv format.');u=n.dims[1]}else{if(n.dims[1]!==t.numHeads||n.dims[3]!==g)throw new Error('Expect "key" shape (batch_size, num_heads, kv_sequence_length, head_size) for past_key');u=n.dims[2]}}else{if(r.dims.length!==3&&r.dims.length!==5)throw new Error('Input "query" is expected to have 3 or 5 dimensions when key is empty');if(r.dims.length===5&&(r.dims[2]!==t.numHeads||r.dims[3]!==3))throw new Error('Expect "query" shape (batch_size, kv_sequence_length, num_heads, 3, head_size) for packed kv');v=3}let x=0,T=!1,C=t.kvNumHeads?g*t.kvNumHeads:m;if(o&&o.dims.length>0){if(o.dims.length!==3&&o.dims.length!==4)throw new Error('Input "value" is expected to have 3 or 4 dimensions');if(r.dims[0]!==o.dims[0])throw new Error('Input "query" and "value" shall have same dim 0 (batch_size)');if(o.dims.length===3){if(u!==o.dims[1])throw new Error('Input "key" and "value" shall have the same dim 1 (kv_sequence_length)');C=o.dims[2]}else{if(u!==o.dims[2])throw new Error('Input "past_key" and "past_value" shall have the same dim 2 (kv_sequence_length)');C=o.dims[1]*o.dims[3],T=!0}}let A=e.length>4?e[5]:void 0;if(A&&A.dims.length!==1&&A.dims[0]!==l)throw new Error('Input "seqlens" is expected to have 1 dimension and the same dim 0 as batch_size');let P=-1,D=-1,W=!1;return{batchSize:l,sequenceLength:c,pastSequenceLength:h,kvSequenceLength:u,totalSequenceLength:P,maxSequenceLength:D,inputHiddenSize:0,hiddenSize:m,vHiddenSize:C,headSize:g,vHeadSize:Math.floor(C/t.kvNumHeads),numHeads:t.numHeads,kvNumHeads:t.kvNumHeads,nReps:t.numHeads/t.kvNumHeads,pastPresentShareBuffer:!1,maskType:x,scale:t.scale,broadcastResPosBias:W,passPastInKv:T,qkvFormat:v}},Rf=ee({perm:[0,2,1,3]}),zd=(e,t,r)=>{let n=t,o=r.kvNumHeads;return t.dims.length===3&&r.kvSequenceLength!==0&&(n=t.reshape([r.batchSize,r.kvSequenceLength,o,r.headSize]),n=e.compute(Pe(n,Rf.perm),{inputs:[n],outputs:[-1]})[0]),n},Od=(e,t)=>{let r=Mf(e.inputs,t);if(e.inputs[0].dims.length===5)throw new Error("Packed QKV is not implemented");if(e.inputs[1]?.dims.length===5)throw new Error("Packed KV is not implemented");let n=e.inputs[0],o=e.inputs[1]&&e.inputs[1].dims.length>0?e.inputs[1]:void 0,i=e.inputs[2]&&e.inputs[2].dims.length>0?e.inputs[2]:void 0,a=e.inputs[3]&&e.inputs[3].dims.length!==0?e.inputs[3]:void 0,d=e.inputs[4]&&e.inputs[4].dims.length!==0?e.inputs[4]:void 0,l=e.inputs.length>4?e.inputs[5]:void 0,c=e.inputs.length>5?e.inputs[6]:void 0,m=r.kvNumHeads?r.kvNumHeads:r.numHeads,u=ee({axis:2,numOutputs:3,splitSizes:[r.numHeads*r.headSize,m*r.headSize,m*r.headSize]}),[h,w,g]=!o&&!i?e.compute(Io([n],u),{inputs:[n],outputs:[-1,-1,-1]}):[n,o,i],y=nr(e,r.batchSize,r.numHeads,r.sequenceLength,r.headSize,h,void 0,0);Nt(e,y,zd(e,w,r),zd(e,g,r),void 0,void 0,a,d,void 0,r,l,c)}});var Bd,Uf,Vf,Md,Rd=U(()=>{"use strict";J();ae();lt();se();Bd=(e,t,r,n,o,i,a,d)=>{let l=we(i),c=l===1?"f32":`vec${l}f`,m=l===1?"vec2f":`mat2x${l}f`,u=o*a,h=[o,a,i/l],w=[o,a,2],g=["rank","type","type"],y=[];y.push(...V(h,w));let S=$=>{let v=E("x",t.dataType,3,l),x=E("scale",r.dataType,r.dims),T=E("bias",n.dataType,n.dims),C=M("output",1,3,2),A=[v,x,T,C],P=64;return` + var workgroup_shared : array<${m}, ${P}>; + const workgroup_size = ${P}u; + ${$.declareVariables(...A)} + ${$.mainStart(P)} + let batch = workgroup_index / uniforms.x_shape[1]; + let channel = workgroup_index % uniforms.x_shape[1]; + let hight = uniforms.x_shape[2]; + // initialize workgroup memory + var sum = ${c}(0); + var squared_sum = ${c}(0); + for (var h = local_idx; h < hight; h += workgroup_size) { + let value = ${c}(${v.get("batch","channel","h")}); + sum += value; + squared_sum += value * value; + } + workgroup_shared[local_idx] = ${m}(sum, squared_sum); + workgroupBarrier(); + + for (var currSize = workgroup_size >> 1; currSize > 0; currSize = currSize >> 1) { + if (local_idx < currSize) { + workgroup_shared[local_idx] = workgroup_shared[local_idx] + workgroup_shared[local_idx + currSize]; + } + workgroupBarrier(); + } + if (local_idx == 0) { + let sum_final = ${Qe("workgroup_shared[0][0]",l)} / f32(hight * ${l}); + let squared_sum_final = ${Qe("workgroup_shared[0][1]",l)} / f32(hight * ${l}); + + let inv_std_dev = inverseSqrt(squared_sum_final - sum_final * sum_final + f32(${d})); + let channel_scale = inv_std_dev * f32(scale[channel]); + let channel_shift = f32(bias[channel]) - sum_final * channel_scale; + output[workgroup_index] = vec2f(channel_scale, channel_shift); + } + }`};return e.compute({name:"InstanceNormComputeChannelScaleShift",shaderCache:{hint:`${l};${d}`,inputDependencies:g},getRunData:()=>({outputs:[{dims:w,dataType:1}],dispatchGroup:{x:u},programUniforms:y}),getShaderSource:S},{inputs:[t,r,n],outputs:[-1]})[0]},Uf=(e,t,r)=>{let n=t[0].dims,o=n,i=2,a=n[0],d=n[1],l=k.sizeFromDimension(n,i),c=we(l),m=k.size(o)/c,u=Bd(e,t[0],t[1],t[2],a,l,d,r.epsilon),h=[a,d,l/c],w=[a,d],g=["type","none"],y=S=>{let $=E("x",t[0].dataType,h.length,c),v=E("scale_shift",1,w.length,2),x=M("output",t[0].dataType,h.length,c),T=[$,v,x];return` + ${S.registerUniform("output_size","u32").declareVariables(...T)} + ${S.mainStart()} + ${S.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + let outputIndices = ${x.offsetToIndices("global_idx")}; + let batch = outputIndices[0]; + let channel = outputIndices[1]; + let scale_shift = ${v.getByIndices("vec2(batch, channel)")}; + let value = ${$.getByOffset("global_idx")} * ${x.type.value}(scale_shift.x) + ${x.type.value}(scale_shift.y); + ${x.setByOffset("global_idx","value")}; + }`};e.compute({name:"InstanceNormalization",shaderCache:{hint:`${c}`,inputDependencies:g},getRunData:()=>({outputs:[{dims:o,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(m/64)},programUniforms:[{type:12,data:m},...V(h,w,h)]}),getShaderSource:y},{inputs:[t[0],u]})},Vf=(e,t,r)=>{let n=t[0].dims,o=n,i=n[0],a=n[n.length-1],d=k.sizeFromDimension(n,1)/a,l=we(a),c=k.size(o)/l,m=[{type:12,data:d},{type:12,data:Math.floor(a/l)}],u=["type","type"],h=[0,n.length-1];for(let S=0;S{let $=he(t[0].dataType),v=l===1?"vec2f":`mat${l}x2f`,x=A=>{let P=A===0?"x":"y",D=l===1?"f32":`vec${l}f`;switch(l){case 1:return`${$}(${D}(scale.${P}))`;case 2:return`vec2<${$}>(${D}(scale[0].${P}, scale[1].${P}))`;case 4:return`vec4<${$}>(${D}(scale[0].${P}, scale[1].${P}, scale[2].${P}, scale[3].${P}))`;default:throw new Error(`Not supported compoents ${l}`)}},T=E("input",t[0].dataType,t[0].dims,l),C=M("output",t[0].dataType,o,l);return` + @group(0) @binding(0) var input : array<${T.type.storage}>; + @group(0) @binding(1) var scale_input : array<${v}>; + @group(0) @binding(2) var output : array<${C.type.storage}>; + struct Uniforms {H: u32, C : u32}; + @group(0) @binding(3) var uniforms: Uniforms; + + ${S.mainStart()} + let current_image_number = global_idx / (uniforms.C * uniforms.H); + let current_channel_number = global_idx % uniforms.C; + + let scale_offset = current_image_number * uniforms.C + current_channel_number; + let scale = scale_input[scale_offset]; + output[global_idx] = fma(input[global_idx], ${x(0)}, ${x(1)}); + }`};e.compute({name:"InstanceNormalizationNHWC",shaderCache:{hint:`${l}`,inputDependencies:u},getRunData:()=>({outputs:[{dims:o,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(c/64)},programUniforms:m}),getShaderSource:y},{inputs:[t[0],g]})},Md=(e,t)=>{t.format==="NHWC"?Vf(e,e.inputs,t):Uf(e,e.inputs,t)}});var Wf,Nf,Ud,Vd=U(()=>{"use strict";J();ae();se();Wf=e=>{if(!e||e.length<2)throw new Error("layerNorm requires at least 2 inputs.")},Nf=(e,t,r)=>{let n=t.simplified,o=e[0].dims,i=e[1],a=!n&&e[2],d=o,l=k.normalizeAxis(t.axis,o.length),c=k.sizeToDimension(o,l),m=k.sizeFromDimension(o,l),u=k.size(i.dims),h=a?k.size(a.dims):0;if(u!==m||a&&h!==m)throw new Error(`Size of X.shape()[axis:] == ${m}. + Size of scale and bias (if provided) must match this. + Got scale size of ${u} and bias size of ${h}`);let w=[];for(let C=0;C1,v=r>2,x=C=>{let A=he(e[0].dataType),P=[E("x",e[0].dataType,e[0].dims,g),E("scale",i.dataType,i.dims,g)];a&&P.push(E("bias",a.dataType,a.dims,g)),P.push(M("output",e[0].dataType,d,g)),$&&P.push(M("mean_data_output",1,w)),v&&P.push(M("inv_std_output",1,w));let D=[{name:"norm_count",type:"u32"},{name:"norm_size",type:"f32"},{name:"norm_size_vectorized",type:"u32"},{name:"epsilon",type:"f32"}];return` + ${C.registerUniforms(D).declareVariables(...P)} + ${C.mainStart()} + ${C.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.norm_count")} + let offset = global_idx * uniforms.norm_size_vectorized; + var mean_vector = ${ao("f32",g)}; + var mean_square_vector = ${ao("f32",g)}; + + for (var h: u32 = 0u; h < uniforms.norm_size_vectorized; h++) { + let value = ${kt(A,g,"x[h + offset]")}; + mean_vector += value; + mean_square_vector += value * value; + } + let mean = ${Qe("mean_vector",g)} / uniforms.norm_size; + let inv_std_dev = inverseSqrt(${Qe("mean_square_vector",g)} / uniforms.norm_size ${n?"":"- mean * mean"} + uniforms.epsilon); + + for (var j: u32 = 0; j < uniforms.norm_size_vectorized; j++) { + let f32input = ${kt(A,g,"x[j + offset]")}; + let f32scale = ${kt(A,g,"scale[j]")}; + output[j + offset] = ${P[0].type.value}((f32input ${n?"":"- mean"}) * inv_std_dev * f32scale + ${a?`+ ${kt(A,g,"bias[j]")}`:""} + ); + } + + ${$?"mean_data_output[global_idx] = mean":""}; + ${v?"inv_std_output[global_idx] = inv_std_dev":""}; + }`},T=[{dims:d,dataType:e[0].dataType}];return $&&T.push({dims:w,dataType:1}),v&&T.push({dims:w,dataType:1}),{name:"LayerNormalization",shaderCache:{hint:`${g};${r};${n}`,inputDependencies:y},getRunData:()=>({outputs:T,dispatchGroup:{x:Math.ceil(c/64)},programUniforms:S}),getShaderSource:x}},Ud=(e,t)=>{Wf(e.inputs),e.compute(Nf(e.inputs,t,e.outputCount))}});var Lf,Hf,Gf,Wd,Nd,Ld=U(()=>{"use strict";J();ae();Ie();se();Lf=(e,t)=>{if(e.length<3||e.length>4)throw new Error("MatMulNBits requires 3 or 4 inputs");let r=e[0],n=r.dims.length;if(r.dims[n-1]!==t.k)throw new Error("The last dim of input shape does not match the k value");let o=Math.floor((t.k+t.blockSize-1)/t.blockSize),i=t.blockSize/8*t.bits,a=e[1];if(!k.areEqual(a.dims,[t.n,o,i]))throw new Error("The second inputs must be 3D tensor with shape N X nBlocksPerCol X blobSize");let l=e[2].dims;if(k.size(l)!==t.n*o)throw new Error("scales input size error.");if(e.length===4){let m=e[3].dims,u=t.bits>4?t.n*o:t.n*Math.floor((o+1)/2);if(k.size(m)!==u)throw new Error("zeroPoints input size error.")}},Hf=(e,t)=>{let r=e[0].dims,n=r.length,o=r[n-2],i=t.k,a=t.n,d=r.slice(0,n-2),l=k.size(d),m=e[1].dims[2]/4,u=e[0].dataType,h=we(t.k),w=we(m),g=we(a),y=d.concat([o,a]),S=o>1&&a/g%2===0?2:1,$=k.size(y)/g/S,v=64,x=[],T=[l,o,i/h],C=k.convertShape(e[1].dims).slice();C.splice(-1,1,m/w),x.push(...V(T)),x.push(...V(C)),x.push(...V(e[2].dims)),e.length===4&&x.push(...V(k.convertShape(e[3].dims)));let A=[l,o,a/g];x.push(...V(A));let P=D=>{let W=T.length,N=E("a",e[0].dataType,W,h),j=E("b",12,C.length,w),Y=E("scales",e[2].dataType,e[2].dims.length),Z=[N,j,Y],te=e.length===4?E("zero_points",12,e[3].dims.length):void 0;te&&Z.push(te);let ue=A.length,K=M("output",e[0].dataType,ue,g),de=he(e[0].dataType),ce=(()=>{switch(h){case 1:return`array<${de}, 8>`;case 2:return`mat4x2<${de}>`;case 4:return`mat2x4<${de}>`;default:throw new Error(`${h}-component is not supported.`)}})(),q=()=>{let ne=` + // reuse a data + var input_offset = ${N.indicesToOffset(`${N.type.indices}(batch, row, word_offset)`)}; + var a_data: ${ce}; + for (var j: u32 = 0; j < ${8/h}; j++) { + a_data[j] = ${N.getByOffset("input_offset")}; + input_offset++; + } + `;for(let oe=0;oe> 4) & b_mask); + b_quantized_values = ${ce}(${Array.from({length:4},(R,G)=>`${de}(b_value_lower[${G}]), ${de}(b_value_upper[${G}])`).join(", ")}); + b_dequantized_values = ${(()=>h===1?`${ce}(${Array.from({length:8},(R,G)=>`(b_quantized_values[${G}] - ${te?`zero_point${oe}`:"zero_point"}) * scale${oe}`).join(", ")});`:`(b_quantized_values - ${ce}(${Array(8).fill(`${te?`zero_point${oe}`:"zero_point"}`).join(",")})) * scale${oe};`)()}; + workgroup_shared[local_id.x * ${S} + ${Math.floor(oe/g)}]${g>1?`[${oe%g}]`:""} += ${Array.from({length:8/h},(R,G)=>`${h===1?`a_data[${G}] * b_dequantized_values[${G}]`:`dot(a_data[${G}], b_dequantized_values[${G}])`}`).join(" + ")}; + `;return ne},le=()=>{let ne=` + var col_index = col * ${g}; + ${te?` + let zero_point_bytes_per_col = (nBlocksPerCol + 1) / 2; + var zero_point_byte_count: u32; + var zero_point_word_index: u32; + var zero_point_byte_offset: u32; + let zero_point_nibble_offset: u32 = block & 0x1u; + var zero_point_bits_offset: u32; + var zero_point_word: u32;`:` + // The default zero point is 8 for unsigned 4-bit quantization. + let zero_point = ${de}(8);`} + `;for(let oe=0;oe> 0x1u); + zero_point_word_index = zero_point_byte_count >> 0x2u; + zero_point_byte_offset = zero_point_byte_count & 0x3u; + zero_point_bits_offset = (zero_point_byte_offset << 3) + (zero_point_nibble_offset << 2); + zero_point_word = ${te.getByOffset("zero_point_word_index")} >> zero_point_bits_offset; + let zero_point${oe} = ${de}((zero_point_word) & 0xFu);`:""} + col_index += 1;`;return ne},re=()=>{let ne=`col_index = col * ${g};`;for(let oe=0;oe; + var b_value_upper: vec4; + var b_quantized_values: ${ce}; + var b_dequantized_values: ${ce};`,ne};return` + var workgroup_shared: array<${K.type.value}, ${S*v}>; + ${D.declareVariables(...Z,K)} + ${D.mainStart([v,1,1])} + let output_indices = ${K.offsetToIndices(`(global_idx / ${v}) * ${S}`)}; + let col = output_indices[2]; + let row = output_indices[1]; + let batch = output_indices[0]; + let nBlocksPerCol = uniforms.b_shape[1]; + + for (var block = local_id.x; block < nBlocksPerCol; block += ${v}) { + //process one block + var word_offset: u32 = block * ${t.blockSize/h}; + ${le()} + for (var word: u32 = 0; word < ${m}; word += ${w}) { + ${re()} + for (var i: u32 = 0; i < ${w}; i++) { + ${q()} + word_offset += ${8/h}; + } + } + } + workgroupBarrier(); + + if (local_id.x < ${S}) { + var output_value: ${K.type.value} = ${K.type.value}(0); + var workgroup_shared_offset: u32 = local_id.x; + for (var b: u32 = 0u; b < ${v}u; b++) { + output_value += workgroup_shared[workgroup_shared_offset]; + workgroup_shared_offset += ${S}; + } + ${K.setByIndices(`${K.type.indices}(batch, row, col + local_id.x)`,"output_value")}; + } + }`};return{name:"MatMulNBits",shaderCache:{hint:`${t.blockSize};${t.bits};${h};${w};${g};${S};${v}`,inputDependencies:Array(e.length).fill("rank")},getRunData:()=>({outputs:[{dims:y,dataType:u}],dispatchGroup:{x:$},programUniforms:x}),getShaderSource:P}},Gf=(e,t)=>{let r=e[0].dims,n=r.length,o=r[n-2],i=t.k,a=t.n,d=r.slice(0,n-2),l=k.size(d),m=e[1].dims[2]/4,u=e[0].dataType,h=we(t.k),w=we(m),g=d.concat([o,a]),y=128,S=a%8===0?8:a%4===0?4:1,$=y/S,v=$*w*8,x=v/h,T=v/t.blockSize,C=k.size(g)/S,A=[],P=[l,o,i/h],D=k.convertShape(e[1].dims).slice();D.splice(-1,1,m/w),A.push(...V(P)),A.push(...V(D)),A.push(...V(e[2].dims)),e.length===4&&A.push(...V(k.convertShape(e[3].dims)));let W=[l,o,a];A.push(...V(W));let N=j=>{let Y=P.length,Z=E("a",e[0].dataType,Y,h),te=E("b",12,D.length,w),ue=E("scales",e[2].dataType,e[2].dims.length),K=[Z,te,ue],de=e.length===4?E("zero_points",12,e[3].dims.length):void 0;de&&K.push(de);let ce=W.length,q=M("output",e[0].dataType,ce),le=he(e[0].dataType),re=()=>{switch(h){case 1:return` + let a_data0 = vec4<${le}>(sub_a[word_offset], sub_a[word_offset + 1], sub_a[word_offset + 2], sub_a[word_offset + 3]); + let a_data1 = vec4<${le}>(sub_a[word_offset + 4], sub_a[word_offset + 5], sub_a[word_offset + 6], sub_a[word_offset + 7]);`;case 2:return` + let a_data0 = vec4<${le}>(sub_a[word_offset], sub_a[word_offset + 1]); + let a_data1 = vec4<${le}>(sub_a[word_offset + 2], sub_a[word_offset + 3]);`;case 4:return` + let a_data0 = sub_a[word_offset]; + let a_data1 = sub_a[word_offset + 1];`;default:throw new Error(`${h}-component is not supported.`)}};return` + var sub_a: array<${Z.type.value}, ${x}>; + var inter_results: array, ${S}>; + ${j.declareVariables(...K,q)} + ${j.mainStart([$,S,1])} + let output_indices = ${q.offsetToIndices(`workgroup_index * ${S}`)}; + let col = output_indices[2]; + let row = output_indices[1]; + let batch = output_indices[0]; + let n_blocks_per_col = uniforms.b_shape[1]; + let num_tiles = (n_blocks_per_col - 1) / ${T} + 1; + + // Loop over shared dimension. + for (var tile: u32 = 0; tile < num_tiles; tile += 1) { + let a_col_start = tile * ${x}; + // load one tile A data into shared memory. + for (var a_offset = local_idx; a_offset < ${x}; a_offset += ${y}) + { + let a_col = a_col_start + a_offset; + if (a_col < uniforms.a_shape[2]) + { + sub_a[a_offset] = ${Z.getByIndices(`${Z.type.indices}(batch, row, a_col)`)}; + } else { + sub_a[a_offset] = ${Z.type.value}(0); + } + } + workgroupBarrier(); + + // each thread process one block + let b_row = col + local_id.y; + let block = tile * ${T} + local_id.x; + ${de?` + let zero_point_bytes_per_col = (n_blocks_per_col + 1) / 2; + let zero_point_byte_count = b_row * zero_point_bytes_per_col + (block >> 0x1u); + let zero_point_word_index = zero_point_byte_count >> 0x2u; + let zero_point_byte_offset = zero_point_byte_count & 0x3u; + let zero_point_nibble_offset: u32 = block & 0x1u; + let zero_point_bits_offset = (zero_point_byte_offset << 3) + (zero_point_nibble_offset << 2); + let zero_point_word = ${de.getByOffset("zero_point_word_index")} >> zero_point_bits_offset; + let zero_point = ${le}((zero_point_word) & 0xFu);`:` + // The default zero point is 8 for unsigned 4-bit quantization. + let zero_point = ${le}(8);`} + let scale = ${ue.getByOffset("b_row * n_blocks_per_col + block")}; + let b_data = ${te.getByIndices(`${te.type.indices}(b_row, block, 0)`)}; + var word_offset = local_id.x * ${t.blockSize/h}; + for (var i: u32 = 0; i < ${w}; i++) { + ${re()} + let b_value = ${w===1?"b_data":"b_data[i]"}; + let b_value_lower = unpack4xU8(b_value & 0x0F0F0F0Fu); + let b_value_upper = unpack4xU8((b_value >> 4) & 0x0F0F0F0Fu); + let b_quantized_values = mat2x4<${le}>(${Array.from({length:4},(ne,oe)=>`${le}(b_value_lower[${oe}]), ${le}(b_value_upper[${oe}])`).join(", ")}); + let b_dequantized_values = (b_quantized_values - mat2x4<${le}>(${Array(8).fill("zero_point").join(",")})) * scale; + inter_results[local_id.y][local_id.x] += ${Array.from({length:2},(ne,oe)=>`${`dot(a_data${oe}, b_dequantized_values[${oe}])`}`).join(" + ")}; + word_offset += ${8/h}; + } + workgroupBarrier(); + } + + if (local_idx < ${S}) { + var output_value: ${q.type.value} = ${q.type.value}(0); + for (var b = 0u; b < ${$}; b++) { + output_value += inter_results[local_idx][b]; + } + if (col + local_idx < uniforms.output_shape[2]) + { + ${q.setByIndices(`${q.type.indices}(batch, row, col + local_idx)`,"output_value")} + } + } + }`};return{name:"BlockwiseMatMulNBits32",shaderCache:{hint:`${t.blockSize};${h};${w};${$};${S}`,inputDependencies:Array(e.length).fill("rank")},getRunData:()=>({outputs:[{dims:g,dataType:u}],dispatchGroup:{x:C},programUniforms:A}),getShaderSource:N}},Wd=(e,t)=>{Lf(e.inputs,t),t.blockSize===32&&e.adapterInfo.isVendor("intel")&&e.adapterInfo.isArchitecture("gen-12lp")?e.compute(Gf(e.inputs,t)):e.compute(Hf(e.inputs,t))},Nd=e=>ee(e)});var Ff,qf,jf,Kf,Yf,Xf,Qf,Zf,Hd,Gd=U(()=>{"use strict";J();ae();se();Ff=e=>{if(!e||e.length<1)throw new Error("Too few inputs");if(e[0].dataType!==1&&e[0].dataType!==10)throw new Error("Input type must be float or float16.");if(e.length>=2){let t=e[0].dims.length*2===e[1].dims[0];if(e.length===4&&(t=e[3].dims[0]*2===e[1].dims[0]),!t)throw new Error("The pads should be a 1D tensor of shape [2 * input_rank] or [2 * num_axes].")}},qf=(e,t,r)=>{let n="";for(let o=t-1;o>=0;--o)n+=` + k = i32(${e.indicesGet("indices",o)}) - ${F("uniforms.pads",o,r)}; + if (k < 0) { + break; + } + if (k >= i32(${F("uniforms.x_shape",o,t)})) { + break; + } + offset += k * i32(${F("uniforms.x_strides",o,t)}); + `;return` + value = ${e.type.value}(uniforms.constant_value); + for (var i = 0; i < 1; i++) { + var offset = 0; + var k = 0; + ${n} + value = x[offset]; + } + `},jf=(e,t,r)=>{let n="";for(let o=t-1;o>=0;--o)n+=` + k = i32(${e.indicesGet("indices",o)}) - ${F("uniforms.pads",o,r)}; + if (k < 0) { + k = -k; + } + { + let _2n_1 = 2 * (i32(${F("uniforms.x_shape",o,t)}) - 1); + k = k % _2n_1; + if(k >= i32(${F("uniforms.x_shape",o,t)})) { + k = _2n_1 - k; + } + } + offset += k * i32(${F("uniforms.x_strides",o,t)}); + `;return` + var offset = 0; + var k = 0; + ${n} + value = x[offset]; + `},Kf=(e,t,r)=>{let n="";for(let o=t-1;o>=0;--o)n+=` + k = i32(${e.indicesGet("indices",o)}) - ${F("uniforms.pads",o,r)}; + if (k < 0) { + k = 0; + } + if (k >= i32(${F("uniforms.x_shape",o,t)})) { + k = i32(${F("uniforms.x_shape",o,t)}) - 1; + } + offset += k * i32(${F("uniforms.x_strides",o,t)}); + `;return` + var offset = 0; + var k = 0; + ${n} + value = x[offset]; + `},Yf=(e,t,r)=>{let n="";for(let o=t-1;o>=0;--o)n+=` + k = i32(${e.indicesGet("indices",o)}) - ${F("uniforms.pads",o,r)}; + if (k < 0) { + k += i32(${F("uniforms.x_shape",o,t)}]); + } + if (k >= i32(${F("uniforms.x_shape",o,t)})) { + k -= i32(${F("uniforms.x_shape",o,t)}); + } + offset += k * i32(${F("uniforms.x_strides",o,t)}); + `;return` + var offset = 0; + var k = 0; + ${n} + value = x[offset]; + `},Xf=(e,t,r)=>{switch(r.mode){case 0:return qf(e,t,r.pads.length);case 1:return jf(e,t,r.pads.length);case 2:return Kf(e,t,r.pads.length);case 3:return Yf(e,t,r.pads.length);default:throw new Error("Invalid mode")}},Qf=(e,t)=>{let r=k.padShape(e[0].dims.slice(),t.pads),n=e[0].dims,o=k.size(r),i=[{type:12,data:o},{type:6,data:t.pads}],a=e.length>=3&&e[2].data;t.mode===0&&i.push({type:a?e[2].dataType:1,data:t.value}),i.push(...V(e[0].dims,r));let d=["rank"],l=c=>{let m=M("output",e[0].dataType,r.length),u=E("x",e[0].dataType,n.length),h=u.type.value,w=Xf(m,n.length,t),g=[{name:"output_size",type:"u32"},{name:"pads",type:"i32",length:t.pads.length}];return t.mode===0&&g.push({name:"constant_value",type:a?h:"f32"}),` + ${c.registerUniforms(g).declareVariables(u,m)} + ${c.mainStart()} + ${c.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + + let indices = ${m.offsetToIndices("global_idx")}; + + var value = ${h}(0); + ${w} + output[global_idx] = value; + }`};return{name:"Pad",shaderCache:{hint:`${t.mode}${a}`,inputDependencies:d},getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(k.size(r)/64)},programUniforms:i}),getShaderSource:l}},Zf=(e,t)=>{if(e.length>1){let r=e[1].getBigInt64Array(),n=e.length>=3&&e[2].data?e[2].dataType===10?e[2].getUint16Array()[0]:e[2].getFloat32Array()[0]:0,o=e[0].dims.length,i=new Int32Array(2*o).fill(0);if(e.length>=4){let d=e[3].getBigInt64Array();for(let l=0;li[Number(l)]=Number(d));let a=[];return i.forEach(d=>a.push(d)),{mode:t.mode,value:n,pads:a}}else return t},Hd=(e,t)=>{Ff(e.inputs);let r=Zf(e.inputs,t);e.compute(Qf(e.inputs,r),{inputs:[0]})}});var nn,Fd,qd,jd,Kd,Jf,eh,Yd,Xd,Qd,Zd,Jd,el,tl,rl,nl,ol,il,al,sl=U(()=>{"use strict";Ke();J();ae();se();nn=e=>{if(_e.webgpu.validateInputContent&&(!e||e.length!==1))throw new Error("Pool ops requires 1 input.")},Fd=(e,t,r)=>{let n=t.format==="NHWC",o=e.dims.slice();n&&o.splice(1,0,o.pop());let i=Object.hasOwnProperty.call(t,"dilations"),a=t.kernelShape.slice(),d=t.strides.slice(),l=i?t.dilations.slice():[],c=t.pads.slice();Ct.adjustPoolAttributes(r,o,a,d,l,c);let m=Ct.computePoolOutputShape(r,o,d,l,a,c,t.autoPad),u=Object.assign({},t);i?Object.assign(u,{kernelShape:a,strides:d,pads:c,dilations:l,cacheKey:t.cacheKey}):Object.assign(u,{kernelShape:a,strides:d,pads:c,cacheKey:t.cacheKey});let h=m.slice();return h.push(h.splice(1,1)[0]),[u,n?h:m]},qd=(e,t)=>{let r=t.format==="NHWC",n=k.size(e),o=k.size(t.kernelShape),i=[{type:12,data:n},{type:12,data:o}],a=[{name:"outputSize",type:"u32"},{name:"kernelSize",type:"u32"}];if(t.kernelShape.length<=2){let d=t.kernelShape[t.kernelShape.length-1],l=t.strides[t.strides.length-1],c=t.pads[t.pads.length/2-1],m=t.pads[t.pads.length-1],u=!!(c+m);i.push({type:12,data:d},{type:12,data:l},{type:12,data:c},{type:12,data:m}),a.push({name:"kw",type:"u32"},{name:"sw",type:"u32"},{name:"pwStart",type:"u32"},{name:"pwEnd",type:"u32"});let h=!1;if(t.kernelShape.length===2){let w=t.kernelShape[t.kernelShape.length-2],g=t.strides[t.strides.length-2],y=t.pads[t.pads.length/2-2],S=t.pads[t.pads.length-2];h=!!(y+S),i.push({type:12,data:w},{type:12,data:g},{type:12,data:y},{type:12,data:S}),a.push({name:"kh",type:"u32"},{name:"sh",type:"u32"},{name:"phStart",type:"u32"},{name:"phEnd",type:"u32"})}return[i,a,!0,u,h]}else{if(r)throw new Error("Pooling with kernelShape.length > 2 is not supported for NHWC format.");let d=k.computeStrides(t.kernelShape);i.push({type:12,data:d},{type:12,data:t.pads},{type:12,data:t.strides}),a.push({name:"kernelStrides",type:"u32",length:d.length},{name:"pads",type:"u32",length:t.pads.length},{name:"strides",type:"u32",length:t.strides.length});let l=t.pads.reduce((c,m)=>c+m);return[i,a,!!l,!1,!1]}},jd=(e,t,r,n,o,i,a,d,l,c,m,u)=>{let h=o.format==="NHWC",w=t.type.value,g=M("output",t.type.tensor,n);if(o.kernelShape.length<=2){let y="",S="",$="",v=r-(h?2:1);if(m?y=` + for (var i: u32 = 0u; i < uniforms.kw; i++) { + xIndices[${v}] = indices[${v}] * uniforms.sw - uniforms.pwStart + i; + if (xIndices[${v}] < 0 || xIndices[${v}] + >= uniforms.x_shape[${v}]) { + pad++; + continue; + } + let x_val = x[${t.indicesToOffset("xIndices")}]; + ${i} + }`:y=` + for (var i: u32 = 0u; i < uniforms.kw; i++) { + xIndices[${v}] = indices[${v}] * uniforms.sw - uniforms.pwStart + i; + let x_val = x[${t.indicesToOffset("xIndices")}]; + ${i} + }`,o.kernelShape.length===2){let T=r-(h?3:2);u?S=` + for (var j: u32 = 0u; j < uniforms.kh; j++) { + xIndices[${T}] = indices[${T}] * uniforms.sh - uniforms.phStart + j; + if (xIndices[${T}] < 0 || xIndices[${T}] >= uniforms.x_shape[${T}]) { + pad += i32(uniforms.kw); + continue; + } + `:S=` + for (var j: u32 = 0u; j < uniforms.kh; j++) { + xIndices[${T}] = indices[${T}] * uniforms.sh - uniforms.phStart + j; + `,$=` + } + `}return` + ${e.registerUniforms(l).declareVariables(t,g)} + + ${e.mainStart()} + ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + + let indices = ${g.offsetToIndices("global_idx")}; + var xIndices = ${g.offsetToIndices("global_idx")}; + + var value = ${w}(${d}); + var pad = 0; + ${S} + ${y} + ${$} + ${a} + + output[global_idx] = value; + }`}else{if(h)throw new Error("Pooling with kernelShape.length > 2 is not supported for NHWC format.");let y=o.kernelShape.length,S=o.pads.length,$="";return c?$=` + if (xIndices[j] >= uniforms.x_shape[j]) { + pad++; + isPad = true; + break; + } + } + if (!isPad) { + let x_val = x[${t.indicesToOffset("xIndices")}]; + ${i} + }`:$=` + } + let x_val = x[${t.indicesToOffset("xIndices")}]; + ${i} + `,` + ${e.registerUniforms(l).declareVariables(t,g)} + + ${e.mainStart()} + ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + let indices = ${g.offsetToIndices("global_idx")}; + var xIndices = ${g.offsetToIndices("global_idx")}; + + var offsets: array; + + var value = ${w}(${d}); + var pad = 0; + var isPad = false; + + for (var i: u32 = 0u; i < uniforms.kernelSize; i++) { + var offset = i; + for (var j = 0u; j < ${y-1}u; j++) { + offsets[j] = offset / ${F("uniforms.kernelStrides","j",y)}; + offset -= offsets[j] * ${F("uniforms.kernelStrides","j",y)}; + } + offsets[${y-1}] = offset; + + isPad = false; + for (var j = ${r-y}u; j < ${r}u; j++) { + xIndices[j] = indices[j] * ${F("uniforms.strides",`j - ${r-y}u`,y)} + + offsets[j - ${r-y}u] - ${F("uniforms.pads","j - 2u",S)}; + ${$} + } + ${a} + + output[global_idx] = value; + }`}},Kd=e=>`${e.format};${e.ceilMode};${e.autoPad};${e.kernelShape.length}`,Jf=e=>`${Kd(e)};${e.countIncludePad}`,eh=e=>`${Kd(e)};${e.storageOrder};${e.dilations}`,Yd=e=>({format:e.format,autoPad:["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][e.auto_pad],ceilMode:e.ceil_mode,kernelShape:e.kernel_shape,strides:e.strides,pads:e.pads}),Xd=(e,t,r,n)=>{let[o,i]=Fd(t,n,r),a=E("x",t.dataType,t.dims.length),d=a.type.value,l="value += x_val;",c="";o.countIncludePad?c+=`value /= ${d}(uniforms.kernelSize);`:c+=`value /= ${d}(i32(uniforms.kernelSize) - pad);`;let[m,u,h,w,g]=qd(i,o);m.push(...V(t.dims,i));let y=["rank"];return{name:e,shaderCache:{hint:`${n.cacheKey};${h};${w};${g}`,inputDependencies:y},getRunData:()=>({outputs:[{dims:i,dataType:t.dataType}],dispatchGroup:{x:Math.ceil(k.size(i)/64)},programUniforms:m}),getShaderSource:S=>jd(S,a,t.dims.length,i.length,o,l,c,0,u,h,w,g)}},Qd=e=>{let t=e.count_include_pad!==0,r=Yd(e);if(r.ceilMode!==0)throw new Error("using ceil() in shape computation is not yet supported for AveragePool");let n={countIncludePad:t,...r,cacheKey:""};return{...n,cacheKey:Jf(n)}},Zd=(e,t)=>{nn(e.inputs),e.compute(Xd("AveragePool",e.inputs[0],!1,t))},Jd={autoPad:"",ceilMode:0,countIncludePad:!1,kernelShape:[],strides:[],pads:[],storageOrder:0,dilations:[]},el=e=>{let t=e.format;return{format:t,...Jd,cacheKey:t}},tl=(e,t)=>{nn(e.inputs),e.compute(Xd("GlobalAveragePool",e.inputs[0],!0,t))},rl=(e,t,r,n)=>{let[o,i]=Fd(t,n,r),a=` + value = max(x_val, value); + `,d="",l=E("x",t.dataType,t.dims.length),c=["rank"],[m,u,h,w,g]=qd(i,o);return m.push(...V(t.dims,i)),{name:e,shaderCache:{hint:`${n.cacheKey};${h};${w};${g}`,inputDependencies:c},getRunData:()=>({outputs:[{dims:i,dataType:t.dataType}],dispatchGroup:{x:Math.ceil(k.size(i)/64)},programUniforms:m}),getShaderSource:y=>jd(y,l,t.dims.length,i.length,o,a,d,t.dataType===10?-65504:-1e5,u,h,w,g)}},nl=(e,t)=>{nn(e.inputs),e.compute(rl("MaxPool",e.inputs[0],!1,t))},ol=e=>{let t=e.storage_order,r=e.dilations,n=Yd(e);if(t!==0)throw new Error("column major storage order is not yet supported for MaxPool");if(n.ceilMode!==0)throw new Error("using ceil() in shape computation is not yet supported for MaxPool");let o={storageOrder:t,dilations:r,...n,cacheKey:""};return{...o,cacheKey:eh(o)}},il=e=>{let t=e.format;return{format:t,...Jd,cacheKey:t}},al=(e,t)=>{nn(e.inputs),e.compute(rl("GlobalMaxPool",e.inputs[0],!0,t))}});var rh,nh,ul,dl,ll=U(()=>{"use strict";J();ae();Ie();se();rh=(e,t)=>{if(e.length<2||e.length>3)throw new Error("DequantizeLinear requires 2 or 3 inputs.");if(e.length===3&&e[1].dims===e[2].dims)throw new Error("x-scale and x-zero-point must have the same shape.");if(e.length===3&&e[0].dataType!==e[2].dataType)throw new Error("x and x-zero-point must have the same data type.");if(e[0].dataType===6&&e.length>2)throw new Error("In the case of dequantizing int32 there is no zero point.");if(e[1].dims.length!==0&&e[1].dims.length!==1&&e[1].dims.length!==e[0].dims.length)throw new Error("scale input must be a scalar, a 1D tensor, or have the same rank as the input tensor.");if(e.length>2){if(e[0].dataType!==e[2].dataType)throw new Error("x and x-zero-point must have the same data type.");if(e[1].dims.length!==e[2].dims.length)throw new Error("scale and zero-point inputs must have the same rank.");if(!e[1].dims.map((r,n)=>r===e[2].dims[n]).reduce((r,n)=>r&&n,!0))throw new Error("scale and zero-point inputs must have the same shape.")}if(t.blockSize>0){if(e[1].dims.length===0||e[1].dims.length===1&&e[1].dims[0]===1)throw new Error("blockSize must be set only for block quantization.");if(!e[1].dims.map((o,i)=>i===t.axis||o===e[0].dims[i]).reduce((o,i)=>o&&i,!0))throw new Error("For block qunatization, scale input shape to match the input shape except for the axis");if(e[1].dims.length!==e[0].dims.length)throw new Error("For block qunatization the scale input rank must be the same as the x rank.");let r=e[0].dims[t.axis],n=e[1].dims[t.axis];if(t.blockSizeMath.ceil(r/(n-1)-1))throw new Error("blockSize must be with in the range [ceil(dI / Si), ceil(dI / (Si - 1) - 1)].")}},nh=(e,t)=>{let r=k.normalizeAxis(t.axis,e[0].dims.length),n=e[0].dataType,o=n===3,i=e[0].dims,a=e[1].dataType,d=k.size(i),l=n===3||n===2,c=l?[Math.ceil(k.size(e[0].dims)/4)]:e[0].dims,m=e[1].dims,u=e.length>2?e[2]:void 0,h=u?l?[Math.ceil(k.size(u.dims)/4)]:u.dims:void 0,w=m.length===0||m.length===1&&m[0]===1,g=w===!1&&m.length===1,y=we(d),S=w&&(!l||y===4),$=S?y:1,v=S&&!l?y:1,x=E("input",l?12:n,c.length,v),T=E("scale",a,m.length),C=u?E("zero_point",l?12:n,h.length):void 0,A=M("output",a,i.length,$),P=[x,T];C&&P.push(C);let D=[c,m];u&&D.push(h);let W=[{type:12,data:d/$},{type:12,data:r},{type:12,data:t.blockSize},...V(...D,i)],N=j=>{let Y=[{name:"output_size",type:"u32"},{name:"axis",type:"u32"},{name:"block_size",type:"u32"}];return` + ${j.registerUniforms(Y).declareVariables(...P,A)} + ${j.mainStart()} + ${j.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + let output_indices = ${A.offsetToIndices("global_idx")}; + + // Set input x + ${(()=>l?` + let input = ${x.getByOffset("global_idx / 4")}; + let x_vec = ${o?"unpack4xI8(input)":"unpack4xU8(input)"}; + let x_value = ${$===1?"x_vec[global_idx % 4]":"x_vec"};`:`let x_value = ${x.getByOffset("global_idx")};`)()}; + + // Set scale input + ${(()=>w?`let scale_value= ${T.getByOffset("0")}`:g?` + let scale_index = ${A.indicesGet("output_indices","uniforms.axis")}; + let scale_value= ${T.getByOffset("scale_index")};`:` + var scale_indices: ${T.type.indices} = output_indices; + let index = ${T.indicesGet("scale_indices","uniforms.axis")} / uniforms.block_size; + ${T.indicesSet("scale_indices","uniforms.axis","index")}; + let scale_value= ${T.getByIndices("scale_indices")};`)()}; + + // Set zero-point input + ${(()=>C?w?l?` + let zero_point_input = ${C.getByOffset("0")}; + let zero_point_vec = ${o?"unpack4xI8(zero_point_input)":"unpack4xU8(zero_point_input)"}; + let zero_point_value= zero_point_vec[0]`:`let zero_point_value = ${C.getByOffset("0")}`:g?l?` + let zero_point_index = ${A.indicesGet("output_indices","uniforms.axis")}; + let zero_point_input = ${C.getByOffset("zero_point_index / 4")}; + let zero_point_vec = ${o?"unpack4xI8(zero_point_input)":"unpack4xU8(zero_point_input)"}; + let zero_point_value = zero_point_vec[zero_point_index % 4]`:` + let zero_point_index = ${A.indicesGet("output_indices","uniforms.axis")}; + let zero_point_value = ${C.getByOffset("zero_point_index")};`:l?` + let zero_point_offset = ${T.indicesToOffset("scale_indices")}; + let zero_point_input = ${C.getByOffset("zero_point_offset / 4")}; + let zero_point_vec = ${o?"unpack4xI8(zero_point_input)":"unpack4xU8(zero_point_input)"}; + let zero_point_value = zero_point_vec[zero_point_offset % 4];`:`let zero_point_value = ${C.getByIndices("scale_indices")};`:`let zero_point_value = ${l?o?"i32":"u32":x.type.value}(0);`)()}; + // Compute and write output + ${A.setByOffset("global_idx",`${A.type.value}(x_value - zero_point_value) * scale_value`)}; + }`};return{name:"DequantizeLinear",shaderCache:{hint:t.cacheKey,inputDependencies:C?["rank","rank","rank"]:["rank","rank"]},getShaderSource:N,getRunData:()=>({outputs:[{dims:i,dataType:a}],dispatchGroup:{x:Math.ceil(d/$/64),y:1,z:1},programUniforms:W})}},ul=(e,t)=>{rh(e.inputs,t),e.compute(nh(e.inputs,t))},dl=e=>ee({axis:e.axis,blockSize:e.blockSize})});var oh,ih,cl,pl=U(()=>{"use strict";Ke();J();se();oh=(e,t,r)=>{let n=e===t,o=et&&r>0;if(n||o||i)throw new Error("Range these inputs' contents are invalid.")},ih=(e,t,r,n)=>{let o=Math.abs(Math.ceil((t-e)/r)),i=[o],a=o,d=[{type:12,data:a},{type:n,data:e},{type:n,data:r},...V(i)],l=c=>{let m=M("output",n,i.length),u=m.type.value,h=[{name:"outputSize",type:"u32"},{name:"start",type:u},{name:"delta",type:u}];return` + ${c.registerUniforms(h).declareVariables(m)} + ${c.mainStart()} + ${c.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + output[global_idx] = uniforms.start + ${u}(global_idx) * uniforms.delta; + }`};return{name:"Range",shaderCache:{hint:`${n}`},getShaderSource:l,getRunData:()=>({outputs:[{dims:i,dataType:n}],dispatchGroup:{x:Math.ceil(a/64)},programUniforms:d})}},cl=e=>{let t=0,r=0,n=0;e.inputs[0].dataType===6?(t=e.inputs[0].getInt32Array()[0],r=e.inputs[1].getInt32Array()[0],n=e.inputs[2].getInt32Array()[0]):e.inputs[0].dataType===1&&(t=e.inputs[0].getFloat32Array()[0],r=e.inputs[1].getFloat32Array()[0],n=e.inputs[2].getFloat32Array()[0]),_e.webgpu.validateInputContent&&oh(t,r,n),e.compute(ih(t,r,n,e.inputs[0].dataType),{inputs:[]})}});var ah,sh,uh,dh,lh,ch,ph,mh,fh,hh,gh,ml,yh,bh,wh,_h,vh,fl,hl,gl=U(()=>{"use strict";J();ae();Ie();se();ah=(e,t)=>{if(e.every(r=>r>0||(()=>{throw new Error("Resize requires scales input values to be positive")})),e.length>0){if(t.mode==="linear"){if(!(e.length===2||e.length===3||e.length===4&&e[0]===1&&e[1]===1||e.length===4&&e[0]===1&&e[3]===1||e.length===5&&e[0]===1&&e[1]===1))throw new Error(`For linear mode, Resize requires scales to be 2D, 3D, 4D with either two outermost or one innermost and + one outermost scale values equal to 1, or 5D with two outermost scale values equal to 1`)}else if(t.mode==="cubic"&&!(e.length===2||e.length===4&&e[0]===1&&e[1]===1||e.length===4&&e[0]===1&&e[3]===1))throw new Error("Resize requires scales input size to be 2 or 4 for cubic mode")}},sh=(e,t,r)=>{t.every(o=>o>=0&&o{throw new Error("Resize requires axes input values to be positive and less than rank")}));let n=new Array(r).fill(1);return t.forEach((o,i)=>n[o]=e[i]),n},uh=(e,t,r,n,o,i)=>{let[a,d,l]=r>10?[1,2,3]:[-1,e.length>1?1:-1,-1],c=e[0].dims.length;if(a>0&&e.length>a&&e[a].dims.length>0)e[a].getFloat32Array().forEach(m=>i.push(m));else if(t.coordinateTransformMode==="tf_crop_and_resize")throw new Error("Resize requires RoI input to be specified when coordinateTransformMode is tfCropAndResize");if(d>0&&e.length>d&&e[d].dims.length===1&&e[d].dims[0]>0){if(e[d].getFloat32Array().forEach(m=>n.push(m)),n.length!==0&&n.length!==c&&r>=18&&n.length!==t.axes.length)throw new Error("Resize requires scales input size to be same as input rank or axes size for opset 18 and up");ah(n,t),t.axes.length>0&&sh(n,t.axes,c).forEach((m,u)=>n[u]=m)}if(l>0&&e.length>l&&e[l].dims.length===1&&e[l].dims[0]>0&&(e[l].getBigInt64Array().forEach(m=>o.push(Number(m))),o.length!==0&&o.length!==c&&r>=18&&o.length!==t.axes.length))throw new Error("Resize requires sizes input size to be same as input rank or axes size for opset 18 and up");if(t.axes.length>0){if(n.length!==0&&n.length!==t.axes.length)throw new Error('Resize requires "scales" input size to be of axes rank when axes attributes is specified');if(o.length!==0&&o.length!==t.axes.length)throw new Error('Resize requires "sizes" input size to be of rank axes rank when axes attributes is specified')}if(typeof n<"u"&&typeof o<"u"&&n.length>0&&o.length>c)throw new Error("Resize requires only of scales or sizes to be specified")},dh=(e,t)=>`fn getOriginalCoordinateFromResizedCoordinate(xResized: u32, xScale: f32, lengthResized: u32, + lengthOriginal: u32, roiStart: f32, roiEnd: f32) -> ${t} { `+(()=>{switch(e){case"asymmetric":return`return ${t}(xResized) / ${t}(xScale);`;case"pytorch_half_pixel":return`if (lengthResized > 1) { + return (${t}(xResized) + 0.5) / ${t}(xScale) - 0.5; + } else { + return 0.0; + }`;case"tf_half_pixel_for_nn":return`return (${t}(xResized) + 0.5) / ${t}(xScale);`;case"align_corners":return`if (lengthResized == 1) { + return 0.0; + } else { + // The whole part and the fractional part are calculated separately due to inaccuracy of floating + // point division. As an example, f32(21) / f32(7) may evaluate to 2.99... instead of 3, causing an + // offset-by-one error later in floor(). + let whole = ${t}(xResized * (lengthOriginal - 1) / (lengthResized - 1)); + let fract = + ${t}(xResized * (lengthOriginal - 1) % (lengthResized - 1)) / ${t}(lengthResized - 1); + return whole + fract; + }`;case"tf_crop_and_resize":return`if (lengthResized > 1) { + return ${t}(roiStart) * ${t}(lengthOriginal - 1) + + (${t}(xResized) * ${t}(roiEnd - roiStart) * ${t}(lengthOriginal - 1)) / + ${t}(lengthResized - 1); + } else { + return 0.5 * ${t}(roiStart + roiEnd) * ${t}(lengthOriginal - 1); + }`;case"half_pixel_symmetric":return`const outputWidth = ${t}xScale * ${t}(lengthResized); + const adjustment = ${t}(lengthResized) / outputWidth; + const center = ${t}(lengthOriginal) / 2; + const offset = center * (1 - adjustment); + return offset + ((${t}(xResized) + 0.5) / ${t}(xScale)) - 0.5;`;case"half_pixel":return`return ((${t}(xResized) + 0.5) / ${t}(xScale)) - 0.5;`;default:throw new Error(`Coordinate transform mode ${e} is not supported`)}})()+"}",lh=(e,t,r)=>`fn getNearestPixelFromOriginal(xOriginal: ${r}, isDownSample: bool) -> ${r} {`+(()=>{switch(e){case"round_prefer_ceil":return"if (fract(xOriginal) == 0.5) { return ceil(xOriginal); } else { return round(xOriginal); }";case"floor":return"return floor(xOriginal);";case"ceil":return"return ceil(xOriginal);";case"round_prefer_floor":return"if (fract(xOriginal) == 0.5) { return floor(xOriginal); } else { return round(xOriginal); }";case"simple":default:if(t<11)return"if (isDownSample) { return ceil(xOriginal); } else { return xOriginal; }";throw new Error(`Nearest mode ${e} is not supported`)}})()+"}",ch=(e,t,r)=>{let n=new Array(r).fill(0).concat(new Array(r).fill(1)),o=e.length===0?n:e.slice();return t.length>0?(t.forEach((i,a)=>{n[i]=o[a],n[a+r]=o[t.length+a]}),n):o},ph=(e,t,r,n)=>{let o=[];if(r.length>0)if(n.length>0){if(e.forEach(i=>o.push(i)),Math.max(...n)>e.length)throw new Error("axes is out of bound");n.forEach((i,a)=>o[i]=r[a])}else r.forEach(i=>o.push(i));else{if(t.length===0)throw new Error("Resize requires either scales or sizes.");o=e.map((i,a)=>Math.round(i*t[a]))}return o},mh=(e,t,r)=>{let n=(()=>{switch(r.keepAspectRatioPolicy){case"not_larger":return r.axes.length>0?Math.min(...r.axes.map(i=>t[i]),Number.MAX_VALUE):Math.min(...t,Number.MAX_VALUE);case"not_smaller":return r.axes.length>0?Math.max(...r.axes.map(i=>t[i]),Number.MIN_VALUE):Math.max(...t,Number.MIN_VALUE);default:throw new Error(`Keep aspect ratio policy ${r.keepAspectRatioPolicy} is not supported`)}})();t.fill(1,0,t.length);let o=e.slice();return r.axes.length>0?(r.axes.forEach(i=>t[i]=n),r.axes.forEach(i=>o[i]=Math.round(e[i]*t[i]))):(t.fill(n,0,t.length),o.forEach((i,a)=>o[a]=Math.round(i*t[a]))),o},fh=(e,t,r,n,o)=>` + fn calculateOriginalIndicesFromOutputIndices(output_indices: ${e.type.indices}) -> array<${e.type.value}, ${r.length}> { + var original_indices: array<${e.type.value}, ${r.length}>; + for (var i:u32 = 0; i < ${r.length}; i++) { + var output_index = ${e.indicesGet("output_indices","i")}; + var scale = ${F("uniforms.scales","i",n)}; + var roi_low = ${F("uniforms.roi","i",o)}; + var roi_hi = ${F("uniforms.roi",`i + ${t.length}`,o)}; + if (scale == 1.0) { + original_indices[i] = ${e.type.value}(output_index); + } else { + var input_shape_i = ${F("uniforms.input_shape","i",t.length)}; + var output_shape_i = ${F("uniforms.output_shape","i",r.length)}; + original_indices[i] = getOriginalCoordinateFromResizedCoordinate(output_index, scale, output_shape_i, + input_shape_i, roi_low, roi_hi); + } + } + return original_indices; + }`,hh=(e,t,r,n,o,i,a)=>` + fn calculateInputIndicesFromOutputIndices(output_indices: ${t.type.indices}) -> ${e.type.indices} { + var input_indices: ${e.type.indices}; + for (var i:u32 = 0; i < ${n.length}; i++) { + var output_index = ${t.indicesGet("output_indices","i")}; + var input_index: u32; + var scale = ${F("uniforms.scales","i",o)}; + if (scale == 1.0) { + input_index = output_index; + } else { + var roi_low = ${F("uniforms.roi","i",i)}; + var roi_hi = ${F("uniforms.roi",`i + ${r.length}`,i)}; + var input_shape_i = ${F("uniforms.input_shape","i",r.length)}; + var output_shape_i = ${F("uniforms.output_shape","i",n.length)}; + var original_idx = getOriginalCoordinateFromResizedCoordinate(output_index, scale, output_shape_i, + input_shape_i, roi_low, roi_hi); + if (!${a} || (original_idx >= 0 && original_idx < ${t.type.value}(input_shape_i))) { + if (original_idx < 0) { + input_index = 0; + } else if (original_idx > ${t.type.value}(input_shape_i - 1)) { + input_index = input_shape_i - 1; + } else { + input_index = u32(getNearestPixelFromOriginal(original_idx, scale < 1)); + } + } else { + input_index = u32(original_idx); + } + } + ${e.indicesSet("input_indices","i"," input_index")} + } + return input_indices; + }`,gh=(e,t)=>` + fn checkInputIndices(input_indices: ${e.type.indices}) -> bool { + for (var i:u32 = 0; i < ${t.length}; i++) { + var input_index = ${e.indicesGet("input_indices","i")}; + if (input_index < 0 || input_index >= ${F("uniforms.input_shape","i",t.length)}) { + return false; + } + } + return true; + }`,ml=(e,t,r,n)=>e.rank>n?` + ${e.indicesSet("input_indices",t,"channel")}; + ${e.indicesSet("input_indices",r,"batch")}; +`:"",yh=(e,t,r,n,o)=>{let[a,d,l,c]=r.length===2?[-1,0,1,-1]:[0,2,3,1],m=e.type.value;return` + fn getInputValue(batch: u32, channel: u32, row: u32, col: u32) -> ${m} { + var input_indices: ${e.type.indices}; + ${e.indicesSet("input_indices",d,`max(0, min(row, ${r[d]} - 1))`)}; + ${e.indicesSet("input_indices",l,`max(0, min(col, ${r[l]} - 1))`)}; + ${ml(e,c,a,2)} + return ${e.getByIndices("input_indices")}; + } + + fn bilinearInterpolation(output_indices: ${t.type.indices}) -> ${m} { + var originalIndices = calculateOriginalIndicesFromOutputIndices(output_indices); + var row:${m} = originalIndices[${d}]; + var col:${m} = originalIndices[${l}]; + ${n?`if (row < 0 || row > (${r[d]} - 1) || col < 0 || col > (${r[l]} - 1)) { + return ${o}; + }`:""}; + row = max(0, min(row, ${r[d]} - 1)); + col = max(0, min(col, ${r[l]} - 1)); + var row1: u32 = u32(row); + var col1: u32 = u32(col); + var row2: u32 = u32(row + 1); + var col2: u32 = u32(col + 1); + var channel: u32 = ${r.length>2?`u32(originalIndices[${c}])`:"0"}; + var batch: u32 = ${r.length>2?`u32(originalIndices[${a}])`:"0"}; + var x11: ${m} = getInputValue(batch, channel, row1, col1); + var x12: ${m} = getInputValue(batch, channel, row1, col2); + var x21: ${m} = getInputValue(batch, channel, row2, col1); + var x22: ${m} = getInputValue(batch, channel, row2, col2); + var dx1: ${m} = abs(row - ${m}(row1)); + var dx2: ${m} = abs(${m}(row2) - row); + var dy1: ${m} = abs(col - ${m}(col1)); + var dy2: ${m} = abs(${m}(col2) - col); + if (row1 == row2) { + dx1 = 0.5; + dx2 = 0.5; + } + if (col1 == col2) { + dy1 = 0.5; + dy2 = 0.5; + } + return (x11 * dx2 * dy2 + x12 * dx2 * dy1 + x21 * dx1 * dy2 + x22 * dx1 * dy1); + }`},bh=(e,t,r,n,o,i,a,d,l,c)=>{let m=r.length===2,u=!0,[h,w]=m?[0,1]:u?[2,3]:[1,2],g=e.type.value,y=S=>{let $=S===h?"row":"col";return` + fn ${$}CubicInterpolation(input_indices: ${e.type.indices}, output_indices: ${t.type.indices}) -> ${g} { + var output_index = ${t.indicesGet("output_indices",S)}; + var originalIdx: ${g} = getOriginalCoordinateFromResizedCoordinate(output_index, ${o[S]}, + ${n[S]}, ${r[S]}, ${i[S]}, ${i[S]} + ${r.length}); + var fractOriginalIdx: ${g} = originalIdx - floor(originalIdx); + var coefs = getCubicInterpolationCoefs(fractOriginalIdx); + + if (${d} && (originalIdx < 0 || originalIdx > (${r[S]} - 1))) { + return ${l}; + } + var data: array<${g}, 4> = array<${g}, 4>(0.0, 0.0, 0.0, 0.0); + for (var i: i32 = -1; i < 3; i++) { + var ${$}: ${g} = originalIdx + ${g}(i); + if (${$} < 0 || ${$} >= ${r[S]}) { + ${(()=>c?`coefs[i + 1] = 0.0; + continue;`:d?`return ${l};`:`${$} = max(0, min(${$}, ${r[S]} - 1));`)()}; + } + var input_indices_copy: ${e.type.indices} = input_indices; + ${e.indicesSet("input_indices_copy",S,`u32(${$})`)}; + data[i + 1] = ${S===h?e.getByIndices("input_indices_copy"):"rowCubicInterpolation(input_indices_copy, output_indices)"}; + } + return cubicInterpolation1D(data, coefs); + }`};return` + ${y(h)}; + ${y(w)}; + fn getCubicInterpolationCoefs(s: ${g}) -> array<${g}, 4> { + var absS = abs(s); + var coeffs: array<${g}, 4> = array<${g}, 4>(0.0, 0.0, 0.0, 0.0); + var oneMinusAbsS: ${g} = 1.0 - absS; + var twoMinusAbsS: ${g} = 2.0 - absS; + var onePlusAbsS: ${g} = 1.0 + absS; + coeffs[0] = ((${a} * onePlusAbsS - 5 * ${a}) * onePlusAbsS + 8 * ${a}) * onePlusAbsS - 4 * ${a}; + coeffs[1] = ((${a} + 2) * absS - (${a} + 3)) * absS * absS + 1; + coeffs[2] = ((${a} + 2) * oneMinusAbsS - (${a} + 3)) * oneMinusAbsS * oneMinusAbsS + 1; + coeffs[3] = ((${a} * twoMinusAbsS - 5 * ${a}) * twoMinusAbsS + 8 * ${a}) * twoMinusAbsS - 4 * ${a}; + return coeffs; + } + + fn cubicInterpolation1D(x: array<${g}, 4>, coefs: array<${g}, 4>) -> ${g} { + var coefsSum: ${g} = coefs[0] + coefs[1] + coefs[2] + coefs[3]; + return (x[0] * coefs[0] + x[1] * coefs[1]+ x[2] * coefs[2]+ x[3] * coefs[3]) / coefsSum; + } + + fn bicubicInterpolation(output_indices: ${t.type.indices}) -> ${g} { + var input_indices: ${e.type.indices} = output_indices; + return colCubicInterpolation(input_indices, output_indices); + } + `},wh=(e,t,r,n,o)=>{let[a,d,l,c,m]=r.length===3?[-1,0,1,2,-1]:[0,2,3,4,1],u=e.type.value;return` + fn getInputValue(batch: u32, channel: u32, depth:u32, height: u32, width: u32) -> ${u} { + var input_indices: ${e.type.indices}; + ${e.indicesSet("input_indices",d,`max(0, min(depth, ${r[d]} - 1))`)}; + ${e.indicesSet("input_indices",l,`max(0, min(height, ${r[l]} - 1))`)}; + ${e.indicesSet("input_indices",c,`max(0, min(width, ${r[c]} - 1))`)}; + ${ml(e,m,a,3)} + return ${e.getByIndices("input_indices")}; + } + + fn trilinearInterpolation(output_indices: ${t.type.indices}) -> ${u} { + var originalIndices = calculateOriginalIndicesFromOutputIndices(output_indices); + var depth:${u} = originalIndices[${d}]; + var height:${u} = originalIndices[${l}]; + var width:${u} = originalIndices[${c}]; + ${n?`if (depth < 0 || depth > (${r[d]} - 1) || height < 0 || height > (${r[l]} - 1) || width < 0 || (width > ${r[c]} - 1)) { + return ${o}; + }`:""}; + + depth = max(0, min(depth, ${r[d]} - 1)); + height = max(0, min(height, ${r[l]} - 1)); + width = max(0, min(width, ${r[c]} - 1)); + var depth1: u32 = u32(depth); + var height1: u32 = u32(height); + var width1: u32 = u32(width); + var depth2: u32 = u32(depth + 1); + var height2: u32 = u32(height + 1); + var width2: u32 = u32(width + 1); + var channel: u32 = ${r.length>3?`u32(originalIndices[${m}])`:"0"}; + var batch: u32 = ${r.length>3?`u32(originalIndices[${a}])`:"0"}; + + var x111: ${u} = getInputValue(batch, channel, depth1, height1, width1); + var x112: ${u} = getInputValue(batch, channel, depth1, height1, width2); + var x121: ${u} = getInputValue(batch, channel, depth1, height2, width1); + var x122: ${u} = getInputValue(batch, channel, depth1, height2, width2); + var x211: ${u} = getInputValue(batch, channel, depth2, height1, width1); + var x212: ${u} = getInputValue(batch, channel, depth2, height1, width2); + var x221: ${u} = getInputValue(batch, channel, depth2, height2, width1); + var x222: ${u} = getInputValue(batch, channel, depth2, height2, width2); + var dx1: ${u} = abs(depth - ${u}(depth1)); + var dx2: ${u} = abs(${u}(depth2) - depth); + var dy1: ${u} = abs(height - ${u}(height1)); + var dy2: ${u} = abs(${u}(height2) - height); + var dz1: ${u} = abs(width - ${u}(width1)); + var dz2: ${u} = abs(${u}(width2) - width); + if (depth1 == depth2) { + dx1 = 0.5; + dx2 = 0.5; + } + if (height1 == height2) { + dy1 = 0.5; + dy2 = 0.5; + } + if (width1 == width2) { + dz1 = 0.5; + dz2 = 0.5; + } + return (x111 * dx2 * dy2 * dz2 + x112 * dx2 * dy2 * dz1 + x121 * dx2 * dy1 *dz2 + x122 * dx2 * dy1 * dz1 + + x211 * dx1 * dy2 * dz2 + x212 * dx1 * dy2 * dz1 + x221 * dx1 * dy1 *dz2 + x222 * dx1 * dy1 * dz1); + }`},_h=(e,t,r,n,o,i)=>{let a=e.dims,d=ch(i,t.axes,a.length),l=ph(a,n,o,t.axes),c=n.slice();n.length===0&&(c=a.map((v,x)=>v===0?1:l[x]/v),t.keepAspectRatioPolicy!=="stretch"&&(l=mh(a,c,t)));let m=M("output",e.dataType,l.length),u=E("input",e.dataType,a.length),h=k.size(l),w=a.length===l.length&&a.every((v,x)=>v===l[x]),g=t.coordinateTransformMode==="tf_crop_and_resize",y=t.extrapolationValue,S=u.type.value,$=v=>` + ${w?"":` + ${dh(t.coordinateTransformMode,S)}; + ${(()=>{switch(t.mode){case"nearest":return` + ${gh(u,a)}; + ${lh(t.nearestMode,r,S)}; + ${hh(u,m,a,l,c.length,d.length,g)}; + `;case"linear":return` + ${fh(m,a,l,c.length,d.length)}; + ${(()=>{if(a.length===2||a.length===4)return`${yh(u,m,a,g,y)}`;if(a.length===3||a.length===5)return`${wh(u,m,a,g,y)}`;throw Error("Linear mode only supports input dims 2, 3, 4 and 5 are supported in linear mode.")})()}; + `;case"cubic":return` + ${(()=>{if(a.length===2||a.length===4)return`${bh(u,m,a,l,c,d,t.cubicCoeffA,g,t.extrapolationValue,t.excludeOutside)}`;throw Error("Cubic mode only supports input dims 2 and 4 are supported in linear mode.")})()}; + `;default:throw Error("Invalid resize mode")}})()}; + `} + ${v.registerUniform("output_size","u32").registerUniform("scales","f32",c.length).registerUniform("roi","f32",d.length).declareVariables(u,m)} + ${v.mainStart()} + ${v.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + ${w?"output[global_idx] = input[global_idx];":` + let output_indices = ${m.offsetToIndices("global_idx")}; + var input_indices: ${u.type.indices}; + ${(()=>{switch(t.mode){case"nearest":return`input_indices = calculateInputIndicesFromOutputIndices(output_indices); + if (checkInputIndices(input_indices)) { + output[global_idx] = ${u.getByIndices("input_indices")}; + } else { + output[global_idx] = ${t.extrapolationValue}; + }`;case"linear":return`output[global_idx] = ${a.length===2||a.length===4?"bilinearInterpolation":"trilinearInterpolation"}(output_indices);`;case"cubic":return"output[global_idx] = bicubicInterpolation(output_indices);";default:throw Error(`Unsupported resize mode: ${t.mode}`)}})()}; +`} + }`;return{name:"Resize",shaderCache:{hint:`${t.cacheKey}|${r}|${c.length>0?c:""}|${o.length>0?o:""}|${d.length>0?d:""}|${w}|${a}`,inputDependencies:["rank"]},getShaderSource:$,getRunData:()=>({outputs:[{dims:l,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(h/64)},programUniforms:[{type:12,data:h},{type:1,data:c},{type:1,data:d},...V(a,l)]})}},vh=e=>{let t=e.customDataBuffer;return new Uint32Array(t,t.byteOffset,1)[0]},fl=(e,t)=>{let r=[],n=[],o=[],i=vh(e);if(t.antialias!==0)throw Error("Only default value (0) for Antialias attribute is supported");uh(e.inputs,t,i,r,n,o),e.compute(_h(e.inputs[0],t,i,r,n,o),{inputs:[0]})},hl=e=>{let t=e.antialias,r=e.axes,n=e.coordinateTransformMode,o=e.cubicCoeffA,i=e.excludeOutside!==0,a=e.extrapolationValue,d=e.keepAspectRatioPolicy,l=e.mode,c=e.nearestMode===""?"simple":e.nearestMode;return ee({antialias:t,axes:r,coordinateTransformMode:n,cubicCoeffA:o,excludeOutside:i,extrapolationValue:a,keepAspectRatioPolicy:d,mode:l,nearestMode:c})}});var $h,xh,yl,bl=U(()=>{"use strict";J();ae();Ie();se();$h=(e,t)=>{let[r,n,o,i]=e,{numHeads:a,rotaryEmbeddingDim:d}=t;if(r.dims.length!==3&&r.dims.length!==4)throw new Error(`Input 'x' is expected to have 3 or 4 dimensions, got ${r.dims.length}`);if(!k.areEqual(n.dims,[])&&!k.areEqual(n.dims,[1])&&n.dims.length!==2)throw new Error(`Input 'position_ids' is expected to have 0, 1, or 2 dimensions, got ${n.dims.length}`);if(o.dims.length!==2)throw new Error(`Input 'cos_cache' is expected to have 2 dimensions, got ${o.dims.length}`);if(i.dims.length!==2)throw new Error(`Input 'sin_cache' is expected to have 2 dimensions, got ${i.dims.length}`);if(!k.areEqual(o.dims,i.dims))throw new Error("Inputs 'cos_cache' and 'sin_cache' are expected to have the same shape");if(d>0&&a===0)throw new Error("num_heads must be provided if rotary_embedding_dim is specified");let l=r.dims[0],c=r.dims[r.dims.length-2],m=o.dims[0],u=k.sizeFromDimension(r.dims,1)/c,h=d===0?o.dims[1]*2:u/a;if(d>h)throw new Error("rotary_embedding_dim must be less than or equal to head_size");if(n.dims.length===2){if(l!==n.dims[0])throw new Error(`Input 'position_ids' dimension 0 should be of size batch_size, got ${n.dims[0]}`);if(c!==n.dims[1])throw new Error(`Input 'position_ids' dimension 1 should be of size sequence_length, got ${n.dims[1]}`)}if(h/2!==o.dims[1]&&d/2!==o.dims[1])throw new Error(`Input 'cos_cache' dimension 1 should be same as head_size / 2 or rotary_embedding_dim / 2, got ${o.dims[1]}`);if(c>m)throw new Error("Updating cos_cache and sin_cache in RotaryEmbedding is not currently supported")},xh=(e,t)=>{let{interleaved:r,numHeads:n,rotaryEmbeddingDim:o,scale:i}=t,a=e[0].dims[0],d=k.sizeFromDimension(e[0].dims,1),l=e[0].dims[e[0].dims.length-2],c=d/l,m=e[2].dims[1],u=o===0?m*2:c/n,h=new Array(a,l,c/u,u-m),w=k.computeStrides(h),g=[{type:1,data:i},{type:12,data:h},{type:12,data:w},...e[0].dims.length===3?new Array({type:12,data:[d,c,u,1]}):[],...e[0].dims.length===4?new Array({type:12,data:[d,u,l*u,1]}):[],...V(e[0].dims,e[1].dims,e[2].dims,e[3].dims,e[0].dims)],y=S=>{let $=E("input",e[0].dataType,e[0].dims.length),v=E("position_ids",e[1].dataType,e[1].dims.length),x=E("cos_cache",e[2].dataType,e[2].dims.length),T=E("sin_cache",e[3].dataType,e[3].dims.length),C=M("output",e[0].dataType,e[0].dims.length);return S.registerUniforms([{name:"scale",type:"f32"},{name:"global_shape",type:"u32",length:h.length},{name:"global_strides",type:"u32",length:w.length},{name:"input_output_strides",type:"u32",length:w.length}]),` + ${S.declareVariables($,v,x,T,C)} + + ${S.mainStart(At)} + let half_rotary_emb_dim = uniforms.${x.name}_shape[1]; + let bsnh = global_idx / uniforms.global_strides % uniforms.global_shape; + let size = uniforms.global_shape[0] * uniforms.global_strides[0]; + ${S.guardAgainstOutOfBoundsWorkgroupSizes("size")} + + if (bsnh[3] < half_rotary_emb_dim) { + let position_ids_idx = + ${v.broadcastedIndicesToOffset("bsnh.xy",M("",v.type.tensor,2))}; + let position_id = + u32(${v.getByOffset("position_ids_idx")}) + select(0, bsnh[1], position_ids_idx == 0); + let i = dot(bsnh, uniforms.input_output_strides) + select(0, bsnh[3], ${r}); + let j = i + select(half_rotary_emb_dim, 1, ${r}); + let re = ${$.getByOffset("i")} * ${x.get("position_id","bsnh[3]")} - + ${$.getByOffset("j")} * ${T.get("position_id","bsnh[3]")}; + ${C.setByOffset("i","re")} + let im = ${$.getByOffset("i")} * ${T.get("position_id","bsnh[3]")} + + ${$.getByOffset("j")} * ${x.get("position_id","bsnh[3]")}; + ${C.setByOffset("j","im")} + } else { + let k = dot(bsnh, uniforms.input_output_strides) + half_rotary_emb_dim; + ${C.setByOffset("k",$.getByOffset("k"))} + } + }`};return{name:"RotaryEmbedding",shaderCache:{hint:ee({interleaved:r}).cacheKey,inputDependencies:["rank","rank","rank","rank"]},getShaderSource:y,getRunData:()=>({outputs:[{dims:e[0].dims,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(k.size(h)/At)},programUniforms:g})}},yl=(e,t)=>{$h(e.inputs,t),e.compute(xh(e.inputs,t))}});var Sh,Th,wl,_l=U(()=>{"use strict";J();ae();se();Sh=e=>{if(!e||e.length<3)throw new Error("layerNorm requires at least 3 inputs.");let t=e[0],r=e[1],n=e[2];if(t.dataType!==r.dataType||t.dataType!==n.dataType)throw new Error("All inputs must have the same data type");if(t.dims.length!==3&&t.dims.length!==2)throw new Error("Input must be 2D or 3D");if(r.dims.length!==3&&r.dims.length!==2)throw new Error("Skip must be 2D or 3D");let o=t.dims[t.dims.length-1],i=t.dims[t.dims.length-2];if(r.dims[r.dims.length-1]!==o)throw new Error("Skip must have the same hidden size as input");if(r.dims[r.dims.length-2]!==i)throw new Error("Skip must have the same sequence length as input");if(n.dims.length!==1)throw new Error("Gamma must be 1D");if(n.dims[n.dims.length-1]!==o)throw new Error("Gamma must have the same hidden size as input");if(e.length>3){let a=e[3];if(a.dims.length!==1)throw new Error("Beta must be 1D");if(a.dims[a.dims.length-1]!==o)throw new Error("Beta must have the same hidden size as input")}if(e.length>4){let a=e[4];if(a.dims.length!==1)throw new Error("Bias must be 1D");if(a.dims[a.dims.length-1]!==o)throw new Error("Bias must have the same hidden size as input")}},Th=(e,t,r,n)=>{let o=t.simplified,i=e[0].dims,a=k.size(i),d=i,l=a,c=i.slice(-1)[0],m=n?i.slice(0,-1).concat(1):[],u=!o&&e.length>3,h=e.length>4,w=n&&r>1,g=n&&r>2,y=r>3,S=64,$=we(c),v=[{type:12,data:l},{type:12,data:$},{type:12,data:c},{type:1,data:t.epsilon}],x=C=>{let A=[{name:"output_size",type:"u32"},{name:"components",type:"u32"},{name:"hidden_size",type:"u32"},{name:"epsilon",type:"f32"}],P=[E("x",e[0].dataType,e[0].dims,$),E("skip",e[1].dataType,e[1].dims,$),E("gamma",e[2].dataType,e[2].dims,$)];u&&P.push(E("beta",e[3].dataType,e[3].dims,$)),h&&P.push(E("bias",e[4].dataType,e[4].dims,$)),P.push(M("output",e[0].dataType,d,$)),w&&P.push(M("mean_output",1,m)),g&&P.push(M("inv_std_output",1,m)),y&&P.push(M("input_skip_bias_sum",e[0].dataType,d,$));let D=he(e[0].dataType),W=he(1,$);return` + + ${C.registerUniforms(A).declareVariables(...P)} + var sum_shared : array<${W}, ${S}>; + var sum_squared_shared : array<${W}, ${S}>; + + ${C.mainStart([S,1,1])} + let ix = local_id.x; + let iy = global_id.x / ${S}; + + let hidden_size_vectorized: u32 = uniforms.hidden_size / uniforms.components; + var stride = hidden_size_vectorized / ${S}; + let offset = ix * stride + iy * hidden_size_vectorized; + let offset1d = stride * ix; + if (ix == ${S-1}) { + stride = hidden_size_vectorized - stride * ix; + } + for (var i: u32 = 0; i < stride; i++) { + let skip_value = skip[offset + i]; + let bias_value = ${h?"bias[offset1d + i]":D+"(0.0)"}; + let input_value = x[offset + i]; + let value = input_value + skip_value + bias_value; + ${y?"input_skip_bias_sum[offset + i] = value;":""} + output[offset + i] = value; + let f32_value = ${kt(D,$,"value")}; + sum_shared[ix] += f32_value; + sum_squared_shared[ix] += f32_value * f32_value; + } + workgroupBarrier(); + + var reduce_size : u32 = ${S}; + for (var curr_size = reduce_size >> 1; curr_size > 0; curr_size = reduce_size >> 1) { + reduce_size = curr_size + (reduce_size & 1); + if (ix < curr_size) { + sum_shared[ix] += sum_shared[ix + reduce_size]; + sum_squared_shared[ix] += sum_squared_shared[ix + reduce_size]; + } + workgroupBarrier(); + } + + let sum = sum_shared[0]; + let square_sum = sum_squared_shared[0]; + let mean = ${Qe("sum",$)} / f32(uniforms.hidden_size); + let inv_std_dev = inverseSqrt(${Qe("square_sum",$)} / f32(uniforms.hidden_size) ${o?"":"- mean * mean"} + uniforms.epsilon); + ${w?"mean_output[global_idx] = mean;":""} + ${g?"inv_std_output[global_idx] = inv_std_dev;":""} + + for (var i: u32 = 0; i < stride; i++) { + output[offset + i] = (output[offset + i] ${o?"":`- ${D}(mean)`}) * + ${D}(inv_std_dev) * gamma[offset1d + i] + ${u?"+ beta[offset1d + i]":""}; + } + }`},T=[{dims:d,dataType:e[0].dataType}];return r>1&&T.push({dims:m,dataType:1}),r>2&&T.push({dims:m,dataType:1}),r>3&&T.push({dims:i,dataType:e[0].dataType}),{name:"SkipLayerNormalization",shaderCache:{hint:`${$};${w};${g};${y}`,inputDependencies:e.map((C,A)=>"type")},getShaderSource:x,getRunData:()=>({outputs:T,dispatchGroup:{x:Math.ceil(l/c)},programUniforms:v})}},wl=(e,t)=>{Sh(e.inputs);let n=[0];e.outputCount>1&&n.push(-3),e.outputCount>2&&n.push(-3),e.outputCount>3&&n.push(3),e.compute(Th(e.inputs,t,e.outputCount,!1),{outputs:n})}});var Ih,on,Ch,vl,Ah,kh,$l,xl,Sl=U(()=>{"use strict";J();ae();Ie();se();Ih=(e,t)=>{if(!e||e.length<1)throw new Error("too few inputs");if(t.axes.length!==0){if(t.axes.length!==t.starts.length||t.axes.length!==t.ends.length)throw new Error("axes, starts and ends must have the same length")}else if(t.starts.length!==t.ends.length)throw new Error("starts and ends must have the same length");e.slice(1).forEach((r,n)=>{if(e[n+1].dataType!==6&&e[n+1].dataType!==7)throw new Error(`Input ${n} must be an array of int32 or int64`)})},on=(e,t)=>{let r=[];if(e.length>t)if(e[t].dataType===7)e[t].getBigInt64Array().forEach(n=>r.push(Number(n)));else if(e[t].dataType===6)e[t].getInt32Array().forEach(n=>r.push(Number(n)));else throw new Error(`Input ${t} must be an array of int32 or int64`);return r},Ch=(e,t)=>{if(e.length>1){let r=on(e,1),n=on(e,2),o=on(e,3);return o.length===0&&(o=[...Array(e[0].dims.length).keys()]),ee({starts:r,ends:n,axes:o})}else return t},vl=(e,t,r,n,o)=>{let i=e;return e<0&&(i+=r[n[t]]),o[t]<0?Math.max(0,Math.min(i,r[n[t]]-1)):Math.max(0,Math.min(i,r[n[t]]))},Ah=(e,t,r)=>`fn calculateInputIndices(output_indices: ${t.type.indices}) -> ${e.type.indices} { + var input_indices: ${e.type.indices}; + var carry = 0u; + for (var i = ${r.length}; i >= 0; i--) { + let input_shape_i = ${F("uniforms.input_shape","i",r.length)}; + let steps_i = ${F("uniforms.steps","i",r.length)}; + let signs_i = ${F("uniforms.signs","i",r.length)}; + let starts_i = ${F("uniforms.starts","i",r.length)}; + var output_index = ${t.indicesGet("output_indices","i")}; + var input_index = output_index * steps_i + starts_i + carry; + carry = input_index / input_shape_i; + input_index = input_index % input_shape_i; + if (signs_i < 0) { + input_index = input_shape_i - input_index - 1u + starts_i; + } + ${e.indicesSet("input_indices","i","input_index")}; + } + return input_indices; + }`,kh=(e,t)=>{let r=e[0].dims,n=k.size(r),o=t.axes.length>0?k.normalizeAxes(t.axes,r.length):[...Array(r.length).keys()],i=on(e,4);i.forEach($=>$!==0||(()=>{throw new Error("step cannot be 0")})),i.length===0&&(i=Array(o.length).fill(1));let a=t.starts.map(($,v)=>vl($,v,r,o,i)),d=t.ends.map(($,v)=>vl($,v,r,o,i));if(o.length!==a.length||o.length!==d.length)throw new Error("start, ends and axes should have the same number of elements");if(o.length!==r.length)for(let $=0;$Math.sign($));i.forEach(($,v,x)=>{if($<0){let T=(d[v]-a[v])/$,C=a[v],A=C+T*i[v];a[v]=A,d[v]=C,x[v]=-$}});let c=r.slice(0);o.forEach(($,v)=>{c[$]=Math.ceil((d[$]-a[$])/i[$])});let m={dims:c,dataType:e[0].dataType},u=M("output",e[0].dataType,c.length),h=E("input",e[0].dataType,e[0].dims.length),w=k.size(c),g=[{name:"outputSize",type:"u32"},{name:"starts",type:"u32",length:a.length},{name:"signs",type:"i32",length:l.length},{name:"steps",type:"u32",length:i.length}],y=[{type:12,data:w},{type:12,data:a},{type:6,data:l},{type:12,data:i},...V(e[0].dims,c)],S=$=>` + ${$.registerUniforms(g).declareVariables(h,u)} + ${Ah(h,u,r)} + ${$.mainStart()} + ${$.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + let output_indices = ${u.offsetToIndices("global_idx")}; + let input_indices = calculateInputIndices(output_indices); + ${u.setByOffset("global_idx",h.getByIndices("input_indices"))} + }`;return{name:"Slice",shaderCache:{hint:`${l.length}_${a.length}_${i.length}`,inputDependencies:["rank"]},getShaderSource:S,getRunData:()=>({outputs:[m],dispatchGroup:{x:Math.ceil(n/64)},programUniforms:y})}},$l=(e,t)=>{Ih(e.inputs,t);let r=Ch(e.inputs,t);e.compute(kh(e.inputs,r),{inputs:[0]})},xl=e=>{let t=e.starts,r=e.ends,n=e.axes;return ee({starts:t,ends:r,axes:n})}});var Eh,Ph,Tl,Il,Cl=U(()=>{"use strict";J();ae();Ie();lt();se();Eh=e=>{if(!e||e.length!==1)throw new Error("Softmax op requires 1 input.")},Ph=(e,t)=>{let r=e.inputs[0],n=r.dims,o=k.size(n),i=64,a=n.length,d=k.normalizeAxis(t.axis,a),l=dD),m[d]=a-1,m[a-1]=d,c=e.compute(Pe(r,m),{inputs:[r],outputs:[-1]})[0]):c=r;let u=c.dims,h=u[a-1],w=o/h,g=we(h),y=h/g,S=(P,D)=>D===4?`max(max(${P}.x, ${P}.y), max(${P}.z, ${P}.w))`:D===2?`max(${P}.x, ${P}.y)`:D===3?`max(max(${P}.x, ${P}.y), ${P}.z)`:P,$=E("x",c.dataType,c.dims,g),v=M("result",c.dataType,c.dims,g),x=$.type.value,T=he(c.dataType)==="f32"?`var threadMax = ${x}(-3.402823e+38f);`:`var threadMax = ${x}(-65504.0h);`,C=P=>` + var rowMaxShared : ${x}; + var rowSumShared : ${x}; + var threadShared : array<${x}, ${i}>; + + fn getValue(row: i32, col: i32, row_stride: i32) -> ${x} { + let index = row * row_stride + col; + return x[index]; + } + + fn setValue(row: i32, col: i32, row_stride: i32, value: ${x}) { + let index = row * row_stride + col; + result[index] = value; + } + ${P.registerUniform("packedCols","i32").declareVariables($,v)} + ${P.mainStart()} + let gindex = i32(global_idx); + let lindex = i32(local_idx); + const wg = ${i}; + let row = gindex / wg; + let cols = uniforms.packedCols; + let row_stride : i32 = uniforms.packedCols; + + // find the rows max + ${T} + for (var col = lindex; col < cols; col += wg) { + let value = getValue(row, col, row_stride); + threadMax = max(threadMax, value); + } + if (lindex < cols) { + threadShared[lindex] = threadMax; + } + workgroupBarrier(); + + var reduceSize = min(cols, wg); + for (var currSize = reduceSize >> 1; currSize > 0; currSize = reduceSize >> 1) { + reduceSize = currSize + (reduceSize & 1); + if (lindex < currSize) { + threadShared[lindex] = max(threadShared[lindex], threadShared[lindex + reduceSize]); + } + workgroupBarrier(); + } + if (lindex == 0) { + rowMaxShared = ${x}(${S("threadShared[0]",g)}); + } + workgroupBarrier(); + + // find the rows sum + var threadSum = ${x}(0.0); + for (var col = lindex; col < cols; col += wg) { + let subExp = exp(getValue(row, col, row_stride) - rowMaxShared); + threadSum += subExp; + } + threadShared[lindex] = threadSum; + workgroupBarrier(); + + for (var currSize = wg >> 1; currSize > 0; currSize = currSize >> 1) { + if (lindex < currSize) { + threadShared[lindex] = threadShared[lindex] + threadShared[lindex + currSize]; + } + workgroupBarrier(); + } + if (lindex == 0) { + rowSumShared = ${x}(${Qe("threadShared[0]",g)}); + } + workgroupBarrier(); + + // calculate final value for each element in the row + for (var col = lindex; col < cols; col += wg) { + let value = exp(getValue(row, col, row_stride) - rowMaxShared) / rowSumShared; + setValue(row, col, row_stride, value); + } + }`,A=e.compute({name:"Softmax",shaderCache:{hint:`${g}`,inputDependencies:["type"]},getRunData:()=>({outputs:[{dims:u,dataType:c.dataType}],dispatchGroup:{x:w},programUniforms:[{type:6,data:y}]}),getShaderSource:C},{inputs:[c],outputs:[l?-1:0]})[0];l&&e.compute(Pe(A,m),{inputs:[A]})},Tl=(e,t)=>{Eh(e.inputs),Ph(e,t)},Il=e=>ee({axis:e.axis})});var Al,zh,Oh,Dh,kl,El=U(()=>{"use strict";J();ae();se();Al=e=>Array.from(e.getBigInt64Array(),Number),zh=e=>{if(!e||e.length!==2)throw new Error("Tile requires 2 inputs.");if(e[0].dataType!==1&&e[0].dataType!==10&&e[0].dataType!==6&&e[0].dataType!==12)throw new Error("Tile only support float, float16, int32, and uint32 data types");if(e[1].dataType!==7)throw new Error("Tile `repeats` input should be of int64 data type");if(e[1].dims.length!==1)throw new Error("Tile `repeats` input should be 1-D");if(Al(e[1]).length!==e[0].dims.length)throw new Error("Tile `repeats` input should have same number of elements as rank of input data tensor")},Oh=(e,t)=>{let r=[];for(let n=0;n{let r=e[0].dims,n=t??Al(e[1]),o=Oh(r,n),i=k.size(o),a=e[0].dataType,d=E("input",a,r.length),l=M("output",a,o.length),c=m=>` + const inputShape = ${d.indices(...r)}; + ${m.registerUniform("output_size","u32").declareVariables(d,l)} + ${m.mainStart()} + ${m.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + let output_indices = ${l.offsetToIndices("global_idx")}; + var input_indices: ${d.type.indices}; + for (var i = 0; i < ${r.length}; i++) { + let input_dim_i = ${d.indicesGet("uniforms.input_shape","i")}; + let input_dim_value = ${l.indicesGet("output_indices","i")} % input_dim_i; + + ${d.indicesSet("input_indices","i","input_dim_value")} + } + ${l.setByOffset("global_idx",d.getByIndices("input_indices"))} + }`;return{name:"Tile",shaderCache:{hint:`${n}`,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:o,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(i/64)},programUniforms:[{type:12,data:i},...V(e[0].dims,o)]}),getShaderSource:c}},kl=e=>{zh(e.inputs),e.compute(Dh(e.inputs),{inputs:[0]})}});var Bh,Mh,Pl,zl=U(()=>{"use strict";J();ae();se();Bh=(e,t,r,n,o)=>{let i=M("output_data",o,r.length,4),a=E("a_data",t[1].dataType,t[1].dims.length,4),d=E("b_data",t[2].dataType,t[2].dims.length,4),l=E("c_data",t[0].dataType,t[0].dims.length,4),c,m=(u,h,w)=>`select(${h}, ${u}, ${w})`;if(!n)c=i.setByOffset("global_idx",m(a.getByOffset("global_idx"),d.getByOffset("global_idx"),l.getByOffset("global_idx")));else{let u=(h,w,g="")=>{let y=`a_data[index_a${w}][component_a${w}]`,S=`b_data[index_b${w}][component_b${w}]`,$=`bool(c_data[index_c${w}] & (0xffu << (component_c${w} * 8)))`;return` + let output_indices${w} = ${i.offsetToIndices(`global_idx * 4u + ${w}u`)}; + let offset_a${w} = ${a.broadcastedIndicesToOffset(`output_indices${w}`,i)}; + let offset_b${w} = ${d.broadcastedIndicesToOffset(`output_indices${w}`,i)}; + let offset_c${w} = ${l.broadcastedIndicesToOffset(`output_indices${w}`,i)}; + let index_a${w} = offset_a${w} / 4u; + let index_b${w} = offset_b${w} / 4u; + let index_c${w} = offset_c${w} / 4u; + let component_a${w} = offset_a${w} % 4u; + let component_b${w} = offset_b${w} % 4u; + let component_c${w} = offset_c${w} % 4u; + ${h}[${w}] = ${g}(${m(y,S,$)}); + `};o===9?c=` + var data = vec4(0); + ${u("data",0,"u32")} + ${u("data",1,"u32")} + ${u("data",2,"u32")} + ${u("data",3,"u32")} + output_data[global_idx] = dot(vec4(0x1, 0x100, 0x10000, 0x1000000), vec4(data));`:c=` + ${u("output_data[global_idx]",0)} + ${u("output_data[global_idx]",1)} + ${u("output_data[global_idx]",2)} + ${u("output_data[global_idx]",3)} + `}return` + ${e.registerUniform("vec_size","u32").declareVariables(l,a,d,i)} + ${e.mainStart()} + ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")} + ${c} + }`},Mh=e=>{let t=e[1].dims,r=e[2].dims,n=e[0].dims,o=e[1].dataType,i=!(k.areEqual(t,r)&&k.areEqual(r,n)),a=t,d=k.size(t);if(i){let c=rt.calcShape(rt.calcShape(t,r,!1),n,!1);if(!c)throw new Error("Can't perform where op on the given tensors");a=c,d=k.size(a)}let l=Math.ceil(d/4);return{name:"Where",shaderCache:{inputDependencies:["rank","rank","rank"]},getShaderSource:c=>Bh(c,e,a,i,o),getRunData:()=>({outputs:[{dims:a,dataType:o}],dispatchGroup:{x:Math.ceil(d/64/4)},programUniforms:[{type:12,data:l},...V(n,t,r,a)]})}},Pl=e=>{e.compute(Mh(e.inputs))}});var Ol,Dl=U(()=>{"use strict";Cs();Kr();Es();zs();yu();Au();Pu();Fu();Zu();td();od();dd();pd();fd();yd();_d();xd();Id();Dd();Rd();Vd();go();Ld();To();Gd();sl();ll();pl();qr();gl();bl();_l();Sl();Cl();Co();El();lt();Xr();zl();Ol=new Map([["Abs",[Os]],["Acos",[Ds]],["Acosh",[Bs]],["Add",[bu]],["ArgMax",[Is,lo]],["ArgMin",[Ts,lo]],["Asin",[Ms]],["Asinh",[Rs]],["Atan",[Us]],["Atanh",[Vs]],["Attention",[As]],["AveragePool",[Zd,Qd]],["BatchNormalization",[ks]],["BiasAdd",[Ps]],["BiasSplitGelu",[gu]],["Cast",[Ns,Ws]],["Ceil",[Hs]],["Clip",[Ls]],["Concat",[ku,Eu]],["Conv",[_o,wo]],["ConvTranspose",[Qu,Xu]],["Cos",[Gs]],["Cosh",[Fs]],["CumSum",[Ju,ed]],["DepthToSpace",[rd,nd]],["DequantizeLinear",[ul,dl]],["Div",[wu]],["Einsum",[sd,ud]],["Elu",[qs,Zt]],["Equal",[_u]],["Erf",[js]],["Exp",[Ks]],["Expand",[cd]],["FastGelu",[md]],["Floor",[Ys]],["FusedConv",[_o,wo]],["Gather",[gd,hd]],["GatherElements",[$d,vd]],["GatherBlockQuantized",[bd,wd]],["Gelu",[Xs]],["Gemm",[Td,Sd]],["GlobalAveragePool",[tl,el]],["GlobalMaxPool",[al,il]],["Greater",[Su]],["GreaterOrEqual",[Iu]],["GroupQueryAttention",[Od]],["HardSigmoid",[ou,nu]],["InstanceNormalization",[Md]],["LayerNormalization",[Ud]],["LeakyRelu",[Qs,Zt]],["Less",[Tu]],["LessOrEqual",[Cu]],["Log",[mu]],["MatMul",[Hu]],["MatMulNBits",[Wd,Nd]],["MaxPool",[nl,ol]],["Mul",[vu]],["MultiHeadAttention",[kd,Ad]],["Neg",[Js]],["Not",[Zs]],["Pad",[Hd]],["Pow",[$u]],["QuickGelu",[fu,Zt]],["Range",[cl]],["Reciprocal",[eu]],["ReduceMin",[ws]],["ReduceMean",[fs]],["ReduceMax",[bs]],["ReduceSum",[vs]],["ReduceProd",[_s]],["ReduceL1",[hs]],["ReduceL2",[gs]],["ReduceLogSum",[xs]],["ReduceLogSumExp",[ys]],["ReduceSumSquare",[$s]],["Relu",[tu]],["Resize",[fl,hl]],["RotaryEmbedding",[yl]],["Sigmoid",[ru]],["Sin",[iu]],["Sinh",[au]],["Slice",[$l,xl]],["SkipLayerNormalization",[wl]],["Split",[Ed,Pd]],["Sqrt",[su]],["Softmax",[Tl,Il]],["Sub",[xu]],["Tan",[uu]],["Tanh",[lu]],["ThresholdedRelu",[pu,Zt]],["Tile",[kl]],["Transpose",[ts,rs]],["Where",[Pl]]])});var an,Bl=U(()=>{"use strict";Ke();Xe();se();an=class{constructor(t){this.backend=t;this.repo=new Map,this.attributesBound=!1}getArtifact(t){return this.repo.get(t)}setArtifact(t,r){this.repo.set(t,r)}run(t,r,n,o,i){Le(t.programInfo.name);let a=this.backend.device,d=this.backend.getComputePassEncoder();this.backend.writeTimestamp(this.backend.pendingDispatchNumber*2);let l=[];for(let m of r)l.push({binding:l.length,resource:{buffer:m.buffer}});for(let m of n)l.push({binding:l.length,resource:{buffer:m.buffer}});i&&l.push({binding:l.length,resource:i});let c=a.createBindGroup({layout:t.computePipeline.getBindGroupLayout(0),entries:l,label:t.programInfo.name});if(this.backend.sessionStatus==="capturing"){let m={kernelId:this.backend.currentKernelId,computePipeline:t.computePipeline,bindGroup:c,dispatchGroup:o};this.backend.capturedCommandList.get(this.backend.currentSessionId).push(m)}d.setPipeline(t.computePipeline),d.setBindGroup(0,c),d.dispatchWorkgroups(...o),this.backend.writeTimestamp(this.backend.pendingDispatchNumber*2+1),this.backend.pendingDispatchNumber++,(this.backend.pendingDispatchNumber>=this.backend.maxDispatchNumber||this.backend.queryType==="at-passes")&&this.backend.endComputePass(),this.backend.pendingDispatchNumber>=this.backend.maxDispatchNumber&&this.backend.flush(),Ve(t.programInfo.name)}dispose(){}build(t,r){Le(t.name);let n=this.backend.device,o=[];n.features.has("shader-f16")&&o.push("enable f16;");let i=Ja(r,this.backend.device.limits),a=t.getShaderSource(i),d=`${o.join(` +`)} +${i.additionalImplementations} +${a}`,l=n.createShaderModule({code:d,label:t.name});pe("verbose",()=>`[WebGPU] ${t.name} shader code: ${d}`);let c=n.createComputePipeline({compute:{module:l,entryPoint:"main"},layout:"auto",label:t.name});return Ve(t.name),{programInfo:t,computePipeline:c,uniformVariablesInfo:i.variablesInfo}}normalizeDispatchGroupSize(t){let r=typeof t=="number"?t:t.x,n=typeof t=="number"?1:t.y||1,o=typeof t=="number"?1:t.z||1,i=this.backend.device.limits.maxComputeWorkgroupsPerDimension;if(r<=i&&n<=i&&o<=i)return[r,n,o];let a=r*n*o,d=Math.ceil(Math.sqrt(a));if(d>i){if(d=Math.ceil(Math.cbrt(a)),d>i)throw new Error("Total dispatch size exceeds WebGPU maximum.");return[d,d,d]}else return[d,d,1]}}});var Rh,Uh,Ao,sn,Ml=U(()=>{"use strict";Ke();J();Xe();Qn();Xa();Dl();Bl();Rh=(e,t)=>{if(t.length!==e.length)throw new Error(`inputDependencies length ${t.length} is not equal to inputTensors length ${e.length}.`);let r=[];for(let n=0;n{let n=e.name;return e.shaderCache?.hint&&(n+="["+e.shaderCache.hint+"]"),n+=":"+r+`:${Rh(t,e.shaderCache?.inputDependencies??new Array(t.length).fill("dims"))}`,n},Ao=class{constructor(t){t&&(this.architecture=t.architecture,this.vendor=t.vendor)}isArchitecture(t){return this.architecture===t}isVendor(t){return this.vendor===t}},sn=class{constructor(){this.currentSessionId=null;this.currentKernelId=null;this.commandEncoder=null;this.computePassEncoder=null;this.maxDispatchNumber=16;this.pendingDispatchNumber=0;this.pendingKernels=[];this.pendingQueries=new Map;this.sessionStatus="default";this.capturedCommandList=new Map;this.capturedPendingKernels=new Map;this.sessionExternalDataMapping=new Map}get currentKernelCustomData(){if(this.currentKernelId===null)throw new Error("currentKernelCustomData(): currentKernelId is null. (should not happen)");let t=this.kernelCustomData.get(this.currentKernelId);return t||(t={},this.kernelCustomData.set(this.currentKernelId,t)),t}async initialize(t,r){this.env=t;let n=[],o={requiredLimits:{maxComputeWorkgroupStorageSize:r.limits.maxComputeWorkgroupStorageSize,maxComputeWorkgroupsPerDimension:r.limits.maxComputeWorkgroupsPerDimension,maxStorageBufferBindingSize:r.limits.maxStorageBufferBindingSize,maxBufferSize:r.limits.maxBufferSize,maxComputeInvocationsPerWorkgroup:r.limits.maxComputeInvocationsPerWorkgroup,maxComputeWorkgroupSizeX:r.limits.maxComputeWorkgroupSizeX,maxComputeWorkgroupSizeY:r.limits.maxComputeWorkgroupSizeY,maxComputeWorkgroupSizeZ:r.limits.maxComputeWorkgroupSizeZ},requiredFeatures:n};r.features.has("chromium-experimental-timestamp-query-inside-passes")?n.push("chromium-experimental-timestamp-query-inside-passes"):r.features.has("timestamp-query")&&n.push("timestamp-query"),r.features.has("shader-f16")&&n.push("shader-f16"),this.device=await r.requestDevice(o),this.adapterInfo=new Ao(r.info||await r.requestAdapterInfo()),this.gpuDataManager=Ya(this),this.programManager=new an(this),this.kernels=new Map,this.kernelPersistentData=new Map,this.kernelCustomData=new Map,Nr(t.logLevel,!!t.debug),this.device.onuncapturederror=i=>{i.error instanceof GPUValidationError&&console.error(`An uncaught WebGPU validation error was raised: ${i.error.message}`)},Object.defineProperty(this.env.webgpu,"device",{value:this.device,writable:!1,enumerable:!0,configurable:!1}),Object.defineProperty(this.env.webgpu,"adapter",{value:r,writable:!1,enumerable:!0,configurable:!1}),this.setQueryType()}dispose(){typeof this.querySet<"u"&&this.querySet.destroy(),this.gpuDataManager.dispose()}getCommandEncoder(){return this.commandEncoder||(this.commandEncoder=this.device.createCommandEncoder()),this.commandEncoder}getComputePassEncoder(){if(!this.computePassEncoder){let t=this.getCommandEncoder(),r={};this.queryType==="at-passes"&&(r.timestampWrites={querySet:this.querySet,beginningOfPassWriteIndex:this.pendingDispatchNumber*2,endOfPassWriteIndex:this.pendingDispatchNumber*2+1}),this.computePassEncoder=t.beginComputePass(r)}return this.computePassEncoder}endComputePass(){this.computePassEncoder&&(this.computePassEncoder.end(),this.computePassEncoder=null)}flush(){if(!this.commandEncoder)return;Le(),this.endComputePass();let t;this.queryType!=="none"&&(this.commandEncoder.resolveQuerySet(this.querySet,0,this.pendingDispatchNumber*2,this.queryResolveBuffer,0),t=this.device.createBuffer({size:this.pendingDispatchNumber*2*8,usage:GPUBufferUsage.MAP_READ|GPUBufferUsage.COPY_DST}),this.pendingQueries.set(t,this.pendingKernels),this.pendingKernels=[],this.commandEncoder.copyBufferToBuffer(this.queryResolveBuffer,0,t,0,this.pendingDispatchNumber*2*8)),this.device.queue.submit([this.commandEncoder.finish()]),this.gpuDataManager.refreshPendingBuffers(),this.commandEncoder=null,this.pendingDispatchNumber=0,this.queryType!=="none"&&t.mapAsync(GPUMapMode.READ).then(()=>{let r=new BigUint64Array(t.getMappedRange()),n=this.pendingQueries.get(t);for(let o=0;o"u"&&(this.queryTimeBase=w);let y=Number(w-this.queryTimeBase),S=Number(g-this.queryTimeBase);if(!Number.isSafeInteger(y)||!Number.isSafeInteger(S))throw new RangeError("incorrect timestamp range");if(this.env.webgpu.profiling?.ondata)this.env.webgpu.profiling.ondata({version:1,inputsMetadata:u.map($=>({dims:$.dims,dataType:yt($.dataType)})),outputsMetadata:h.map($=>({dims:$.dims,dataType:yt($.dataType)})),kernelId:a,kernelType:l,kernelName:c,programName:m,startTime:y,endTime:S});else{let $="";u.forEach((x,T)=>{$+=`input[${T}]: [${x.dims}] | ${yt(x.dataType)}, `});let v="";h.forEach((x,T)=>{v+=`output[${T}]: [${x.dims}] | ${yt(x.dataType)}, `}),console.log(`[profiling] kernel "${a}|${l}|${c}|${m}" ${$}${v}execution time: ${S-y} ns`)}Sr("GPU",`${m}::${w}::${g}`)}t.unmap(),this.pendingQueries.delete(t)}),Ve()}run(t,r,n,o,i,a){Le(t.name);let d=[];for(let x=0;xT):n;if(u.length!==l.length)throw new Error(`Output size ${u.length} must be equal to ${l.length}.`);let h=[],w=[];for(let x=0;x=a)throw new Error(`Invalid output index: ${u[x]}`);if(u[x]===-3)continue;let T=u[x]===-1,C=u[x]===-2,A=T||C?i(l[x].dataType,l[x].dims):o(u[x],l[x].dataType,l[x].dims);if(h.push(A),A.data===0)continue;let P=this.gpuDataManager.get(A.data);if(!P)throw new Error(`no GPU data for output: ${A.data}`);if(T&&this.temporaryData.push(P),C){let D=this.kernelPersistentData.get(this.currentKernelId);D||(D=[],this.kernelPersistentData.set(this.currentKernelId,D)),D.push(P)}w.push(P)}if(d.length!==r.length||w.length!==h.length){if(w.length===0)return Ve(t.name),h;throw new Error(`Program ${t.name} has zero-sized tensor(s) in inputs or outputs. This is not supported now.`)}let g;if(m){let x=0,T=[];m.forEach(D=>{let W=typeof D.data=="number"?[D.data]:D.data;if(W.length===0)return;let N=D.type===10?2:4,j,Y;D.type===10?(Y=W.length>4?16:W.length>2?8:W.length*N,j=W.length>4?16:N*W.length):(Y=W.length<=2?W.length*N:16,j=16),x=Math.ceil(x/Y)*Y,T.push(x);let Z=D.type===10?8:4;x+=W.length>4?Math.ceil(W.length/Z)*j:W.length*N});let C=16;x=Math.ceil(x/C)*C;let A=new ArrayBuffer(x);m.forEach((D,W)=>{let N=T[W],j=typeof D.data=="number"?[D.data]:D.data;if(D.type===6)new Int32Array(A,N,j.length).set(j);else if(D.type===12)new Uint32Array(A,N,j.length).set(j);else if(D.type===10)new Uint16Array(A,N,j.length).set(j);else if(D.type===1)new Float32Array(A,N,j.length).set(j);else throw new Error(`Unsupported uniform type: ${yt(D.type)}`)});let P=this.gpuDataManager.create(x,GPUBufferUsage.COPY_DST|GPUBufferUsage.UNIFORM);this.device.queue.writeBuffer(P.buffer,0,A,0,x),this.gpuDataManager.release(P.id),g={offset:0,size:x,buffer:P.buffer}}let y=this.programManager.normalizeDispatchGroupSize(c),S=y[1]===1&&y[2]===1,$=Uh(t,r,S),v=this.programManager.getArtifact($);if(v||(v=this.programManager.build(t,y),this.programManager.setArtifact($,v),pe("info",()=>`[artifact] key: ${$}, programName: ${t.name}`)),m&&v.uniformVariablesInfo){if(m.length!==v.uniformVariablesInfo.length)throw new Error(`Uniform variables count mismatch: expect ${v.uniformVariablesInfo.length}, got ${m.length} in program "${v.programInfo.name}".`);for(let x=0;x`[ProgramManager] run "${t.name}" (key=${$}) with ${y[0]}x${y[1]}x${y[2]}`),this.queryType!=="none"||this.sessionStatus==="capturing"){let x={kernelId:this.currentKernelId,programName:v.programInfo.name,inputTensorViews:r,outputTensorViews:h};this.pendingKernels.push(x),this.sessionStatus==="capturing"&&this.capturedPendingKernels.get(this.currentSessionId).push(x)}return this.programManager.run(v,d,w,y,g),Ve(t.name),h}upload(t,r){this.gpuDataManager.upload(t,r)}memcpy(t,r){this.gpuDataManager.memcpy(t,r)}async download(t,r){await this.gpuDataManager.download(t,r)}alloc(t){return this.gpuDataManager.create(t).id}free(t){return this.gpuDataManager.release(t)}createKernel(t,r,n,o){let i=Ol.get(t);if(!i)throw new Error(`kernel not implemented: ${t}`);let a={kernelType:t,kernelName:o,kernelEntry:i[0],attributes:[i[1],n]};this.kernels.set(r,a)}releaseKernel(t){let r=this.kernelPersistentData.get(t);if(r){for(let n of r)this.gpuDataManager.release(n.id);this.kernelPersistentData.delete(t)}this.kernelCustomData.delete(t),this.kernels.delete(t)}computeKernel(t,r,n){let o=this.kernels.get(t);if(!o)throw new Error(`kernel not created: ${t}`);let i=o.kernelType,a=o.kernelName,d=o.kernelEntry,l=o.attributes;if(this.currentKernelId!==null)throw new Error(`kernel "[${i}] ${a}" is not allowed to be called recursively`);this.currentKernelId=t,l[0]&&(l[1]=l[0](l[1]),l[0]=void 0),pe("info",()=>`[WebGPU] Start to run kernel "[${i}] ${a}"...`);let c=this.env.debug;this.temporaryData=[];try{return c&&this.device.pushErrorScope("validation"),d(r,l[1]),0}catch(m){return n.push(Promise.resolve(`[WebGPU] Kernel "[${i}] ${a}" failed. ${m}`)),1}finally{c&&n.push(this.device.popErrorScope().then(m=>m?`GPU validation error for kernel "[${i}] ${a}": ${m.message}`:null));for(let m of this.temporaryData)this.gpuDataManager.release(m.id);this.temporaryData=[],this.currentKernelId=null}}registerBuffer(t,r,n,o){let i=this.sessionExternalDataMapping.get(t);i||(i=new Map,this.sessionExternalDataMapping.set(t,i));let a=i.get(r),d=this.gpuDataManager.registerExternalBuffer(n,o,a);return i.set(r,[d,n]),d}unregisterBuffers(t){let r=this.sessionExternalDataMapping.get(t);r&&(r.forEach(n=>this.gpuDataManager.unregisterExternalBuffer(n[0])),this.sessionExternalDataMapping.delete(t))}getBuffer(t){let r=this.gpuDataManager.get(t);if(!r)throw new Error(`no GPU data for buffer: ${t}`);return r.buffer}createDownloader(t,r,n){return async()=>{let o=await to(this,t,r);return Lr(o.buffer,n)}}writeTimestamp(t){this.queryType==="inside-passes"&&this.computePassEncoder.writeTimestamp(this.querySet,t)}setQueryType(){this.queryType="none",(this.env.webgpu.profiling?.mode==="default"||(typeof this.env.trace>"u"?this.env.wasm.trace:this.env.trace))&&(this.device.features.has("chromium-experimental-timestamp-query-inside-passes")?this.queryType="inside-passes":this.device.features.has("timestamp-query")&&(this.queryType="at-passes"),this.queryType!=="none"&&typeof this.querySet>"u"&&(this.querySet=this.device.createQuerySet({type:"timestamp",count:this.maxDispatchNumber*2}),this.queryResolveBuffer=this.device.createBuffer({size:this.maxDispatchNumber*2*8,usage:GPUBufferUsage.COPY_SRC|GPUBufferUsage.QUERY_RESOLVE})))}captureBegin(){pe("info","captureBegin"),this.capturedCommandList.get(this.currentSessionId)||this.capturedCommandList.set(this.currentSessionId,[]),this.capturedPendingKernels.get(this.currentSessionId)||this.capturedPendingKernels.set(this.currentSessionId,[]),this.flush(),this.sessionStatus="capturing"}captureEnd(){pe("info","captureEnd"),this.flush(),this.sessionStatus="default"}replay(){pe("info","replay"),this.sessionStatus="replaying";let t=this.capturedCommandList.get(this.currentSessionId),r=this.capturedPendingKernels.get(this.currentSessionId),n=t.length;this.pendingKernels=[];for(let o=0;o=this.maxDispatchNumber||this.queryType==="at-passes")&&this.endComputePass(),this.pendingDispatchNumber>=this.maxDispatchNumber&&this.flush()}this.flush(),this.sessionStatus="default"}onCreateSession(){this.gpuDataManager.onCreateSession()}onReleaseSession(t){this.unregisterBuffers(t),this.capturedCommandList.has(t)&&this.capturedCommandList.delete(t),this.capturedPendingKernels.has(t)&&this.capturedPendingKernels.delete(t),this.gpuDataManager.onReleaseSession(t)}onRunStart(t){this.currentSessionId=t,this.setQueryType()}}});var Vh,Rl,un,dn,ko,Ul,Vl=U(()=>{"use strict";Xe();Vh=1,Rl=()=>Vh++,un=class{constructor(t){this.sessionId=t.sessionId,this.mlContext=t.context,this.mlTensor=t.tensor,this.dataType=t.dataType,this.tensorShape=t.shape}get tensor(){return this.mlTensor}get type(){return this.dataType}get shape(){return this.tensorShape}destroy(){pe("verbose",()=>"[WebNN] TensorWrapper.destroy"),this.mlTensor.destroy()}write(t){this.mlContext.writeTensor(this.mlTensor,t)}async read(t){return t?this.mlContext.readTensor(this.mlTensor,t):this.mlContext.readTensor(this.mlTensor)}sameTypeAndShape(t,r){return this.dataType===t&&this.tensorShape.every((n,o)=>n===r[o])}},dn=class{constructor(t,r){this.tensorManager=t;this.wrapper=r}get tensorWrapper(){return this.wrapper}releaseTensor(){this.tensorWrapper&&this.tensorManager.releaseTensor(this.tensorWrapper)}async ensureTensor(t,r,n){if(this.wrapper){if(this.wrapper.sameTypeAndShape(t,r))return this.wrapper.tensor;n&&(this.activeUpload=new Uint8Array(await this.wrapper.read())),this.tensorManager.releaseTensor(this.wrapper)}let o=MLTensorUsage.READ|MLTensorUsage.WRITE;return this.wrapper=await this.tensorManager.getCachedTensor(t,r,o,!0,!0),n&&this.activeUpload&&(this.wrapper.write(this.activeUpload),this.activeUpload=void 0),this.wrapper.tensor}upload(t){if(this.wrapper){this.wrapper.write(t);return}this.activeUpload?this.activeUpload.set(t):this.activeUpload=new Uint8Array(t)}async download(t){if(this.activeUpload)if(t){t instanceof ArrayBuffer?new Uint8Array(t).set(this.activeUpload):new Uint8Array(t.buffer,t.byteOffset,t.byteLength).set(this.activeUpload);return}else return this.activeUpload.buffer;if(!this.wrapper)throw new Error("Tensor has not been created.");return t?this.wrapper.read(t):this.wrapper.read()}},ko=class{constructor(t){this.backend=t;this.tensorTrackersById=new Map;this.freeTensors=[];this.externalTensors=new Set}reserveTensorId(){let t=Rl();return this.tensorTrackersById.set(t,new dn(this)),t}releaseTensorId(t){let r=this.tensorTrackersById.get(t);r&&(this.tensorTrackersById.delete(t),r.tensorWrapper&&this.releaseTensor(r.tensorWrapper))}async ensureTensor(t,r,n,o){pe("verbose",()=>`[WebNN] TensorManager.ensureTensor {tensorId: ${t}, dataType: ${r}, shape: ${n}, copyOld: ${o}}`);let i=this.tensorTrackersById.get(t);if(!i)throw new Error("Tensor not found.");return i.ensureTensor(r,n,o)}upload(t,r){let n=this.tensorTrackersById.get(t);if(!n)throw new Error("Tensor not found.");n.upload(r)}async download(t,r){pe("verbose",()=>`[WebNN] TensorManager.download {tensorId: ${t}, dstBuffer: ${r?.byteLength}}`);let n=this.tensorTrackersById.get(t);if(!n)throw new Error("Tensor not found.");return n.download(r)}releaseTensorsForSession(t){for(let r of this.freeTensors)r.sessionId===t&&r.destroy();this.freeTensors=this.freeTensors.filter(r=>r.sessionId!==t)}registerTensor(t,r,n,o){let i=Rl(),a=new un({sessionId:this.backend.currentSessionId,context:t,tensor:r,dataType:n,shape:o});return this.tensorTrackersById.set(i,new dn(this,a)),this.externalTensors.add(a),i}async getCachedTensor(t,r,n,o,i){let a=this.backend.currentSessionId;for(let[c,m]of this.freeTensors.entries())if(m.sameTypeAndShape(t,r)){let u=this.freeTensors.splice(c,1)[0];return u.sessionId=a,u}let d=this.backend.currentContext;pe("verbose",()=>`[WebNN] MLContext.createTensor {dataType: ${t}, shape: ${r}}`);let l=await d.createTensor({dataType:t,shape:r,dimensions:r,usage:n,writable:o,readable:i});return new un({sessionId:a,context:d,tensor:l,dataType:t,shape:r})}releaseTensor(t){this.externalTensors.has(t)&&this.externalTensors.delete(t),this.freeTensors.push(t)}},Ul=(...e)=>new ko(...e)});var Wl,ln,Nl=U(()=>{"use strict";J();gt();Qn();Vl();Xe();Wl=new Map([[1,"float32"],[10,"float16"],[6,"int32"],[12,"uint32"],[7,"int64"],[13,"uint64"],[3,"int8"],[2,"uint8"],[9,"uint8"]]),ln=class{constructor(t){this.tensorManager=Ul(this);this.mlContextBySessionId=new Map;this.sessionIdsByMLContext=new Map;Nr(t.logLevel,!!t.debug)}get currentSessionId(){if(this.activeSessionId===void 0)throw new Error("No active session");return this.activeSessionId}onRunStart(t){this.activeSessionId=t}get currentContext(){let t=this.getMLContext(this.currentSessionId);if(!t)throw new Error(`No MLContext found for session ${this.currentSessionId}`);return t}registerMLContext(t,r){this.mlContextBySessionId.set(t,r);let n=this.sessionIdsByMLContext.get(r);n||(n=new Set,this.sessionIdsByMLContext.set(r,n)),n.add(t)}onReleaseSession(t){let r=this.mlContextBySessionId.get(t);if(!r)return;this.tensorManager.releaseTensorsForSession(t),this.mlContextBySessionId.delete(t);let n=this.sessionIdsByMLContext.get(r);n.delete(t),n.size===0&&this.sessionIdsByMLContext.delete(r)}getMLContext(t){return this.mlContextBySessionId.get(t)}reserveTensorId(){return this.tensorManager.reserveTensorId()}releaseTensorId(t){pe("verbose",()=>`[WebNN] releaseTensorId {tensorId: ${t}}`),this.tensorManager.releaseTensorId(t)}async ensureTensor(t,r,n,o){let i=Wl.get(r);if(!i)throw new Error(`Unsupported ONNX data type: ${r}`);return this.tensorManager.ensureTensor(t,i,n,o)}uploadTensor(t,r){if(!Te().shouldTransferToMLTensor)throw new Error("Trying to upload to a MLTensor while shouldTransferToMLTensor is false");pe("verbose",()=>`[WebNN] uploadTensor {tensorId: ${t}, data: ${r.byteLength}}`),this.tensorManager.upload(t,r)}async downloadTensor(t,r){return this.tensorManager.download(t,r)}createMLTensorDownloader(t,r){return async()=>{let n=await this.tensorManager.download(t);return Lr(n,r)}}registerMLTensor(t,r,n){let o=Wl.get(r);if(!o)throw new Error(`Unsupported ONNX data type: ${r}`);let i=this.tensorManager.registerTensor(this.currentContext,t,o,n);return pe("verbose",()=>`[WebNN] registerMLTensor {tensor: ${t}, dataType: ${o}, dimensions: ${n}} -> {tensorId: ${i}}`),i}registerMLConstant(t,r,n,o,i,a){if(!a)throw new Error("External mounted files are not available.");let d=t;t.startsWith("./")&&(d=t.substring(2));let l=a.get(d);if(!l)throw new Error(`File with name ${d} not found in preloaded files.`);if(r+n>l.byteLength)throw new Error("Out of bounds: data offset and length exceed the external file data size.");let c=l.slice(r,r+n).buffer,m;switch(i.dataType){case"float32":m=new Float32Array(c);break;case"float16":m=new Uint16Array(c);break;case"int32":m=new Int32Array(c);break;case"uint32":m=new Uint32Array(c);break;case"int64":m=new BigInt64Array(c);break;case"uint64":m=new BigUint64Array(c);break;case"int8":m=new Int8Array(c);break;case"uint8":m=new Uint8Array(c);break;default:throw new Error(`Unsupported data type: ${i.dataType} in creating WebNN Constant from external data.`)}return pe("verbose",()=>`[WebNN] registerMLConstant {dataType: ${i.dataType}, shape: ${i.shape}}}`),o.constant(i,m)}flush(){}}});var Ll={};Gt(Ll,{init:()=>Wh});var or,Eo,Wh,Hl=U(()=>{"use strict";J();Ml();Xe();ae();Nl();or=class e{constructor(t,r,n,o){this.module=t;this.dataType=r;this.data=n;this.dims=o}getFloat32Array(){if(this.dataType!==1)throw new Error("Invalid data type");let t=k.size(this.dims);return t===0?new Float32Array:new Float32Array(this.module.HEAP8.buffer,this.data,t)}getBigInt64Array(){if(this.dataType!==7)throw new Error("Invalid data type");let t=k.size(this.dims);return t===0?new BigInt64Array:new BigInt64Array(this.module.HEAP8.buffer,this.data,t)}getInt32Array(){if(this.dataType!==6)throw new Error("Invalid data type");let t=k.size(this.dims);return t===0?new Int32Array:new Int32Array(this.module.HEAP8.buffer,this.data,t)}getUint16Array(){if(this.dataType!==10&&this.dataType!==4)throw new Error("Invalid data type");let t=k.size(this.dims);return t===0?new Uint16Array:new Uint16Array(this.module.HEAP8.buffer,this.data,t)}reshape(t){if(k.size(t)!==k.size(this.dims))throw new Error("Invalid new shape");return new e(this.module,this.dataType,this.data,t)}},Eo=class{constructor(t,r,n){this.module=t;this.backend=r;this.customDataOffset=0;this.customDataSize=0;this.adapterInfo=r.adapterInfo;let o=t.HEAPU32,i=n>>>2;this.opKernelContext=o[i++];let a=o[i++];this.outputCount=o[i++],this.customDataOffset=o[i++],this.customDataSize=o[i++];let d=[];for(let l=0;ltypeof d=="number"?this.inputs[d]:d)??this.inputs,o=r?.outputs??[],i=(d,l,c)=>new or(this.module,l,this.output(d,c),c),a=(d,l)=>{let c=It(d,l);if(!c)throw new Error(`Unsupported data type: ${d}`);let m=c>0?this.backend.gpuDataManager.create(c).id:0;return new or(this.module,d,m,l)};return this.backend.run(t,n,o,i,a,this.outputCount)}output(t,r){let n=this.module.stackSave();try{let o=this.module.stackAlloc((1+r.length)*4),i=o>>2;this.module.HEAPU32[i++]=r.length;for(let a=0;a{let o=t.jsepInit;if(!o)throw new Error("Failed to initialize JSEP. The WebAssembly module is not built with JSEP support.");if(e==="webgpu"){let i=new sn;await i.initialize(r,n),o("webgpu",[i,a=>i.alloc(a),a=>i.free(a),(a,d,l,c=!1)=>{if(c)pe("verbose",()=>`[WebGPU] jsepCopyGpuToGpu: src=${a}, dst=${d}, size=${l}`),i.memcpy(a,d);else{pe("verbose",()=>`[WebGPU] jsepCopyCpuToGpu: dataOffset=${a}, gpuDataId=${d}, size=${l}`);let m=t.HEAPU8.subarray(a>>>0,(a>>>0)+l);i.upload(d,m)}},async(a,d,l)=>{pe("verbose",()=>`[WebGPU] jsepCopyGpuToCpu: gpuDataId=${a}, dataOffset=${d}, size=${l}`),await i.download(a,()=>t.HEAPU8.subarray(d>>>0,(d>>>0)+l))},(a,d,l)=>i.createKernel(a,d,l,t.UTF8ToString(t._JsepGetNodeName(d))),a=>i.releaseKernel(a),(a,d,l,c)=>{pe("verbose",()=>`[WebGPU] jsepRun: sessionHandle=${l}, kernel=${a}, contextDataOffset=${d}`);let m=new Eo(t,i,d);return i.computeKernel(a,m,c)},()=>i.captureBegin(),()=>i.captureEnd(),()=>i.replay()])}else{let i=new ln(r);o("webnn",[i,()=>i.reserveTensorId(),a=>i.releaseTensorId(a),async(a,d,l,c)=>i.ensureTensor(a,d,l,c),(a,d)=>{i.uploadTensor(a,d)},async(a,d)=>i.downloadTensor(a,d)])}}});var Nh,kr,Er,Et,Lh,jt,Pr,zr,Gl,Or,Dr,Br,Fn=U(()=>{"use strict";Na();Ha();J();gt();Rr();Xn();Nh=(e,t)=>{Te()._OrtInit(e,t)!==0&&ve("Can't initialize onnxruntime.")},kr=async e=>{Nh(e.wasm.numThreads,Xt(e.logLevel))},Er=async(e,t)=>{{let r=(Hl(),br(Ll)).init;if(t==="webgpu"){if(typeof navigator>"u"||!navigator.gpu)throw new Error("WebGPU is not supported in current environment");let n=e.webgpu.adapter;if(n){if(typeof n.limits!="object"||typeof n.features!="object"||typeof n.requestDevice!="function")throw new Error("Invalid GPU adapter set in `env.webgpu.adapter`. It must be a GPUAdapter object.")}else{let o=e.webgpu.powerPreference;if(o!==void 0&&o!=="low-power"&&o!=="high-performance")throw new Error(`Invalid powerPreference setting: "${o}"`);let i=e.webgpu.forceFallbackAdapter;if(i!==void 0&&typeof i!="boolean")throw new Error(`Invalid forceFallbackAdapter setting: "${i}"`);if(n=await navigator.gpu.requestAdapter({powerPreference:o,forceFallbackAdapter:i}),!n)throw new Error('Failed to get GPU adapter. You may need to enable flag "--enable-unsafe-webgpu" if you are using Chrome.')}await r("webgpu",Te(),e,n)}if(t==="webnn"){if(typeof navigator>"u"||!navigator.ml)throw new Error("WebNN is not supported in current environment");await r("webnn",Te(),e)}}},Et=new Map,Lh=e=>{let t=Te(),r=t.stackSave();try{let n=t.stackAlloc(8);return t._OrtGetInputOutputCount(e,n,n+4)!==0&&ve("Can't get session input/output count."),[t.HEAP32[n/4],t.HEAP32[n/4+1]]}finally{t.stackRestore(r)}},jt=e=>{let t=Te(),r=t._malloc(e.byteLength);if(r===0)throw new Error(`Can't create a session. failed to allocate a buffer of size ${e.byteLength}.`);return t.HEAPU8.set(e,r),[r,e.byteLength]},Pr=async(e,t)=>{let r,n,o=Te();Array.isArray(e)?[r,n]=e:e.buffer===o.HEAPU8.buffer?[r,n]=[e.byteOffset,e.byteLength]:[r,n]=jt(e);let i=0,a=0,d=0,l=[],c=[],m=[];try{if([a,l]=La(t),t?.externalData&&o.mountExternalData){let v=[];for(let x of t.externalData){let T=typeof x=="string"?x:x.path;v.push(Qt(typeof x=="string"?x:x.data).then(C=>{o.mountExternalData(T,C)}))}await Promise.all(v)}for(let v of t?.executionProviders??[])if((typeof v=="string"?v:v.name)==="webnn"){if(o.shouldTransferToMLTensor=!1,o.currentContext)throw new Error("WebNN execution provider is already set.");if(typeof v!="string"){let T=v,C=T?.context,A=T?.gpuDevice,P=T?.deviceType,D=T?.powerPreference;C?o.currentContext=C:A?o.currentContext=await navigator.ml.createContext(A):o.currentContext=await navigator.ml.createContext({deviceType:P,powerPreference:D})}else o.currentContext=await navigator.ml.createContext();break}i=await o._OrtCreateSession(r,n,a),i===0&&ve("Can't create a session."),o.jsepOnCreateSession?.(),o.currentContext&&(o.jsepRegisterMLContext(i,o.currentContext),o.currentContext=void 0,o.shouldTransferToMLTensor=!0);let[u,h]=Lh(i),w=!!t?.enableGraphCapture,g=[],y=[],S=[];for(let v=0;vv==="gpu-buffer"||v==="ml-tensor")&&(d=o._OrtCreateBinding(i),d===0&&ve("Can't create IO binding."),$={handle:d,outputPreferredLocations:S,outputPreferredLocationsEncoded:S.map(v=>Yn(v))}),Et.set(i,[i,c,m,$,w,!1]),[i,g,y]}catch(u){throw c.forEach(h=>o._OrtFree(h)),m.forEach(h=>o._OrtFree(h)),d!==0&&o._OrtReleaseBinding(d),i!==0&&o._OrtReleaseSession(i),u}finally{o._free(r),a!==0&&o._OrtReleaseSessionOptions(a),l.forEach(u=>o._free(u)),o.unmountExternalData?.()}},zr=e=>{let t=Te(),r=Et.get(e);if(!r)throw new Error(`cannot release session. invalid session id: ${e}`);let[n,o,i,a,d]=r;a&&(d&&t._OrtClearBoundOutputs(a.handle),t._OrtReleaseBinding(a.handle)),t.jsepOnReleaseSession?.(e),o.forEach(l=>t._OrtFree(l)),i.forEach(l=>t._OrtFree(l)),t._OrtReleaseSession(n),Et.delete(e)},Gl=(e,t,r,n,o,i=!1)=>{if(!e){t.push(0);return}let a=Te(),d=e[0],l=e[1],c=e[3],m,u;if(d==="string"&&(c==="gpu-buffer"||c==="ml-tensor"))throw new Error("String tensor is not supported on GPU.");if(i&&c!=="gpu-buffer")throw new Error(`External buffer must be provided for input/output index ${o} when enableGraphCapture is true.`);if(c==="gpu-buffer"){let g=e[2].gpuBuffer;u=It(Yt(d),l);let y=a.jsepRegisterBuffer;if(!y)throw new Error('Tensor location "gpu-buffer" is not supported without using WebGPU.');m=y(n,o,g,u)}else if(c==="ml-tensor"){let g=e[2].mlTensor;u=It(Yt(d),l);let y=a.jsepRegisterMLTensor;if(!y)throw new Error('Tensor location "ml-tensor" is not supported without using WebNN.');m=y(g,Yt(d),l)}else{let g=e[2];if(Array.isArray(g)){u=4*g.length,m=a._malloc(u),r.push(m);let y=m/4;for(let S=0;Sa.HEAP32[g++]=S);let y=a._OrtCreateTensor(Yt(d),m,u,w,l.length,Yn(c));y===0&&ve(`Can't create tensor for input/output. session=${n}, index=${o}.`),t.push(y)}finally{a.stackRestore(h)}},Or=async(e,t,r,n,o,i)=>{let a=Te(),d=Et.get(e);if(!d)throw new Error(`cannot run inference. invalid session id: ${e}`);let l=d[0],c=d[1],m=d[2],u=d[3],h=d[4],w=d[5],g=t.length,y=n.length,S=0,$=[],v=[],x=[],T=[],C=a.stackSave(),A=a.stackAlloc(g*4),P=a.stackAlloc(g*4),D=a.stackAlloc(y*4),W=a.stackAlloc(y*4);try{a.jsepOnRunStart?.(l),[S,$]=Wa(i);for(let K=0;KAe*Me,1);re=yt(G);let bt=u?.outputPreferredLocations[n[K]];if(re==="string"){if(bt==="gpu-buffer"||bt==="ml-tensor")throw new Error("String tensor is not supported on GPU.");let Ae=[],Me=ne/4;for(let Ue=0;Ue0){let Ae=a.jsepGetBuffer;if(!Ae)throw new Error('preferredLocation "gpu-buffer" is not supported without using WebGPU.');let Me=Ae(ne),Ue=It(G,Ce);if(Ue===void 0||!Vr(re))throw new Error(`Unsupported data type: ${re}`);le=!0,ue.push([re,$e,{gpuBuffer:Me,download:a.jsepCreateDownloader(Me,Ue,re),dispose:()=>{a._OrtReleaseTensor(de)}},"gpu-buffer"])}else if(bt==="ml-tensor"&&Ce>0){let Ae=a.jsepEnsureTensor;if(!Ae)throw new Error('preferredLocation "ml-tensor" is not supported without using WebNN.');if(It(G,Ce)===void 0||!Wr(re))throw new Error(`Unsupported data type: ${re}`);let Ue=await Ae(ne,G,$e,!1);le=!0,ue.push([re,$e,{mlTensor:Ue,download:a.jsepCreateMLTensorDownloader(ne,re),dispose:()=>{a.jsepReleaseTensorId(ne),a._OrtReleaseTensor(de)}},"ml-tensor"])}else{let Ae=Ur(re),Me=new Ae(Ce);new Uint8Array(Me.buffer,Me.byteOffset,Me.byteLength).set(a.HEAPU8.subarray(ne,ne+Me.byteLength)),ue.push([re,$e,Me,"cpu"])}}finally{a.stackRestore(ce),re==="string"&&ne&&a._free(ne),le||a._OrtReleaseTensor(de)}}return u&&!h&&(a._OrtClearBoundOutputs(u.handle),Et.set(e,[l,c,m,u,h,!1])),ue}finally{a.stackRestore(C),v.forEach(N=>a._OrtReleaseTensor(N)),x.forEach(N=>a._OrtReleaseTensor(N)),T.forEach(N=>a._free(N)),S!==0&&a._OrtReleaseRunOptions(S),$.forEach(N=>a._free(N))}},Dr=e=>{let t=Te(),r=Et.get(e);if(!r)throw new Error("invalid session id");let n=r[0],o=t._OrtEndProfiling(n);o===0&&ve("Can't get an profile file name."),t._OrtFree(o)},Br=e=>{let t=[];for(let r of e){let n=r[2];!Array.isArray(n)&&"buffer"in n&&t.push(n.buffer)}return t}});var Pt,Ye,ir,pn,mn,cn,Po,zo,Lt,Ht,Gh,Fl,ql,jl,Kl,Yl,Xl,Ql,Oo=U(()=>{"use strict";Ke();Fn();gt();qt();Pt=()=>!!_e.wasm.proxy&&typeof document<"u",ir=!1,pn=!1,mn=!1,zo=new Map,Lt=(e,t)=>{let r=zo.get(e);r?r.push(t):zo.set(e,[t])},Ht=()=>{if(ir||!pn||mn||!Ye)throw new Error("worker not ready")},Gh=e=>{switch(e.data.type){case"init-wasm":ir=!1,e.data.err?(mn=!0,Po[1](e.data.err)):(pn=!0,Po[0]()),cn&&(URL.revokeObjectURL(cn),cn=void 0);break;case"init-ep":case"copy-from":case"create":case"release":case"run":case"end-profiling":{let t=zo.get(e.data.type);e.data.err?t.shift()[1](e.data.err):t.shift()[0](e.data.out);break}default:}},Fl=async()=>{if(!pn){if(ir)throw new Error("multiple calls to 'initWasm()' detected.");if(mn)throw new Error("previous call to 'initWasm()' failed.");if(ir=!0,Pt())return new Promise((e,t)=>{Ye?.terminate(),Ra().then(([r,n])=>{try{Ye=n,Ye.onerror=i=>t(i),Ye.onmessage=Gh,Po=[e,t];let o={type:"init-wasm",in:_e};Ye.postMessage(o),cn=r}catch(o){t(o)}},t)});try{await Ar(_e.wasm),await kr(_e),pn=!0}catch(e){throw mn=!0,e}finally{ir=!1}}},ql=async e=>{if(Pt())return Ht(),new Promise((t,r)=>{Lt("init-ep",[t,r]);let n={type:"init-ep",in:{epName:e,env:_e}};Ye.postMessage(n)});await Er(_e,e)},jl=async e=>Pt()?(Ht(),new Promise((t,r)=>{Lt("copy-from",[t,r]);let n={type:"copy-from",in:{buffer:e}};Ye.postMessage(n,[e.buffer])})):jt(e),Kl=async(e,t)=>{if(Pt()){if(t?.preferredOutputLocation)throw new Error('session option "preferredOutputLocation" is not supported for proxy.');return Ht(),new Promise((r,n)=>{Lt("create",[r,n]);let o={type:"create",in:{model:e,options:{...t}}},i=[];e instanceof Uint8Array&&i.push(e.buffer),Ye.postMessage(o,i)})}else return Pr(e,t)},Yl=async e=>{if(Pt())return Ht(),new Promise((t,r)=>{Lt("release",[t,r]);let n={type:"release",in:e};Ye.postMessage(n)});zr(e)},Xl=async(e,t,r,n,o,i)=>{if(Pt()){if(r.some(a=>a[3]!=="cpu"))throw new Error("input tensor on GPU is not supported for proxy.");if(o.some(a=>a))throw new Error("pre-allocated output tensor is not supported for proxy.");return Ht(),new Promise((a,d)=>{Lt("run",[a,d]);let l=r,c={type:"run",in:{sessionId:e,inputIndices:t,inputs:l,outputIndices:n,options:i}};Ye.postMessage(c,Br(l))})}else return Or(e,t,r,n,o,i)},Ql=async e=>{if(Pt())return Ht(),new Promise((t,r)=>{Lt("end-profiling",[t,r]);let n={type:"end-profiling",in:e};Ye.postMessage(n)});Dr(e)}});var Zl,Fh,fn,Jl=U(()=>{"use strict";Ke();Oo();J();Cr();Xn();Zl=(e,t)=>{switch(e.location){case"cpu":return[e.type,e.dims,e.data,"cpu"];case"gpu-buffer":return[e.type,e.dims,{gpuBuffer:e.gpuBuffer},"gpu-buffer"];case"ml-tensor":return[e.type,e.dims,{mlTensor:e.mlTensor},"ml-tensor"];default:throw new Error(`invalid data location: ${e.location} for ${t()}`)}},Fh=e=>{switch(e[3]){case"cpu":return new Be(e[0],e[2],e[1]);case"gpu-buffer":{let t=e[0];if(!Vr(t))throw new Error(`not supported data type: ${t} for deserializing GPU tensor`);let{gpuBuffer:r,download:n,dispose:o}=e[2];return Be.fromGpuBuffer(r,{dataType:t,dims:e[1],download:n,dispose:o})}case"ml-tensor":{let t=e[0];if(!Wr(t))throw new Error(`not supported data type: ${t} for deserializing MLTensor tensor`);let{mlTensor:r,download:n,dispose:o}=e[2];return Be.fromMLTensor(r,{dataType:t,dims:e[1],download:n,dispose:o})}default:throw new Error(`invalid data location: ${e[3]}`)}},fn=class{async fetchModelAndCopyToWasmMemory(t){return jl(await Qt(t))}async loadModel(t,r){Le();let n;typeof t=="string"? false?0:n=await this.fetchModelAndCopyToWasmMemory(t):n=t,[this.sessionId,this.inputNames,this.outputNames]=await Kl(n,r),Ve()}async dispose(){return Yl(this.sessionId)}async run(t,r,n){Le();let o=[],i=[];Object.entries(t).forEach(h=>{let w=h[0],g=h[1],y=this.inputNames.indexOf(w);if(y===-1)throw new Error(`invalid input '${w}'`);o.push(g),i.push(y)});let a=[],d=[];Object.entries(r).forEach(h=>{let w=h[0],g=h[1],y=this.outputNames.indexOf(w);if(y===-1)throw new Error(`invalid output '${w}'`);a.push(g),d.push(y)});let l=o.map((h,w)=>Zl(h,()=>`input "${this.inputNames[i[w]]}"`)),c=a.map((h,w)=>h?Zl(h,()=>`output "${this.outputNames[d[w]]}"`):null),m=await Xl(this.sessionId,i,l,d,c,n),u={};for(let h=0;hhn,initializeFlags:()=>ec,wasmBackend:()=>qh});var ec,hn,qh,rc=U(()=>{"use strict";Ke();Oo();Jl();qt();ec=()=>{if((typeof _e.wasm.initTimeout!="number"||_e.wasm.initTimeout<0)&&(_e.wasm.initTimeout=0),_e.wasm.simd===!1&&console.warn('Deprecated property "env.wasm.simd" is set to false. non-SIMD build is no longer provided, and this setting will be ignored.'),typeof _e.wasm.proxy!="boolean"&&(_e.wasm.proxy=!1),typeof _e.wasm.trace!="boolean"&&(_e.wasm.trace=!1),typeof _e.wasm.numThreads!="number"||!Number.isInteger(_e.wasm.numThreads)||_e.wasm.numThreads<=0)if(typeof self<"u"&&!self.crossOriginIsolated)_e.wasm.numThreads=1;else{let e=typeof navigator>"u"?Wn("node:os").cpus().length:navigator.hardwareConcurrency;_e.wasm.numThreads=Math.min(4,Math.ceil((e||1)/2))}},hn=class{async init(t){ec(),await Fl(),await ql(t)}async createInferenceSessionHandler(t,r){let n=new fn;return await n.loadModel(t,r),Promise.resolve(n)}},qh=new hn});Ke();Ke();Ke();var Aa="1.21.0-dev.20241024-d9ca84ef96";var Vx=Gn;{let e=(rc(),br(tc)).wasmBackend;St("webgpu",e,5),St("webnn",e,5),St("cpu",e,10),St("wasm",e,10)}Object.defineProperty(_e.versions,"web",{value:Aa,enumerable:!0}); +/** + * @license + * Copyright 2021 Google LLC. All Rights Reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============================================================================= + */ +/** + * @license + * Copyright 2020 Google LLC. All Rights Reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============================================================================= + */ +/** + * @license + * Copyright 2019 Google LLC. All Rights Reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============================================================================= + */ +//# sourceMappingURL=ort.webgpu.bundle.min.mjs.map + + +/***/ }), + +/***/ "./src/backends/onnx.js": +/*!******************************!*\ + !*** ./src/backends/onnx.js ***! + \******************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +var onnxruntime_node__WEBPACK_IMPORTED_MODULE_1___namespace_cache; +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ Tensor: () => (/* reexport safe */ onnxruntime_common__WEBPACK_IMPORTED_MODULE_3__.Tensor), +/* harmony export */ createInferenceSession: () => (/* binding */ createInferenceSession), +/* harmony export */ deviceToExecutionProviders: () => (/* binding */ deviceToExecutionProviders), +/* harmony export */ isONNXProxy: () => (/* binding */ isONNXProxy), +/* harmony export */ isONNXTensor: () => (/* binding */ isONNXTensor) +/* harmony export */ }); +/* harmony import */ var _env_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../env.js */ "./src/env.js"); +/* harmony import */ var onnxruntime_node__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! onnxruntime-node */ "?2ce3"); +/* harmony import */ var _onnxruntime_webgpu__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! #onnxruntime-webgpu */ "./node_modules/onnxruntime-web/dist/ort.webgpu.bundle.min.mjs"); +/* harmony import */ var onnxruntime_common__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! onnxruntime-common */ "./node_modules/onnxruntime-common/dist/esm/index.js"); +/** + * @file Handler file for choosing the correct version of ONNX Runtime, based on the environment. + * Ideally, we could import the `onnxruntime-web` and `onnxruntime-node` packages only when needed, + * but dynamic imports don't seem to work with the current webpack version and/or configuration. + * This is possibly due to the experimental nature of top-level await statements. + * So, we just import both packages, and use the appropriate one based on the environment: + * - When running in node, we use `onnxruntime-node`. + * - When running in the browser, we use `onnxruntime-web` (`onnxruntime-node` is not bundled). + * + * This module is not directly exported, but can be accessed through the environment variables: + * ```javascript + * import { env } from '@huggingface/transformers'; + * console.log(env.backends.onnx); + * ``` + * + * @module backends/onnx + */ + + + +// NOTE: Import order matters here. We need to import `onnxruntime-node` before `onnxruntime-web`. +// In either case, we select the default export if it exists, otherwise we use the named export. + + +// Use subpath-imports to ensure Node.js and browser interoperability. +// See package.json and https://nodejs.org/api/packages.html#subpath-imports +// for more information. +// @ts-ignore + + + + +/** + * @typedef {import('onnxruntime-common').InferenceSession.ExecutionProviderConfig} ONNXExecutionProviders + */ + +/** @type {Record} */ +const DEVICE_TO_EXECUTION_PROVIDER_MAPPING = Object.freeze({ + auto: null, // Auto-detect based on device and environment + gpu: null, // Auto-detect GPU + cpu: 'cpu', // CPU + wasm: 'wasm', // WebAssembly + webgpu: 'webgpu', // WebGPU + cuda: 'cuda', // CUDA + dml: 'dml', // DirectML + + webnn: { name: 'webnn', deviceType: 'cpu' }, // WebNN (default) + 'webnn-npu': { name: 'webnn', deviceType: 'npu' }, // WebNN NPU + 'webnn-gpu': { name: 'webnn', deviceType: 'gpu' }, // WebNN GPU + 'webnn-cpu': { name: 'webnn', deviceType: 'cpu' }, // WebNN CPU +}); + +/** + * The list of supported devices, sorted by priority/performance. + * @type {import("../utils/devices.js").DeviceType[]} + */ +const supportedDevices = []; + +/** @type {ONNXExecutionProviders[]} */ +let defaultDevices; +let ONNX; +const ORT_SYMBOL = Symbol.for('onnxruntime'); + +if (ORT_SYMBOL in globalThis) { + // If the JS runtime exposes their own ONNX runtime, use it + ONNX = globalThis[ORT_SYMBOL]; + +} else if (_env_js__WEBPACK_IMPORTED_MODULE_0__.apis.IS_NODE_ENV) { + ONNX = onnxruntime_node__WEBPACK_IMPORTED_MODULE_1__ ?? /*#__PURE__*/ (onnxruntime_node__WEBPACK_IMPORTED_MODULE_1___namespace_cache || (onnxruntime_node__WEBPACK_IMPORTED_MODULE_1___namespace_cache = __webpack_require__.t(onnxruntime_node__WEBPACK_IMPORTED_MODULE_1__, 2))); + + // Updated as of ONNX Runtime 1.18.0 + // The following table lists the supported versions of ONNX Runtime Node.js binding provided with pre-built binaries. + // | EPs/Platforms | Windows x64 | Windows arm64 | Linux x64 | Linux arm64 | MacOS x64 | MacOS arm64 | + // | ------------- | ----------- | ------------- | ----------------- | ----------- | --------- | ----------- | + // | CPU | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | + // | DirectML | ✔️ | ✔️ | ❌ | ❌ | ❌ | ❌ | + // | CUDA | ❌ | ❌ | ✔️ (CUDA v11.8) | ❌ | ❌ | ❌ | + switch (process.platform) { + case 'win32': // Windows x64 and Windows arm64 + supportedDevices.push('dml'); + break; + case 'linux': // Linux x64 and Linux arm64 + if (process.arch === 'x64') { + supportedDevices.push('cuda'); + } + break; + case 'darwin': // MacOS x64 and MacOS arm64 + break; + } + + supportedDevices.push('cpu'); + defaultDevices = ['cpu']; +} else { + ONNX = _onnxruntime_webgpu__WEBPACK_IMPORTED_MODULE_2__; + + if (_env_js__WEBPACK_IMPORTED_MODULE_0__.apis.IS_WEBNN_AVAILABLE) { + // TODO: Only push supported providers (depending on available hardware) + supportedDevices.push('webnn-npu', 'webnn-gpu', 'webnn-cpu', 'webnn'); + } + + if (_env_js__WEBPACK_IMPORTED_MODULE_0__.apis.IS_WEBGPU_AVAILABLE) { + supportedDevices.push('webgpu'); + } + + supportedDevices.push('wasm'); + defaultDevices = ['wasm']; +} + +// @ts-ignore +const InferenceSession = ONNX.InferenceSession; + +/** + * Map a device to the execution providers to use for the given device. + * @param {import("../utils/devices.js").DeviceType|"auto"|null} [device=null] (Optional) The device to run the inference on. + * @returns {ONNXExecutionProviders[]} The execution providers to use for the given device. + */ +function deviceToExecutionProviders(device = null) { + // Use the default execution providers if the user hasn't specified anything + if (!device) return defaultDevices; + + // Handle overloaded cases + switch (device) { + case "auto": + return supportedDevices; + case "gpu": + return supportedDevices.filter(x => + ["webgpu", "cuda", "dml", "webnn-gpu"].includes(x), + ); + } + + if (supportedDevices.includes(device)) { + return [DEVICE_TO_EXECUTION_PROVIDER_MAPPING[device] ?? device]; + } + + throw new Error(`Unsupported device: "${device}". Should be one of: ${supportedDevices.join(', ')}.`) +} + + +/** + * To prevent multiple calls to `initWasm()`, we store the first call in a Promise + * that is resolved when the first InferenceSession is created. Subsequent calls + * will wait for this Promise to resolve before creating their own InferenceSession. + * @type {Promise|null} + */ +let wasmInitPromise = null; + +/** + * Create an ONNX inference session. + * @param {Uint8Array} buffer The ONNX model buffer. + * @param {import('onnxruntime-common').InferenceSession.SessionOptions} session_options ONNX inference session options. + * @param {Object} session_config ONNX inference session configuration. + * @returns {Promise} The ONNX inference session. + */ +async function createInferenceSession(buffer, session_options, session_config) { + if (wasmInitPromise) { + // A previous session has already initialized the WASM runtime + // so we wait for it to resolve before creating this new session. + await wasmInitPromise; + } + + const sessionPromise = InferenceSession.create(buffer, session_options); + wasmInitPromise ??= sessionPromise; + const session = await sessionPromise; + session.config = session_config; + return session; +} + +/** + * Check if an object is an ONNX tensor. + * @param {any} x The object to check + * @returns {boolean} Whether the object is an ONNX tensor. + */ +function isONNXTensor(x) { + return x instanceof ONNX.Tensor; +} + +/** @type {import('onnxruntime-common').Env} */ +// @ts-ignore +const ONNX_ENV = ONNX?.env; +if (ONNX_ENV?.wasm) { + // Initialize wasm backend with suitable default settings. + + // (Optional) Set path to wasm files. This is needed when running in a web worker. + // https://onnxruntime.ai/docs/api/js/interfaces/Env.WebAssemblyFlags.html#wasmPaths + // We use remote wasm files by default to make it easier for newer users. + // In practice, users should probably self-host the necessary .wasm files. + ONNX_ENV.wasm.wasmPaths = `https://cdn.jsdelivr.net/npm/@huggingface/transformers@${_env_js__WEBPACK_IMPORTED_MODULE_0__.env.version}/dist/`; + + // TODO: Add support for loading WASM files from cached buffer when we upgrade to onnxruntime-web@1.19.0 + // https://github.com/microsoft/onnxruntime/pull/21534 + + // Users may wish to proxy the WASM backend to prevent the UI from freezing, + // However, this is not necessary when using WebGPU, so we default to false. + ONNX_ENV.wasm.proxy = false; + + // https://developer.mozilla.org/en-US/docs/Web/API/crossOriginIsolated + if (typeof crossOriginIsolated === 'undefined' || !crossOriginIsolated) { + ONNX_ENV.wasm.numThreads = 1; + } +} + +if (ONNX_ENV?.webgpu) { + ONNX_ENV.webgpu.powerPreference = 'high-performance'; +} + +/** + * Check if ONNX's WASM backend is being proxied. + * @returns {boolean} Whether ONNX's WASM backend is being proxied. + */ +function isONNXProxy() { + // TODO: Update this when allowing non-WASM backends. + return ONNX_ENV?.wasm?.proxy; +} + +// Expose ONNX environment variables to `env.backends.onnx` +_env_js__WEBPACK_IMPORTED_MODULE_0__.env.backends.onnx = ONNX_ENV; + + +/***/ }), + +/***/ "./src/configs.js": +/*!************************!*\ + !*** ./src/configs.js ***! + \************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ AutoConfig: () => (/* binding */ AutoConfig), +/* harmony export */ PretrainedConfig: () => (/* binding */ PretrainedConfig), +/* harmony export */ getKeyValueShapes: () => (/* binding */ getKeyValueShapes) +/* harmony export */ }); +/* harmony import */ var _utils_core_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./utils/core.js */ "./src/utils/core.js"); +/* harmony import */ var _utils_hub_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./utils/hub.js */ "./src/utils/hub.js"); + +/** + * @file Helper module for using model configs. For more information, see the corresponding + * [Python documentation](https://huggingface.co/docs/transformers/main/en/model_doc/auto#transformers.AutoConfig). + * + * **Example:** Load an `AutoConfig`. + * + * ```javascript + * import { AutoConfig } from '@huggingface/transformers'; + * const config = await AutoConfig.from_pretrained('bert-base-uncased'); + * console.log(config); + * // PretrainedConfig { + * // "model_type": "bert", + * // "is_encoder_decoder": false, + * // "architectures": [ + * // "BertForMaskedLM" + * // ], + * // "vocab_size": 30522 + * // "num_attention_heads": 12, + * // "num_hidden_layers": 12, + * // "hidden_size": 768, + * // "max_position_embeddings": 512, + * // ... + * // } + * ``` + * + * @module configs + */ + + + + +/** + * @typedef {import('./utils/hub.js').PretrainedOptions} PretrainedOptions + */ + + +/** + * Loads a config from the specified path. + * @param {string} pretrained_model_name_or_path The path to the config directory. + * @param {PretrainedOptions} options Additional options for loading the config. + * @returns {Promise} A promise that resolves with information about the loaded config. + */ +async function loadConfig(pretrained_model_name_or_path, options) { + return await (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_1__.getModelJSON)(pretrained_model_name_or_path, 'config.json', true, options); +} + +/** + * + * @param {PretrainedConfig} config + * @returns {Object} The normalized configuration. + */ +function getNormalizedConfig(config) { + const mapping = {}; + + let init_normalized_config = {}; + switch (config.model_type) { + // Sub-configs + case 'llava': + case 'paligemma': + case 'florence2': + init_normalized_config = getNormalizedConfig(config.text_config); + break; + case 'moondream1': + init_normalized_config = getNormalizedConfig(config.phi_config); + break; + case 'musicgen': + init_normalized_config = getNormalizedConfig(config.decoder); + break; + + // Decoder-only models + case 'gpt2': + case 'gptj': + case 'jais': + case 'codegen': + case 'gpt_bigcode': + mapping['num_heads'] = 'n_head'; + mapping['num_layers'] = 'n_layer'; + mapping['hidden_size'] = 'n_embd'; + break; + case 'gpt_neox': + case 'stablelm': + case 'opt': + case 'phi': + case 'phi3': + case 'falcon': + mapping['num_heads'] = 'num_attention_heads'; + mapping['num_layers'] = 'num_hidden_layers'; + mapping['hidden_size'] = 'hidden_size'; + break; + case 'llama': + case 'olmo': + case 'mobilellm': + case 'granite': + case 'cohere': + case 'mistral': + case 'starcoder2': + case 'qwen2': + mapping['num_heads'] = 'num_key_value_heads'; + mapping['num_layers'] = 'num_hidden_layers'; + mapping['hidden_size'] = 'hidden_size'; + mapping['num_attention_heads'] = 'num_attention_heads'; + break; + case 'gemma': + case 'gemma2': + mapping['num_heads'] = 'num_key_value_heads'; + mapping['num_layers'] = 'num_hidden_layers'; + mapping['dim_kv'] = 'head_dim'; + break; + case 'openelm': + mapping['num_heads'] = 'num_kv_heads'; + mapping['num_layers'] = 'num_transformer_layers'; + mapping['dim_kv'] = 'head_dim'; + break; + case 'gpt_neo': + case 'donut-swin': + mapping['num_heads'] = 'num_heads'; + mapping['num_layers'] = 'num_layers'; + mapping['hidden_size'] = 'hidden_size'; + break; + case 'bloom': + mapping['num_heads'] = 'n_head'; + mapping['num_layers'] = 'n_layer'; + mapping['hidden_size'] = 'hidden_size'; + break; + case 'mpt': + mapping['num_heads'] = 'n_heads'; + mapping['num_layers'] = 'n_layers'; + mapping['hidden_size'] = 'd_model'; + break; + + // Encoder-decoder models + case 't5': + case 'mt5': + case 'longt5': + mapping['num_decoder_layers'] = 'num_decoder_layers'; + mapping['num_decoder_heads'] = 'num_heads'; + mapping['decoder_dim_kv'] = 'd_kv'; + mapping['num_encoder_layers'] = 'num_layers'; + mapping['num_encoder_heads'] = 'num_heads'; + mapping['encoder_dim_kv'] = 'd_kv'; + break; + case 'bart': + case 'mbart': + case 'marian': + case 'whisper': + case 'm2m_100': + case 'blenderbot': + case 'blenderbot-small': + case 'florence2_language': + mapping['num_decoder_layers'] = 'decoder_layers'; + mapping['num_decoder_heads'] = 'decoder_attention_heads'; + mapping['decoder_hidden_size'] = 'd_model'; + mapping['num_encoder_layers'] = 'encoder_layers'; + mapping['num_encoder_heads'] = 'encoder_attention_heads'; + mapping['encoder_hidden_size'] = 'd_model'; + break; + case 'speecht5': + mapping['num_decoder_layers'] = 'decoder_layers'; + mapping['num_decoder_heads'] = 'decoder_attention_heads'; + mapping['decoder_hidden_size'] = 'hidden_size'; + mapping['num_encoder_layers'] = 'encoder_layers'; + mapping['num_encoder_heads'] = 'encoder_attention_heads'; + mapping['encoder_hidden_size'] = 'hidden_size'; + break; + case 'trocr': + mapping['num_encoder_layers'] = mapping['num_decoder_layers'] = 'decoder_layers'; + mapping['num_encoder_heads'] = mapping['num_decoder_heads'] = 'decoder_attention_heads'; + mapping['encoder_hidden_size'] = mapping['decoder_hidden_size'] = 'd_model'; + break; + case 'musicgen_decoder': + mapping['num_encoder_layers'] = mapping['num_decoder_layers'] = 'num_hidden_layers'; + mapping['num_encoder_heads'] = mapping['num_decoder_heads'] = 'num_attention_heads'; + mapping['encoder_hidden_size'] = mapping['decoder_hidden_size'] = 'hidden_size'; + break; + + case 'vision-encoder-decoder': + const decoderConfig = getNormalizedConfig(config.decoder); + + const add_encoder_pkv = 'num_decoder_layers' in decoderConfig; + const result = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_0__.pick)(config, ['model_type', 'is_encoder_decoder']); + if (add_encoder_pkv) { + // Decoder is part of an encoder-decoder model + result.num_decoder_layers = decoderConfig.num_decoder_layers; + result.num_decoder_heads = decoderConfig.num_decoder_heads; + result.decoder_hidden_size = decoderConfig.decoder_hidden_size; + + result.num_encoder_layers = decoderConfig.num_encoder_layers; + result.num_encoder_heads = decoderConfig.num_encoder_heads; + result.encoder_hidden_size = decoderConfig.encoder_hidden_size; + } else { + // Decoder is a decoder-only model + result.num_layers = decoderConfig.num_layers; + result.num_heads = decoderConfig.num_heads; + result.hidden_size = decoderConfig.hidden_size; + } + return result; + + } + + // NOTE: If `num_attention_heads` is not set, it is assumed to be equal to `num_heads` + const normalized_config = { + ...init_normalized_config, + ...(0,_utils_core_js__WEBPACK_IMPORTED_MODULE_0__.pick)(config, ['model_type', 'multi_query', 'is_encoder_decoder']), + }; + for (const key in mapping) { + normalized_config[key] = config[mapping[key]]; + } + return normalized_config; +} + +/** + * + * @param {PretrainedConfig} config + * @returns {Record} + */ +function getKeyValueShapes(config, { + prefix = 'past_key_values', +} = {}) { + /** @type {Record} */ + const decoderFeeds = {}; + const normalized_config = config.normalized_config; + + // TODO support batches (i.e., batch_size > 1) + const batch_size = 1; + + if (normalized_config.is_encoder_decoder && ( + 'num_encoder_heads' in normalized_config && 'num_decoder_heads' in normalized_config + )) { + const encoder_dim_kv = normalized_config.encoder_dim_kv ?? ( + normalized_config.encoder_hidden_size / normalized_config.num_encoder_heads + ); + const decoder_dim_kv = normalized_config.decoder_dim_kv ?? ( + normalized_config.decoder_hidden_size / normalized_config.num_decoder_heads + ); + + const encoder_dims = [batch_size, normalized_config.num_encoder_heads, 0, encoder_dim_kv]; + const decoder_dims = [batch_size, normalized_config.num_decoder_heads, 0, decoder_dim_kv]; + for (let i = 0; i < normalized_config.num_decoder_layers; ++i) { + decoderFeeds[`${prefix}.${i}.encoder.key`] = encoder_dims; + decoderFeeds[`${prefix}.${i}.encoder.value`] = encoder_dims; + decoderFeeds[`${prefix}.${i}.decoder.key`] = decoder_dims; + decoderFeeds[`${prefix}.${i}.decoder.value`] = decoder_dims; + } + } else { // Decoders + const num_heads = normalized_config.num_heads; + const num_layers = normalized_config.num_layers; + const dim_kv = normalized_config.dim_kv ?? ( + normalized_config.hidden_size / + (normalized_config.num_attention_heads ?? num_heads) + ); + + if (normalized_config.model_type === 'falcon') { + // NOTE: Custom implementation for Falcon + const dims = [batch_size * num_heads, 0, dim_kv] + for (let i = 0; i < num_layers; ++i) { + decoderFeeds[`${prefix}.${i}.key`] = dims; + decoderFeeds[`${prefix}.${i}.value`] = dims; + } + } else if (normalized_config.multi_query) { // e.g., for `gpt_bigcode` + const dims = [batch_size * num_heads, 0, 2 * dim_kv] + + for (let i = 0; i < num_layers; ++i) { + decoderFeeds[`${prefix}.${i}.key_value`] = dims; + } + } else if (normalized_config.model_type === 'bloom') { + // NOTE: Custom implementation for Bloom + + const keyDims = [batch_size * num_heads, dim_kv, 0] // [batch_size x num_heads,64,past_sequence_length] + const valueDims = [batch_size * num_heads, 0, dim_kv] // [batch_size x num_heads,past_sequence_length,64] + for (let i = 0; i < num_layers; ++i) { + decoderFeeds[`${prefix}.${i}.key`] = keyDims; + decoderFeeds[`${prefix}.${i}.value`] = valueDims; + } + } else if (normalized_config.model_type === 'openelm') { + for (let i = 0; i < num_layers; ++i) { + const dims = [batch_size, num_heads[i], 0, dim_kv] + + decoderFeeds[`${prefix}.${i}.key`] = dims; + decoderFeeds[`${prefix}.${i}.value`] = dims; + } + } else { // Decoder-only + const dims = [batch_size, num_heads, 0, dim_kv] + for (let i = 0; i < num_layers; ++i) { + decoderFeeds[`${prefix}.${i}.key`] = dims; + decoderFeeds[`${prefix}.${i}.value`] = dims; + } + } + } + + return decoderFeeds; +} +/** + * Base class for all configuration classes. For more information, see the corresponding + * [Python documentation](https://huggingface.co/docs/transformers/main/en/main_classes/configuration#transformers.PretrainedConfig). + */ +class PretrainedConfig { + // NOTE: Typo in original + + /** @type {string|null} */ + model_type = null; + + /** @type {boolean} */ + is_encoder_decoder = false; + + /** @type {number} */ + max_position_embeddings; + + /** @type {TransformersJSConfig} */ + 'transformers.js_config'; + + /** + * Create a new PreTrainedTokenizer instance. + * @param {Object} configJSON The JSON of the config. + */ + constructor(configJSON) { + Object.assign(this, configJSON); + this.normalized_config = getNormalizedConfig(this); + } + + /** + * Loads a pre-trained config from the given `pretrained_model_name_or_path`. + * + * @param {string} pretrained_model_name_or_path The path to the pre-trained config. + * @param {PretrainedOptions} options Additional options for loading the config. + * @throws {Error} Throws an error if the config.json is not found in the `pretrained_model_name_or_path`. + * + * @returns {Promise} A new instance of the `PretrainedConfig` class. + */ + static async from_pretrained(pretrained_model_name_or_path, { + progress_callback = null, + config = null, + cache_dir = null, + local_files_only = false, + revision = 'main', + } = {}) { + if (config && !(config instanceof PretrainedConfig)) { + config = new PretrainedConfig(config); + } + + const data = config ?? await loadConfig(pretrained_model_name_or_path, { + progress_callback, + config, + cache_dir, + local_files_only, + revision, + }) + return new this(data); + } +} + +/** + * Helper class which is used to instantiate pretrained configs with the `from_pretrained` function. + * + * @example + * const config = await AutoConfig.from_pretrained('Xenova/bert-base-uncased'); + */ +class AutoConfig { + /** @type {typeof PretrainedConfig.from_pretrained} */ + static async from_pretrained(...args) { + return PretrainedConfig.from_pretrained(...args); + } +} + +/** + * Transformers.js-specific configuration, possibly present in config.json under the key `transformers.js_config`. + * @typedef {Object} TransformersJSConfig + * @property {import('./utils/tensor.js').DataType|Record} [kv_cache_dtype] The data type of the key-value cache. + * @property {Record} [free_dimension_overrides] Override the free dimensions of the model. + * See https://onnxruntime.ai/docs/tutorials/web/env-flags-and-session-options.html#freedimensionoverrides + * for more information. + * @property {import('./utils/devices.js').DeviceType} [device] The default device to use for the model. + * @property {import('./utils/dtypes.js').DataType} [dtype] The default data type to use for the model. + * @property {boolean|Record} [use_external_data_format=false] Whether to load the model using the external data format (used for models >= 2GB in size). + */ + + +/***/ }), + +/***/ "./src/env.js": +/*!********************!*\ + !*** ./src/env.js ***! + \********************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ apis: () => (/* binding */ apis), +/* harmony export */ env: () => (/* binding */ env) +/* harmony export */ }); +/* harmony import */ var fs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! fs */ "?569f"); +/* harmony import */ var path__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! path */ "?3f59"); +/* harmony import */ var url__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! url */ "?154a"); +/** + * @file Module used to configure Transformers.js. + * + * **Example:** Disable remote models. + * ```javascript + * import { env } from '@huggingface/transformers'; + * env.allowRemoteModels = false; + * ``` + * + * **Example:** Set local model path. + * ```javascript + * import { env } from '@huggingface/transformers'; + * env.localModelPath = '/path/to/local/models/'; + * ``` + * + * **Example:** Set cache directory. + * ```javascript + * import { env } from '@huggingface/transformers'; + * env.cacheDir = '/path/to/cache/directory/'; + * ``` + * + * @module env + */ + + + + + +const VERSION = '3.0.2'; + +// Check if various APIs are available (depends on environment) +const IS_BROWSER_ENV = typeof self !== 'undefined'; +const IS_WEBWORKER_ENV = IS_BROWSER_ENV && self.constructor.name === 'DedicatedWorkerGlobalScope'; +const IS_WEB_CACHE_AVAILABLE = IS_BROWSER_ENV && 'caches' in self; +const IS_WEBGPU_AVAILABLE = typeof navigator !== 'undefined' && 'gpu' in navigator; +const IS_WEBNN_AVAILABLE = typeof navigator !== 'undefined' && 'ml' in navigator; + +const IS_PROCESS_AVAILABLE = typeof process !== 'undefined'; +const IS_NODE_ENV = IS_PROCESS_AVAILABLE && process?.release?.name === 'node'; +const IS_FS_AVAILABLE = !isEmpty(fs__WEBPACK_IMPORTED_MODULE_0__); +const IS_PATH_AVAILABLE = !isEmpty(path__WEBPACK_IMPORTED_MODULE_1__); + +/** + * A read-only object containing information about the APIs available in the current environment. + */ +const apis = Object.freeze({ + /** Whether we are running in a browser environment */ + IS_BROWSER_ENV, + + /** Whether we are running in a web worker environment */ + IS_WEBWORKER_ENV, + + /** Whether the Cache API is available */ + IS_WEB_CACHE_AVAILABLE, + + /** Whether the WebGPU API is available */ + IS_WEBGPU_AVAILABLE, + + /** Whether the WebNN API is available */ + IS_WEBNN_AVAILABLE, + + /** Whether the Node.js process API is available */ + IS_PROCESS_AVAILABLE, + + /** Whether we are running in a Node.js environment */ + IS_NODE_ENV, + + /** Whether the filesystem API is available */ + IS_FS_AVAILABLE, + + /** Whether the path API is available */ + IS_PATH_AVAILABLE, +}); + +const RUNNING_LOCALLY = IS_FS_AVAILABLE && IS_PATH_AVAILABLE; + +let dirname__ = './'; +if (RUNNING_LOCALLY) { + // NOTE: We wrap `import.meta` in a call to `Object` to prevent Webpack from trying to bundle it in CommonJS. + // Although we get the warning: "Accessing import.meta directly is unsupported (only property access or destructuring is supported)", + // it is safe to ignore since the bundled value (`{}`) isn't used for CommonJS environments (we use __dirname instead). + const _import_meta_url = Object(import.meta).url; + + if (_import_meta_url) { + dirname__ = path__WEBPACK_IMPORTED_MODULE_1__.dirname(path__WEBPACK_IMPORTED_MODULE_1__.dirname(url__WEBPACK_IMPORTED_MODULE_2__.fileURLToPath(_import_meta_url))) // ESM + } else if (typeof __dirname !== 'undefined') { + dirname__ = path__WEBPACK_IMPORTED_MODULE_1__.dirname(__dirname) // CommonJS + } +} + +// Only used for environments with access to file system +const DEFAULT_CACHE_DIR = RUNNING_LOCALLY + ? path__WEBPACK_IMPORTED_MODULE_1__.join(dirname__, '/.cache/') + : null; + +// Set local model path, based on available APIs +const DEFAULT_LOCAL_MODEL_PATH = '/models/'; +const localModelPath = RUNNING_LOCALLY + ? path__WEBPACK_IMPORTED_MODULE_1__.join(dirname__, DEFAULT_LOCAL_MODEL_PATH) + : DEFAULT_LOCAL_MODEL_PATH; + +/** + * Global variable given visible to users to control execution. This provides users a simple way to configure Transformers.js. + * @typedef {Object} TransformersEnvironment + * @property {string} version This version of Transformers.js. + * @property {{onnx: Partial}} backends Expose environment variables of different backends, + * allowing users to set these variables if they want to. + * @property {boolean} allowRemoteModels Whether to allow loading of remote files, defaults to `true`. + * If set to `false`, it will have the same effect as setting `local_files_only=true` when loading pipelines, models, tokenizers, processors, etc. + * @property {string} remoteHost Host URL to load models from. Defaults to the Hugging Face Hub. + * @property {string} remotePathTemplate Path template to fill in and append to `remoteHost` when loading models. + * @property {boolean} allowLocalModels Whether to allow loading of local files, defaults to `false` if running in-browser, and `true` otherwise. + * If set to `false`, it will skip the local file check and try to load the model from the remote host. + * @property {string} localModelPath Path to load local models from. Defaults to `/models/`. + * @property {boolean} useFS Whether to use the file system to load files. By default, it is `true` if available. + * @property {boolean} useBrowserCache Whether to use Cache API to cache models. By default, it is `true` if available. + * @property {boolean} useFSCache Whether to use the file system to cache files. By default, it is `true` if available. + * @property {string} cacheDir The directory to use for caching files with the file system. By default, it is `./.cache`. + * @property {boolean} useCustomCache Whether to use a custom cache system (defined by `customCache`), defaults to `false`. + * @property {Object} customCache The custom cache to use. Defaults to `null`. Note: this must be an object which + * implements the `match` and `put` functions of the Web Cache API. For more information, see https://developer.mozilla.org/en-US/docs/Web/API/Cache + */ + +/** @type {TransformersEnvironment} */ +const env = { + version: VERSION, + + /////////////////// Backends settings /////////////////// + // NOTE: These will be populated later by the backends themselves. + backends: { + // onnxruntime-web/onnxruntime-node + onnx: {}, + }, + + /////////////////// Model settings /////////////////// + allowRemoteModels: true, + remoteHost: 'https://huggingface.co/', + remotePathTemplate: '{model}/resolve/{revision}/', + + allowLocalModels: !IS_BROWSER_ENV, + localModelPath: localModelPath, + useFS: IS_FS_AVAILABLE, + + /////////////////// Cache settings /////////////////// + useBrowserCache: IS_WEB_CACHE_AVAILABLE, + + useFSCache: IS_FS_AVAILABLE, + cacheDir: DEFAULT_CACHE_DIR, + + useCustomCache: false, + customCache: null, + ////////////////////////////////////////////////////// +} + + +/** + * @param {Object} obj + * @private + */ +function isEmpty(obj) { + return Object.keys(obj).length === 0; +} + + + +/***/ }), + +/***/ "./src/generation/configuration_utils.js": +/*!***********************************************!*\ + !*** ./src/generation/configuration_utils.js ***! + \***********************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ GenerationConfig: () => (/* binding */ GenerationConfig) +/* harmony export */ }); +/* harmony import */ var _utils_core_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../utils/core.js */ "./src/utils/core.js"); + +/** + * @module generation/configuration_utils + */ + + + +/** + * Class that holds a configuration for a generation task. + */ +class GenerationConfig { + // Parameters that control the length of the output + /** + * The maximum length the generated tokens can have. + * Corresponds to the length of the input prompt + `max_new_tokens`. + * Its effect is overridden by `max_new_tokens`, if also set. + * @type {number} + * @default 20 + */ + max_length = 20; + + /** + * The maximum numbers of tokens to generate, ignoring the number of tokens in the prompt. + * @type {number} + * @default null + */ + max_new_tokens = null; + + /** + * The minimum length of the sequence to be generated. + * Corresponds to the length of the input prompt + `min_new_tokens`. + * Its effect is overridden by `min_new_tokens`, if also set. + * @type {number} + * @default 0 + */ + min_length = 0; + + /** + * The minimum numbers of tokens to generate, ignoring the number of tokens in the prompt. + * @type {number} + * @default null + */ + min_new_tokens = null; + + /** + * Controls the stopping condition for beam-based methods, like beam-search. It accepts the following values: + * - `true`, where the generation stops as soon as there are `num_beams` complete candidates; + * - `false`, where an heuristic is applied and the generation stops when is it very unlikely to find better candidates; + * - `"never"`, where the beam search procedure only stops when there cannot be better candidates (canonical beam search algorithm). + * @type {boolean|"never"} + * @default false + */ + early_stopping = false; + + /** + * The maximum amount of time you allow the computation to run for in seconds. + * Generation will still finish the current pass after allocated time has been passed. + * @type {number} + * @default null + */ + max_time = null; + + // Parameters that control the generation strategy used + /** + * Whether or not to use sampling; use greedy decoding otherwise. + * @type {boolean} + * @default false + */ + do_sample = false; + + /** + * Number of beams for beam search. 1 means no beam search. + * @type {number} + * @default 1 + */ + num_beams = 1; + + /** + * Number of groups to divide `num_beams` into in order to ensure diversity among different groups of beams. + * See [this paper](https://arxiv.org/pdf/1610.02424.pdf) for more details. + * @type {number} + * @default 1 + */ + num_beam_groups = 1; + + /** + * The values balance the model confidence and the degeneration penalty in contrastive search decoding. + * @type {number} + * @default null + */ + penalty_alpha = null; + + /** + * Whether or not the model should use the past last key/values attentions (if applicable to the model) to speed up decoding. + * @type {boolean} + * @default true + */ + use_cache = true; + + // Parameters for manipulation of the model output logits + /** + * The value used to modulate the next token probabilities. + * @type {number} + * @default 1.0 + */ + temperature = 1.0; + + /** + * The number of highest probability vocabulary tokens to keep for top-k-filtering. + * @type {number} + * @default 50 + */ + top_k = 50; + + /** + * If set to float < 1, only the smallest set of most probable tokens with probabilities that add up to `top_p` or higher are kept for generation. + * @type {number} + * @default 1.0 + */ + top_p = 1.0; + + /** + * Local typicality measures how similar the conditional probability of predicting a target token next is to the expected conditional probability of predicting a random token next, given the partial text already generated. + * If set to float < 1, the smallest set of the most locally typical tokens with probabilities that add up to `typical_p` or higher are kept for generation. + * See [this paper](https://arxiv.org/pdf/2202.00666.pdf) for more details. + * @type {number} + * @default 1.0 + */ + typical_p = 1.0; + + /** + * If set to float strictly between 0 and 1, only tokens with a conditional probability greater than `epsilon_cutoff` will be sampled. + * In the paper, suggested values range from 3e-4 to 9e-4, depending on the size of the model. + * See [Truncation Sampling as Language Model Desmoothing](https://arxiv.org/abs/2210.15191) for more details. + * @type {number} + * @default 0.0 + */ + epsilon_cutoff = 0.0; + + /** + * Eta sampling is a hybrid of locally typical sampling and epsilon sampling. + * If set to float strictly between 0 and 1, a token is only considered if it is greater than either `eta_cutoff` or `sqrt(eta_cutoff) * exp(-entropy(softmax(next_token_logits)))`. + * The latter term is intuitively the expected next token probability, scaled by `sqrt(eta_cutoff)`. In the paper, suggested values range from 3e-4 to 2e-3, depending on the size of the model. + * See [Truncation Sampling as Language Model Desmoothing](https://arxiv.org/abs/2210.15191) for more details. + * @type {number} + * @default 0.0 + */ + eta_cutoff = 0.0; + + /** + * This value is subtracted from a beam's score if it generates a token same as any beam from other group at a particular time. + * Note that `diversity_penalty` is only effective if `group beam search` is enabled. + * @type {number} + * @default 0.0 + */ + diversity_penalty = 0.0; + + /** + * The parameter for repetition penalty. 1.0 means no penalty. + * See [this paper](https://arxiv.org/pdf/1909.05858.pdf) for more details. + * @type {number} + * @default 1.0 + */ + repetition_penalty = 1.0; + + /** + * The paramater for encoder_repetition_penalty. + * An exponential penalty on sequences that are not in the original input. + * 1.0 means no penalty. + * @type {number} + * @default 1.0 + */ + encoder_repetition_penalty = 1.0; + + /** + * Exponential penalty to the length that is used with beam-based generation. + * It is applied as an exponent to the sequence length, which in turn is used to divide the score of the sequence. + * Since the score is the log likelihood of the sequence (i.e. negative), `length_penalty` > 0.0 promotes longer sequences, while `length_penalty` < 0.0 encourages shorter sequences. + * @type {number} + * @default 1.0 + */ + length_penalty = 1.0; + + /** + * If set to int > 0, all ngrams of that size can only occur once. + * @type {number} + * @default 0 + */ + no_repeat_ngram_size = 0; + + /** + * List of token ids that are not allowed to be generated. + * In order to get the token ids of the words that should not appear in the generated text, use + * `tokenizer(bad_words, { add_prefix_space: true, add_special_tokens: false }).input_ids`. + * @type {number[][]} + * @default null + */ + bad_words_ids = null; + + /** + * List of token ids that must be generated. + * If given a `number[][]`, this is treated as a simple list of words that must be included, the opposite to `bad_words_ids`. + * If given `number[][][]`, this triggers a [disjunctive constraint](https://github.com/huggingface/transformers/issues/14081), where one can allow different forms of each word. + * @type {number[][]|number[][][]} + * @default null + */ + force_words_ids = null; + + /** + * Whether to renormalize the logits after applying all the logits processors or warpers (including the custom ones). + * It's highly recommended to set this flag to `true` as the search algorithms suppose the score logits are normalized but some logit processors or warpers break the normalization. + * @type {boolean} + * @default false + */ + renormalize_logits = false; + + /** + * Custom constraints that can be added to the generation to ensure that the output will contain the use of certain tokens as defined by `Constraint` objects, in the most sensible way possible. + * @type {Object[]} + * @default null + */ + constraints = null; + + /** + * The id of the token to force as the first generated token after the `decoder_start_token_id`. + * Useful for multilingual models like mBART where the first generated token needs to be the target language token. + * @type {number} + * @default null + */ + forced_bos_token_id = null; + + /** + * The id of the token to force as the last generated token when `max_length` is reached. + * Optionally, use a list to set multiple *end-of-sequence* tokens. + * @type {number|number[]} + * @default null + */ + forced_eos_token_id = null; + + /** + * Whether to remove possible *nan* and *inf* outputs of the model to prevent the generation method to crash. Note that using `remove_invalid_values` can slow down generation. + * @type {boolean} + */ + remove_invalid_values = false; + + /** + * This Tuple adds an exponentially increasing length penalty, after a certain amount of tokens have been generated. + * The tuple shall consist of: `(start_index, decay_factor)` where `start_index` indicates where penalty starts and `decay_factor` represents the factor of exponential decay. + * @type {[number, number]} + * @default null + */ + exponential_decay_length_penalty = null; + + /** + * A list of tokens that will be suppressed at generation. + * The `SuppressTokens` logit processor will set their log probs to `-inf` so that they are not sampled. + * @type {number[]} + * @default null + */ + suppress_tokens = null; + + /** + * A list of tokens that will be suppressed at the beginning of the generation. + * The `SuppressBeginTokens` logit processor will set their log probs to `-inf` so that they are not sampled. + * @type {number[]} + * @default null + */ + begin_suppress_tokens = null; + + /** + * A list of pairs of integers which indicates a mapping from generation indices to token indices that will be forced before sampling. + * For example, `[[1, 123]]` means the second generated token will always be a token of index 123. + * @type {[number, number][]} + * @default null + */ + forced_decoder_ids = null; + + /** + * The guidance scale for classifier free guidance (CFG). CFG is enabled by setting `guidance_scale > 1`. + * Higher guidance scale encourages the model to generate samples that are more closely linked to the input + * prompt, usually at the expense of poorer quality. + * @type {number} + * @default null + */ + guidance_scale = null; + + // Parameters that define the output variables of `generate` + /** + * The number of independently computed returned sequences for each element in the batch. + * @type {number} + * @default 1 + */ + num_return_sequences = 1; + + /** + * Whether or not to return the attentions tensors of all attention layers. + * See `attentions` under returned tensors for more details. + * @type {boolean} + * @default false + */ + output_attentions = false; + + /** + * Whether or not to return the hidden states of all layers. + * See `hidden_states` under returned tensors for more details. + * @type {boolean} + * @default false + */ + output_hidden_states = false; + + /** + * Whether or not to return the prediction scores. + * See `scores` under returned tensors for more details. + * @type {boolean} + * @default false + */ + output_scores = false; + + /** + * Whether or not to return a `ModelOutput` instead of a plain tuple. + * @type {boolean} + * @default false + */ + return_dict_in_generate = false; + + // Special tokens that can be used at generation time + /** + * The id of the *padding* token. + * @type {number} + * @default null + */ + pad_token_id = null; + + /** + * The id of the *beginning-of-sequence* token. + * @type {number} + * @default null + */ + bos_token_id = null; + + /** + * The id of the *end-of-sequence* token. + * Optionally, use a list to set multiple *end-of-sequence* tokens. + * @type {number|number[]} + * @default null + */ + eos_token_id = null; + + // Generation parameters exclusive to encoder-decoder models + /** + * If set to int > 0, all ngrams of that size that occur in the `encoder_input_ids` cannot occur in the `decoder_input_ids`. + * @type {number} + * @default 0 + */ + encoder_no_repeat_ngram_size = 0; + + /** + * If an encoder-decoder model starts decoding with a different token than *bos*, the id of that token. + * @type {number} + * @default null + */ + decoder_start_token_id = null; + + // Wild card + /** + * Additional generation kwargs will be forwarded to the `generate` function of the model. + * Kwargs that are not present in `generate`'s signature will be used in the model forward pass. + * @type {Object} + * @default {} + */ + generation_kwargs = {}; + + /** + * + * @param {GenerationConfig|import('../configs.js').PretrainedConfig} config + */ + constructor(config) { + Object.assign(this, (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_0__.pick)(config, Object.getOwnPropertyNames(this))); + } +} + + + +/***/ }), + +/***/ "./src/generation/logits_process.js": +/*!******************************************!*\ + !*** ./src/generation/logits_process.js ***! + \******************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ ClassifierFreeGuidanceLogitsProcessor: () => (/* binding */ ClassifierFreeGuidanceLogitsProcessor), +/* harmony export */ ForcedBOSTokenLogitsProcessor: () => (/* binding */ ForcedBOSTokenLogitsProcessor), +/* harmony export */ ForcedEOSTokenLogitsProcessor: () => (/* binding */ ForcedEOSTokenLogitsProcessor), +/* harmony export */ LogitsProcessor: () => (/* binding */ LogitsProcessor), +/* harmony export */ LogitsProcessorList: () => (/* binding */ LogitsProcessorList), +/* harmony export */ LogitsWarper: () => (/* binding */ LogitsWarper), +/* harmony export */ MinLengthLogitsProcessor: () => (/* binding */ MinLengthLogitsProcessor), +/* harmony export */ MinNewTokensLengthLogitsProcessor: () => (/* binding */ MinNewTokensLengthLogitsProcessor), +/* harmony export */ NoBadWordsLogitsProcessor: () => (/* binding */ NoBadWordsLogitsProcessor), +/* harmony export */ NoRepeatNGramLogitsProcessor: () => (/* binding */ NoRepeatNGramLogitsProcessor), +/* harmony export */ RepetitionPenaltyLogitsProcessor: () => (/* binding */ RepetitionPenaltyLogitsProcessor), +/* harmony export */ SuppressTokensAtBeginLogitsProcessor: () => (/* binding */ SuppressTokensAtBeginLogitsProcessor), +/* harmony export */ TemperatureLogitsWarper: () => (/* binding */ TemperatureLogitsWarper), +/* harmony export */ TopKLogitsWarper: () => (/* binding */ TopKLogitsWarper), +/* harmony export */ TopPLogitsWarper: () => (/* binding */ TopPLogitsWarper), +/* harmony export */ WhisperTimeStampLogitsProcessor: () => (/* binding */ WhisperTimeStampLogitsProcessor) +/* harmony export */ }); +/* harmony import */ var _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../utils/generic.js */ "./src/utils/generic.js"); +/* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../utils/tensor.js */ "./src/utils/tensor.js"); +/* harmony import */ var _utils_maths_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../utils/maths.js */ "./src/utils/maths.js"); + +/** + * @module generation/logits_process + */ + + + + + + +/** + * Abstract base class for all logit processors that can be applied during generation. + */ +class LogitsProcessor extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * Apply the processor to the input logits. + * + * @abstract + * @param {bigint[][]} input_ids The input ids. + * @param {Tensor} logits The logits to process. + * @throws {Error} Throws an error if `_call` is not implemented in the subclass. + */ + _call(input_ids, logits) { + throw Error("`_call` should be implemented in a subclass") + } +} + + +/** + * Abstract base class for all logit warpers that can be applied during generation with multinomial sampling. + */ +class LogitsWarper extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * Apply the processor to the input logits. + * + * @abstract + * @param {bigint[][]} input_ids The input ids. + * @param {Tensor} logits The logits to process. + * @throws {Error} Throws an error if `_call` is not implemented in the subclass. + */ + _call(input_ids, logits) { + throw Error("`_call` should be implemented in a subclass") + } +} + + +/** + * A class representing a list of logits processors. A logits processor is a function that modifies the logits + * output of a language model. This class provides methods for adding new processors and applying all processors to a + * batch of logits. + */ +class LogitsProcessorList extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * Constructs a new instance of `LogitsProcessorList`. + */ + constructor() { + super(); + this.processors = []; + } + + /** + * Adds a new logits processor to the list. + * + * @param {LogitsProcessor} item The logits processor function to add. + */ + push(item) { + this.processors.push(item); + } + + /** + * Adds multiple logits processors to the list. + * + * @param {LogitsProcessor[]} items The logits processor functions to add. + */ + extend(items) { + this.processors.push(...items); + } + + /** + * Applies all logits processors in the list to a batch of logits, modifying them in-place. + * + * @param {bigint[][]} input_ids The input IDs for the language model. + * @param {Tensor} logits + */ + _call(input_ids, logits) { + let toReturn = logits; + // NOTE: Most processors modify logits inplace + for (const processor of this.processors) { + toReturn = processor(input_ids, toReturn); + } + return toReturn; + } + + [Symbol.iterator]() { + return this.processors.values(); + } +} + +// DEPRECATED: https://github.com/huggingface/transformers/pull/29485 +// /** +// * A logits processor that forces a specific token to be generated by the decoder. +// */ +// export class ForceTokensLogitsProcessor extends LogitsProcessor { +// /** +// * Constructs a new instance of `ForceTokensLogitsProcessor`. +// * +// * @param {[number, number][]} forced_decoder_ids The ids of tokens that should be forced. +// */ +// constructor(forced_decoder_ids) { +// super(); +// // TODO: convert to `new Map(forced_decoder_ids)` +// this.force_token_map = Object.fromEntries(forced_decoder_ids ?? []); +// } + +// /** +// * Apply the processor to the input logits. +// * +// * @param {bigint[][]} input_ids The input ids. +// * @param {Tensor} logits The logits to process. +// * @returns {Tensor} The processed logits. +// */ +// _call(input_ids, logits) { +// console.log('this.force_token_map', this.force_token_map) +// console.log('call ForceTokensLogitsProcessor', input_ids, logits) +// console.log('input_ids.length', input_ids.length) +// let map = this.force_token_map[input_ids.length]; +// if (map) { // There exists a mapping +// logits.data.fill(-Infinity) +// logits.data[map] = 0; +// } +// console.log('map', map) +// // throw Error("Not implemented") +// return logits; +// } +// } + +/** + * A LogitsProcessor that forces a BOS token at the beginning of the generated sequence. + */ +class ForcedBOSTokenLogitsProcessor extends LogitsProcessor { + /** + * Create a ForcedBOSTokenLogitsProcessor. + * @param {number} bos_token_id The ID of the beginning-of-sequence token to be forced. + */ + constructor(bos_token_id) { + super(); + this.bos_token_id = bos_token_id; + } + + /** + * Apply the BOS token forcing to the logits. + * @param {bigint[][]} input_ids The input IDs. + * @param {Tensor} logits The logits. + * @returns {Object} The logits with BOS token forcing. + */ + _call(input_ids, logits) { + for (let i = 0; i < input_ids.length; ++i) { + if (input_ids[i].length === 1) { + const batch_logits_data = /** @type {Float32Array} */(logits[i].data); + batch_logits_data.fill(-Infinity); + batch_logits_data[this.bos_token_id] = 0; + } + } + return logits; + } +} + +/** + * A logits processor that enforces the specified token as the last generated token when `max_length` is reached. + */ +class ForcedEOSTokenLogitsProcessor extends LogitsProcessor { + /** + * Create a ForcedEOSTokenLogitsProcessor. + * @param {number} max_length The maximum length of the sequence to be generated. + * @param {number|number[]} eos_token_id The id(s) of the *end-of-sequence* token. + */ + constructor(max_length, eos_token_id) { + super(); + this.max_length = max_length; + this.eos_token_id = Array.isArray(eos_token_id) ? eos_token_id : [eos_token_id]; + } + + /** + * Apply the processor to input_ids and logits. + * + * @param {bigint[][]} input_ids The input ids. + * @param {Tensor} logits The logits tensor. + */ + _call(input_ids, logits) { + for (let i = 0; i < input_ids.length; ++i) { + if (input_ids[i].length === this.max_length - 1) { + const batch_logits_data = /** @type {Float32Array} */(logits[i].data); + batch_logits_data.fill(-Infinity); + for (const eos_token of this.eos_token_id) { + batch_logits_data[eos_token] = 0; + } + } + } + return logits; + } +} + +/** + * A LogitsProcessor that suppresses a list of tokens as soon as the `generate` function starts + * generating using `begin_index` tokens. This should ensure that the tokens defined by + * `begin_suppress_tokens` at not sampled at the begining of the generation. + */ +class SuppressTokensAtBeginLogitsProcessor extends LogitsProcessor { + /** + * Create a SuppressTokensAtBeginLogitsProcessor. + * @param {number[]} begin_suppress_tokens The IDs of the tokens to suppress. + * @param {number} begin_index The number of tokens to generate before suppressing tokens. + */ + constructor(begin_suppress_tokens, begin_index) { + super(); + this.begin_suppress_tokens = begin_suppress_tokens; + this.begin_index = begin_index; + } + + /** + * Apply the BOS token forcing to the logits. + * @param {bigint[][]} input_ids The input IDs. + * @param {Tensor} logits The logits. + * @returns {Object} The logits with BOS token forcing. + */ + _call(input_ids, logits) { + for (let i = 0; i < input_ids.length; ++i) { + if (input_ids[i].length === this.begin_index) { + const batch_logits_data = /** @type {Float32Array} */(logits[i].data); + for (const token_id of this.begin_suppress_tokens) { + batch_logits_data[token_id] = -Infinity; + } + } + } + return logits; + } +} + +/** + * A LogitsProcessor that handles adding timestamps to generated text. + */ +class WhisperTimeStampLogitsProcessor extends LogitsProcessor { + /** + * Constructs a new WhisperTimeStampLogitsProcessor. + * @param {import('../models/whisper/generation_whisper.js').WhisperGenerationConfig} generate_config The config object passed to the `generate()` method of a transformer model. + * @param {number[]} init_tokens The initial tokens of the input sequence. + */ + constructor(generate_config, init_tokens) { + super(); + this.eos_token_id = + Array.isArray(generate_config.eos_token_id) + ? generate_config.eos_token_id[0] + : generate_config.eos_token_id; + + this.no_timestamps_token_id = generate_config.no_timestamps_token_id; + this.timestamp_begin = this.no_timestamps_token_id + 1; + + this.begin_index = init_tokens.length; + if (init_tokens.at(-1) === this.no_timestamps_token_id) { + this.begin_index -= 1; + } + this.max_initial_timestamp_index = generate_config.max_initial_timestamp_index; + } + + /** + * Modify the logits to handle timestamp tokens. + * @param {bigint[][]} input_ids The input sequence of tokens. + * @param {Tensor} logits The logits output by the model. + * @returns {Tensor} The modified logits. + */ + _call(input_ids, logits) { + for (let i = 0; i < input_ids.length; ++i) { + const batch_logits_data = /** @type {Float32Array} */(logits[i].data); + + // suppress <|notimestamps|> which is handled by without_timestamps + batch_logits_data[this.no_timestamps_token_id] = -Infinity; + + if (input_ids[i].length === this.begin_index - 1) { + batch_logits_data.fill(-Infinity); + batch_logits_data[this.timestamp_begin] = 0; + continue; + } + + // timestamps have to appear in pairs, except directly before eos_token; mask logits accordingly + const seq = input_ids[i].slice(this.begin_index); + const last_was_timestamp = seq.length >= 1 && seq[seq.length - 1] >= this.timestamp_begin; + const penultimate_was_timestamp = seq.length < 2 || seq[seq.length - 2] >= this.timestamp_begin; + + if (last_was_timestamp) { + if (penultimate_was_timestamp) { // has to be non-timestamp + batch_logits_data.subarray(this.timestamp_begin).fill(-Infinity); + } else { // cannot be normal text tokens + batch_logits_data.subarray(0, this.eos_token_id).fill(-Infinity); + } + } + + // apply the `max_initial_timestamp` option + if (input_ids[i].length === this.begin_index && this.max_initial_timestamp_index !== null) { + const last_allowed = this.timestamp_begin + this.max_initial_timestamp_index; + batch_logits_data.subarray(last_allowed + 1).fill(-Infinity); + } + + // if sum of probability over timestamps is above any other token, sample timestamp + const logprobs = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_2__.log_softmax)(batch_logits_data); + const timestamp_logprob = Math.log(logprobs.subarray(this.timestamp_begin).map(Math.exp).reduce((a, b) => a + b)); + const max_text_token_logprob = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_2__.max)(logprobs.subarray(0, this.timestamp_begin))[0]; + + if (timestamp_logprob > max_text_token_logprob) { + batch_logits_data.subarray(0, this.timestamp_begin).fill(-Infinity); + } + } + + return logits; + } +} + +/** + * A logits processor that disallows ngrams of a certain size to be repeated. + */ +class NoRepeatNGramLogitsProcessor extends LogitsProcessor { + /** + * Create a NoRepeatNGramLogitsProcessor. + * @param {number} no_repeat_ngram_size The no-repeat-ngram size. All ngrams of this size can only occur once. + */ + constructor(no_repeat_ngram_size) { + super(); + this.no_repeat_ngram_size = no_repeat_ngram_size; + } + + /** + * Generate n-grams from a sequence of token ids. + * @param {bigint[]} prevInputIds List of previous input ids + * @returns {Map} Map of generated n-grams + */ + getNgrams(prevInputIds) { + const curLen = prevInputIds.length; + + /**@type {number[][]} */ + const ngrams = []; + for (let j = 0; j < curLen + 1 - this.no_repeat_ngram_size; ++j) { + const ngram = []; + for (let k = 0; k < this.no_repeat_ngram_size; ++k) { + ngram.push(prevInputIds[j + k]); + } + ngrams.push(ngram.map(Number)); + } + + /** @type {Map} */ + const generatedNgram = new Map(); + for (const ngram of ngrams) { + const prevNgram = ngram.slice(0, ngram.length - 1); + const prevNgramKey = JSON.stringify(prevNgram); + const prevNgramValue = generatedNgram.get(prevNgramKey) ?? []; + prevNgramValue.push(ngram[ngram.length - 1]); + generatedNgram.set(prevNgramKey, prevNgramValue); + } + return generatedNgram; + } + + /** + * Generate n-grams from a sequence of token ids. + * @param {Map} bannedNgrams Map of banned n-grams + * @param {bigint[]} prevInputIds List of previous input ids + * @returns {number[]} Map of generated n-grams + */ + getGeneratedNgrams(bannedNgrams, prevInputIds) { + const ngramIdx = prevInputIds.slice(prevInputIds.length + 1 - this.no_repeat_ngram_size, prevInputIds.length); + const banned = bannedNgrams.get(JSON.stringify(ngramIdx.map(Number))) ?? []; + return banned; + } + + /** + * Calculate banned n-gram tokens + * @param {bigint[]} prevInputIds List of previous input ids + * @returns {number[]} Map of generated n-grams + */ + calcBannedNgramTokens(prevInputIds) { + const bannedTokens = []; + if (prevInputIds.length + 1 < this.no_repeat_ngram_size) { + // return no banned tokens if we haven't generated no_repeat_ngram_size tokens yet + return bannedTokens; + + } else { + const generatedNgrams = this.getNgrams(prevInputIds); + const bannedTokens = this.getGeneratedNgrams(generatedNgrams, prevInputIds); + return bannedTokens; + } + } + + /** + * Apply the no-repeat-ngram processor to the logits. + * @param {bigint[][]} input_ids The input IDs. + * @param {Tensor} logits The logits. + * @returns {Object} The logits with no-repeat-ngram processing. + */ + _call(input_ids, logits) { + for (let i = 0; i < input_ids.length; ++i) { + const batch_logits_data = /** @type {Float32Array} */(logits[i].data); + const bannedTokens = this.calcBannedNgramTokens(input_ids[i]); + for (const token of bannedTokens) { + batch_logits_data[token] = -Infinity; + } + } + return logits; + } +} + +/** + * A logits processor that penalises repeated output tokens. + */ +class RepetitionPenaltyLogitsProcessor extends LogitsProcessor { + /** + * Create a RepetitionPenaltyLogitsProcessor. + * @param {number} penalty The penalty to apply for repeated tokens. + */ + constructor(penalty) { + super(); + this.penalty = penalty; + } + + /** + * Apply the repetition penalty to the logits. + * @param {bigint[][]} input_ids The input IDs. + * @param {Tensor} logits The logits. + * @returns {Object} The logits with repetition penalty processing. + */ + _call(input_ids, logits) { + // Modify the logits corresponding to each element in `input_ids`. + // As a consequence, the logits corresponding to tokens that appear + // many times in the output will be penalised more. + + for (let i = 0; i < input_ids.length; ++i) { + const batch_logits_data = /** @type {Float32Array} */(logits[i].data); + for (const input_id of input_ids[i]) { + const token = Number(input_id); + if (batch_logits_data[token] < 0) { + batch_logits_data[token] *= this.penalty; + } else { + batch_logits_data[token] /= this.penalty; + } + } + } + + return logits + } +} + +/** + * A logits processor that enforces a minimum number of tokens. + */ +class MinLengthLogitsProcessor extends LogitsProcessor { + /** + * Create a MinLengthLogitsProcessor. + * @param {number} min_length The minimum length below which the score of `eos_token_id` is set to negative infinity. + * @param {number|number[]} eos_token_id The ID/IDs of the end-of-sequence token. + */ + constructor(min_length, eos_token_id) { + super(); + this.min_length = min_length; + this.eos_token_id = Array.isArray(eos_token_id) ? eos_token_id : [eos_token_id]; + } + + /** + * Apply logit processor. + * @param {bigint[][]} input_ids The input IDs. + * @param {Tensor} logits The logits. + * @returns {Object} The processed logits. + */ + _call(input_ids, logits) { + for (let i = 0; i < input_ids.length; ++i) { + if (input_ids[i].length < this.min_length) { + const batch_logits_data = /** @type {Float32Array} */(logits[i].data); + + for (const eos_token of this.eos_token_id) { + batch_logits_data[eos_token] = -Infinity; + } + } + } + + return logits + } +} + +/** + * A logits processor that enforces a minimum number of new tokens. + */ +class MinNewTokensLengthLogitsProcessor extends LogitsProcessor { + /** + * Create a MinNewTokensLengthLogitsProcessor. + * @param {number} prompt_length_to_skip The input tokens length. + * @param {number} min_new_tokens The minimum *new* tokens length below which the score of `eos_token_id` is set to negative infinity. + * @param {number|number[]} eos_token_id The ID/IDs of the end-of-sequence token. + */ + constructor(prompt_length_to_skip, min_new_tokens, eos_token_id) { + super(); + this.prompt_length_to_skip = prompt_length_to_skip; + this.min_new_tokens = min_new_tokens; + this.eos_token_id = Array.isArray(eos_token_id) ? eos_token_id : [eos_token_id]; + } + + /** + * Apply logit processor. + * @param {bigint[][]} input_ids The input IDs. + * @param {Tensor} logits The logits. + * @returns {Object} The processed logits. + */ + _call(input_ids, logits) { + for (let i = 0; i < input_ids.length; ++i) { + const new_tokens_length = input_ids[i].length - this.prompt_length_to_skip; + if (new_tokens_length < this.min_new_tokens) { + const batch_logits_data = /** @type {Float32Array} */(logits[i].data); + + for (const eos_token of this.eos_token_id) { + batch_logits_data[eos_token] = -Infinity; + } + } + } + return logits + } +} + +class NoBadWordsLogitsProcessor extends LogitsProcessor { + /** + * Create a `NoBadWordsLogitsProcessor`. + * @param {number[][]} bad_words_ids List of list of token ids that are not allowed to be generated. + * @param {number|number[]} eos_token_id The id of the *end-of-sequence* token. Optionally, use a list to set multiple *end-of-sequence* tokens. + */ + constructor(bad_words_ids, eos_token_id) { + super(); + this.bad_words_ids = bad_words_ids; + this.eos_token_id = Array.isArray(eos_token_id) ? eos_token_id : [eos_token_id]; + } + + /** + * Apply logit processor. + * @param {bigint[][]} input_ids The input IDs. + * @param {Tensor} logits The logits. + * @returns {Object} The processed logits. + */ + _call(input_ids, logits) { + for (let i = 0; i < input_ids.length; ++i) { + const batch_logits_data = /** @type {Float32Array} */(logits[i].data); + const ids = input_ids[i]; + for (const bad_word_ids of this.bad_words_ids) { + // Whether to modify the logits of the last token in the bad word id sequence + let mark = true; + + // For each bad word in the list, if the current sequence of input ids ends with this sequence (excluding the last), + // then we set the logits of the last bad word id to -Infinity. + for (let j = 1; j <= bad_word_ids.length - 1 && bad_word_ids.length < ids.length; ++j) { + + // NOTE: We use != instead of !== to compare bigint and number + // @ts-ignore + if (bad_word_ids.at(-j - 1) != ids.at(-j)) { + // We have found a mismatch + mark = false; + break; + } + } + if (mark) { + batch_logits_data[bad_word_ids.at(-1)] = -Infinity; + } + } + } + return logits + } +} + +/** + * [`LogitsProcessor`] for classifier free guidance (CFG). The scores are split over the batch dimension, + * where the first half correspond to the conditional logits (predicted from the input prompt) and the second half + * correspond to the unconditional logits (predicted from an empty or 'null' prompt). The processor computes a + * weighted average across the conditional and unconditional logits, parameterised by the `guidance_scale`. + * + * See [the paper](https://arxiv.org/abs/2306.05284) for more information. + */ +class ClassifierFreeGuidanceLogitsProcessor extends LogitsProcessor { + + /** + * Create a `ClassifierFreeGuidanceLogitsProcessor`. + * @param {number} guidance_scale The guidance scale for classifier free guidance (CFG). CFG is enabled by setting `guidance_scale > 1`. + * Higher guidance scale encourages the model to generate samples that are more closely linked to the input + * prompt, usually at the expense of poorer quality. + */ + constructor(guidance_scale) { + super(); + if (guidance_scale <= 1) { + throw new Error( + `Require guidance scale >1 to use the classifier free guidance processor, got guidance scale ${guidance_scale}.` + ) + } + this.guidance_scale = guidance_scale; + } + + /** + * Apply logit processor. + * @param {bigint[][]} input_ids The input IDs. + * @param {Tensor} logits The logits. + * @returns {Object} The processed logits. + */ + _call(input_ids, logits) { + if (logits.dims[0] !== 2 * input_ids.length) { + throw new Error( + `Logits should have twice the batch size of the input ids, the first half of batches corresponding to ` + + `the conditional inputs, and the second half of batches corresponding to the unconditional inputs. Got ` + + `batch size ${logits.dims[0]} for the logits and ${input_ids.length} for the input ids.` + ) + } + + const unguided_bsz = input_ids.length; + const cond_logits = logits.slice([0, unguided_bsz], null); + const uncond_logits = logits.slice([unguided_bsz, logits.dims[0]], null); + + // Merge into uncond_logits (to save memory). This is equivalent to the following: + // scores = uncond_logits + (cond_logits - uncond_logits) * guidance_scale + for (let i = 0; i < uncond_logits.data.length; ++i) { + uncond_logits.data[i] += (cond_logits.data[i] - uncond_logits.data[i]) * this.guidance_scale; + } + + return uncond_logits; + } +} + +/** + * [`LogitsWarper`] for temperature (exponential scaling output probability distribution), which effectively means + * that it can control the randomness of the predicted tokens. Often used together with [`TopPLogitsWarper`] and [`TopKLogitsWarper`]. + */ +class TemperatureLogitsWarper extends LogitsWarper { + /** + * Create a `TemperatureLogitsWarper`. + * @param {number} temperature Strictly positive float value used to modulate the logits distribution. + * A value smaller than `1` decreases randomness (and vice versa), with `0` being equivalent to shifting + * all probability mass to the most likely token. + */ + constructor(temperature) { + super(); + + if (typeof temperature !== 'number' || temperature <= 0) { + let errorMessage = + `\`temperature\` (=${temperature}) must be a strictly positive float, otherwise your next token scores will be invalid.`; + + if (temperature === 0) { + errorMessage += " If you're looking for greedy decoding strategies, set `do_sample=false`." + } + } + this.temperature = temperature; + } + + /** + * Apply logit warper. + * @param {bigint[][]} input_ids The input IDs. + * @param {Tensor} logits The logits. + * @returns {Object} The processed logits. + */ + _call(input_ids, logits) { + const batch_logits_data = /** @type {Float32Array} */(logits.data); + for (let i = 0; i < batch_logits_data.length; ++i) { + batch_logits_data[i] /= this.temperature; + } + return logits; + } +} + +/** + * [`LogitsWarper`] that performs top-p, i.e. restricting to top tokens summing to prob_cut_off <= prob_cut_off. + * Often used together with [`TemperatureLogitsWarper`] and [`TopKLogitsWarper`]. + */ +class TopPLogitsWarper extends LogitsWarper { + /** + * Create a `TopPLogitsWarper`. + * @param {number} top_p If set to < 1, only the smallest set of most probable tokens with + * probabilities that add up to `top_p` or higher are kept for generation. + * @param {Object} options Additional options for the top-p sampling. + * @param {number} [options.filter_value=-Infinity] All filtered values will be set to this float value. + * @param {number} [options.min_tokens_to_keep=1] Minimum number of tokens that cannot be filtered. + */ + constructor(top_p, { + filter_value = -Infinity, + min_tokens_to_keep = 1, + } = {}) { + super(); + if (top_p < 0 || top_p > 1.0) { + throw new Error(`\`top_p\` must be a float > 0 and < 1, but is ${top_p}`) + } + if (!Number.isInteger(min_tokens_to_keep) || min_tokens_to_keep < 1) { + throw new Error(`\`min_tokens_to_keep\` must be a positive integer, but is ${min_tokens_to_keep}`) + } + + this.top_p = top_p + this.filter_value = filter_value + this.min_tokens_to_keep = min_tokens_to_keep + } +} + +/** + * [`LogitsWarper`] that performs top-k, i.e. restricting to the k highest probability elements. + * Often used together with [`TemperatureLogitsWarper`] and [`TopPLogitsWarper`]. + */ +class TopKLogitsWarper extends LogitsWarper { + /** + * Create a `TopKLogitsWarper`. + * @param {number} top_k If set to > 0, only the top `top_k` tokens are kept for generation. + * @param {Object} options Additional options for the top-k sampling. + * @param {number} [options.filter_value=-Infinity] All filtered values will be set to this float value. + * @param {number} [options.min_tokens_to_keep=1] Minimum number of tokens that cannot be filtered. + */ + constructor(top_k, { + filter_value = -Infinity, + min_tokens_to_keep = 1, + } = {}) { + super(); + if (!Number.isInteger(top_k) || top_k < 0) { + throw new Error(`\`top_k\` must be a positive integer, but is ${top_k}`) + } + + this.top_k = Math.max(top_k, min_tokens_to_keep) + this.filter_value = filter_value + } +} + +/***/ }), + +/***/ "./src/generation/logits_sampler.js": +/*!******************************************!*\ + !*** ./src/generation/logits_sampler.js ***! + \******************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ LogitsSampler: () => (/* binding */ LogitsSampler) +/* harmony export */ }); +/* harmony import */ var _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../utils/generic.js */ "./src/utils/generic.js"); +/* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../utils/tensor.js */ "./src/utils/tensor.js"); +/* harmony import */ var _utils_maths_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../utils/maths.js */ "./src/utils/maths.js"); +/* harmony import */ var _generation_configuration_utils_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ../generation/configuration_utils.js */ "./src/generation/configuration_utils.js"); + +/** + * @module generation/logits_sampler + */ + + + + + + + +/** + * Sampler is a base class for all sampling methods used for text generation. + */ +class LogitsSampler extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * Creates a new Sampler object with the specified generation config. + * @param {GenerationConfig} generation_config The generation config. + */ + constructor(generation_config) { + super(); + this.generation_config = generation_config; + } + + /** + * Executes the sampler, using the specified logits. + * @param {Tensor} logits + * @returns {Promise<[bigint, number][]>} + */ + async _call(logits) { + // Sample from logits, of dims [batch, sequence_length, vocab_size]. + // If index is specified, sample from [batch, index, vocab_size]. + return this.sample(logits); + } + + /** + * Abstract method for sampling the logits. + * @param {Tensor} logits + * @throws {Error} If not implemented in subclass. + * @returns {Promise<[bigint, number][]>} + */ + async sample(logits) { + throw Error("sample should be implemented in subclasses.") + } + + /** + * Returns the specified logits as an array, with temperature applied. + * @param {Tensor} logits + * @param {number} index + * @returns {Float32Array} + */ + getLogits(logits, index) { + let vocabSize = logits.dims.at(-1); + + let logs = /** @type {Float32Array} */(logits.data); + + if (index === -1) { + logs = logs.slice(-vocabSize); + } else { + let startIndex = index * vocabSize; + logs = logs.slice(startIndex, startIndex + vocabSize); + } + return logs; + } + + /** + * Selects an item randomly based on the specified probabilities. + * @param {import("../transformers.js").DataArray} probabilities An array of probabilities to use for selection. + * @returns {number} The index of the selected item. + */ + randomSelect(probabilities) { + // Return index of chosen item + let sumProbabilities = 0; + for (let i = 0; i < probabilities.length; ++i) { + sumProbabilities += probabilities[i]; + } + + let r = Math.random() * sumProbabilities; + for (let i = 0; i < probabilities.length; ++i) { + r -= probabilities[i]; + if (r <= 0) { + return i; + } + } + return 0; // return first (most probable) as a fallback + } + + /** + * Returns a Sampler object based on the specified options. + * @param {GenerationConfig} generation_config An object containing options for the sampler. + * @returns {LogitsSampler} A Sampler object. + */ + static getSampler(generation_config) { + // - *greedy decoding*: `num_beams=1` and `do_sample=False` + // - *contrastive search*: `penalty_alpha>0` and `top_k>1` + // - *multinomial sampling*: `num_beams=1` and `do_sample=True` + // - *beam-search decoding*: `num_beams>1` and `do_sample=False` + // - *beam-search multinomial sampling*: `num_beams>1` and `do_sample=True` + // - *diverse beam-search decoding*: `num_beams>1` and `num_beam_groups>1` + // - *constrained beam-search decoding*: `constraints!=None` or `force_words_ids!=None` + + // NOTE: beam search is implemented directly into the generation function + if (generation_config.do_sample) { + return new MultinomialSampler(generation_config); + + } else if (generation_config.num_beams > 1) { + return new BeamSearchSampler(generation_config); + + } else { + if (generation_config.num_return_sequences > 1) { + throw Error(`num_return_sequences has to be 1 when doing greedy search, but is ${generation_config.num_return_sequences}.`) + } + return new GreedySampler(generation_config); + } + } +} + +/** + * Class representing a Greedy Sampler. + */ +class GreedySampler extends LogitsSampler { + /** + * Sample the maximum probability of a given logits tensor. + * @param {Tensor} logits + * @returns {Promise<[bigint, number][]>} An array with a single tuple, containing the index of the maximum value and a meaningless score (since this is a greedy search). + */ + async sample(logits) { + // NOTE: no need to do log_softmax here since we only take the maximum + const argmax = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_2__.max)(logits.data)[1]; + + // Note: score is meaningless in this context, since we are performing + // greedy search (p = 1 => log(p) = 0) + return [ + [BigInt(argmax), 0] + ]; + } +} + +/** + * Class representing a MultinomialSampler. + */ +class MultinomialSampler extends LogitsSampler { + + /** + * Sample from the logits. + * @param {Tensor} logits + * @returns {Promise<[bigint, number][]>} + */ + async sample(logits) { + let k = logits.dims.at(-1); // defaults to vocab size + if (this.generation_config.top_k > 0) { + k = Math.min(this.generation_config.top_k, k); + } + + // Get top k tokens + const [v, i] = await (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.topk)(logits, k); + + // Compute softmax over logits + const probabilities = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_2__.softmax)(/** @type {Float32Array} */(v.data)); + + return Array.from({ length: this.generation_config.num_beams }, () => { + const sampledIndex = this.randomSelect(probabilities); + return [ + i.data[sampledIndex], // token id + Math.log(probabilities[sampledIndex]), // score + ]; + }); + } +} + + +/** + * Class representing a BeamSearchSampler. + */ +class BeamSearchSampler extends LogitsSampler { + + /** + * Sample from the logits. + * @param {Tensor} logits + * @returns {Promise<[bigint, number][]>} + */ + async sample(logits) { + let k = logits.dims.at(-1); // defaults to vocab size + if (this.generation_config.top_k > 0) { + k = Math.min(this.generation_config.top_k, k); + } + + // Get top k tokens + const [v, i] = await (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.topk)(logits, k); + + // Compute softmax over logits + const probabilities = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_2__.softmax)(/** @type {Float32Array} */(v.data)); + + return Array.from({ length: this.generation_config.num_beams }, (_, x) => { + return [ + i.data[x], // token id + Math.log(probabilities[x]), // score + ]; + }); + } +} + + +/***/ }), + +/***/ "./src/generation/stopping_criteria.js": +/*!*********************************************!*\ + !*** ./src/generation/stopping_criteria.js ***! + \*********************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ EosTokenCriteria: () => (/* binding */ EosTokenCriteria), +/* harmony export */ InterruptableStoppingCriteria: () => (/* binding */ InterruptableStoppingCriteria), +/* harmony export */ MaxLengthCriteria: () => (/* binding */ MaxLengthCriteria), +/* harmony export */ StoppingCriteria: () => (/* binding */ StoppingCriteria), +/* harmony export */ StoppingCriteriaList: () => (/* binding */ StoppingCriteriaList) +/* harmony export */ }); +/* harmony import */ var _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../utils/generic.js */ "./src/utils/generic.js"); + +/** + * @module generation/stopping_criteria + */ + + + +// NOTE: +// Stopping Criteria returns a list of `batch_size` booleans, indicating whether each sequence in the batch should be stopped. + +/** + * Abstract base class for all stopping criteria that can be applied during generation. + */ +class StoppingCriteria extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * + * @param {number[][]} input_ids (`number[][]` of shape `(batch_size, sequence_length)`): + * Indices of input sequence tokens in the vocabulary. + * @param {number[][]} scores scores (`number[][]` of shape `(batch_size, config.vocab_size)`): + * Prediction scores of a language modeling head. These can be scores for each vocabulary token before SoftMax + * or scores for each vocabulary token after SoftMax. + * @returns {boolean[]} A list of booleans indicating whether each sequence should be stopped. + */ + _call(input_ids, scores) { + throw Error("StoppingCriteria needs to be subclassed"); + } +} +/** + */ +class StoppingCriteriaList extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * Constructs a new instance of `StoppingCriteriaList`. + */ + constructor() { + super(); + this.criteria = []; + } + + /** + * Adds a new stopping criterion to the list. + * + * @param {StoppingCriteria} item The stopping criterion to add. + */ + push(item) { + this.criteria.push(item); + } + + /** + * Adds multiple stopping criteria to the list. + * + * @param {StoppingCriteria|StoppingCriteriaList|StoppingCriteria[]} items The stopping criteria to add. + */ + extend(items) { + if (items instanceof StoppingCriteriaList) { + items = items.criteria; + } else if (items instanceof StoppingCriteria) { + items = [items]; + } + this.criteria.push(...items); + } + + _call(input_ids, scores) { + const is_done = new Array(input_ids.length).fill(false); + for (const criterion of this.criteria) { + const criterion_done = criterion(input_ids, scores); + for (let i = 0; i < is_done.length; ++i) { + is_done[i] ||= criterion_done[i]; + } + } + return is_done; + } + + [Symbol.iterator]() { + return this.criteria.values(); + } +} + +/** + * This class can be used to stop generation whenever the full generated number of tokens exceeds `max_length`. + * Keep in mind for decoder-only type of transformers, this will include the initial prompted tokens. + */ +class MaxLengthCriteria extends StoppingCriteria { + + /** + * + * @param {number} max_length The maximum length that the output sequence can have in number of tokens. + * @param {number} [max_position_embeddings=null] The maximum model length, as defined by the model's `config.max_position_embeddings` attribute. + */ + constructor(max_length, max_position_embeddings = null) { + super(); + this.max_length = max_length; + this.max_position_embeddings = max_position_embeddings; + } + + _call(input_ids) { + return input_ids.map(ids => ids.length >= this.max_length); + } +} + +// TODO: add MaxTimeCriteria + +/** + * This class can be used to stop generation whenever the "end-of-sequence" token is generated. + * By default, it uses the `model.generation_config.eos_token_id`. + */ +class EosTokenCriteria extends StoppingCriteria { + + /** + * + * @param {number|number[]} eos_token_id The id of the *end-of-sequence* token. + * Optionally, use a list to set multiple *end-of-sequence* tokens. + */ + constructor(eos_token_id) { + super(); + if (!Array.isArray(eos_token_id)) { + eos_token_id = [eos_token_id]; + } + this.eos_token_id = eos_token_id; + } + + /** + * + * @param {number[][]} input_ids + * @param {number[][]} scores + * @returns {boolean[]} + */ + _call(input_ids, scores) { + return input_ids.map(ids => { + const last = ids.at(-1); + // NOTE: We use == instead of === to allow for number/bigint comparison + return this.eos_token_id.some(eos_id => last == eos_id); + }); + } +} + +/** + * This class can be used to stop generation whenever the user interrupts the process. + */ +class InterruptableStoppingCriteria extends StoppingCriteria { + constructor() { + super(); + this.interrupted = false; + } + + interrupt() { + this.interrupted = true; + } + + reset() { + this.interrupted = false; + } + + _call(input_ids, scores) { + return new Array(input_ids.length).fill(this.interrupted); + } +} + + +/***/ }), + +/***/ "./src/generation/streamers.js": +/*!*************************************!*\ + !*** ./src/generation/streamers.js ***! + \*************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ BaseStreamer: () => (/* binding */ BaseStreamer), +/* harmony export */ TextStreamer: () => (/* binding */ TextStreamer), +/* harmony export */ WhisperTextStreamer: () => (/* binding */ WhisperTextStreamer) +/* harmony export */ }); +/* harmony import */ var _utils_core_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../utils/core.js */ "./src/utils/core.js"); +/* harmony import */ var _tokenizers_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../tokenizers.js */ "./src/tokenizers.js"); +/* harmony import */ var _env_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../env.js */ "./src/env.js"); + +/** + * @module generation/streamers + */ + + + + + +class BaseStreamer { + /** + * Function that is called by `.generate()` to push new tokens + * @param {bigint[][]} value + */ + put(value) { + throw Error('Not implemented'); + } + + /** + * Function that is called by `.generate()` to signal the end of generation + */ + end() { + throw Error('Not implemented'); + } +} + +const stdout_write = _env_js__WEBPACK_IMPORTED_MODULE_2__.apis.IS_PROCESS_AVAILABLE + ? x => process.stdout.write(x) + : x => console.log(x); + +/** + * Simple text streamer that prints the token(s) to stdout as soon as entire words are formed. + */ +class TextStreamer extends BaseStreamer { + /** + * + * @param {import('../tokenizers.js').PreTrainedTokenizer} tokenizer + */ + constructor(tokenizer, { + skip_prompt = false, + callback_function = null, + token_callback_function = null, + decode_kwargs = {}, + ...kwargs + } = {}) { + super(); + this.tokenizer = tokenizer; + this.skip_prompt = skip_prompt; + this.callback_function = callback_function ?? stdout_write; + this.token_callback_function = token_callback_function; + this.decode_kwargs = { ...decode_kwargs, ...kwargs }; + + // variables used in the streaming process + this.token_cache = []; + this.print_len = 0; + this.next_tokens_are_prompt = true; + } + + /** + * Receives tokens, decodes them, and prints them to stdout as soon as they form entire words. + * @param {bigint[][]} value + */ + put(value) { + if (value.length > 1) { + throw Error('TextStreamer only supports batch size of 1'); + } + + if (this.skip_prompt && this.next_tokens_are_prompt) { + this.next_tokens_are_prompt = false; + return; + } + + const tokens = value[0]; + this.token_callback_function?.(tokens) + + // Add the new token to the cache and decodes the entire thing. + this.token_cache = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_0__.mergeArrays)(this.token_cache, tokens); + const text = this.tokenizer.decode(this.token_cache, this.decode_kwargs); + + let printable_text; + if (text.endsWith('\n')) { + // After the symbol for a new line, we flush the cache. + printable_text = text.slice(this.print_len); + this.token_cache = []; + this.print_len = 0; + } else if (text.length > 0 && (0,_tokenizers_js__WEBPACK_IMPORTED_MODULE_1__.is_chinese_char)(text.charCodeAt(text.length - 1))) { + // If the last token is a CJK character, we print the characters. + printable_text = text.slice(this.print_len); + this.print_len += printable_text.length; + } else { + // Otherwise, prints until the last space char (simple heuristic to avoid printing incomplete words, + // which may change with the subsequent token -- there are probably smarter ways to do this!) + printable_text = text.slice(this.print_len, text.lastIndexOf(' ') + 1); + this.print_len += printable_text.length; + } + + this.on_finalized_text(printable_text, false); + } + + /** + * Flushes any remaining cache and prints a newline to stdout. + */ + end() { + let printable_text; + if (this.token_cache.length > 0) { + const text = this.tokenizer.decode(this.token_cache, this.decode_kwargs); + printable_text = text.slice(this.print_len); + this.token_cache = []; + this.print_len = 0; + } else { + printable_text = ''; + } + this.next_tokens_are_prompt = true; + this.on_finalized_text(printable_text, true); + } + + /** + * Prints the new text to stdout. If the stream is ending, also prints a newline. + * @param {string} text + * @param {boolean} stream_end + */ + on_finalized_text(text, stream_end) { + if (text.length > 0) { + this.callback_function?.(text); + } + if (stream_end && this.callback_function === stdout_write && _env_js__WEBPACK_IMPORTED_MODULE_2__.apis.IS_PROCESS_AVAILABLE) { + this.callback_function?.('\n'); + } + } +} + +/** + * Utility class to handle streaming of tokens generated by whisper speech-to-text models. + * Callback functions are invoked when each of the following events occur: + * - A new chunk starts (on_chunk_start) + * - A new token is generated (callback_function) + * - A chunk ends (on_chunk_end) + * - The stream is finalized (on_finalize) + */ +class WhisperTextStreamer extends TextStreamer { + /** + * @param {import('../tokenizers.js').WhisperTokenizer} tokenizer + * @param {Object} options + * @param {boolean} [options.skip_prompt=false] Whether to skip the prompt tokens + * @param {function(string): void} [options.callback_function=null] Function to call when a piece of text is ready to display + * @param {function(string): void} [options.token_callback_function=null] Function to call when a new token is generated + * @param {function(number): void} [options.on_chunk_start=null] Function to call when a new chunk starts + * @param {function(number): void} [options.on_chunk_end=null] Function to call when a chunk ends + * @param {function(): void} [options.on_finalize=null] Function to call when the stream is finalized + * @param {number} [options.time_precision=0.02] Precision of the timestamps + * @param {boolean} [options.skip_special_tokens=true] Whether to skip special tokens when decoding + * @param {Object} [options.decode_kwargs={}] Additional keyword arguments to pass to the tokenizer's decode method + */ + constructor(tokenizer, { + skip_prompt = false, + callback_function = null, + token_callback_function = null, + on_chunk_start = null, + on_chunk_end = null, + on_finalize = null, + time_precision = 0.02, + skip_special_tokens = true, + decode_kwargs = {}, + } = {}) { + super(tokenizer, { + skip_prompt, + callback_function, + token_callback_function, + decode_kwargs: { skip_special_tokens, ...decode_kwargs }, + }); + this.timestamp_begin = tokenizer.timestamp_begin; + + this.on_chunk_start = on_chunk_start; + this.on_chunk_end = on_chunk_end; + this.on_finalize = on_finalize; + + this.time_precision = time_precision; + + this.waiting_for_timestamp = false; + } + + /** + * @param {bigint[][]} value + */ + put(value) { + if (value.length > 1) { + throw Error('WhisperTextStreamer only supports batch size of 1'); + } + const tokens = value[0]; + + // Check if the token is a timestamp + if (tokens.length === 1) { + const offset = Number(tokens[0]) - this.timestamp_begin; + if (offset >= 0) { + const time = offset * this.time_precision; + if (this.waiting_for_timestamp) { + this.on_chunk_end?.(time); + } else { + this.on_chunk_start?.(time); + } + this.waiting_for_timestamp = !this.waiting_for_timestamp; // Toggle + value = [[]]; // Skip timestamp + } + } + return super.put(value); + } + + end() { + super.end(); + this.on_finalize?.(); + } +} + + +/***/ }), + +/***/ "./src/models.js": +/*!***********************!*\ + !*** ./src/models.js ***! + \***********************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ ASTForAudioClassification: () => (/* binding */ ASTForAudioClassification), +/* harmony export */ ASTModel: () => (/* binding */ ASTModel), +/* harmony export */ ASTPreTrainedModel: () => (/* binding */ ASTPreTrainedModel), +/* harmony export */ AlbertForMaskedLM: () => (/* binding */ AlbertForMaskedLM), +/* harmony export */ AlbertForQuestionAnswering: () => (/* binding */ AlbertForQuestionAnswering), +/* harmony export */ AlbertForSequenceClassification: () => (/* binding */ AlbertForSequenceClassification), +/* harmony export */ AlbertModel: () => (/* binding */ AlbertModel), +/* harmony export */ AlbertPreTrainedModel: () => (/* binding */ AlbertPreTrainedModel), +/* harmony export */ AutoModel: () => (/* binding */ AutoModel), +/* harmony export */ AutoModelForAudioClassification: () => (/* binding */ AutoModelForAudioClassification), +/* harmony export */ AutoModelForAudioFrameClassification: () => (/* binding */ AutoModelForAudioFrameClassification), +/* harmony export */ AutoModelForCTC: () => (/* binding */ AutoModelForCTC), +/* harmony export */ AutoModelForCausalLM: () => (/* binding */ AutoModelForCausalLM), +/* harmony export */ AutoModelForDepthEstimation: () => (/* binding */ AutoModelForDepthEstimation), +/* harmony export */ AutoModelForDocumentQuestionAnswering: () => (/* binding */ AutoModelForDocumentQuestionAnswering), +/* harmony export */ AutoModelForImageClassification: () => (/* binding */ AutoModelForImageClassification), +/* harmony export */ AutoModelForImageFeatureExtraction: () => (/* binding */ AutoModelForImageFeatureExtraction), +/* harmony export */ AutoModelForImageMatting: () => (/* binding */ AutoModelForImageMatting), +/* harmony export */ AutoModelForImageSegmentation: () => (/* binding */ AutoModelForImageSegmentation), +/* harmony export */ AutoModelForImageToImage: () => (/* binding */ AutoModelForImageToImage), +/* harmony export */ AutoModelForMaskGeneration: () => (/* binding */ AutoModelForMaskGeneration), +/* harmony export */ AutoModelForMaskedLM: () => (/* binding */ AutoModelForMaskedLM), +/* harmony export */ AutoModelForNormalEstimation: () => (/* binding */ AutoModelForNormalEstimation), +/* harmony export */ AutoModelForObjectDetection: () => (/* binding */ AutoModelForObjectDetection), +/* harmony export */ AutoModelForQuestionAnswering: () => (/* binding */ AutoModelForQuestionAnswering), +/* harmony export */ AutoModelForSemanticSegmentation: () => (/* binding */ AutoModelForSemanticSegmentation), +/* harmony export */ AutoModelForSeq2SeqLM: () => (/* binding */ AutoModelForSeq2SeqLM), +/* harmony export */ AutoModelForSequenceClassification: () => (/* binding */ AutoModelForSequenceClassification), +/* harmony export */ AutoModelForSpeechSeq2Seq: () => (/* binding */ AutoModelForSpeechSeq2Seq), +/* harmony export */ AutoModelForTextToSpectrogram: () => (/* binding */ AutoModelForTextToSpectrogram), +/* harmony export */ AutoModelForTextToWaveform: () => (/* binding */ AutoModelForTextToWaveform), +/* harmony export */ AutoModelForTokenClassification: () => (/* binding */ AutoModelForTokenClassification), +/* harmony export */ AutoModelForUniversalSegmentation: () => (/* binding */ AutoModelForUniversalSegmentation), +/* harmony export */ AutoModelForVision2Seq: () => (/* binding */ AutoModelForVision2Seq), +/* harmony export */ AutoModelForXVector: () => (/* binding */ AutoModelForXVector), +/* harmony export */ AutoModelForZeroShotObjectDetection: () => (/* binding */ AutoModelForZeroShotObjectDetection), +/* harmony export */ BartForConditionalGeneration: () => (/* binding */ BartForConditionalGeneration), +/* harmony export */ BartForSequenceClassification: () => (/* binding */ BartForSequenceClassification), +/* harmony export */ BartModel: () => (/* binding */ BartModel), +/* harmony export */ BartPretrainedModel: () => (/* binding */ BartPretrainedModel), +/* harmony export */ BaseModelOutput: () => (/* binding */ BaseModelOutput), +/* harmony export */ BeitForImageClassification: () => (/* binding */ BeitForImageClassification), +/* harmony export */ BeitModel: () => (/* binding */ BeitModel), +/* harmony export */ BeitPreTrainedModel: () => (/* binding */ BeitPreTrainedModel), +/* harmony export */ BertForMaskedLM: () => (/* binding */ BertForMaskedLM), +/* harmony export */ BertForQuestionAnswering: () => (/* binding */ BertForQuestionAnswering), +/* harmony export */ BertForSequenceClassification: () => (/* binding */ BertForSequenceClassification), +/* harmony export */ BertForTokenClassification: () => (/* binding */ BertForTokenClassification), +/* harmony export */ BertModel: () => (/* binding */ BertModel), +/* harmony export */ BertPreTrainedModel: () => (/* binding */ BertPreTrainedModel), +/* harmony export */ BlenderbotForConditionalGeneration: () => (/* binding */ BlenderbotForConditionalGeneration), +/* harmony export */ BlenderbotModel: () => (/* binding */ BlenderbotModel), +/* harmony export */ BlenderbotPreTrainedModel: () => (/* binding */ BlenderbotPreTrainedModel), +/* harmony export */ BlenderbotSmallForConditionalGeneration: () => (/* binding */ BlenderbotSmallForConditionalGeneration), +/* harmony export */ BlenderbotSmallModel: () => (/* binding */ BlenderbotSmallModel), +/* harmony export */ BlenderbotSmallPreTrainedModel: () => (/* binding */ BlenderbotSmallPreTrainedModel), +/* harmony export */ BloomForCausalLM: () => (/* binding */ BloomForCausalLM), +/* harmony export */ BloomModel: () => (/* binding */ BloomModel), +/* harmony export */ BloomPreTrainedModel: () => (/* binding */ BloomPreTrainedModel), +/* harmony export */ CLIPModel: () => (/* binding */ CLIPModel), +/* harmony export */ CLIPPreTrainedModel: () => (/* binding */ CLIPPreTrainedModel), +/* harmony export */ CLIPSegForImageSegmentation: () => (/* binding */ CLIPSegForImageSegmentation), +/* harmony export */ CLIPSegModel: () => (/* binding */ CLIPSegModel), +/* harmony export */ CLIPSegPreTrainedModel: () => (/* binding */ CLIPSegPreTrainedModel), +/* harmony export */ CLIPTextModel: () => (/* binding */ CLIPTextModel), +/* harmony export */ CLIPTextModelWithProjection: () => (/* binding */ CLIPTextModelWithProjection), +/* harmony export */ CLIPVisionModel: () => (/* binding */ CLIPVisionModel), +/* harmony export */ CLIPVisionModelWithProjection: () => (/* binding */ CLIPVisionModelWithProjection), +/* harmony export */ CamembertForMaskedLM: () => (/* binding */ CamembertForMaskedLM), +/* harmony export */ CamembertForQuestionAnswering: () => (/* binding */ CamembertForQuestionAnswering), +/* harmony export */ CamembertForSequenceClassification: () => (/* binding */ CamembertForSequenceClassification), +/* harmony export */ CamembertForTokenClassification: () => (/* binding */ CamembertForTokenClassification), +/* harmony export */ CamembertModel: () => (/* binding */ CamembertModel), +/* harmony export */ CamembertPreTrainedModel: () => (/* binding */ CamembertPreTrainedModel), +/* harmony export */ CausalLMOutput: () => (/* binding */ CausalLMOutput), +/* harmony export */ CausalLMOutputWithPast: () => (/* binding */ CausalLMOutputWithPast), +/* harmony export */ ChineseCLIPModel: () => (/* binding */ ChineseCLIPModel), +/* harmony export */ ChineseCLIPPreTrainedModel: () => (/* binding */ ChineseCLIPPreTrainedModel), +/* harmony export */ ClapAudioModelWithProjection: () => (/* binding */ ClapAudioModelWithProjection), +/* harmony export */ ClapModel: () => (/* binding */ ClapModel), +/* harmony export */ ClapPreTrainedModel: () => (/* binding */ ClapPreTrainedModel), +/* harmony export */ ClapTextModelWithProjection: () => (/* binding */ ClapTextModelWithProjection), +/* harmony export */ CodeGenForCausalLM: () => (/* binding */ CodeGenForCausalLM), +/* harmony export */ CodeGenModel: () => (/* binding */ CodeGenModel), +/* harmony export */ CodeGenPreTrainedModel: () => (/* binding */ CodeGenPreTrainedModel), +/* harmony export */ CohereForCausalLM: () => (/* binding */ CohereForCausalLM), +/* harmony export */ CohereModel: () => (/* binding */ CohereModel), +/* harmony export */ CoherePreTrainedModel: () => (/* binding */ CoherePreTrainedModel), +/* harmony export */ ConvBertForMaskedLM: () => (/* binding */ ConvBertForMaskedLM), +/* harmony export */ ConvBertForQuestionAnswering: () => (/* binding */ ConvBertForQuestionAnswering), +/* harmony export */ ConvBertForSequenceClassification: () => (/* binding */ ConvBertForSequenceClassification), +/* harmony export */ ConvBertForTokenClassification: () => (/* binding */ ConvBertForTokenClassification), +/* harmony export */ ConvBertModel: () => (/* binding */ ConvBertModel), +/* harmony export */ ConvBertPreTrainedModel: () => (/* binding */ ConvBertPreTrainedModel), +/* harmony export */ ConvNextForImageClassification: () => (/* binding */ ConvNextForImageClassification), +/* harmony export */ ConvNextModel: () => (/* binding */ ConvNextModel), +/* harmony export */ ConvNextPreTrainedModel: () => (/* binding */ ConvNextPreTrainedModel), +/* harmony export */ ConvNextV2ForImageClassification: () => (/* binding */ ConvNextV2ForImageClassification), +/* harmony export */ ConvNextV2Model: () => (/* binding */ ConvNextV2Model), +/* harmony export */ ConvNextV2PreTrainedModel: () => (/* binding */ ConvNextV2PreTrainedModel), +/* harmony export */ DPTForDepthEstimation: () => (/* binding */ DPTForDepthEstimation), +/* harmony export */ DPTModel: () => (/* binding */ DPTModel), +/* harmony export */ DPTPreTrainedModel: () => (/* binding */ DPTPreTrainedModel), +/* harmony export */ DebertaForMaskedLM: () => (/* binding */ DebertaForMaskedLM), +/* harmony export */ DebertaForQuestionAnswering: () => (/* binding */ DebertaForQuestionAnswering), +/* harmony export */ DebertaForSequenceClassification: () => (/* binding */ DebertaForSequenceClassification), +/* harmony export */ DebertaForTokenClassification: () => (/* binding */ DebertaForTokenClassification), +/* harmony export */ DebertaModel: () => (/* binding */ DebertaModel), +/* harmony export */ DebertaPreTrainedModel: () => (/* binding */ DebertaPreTrainedModel), +/* harmony export */ DebertaV2ForMaskedLM: () => (/* binding */ DebertaV2ForMaskedLM), +/* harmony export */ DebertaV2ForQuestionAnswering: () => (/* binding */ DebertaV2ForQuestionAnswering), +/* harmony export */ DebertaV2ForSequenceClassification: () => (/* binding */ DebertaV2ForSequenceClassification), +/* harmony export */ DebertaV2ForTokenClassification: () => (/* binding */ DebertaV2ForTokenClassification), +/* harmony export */ DebertaV2Model: () => (/* binding */ DebertaV2Model), +/* harmony export */ DebertaV2PreTrainedModel: () => (/* binding */ DebertaV2PreTrainedModel), +/* harmony export */ DecisionTransformerModel: () => (/* binding */ DecisionTransformerModel), +/* harmony export */ DecisionTransformerPreTrainedModel: () => (/* binding */ DecisionTransformerPreTrainedModel), +/* harmony export */ DeiTForImageClassification: () => (/* binding */ DeiTForImageClassification), +/* harmony export */ DeiTModel: () => (/* binding */ DeiTModel), +/* harmony export */ DeiTPreTrainedModel: () => (/* binding */ DeiTPreTrainedModel), +/* harmony export */ DepthAnythingForDepthEstimation: () => (/* binding */ DepthAnythingForDepthEstimation), +/* harmony export */ DepthAnythingPreTrainedModel: () => (/* binding */ DepthAnythingPreTrainedModel), +/* harmony export */ DepthProForDepthEstimation: () => (/* binding */ DepthProForDepthEstimation), +/* harmony export */ DepthProPreTrainedModel: () => (/* binding */ DepthProPreTrainedModel), +/* harmony export */ DetrForObjectDetection: () => (/* binding */ DetrForObjectDetection), +/* harmony export */ DetrForSegmentation: () => (/* binding */ DetrForSegmentation), +/* harmony export */ DetrModel: () => (/* binding */ DetrModel), +/* harmony export */ DetrObjectDetectionOutput: () => (/* binding */ DetrObjectDetectionOutput), +/* harmony export */ DetrPreTrainedModel: () => (/* binding */ DetrPreTrainedModel), +/* harmony export */ DetrSegmentationOutput: () => (/* binding */ DetrSegmentationOutput), +/* harmony export */ Dinov2ForImageClassification: () => (/* binding */ Dinov2ForImageClassification), +/* harmony export */ Dinov2Model: () => (/* binding */ Dinov2Model), +/* harmony export */ Dinov2PreTrainedModel: () => (/* binding */ Dinov2PreTrainedModel), +/* harmony export */ DistilBertForMaskedLM: () => (/* binding */ DistilBertForMaskedLM), +/* harmony export */ DistilBertForQuestionAnswering: () => (/* binding */ DistilBertForQuestionAnswering), +/* harmony export */ DistilBertForSequenceClassification: () => (/* binding */ DistilBertForSequenceClassification), +/* harmony export */ DistilBertForTokenClassification: () => (/* binding */ DistilBertForTokenClassification), +/* harmony export */ DistilBertModel: () => (/* binding */ DistilBertModel), +/* harmony export */ DistilBertPreTrainedModel: () => (/* binding */ DistilBertPreTrainedModel), +/* harmony export */ DonutSwinModel: () => (/* binding */ DonutSwinModel), +/* harmony export */ DonutSwinPreTrainedModel: () => (/* binding */ DonutSwinPreTrainedModel), +/* harmony export */ EfficientNetForImageClassification: () => (/* binding */ EfficientNetForImageClassification), +/* harmony export */ EfficientNetModel: () => (/* binding */ EfficientNetModel), +/* harmony export */ EfficientNetPreTrainedModel: () => (/* binding */ EfficientNetPreTrainedModel), +/* harmony export */ ElectraForMaskedLM: () => (/* binding */ ElectraForMaskedLM), +/* harmony export */ ElectraForQuestionAnswering: () => (/* binding */ ElectraForQuestionAnswering), +/* harmony export */ ElectraForSequenceClassification: () => (/* binding */ ElectraForSequenceClassification), +/* harmony export */ ElectraForTokenClassification: () => (/* binding */ ElectraForTokenClassification), +/* harmony export */ ElectraModel: () => (/* binding */ ElectraModel), +/* harmony export */ ElectraPreTrainedModel: () => (/* binding */ ElectraPreTrainedModel), +/* harmony export */ EsmForMaskedLM: () => (/* binding */ EsmForMaskedLM), +/* harmony export */ EsmForSequenceClassification: () => (/* binding */ EsmForSequenceClassification), +/* harmony export */ EsmForTokenClassification: () => (/* binding */ EsmForTokenClassification), +/* harmony export */ EsmModel: () => (/* binding */ EsmModel), +/* harmony export */ EsmPreTrainedModel: () => (/* binding */ EsmPreTrainedModel), +/* harmony export */ FalconForCausalLM: () => (/* binding */ FalconForCausalLM), +/* harmony export */ FalconModel: () => (/* binding */ FalconModel), +/* harmony export */ FalconPreTrainedModel: () => (/* binding */ FalconPreTrainedModel), +/* harmony export */ FastViTForImageClassification: () => (/* binding */ FastViTForImageClassification), +/* harmony export */ FastViTModel: () => (/* binding */ FastViTModel), +/* harmony export */ FastViTPreTrainedModel: () => (/* binding */ FastViTPreTrainedModel), +/* harmony export */ Florence2ForConditionalGeneration: () => (/* binding */ Florence2ForConditionalGeneration), +/* harmony export */ Florence2PreTrainedModel: () => (/* binding */ Florence2PreTrainedModel), +/* harmony export */ GLPNForDepthEstimation: () => (/* binding */ GLPNForDepthEstimation), +/* harmony export */ GLPNModel: () => (/* binding */ GLPNModel), +/* harmony export */ GLPNPreTrainedModel: () => (/* binding */ GLPNPreTrainedModel), +/* harmony export */ GPT2LMHeadModel: () => (/* binding */ GPT2LMHeadModel), +/* harmony export */ GPT2Model: () => (/* binding */ GPT2Model), +/* harmony export */ GPT2PreTrainedModel: () => (/* binding */ GPT2PreTrainedModel), +/* harmony export */ GPTBigCodeForCausalLM: () => (/* binding */ GPTBigCodeForCausalLM), +/* harmony export */ GPTBigCodeModel: () => (/* binding */ GPTBigCodeModel), +/* harmony export */ GPTBigCodePreTrainedModel: () => (/* binding */ GPTBigCodePreTrainedModel), +/* harmony export */ GPTJForCausalLM: () => (/* binding */ GPTJForCausalLM), +/* harmony export */ GPTJModel: () => (/* binding */ GPTJModel), +/* harmony export */ GPTJPreTrainedModel: () => (/* binding */ GPTJPreTrainedModel), +/* harmony export */ GPTNeoForCausalLM: () => (/* binding */ GPTNeoForCausalLM), +/* harmony export */ GPTNeoModel: () => (/* binding */ GPTNeoModel), +/* harmony export */ GPTNeoPreTrainedModel: () => (/* binding */ GPTNeoPreTrainedModel), +/* harmony export */ GPTNeoXForCausalLM: () => (/* binding */ GPTNeoXForCausalLM), +/* harmony export */ GPTNeoXModel: () => (/* binding */ GPTNeoXModel), +/* harmony export */ GPTNeoXPreTrainedModel: () => (/* binding */ GPTNeoXPreTrainedModel), +/* harmony export */ Gemma2ForCausalLM: () => (/* binding */ Gemma2ForCausalLM), +/* harmony export */ Gemma2Model: () => (/* binding */ Gemma2Model), +/* harmony export */ Gemma2PreTrainedModel: () => (/* binding */ Gemma2PreTrainedModel), +/* harmony export */ GemmaForCausalLM: () => (/* binding */ GemmaForCausalLM), +/* harmony export */ GemmaModel: () => (/* binding */ GemmaModel), +/* harmony export */ GemmaPreTrainedModel: () => (/* binding */ GemmaPreTrainedModel), +/* harmony export */ GraniteForCausalLM: () => (/* binding */ GraniteForCausalLM), +/* harmony export */ GraniteModel: () => (/* binding */ GraniteModel), +/* harmony export */ GranitePreTrainedModel: () => (/* binding */ GranitePreTrainedModel), +/* harmony export */ GroupViTModel: () => (/* binding */ GroupViTModel), +/* harmony export */ GroupViTPreTrainedModel: () => (/* binding */ GroupViTPreTrainedModel), +/* harmony export */ HieraForImageClassification: () => (/* binding */ HieraForImageClassification), +/* harmony export */ HieraModel: () => (/* binding */ HieraModel), +/* harmony export */ HieraPreTrainedModel: () => (/* binding */ HieraPreTrainedModel), +/* harmony export */ HubertForCTC: () => (/* binding */ HubertForCTC), +/* harmony export */ HubertForSequenceClassification: () => (/* binding */ HubertForSequenceClassification), +/* harmony export */ HubertModel: () => (/* binding */ HubertModel), +/* harmony export */ HubertPreTrainedModel: () => (/* binding */ HubertPreTrainedModel), +/* harmony export */ ImageMattingOutput: () => (/* binding */ ImageMattingOutput), +/* harmony export */ JAISLMHeadModel: () => (/* binding */ JAISLMHeadModel), +/* harmony export */ JAISModel: () => (/* binding */ JAISModel), +/* harmony export */ JAISPreTrainedModel: () => (/* binding */ JAISPreTrainedModel), +/* harmony export */ LlamaForCausalLM: () => (/* binding */ LlamaForCausalLM), +/* harmony export */ LlamaModel: () => (/* binding */ LlamaModel), +/* harmony export */ LlamaPreTrainedModel: () => (/* binding */ LlamaPreTrainedModel), +/* harmony export */ LlavaForConditionalGeneration: () => (/* binding */ LlavaForConditionalGeneration), +/* harmony export */ LlavaPreTrainedModel: () => (/* binding */ LlavaPreTrainedModel), +/* harmony export */ LongT5ForConditionalGeneration: () => (/* binding */ LongT5ForConditionalGeneration), +/* harmony export */ LongT5Model: () => (/* binding */ LongT5Model), +/* harmony export */ LongT5PreTrainedModel: () => (/* binding */ LongT5PreTrainedModel), +/* harmony export */ M2M100ForConditionalGeneration: () => (/* binding */ M2M100ForConditionalGeneration), +/* harmony export */ M2M100Model: () => (/* binding */ M2M100Model), +/* harmony export */ M2M100PreTrainedModel: () => (/* binding */ M2M100PreTrainedModel), +/* harmony export */ MBartForCausalLM: () => (/* binding */ MBartForCausalLM), +/* harmony export */ MBartForConditionalGeneration: () => (/* binding */ MBartForConditionalGeneration), +/* harmony export */ MBartForSequenceClassification: () => (/* binding */ MBartForSequenceClassification), +/* harmony export */ MBartModel: () => (/* binding */ MBartModel), +/* harmony export */ MBartPreTrainedModel: () => (/* binding */ MBartPreTrainedModel), +/* harmony export */ MPNetForMaskedLM: () => (/* binding */ MPNetForMaskedLM), +/* harmony export */ MPNetForQuestionAnswering: () => (/* binding */ MPNetForQuestionAnswering), +/* harmony export */ MPNetForSequenceClassification: () => (/* binding */ MPNetForSequenceClassification), +/* harmony export */ MPNetForTokenClassification: () => (/* binding */ MPNetForTokenClassification), +/* harmony export */ MPNetModel: () => (/* binding */ MPNetModel), +/* harmony export */ MPNetPreTrainedModel: () => (/* binding */ MPNetPreTrainedModel), +/* harmony export */ MT5ForConditionalGeneration: () => (/* binding */ MT5ForConditionalGeneration), +/* harmony export */ MT5Model: () => (/* binding */ MT5Model), +/* harmony export */ MT5PreTrainedModel: () => (/* binding */ MT5PreTrainedModel), +/* harmony export */ MarianMTModel: () => (/* binding */ MarianMTModel), +/* harmony export */ MarianModel: () => (/* binding */ MarianModel), +/* harmony export */ MarianPreTrainedModel: () => (/* binding */ MarianPreTrainedModel), +/* harmony export */ MaskFormerForInstanceSegmentation: () => (/* binding */ MaskFormerForInstanceSegmentation), +/* harmony export */ MaskFormerModel: () => (/* binding */ MaskFormerModel), +/* harmony export */ MaskFormerPreTrainedModel: () => (/* binding */ MaskFormerPreTrainedModel), +/* harmony export */ MaskedLMOutput: () => (/* binding */ MaskedLMOutput), +/* harmony export */ MistralForCausalLM: () => (/* binding */ MistralForCausalLM), +/* harmony export */ MistralModel: () => (/* binding */ MistralModel), +/* harmony export */ MistralPreTrainedModel: () => (/* binding */ MistralPreTrainedModel), +/* harmony export */ MobileBertForMaskedLM: () => (/* binding */ MobileBertForMaskedLM), +/* harmony export */ MobileBertForQuestionAnswering: () => (/* binding */ MobileBertForQuestionAnswering), +/* harmony export */ MobileBertForSequenceClassification: () => (/* binding */ MobileBertForSequenceClassification), +/* harmony export */ MobileBertModel: () => (/* binding */ MobileBertModel), +/* harmony export */ MobileBertPreTrainedModel: () => (/* binding */ MobileBertPreTrainedModel), +/* harmony export */ MobileLLMForCausalLM: () => (/* binding */ MobileLLMForCausalLM), +/* harmony export */ MobileLLMModel: () => (/* binding */ MobileLLMModel), +/* harmony export */ MobileLLMPreTrainedModel: () => (/* binding */ MobileLLMPreTrainedModel), +/* harmony export */ MobileNetV1ForImageClassification: () => (/* binding */ MobileNetV1ForImageClassification), +/* harmony export */ MobileNetV1Model: () => (/* binding */ MobileNetV1Model), +/* harmony export */ MobileNetV1PreTrainedModel: () => (/* binding */ MobileNetV1PreTrainedModel), +/* harmony export */ MobileNetV2ForImageClassification: () => (/* binding */ MobileNetV2ForImageClassification), +/* harmony export */ MobileNetV2Model: () => (/* binding */ MobileNetV2Model), +/* harmony export */ MobileNetV2PreTrainedModel: () => (/* binding */ MobileNetV2PreTrainedModel), +/* harmony export */ MobileNetV3ForImageClassification: () => (/* binding */ MobileNetV3ForImageClassification), +/* harmony export */ MobileNetV3Model: () => (/* binding */ MobileNetV3Model), +/* harmony export */ MobileNetV3PreTrainedModel: () => (/* binding */ MobileNetV3PreTrainedModel), +/* harmony export */ MobileNetV4ForImageClassification: () => (/* binding */ MobileNetV4ForImageClassification), +/* harmony export */ MobileNetV4Model: () => (/* binding */ MobileNetV4Model), +/* harmony export */ MobileNetV4PreTrainedModel: () => (/* binding */ MobileNetV4PreTrainedModel), +/* harmony export */ MobileViTForImageClassification: () => (/* binding */ MobileViTForImageClassification), +/* harmony export */ MobileViTModel: () => (/* binding */ MobileViTModel), +/* harmony export */ MobileViTPreTrainedModel: () => (/* binding */ MobileViTPreTrainedModel), +/* harmony export */ MobileViTV2ForImageClassification: () => (/* binding */ MobileViTV2ForImageClassification), +/* harmony export */ MobileViTV2Model: () => (/* binding */ MobileViTV2Model), +/* harmony export */ MobileViTV2PreTrainedModel: () => (/* binding */ MobileViTV2PreTrainedModel), +/* harmony export */ ModelOutput: () => (/* binding */ ModelOutput), +/* harmony export */ Moondream1ForConditionalGeneration: () => (/* binding */ Moondream1ForConditionalGeneration), +/* harmony export */ MptForCausalLM: () => (/* binding */ MptForCausalLM), +/* harmony export */ MptModel: () => (/* binding */ MptModel), +/* harmony export */ MptPreTrainedModel: () => (/* binding */ MptPreTrainedModel), +/* harmony export */ MusicgenForCausalLM: () => (/* binding */ MusicgenForCausalLM), +/* harmony export */ MusicgenForConditionalGeneration: () => (/* binding */ MusicgenForConditionalGeneration), +/* harmony export */ MusicgenModel: () => (/* binding */ MusicgenModel), +/* harmony export */ MusicgenPreTrainedModel: () => (/* binding */ MusicgenPreTrainedModel), +/* harmony export */ NomicBertModel: () => (/* binding */ NomicBertModel), +/* harmony export */ NomicBertPreTrainedModel: () => (/* binding */ NomicBertPreTrainedModel), +/* harmony export */ OPTForCausalLM: () => (/* binding */ OPTForCausalLM), +/* harmony export */ OPTModel: () => (/* binding */ OPTModel), +/* harmony export */ OPTPreTrainedModel: () => (/* binding */ OPTPreTrainedModel), +/* harmony export */ OlmoForCausalLM: () => (/* binding */ OlmoForCausalLM), +/* harmony export */ OlmoModel: () => (/* binding */ OlmoModel), +/* harmony export */ OlmoPreTrainedModel: () => (/* binding */ OlmoPreTrainedModel), +/* harmony export */ OpenELMForCausalLM: () => (/* binding */ OpenELMForCausalLM), +/* harmony export */ OpenELMModel: () => (/* binding */ OpenELMModel), +/* harmony export */ OpenELMPreTrainedModel: () => (/* binding */ OpenELMPreTrainedModel), +/* harmony export */ OwlViTForObjectDetection: () => (/* binding */ OwlViTForObjectDetection), +/* harmony export */ OwlViTModel: () => (/* binding */ OwlViTModel), +/* harmony export */ OwlViTPreTrainedModel: () => (/* binding */ OwlViTPreTrainedModel), +/* harmony export */ Owlv2ForObjectDetection: () => (/* binding */ Owlv2ForObjectDetection), +/* harmony export */ Owlv2Model: () => (/* binding */ Owlv2Model), +/* harmony export */ Owlv2PreTrainedModel: () => (/* binding */ Owlv2PreTrainedModel), +/* harmony export */ Phi3ForCausalLM: () => (/* binding */ Phi3ForCausalLM), +/* harmony export */ Phi3Model: () => (/* binding */ Phi3Model), +/* harmony export */ Phi3PreTrainedModel: () => (/* binding */ Phi3PreTrainedModel), +/* harmony export */ PhiForCausalLM: () => (/* binding */ PhiForCausalLM), +/* harmony export */ PhiModel: () => (/* binding */ PhiModel), +/* harmony export */ PhiPreTrainedModel: () => (/* binding */ PhiPreTrainedModel), +/* harmony export */ PreTrainedModel: () => (/* binding */ PreTrainedModel), +/* harmony export */ PretrainedMixin: () => (/* binding */ PretrainedMixin), +/* harmony export */ PvtForImageClassification: () => (/* binding */ PvtForImageClassification), +/* harmony export */ PvtModel: () => (/* binding */ PvtModel), +/* harmony export */ PvtPreTrainedModel: () => (/* binding */ PvtPreTrainedModel), +/* harmony export */ PyAnnoteForAudioFrameClassification: () => (/* binding */ PyAnnoteForAudioFrameClassification), +/* harmony export */ PyAnnoteModel: () => (/* binding */ PyAnnoteModel), +/* harmony export */ PyAnnotePreTrainedModel: () => (/* binding */ PyAnnotePreTrainedModel), +/* harmony export */ QuestionAnsweringModelOutput: () => (/* binding */ QuestionAnsweringModelOutput), +/* harmony export */ Qwen2ForCausalLM: () => (/* binding */ Qwen2ForCausalLM), +/* harmony export */ Qwen2Model: () => (/* binding */ Qwen2Model), +/* harmony export */ Qwen2PreTrainedModel: () => (/* binding */ Qwen2PreTrainedModel), +/* harmony export */ RTDetrForObjectDetection: () => (/* binding */ RTDetrForObjectDetection), +/* harmony export */ RTDetrModel: () => (/* binding */ RTDetrModel), +/* harmony export */ RTDetrObjectDetectionOutput: () => (/* binding */ RTDetrObjectDetectionOutput), +/* harmony export */ RTDetrPreTrainedModel: () => (/* binding */ RTDetrPreTrainedModel), +/* harmony export */ ResNetForImageClassification: () => (/* binding */ ResNetForImageClassification), +/* harmony export */ ResNetModel: () => (/* binding */ ResNetModel), +/* harmony export */ ResNetPreTrainedModel: () => (/* binding */ ResNetPreTrainedModel), +/* harmony export */ RoFormerForMaskedLM: () => (/* binding */ RoFormerForMaskedLM), +/* harmony export */ RoFormerForQuestionAnswering: () => (/* binding */ RoFormerForQuestionAnswering), +/* harmony export */ RoFormerForSequenceClassification: () => (/* binding */ RoFormerForSequenceClassification), +/* harmony export */ RoFormerForTokenClassification: () => (/* binding */ RoFormerForTokenClassification), +/* harmony export */ RoFormerModel: () => (/* binding */ RoFormerModel), +/* harmony export */ RoFormerPreTrainedModel: () => (/* binding */ RoFormerPreTrainedModel), +/* harmony export */ RobertaForMaskedLM: () => (/* binding */ RobertaForMaskedLM), +/* harmony export */ RobertaForQuestionAnswering: () => (/* binding */ RobertaForQuestionAnswering), +/* harmony export */ RobertaForSequenceClassification: () => (/* binding */ RobertaForSequenceClassification), +/* harmony export */ RobertaForTokenClassification: () => (/* binding */ RobertaForTokenClassification), +/* harmony export */ RobertaModel: () => (/* binding */ RobertaModel), +/* harmony export */ RobertaPreTrainedModel: () => (/* binding */ RobertaPreTrainedModel), +/* harmony export */ SamImageSegmentationOutput: () => (/* binding */ SamImageSegmentationOutput), +/* harmony export */ SamModel: () => (/* binding */ SamModel), +/* harmony export */ SamPreTrainedModel: () => (/* binding */ SamPreTrainedModel), +/* harmony export */ SapiensForDepthEstimation: () => (/* binding */ SapiensForDepthEstimation), +/* harmony export */ SapiensForNormalEstimation: () => (/* binding */ SapiensForNormalEstimation), +/* harmony export */ SapiensForSemanticSegmentation: () => (/* binding */ SapiensForSemanticSegmentation), +/* harmony export */ SapiensPreTrainedModel: () => (/* binding */ SapiensPreTrainedModel), +/* harmony export */ SegformerForImageClassification: () => (/* binding */ SegformerForImageClassification), +/* harmony export */ SegformerForSemanticSegmentation: () => (/* binding */ SegformerForSemanticSegmentation), +/* harmony export */ SegformerModel: () => (/* binding */ SegformerModel), +/* harmony export */ SegformerPreTrainedModel: () => (/* binding */ SegformerPreTrainedModel), +/* harmony export */ Seq2SeqLMOutput: () => (/* binding */ Seq2SeqLMOutput), +/* harmony export */ SequenceClassifierOutput: () => (/* binding */ SequenceClassifierOutput), +/* harmony export */ SiglipModel: () => (/* binding */ SiglipModel), +/* harmony export */ SiglipPreTrainedModel: () => (/* binding */ SiglipPreTrainedModel), +/* harmony export */ SiglipTextModel: () => (/* binding */ SiglipTextModel), +/* harmony export */ SiglipVisionModel: () => (/* binding */ SiglipVisionModel), +/* harmony export */ SpeechT5ForSpeechToText: () => (/* binding */ SpeechT5ForSpeechToText), +/* harmony export */ SpeechT5ForTextToSpeech: () => (/* binding */ SpeechT5ForTextToSpeech), +/* harmony export */ SpeechT5HifiGan: () => (/* binding */ SpeechT5HifiGan), +/* harmony export */ SpeechT5Model: () => (/* binding */ SpeechT5Model), +/* harmony export */ SpeechT5PreTrainedModel: () => (/* binding */ SpeechT5PreTrainedModel), +/* harmony export */ SqueezeBertForMaskedLM: () => (/* binding */ SqueezeBertForMaskedLM), +/* harmony export */ SqueezeBertForQuestionAnswering: () => (/* binding */ SqueezeBertForQuestionAnswering), +/* harmony export */ SqueezeBertForSequenceClassification: () => (/* binding */ SqueezeBertForSequenceClassification), +/* harmony export */ SqueezeBertModel: () => (/* binding */ SqueezeBertModel), +/* harmony export */ SqueezeBertPreTrainedModel: () => (/* binding */ SqueezeBertPreTrainedModel), +/* harmony export */ StableLmForCausalLM: () => (/* binding */ StableLmForCausalLM), +/* harmony export */ StableLmModel: () => (/* binding */ StableLmModel), +/* harmony export */ StableLmPreTrainedModel: () => (/* binding */ StableLmPreTrainedModel), +/* harmony export */ Starcoder2ForCausalLM: () => (/* binding */ Starcoder2ForCausalLM), +/* harmony export */ Starcoder2Model: () => (/* binding */ Starcoder2Model), +/* harmony export */ Starcoder2PreTrainedModel: () => (/* binding */ Starcoder2PreTrainedModel), +/* harmony export */ Swin2SRForImageSuperResolution: () => (/* binding */ Swin2SRForImageSuperResolution), +/* harmony export */ Swin2SRModel: () => (/* binding */ Swin2SRModel), +/* harmony export */ Swin2SRPreTrainedModel: () => (/* binding */ Swin2SRPreTrainedModel), +/* harmony export */ SwinForImageClassification: () => (/* binding */ SwinForImageClassification), +/* harmony export */ SwinModel: () => (/* binding */ SwinModel), +/* harmony export */ SwinPreTrainedModel: () => (/* binding */ SwinPreTrainedModel), +/* harmony export */ T5ForConditionalGeneration: () => (/* binding */ T5ForConditionalGeneration), +/* harmony export */ T5Model: () => (/* binding */ T5Model), +/* harmony export */ T5PreTrainedModel: () => (/* binding */ T5PreTrainedModel), +/* harmony export */ TableTransformerForObjectDetection: () => (/* binding */ TableTransformerForObjectDetection), +/* harmony export */ TableTransformerModel: () => (/* binding */ TableTransformerModel), +/* harmony export */ TableTransformerObjectDetectionOutput: () => (/* binding */ TableTransformerObjectDetectionOutput), +/* harmony export */ TableTransformerPreTrainedModel: () => (/* binding */ TableTransformerPreTrainedModel), +/* harmony export */ TokenClassifierOutput: () => (/* binding */ TokenClassifierOutput), +/* harmony export */ TrOCRForCausalLM: () => (/* binding */ TrOCRForCausalLM), +/* harmony export */ TrOCRPreTrainedModel: () => (/* binding */ TrOCRPreTrainedModel), +/* harmony export */ UniSpeechForCTC: () => (/* binding */ UniSpeechForCTC), +/* harmony export */ UniSpeechForSequenceClassification: () => (/* binding */ UniSpeechForSequenceClassification), +/* harmony export */ UniSpeechModel: () => (/* binding */ UniSpeechModel), +/* harmony export */ UniSpeechPreTrainedModel: () => (/* binding */ UniSpeechPreTrainedModel), +/* harmony export */ UniSpeechSatForAudioFrameClassification: () => (/* binding */ UniSpeechSatForAudioFrameClassification), +/* harmony export */ UniSpeechSatForCTC: () => (/* binding */ UniSpeechSatForCTC), +/* harmony export */ UniSpeechSatForSequenceClassification: () => (/* binding */ UniSpeechSatForSequenceClassification), +/* harmony export */ UniSpeechSatModel: () => (/* binding */ UniSpeechSatModel), +/* harmony export */ UniSpeechSatPreTrainedModel: () => (/* binding */ UniSpeechSatPreTrainedModel), +/* harmony export */ ViTForImageClassification: () => (/* binding */ ViTForImageClassification), +/* harmony export */ ViTMAEModel: () => (/* binding */ ViTMAEModel), +/* harmony export */ ViTMAEPreTrainedModel: () => (/* binding */ ViTMAEPreTrainedModel), +/* harmony export */ ViTMSNForImageClassification: () => (/* binding */ ViTMSNForImageClassification), +/* harmony export */ ViTMSNModel: () => (/* binding */ ViTMSNModel), +/* harmony export */ ViTMSNPreTrainedModel: () => (/* binding */ ViTMSNPreTrainedModel), +/* harmony export */ ViTModel: () => (/* binding */ ViTModel), +/* harmony export */ ViTPreTrainedModel: () => (/* binding */ ViTPreTrainedModel), +/* harmony export */ VisionEncoderDecoderModel: () => (/* binding */ VisionEncoderDecoderModel), +/* harmony export */ VitMatteForImageMatting: () => (/* binding */ VitMatteForImageMatting), +/* harmony export */ VitMattePreTrainedModel: () => (/* binding */ VitMattePreTrainedModel), +/* harmony export */ VitsModel: () => (/* binding */ VitsModel), +/* harmony export */ VitsModelOutput: () => (/* binding */ VitsModelOutput), +/* harmony export */ VitsPreTrainedModel: () => (/* binding */ VitsPreTrainedModel), +/* harmony export */ Wav2Vec2BertForCTC: () => (/* binding */ Wav2Vec2BertForCTC), +/* harmony export */ Wav2Vec2BertForSequenceClassification: () => (/* binding */ Wav2Vec2BertForSequenceClassification), +/* harmony export */ Wav2Vec2BertModel: () => (/* binding */ Wav2Vec2BertModel), +/* harmony export */ Wav2Vec2BertPreTrainedModel: () => (/* binding */ Wav2Vec2BertPreTrainedModel), +/* harmony export */ Wav2Vec2ForAudioFrameClassification: () => (/* binding */ Wav2Vec2ForAudioFrameClassification), +/* harmony export */ Wav2Vec2ForCTC: () => (/* binding */ Wav2Vec2ForCTC), +/* harmony export */ Wav2Vec2ForSequenceClassification: () => (/* binding */ Wav2Vec2ForSequenceClassification), +/* harmony export */ Wav2Vec2Model: () => (/* binding */ Wav2Vec2Model), +/* harmony export */ Wav2Vec2PreTrainedModel: () => (/* binding */ Wav2Vec2PreTrainedModel), +/* harmony export */ WavLMForAudioFrameClassification: () => (/* binding */ WavLMForAudioFrameClassification), +/* harmony export */ WavLMForCTC: () => (/* binding */ WavLMForCTC), +/* harmony export */ WavLMForSequenceClassification: () => (/* binding */ WavLMForSequenceClassification), +/* harmony export */ WavLMForXVector: () => (/* binding */ WavLMForXVector), +/* harmony export */ WavLMModel: () => (/* binding */ WavLMModel), +/* harmony export */ WavLMPreTrainedModel: () => (/* binding */ WavLMPreTrainedModel), +/* harmony export */ WeSpeakerResNetModel: () => (/* binding */ WeSpeakerResNetModel), +/* harmony export */ WeSpeakerResNetPreTrainedModel: () => (/* binding */ WeSpeakerResNetPreTrainedModel), +/* harmony export */ WhisperForConditionalGeneration: () => (/* binding */ WhisperForConditionalGeneration), +/* harmony export */ WhisperModel: () => (/* binding */ WhisperModel), +/* harmony export */ WhisperPreTrainedModel: () => (/* binding */ WhisperPreTrainedModel), +/* harmony export */ XLMForQuestionAnswering: () => (/* binding */ XLMForQuestionAnswering), +/* harmony export */ XLMForSequenceClassification: () => (/* binding */ XLMForSequenceClassification), +/* harmony export */ XLMForTokenClassification: () => (/* binding */ XLMForTokenClassification), +/* harmony export */ XLMModel: () => (/* binding */ XLMModel), +/* harmony export */ XLMPreTrainedModel: () => (/* binding */ XLMPreTrainedModel), +/* harmony export */ XLMRobertaForMaskedLM: () => (/* binding */ XLMRobertaForMaskedLM), +/* harmony export */ XLMRobertaForQuestionAnswering: () => (/* binding */ XLMRobertaForQuestionAnswering), +/* harmony export */ XLMRobertaForSequenceClassification: () => (/* binding */ XLMRobertaForSequenceClassification), +/* harmony export */ XLMRobertaForTokenClassification: () => (/* binding */ XLMRobertaForTokenClassification), +/* harmony export */ XLMRobertaModel: () => (/* binding */ XLMRobertaModel), +/* harmony export */ XLMRobertaPreTrainedModel: () => (/* binding */ XLMRobertaPreTrainedModel), +/* harmony export */ XLMWithLMHeadModel: () => (/* binding */ XLMWithLMHeadModel), +/* harmony export */ XVectorOutput: () => (/* binding */ XVectorOutput), +/* harmony export */ YolosForObjectDetection: () => (/* binding */ YolosForObjectDetection), +/* harmony export */ YolosModel: () => (/* binding */ YolosModel), +/* harmony export */ YolosObjectDetectionOutput: () => (/* binding */ YolosObjectDetectionOutput), +/* harmony export */ YolosPreTrainedModel: () => (/* binding */ YolosPreTrainedModel) +/* harmony export */ }); +/* harmony import */ var _configs_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./configs.js */ "./src/configs.js"); +/* harmony import */ var _backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./backends/onnx.js */ "./src/backends/onnx.js"); +/* harmony import */ var _utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./utils/dtypes.js */ "./src/utils/dtypes.js"); +/* harmony import */ var _utils_generic_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./utils/generic.js */ "./src/utils/generic.js"); +/* harmony import */ var _utils_core_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ./utils/core.js */ "./src/utils/core.js"); +/* harmony import */ var _utils_hub_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ./utils/hub.js */ "./src/utils/hub.js"); +/* harmony import */ var _utils_constants_js__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! ./utils/constants.js */ "./src/utils/constants.js"); +/* harmony import */ var _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__(/*! ./generation/logits_process.js */ "./src/generation/logits_process.js"); +/* harmony import */ var _generation_configuration_utils_js__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__(/*! ./generation/configuration_utils.js */ "./src/generation/configuration_utils.js"); +/* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__ = __webpack_require__(/*! ./utils/tensor.js */ "./src/utils/tensor.js"); +/* harmony import */ var _utils_maths_js__WEBPACK_IMPORTED_MODULE_10__ = __webpack_require__(/*! ./utils/maths.js */ "./src/utils/maths.js"); +/* harmony import */ var _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_11__ = __webpack_require__(/*! ./generation/stopping_criteria.js */ "./src/generation/stopping_criteria.js"); +/* harmony import */ var _generation_logits_sampler_js__WEBPACK_IMPORTED_MODULE_12__ = __webpack_require__(/*! ./generation/logits_sampler.js */ "./src/generation/logits_sampler.js"); +/* harmony import */ var _env_js__WEBPACK_IMPORTED_MODULE_13__ = __webpack_require__(/*! ./env.js */ "./src/env.js"); +/* harmony import */ var _models_whisper_generation_whisper_js__WEBPACK_IMPORTED_MODULE_14__ = __webpack_require__(/*! ./models/whisper/generation_whisper.js */ "./src/models/whisper/generation_whisper.js"); +/* harmony import */ var _models_whisper_common_whisper_js__WEBPACK_IMPORTED_MODULE_15__ = __webpack_require__(/*! ./models/whisper/common_whisper.js */ "./src/models/whisper/common_whisper.js"); + +/** + * @file Definitions of all models available in Transformers.js. + * + * **Example:** Load and run an `AutoModel`. + * + * ```javascript + * import { AutoModel, AutoTokenizer } from '@huggingface/transformers'; + * + * let tokenizer = await AutoTokenizer.from_pretrained('Xenova/bert-base-uncased'); + * let model = await AutoModel.from_pretrained('Xenova/bert-base-uncased'); + * + * let inputs = await tokenizer('I love transformers!'); + * let { logits } = await model(inputs); + * // Tensor { + * // data: Float32Array(183132) [-7.117443084716797, -7.107812881469727, -7.092104911804199, ...] + * // dims: (3) [1, 6, 30522], + * // type: "float32", + * // size: 183132, + * // } + * ``` + * + * We also provide other `AutoModel`s (listed below), which you can use in the same way as the Python library. For example: + * + * **Example:** Load and run an `AutoModelForSeq2SeqLM`. + * ```javascript + * import { AutoModelForSeq2SeqLM, AutoTokenizer } from '@huggingface/transformers'; + * + * let tokenizer = await AutoTokenizer.from_pretrained('Xenova/t5-small'); + * let model = await AutoModelForSeq2SeqLM.from_pretrained('Xenova/t5-small'); + * + * let { input_ids } = await tokenizer('translate English to German: I love transformers!'); + * let outputs = await model.generate(input_ids); + * let decoded = tokenizer.decode(outputs[0], { skip_special_tokens: true }); + * // 'Ich liebe Transformatoren!' + * ``` + * + * @module models + */ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +////////////////////////////////////////////////// +// Model types: used internally +const MODEL_TYPES = { + EncoderOnly: 0, + EncoderDecoder: 1, + Seq2Seq: 2, + Vision2Seq: 3, + DecoderOnly: 4, + MaskGeneration: 5, + ImageTextToText: 6, + Musicgen: 7, +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Helper functions + +// NOTE: These will be populated fully later +const MODEL_TYPE_MAPPING = new Map(); +const MODEL_NAME_TO_CLASS_MAPPING = new Map(); +const MODEL_CLASS_TO_NAME_MAPPING = new Map(); + + +/** + * Constructs an InferenceSession using a model file located at the specified path. + * @param {string} pretrained_model_name_or_path The path to the directory containing the model file. + * @param {string} fileName The name of the model file. + * @param {import('./utils/hub.js').PretrainedModelOptions} options Additional options for loading the model. + * @returns {Promise<{buffer: Uint8Array, session_options: Object, session_config: Object}>} A Promise that resolves to the data needed to create an InferenceSession object. + * @private + */ +async function getSession(pretrained_model_name_or_path, fileName, options) { + const custom_config = options.config?.['transformers.js_config'] ?? {}; + let device = options.device ?? custom_config.device; + if (device && typeof device !== 'string') { + if (device.hasOwnProperty(fileName)) { + device = device[fileName]; + } else { + console.warn(`device not specified for "${fileName}". Using the default device.`); + device = null; + } + } + + // If the device is not specified, we use the default (supported) execution providers. + const selectedDevice = /** @type {import("./utils/devices.js").DeviceType} */( + device ?? (_env_js__WEBPACK_IMPORTED_MODULE_13__.apis.IS_NODE_ENV ? 'cpu' : 'wasm') + ); + const executionProviders = (0,_backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.deviceToExecutionProviders)(selectedDevice); + + // If options.dtype is specified, we use it to choose the suffix for the model file. + // Otherwise, we use the default dtype for the device. + let dtype = options.dtype ?? custom_config.dtype; + if (typeof dtype !== 'string') { + if (dtype && dtype.hasOwnProperty(fileName)) { + dtype = dtype[fileName]; + } else { + dtype = _utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DEFAULT_DEVICE_DTYPE_MAPPING[selectedDevice] ?? _utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DATA_TYPES.fp32; + console.warn(`dtype not specified for "${fileName}". Using the default dtype (${dtype}) for this device (${selectedDevice}).`); + } + } + + const selectedDtype = /** @type {import("./utils/dtypes.js").DataType} */(dtype); + + if (!_utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DEFAULT_DTYPE_SUFFIX_MAPPING.hasOwnProperty(selectedDtype)) { + throw new Error(`Invalid dtype: ${selectedDtype}. Should be one of: ${Object.keys(_utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DATA_TYPES).join(', ')}`); + } else if (selectedDtype === _utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DATA_TYPES.fp16 && selectedDevice === 'webgpu' && !(await (0,_utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.isWebGpuFp16Supported)())) { + throw new Error(`The device (${selectedDevice}) does not support fp16.`); + } + + // Only valid for models with a decoder + const kv_cache_dtype = custom_config.kv_cache_dtype + ? (typeof custom_config.kv_cache_dtype === 'string' + ? custom_config.kv_cache_dtype + : custom_config.kv_cache_dtype[selectedDtype] ?? 'float32') + : undefined; + + if (kv_cache_dtype && !['float32', 'float16'].includes(kv_cache_dtype)) { + throw new Error(`Invalid kv_cache_dtype: ${kv_cache_dtype}. Should be one of: float32, float16`); + } + + const session_config = { + dtype: selectedDtype, + kv_cache_dtype, + } + + // Construct the model file name + const suffix = _utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DEFAULT_DTYPE_SUFFIX_MAPPING[selectedDtype]; + const modelFileName = `${options.subfolder ?? ''}/${fileName}${suffix}.onnx`; + + const session_options = { ...options.session_options }; + + // Overwrite `executionProviders` if not specified + session_options.executionProviders ??= executionProviders; + + // Overwrite `freeDimensionOverrides` if specified in config and not set in session options + const free_dimension_overrides = custom_config.free_dimension_overrides; + if (free_dimension_overrides) { + session_options.freeDimensionOverrides ??= free_dimension_overrides; + } else if (selectedDevice.startsWith('webnn') && !session_options.freeDimensionOverrides) { + console.warn( + 'WebNN does not currently support dynamic shapes and requires `free_dimension_overrides` to be set in config.json as a field within "transformers.js_config". ' + + 'When `free_dimension_overrides` is not set, you may experience significant performance degradation.' + ); + } + + const bufferPromise = (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_5__.getModelFile)(pretrained_model_name_or_path, modelFileName, true, options); + + // handle onnx external data files + const use_external_data_format = options.use_external_data_format ?? custom_config.use_external_data_format; + /** @type {Promise<{path: string, data: Uint8Array}>[]} */ + let externalDataPromises = []; + if (use_external_data_format && ( + use_external_data_format === true || + ( + typeof use_external_data_format === 'object' && + use_external_data_format.hasOwnProperty(fileName) && + use_external_data_format[fileName] === true + ) + )) { + if (_env_js__WEBPACK_IMPORTED_MODULE_13__.apis.IS_NODE_ENV) { + throw new Error('External data format is not yet supported in Node.js'); + } + const path = `${fileName}${suffix}.onnx_data`; + const fullPath = `${options.subfolder ?? ''}/${path}`; + externalDataPromises.push(new Promise(async (resolve, reject) => { + const data = await (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_5__.getModelFile)(pretrained_model_name_or_path, fullPath, true, options); + resolve({ path, data }) + })); + + } else if (session_options.externalData !== undefined) { + externalDataPromises = session_options.externalData.map(async (ext) => { + // if the external data is a string, fetch the file and replace the string with its content + if (typeof ext.data === "string") { + const ext_buffer = await (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_5__.getModelFile)(pretrained_model_name_or_path, ext.data, true, options); + return { ...ext, data: ext_buffer }; + } + return ext; + }); + } + + if (externalDataPromises.length > 0) { + session_options.externalData = await Promise.all(externalDataPromises); + } + + if (selectedDevice === 'webgpu') { + const shapes = (0,_configs_js__WEBPACK_IMPORTED_MODULE_0__.getKeyValueShapes)(options.config, { + prefix: 'present', + }); + if (Object.keys(shapes).length > 0 && !(0,_backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.isONNXProxy)()) { + // Only set preferredOutputLocation if shapes are present and we aren't proxying ONNX + /** @type {Record} */ + const preferredOutputLocation = {}; + for (const key in shapes) { + preferredOutputLocation[key] = 'gpu-buffer'; + } + session_options.preferredOutputLocation = preferredOutputLocation; + } + } + + const buffer = await bufferPromise; + + return { buffer, session_options, session_config }; +} + +/** + * Helper function to create multiple InferenceSession objects. + * + * @param {string} pretrained_model_name_or_path The path to the directory containing the model file. + * @param {Record} names The names of the model files to load. + * @param {import('./utils/hub.js').PretrainedModelOptions} options Additional options for loading the model. + * @returns {Promise>} A Promise that resolves to a dictionary of InferenceSession objects. + * @private + */ +async function constructSessions(pretrained_model_name_or_path, names, options) { + return Object.fromEntries(await Promise.all( + Object.keys(names).map(async (name) => { + const { buffer, session_options, session_config } = await getSession(pretrained_model_name_or_path, names[name], options); + const session = await (0,_backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.createInferenceSession)(buffer, session_options, session_config); + return [name, session]; + }) + )); +} + +/** + * Helper function to load multiple optional configuration files + * @param {string} pretrained_model_name_or_path The path to the directory containing the config file. + * @param {Record} names The names of the config files to load. + * @param {import('./utils/hub.js').PretrainedModelOptions} options Additional options for loading the configs. + * @returns {Promise>} A Promise that resolves to a dictionary of configuration objects. + * @private + */ +async function getOptionalConfigs(pretrained_model_name_or_path, names, options) { + return Object.fromEntries(await Promise.all( + Object.keys(names).map(async (name) => { + const config = await (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_5__.getModelJSON)(pretrained_model_name_or_path, names[name], false, options); + return [name, config]; + }) + )); +} + +/** + * Validate model inputs + * @param {Object} session The InferenceSession object that will be run. + * @param {Object} inputs The inputs to check. + * @returns {Record} The checked inputs. + * @throws {Error} If any inputs are missing. + * @private + */ +function validateInputs(session, inputs) { + /** + * NOTE: Create either a shallow or deep copy based on `onnx.wasm.proxy` + * @type {Record} + */ + const checkedInputs = Object.create(null); + const missingInputs = []; + for (const inputName of session.inputNames) { + const tensor = inputs[inputName]; + // Rare case where one of the model's input names corresponds to a built-in + // object name (e.g., toString), which would cause a simple (!tensor) check to fail, + // because it's not undefined but a function. + if (!(tensor instanceof _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor)) { + missingInputs.push(inputName); + continue; + } + // NOTE: When `env.wasm.proxy is true` the tensor is moved across the Worker + // boundary, transferring ownership to the worker and invalidating the tensor. + // So, in this case, we simply sacrifice a clone for it. + checkedInputs[inputName] = (0,_backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.isONNXProxy)() ? tensor.clone() : tensor; + } + if (missingInputs.length > 0) { + throw new Error( + `An error occurred during model execution: "Missing the following inputs: ${missingInputs.join(', ')}.`); + } + + const numInputsProvided = Object.keys(inputs).length; + const numInputsNeeded = session.inputNames.length; + if (numInputsProvided > numInputsNeeded) { + // No missing inputs, but too many inputs were provided. + // Warn the user and ignore the extra inputs. + let ignored = Object.keys(inputs).filter(inputName => !session.inputNames.includes(inputName)); + console.warn(`WARNING: Too many inputs were provided (${numInputsProvided} > ${numInputsNeeded}). The following inputs will be ignored: "${ignored.join(', ')}".`); + } + + return checkedInputs; +} + +/** + * Executes an InferenceSession using the specified inputs. + * NOTE: `inputs` must contain at least the input names of the model. + * - If additional inputs are passed, they will be ignored. + * - If inputs are missing, an error will be thrown. + * + * @param {Object} session The InferenceSession object to run. + * @param {Object} inputs An object that maps input names to input tensors. + * @returns {Promise} A Promise that resolves to an object that maps output names to output tensors. + * @private + */ +async function sessionRun(session, inputs) { + const checkedInputs = validateInputs(session, inputs); + try { + // pass the original ort tensor + const ortFeed = Object.fromEntries(Object.entries(checkedInputs).map(([k, v]) => [k, v.ort_tensor])); + let output = await session.run(ortFeed); + output = replaceTensors(output); + return output; + } catch (e) { + // This usually occurs when the inputs are of the wrong type. + console.error(`An error occurred during model execution: "${e}".`); + console.error('Inputs given to model:', checkedInputs); + throw e; + } +} + +/** + * Replaces ONNX Tensor objects with custom Tensor objects to support additional functions. + * @param {Object} obj The object to replace tensor objects in. + * @returns {Object} The object with tensor objects replaced by custom Tensor objects. + * @private + */ +function replaceTensors(obj) { + for (let prop in obj) { + if ((0,_backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.isONNXTensor)(obj[prop])) { + obj[prop] = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor(obj[prop]); + } else if (typeof obj[prop] === 'object') { + replaceTensors(obj[prop]); + } + } + return obj; +} + + +/** + * Converts an array or Tensor of integers to an int64 Tensor. + * @param {any[]|Tensor} items The input integers to be converted. + * @returns {Tensor} The int64 Tensor with the converted values. + * @throws {Error} If the input array is empty or the input is a batched Tensor and not all sequences have the same length. + * @private + */ +function toI64Tensor(items) { + if (items instanceof _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor) { + return items; + } + // items is an array + if (items.length === 0) { + throw Error("items must be non-empty"); + } + + if (Array.isArray(items[0])) { + // batched + if (items.some(x => x.length !== items[0].length)) { + throw Error("Unable to create tensor, you should probably activate truncation and/or padding with 'padding=True' and/or 'truncation=True' to have batched tensors with the same length.") + } + + return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor('int64', + BigInt64Array.from(items.flat().map(x => BigInt(x))), + [items.length, items[0].length] + ); + } else { + //flat + return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor('int64', + BigInt64Array.from(items.map(x => BigInt(x))), + [1, items.length] + ); + } +} + +/** + * Creates a boolean tensor with a single value. + * @param {boolean} value The value of the tensor. + * @returns {Tensor} The boolean tensor. + * @private + */ +function boolTensor(value) { + return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor('bool', [value], [1]); +} + +// JS doesn't support mixins, so we define some reused functions here, and allow "this" to be passed in +/** + * Perform forward pass on the seq2seq model (both encoder and decoder). + * @param {Object} self The seq2seq model object. + * @param {Object} model_inputs The input object for the model containing encoder and decoder inputs. + * @returns {Promise} Promise that resolves with the output of the seq2seq model. + * @private + */ +async function seq2seqForward(self, model_inputs) { + let { encoder_outputs, input_ids, decoder_input_ids, ...other_decoder_inputs } = model_inputs; + // Encode if needed + if (!encoder_outputs) { + const encoder_inputs = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.pick)(model_inputs, self.sessions['model'].inputNames); + // Encoder outputs are not given, so we must compute them. + encoder_outputs = (await encoderForward(self, encoder_inputs)).last_hidden_state; + } + + other_decoder_inputs.input_ids = decoder_input_ids; + other_decoder_inputs.encoder_hidden_states = encoder_outputs; + + if (self.sessions['decoder_model_merged'].inputNames.includes('encoder_attention_mask')) { + other_decoder_inputs.encoder_attention_mask = model_inputs.attention_mask + } + + const decoderResults = await decoderForward(self, other_decoder_inputs, true); + + return decoderResults; +} + +/** + * Forward pass of an encoder model. + * @param {Object} self The encoder model. + * @param {Object} model_inputs The input data to be used for the forward pass. + * @returns {Promise} The model's outputs. + * @private + */ +async function encoderForward(self, model_inputs) { + const session = self.sessions['model']; + const encoderFeeds = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.pick)(model_inputs, session.inputNames); + + if (session.inputNames.includes('inputs_embeds') && !encoderFeeds.inputs_embeds) { + if (!model_inputs.input_ids) { + throw new Error('Both `input_ids` and `inputs_embeds` are missing in the model inputs.'); + } + encoderFeeds.inputs_embeds = await self.encode_text({ input_ids: model_inputs.input_ids }); + } + if (session.inputNames.includes('token_type_ids') && !encoderFeeds.token_type_ids) { + // Assign default `token_type_ids` (all zeroes) to the `encoderFeeds` if the model expects it, + // but they weren't created by the tokenizer. + encoderFeeds.token_type_ids = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor( + 'int64', + new BigInt64Array(encoderFeeds.input_ids.data.length), + encoderFeeds.input_ids.dims + ) + } + return await sessionRun(session, encoderFeeds); +} + +/** + * Forward pass of a decoder model. + * @param {Object} self The decoder model. + * @param {Object} model_inputs The input data to be used for the forward pass. + * @returns {Promise} The logits and past key values. + * @private + */ +async function decoderForward(self, model_inputs, is_encoder_decoder = false) { + + const session = self.sessions[ + is_encoder_decoder ? 'decoder_model_merged' : 'model' + ] + + const { past_key_values, ...new_model_inputs } = model_inputs; + + if (session.inputNames.includes('use_cache_branch')) { + new_model_inputs.use_cache_branch = boolTensor(!!past_key_values); + } + if (session.inputNames.includes('position_ids') && new_model_inputs.attention_mask && !new_model_inputs.position_ids) { + new_model_inputs.position_ids = createPositionIds(new_model_inputs, past_key_values); + } + + // Unpack the `past_key_values` object into model inputs + self.addPastKeyValues(new_model_inputs, past_key_values); + + // Select only the inputs that are needed for the current session + const fixed = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.pick)(new_model_inputs, session.inputNames); + return await sessionRun(session, fixed); +} + + +/** + * Forward pass of an image-text-to-text model. + * @param {Object} self The image-text-to-text model model. + * @param {Object} model_inputs The input data to be used for the forward pass. + * @param {Tensor} [model_inputs.input_ids=null] + * @param {Tensor} [model_inputs.attention_mask=null] + * @param {Tensor} [model_inputs.pixel_values=null] + * @param {Tensor} [model_inputs.position_ids=null] + * @param {Tensor} [model_inputs.inputs_embeds=null] + * @param {Tensor} [model_inputs.past_key_values=null] + * @param {Object} [model_inputs.generation_config=null] + * @param {Object} [model_inputs.logits_processor=null] + * @returns {Promise} The model's output tensor + * @private + */ +async function imageTextToTextForward(self, { + // Produced by the tokenizer/processor: + input_ids = null, + attention_mask = null, + pixel_values = null, + + // Used during generation: + position_ids = null, + inputs_embeds = null, + past_key_values = null, + + // Generic generation parameters + generation_config = null, + logits_processor = null, + + // TODO: needed? + ...kwargs +}) { + + if (!inputs_embeds) { + // 1. Extract the input embeddings + inputs_embeds = await self.encode_text({ input_ids }); + + // 2. Possibly, merge text and images + if (pixel_values && input_ids.dims[1] !== 1) { + const image_features = await self.encode_image({ pixel_values }); + + ({ inputs_embeds, attention_mask } = self._merge_input_ids_with_image_features({ + image_features, + inputs_embeds, + input_ids, + attention_mask, + })); + + } else if (past_key_values && pixel_values && input_ids.dims[1] === 1) { + // This is the case when we are generating with cache + const target_length = input_ids.dims[1]; // always 1 + const past_length = Object.values(past_key_values)[0].dims.at(-2); + + attention_mask = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)([ + (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.ones)([input_ids.dims[0], past_length]), + attention_mask.slice(null, [attention_mask.dims[1] - target_length, attention_mask.dims[1]]), + ], 1); + } + } + + const outputs = await decoderForward(self, { + inputs_embeds, + past_key_values, + attention_mask, + position_ids, + generation_config, + logits_processor, + }, true); + return outputs; +} + +function createPositionIds(model_inputs, past_key_values = null) { + // If the model supports providing position_ids, we create position_ids on the fly for batch generation, + // by computing the cumulative sum of the attention mask along the sequence length dimension. + // + // Equivalent to: + // position_ids = attention_mask.long().cumsum(-1) - 1 + // position_ids.masked_fill_(attention_mask == 0, 1) + // if past_key_values: + // position_ids = position_ids[:, -input_ids.shape[1] :] + const { input_ids, inputs_embeds, attention_mask } = model_inputs; + const [bz, seq_len] = attention_mask.dims; + + const data = new BigInt64Array(attention_mask.data.length); + for (let i = 0; i < bz; ++i) { + const start = i * seq_len; + let sum = BigInt(0); + for (let j = 0; j < seq_len; ++j) { + const index = start + j; + if (attention_mask.data[index] === 0n) { + data[index] = BigInt(1); + } else { // === 1n + data[index] = sum; + sum += attention_mask.data[index]; + } + } + } + + let position_ids = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor('int64', data, attention_mask.dims); + if (past_key_values) { + const offset = -(input_ids ?? inputs_embeds).dims.at(1); + position_ids = position_ids.slice(null, [offset, null]); + } + return position_ids; +} + +function decoder_prepare_inputs_for_generation(self, input_ids, model_inputs, generation_config) { + if (model_inputs.past_key_values) { + const past_length = Object.values(model_inputs.past_key_values)[0].dims.at(-2); + const { input_ids, attention_mask } = model_inputs; + + // Keep only the unprocessed tokens: + // 1 - If the length of the attention_mask exceeds the length of input_ids, then we are in a setting where + // some of the inputs are exclusively passed as part of the cache (e.g. when passing input_embeds as + // input) + if (attention_mask && attention_mask.dims[1] > input_ids.dims[1]) { + // NOTE: not needed since we only pass the generated tokens to the next forward pass + // const offset = -(attention_mask.dims[1] - past_length); + // model_inputs.input_ids = input_ids.slice(null, [offset, null]); + } + // 2 - If the past_length is smaller than input_ids', then input_ids holds all input tokens. + // We can discard input_ids based on the past_length. + else if (past_length < input_ids.dims[1]) { + // NOTE: Required for phi models. + // See https://github.com/huggingface/transformers/issues/30809#issuecomment-2111918479 for more information. + model_inputs.input_ids = input_ids.slice(null, [past_length, null]); + } + // 3 - Otherwise (past_length >= input_ids.shape[1]), let's assume input_ids only has unprocessed tokens. + else { + if ( + // NOTE: Only used by VLMs (!= so that null matches undefined) + self.config.image_token_index != null && + // Equivalent to `self.config.image_token_index in input_ids` (== so that int matches bigint) + input_ids.data.some(x => x == self.config.image_token_index) + ) { + // TODO: Support multiple image tokens + const num_image_tokens = self.config.num_image_tokens; + if (!num_image_tokens) { + throw new Error('`num_image_tokens` is missing in the model configuration.'); + } + + const num_new_tokens = input_ids.dims[1] - (past_length - num_image_tokens); + model_inputs.input_ids = input_ids.slice(null, [-num_new_tokens, null]); + + // TODO: The attention mask should be formed from the attention mask passed in model_inputs + model_inputs.attention_mask = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.ones)([1, past_length + num_new_tokens]); + } + } + } + + return model_inputs; +} + +function encoder_decoder_prepare_inputs_for_generation(self, input_ids, model_inputs, generation_config) { + if (model_inputs.past_key_values) { + input_ids = input_ids.map(x => [x.at(-1)]); + } + + return { + ...model_inputs, + decoder_input_ids: toI64Tensor(input_ids), + }; +} + +function image_text_to_text_prepare_inputs_for_generation(self, ...args) { + if (self.config.is_encoder_decoder) { + return encoder_decoder_prepare_inputs_for_generation(self, ...args); + } else { + return decoder_prepare_inputs_for_generation(self, ...args); + } +} + +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +/** + * A base class for pre-trained models that provides the model configuration and an ONNX session. + */ +class PreTrainedModel extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_3__.Callable { + main_input_name = 'input_ids'; + forward_params = ['input_ids', 'attention_mask']; + /** + * Creates a new instance of the `PreTrainedModel` class. + * @param {import('./configs.js').PretrainedConfig} config The model configuration. + * @param {Record} sessions The inference sessions for the model. + * @param {Record} configs Additional configuration files (e.g., generation_config.json). + */ + constructor(config, sessions, configs) { + super(); + + this.config = config; + this.sessions = sessions; + this.configs = configs; + + const modelName = MODEL_CLASS_TO_NAME_MAPPING.get(this.constructor); + const modelType = MODEL_TYPE_MAPPING.get(modelName); + + this.can_generate = false; + this._forward = null; + + this._prepare_inputs_for_generation = null; + switch (modelType) { + case MODEL_TYPES.DecoderOnly: + this.can_generate = true; + this._forward = decoderForward; + this._prepare_inputs_for_generation = decoder_prepare_inputs_for_generation; + break; + case MODEL_TYPES.Seq2Seq: + case MODEL_TYPES.Vision2Seq: + case MODEL_TYPES.Musicgen: + this.can_generate = true; + + this._forward = seq2seqForward; + this._prepare_inputs_for_generation = encoder_decoder_prepare_inputs_for_generation; + break; + + case MODEL_TYPES.EncoderDecoder: + this._forward = seq2seqForward; + break; + case MODEL_TYPES.ImageTextToText: + this.can_generate = true; + this._forward = imageTextToTextForward; + this._prepare_inputs_for_generation = image_text_to_text_prepare_inputs_for_generation; + break; + + default: + // should be MODEL_TYPES.EncoderOnly + this._forward = encoderForward; + break; + } + + if (this.can_generate) { + this.forward_params.push('past_key_values'); + } + + /** @type {import('./configs.js').TransformersJSConfig} */ + this.custom_config = this.config['transformers.js_config'] ?? {}; + } + + /** + * Disposes of all the ONNX sessions that were created during inference. + * @returns {Promise} An array of promises, one for each ONNX session that is being disposed. + * @todo Use https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/FinalizationRegistry + */ + async dispose() { + const promises = []; + for (const session of Object.values(this.sessions)) { + if (session?.handler?.dispose) { + promises.push(session.handler.dispose()) + } + } + return await Promise.all(promises); + } + + /** + * Instantiate one of the model classes of the library from a pretrained model. + * + * The model class to instantiate is selected based on the `model_type` property of the config object + * (either passed as an argument or loaded from `pretrained_model_name_or_path` if possible) + * + * @param {string} pretrained_model_name_or_path The name or path of the pretrained model. Can be either: + * - A string, the *model id* of a pretrained model hosted inside a model repo on huggingface.co. + * Valid model ids can be located at the root-level, like `bert-base-uncased`, or namespaced under a + * user or organization name, like `dbmdz/bert-base-german-cased`. + * - A path to a *directory* containing model weights, e.g., `./my_model_directory/`. + * @param {import('./utils/hub.js').PretrainedModelOptions} options Additional options for loading the model. + * + * @returns {Promise} A new instance of the `PreTrainedModel` class. + */ + static async from_pretrained(pretrained_model_name_or_path, { + progress_callback = null, + config = null, + cache_dir = null, + local_files_only = false, + revision = 'main', + model_file_name = null, + subfolder = 'onnx', + device = null, + dtype = null, + use_external_data_format = null, + session_options = {}, + } = {}) { + + let options = { + progress_callback, + config, + cache_dir, + local_files_only, + revision, + model_file_name, + subfolder, + device, + dtype, + use_external_data_format, + session_options, + } + + const modelName = MODEL_CLASS_TO_NAME_MAPPING.get(this); + const modelType = MODEL_TYPE_MAPPING.get(modelName); + + config = options.config = await _configs_js__WEBPACK_IMPORTED_MODULE_0__.AutoConfig.from_pretrained(pretrained_model_name_or_path, options); + + let info; + if (modelType === MODEL_TYPES.DecoderOnly) { + info = await Promise.all([ + constructSessions(pretrained_model_name_or_path, { + model: options.model_file_name ?? 'model', + }, options), + getOptionalConfigs(pretrained_model_name_or_path, { + generation_config: 'generation_config.json', + }, options), + ]); + + } else if (modelType === MODEL_TYPES.Seq2Seq || modelType === MODEL_TYPES.Vision2Seq) { + info = await Promise.all([ + constructSessions(pretrained_model_name_or_path, { + model: 'encoder_model', + decoder_model_merged: 'decoder_model_merged', + }, options), + getOptionalConfigs(pretrained_model_name_or_path, { + generation_config: 'generation_config.json', + }, options), + ]); + + } else if (modelType === MODEL_TYPES.MaskGeneration) { + info = await Promise.all([ + constructSessions(pretrained_model_name_or_path, { + model: 'vision_encoder', + prompt_encoder_mask_decoder: 'prompt_encoder_mask_decoder', + }, options), + ]); + + } else if (modelType === MODEL_TYPES.EncoderDecoder) { + info = await Promise.all([ + constructSessions(pretrained_model_name_or_path, { + model: 'encoder_model', + decoder_model_merged: 'decoder_model_merged', + }, options), + ]); + + } else if (modelType === MODEL_TYPES.ImageTextToText) { + const sessions = { + embed_tokens: 'embed_tokens', + vision_encoder: 'vision_encoder', + decoder_model_merged: 'decoder_model_merged', + } + if (config.is_encoder_decoder) { + sessions['model'] = 'encoder_model'; + } + info = await Promise.all([ + constructSessions(pretrained_model_name_or_path, sessions, options), + getOptionalConfigs(pretrained_model_name_or_path, { + generation_config: 'generation_config.json', + }, options), + ]); + + } else if (modelType === MODEL_TYPES.Musicgen) { + info = await Promise.all([ + constructSessions(pretrained_model_name_or_path, { + model: 'text_encoder', + decoder_model_merged: 'decoder_model_merged', + encodec_decode: 'encodec_decode', + }, options), + getOptionalConfigs(pretrained_model_name_or_path, { + generation_config: 'generation_config.json', + }, options), + ]); + + } else { // should be MODEL_TYPES.EncoderOnly + if (modelType !== MODEL_TYPES.EncoderOnly) { + console.warn(`Model type for '${modelName ?? config?.model_type}' not found, assuming encoder-only architecture. Please report this at ${_utils_constants_js__WEBPACK_IMPORTED_MODULE_6__.GITHUB_ISSUE_URL}.`) + } + info = await Promise.all([ + constructSessions(pretrained_model_name_or_path, { + model: options.model_file_name ?? 'model', + }, options), + ]); + } + + // @ts-ignore + return new this(config, ...info); + } + + /** + * Runs the model with the provided inputs + * @param {Object} model_inputs Object containing input tensors + * @returns {Promise} Object containing output tensors + */ + async _call(model_inputs) { + return await this.forward(model_inputs); + } + + /** + * Forward method for a pretrained model. If not overridden by a subclass, the correct forward method + * will be chosen based on the model type. + * @param {Object} model_inputs The input data to the model in the format specified in the ONNX model. + * @returns {Promise} The output data from the model in the format specified in the ONNX model. + * @throws {Error} This method must be implemented in subclasses. + */ + async forward(model_inputs) { + return await this._forward(this, model_inputs); + } + + /** + * Get the model's generation config, if it exists. + * @returns {GenerationConfig|null} The model's generation config if it exists, otherwise `null`. + */ + get generation_config() { + return this.configs?.generation_config ?? null; + } + + /** + * This function returns a [`LogitsProcessorList`] list object that contains all relevant [`LogitsWarper`] + * instances used for multinomial sampling. + * @param {GenerationConfig} generation_config The generation config. + * @returns {LogitsProcessorList} generation_config + */ + _get_logits_warper(generation_config) { + + // instantiate warpers list + const warpers = new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.LogitsProcessorList(); + + if (generation_config.temperature !== null && generation_config.temperature !== 1.0) { + warpers.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.TemperatureLogitsWarper(generation_config.temperature)); + } + if (generation_config.top_k !== null && generation_config.top_k !== 0) { + // TODO: add min_tokens_to_keep + warpers.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.TopKLogitsWarper(generation_config.top_k)); + } + if (generation_config.top_p !== null && generation_config.top_p < 1.0) { + // TODO: add min_tokens_to_keep + warpers.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.TopPLogitsWarper(generation_config.top_p)); + } + + return warpers; + } + + /** + * @param {GenerationConfig} generation_config + * @param {number} input_ids_seq_length The starting sequence length for the input ids. + * @returns {LogitsProcessorList} + * @private + */ + _get_logits_processor( + generation_config, + input_ids_seq_length, + // encoder_input_ids, TODO + // prefix_allowed_tokens_fn, TODO + logits_processor = null + ) { + const processors = new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.LogitsProcessorList(); + + // if (generation_config.diversity_penalty !== null && generation_config.diversity_penalty > 0.0) { + // processors.push(new HammingDiversityLogitsProcessor( + // generation_config.diversity_penalty, + // generation_config.num_beams, + // generation_config.num_beam_groups + // )); + // } + + // if (generation_config.encoder_repetition_penalty !== null && generation_config.encoder_repetition_penalty !== 1.0) { + // processors.push(new EncoderRepetitionPenaltyLogitsProcessor( + // generation_config.encoder_repetition_penalty, + // encoder_input_ids + // )); + // } + + if (generation_config.repetition_penalty !== null && generation_config.repetition_penalty !== 1.0) { + processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.RepetitionPenaltyLogitsProcessor(generation_config.repetition_penalty)); + } + + if (generation_config.no_repeat_ngram_size !== null && generation_config.no_repeat_ngram_size > 0) { + processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.NoRepeatNGramLogitsProcessor(generation_config.no_repeat_ngram_size)); + } + + // if (generation_config.encoder_no_repeat_ngram_size !== null && generation_config.encoder_no_repeat_ngram_size > 0) { + // if (this.config.is_encoder_decoder) { + // processors.push(new EncoderNoRepeatNGramLogitsProcessor( + // generation_config.encoder_no_repeat_ngram_size, + // encoder_input_ids + // )); + // } else { + // throw new Error("It's impossible to use `encoder_no_repeat_ngram_size` with decoder-only architecture"); + // } + // } + + if (generation_config.bad_words_ids !== null) { + processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.NoBadWordsLogitsProcessor(generation_config.bad_words_ids, generation_config.eos_token_id)); + } + + if (generation_config.min_length !== null && generation_config.eos_token_id !== null && generation_config.min_length > 0) { + processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.MinLengthLogitsProcessor(generation_config.min_length, generation_config.eos_token_id)); + } + + if (generation_config.min_new_tokens !== null && generation_config.eos_token_id !== null && generation_config.min_new_tokens > 0) { + processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.MinNewTokensLengthLogitsProcessor( + input_ids_seq_length, + generation_config.min_new_tokens, + generation_config.eos_token_id + )); + } + + // if (prefix_allowed_tokens_fn !== null) { + // processors.push(new PrefixConstrainedLogitsProcessor( + // prefix_allowed_tokens_fn, + // generation_config.num_beams / generation_config.num_beam_groups + // )); + // } + + + if (generation_config.forced_bos_token_id !== null) { + processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.ForcedBOSTokenLogitsProcessor(generation_config.forced_bos_token_id)); + } + + if (generation_config.forced_eos_token_id !== null) { + processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.ForcedEOSTokenLogitsProcessor( + generation_config.max_length, + generation_config.forced_eos_token_id + )); + } + + // if (generation_config.remove_invalid_values === true) { + // processors.push(new InfNanRemoveLogitsProcessor()); + // } + + // if (generation_config.exponential_decay_length_penalty !== null) { + // processors.push(new ExponentialDecayLengthPenalty( + // generation_config.exponential_decay_length_penalty, + // generation_config.eos_token_id, + // input_ids_seq_length + // )); + // } + + // if (generation_config.suppress_tokens !== null) { + // processors.push(new SuppressTokensLogitsProcessor(generation_config.suppress_tokens)); + // } + + if (generation_config.begin_suppress_tokens !== null) { + const begin_index = (input_ids_seq_length > 1 || generation_config.forced_bos_token_id === null) + ? input_ids_seq_length + : input_ids_seq_length + 1; + + processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.SuppressTokensAtBeginLogitsProcessor(generation_config.begin_suppress_tokens, begin_index)); + } + + // DEPRECATED: https://github.com/huggingface/transformers/pull/29485 + // if (generation_config.forced_decoder_ids !== null) { + // processors.push(new ForceTokensLogitsProcessor(generation_config.forced_decoder_ids)); + // } + + + // 8. prepare batched CFG externally + if (generation_config.guidance_scale !== null && generation_config.guidance_scale > 1) { + processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.ClassifierFreeGuidanceLogitsProcessor(generation_config.guidance_scale)); + } + + if (logits_processor !== null) { + processors.extend(logits_processor) + } + + // `LogitNormalization` should always be the last logit processor, when present + // if (generation_config.renormalize_logits === true) { + // processors.push(new LogitNormalization()); + // } + + return processors; + } + + /** + * This function merges multiple generation configs together to form a final generation config to be used by the model for text generation. + * It first creates an empty `GenerationConfig` object, then it applies the model's own `generation_config` property to it. Finally, if a `generation_config` object was passed in the arguments, it overwrites the corresponding properties in the final config with those of the passed config object. + * @param {GenerationConfig|null} generation_config A `GenerationConfig` object containing generation parameters. + * @param {Object} kwargs Additional generation parameters to be used in place of those in the `generation_config` object. + * @returns {GenerationConfig} The final generation config object to be used by the model for text generation. + */ + _prepare_generation_config(generation_config, kwargs, cls = _generation_configuration_utils_js__WEBPACK_IMPORTED_MODULE_8__.GenerationConfig) { + // Create empty generation config (contains defaults) + // We pass `this.config` so that if `eos_token_id` or `bos_token_id` exist in the model's config, we will use them + const config = { ...this.config }; + for (const key of ["decoder", "generator", "text_config"]) { + // Special case: some models have generation attributes set in the decoder. + // Use them if still unset in the generation config. + if (key in config) { + Object.assign(config, config[key]); + } + } + + const gen_config = new cls(config); + + // Apply model's generation config, if it exists + Object.assign(gen_config, this.generation_config ?? {}); + + // Next, use any generation config specified by the user + // when calling `generate` + if (generation_config) { + Object.assign(gen_config, generation_config); + } + + // Finally, if any kwargs were passed, use them to overwrite + if (kwargs) { + Object.assign(gen_config, (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.pick)(kwargs, Object.getOwnPropertyNames(gen_config))); + } + + return gen_config; + } + + /** + * + * @param {GenerationConfig} generation_config + * @param {StoppingCriteriaList} [stopping_criteria=null] + */ + _get_stopping_criteria(generation_config, stopping_criteria = null) { + const criteria = new _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_11__.StoppingCriteriaList(); + + if (generation_config.max_length !== null) { + criteria.push(new _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_11__.MaxLengthCriteria( + generation_config.max_length, + this.config.max_position_embeddings ?? null, + )); + } + // if (generation_config.max_time !== null) { + // criteria.push(new MaxTimeCriteria(generation_config.max_time)); + // } + if (generation_config.eos_token_id !== null) { + criteria.push(new _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_11__.EosTokenCriteria(generation_config.eos_token_id)); + } + + if (stopping_criteria) { + criteria.extend(stopping_criteria); + } + return criteria; + + } + + /** + * Confirms that the model class is compatible with generation. + * If not, raises an exception that points to the right class to use. + */ + _validate_model_class() { + if (!this.can_generate) { + const generate_compatible_mappings = [ + MODEL_FOR_CAUSAL_LM_MAPPING_NAMES, + // MODEL_FOR_CAUSAL_IMAGE_MODELING_MAPPING, // TODO + MODEL_FOR_VISION_2_SEQ_MAPPING_NAMES, + MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES, + MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES, + ]; + + const modelName = MODEL_CLASS_TO_NAME_MAPPING.get(this.constructor); + + const generate_compatible_classes = new Set(); + const modelType = this.config.model_type; + for (const model_mapping of generate_compatible_mappings) { + const supported_models = model_mapping.get(modelType); + if (supported_models) { + generate_compatible_classes.add(supported_models[0]); + } + } + + let errorMessage = `The current model class (${modelName}) is not compatible with \`.generate()\`, as it doesn't have a language model head.` + if (generate_compatible_classes.size > 0) { + errorMessage += ` Please use the following class instead: ${[...generate_compatible_classes].join(', ')}`; + } + throw Error(errorMessage); + } + } + + prepare_inputs_for_generation(...args) { + return this._prepare_inputs_for_generation(this, ...args); + } + + /** + * + * @param {Object} inputs + * @param {bigint[][]} inputs.generated_input_ids + * @param {Object} inputs.outputs + * @param {Object} inputs.model_inputs + * @param {boolean} inputs.is_encoder_decoder + * @returns {Object} The updated model inputs for the next generation iteration. + */ + _update_model_kwargs_for_generation({ generated_input_ids, outputs, model_inputs, is_encoder_decoder }) { + // update past_key_values + model_inputs['past_key_values'] = this.getPastKeyValues(outputs, model_inputs.past_key_values); + + // update inputs for next run + model_inputs['input_ids'] = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor('int64', generated_input_ids.flat(), [generated_input_ids.length, 1]); + + if (!is_encoder_decoder) { + // update attention mask + model_inputs.attention_mask = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)( + [ + model_inputs.attention_mask, + (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.ones)([model_inputs.attention_mask.dims[0], 1]), + ], 1 + ); + } else if ('decoder_attention_mask' in model_inputs) { + // TODO: update decoder attention mask if the model requires it + } + + // force recreate position_ids in next iteration + model_inputs['position_ids'] = null; + + return model_inputs; + } + + /** + * This function extracts the model-specific `inputs` for generation. + * @param {Object} params + * @param {Tensor} [params.inputs=null] + * @param {number} [params.bos_token_id=null] + * @param {Record} [params.model_kwargs] + * @returns {{inputs_tensor: Tensor, model_inputs: Record, model_input_name: string}} The model-specific inputs for generation. + */ + _prepare_model_inputs({ inputs, bos_token_id, model_kwargs }) { + const model_inputs = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.pick)(model_kwargs, this.forward_params); + const input_name = this.main_input_name; + if (input_name in model_inputs) { + if (inputs) { + throw new Error( + "`inputs`: {inputs}` were passed alongside {input_name} which is not allowed. " + + "Make sure to either pass {inputs} or {input_name}=..." + ); + } + } else { + model_inputs[input_name] = inputs; + } + + const inputs_tensor = model_inputs[input_name]; + + return { inputs_tensor, model_inputs, model_input_name: input_name }; + } + + async _prepare_encoder_decoder_kwargs_for_generation({ inputs_tensor, model_inputs, model_input_name, generation_config }) { + if ( + this.sessions['model'].inputNames.includes('inputs_embeds') + && !model_inputs.inputs_embeds + && '_prepare_inputs_embeds' in this + ) { + // Encoder expects `inputs_embeds` instead of `input_ids` + const { input_ids, pixel_values, attention_mask, ...kwargs } = model_inputs; + // @ts-ignore + const prepared_inputs = await this._prepare_inputs_embeds(model_inputs); + model_inputs = { + ...kwargs, + ...(0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.pick)(prepared_inputs, ['inputs_embeds', 'attention_mask']), + }; + } + let { last_hidden_state } = await encoderForward(this, model_inputs); + + // for classifier free guidance we need to add a 'null' input to our encoder hidden states + if (generation_config.guidance_scale !== null && generation_config.guidance_scale > 1) { + + last_hidden_state = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)([ + last_hidden_state, + (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.full_like)(last_hidden_state, 0.0), + ], 0); + + if ('attention_mask' in model_inputs) { + model_inputs['attention_mask'] = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)([ + model_inputs['attention_mask'], + (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.zeros_like)(model_inputs['attention_mask']), + ], 0); + } + + } else if (model_inputs.decoder_input_ids) { + // Ensure that the encoder outputs have the same batch size as the decoder inputs, + // allowing for more efficient batched generation for single inputs + const decoder_input_ids_batch_size = toI64Tensor(model_inputs.decoder_input_ids).dims[0]; + if (decoder_input_ids_batch_size !== last_hidden_state.dims[0]) { + if (last_hidden_state.dims[0] !== 1) { + throw new Error( + `The encoder outputs have a different batch size (${last_hidden_state.dims[0]}) than the decoder inputs (${decoder_input_ids_batch_size}).` + ) + } + last_hidden_state = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)(Array.from({ length: decoder_input_ids_batch_size }, () => last_hidden_state), 0); + } + } + model_inputs['encoder_outputs'] = last_hidden_state; + + return model_inputs; + } + + /** + * Prepares `decoder_input_ids` for generation with encoder-decoder models + * @param {*} param0 + */ + _prepare_decoder_input_ids_for_generation({ batch_size, model_input_name, model_kwargs, decoder_start_token_id, bos_token_id, generation_config }) { + let { decoder_input_ids, ...model_inputs } = model_kwargs; + + // Prepare input ids if the user has not defined `decoder_input_ids` manually. + if (!(decoder_input_ids instanceof _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor)) { + if (!decoder_input_ids) { + decoder_start_token_id ??= bos_token_id; + + if (this.config.model_type === 'musicgen') { + // Custom logic (TODO: move to Musicgen class) + decoder_input_ids = Array.from({ + length: batch_size * this.config.decoder.num_codebooks + }, () => [decoder_start_token_id]); + + } else if (Array.isArray(decoder_start_token_id)) { + if (decoder_start_token_id.length !== batch_size) { + throw new Error( + `\`decoder_start_token_id\` expcted to have length ${batch_size} but got ${decoder_start_token_id.length}` + ) + } + decoder_input_ids = decoder_start_token_id; + } else { + decoder_input_ids = Array.from({ + length: batch_size, + }, () => [decoder_start_token_id]); + } + } else if (!Array.isArray(decoder_input_ids[0])) { + // Correct batch size + decoder_input_ids = Array.from({ + length: batch_size, + }, () => decoder_input_ids); + } + decoder_input_ids = toI64Tensor(decoder_input_ids); + } + + model_kwargs['decoder_attention_mask'] = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.ones_like)(decoder_input_ids); + + return { input_ids: decoder_input_ids, model_inputs }; + } + + /** + * Generates sequences of token ids for models with a language modeling head. + * @param {import('./generation/parameters.js').GenerationFunctionParameters} options + * @returns {Promise} The output of the model, which can contain the generated token ids, attentions, and scores. + */ + async generate({ + inputs = null, + generation_config = null, + logits_processor = null, + stopping_criteria = null, + streamer = null, + + // inputs_attention_mask = null, + ...kwargs + }) { + this._validate_model_class(); + + // Update generation config with defaults and kwargs + generation_config = this._prepare_generation_config(generation_config, kwargs); + + // 3. Define model inputs + let { inputs_tensor, model_inputs, model_input_name } = this._prepare_model_inputs({ + inputs, + model_kwargs: kwargs, + }); + + const is_encoder_decoder = this.config.is_encoder_decoder; + + // 4. Define other model kwargs + if (!is_encoder_decoder) { + // decoder-only models should use left-padding for generation + } else if (!('encoder_outputs' in model_inputs)) { + // if model is encoder decoder encoder_outputs are created + // and added to `model_kwargs` + model_inputs = await this._prepare_encoder_decoder_kwargs_for_generation( + { inputs_tensor, model_inputs, model_input_name, generation_config } + ) + } + + // 5. Prepare `input_ids` which will be used for auto-regressive generation + // TODO: Update to align with HF transformers' implementation + let input_ids; + if (is_encoder_decoder) { + // Generating from the encoder outputs + ({ input_ids, model_inputs } = this._prepare_decoder_input_ids_for_generation({ + batch_size: model_inputs[model_input_name].dims.at(0), + model_input_name, + model_kwargs: model_inputs, + decoder_start_token_id: generation_config.decoder_start_token_id, + bos_token_id: generation_config.bos_token_id, + generation_config, + })); + } else { + input_ids = model_inputs[model_input_name] + } + + // 6. Prepare `max_length` depending on other stopping criteria. + let input_ids_length = input_ids.dims.at(-1); + + if (generation_config.max_new_tokens !== null) { + generation_config.max_length = input_ids_length + generation_config.max_new_tokens; + } + + // input_ids_length = model_inputs[model_input_name].dims.at(1); + // // inputs instanceof Tensor ? : inputs.length; + + // // decoder-only + // if (input_ids_length === 0) { + // throw Error("Must supply a non-empty array of input token ids.") + // } + + // let decoder_input_ids = + // generation_config.decoder_input_ids + // ?? generation_config.decoder_start_token_id + // ?? generation_config.bos_token_id + // ?? generation_config.eos_token_id; + + // Update logits processor + // 8. prepare distribution pre_processing samplers + const prepared_logits_processor = this._get_logits_processor( + generation_config, + input_ids_length, + logits_processor, + ) + + // 9. prepare stopping criteria + const prepared_stopping_criteria = this._get_stopping_criteria( + generation_config, stopping_criteria + ) + + // /** @type {number[]} */ + // let eos_token_ids = generation_config.eos_token_id; + // if (eos_token_ids !== null && !Array.isArray(eos_token_ids)) { + // eos_token_ids = [eos_token_ids]; + // } + + const numInputs = model_inputs[model_input_name].dims.at(0); + + // TODO: + // done is a list of booleans to keep track of which inputs are done + // const done = new Array(numInputs).fill(false); + // For efficiency purposes, we remove completed rows from model_inputs + // when the beam is complete, and we keep track of the row index + // const rowIndexToBatchIndex = new Map(); + + const sampler = _generation_logits_sampler_js__WEBPACK_IMPORTED_MODULE_12__.LogitsSampler.getSampler(generation_config); + + // TODO make > numInputs + const scores = new Array(numInputs).fill(0); + /** @type {bigint[][]} */ + const all_input_ids = input_ids.tolist(); + if (streamer) { + streamer.put(all_input_ids); + } + // const all_generated_input_ids = Array.from({ length: numInputs }, () => []); + + // NOTE: For now, we don't support spawning new beams + // TODO: when we do, we simply copy past key values and accumulate into single large tensor + + //////////////////////////////////////////////////// + // Generic search which handles 4 generation modes: + // - GenerationMode.GREEDY_SEARCH + // - GenerationMode.SAMPLE + // - GenerationMode.BEAM_SEARCH + // - GenerationMode.BEAM_SAMPLE + //////////////////////////////////////////////////// + let outputs; + let attentions = {}; + while (true) { + // prepare model inputs + model_inputs = this.prepare_inputs_for_generation(all_input_ids, model_inputs, generation_config); + outputs = await this.forward(model_inputs); + + if (generation_config.output_attentions && generation_config.return_dict_in_generate) { + // Get attentions if they are present + const token_attentions = this.getAttentions(outputs); + for (const key in token_attentions) { + if (!(key in attentions)) { + attentions[key] = []; + } + attentions[key].push(token_attentions[key]); + } + } + + // Logits are of the form [batch_size, out_seq_length, vocab_size] + // In most cases, this will be [batch_size, 1, vocab_size] + // So, we select the last token's logits: + // (equivalent to `logits = outputs.logits[:, -1, :]`) + const logits = outputs.logits.slice(null, -1, null); + + const next_tokens_scores = prepared_logits_processor(all_input_ids, logits); + + /** @type {[bigint][]} */ + const generated_input_ids = []; + // const new_kv_cache = [];// NOTE: Only used for beam search when concatenating new kv + // Loop over each batch + for (let batch_idx = 0; batch_idx < next_tokens_scores.dims.at(0); ++batch_idx) { + const logs = next_tokens_scores[batch_idx]; + + const sampledTokens = await sampler(logs); + for (const [newTokenId, logProb] of sampledTokens) { + const bigint = BigInt(newTokenId); + // TODO: If branching, use previous beam as a starting point + // update generated ids, model inputs, and length for next step + scores[batch_idx] += logProb; + all_input_ids[batch_idx].push(bigint); + generated_input_ids.push([bigint]); + + // TODO: Support beam search + break; + } + } + if (streamer) { + streamer.put(generated_input_ids); + } + + const stop = prepared_stopping_criteria(all_input_ids); + if (stop.every(x => x)) { + break; + } + + model_inputs = this._update_model_kwargs_for_generation({ + generated_input_ids, outputs, model_inputs, is_encoder_decoder, + }); + } + + if (streamer) { + streamer.end(); + } + + // Retrieve and dispose all final past key values (including encoder attentions) + const past_key_values = this.getPastKeyValues(outputs, model_inputs.past_key_values, true); + + // TODO: ensure all_input_ids is padded correctly... + const sequences = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor('int64', all_input_ids.flat(), [all_input_ids.length, all_input_ids[0].length]); + + if (generation_config.return_dict_in_generate) { + return { + sequences, + past_key_values, + ...attentions, + // TODO: + // scores, + // logits, + } + } else { + // Dispose all remaining tensors + for (const tensor of Object.values(outputs)) { + if (tensor.location === 'gpu-buffer') { + tensor.dispose(); + } + } + return sequences; + } + } + + /** + * Returns an object containing past key values from the given decoder results object. + * + * @param {Object} decoderResults The decoder results object. + * @param {Object} pastKeyValues The previous past key values. + * @returns {Object} An object containing past key values. + */ + getPastKeyValues(decoderResults, pastKeyValues, disposeEncoderPKVs = false) { + const pkvs = Object.create(null); + + for (const name in decoderResults) { + if (name.startsWith('present')) { + const newName = name.replace('present', 'past_key_values'); + const is_encoder_pkv = name.includes('encoder'); + if (is_encoder_pkv && pastKeyValues) { + // Optimization introduced by optimum to reuse past key values. + // So, we just replace the constant outputs (`decoderResults[name]`) with the previous past key values. + // https://github.com/huggingface/optimum/blob/0bf2c05fb7e1182b52d21b703cfc95fd9e4ea3dc/optimum/onnxruntime/base.py#L677-L704 + pkvs[newName] = pastKeyValues[newName]; + } else { // decoder or using first encoder PKVs + pkvs[newName] = decoderResults[name]; + } + + if (pastKeyValues && (!is_encoder_pkv || disposeEncoderPKVs)) { + // - Always dispose decoder PKVs + // - Only dispose encoder past key values when requested (after generation) + const t = pastKeyValues[newName]; + if (t.location === 'gpu-buffer') { + t.dispose(); + } + } + } + } + return pkvs; + } + + /** + * Returns an object containing attentions from the given model output object. + * + * @param {Object} model_output The output of the model. + * @returns {{cross_attentions?: Tensor[]}} An object containing attentions. + */ + getAttentions(model_output) { + const attentions = {}; + + for (const attnName of ['cross_attentions', 'encoder_attentions', 'decoder_attentions']) { + for (const name in model_output) { + if (name.startsWith(attnName)) { + if (!(attnName in attentions)) { + attentions[attnName] = []; + } + attentions[attnName].push(model_output[name]); + } + } + } + return attentions; + } + + /** + * Adds past key values to the decoder feeds object. If pastKeyValues is null, creates new tensors for past key values. + * + * @param {Object} decoderFeeds The decoder feeds object to add past key values to. + * @param {Object} pastKeyValues An object containing past key values. + */ + addPastKeyValues(decoderFeeds, pastKeyValues) { + if (pastKeyValues) { + Object.assign(decoderFeeds, pastKeyValues) + } else { + const session = this.sessions['decoder_model_merged'] ?? this.sessions['model']; + const dtype = session?.config?.kv_cache_dtype ?? 'float32'; + const empty = (dtype === 'float16') ? new Uint16Array() : []; + + const shapes = (0,_configs_js__WEBPACK_IMPORTED_MODULE_0__.getKeyValueShapes)(this.config); + + for (const name in shapes) { + decoderFeeds[name] = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor(dtype, empty, shapes[name]); + } + } + } + + async encode_image({ pixel_values }) { + // image_inputs === { pixel_values } + const features = (await sessionRun(this.sessions['vision_encoder'], { pixel_values })).image_features; + if (!this.config.num_image_tokens) { + console.warn( + 'The number of image tokens was not set in the model configuration. ' + + `Setting it to the number of features detected by the vision encoder (${features.dims[1]}).` + ) + this.config.num_image_tokens = features.dims[1]; + } + return features; + } + + async encode_text({ input_ids }) { + // text_inputs === { input_ids, attention_mask } + return (await sessionRun(this.sessions['embed_tokens'], { input_ids })).inputs_embeds; + } +} + +////////////////////////////////////////////////// +// Base model output class +class ModelOutput { } + +/** + * Base class for model's outputs, with potential hidden states and attentions. + */ +class BaseModelOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.last_hidden_state Sequence of hidden-states at the output of the last layer of the model. + * @param {Tensor} [output.hidden_states] Hidden-states of the model at the output of each layer plus the optional initial embedding outputs. + * @param {Tensor} [output.attentions] Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads. + */ + constructor({ last_hidden_state, hidden_states = null, attentions = null }) { + super(); + this.last_hidden_state = last_hidden_state; + this.hidden_states = hidden_states; + this.attentions = attentions; + } +} +////////////////////////////////////////////////// +// Bert models +class BertPreTrainedModel extends PreTrainedModel { } +class BertModel extends BertPreTrainedModel { } + +/** + * BertForMaskedLM is a class representing a BERT model for masked language modeling. + */ +class BertForMaskedLM extends BertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for masked language modeling. + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} + +/** + * BertForSequenceClassification is a class representing a BERT model for sequence classification. + */ +class BertForSequenceClassification extends BertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * BertForTokenClassification is a class representing a BERT model for token classification. + */ +class BertForTokenClassification extends BertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * BertForQuestionAnswering is a class representing a BERT model for question answering. + */ +class BertForQuestionAnswering extends BertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for question answering. + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// NomicBert models +class NomicBertPreTrainedModel extends PreTrainedModel { } +class NomicBertModel extends NomicBertPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// RoFormer models +class RoFormerPreTrainedModel extends PreTrainedModel { } + +/** + * The bare RoFormer Model transformer outputting raw hidden-states without any specific head on top. + */ +class RoFormerModel extends RoFormerPreTrainedModel { } + +/** + * RoFormer Model with a `language modeling` head on top. + */ +class RoFormerForMaskedLM extends RoFormerPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for masked language modeling. + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} + +/** + * RoFormer Model transformer with a sequence classification/regression head on top (a linear layer on top of the pooled output) + */ +class RoFormerForSequenceClassification extends RoFormerPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * RoFormer Model with a token classification head on top (a linear layer on top of the hidden-states output) + * e.g. for Named-Entity-Recognition (NER) tasks. + */ +class RoFormerForTokenClassification extends RoFormerPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * RoFormer Model with a span classification head on top for extractive question-answering tasks like SQuAD + * (a linear layers on top of the hidden-states output to compute `span start logits` and `span end logits`). + */ +class RoFormerForQuestionAnswering extends RoFormerPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for question answering. + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +// TODO: Add RoFormerForCausalLM and RoFormerForMultipleChoice +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// ConvBert models +class ConvBertPreTrainedModel extends PreTrainedModel { } + +/** + * The bare ConvBERT Model transformer outputting raw hidden-states without any specific head on top. + */ +class ConvBertModel extends ConvBertPreTrainedModel { } + +/** + * ConvBERT Model with a language modeling head on top. + */ +class ConvBertForMaskedLM extends ConvBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for masked language modeling. + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} + +/** + * ConvBERT Model transformer with a sequence classification/regression head on top (a linear layer on top of the pooled output) + */ +class ConvBertForSequenceClassification extends ConvBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * ConvBERT Model with a token classification head on top (a linear layer on top of the hidden-states output) + * e.g. for Named-Entity-Recognition (NER) tasks. + */ +class ConvBertForTokenClassification extends ConvBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * ConvBERT Model with a span classification head on top for extractive question-answering tasks like SQuAD + * (a linear layers on top of the hidden-states output to compute `span start logits` and `span end logits`) + */ +class ConvBertForQuestionAnswering extends ConvBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for question answering. + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Electra models +class ElectraPreTrainedModel extends PreTrainedModel { } + +/** + * The bare Electra Model transformer outputting raw hidden-states without any specific head on top. + * Identical to the BERT model except that it uses an additional linear layer between the embedding + * layer and the encoder if the hidden size and embedding size are different. + */ +class ElectraModel extends ElectraPreTrainedModel { } +// TODO add ElectraForPreTraining +/** + * Electra model with a language modeling head on top. + */ +class ElectraForMaskedLM extends ElectraPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for masked language modeling. + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} + +/** + * ELECTRA Model transformer with a sequence classification/regression head on top (a linear layer on top of the pooled output) + */ +class ElectraForSequenceClassification extends ElectraPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * Electra model with a token classification head on top. + */ +class ElectraForTokenClassification extends ElectraPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * LECTRA Model with a span classification head on top for extractive question-answering tasks like SQuAD + * (a linear layers on top of the hidden-states output to compute `span start logits` and `span end logits`). + */ +class ElectraForQuestionAnswering extends ElectraPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for question answering. + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// CamemBERT models +class CamembertPreTrainedModel extends PreTrainedModel { } + +/** + * The bare CamemBERT Model transformer outputting raw hidden-states without any specific head on top. + */ +class CamembertModel extends CamembertPreTrainedModel { } + +/** + * CamemBERT Model with a `language modeling` head on top. + */ +class CamembertForMaskedLM extends CamembertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for masked language modeling. + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} + +/** + * CamemBERT Model transformer with a sequence classification/regression head on top (a linear layer on top of the pooled output) e.g. for GLUE tasks. + */ +class CamembertForSequenceClassification extends CamembertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * CamemBERT Model with a token classification head on top (a linear layer on top of the hidden-states output) e.g. for Named-Entity-Recognition (NER) tasks. + */ +class CamembertForTokenClassification extends CamembertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * CamemBERT Model with a span classification head on top for extractive question-answering tasks + */ +class CamembertForQuestionAnswering extends CamembertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for question answering. + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// DeBERTa models +class DebertaPreTrainedModel extends PreTrainedModel { } + +/** + * The bare DeBERTa Model transformer outputting raw hidden-states without any specific head on top. + */ +class DebertaModel extends DebertaPreTrainedModel { } + +/** + * DeBERTa Model with a `language modeling` head on top. + */ +class DebertaForMaskedLM extends DebertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for masked language modeling. + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} + +/** + * DeBERTa Model transformer with a sequence classification/regression head on top (a linear layer on top of the pooled output) + */ +class DebertaForSequenceClassification extends DebertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * DeBERTa Model with a token classification head on top (a linear layer on top of the hidden-states output) e.g. for Named-Entity-Recognition (NER) tasks. + */ +class DebertaForTokenClassification extends DebertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * DeBERTa Model with a span classification head on top for extractive question-answering tasks like SQuAD (a linear + * layers on top of the hidden-states output to compute `span start logits` and `span end logits`). + */ +class DebertaForQuestionAnswering extends DebertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for question answering. + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// DeBERTa-v2 models +class DebertaV2PreTrainedModel extends PreTrainedModel { } + +/** + * The bare DeBERTa-V2 Model transformer outputting raw hidden-states without any specific head on top. + */ +class DebertaV2Model extends DebertaV2PreTrainedModel { } + +/** + * DeBERTa-V2 Model with a `language modeling` head on top. + */ +class DebertaV2ForMaskedLM extends DebertaV2PreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for masked language modeling. + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} + +/** + * DeBERTa-V2 Model transformer with a sequence classification/regression head on top (a linear layer on top of the pooled output) + */ +class DebertaV2ForSequenceClassification extends DebertaV2PreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * DeBERTa-V2 Model with a token classification head on top (a linear layer on top of the hidden-states output) e.g. for Named-Entity-Recognition (NER) tasks. + */ +class DebertaV2ForTokenClassification extends DebertaV2PreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * DeBERTa-V2 Model with a span classification head on top for extractive question-answering tasks like SQuAD (a linear + * layers on top of the hidden-states output to compute `span start logits` and `span end logits`). + */ +class DebertaV2ForQuestionAnswering extends DebertaV2PreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for question answering. + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// DistilBert models +class DistilBertPreTrainedModel extends PreTrainedModel { } +class DistilBertModel extends DistilBertPreTrainedModel { } + +/** + * DistilBertForSequenceClassification is a class representing a DistilBERT model for sequence classification. + */ +class DistilBertForSequenceClassification extends DistilBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * DistilBertForTokenClassification is a class representing a DistilBERT model for token classification. + */ +class DistilBertForTokenClassification extends DistilBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} + + +/** + * DistilBertForQuestionAnswering is a class representing a DistilBERT model for question answering. + */ +class DistilBertForQuestionAnswering extends DistilBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for question answering. + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} + +/** + * DistilBertForMaskedLM is a class representing a DistilBERT model for masking task. + */ +class DistilBertForMaskedLM extends DistilBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// ESM models +class EsmPreTrainedModel extends PreTrainedModel { } + +/** + * The bare ESM Model transformer outputting raw hidden-states without any specific head on top. + */ +class EsmModel extends EsmPreTrainedModel { } + +/** + * ESM Model with a `language modeling` head on top. + */ +class EsmForMaskedLM extends EsmPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for masked language modeling. + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} + +/** + * ESM Model transformer with a sequence classification/regression head on top (a linear layer on top of the pooled output) + */ +class EsmForSequenceClassification extends EsmPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * ESM Model with a token classification head on top (a linear layer on top of the hidden-states output) + * e.g. for Named-Entity-Recognition (NER) tasks. + */ +class EsmForTokenClassification extends EsmPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// MobileBert models +class MobileBertPreTrainedModel extends PreTrainedModel { } +class MobileBertModel extends MobileBertPreTrainedModel { } + +/** + * MobileBertForMaskedLM is a class representing a MobileBERT model for masking task. + */ +class MobileBertForMaskedLM extends MobileBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} + +/** + * MobileBert Model transformer with a sequence classification/regression head on top (a linear layer on top of the pooled output) + */ +class MobileBertForSequenceClassification extends MobileBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * MobileBert Model with a span classification head on top for extractive question-answering tasks + */ +class MobileBertForQuestionAnswering extends MobileBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// MPNet models +class MPNetPreTrainedModel extends PreTrainedModel { } + +/** + * The bare MPNet Model transformer outputting raw hidden-states without any specific head on top. + */ +class MPNetModel extends MPNetPreTrainedModel { } + +/** + * MPNetForMaskedLM is a class representing a MPNet model for masked language modeling. + */ +class MPNetForMaskedLM extends MPNetPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for masked language modeling. + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} + +/** + * MPNetForSequenceClassification is a class representing a MPNet model for sequence classification. + */ +class MPNetForSequenceClassification extends MPNetPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * MPNetForTokenClassification is a class representing a MPNet model for token classification. + */ +class MPNetForTokenClassification extends MPNetPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * MPNetForQuestionAnswering is a class representing a MPNet model for question answering. + */ +class MPNetForQuestionAnswering extends MPNetPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for question answering. + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// SqueezeBert models +class SqueezeBertPreTrainedModel extends PreTrainedModel { } +class SqueezeBertModel extends SqueezeBertPreTrainedModel { } +class SqueezeBertForMaskedLM extends SqueezeBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} +class SqueezeBertForSequenceClassification extends SqueezeBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +class SqueezeBertForQuestionAnswering extends SqueezeBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Albert models +class AlbertPreTrainedModel extends PreTrainedModel { } +class AlbertModel extends AlbertPreTrainedModel { } +class AlbertForSequenceClassification extends AlbertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +class AlbertForQuestionAnswering extends AlbertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +class AlbertForMaskedLM extends AlbertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// T5 models +class T5PreTrainedModel extends PreTrainedModel { + forward_params = [ + 'input_ids', + 'attention_mask', + 'encoder_outputs', + 'decoder_input_ids', + 'decoder_attention_mask', + 'past_key_values', + ]; +}; + +class T5Model extends T5PreTrainedModel { } + +/** + * T5Model is a class representing a T5 model for conditional generation. + */ +class T5ForConditionalGeneration extends T5PreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// LONGT5 models +/** + * An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained models. + */ +class LongT5PreTrainedModel extends PreTrainedModel { }; + +/** + * The bare LONGT5 Model transformer outputting raw hidden-states without any specific head on top. + */ +class LongT5Model extends LongT5PreTrainedModel { } + +/** + * LONGT5 Model with a `language modeling` head on top. + */ +class LongT5ForConditionalGeneration extends LongT5PreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// MT5 models +class MT5PreTrainedModel extends PreTrainedModel { }; + +class MT5Model extends MT5PreTrainedModel { } + +/** + * A class representing a conditional sequence-to-sequence model based on the MT5 architecture. + */ +class MT5ForConditionalGeneration extends MT5PreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Bart models +class BartPretrainedModel extends PreTrainedModel { }; + +/** + * The bare BART Model outputting raw hidden-states without any specific head on top. + */ +class BartModel extends BartPretrainedModel { } + +/** + * The BART Model with a language modeling head. Can be used for summarization. + */ +class BartForConditionalGeneration extends BartPretrainedModel { } + +/** + * Bart model with a sequence classification/head on top (a linear layer on top of the pooled output) + */ +class BartForSequenceClassification extends BartPretrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// MBart models +class MBartPreTrainedModel extends PreTrainedModel { }; + +/** + * The bare MBART Model outputting raw hidden-states without any specific head on top. + */ +class MBartModel extends MBartPreTrainedModel { } + +/** + * The MBART Model with a language modeling head. Can be used for summarization, after fine-tuning the pretrained models. + */ +class MBartForConditionalGeneration extends MBartPreTrainedModel { } + +/** + * MBart model with a sequence classification/head on top (a linear layer on top of the pooled output). + */ +class MBartForSequenceClassification extends MBartPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + + +class MBartForCausalLM extends MBartPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Blenderbot models +class BlenderbotPreTrainedModel extends PreTrainedModel { }; + +/** + * The bare Blenderbot Model outputting raw hidden-states without any specific head on top. + */ +class BlenderbotModel extends BlenderbotPreTrainedModel { } + +/** + * The Blenderbot Model with a language modeling head. Can be used for summarization. + */ +class BlenderbotForConditionalGeneration extends BlenderbotPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Blenderbot models +class BlenderbotSmallPreTrainedModel extends PreTrainedModel { }; + +/** + * The bare BlenderbotSmall Model outputting raw hidden-states without any specific head on top. + */ +class BlenderbotSmallModel extends BlenderbotSmallPreTrainedModel { } + +/** + * The BlenderbotSmall Model with a language modeling head. Can be used for summarization. + */ +class BlenderbotSmallForConditionalGeneration extends BlenderbotSmallPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Roberta models +class RobertaPreTrainedModel extends PreTrainedModel { } +class RobertaModel extends RobertaPreTrainedModel { } + +/** + * RobertaForMaskedLM class for performing masked language modeling on Roberta models. + */ +class RobertaForMaskedLM extends RobertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} + +/** + * RobertaForSequenceClassification class for performing sequence classification on Roberta models. + */ +class RobertaForSequenceClassification extends RobertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * RobertaForTokenClassification class for performing token classification on Roberta models. + */ +class RobertaForTokenClassification extends RobertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * RobertaForQuestionAnswering class for performing question answering on Roberta models. + */ +class RobertaForQuestionAnswering extends RobertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// XLM models +/** + * An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained models. + */ +class XLMPreTrainedModel extends PreTrainedModel { } + +/** + * The bare XLM Model transformer outputting raw hidden-states without any specific head on top. + */ +class XLMModel extends XLMPreTrainedModel { } + +/** + * The XLM Model transformer with a language modeling head on top (linear layer with weights tied to the input embeddings). + */ +class XLMWithLMHeadModel extends XLMPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} + +/** + * XLM Model with a sequence classification/regression head on top (a linear layer on top of the pooled output) + */ +class XLMForSequenceClassification extends XLMPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * XLM Model with a token classification head on top (a linear layer on top of the hidden-states output) + */ +class XLMForTokenClassification extends XLMPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * XLM Model with a span classification head on top for extractive question-answering tasks + */ +class XLMForQuestionAnswering extends XLMPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// XLMRoberta models +class XLMRobertaPreTrainedModel extends PreTrainedModel { } +class XLMRobertaModel extends XLMRobertaPreTrainedModel { } + +/** + * XLMRobertaForMaskedLM class for performing masked language modeling on XLMRoberta models. + */ +class XLMRobertaForMaskedLM extends XLMRobertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} + +/** + * XLMRobertaForSequenceClassification class for performing sequence classification on XLMRoberta models. + */ +class XLMRobertaForSequenceClassification extends XLMRobertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * XLMRobertaForTokenClassification class for performing token classification on XLMRoberta models. + */ +class XLMRobertaForTokenClassification extends XLMRobertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * XLMRobertaForQuestionAnswering class for performing question answering on XLMRoberta models. + */ +class XLMRobertaForQuestionAnswering extends XLMRobertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Audio Spectrogram Transformer (AST) models +class ASTPreTrainedModel extends PreTrainedModel { }; + +/** + * The bare AST Model transformer outputting raw hidden-states without any specific head on top. + */ +class ASTModel extends ASTPreTrainedModel { } + +/** + * Audio Spectrogram Transformer model with an audio classification head on top + * (a linear layer on top of the pooled output) e.g. for datasets like AudioSet, Speech Commands v2. + */ +class ASTForAudioClassification extends ASTPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Whisper models +class WhisperPreTrainedModel extends PreTrainedModel { + + requires_attention_mask = false; + main_input_name = 'input_features'; + forward_params = [ + 'input_features', + 'attention_mask', + 'decoder_input_ids', + 'decoder_attention_mask', + 'past_key_values', + ]; +}; + +/** + * WhisperModel class for training Whisper models without a language model head. + */ +class WhisperModel extends WhisperPreTrainedModel { } + + +/** + * WhisperForConditionalGeneration class for generating conditional outputs from Whisper models. + */ +class WhisperForConditionalGeneration extends WhisperPreTrainedModel { + + _prepare_generation_config(generation_config, kwargs) { + return /** @type {WhisperGenerationConfig} */ (super._prepare_generation_config(generation_config, kwargs, _models_whisper_generation_whisper_js__WEBPACK_IMPORTED_MODULE_14__.WhisperGenerationConfig)); + } + + /** + * + * @param {WhisperGenerationConfig} generation_config + */ + _retrieve_init_tokens(generation_config) { + // prefix tokens are of the form: + // - Multilingual: <|startoftranscript|> <|lang_id|> <|task|> [<|notimestamps|>] + // - English-only: <|startoftranscript|> [<|notimestamps|>] + + // 1. Handle <|startoftranscript|> token + const init_tokens = [generation_config.decoder_start_token_id]; + + // 2. Handle <|lang_id|> and <|task> tokens + let language = generation_config.language; + const task = generation_config.task; + if (generation_config.is_multilingual) { + if (!language) { + // TODO: Implement language detection + console.warn('No language specified - defaulting to English (en).'); + language = 'en'; + } + + // Add language token + const language_code = (0,_models_whisper_common_whisper_js__WEBPACK_IMPORTED_MODULE_15__.whisper_language_to_code)(language); + const language_token = `<|${language_code}|>`; + init_tokens.push(generation_config.lang_to_id[language_token]) + + // Add task token + // NOTE: Defaults to 'transcribe' if no task is specified + init_tokens.push(generation_config.task_to_id[task ?? 'transcribe']); + + } else if (language || task) { + throw new Error( + "Cannot specify `task` or `language` for an English-only model. If the model is intended to be multilingual, pass `is_multilingual=true` to generate, or update the generation config." + ) + } + + // 3. Handle <|notimestamps|> token + if ( + !generation_config.return_timestamps + && generation_config.no_timestamps_token_id + && init_tokens.at(-1) !== generation_config.no_timestamps_token_id + ) { + init_tokens.push(generation_config.no_timestamps_token_id); + } else if ( + generation_config.return_timestamps + && + init_tokens.at(-1) === generation_config.no_timestamps_token_id + ) { + console.warn("<|notimestamps|> prompt token is removed from generation_config since `return_timestamps` is set to `true`."); + init_tokens.pop(); + } + + // let's make sure we don't pass `null` tokens as prompt tokens + return init_tokens.filter(token => token != null); + } + + /** + * Transcribes or translates log-mel input features to a sequence of auto-regressively generated token ids. + * @param {import('./models/whisper/generation_whisper.js').WhisperGenerationFunctionParameters} options + * @returns {Promise} The output of the model, which can contain the generated token ids, attentions, and scores. + */ + async generate({ + inputs = null, + generation_config = null, + logits_processor = null, + stopping_criteria = null, + + // Whisper-specific options (passed to kwargs) + // prompt_ids = null, + // language = null, + // task = null, + + ...kwargs + }) { + generation_config = this._prepare_generation_config(generation_config, kwargs); + + const init_tokens = kwargs.decoder_input_ids ?? this._retrieve_init_tokens(generation_config); + + if (generation_config.return_timestamps) { + logits_processor ??= new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.LogitsProcessorList(); + logits_processor.push( + new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.WhisperTimeStampLogitsProcessor(generation_config, init_tokens) + ); + } + + if (generation_config.begin_suppress_tokens) { + logits_processor ??= new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.LogitsProcessorList(); + logits_processor.push( + new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.SuppressTokensAtBeginLogitsProcessor(generation_config.begin_suppress_tokens, init_tokens.length) + ); + } + + if (generation_config.return_token_timestamps) { + if (!generation_config.alignment_heads) { + throw new Error( + "Model generation config has no `alignment_heads`, token-level timestamps not available. " + + "See https://gist.github.com/hollance/42e32852f24243b748ae6bc1f985b13a on how to add this property to the generation config." + ) + } + + if (generation_config.task === 'translate') { + console.warn("Token-level timestamps may not be reliable for task 'translate'.") + } + + generation_config.output_attentions = true; + generation_config.return_dict_in_generate = true; + } + + const outputs = await super.generate({ + inputs, + generation_config, + logits_processor, + decoder_input_ids: init_tokens, + ...kwargs + }); + + if (generation_config.return_token_timestamps) { + outputs["token_timestamps"] = this._extract_token_timestamps( + outputs, + generation_config.alignment_heads, + generation_config.num_frames, + ); + } + + return outputs; + } + + /** + * Calculates token-level timestamps using the encoder-decoder cross-attentions and + * dynamic time-warping (DTW) to map each output token to a position in the input audio. + * If `num_frames` is specified, the encoder-decoder cross-attentions will be cropped before applying DTW. + * @param {Object} generate_outputs Outputs generated by the model + * @param {Tensor[][]} generate_outputs.cross_attentions The cross attentions output by the model + * @param {Tensor} generate_outputs.sequences The sequences output by the model + * @param {number[][]} alignment_heads Alignment heads of the model + * @param {number} [num_frames=null] Number of frames in the input audio. + * @param {number} [time_precision=0.02] Precision of the timestamps in seconds + * @returns {Tensor} tensor containing the timestamps in seconds for each predicted token + */ + _extract_token_timestamps(generate_outputs, alignment_heads, num_frames = null, time_precision = 0.02) { + if (!generate_outputs.cross_attentions) { + throw new Error( + "Model outputs must contain cross attentions to extract timestamps. " + + "This is most likely because the model was not exported with `output_attentions=True`." + ) + } + if (num_frames == null) { + console.warn( + "`num_frames` has not been set, meaning the entire audio will be analyzed. " + + "This may lead to inaccurate token-level timestamps for short audios (< 30 seconds)." + ); + } + + let median_filter_width = this.config.median_filter_width; + if (median_filter_width === undefined) { + console.warn("Model config has no `median_filter_width`, using default value of 7.") + median_filter_width = 7; + } + + // TODO: Improve batch processing + const batch = generate_outputs.cross_attentions; + // Create a list with `decoder_layers` elements, each a tensor of shape + // (batch size, attention_heads, output length, input length). + const cross_attentions = Array.from({ length: this.config.decoder_layers }, + // Concatenate the cross attentions for each layer across sequence length dimension. + (_, i) => (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)(batch.map(x => x[i]), 2) + ); + + const weights = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.stack)(alignment_heads.map(([l, h]) => { + if (l >= cross_attentions.length) { + throw new Error(`Layer index ${l} is out of bounds for cross attentions (length ${cross_attentions.length}).`) + } + return num_frames + ? cross_attentions[l].slice(null, h, null, [0, num_frames]) + : cross_attentions[l].slice(null, h); + })).transpose(1, 0, 2, 3); + + const [std, calculatedMean] = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.std_mean)(weights, -2, 0, true); + + // Normalize and smoothen the weights. + const smoothedWeights = weights.clone(); // [1, 8, seqLength, 1500] + + for (let a = 0; a < smoothedWeights.dims[0]; ++a) { + const aTensor = smoothedWeights[a]; // [8, seqLength, 1500] + + for (let b = 0; b < aTensor.dims[0]; ++b) { + const bTensor = aTensor[b]; // [seqLength, 1500] + + const stdTensorData = std[a][b][0].data; // [1500] + const meanTensorData = calculatedMean[a][b][0].data; // [1500] + + for (let c = 0; c < bTensor.dims[0]; ++c) { + + let cTensorData = bTensor[c].data; // [1500] + for (let d = 0; d < cTensorData.length; ++d) { + cTensorData[d] = (cTensorData[d] - meanTensorData[d]) / stdTensorData[d] + } + + // Apply median filter. + cTensorData.set((0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_10__.medianFilter)(cTensorData, median_filter_width)) + } + } + } + + // Average the different cross-attention heads. + const batchedMatrices = [(0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.mean)(smoothedWeights, 1)]; + + const timestampsShape = generate_outputs.sequences.dims; + + const timestamps = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor( + 'float32', + new Float32Array(timestampsShape[0] * timestampsShape[1]), + timestampsShape + ); + + // Perform dynamic time warping on each element of the batch. + for (let batch_idx = 0; batch_idx < timestampsShape[0]; ++batch_idx) { + // NOTE: Since we run only one batch at a time, we can squeeze to get the same dimensions + // as the python implementation + const matrix = batchedMatrices[batch_idx].neg().squeeze_(0); + const [text_indices, time_indices] = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_10__.dynamic_time_warping)(matrix.tolist()); + + const diffs = Array.from({ length: text_indices.length - 1 }, (v, i) => text_indices[i + 1] - text_indices[i]); + const jumps = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.mergeArrays)([1], diffs).map(x => !!x); // convert to boolean + + const jump_times = []; + for (let i = 0; i < jumps.length; ++i) { + if (jumps[i]) { + // NOTE: No point in rounding here, since we set to Float32Array later + jump_times.push(time_indices[i] * time_precision); + } + } + timestamps[batch_idx].data.set(jump_times, 1) + } + + return timestamps; + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +/** + * Vision Encoder-Decoder model based on OpenAI's GPT architecture for image captioning and other vision tasks + */ +class VisionEncoderDecoderModel extends PreTrainedModel { + main_input_name = 'pixel_values'; + forward_params = [ + // Encoder inputs + 'pixel_values', + + // Decoder inpputs + 'decoder_input_ids', + 'encoder_hidden_states', + 'past_key_values', + ]; +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// LLaVa Models +class LlavaPreTrainedModel extends PreTrainedModel { + forward_params = [ + 'input_ids', + 'pixel_values', + 'attention_mask', + 'position_ids', + 'past_key_values', + ]; +} + +/** + * The LLAVA model which consists of a vision backbone and a language model. + */ +class LlavaForConditionalGeneration extends LlavaPreTrainedModel { + + _merge_input_ids_with_image_features({ + inputs_embeds, + image_features, + input_ids, + attention_mask, + }) { + + const image_token_index = this.config.image_token_index; + + const idsList = input_ids.tolist(); + + // NOTE: we use .findIndex instead of .indexOf to perform weak comparison (==) between BigInt and Number + const indexOfImage = idsList.map(x => x.findIndex(x => x == image_token_index)); + + const noImages = indexOfImage.every(x => x === -1); + const allImages = indexOfImage.every(x => x !== -1); + if (!noImages && !allImages) { + // Check for padding reasons + throw new Error('Every input should contain either 0 or 1 image token.'); + } + + if (noImages) { + return { + inputs_embeds, + attention_mask, + } + } + + const stacked = []; + const stacked_attention_mask = []; + for (let i = 0; i < indexOfImage.length; ++i) { + const index = indexOfImage[i]; + + const e = inputs_embeds[i]; + const im = image_features[i]; + const am = attention_mask[i]; + stacked.push( + (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)([ + e.slice([0, index]), + im, + e.slice([index + 1, e.dims[0]]), + ], 0) + ); + + stacked_attention_mask.push( + (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)([ + am.slice([0, index]), + (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.ones)([im.dims[0]]), + am.slice([index + 1, am.dims[0]]) + ], 0) + ) + } + + return { + inputs_embeds: (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.stack)(stacked, 0), + attention_mask: (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.stack)(stacked_attention_mask, 0), + } + } +} +////////////////////////////////////////////////// + +class Moondream1ForConditionalGeneration extends LlavaForConditionalGeneration { } // NOTE: extends LlavaForConditionalGeneration + +class Florence2PreTrainedModel extends PreTrainedModel { + forward_params = [ + // Encoder inputs + 'input_ids', + 'inputs_embeds', + 'attention_mask', + 'pixel_values', + + // Decoder inputs + 'encoder_outputs', + 'decoder_input_ids', + 'decoder_inputs_embeds', + 'decoder_attention_mask', + 'past_key_values', + ]; + main_input_name = 'inputs_embeds'; +} + +class Florence2ForConditionalGeneration extends Florence2PreTrainedModel { + + _merge_input_ids_with_image_features({ + inputs_embeds, + image_features, + input_ids, + attention_mask, + }) { + return { + inputs_embeds: (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)([ + image_features, // image embeds + inputs_embeds, // task prefix embeds + ], 1), + attention_mask: (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)([ + (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.ones)(image_features.dims.slice(0, 2)), // image attention mask + attention_mask, // task prefix attention mask + ], 1), + } + } + + async _prepare_inputs_embeds({ input_ids, pixel_values, inputs_embeds, attention_mask }) { + if (!input_ids && !pixel_values) { + throw new Error('Either `input_ids` or `pixel_values` should be provided.'); + } + + // 1. Possibly, extract the input embeddings + let text_features, image_features; + if (input_ids) { + text_features = await this.encode_text({ input_ids }); + } + if (pixel_values) { + image_features = await this.encode_image({ pixel_values }); + } + + // 2. Possibly, merge text and images + if (text_features && image_features) { + ({ inputs_embeds, attention_mask } = this._merge_input_ids_with_image_features({ + inputs_embeds: text_features, + image_features, + input_ids, + attention_mask, + })); + } else { + inputs_embeds = text_features || image_features; + } + + return { inputs_embeds, attention_mask }; + } + + async forward({ + input_ids, + pixel_values, + attention_mask, + decoder_input_ids, + decoder_attention_mask, + encoder_outputs, + past_key_values, + + inputs_embeds, + decoder_inputs_embeds, + }) { + if (!inputs_embeds) { + ({ inputs_embeds, attention_mask } = await this._prepare_inputs_embeds({ input_ids, pixel_values, inputs_embeds, attention_mask })); + } + + if (!encoder_outputs) { + // Must compute encoder outputs + let { last_hidden_state } = await encoderForward(this, { inputs_embeds, attention_mask }); + encoder_outputs = last_hidden_state; + } + + if (!decoder_inputs_embeds) { + if (!decoder_input_ids) { + throw new Error('Either `decoder_input_ids` or `decoder_inputs_embeds` should be provided.'); + } + decoder_inputs_embeds = await this.encode_text({ input_ids: decoder_input_ids }); + } + + const decoderFeeds = { + inputs_embeds: decoder_inputs_embeds, + attention_mask: decoder_attention_mask, + encoder_attention_mask: attention_mask, + encoder_hidden_states: encoder_outputs, + past_key_values, + }; + const decoder_outputs = await decoderForward(this, decoderFeeds, true); + return decoder_outputs; + } +} +class CLIPPreTrainedModel extends PreTrainedModel { } + +/** + * CLIP Text and Vision Model with a projection layers on top + * + * **Example:** Perform zero-shot image classification with a `CLIPModel`. + * + * ```javascript + * import { AutoTokenizer, AutoProcessor, CLIPModel, RawImage } from '@huggingface/transformers'; + * + * // Load tokenizer, processor, and model + * let tokenizer = await AutoTokenizer.from_pretrained('Xenova/clip-vit-base-patch16'); + * let processor = await AutoProcessor.from_pretrained('Xenova/clip-vit-base-patch16'); + * let model = await CLIPModel.from_pretrained('Xenova/clip-vit-base-patch16'); + * + * // Run tokenization + * let texts = ['a photo of a car', 'a photo of a football match'] + * let text_inputs = tokenizer(texts, { padding: true, truncation: true }); + * + * // Read image and run processor + * let image = await RawImage.read('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/football-match.jpg'); + * let image_inputs = await processor(image); + * + * // Run model with both text and pixel inputs + * let output = await model({ ...text_inputs, ...image_inputs }); + * // { + * // logits_per_image: Tensor { + * // dims: [ 1, 2 ], + * // data: Float32Array(2) [ 18.579734802246094, 24.31830596923828 ], + * // }, + * // logits_per_text: Tensor { + * // dims: [ 2, 1 ], + * // data: Float32Array(2) [ 18.579734802246094, 24.31830596923828 ], + * // }, + * // text_embeds: Tensor { + * // dims: [ 2, 512 ], + * // data: Float32Array(1024) [ ... ], + * // }, + * // image_embeds: Tensor { + * // dims: [ 1, 512 ], + * // data: Float32Array(512) [ ... ], + * // } + * // } + * ``` + */ +class CLIPModel extends CLIPPreTrainedModel { } + +/** + * The text model from CLIP without any head or projection on top. + */ +class CLIPTextModel extends CLIPPreTrainedModel { + /** @type {PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options = {}) { + // Update default model file name if not provided + options.model_file_name ??= 'text_model'; + return super.from_pretrained(pretrained_model_name_or_path, options); + } +} + +/** + * CLIP Text Model with a projection layer on top (a linear layer on top of the pooled output) + * + * **Example:** Compute text embeddings with `CLIPTextModelWithProjection`. + * + * ```javascript + * import { AutoTokenizer, CLIPTextModelWithProjection } from '@huggingface/transformers'; + * + * // Load tokenizer and text model + * const tokenizer = await AutoTokenizer.from_pretrained('Xenova/clip-vit-base-patch16'); + * const text_model = await CLIPTextModelWithProjection.from_pretrained('Xenova/clip-vit-base-patch16'); + * + * // Run tokenization + * let texts = ['a photo of a car', 'a photo of a football match']; + * let text_inputs = tokenizer(texts, { padding: true, truncation: true }); + * + * // Compute embeddings + * const { text_embeds } = await text_model(text_inputs); + * // Tensor { + * // dims: [ 2, 512 ], + * // type: 'float32', + * // data: Float32Array(1024) [ ... ], + * // size: 1024 + * // } + * ``` + */ +class CLIPTextModelWithProjection extends CLIPPreTrainedModel { + /** @type {PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options = {}) { + // Update default model file name if not provided + options.model_file_name ??= 'text_model'; + return super.from_pretrained(pretrained_model_name_or_path, options); + } +} + +/** + * The vision model from CLIP without any head or projection on top. + */ +class CLIPVisionModel extends CLIPPreTrainedModel { + /** @type {PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options = {}) { + // Update default model file name if not provided + options.model_file_name ??= 'vision_model'; + return super.from_pretrained(pretrained_model_name_or_path, options); + } +} + +/** + * CLIP Vision Model with a projection layer on top (a linear layer on top of the pooled output) + * + * **Example:** Compute vision embeddings with `CLIPVisionModelWithProjection`. + * + * ```javascript + * import { AutoProcessor, CLIPVisionModelWithProjection, RawImage} from '@huggingface/transformers'; + * + * // Load processor and vision model + * const processor = await AutoProcessor.from_pretrained('Xenova/clip-vit-base-patch16'); + * const vision_model = await CLIPVisionModelWithProjection.from_pretrained('Xenova/clip-vit-base-patch16'); + * + * // Read image and run processor + * let image = await RawImage.read('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/football-match.jpg'); + * let image_inputs = await processor(image); + * + * // Compute embeddings + * const { image_embeds } = await vision_model(image_inputs); + * // Tensor { + * // dims: [ 1, 512 ], + * // type: 'float32', + * // data: Float32Array(512) [ ... ], + * // size: 512 + * // } + * ``` + */ +class CLIPVisionModelWithProjection extends CLIPPreTrainedModel { + /** @type {PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options = {}) { + // Update default model file name if not provided + options.model_file_name ??= 'vision_model'; + return super.from_pretrained(pretrained_model_name_or_path, options); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// SigLIP models +class SiglipPreTrainedModel extends PreTrainedModel { } + +/** + * SigLIP Text and Vision Model with a projection layers on top + * + * **Example:** Perform zero-shot image classification with a `SiglipModel`. + * + * ```javascript + * import { AutoTokenizer, AutoProcessor, SiglipModel, RawImage } from '@huggingface/transformers'; + * + * // Load tokenizer, processor, and model + * const tokenizer = await AutoTokenizer.from_pretrained('Xenova/siglip-base-patch16-224'); + * const processor = await AutoProcessor.from_pretrained('Xenova/siglip-base-patch16-224'); + * const model = await SiglipModel.from_pretrained('Xenova/siglip-base-patch16-224'); + * + * // Run tokenization + * const texts = ['a photo of 2 cats', 'a photo of 2 dogs']; + * const text_inputs = tokenizer(texts, { padding: 'max_length', truncation: true }); + * + * // Read image and run processor + * const image = await RawImage.read('http://images.cocodataset.org/val2017/000000039769.jpg'); + * const image_inputs = await processor(image); + * + * // Run model with both text and pixel inputs + * const output = await model({ ...text_inputs, ...image_inputs }); + * // { + * // logits_per_image: Tensor { + * // dims: [ 1, 2 ], + * // data: Float32Array(2) [ -1.6019744873046875, -10.720091819763184 ], + * // }, + * // logits_per_text: Tensor { + * // dims: [ 2, 1 ], + * // data: Float32Array(2) [ -1.6019744873046875, -10.720091819763184 ], + * // }, + * // text_embeds: Tensor { + * // dims: [ 2, 768 ], + * // data: Float32Array(1536) [ ... ], + * // }, + * // image_embeds: Tensor { + * // dims: [ 1, 768 ], + * // data: Float32Array(768) [ ... ], + * // } + * // } + * ``` + */ +class SiglipModel extends SiglipPreTrainedModel { } + +/** + * The text model from SigLIP without any head or projection on top. + * + * **Example:** Compute text embeddings with `SiglipTextModel`. + * + * ```javascript + * import { AutoTokenizer, SiglipTextModel } from '@huggingface/transformers'; + * + * // Load tokenizer and text model + * const tokenizer = await AutoTokenizer.from_pretrained('Xenova/siglip-base-patch16-224'); + * const text_model = await SiglipTextModel.from_pretrained('Xenova/siglip-base-patch16-224'); + * + * // Run tokenization + * const texts = ['a photo of 2 cats', 'a photo of 2 dogs']; + * const text_inputs = tokenizer(texts, { padding: 'max_length', truncation: true }); + * + * // Compute embeddings + * const { pooler_output } = await text_model(text_inputs); + * // Tensor { + * // dims: [ 2, 768 ], + * // type: 'float32', + * // data: Float32Array(1536) [ ... ], + * // size: 1536 + * // } + * ``` + */ +class SiglipTextModel extends SiglipPreTrainedModel { + + /** @type {PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options = {}) { + // Update default model file name if not provided + options.model_file_name ??= 'text_model'; + return super.from_pretrained(pretrained_model_name_or_path, options); + } +} + +/** + * The vision model from SigLIP without any head or projection on top. + * + * **Example:** Compute vision embeddings with `SiglipVisionModel`. + * + * ```javascript + * import { AutoProcessor, SiglipVisionModel, RawImage} from '@huggingface/transformers'; + * + * // Load processor and vision model + * const processor = await AutoProcessor.from_pretrained('Xenova/siglip-base-patch16-224'); + * const vision_model = await SiglipVisionModel.from_pretrained('Xenova/siglip-base-patch16-224'); + * + * // Read image and run processor + * const image = await RawImage.read('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/football-match.jpg'); + * const image_inputs = await processor(image); + * + * // Compute embeddings + * const { pooler_output } = await vision_model(image_inputs); + * // Tensor { + * // dims: [ 1, 768 ], + * // type: 'float32', + * // data: Float32Array(768) [ ... ], + * // size: 768 + * // } + * ``` + */ +class SiglipVisionModel extends CLIPPreTrainedModel { + /** @type {PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options = {}) { + // Update default model file name if not provided + options.model_file_name ??= 'vision_model'; + return super.from_pretrained(pretrained_model_name_or_path, options); + } +} +////////////////////////////////////////////////// +// ChineseCLIP models +class ChineseCLIPPreTrainedModel extends PreTrainedModel { } + +class ChineseCLIPModel extends ChineseCLIPPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// CLIPSeg models +class CLIPSegPreTrainedModel extends PreTrainedModel { } + +class CLIPSegModel extends CLIPSegPreTrainedModel { } + +/** + * CLIPSeg model with a Transformer-based decoder on top for zero-shot and one-shot image segmentation. + * + * **Example:** Perform zero-shot image segmentation with a `CLIPSegForImageSegmentation` model. + * + * ```javascript + * import { AutoTokenizer, AutoProcessor, CLIPSegForImageSegmentation, RawImage } from '@huggingface/transformers'; + * + * // Load tokenizer, processor, and model + * const tokenizer = await AutoTokenizer.from_pretrained('Xenova/clipseg-rd64-refined'); + * const processor = await AutoProcessor.from_pretrained('Xenova/clipseg-rd64-refined'); + * const model = await CLIPSegForImageSegmentation.from_pretrained('Xenova/clipseg-rd64-refined'); + * + * // Run tokenization + * const texts = ['a glass', 'something to fill', 'wood', 'a jar']; + * const text_inputs = tokenizer(texts, { padding: true, truncation: true }); + * + * // Read image and run processor + * const image = await RawImage.read('https://github.com/timojl/clipseg/blob/master/example_image.jpg?raw=true'); + * const image_inputs = await processor(image); + * + * // Run model with both text and pixel inputs + * const { logits } = await model({ ...text_inputs, ...image_inputs }); + * // logits: Tensor { + * // dims: [4, 352, 352], + * // type: 'float32', + * // data: Float32Array(495616) [ ... ], + * // size: 495616 + * // } + * ``` + * + * You can visualize the predictions as follows: + * ```javascript + * const preds = logits + * .unsqueeze_(1) + * .sigmoid_() + * .mul_(255) + * .round_() + * .to('uint8'); + * + * for (let i = 0; i < preds.dims[0]; ++i) { + * const img = RawImage.fromTensor(preds[i]); + * img.save(`prediction_${i}.png`); + * } + * ``` + */ +class CLIPSegForImageSegmentation extends CLIPSegPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// GPT2 models +class GPT2PreTrainedModel extends PreTrainedModel { } + +class GPT2Model extends GPT2PreTrainedModel { } + +/** + * GPT-2 language model head on top of the GPT-2 base model. This model is suitable for text generation tasks. + */ +class GPT2LMHeadModel extends GPT2PreTrainedModel { } +// export class GPT2ForSequenceClassification extends GPT2PreTrainedModel { +// TODO +// } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// JAIS models +class JAISPreTrainedModel extends PreTrainedModel { } + +/** + * The bare JAIS Model transformer outputting raw hidden-states without any specific head on top. + */ +class JAISModel extends JAISPreTrainedModel { } + +/** + * The JAIS Model transformer with a language modeling head on top (linear layer with weights tied to the input embeddings). + */ +class JAISLMHeadModel extends JAISPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// GPTNeo models +class GPTNeoPreTrainedModel extends PreTrainedModel { } +class GPTNeoModel extends GPTNeoPreTrainedModel { } + +class GPTNeoForCausalLM extends GPTNeoPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// GPTNeoX models +class GPTNeoXPreTrainedModel extends PreTrainedModel { } +class GPTNeoXModel extends GPTNeoXPreTrainedModel { } + +class GPTNeoXForCausalLM extends GPTNeoXPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// GPT-J models +class GPTJPreTrainedModel extends PreTrainedModel { } + +class GPTJModel extends GPTJPreTrainedModel { } + +class GPTJForCausalLM extends GPTJPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// GPTBigCode models +class GPTBigCodePreTrainedModel extends PreTrainedModel { } + +class GPTBigCodeModel extends GPTBigCodePreTrainedModel { } + +class GPTBigCodeForCausalLM extends GPTBigCodePreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// CodeGen models +class CodeGenPreTrainedModel extends PreTrainedModel { } +/** + * CodeGenModel is a class representing a code generation model without a language model head. + */ +class CodeGenModel extends CodeGenPreTrainedModel { } + +/** + * CodeGenForCausalLM is a class that represents a code generation model based on the GPT-2 architecture. It extends the `CodeGenPreTrainedModel` class. + */ +class CodeGenForCausalLM extends CodeGenPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// LLama models + +/** + * The bare LLama Model outputting raw hidden-states without any specific head on top. + */ +class LlamaPreTrainedModel extends PreTrainedModel { } +/** + * The bare LLaMA Model outputting raw hidden-states without any specific head on top. + */ +class LlamaModel extends LlamaPreTrainedModel { } + +class LlamaForCausalLM extends LlamaPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// MobileLLM models +class MobileLLMPreTrainedModel extends PreTrainedModel { } +class MobileLLMModel extends MobileLLMPreTrainedModel { } +class MobileLLMForCausalLM extends MobileLLMPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// OLMo models +class OlmoPreTrainedModel extends PreTrainedModel { } +class OlmoModel extends OlmoPreTrainedModel { } +class OlmoForCausalLM extends OlmoPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Granite models +class GranitePreTrainedModel extends PreTrainedModel { } +class GraniteModel extends GranitePreTrainedModel { } +class GraniteForCausalLM extends GranitePreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Cohere models + +/** + * The bare Cohere Model outputting raw hidden-states without any specific head on top. + */ +class CoherePreTrainedModel extends PreTrainedModel { } +class CohereModel extends CoherePreTrainedModel { } + +class CohereForCausalLM extends CoherePreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Gemma models + +/** + * The bare Gemma Model outputting raw hidden-states without any specific head on top. + */ +class GemmaPreTrainedModel extends PreTrainedModel { } +/** + * The bare Gemma Model outputting raw hidden-states without any specific head on top. + */ +class GemmaModel extends GemmaPreTrainedModel { } + +class GemmaForCausalLM extends GemmaPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Gemma2 models + +/** + * The bare Gemma2 Model outputting raw hidden-states without any specific head on top. + */ +class Gemma2PreTrainedModel extends PreTrainedModel { } +/** + * The bare Gemma2 Model outputting raw hidden-states without any specific head on top. + */ +class Gemma2Model extends Gemma2PreTrainedModel { } + +class Gemma2ForCausalLM extends Gemma2PreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class OpenELMPreTrainedModel extends PreTrainedModel { } +class OpenELMModel extends OpenELMPreTrainedModel { } + +class OpenELMForCausalLM extends OpenELMPreTrainedModel { } + + +////////////////////////////////////////////////// +// Qwen2 models + +/** + * The bare Qwen2 Model outputting raw hidden-states without any specific head on top. + */ +class Qwen2PreTrainedModel extends PreTrainedModel { } +/** + * The bare Qwen2 Model outputting raw hidden-states without any specific head on top. + */ +class Qwen2Model extends Qwen2PreTrainedModel { } + +class Qwen2ForCausalLM extends Qwen2PreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Phi models +class PhiPreTrainedModel extends PreTrainedModel { } +/** + * The bare Phi Model outputting raw hidden-states without any specific head on top. + */ +class PhiModel extends PhiPreTrainedModel { } + +class PhiForCausalLM extends PhiPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Phi3 models +class Phi3PreTrainedModel extends PreTrainedModel { } + +/** + * The bare Phi3 Model outputting raw hidden-states without any specific head on top. + */ +class Phi3Model extends Phi3PreTrainedModel { } + +class Phi3ForCausalLM extends Phi3PreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Bloom models +/** + * The Bloom Model transformer with a language modeling head on top (linear layer with weights tied to the input embeddings). + */ +class BloomPreTrainedModel extends PreTrainedModel { } + +/** + * The bare Bloom Model transformer outputting raw hidden-states without any specific head on top. + */ +class BloomModel extends BloomPreTrainedModel { } + +/** + * The Bloom Model transformer with a language modeling head on top (linear layer with weights tied to the input embeddings). + */ +class BloomForCausalLM extends BloomPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// MPT models +class MptPreTrainedModel extends PreTrainedModel { } + +/** + * The bare Mpt Model transformer outputting raw hidden-states without any specific head on top. + */ +class MptModel extends MptPreTrainedModel { } + +/** + * The MPT Model transformer with a language modeling head on top (linear layer with weights tied to the input embeddings). + */ +class MptForCausalLM extends MptPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// OPT models +class OPTPreTrainedModel extends PreTrainedModel { } + +/** + * The bare OPT Model outputting raw hidden-states without any specific head on top. + */ +class OPTModel extends OPTPreTrainedModel { } + +/** + * The OPT Model transformer with a language modeling head on top (linear layer with weights tied to the input embeddings). + */ +class OPTForCausalLM extends OPTPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class ViTPreTrainedModel extends PreTrainedModel { } +class ViTModel extends ViTPreTrainedModel { } +class ViTForImageClassification extends ViTPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class PvtPreTrainedModel extends PreTrainedModel { } +class PvtModel extends PvtPreTrainedModel { } +class PvtForImageClassification extends PvtPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class ViTMAEPreTrainedModel extends PreTrainedModel { } +class ViTMAEModel extends ViTMAEPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +class ViTMSNPreTrainedModel extends PreTrainedModel { } +class ViTMSNModel extends ViTMSNPreTrainedModel { } +class ViTMSNForImageClassification extends ViTMSNPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class GroupViTPreTrainedModel extends PreTrainedModel { } +class GroupViTModel extends GroupViTPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +class FastViTPreTrainedModel extends PreTrainedModel { } +class FastViTModel extends FastViTPreTrainedModel { } +class FastViTForImageClassification extends FastViTPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class VitMattePreTrainedModel extends PreTrainedModel { } + +/** + * ViTMatte framework leveraging any vision backbone e.g. for ADE20k, CityScapes. + * + * **Example:** Perform image matting with a `VitMatteForImageMatting` model. + * ```javascript + * import { AutoProcessor, VitMatteForImageMatting, RawImage } from '@huggingface/transformers'; + * + * // Load processor and model + * const processor = await AutoProcessor.from_pretrained('Xenova/vitmatte-small-distinctions-646'); + * const model = await VitMatteForImageMatting.from_pretrained('Xenova/vitmatte-small-distinctions-646'); + * + * // Load image and trimap + * const image = await RawImage.fromURL('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/vitmatte_image.png'); + * const trimap = await RawImage.fromURL('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/vitmatte_trimap.png'); + * + * // Prepare image + trimap for the model + * const inputs = await processor(image, trimap); + * + * // Predict alpha matte + * const { alphas } = await model(inputs); + * // Tensor { + * // dims: [ 1, 1, 640, 960 ], + * // type: 'float32', + * // size: 614400, + * // data: Float32Array(614400) [ 0.9894027709960938, 0.9970508813858032, ... ] + * // } + * ``` + * + * You can visualize the alpha matte as follows: + * ```javascript + * import { Tensor, cat } from '@huggingface/transformers'; + * + * // Visualize predicted alpha matte + * const imageTensor = image.toTensor(); + * + * // Convert float (0-1) alpha matte to uint8 (0-255) + * const alphaChannel = alphas + * .squeeze(0) + * .mul_(255) + * .clamp_(0, 255) + * .round_() + * .to('uint8'); + * + * // Concatenate original image with predicted alpha + * const imageData = cat([imageTensor, alphaChannel], 0); + * + * // Save output image + * const outputImage = RawImage.fromTensor(imageData); + * outputImage.save('output.png'); + * ``` + */ +class VitMatteForImageMatting extends VitMattePreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new ImageMattingOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class MobileViTPreTrainedModel extends PreTrainedModel { } +class MobileViTModel extends MobileViTPreTrainedModel { } +class MobileViTForImageClassification extends MobileViTPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +// TODO: MobileViTForSemanticSegmentation + +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class MobileViTV2PreTrainedModel extends PreTrainedModel { } +class MobileViTV2Model extends MobileViTV2PreTrainedModel { } +class MobileViTV2ForImageClassification extends MobileViTV2PreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +// TODO: MobileViTV2ForSemanticSegmentation + +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class OwlViTPreTrainedModel extends PreTrainedModel { } +class OwlViTModel extends OwlViTPreTrainedModel { } +class OwlViTForObjectDetection extends OwlViTPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class Owlv2PreTrainedModel extends PreTrainedModel { } +class Owlv2Model extends Owlv2PreTrainedModel { } +class Owlv2ForObjectDetection extends Owlv2PreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Beit Models +class BeitPreTrainedModel extends PreTrainedModel { } +class BeitModel extends BeitPreTrainedModel { } +class BeitForImageClassification extends BeitPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +class DetrPreTrainedModel extends PreTrainedModel { } +class DetrModel extends DetrPreTrainedModel { } +class DetrForObjectDetection extends DetrPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new DetrObjectDetectionOutput(await super._call(model_inputs)); + } +} + +class DetrForSegmentation extends DetrPreTrainedModel { + /** + * Runs the model with the provided inputs + * @param {Object} model_inputs Model inputs + * @returns {Promise} Object containing segmentation outputs + */ + async _call(model_inputs) { + return new DetrSegmentationOutput(await super._call(model_inputs)); + } +} + +class DetrObjectDetectionOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits Classification logits (including no-object) for all queries. + * @param {Tensor} output.pred_boxes Normalized boxes coordinates for all queries, represented as (center_x, center_y, width, height). + * These values are normalized in [0, 1], relative to the size of each individual image in the batch (disregarding possible padding). + */ + constructor({ logits, pred_boxes }) { + super(); + this.logits = logits; + this.pred_boxes = pred_boxes; + } +} + +class DetrSegmentationOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits The output logits of the model. + * @param {Tensor} output.pred_boxes Predicted boxes. + * @param {Tensor} output.pred_masks Predicted masks. + */ + constructor({ logits, pred_boxes, pred_masks }) { + super(); + this.logits = logits; + this.pred_boxes = pred_boxes; + this.pred_masks = pred_masks; + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class RTDetrPreTrainedModel extends PreTrainedModel { } +class RTDetrModel extends RTDetrPreTrainedModel { } +class RTDetrForObjectDetection extends RTDetrPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new RTDetrObjectDetectionOutput(await super._call(model_inputs)); + } +} + +class RTDetrObjectDetectionOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits Classification logits (including no-object) for all queries. + * @param {Tensor} output.pred_boxes Normalized boxes coordinates for all queries, represented as (center_x, center_y, width, height). + * These values are normalized in [0, 1], relative to the size of each individual image in the batch (disregarding possible padding). + */ + constructor({ logits, pred_boxes }) { + super(); + this.logits = logits; + this.pred_boxes = pred_boxes; + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class TableTransformerPreTrainedModel extends PreTrainedModel { } + +/** + * The bare Table Transformer Model (consisting of a backbone and encoder-decoder Transformer) + * outputting raw hidden-states without any specific head on top. + */ +class TableTransformerModel extends TableTransformerPreTrainedModel { } + +/** + * Table Transformer Model (consisting of a backbone and encoder-decoder Transformer) + * with object detection heads on top, for tasks such as COCO detection. + */ +class TableTransformerForObjectDetection extends TableTransformerPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new TableTransformerObjectDetectionOutput(await super._call(model_inputs)); + } +} +class TableTransformerObjectDetectionOutput extends DetrObjectDetectionOutput { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +class DeiTPreTrainedModel extends PreTrainedModel { } +class DeiTModel extends DeiTPreTrainedModel { } +class DeiTForImageClassification extends DeiTPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class HieraPreTrainedModel extends PreTrainedModel { } +class HieraModel extends HieraPreTrainedModel { } +class HieraForImageClassification extends HieraPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +/** + * An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained models. + */ +class ResNetPreTrainedModel extends PreTrainedModel { } + +/** + * The bare ResNet model outputting raw features without any specific head on top. + */ +class ResNetModel extends ResNetPreTrainedModel { } + +/** + * ResNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for ImageNet. + */ +class ResNetForImageClassification extends ResNetPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +class SwinPreTrainedModel extends PreTrainedModel { } +class SwinModel extends SwinPreTrainedModel { } +class SwinForImageClassification extends SwinPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class Swin2SRPreTrainedModel extends PreTrainedModel { } + +/** + * The bare Swin2SR Model transformer outputting raw hidden-states without any specific head on top. + */ +class Swin2SRModel extends Swin2SRPreTrainedModel { } + +/** + * Swin2SR Model transformer with an upsampler head on top for image super resolution and restoration. + * + * **Example:** Super-resolution w/ `Xenova/swin2SR-classical-sr-x2-64`. + * + * ```javascript + * import { AutoProcessor, Swin2SRForImageSuperResolution, RawImage } from '@huggingface/transformers'; + * + * // Load processor and model + * const model_id = 'Xenova/swin2SR-classical-sr-x2-64'; + * const processor = await AutoProcessor.from_pretrained(model_id); + * const model = await Swin2SRForImageSuperResolution.from_pretrained(model_id); + * + * // Prepare model inputs + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/butterfly.jpg'; + * const image = await RawImage.fromURL(url); + * const inputs = await processor(image); + * + * // Run model + * const outputs = await model(inputs); + * + * // Convert Tensor to RawImage + * const output = outputs.reconstruction.squeeze().clamp_(0, 1).mul_(255).round_().to('uint8'); + * const outputImage = RawImage.fromTensor(output); + * // RawImage { + * // data: Uint8Array(786432) [ 41, 31, 24, ... ], + * // width: 512, + * // height: 512, + * // channels: 3 + * // } + * ``` + */ +class Swin2SRForImageSuperResolution extends Swin2SRPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class DPTPreTrainedModel extends PreTrainedModel { } + +/** + * The bare DPT Model transformer outputting raw hidden-states without any specific head on top. + */ +class DPTModel extends DPTPreTrainedModel { } + +/** + * DPT Model with a depth estimation head on top (consisting of 3 convolutional layers) e.g. for KITTI, NYUv2. + * + * **Example:** Depth estimation w/ `Xenova/dpt-hybrid-midas`. + * ```javascript + * import { DPTForDepthEstimation, AutoProcessor, RawImage, interpolate, max } from '@huggingface/transformers'; + * + * // Load model and processor + * const model_id = 'Xenova/dpt-hybrid-midas'; + * const model = await DPTForDepthEstimation.from_pretrained(model_id); + * const processor = await AutoProcessor.from_pretrained(model_id); + * + * // Load image from URL + * const url = 'http://images.cocodataset.org/val2017/000000039769.jpg'; + * const image = await RawImage.fromURL(url); + * + * // Prepare image for the model + * const inputs = await processor(image); + * + * // Run model + * const { predicted_depth } = await model(inputs); + * + * // Interpolate to original size + * const prediction = interpolate(predicted_depth, image.size.reverse(), 'bilinear', false); + * + * // Visualize the prediction + * const formatted = prediction.mul_(255 / max(prediction.data)[0]).to('uint8'); + * const depth = RawImage.fromTensor(formatted); + * // RawImage { + * // data: Uint8Array(307200) [ 85, 85, 84, ... ], + * // width: 640, + * // height: 480, + * // channels: 1 + * // } + * ``` + */ +class DPTForDepthEstimation extends DPTPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class DepthAnythingPreTrainedModel extends PreTrainedModel { } + +/** + * Depth Anything Model with a depth estimation head on top (consisting of 3 convolutional layers) e.g. for KITTI, NYUv2. + */ +class DepthAnythingForDepthEstimation extends DepthAnythingPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +class SapiensPreTrainedModel extends PreTrainedModel { } +class SapiensForSemanticSegmentation extends SapiensPreTrainedModel { } +class SapiensForDepthEstimation extends SapiensPreTrainedModel { } +class SapiensForNormalEstimation extends SapiensPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class DepthProPreTrainedModel extends PreTrainedModel { } +class DepthProForDepthEstimation extends DepthProPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class MaskFormerPreTrainedModel extends PreTrainedModel { } +class MaskFormerModel extends MaskFormerPreTrainedModel { } +class MaskFormerForInstanceSegmentation extends MaskFormerPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class GLPNPreTrainedModel extends PreTrainedModel { } + +/** + * The bare GLPN encoder (Mix-Transformer) outputting raw hidden-states without any specific head on top. + */ +class GLPNModel extends GLPNPreTrainedModel { } + +/** + * GLPN Model transformer with a lightweight depth estimation head on top e.g. for KITTI, NYUv2. + * + * **Example:** Depth estimation w/ `Xenova/glpn-kitti`. + * ```javascript + * import { GLPNForDepthEstimation, AutoProcessor, RawImage, interpolate, max } from '@huggingface/transformers'; + * + * // Load model and processor + * const model_id = 'Xenova/glpn-kitti'; + * const model = await GLPNForDepthEstimation.from_pretrained(model_id); + * const processor = await AutoProcessor.from_pretrained(model_id); + * + * // Load image from URL + * const url = 'http://images.cocodataset.org/val2017/000000039769.jpg'; + * const image = await RawImage.fromURL(url); + * + * // Prepare image for the model + * const inputs = await processor(image); + * + * // Run model + * const { predicted_depth } = await model(inputs); + * + * // Interpolate to original size + * const prediction = interpolate(predicted_depth, image.size.reverse(), 'bilinear', false); + * + * // Visualize the prediction + * const formatted = prediction.mul_(255 / max(prediction.data)[0]).to('uint8'); + * const depth = RawImage.fromTensor(formatted); + * // RawImage { + * // data: Uint8Array(307200) [ 207, 169, 154, ... ], + * // width: 640, + * // height: 480, + * // channels: 1 + * // } + * ``` + */ +class GLPNForDepthEstimation extends GLPNPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class DonutSwinPreTrainedModel extends PreTrainedModel { } + +/** + * The bare Donut Swin Model transformer outputting raw hidden-states without any specific head on top. + * + * **Example:** Step-by-step Document Parsing. + * + * ```javascript + * import { AutoProcessor, AutoTokenizer, AutoModelForVision2Seq, RawImage } from '@huggingface/transformers'; + * + * // Choose model to use + * const model_id = 'Xenova/donut-base-finetuned-cord-v2'; + * + * // Prepare image inputs + * const processor = await AutoProcessor.from_pretrained(model_id); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/receipt.png'; + * const image = await RawImage.read(url); + * const image_inputs = await processor(image); + * + * // Prepare decoder inputs + * const tokenizer = await AutoTokenizer.from_pretrained(model_id); + * const task_prompt = ''; + * const decoder_input_ids = tokenizer(task_prompt, { + * add_special_tokens: false, + * }).input_ids; + * + * // Create the model + * const model = await AutoModelForVision2Seq.from_pretrained(model_id); + * + * // Run inference + * const output = await model.generate(image_inputs.pixel_values, { + * decoder_input_ids, + * max_length: model.config.decoder.max_position_embeddings, + * }); + * + * // Decode output + * const decoded = tokenizer.batch_decode(output)[0]; + * // CINNAMON SUGAR 17,000 1 x 17,000 17,000 17,000 20,000 3,000 + * ``` + * + * **Example:** Step-by-step Document Visual Question Answering (DocVQA) + * + * ```javascript + * import { AutoProcessor, AutoTokenizer, AutoModelForVision2Seq, RawImage } from '@huggingface/transformers'; + * + * // Choose model to use + * const model_id = 'Xenova/donut-base-finetuned-docvqa'; + * + * // Prepare image inputs + * const processor = await AutoProcessor.from_pretrained(model_id); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/invoice.png'; + * const image = await RawImage.read(url); + * const image_inputs = await processor(image); + * + * // Prepare decoder inputs + * const tokenizer = await AutoTokenizer.from_pretrained(model_id); + * const question = 'What is the invoice number?'; + * const task_prompt = `${question}`; + * const decoder_input_ids = tokenizer(task_prompt, { + * add_special_tokens: false, + * }).input_ids; + * + * // Create the model + * const model = await AutoModelForVision2Seq.from_pretrained(model_id); + * + * // Run inference + * const output = await model.generate(image_inputs.pixel_values, { + * decoder_input_ids, + * max_length: model.config.decoder.max_position_embeddings, + * }); + * + * // Decode output + * const decoded = tokenizer.batch_decode(output)[0]; + * // What is the invoice number? us-001 + * ``` + */ +class DonutSwinModel extends DonutSwinPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +class ConvNextPreTrainedModel extends PreTrainedModel { } + +/** + * The bare ConvNext model outputting raw features without any specific head on top. + */ +class ConvNextModel extends ConvNextPreTrainedModel { } + +/** + * ConvNext Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for ImageNet. + */ +class ConvNextForImageClassification extends ConvNextPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +class ConvNextV2PreTrainedModel extends PreTrainedModel { } + +/** + * The bare ConvNextV2 model outputting raw features without any specific head on top. + */ +class ConvNextV2Model extends ConvNextV2PreTrainedModel { } + +/** + * ConvNextV2 Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for ImageNet. + */ +class ConvNextV2ForImageClassification extends ConvNextV2PreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class Dinov2PreTrainedModel extends PreTrainedModel { } + +/** + * The bare DINOv2 Model transformer outputting raw hidden-states without any specific head on top. + */ +class Dinov2Model extends Dinov2PreTrainedModel { } + +/** + * Dinov2 Model transformer with an image classification head on top (a linear layer on top of the final hidden state of the [CLS] token) e.g. for ImageNet. + */ +class Dinov2ForImageClassification extends Dinov2PreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +class YolosPreTrainedModel extends PreTrainedModel { } +class YolosModel extends YolosPreTrainedModel { } +class YolosForObjectDetection extends YolosPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new YolosObjectDetectionOutput(await super._call(model_inputs)); + } +} + +class YolosObjectDetectionOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits Classification logits (including no-object) for all queries. + * @param {Tensor} output.pred_boxes Normalized boxes coordinates for all queries, represented as (center_x, center_y, width, height). + * These values are normalized in [0, 1], relative to the size of each individual image in the batch (disregarding possible padding). + */ + constructor({ logits, pred_boxes }) { + super(); + this.logits = logits; + this.pred_boxes = pred_boxes; + } +} +////////////////////////////////////////////////// + + + + +////////////////////////////////////////////////// +class SamPreTrainedModel extends PreTrainedModel { } + +/** + * Segment Anything Model (SAM) for generating segmentation masks, given an input image + * and optional 2D location and bounding boxes. + * + * **Example:** Perform mask generation w/ `Xenova/sam-vit-base`. + * ```javascript + * import { SamModel, AutoProcessor, RawImage } from '@huggingface/transformers'; + * + * const model = await SamModel.from_pretrained('Xenova/sam-vit-base'); + * const processor = await AutoProcessor.from_pretrained('Xenova/sam-vit-base'); + * + * const img_url = 'https://huggingface.co/ybelkada/segment-anything/resolve/main/assets/car.png'; + * const raw_image = await RawImage.read(img_url); + * const input_points = [[[450, 600]]] // 2D localization of a window + * + * const inputs = await processor(raw_image, { input_points }); + * const outputs = await model(inputs); + * + * const masks = await processor.post_process_masks(outputs.pred_masks, inputs.original_sizes, inputs.reshaped_input_sizes); + * // [ + * // Tensor { + * // dims: [ 1, 3, 1764, 2646 ], + * // type: 'bool', + * // data: Uint8Array(14002632) [ ... ], + * // size: 14002632 + * // } + * // ] + * const scores = outputs.iou_scores; + * // Tensor { + * // dims: [ 1, 1, 3 ], + * // type: 'float32', + * // data: Float32Array(3) [ + * // 0.8892380595207214, + * // 0.9311248064041138, + * // 0.983696699142456 + * // ], + * // size: 3 + * // } + * ``` + */ +class SamModel extends SamPreTrainedModel { + + /** + * Compute image embeddings and positional image embeddings, given the pixel values of an image. + * @param {Object} model_inputs Object containing the model inputs. + * @param {Tensor} model_inputs.pixel_values Pixel values obtained using a `SamProcessor`. + * @returns {Promise<{ image_embeddings: Tensor, image_positional_embeddings: Tensor }>} The image embeddings and positional image embeddings. + */ + async get_image_embeddings({ pixel_values }) { + // in: + // - pixel_values: tensor.float32[batch_size,3,1024,1024] + // + // out: + // - image_embeddings: tensor.float32[batch_size,256,64,64] + // - image_positional_embeddings: tensor.float32[batch_size,256,64,64] + return await encoderForward(this, { pixel_values }) + } + + /** + * @typedef {Object} SamModelInputs Object containing the model inputs. + * @property {Tensor} pixel_values Pixel values as a Tensor with shape `(batch_size, num_channels, height, width)`. + * These can be obtained using a `SamProcessor`. + * @property {Tensor} [input_points] Input 2D spatial points with shape `(batch_size, num_points, 2)`. + * This is used by the prompt encoder to encode the prompt. + * @property {Tensor} [input_labels] Input labels for the points, as a Tensor of shape `(batch_size, point_batch_size, num_points)`. + * This is used by the prompt encoder to encode the prompt. There are 4 types of labels: + * - `1`: the point is a point that contains the object of interest + * - `0`: the point is a point that does not contain the object of interest + * - `-1`: the point corresponds to the background + * - `-10`: the point is a padding point, thus should be ignored by the prompt encoder + * @property {Tensor} [input_boxes] Input bounding boxes with shape `(batch_size, num_boxes, 4)`. + * @property {Tensor} [image_embeddings] Image embeddings used by the mask decoder. + * @property {Tensor} [image_positional_embeddings] Image positional embeddings used by the mask decoder. + */ + + /** + * @param {SamModelInputs} model_inputs Object containing the model inputs. + * @returns {Promise} The output of the model. + */ + async forward(model_inputs) { + if (!model_inputs.image_embeddings || !model_inputs.image_positional_embeddings) { + // Compute the image embeddings if they are missing + model_inputs = { + ...model_inputs, + ...(await this.get_image_embeddings(model_inputs)) + } + } + + if (!model_inputs.input_labels && model_inputs.input_points) { + // Set default input labels if they are missing + const shape = model_inputs.input_points.dims.slice(0, -1); + const numElements = shape.reduce((a, b) => a * b, 1); + model_inputs.input_labels = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor( + 'int64', + new BigInt64Array(numElements).fill(1n), + shape + ); + } + + const decoder_inputs = { + image_embeddings: model_inputs.image_embeddings, + image_positional_embeddings: model_inputs.image_positional_embeddings, + }; + if (model_inputs.input_points) { + decoder_inputs.input_points = model_inputs.input_points; + } + if (model_inputs.input_labels) { + decoder_inputs.input_labels = model_inputs.input_labels; + } + if (model_inputs.input_boxes) { + decoder_inputs.input_boxes = model_inputs.input_boxes; + } + + // Returns: + // - iou_scores: tensor.float32[batch_size,point_batch_size,3] + // - pred_masks: tensor.float32[batch_size,point_batch_size,3,256,256] + return await sessionRun(this.sessions['prompt_encoder_mask_decoder'], decoder_inputs); + } + + /** + * Runs the model with the provided inputs + * @param {Object} model_inputs Model inputs + * @returns {Promise} Object containing segmentation outputs + */ + async _call(model_inputs) { + return new SamImageSegmentationOutput(await super._call(model_inputs)); + } +} + + +/** + * Base class for Segment-Anything model's output. + */ +class SamImageSegmentationOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.iou_scores The output logits of the model. + * @param {Tensor} output.pred_masks Predicted boxes. + */ + constructor({ iou_scores, pred_masks }) { + super(); + this.iou_scores = iou_scores; + this.pred_masks = pred_masks; + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// MarianMT models +class MarianPreTrainedModel extends PreTrainedModel { }; + +class MarianModel extends MarianPreTrainedModel { } + +class MarianMTModel extends MarianPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// M2M100 models +class M2M100PreTrainedModel extends PreTrainedModel { }; + +class M2M100Model extends M2M100PreTrainedModel { } + +class M2M100ForConditionalGeneration extends M2M100PreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Wav2Vec2 models +class Wav2Vec2PreTrainedModel extends PreTrainedModel { }; + +/** + * The bare Wav2Vec2 Model transformer outputting raw hidden-states without any specific head on top. + * + * **Example:** Load and run a `Wav2Vec2Model` for feature extraction. + * + * ```javascript + * import { AutoProcessor, AutoModel, read_audio } from '@huggingface/transformers'; + * + * // Read and preprocess audio + * const processor = await AutoProcessor.from_pretrained('Xenova/mms-300m'); + * const audio = await read_audio('https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac', 16000); + * const inputs = await processor(audio); + * + * // Run model with inputs + * const model = await AutoModel.from_pretrained('Xenova/mms-300m'); + * const output = await model(inputs); + * // { + * // last_hidden_state: Tensor { + * // dims: [ 1, 1144, 1024 ], + * // type: 'float32', + * // data: Float32Array(1171456) [ ... ], + * // size: 1171456 + * // } + * // } + * ``` + */ +class Wav2Vec2Model extends Wav2Vec2PreTrainedModel { } + +class Wav2Vec2ForCTC extends Wav2Vec2PreTrainedModel { + /** + * @param {Object} model_inputs + * @param {Tensor} model_inputs.input_values Float values of input raw speech waveform. + * @param {Tensor} model_inputs.attention_mask Mask to avoid performing convolution and attention on padding token indices. Mask values selected in [0, 1] + */ + async _call(model_inputs) { + return new CausalLMOutput(await super._call(model_inputs)); + } +} + +class Wav2Vec2ForSequenceClassification extends Wav2Vec2PreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * Wav2Vec2 Model with a frame classification head on top for tasks like Speaker Diarization. + */ +class Wav2Vec2ForAudioFrameClassification extends Wav2Vec2PreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// PyAnnote models +class PyAnnotePreTrainedModel extends PreTrainedModel { }; + +/** + * The bare PyAnnote Model transformer outputting raw hidden-states without any specific head on top. + */ +class PyAnnoteModel extends PyAnnotePreTrainedModel { } + +/** + * PyAnnote Model with a frame classification head on top for tasks like Speaker Diarization. + * + * **Example:** Load and run a `PyAnnoteForAudioFrameClassification` for speaker diarization. + * + * ```javascript + * import { AutoProcessor, AutoModelForAudioFrameClassification, read_audio } from '@huggingface/transformers'; + * + * // Load model and processor + * const model_id = 'onnx-community/pyannote-segmentation-3.0'; + * const model = await AutoModelForAudioFrameClassification.from_pretrained(model_id); + * const processor = await AutoProcessor.from_pretrained(model_id); + * + * // Read and preprocess audio + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/mlk.wav'; + * const audio = await read_audio(url, processor.feature_extractor.config.sampling_rate); + * const inputs = await processor(audio); + * + * // Run model with inputs + * const { logits } = await model(inputs); + * // { + * // logits: Tensor { + * // dims: [ 1, 767, 7 ], // [batch_size, num_frames, num_classes] + * // type: 'float32', + * // data: Float32Array(5369) [ ... ], + * // size: 5369 + * // } + * // } + * + * const result = processor.post_process_speaker_diarization(logits, audio.length); + * // [ + * // [ + * // { id: 0, start: 0, end: 1.0512535626298245, confidence: 0.8220156481664611 }, + * // { id: 2, start: 1.0512535626298245, end: 2.3398869619825127, confidence: 0.9008811707860472 }, + * // ... + * // ] + * // ] + * + * // Display result + * console.table(result[0], ['start', 'end', 'id', 'confidence']); + * // ┌─────────┬────────────────────┬────────────────────┬────┬─────────────────────┐ + * // │ (index) │ start │ end │ id │ confidence │ + * // ├─────────┼────────────────────┼────────────────────┼────┼─────────────────────┤ + * // │ 0 │ 0 │ 1.0512535626298245 │ 0 │ 0.8220156481664611 │ + * // │ 1 │ 1.0512535626298245 │ 2.3398869619825127 │ 2 │ 0.9008811707860472 │ + * // │ 2 │ 2.3398869619825127 │ 3.5946089560890773 │ 0 │ 0.7521651315796233 │ + * // │ 3 │ 3.5946089560890773 │ 4.578039708226655 │ 2 │ 0.8491978128022479 │ + * // │ 4 │ 4.578039708226655 │ 4.594995410849717 │ 0 │ 0.2935352600416393 │ + * // │ 5 │ 4.594995410849717 │ 6.121008646925269 │ 3 │ 0.6788051309866024 │ + * // │ 6 │ 6.121008646925269 │ 6.256654267909762 │ 0 │ 0.37125512393851134 │ + * // │ 7 │ 6.256654267909762 │ 8.630452635138397 │ 2 │ 0.7467035186353542 │ + * // │ 8 │ 8.630452635138397 │ 10.088643060721703 │ 0 │ 0.7689364814666032 │ + * // │ 9 │ 10.088643060721703 │ 12.58113134631177 │ 2 │ 0.9123324509131324 │ + * // │ 10 │ 12.58113134631177 │ 13.005023911888312 │ 0 │ 0.4828358177572041 │ + * // └─────────┴────────────────────┴────────────────────┴────┴─────────────────────┘ + * ``` + */ +class PyAnnoteForAudioFrameClassification extends PyAnnotePreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// WeSpeakerResNet models +class WeSpeakerResNetPreTrainedModel extends PreTrainedModel { }; +class WeSpeakerResNetModel extends WeSpeakerResNetPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// UniSpeech models +class UniSpeechPreTrainedModel extends PreTrainedModel { }; + +/** + * The bare UniSpeech Model transformer outputting raw hidden-states without any specific head on top. + */ +class UniSpeechModel extends UniSpeechPreTrainedModel { } + +/** + * UniSpeech Model with a `language modeling` head on top for Connectionist Temporal Classification (CTC). + */ +class UniSpeechForCTC extends UniSpeechPreTrainedModel { + /** + * @param {Object} model_inputs + * @param {Tensor} model_inputs.input_values Float values of input raw speech waveform. + * @param {Tensor} model_inputs.attention_mask Mask to avoid performing convolution and attention on padding token indices. Mask values selected in [0, 1] + */ + async _call(model_inputs) { + return new CausalLMOutput(await super._call(model_inputs)); + } +} + +/** + * UniSpeech Model with a sequence classification head on top (a linear layer over the pooled output). + */ +class UniSpeechForSequenceClassification extends UniSpeechPreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// UniSpeechSat models +class UniSpeechSatPreTrainedModel extends PreTrainedModel { }; + +/** + * The bare UniSpeechSat Model transformer outputting raw hidden-states without any specific head on top. + */ +class UniSpeechSatModel extends UniSpeechSatPreTrainedModel { } + +/** + * UniSpeechSat Model with a `language modeling` head on top for Connectionist Temporal Classification (CTC). + */ +class UniSpeechSatForCTC extends UniSpeechSatPreTrainedModel { + /** + * @param {Object} model_inputs + * @param {Tensor} model_inputs.input_values Float values of input raw speech waveform. + * @param {Tensor} model_inputs.attention_mask Mask to avoid performing convolution and attention on padding token indices. Mask values selected in [0, 1] + */ + async _call(model_inputs) { + return new CausalLMOutput(await super._call(model_inputs)); + } +} + +/** + * UniSpeechSat Model with a sequence classification head on top (a linear layer over the pooled output). + */ +class UniSpeechSatForSequenceClassification extends UniSpeechSatPreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * UniSpeechSat Model with a frame classification head on top for tasks like Speaker Diarization. + */ +class UniSpeechSatForAudioFrameClassification extends UniSpeechSatPreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Wav2Vec2Bert models +class Wav2Vec2BertPreTrainedModel extends PreTrainedModel { }; + +/** + * The bare Wav2Vec2Bert Model transformer outputting raw hidden-states without any specific head on top. + */ +class Wav2Vec2BertModel extends Wav2Vec2BertPreTrainedModel { } + +/** + * Wav2Vec2Bert Model with a `language modeling` head on top for Connectionist Temporal Classification (CTC). + */ +class Wav2Vec2BertForCTC extends Wav2Vec2BertPreTrainedModel { + /** + * @param {Object} model_inputs + * @param {Tensor} model_inputs.input_features Float values of input mel-spectrogram. + * @param {Tensor} model_inputs.attention_mask Mask to avoid performing convolution and attention on padding token indices. Mask values selected in [0, 1] + */ + async _call(model_inputs) { + return new CausalLMOutput(await super._call(model_inputs)); + } +} + +/** + * Wav2Vec2Bert Model with a sequence classification head on top (a linear layer over the pooled output). + */ +class Wav2Vec2BertForSequenceClassification extends Wav2Vec2BertPreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Hubert models +class HubertPreTrainedModel extends PreTrainedModel { } + +/** + * The bare Hubert Model transformer outputting raw hidden-states without any specific head on top. + * + * **Example:** Load and run a `HubertModel` for feature extraction. + * + * ```javascript + * import { AutoProcessor, AutoModel, read_audio } from '@huggingface/transformers'; + * + * // Read and preprocess audio + * const processor = await AutoProcessor.from_pretrained('Xenova/hubert-base-ls960'); + * const audio = await read_audio('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav', 16000); + * const inputs = await processor(audio); + * + * // Load and run model with inputs + * const model = await AutoModel.from_pretrained('Xenova/hubert-base-ls960'); + * const output = await model(inputs); + * // { + * // last_hidden_state: Tensor { + * // dims: [ 1, 549, 768 ], + * // type: 'float32', + * // data: Float32Array(421632) [0.0682469978928566, 0.08104046434164047, -0.4975186586380005, ...], + * // size: 421632 + * // } + * // } + * ``` + */ +class HubertModel extends Wav2Vec2PreTrainedModel { } + +/** + * Hubert Model with a `language modeling` head on top for Connectionist Temporal Classification (CTC). + */ +class HubertForCTC extends Wav2Vec2PreTrainedModel { + /** + * @param {Object} model_inputs + * @param {Tensor} model_inputs.input_values Float values of input raw speech waveform. + * @param {Tensor} model_inputs.attention_mask Mask to avoid performing convolution and attention on padding token indices. Mask values selected in [0, 1] + */ + async _call(model_inputs) { + return new CausalLMOutput(await super._call(model_inputs)); + } +} + +/** + * Hubert Model with a sequence classification head on top (a linear layer over the pooled output) for tasks like SUPERB Keyword Spotting. + */ +class HubertForSequenceClassification extends Wav2Vec2PreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// WavLM models +/** + * An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained models. + */ +class WavLMPreTrainedModel extends PreTrainedModel { }; + +/** + * The bare WavLM Model transformer outputting raw hidden-states without any specific head on top. + * + * **Example:** Load and run a `WavLMModel` for feature extraction. + * + * ```javascript + * import { AutoProcessor, AutoModel, read_audio } from '@huggingface/transformers'; + * + * // Read and preprocess audio + * const processor = await AutoProcessor.from_pretrained('Xenova/wavlm-base'); + * const audio = await read_audio('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav', 16000); + * const inputs = await processor(audio); + * + * // Run model with inputs + * const model = await AutoModel.from_pretrained('Xenova/wavlm-base'); + * const output = await model(inputs); + * // { + * // last_hidden_state: Tensor { + * // dims: [ 1, 549, 768 ], + * // type: 'float32', + * // data: Float32Array(421632) [-0.349443256855011, -0.39341306686401367, 0.022836603224277496, ...], + * // size: 421632 + * // } + * // } + * ``` + */ +class WavLMModel extends WavLMPreTrainedModel { } + +/** + * WavLM Model with a `language modeling` head on top for Connectionist Temporal Classification (CTC). + */ +class WavLMForCTC extends WavLMPreTrainedModel { + /** + * @param {Object} model_inputs + * @param {Tensor} model_inputs.input_values Float values of input raw speech waveform. + * @param {Tensor} model_inputs.attention_mask Mask to avoid performing convolution and attention on padding token indices. Mask values selected in [0, 1] + */ + async _call(model_inputs) { + return new CausalLMOutput(await super._call(model_inputs)); + } +} + +/** + * WavLM Model with a sequence classification head on top (a linear layer over the pooled output). + */ +class WavLMForSequenceClassification extends WavLMPreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * WavLM Model with an XVector feature extraction head on top for tasks like Speaker Verification. + * + * **Example:** Extract speaker embeddings with `WavLMForXVector`. + * ```javascript + * import { AutoProcessor, AutoModel, read_audio } from '@huggingface/transformers'; + * + * // Read and preprocess audio + * const processor = await AutoProcessor.from_pretrained('Xenova/wavlm-base-plus-sv'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav'; + * const audio = await read_audio(url, 16000); + * const inputs = await processor(audio); + * + * // Run model with inputs + * const model = await AutoModel.from_pretrained('Xenova/wavlm-base-plus-sv'); + * const outputs = await model(inputs); + * // { + * // logits: Tensor { + * // dims: [ 1, 512 ], + * // type: 'float32', + * // data: Float32Array(512) [0.5847219228744507, ...], + * // size: 512 + * // }, + * // embeddings: Tensor { + * // dims: [ 1, 512 ], + * // type: 'float32', + * // data: Float32Array(512) [-0.09079201519489288, ...], + * // size: 512 + * // } + * // } + * ``` + */ +class WavLMForXVector extends WavLMPreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits and speaker embeddings. + */ + async _call(model_inputs) { + return new XVectorOutput(await super._call(model_inputs)); + } +} + +/** + * WavLM Model with a frame classification head on top for tasks like Speaker Diarization. + * + * **Example:** Perform speaker diarization with `WavLMForAudioFrameClassification`. + * ```javascript + * import { AutoProcessor, AutoModelForAudioFrameClassification, read_audio } from '@huggingface/transformers'; + * + * // Read and preprocess audio + * const processor = await AutoProcessor.from_pretrained('Xenova/wavlm-base-plus-sd'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav'; + * const audio = await read_audio(url, 16000); + * const inputs = await processor(audio); + * + * // Run model with inputs + * const model = await AutoModelForAudioFrameClassification.from_pretrained('Xenova/wavlm-base-plus-sd'); + * const { logits } = await model(inputs); + * // { + * // logits: Tensor { + * // dims: [ 1, 549, 2 ], // [batch_size, num_frames, num_speakers] + * // type: 'float32', + * // data: Float32Array(1098) [-3.5301010608673096, ...], + * // size: 1098 + * // } + * // } + * + * const labels = logits[0].sigmoid().tolist().map( + * frames => frames.map(speaker => speaker > 0.5 ? 1 : 0) + * ); + * console.log(labels); // labels is a one-hot array of shape (num_frames, num_speakers) + * // [ + * // [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], + * // [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], + * // [0, 0], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], + * // ... + * // ] + * ``` + */ +class WavLMForAudioFrameClassification extends WavLMPreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} + +////////////////////////////////////////////////// +// SpeechT5 models +/** + * An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained models. + */ +class SpeechT5PreTrainedModel extends PreTrainedModel { }; + +/** + * The bare SpeechT5 Encoder-Decoder Model outputting raw hidden-states without any specific pre- or post-nets. + */ +class SpeechT5Model extends SpeechT5PreTrainedModel { }; + +/** + * SpeechT5 Model with a speech encoder and a text decoder. + * + * **Example:** Generate speech from text with `SpeechT5ForSpeechToText`. + * ```javascript + * import { AutoTokenizer, AutoProcessor, SpeechT5ForTextToSpeech, SpeechT5HifiGan, Tensor } from '@huggingface/transformers'; + * + * // Load the tokenizer and processor + * const tokenizer = await AutoTokenizer.from_pretrained('Xenova/speecht5_tts'); + * const processor = await AutoProcessor.from_pretrained('Xenova/speecht5_tts'); + * + * // Load the models + * // NOTE: We use the full-precision versions as they are more accurate + * const model = await SpeechT5ForTextToSpeech.from_pretrained('Xenova/speecht5_tts', { dtype: 'fp32' }); + * const vocoder = await SpeechT5HifiGan.from_pretrained('Xenova/speecht5_hifigan', { dtype: 'fp32' }); + * + * // Load speaker embeddings from URL + * const speaker_embeddings_data = new Float32Array( + * await (await fetch('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/speaker_embeddings.bin')).arrayBuffer() + * ); + * const speaker_embeddings = new Tensor( + * 'float32', + * speaker_embeddings_data, + * [1, speaker_embeddings_data.length] + * ) + * + * // Run tokenization + * const { input_ids } = tokenizer('Hello, my dog is cute'); + * + * // Generate waveform + * const { waveform } = await model.generate_speech(input_ids, speaker_embeddings, { vocoder }); + * console.log(waveform) + * // Tensor { + * // dims: [ 26112 ], + * // type: 'float32', + * // size: 26112, + * // data: Float32Array(26112) [ -0.00043630177970044315, -0.00018082228780258447, ... ], + * // } + * ``` + */ +class SpeechT5ForSpeechToText extends SpeechT5PreTrainedModel { } + +/** + * SpeechT5 Model with a text encoder and a speech decoder. + */ +class SpeechT5ForTextToSpeech extends SpeechT5PreTrainedModel { + + /** + * @typedef {Object} SpeechOutput + * @property {Tensor} [spectrogram] The predicted log-mel spectrogram of shape + * `(output_sequence_length, config.num_mel_bins)`. Returned when no `vocoder` is provided + * @property {Tensor} [waveform] The predicted waveform of shape `(num_frames,)`. Returned when a `vocoder` is provided. + * @property {Tensor} [cross_attentions] The outputs of the decoder's cross-attention layers of shape + * `(config.decoder_layers, config.decoder_attention_heads, output_sequence_length, input_sequence_length)`. returned when `output_cross_attentions` is `true`. + */ + + /** + * Converts a sequence of input tokens into a sequence of mel spectrograms, which are subsequently turned into a speech waveform using a vocoder. + * @param {Tensor} input_values Indices of input sequence tokens in the vocabulary. + * @param {Tensor} speaker_embeddings Tensor containing the speaker embeddings. + * @param {Object} options Optional parameters for generating speech. + * @param {number} [options.threshold=0.5] The generated sequence ends when the predicted stop token probability exceeds this value. + * @param {number} [options.minlenratio=0.0] Used to calculate the minimum required length for the output sequence. + * @param {number} [options.maxlenratio=20.0] Used to calculate the maximum allowed length for the output sequence. + * @param {Object} [options.vocoder=null] The vocoder that converts the mel spectrogram into a speech waveform. If `null`, the output is the mel spectrogram. + * @param {boolean} [options.output_cross_attentions=false] Whether or not to return the attentions tensors of the decoder's cross-attention layers. + * @returns {Promise} A promise which resolves to an object containing the spectrogram, waveform, and cross-attention tensors. + */ + async generate_speech(input_values, speaker_embeddings, { + threshold = 0.5, + minlenratio = 0.0, + maxlenratio = 20.0, + vocoder = null, + // output_cross_attentions = false, // TODO add + } = {}) { + + const model_inputs = { + input_ids: input_values + } + + const { encoder_outputs, encoder_attention_mask } = await encoderForward(this, model_inputs); + + const r = encoder_outputs.dims[1] / this.config.reduction_factor; + const maxlen = Math.floor(r * maxlenratio); + const minlen = Math.floor(r * minlenratio); + + const num_mel_bins = this.config.num_mel_bins; + + let spectrogramParts = []; + let past_key_values = null; + let decoder_outputs = null; + let idx = 0; + + while (true) { + ++idx; + + const use_cache_branch = boolTensor(!!decoder_outputs); + let output_sequence; + if (decoder_outputs) { + output_sequence = decoder_outputs.output_sequence_out; + } else { + output_sequence = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor( + 'float32', + new Float32Array(num_mel_bins), + [1, 1, num_mel_bins], + ) + } + let decoderFeeds = { + use_cache_branch, + output_sequence, + encoder_attention_mask: encoder_attention_mask, + speaker_embeddings: speaker_embeddings, + encoder_hidden_states: encoder_outputs, + }; + + this.addPastKeyValues(decoderFeeds, past_key_values); + decoder_outputs = await sessionRun(this.sessions['decoder_model_merged'], decoderFeeds); + past_key_values = this.getPastKeyValues(decoder_outputs, past_key_values); + + const { prob, spectrum } = decoder_outputs; + spectrogramParts.push(spectrum); + + if (idx >= minlen && ( + // Finished when stop token or maximum length is reached. + Array.from(prob.data).filter(p => p >= threshold).length > 0 || idx >= maxlen + )) { + break; + } + } + + const spectrogram = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)(spectrogramParts); + const { waveform } = await sessionRun(vocoder.sessions['model'], { spectrogram }); + + return { + spectrogram, + waveform, + // cross_attentions: null, // TODO add + } + } +} + +/** + * HiFi-GAN vocoder. + * + * See [SpeechT5ForSpeechToText](./models#module_models.SpeechT5ForSpeechToText) for example usage. + */ +class SpeechT5HifiGan extends PreTrainedModel { + main_input_name = 'spectrogram'; +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// TrOCR models +class TrOCRPreTrainedModel extends PreTrainedModel { } + +/** + * The TrOCR Decoder with a language modeling head. + */ +class TrOCRForCausalLM extends TrOCRPreTrainedModel { } + +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Mistral models +/** + * The bare Mistral Model outputting raw hidden-states without any specific head on top. + */ +class MistralPreTrainedModel extends PreTrainedModel { } + +class MistralModel extends MistralPreTrainedModel { } + +class MistralForCausalLM extends MistralPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Starcoder2 models +/** + * The bare Starcoder2 Model outputting raw hidden-states without any specific head on top. + */ +class Starcoder2PreTrainedModel extends PreTrainedModel { } + +class Starcoder2Model extends Starcoder2PreTrainedModel { } + +class Starcoder2ForCausalLM extends Starcoder2PreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Falcon models +/** + * The bare Falcon Model outputting raw hidden-states without any specific head on top. + */ +class FalconPreTrainedModel extends PreTrainedModel { } + +class FalconModel extends FalconPreTrainedModel { } + +class FalconForCausalLM extends FalconPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// CLAP models +class ClapPreTrainedModel extends PreTrainedModel { } + +class ClapModel extends ClapPreTrainedModel { } + +/** + * CLAP Text Model with a projection layer on top (a linear layer on top of the pooled output). + * + * **Example:** Compute text embeddings with `ClapTextModelWithProjection`. + * + * ```javascript + * import { AutoTokenizer, ClapTextModelWithProjection } from '@huggingface/transformers'; + * + * // Load tokenizer and text model + * const tokenizer = await AutoTokenizer.from_pretrained('Xenova/clap-htsat-unfused'); + * const text_model = await ClapTextModelWithProjection.from_pretrained('Xenova/clap-htsat-unfused'); + * + * // Run tokenization + * const texts = ['a sound of a cat', 'a sound of a dog']; + * const text_inputs = tokenizer(texts, { padding: true, truncation: true }); + * + * // Compute embeddings + * const { text_embeds } = await text_model(text_inputs); + * // Tensor { + * // dims: [ 2, 512 ], + * // type: 'float32', + * // data: Float32Array(1024) [ ... ], + * // size: 1024 + * // } + * ``` + */ +class ClapTextModelWithProjection extends ClapPreTrainedModel { + + /** @type {PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options = {}) { + // Update default model file name if not provided + options.model_file_name ??= 'text_model'; + return super.from_pretrained(pretrained_model_name_or_path, options); + } +} + +/** + * CLAP Audio Model with a projection layer on top (a linear layer on top of the pooled output). + * + * **Example:** Compute audio embeddings with `ClapAudioModelWithProjection`. + * + * ```javascript + * import { AutoProcessor, ClapAudioModelWithProjection, read_audio } from '@huggingface/transformers'; + * + * // Load processor and audio model + * const processor = await AutoProcessor.from_pretrained('Xenova/clap-htsat-unfused'); + * const audio_model = await ClapAudioModelWithProjection.from_pretrained('Xenova/clap-htsat-unfused'); + * + * // Read audio and run processor + * const audio = await read_audio('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/cat_meow.wav'); + * const audio_inputs = await processor(audio); + * + * // Compute embeddings + * const { audio_embeds } = await audio_model(audio_inputs); + * // Tensor { + * // dims: [ 1, 512 ], + * // type: 'float32', + * // data: Float32Array(512) [ ... ], + * // size: 512 + * // } + * ``` + */ +class ClapAudioModelWithProjection extends ClapPreTrainedModel { + /** @type {PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options = {}) { + // Update default model file name if not provided + options.model_file_name ??= 'audio_model'; + return super.from_pretrained(pretrained_model_name_or_path, options); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// VITS models +class VitsPreTrainedModel extends PreTrainedModel { } + +/** + * The complete VITS model, for text-to-speech synthesis. + * + * **Example:** Generate speech from text with `VitsModel`. + * ```javascript + * import { AutoTokenizer, VitsModel } from '@huggingface/transformers'; + * + * // Load the tokenizer and model + * const tokenizer = await AutoTokenizer.from_pretrained('Xenova/mms-tts-eng'); + * const model = await VitsModel.from_pretrained('Xenova/mms-tts-eng'); + * + * // Run tokenization + * const inputs = tokenizer('I love transformers'); + * + * // Generate waveform + * const { waveform } = await model(inputs); + * // Tensor { + * // dims: [ 1, 35328 ], + * // type: 'float32', + * // data: Float32Array(35328) [ ... ], + * // size: 35328, + * // } + * ``` + */ +class VitsModel extends VitsPreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} The outputs for the VITS model. + */ + async _call(model_inputs) { + return new VitsModelOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Segformer models +class SegformerPreTrainedModel extends PreTrainedModel { } + +/** + * The bare SegFormer encoder (Mix-Transformer) outputting raw hidden-states without any specific head on top. + */ +class SegformerModel extends SegformerPreTrainedModel { } + +/** + * SegFormer Model transformer with an image classification head on top (a linear layer on top of the final hidden states) e.g. for ImageNet. + */ +class SegformerForImageClassification extends SegformerPreTrainedModel { } + +/** + * SegFormer Model transformer with an all-MLP decode head on top e.g. for ADE20k, CityScapes. + */ +class SegformerForSemanticSegmentation extends SegformerPreTrainedModel { } + +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// StableLm models +class StableLmPreTrainedModel extends PreTrainedModel { } + +/** + * The bare StableLm Model transformer outputting raw hidden-states without any specific head on top. + */ +class StableLmModel extends StableLmPreTrainedModel { } + +/** + * StableLm Model with a `language modeling` head on top for Causal Language Modeling (with past). + */ +class StableLmForCausalLM extends StableLmPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +class EfficientNetPreTrainedModel extends PreTrainedModel { } + +/** + * The bare EfficientNet model outputting raw features without any specific head on top. + */ +class EfficientNetModel extends EfficientNetPreTrainedModel { } + +/** + * EfficientNet Model with an image classification head on top (a linear layer on top of the pooled features). + */ +class EfficientNetForImageClassification extends EfficientNetPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Musicgen models +class MusicgenPreTrainedModel extends PreTrainedModel { } + +/** + * The bare Musicgen decoder model outputting raw hidden-states without any specific head on top. + */ +class MusicgenModel extends MusicgenPreTrainedModel { } + +/** + * The MusicGen decoder model with a language modelling head on top. + */ +class MusicgenForCausalLM extends MusicgenPreTrainedModel { } + +/** + * The composite MusicGen model with a text encoder, audio encoder and Musicgen decoder, + * for music generation tasks with one or both of text and audio prompts. + * + * **Example:** Generate music from text with `Xenova/musicgen-small`. + * ```javascript + * import { AutoTokenizer, MusicgenForConditionalGeneration } from '@huggingface/transformers'; + * + * // Load tokenizer and model + * const tokenizer = await AutoTokenizer.from_pretrained('Xenova/musicgen-small'); + * const model = await MusicgenForConditionalGeneration.from_pretrained( + * 'Xenova/musicgen-small', { dtype: 'fp32' } + * ); + * + * // Prepare text input + * const prompt = '80s pop track with bassy drums and synth'; + * const inputs = tokenizer(prompt); + * + * // Generate audio + * const audio_values = await model.generate({ + * ...inputs, + * max_new_tokens: 512, + * do_sample: true, + * guidance_scale: 3, + * }); + * + * // (Optional) Write the output to a WAV file + * import wavefile from 'wavefile'; + * import fs from 'fs'; + * + * const wav = new wavefile.WaveFile(); + * wav.fromScratch(1, model.config.audio_encoder.sampling_rate, '32f', audio_values.data); + * fs.writeFileSync('musicgen_out.wav', wav.toBuffer()); + * ``` + */ +class MusicgenForConditionalGeneration extends PreTrainedModel { // NOTE: not MusicgenPreTrainedModel + forward_params = [ + 'input_ids', + 'attention_mask', + 'encoder_outputs', + 'decoder_input_ids', + 'decoder_attention_mask', + 'past_key_values', + ]; + + /** + * Apply the pattern mask to the final ids, + * then revert the pattern delay mask by filtering the pad token id in a single step. + * @param {Tensor} outputs The output tensor from the model. + * @returns {Tensor} The filtered output tensor. + */ + _apply_and_filter_by_delay_pattern_mask(outputs) { + const [bs_x_codebooks, seqLength] = outputs.dims; + const num_codebooks = this.config.decoder.num_codebooks; + const upperBound = (seqLength - num_codebooks); + + let newDataSize = 0; + for (let i = 0; i < outputs.size; ++i) { + if (outputs.data[i] === this.config.decoder.pad_token_id) { + continue; + } + + const row = (i % seqLength); + const col = Math.floor(i / seqLength) % num_codebooks; + + const diff = row - col; + if (diff > 0 && diff <= upperBound) { + outputs.data[newDataSize++] = outputs.data[i]; + } + } + + const batch_size = Math.floor(bs_x_codebooks / num_codebooks); + const inferred = newDataSize / (batch_size * num_codebooks); + // TODO: assert `inferred` is an integer + return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor( + outputs.type, + outputs.data.slice(0, newDataSize), + [batch_size, num_codebooks, inferred] + ); + } + + + prepare_inputs_for_generation(input_ids, model_inputs, generation_config) { + // apply the delay pattern mask + let clonedInputIds = structuredClone(input_ids); + for (let i = 0; i < clonedInputIds.length; ++i) { + for (let j = 0; j < clonedInputIds[i].length; ++j) { + if ((i % this.config.decoder.num_codebooks) >= j) { + clonedInputIds[i][j] = BigInt(this.config.decoder.pad_token_id); + } + } + } + // for classifier free guidance we need to replicate the decoder args across the batch dim + // (we'll split these before sampling) + if (generation_config.guidance_scale !== null && generation_config.guidance_scale > 1) { + // [batch, seqLength] -> [2 * batch, seqLength] + clonedInputIds = clonedInputIds.concat(clonedInputIds); + } + + const prepped = super.prepare_inputs_for_generation(clonedInputIds, model_inputs, generation_config); + return prepped; + } + + /** + * Generates sequences of token ids for models with a language modeling head. + * @param {import('./generation/parameters.js').GenerationFunctionParameters} options + * @returns {Promise} The output of the model, which can contain the generated token ids, attentions, and scores. + */ + async generate(options) { + + const output_ids = await super.generate(options); + + // apply the pattern mask to the final ids + // tensor: int64[1,batch_size,4,chunk_length] + const audio_codes = this._apply_and_filter_by_delay_pattern_mask( + /** @type {Tensor} */(output_ids) + ).unsqueeze_(0); // append the frame dimension back to the audio codes + + const { audio_values } = await sessionRun(this.sessions['encodec_decode'], { audio_codes }) + + return audio_values; + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// MobileNetV1 models +class MobileNetV1PreTrainedModel extends PreTrainedModel { } + +/** + * The bare MobileNetV1 model outputting raw hidden-states without any specific head on top. + */ +class MobileNetV1Model extends MobileNetV1PreTrainedModel { } + +/** + * MobileNetV1 model with an image classification head on top (a linear layer on top of the pooled features), + * e.g. for ImageNet. + */ +class MobileNetV1ForImageClassification extends MobileNetV1PreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// MobileNetV2 models +class MobileNetV2PreTrainedModel extends PreTrainedModel { } + +/** + * The bare MobileNetV2 model outputting raw hidden-states without any specific head on top. + */ +class MobileNetV2Model extends MobileNetV2PreTrainedModel { } + +/** + * MobileNetV2 model with an image classification head on top (a linear layer on top of the pooled features), + * e.g. for ImageNet. + */ +class MobileNetV2ForImageClassification extends MobileNetV2PreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// MobileNetV3 models +class MobileNetV3PreTrainedModel extends PreTrainedModel { } + +/** + * The bare MobileNetV3 model outputting raw hidden-states without any specific head on top. + */ +class MobileNetV3Model extends MobileNetV3PreTrainedModel { } + +/** + * MobileNetV3 model with an image classification head on top (a linear layer on top of the pooled features), + * e.g. for ImageNet. + */ +class MobileNetV3ForImageClassification extends MobileNetV3PreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// MobileNetV4 models +class MobileNetV4PreTrainedModel extends PreTrainedModel { } + +/** + * The bare MobileNetV4 model outputting raw hidden-states without any specific head on top. + */ +class MobileNetV4Model extends MobileNetV4PreTrainedModel { } + +/** + * MobileNetV4 model with an image classification head on top (a linear layer on top of the pooled features), + * e.g. for ImageNet. + */ +class MobileNetV4ForImageClassification extends MobileNetV4PreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Decision Transformer models +class DecisionTransformerPreTrainedModel extends PreTrainedModel { } + +/** + * The model builds upon the GPT2 architecture to perform autoregressive prediction of actions in an offline RL setting. + * Refer to the paper for more details: https://arxiv.org/abs/2106.01345 + */ +class DecisionTransformerModel extends DecisionTransformerPreTrainedModel { } + +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// AutoModels, used to simplify construction of PreTrainedModels +// (uses config to instantiate correct class) + +/** + * Base class of all AutoModels. Contains the `from_pretrained` function + * which is used to instantiate pretrained models. + */ +class PretrainedMixin { + /** + * Mapping from model type to model class. + * @type {Map[]} + */ + static MODEL_CLASS_MAPPINGS = null; + + /** + * Whether to attempt to instantiate the base class (`PretrainedModel`) if + * the model type is not found in the mapping. + */ + static BASE_IF_FAIL = false; + + + /** @type {typeof PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, { + progress_callback = null, + config = null, + cache_dir = null, + local_files_only = false, + revision = 'main', + model_file_name = null, + subfolder = 'onnx', + device = null, + dtype = null, + use_external_data_format = null, + session_options = {}, + } = {}) { + + const options = { + progress_callback, + config, + cache_dir, + local_files_only, + revision, + model_file_name, + subfolder, + device, + dtype, + use_external_data_format, + session_options, + } + options.config = await _configs_js__WEBPACK_IMPORTED_MODULE_0__.AutoConfig.from_pretrained(pretrained_model_name_or_path, options); + + if (!this.MODEL_CLASS_MAPPINGS) { + throw new Error("`MODEL_CLASS_MAPPINGS` not implemented for this type of `AutoClass`: " + this.name); + } + + for (const MODEL_CLASS_MAPPING of this.MODEL_CLASS_MAPPINGS) { + const modelInfo = MODEL_CLASS_MAPPING.get(options.config.model_type); + if (!modelInfo) { + continue; // Item not found in this mapping + } + return await modelInfo[1].from_pretrained(pretrained_model_name_or_path, options); + } + + if (this.BASE_IF_FAIL) { + console.warn(`Unknown model class "${options.config.model_type}", attempting to construct from base class.`); + return await PreTrainedModel.from_pretrained(pretrained_model_name_or_path, options); + } else { + throw Error(`Unsupported model type: ${options.config.model_type}`) + } + } +} + +const MODEL_MAPPING_NAMES_ENCODER_ONLY = new Map([ + ['bert', ['BertModel', BertModel]], + ['nomic_bert', ['NomicBertModel', NomicBertModel]], + ['roformer', ['RoFormerModel', RoFormerModel]], + ['electra', ['ElectraModel', ElectraModel]], + ['esm', ['EsmModel', EsmModel]], + ['convbert', ['ConvBertModel', ConvBertModel]], + ['camembert', ['CamembertModel', CamembertModel]], + ['deberta', ['DebertaModel', DebertaModel]], + ['deberta-v2', ['DebertaV2Model', DebertaV2Model]], + ['mpnet', ['MPNetModel', MPNetModel]], + ['albert', ['AlbertModel', AlbertModel]], + ['distilbert', ['DistilBertModel', DistilBertModel]], + ['roberta', ['RobertaModel', RobertaModel]], + ['xlm', ['XLMModel', XLMModel]], + ['xlm-roberta', ['XLMRobertaModel', XLMRobertaModel]], + ['clap', ['ClapModel', ClapModel]], + ['clip', ['CLIPModel', CLIPModel]], + ['clipseg', ['CLIPSegModel', CLIPSegModel]], + ['chinese_clip', ['ChineseCLIPModel', ChineseCLIPModel]], + ['siglip', ['SiglipModel', SiglipModel]], + ['mobilebert', ['MobileBertModel', MobileBertModel]], + ['squeezebert', ['SqueezeBertModel', SqueezeBertModel]], + ['wav2vec2', ['Wav2Vec2Model', Wav2Vec2Model]], + ['wav2vec2-bert', ['Wav2Vec2BertModel', Wav2Vec2BertModel]], + ['unispeech', ['UniSpeechModel', UniSpeechModel]], + ['unispeech-sat', ['UniSpeechSatModel', UniSpeechSatModel]], + ['hubert', ['HubertModel', HubertModel]], + ['wavlm', ['WavLMModel', WavLMModel]], + ['audio-spectrogram-transformer', ['ASTModel', ASTModel]], + ['vits', ['VitsModel', VitsModel]], + ['pyannote', ['PyAnnoteModel', PyAnnoteModel]], + ['wespeaker-resnet', ['WeSpeakerResNetModel', WeSpeakerResNetModel]], + + ['detr', ['DetrModel', DetrModel]], + ['rt_detr', ['RTDetrModel', RTDetrModel]], + ['table-transformer', ['TableTransformerModel', TableTransformerModel]], + ['vit', ['ViTModel', ViTModel]], + ['pvt', ['PvtModel', PvtModel]], + ['vit_msn', ['ViTMSNModel', ViTMSNModel]], + ['vit_mae', ['ViTMAEModel', ViTMAEModel]], + ['groupvit', ['GroupViTModel', GroupViTModel]], + ['fastvit', ['FastViTModel', FastViTModel]], + ['mobilevit', ['MobileViTModel', MobileViTModel]], + ['mobilevitv2', ['MobileViTV2Model', MobileViTV2Model]], + ['owlvit', ['OwlViTModel', OwlViTModel]], + ['owlv2', ['Owlv2Model', Owlv2Model]], + ['beit', ['BeitModel', BeitModel]], + ['deit', ['DeiTModel', DeiTModel]], + ['hiera', ['HieraModel', HieraModel]], + ['convnext', ['ConvNextModel', ConvNextModel]], + ['convnextv2', ['ConvNextV2Model', ConvNextV2Model]], + ['dinov2', ['Dinov2Model', Dinov2Model]], + ['resnet', ['ResNetModel', ResNetModel]], + ['swin', ['SwinModel', SwinModel]], + ['swin2sr', ['Swin2SRModel', Swin2SRModel]], + ['donut-swin', ['DonutSwinModel', DonutSwinModel]], + ['yolos', ['YolosModel', YolosModel]], + ['dpt', ['DPTModel', DPTModel]], + ['glpn', ['GLPNModel', GLPNModel]], + + ['hifigan', ['SpeechT5HifiGan', SpeechT5HifiGan]], + ['efficientnet', ['EfficientNetModel', EfficientNetModel]], + + ['decision_transformer', ['DecisionTransformerModel', DecisionTransformerModel]], + + ['mobilenet_v1', ['MobileNetV1Model', MobileNetV1Model]], + ['mobilenet_v2', ['MobileNetV2Model', MobileNetV2Model]], + ['mobilenet_v3', ['MobileNetV3Model', MobileNetV3Model]], + ['mobilenet_v4', ['MobileNetV4Model', MobileNetV4Model]], + + ['maskformer', ['MaskFormerModel', MaskFormerModel]], +]); + +const MODEL_MAPPING_NAMES_ENCODER_DECODER = new Map([ + ['t5', ['T5Model', T5Model]], + ['longt5', ['LongT5Model', LongT5Model]], + ['mt5', ['MT5Model', MT5Model]], + ['bart', ['BartModel', BartModel]], + ['mbart', ['MBartModel', MBartModel]], + ['marian', ['MarianModel', MarianModel]], + ['whisper', ['WhisperModel', WhisperModel]], + ['m2m_100', ['M2M100Model', M2M100Model]], + ['blenderbot', ['BlenderbotModel', BlenderbotModel]], + ['blenderbot-small', ['BlenderbotSmallModel', BlenderbotSmallModel]], +]); + + +const MODEL_MAPPING_NAMES_DECODER_ONLY = new Map([ + ['bloom', ['BloomModel', BloomModel]], + ['jais', ['JAISModel', JAISModel]], + ['gpt2', ['GPT2Model', GPT2Model]], + ['gptj', ['GPTJModel', GPTJModel]], + ['gpt_bigcode', ['GPTBigCodeModel', GPTBigCodeModel]], + ['gpt_neo', ['GPTNeoModel', GPTNeoModel]], + ['gpt_neox', ['GPTNeoXModel', GPTNeoXModel]], + ['codegen', ['CodeGenModel', CodeGenModel]], + ['llama', ['LlamaModel', LlamaModel]], + ['olmo', ['OlmoModel', OlmoModel]], + ['mobilellm', ['MobileLLMModel', MobileLLMModel]], + ['granite', ['GraniteModel', GraniteModel]], + ['cohere', ['CohereModel', CohereModel]], + ['gemma', ['GemmaModel', GemmaModel]], + ['gemma2', ['Gemma2Model', Gemma2Model]], + ['openelm', ['OpenELMModel', OpenELMModel]], + ['qwen2', ['Qwen2Model', Qwen2Model]], + ['phi', ['PhiModel', PhiModel]], + ['phi3', ['Phi3Model', Phi3Model]], + ['mpt', ['MptModel', MptModel]], + ['opt', ['OPTModel', OPTModel]], + ['mistral', ['MistralModel', MistralModel]], + ['starcoder2', ['Starcoder2Model', Starcoder2Model]], + ['falcon', ['FalconModel', FalconModel]], + ['stablelm', ['StableLmModel', StableLmModel]], +]); + +const MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES = new Map([ + ['speecht5', ['SpeechT5ForSpeechToText', SpeechT5ForSpeechToText]], + ['whisper', ['WhisperForConditionalGeneration', WhisperForConditionalGeneration]], +]); + +const MODEL_FOR_TEXT_TO_SPECTROGRAM_MAPPING_NAMES = new Map([ + ['speecht5', ['SpeechT5ForTextToSpeech', SpeechT5ForTextToSpeech]], +]); + +const MODEL_FOR_TEXT_TO_WAVEFORM_MAPPING_NAMES = new Map([ + ['vits', ['VitsModel', VitsModel]], + ['musicgen', ['MusicgenForConditionalGeneration', MusicgenForConditionalGeneration]], +]); + +const MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES = new Map([ + ['bert', ['BertForSequenceClassification', BertForSequenceClassification]], + ['roformer', ['RoFormerForSequenceClassification', RoFormerForSequenceClassification]], + ['electra', ['ElectraForSequenceClassification', ElectraForSequenceClassification]], + ['esm', ['EsmForSequenceClassification', EsmForSequenceClassification]], + ['convbert', ['ConvBertForSequenceClassification', ConvBertForSequenceClassification]], + ['camembert', ['CamembertForSequenceClassification', CamembertForSequenceClassification]], + ['deberta', ['DebertaForSequenceClassification', DebertaForSequenceClassification]], + ['deberta-v2', ['DebertaV2ForSequenceClassification', DebertaV2ForSequenceClassification]], + ['mpnet', ['MPNetForSequenceClassification', MPNetForSequenceClassification]], + ['albert', ['AlbertForSequenceClassification', AlbertForSequenceClassification]], + ['distilbert', ['DistilBertForSequenceClassification', DistilBertForSequenceClassification]], + ['roberta', ['RobertaForSequenceClassification', RobertaForSequenceClassification]], + ['xlm', ['XLMForSequenceClassification', XLMForSequenceClassification]], + ['xlm-roberta', ['XLMRobertaForSequenceClassification', XLMRobertaForSequenceClassification]], + ['bart', ['BartForSequenceClassification', BartForSequenceClassification]], + ['mbart', ['MBartForSequenceClassification', MBartForSequenceClassification]], + ['mobilebert', ['MobileBertForSequenceClassification', MobileBertForSequenceClassification]], + ['squeezebert', ['SqueezeBertForSequenceClassification', SqueezeBertForSequenceClassification]], +]); + +const MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES = new Map([ + ['bert', ['BertForTokenClassification', BertForTokenClassification]], + ['roformer', ['RoFormerForTokenClassification', RoFormerForTokenClassification]], + ['electra', ['ElectraForTokenClassification', ElectraForTokenClassification]], + ['esm', ['EsmForTokenClassification', EsmForTokenClassification]], + ['convbert', ['ConvBertForTokenClassification', ConvBertForTokenClassification]], + ['camembert', ['CamembertForTokenClassification', CamembertForTokenClassification]], + ['deberta', ['DebertaForTokenClassification', DebertaForTokenClassification]], + ['deberta-v2', ['DebertaV2ForTokenClassification', DebertaV2ForTokenClassification]], + ['mpnet', ['MPNetForTokenClassification', MPNetForTokenClassification]], + ['distilbert', ['DistilBertForTokenClassification', DistilBertForTokenClassification]], + ['roberta', ['RobertaForTokenClassification', RobertaForTokenClassification]], + ['xlm', ['XLMForTokenClassification', XLMForTokenClassification]], + ['xlm-roberta', ['XLMRobertaForTokenClassification', XLMRobertaForTokenClassification]], +]); + +const MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES = new Map([ + ['t5', ['T5ForConditionalGeneration', T5ForConditionalGeneration]], + ['longt5', ['LongT5ForConditionalGeneration', LongT5ForConditionalGeneration]], + ['mt5', ['MT5ForConditionalGeneration', MT5ForConditionalGeneration]], + ['bart', ['BartForConditionalGeneration', BartForConditionalGeneration]], + ['mbart', ['MBartForConditionalGeneration', MBartForConditionalGeneration]], + ['marian', ['MarianMTModel', MarianMTModel]], + ['m2m_100', ['M2M100ForConditionalGeneration', M2M100ForConditionalGeneration]], + ['blenderbot', ['BlenderbotForConditionalGeneration', BlenderbotForConditionalGeneration]], + ['blenderbot-small', ['BlenderbotSmallForConditionalGeneration', BlenderbotSmallForConditionalGeneration]], +]); + +const MODEL_FOR_CAUSAL_LM_MAPPING_NAMES = new Map([ + ['bloom', ['BloomForCausalLM', BloomForCausalLM]], + ['gpt2', ['GPT2LMHeadModel', GPT2LMHeadModel]], + ['jais', ['JAISLMHeadModel', JAISLMHeadModel]], + ['gptj', ['GPTJForCausalLM', GPTJForCausalLM]], + ['gpt_bigcode', ['GPTBigCodeForCausalLM', GPTBigCodeForCausalLM]], + ['gpt_neo', ['GPTNeoForCausalLM', GPTNeoForCausalLM]], + ['gpt_neox', ['GPTNeoXForCausalLM', GPTNeoXForCausalLM]], + ['codegen', ['CodeGenForCausalLM', CodeGenForCausalLM]], + ['llama', ['LlamaForCausalLM', LlamaForCausalLM]], + ['olmo', ['OlmoForCausalLM', OlmoForCausalLM]], + ['mobilellm', ['MobileLLMForCausalLM', MobileLLMForCausalLM]], + ['granite', ['GraniteForCausalLM', GraniteForCausalLM]], + ['cohere', ['CohereForCausalLM', CohereForCausalLM]], + ['gemma', ['GemmaForCausalLM', GemmaForCausalLM]], + ['gemma2', ['Gemma2ForCausalLM', Gemma2ForCausalLM]], + ['openelm', ['OpenELMForCausalLM', OpenELMForCausalLM]], + ['qwen2', ['Qwen2ForCausalLM', Qwen2ForCausalLM]], + ['phi', ['PhiForCausalLM', PhiForCausalLM]], + ['phi3', ['Phi3ForCausalLM', Phi3ForCausalLM]], + ['mpt', ['MptForCausalLM', MptForCausalLM]], + ['opt', ['OPTForCausalLM', OPTForCausalLM]], + ['mbart', ['MBartForCausalLM', MBartForCausalLM]], + ['mistral', ['MistralForCausalLM', MistralForCausalLM]], + ['starcoder2', ['Starcoder2ForCausalLM', Starcoder2ForCausalLM]], + ['falcon', ['FalconForCausalLM', FalconForCausalLM]], + ['trocr', ['TrOCRForCausalLM', TrOCRForCausalLM]], + ['stablelm', ['StableLmForCausalLM', StableLmForCausalLM]], +]); + +const MODEL_FOR_MASKED_LM_MAPPING_NAMES = new Map([ + ['bert', ['BertForMaskedLM', BertForMaskedLM]], + ['roformer', ['RoFormerForMaskedLM', RoFormerForMaskedLM]], + ['electra', ['ElectraForMaskedLM', ElectraForMaskedLM]], + ['esm', ['EsmForMaskedLM', EsmForMaskedLM]], + ['convbert', ['ConvBertForMaskedLM', ConvBertForMaskedLM]], + ['camembert', ['CamembertForMaskedLM', CamembertForMaskedLM]], + ['deberta', ['DebertaForMaskedLM', DebertaForMaskedLM]], + ['deberta-v2', ['DebertaV2ForMaskedLM', DebertaV2ForMaskedLM]], + ['mpnet', ['MPNetForMaskedLM', MPNetForMaskedLM]], + ['albert', ['AlbertForMaskedLM', AlbertForMaskedLM]], + ['distilbert', ['DistilBertForMaskedLM', DistilBertForMaskedLM]], + ['roberta', ['RobertaForMaskedLM', RobertaForMaskedLM]], + ['xlm', ['XLMWithLMHeadModel', XLMWithLMHeadModel]], + ['xlm-roberta', ['XLMRobertaForMaskedLM', XLMRobertaForMaskedLM]], + ['mobilebert', ['MobileBertForMaskedLM', MobileBertForMaskedLM]], + ['squeezebert', ['SqueezeBertForMaskedLM', SqueezeBertForMaskedLM]], +]); + +const MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES = new Map([ + ['bert', ['BertForQuestionAnswering', BertForQuestionAnswering]], + ['roformer', ['RoFormerForQuestionAnswering', RoFormerForQuestionAnswering]], + ['electra', ['ElectraForQuestionAnswering', ElectraForQuestionAnswering]], + ['convbert', ['ConvBertForQuestionAnswering', ConvBertForQuestionAnswering]], + ['camembert', ['CamembertForQuestionAnswering', CamembertForQuestionAnswering]], + ['deberta', ['DebertaForQuestionAnswering', DebertaForQuestionAnswering]], + ['deberta-v2', ['DebertaV2ForQuestionAnswering', DebertaV2ForQuestionAnswering]], + ['mpnet', ['MPNetForQuestionAnswering', MPNetForQuestionAnswering]], + ['albert', ['AlbertForQuestionAnswering', AlbertForQuestionAnswering]], + ['distilbert', ['DistilBertForQuestionAnswering', DistilBertForQuestionAnswering]], + ['roberta', ['RobertaForQuestionAnswering', RobertaForQuestionAnswering]], + ['xlm', ['XLMForQuestionAnswering', XLMForQuestionAnswering]], + ['xlm-roberta', ['XLMRobertaForQuestionAnswering', XLMRobertaForQuestionAnswering]], + ['mobilebert', ['MobileBertForQuestionAnswering', MobileBertForQuestionAnswering]], + ['squeezebert', ['SqueezeBertForQuestionAnswering', SqueezeBertForQuestionAnswering]], +]); + +const MODEL_FOR_VISION_2_SEQ_MAPPING_NAMES = new Map([ + ['vision-encoder-decoder', ['VisionEncoderDecoderModel', VisionEncoderDecoderModel]], +]); + +const MODEL_FOR_IMAGE_TEXT_TO_TEXT_MAPPING_NAMES = new Map([ + ['llava', ['LlavaForConditionalGeneration', LlavaForConditionalGeneration]], + ['moondream1', ['Moondream1ForConditionalGeneration', Moondream1ForConditionalGeneration]], + ['florence2', ['Florence2ForConditionalGeneration', Florence2ForConditionalGeneration]], +]); + +const MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING_NAMES = new Map([ + ['vision-encoder-decoder', ['VisionEncoderDecoderModel', VisionEncoderDecoderModel]], +]); + +const MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES = new Map([ + ['vit', ['ViTForImageClassification', ViTForImageClassification]], + ['pvt', ['PvtForImageClassification', PvtForImageClassification]], + ['vit_msn', ['ViTMSNForImageClassification', ViTMSNForImageClassification]], + ['fastvit', ['FastViTForImageClassification', FastViTForImageClassification]], + ['mobilevit', ['MobileViTForImageClassification', MobileViTForImageClassification]], + ['mobilevitv2', ['MobileViTV2ForImageClassification', MobileViTV2ForImageClassification]], + ['beit', ['BeitForImageClassification', BeitForImageClassification]], + ['deit', ['DeiTForImageClassification', DeiTForImageClassification]], + ['hiera', ['HieraForImageClassification', HieraForImageClassification]], + ['convnext', ['ConvNextForImageClassification', ConvNextForImageClassification]], + ['convnextv2', ['ConvNextV2ForImageClassification', ConvNextV2ForImageClassification]], + ['dinov2', ['Dinov2ForImageClassification', Dinov2ForImageClassification]], + ['resnet', ['ResNetForImageClassification', ResNetForImageClassification]], + ['swin', ['SwinForImageClassification', SwinForImageClassification]], + ['segformer', ['SegformerForImageClassification', SegformerForImageClassification]], + ['efficientnet', ['EfficientNetForImageClassification', EfficientNetForImageClassification]], + ['mobilenet_v1', ['MobileNetV1ForImageClassification', MobileNetV1ForImageClassification]], + ['mobilenet_v2', ['MobileNetV2ForImageClassification', MobileNetV2ForImageClassification]], + ['mobilenet_v3', ['MobileNetV3ForImageClassification', MobileNetV3ForImageClassification]], + ['mobilenet_v4', ['MobileNetV4ForImageClassification', MobileNetV4ForImageClassification]], +]); + +const MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES = new Map([ + ['detr', ['DetrForObjectDetection', DetrForObjectDetection]], + ['rt_detr', ['RTDetrForObjectDetection', RTDetrForObjectDetection]], + ['table-transformer', ['TableTransformerForObjectDetection', TableTransformerForObjectDetection]], + ['yolos', ['YolosForObjectDetection', YolosForObjectDetection]], +]); + +const MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING_NAMES = new Map([ + ['owlvit', ['OwlViTForObjectDetection', OwlViTForObjectDetection]], + ['owlv2', ['Owlv2ForObjectDetection', Owlv2ForObjectDetection]], +]); + +const MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES = new Map([ + // TODO: Do not add new models here + ['detr', ['DetrForSegmentation', DetrForSegmentation]], + ['clipseg', ['CLIPSegForImageSegmentation', CLIPSegForImageSegmentation]], +]); + +const MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING_NAMES = new Map([ + ['segformer', ['SegformerForSemanticSegmentation', SegformerForSemanticSegmentation]], + ['sapiens', ['SapiensForSemanticSegmentation', SapiensForSemanticSegmentation]], +]); + +const MODEL_FOR_UNIVERSAL_SEGMENTATION_MAPPING_NAMES = new Map([ + ['detr', ['DetrForSegmentation', DetrForSegmentation]], + ['maskformer', ['MaskFormerForInstanceSegmentation', MaskFormerForInstanceSegmentation]], +]); + +const MODEL_FOR_MASK_GENERATION_MAPPING_NAMES = new Map([ + ['sam', ['SamModel', SamModel]], +]); + +const MODEL_FOR_CTC_MAPPING_NAMES = new Map([ + ['wav2vec2', ['Wav2Vec2ForCTC', Wav2Vec2ForCTC]], + ['wav2vec2-bert', ['Wav2Vec2BertForCTC', Wav2Vec2BertForCTC]], + ['unispeech', ['UniSpeechForCTC', UniSpeechForCTC]], + ['unispeech-sat', ['UniSpeechSatForCTC', UniSpeechSatForCTC]], + ['wavlm', ['WavLMForCTC', WavLMForCTC]], + ['hubert', ['HubertForCTC', HubertForCTC]], +]); + +const MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES = new Map([ + ['wav2vec2', ['Wav2Vec2ForSequenceClassification', Wav2Vec2ForSequenceClassification]], + ['wav2vec2-bert', ['Wav2Vec2BertForSequenceClassification', Wav2Vec2BertForSequenceClassification]], + ['unispeech', ['UniSpeechForSequenceClassification', UniSpeechForSequenceClassification]], + ['unispeech-sat', ['UniSpeechSatForSequenceClassification', UniSpeechSatForSequenceClassification]], + ['wavlm', ['WavLMForSequenceClassification', WavLMForSequenceClassification]], + ['hubert', ['HubertForSequenceClassification', HubertForSequenceClassification]], + ['audio-spectrogram-transformer', ['ASTForAudioClassification', ASTForAudioClassification]], +]); + +const MODEL_FOR_AUDIO_XVECTOR_MAPPING_NAMES = new Map([ + ['wavlm', ['WavLMForXVector', WavLMForXVector]], +]); + +const MODEL_FOR_AUDIO_FRAME_CLASSIFICATION_MAPPING_NAMES = new Map([ + ['unispeech-sat', ['UniSpeechSatForAudioFrameClassification', UniSpeechSatForAudioFrameClassification]], + ['wavlm', ['WavLMForAudioFrameClassification', WavLMForAudioFrameClassification]], + ['wav2vec2', ['Wav2Vec2ForAudioFrameClassification', Wav2Vec2ForAudioFrameClassification]], + ['pyannote', ['PyAnnoteForAudioFrameClassification', PyAnnoteForAudioFrameClassification]], +]); + +const MODEL_FOR_IMAGE_MATTING_MAPPING_NAMES = new Map([ + ['vitmatte', ['VitMatteForImageMatting', VitMatteForImageMatting]], +]); + +const MODEL_FOR_IMAGE_TO_IMAGE_MAPPING_NAMES = new Map([ + ['swin2sr', ['Swin2SRForImageSuperResolution', Swin2SRForImageSuperResolution]], +]) + +const MODEL_FOR_DEPTH_ESTIMATION_MAPPING_NAMES = new Map([ + ['dpt', ['DPTForDepthEstimation', DPTForDepthEstimation]], + ['depth_anything', ['DepthAnythingForDepthEstimation', DepthAnythingForDepthEstimation]], + ['glpn', ['GLPNForDepthEstimation', GLPNForDepthEstimation]], + ['sapiens', ['SapiensForDepthEstimation', SapiensForDepthEstimation]], + ['depth_pro', ['DepthProForDepthEstimation', DepthProForDepthEstimation]], +]) + +const MODEL_FOR_NORMAL_ESTIMATION_MAPPING_NAMES = new Map([ + ['sapiens', ['SapiensForNormalEstimation', SapiensForNormalEstimation]], +]) + +// NOTE: This is custom to Transformers.js, and is necessary because certain models +// (e.g., CLIP) are split into vision and text components +const MODEL_FOR_IMAGE_FEATURE_EXTRACTION_MAPPING_NAMES = new Map([ + ['clip', ['CLIPVisionModelWithProjection', CLIPVisionModelWithProjection]], + ['siglip', ['SiglipVisionModel', SiglipVisionModel]], +]) + +const MODEL_CLASS_TYPE_MAPPING = [ + [MODEL_MAPPING_NAMES_ENCODER_ONLY, MODEL_TYPES.EncoderOnly], + [MODEL_MAPPING_NAMES_ENCODER_DECODER, MODEL_TYPES.EncoderDecoder], + [MODEL_MAPPING_NAMES_DECODER_ONLY, MODEL_TYPES.DecoderOnly], + [MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES, MODEL_TYPES.Seq2Seq], + [MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES, MODEL_TYPES.Seq2Seq], + [MODEL_FOR_CAUSAL_LM_MAPPING_NAMES, MODEL_TYPES.DecoderOnly], + [MODEL_FOR_MASKED_LM_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_VISION_2_SEQ_MAPPING_NAMES, MODEL_TYPES.Vision2Seq], + [MODEL_FOR_IMAGE_TEXT_TO_TEXT_MAPPING_NAMES, MODEL_TYPES.ImageTextToText], + [MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_UNIVERSAL_SEGMENTATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_IMAGE_MATTING_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_IMAGE_TO_IMAGE_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_DEPTH_ESTIMATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_NORMAL_ESTIMATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_MASK_GENERATION_MAPPING_NAMES, MODEL_TYPES.MaskGeneration], + [MODEL_FOR_CTC_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_TEXT_TO_SPECTROGRAM_MAPPING_NAMES, MODEL_TYPES.Seq2Seq], + [MODEL_FOR_TEXT_TO_WAVEFORM_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_AUDIO_XVECTOR_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_AUDIO_FRAME_CLASSIFICATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + + // Custom: + [MODEL_FOR_IMAGE_FEATURE_EXTRACTION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], +]; + +for (const [mappings, type] of MODEL_CLASS_TYPE_MAPPING) { + // @ts-ignore + for (const [name, model] of mappings.values()) { + MODEL_TYPE_MAPPING.set(name, type); + MODEL_CLASS_TO_NAME_MAPPING.set(model, name); + MODEL_NAME_TO_CLASS_MAPPING.set(name, model); + } +} + +const CUSTOM_MAPPING = [ + // OVERRIDE: + // TODO: Refactor to allow class to specify model + ['MusicgenForConditionalGeneration', MusicgenForConditionalGeneration, MODEL_TYPES.Musicgen], + + ['CLIPTextModelWithProjection', CLIPTextModelWithProjection, MODEL_TYPES.EncoderOnly], + ['SiglipTextModel', SiglipTextModel, MODEL_TYPES.EncoderOnly], + ['ClapTextModelWithProjection', ClapTextModelWithProjection, MODEL_TYPES.EncoderOnly], + ['ClapAudioModelWithProjection', ClapAudioModelWithProjection, MODEL_TYPES.EncoderOnly], +] +for (const [name, model, type] of CUSTOM_MAPPING) { + MODEL_TYPE_MAPPING.set(name, type); + MODEL_CLASS_TO_NAME_MAPPING.set(model, name); + MODEL_NAME_TO_CLASS_MAPPING.set(name, model); +} + + +/** + * Helper class which is used to instantiate pretrained models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModel.from_pretrained('Xenova/bert-base-uncased'); + */ +class AutoModel extends PretrainedMixin { + /** @type {Map[]} */ + // @ts-ignore + static MODEL_CLASS_MAPPINGS = MODEL_CLASS_TYPE_MAPPING.map(x => x[0]); + static BASE_IF_FAIL = true; +} + +/** + * Helper class which is used to instantiate pretrained sequence classification models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForSequenceClassification.from_pretrained('Xenova/distilbert-base-uncased-finetuned-sst-2-english'); + */ +class AutoModelForSequenceClassification extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained token classification models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForTokenClassification.from_pretrained('Xenova/distilbert-base-multilingual-cased-ner-hrl'); + */ +class AutoModelForTokenClassification extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained sequence-to-sequence models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForSeq2SeqLM.from_pretrained('Xenova/t5-small'); + */ +class AutoModelForSeq2SeqLM extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained sequence-to-sequence speech-to-text models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForSpeechSeq2Seq.from_pretrained('openai/whisper-tiny.en'); + */ +class AutoModelForSpeechSeq2Seq extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained sequence-to-sequence text-to-spectrogram models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForTextToSpectrogram.from_pretrained('microsoft/speecht5_tts'); + */ +class AutoModelForTextToSpectrogram extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_TEXT_TO_SPECTROGRAM_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained text-to-waveform models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForTextToSpectrogram.from_pretrained('facebook/mms-tts-eng'); + */ +class AutoModelForTextToWaveform extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_TEXT_TO_WAVEFORM_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained causal language models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForCausalLM.from_pretrained('Xenova/gpt2'); + */ +class AutoModelForCausalLM extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_CAUSAL_LM_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained masked language models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForMaskedLM.from_pretrained('Xenova/bert-base-uncased'); + */ +class AutoModelForMaskedLM extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_MASKED_LM_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained question answering models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForQuestionAnswering.from_pretrained('Xenova/distilbert-base-cased-distilled-squad'); + */ +class AutoModelForQuestionAnswering extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained vision-to-sequence models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForVision2Seq.from_pretrained('Xenova/vit-gpt2-image-captioning'); + */ +class AutoModelForVision2Seq extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_VISION_2_SEQ_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained image classification models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForImageClassification.from_pretrained('Xenova/vit-base-patch16-224'); + */ +class AutoModelForImageClassification extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained image segmentation models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForImageSegmentation.from_pretrained('Xenova/detr-resnet-50-panoptic'); + */ +class AutoModelForImageSegmentation extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained image segmentation models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForSemanticSegmentation.from_pretrained('nvidia/segformer-b3-finetuned-cityscapes-1024-1024'); + */ +class AutoModelForSemanticSegmentation extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained universal image segmentation models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForUniversalSegmentation.from_pretrained('hf-internal-testing/tiny-random-MaskFormerForInstanceSegmentation'); + */ +class AutoModelForUniversalSegmentation extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_UNIVERSAL_SEGMENTATION_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained object detection models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForObjectDetection.from_pretrained('Xenova/detr-resnet-50'); + */ +class AutoModelForObjectDetection extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES]; +} + +class AutoModelForZeroShotObjectDetection extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING_NAMES]; +} + + +/** + * Helper class which is used to instantiate pretrained mask generation models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForMaskGeneration.from_pretrained('Xenova/sam-vit-base'); + */ +class AutoModelForMaskGeneration extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_MASK_GENERATION_MAPPING_NAMES]; +} + +class AutoModelForCTC extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_CTC_MAPPING_NAMES]; +} + +class AutoModelForAudioClassification extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES]; +} + +class AutoModelForXVector extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_AUDIO_XVECTOR_MAPPING_NAMES]; +} + +class AutoModelForAudioFrameClassification extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_AUDIO_FRAME_CLASSIFICATION_MAPPING_NAMES]; +} + +class AutoModelForDocumentQuestionAnswering extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING_NAMES]; +} + +class AutoModelForImageMatting extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_IMAGE_MATTING_MAPPING_NAMES]; +} + +class AutoModelForImageToImage extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_IMAGE_TO_IMAGE_MAPPING_NAMES]; +} + +class AutoModelForDepthEstimation extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_DEPTH_ESTIMATION_MAPPING_NAMES]; +} + +class AutoModelForNormalEstimation extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_NORMAL_ESTIMATION_MAPPING_NAMES]; +} + +class AutoModelForImageFeatureExtraction extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_IMAGE_FEATURE_EXTRACTION_MAPPING_NAMES]; +} + +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class Seq2SeqLMOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits The output logits of the model. + * @param {Tensor} output.past_key_values An tensor of key/value pairs that represent the previous state of the model. + * @param {Tensor} output.encoder_outputs The output of the encoder in a sequence-to-sequence model. + * @param {Tensor} [output.decoder_attentions] Attentions weights of the decoder, after the attention softmax, used to compute the weighted average in the self-attention heads. + * @param {Tensor} [output.cross_attentions] Attentions weights of the decoder's cross-attention layer, after the attention softmax, used to compute the weighted average in the cross-attention heads. + */ + constructor({ logits, past_key_values, encoder_outputs, decoder_attentions = null, cross_attentions = null }) { + super(); + this.logits = logits; + this.past_key_values = past_key_values; + this.encoder_outputs = encoder_outputs; + this.decoder_attentions = decoder_attentions; + this.cross_attentions = cross_attentions; + } +} + +/** + * Base class for outputs of sentence classification models. + */ +class SequenceClassifierOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits classification (or regression if config.num_labels==1) scores (before SoftMax). + */ + constructor({ logits }) { + super(); + this.logits = logits; + } +} + +/** + * Base class for outputs of XVector models. + */ +class XVectorOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits Classification hidden states before AMSoftmax, of shape `(batch_size, config.xvector_output_dim)`. + * @param {Tensor} output.embeddings Utterance embeddings used for vector similarity-based retrieval, of shape `(batch_size, config.xvector_output_dim)`. + */ + constructor({ logits, embeddings }) { + super(); + this.logits = logits; + this.embeddings = embeddings; + } +} + +/** + * Base class for outputs of token classification models. + */ +class TokenClassifierOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits Classification scores (before SoftMax). + */ + constructor({ logits }) { + super(); + this.logits = logits; + } +} + +/** + * Base class for masked language models outputs. + */ +class MaskedLMOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax). + */ + constructor({ logits }) { + super(); + this.logits = logits; + } +} + +/** + * Base class for outputs of question answering models. + */ +class QuestionAnsweringModelOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.start_logits Span-start scores (before SoftMax). + * @param {Tensor} output.end_logits Span-end scores (before SoftMax). + */ + constructor({ start_logits, end_logits }) { + super(); + this.start_logits = start_logits; + this.end_logits = end_logits; + } +} + + +/** + * Base class for causal language model (or autoregressive) outputs. + */ +class CausalLMOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits Prediction scores of the language modeling head (scores for each vocabulary token before softmax). + */ + constructor({ logits }) { + super(); + this.logits = logits; + } +} + +/** + * Base class for causal language model (or autoregressive) outputs. + */ +class CausalLMOutputWithPast extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits Prediction scores of the language modeling head (scores for each vocabulary token before softmax). + * @param {Tensor} output.past_key_values Contains pre-computed hidden-states (key and values in the self-attention blocks) + * that can be used (see `past_key_values` input) to speed up sequential decoding. + */ + constructor({ logits, past_key_values }) { + super(); + this.logits = logits; + this.past_key_values = past_key_values; + } +} + +class ImageMattingOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.alphas Estimated alpha values, of shape `(batch_size, num_channels, height, width)`. + */ + constructor({ alphas }) { + super(); + this.alphas = alphas; + } +} + +/** + * Describes the outputs for the VITS model. + */ +class VitsModelOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.waveform The final audio waveform predicted by the model, of shape `(batch_size, sequence_length)`. + * @param {Tensor} output.spectrogram The log-mel spectrogram predicted at the output of the flow model. + * This spectrogram is passed to the Hi-Fi GAN decoder model to obtain the final audio waveform. + */ + constructor({ waveform, spectrogram }) { + super(); + this.waveform = waveform; + this.spectrogram = spectrogram; + } +} + + +/***/ }), + +/***/ "./src/models/whisper/common_whisper.js": +/*!**********************************************!*\ + !*** ./src/models/whisper/common_whisper.js ***! + \**********************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ WHISPER_LANGUAGE_MAPPING: () => (/* binding */ WHISPER_LANGUAGE_MAPPING), +/* harmony export */ WHISPER_TO_LANGUAGE_CODE_MAPPING: () => (/* binding */ WHISPER_TO_LANGUAGE_CODE_MAPPING), +/* harmony export */ whisper_language_to_code: () => (/* binding */ whisper_language_to_code) +/* harmony export */ }); + + +const WHISPER_LANGUAGES = [ + ["en", "english"], + ["zh", "chinese"], + ["de", "german"], + ["es", "spanish"], + ["ru", "russian"], + ["ko", "korean"], + ["fr", "french"], + ["ja", "japanese"], + ["pt", "portuguese"], + ["tr", "turkish"], + ["pl", "polish"], + ["ca", "catalan"], + ["nl", "dutch"], + ["ar", "arabic"], + ["sv", "swedish"], + ["it", "italian"], + ["id", "indonesian"], + ["hi", "hindi"], + ["fi", "finnish"], + ["vi", "vietnamese"], + ["he", "hebrew"], + ["uk", "ukrainian"], + ["el", "greek"], + ["ms", "malay"], + ["cs", "czech"], + ["ro", "romanian"], + ["da", "danish"], + ["hu", "hungarian"], + ["ta", "tamil"], + ["no", "norwegian"], + ["th", "thai"], + ["ur", "urdu"], + ["hr", "croatian"], + ["bg", "bulgarian"], + ["lt", "lithuanian"], + ["la", "latin"], + ["mi", "maori"], + ["ml", "malayalam"], + ["cy", "welsh"], + ["sk", "slovak"], + ["te", "telugu"], + ["fa", "persian"], + ["lv", "latvian"], + ["bn", "bengali"], + ["sr", "serbian"], + ["az", "azerbaijani"], + ["sl", "slovenian"], + ["kn", "kannada"], + ["et", "estonian"], + ["mk", "macedonian"], + ["br", "breton"], + ["eu", "basque"], + ["is", "icelandic"], + ["hy", "armenian"], + ["ne", "nepali"], + ["mn", "mongolian"], + ["bs", "bosnian"], + ["kk", "kazakh"], + ["sq", "albanian"], + ["sw", "swahili"], + ["gl", "galician"], + ["mr", "marathi"], + ["pa", "punjabi"], + ["si", "sinhala"], + ["km", "khmer"], + ["sn", "shona"], + ["yo", "yoruba"], + ["so", "somali"], + ["af", "afrikaans"], + ["oc", "occitan"], + ["ka", "georgian"], + ["be", "belarusian"], + ["tg", "tajik"], + ["sd", "sindhi"], + ["gu", "gujarati"], + ["am", "amharic"], + ["yi", "yiddish"], + ["lo", "lao"], + ["uz", "uzbek"], + ["fo", "faroese"], + ["ht", "haitian creole"], + ["ps", "pashto"], + ["tk", "turkmen"], + ["nn", "nynorsk"], + ["mt", "maltese"], + ["sa", "sanskrit"], + ["lb", "luxembourgish"], + ["my", "myanmar"], + ["bo", "tibetan"], + ["tl", "tagalog"], + ["mg", "malagasy"], + ["as", "assamese"], + ["tt", "tatar"], + ["haw", "hawaiian"], + ["ln", "lingala"], + ["ha", "hausa"], + ["ba", "bashkir"], + ["jw", "javanese"], + ["su", "sundanese"], +] + +// @ts-ignore +const WHISPER_LANGUAGE_MAPPING = new Map(WHISPER_LANGUAGES); +// @ts-ignore +const WHISPER_TO_LANGUAGE_CODE_MAPPING = new Map([ + ...WHISPER_LANGUAGES.map(([k, v]) => [v, k]), + ...[ + ["burmese", "my"], + ["valencian", "ca"], + ["flemish", "nl"], + ["haitian", "ht"], + ["letzeburgesch", "lb"], + ["pushto", "ps"], + ["panjabi", "pa"], + ["moldavian", "ro"], + ["moldovan", "ro"], + ["sinhalese", "si"], + ["castilian", "es"], + ] +]); + +/** + * @param {string} language The language name or code + * @returns {string} The language code + */ +function whisper_language_to_code(language) { + language = language.toLowerCase(); + + // Map to code from user-friendly name (e.g., "english" -> "en") + let language_code = WHISPER_TO_LANGUAGE_CODE_MAPPING.get(language); + + if (language_code === undefined) { + // User provided something that is not a language name + + if (WHISPER_LANGUAGE_MAPPING.has(language)) { + // User provided the language code directly (e.g., "en") + language_code = language; + + } else { + // User provided something that is not a language code or name + const is_language_code = language.length === 2; + const langs = is_language_code ? WHISPER_LANGUAGE_MAPPING.keys() : WHISPER_LANGUAGE_MAPPING.values(); + + throw new Error(`Language "${language}" is not supported. Must be one of: ${JSON.stringify(langs)}`); + } + } + return language_code; +} + + +/***/ }), + +/***/ "./src/models/whisper/generation_whisper.js": +/*!**************************************************!*\ + !*** ./src/models/whisper/generation_whisper.js ***! + \**************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ WhisperGenerationConfig: () => (/* binding */ WhisperGenerationConfig) +/* harmony export */ }); +/* harmony import */ var _generation_configuration_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../generation/configuration_utils.js */ "./src/generation/configuration_utils.js"); + + +class WhisperGenerationConfig extends _generation_configuration_utils_js__WEBPACK_IMPORTED_MODULE_0__.GenerationConfig { + + /** + * Whether to return the timestamps with the text. This enables the `WhisperTimestampsLogitsProcessor`. + * @type {boolean} + */ + return_timestamps = null; + + /** + * Whether to return token-level timestamps + * with the text. This can be used with or without the `return_timestamps` option. To get word-level + * timestamps, use the tokenizer to group the tokens into words. + * @type {boolean} + */ + return_token_timestamps = null; + + /** + * The number of audio frames available in this chunk. This is only used generating word-level timestamps. + * @type {number} + */ + num_frames = null; + + /** + * Alignment heads to predict word-level timestamps. This is a list of [layer, head] pairs that + * select the cross-attention heads that are highly correlated to word-level timing. + * @type {[number, number][]} + */ + alignment_heads = null; + + /** + * Task to use for generation, either "translate" or "transcribe". + * @type {string} + */ + task = null; + + /** + * Language token to use for generation, can be either in the form of `<|en|>`, `en` or `english`. + * You can find all the possible language tokens in the `model.generation_config.lang_to_id` dictionary. + * @type {string} + */ + language = null; + + /** + * The id of the `"<|notimestamps|>"` token. + * @type {number} + */ + no_timestamps_token_id = null; + + /** + * Rank-1 list of token IDs created by passing text to [`~WhisperProcessor.get_prompt_ids`] that is + * provided as a prompt to each chunk. This can be used to provide or "prompt-engineer" a context for + * transcription, e.g. custom vocabularies or proper nouns to make it more likely to predict those words + * correctly. It cannot be used in conjunction with `decoder_start_token_id` as it overwrites this value. + * @type {number[]} + */ + prompt_ids = null; + + /** + * Whether the model is multilingual or not. + * @type {boolean} + */ + is_multilingual = null; + + /** + * (Optional) A mapping from language tokens to their corresponding IDs. + * Only required if the model is multilingual. + * @type {Record|null} + */ + lang_to_id = null; + + /** + * (Optional) A mapping from task tokens to their corresponding IDs. + * @type {Record|null} + */ + task_to_id = null; + + /** + * Used to set the maximum value of the initial timestamp. This is used to prevent the model from + * predicting timestamps that are too far in the future. + * @type {number} + */ + max_initial_timestamp_index = 1; +} + +/** + * @typedef {import('../../generation/parameters.js').GenerationFunctionParameters & {generation_config: WhisperGenerationConfig} & WhisperGenerationConfig} WhisperGenerationFunctionParameters + */ + + +/***/ }), + +/***/ "./src/ops/registry.js": +/*!*****************************!*\ + !*** ./src/ops/registry.js ***! + \*****************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ TensorOpRegistry: () => (/* binding */ TensorOpRegistry) +/* harmony export */ }); +/* harmony import */ var _backends_onnx_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../backends/onnx.js */ "./src/backends/onnx.js"); +/* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../utils/tensor.js */ "./src/utils/tensor.js"); + + + +/** + * Asynchronously creates a wrapper function for running an ONNX inference session. + * + * @param {number[]} session_bytes The session data in bytes. + * @param {import('onnxruntime-common').InferenceSession.SessionOptions} session_options The options for the ONNX session. + * @template {string | [string] | string[]} T + * @param {T} names The name(s) of the output tensor(s). + * + * @returns {Promise): Promise>} + * The wrapper function for running the ONNX inference session. + */ +const wrap = async (session_bytes, session_options, names) => { + const session = await (0,_backends_onnx_js__WEBPACK_IMPORTED_MODULE_0__.createInferenceSession)( + new Uint8Array(session_bytes), session_options, + ); + return /** @type {any} */(async (/** @type {Record} */ inputs) => { + const ortFeed = Object.fromEntries(Object.entries(inputs).map(([k, v]) => [k, v.ort_tensor])); + const outputs = await session.run(ortFeed); + + if (Array.isArray(names)) { + return names.map((n) => new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor(outputs[n])); + } else { + return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor(outputs[/** @type {string} */(names)]); + } + }) +} + +// In-memory registry of initialized ONNX operators +class TensorOpRegistry { + static session_options = { + // TODO: Allow for multiple execution providers + // executionProviders: ['webgpu'], + }; + + static get bilinear_interpolate_4d() { + if (!this._bilinear_interpolate_4d) { + this._bilinear_interpolate_4d = wrap( + [8, 9, 18, 0, 58, 128, 1, 10, 40, 10, 1, 120, 10, 0, 10, 0, 10, 1, 115, 18, 1, 121, 34, 6, 82, 101, 115, 105, 122, 101, 42, 17, 10, 4, 109, 111, 100, 101, 34, 6, 108, 105, 110, 101, 97, 114, 160, 1, 3, 18, 1, 114, 90, 31, 10, 1, 120, 18, 26, 10, 24, 8, 1, 18, 20, 10, 3, 18, 1, 98, 10, 3, 18, 1, 99, 10, 3, 18, 1, 104, 10, 3, 18, 1, 119, 90, 15, 10, 1, 115, 18, 10, 10, 8, 8, 7, 18, 4, 10, 2, 8, 4, 98, 31, 10, 1, 121, 18, 26, 10, 24, 8, 1, 18, 20, 10, 3, 18, 1, 98, 10, 3, 18, 1, 99, 10, 3, 18, 1, 104, 10, 3, 18, 1, 119, 66, 2, 16, 20], + this.session_options, + 'y', + ); + } + return this._bilinear_interpolate_4d; + } + + static get bicubic_interpolate_4d() { + if (!this._bicubic_interpolate_4d) { + this._bicubic_interpolate_4d = wrap( + [8, 9, 18, 0, 58, 127, 10, 39, 10, 1, 120, 10, 0, 10, 0, 10, 1, 115, 18, 1, 121, 34, 6, 82, 101, 115, 105, 122, 101, 42, 16, 10, 4, 109, 111, 100, 101, 34, 5, 99, 117, 98, 105, 99, 160, 1, 3, 18, 1, 114, 90, 31, 10, 1, 120, 18, 26, 10, 24, 8, 1, 18, 20, 10, 3, 18, 1, 98, 10, 3, 18, 1, 99, 10, 3, 18, 1, 104, 10, 3, 18, 1, 119, 90, 15, 10, 1, 115, 18, 10, 10, 8, 8, 7, 18, 4, 10, 2, 8, 4, 98, 31, 10, 1, 121, 18, 26, 10, 24, 8, 1, 18, 20, 10, 3, 18, 1, 98, 10, 3, 18, 1, 99, 10, 3, 18, 1, 104, 10, 3, 18, 1, 119, 66, 2, 16, 20], + this.session_options, + 'y', + ); + } + return this._bicubic_interpolate_4d; + } + + static get matmul() { + if (!this._matmul) { + this._matmul = wrap( + [8, 9, 18, 0, 58, 55, 10, 17, 10, 1, 97, 10, 1, 98, 18, 1, 99, 34, 6, 77, 97, 116, 77, 117, 108, 18, 1, 114, 90, 9, 10, 1, 97, 18, 4, 10, 2, 8, 1, 90, 9, 10, 1, 98, 18, 4, 10, 2, 8, 1, 98, 9, 10, 1, 99, 18, 4, 10, 2, 8, 1, 66, 2, 16, 20], + this.session_options, + 'c', + ); + } + return this._matmul; + } + + static get stft() { + if (!this._stft) { + this._stft = wrap( + [8, 7, 18, 0, 58, 148, 1, 10, 38, 10, 1, 115, 10, 1, 106, 10, 1, 119, 10, 1, 108, 18, 1, 111, 34, 4, 83, 84, 70, 84, 42, 15, 10, 8, 111, 110, 101, 115, 105, 100, 101, 100, 24, 1, 160, 1, 2, 18, 1, 115, 90, 26, 10, 1, 115, 18, 21, 10, 19, 8, 1, 18, 15, 10, 3, 18, 1, 98, 10, 3, 18, 1, 115, 10, 3, 18, 1, 99, 90, 11, 10, 1, 106, 18, 6, 10, 4, 8, 7, 18, 0, 90, 16, 10, 1, 119, 18, 11, 10, 9, 8, 1, 18, 5, 10, 3, 18, 1, 119, 90, 11, 10, 1, 108, 18, 6, 10, 4, 8, 7, 18, 0, 98, 31, 10, 1, 111, 18, 26, 10, 24, 8, 1, 18, 20, 10, 3, 18, 1, 98, 10, 3, 18, 1, 102, 10, 3, 18, 1, 100, 10, 3, 18, 1, 99, 66, 2, 16, 17], + this.session_options, + 'o', + ) + } + return this._stft; + } + + static get rfft() { + if (!this._rfft) { + this._rfft = wrap( + [8, 9, 18, 0, 58, 97, 10, 33, 10, 1, 120, 10, 0, 10, 1, 97, 18, 1, 121, 34, 3, 68, 70, 84, 42, 15, 10, 8, 111, 110, 101, 115, 105, 100, 101, 100, 24, 1, 160, 1, 2, 18, 1, 100, 90, 21, 10, 1, 120, 18, 16, 10, 14, 8, 1, 18, 10, 10, 3, 18, 1, 115, 10, 3, 18, 1, 99, 90, 11, 10, 1, 97, 18, 6, 10, 4, 8, 7, 18, 0, 98, 21, 10, 1, 121, 18, 16, 10, 14, 8, 1, 18, 10, 10, 3, 18, 1, 115, 10, 3, 18, 1, 99, 66, 2, 16, 20], + this.session_options, + 'y', + ) + } + return this._rfft; + } + + static get top_k() { + if (!this._top_k) { + this._top_k = wrap( + [8, 10, 18, 0, 58, 73, 10, 18, 10, 1, 120, 10, 1, 107, 18, 1, 118, 18, 1, 105, 34, 4, 84, 111, 112, 75, 18, 1, 116, 90, 9, 10, 1, 120, 18, 4, 10, 2, 8, 1, 90, 15, 10, 1, 107, 18, 10, 10, 8, 8, 7, 18, 4, 10, 2, 8, 1, 98, 9, 10, 1, 118, 18, 4, 10, 2, 8, 1, 98, 9, 10, 1, 105, 18, 4, 10, 2, 8, 7, 66, 2, 16, 21], + this.session_options, + [ /* Values */ 'v', /* Indices */ 'i'] + ) + } + return this._top_k; + } +} + + +/***/ }), + +/***/ "./src/pipelines.js": +/*!**************************!*\ + !*** ./src/pipelines.js ***! + \**************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ AudioClassificationPipeline: () => (/* binding */ AudioClassificationPipeline), +/* harmony export */ AutomaticSpeechRecognitionPipeline: () => (/* binding */ AutomaticSpeechRecognitionPipeline), +/* harmony export */ DepthEstimationPipeline: () => (/* binding */ DepthEstimationPipeline), +/* harmony export */ DocumentQuestionAnsweringPipeline: () => (/* binding */ DocumentQuestionAnsweringPipeline), +/* harmony export */ FeatureExtractionPipeline: () => (/* binding */ FeatureExtractionPipeline), +/* harmony export */ FillMaskPipeline: () => (/* binding */ FillMaskPipeline), +/* harmony export */ ImageClassificationPipeline: () => (/* binding */ ImageClassificationPipeline), +/* harmony export */ ImageFeatureExtractionPipeline: () => (/* binding */ ImageFeatureExtractionPipeline), +/* harmony export */ ImageSegmentationPipeline: () => (/* binding */ ImageSegmentationPipeline), +/* harmony export */ ImageToImagePipeline: () => (/* binding */ ImageToImagePipeline), +/* harmony export */ ImageToTextPipeline: () => (/* binding */ ImageToTextPipeline), +/* harmony export */ ObjectDetectionPipeline: () => (/* binding */ ObjectDetectionPipeline), +/* harmony export */ Pipeline: () => (/* binding */ Pipeline), +/* harmony export */ QuestionAnsweringPipeline: () => (/* binding */ QuestionAnsweringPipeline), +/* harmony export */ SummarizationPipeline: () => (/* binding */ SummarizationPipeline), +/* harmony export */ Text2TextGenerationPipeline: () => (/* binding */ Text2TextGenerationPipeline), +/* harmony export */ TextClassificationPipeline: () => (/* binding */ TextClassificationPipeline), +/* harmony export */ TextGenerationPipeline: () => (/* binding */ TextGenerationPipeline), +/* harmony export */ TextToAudioPipeline: () => (/* binding */ TextToAudioPipeline), +/* harmony export */ TokenClassificationPipeline: () => (/* binding */ TokenClassificationPipeline), +/* harmony export */ TranslationPipeline: () => (/* binding */ TranslationPipeline), +/* harmony export */ ZeroShotAudioClassificationPipeline: () => (/* binding */ ZeroShotAudioClassificationPipeline), +/* harmony export */ ZeroShotClassificationPipeline: () => (/* binding */ ZeroShotClassificationPipeline), +/* harmony export */ ZeroShotImageClassificationPipeline: () => (/* binding */ ZeroShotImageClassificationPipeline), +/* harmony export */ ZeroShotObjectDetectionPipeline: () => (/* binding */ ZeroShotObjectDetectionPipeline), +/* harmony export */ pipeline: () => (/* binding */ pipeline) +/* harmony export */ }); +/* harmony import */ var _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./tokenizers.js */ "./src/tokenizers.js"); +/* harmony import */ var _models_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./models.js */ "./src/models.js"); +/* harmony import */ var _processors_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./processors.js */ "./src/processors.js"); +/* harmony import */ var _utils_generic_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./utils/generic.js */ "./src/utils/generic.js"); +/* harmony import */ var _utils_core_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ./utils/core.js */ "./src/utils/core.js"); +/* harmony import */ var _utils_maths_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ./utils/maths.js */ "./src/utils/maths.js"); +/* harmony import */ var _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! ./utils/audio.js */ "./src/utils/audio.js"); +/* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__(/*! ./utils/tensor.js */ "./src/utils/tensor.js"); +/* harmony import */ var _utils_image_js__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__(/*! ./utils/image.js */ "./src/utils/image.js"); +/** + * @file Pipelines provide a high-level, easy to use, API for running machine learning models. + * + * **Example:** Instantiate pipeline using the `pipeline` function. + * ```javascript + * import { pipeline } from '@huggingface/transformers'; + * + * const classifier = await pipeline('sentiment-analysis'); + * const output = await classifier('I love transformers!'); + * // [{'label': 'POSITIVE', 'score': 0.999817686}] + * ``` + * + * @module pipelines + */ + + + + + + + + + + + + + + +/** + * @typedef {string | RawImage | URL} ImageInput + * @typedef {ImageInput|ImageInput[]} ImagePipelineInputs + */ + +/** + * Prepare images for further tasks. + * @param {ImagePipelineInputs} images images to prepare. + * @returns {Promise} returns processed images. + * @private + */ +async function prepareImages(images) { + if (!Array.isArray(images)) { + images = [images]; + } + + // Possibly convert any non-images to images + return await Promise.all(images.map(x => _utils_image_js__WEBPACK_IMPORTED_MODULE_8__.RawImage.read(x))); +} + +/** + * @typedef {string | URL | Float32Array | Float64Array} AudioInput + * @typedef {AudioInput|AudioInput[]} AudioPipelineInputs + */ + +/** + * Prepare audios for further tasks. + * @param {AudioPipelineInputs} audios audios to prepare. + * @param {number} sampling_rate sampling rate of the audios. + * @returns {Promise} The preprocessed audio data. + * @private + */ +async function prepareAudios(audios, sampling_rate) { + if (!Array.isArray(audios)) { + audios = [audios]; + } + + return await Promise.all(audios.map(x => { + if (typeof x === 'string' || x instanceof URL) { + return (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.read_audio)(x, sampling_rate); + } else if (x instanceof Float64Array) { + return new Float32Array(x); + } + return x; + })); +} + +/** + * @typedef {Object} BoundingBox + * @property {number} xmin The minimum x coordinate of the bounding box. + * @property {number} ymin The minimum y coordinate of the bounding box. + * @property {number} xmax The maximum x coordinate of the bounding box. + * @property {number} ymax The maximum y coordinate of the bounding box. + */ + +/** + * Helper function to convert list [xmin, xmax, ymin, ymax] into object { "xmin": xmin, ... } + * @param {number[]} box The bounding box as a list. + * @param {boolean} asInteger Whether to cast to integers. + * @returns {BoundingBox} The bounding box as an object. + * @private + */ +function get_bounding_box(box, asInteger) { + if (asInteger) { + box = box.map(x => x | 0); + } + const [xmin, ymin, xmax, ymax] = box; + + return { xmin, ymin, xmax, ymax }; +} + + +/** + * @callback DisposeType Disposes the item. + * @returns {Promise} A promise that resolves when the item has been disposed. + * + * @typedef {Object} Disposable + * @property {DisposeType} dispose A promise that resolves when the pipeline has been disposed. + */ + +/** + * The Pipeline class is the class from which all pipelines inherit. + * Refer to this class for methods shared across different pipelines. + * @extends Callable + */ +class Pipeline extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_3__.Callable { + /** + * Create a new Pipeline. + * @param {Object} options An object containing the following properties: + * @param {string} [options.task] The task of the pipeline. Useful for specifying subtasks. + * @param {PreTrainedModel} [options.model] The model used by the pipeline. + * @param {PreTrainedTokenizer} [options.tokenizer=null] The tokenizer used by the pipeline (if any). + * @param {Processor} [options.processor=null] The processor used by the pipeline (if any). + */ + constructor({ task, model, tokenizer = null, processor = null }) { + super(); + this.task = task; + this.model = model; + this.tokenizer = tokenizer; + this.processor = processor; + } + + /** @type {DisposeType} */ + async dispose() { + await this.model.dispose(); + } +} + +/** + * @typedef {Object} ModelTokenizerConstructorArgs + * @property {string} task The task of the pipeline. Useful for specifying subtasks. + * @property {PreTrainedModel} model The model used by the pipeline. + * @property {PreTrainedTokenizer} tokenizer The tokenizer used by the pipeline. + * + * @typedef {ModelTokenizerConstructorArgs} TextPipelineConstructorArgs An object used to instantiate a text-based pipeline. + */ + +/** + * @typedef {Object} ModelProcessorConstructorArgs + * @property {string} task The task of the pipeline. Useful for specifying subtasks. + * @property {PreTrainedModel} model The model used by the pipeline. + * @property {Processor} processor The processor used by the pipeline. + * + * @typedef {ModelProcessorConstructorArgs} AudioPipelineConstructorArgs An object used to instantiate an audio-based pipeline. + * @typedef {ModelProcessorConstructorArgs} ImagePipelineConstructorArgs An object used to instantiate an image-based pipeline. + */ + + +/** + * @typedef {Object} ModelTokenizerProcessorConstructorArgs + * @property {string} task The task of the pipeline. Useful for specifying subtasks. + * @property {PreTrainedModel} model The model used by the pipeline. + * @property {PreTrainedTokenizer} tokenizer The tokenizer used by the pipeline. + * @property {Processor} processor The processor used by the pipeline. + * + * @typedef {ModelTokenizerProcessorConstructorArgs} TextAudioPipelineConstructorArgs An object used to instantiate a text- and audio-based pipeline. + * @typedef {ModelTokenizerProcessorConstructorArgs} TextImagePipelineConstructorArgs An object used to instantiate a text- and image-based pipeline. + */ + +/** + * @typedef {Object} TextClassificationSingle + * @property {string} label The label predicted. + * @property {number} score The corresponding probability. + * @typedef {TextClassificationSingle[]} TextClassificationOutput + * + * @typedef {Object} TextClassificationPipelineOptions Parameters specific to text classification pipelines. + * @property {number} [top_k=1] The number of top predictions to be returned. + * + * @callback TextClassificationPipelineCallback Classify the text(s) given as inputs. + * @param {string|string[]} texts The input text(s) to be classified. + * @param {TextClassificationPipelineOptions} [options] The options to use for text classification. + * @returns {Promise} An array or object containing the predicted labels and scores. + * + * @typedef {TextPipelineConstructorArgs & TextClassificationPipelineCallback & Disposable} TextClassificationPipelineType + */ + +/** + * Text classification pipeline using any `ModelForSequenceClassification`. + * + * **Example:** Sentiment-analysis w/ `Xenova/distilbert-base-uncased-finetuned-sst-2-english`. + * ```javascript + * const classifier = await pipeline('sentiment-analysis', 'Xenova/distilbert-base-uncased-finetuned-sst-2-english'); + * const output = await classifier('I love transformers!'); + * // [{ label: 'POSITIVE', score: 0.999788761138916 }] + * ``` + * + * **Example:** Multilingual sentiment-analysis w/ `Xenova/bert-base-multilingual-uncased-sentiment` (and return top 5 classes). + * ```javascript + * const classifier = await pipeline('sentiment-analysis', 'Xenova/bert-base-multilingual-uncased-sentiment'); + * const output = await classifier('Le meilleur film de tous les temps.', { top_k: 5 }); + * // [ + * // { label: '5 stars', score: 0.9610759615898132 }, + * // { label: '4 stars', score: 0.03323351591825485 }, + * // { label: '3 stars', score: 0.0036155181005597115 }, + * // { label: '1 star', score: 0.0011325967498123646 }, + * // { label: '2 stars', score: 0.0009423971059732139 } + * // ] + * ``` + * + * **Example:** Toxic comment classification w/ `Xenova/toxic-bert` (and return all classes). + * ```javascript + * const classifier = await pipeline('text-classification', 'Xenova/toxic-bert'); + * const output = await classifier('I hate you!', { top_k: null }); + * // [ + * // { label: 'toxic', score: 0.9593140482902527 }, + * // { label: 'insult', score: 0.16187334060668945 }, + * // { label: 'obscene', score: 0.03452680632472038 }, + * // { label: 'identity_hate', score: 0.0223250575363636 }, + * // { label: 'threat', score: 0.019197041168808937 }, + * // { label: 'severe_toxic', score: 0.005651099607348442 } + * // ] + * ``` + */ +class TextClassificationPipeline extends (/** @type {new (options: TextPipelineConstructorArgs) => TextClassificationPipelineType} */ (Pipeline)) { + + /** + * Create a new TextClassificationPipeline. + * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {TextClassificationPipelineCallback} */ + async _call(texts, { + top_k = 1 + } = {}) { + + // Run tokenization + const model_inputs = this.tokenizer(texts, { + padding: true, + truncation: true, + }); + + // Run model + const outputs = await this.model(model_inputs) + + // TODO: Use softmax tensor function + const function_to_apply = + this.model.config.problem_type === 'multi_label_classification' + ? batch => batch.sigmoid() + : batch => new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.Tensor( + 'float32', + (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(batch.data), + batch.dims, + ); // single_label_classification (default) + + const id2label = this.model.config.id2label; + + const toReturn = []; + for (const batch of outputs.logits) { + const output = function_to_apply(batch); + + const scores = await (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.topk)(output, top_k); + + const values = scores[0].tolist(); + const indices = scores[1].tolist(); + const vals = indices.map((x, i) => ({ + label: id2label ? id2label[x] : `LABEL_${x}`, + score: values[i], + })); + if (top_k === 1) { + toReturn.push(...vals); + } else { + toReturn.push(vals); + } + } + + return Array.isArray(texts) || top_k === 1 ? /** @type {TextClassificationOutput} */ (toReturn) : /** @type {TextClassificationOutput[]} */ (toReturn)[0]; + } +} + +/** + * @typedef {Object} TokenClassificationSingle + * @property {string} word The token/word classified. This is obtained by decoding the selected tokens. + * @property {number} score The corresponding probability for `entity`. + * @property {string} entity The entity predicted for that token/word. + * @property {number} index The index of the corresponding token in the sentence. + * @property {number} [start] The index of the start of the corresponding entity in the sentence. + * @property {number} [end] The index of the end of the corresponding entity in the sentence. + * @typedef {TokenClassificationSingle[]} TokenClassificationOutput + * + * @typedef {Object} TokenClassificationPipelineOptions Parameters specific to token classification pipelines. + * @property {string[]} [ignore_labels] A list of labels to ignore. + * + * @callback TokenClassificationPipelineCallback Classify each token of the text(s) given as inputs. + * @param {string|string[]} texts One or several texts (or one list of texts) for token classification. + * @param {TokenClassificationPipelineOptions} [options] The options to use for token classification. + * @returns {Promise} The result. + * + * @typedef {TextPipelineConstructorArgs & TokenClassificationPipelineCallback & Disposable} TokenClassificationPipelineType + */ + +/** + * Named Entity Recognition pipeline using any `ModelForTokenClassification`. + * + * **Example:** Perform named entity recognition with `Xenova/bert-base-NER`. + * ```javascript + * const classifier = await pipeline('token-classification', 'Xenova/bert-base-NER'); + * const output = await classifier('My name is Sarah and I live in London'); + * // [ + * // { entity: 'B-PER', score: 0.9980202913284302, index: 4, word: 'Sarah' }, + * // { entity: 'B-LOC', score: 0.9994474053382874, index: 9, word: 'London' } + * // ] + * ``` + * + * **Example:** Perform named entity recognition with `Xenova/bert-base-NER` (and return all labels). + * ```javascript + * const classifier = await pipeline('token-classification', 'Xenova/bert-base-NER'); + * const output = await classifier('Sarah lives in the United States of America', { ignore_labels: [] }); + * // [ + * // { entity: 'B-PER', score: 0.9966587424278259, index: 1, word: 'Sarah' }, + * // { entity: 'O', score: 0.9987385869026184, index: 2, word: 'lives' }, + * // { entity: 'O', score: 0.9990072846412659, index: 3, word: 'in' }, + * // { entity: 'O', score: 0.9988298416137695, index: 4, word: 'the' }, + * // { entity: 'B-LOC', score: 0.9995510578155518, index: 5, word: 'United' }, + * // { entity: 'I-LOC', score: 0.9990395307540894, index: 6, word: 'States' }, + * // { entity: 'I-LOC', score: 0.9986724853515625, index: 7, word: 'of' }, + * // { entity: 'I-LOC', score: 0.9975294470787048, index: 8, word: 'America' } + * // ] + * ``` + */ +class TokenClassificationPipeline extends (/** @type {new (options: TextPipelineConstructorArgs) => TokenClassificationPipelineType} */ (Pipeline)) { + + /** + * Create a new TokenClassificationPipeline. + * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {TokenClassificationPipelineCallback} */ + async _call(texts, { + ignore_labels = ['O'], + } = {}) { + + const isBatched = Array.isArray(texts); + + // Run tokenization + const model_inputs = this.tokenizer(isBatched ? texts : [texts], { + padding: true, + truncation: true, + }); + + // Run model + const outputs = await this.model(model_inputs) + + const logits = outputs.logits; + const id2label = this.model.config.id2label; + + const toReturn = []; + for (let i = 0; i < logits.dims[0]; ++i) { + const ids = model_inputs.input_ids[i]; + const batch = logits[i]; + + // List of tokens that aren't ignored + const tokens = []; + for (let j = 0; j < batch.dims[0]; ++j) { + const tokenData = batch[j]; + const topScoreIndex = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.max)(tokenData.data)[1]; + + const entity = id2label ? id2label[topScoreIndex] : `LABEL_${topScoreIndex}`; + if (ignore_labels.includes(entity)) { + // We predicted a token that should be ignored. So, we skip it. + continue; + } + + // TODO add option to keep special tokens? + const word = this.tokenizer.decode([ids[j].item()], { skip_special_tokens: true }); + if (word === '') { + // Was a special token. So, we skip it. + continue; + } + + const scores = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(tokenData.data); + + tokens.push({ + entity: entity, + score: scores[topScoreIndex], + index: j, + word: word, + + // TODO: Add support for start and end + // start: null, + // end: null, + }); + } + toReturn.push(tokens); + } + return isBatched ? toReturn : toReturn[0]; + } +} + +/** + * @typedef {Object} QuestionAnsweringOutput + * @property {number} score The probability associated to the answer. + * @property {number} [start] The character start index of the answer (in the tokenized version of the input). + * @property {number} [end] The character end index of the answer (in the tokenized version of the input). + * @property {string} answer The answer to the question. + * + * @typedef {Object} QuestionAnsweringPipelineOptions Parameters specific to question answering pipelines. + * @property {number} [top_k=1] The number of top answer predictions to be returned. + * + * @callback QuestionAnsweringPipelineCallback Answer the question(s) given as inputs by using the context(s). + * @param {string|string[]} question One or several question(s) (must be used in conjunction with the `context` argument). + * @param {string|string[]} context One or several context(s) associated with the question(s) (must be used in conjunction with the `question` argument). + * @param {QuestionAnsweringPipelineOptions} [options] The options to use for question answering. + * @returns {Promise} An array or object containing the predicted answers and scores. + * + * @typedef {TextPipelineConstructorArgs & QuestionAnsweringPipelineCallback & Disposable} QuestionAnsweringPipelineType + */ + +/** + * Question Answering pipeline using any `ModelForQuestionAnswering`. + * + * **Example:** Run question answering with `Xenova/distilbert-base-uncased-distilled-squad`. + * ```javascript + * const answerer = await pipeline('question-answering', 'Xenova/distilbert-base-uncased-distilled-squad'); + * const question = 'Who was Jim Henson?'; + * const context = 'Jim Henson was a nice puppet.'; + * const output = await answerer(question, context); + * // { + * // answer: "a nice puppet", + * // score: 0.5768911502526741 + * // } + * ``` + */ +class QuestionAnsweringPipeline extends (/** @type {new (options: TextPipelineConstructorArgs) => QuestionAnsweringPipelineType} */ (Pipeline)) { + + /** + * Create a new QuestionAnsweringPipeline. + * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {QuestionAnsweringPipelineCallback} */ + async _call(question, context, { + top_k = 1 + } = {}) { + + // Run tokenization + const inputs = this.tokenizer(question, { + text_pair: context, + padding: true, + truncation: true, + }); + + const { start_logits, end_logits } = await this.model(inputs); + const input_ids = inputs.input_ids.tolist(); + const attention_mask = inputs.attention_mask.tolist(); + + // TODO: add support for `return_special_tokens_mask` + const special_tokens = this.tokenizer.all_special_ids; + + /** @type {QuestionAnsweringOutput[]} */ + const toReturn = []; + for (let j = 0; j < start_logits.dims[0]; ++j) { + const ids = input_ids[j]; + const sepIndex = ids.findIndex(x => + // We use == to match bigint with number + // @ts-ignore + x == this.tokenizer.sep_token_id + ); + + + const valid_mask = attention_mask[j].map((y, ix) => ( + y == 1 + && ( + ix === 0 // is cls_token + || ( + ix > sepIndex + && special_tokens.findIndex(x => x == ids[ix]) === -1 // token is not a special token (special_tokens_mask == 0) + ) + ) + )); + + const start = start_logits[j].tolist(); + const end = end_logits[j].tolist(); + + // Now, we mask out values that can't be in the answer + // NOTE: We keep the cls_token unmasked (some models use it to indicate unanswerable questions) + for (let i = 1; i < start.length; ++i) { + if ( + attention_mask[j] == 0 // is part of padding + || i <= sepIndex // is before the sep_token + || special_tokens.findIndex(x => x == ids[i]) !== -1 // Is a special token + ) { + // Make sure non-context indexes in the tensor cannot contribute to the softmax + start[i] = -Infinity; + end[i] = -Infinity; + } + } + + // Normalize logits and spans to retrieve the answer + const start_scores = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(start).map((x, i) => [x, i]); + const end_scores = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(end).map((x, i) => [x, i]); + + // Mask CLS + start_scores[0][0] = 0; + end_scores[0][0] = 0; + + // Generate all valid spans and select best ones + const options = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.product)(start_scores, end_scores) + .filter(x => x[0][1] <= x[1][1]) + .map(x => [x[0][1], x[1][1], x[0][0] * x[1][0]]) + .sort((a, b) => b[2] - a[2]); + + for (let k = 0; k < Math.min(options.length, top_k); ++k) { + const [start, end, score] = options[k]; + + const answer_tokens = ids.slice(start, end + 1) + + const answer = this.tokenizer.decode(answer_tokens, { + skip_special_tokens: true, + }); + + // TODO add start and end? + // NOTE: HF returns character index + toReturn.push({ + answer, score + }); + } + } + + // Mimic HF's return type based on top_k + return (top_k === 1) ? toReturn[0] : toReturn; + } +} + + +/** + * @typedef {Object} FillMaskSingle + * @property {string} sequence The corresponding input with the mask token prediction. + * @property {number} score The corresponding probability. + * @property {number} token The predicted token id (to replace the masked one). + * @property {string} token_str The predicted token (to replace the masked one). + * @typedef {FillMaskSingle[]} FillMaskOutput + * + * @typedef {Object} FillMaskPipelineOptions Parameters specific to fill mask pipelines. + * @property {number} [top_k=5] When passed, overrides the number of predictions to return. + * + * @callback FillMaskPipelineCallback Fill the masked token in the text(s) given as inputs. + * @param {string|string[]} texts One or several texts (or one list of prompts) with masked tokens. + * @param {FillMaskPipelineOptions} [options] The options to use for masked language modelling. + * @returns {Promise} An array of objects containing the score, predicted token, predicted token string, + * and the sequence with the predicted token filled in, or an array of such arrays (one for each input text). + * If only one input text is given, the output will be an array of objects. + * @throws {Error} When the mask token is not found in the input text. + * + * @typedef {TextPipelineConstructorArgs & FillMaskPipelineCallback & Disposable} FillMaskPipelineType + */ + +/** + * Masked language modeling prediction pipeline using any `ModelWithLMHead`. + * + * **Example:** Perform masked language modelling (a.k.a. "fill-mask") with `Xenova/bert-base-uncased`. + * ```javascript + * const unmasker = await pipeline('fill-mask', 'Xenova/bert-base-cased'); + * const output = await unmasker('The goal of life is [MASK].'); + * // [ + * // { token_str: 'survival', score: 0.06137419492006302, token: 8115, sequence: 'The goal of life is survival.' }, + * // { token_str: 'love', score: 0.03902450203895569, token: 1567, sequence: 'The goal of life is love.' }, + * // { token_str: 'happiness', score: 0.03253183513879776, token: 9266, sequence: 'The goal of life is happiness.' }, + * // { token_str: 'freedom', score: 0.018736306577920914, token: 4438, sequence: 'The goal of life is freedom.' }, + * // { token_str: 'life', score: 0.01859794743359089, token: 1297, sequence: 'The goal of life is life.' } + * // ] + * ``` + * + * **Example:** Perform masked language modelling (a.k.a. "fill-mask") with `Xenova/bert-base-cased` (and return top result). + * ```javascript + * const unmasker = await pipeline('fill-mask', 'Xenova/bert-base-cased'); + * const output = await unmasker('The Milky Way is a [MASK] galaxy.', { top_k: 1 }); + * // [{ token_str: 'spiral', score: 0.6299987435340881, token: 14061, sequence: 'The Milky Way is a spiral galaxy.' }] + * ``` + */ +class FillMaskPipeline extends (/** @type {new (options: TextPipelineConstructorArgs) => FillMaskPipelineType} */ (Pipeline)) { + + /** + * Create a new FillMaskPipeline. + * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {FillMaskPipelineCallback} */ + async _call(texts, { + top_k = 5 + } = {}) { + + // Run tokenization + const model_inputs = this.tokenizer(texts, { + padding: true, + truncation: true, + }); + + // Run model + const { logits } = await this.model(model_inputs) + + const toReturn = []; + + /** @type {bigint[][]} */ + const input_ids = model_inputs.input_ids.tolist(); + for (let i = 0; i < input_ids.length; ++i) { + const ids = input_ids[i]; + const mask_token_index = ids.findIndex(x => + // We use == to match bigint with number + // @ts-ignore + x == this.tokenizer.mask_token_id + ); + if (mask_token_index === -1) { + throw Error(`Mask token (${this.tokenizer.mask_token}) not found in text.`) + } + const itemLogits = logits[i][mask_token_index]; + + const scores = await (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.topk)(new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.Tensor( + 'float32', + (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(itemLogits.data), + itemLogits.dims, + ), top_k); + const values = scores[0].tolist(); + const indices = scores[1].tolist(); + + toReturn.push(indices.map((x, i) => { + const sequence = ids.slice(); + sequence[mask_token_index] = x; + + return { + score: values[i], + token: Number(x), + token_str: this.tokenizer.model.vocab[x], + sequence: this.tokenizer.decode(sequence, { skip_special_tokens: true }), + } + })); + } + return Array.isArray(texts) ? toReturn : toReturn[0]; + } +} + + +/** + * @typedef {Object} Text2TextGenerationSingle + * @property {string} generated_text The generated text. + * @typedef {Text2TextGenerationSingle[]} Text2TextGenerationOutput + * + * @callback Text2TextGenerationPipelineCallback Generate the output text(s) using text(s) given as inputs. + * @param {string|string[]} texts Input text for the encoder. + * @param {Partial} [options] Additional keyword arguments to pass along to the generate method of the model. + * @returns {Promise} + * + * @typedef {TextPipelineConstructorArgs & Text2TextGenerationPipelineCallback & Disposable} Text2TextGenerationPipelineType + */ + +/** + * Text2TextGenerationPipeline class for generating text using a model that performs text-to-text generation tasks. + * + * **Example:** Text-to-text generation w/ `Xenova/LaMini-Flan-T5-783M`. + * ```javascript + * const generator = await pipeline('text2text-generation', 'Xenova/LaMini-Flan-T5-783M'); + * const output = await generator('how can I become more healthy?', { + * max_new_tokens: 100, + * }); + * // [{ generated_text: "To become more healthy, you can: 1. Eat a balanced diet with plenty of fruits, vegetables, whole grains, lean proteins, and healthy fats. 2. Stay hydrated by drinking plenty of water. 3. Get enough sleep and manage stress levels. 4. Avoid smoking and excessive alcohol consumption. 5. Regularly exercise and maintain a healthy weight. 6. Practice good hygiene and sanitation. 7. Seek medical attention if you experience any health issues." }] + * ``` + */ +class Text2TextGenerationPipeline extends (/** @type {new (options: TextPipelineConstructorArgs) => Text2TextGenerationPipelineType} */ (Pipeline)) { + /** @type {'generated_text'} */ + _key = 'generated_text'; + + /** + * Create a new Text2TextGenerationPipeline. + * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {Text2TextGenerationPipelineCallback} */ + async _call(texts, generate_kwargs = {}) { + if (!Array.isArray(texts)) { + texts = [texts]; + } + + + // Add global prefix, if present + if (this.model.config.prefix) { + texts = texts.map(x => this.model.config.prefix + x) + } + + // Handle task specific params: + const task_specific_params = this.model.config.task_specific_params + if (task_specific_params && task_specific_params[this.task]) { + // Add prefixes, if present + if (task_specific_params[this.task].prefix) { + texts = texts.map(x => task_specific_params[this.task].prefix + x) + } + + // TODO update generation config + } + + const tokenizer = this.tokenizer; + const tokenizer_options = { + padding: true, + truncation: true, + } + let inputs; + if (this instanceof TranslationPipeline && '_build_translation_inputs' in tokenizer) { + // TODO: move to Translation pipeline? + // Currently put here to avoid code duplication + // @ts-ignore + inputs = tokenizer._build_translation_inputs(texts, tokenizer_options, generate_kwargs); + + } else { + inputs = tokenizer(texts, tokenizer_options); + } + + const outputTokenIds = await this.model.generate({ ...inputs, ...generate_kwargs }); + return tokenizer.batch_decode(/** @type {Tensor} */(outputTokenIds), { + skip_special_tokens: true, + }).map(text => ({ [this._key]: text })); + } +} + + +/** + * @typedef {Object} SummarizationSingle + * @property {string} summary_text The summary text. + * @typedef {SummarizationSingle[]} SummarizationOutput + * + * @callback SummarizationPipelineCallback Summarize the text(s) given as inputs. + * @param {string|string[]} texts One or several articles (or one list of articles) to summarize. + * @param {import('./generation/configuration_utils.js').GenerationConfig} [options] Additional keyword arguments to pass along to the generate method of the model. + * @returns {Promise} + * + * @typedef {TextPipelineConstructorArgs & SummarizationPipelineCallback & Disposable} SummarizationPipelineType + */ + +/** + * A pipeline for summarization tasks, inheriting from Text2TextGenerationPipeline. + * + * **Example:** Summarization w/ `Xenova/distilbart-cnn-6-6`. + * ```javascript + * const generator = await pipeline('summarization', 'Xenova/distilbart-cnn-6-6'); + * const text = 'The tower is 324 metres (1,063 ft) tall, about the same height as an 81-storey building, ' + + * 'and the tallest structure in Paris. Its base is square, measuring 125 metres (410 ft) on each side. ' + + * 'During its construction, the Eiffel Tower surpassed the Washington Monument to become the tallest ' + + * 'man-made structure in the world, a title it held for 41 years until the Chrysler Building in New ' + + * 'York City was finished in 1930. It was the first structure to reach a height of 300 metres. Due to ' + + * 'the addition of a broadcasting aerial at the top of the tower in 1957, it is now taller than the ' + + * 'Chrysler Building by 5.2 metres (17 ft). Excluding transmitters, the Eiffel Tower is the second ' + + * 'tallest free-standing structure in France after the Millau Viaduct.'; + * const output = await generator(text, { + * max_new_tokens: 100, + * }); + * // [{ summary_text: ' The Eiffel Tower is about the same height as an 81-storey building and the tallest structure in Paris. It is the second tallest free-standing structure in France after the Millau Viaduct.' }] + * ``` + */ +class SummarizationPipeline extends (/** @type {new (options: TextPipelineConstructorArgs) => SummarizationPipelineType} */ (/** @type {any} */ (Text2TextGenerationPipeline))) { + /** @type {'summary_text'} */ + _key = 'summary_text'; + + /** + * Create a new SummarizationPipeline. + * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } +} + + +/** + * @typedef {Object} TranslationSingle + * @property {string} translation_text The translated text. + * @typedef {TranslationSingle[]} TranslationOutput + * + * @callback TranslationPipelineCallback Translate the text(s) given as inputs. + * @param {string|string[]} texts Texts to be translated. + * @param {import('./generation/configuration_utils.js').GenerationConfig} [options] Additional keyword arguments to pass along to the generate method of the model. + * @returns {Promise} + * + * @typedef {TextPipelineConstructorArgs & TranslationPipelineCallback & Disposable} TranslationPipelineType + */ + +/** + * Translates text from one language to another. + * + * **Example:** Multilingual translation w/ `Xenova/nllb-200-distilled-600M`. + * + * See [here](https://github.com/facebookresearch/flores/blob/main/flores200/README.md#languages-in-flores-200) + * for the full list of languages and their corresponding codes. + * + * ```javascript + * const translator = await pipeline('translation', 'Xenova/nllb-200-distilled-600M'); + * const output = await translator('जीवन एक चॉकलेट बॉक्स की तरह है।', { + * src_lang: 'hin_Deva', // Hindi + * tgt_lang: 'fra_Latn', // French + * }); + * // [{ translation_text: 'La vie est comme une boîte à chocolat.' }] + * ``` + * + * **Example:** Multilingual translation w/ `Xenova/m2m100_418M`. + * + * See [here](https://huggingface.co/facebook/m2m100_418M#languages-covered) + * for the full list of languages and their corresponding codes. + * + * ```javascript + * const translator = await pipeline('translation', 'Xenova/m2m100_418M'); + * const output = await translator('生活就像一盒巧克力。', { + * src_lang: 'zh', // Chinese + * tgt_lang: 'en', // English + * }); + * // [{ translation_text: 'Life is like a box of chocolate.' }] + * ``` + * + * **Example:** Multilingual translation w/ `Xenova/mbart-large-50-many-to-many-mmt`. + * + * See [here](https://huggingface.co/facebook/mbart-large-50-many-to-many-mmt#languages-covered) + * for the full list of languages and their corresponding codes. + * + * ```javascript + * const translator = await pipeline('translation', 'Xenova/mbart-large-50-many-to-many-mmt'); + * const output = await translator('संयुक्त राष्ट्र के प्रमुख का कहना है कि सीरिया में कोई सैन्य समाधान नहीं है', { + * src_lang: 'hi_IN', // Hindi + * tgt_lang: 'fr_XX', // French + * }); + * // [{ translation_text: 'Le chef des Nations affirme qu 'il n 'y a military solution in Syria.' }] + * ``` + */ +class TranslationPipeline extends (/** @type {new (options: TextPipelineConstructorArgs) => TranslationPipelineType} */ (/** @type {any} */ (Text2TextGenerationPipeline))) { + /** @type {'translation_text'} */ + _key = 'translation_text'; + + /** + * Create a new TranslationPipeline. + * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } +} + +function isChat(x) { + return Array.isArray(x) && x.every(x => 'role' in x && 'content' in x); +} + +/** + * @typedef {import('./tokenizers.js').Message[]} Chat + * + * @typedef {Object} TextGenerationSingle + * @property {string|Chat} generated_text The generated text. + * @typedef {TextGenerationSingle[]} TextGenerationOutput + * + * @typedef {Object} TextGenerationSpecificParams Parameters specific to text-generation pipelines. + * @property {boolean} [add_special_tokens] Whether or not to add special tokens when tokenizing the sequences. + * @property {boolean} [return_full_text=true] If set to `false` only added text is returned, otherwise the full text is returned. + * @typedef {import('./generation/configuration_utils.js').GenerationConfig & TextGenerationSpecificParams} TextGenerationConfig + * + * @callback TextGenerationPipelineCallback Complete the prompt(s) given as inputs. + * @param {string|string[]|Chat|Chat[]} texts One or several prompts (or one list of prompts) to complete. + * @param {Partial} [options] Additional keyword arguments to pass along to the generate method of the model. + * @returns {Promise} An array or object containing the generated texts. + * + * @typedef {TextPipelineConstructorArgs & TextGenerationPipelineCallback & Disposable} TextGenerationPipelineType + */ + +/** + * Language generation pipeline using any `ModelWithLMHead` or `ModelForCausalLM`. + * This pipeline predicts the words that will follow a specified text prompt. + * NOTE: For the full list of generation parameters, see [`GenerationConfig`](./utils/generation#module_utils/generation.GenerationConfig). + * + * **Example:** Text generation with `Xenova/distilgpt2` (default settings). + * ```javascript + * const generator = await pipeline('text-generation', 'Xenova/distilgpt2'); + * const text = 'I enjoy walking with my cute dog,'; + * const output = await generator(text); + * // [{ generated_text: "I enjoy walking with my cute dog, and I love to play with the other dogs." }] + * ``` + * + * **Example:** Text generation with `Xenova/distilgpt2` (custom settings). + * ```javascript + * const generator = await pipeline('text-generation', 'Xenova/distilgpt2'); + * const text = 'Once upon a time, there was'; + * const output = await generator(text, { + * temperature: 2, + * max_new_tokens: 10, + * repetition_penalty: 1.5, + * no_repeat_ngram_size: 2, + * num_beams: 2, + * num_return_sequences: 2, + * }); + * // [{ + * // "generated_text": "Once upon a time, there was an abundance of information about the history and activities that" + * // }, { + * // "generated_text": "Once upon a time, there was an abundance of information about the most important and influential" + * // }] + * ``` + * + * **Example:** Run code generation with `Xenova/codegen-350M-mono`. + * ```javascript + * const generator = await pipeline('text-generation', 'Xenova/codegen-350M-mono'); + * const text = 'def fib(n):'; + * const output = await generator(text, { + * max_new_tokens: 44, + * }); + * // [{ + * // generated_text: 'def fib(n):\n' + + * // ' if n == 0:\n' + + * // ' return 0\n' + + * // ' elif n == 1:\n' + + * // ' return 1\n' + + * // ' else:\n' + + * // ' return fib(n-1) + fib(n-2)\n' + * // }] + * ``` + */ +class TextGenerationPipeline extends (/** @type {new (options: TextPipelineConstructorArgs) => TextGenerationPipelineType} */ (Pipeline)) { + + /** + * Create a new TextGenerationPipeline. + * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {TextGenerationPipelineCallback} */ + async _call(texts, generate_kwargs = {}) { + let isBatched = false; + let isChatInput = false; + + // Normalize inputs + /** @type {string[]} */ + let inputs; + if (typeof texts === 'string') { + inputs = texts = [texts]; + } else if (Array.isArray(texts) && texts.every(x => typeof x === 'string')) { + isBatched = true; + inputs = /** @type {string[]} */(texts); + } else { + if (isChat(texts)) { + texts = [/** @type {Chat} */(texts)]; + } else if (Array.isArray(texts) && texts.every(isChat)) { + isBatched = true; + } else { + throw new Error('Input must be a string, an array of strings, a Chat, or an array of Chats'); + } + isChatInput = true; + + // If the input is a chat, we need to apply the chat template + inputs = /** @type {string[]} */(/** @type {Chat[]} */ (texts).map( + x => this.tokenizer.apply_chat_template(x, { + tokenize: false, + add_generation_prompt: true, + }) + )); + } + + // By default, do not add special tokens + const add_special_tokens = generate_kwargs.add_special_tokens ?? false; + + // By default, return full text + const return_full_text = isChatInput + ? false + : generate_kwargs.return_full_text ?? true; + + this.tokenizer.padding_side = 'left'; + const text_inputs = this.tokenizer(inputs, { + add_special_tokens, + padding: true, + truncation: true, + }); + + const outputTokenIds = /** @type {Tensor} */(await this.model.generate({ + ...text_inputs, + ...generate_kwargs + })); + + const decoded = this.tokenizer.batch_decode(outputTokenIds, { + skip_special_tokens: true, + }); + + let promptLengths; + if (!return_full_text && text_inputs.input_ids.dims.at(-1) > 0) { + promptLengths = this.tokenizer.batch_decode(text_inputs.input_ids, { + skip_special_tokens: true, + }).map(x => x.length); + } + + /** @type {TextGenerationOutput[]} */ + const toReturn = Array.from({ length: texts.length }, _ => []); + for (let i = 0; i < decoded.length; ++i) { + const textIndex = Math.floor(i / outputTokenIds.dims[0] * texts.length); + + if (promptLengths) { + // Trim the decoded text to only include the generated part + decoded[i] = decoded[i].slice(promptLengths[textIndex]); + } + toReturn[textIndex].push({ + generated_text: isChatInput + ? [ + ...((/** @type {Chat[]} */(texts)[textIndex])), + { role: 'assistant', content: decoded[i] }, + ] + : decoded[i] + }); + } + return (!isBatched && toReturn.length === 1) ? toReturn[0] : toReturn; + } +} + +/** + * @typedef {Object} ZeroShotClassificationOutput + * @property {string} sequence The sequence for which this is the output. + * @property {string[]} labels The labels sorted by order of likelihood. + * @property {number[]} scores The probabilities for each of the labels. + * + * @typedef {Object} ZeroShotClassificationPipelineOptions Parameters specific to zero-shot classification pipelines. + * @property {string} [hypothesis_template="This example is {}."] The template used to turn each + * candidate label into an NLI-style hypothesis. The candidate label will replace the {} placeholder. + * @property {boolean} [multi_label=false] Whether or not multiple candidate labels can be true. + * If `false`, the scores are normalized such that the sum of the label likelihoods for each sequence + * is 1. If `true`, the labels are considered independent and probabilities are normalized for each + * candidate by doing a softmax of the entailment score vs. the contradiction score. + * + * @callback ZeroShotClassificationPipelineCallback Classify the sequence(s) given as inputs. + * @param {string|string[]} texts The sequence(s) to classify, will be truncated if the model input is too large. + * @param {string|string[]} candidate_labels The set of possible class labels to classify each sequence into. + * Can be a single label, a string of comma-separated labels, or a list of labels. + * @param {ZeroShotClassificationPipelineOptions} [options] The options to use for zero-shot classification. + * @returns {Promise} An array or object containing the predicted labels and scores. + * + * @typedef {TextPipelineConstructorArgs & ZeroShotClassificationPipelineCallback & Disposable} ZeroShotClassificationPipelineType + */ + +/** + * NLI-based zero-shot classification pipeline using a `ModelForSequenceClassification` + * trained on NLI (natural language inference) tasks. Equivalent of `text-classification` + * pipelines, but these models don't require a hardcoded number of potential classes, they + * can be chosen at runtime. It usually means it's slower but it is **much** more flexible. + * + * **Example:** Zero shot classification with `Xenova/mobilebert-uncased-mnli`. + * ```javascript + * const classifier = await pipeline('zero-shot-classification', 'Xenova/mobilebert-uncased-mnli'); + * const text = 'Last week I upgraded my iOS version and ever since then my phone has been overheating whenever I use your app.'; + * const labels = [ 'mobile', 'billing', 'website', 'account access' ]; + * const output = await classifier(text, labels); + * // { + * // sequence: 'Last week I upgraded my iOS version and ever since then my phone has been overheating whenever I use your app.', + * // labels: [ 'mobile', 'website', 'billing', 'account access' ], + * // scores: [ 0.5562091040482018, 0.1843621307860853, 0.13942646639336376, 0.12000229877234923 ] + * // } + * ``` + * + * **Example:** Zero shot classification with `Xenova/nli-deberta-v3-xsmall` (multi-label). + * ```javascript + * const classifier = await pipeline('zero-shot-classification', 'Xenova/nli-deberta-v3-xsmall'); + * const text = 'I have a problem with my iphone that needs to be resolved asap!'; + * const labels = [ 'urgent', 'not urgent', 'phone', 'tablet', 'computer' ]; + * const output = await classifier(text, labels, { multi_label: true }); + * // { + * // sequence: 'I have a problem with my iphone that needs to be resolved asap!', + * // labels: [ 'urgent', 'phone', 'computer', 'tablet', 'not urgent' ], + * // scores: [ 0.9958870956360275, 0.9923963400697035, 0.002333537946160235, 0.0015134138567598765, 0.0010699384208377163 ] + * // } + * ``` + */ +class ZeroShotClassificationPipeline extends (/** @type {new (options: TextPipelineConstructorArgs) => ZeroShotClassificationPipelineType} */ (Pipeline)) { + /** + * Create a new ZeroShotClassificationPipeline. + * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + + // Use model config to get label2id mapping + this.label2id = Object.fromEntries( + Object.entries((/** @type {any} */(this).model).config.label2id).map( + ([k, v]) => [k.toLowerCase(), v] + ) + ); + + this.entailment_id = this.label2id['entailment']; + if (this.entailment_id === undefined) { + console.warn("Could not find 'entailment' in label2id mapping. Using 2 as entailment_id."); + this.entailment_id = 2; + } + + this.contradiction_id = this.label2id['contradiction'] ?? this.label2id['not_entailment']; + if (this.contradiction_id === undefined) { + console.warn("Could not find 'contradiction' in label2id mapping. Using 0 as contradiction_id."); + this.contradiction_id = 0; + } + } + + /** @type {ZeroShotClassificationPipelineCallback} */ + async _call(texts, candidate_labels, { + hypothesis_template = "This example is {}.", + multi_label = false, + } = {}) { + + const isBatched = Array.isArray(texts); + if (!isBatched) { + texts = [/** @type {string} */ (texts)]; + } + if (!Array.isArray(candidate_labels)) { + candidate_labels = [candidate_labels]; + } + + // Insert labels into hypothesis template + const hypotheses = candidate_labels.map( + x => hypothesis_template.replace('{}', x) + ); + + // How to perform the softmax over the logits: + // - true: softmax over the entailment vs. contradiction dim for each label independently + // - false: softmax the "entailment" logits over all candidate labels + const softmaxEach = multi_label || candidate_labels.length === 1; + + /** @type {ZeroShotClassificationOutput[]} */ + const toReturn = []; + for (const premise of texts) { + const entails_logits = []; + + for (const hypothesis of hypotheses) { + const inputs = this.tokenizer(premise, { + text_pair: hypothesis, + padding: true, + truncation: true, + }) + const outputs = await this.model(inputs) + + if (softmaxEach) { + entails_logits.push([ + outputs.logits.data[this.contradiction_id], + outputs.logits.data[this.entailment_id] + ]) + } else { + entails_logits.push(outputs.logits.data[this.entailment_id]) + } + } + + /** @type {number[]} */ + const scores = softmaxEach + ? entails_logits.map(x => (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(x)[1]) + : (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(entails_logits); + + // Sort by scores (desc) and return scores with indices + const scores_sorted = scores + .map((x, i) => [x, i]) + .sort((a, b) => (b[0] - a[0])); + + toReturn.push({ + sequence: premise, + labels: scores_sorted.map(x => candidate_labels[x[1]]), + scores: scores_sorted.map(x => x[0]), + }); + } + return isBatched ? toReturn : toReturn[0]; + } +} + +/** + * @typedef {Object} FeatureExtractionPipelineOptions Parameters specific to feature extraction pipelines. + * @property {'none'|'mean'|'cls'} [pooling="none"] The pooling method to use. + * @property {boolean} [normalize=false] Whether or not to normalize the embeddings in the last dimension. + * @property {boolean} [quantize=false] Whether or not to quantize the embeddings. + * @property {'binary'|'ubinary'} [precision='binary'] The precision to use for quantization. + * + * @callback FeatureExtractionPipelineCallback Extract the features of the input(s). + * @param {string|string[]} texts One or several texts (or one list of texts) to get the features of. + * @param {FeatureExtractionPipelineOptions} [options] The options to use for feature extraction. + * @returns {Promise} The features computed by the model. + * + * @typedef {TextPipelineConstructorArgs & FeatureExtractionPipelineCallback & Disposable} FeatureExtractionPipelineType + */ + +/** + * Feature extraction pipeline using no model head. This pipeline extracts the hidden + * states from the base transformer, which can be used as features in downstream tasks. + * + * **Example:** Run feature extraction with `bert-base-uncased` (without pooling/normalization). + * ```javascript + * const extractor = await pipeline('feature-extraction', 'Xenova/bert-base-uncased', { revision: 'default' }); + * const output = await extractor('This is a simple test.'); + * // Tensor { + * // type: 'float32', + * // data: Float32Array [0.05939924716949463, 0.021655935794115067, ...], + * // dims: [1, 8, 768] + * // } + * ``` + * + * **Example:** Run feature extraction with `bert-base-uncased` (with pooling/normalization). + * ```javascript + * const extractor = await pipeline('feature-extraction', 'Xenova/bert-base-uncased', { revision: 'default' }); + * const output = await extractor('This is a simple test.', { pooling: 'mean', normalize: true }); + * // Tensor { + * // type: 'float32', + * // data: Float32Array [0.03373778983950615, -0.010106077417731285, ...], + * // dims: [1, 768] + * // } + * ``` + * + * **Example:** Calculating embeddings with `sentence-transformers` models. + * ```javascript + * const extractor = await pipeline('feature-extraction', 'Xenova/all-MiniLM-L6-v2'); + * const output = await extractor('This is a simple test.', { pooling: 'mean', normalize: true }); + * // Tensor { + * // type: 'float32', + * // data: Float32Array [0.09094982594251633, -0.014774246141314507, ...], + * // dims: [1, 384] + * // } + * ``` + * **Example:** Calculating binary embeddings with `sentence-transformers` models. + * ```javascript + * const extractor = await pipeline('feature-extraction', 'Xenova/all-MiniLM-L6-v2'); + * const output = await extractor('This is a simple test.', { pooling: 'mean', quantize: true, precision: 'binary' }); + * // Tensor { + * // type: 'int8', + * // data: Int8Array [49, 108, 24, ...], + * // dims: [1, 48] + * // } + * ``` + */ +class FeatureExtractionPipeline extends (/** @type {new (options: TextPipelineConstructorArgs) => FeatureExtractionPipelineType} */ (Pipeline)) { + /** + * Create a new FeatureExtractionPipeline. + * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {FeatureExtractionPipelineCallback} */ + async _call(texts, { + pooling = /** @type {'none'} */('none'), + normalize = false, + quantize = false, + precision = /** @type {'binary'} */('binary'), + } = {}) { + + // Run tokenization + const model_inputs = this.tokenizer(texts, { + padding: true, + truncation: true, + }); + + // Run model + const outputs = await this.model(model_inputs) + + // TODO: Provide warning to the user that they might be using model which was not exported + // specifically for feature extraction + // console.log(this.model.config) + // console.log(outputs) + + /** @type {Tensor} */ + let result = outputs.last_hidden_state ?? outputs.logits ?? outputs.token_embeddings; + if (pooling === 'none') { + // Skip pooling + } else if (pooling === 'mean') { + result = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.mean_pooling)(result, model_inputs.attention_mask); + } else if (pooling === 'cls') { + result = result.slice(null, 0); + } else { + throw Error(`Pooling method '${pooling}' not supported.`); + } + + if (normalize) { + result = result.normalize(2, -1); + } + + if (quantize) { + result = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.quantize_embeddings)(result, precision); + } + + return result; + } +} + + +/** + * @typedef {Object} ImageFeatureExtractionPipelineOptions Parameters specific to image feature extraction pipelines. + * @property {boolean} [pool=null] Whether or not to return the pooled output. If set to `false`, the model will return the raw hidden states. + * + * @callback ImageFeatureExtractionPipelineCallback Extract the features of the input(s). + * @param {ImagePipelineInputs} images One or several images (or one list of images) to get the features of. + * @param {ImageFeatureExtractionPipelineOptions} [options] The options to use for image feature extraction. + * @returns {Promise} The image features computed by the model. + * + * @typedef {ImagePipelineConstructorArgs & ImageFeatureExtractionPipelineCallback & Disposable} ImageFeatureExtractionPipelineType + */ + +/** + * Image feature extraction pipeline using no model head. This pipeline extracts the hidden + * states from the base transformer, which can be used as features in downstream tasks. + * + * **Example:** Perform image feature extraction with `Xenova/vit-base-patch16-224-in21k`. + * ```javascript + * const image_feature_extractor = await pipeline('image-feature-extraction', 'Xenova/vit-base-patch16-224-in21k'); + * const url = 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/cats.png'; + * const features = await image_feature_extractor(url); + * // Tensor { + * // dims: [ 1, 197, 768 ], + * // type: 'float32', + * // data: Float32Array(151296) [ ... ], + * // size: 151296 + * // } + * ``` + * + * **Example:** Compute image embeddings with `Xenova/clip-vit-base-patch32`. + * ```javascript + * const image_feature_extractor = await pipeline('image-feature-extraction', 'Xenova/clip-vit-base-patch32'); + * const url = 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/cats.png'; + * const features = await image_feature_extractor(url); + * // Tensor { + * // dims: [ 1, 512 ], + * // type: 'float32', + * // data: Float32Array(512) [ ... ], + * // size: 512 + * // } + * ``` + */ +class ImageFeatureExtractionPipeline extends (/** @type {new (options: ImagePipelineConstructorArgs) => ImageFeatureExtractionPipelineType} */ (Pipeline)) { + /** + * Create a new ImageFeatureExtractionPipeline. + * @param {ImagePipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {ImageFeatureExtractionPipelineCallback} */ + async _call(images, { + pool = null, + } = {}) { + + const preparedImages = await prepareImages(images); + const { pixel_values } = await this.processor(preparedImages); + const outputs = await this.model({ pixel_values }); + + /** @type {Tensor} */ + let result; + if (pool) { + if (!('pooler_output' in outputs)) { + throw Error(`No pooled output was returned. Make sure the model has a 'pooler' layer when using the 'pool' option.`); + } + result = outputs.pooler_output; + + } else { + result = outputs.last_hidden_state ?? outputs.logits ?? outputs.image_embeds; + } + return result; + } +} + +// TODO +// export class SentenceSimilarityPipeline extends Pipeline { +// } + +/** + * @typedef {Object} AudioClassificationSingle + * @property {string} label The label predicted. + * @property {number} score The corresponding probability. + * @typedef {AudioClassificationSingle[]} AudioClassificationOutput + * + * @typedef {Object} AudioClassificationPipelineOptions Parameters specific to audio classification pipelines. + * @property {number} [top_k=5] The number of top labels that will be returned by the pipeline. + * If the provided number is `null` or higher than the number of labels available in the model configuration, + * it will default to the number of labels. + * + * @callback AudioClassificationPipelineCallback Classify the sequence(s) given as inputs. + * @param {AudioPipelineInputs} audio The input audio file(s) to be classified. The input is either: + * - `string` or `URL` that is the filename/URL of the audio file, the file will be read at the processor's sampling rate + * to get the waveform using the [`AudioContext`](https://developer.mozilla.org/en-US/docs/Web/API/AudioContext) API. + * If `AudioContext` is not available, you should pass the raw waveform in as a Float32Array of shape `(n, )`. + * - `Float32Array` or `Float64Array` of shape `(n, )`, representing the raw audio at the correct sampling rate (no further check will be done). + * @param {AudioClassificationPipelineOptions} [options] The options to use for audio classification. + * @returns {Promise} An array or object containing the predicted labels and scores. + * + * @typedef {AudioPipelineConstructorArgs & AudioClassificationPipelineCallback & Disposable} AudioClassificationPipelineType + */ + +/** + * Audio classification pipeline using any `AutoModelForAudioClassification`. + * This pipeline predicts the class of a raw waveform or an audio file. + * + * **Example:** Perform audio classification with `Xenova/wav2vec2-large-xlsr-53-gender-recognition-librispeech`. + * ```javascript + * const classifier = await pipeline('audio-classification', 'Xenova/wav2vec2-large-xlsr-53-gender-recognition-librispeech'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav'; + * const output = await classifier(url); + * // [ + * // { label: 'male', score: 0.9981542229652405 }, + * // { label: 'female', score: 0.001845747814513743 } + * // ] + * ``` + * + * **Example:** Perform audio classification with `Xenova/ast-finetuned-audioset-10-10-0.4593` and return top 4 results. + * ```javascript + * const classifier = await pipeline('audio-classification', 'Xenova/ast-finetuned-audioset-10-10-0.4593'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/cat_meow.wav'; + * const output = await classifier(url, { top_k: 4 }); + * // [ + * // { label: 'Meow', score: 0.5617874264717102 }, + * // { label: 'Cat', score: 0.22365376353263855 }, + * // { label: 'Domestic animals, pets', score: 0.1141069084405899 }, + * // { label: 'Animal', score: 0.08985692262649536 }, + * // ] + * ``` + */ +class AudioClassificationPipeline extends (/** @type {new (options: AudioPipelineConstructorArgs) => AudioClassificationPipelineType} */ (Pipeline)) { + + /** + * Create a new AudioClassificationPipeline. + * @param {AudioPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {AudioClassificationPipelineCallback} */ + async _call(audio, { + top_k = 5 + } = {}) { + + const sampling_rate = this.processor.feature_extractor.config.sampling_rate; + const preparedAudios = await prepareAudios(audio, sampling_rate); + + const id2label = this.model.config.id2label; + + const toReturn = []; + for (const aud of preparedAudios) { + const inputs = await this.processor(aud); + const output = await this.model(inputs); + const logits = output.logits[0]; + + const scores = await (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.topk)(new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.Tensor( + 'float32', + (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(logits.data), + logits.dims, + ), top_k); + + const values = scores[0].tolist(); + const indices = scores[1].tolist(); + + const vals = indices.map((x, i) => ({ + label: /** @type {string} */ (id2label ? id2label[x] : `LABEL_${x}`), + score: /** @type {number} */ (values[i]), + })); + + toReturn.push(vals); + }; + return Array.isArray(audio) ? toReturn : toReturn[0]; + } +} + +/** + * @typedef {Object} ZeroShotAudioClassificationOutput + * @property {string} label The label identified by the model. It is one of the suggested `candidate_label`. + * @property {number} score The score attributed by the model for that label (between 0 and 1). + * + * @typedef {Object} ZeroShotAudioClassificationPipelineOptions Parameters specific to zero-shot audio classification pipelines. + * @property {string} [hypothesis_template="This is a sound of {}."] The sentence used in conjunction with `candidate_labels` + * to attempt the audio classification by replacing the placeholder with the candidate_labels. + * Then likelihood is estimated by using `logits_per_audio`. + * + * @callback ZeroShotAudioClassificationPipelineCallback Classify the sequence(s) given as inputs. + * @param {AudioPipelineInputs} audio The input audio file(s) to be classified. The input is either: + * - `string` or `URL` that is the filename/URL of the audio file, the file will be read at the processor's sampling rate + * to get the waveform using the [`AudioContext`](https://developer.mozilla.org/en-US/docs/Web/API/AudioContext) API. + * If `AudioContext` is not available, you should pass the raw waveform in as a Float32Array of shape `(n, )`. + * - `Float32Array` or `Float64Array` of shape `(n, )`, representing the raw audio at the correct sampling rate (no further check will be done). + * @param {string[]} candidate_labels The candidate labels for this audio. + * @param {ZeroShotAudioClassificationPipelineOptions} [options] The options to use for zero-shot audio classification. + * @returns {Promise} An array of objects containing the predicted labels and scores. + * + * @typedef {TextAudioPipelineConstructorArgs & ZeroShotAudioClassificationPipelineCallback & Disposable} ZeroShotAudioClassificationPipelineType + */ + +/** + * Zero shot audio classification pipeline using `ClapModel`. This pipeline predicts the class of an audio when you + * provide an audio and a set of `candidate_labels`. + * + * **Example**: Perform zero-shot audio classification with `Xenova/clap-htsat-unfused`. + * ```javascript + * const classifier = await pipeline('zero-shot-audio-classification', 'Xenova/clap-htsat-unfused'); + * const audio = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/dog_barking.wav'; + * const candidate_labels = ['dog', 'vaccum cleaner']; + * const scores = await classifier(audio, candidate_labels); + * // [ + * // { score: 0.9993992447853088, label: 'dog' }, + * // { score: 0.0006007603369653225, label: 'vaccum cleaner' } + * // ] + * ``` + */ +class ZeroShotAudioClassificationPipeline extends (/** @type {new (options: TextAudioPipelineConstructorArgs) => ZeroShotAudioClassificationPipelineType} */ (Pipeline)) { + + /** + * Create a new ZeroShotAudioClassificationPipeline. + * @param {TextAudioPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {ZeroShotAudioClassificationPipelineCallback} */ + async _call(audio, candidate_labels, { + hypothesis_template = "This is a sound of {}." + } = {}) { + + const single = !Array.isArray(audio); + if (single) { + audio = [/** @type {AudioInput} */ (audio)]; + } + + // Insert label into hypothesis template + const texts = candidate_labels.map( + x => hypothesis_template.replace('{}', x) + ); + + // Run tokenization + const text_inputs = this.tokenizer(texts, { + padding: true, + truncation: true, + }); + + const sampling_rate = this.processor.feature_extractor.config.sampling_rate; + const preparedAudios = await prepareAudios(audio, sampling_rate); + + const toReturn = []; + for (const aud of preparedAudios) { + const audio_inputs = await this.processor(aud); + + // Run model with both text and audio inputs + const output = await this.model({ ...text_inputs, ...audio_inputs }); + + // Compute softmax per audio + const probs = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(output.logits_per_audio.data); + + toReturn.push([...probs].map((x, i) => ({ + score: x, + label: candidate_labels[i] + }))); + } + return single ? toReturn[0] : toReturn; + } +} + +/** + * @typedef {Object} Chunk + * @property {[number, number]} timestamp The start and end timestamp of the chunk in seconds. + * @property {string} text The recognized text. + */ + +/** + * @typedef {Object} AutomaticSpeechRecognitionOutput + * @property {string} text The recognized text. + * @property {Chunk[]} [chunks] When using `return_timestamps`, the `chunks` will become a list + * containing all the various text chunks identified by the model. + * + * @typedef {Object} AutomaticSpeechRecognitionSpecificParams Parameters specific to automatic-speech-recognition pipelines. + * @property {boolean|'word'} [return_timestamps] Whether to return timestamps or not. Default is `false`. + * @property {number} [chunk_length_s] The length of audio chunks to process in seconds. Default is 0 (no chunking). + * @property {number} [stride_length_s] The length of overlap between consecutive audio chunks in seconds. If not provided, defaults to `chunk_length_s / 6`. + * @property {boolean} [force_full_sequences] Whether to force outputting full sequences or not. Default is `false`. + * @property {string} [language] The source language. Default is `null`, meaning it should be auto-detected. Use this to potentially improve performance if the source language is known. + * @property {string} [task] The task to perform. Default is `null`, meaning it should be auto-detected. + * @property {number} [num_frames] The number of frames in the input audio. + * @typedef {import('./generation/configuration_utils.js').GenerationConfig & AutomaticSpeechRecognitionSpecificParams} AutomaticSpeechRecognitionConfig + * + * @callback AutomaticSpeechRecognitionPipelineCallback Transcribe the audio sequence(s) given as inputs to text. + * @param {AudioPipelineInputs} audio The input audio file(s) to be transcribed. The input is either: + * - `string` or `URL` that is the filename/URL of the audio file, the file will be read at the processor's sampling rate + * to get the waveform using the [`AudioContext`](https://developer.mozilla.org/en-US/docs/Web/API/AudioContext) API. + * If `AudioContext` is not available, you should pass the raw waveform in as a Float32Array of shape `(n, )`. + * - `Float32Array` or `Float64Array` of shape `(n, )`, representing the raw audio at the correct sampling rate (no further check will be done). + * @param {Partial} [options] Additional keyword arguments to pass along to the generate method of the model. + * @returns {Promise} An object containing the transcription text and optionally timestamps if `return_timestamps` is `true`. + * + * @typedef {TextAudioPipelineConstructorArgs & AutomaticSpeechRecognitionPipelineCallback & Disposable} AutomaticSpeechRecognitionPipelineType + */ + +/** + * Pipeline that aims at extracting spoken text contained within some audio. + * + * **Example:** Transcribe English. + * ```javascript + * const transcriber = await pipeline('automatic-speech-recognition', 'Xenova/whisper-tiny.en'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav'; + * const output = await transcriber(url); + * // { text: " And so my fellow Americans ask not what your country can do for you, ask what you can do for your country." } + * ``` + * + * **Example:** Transcribe English w/ timestamps. + * ```javascript + * const transcriber = await pipeline('automatic-speech-recognition', 'Xenova/whisper-tiny.en'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav'; + * const output = await transcriber(url, { return_timestamps: true }); + * // { + * // text: " And so my fellow Americans ask not what your country can do for you, ask what you can do for your country." + * // chunks: [ + * // { timestamp: [0, 8], text: " And so my fellow Americans ask not what your country can do for you" } + * // { timestamp: [8, 11], text: " ask what you can do for your country." } + * // ] + * // } + * ``` + * + * **Example:** Transcribe English w/ word-level timestamps. + * ```javascript + * const transcriber = await pipeline('automatic-speech-recognition', 'Xenova/whisper-tiny.en'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav'; + * const output = await transcriber(url, { return_timestamps: 'word' }); + * // { + * // "text": " And so my fellow Americans ask not what your country can do for you ask what you can do for your country.", + * // "chunks": [ + * // { "text": " And", "timestamp": [0, 0.78] }, + * // { "text": " so", "timestamp": [0.78, 1.06] }, + * // { "text": " my", "timestamp": [1.06, 1.46] }, + * // ... + * // { "text": " for", "timestamp": [9.72, 9.92] }, + * // { "text": " your", "timestamp": [9.92, 10.22] }, + * // { "text": " country.", "timestamp": [10.22, 13.5] } + * // ] + * // } + * ``` + * + * **Example:** Transcribe French. + * ```javascript + * const transcriber = await pipeline('automatic-speech-recognition', 'Xenova/whisper-small'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/french-audio.mp3'; + * const output = await transcriber(url, { language: 'french', task: 'transcribe' }); + * // { text: " J'adore, j'aime, je n'aime pas, je déteste." } + * ``` + * + * **Example:** Translate French to English. + * ```javascript + * const transcriber = await pipeline('automatic-speech-recognition', 'Xenova/whisper-small'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/french-audio.mp3'; + * const output = await transcriber(url, { language: 'french', task: 'translate' }); + * // { text: " I love, I like, I don't like, I hate." } + * ``` + * + * **Example:** Transcribe/translate audio longer than 30 seconds. + * ```javascript + * const transcriber = await pipeline('automatic-speech-recognition', 'Xenova/whisper-tiny.en'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/ted_60.wav'; + * const output = await transcriber(url, { chunk_length_s: 30, stride_length_s: 5 }); + * // { text: " So in college, I was a government major, which means [...] So I'd start off light and I'd bump it up" } + * ``` + */ +class AutomaticSpeechRecognitionPipeline extends (/** @type {new (options: TextAudioPipelineConstructorArgs) => AutomaticSpeechRecognitionPipelineType} */ (Pipeline)) { + + /** + * Create a new AutomaticSpeechRecognitionPipeline. + * @param {TextAudioPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {AutomaticSpeechRecognitionPipelineCallback} */ + async _call(audio, kwargs = {}) { + switch (this.model.config.model_type) { + case 'whisper': + return this._call_whisper(audio, kwargs) + case 'wav2vec2': + case 'wav2vec2-bert': + case 'unispeech': + case 'unispeech-sat': + case 'hubert': + return this._call_wav2vec2(audio, kwargs) + default: + throw new Error(`AutomaticSpeechRecognitionPipeline does not support model type '${this.model.config.model_type}'.`) + } + } + + /** + * @type {AutomaticSpeechRecognitionPipelineCallback} + * @private + */ + async _call_wav2vec2(audio, kwargs) { + // TODO use kwargs + + if (kwargs.language) { + console.warn('`language` parameter is not yet supported for `wav2vec2` models, defaulting to "English".'); + } + if (kwargs.task) { + console.warn('`task` parameter is not yet supported for `wav2vec2` models, defaulting to "transcribe".'); + } + + const single = !Array.isArray(audio); + if (single) { + audio = [/** @type {AudioInput} */ (audio)]; + } + + const sampling_rate = this.processor.feature_extractor.config.sampling_rate; + const preparedAudios = await prepareAudios(audio, sampling_rate); + + const toReturn = []; + for (const aud of preparedAudios) { + const inputs = await this.processor(aud); + const output = await this.model(inputs); + const logits = output.logits[0]; + + const predicted_ids = []; + for (const item of logits) { + predicted_ids.push((0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.max)(item.data)[1]) + } + const predicted_sentences = this.tokenizer.decode(predicted_ids) + toReturn.push({ text: predicted_sentences }) + } + return single ? toReturn[0] : toReturn; + } + + /** + * @type {AutomaticSpeechRecognitionPipelineCallback} + * @private + */ + async _call_whisper(audio, kwargs) { + const return_timestamps = kwargs.return_timestamps ?? false; + const chunk_length_s = kwargs.chunk_length_s ?? 0; + const force_full_sequences = kwargs.force_full_sequences ?? false; + let stride_length_s = kwargs.stride_length_s ?? null; + + const generation_config = { ...kwargs } + + if (return_timestamps === 'word') { + generation_config['return_token_timestamps'] = true; + generation_config['return_timestamps'] = false; // Do not predict timestamp tokens + } + + const single = !Array.isArray(audio); + if (single) { + audio = [/** @type {AudioInput} */ (audio)]; + } + + const time_precision = this.processor.feature_extractor.config.chunk_length / this.model.config.max_source_positions; + const hop_length = this.processor.feature_extractor.config.hop_length; + + const sampling_rate = this.processor.feature_extractor.config.sampling_rate; + const preparedAudios = await prepareAudios(audio, sampling_rate); + + const toReturn = []; + for (const aud of preparedAudios) { + /** @type {{stride: number[], input_features: Tensor, is_last: boolean, tokens?: bigint[], token_timestamps?: number[]}[]} */ + let chunks = []; + if (chunk_length_s > 0) { + if (stride_length_s === null) { + stride_length_s = chunk_length_s / 6; + } else if (chunk_length_s <= stride_length_s) { + throw Error("`chunk_length_s` must be larger than `stride_length_s`.") + } + + // TODO support different stride_length_s (for left and right) + + const window = sampling_rate * chunk_length_s; + const stride = sampling_rate * stride_length_s; + const jump = window - 2 * stride; + let offset = 0; + + // Create subarrays of audio with overlaps + while (true) { + const offset_end = offset + window; + const subarr = aud.subarray(offset, offset_end); + const feature = await this.processor(subarr); + + const is_first = offset === 0; + const is_last = offset_end >= aud.length; + chunks.push({ + stride: [ + subarr.length, + is_first ? 0 : stride, + is_last ? 0 : stride + ], + input_features: feature.input_features, + is_last, + }) + if (is_last) break; + offset += jump; + } + + } else { + chunks = [{ + stride: [aud.length, 0, 0], + input_features: (await this.processor(aud)).input_features, + is_last: true + }] + } + + // Generate for each set of input features + for (const chunk of chunks) { + generation_config.num_frames = Math.floor(chunk.stride[0] / hop_length); + + // NOTE: doing sequentially for now + const data = await this.model.generate({ + inputs: chunk.input_features, + ...generation_config + }); + + // TODO: Right now we only get top beam + if (return_timestamps === 'word') { + chunk.tokens = data.sequences.tolist()[0]; + chunk.token_timestamps = data.token_timestamps.tolist()[0].map( + (/** @type {number} */ x) => (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.round)(x, 2) + ); + + } else { + chunk.tokens = (/** @type {Tensor} */(data))[0].tolist(); + } + + // convert stride to seconds + chunk.stride = chunk.stride.map(x => x / sampling_rate); + } + + // Merge text chunks + // @ts-ignore + const [full_text, optional] = this.tokenizer._decode_asr(chunks, { + time_precision, return_timestamps, force_full_sequences + }); + + toReturn.push({ text: full_text, ...optional }) + } + return single ? toReturn[0] : toReturn; + } +} + +/** + * @typedef {Object} ImageToTextSingle + * @property {string} generated_text The generated text. + * @typedef {ImageToTextSingle[]} ImageToTextOutput + * + * @callback ImageToTextPipelineCallback Assign labels to the image(s) passed as inputs. + * @param {ImagePipelineInputs} texts The images to be captioned. + * @param {Partial} [options] Additional keyword arguments to pass along to the generate method of the model. + * @returns {Promise} An object (or array of objects) containing the generated text(s). + * + * @typedef {TextImagePipelineConstructorArgs & ImageToTextPipelineCallback & Disposable} ImageToTextPipelineType + */ + +/** + * Image To Text pipeline using a `AutoModelForVision2Seq`. This pipeline predicts a caption for a given image. + * + * **Example:** Generate a caption for an image w/ `Xenova/vit-gpt2-image-captioning`. + * ```javascript + * const captioner = await pipeline('image-to-text', 'Xenova/vit-gpt2-image-captioning'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/cats.jpg'; + * const output = await captioner(url); + * // [{ generated_text: 'a cat laying on a couch with another cat' }] + * ``` + * + * **Example:** Optical Character Recognition (OCR) w/ `Xenova/trocr-small-handwritten`. + * ```javascript + * const captioner = await pipeline('image-to-text', 'Xenova/trocr-small-handwritten'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/handwriting.jpg'; + * const output = await captioner(url); + * // [{ generated_text: 'Mr. Brown commented icily.' }] + * ``` + */ +class ImageToTextPipeline extends (/** @type {new (options: TextImagePipelineConstructorArgs) => ImageToTextPipelineType} */ (Pipeline)) { + + /** + * Create a new ImageToTextPipeline. + * @param {TextImagePipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {ImageToTextPipelineCallback} */ + async _call(images, generate_kwargs = {}) { + + const isBatched = Array.isArray(images); + const preparedImages = await prepareImages(images); + + const { pixel_values } = await this.processor(preparedImages); + + const toReturn = []; + for (const batch of pixel_values) { + batch.dims = [1, ...batch.dims] + const output = await this.model.generate({ inputs: batch, ...generate_kwargs }); + const decoded = this.tokenizer.batch_decode(/** @type {Tensor} */(output), { + skip_special_tokens: true, + }).map(x => ({ generated_text: x.trim() })) + toReturn.push(decoded); + } + + return isBatched ? toReturn : toReturn[0]; + } +} + +/** + * @typedef {Object} ImageClassificationSingle + * @property {string} label The label identified by the model. + * @property {number} score The score attributed by the model for that label. + * @typedef {ImageClassificationSingle[]} ImageClassificationOutput + * + * @typedef {Object} ImageClassificationPipelineOptions Parameters specific to image classification pipelines. + * @property {number} [top_k=1] The number of top labels that will be returned by the pipeline. + * + * @callback ImageClassificationPipelineCallback Assign labels to the image(s) passed as inputs. + * @param {ImagePipelineInputs} images The input images(s) to be classified. + * @param {ImageClassificationPipelineOptions} [options] The options to use for image classification. + * @returns {Promise} An array or object containing the predicted labels and scores. + * + * @typedef {ImagePipelineConstructorArgs & ImageClassificationPipelineCallback & Disposable} ImageClassificationPipelineType + */ + +/** + * Image classification pipeline using any `AutoModelForImageClassification`. + * This pipeline predicts the class of an image. + * + * **Example:** Classify an image. + * ```javascript + * const classifier = await pipeline('image-classification', 'Xenova/vit-base-patch16-224'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/tiger.jpg'; + * const output = await classifier(url); + * // [ + * // { label: 'tiger, Panthera tigris', score: 0.632695734500885 }, + * // ] + * ``` + * + * **Example:** Classify an image and return top `n` classes. + * ```javascript + * const classifier = await pipeline('image-classification', 'Xenova/vit-base-patch16-224'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/tiger.jpg'; + * const output = await classifier(url, { top_k: 3 }); + * // [ + * // { label: 'tiger, Panthera tigris', score: 0.632695734500885 }, + * // { label: 'tiger cat', score: 0.3634825646877289 }, + * // { label: 'lion, king of beasts, Panthera leo', score: 0.00045060308184474707 }, + * // ] + * ``` + * + * **Example:** Classify an image and return all classes. + * ```javascript + * const classifier = await pipeline('image-classification', 'Xenova/vit-base-patch16-224'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/tiger.jpg'; + * const output = await classifier(url, { top_k: 0 }); + * // [ + * // { label: 'tiger, Panthera tigris', score: 0.632695734500885 }, + * // { label: 'tiger cat', score: 0.3634825646877289 }, + * // { label: 'lion, king of beasts, Panthera leo', score: 0.00045060308184474707 }, + * // { label: 'jaguar, panther, Panthera onca, Felis onca', score: 0.00035465499968267977 }, + * // ... + * // ] + * ``` + */ +class ImageClassificationPipeline extends (/** @type {new (options: ImagePipelineConstructorArgs) => ImageClassificationPipelineType} */ (Pipeline)) { + + /** + * Create a new ImageClassificationPipeline. + * @param {ImagePipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {ImageClassificationPipelineCallback} */ + async _call(images, { + top_k = 5 + } = {}) { + + const preparedImages = await prepareImages(images); + + const { pixel_values } = await this.processor(preparedImages); + const output = await this.model({ pixel_values }); + + const id2label = this.model.config.id2label; + + /** @type {ImageClassificationOutput[]} */ + const toReturn = []; + for (const batch of output.logits) { + const scores = await (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.topk)(new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.Tensor( + 'float32', + (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(batch.data), + batch.dims, + ), top_k); + + const values = scores[0].tolist(); + const indices = scores[1].tolist(); + + const vals = indices.map((x, i) => ({ + label: /** @type {string} */ (id2label ? id2label[x] : `LABEL_${x}`), + score: /** @type {number} */ (values[i]), + })); + toReturn.push(vals); + } + + return Array.isArray(images) ? toReturn : toReturn[0]; + } + +} + +/** + * @typedef {Object} ImageSegmentationPipelineOutput + * @property {string} label The label of the segment. + * @property {number|null} score The score of the segment. + * @property {RawImage} mask The mask of the segment. + * + * @typedef {Object} ImageSegmentationPipelineOptions Parameters specific to image segmentation pipelines. + * @property {number} [threshold=0.5] Probability threshold to filter out predicted masks. + * @property {number} [mask_threshold=0.5] Threshold to use when turning the predicted masks into binary values. + * @property {number} [overlap_mask_area_threshold=0.8] Mask overlap threshold to eliminate small, disconnected segments. + * @property {null|string} [subtask=null] Segmentation task to be performed. One of [`panoptic`, `instance`, and `semantic`], + * depending on model capabilities. If not set, the pipeline will attempt to resolve (in that order). + * @property {number[]} [label_ids_to_fuse=null] List of label ids to fuse. If not set, do not fuse any labels. + * @property {number[][]} [target_sizes=null] List of target sizes for the input images. If not set, use the original image sizes. + * + * @callback ImageSegmentationPipelineCallback Segment the input images. + * @param {ImagePipelineInputs} images The input images. + * @param {ImageSegmentationPipelineOptions} [options] The options to use for image segmentation. + * @returns {Promise} The annotated segments. + * + * @typedef {ImagePipelineConstructorArgs & ImageSegmentationPipelineCallback & Disposable} ImageSegmentationPipelineType + */ + +/** + * Image segmentation pipeline using any `AutoModelForXXXSegmentation`. + * This pipeline predicts masks of objects and their classes. + * + * **Example:** Perform image segmentation with `Xenova/detr-resnet-50-panoptic`. + * ```javascript + * const segmenter = await pipeline('image-segmentation', 'Xenova/detr-resnet-50-panoptic'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/cats.jpg'; + * const output = await segmenter(url); + * // [ + * // { label: 'remote', score: 0.9984649419784546, mask: RawImage { ... } }, + * // { label: 'cat', score: 0.9994316101074219, mask: RawImage { ... } } + * // ] + * ``` + */ +class ImageSegmentationPipeline extends (/** @type {new (options: ImagePipelineConstructorArgs) => ImageSegmentationPipelineType} */ (Pipeline)) { + /** + * Create a new ImageSegmentationPipeline. + * @param {ImagePipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + + this.subtasks_mapping = { + // Mapping of subtasks to their corresponding post-processing function names. + panoptic: 'post_process_panoptic_segmentation', + instance: 'post_process_instance_segmentation', + semantic: 'post_process_semantic_segmentation' + } + } + + /** @type {ImageSegmentationPipelineCallback} */ + async _call(images, { + threshold = 0.5, + mask_threshold = 0.5, + overlap_mask_area_threshold = 0.8, + label_ids_to_fuse = null, + target_sizes = null, + subtask = null, + } = {}) { + const isBatched = Array.isArray(images); + + if (isBatched && images.length !== 1) { + throw Error("Image segmentation pipeline currently only supports a batch size of 1."); + } + + const preparedImages = await prepareImages(images); + const imageSizes = preparedImages.map(x => [x.height, x.width]); + + const { pixel_values, pixel_mask } = await this.processor(preparedImages); + const output = await this.model({ pixel_values, pixel_mask }); + + let fn = null; + if (subtask !== null) { + fn = this.subtasks_mapping[subtask]; + } else { + for (let [task, func] of Object.entries(this.subtasks_mapping)) { + if (func in this.processor.feature_extractor) { + fn = this.processor.feature_extractor[func].bind(this.processor.feature_extractor); + subtask = task; + break; + } + } + } + + const id2label = this.model.config.id2label; + + /** @type {ImageSegmentationPipelineOutput[]} */ + const annotation = []; + if (subtask === 'panoptic' || subtask === 'instance') { + const processed = fn( + output, + threshold, + mask_threshold, + overlap_mask_area_threshold, + label_ids_to_fuse, + target_sizes ?? imageSizes, // TODO FIX? + )[0]; + + const segmentation = processed.segmentation; + + for (const segment of processed.segments_info) { + const maskData = new Uint8ClampedArray(segmentation.data.length); + for (let i = 0; i < segmentation.data.length; ++i) { + if (segmentation.data[i] === segment.id) { + maskData[i] = 255; + } + } + + const mask = new _utils_image_js__WEBPACK_IMPORTED_MODULE_8__.RawImage(maskData, segmentation.dims[1], segmentation.dims[0], 1) + + annotation.push({ + score: segment.score, + label: id2label[segment.label_id], + mask: mask + }) + } + + } else if (subtask === 'semantic') { + const { segmentation, labels } = fn(output, target_sizes ?? imageSizes)[0]; + + for (const label of labels) { + const maskData = new Uint8ClampedArray(segmentation.data.length); + for (let i = 0; i < segmentation.data.length; ++i) { + if (segmentation.data[i] === label) { + maskData[i] = 255; + } + } + + const mask = new _utils_image_js__WEBPACK_IMPORTED_MODULE_8__.RawImage(maskData, segmentation.dims[1], segmentation.dims[0], 1); + + annotation.push({ + score: null, + label: id2label[label], + mask: mask + }); + } + } else { + throw Error(`Subtask ${subtask} not supported.`); + } + + return annotation; + } +} + +/** + * @typedef {Object} ZeroShotImageClassificationOutput + * @property {string} label The label identified by the model. It is one of the suggested `candidate_label`. + * @property {number} score The score attributed by the model for that label (between 0 and 1). + * + * @typedef {Object} ZeroShotImageClassificationPipelineOptions Parameters specific to zero-shot image classification pipelines. + * @property {string} [hypothesis_template="This is a photo of {}"] The sentence used in conjunction with `candidate_labels` + * to attempt the image classification by replacing the placeholder with the candidate_labels. + * Then likelihood is estimated by using `logits_per_image`. + * + * @callback ZeroShotImageClassificationPipelineCallback Assign labels to the image(s) passed as inputs. + * @param {ImagePipelineInputs} images The input images. + * @param {string[]} candidate_labels The candidate labels for this image. + * @param {ZeroShotImageClassificationPipelineOptions} [options] The options to use for zero-shot image classification. + * @returns {Promise} An array of objects containing the predicted labels and scores. + * + * @typedef {TextImagePipelineConstructorArgs & ZeroShotImageClassificationPipelineCallback & Disposable} ZeroShotImageClassificationPipelineType + */ + +/** + * Zero shot image classification pipeline. This pipeline predicts the class of + * an image when you provide an image and a set of `candidate_labels`. + * + * **Example:** Zero shot image classification w/ `Xenova/clip-vit-base-patch32`. + * ```javascript + * const classifier = await pipeline('zero-shot-image-classification', 'Xenova/clip-vit-base-patch32'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/tiger.jpg'; + * const output = await classifier(url, ['tiger', 'horse', 'dog']); + * // [ + * // { score: 0.9993917942047119, label: 'tiger' }, + * // { score: 0.0003519294841680676, label: 'horse' }, + * // { score: 0.0002562698791734874, label: 'dog' } + * // ] + * ``` + */ +class ZeroShotImageClassificationPipeline extends (/** @type {new (options: TextImagePipelineConstructorArgs) => ZeroShotImageClassificationPipelineType} */ (Pipeline)) { + /** + * Create a new ZeroShotImageClassificationPipeline. + * @param {TextImagePipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {ZeroShotImageClassificationPipelineCallback} */ + async _call(images, candidate_labels, { + hypothesis_template = "This is a photo of {}" + } = {}) { + + const isBatched = Array.isArray(images); + const preparedImages = await prepareImages(images); + + // Insert label into hypothesis template + const texts = candidate_labels.map( + x => hypothesis_template.replace('{}', x) + ); + + // Run tokenization + const text_inputs = this.tokenizer(texts, { + padding: this.model.config.model_type === 'siglip' ? 'max_length' : true, + truncation: true, + }); + + // Run processor + const { pixel_values } = await this.processor(preparedImages); + + // Run model with both text and pixel inputs + const output = await this.model({ ...text_inputs, pixel_values }); + + const function_to_apply = + this.model.config.model_type === 'siglip' + ? batch => batch.sigmoid().data + : batch => (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(batch.data); + + // Compare each image with each candidate label + const toReturn = []; + for (const batch of output.logits_per_image) { + // Compute softmax per image + const probs = function_to_apply(batch); + + const result = [...probs].map((x, i) => ({ + score: x, + label: candidate_labels[i] + })); + result.sort((a, b) => b.score - a.score); // sort by score in descending order + toReturn.push(result); + } + + return isBatched ? toReturn : toReturn[0]; + } +} + + +/** + * @typedef {Object} ObjectDetectionPipelineSingle + * @property {string} label The class label identified by the model. + * @property {number} score The score attributed by the model for that label. + * @property {BoundingBox} box The bounding box of detected object in image's original size, or as a percentage if `percentage` is set to true. + * @typedef {ObjectDetectionPipelineSingle[]} ObjectDetectionPipelineOutput + * + * @typedef {Object} ObjectDetectionPipelineOptions Parameters specific to object detection pipelines. + * @property {number} [threshold=0.9] The threshold used to filter boxes by score. + * @property {boolean} [percentage=false] Whether to return the boxes coordinates in percentage (true) or in pixels (false). + * + * @callback ObjectDetectionPipelineCallback Detect objects (bounding boxes & classes) in the image(s) passed as inputs. + * @param {ImagePipelineInputs} images The input images. + * @param {ObjectDetectionPipelineOptions} [options] The options to use for object detection. + * @returns {Promise} A list of objects or a list of list of objects. + * + * @typedef {ImagePipelineConstructorArgs & ObjectDetectionPipelineCallback & Disposable} ObjectDetectionPipelineType + */ + +/** + * Object detection pipeline using any `AutoModelForObjectDetection`. + * This pipeline predicts bounding boxes of objects and their classes. + * + * **Example:** Run object-detection with `Xenova/detr-resnet-50`. + * ```javascript + * const detector = await pipeline('object-detection', 'Xenova/detr-resnet-50'); + * const img = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/cats.jpg'; + * const output = await detector(img, { threshold: 0.9 }); + * // [{ + * // score: 0.9976370930671692, + * // label: "remote", + * // box: { xmin: 31, ymin: 68, xmax: 190, ymax: 118 } + * // }, + * // ... + * // { + * // score: 0.9984092116355896, + * // label: "cat", + * // box: { xmin: 331, ymin: 19, xmax: 649, ymax: 371 } + * // }] + * ``` + */ +class ObjectDetectionPipeline extends (/** @type {new (options: ImagePipelineConstructorArgs) => ObjectDetectionPipelineType} */ (Pipeline)) { + + /** + * Create a new ObjectDetectionPipeline. + * @param {ImagePipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {ObjectDetectionPipelineCallback} */ + async _call(images, { + threshold = 0.9, + percentage = false, + } = {}) { + + const isBatched = Array.isArray(images); + + if (isBatched && images.length !== 1) { + throw Error("Object detection pipeline currently only supports a batch size of 1."); + } + const preparedImages = await prepareImages(images); + + const imageSizes = percentage ? null : preparedImages.map(x => [x.height, x.width]); + + const { pixel_values, pixel_mask } = await this.processor(preparedImages); + const output = await this.model({ pixel_values, pixel_mask }); + + // @ts-ignore + const processed = this.processor.feature_extractor.post_process_object_detection(output, threshold, imageSizes); + + // Add labels + const id2label = this.model.config.id2label; + + // Format output + /** @type {ObjectDetectionPipelineOutput[]} */ + const result = processed.map(batch => ( + batch.boxes.map((box, i) => ({ + score: batch.scores[i], + label: id2label[batch.classes[i]], + box: get_bounding_box(box, !percentage), + })) + )) + + return isBatched ? result : result[0]; + } +} + + +/** + * @typedef {Object} ZeroShotObjectDetectionOutput + * @property {string} label Text query corresponding to the found object. + * @property {number} score Score corresponding to the object (between 0 and 1). + * @property {BoundingBox} box Bounding box of the detected object in image's original size, or as a percentage if `percentage` is set to true. + * + * @typedef {Object} ZeroShotObjectDetectionPipelineOptions Parameters specific to zero-shot object detection pipelines. + * @property {number} [threshold=0.1] The probability necessary to make a prediction. + * @property {number} [top_k=null] The number of top predictions that will be returned by the pipeline. + * If the provided number is `null` or higher than the number of predictions available, it will default + * to the number of predictions. + * @property {boolean} [percentage=false] Whether to return the boxes coordinates in percentage (true) or in pixels (false). + * + * @callback ZeroShotObjectDetectionPipelineCallback Detect objects (bounding boxes & classes) in the image(s) passed as inputs. + * @param {ImagePipelineInputs} images The input images. + * @param {string[]} candidate_labels What the model should recognize in the image. + * @param {ZeroShotObjectDetectionPipelineOptions} [options] The options to use for zero-shot object detection. + * @returns {Promise} An array of objects containing the predicted labels, scores, and bounding boxes. + * + * @typedef {TextImagePipelineConstructorArgs & ZeroShotObjectDetectionPipelineCallback & Disposable} ZeroShotObjectDetectionPipelineType + */ + +/** + * Zero-shot object detection pipeline. This pipeline predicts bounding boxes of + * objects when you provide an image and a set of `candidate_labels`. + * + * **Example:** Zero-shot object detection w/ `Xenova/owlvit-base-patch32`. + * ```javascript + * const detector = await pipeline('zero-shot-object-detection', 'Xenova/owlvit-base-patch32'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/astronaut.png'; + * const candidate_labels = ['human face', 'rocket', 'helmet', 'american flag']; + * const output = await detector(url, candidate_labels); + * // [ + * // { + * // score: 0.24392342567443848, + * // label: 'human face', + * // box: { xmin: 180, ymin: 67, xmax: 274, ymax: 175 } + * // }, + * // { + * // score: 0.15129457414150238, + * // label: 'american flag', + * // box: { xmin: 0, ymin: 4, xmax: 106, ymax: 513 } + * // }, + * // { + * // score: 0.13649864494800568, + * // label: 'helmet', + * // box: { xmin: 277, ymin: 337, xmax: 511, ymax: 511 } + * // }, + * // { + * // score: 0.10262022167444229, + * // label: 'rocket', + * // box: { xmin: 352, ymin: -1, xmax: 463, ymax: 287 } + * // } + * // ] + * ``` + * + * **Example:** Zero-shot object detection w/ `Xenova/owlvit-base-patch32` (returning top 4 matches and setting a threshold). + * ```javascript + * const detector = await pipeline('zero-shot-object-detection', 'Xenova/owlvit-base-patch32'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/beach.png'; + * const candidate_labels = ['hat', 'book', 'sunglasses', 'camera']; + * const output = await detector(url, candidate_labels, { top_k: 4, threshold: 0.05 }); + * // [ + * // { + * // score: 0.1606510728597641, + * // label: 'sunglasses', + * // box: { xmin: 347, ymin: 229, xmax: 429, ymax: 264 } + * // }, + * // { + * // score: 0.08935828506946564, + * // label: 'hat', + * // box: { xmin: 38, ymin: 174, xmax: 258, ymax: 364 } + * // }, + * // { + * // score: 0.08530698716640472, + * // label: 'camera', + * // box: { xmin: 187, ymin: 350, xmax: 260, ymax: 411 } + * // }, + * // { + * // score: 0.08349756896495819, + * // label: 'book', + * // box: { xmin: 261, ymin: 280, xmax: 494, ymax: 425 } + * // } + * // ] + * ``` + */ +class ZeroShotObjectDetectionPipeline extends (/** @type {new (options: TextImagePipelineConstructorArgs) => ZeroShotObjectDetectionPipelineType} */ (Pipeline)) { + + /** + * Create a new ZeroShotObjectDetectionPipeline. + * @param {TextImagePipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {ZeroShotObjectDetectionPipelineCallback} */ + async _call(images, candidate_labels, { + threshold = 0.1, + top_k = null, + percentage = false, + } = {}) { + + const isBatched = Array.isArray(images); + const preparedImages = await prepareImages(images); + + // Run tokenization + const text_inputs = this.tokenizer(candidate_labels, { + padding: true, + truncation: true, + }); + + // Run processor + const model_inputs = await this.processor(preparedImages); + + // Since non-maximum suppression is performed for exporting, we need to + // process each image separately. For more information, see: + // https://github.com/huggingface/optimum/blob/e3b7efb1257c011db907ef40ab340e795cc5684c/optimum/exporters/onnx/model_configs.py#L1028-L1032 + const toReturn = []; + for (let i = 0; i < preparedImages.length; ++i) { + const image = preparedImages[i]; + const imageSize = percentage ? null : [[image.height, image.width]]; + const pixel_values = model_inputs.pixel_values[i].unsqueeze_(0); + + // Run model with both text and pixel inputs + const output = await this.model({ ...text_inputs, pixel_values }); + + // @ts-ignore + const processed = this.processor.feature_extractor.post_process_object_detection(output, threshold, imageSize, true)[0]; + let result = processed.boxes.map((box, i) => ({ + score: processed.scores[i], + label: candidate_labels[processed.classes[i]], + box: get_bounding_box(box, !percentage), + })).sort((a, b) => b.score - a.score); + if (top_k !== null) { + result = result.slice(0, top_k); + } + toReturn.push(result) + } + + return isBatched ? toReturn : toReturn[0]; + } +} + +/** + * @typedef {Object} DocumentQuestionAnsweringSingle + * @property {string} answer The generated text. + * @typedef {DocumentQuestionAnsweringSingle[]} DocumentQuestionAnsweringOutput + * + * @callback DocumentQuestionAnsweringPipelineCallback Answer the question given as input by using the document. + * @param {ImageInput} image The image of the document to use. + * @param {string} question A question to ask of the document. + * @param {Partial} [options] Additional keyword arguments to pass along to the generate method of the model. + * @returns {Promise} An object (or array of objects) containing the answer(s). + * + * @typedef {TextImagePipelineConstructorArgs & DocumentQuestionAnsweringPipelineCallback & Disposable} DocumentQuestionAnsweringPipelineType + */ + +/** + * Document Question Answering pipeline using any `AutoModelForDocumentQuestionAnswering`. + * The inputs/outputs are similar to the (extractive) question answering pipeline; however, + * the pipeline takes an image (and optional OCR'd words/boxes) as input instead of text context. + * + * **Example:** Answer questions about a document with `Xenova/donut-base-finetuned-docvqa`. + * ```javascript + * const qa_pipeline = await pipeline('document-question-answering', 'Xenova/donut-base-finetuned-docvqa'); + * const image = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/invoice.png'; + * const question = 'What is the invoice number?'; + * const output = await qa_pipeline(image, question); + * // [{ answer: 'us-001' }] + * ``` + */ +class DocumentQuestionAnsweringPipeline extends (/** @type {new (options: TextImagePipelineConstructorArgs) => DocumentQuestionAnsweringPipelineType} */ (Pipeline)) { + + /** + * Create a new DocumentQuestionAnsweringPipeline. + * @param {TextImagePipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {DocumentQuestionAnsweringPipelineCallback} */ + async _call(image, question, generate_kwargs = {}) { + + // NOTE: For now, we only support a batch size of 1 + + // Preprocess image + const preparedImage = (await prepareImages(image))[0]; + const { pixel_values } = await this.processor(preparedImage); + + // Run tokenization + const task_prompt = `${question}`; + const decoder_input_ids = this.tokenizer(task_prompt, { + add_special_tokens: false, + padding: true, + truncation: true, + }).input_ids; + + // Run model + const output = await this.model.generate({ + inputs: pixel_values, + max_length: this.model.config.decoder.max_position_embeddings, + decoder_input_ids, + ...generate_kwargs, + }); + + // Decode output + const decoded = this.tokenizer.batch_decode(/** @type {Tensor} */(output))[0]; + + // Parse answer + const match = decoded.match(/(.*?)<\/s_answer>/); + let answer = null; + if (match && match.length >= 2) { + answer = match[1].trim(); + } + return [{ answer }]; + } +} + + +/** + * @typedef {Object} VocoderOptions + * @property {PreTrainedModel} [vocoder] The vocoder used by the pipeline (if the model uses one). If not provided, use the default HifiGan vocoder. + * @typedef {TextAudioPipelineConstructorArgs & VocoderOptions} TextToAudioPipelineConstructorArgs + */ + +/** + * @typedef {Object} TextToAudioOutput + * @property {Float32Array} audio The generated audio waveform. + * @property {number} sampling_rate The sampling rate of the generated audio waveform. + * + * @typedef {Object} TextToAudioPipelineOptions Parameters specific to text-to-audio pipelines. + * @property {Tensor|Float32Array|string|URL} [speaker_embeddings=null] The speaker embeddings (if the model requires it). + * + * @callback TextToAudioPipelineCallback Generates speech/audio from the inputs. + * @param {string|string[]} texts The text(s) to generate. + * @param {TextToAudioPipelineOptions} options Parameters passed to the model generation/forward method. + * @returns {Promise} An object containing the generated audio and sampling rate. + * + * @typedef {TextToAudioPipelineConstructorArgs & TextToAudioPipelineCallback & Disposable} TextToAudioPipelineType + */ + +/** + * Text-to-audio generation pipeline using any `AutoModelForTextToWaveform` or `AutoModelForTextToSpectrogram`. + * This pipeline generates an audio file from an input text and optional other conditional inputs. + * + * **Example:** Generate audio from text with `Xenova/speecht5_tts`. + * ```javascript + * const synthesizer = await pipeline('text-to-speech', 'Xenova/speecht5_tts', { quantized: false }); + * const speaker_embeddings = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/speaker_embeddings.bin'; + * const out = await synthesizer('Hello, my dog is cute', { speaker_embeddings }); + * // { + * // audio: Float32Array(26112) [-0.00005657337896991521, 0.00020583874720614403, ...], + * // sampling_rate: 16000 + * // } + * ``` + * + * You can then save the audio to a .wav file with the `wavefile` package: + * ```javascript + * import wavefile from 'wavefile'; + * import fs from 'fs'; + * + * const wav = new wavefile.WaveFile(); + * wav.fromScratch(1, out.sampling_rate, '32f', out.audio); + * fs.writeFileSync('out.wav', wav.toBuffer()); + * ``` + * + * **Example:** Multilingual speech generation with `Xenova/mms-tts-fra`. See [here](https://huggingface.co/models?pipeline_tag=text-to-speech&other=vits&sort=trending) for the full list of available languages (1107). + * ```javascript + * const synthesizer = await pipeline('text-to-speech', 'Xenova/mms-tts-fra'); + * const out = await synthesizer('Bonjour'); + * // { + * // audio: Float32Array(23808) [-0.00037693005288019776, 0.0003325853613205254, ...], + * // sampling_rate: 16000 + * // } + * ``` + */ +class TextToAudioPipeline extends (/** @type {new (options: TextToAudioPipelineConstructorArgs) => TextToAudioPipelineType} */ (Pipeline)) { + DEFAULT_VOCODER_ID = "Xenova/speecht5_hifigan" + + /** + * Create a new TextToAudioPipeline. + * @param {TextToAudioPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + + // TODO: Find a better way for `pipeline` to set the default vocoder + this.vocoder = options.vocoder ?? null; + } + + + /** @type {TextToAudioPipelineCallback} */ + async _call(text_inputs, { + speaker_embeddings = null, + } = {}) { + + // If this.processor is not set, we are using a `AutoModelForTextToWaveform` model + if (this.processor) { + return this._call_text_to_spectrogram(text_inputs, { speaker_embeddings }); + } else { + return this._call_text_to_waveform(text_inputs); + } + } + + async _call_text_to_waveform(text_inputs) { + + // Run tokenization + const inputs = this.tokenizer(text_inputs, { + padding: true, + truncation: true, + }); + + // Generate waveform + const { waveform } = await this.model(inputs); + + const sampling_rate = this.model.config.sampling_rate; + return { + audio: waveform.data, + sampling_rate, + } + } + + async _call_text_to_spectrogram(text_inputs, { speaker_embeddings }) { + + // Load vocoder, if not provided + if (!this.vocoder) { + console.log('No vocoder specified, using default HifiGan vocoder.'); + this.vocoder = await _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModel.from_pretrained(this.DEFAULT_VOCODER_ID, { dtype: 'fp32' }); + } + + // Load speaker embeddings as Float32Array from path/URL + if (typeof speaker_embeddings === 'string' || speaker_embeddings instanceof URL) { + // Load from URL with fetch + speaker_embeddings = new Float32Array( + await (await fetch(speaker_embeddings)).arrayBuffer() + ); + } + + if (speaker_embeddings instanceof Float32Array) { + speaker_embeddings = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.Tensor( + 'float32', + speaker_embeddings, + [1, speaker_embeddings.length] + ) + } else if (!(speaker_embeddings instanceof _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.Tensor)) { + throw new Error("Speaker embeddings must be a `Tensor`, `Float32Array`, `string`, or `URL`.") + } + + // Run tokenization + const { input_ids } = this.tokenizer(text_inputs, { + padding: true, + truncation: true, + }); + + // NOTE: At this point, we are guaranteed that `speaker_embeddings` is a `Tensor` + // @ts-ignore + const { waveform } = await this.model.generate_speech(input_ids, speaker_embeddings, { vocoder: this.vocoder }); + + const sampling_rate = this.processor.feature_extractor.config.sampling_rate; + return { + audio: waveform.data, + sampling_rate, + } + } +} + +/** + * @callback ImageToImagePipelineCallback Transform the image(s) passed as inputs. + * @param {ImagePipelineInputs} images The images to transform. + * @returns {Promise} The transformed image or list of images. + * + * @typedef {ImagePipelineConstructorArgs & ImageToImagePipelineCallback & Disposable} ImageToImagePipelineType + */ + +/** + * Image to Image pipeline using any `AutoModelForImageToImage`. This pipeline generates an image based on a previous image input. + * + * **Example:** Super-resolution w/ `Xenova/swin2SR-classical-sr-x2-64` + * ```javascript + * const upscaler = await pipeline('image-to-image', 'Xenova/swin2SR-classical-sr-x2-64'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/butterfly.jpg'; + * const output = await upscaler(url); + * // RawImage { + * // data: Uint8Array(786432) [ 41, 31, 24, 43, ... ], + * // width: 512, + * // height: 512, + * // channels: 3 + * // } + * ``` + */ +class ImageToImagePipeline extends (/** @type {new (options: ImagePipelineConstructorArgs) => ImageToImagePipelineType} */ (Pipeline)) { + /** + * Create a new ImageToImagePipeline. + * @param {ImagePipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {ImageToImagePipelineCallback} */ + async _call(images) { + + const preparedImages = await prepareImages(images); + const inputs = await this.processor(preparedImages); + const outputs = await this.model(inputs); + + /** @type {RawImage[]} */ + const toReturn = []; + for (const batch of outputs.reconstruction) { + const output = batch.squeeze().clamp_(0, 1).mul_(255).round_().to('uint8'); + toReturn.push(_utils_image_js__WEBPACK_IMPORTED_MODULE_8__.RawImage.fromTensor(output)); + } + + return toReturn.length > 1 ? toReturn : toReturn[0]; + } +} + +/** + * @typedef {Object} DepthEstimationPipelineOutput + * @property {Tensor} predicted_depth The raw depth map predicted by the model. + * @property {RawImage} depth The processed depth map as an image (with the same size as the input image). + * + * @callback DepthEstimationPipelineCallback Predicts the depth for the image(s) passed as inputs. + * @param {ImagePipelineInputs} images The images to compute depth for. + * @returns {Promise} An image or a list of images containing result(s). + * + * @typedef {ImagePipelineConstructorArgs & DepthEstimationPipelineCallback & Disposable} DepthEstimationPipelineType + */ + +/** + * Depth estimation pipeline using any `AutoModelForDepthEstimation`. This pipeline predicts the depth of an image. + * + * **Example:** Depth estimation w/ `Xenova/dpt-hybrid-midas` + * ```javascript + * const depth_estimator = await pipeline('depth-estimation', 'Xenova/dpt-hybrid-midas'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/cats.jpg'; + * const out = await depth_estimator(url); + * // { + * // predicted_depth: Tensor { + * // dims: [ 384, 384 ], + * // type: 'float32', + * // data: Float32Array(147456) [ 542.859130859375, 545.2833862304688, 546.1649169921875, ... ], + * // size: 147456 + * // }, + * // depth: RawImage { + * // data: Uint8Array(307200) [ 86, 86, 86, ... ], + * // width: 640, + * // height: 480, + * // channels: 1 + * // } + * // } + * ``` + */ +class DepthEstimationPipeline extends (/** @type {new (options: ImagePipelineConstructorArgs) => DepthEstimationPipelineType} */ (Pipeline)) { + /** + * Create a new DepthEstimationPipeline. + * @param {ImagePipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {DepthEstimationPipelineCallback} */ + async _call(images) { + + const preparedImages = await prepareImages(images); + + const inputs = await this.processor(preparedImages); + const { predicted_depth } = await this.model(inputs); + + const toReturn = []; + for (let i = 0; i < preparedImages.length; ++i) { + const prediction = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.interpolate)(predicted_depth[i], preparedImages[i].size.reverse(), 'bilinear', false); + const formatted = prediction.mul_(255 / (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.max)(prediction.data)[0]).to('uint8'); + toReturn.push({ + predicted_depth: predicted_depth[i], + depth: _utils_image_js__WEBPACK_IMPORTED_MODULE_8__.RawImage.fromTensor(formatted), + }); + } + + return toReturn.length > 1 ? toReturn : toReturn[0]; + } +} + +const SUPPORTED_TASKS = Object.freeze({ + "text-classification": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": TextClassificationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSequenceClassification, + "default": { + // TODO: replace with original + // "model": "distilbert-base-uncased-finetuned-sst-2-english", + "model": "Xenova/distilbert-base-uncased-finetuned-sst-2-english", + }, + "type": "text", + }, + "token-classification": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": TokenClassificationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForTokenClassification, + "default": { + // TODO: replace with original + // "model": "Davlan/bert-base-multilingual-cased-ner-hrl", + "model": "Xenova/bert-base-multilingual-cased-ner-hrl", + }, + "type": "text", + }, + "question-answering": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": QuestionAnsweringPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForQuestionAnswering, + "default": { + // TODO: replace with original + // "model": "distilbert-base-cased-distilled-squad", + "model": "Xenova/distilbert-base-cased-distilled-squad", + }, + "type": "text", + }, + + "fill-mask": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": FillMaskPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForMaskedLM, + "default": { + // TODO: replace with original + // "model": "bert-base-uncased", + "model": "Xenova/bert-base-uncased", + }, + "type": "text", + }, + "summarization": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": SummarizationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSeq2SeqLM, + "default": { + // TODO: replace with original + // "model": "sshleifer/distilbart-cnn-6-6", + "model": "Xenova/distilbart-cnn-6-6", + }, + "type": "text", + }, + "translation": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": TranslationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSeq2SeqLM, + "default": { + // TODO: replace with original + // "model": "t5-small", + "model": "Xenova/t5-small", + }, + "type": "text", + }, + "text2text-generation": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": Text2TextGenerationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSeq2SeqLM, + "default": { + // TODO: replace with original + // "model": "google/flan-t5-small", + "model": "Xenova/flan-t5-small", + }, + "type": "text", + }, + "text-generation": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": TextGenerationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForCausalLM, + "default": { + // TODO: replace with original + // "model": "gpt2", + "model": "Xenova/gpt2", + }, + "type": "text", + }, + "zero-shot-classification": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": ZeroShotClassificationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSequenceClassification, + "default": { + // TODO: replace with original + // "model": "typeform/distilbert-base-uncased-mnli", + "model": "Xenova/distilbert-base-uncased-mnli", + }, + "type": "text", + }, + "audio-classification": { + "pipeline": AudioClassificationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForAudioClassification, + "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "superb/wav2vec2-base-superb-ks", + "model": "Xenova/wav2vec2-base-superb-ks", + }, + "type": "audio", + }, + "zero-shot-audio-classification": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": ZeroShotAudioClassificationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModel, + "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "laion/clap-htsat-fused", + "model": "Xenova/clap-htsat-unfused", + }, + "type": "multimodal", + }, + "automatic-speech-recognition": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": AutomaticSpeechRecognitionPipeline, + "model": [_models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSpeechSeq2Seq, _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForCTC], + "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "openai/whisper-tiny.en", + "model": "Xenova/whisper-tiny.en", + }, + "type": "multimodal", + }, + "text-to-audio": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": TextToAudioPipeline, + "model": [_models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForTextToWaveform, _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForTextToSpectrogram], + "processor": [_processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, /* Some don't use a processor */ null], + "default": { + // TODO: replace with original + // "model": "microsoft/speecht5_tts", + "model": "Xenova/speecht5_tts", + }, + "type": "text", + }, + "image-to-text": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": ImageToTextPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForVision2Seq, + "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "nlpconnect/vit-gpt2-image-captioning", + "model": "Xenova/vit-gpt2-image-captioning", + }, + "type": "multimodal", + }, + + "image-classification": { + // no tokenizer + "pipeline": ImageClassificationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForImageClassification, + "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "google/vit-base-patch16-224", + "model": "Xenova/vit-base-patch16-224", + }, + "type": "multimodal", + }, + + "image-segmentation": { + // no tokenizer + "pipeline": ImageSegmentationPipeline, + "model": [_models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForImageSegmentation, _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSemanticSegmentation, _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForUniversalSegmentation], + "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "facebook/detr-resnet-50-panoptic", + "model": "Xenova/detr-resnet-50-panoptic", + }, + "type": "multimodal", + }, + + "zero-shot-image-classification": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": ZeroShotImageClassificationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModel, + "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "openai/clip-vit-base-patch32", + "model": "Xenova/clip-vit-base-patch32", + }, + "type": "multimodal", + }, + + "object-detection": { + // no tokenizer + "pipeline": ObjectDetectionPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForObjectDetection, + "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "facebook/detr-resnet-50", + "model": "Xenova/detr-resnet-50", + }, + "type": "multimodal", + }, + "zero-shot-object-detection": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": ZeroShotObjectDetectionPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForZeroShotObjectDetection, + "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "google/owlvit-base-patch32", + "model": "Xenova/owlvit-base-patch32", + }, + "type": "multimodal", + }, + "document-question-answering": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": DocumentQuestionAnsweringPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForDocumentQuestionAnswering, + "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "naver-clova-ix/donut-base-finetuned-docvqa", + "model": "Xenova/donut-base-finetuned-docvqa", + }, + "type": "multimodal", + }, + "image-to-image": { + // no tokenizer + "pipeline": ImageToImagePipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForImageToImage, + "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "caidas/swin2SR-classical-sr-x2-64", + "model": "Xenova/swin2SR-classical-sr-x2-64", + }, + "type": "image", + }, + "depth-estimation": { + // no tokenizer + "pipeline": DepthEstimationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForDepthEstimation, + "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "Intel/dpt-large", + "model": "Xenova/dpt-large", + }, + "type": "image", + }, + + // This task serves as a useful interface for dealing with sentence-transformers (https://huggingface.co/sentence-transformers). + "feature-extraction": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": FeatureExtractionPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModel, + "default": { + // TODO: replace with original + // "model": "sentence-transformers/all-MiniLM-L6-v2", + "model": "Xenova/all-MiniLM-L6-v2", + }, + "type": "text", + }, + "image-feature-extraction": { + "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "pipeline": ImageFeatureExtractionPipeline, + "model": [_models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForImageFeatureExtraction, _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModel], + "default": { + // TODO: replace with original + // "model": "google/vit-base-patch16-224", + "model": "Xenova/vit-base-patch16-224-in21k", + }, + "type": "image", + }, +}) + + +// TODO: Add types for TASK_ALIASES +const TASK_ALIASES = Object.freeze({ + "sentiment-analysis": "text-classification", + "ner": "token-classification", + // "vqa": "visual-question-answering", // TODO: Add + "asr": "automatic-speech-recognition", + "text-to-speech": "text-to-audio", + + // Add for backwards compatibility + "embeddings": "feature-extraction", +}); + +/** + * @typedef {keyof typeof SUPPORTED_TASKS} TaskType + * @typedef {keyof typeof TASK_ALIASES} AliasType + * @typedef {TaskType | AliasType} PipelineType All possible pipeline types. + * @typedef {{[K in TaskType]: InstanceType}} SupportedTasks A mapping of pipeline names to their corresponding pipeline classes. + * @typedef {{[K in AliasType]: InstanceType}} AliasTasks A mapping from pipeline aliases to their corresponding pipeline classes. + * @typedef {SupportedTasks & AliasTasks} AllTasks A mapping from all pipeline names and aliases to their corresponding pipeline classes. + */ + +/** + * Utility factory method to build a `Pipeline` object. + * + * @template {PipelineType} T The type of pipeline to return. + * @param {T} task The task defining which pipeline will be returned. Currently accepted tasks are: + * - `"audio-classification"`: will return a `AudioClassificationPipeline`. + * - `"automatic-speech-recognition"`: will return a `AutomaticSpeechRecognitionPipeline`. + * - `"depth-estimation"`: will return a `DepthEstimationPipeline`. + * - `"document-question-answering"`: will return a `DocumentQuestionAnsweringPipeline`. + * - `"feature-extraction"`: will return a `FeatureExtractionPipeline`. + * - `"fill-mask"`: will return a `FillMaskPipeline`. + * - `"image-classification"`: will return a `ImageClassificationPipeline`. + * - `"image-segmentation"`: will return a `ImageSegmentationPipeline`. + * - `"image-to-text"`: will return a `ImageToTextPipeline`. + * - `"object-detection"`: will return a `ObjectDetectionPipeline`. + * - `"question-answering"`: will return a `QuestionAnsweringPipeline`. + * - `"summarization"`: will return a `SummarizationPipeline`. + * - `"text2text-generation"`: will return a `Text2TextGenerationPipeline`. + * - `"text-classification"` (alias "sentiment-analysis" available): will return a `TextClassificationPipeline`. + * - `"text-generation"`: will return a `TextGenerationPipeline`. + * - `"token-classification"` (alias "ner" available): will return a `TokenClassificationPipeline`. + * - `"translation"`: will return a `TranslationPipeline`. + * - `"translation_xx_to_yy"`: will return a `TranslationPipeline`. + * - `"zero-shot-classification"`: will return a `ZeroShotClassificationPipeline`. + * - `"zero-shot-audio-classification"`: will return a `ZeroShotAudioClassificationPipeline`. + * - `"zero-shot-image-classification"`: will return a `ZeroShotImageClassificationPipeline`. + * - `"zero-shot-object-detection"`: will return a `ZeroShotObjectDetectionPipeline`. + * @param {string} [model=null] The name of the pre-trained model to use. If not specified, the default model for the task will be used. + * @param {import('./utils/hub.js').PretrainedModelOptions} [options] Optional parameters for the pipeline. + * @returns {Promise} A Pipeline object for the specified task. + * @throws {Error} If an unsupported pipeline is requested. + */ +async function pipeline( + task, + model = null, + { + progress_callback = null, + config = null, + cache_dir = null, + local_files_only = false, + revision = 'main', + device = null, + dtype = null, + model_file_name = null, + session_options = {}, + } = {} +) { + // Helper method to construct pipeline + + // Apply aliases + // @ts-ignore + task = TASK_ALIASES[task] ?? task; + + // Get pipeline info + const pipelineInfo = SUPPORTED_TASKS[task.split('_', 1)[0]]; + if (!pipelineInfo) { + throw Error(`Unsupported pipeline: ${task}. Must be one of [${Object.keys(SUPPORTED_TASKS)}]`) + } + + // Use model if specified, otherwise, use default + if (!model) { + model = pipelineInfo.default.model + console.log(`No model specified. Using default model: "${model}".`); + } + + const pretrainedOptions = { + progress_callback, + config, + cache_dir, + local_files_only, + revision, + device, + dtype, + model_file_name, + session_options, + } + + const classes = new Map([ + ['tokenizer', pipelineInfo.tokenizer], + ['model', pipelineInfo.model], + ['processor', pipelineInfo.processor], + ]); + + // Load model, tokenizer, and processor (if they exist) + const results = await loadItems(classes, model, pretrainedOptions); + results.task = task; + + (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.dispatchCallback)(progress_callback, { + 'status': 'ready', + 'task': task, + 'model': model, + }); + + const pipelineClass = pipelineInfo.pipeline; + return new pipelineClass(results); +} + + +/** + * Helper function to get applicable model, tokenizer, or processor classes for a given model. + * @param {Map} mapping The mapping of names to classes, arrays of classes, or null. + * @param {string} model The name of the model to load. + * @param {import('./utils/hub.js').PretrainedOptions} pretrainedOptions The options to pass to the `from_pretrained` method. + * @private + */ +async function loadItems(mapping, model, pretrainedOptions) { + + const result = Object.create(null); + + /**@type {Promise[]} */ + const promises = []; + for (const [name, cls] of mapping.entries()) { + if (!cls) continue; + + /**@type {Promise} */ + let promise; + if (Array.isArray(cls)) { + promise = new Promise(async (resolve, reject) => { + let e; + for (const c of cls) { + if (c === null) { + // If null, we resolve it immediately, meaning the relevant + // class was not found, but it is optional. + resolve(null); + return; + } + try { + resolve(await c.from_pretrained(model, pretrainedOptions)); + return; + } catch (err) { + if (err.message?.includes('Unsupported model type')) { + // If the error is due to an unsupported model type, we + // save the error and try the next class. + e = err; + } else if (err.message?.includes('Could not locate file')) { + e = err; + } else { + reject(err); + return; + } + + } + } + reject(e); + }) + } else { + promise = cls.from_pretrained(model, pretrainedOptions); + } + + result[name] = promise; + promises.push(promise); + } + + // Wait for all promises to resolve (in parallel) + await Promise.all(promises); + + // Then assign to result + for (const [name, promise] of Object.entries(result)) { + result[name] = await promise; + } + + return result; +} + +/***/ }), + +/***/ "./src/processors.js": +/*!***************************!*\ + !*** ./src/processors.js ***! + \***************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ ASTFeatureExtractor: () => (/* binding */ ASTFeatureExtractor), +/* harmony export */ AutoProcessor: () => (/* binding */ AutoProcessor), +/* harmony export */ BeitFeatureExtractor: () => (/* binding */ BeitFeatureExtractor), +/* harmony export */ BitImageProcessor: () => (/* binding */ BitImageProcessor), +/* harmony export */ CLIPFeatureExtractor: () => (/* binding */ CLIPFeatureExtractor), +/* harmony export */ CLIPImageProcessor: () => (/* binding */ CLIPImageProcessor), +/* harmony export */ ChineseCLIPFeatureExtractor: () => (/* binding */ ChineseCLIPFeatureExtractor), +/* harmony export */ ClapFeatureExtractor: () => (/* binding */ ClapFeatureExtractor), +/* harmony export */ ConvNextFeatureExtractor: () => (/* binding */ ConvNextFeatureExtractor), +/* harmony export */ ConvNextImageProcessor: () => (/* binding */ ConvNextImageProcessor), +/* harmony export */ DPTFeatureExtractor: () => (/* binding */ DPTFeatureExtractor), +/* harmony export */ DPTImageProcessor: () => (/* binding */ DPTImageProcessor), +/* harmony export */ DeiTFeatureExtractor: () => (/* binding */ DeiTFeatureExtractor), +/* harmony export */ DetrFeatureExtractor: () => (/* binding */ DetrFeatureExtractor), +/* harmony export */ DonutFeatureExtractor: () => (/* binding */ DonutFeatureExtractor), +/* harmony export */ DonutImageProcessor: () => (/* binding */ DonutImageProcessor), +/* harmony export */ EfficientNetImageProcessor: () => (/* binding */ EfficientNetImageProcessor), +/* harmony export */ FeatureExtractor: () => (/* binding */ FeatureExtractor), +/* harmony export */ Florence2Processor: () => (/* binding */ Florence2Processor), +/* harmony export */ GLPNFeatureExtractor: () => (/* binding */ GLPNFeatureExtractor), +/* harmony export */ ImageFeatureExtractor: () => (/* binding */ ImageFeatureExtractor), +/* harmony export */ MaskFormerFeatureExtractor: () => (/* binding */ MaskFormerFeatureExtractor), +/* harmony export */ MobileNetV1FeatureExtractor: () => (/* binding */ MobileNetV1FeatureExtractor), +/* harmony export */ MobileNetV2FeatureExtractor: () => (/* binding */ MobileNetV2FeatureExtractor), +/* harmony export */ MobileNetV3FeatureExtractor: () => (/* binding */ MobileNetV3FeatureExtractor), +/* harmony export */ MobileNetV4FeatureExtractor: () => (/* binding */ MobileNetV4FeatureExtractor), +/* harmony export */ MobileViTFeatureExtractor: () => (/* binding */ MobileViTFeatureExtractor), +/* harmony export */ MobileViTImageProcessor: () => (/* binding */ MobileViTImageProcessor), +/* harmony export */ NougatImageProcessor: () => (/* binding */ NougatImageProcessor), +/* harmony export */ OwlViTFeatureExtractor: () => (/* binding */ OwlViTFeatureExtractor), +/* harmony export */ OwlViTProcessor: () => (/* binding */ OwlViTProcessor), +/* harmony export */ Owlv2ImageProcessor: () => (/* binding */ Owlv2ImageProcessor), +/* harmony export */ Processor: () => (/* binding */ Processor), +/* harmony export */ PvtImageProcessor: () => (/* binding */ PvtImageProcessor), +/* harmony export */ PyAnnoteFeatureExtractor: () => (/* binding */ PyAnnoteFeatureExtractor), +/* harmony export */ PyAnnoteProcessor: () => (/* binding */ PyAnnoteProcessor), +/* harmony export */ RTDetrImageProcessor: () => (/* binding */ RTDetrImageProcessor), +/* harmony export */ SamImageProcessor: () => (/* binding */ SamImageProcessor), +/* harmony export */ SamProcessor: () => (/* binding */ SamProcessor), +/* harmony export */ SapiensFeatureExtractor: () => (/* binding */ SapiensFeatureExtractor), +/* harmony export */ SeamlessM4TFeatureExtractor: () => (/* binding */ SeamlessM4TFeatureExtractor), +/* harmony export */ SegformerFeatureExtractor: () => (/* binding */ SegformerFeatureExtractor), +/* harmony export */ SiglipImageProcessor: () => (/* binding */ SiglipImageProcessor), +/* harmony export */ SpeechT5FeatureExtractor: () => (/* binding */ SpeechT5FeatureExtractor), +/* harmony export */ SpeechT5Processor: () => (/* binding */ SpeechT5Processor), +/* harmony export */ Swin2SRImageProcessor: () => (/* binding */ Swin2SRImageProcessor), +/* harmony export */ ViTFeatureExtractor: () => (/* binding */ ViTFeatureExtractor), +/* harmony export */ ViTImageProcessor: () => (/* binding */ ViTImageProcessor), +/* harmony export */ VitMatteImageProcessor: () => (/* binding */ VitMatteImageProcessor), +/* harmony export */ Wav2Vec2FeatureExtractor: () => (/* binding */ Wav2Vec2FeatureExtractor), +/* harmony export */ Wav2Vec2ProcessorWithLM: () => (/* binding */ Wav2Vec2ProcessorWithLM), +/* harmony export */ WeSpeakerFeatureExtractor: () => (/* binding */ WeSpeakerFeatureExtractor), +/* harmony export */ WhisperFeatureExtractor: () => (/* binding */ WhisperFeatureExtractor), +/* harmony export */ WhisperProcessor: () => (/* binding */ WhisperProcessor), +/* harmony export */ YolosFeatureExtractor: () => (/* binding */ YolosFeatureExtractor) +/* harmony export */ }); +/* harmony import */ var _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./utils/generic.js */ "./src/utils/generic.js"); +/* harmony import */ var _utils_core_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./utils/core.js */ "./src/utils/core.js"); +/* harmony import */ var _utils_hub_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./utils/hub.js */ "./src/utils/hub.js"); +/* harmony import */ var _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./utils/maths.js */ "./src/utils/maths.js"); +/* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ./utils/tensor.js */ "./src/utils/tensor.js"); +/* harmony import */ var _utils_image_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ./utils/image.js */ "./src/utils/image.js"); +/* harmony import */ var _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! ./utils/audio.js */ "./src/utils/audio.js"); + +/** + * @file Processors are used to prepare non-textual inputs (e.g., image or audio) for a model. + * + * **Example:** Using a `WhisperProcessor` to prepare an audio input for a model. + * ```javascript + * import { AutoProcessor, read_audio } from '@huggingface/transformers'; + * + * let processor = await AutoProcessor.from_pretrained('openai/whisper-tiny.en'); + * let audio = await read_audio('https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac', 16000); + * let { input_features } = await processor(audio); + * // Tensor { + * // data: Float32Array(240000) [0.4752984642982483, 0.5597258806228638, 0.56434166431427, ...], + * // dims: [1, 80, 3000], + * // type: 'float32', + * // size: 240000, + * // } + * ``` + * + * @module processors + */ + + + + + + + + + + + + + + + +// Helper functions + +/** + * Converts bounding boxes from center format to corners format. + * + * @param {number[]} arr The coordinate for the center of the box and its width, height dimensions (center_x, center_y, width, height) + * @returns {number[]} The coodinates for the top-left and bottom-right corners of the box (top_left_x, top_left_y, bottom_right_x, bottom_right_y) + */ +function center_to_corners_format([centerX, centerY, width, height]) { + return [ + centerX - width / 2, + centerY - height / 2, + centerX + width / 2, + centerY + height / 2 + ]; +} + +/** + * Post-processes the outputs of the model (for object detection). + * @param {Object} outputs The outputs of the model that must be post-processed + * @param {Tensor} outputs.logits The logits + * @param {Tensor} outputs.pred_boxes The predicted boxes. + * @param {number} [threshold=0.5] The threshold to use for the scores. + * @param {[number, number][]} [target_sizes=null] The sizes of the original images. + * @param {boolean} [is_zero_shot=false] Whether zero-shot object detection was performed. + * @return {Object[]} An array of objects containing the post-processed outputs. + * @private + */ +function post_process_object_detection(outputs, threshold = 0.5, target_sizes = null, is_zero_shot = false) { + const out_logits = outputs.logits; + const out_bbox = outputs.pred_boxes; + const [batch_size, num_boxes, num_classes] = out_logits.dims; + + if (target_sizes !== null && target_sizes.length !== batch_size) { + throw Error("Make sure that you pass in as many target sizes as the batch dimension of the logits") + } + let toReturn = []; + for (let i = 0; i < batch_size; ++i) { + let target_size = target_sizes !== null ? target_sizes[i] : null; + let info = { + boxes: [], + classes: [], + scores: [] + } + let logits = out_logits[i]; + let bbox = out_bbox[i]; + + for (let j = 0; j < num_boxes; ++j) { + let logit = logits[j]; + + let indices = []; + let probs; + if (is_zero_shot) { + // Get indices of classes with high enough probability + probs = logit.sigmoid().data; + for (let k = 0; k < probs.length; ++k) { + if (probs[k] > threshold) { + indices.push(k); + } + } + + } else { + // Get most probable class + let maxIndex = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.max)(logit.data)[1]; + + if (maxIndex === num_classes - 1) { + // This is the background class, skip it + continue; + } + // Compute softmax over classes + probs = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.softmax)(logit.data); + + if (probs[maxIndex] < threshold) { + continue; + } + indices.push(maxIndex); + } + + for (const index of indices) { + + // Some class has a high enough probability + /** @type {number[]} */ + let box = bbox[j].data; + + // convert to [x0, y0, x1, y1] format + box = center_to_corners_format(box) + if (target_size !== null) { + box = box.map((x, i) => x * target_size[(i + 1) % 2]) + } + + info.boxes.push(box); + info.classes.push(index); + info.scores.push(probs[index]); + } + } + toReturn.push(info); + } + return toReturn; +} + + +/** + * Post-processes the outputs of the model (for semantic segmentation). + * @param {*} outputs Raw outputs of the model. + * @param {[number, number][]} [target_sizes=null] List of tuples corresponding to the requested final size + * (height, width) of each prediction. If unset, predictions will not be resized. + * @returns {{segmentation: Tensor; labels: number[]}[]} The semantic segmentation maps. + */ +function post_process_semantic_segmentation(outputs, target_sizes = null) { + + const logits = outputs.logits; + const batch_size = logits.dims[0]; + + if (target_sizes !== null && target_sizes.length !== batch_size) { + throw Error("Make sure that you pass in as many target sizes as the batch dimension of the logits") + } + + const toReturn = []; + for (let i = 0; i < batch_size; ++i) { + const target_size = target_sizes !== null ? target_sizes[i] : null; + + let data = logits[i]; + + // 1. If target_size is not null, we need to resize the masks to the target size + if (target_size !== null) { + // resize the masks to the target size + data = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.interpolate)(data, target_size, 'bilinear', false); + } + const [height, width] = target_size ?? data.dims.slice(-2); + + const segmentation = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( + 'int32', + new Int32Array(height * width), + [height, width] + ); + + // Buffer to store current largest value + const buffer = data[0].data; + const segmentation_data = segmentation.data; + for (let j = 1; j < data.dims[0]; ++j) { + const row = data[j].data; + for (let k = 0; k < row.length; ++k) { + if (row[k] > buffer[k]) { + buffer[k] = row[k]; + segmentation_data[k] = j; + } + } + } + + // Store which objects have labels + // This is much more efficient that creating a set of the final values + const hasLabel = new Array(data.dims[0]); + for (let j = 0; j < segmentation_data.length; ++j) { + const index = segmentation_data[j]; + hasLabel[index] = index; + } + /** @type {number[]} The unique list of labels that were detected */ + const labels = hasLabel.filter(x => x !== undefined); + + toReturn.push({ segmentation, labels }); + } + return toReturn; +} + + +/** + * Binarize the given masks using `object_mask_threshold`, it returns the associated values of `masks`, `scores` and `labels`. + * @param {Tensor} class_logits The class logits. + * @param {Tensor} mask_logits The mask logits. + * @param {number} object_mask_threshold A number between 0 and 1 used to binarize the masks. + * @param {number} num_labels The number of labels. + * @returns {[Tensor[], number[], number[]]} The binarized masks, the scores, and the labels. + * @private + */ +function remove_low_and_no_objects(class_logits, mask_logits, object_mask_threshold, num_labels) { + + const mask_probs_item = []; + const pred_scores_item = []; + const pred_labels_item = []; + + for (let j = 0; j < class_logits.dims[0]; ++j) { + const cls = class_logits[j]; + const mask = mask_logits[j]; + + const pred_label = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.max)(cls.data)[1]; + if (pred_label === num_labels) { + // Is the background, so we ignore it + continue; + } + + const scores = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.softmax)(cls.data); + const pred_score = scores[pred_label]; + if (pred_score > object_mask_threshold) { + mask_probs_item.push(mask); + pred_scores_item.push(pred_score); + pred_labels_item.push(pred_label); + } + } + + return [mask_probs_item, pred_scores_item, pred_labels_item]; +} + +/** + * Checks whether the segment is valid or not. + * @param {Int32Array} mask_labels Labels for each pixel in the mask. + * @param {Tensor[]} mask_probs Probabilities for each pixel in the masks. + * @param {number} k The class id of the segment. + * @param {number} mask_threshold The mask threshold. + * @param {number} overlap_mask_area_threshold The overlap mask area threshold. + * @returns {[boolean, number[]]} Whether the segment is valid or not, and the indices of the valid labels. + * @private + */ +function check_segment_validity( + mask_labels, + mask_probs, + k, + mask_threshold = 0.5, + overlap_mask_area_threshold = 0.8 +) { + // mask_k is a 1D array of indices, indicating where the mask is equal to k + const mask_k = []; + let mask_k_area = 0; + let original_area = 0; + + const mask_probs_k_data = mask_probs[k].data; + + // Compute the area of all the stuff in query k + for (let i = 0; i < mask_labels.length; ++i) { + if (mask_labels[i] === k) { + mask_k.push(i); + ++mask_k_area; + } + + if (mask_probs_k_data[i] >= mask_threshold) { + ++original_area; + } + } + let mask_exists = mask_k_area > 0 && original_area > 0; + + // Eliminate disconnected tiny segments + if (mask_exists) { + // Perform additional check + let area_ratio = mask_k_area / original_area; + mask_exists = area_ratio > overlap_mask_area_threshold; + } + + return [mask_exists, mask_k] +} + +/** + * Computes the segments. + * @param {Tensor[]} mask_probs The mask probabilities. + * @param {number[]} pred_scores The predicted scores. + * @param {number[]} pred_labels The predicted labels. + * @param {number} mask_threshold The mask threshold. + * @param {number} overlap_mask_area_threshold The overlap mask area threshold. + * @param {Set} label_ids_to_fuse The label ids to fuse. + * @param {number[]} target_size The target size of the image. + * @returns {[Tensor, Array<{id: number, label_id: number, score: number}>]} The computed segments. + * @private + */ +function compute_segments( + mask_probs, + pred_scores, + pred_labels, + mask_threshold, + overlap_mask_area_threshold, + label_ids_to_fuse = null, + target_size = null, +) { + const [height, width] = target_size ?? mask_probs[0].dims; + + const segmentation = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( + 'int32', + new Int32Array(height * width), + [height, width] + ); + const segments = []; + + // 1. If target_size is not null, we need to resize the masks to the target size + if (target_size !== null) { + // resize the masks to the target size + for (let i = 0; i < mask_probs.length; ++i) { + mask_probs[i] = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.interpolate)(mask_probs[i], target_size, 'bilinear', false); + } + } + + // 2. Weigh each mask by its prediction score + // NOTE: `mask_probs` is updated in-place + // + // Temporary storage for the best label/scores for each pixel ([height, width]): + const mask_labels = new Int32Array(mask_probs[0].data.length); + const bestScores = new Float32Array(mask_probs[0].data.length); + + for (let i = 0; i < mask_probs.length; ++i) { + let score = pred_scores[i]; + + const mask_probs_i_data = mask_probs[i].data; + + for (let j = 0; j < mask_probs_i_data.length; ++j) { + mask_probs_i_data[j] *= score + if (mask_probs_i_data[j] > bestScores[j]) { + mask_labels[j] = i; + bestScores[j] = mask_probs_i_data[j]; + } + } + } + + let current_segment_id = 0; + + // let stuff_memory_list = {} + const segmentation_data = segmentation.data; + for (let k = 0; k < pred_labels.length; ++k) { + const pred_class = pred_labels[k]; + + // TODO add `should_fuse` + // let should_fuse = pred_class in label_ids_to_fuse + + // Check if mask exists and large enough to be a segment + const [mask_exists, mask_k] = check_segment_validity( + mask_labels, + mask_probs, + k, + mask_threshold, + overlap_mask_area_threshold + ) + + if (!mask_exists) { + // Nothing to see here + continue; + } + + // TODO + // if (pred_class in stuff_memory_list) { + // current_segment_id = stuff_memory_list[pred_class] + // } else { + // current_segment_id += 1; + // } + ++current_segment_id; + + + // Add current object segment to final segmentation map + for (const index of mask_k) { + segmentation_data[index] = current_segment_id; + } + + segments.push({ + id: current_segment_id, + label_id: pred_class, + // was_fused: should_fuse, TODO + score: pred_scores[k], + }) + + // TODO + // if(should_fuse){ + // stuff_memory_list[pred_class] = current_segment_id + // } + } + + return [segmentation, segments]; +} + + +/** + * Post-process the model output to generate the final panoptic segmentation. + * @param {*} outputs The model output to post process + * @param {number} [threshold=0.5] The probability score threshold to keep predicted instance masks. + * @param {number} [mask_threshold=0.5] Threshold to use when turning the predicted masks into binary values. + * @param {number} [overlap_mask_area_threshold=0.8] The overlap mask area threshold to merge or discard small disconnected parts within each binary instance mask. + * @param {Set} [label_ids_to_fuse=null] The labels in this state will have all their instances be fused together. + * @param {[number, number][]} [target_sizes=null] The target sizes to resize the masks to. + * @returns {Array<{ segmentation: Tensor, segments_info: Array<{id: number, label_id: number, score: number}>}>} + */ +function post_process_panoptic_segmentation( + outputs, + threshold = 0.5, + mask_threshold = 0.5, + overlap_mask_area_threshold = 0.8, + label_ids_to_fuse = null, + target_sizes = null, +) { + if (label_ids_to_fuse === null) { + console.warn("`label_ids_to_fuse` unset. No instance will be fused.") + label_ids_to_fuse = new Set(); + } + + const class_queries_logits = outputs.class_queries_logits ?? outputs.logits; // [batch_size, num_queries, num_classes+1] + const masks_queries_logits = outputs.masks_queries_logits ?? outputs.pred_masks; // [batch_size, num_queries, height, width] + + const mask_probs = masks_queries_logits.sigmoid() // [batch_size, num_queries, height, width] + + let [batch_size, num_queries, num_labels] = class_queries_logits.dims; + num_labels -= 1; // Remove last class (background) + + if (target_sizes !== null && target_sizes.length !== batch_size) { + throw Error("Make sure that you pass in as many target sizes as the batch dimension of the logits") + } + + let toReturn = []; + for (let i = 0; i < batch_size; ++i) { + let target_size = target_sizes !== null ? target_sizes[i] : null; + + let class_logits = class_queries_logits[i]; + let mask_logits = mask_probs[i]; + + let [mask_probs_item, pred_scores_item, pred_labels_item] = remove_low_and_no_objects(class_logits, mask_logits, threshold, num_labels); + + if (pred_labels_item.length === 0) { + // No mask found + let [height, width] = target_size ?? mask_logits.dims.slice(-2); + + let segmentation = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( + 'int32', + new Int32Array(height * width).fill(-1), + [height, width] + ) + toReturn.push({ + segmentation: segmentation, + segments_info: [] + }); + continue; + } + + + // Get segmentation map and segment information of batch item + let [segmentation, segments] = compute_segments( + mask_probs_item, + pred_scores_item, + pred_labels_item, + mask_threshold, + overlap_mask_area_threshold, + label_ids_to_fuse, + target_size, + ) + + toReturn.push({ + segmentation: segmentation, + segments_info: segments + }) + } + + return toReturn; +} + + +/** + * Post-processes the outputs of the model (for instance segmentation). + * @param {*} outputs Raw outputs of the model. + * @param {number} [threshold=0.5] The probability score threshold to keep predicted instance masks. + * @param {[number, number][]} [target_sizes=null] List of tuples corresponding to the requested final size + * (height, width) of each prediction. If unset, predictions will not be resized. + * @returns {Array<{ segmentation: Tensor, segments_info: Array<{id: number, label_id: number, score: number}>}>} + */ +function post_process_instance_segmentation(outputs, threshold = 0.5, target_sizes = null) { + throw new Error('Not implemented yet'); + return []; +} + +/** + * Named tuple to indicate the order we are using is (height x width), even though + * the Graphics’ industry standard is (width x height). + * @typedef {[height: number, width: number]} HeightWidth + */ + +/** + * Helper function to validate audio inputs. + * @param {any} audio The audio data. + * @param {string} feature_extractor The name of the feature extractor. + * @private + */ +function validate_audio_inputs(audio, feature_extractor) { + if (!(audio instanceof Float32Array || audio instanceof Float64Array)) { + throw new Error( + `${feature_extractor} expects input to be a Float32Array or a Float64Array, but got ${audio?.constructor?.name ?? typeof audio} instead. ` + + `If using the feature extractor directly, remember to use \`read_audio(url, sampling_rate)\` to obtain the raw audio data of the file/url.` + ) + } +} + +/** + * Helper function to constrain a value to be a multiple of a number. + * @param {number} val The value to constrain. + * @param {number} multiple The number to constrain to. + * @param {number} [minVal=0] The minimum value to constrain to. + * @param {number} [maxVal=null] The maximum value to constrain to. + * @returns {number} The constrained value. + * @private + */ +function constraint_to_multiple_of(val, multiple, minVal = 0, maxVal = null) { + const a = val / multiple; + let x = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.bankers_round)(a) * multiple; + + if (maxVal !== null && x > maxVal) { + x = Math.floor(a) * multiple; + } + + if (x < minVal) { + x = Math.ceil(a) * multiple; + } + + return x; +} + +/** + * Rounds the height and width down to the closest multiple of size_divisibility + * @param {[number, number]} size The size of the image + * @param {number} divisor The divisor to use. + * @returns {[number, number]} The rounded size. + */ +function enforce_size_divisibility([width, height], divisor) { + return [ + Math.max(Math.floor(width / divisor), 1) * divisor, + Math.max(Math.floor(height / divisor), 1) * divisor + ]; +} + + +/** + * Base class for feature extractors. + * + * @extends Callable + */ +class FeatureExtractor extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * Constructs a new FeatureExtractor instance. + * + * @param {Object} config The configuration for the feature extractor. + */ + constructor(config) { + super(); + this.config = config + } +} + +/** + * @typedef {object} ImageFeatureExtractorResult + * @property {Tensor} pixel_values The pixel values of the batched preprocessed images. + * @property {HeightWidth[]} original_sizes Array of two-dimensional tuples like [[480, 640]]. + * @property {HeightWidth[]} reshaped_input_sizes Array of two-dimensional tuples like [[1000, 1330]]. + */ + +/** + * Feature extractor for image models. + * + * @extends FeatureExtractor + */ +class ImageFeatureExtractor extends FeatureExtractor { + + /** + * Constructs a new ImageFeatureExtractor instance. + * + * @param {Object} config The configuration for the feature extractor. + * @param {number[]} config.image_mean The mean values for image normalization. + * @param {number[]} config.image_std The standard deviation values for image normalization. + * @param {boolean} config.do_rescale Whether to rescale the image pixel values to the [0,1] range. + * @param {number} config.rescale_factor The factor to use for rescaling the image pixel values. + * @param {boolean} config.do_normalize Whether to normalize the image pixel values. + * @param {boolean} config.do_resize Whether to resize the image. + * @param {number} config.resample What method to use for resampling. + * @param {number|Object} config.size The size to resize the image to. + * @param {boolean} [config.do_flip_channel_order=false] Whether to flip the color channels from RGB to BGR. + * Can be overridden by the `do_flip_channel_order` parameter in the `preprocess` method. + */ + constructor(config) { + super(config); + + this.image_mean = this.config.image_mean ?? this.config.mean; + this.image_std = this.config.image_std ?? this.config.std; + + this.resample = this.config.resample ?? 2; // 2 => bilinear + this.do_rescale = this.config.do_rescale ?? true; + this.rescale_factor = this.config.rescale_factor ?? (1 / 255); + this.do_normalize = this.config.do_normalize; + + this.do_resize = this.config.do_resize; + this.do_thumbnail = this.config.do_thumbnail; + this.size = this.config.size; + this.size_divisibility = this.config.size_divisibility ?? this.config.size_divisor; + + this.do_center_crop = this.config.do_center_crop; + this.crop_size = this.config.crop_size; + this.do_convert_rgb = this.config.do_convert_rgb ?? true; + this.do_crop_margin = this.config.do_crop_margin; + + this.pad_size = this.config.pad_size; + this.do_pad = this.config.do_pad; + + if (this.do_pad && !this.pad_size && this.size && this.size.width !== undefined && this.size.height !== undefined) { + // Should pad, but no pad size specified + // We infer the pad size from the resize size + this.pad_size = this.size + } + + this.do_flip_channel_order = this.config.do_flip_channel_order ?? false; + } + + /** + * Resize the image to make a thumbnail. The image is resized so that no dimension is larger than any + * corresponding dimension of the specified size. + * @param {RawImage} image The image to be resized. + * @param {{height:number, width:number}} size The size `{"height": h, "width": w}` to resize the image to. + * @param {string | 0 | 1 | 2 | 3 | 4 | 5} [resample=2] The resampling filter to use. + * @returns {Promise} The resized image. + */ + async thumbnail(image, size, resample = 2) { + const input_height = image.height; + const input_width = image.width; + + const output_height = size.height; + const output_width = size.width; + + // We always resize to the smallest of either the input or output size. + let height = Math.min(input_height, output_height) + let width = Math.min(input_width, output_width) + + if (height === input_height && width === input_width) { + return image; + } + if (input_height > input_width) { + width = Math.floor(input_width * height / input_height); + } else if (input_width > input_height) { + height = Math.floor(input_height * width / input_width); + } + return await image.resize(width, height, { resample }); + } + + + /** + * Crops the margin of the image. Gray pixels are considered margin (i.e., pixels with a value below the threshold). + * @param {RawImage} image The image to be cropped. + * @param {number} gray_threshold Value below which pixels are considered to be gray. + * @returns {Promise} The cropped image. + */ + async crop_margin(image, gray_threshold = 200) { + + const gray_image = image.clone().grayscale(); + + const minValue = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.min)(gray_image.data)[0]; + const maxValue = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.max)(gray_image.data)[0]; + const diff = maxValue - minValue; + + if (diff === 0) { + return image; + } + + const threshold = gray_threshold / 255; + + let x_min = gray_image.width, y_min = gray_image.height, x_max = 0, y_max = 0; + const gray_image_data = gray_image.data; + for (let j = 0; j < gray_image.height; ++j) { + const row = j * gray_image.width; + for (let i = 0; i < gray_image.width; ++i) { + if ((gray_image_data[row + i] - minValue) / diff < threshold) { + // We have a non-zero pixel, so we update the min/max values accordingly + x_min = Math.min(x_min, i); + y_min = Math.min(y_min, j); + x_max = Math.max(x_max, i); + y_max = Math.max(y_max, j); + } + } + } + + image = await image.crop([x_min, y_min, x_max, y_max]); + return image; + } + + /** + * Pad the image by a certain amount. + * @param {Float32Array} pixelData The pixel data to pad. + * @param {number[]} imgDims The dimensions of the image (height, width, channels). + * @param {{width:number; height:number}|number} padSize The dimensions of the padded image. + * @param {Object} options The options for padding. + * @param {'constant'|'symmetric'} [options.mode='constant'] The type of padding to add. + * @param {boolean} [options.center=false] Whether to center the image. + * @param {number} [options.constant_values=0] The constant value to use for padding. + * @returns {[Float32Array, number[]]} The padded pixel data and image dimensions. + */ + pad_image(pixelData, imgDims, padSize, { + mode = 'constant', + center = false, + constant_values = 0, + } = {}) { + const [imageHeight, imageWidth, imageChannels] = imgDims; + + let paddedImageWidth, paddedImageHeight; + if (typeof padSize === 'number') { + paddedImageWidth = padSize; + paddedImageHeight = padSize; + } else { + paddedImageWidth = padSize.width; + paddedImageHeight = padSize.height; + } + + // Only add padding if there is a difference in size + if (paddedImageWidth !== imageWidth || paddedImageHeight !== imageHeight) { + const paddedPixelData = new Float32Array(paddedImageWidth * paddedImageHeight * imageChannels); + if (Array.isArray(constant_values)) { + // Fill with constant values, cycling through the array + for (let i = 0; i < paddedPixelData.length; ++i) { + paddedPixelData[i] = constant_values[i % imageChannels]; + } + } else if (constant_values !== 0) { + paddedPixelData.fill(constant_values); + } + + const [left, top] = center + ? [Math.floor((paddedImageWidth - imageWidth) / 2), Math.floor((paddedImageHeight - imageHeight) / 2)] + : [0, 0]; + + // Copy the original image into the padded image + for (let i = 0; i < imageHeight; ++i) { + const a = (i + top) * paddedImageWidth; + const b = i * imageWidth; + for (let j = 0; j < imageWidth; ++j) { + const c = (a + j + left) * imageChannels; + const d = (b + j) * imageChannels; + for (let k = 0; k < imageChannels; ++k) { + paddedPixelData[c + k] = pixelData[d + k]; + } + } + } + + if (mode === 'symmetric') { + if (center) { + throw new Error('`center` padding is not supported when `mode` is set to `symmetric`.'); + // TODO: Implement this + } + const h1 = imageHeight - 1; + const w1 = imageWidth - 1; + for (let i = 0; i < paddedImageHeight; ++i) { + const a = i * paddedImageWidth; + const b = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.calculateReflectOffset)(i, h1) * imageWidth; + + for (let j = 0; j < paddedImageWidth; ++j) { + if (i < imageHeight && j < imageWidth) continue; // Do not overwrite original image + const c = (a + j) * imageChannels; + const d = (b + (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.calculateReflectOffset)(j, w1)) * imageChannels; + + // Copy channel-wise + for (let k = 0; k < imageChannels; ++k) { + paddedPixelData[c + k] = pixelData[d + k]; + } + } + } + } + + + // Update pixel data and image dimensions + pixelData = paddedPixelData; + imgDims = [paddedImageHeight, paddedImageWidth, imageChannels] + } + return [pixelData, imgDims]; + } + + /** + * Rescale the image' pixel values by `this.rescale_factor`. + * @param {Float32Array} pixelData The pixel data to rescale. + * @returns {void} + */ + rescale(pixelData) { + for (let i = 0; i < pixelData.length; ++i) { + pixelData[i] = this.rescale_factor * pixelData[i]; + } + } + + /** + * Find the target (width, height) dimension of the output image after + * resizing given the input image and the desired size. + * @param {RawImage} image The image to resize. + * @param {any} size The size to use for resizing the image. + * @returns {[number, number]} The target (width, height) dimension of the output image after resizing. + */ + get_resize_output_image_size(image, size) { + // `size` comes in many forms, so we need to handle them all here: + // 1. `size` is an integer, in which case we resize the image to be a square + + const [srcWidth, srcHeight] = image.size; + + let shortest_edge; + let longest_edge; + + if (this.do_thumbnail) { + // NOTE: custom logic for `Donut` models + const { height, width } = size; + shortest_edge = Math.min(height, width) + } + // Support both formats for backwards compatibility + else if (Number.isInteger(size)) { + shortest_edge = size; + longest_edge = this.config.max_size ?? shortest_edge; + + } else if (size !== undefined) { + // Extract known properties from `size` + shortest_edge = size.shortest_edge; + longest_edge = size.longest_edge; + } + + // If `longest_edge` and `shortest_edge` are set, maintain aspect ratio and resize to `shortest_edge` + // while keeping the largest dimension <= `longest_edge` + if (shortest_edge !== undefined || longest_edge !== undefined) { + // http://opensourcehacker.com/2011/12/01/calculate-aspect-ratio-conserving-resize-for-images-in-javascript/ + // Try resize so that shortest edge is `shortest_edge` (target) + const shortResizeFactor = shortest_edge === undefined + ? 1 // If `shortest_edge` is not set, don't upscale + : Math.max(shortest_edge / srcWidth, shortest_edge / srcHeight); + + const newWidth = srcWidth * shortResizeFactor; + const newHeight = srcHeight * shortResizeFactor; + + // The new width and height might be greater than `longest_edge`, so + // we downscale again to ensure the largest dimension is `longest_edge` + const longResizeFactor = longest_edge === undefined + ? 1 // If `longest_edge` is not set, don't downscale + : Math.min(longest_edge / newWidth, longest_edge / newHeight); + + // To avoid certain floating point precision issues, we round to 2 decimal places + let finalWidth = Math.floor(Number((newWidth * longResizeFactor).toFixed(2))); + let finalHeight = Math.floor(Number((newHeight * longResizeFactor).toFixed(2))); + + if (this.size_divisibility !== undefined) { + [finalWidth, finalHeight] = enforce_size_divisibility([finalWidth, finalHeight], this.size_divisibility) + } + return [finalWidth, finalHeight]; + + } else if (size !== undefined && size.width !== undefined && size.height !== undefined) { + // If `width` and `height` are set, resize to those dimensions + + let newWidth = size.width; + let newHeight = size.height; + + // Custom for DPT models + if (this.config.keep_aspect_ratio && this.config.ensure_multiple_of) { + + // determine new height and width + let scale_height = newHeight / srcHeight; + let scale_width = newWidth / srcWidth; + + // scale as little as possible + if (Math.abs(1 - scale_width) < Math.abs(1 - scale_height)) { + // fit width + scale_height = scale_width; + } else { + // fit height + scale_width = scale_height; + } + + newHeight = constraint_to_multiple_of(scale_height * srcHeight, this.config.ensure_multiple_of); + newWidth = constraint_to_multiple_of(scale_width * srcWidth, this.config.ensure_multiple_of); + } + + return [newWidth, newHeight]; + + } else if (this.size_divisibility !== undefined) { + return enforce_size_divisibility([srcWidth, srcHeight], this.size_divisibility); + } else { + throw new Error(`Could not resize image due to unsupported \`this.size\` option in config: ${JSON.stringify(size)}`); + } + } + + /** + * Resizes the image. + * @param {RawImage} image The image to resize. + * @returns {Promise} The resized image. + */ + async resize(image) { + const [newWidth, newHeight] = this.get_resize_output_image_size(image, this.size); + return await image.resize(newWidth, newHeight, { + resample: this.resample, + }); + } + + /** + * @typedef {object} PreprocessedImage + * @property {HeightWidth} original_size The original size of the image. + * @property {HeightWidth} reshaped_input_size The reshaped input size of the image. + * @property {Tensor} pixel_values The pixel values of the preprocessed image. + */ + + /** + * Preprocesses the given image. + * + * @param {RawImage} image The image to preprocess. + * @param {Object} overrides The overrides for the preprocessing options. + * @returns {Promise} The preprocessed image. + */ + async preprocess(image, { + do_normalize = null, + do_pad = null, + do_convert_rgb = null, + do_convert_grayscale = null, + do_flip_channel_order = null, + } = {}) { + if (this.do_crop_margin) { + // NOTE: Specific to nougat processors. This is done before resizing, + // and can be interpreted as a pre-preprocessing step. + image = await this.crop_margin(image); + } + + const [srcWidth, srcHeight] = image.size; // original image size + + // Convert image to RGB if specified in config. + if (do_convert_rgb ?? this.do_convert_rgb) { + image = image.rgb(); + } else if (do_convert_grayscale) { + image = image.grayscale(); + } + + // TODO: + // For efficiency reasons, it might be best to merge the resize and center crop operations into one. + + // Resize all images + if (this.do_resize) { + image = await this.resize(image); + } + + // Resize the image using thumbnail method. + if (this.do_thumbnail) { + image = await this.thumbnail(image, this.size, this.resample); + } + + if (this.do_center_crop) { + + let crop_width; + let crop_height; + if (Number.isInteger(this.crop_size)) { + crop_width = this.crop_size; + crop_height = this.crop_size; + } else { + crop_width = this.crop_size.width; + crop_height = this.crop_size.height; + } + + image = await image.center_crop(crop_width, crop_height); + } + + /** @type {HeightWidth} */ + const reshaped_input_size = [image.height, image.width]; + + // NOTE: All pixel-level manipulation (i.e., modifying `pixelData`) + // occurs with data in the hwc format (height, width, channels), + // to emulate the behavior of the original Python code (w/ numpy). + let pixelData = Float32Array.from(image.data); + let imgDims = [image.height, image.width, image.channels]; + + if (this.do_rescale) { + this.rescale(pixelData); + } + + if (do_normalize ?? this.do_normalize) { + let image_mean = this.image_mean; + if (!Array.isArray(this.image_mean)) { + image_mean = new Array(image.channels).fill(image_mean); + } + + let image_std = this.image_std; + if (!Array.isArray(this.image_std)) { + image_std = new Array(image.channels).fill(image_mean); + } + + if (image_mean.length !== image.channels || image_std.length !== image.channels) { + throw new Error(`When set to arrays, the length of \`image_mean\` (${image_mean.length}) and \`image_std\` (${image_std.length}) must match the number of channels in the image (${image.channels}).`); + } + + for (let i = 0; i < pixelData.length; i += image.channels) { + for (let j = 0; j < image.channels; ++j) { + pixelData[i + j] = (pixelData[i + j] - image_mean[j]) / image_std[j]; + } + } + } + + // do padding after rescaling/normalizing + if (do_pad ?? this.do_pad) { + if (this.pad_size) { + const padded = this.pad_image(pixelData, [image.height, image.width, image.channels], this.pad_size); + [pixelData, imgDims] = padded; // Update pixel data and image dimensions + } else if (this.size_divisibility) { + const [paddedWidth, paddedHeight] = enforce_size_divisibility([imgDims[1], imgDims[0]], this.size_divisibility); + [pixelData, imgDims] = this.pad_image(pixelData, imgDims, { width: paddedWidth, height: paddedHeight }); + } + } + + if (do_flip_channel_order ?? this.do_flip_channel_order) { + if (imgDims[2] !== 3) { + throw new Error('Flipping channel order is only supported for RGB images.'); + } + // Convert RGB to BGR + for (let i = 0; i < pixelData.length; i += 3) { + const temp = pixelData[i]; + pixelData[i] = pixelData[i + 2]; + pixelData[i + 2] = temp; + } + } + + const pixel_values = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor('float32', pixelData, imgDims) + .permute(2, 0, 1); // convert to channel dimension format (hwc -> chw) + + return { + original_size: [srcHeight, srcWidth], + reshaped_input_size: reshaped_input_size, + pixel_values, + } + } + + /** + * Calls the feature extraction process on an array of images, + * preprocesses each image, and concatenates the resulting + * features into a single Tensor. + * @param {RawImage[]} images The image(s) to extract features from. + * @param {...any} args Additional arguments. + * @returns {Promise} An object containing the concatenated pixel values (and other metadata) of the preprocessed images. + */ + async _call(images, ...args) { + if (!Array.isArray(images)) { + images = [images]; + } + /** @type {PreprocessedImage[]} */ + const imageData = await Promise.all(images.map(x => this.preprocess(x))); + + // Stack pixel values + const pixel_values = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.stack)(imageData.map(x => x.pixel_values), 0); + + return { + pixel_values, + + // Original sizes of images + original_sizes: imageData.map(x => x.original_size), + + // Reshaped sizes of images, before padding or cropping + reshaped_input_sizes: imageData.map(x => x.reshaped_input_size), + } + } + +} + +class SapiensFeatureExtractor extends ImageFeatureExtractor { + /** @type {typeof post_process_semantic_segmentation} */ + post_process_semantic_segmentation(...args) { + return post_process_semantic_segmentation(...args); + } +} +class SegformerFeatureExtractor extends ImageFeatureExtractor { + /** @type {typeof post_process_semantic_segmentation} */ + post_process_semantic_segmentation(...args) { + return post_process_semantic_segmentation(...args); + } +} +class PvtImageProcessor extends ImageFeatureExtractor { } +class DPTFeatureExtractor extends ImageFeatureExtractor { } +class DPTImageProcessor extends DPTFeatureExtractor { } // NOTE: extends DPTFeatureExtractor +class BitImageProcessor extends ImageFeatureExtractor { } +class GLPNFeatureExtractor extends ImageFeatureExtractor { } +class CLIPFeatureExtractor extends ImageFeatureExtractor { } +class CLIPImageProcessor extends CLIPFeatureExtractor { } // NOTE: extends CLIPFeatureExtractor +class ChineseCLIPFeatureExtractor extends ImageFeatureExtractor { } +class SiglipImageProcessor extends ImageFeatureExtractor { } +class ConvNextFeatureExtractor extends ImageFeatureExtractor { + constructor(config) { + super(config); + + /** + * Percentage of the image to crop. Only has an effect if this.size < 384. + */ + this.crop_pct = this.config.crop_pct ?? (224 / 256); + } + + async resize(image) { + const shortest_edge = this.size?.shortest_edge; + if (shortest_edge === undefined) { + throw new Error(`Size dictionary must contain 'shortest_edge' key.`); + } + + if (shortest_edge < 384) { + // maintain same ratio, resizing shortest edge to shortest_edge/crop_pct + const resize_shortest_edge = Math.floor(shortest_edge / this.crop_pct); + + const [newWidth, newHeight] = this.get_resize_output_image_size(image, { + shortest_edge: resize_shortest_edge, + }); + + image = await image.resize(newWidth, newHeight, { + resample: this.resample, + }); + + // then crop to (shortest_edge, shortest_edge) + image = await image.center_crop(shortest_edge, shortest_edge); + } else { + // warping (no cropping) when evaluated at 384 or larger + image = await image.resize(shortest_edge, shortest_edge, { + resample: this.resample, + }); + } + + return image; + } +} +class ConvNextImageProcessor extends ConvNextFeatureExtractor { } // NOTE extends ConvNextFeatureExtractor +class ViTFeatureExtractor extends ImageFeatureExtractor { } +class ViTImageProcessor extends ImageFeatureExtractor { } + +class EfficientNetImageProcessor extends ImageFeatureExtractor { + constructor(config) { + super(config); + this.include_top = this.config.include_top ?? true; + if (this.include_top) { + this.image_std = this.image_std.map(x => x * x); + } + } +} + +class MobileNetV1FeatureExtractor extends ImageFeatureExtractor { } +class MobileNetV2FeatureExtractor extends ImageFeatureExtractor { } +class MobileNetV3FeatureExtractor extends ImageFeatureExtractor { } +class MobileNetV4FeatureExtractor extends ImageFeatureExtractor { } + +class MobileViTFeatureExtractor extends ImageFeatureExtractor { } +class MobileViTImageProcessor extends MobileViTFeatureExtractor { } // NOTE extends MobileViTFeatureExtractor +class OwlViTFeatureExtractor extends ImageFeatureExtractor { + /** @type {typeof post_process_object_detection} */ + post_process_object_detection(...args) { + return post_process_object_detection(...args); + } +} +class Owlv2ImageProcessor extends OwlViTFeatureExtractor { } // NOTE extends OwlViTFeatureExtractor + +class RTDetrImageProcessor extends ImageFeatureExtractor { + /** @type {typeof post_process_object_detection} */ + post_process_object_detection(...args) { + return post_process_object_detection(...args); + } +} + +class DeiTFeatureExtractor extends ImageFeatureExtractor { } +class BeitFeatureExtractor extends ImageFeatureExtractor { } +class DonutFeatureExtractor extends ImageFeatureExtractor { + pad_image(pixelData, imgDims, padSize, options = {}) { + const [imageHeight, imageWidth, imageChannels] = imgDims; + + let image_mean = this.image_mean; + if (!Array.isArray(this.image_mean)) { + image_mean = new Array(imageChannels).fill(image_mean); + } + + let image_std = this.image_std; + if (!Array.isArray(image_std)) { + image_std = new Array(imageChannels).fill(image_mean); + } + + const constant_values = image_mean.map((x, i) => - x / image_std[i]); + + return super.pad_image(pixelData, imgDims, padSize, { + center: true, + + // Since normalization is done after padding, we need to use certain constant values to ensure the same behaviour is observed. + // For more information, see https://github.com/huggingface/transformers/blob/main/src/transformers/models/donut/image_processing_donut.py#L433-L451 + constant_values: constant_values, + ...options, + }); + } +} +class DonutImageProcessor extends DonutFeatureExtractor { } // NOTE extends DonutFeatureExtractor +class NougatImageProcessor extends DonutFeatureExtractor { } // NOTE extends DonutFeatureExtractor + +/** + * @typedef {object} DetrFeatureExtractorResultProps + * @property {Tensor} pixel_mask + * @typedef {ImageFeatureExtractorResult & DetrFeatureExtractorResultProps} DetrFeatureExtractorResult + */ + +/** + * Detr Feature Extractor. + * + * @extends ImageFeatureExtractor + */ +class DetrFeatureExtractor extends ImageFeatureExtractor { + /** + * Calls the feature extraction process on an array of images, preprocesses + * each image, and concatenates the resulting features into a single Tensor. + * @param {RawImage[]} images The image(s) to extract features from. + * @returns {Promise} An object containing the concatenated pixel values of the preprocessed images. + */ + async _call(images) { + const result = await super._call(images); + + // TODO support differently-sized images, for now assume all images are the same size. + // TODO support different mask sizes (not just 64x64) + // Currently, just fill pixel mask with 1s + const maskSize = [result.pixel_values.dims[0], 64, 64]; + const pixel_mask = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.full)(maskSize, 1n); + + return { ...result, pixel_mask }; + } + + /** @type {typeof post_process_object_detection} */ + post_process_object_detection(...args) { + return post_process_object_detection(...args); + } + + /** @type {typeof post_process_panoptic_segmentation} */ + post_process_panoptic_segmentation(...args) { + return post_process_panoptic_segmentation(...args); + } + + post_process_instance_segmentation() { + // TODO + throw Error("Not implemented yet"); + } +} + +class MaskFormerFeatureExtractor extends ImageFeatureExtractor { + + /** @type {typeof post_process_panoptic_segmentation} */ + post_process_panoptic_segmentation(...args) { + return post_process_panoptic_segmentation(...args); + } + + post_process_instance_segmentation() { + // TODO + throw Error("Not implemented yet"); + } +} + + +class YolosFeatureExtractor extends ImageFeatureExtractor { + /** @type {typeof post_process_object_detection} */ + post_process_object_detection(...args) { + return post_process_object_detection(...args); + } +} + +/** + * @typedef {object} SamImageProcessorResult + * @property {Tensor} pixel_values + * @property {HeightWidth[]} original_sizes + * @property {HeightWidth[]} reshaped_input_sizes + * @property {Tensor} [input_points] + * @property {Tensor} [input_labels] + * @property {Tensor} [input_boxes] + */ + +class SamImageProcessor extends ImageFeatureExtractor { + + /** + * + * @param {any} input_points + * @param {HeightWidth[]} original_sizes + * @param {HeightWidth[]} reshaped_input_sizes + * @returns {Tensor} + */ + reshape_input_points(input_points, original_sizes, reshaped_input_sizes, is_bounding_box = false) { + + // Make deep copy to avoid altering user's input + input_points = structuredClone(input_points); + let shape = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.calculateDimensions)(input_points); + + // TODO: add support for 2D input_points + if (shape.length === 3) { + // Correct user's input + if (!is_bounding_box) { + shape = [1, ...shape]; + } + input_points = [input_points]; + } else if (shape.length !== 4) { + throw Error("The input_points must be a 4D tensor of shape `batch_size`, `point_batch_size`, `nb_points_per_image`, `2`.") + } + + // Reshape input points + for (let i = 0; i < input_points.length; ++i) { // batch_size + let originalImageSize = original_sizes[i]; + let reshapedImageSize = reshaped_input_sizes[i]; + + let resizeFactors = [ + reshapedImageSize[0] / originalImageSize[0], + reshapedImageSize[1] / originalImageSize[1] + ] + + for (let j = 0; j < input_points[i].length; ++j) { // point_batch_size + for (let k = 0; k < input_points[i][j].length; ++k) { // nb_points_per_image + for (let w = 0; w < input_points[i][j][k].length; ++w) { // 2 or 4 + input_points[i][j][k][w] *= resizeFactors[w % 2]; + } + } + } + } + + return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( + 'float32', + Float32Array.from(input_points.flat(Infinity)), + shape + ) + + } + + /** + * + * @param {any} input_labels + * @param {Tensor} input_points + * @returns {Tensor} + */ + add_input_labels(input_labels, input_points) { + let shape = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.calculateDimensions)(input_labels); + if (shape.length === 2) { + // Correct user's input + shape = [1, ...shape]; + input_labels = [input_labels]; + } else if (shape.length !== 3) { + throw Error("The input_points must be a 4D tensor of shape `batch_size`, `point_batch_size`, `nb_points_per_image`, `2`.") + } + + if (shape.some((x, i) => x !== input_points.dims[i])) { + throw Error(`The first ${shape.length} dimensions of 'input_points' and 'input_labels' must be the same.`) + } + return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( + 'int64', + input_labels.flat(Infinity).map(BigInt), + shape, + ) + } + /** + * @param {any[]} images The URL(s) of the image(s) to extract features from. + * @param {Object} [options] Additional options for the processor. + * @param {any} [options.input_points=null] A 3D or 4D array, representing the input points provided by the user. + * - 3D: `[point_batch_size, nb_points_per_image, 2]`. In this case, `batch_size` is assumed to be 1. + * - 4D: `[batch_size, point_batch_size, nb_points_per_image, 2]`. + * @param {any} [options.input_labels=null] A 2D or 3D array, representing the input labels for the points, used by the prompt encoder to encode the prompt. + * - 2D: `[point_batch_size, nb_points_per_image]`. In this case, `batch_size` is assumed to be 1. + * - 3D: `[batch_size, point_batch_size, nb_points_per_image]`. + * @param {number[][][]} [options.input_boxes=null] A 3D array of shape `(batch_size, num_boxes, 4)`, representing the input boxes provided by the user. + * This is used by the prompt encoder to encode the prompt. Generally yields to much better generated masks. + * The processor will generate a tensor, with each dimension corresponding respectively to the image batch size, + * the number of boxes per image and the coordinates of the top left and botton right point of the box. + * In the order (`x1`, `y1`, `x2`, `y2`): + * - `x1`: the x coordinate of the top left point of the input box + * - `y1`: the y coordinate of the top left point of the input box + * - `x2`: the x coordinate of the bottom right point of the input box + * - `y2`: the y coordinate of the bottom right point of the input box + * @returns {Promise} + */ + async _call(images, { + input_points = null, + input_labels = null, + input_boxes = null + } = {}) { + // TODO allow user to use preprocessed images + /** @type {SamImageProcessorResult} */ + const processed = await super._call(images); + + if (input_points) { + processed.input_points = this.reshape_input_points( + input_points, processed.original_sizes, processed.reshaped_input_sizes + ); + } + + if (input_labels) { + if (!processed.input_points) { + throw Error("`input_points` must be provided if `input_labels` are provided.") + } + processed.input_labels = this.add_input_labels(input_labels, processed.input_points); + } + + if (input_boxes) { + processed.input_boxes = this.reshape_input_points( + input_boxes, processed.original_sizes, processed.reshaped_input_sizes, true, + ); + } + + return processed; + } + + /** + * Remove padding and upscale masks to the original image size. + * @param {Tensor} masks Batched masks from the mask_decoder in (batch_size, num_channels, height, width) format. + * @param {[number, number][]} original_sizes The original sizes of each image before it was resized to the model's expected input shape, in (height, width) format. + * @param {[number, number][]} reshaped_input_sizes The size of each image as it is fed to the model, in (height, width) format. Used to remove padding. + * @param {Object} options Optional parameters for post-processing. + * @param {number} [options.mask_threshold] The threshold to use for binarizing the masks. + * @param {boolean} [options.binarize] Whether to binarize the masks. + * @param {Object} [options.pad_size] The target size the images were padded to before being passed to the model. If `null`, the target size is assumed to be the processor's `pad_size`. + * @param {number} [options.pad_size.height] The height the images were padded to. + * @param {number} [options.pad_size.width] The width the images were padded to. + * @returns {Promise} Batched masks in batch_size, num_channels, height, width) format, where (height, width) is given by original_size. + */ + async post_process_masks(masks, original_sizes, reshaped_input_sizes, { + mask_threshold = 0.0, + binarize = true, + pad_size = null, + } = {}) { + // masks: [1, 1, 3, 256, 256] + + const output_masks = []; + + pad_size = pad_size ?? this.pad_size; + + /** @type {[number, number]} */ + const target_image_size = [pad_size.height, pad_size.width]; + + for (let i = 0; i < original_sizes.length; ++i) { + const original_size = original_sizes[i]; + const reshaped_input_size = reshaped_input_sizes[i]; + + // Upscale mask to padded size + let interpolated_mask = (await (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.interpolate_4d)( + masks[i], + { mode: 'bilinear', size: target_image_size } + )); + + // Crop mask + interpolated_mask = interpolated_mask.slice(null, null, [0, reshaped_input_size[0]], [0, reshaped_input_size[1]]); + + // Downscale mask + interpolated_mask = (await (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.interpolate_4d)( + interpolated_mask, + { mode: 'bilinear', size: original_size } + )); + + if (binarize) { + const data = interpolated_mask.data; + const binarizedMaskData = new Uint8Array(data.length); + for (let i = 0; i < data.length; ++i) { + if (data[i] > mask_threshold) { + binarizedMaskData[i] = 1; + } + } + interpolated_mask = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( + 'bool', + binarizedMaskData, + interpolated_mask.dims + ) + } + + output_masks.push(interpolated_mask); + } + + return output_masks; + } + + /** + * Generates a list of crop boxes of different sizes. Each layer has (2**i)**2 boxes for the ith layer. + * @param {RawImage} image Input original image + * @param {number} target_size Target size of the resized image + * @param {Object} options Options for generating crop boxes + * @param {number} [options.crop_n_layers] If >0, mask prediction will be run again on crops of the image. + * Sets the number of layers to run, where each layer has 2**i_layer number of image crops. + * @param {number} [options.overlap_ratio] Sets the degree to which crops overlap. In the first crop layer, + * crops will overlap by this fraction of the image length. Later layers with more crops scale down this overlap. + * @param {number} [options.points_per_crop] Number of points to sample from each crop. + * @param {number} [options.crop_n_points_downscale_factor] The number of points-per-side sampled in layer n is + * scaled down by crop_n_points_downscale_factor**n. + * @returns {Object} An object containing the crop boxes, number of points per crop, cropped images, and input labels. + */ + generate_crop_boxes(image, target_size, { + crop_n_layers = 0, + overlap_ratio = 512 / 1500, + points_per_crop = 32, + crop_n_points_downscale_factor = 1, + } = {}) { + // TODO: Implement + // return { crop_boxes, points_per_crop, cropped_images, input_labels } + } +} + +class Swin2SRImageProcessor extends ImageFeatureExtractor { + pad_image(pixelData, imgDims, padSize, options = {}) { + // NOTE: In this case, `padSize` represents the size of the sliding window for the local attention. + // In other words, the image is padded so that its width and height are multiples of `padSize`. + const [imageHeight, imageWidth, imageChannels] = imgDims; + + return super.pad_image(pixelData, imgDims, { + // NOTE: For Swin2SR models, the original python implementation adds padding even when the image's width/height is already + // a multiple of `pad_size`. However, this is most likely a bug (PR: https://github.com/mv-lab/swin2sr/pull/19). + // For this reason, we only add padding when the image's width/height is not a multiple of `pad_size`. + width: imageWidth + (padSize - imageWidth % padSize) % padSize, + height: imageHeight + (padSize - imageHeight % padSize) % padSize, + }, { + mode: 'symmetric', + center: false, + constant_values: -1, + ...options, + }) + } +} + +class VitMatteImageProcessor extends ImageFeatureExtractor { + /** + * Calls the feature extraction process on an array of images, preprocesses + * each image, and concatenates the resulting features into a single Tensor. + * @param {RawImage[]} images The image(s) to extract features from. + * @param {RawImage[]} trimaps The trimaps(s) to extract features from. + * @returns {Promise} An object containing the concatenated pixel values of the preprocessed images. + */ + async _call(images, trimaps) { + if (!Array.isArray(images)) { + images = [images]; + } + if (!Array.isArray(trimaps)) { + trimaps = [trimaps]; + } + + const imageData = await Promise.all(images.map(x => this.preprocess(x))); + const trimapData = await Promise.all(trimaps.map(x => this.preprocess(x, { + do_normalize: false, + do_convert_rgb: false, + do_convert_grayscale: true, + }))); + + + // Stack pixel values + const pixel_values = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.stack)(imageData.map( + // Concatenate images and trimaps + (x, i) => (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.cat)([x.pixel_values, trimapData[i].pixel_values], 0) + ), 0); + + return { + pixel_values, + + // Original sizes of images + original_sizes: imageData.map(x => x.original_size), + + // Reshaped sizes of images, before padding or cropping + reshaped_input_sizes: imageData.map(x => x.reshaped_input_size), + } + } +} + +class WhisperFeatureExtractor extends FeatureExtractor { + + constructor(config) { + super(config); + + // Prefer given `mel_filters` from preprocessor_config.json, or calculate them if they don't exist. + this.config.mel_filters ??= (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.mel_filter_bank)( + Math.floor(1 + this.config.n_fft / 2), // num_frequency_bins + this.config.feature_size, // num_mel_filters + 0.0, // min_frequency + 8000.0, // max_frequency + this.config.sampling_rate, // sampling_rate + "slaney", // norm + "slaney", // mel_scale + ); + + this.window = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.window_function)(this.config.n_fft, 'hann'); + } + + /** + * Computes the log-Mel spectrogram of the provided audio waveform. + * @param {Float32Array|Float64Array} waveform The audio waveform to process. + * @returns {Promise} An object containing the log-Mel spectrogram data as a Float32Array and its dimensions as an array of numbers. + */ + async _extract_fbank_features(waveform) { + const features = await (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.spectrogram)( + waveform, + this.window, // window + this.config.n_fft, // frame_length + this.config.hop_length, // hop_length + { + power: 2.0, + mel_filters: this.config.mel_filters, + log_mel: 'log10', + + // Custom + max_num_frames: this.config.nb_max_frames, // 3000 + } + ) + + const data = features.data; + const maxValue = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.max)(data)[0]; + + for (let i = 0; i < data.length; ++i) { + data[i] = (Math.max(data[i], maxValue - 8.0) + 4.0) / 4.0; + } + + return features; + } + + /** + * Asynchronously extracts features from a given audio using the provided configuration. + * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. + * @returns {Promise<{ input_features: Tensor }>} A Promise resolving to an object containing the extracted input features as a Tensor. + */ + async _call(audio) { + validate_audio_inputs(audio, 'WhisperFeatureExtractor'); + + let waveform; + if (audio.length > this.config.n_samples) { + console.warn( + "Attempting to extract features for audio longer than 30 seconds. " + + "If using a pipeline to extract transcript from a long audio clip, " + + "remember to specify `chunk_length_s` and/or `stride_length_s`." + ); + waveform = audio.slice(0, this.config.n_samples); + } else { + // pad with zeros + waveform = new Float32Array(this.config.n_samples); + waveform.set(audio); + } + + const features = await this._extract_fbank_features(waveform); + + return { + input_features: features.unsqueeze_(0) + }; + } +} + +class Wav2Vec2FeatureExtractor extends FeatureExtractor { + + /** + * @param {Float32Array} input_values + * @returns {Float32Array} + */ + _zero_mean_unit_var_norm(input_values) { + // TODO support batch? + const sum = input_values.reduce((a, b) => a + b, 0); + const mean = sum / input_values.length; + const variance = input_values.reduce((a, b) => a + (b - mean) ** 2, 0) / input_values.length; + return input_values.map(x => (x - mean) / Math.sqrt(variance + 1e-7)); + } + + /** + * Asynchronously extracts features from a given audio using the provided configuration. + * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. + * @returns {Promise<{ input_values: Tensor; attention_mask: Tensor }>} A Promise resolving to an object containing the extracted input features and attention mask as Tensors. + */ + async _call(audio) { + validate_audio_inputs(audio, 'Wav2Vec2FeatureExtractor'); + + if (audio instanceof Float64Array) { + audio = new Float32Array(audio); + } + + let input_values = audio; + + // zero-mean and unit-variance normalization + if (this.config.do_normalize) { + input_values = this._zero_mean_unit_var_norm(input_values); + } + + // TODO: allow user to pass in attention mask + const shape = [1, input_values.length]; + return { + input_values: new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor('float32', input_values, shape), + attention_mask: new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor('int64', new BigInt64Array(input_values.length).fill(1n), shape) + }; + } +} + +class SeamlessM4TFeatureExtractor extends FeatureExtractor { + + constructor(config) { + super(config); + + const sampling_rate = this.config.sampling_rate; + const mel_filters = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.mel_filter_bank)( + 256, // num_frequency_bins + this.config.num_mel_bins, // num_mel_filters + 20, // min_frequency + Math.floor(sampling_rate / 2), // max_frequency + sampling_rate, // sampling_rate + null, // norm + "kaldi", // mel_scale + true, // triangularize_in_mel_space + ); + + // Do padding: + for (let i = 0; i < mel_filters.length; ++i) { + mel_filters[i].push(0); + } + this.mel_filters = mel_filters; + + this.window = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.window_function)(400, 'povey', { + periodic: false, + }) + } + + /** + * Computes the log-Mel spectrogram of the provided audio waveform. + * @param {Float32Array|Float64Array} waveform The audio waveform to process. + * @param {number} max_length The maximum number of frames to return. + * @returns {Promise} An object containing the log-Mel spectrogram data as a Float32Array and its dimensions as an array of numbers. + */ + async _extract_fbank_features(waveform, max_length) { + // NOTE: We don't pad/truncate since that is passed in as `max_num_frames` + + // Kaldi compliance: 16-bit signed integers + // 32768 == 2 ** 15 + waveform = waveform.map((/** @type {number} */ x) => x * 32768) + + return (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.spectrogram)( + waveform, + this.window, // window + 400, // frame_length + 160, // hop_length + { + fft_length: 512, + power: 2.0, + center: false, + preemphasis: 0.97, + mel_filters: this.mel_filters, + log_mel: 'log', + mel_floor: 1.192092955078125e-07, + remove_dc_offset: true, + + // Custom + max_num_frames: max_length, + transpose: true, + } + ) + } + + /** + * Asynchronously extracts features from a given audio using the provided configuration. + * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. + * @param {Object} options Optional parameters for feature extraction. + * @param {boolean} [options.padding=true] Whether to pad the sequence to a multiple of `pad_to_multiple_of`. + * @param {number} [options.pad_to_multiple_of=2] The number to pad the sequence to a multiple of. + * @param {boolean} [options.do_normalize_per_mel_bins=true] Whether or not to zero-mean unit-variance normalize the input per mel-channel. + * @param {boolean} [options.return_attention_mask=true] Whether to return the attention mask. + * @returns {Promise<{ input_features: Tensor, attention_mask?: Tensor }>} A Promise resolving to an object containing the extracted input features and attention masks as Tensors. + */ + async _call(audio, { + padding = true, + pad_to_multiple_of = 2, + do_normalize_per_mel_bins = true, + return_attention_mask = true, + } = {}) { + validate_audio_inputs(audio, 'SeamlessM4TFeatureExtractor'); + + let features = await this._extract_fbank_features(audio, this.config.max_length); + + if (do_normalize_per_mel_bins) { + const [num_features, feature_size] = features.dims; + const data = features.data; + for (let i = 0; i < feature_size; ++i) { + let sum = 0; + for (let j = 0; j < num_features; ++j) { + sum += data[j * feature_size + i]; + } + + const mean = sum / num_features; + + let variance = 0; + for (let j = 0; j < num_features; ++j) { + variance += (data[j * feature_size + i] - mean) ** 2; + } + variance /= num_features - 1; // NOTE: We use ddof=1 + + const std = Math.sqrt(variance + 1e-7); + for (let j = 0; j < num_features; ++j) { + const index = j * feature_size + i; + data[index] = (data[index] - mean) / std; + } + } + } + + let padded_attention_mask; + if (padding) { + const [num_frames, num_channels] = features.dims; + const data = /** @type {Float32Array} */(features.data); + + const pad_size = num_frames % pad_to_multiple_of; + if (pad_size > 0) { + const padded_data = new Float32Array(num_channels * (num_frames + pad_size)); + padded_data.set(data) + padded_data.fill(this.config.padding_value, data.length) + + const numPaddedFrames = num_frames + pad_size; + features = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( + features.type, + padded_data, + [numPaddedFrames, num_channels], + ) + + if (return_attention_mask) { + padded_attention_mask = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( + 'int64', + new BigInt64Array(numPaddedFrames), + [1, numPaddedFrames], + ) + padded_attention_mask.data.fill(1n, 0, num_frames); + } + } + } + + const [num_frames, num_channels] = features.dims; + + const stride = this.config.stride; + const remainder = num_frames % stride; + if (remainder !== 0) { + throw new Error(`The number of frames (${num_frames}) must be a multiple of the stride (${stride}).`) + } + + const input_features = features.view( + 1, + Math.floor(num_frames / stride), + num_channels * stride, + ); + + const result = { input_features } + + if (return_attention_mask) { + const reshapedNumFrames = input_features.dims[1]; + + const attention_mask_data = new BigInt64Array(reshapedNumFrames); + + if (padded_attention_mask) { + const padded_attention_mask_data = padded_attention_mask.data; + for (let i = 1, j = 0; i < num_frames; i += stride, ++j) { + attention_mask_data[j] = padded_attention_mask_data[i]; + } + } else { + attention_mask_data.fill(1n); + } + result.attention_mask = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( + 'int64', + attention_mask_data, + [1, reshapedNumFrames], + ); + } + + return result; + } +} + +class ASTFeatureExtractor extends FeatureExtractor { + + + constructor(config) { + super(config); + + const sampling_rate = this.config.sampling_rate; + const mel_filters = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.mel_filter_bank)( + 256, // num_frequency_bins + this.config.num_mel_bins, // num_mel_filters + 20, // min_frequency + Math.floor(sampling_rate / 2), // max_frequency + sampling_rate, // sampling_rate + null, // norm + "kaldi", // mel_scale + true, // triangularize_in_mel_space + ); + + // Do padding: + for (let i = 0; i < mel_filters.length; ++i) { + mel_filters[i].push(0); + } + this.mel_filters = mel_filters; + + this.window = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.window_function)(400, 'hann', { + periodic: false, + }) + + this.mean = this.config.mean; + this.std = this.config.std; + } + + /** + * Computes the log-Mel spectrogram of the provided audio waveform. + * @param {Float32Array|Float64Array} waveform The audio waveform to process. + * @param {number} max_length The maximum number of frames to return. + * @returns {Promise} An object containing the log-Mel spectrogram data as a Float32Array and its dimensions as an array of numbers. + */ + async _extract_fbank_features(waveform, max_length) { + // NOTE: We don't pad/truncate since that is passed in as `max_num_frames` + return (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.spectrogram)( + waveform, + this.window, // window + 400, // frame_length + 160, // hop_length + { + fft_length: 512, + power: 2.0, + center: false, + preemphasis: 0.97, + mel_filters: this.mel_filters, + log_mel: 'log', + mel_floor: 1.192092955078125e-07, + remove_dc_offset: true, + + // Custom + max_num_frames: max_length, + transpose: true, + } + ) + } + + + /** + * Asynchronously extracts features from a given audio using the provided configuration. + * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. + * @returns {Promise<{ input_values: Tensor }>} A Promise resolving to an object containing the extracted input features as a Tensor. + */ + async _call(audio) { + validate_audio_inputs(audio, 'ASTFeatureExtractor'); + + const features = await this._extract_fbank_features(audio, this.config.max_length); + if (this.config.do_normalize) { + // Normalize the input audio spectrogram to have mean=0, std=0.5 + const denom = this.std * 2; + const features_data = features.data; + for (let i = 0; i < features_data.length; ++i) { + features_data[i] = (features_data[i] - this.mean) / denom; + } + } + + return { + input_values: features.unsqueeze_(0) + }; + } +} + +class ClapFeatureExtractor extends FeatureExtractor { + + constructor(config) { + super(config); + + this.mel_filters = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.mel_filter_bank)( + this.config.nb_frequency_bins, // num_frequency_bins + this.config.feature_size, // num_mel_filters + this.config.frequency_min, // min_frequency + this.config.frequency_max, // max_frequency + this.config.sampling_rate, // sampling_rate + null, // norm + "htk", // mel_scale + ); + + this.mel_filters_slaney = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.mel_filter_bank)( + this.config.nb_frequency_bins, // num_frequency_bins + this.config.feature_size, // num_mel_filters + this.config.frequency_min, // min_frequency + this.config.frequency_max, // max_frequency + this.config.sampling_rate, // sampling_rate + "slaney", // norm + "slaney", // mel_scale + ); + + this.window = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.window_function)(this.config.fft_window_size, 'hann') + + } + + + /** + * Extracts the mel spectrogram and prepares it for the mode based on the `truncation` and `padding` arguments. + * + * Four different path are possible: + * - `truncation="fusion"` and the length of the waveform is greater than the max length: the mel spectrogram + * will be computed on the entire audio. 3 random crops and a dowsampled version of the full mel spectrogram + * are then stacked together. They will later be used for `feature_fusion`. + * - `truncation="rand_trunc"` and the length of the waveform is smaller than the max length: the audio is + * padded based on `padding`. + * - `truncation="fusion"` and the length of the waveform is smaller than the max length: the audio is padded + * based on `padding`, and is repeated `4` times. + * - `truncation="rand_trunc"` and the length of the waveform is greater than the max length: the mel + * spectrogram will be computed on a random crop of the waveform. + * + * @param {Float32Array|Float64Array} waveform The input waveform. + * @param {number} max_length The maximum length of the waveform. + * @param {string} truncation The truncation strategy to use. + * @param {string} padding The padding strategy to use. + * @returns {Promise} An object containing the mel spectrogram data as a Float32Array, its dimensions as an array of numbers, and a boolean indicating whether the waveform was longer than the max length. + * @private + */ + async _get_input_mel(waveform, max_length, truncation, padding) { + + /** @type {Tensor} */ + let input_mel; + let longer = false; + const diff = waveform.length - max_length; + if (diff > 0) { + if (truncation === 'rand_trunc') { + longer = true; + const idx = Math.floor(Math.random() * (diff + 1)); + waveform = waveform.subarray(idx, idx + max_length); + + input_mel = await this._extract_fbank_features(waveform, this.mel_filters_slaney, this.config.nb_max_samples); + } else { + // TODO implement fusion strategy + throw new Error(`Truncation strategy "${truncation}" not implemented`) + } + } else { + if (diff < 0) { + let padded = new Float64Array(max_length); // already padded with zeros + padded.set(waveform); + + if (padding === 'repeat') { + for (let i = waveform.length; i < max_length; i += waveform.length) { + padded.set(waveform.subarray(0, Math.min(waveform.length, max_length - i)), i); + } + } else if (padding === 'repeatpad') { + for (let i = waveform.length; i < -diff; i += waveform.length) { + padded.set(waveform, i); + } + } + waveform = padded; + } + + if (truncation === 'fusion') { + throw new Error(`Truncation strategy "${truncation}" not implemented`) + } + + input_mel = await this._extract_fbank_features(waveform, this.mel_filters_slaney, this.config.nb_max_samples); + } + + return input_mel.unsqueeze_(0); + } + + /** + * Compute the log-mel spectrogram of the provided `waveform` using the Hann window. + * In CLAP, two different filter banks are used depending on the truncation pattern: + * - `self.mel_filters`: they correspond to the default parameters of `torchaudio` which can be obtained from + * calling `torchaudio.transforms.MelSpectrogram().mel_scale.fb`. These filters are used when `truncation` + * is set to `"fusion"`. + * - `self.mel_filteres_slaney` : they correspond to the default parameters of `librosa` which used + * `librosa.filters.mel` when computing the mel spectrogram. These filters were only used in the original + * implementation when the truncation mode is not `"fusion"`. + * + * @param {Float32Array|Float64Array} waveform The audio waveform to process. + * @param {number[][]} mel_filters The mel filters to use. + * @param {number} [max_length=null] The maximum number of frames to return. + * @returns {Promise} An object containing the log-Mel spectrogram data as a Float32Array and its dimensions as an array of numbers. + */ + async _extract_fbank_features(waveform, mel_filters, max_length = null) { + // NOTE: We don't pad/truncate since that is passed in as `max_num_frames` + return (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.spectrogram)( + waveform, + this.window, // window + this.config.fft_window_size, // frame_length + this.config.hop_length, // hop_length + { + power: 2.0, + mel_filters, + log_mel: 'dB', + + // Custom + max_num_frames: max_length, + do_pad: false, + transpose: true, + } + ) + } + + + /** + * Asynchronously extracts features from a given audio using the provided configuration. + * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. + * @returns {Promise<{ input_features: Tensor }>} A Promise resolving to an object containing the extracted input features as a Tensor. + */ + async _call(audio, { + max_length = null, + } = {}) { + validate_audio_inputs(audio, 'ClapFeatureExtractor'); + + // convert to mel spectrogram, truncate and pad if needed. + const padded_inputs = await this._get_input_mel( + audio, + max_length ?? this.config.nb_max_samples, + this.config.truncation, + this.config.padding, + ); + + return { + input_features: padded_inputs.unsqueeze_(0), + } + } +} + + +class PyAnnoteFeatureExtractor extends FeatureExtractor { + /** + * Asynchronously extracts features from a given audio using the provided configuration. + * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. + * @returns {Promise<{ input_values: Tensor; }>} The extracted input features. + */ + async _call(audio) { + validate_audio_inputs(audio, 'PyAnnoteFeatureExtractor'); + + if (audio instanceof Float64Array) { + audio = new Float32Array(audio); + } + + const shape = [ + 1, /* batch_size */ + 1, /* num_channels */ + audio.length, /* num_samples */ + ]; + return { + input_values: new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor('float32', audio, shape), + }; + } + + /** + * NOTE: Can return fractional values. `Math.ceil` will ensure correct value. + * @param {number} samples The number of frames in the audio. + * @returns {number} The number of frames in the audio. + */ + samples_to_frames(samples) { + return ((samples - this.config.offset) / this.config.step); + } + + /** + * Post-processes the speaker diarization logits output by the model. + * @param {Tensor} logits The speaker diarization logits output by the model. + * @param {number} num_samples Number of samples in the input audio. + * @returns {Array>} The post-processed speaker diarization results. + */ + post_process_speaker_diarization(logits, num_samples) { + const ratio = ( + num_samples / this.samples_to_frames(num_samples) + ) / this.config.sampling_rate; + + const results = []; + for (const scores of logits.tolist()) { + const accumulated_segments = []; + + let current_speaker = -1; + for (let i = 0; i < scores.length; ++i) { + const probabilities = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.softmax)(scores[i]); + const [score, id] = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.max)(probabilities); + const [start, end] = [i, i + 1]; + + if (id !== current_speaker) { + // Speaker has changed + current_speaker = id; + accumulated_segments.push({ id, start, end, score }); + } else { + // Continue the current segment + accumulated_segments.at(-1).end = end; + accumulated_segments.at(-1).score += score; + } + } + + results.push(accumulated_segments.map( + // Convert frame-space to time-space + // and compute the confidence + ({ id, start, end, score }) => ({ + id, + start: start * ratio, + end: end * ratio, + confidence: score / (end - start), + }) + )); + } + return results; + } + +} + +class WeSpeakerFeatureExtractor extends FeatureExtractor { + + constructor(config) { + super(config); + + const sampling_rate = this.config.sampling_rate; + const mel_filters = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.mel_filter_bank)( + 256, // num_frequency_bins + this.config.num_mel_bins, // num_mel_filters + 20, // min_frequency + Math.floor(sampling_rate / 2), // max_frequency + sampling_rate, // sampling_rate + null, // norm + "kaldi", // mel_scale + true, // triangularize_in_mel_space + ); + + // Do padding: + for (let i = 0; i < mel_filters.length; ++i) { + mel_filters[i].push(0); + } + this.mel_filters = mel_filters; + + this.window = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.window_function)(400, 'hamming', { + periodic: false, + }) + this.min_num_frames = this.config.min_num_frames; + } + + /** + * Computes the log-Mel spectrogram of the provided audio waveform. + * @param {Float32Array|Float64Array} waveform The audio waveform to process. + * @returns {Promise} An object containing the log-Mel spectrogram data as a Float32Array and its dimensions as an array of numbers. + */ + async _extract_fbank_features(waveform) { + // Kaldi compliance: 16-bit signed integers + // 32768 == 2 ** 15 + waveform = waveform.map((/** @type {number} */ x) => x * 32768) + + return (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.spectrogram)( + waveform, + this.window, // window + 400, // frame_length + 160, // hop_length + { + fft_length: 512, + power: 2.0, + center: false, + preemphasis: 0.97, + mel_filters: this.mel_filters, + log_mel: 'log', + mel_floor: 1.192092955078125e-07, + remove_dc_offset: true, + + // Custom + transpose: true, + min_num_frames: this.min_num_frames, + } + ) + } + + + /** + * Asynchronously extracts features from a given audio using the provided configuration. + * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. + * @returns {Promise<{ input_features: Tensor }>} A Promise resolving to an object containing the extracted input features as a Tensor. + */ + async _call(audio) { + validate_audio_inputs(audio, 'WeSpeakerFeatureExtractor'); + + const features = (await this._extract_fbank_features(audio)).unsqueeze_(0); + + if (this.config.fbank_centering_span === null) { + // center features with global average + const meanData = /** @type {Float32Array} */ (features.mean(1).data); + const featuresData = /** @type {Float32Array} */(features.data); + const [batch_size, num_frames, feature_size] = features.dims; + + for (let i = 0; i < batch_size; ++i) { + const offset1 = i * num_frames * feature_size; + const offset2 = i * feature_size; + for (let j = 0; j < num_frames; ++j) { + const offset3 = offset1 + j * feature_size; + for (let k = 0; k < feature_size; ++k) { + featuresData[offset3 + k] -= meanData[offset2 + k]; + } + } + } + } + + return { + input_features: features + }; + } +} + +class SpeechT5FeatureExtractor extends FeatureExtractor { } + +/** + * Represents a Processor that extracts features from an input. + * @extends Callable + */ +class Processor extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * Creates a new Processor with the given feature extractor. + * @param {FeatureExtractor} feature_extractor The function used to extract features from the input. + */ + constructor(feature_extractor) { + super(); + this.feature_extractor = feature_extractor; + // TODO use tokenizer here? + } + + /** + * Calls the feature_extractor function with the given input. + * @param {any} input The input to extract features from. + * @param {...any} args Additional arguments. + * @returns {Promise} A Promise that resolves with the extracted features. + */ + async _call(input, ...args) { + return await this.feature_extractor(input, ...args); + } +} + +class SamProcessor extends Processor { + /** + * @borrows SamImageProcessor#_call as _call + */ + async _call(...args) { + return await this.feature_extractor(...args); + } + + /** + * @borrows SamImageProcessor#post_process_masks as post_process_masks + */ + post_process_masks(...args) { + // @ts-ignore + return this.feature_extractor.post_process_masks(...args); + } + /** + * @borrows SamImageProcessor#reshape_input_points as reshape_input_points + */ + reshape_input_points(...args) { + // @ts-ignore + return this.feature_extractor.reshape_input_points(...args); + } +} + +/** + * Represents a WhisperProcessor that extracts features from an audio input. + * @extends Processor + */ +class WhisperProcessor extends Processor { + /** + * Calls the feature_extractor function with the given audio input. + * @param {any} audio The audio input to extract features from. + * @returns {Promise} A Promise that resolves with the extracted features. + */ + async _call(audio) { + return await this.feature_extractor(audio) + } +} + + +class Wav2Vec2ProcessorWithLM extends Processor { + /** + * Calls the feature_extractor function with the given audio input. + * @param {any} audio The audio input to extract features from. + * @returns {Promise} A Promise that resolves with the extracted features. + */ + async _call(audio) { + return await this.feature_extractor(audio) + } +} + +class PyAnnoteProcessor extends Processor { + /** + * Calls the feature_extractor function with the given audio input. + * @param {any} audio The audio input to extract features from. + * @returns {Promise} A Promise that resolves with the extracted features. + */ + async _call(audio) { + return await this.feature_extractor(audio) + } + + post_process_speaker_diarization(...args) { + // @ts-ignore + return this.feature_extractor.post_process_speaker_diarization(...args); + } + +} + +class SpeechT5Processor extends Processor { + /** + * Calls the feature_extractor function with the given input. + * @param {any} input The input to extract features from. + * @returns {Promise} A Promise that resolves with the extracted features. + */ + async _call(input) { + return await this.feature_extractor(input) + } +} + +class OwlViTProcessor extends Processor { } + +class Florence2Processor extends Processor { + constructor(feature_extractor) { + super(feature_extractor); + + const { + tasks_answer_post_processing_type, + task_prompts_without_inputs, + task_prompts_with_input, + } = feature_extractor.config; + + /** @type {Map} */ + this.tasks_answer_post_processing_type = new Map(Object.entries(tasks_answer_post_processing_type ?? {})); + + /** @type {Map} */ + this.task_prompts_without_inputs = new Map(Object.entries(task_prompts_without_inputs ?? {})); + + /** @type {Map} */ + this.task_prompts_with_input = new Map(Object.entries(task_prompts_with_input ?? {})); + + this.regexes = { + quad_boxes: /(.+?)/gm, + bboxes: /([^<]+)?/gm, + } + this.size_per_bin = 1000; + } + + /** + * Helper function to construct prompts from input texts + * @param {string|string[]} text + * @returns {string[]} + */ + construct_prompts(text) { + if (typeof text === 'string') { + text = [text]; + } + + const prompts = []; + for (const t of text) { + // 1. fixed task prompts without additional inputs + if (this.task_prompts_without_inputs.has(t)) { + prompts.push(this.task_prompts_without_inputs.get(t)); + } + // 2. task prompts with additional inputs + else { + for (const [task, prompt] of this.task_prompts_with_input) { + if (t.includes(task)) { + prompts.push(prompt.replaceAll('{input}', t).replaceAll(task, '')); + break; + } + } + + // 3. default prompt + if (prompts.length !== text.length) { + prompts.push(t); + } + } + } + return prompts; + } + + /** + * Post-process the output of the model to each of the task outputs. + * @param {string} text The text to post-process. + * @param {string} task The task to post-process the text for. + * @param {[number, number]} image_size The size of the image. height x width. + */ + post_process_generation(text, task, image_size) { + const task_answer_post_processing_type = this.tasks_answer_post_processing_type.get(task) ?? 'pure_text'; + + // remove the special tokens + text = text.replaceAll('', '').replaceAll('', ''); + + let final_answer; + switch (task_answer_post_processing_type) { + case 'pure_text': + final_answer = text; + break; + + case 'description_with_bboxes': + case 'bboxes': + case 'phrase_grounding': + case 'ocr': + const key = task_answer_post_processing_type === 'ocr' ? 'quad_boxes' : 'bboxes'; + const matches = text.matchAll(this.regexes[key]); + const labels = []; + const items = []; + for (const [_, label, ...locations] of matches) { + // Push new label, or duplicate the last label + labels.push(label ? label.trim() : labels.at(-1) ?? ''); + items.push(locations.map((x, i) => + // NOTE: Add 0.5 to use the center position of the bin as the coordinate. + (Number(x) + 0.5) / this.size_per_bin * image_size[i % 2]) + ); + } + final_answer = { labels, [key]: items }; + break; + + default: + throw new Error(`Task "${task}" (of type "${task_answer_post_processing_type}") not yet implemented.`); + } + + return { [task]: final_answer } + } +} + +////////////////////////////////////////////////// +/** + * Helper class which is used to instantiate pretrained processors with the `from_pretrained` function. + * The chosen processor class is determined by the type specified in the processor config. + * + * **Example:** Load a processor using `from_pretrained`. + * ```javascript + * let processor = await AutoProcessor.from_pretrained('openai/whisper-tiny.en'); + * ``` + * + * **Example:** Run an image through a processor. + * ```javascript + * let processor = await AutoProcessor.from_pretrained('Xenova/clip-vit-base-patch16'); + * let image = await RawImage.read('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/football-match.jpg'); + * let image_inputs = await processor(image); + * // { + * // "pixel_values": { + * // "dims": [ 1, 3, 224, 224 ], + * // "type": "float32", + * // "data": Float32Array [ -1.558687686920166, -1.558687686920166, -1.5440893173217773, ... ], + * // "size": 150528 + * // }, + * // "original_sizes": [ + * // [ 533, 800 ] + * // ], + * // "reshaped_input_sizes": [ + * // [ 224, 224 ] + * // ] + * // } + * ``` + */ +class AutoProcessor { + static FEATURE_EXTRACTOR_CLASS_MAPPING = { + ImageFeatureExtractor, + WhisperFeatureExtractor, + ViTFeatureExtractor, + MobileViTFeatureExtractor, + MobileViTImageProcessor, + MobileNetV1FeatureExtractor, + MobileNetV2FeatureExtractor, + MobileNetV3FeatureExtractor, + MobileNetV4FeatureExtractor, + OwlViTFeatureExtractor, + Owlv2ImageProcessor, + CLIPFeatureExtractor, + CLIPImageProcessor, + Florence2Processor, + ChineseCLIPFeatureExtractor, + SiglipImageProcessor, + ConvNextFeatureExtractor, + ConvNextImageProcessor, + SegformerFeatureExtractor, + SapiensFeatureExtractor, + BitImageProcessor, + DPTImageProcessor, + DPTFeatureExtractor, + PvtImageProcessor, + GLPNFeatureExtractor, + BeitFeatureExtractor, + DeiTFeatureExtractor, + DetrFeatureExtractor, + RTDetrImageProcessor, + MaskFormerFeatureExtractor, + YolosFeatureExtractor, + DonutFeatureExtractor, + DonutImageProcessor, + NougatImageProcessor, + EfficientNetImageProcessor, + + ViTImageProcessor, + VitMatteImageProcessor, + SamImageProcessor, + Swin2SRImageProcessor, + Wav2Vec2FeatureExtractor, + SeamlessM4TFeatureExtractor, + SpeechT5FeatureExtractor, + ASTFeatureExtractor, + ClapFeatureExtractor, + PyAnnoteFeatureExtractor, + WeSpeakerFeatureExtractor, + } + + static PROCESSOR_CLASS_MAPPING = { + WhisperProcessor, + Wav2Vec2ProcessorWithLM, + PyAnnoteProcessor, + SamProcessor, + SpeechT5Processor, + OwlViTProcessor, + Florence2Processor, + } + + /** + * Instantiate one of the processor classes of the library from a pretrained model. + * + * The processor class to instantiate is selected based on the `feature_extractor_type` property of the config object + * (either passed as an argument or loaded from `pretrained_model_name_or_path` if possible) + * + * @param {string} pretrained_model_name_or_path The name or path of the pretrained model. Can be either: + * - A string, the *model id* of a pretrained processor hosted inside a model repo on huggingface.co. + * Valid model ids can be located at the root-level, like `bert-base-uncased`, or namespaced under a + * user or organization name, like `dbmdz/bert-base-german-cased`. + * - A path to a *directory* containing processor files, e.g., `./my_model_directory/`. + * @param {import('./utils/hub.js').PretrainedOptions} options Additional options for loading the processor. + * + * @returns {Promise} A new instance of the Processor class. + */ + static async from_pretrained(pretrained_model_name_or_path, { + progress_callback = null, + config = null, + cache_dir = null, + local_files_only = false, + revision = 'main', + } = {}) { + + let preprocessorConfig = config ?? await (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_2__.getModelJSON)(pretrained_model_name_or_path, 'preprocessor_config.json', true, { + progress_callback, + config, + cache_dir, + local_files_only, + revision, + }) + + // Determine feature extractor class + // TODO: Ensure backwards compatibility with old configs + let key = preprocessorConfig.feature_extractor_type ?? preprocessorConfig.image_processor_type; + let feature_extractor_class = this.FEATURE_EXTRACTOR_CLASS_MAPPING[key]; + + if (!feature_extractor_class) { + if (preprocessorConfig.size !== undefined) { + // Assume ImageFeatureExtractor + console.warn(`Feature extractor type "${key}" not found, assuming ImageFeatureExtractor due to size parameter in config.`); + feature_extractor_class = ImageFeatureExtractor; + } else { + throw new Error(`Unknown Feature Extractor type: ${key}`); + } + } + + // If no associated processor class, use default + let processor_class = this.PROCESSOR_CLASS_MAPPING[preprocessorConfig.processor_class] ?? Processor; + + // Instantiate processor and feature extractor + let feature_extractor = new feature_extractor_class(preprocessorConfig); + return new processor_class(feature_extractor); + } +} +////////////////////////////////////////////////// + + + +/***/ }), + +/***/ "./src/tokenizers.js": +/*!***************************!*\ + !*** ./src/tokenizers.js ***! + \***************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ AlbertTokenizer: () => (/* binding */ AlbertTokenizer), +/* harmony export */ AutoTokenizer: () => (/* binding */ AutoTokenizer), +/* harmony export */ BartTokenizer: () => (/* binding */ BartTokenizer), +/* harmony export */ BertTokenizer: () => (/* binding */ BertTokenizer), +/* harmony export */ BlenderbotSmallTokenizer: () => (/* binding */ BlenderbotSmallTokenizer), +/* harmony export */ BlenderbotTokenizer: () => (/* binding */ BlenderbotTokenizer), +/* harmony export */ BloomTokenizer: () => (/* binding */ BloomTokenizer), +/* harmony export */ CLIPTokenizer: () => (/* binding */ CLIPTokenizer), +/* harmony export */ CamembertTokenizer: () => (/* binding */ CamembertTokenizer), +/* harmony export */ CodeGenTokenizer: () => (/* binding */ CodeGenTokenizer), +/* harmony export */ CodeLlamaTokenizer: () => (/* binding */ CodeLlamaTokenizer), +/* harmony export */ CohereTokenizer: () => (/* binding */ CohereTokenizer), +/* harmony export */ ConvBertTokenizer: () => (/* binding */ ConvBertTokenizer), +/* harmony export */ DebertaTokenizer: () => (/* binding */ DebertaTokenizer), +/* harmony export */ DebertaV2Tokenizer: () => (/* binding */ DebertaV2Tokenizer), +/* harmony export */ DistilBertTokenizer: () => (/* binding */ DistilBertTokenizer), +/* harmony export */ ElectraTokenizer: () => (/* binding */ ElectraTokenizer), +/* harmony export */ EsmTokenizer: () => (/* binding */ EsmTokenizer), +/* harmony export */ FalconTokenizer: () => (/* binding */ FalconTokenizer), +/* harmony export */ GPT2Tokenizer: () => (/* binding */ GPT2Tokenizer), +/* harmony export */ GPTNeoXTokenizer: () => (/* binding */ GPTNeoXTokenizer), +/* harmony export */ GemmaTokenizer: () => (/* binding */ GemmaTokenizer), +/* harmony export */ Grok1Tokenizer: () => (/* binding */ Grok1Tokenizer), +/* harmony export */ HerbertTokenizer: () => (/* binding */ HerbertTokenizer), +/* harmony export */ LlamaTokenizer: () => (/* binding */ LlamaTokenizer), +/* harmony export */ M2M100Tokenizer: () => (/* binding */ M2M100Tokenizer), +/* harmony export */ MBart50Tokenizer: () => (/* binding */ MBart50Tokenizer), +/* harmony export */ MBartTokenizer: () => (/* binding */ MBartTokenizer), +/* harmony export */ MPNetTokenizer: () => (/* binding */ MPNetTokenizer), +/* harmony export */ MarianTokenizer: () => (/* binding */ MarianTokenizer), +/* harmony export */ MobileBertTokenizer: () => (/* binding */ MobileBertTokenizer), +/* harmony export */ NllbTokenizer: () => (/* binding */ NllbTokenizer), +/* harmony export */ NougatTokenizer: () => (/* binding */ NougatTokenizer), +/* harmony export */ PreTrainedTokenizer: () => (/* binding */ PreTrainedTokenizer), +/* harmony export */ Qwen2Tokenizer: () => (/* binding */ Qwen2Tokenizer), +/* harmony export */ RoFormerTokenizer: () => (/* binding */ RoFormerTokenizer), +/* harmony export */ RobertaTokenizer: () => (/* binding */ RobertaTokenizer), +/* harmony export */ SiglipTokenizer: () => (/* binding */ SiglipTokenizer), +/* harmony export */ SpeechT5Tokenizer: () => (/* binding */ SpeechT5Tokenizer), +/* harmony export */ SqueezeBertTokenizer: () => (/* binding */ SqueezeBertTokenizer), +/* harmony export */ T5Tokenizer: () => (/* binding */ T5Tokenizer), +/* harmony export */ TokenizerModel: () => (/* binding */ TokenizerModel), +/* harmony export */ VitsTokenizer: () => (/* binding */ VitsTokenizer), +/* harmony export */ Wav2Vec2CTCTokenizer: () => (/* binding */ Wav2Vec2CTCTokenizer), +/* harmony export */ WhisperTokenizer: () => (/* binding */ WhisperTokenizer), +/* harmony export */ XLMRobertaTokenizer: () => (/* binding */ XLMRobertaTokenizer), +/* harmony export */ XLMTokenizer: () => (/* binding */ XLMTokenizer), +/* harmony export */ is_chinese_char: () => (/* binding */ is_chinese_char) +/* harmony export */ }); +/* harmony import */ var _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./utils/generic.js */ "./src/utils/generic.js"); +/* harmony import */ var _utils_core_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./utils/core.js */ "./src/utils/core.js"); +/* harmony import */ var _utils_hub_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./utils/hub.js */ "./src/utils/hub.js"); +/* harmony import */ var _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./utils/maths.js */ "./src/utils/maths.js"); +/* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ./utils/tensor.js */ "./src/utils/tensor.js"); +/* harmony import */ var _utils_data_structures_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ./utils/data-structures.js */ "./src/utils/data-structures.js"); +/* harmony import */ var _huggingface_jinja__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! @huggingface/jinja */ "./node_modules/@huggingface/jinja/dist/index.js"); +/* harmony import */ var _models_whisper_common_whisper_js__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__(/*! ./models/whisper/common_whisper.js */ "./src/models/whisper/common_whisper.js"); +/* harmony import */ var _utils_constants_js__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__(/*! ./utils/constants.js */ "./src/utils/constants.js"); + +/** + * @file Tokenizers are used to prepare textual inputs for a model. + * + * **Example:** Create an `AutoTokenizer` and use it to tokenize a sentence. + * This will automatically detect the tokenizer type based on the tokenizer class defined in `tokenizer.json`. + * ```javascript + * import { AutoTokenizer } from '@huggingface/transformers'; + * + * const tokenizer = await AutoTokenizer.from_pretrained('Xenova/bert-base-uncased'); + * const { input_ids } = await tokenizer('I love transformers!'); + * // Tensor { + * // data: BigInt64Array(6) [101n, 1045n, 2293n, 19081n, 999n, 102n], + * // dims: [1, 6], + * // type: 'int64', + * // size: 6, + * // } + * ``` + * + * @module tokenizers + */ + + + + + + + + + + + + + + + + +/** + * @typedef {Object} TokenizerProperties Additional tokenizer-specific properties. + * @property {boolean} [legacy=false] Whether or not the `legacy` behavior of the tokenizer should be used. + * @typedef {import('./utils/hub.js').PretrainedOptions & TokenizerProperties} PretrainedTokenizerOptions + */ + +/** + * Loads a tokenizer from the specified path. + * @param {string} pretrained_model_name_or_path The path to the tokenizer directory. + * @param {PretrainedTokenizerOptions} options Additional options for loading the tokenizer. + * @returns {Promise} A promise that resolves with information about the loaded tokenizer. + */ +async function loadTokenizer(pretrained_model_name_or_path, options) { + + const info = await Promise.all([ + (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_2__.getModelJSON)(pretrained_model_name_or_path, 'tokenizer.json', true, options), + (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_2__.getModelJSON)(pretrained_model_name_or_path, 'tokenizer_config.json', true, options), + ]) + + // Override legacy option if `options.legacy` is not null + if (options.legacy !== null) { + info[1].legacy = options.legacy; + } + return info; +} + + +/** + * Helper function to split a string on a regex, but keep the delimiters. + * This is required, because the JavaScript `.split()` method does not keep the delimiters, + * and wrapping in a capturing group causes issues with existing capturing groups (due to nesting). + * @param {string} text The text to split. + * @param {RegExp} regex The regex to split on. + * @returns {string[]} The split string. + */ +function regexSplit(text, regex) { + const result = []; + let prev = 0; + for (const match of text.matchAll(regex)) { + const fullMatch = match[0]; + if (prev < match.index) { + result.push(text.slice(prev, match.index)); + } + if (fullMatch.length > 0) { + result.push(fullMatch); + } + prev = match.index + fullMatch.length; + } + if (prev < text.length) { + result.push(text.slice(prev)); + } + return result; +} + + +/** + * Helper method to construct a pattern from a config object. + * @param {Object} pattern The pattern object. + * @param {boolean} invert Whether to invert the pattern. + * @returns {RegExp|null} The compiled pattern. + */ +function createPattern(pattern, invert = true) { + + if (pattern.Regex !== undefined) { + // In certain cases, the pattern may contain unnecessary escape sequences (e.g., \# or \& or \~). + // i.e., valid in Python (where the patterns are exported from) but invalid in JavaScript (where the patterns are parsed). + // This isn't an issue when creating the regex w/o the 'u' flag, but it is when the 'u' flag is used. + // For this reason, it is necessary to remove these backslashes before creating the regex. + // See https://stackoverflow.com/a/63007777/13989043 for more information + let regex = pattern.Regex.replace(/\\([#&~])/g, '$1'); // TODO: add more characters to this list if necessary + + // We also handle special cases where the regex contains invalid (non-JS compatible) syntax. + for (const [key, value] of PROBLEMATIC_REGEX_MAP) { + regex = regex.replaceAll(key, value); + } + + return new RegExp(regex, 'gu'); + + } else if (pattern.String !== undefined) { + const escaped = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.escapeRegExp)(pattern.String); + // NOTE: if invert is true, we wrap the pattern in a group so that it is kept when performing .split() + return new RegExp(invert ? escaped : `(${escaped})`, 'gu'); + + } else { + console.warn('Unknown pattern type:', pattern) + return null; + } +} + +/** + * Helper function to convert an Object to a Map + * @param {Object} obj The object to convert. + * @returns {Map} The map. + */ +function objectToMap(obj) { + return new Map(Object.entries(obj)); +} + +/** + * Helper function to convert a tensor to a list before decoding. + * @param {Tensor} tensor The tensor to convert. + * @returns {number[]} The tensor as a list. + */ +function prepareTensorForDecode(tensor) { + const dims = tensor.dims; + switch (dims.length) { + case 1: + return tensor.tolist(); + case 2: + if (dims[0] !== 1) { + throw new Error('Unable to decode tensor with `batch size !== 1`. Use `tokenizer.batch_decode(...)` for batched inputs.'); + } + return tensor.tolist()[0]; + default: + throw new Error(`Expected tensor to have 1-2 dimensions, got ${dims.length}.`) + } +} + +/** + * Clean up a list of simple English tokenization artifacts like spaces before punctuations and abbreviated forms + * @param {string} text The text to clean up. + * @returns {string} The cleaned up text. + */ +function clean_up_tokenization(text) { + // Clean up a list of simple English tokenization artifacts + // like spaces before punctuations and abbreviated forms + return text.replace(/ \./g, '.') + .replace(/ \?/g, '?') + .replace(/ \!/g, '!') + .replace(/ ,/g, ',') + .replace(/ \' /g, "'") + .replace(/ n\'t/g, "n't") + .replace(/ \'m/g, "'m") + .replace(/ \'s/g, "'s") + .replace(/ \'ve/g, "'ve") + .replace(/ \'re/g, "'re"); +} + +/** + * Helper function to remove accents from a string. + * @param {string} text The text to remove accents from. + * @returns {string} The text with accents removed. + */ +function remove_accents(text) { + return text.replace(/\p{M}/gu, ''); +} + +/** + * Helper function to lowercase a string and remove accents. + * @param {string} text The text to lowercase and remove accents from. + * @returns {string} The lowercased text with accents removed. + */ +function lowercase_and_remove_accent(text) { + return remove_accents(text.toLowerCase()); +} + + +/** + * Checks whether the given Unicode codepoint represents a CJK (Chinese, Japanese, or Korean) character. + * + * A "chinese character" is defined as anything in the CJK Unicode block: + * https://en.wikipedia.org/wiki/CJK_Unified_Ideographs_(Unicode_block) + * + * Note that the CJK Unicode block is NOT all Japanese and Korean characters, despite its name. + * The modern Korean Hangul alphabet is a different block, as is Japanese Hiragana and Katakana. + * Those alphabets are used to write space-separated words, so they are not treated specially + * and are handled like all other languages. + * + * @param {number|bigint} cp The Unicode codepoint to check. + * @returns {boolean} True if the codepoint represents a CJK character, false otherwise. + */ +function is_chinese_char(cp) { + return ( + (cp >= 0x4E00 && cp <= 0x9FFF) + || (cp >= 0x3400 && cp <= 0x4DBF) + || (cp >= 0x20000 && cp <= 0x2A6DF) + || (cp >= 0x2A700 && cp <= 0x2B73F) + || (cp >= 0x2B740 && cp <= 0x2B81F) + || (cp >= 0x2B820 && cp <= 0x2CEAF) + || (cp >= 0xF900 && cp <= 0xFAFF) + || (cp >= 0x2F800 && cp <= 0x2FA1F) + ) +} + +/** + * Helper function to fuse consecutive unknown tokens. + * @param {string[]} arr The list of input tokens + * @param {Map} tokens_to_ids The mapping from tokens to token ids. + * @param {number} unk_token_id The value to fuse on. + * @private + */ +function fuse_unk(arr, tokens_to_ids, unk_token_id) { + const fused = []; + let i = 0; + while (i < arr.length) { + fused.push(arr[i]) + if ((tokens_to_ids.get(arr[i]) ?? unk_token_id) !== unk_token_id) { + ++i; + continue; + } + + while (++i < arr.length && (tokens_to_ids.get(arr[i]) ?? unk_token_id) === unk_token_id) { + if (tokens_to_ids.get(fused.at(-1)) !== unk_token_id) { + fused[fused.length - 1] += arr[i]; + } + } + } + + return fused; +} + +/** + * Split a string on whitespace. + * @param {string} text The text to split. + * @returns {string[]} The split string. + */ +function whitespace_split(text) { + return text.match(/\S+/g) || []; +} + +const PUNCTUATION_REGEX = '\\p{P}\\u0021-\\u002F\\u003A-\\u0040\\u005B-\\u0060\\u007B-\\u007E'; +const PUNCTUATION_ONLY_REGEX = new RegExp(`^[${PUNCTUATION_REGEX}]+$`, 'gu'); +const BLOOM_SPLIT_CHARS = '.,!?\u2026\u3002\uff0c\u3001\u0964\u06d4\u060c'; + +// A mapping of regex patterns to their equivalent (but possibly longer) JS-compatible versions. +const PROBLEMATIC_REGEX_MAP = new Map([ + // This uses the case insensitive group modifier, which is not supported in JavaScript. + // When parsing the regex, an "Invalid group" error is thrown. + ["(?i:'s|'t|'re|'ve|'m|'ll|'d)", "(?:'([sS]|[tT]|[rR][eE]|[vV][eE]|[mM]|[lL][lL]|[dD]))"], + + // Used to override the default (invalid) regex of the bloom pretokenizer. + // For more information, see https://github.com/huggingface/transformers.js/issues/94 + [` ?[^(\\s|[${BLOOM_SPLIT_CHARS}])]+`, ` ?[^\\s${BLOOM_SPLIT_CHARS}]+`], +]) + + +/** + * Represent a token added by the user on top of the existing Model vocabulary. + * AddedToken can be configured to specify the behavior they should have in various situations like: + * - Whether they should only match single words + * - Whether to include any whitespace on its left or right + */ +class AddedToken { + /** + * Creates a new instance of AddedToken. + * @param {Object} config Added token configuration object. + * @param {string} config.content The content of the added token. + * @param {number} config.id The id of the added token. + * @param {boolean} [config.single_word=false] Whether this token must be a single word or can break words. + * @param {boolean} [config.lstrip=false] Whether this token should strip whitespaces on its left. + * @param {boolean} [config.rstrip=false] Whether this token should strip whitespaces on its right. + * @param {boolean} [config.normalized=false] Whether this token should be normalized. + * @param {boolean} [config.special=false] Whether this token is special. + */ + constructor(config) { + this.content = config.content; + this.id = config.id; + this.single_word = config.single_word ?? false; + this.lstrip = config.lstrip ?? false; + this.rstrip = config.rstrip ?? false; + this.special = config.special ?? false; + this.normalized = config.normalized ?? null; + } +} + +/** + * Abstract base class for tokenizer models. + * + * @extends Callable + */ +class TokenizerModel extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * Creates a new instance of TokenizerModel. + * @param {Object} config The configuration object for the TokenizerModel. + */ + constructor(config) { + super(); + this.config = config; + + /** @type {string[]} */ + this.vocab = []; + + /** + * A mapping of tokens to ids. + * @type {Map} + */ + this.tokens_to_ids = new Map(); + + this.unk_token_id = undefined; + this.unk_token = undefined; + this.end_of_word_suffix = undefined; + + /** @type {boolean} Whether to fuse unknown tokens when encoding. Defaults to false. */ + this.fuse_unk = this.config.fuse_unk ?? false; + } + + /** + * Instantiates a new TokenizerModel instance based on the configuration object provided. + * @param {Object} config The configuration object for the TokenizerModel. + * @param {...*} args Optional arguments to pass to the specific TokenizerModel constructor. + * @returns {TokenizerModel} A new instance of a TokenizerModel. + * @throws Will throw an error if the TokenizerModel type in the config is not recognized. + */ + static fromConfig(config, ...args) { + switch (config.type) { + case 'WordPiece': + return new WordPieceTokenizer(config); + case 'Unigram': + // @ts-ignore + return new Unigram(config, ...args); + case 'BPE': + return new BPE(config); + + default: + // Some tokenizers, like for google-t5/t5-small, do not have a `type` field. + // In this case, we can infer the tokenizer type based on the structure of the `vocab` field. + if (config.vocab) { + if (Array.isArray(config.vocab)) { + // config.vocab is of type `[string, number][]` + // @ts-ignore + return new Unigram(config, ...args); + } else { + // @ts-ignore + return new LegacyTokenizerModel(config, ...args); + } + } + throw new Error(`Unknown TokenizerModel type: ${config.type}`); + } + } + + /** + * Internal function to call the TokenizerModel instance. + * @param {string[]} tokens The tokens to encode. + * @returns {string[]} The encoded tokens. + */ + _call(tokens) { + tokens = this.encode(tokens); + if (this.fuse_unk) { + // Fuse unknown tokens + tokens = fuse_unk(tokens, this.tokens_to_ids, this.unk_token_id); + } + return tokens; + } + + /** + * Encodes a list of tokens into a list of token IDs. + * @param {string[]} tokens The tokens to encode. + * @returns {string[]} The encoded tokens. + * @throws Will throw an error if not implemented in a subclass. + */ + encode(tokens) { + throw Error("encode should be implemented in subclass.") + } + + /** + * Converts a list of tokens into a list of token IDs. + * @param {string[]} tokens The tokens to convert. + * @returns {number[]} The converted token IDs. + */ + convert_tokens_to_ids(tokens) { + return tokens.map(t => this.tokens_to_ids.get(t) ?? this.unk_token_id); + } + + /** + * Converts a list of token IDs into a list of tokens. + * @param {number[]|bigint[]} ids The token IDs to convert. + * @returns {string[]} The converted tokens. + */ + convert_ids_to_tokens(ids) { + return ids.map(i => this.vocab[i] ?? this.unk_token); + } +} + +/** + * A subclass of TokenizerModel that uses WordPiece encoding to encode tokens. + * @extends TokenizerModel + */ +class WordPieceTokenizer extends TokenizerModel { + /** + * @param {Object} config The configuration object. + * @param {Object} config.vocab A mapping of tokens to ids. + * @param {string} config.unk_token The unknown token string. + * @param {string} config.continuing_subword_prefix The prefix to use for continuing subwords. + * @param {number} [config.max_input_chars_per_word=100] The maximum number of characters per word. + */ + constructor(config) { + super(config); + /** + * A mapping of tokens to ids. + * @type {Map} + */ + this.tokens_to_ids = objectToMap(config.vocab); + + /** + * The id of the unknown token. + * @type {number} + */ + this.unk_token_id = this.tokens_to_ids.get(config.unk_token); + + /** + * The unknown token string. + * @type {string} + */ + this.unk_token = config.unk_token; + + /** + * The maximum number of characters allowed per word. + * @type {number} + */ + this.max_input_chars_per_word = config.max_input_chars_per_word ?? 100; + + /** + * An array of tokens. + * @type {string[]} + */ + this.vocab = new Array(this.tokens_to_ids.size); + for (const [key, value] of this.tokens_to_ids) { + this.vocab[value] = key; + } + } + + /** + * Encodes an array of tokens using WordPiece encoding. + * @param {string[]} tokens The tokens to encode. + * @returns {string[]} An array of encoded tokens. + */ + encode(tokens) { + const outputTokens = []; + for (const token of tokens) { + const chars = [...token]; + if (chars.length > this.max_input_chars_per_word) { + outputTokens.push(this.unk_token); + continue; + } + + let isUnknown = false; + let start = 0; + const subTokens = []; + + while (start < chars.length) { + let end = chars.length; + let currentSubstring = null; + while (start < end) { + let substr = chars.slice(start, end).join(''); + + if (start > 0) { + substr = this.config.continuing_subword_prefix + substr; + } + if (this.tokens_to_ids.has(substr)) { + currentSubstring = substr; + break; + } + + --end; + } + if (currentSubstring === null) { + isUnknown = true; + break; + } + subTokens.push(currentSubstring); + start = end; + } + if (isUnknown) { + outputTokens.push(this.unk_token); + } else { + outputTokens.push(...subTokens); + } + } + + return outputTokens; + } + +} + +/** + * Class representing a Unigram tokenizer model. + * @extends TokenizerModel + */ +class Unigram extends TokenizerModel { + /** + * Create a new Unigram tokenizer model. + * @param {Object} config The configuration object for the Unigram model. + * @param {number} config.unk_id The ID of the unknown token + * @param {any[][]} config.vocab A 2D array representing a mapping of tokens to scores. + * @param {Object} moreConfig Additional configuration object for the Unigram model. + */ + constructor(config, moreConfig) { + super(config); + + const vocabSize = config.vocab.length; + this.vocab = new Array(vocabSize); + this.scores = new Array(vocabSize); + for (let i = 0; i < vocabSize; ++i) { + const piece = config.vocab[i]; + this.vocab[i] = piece[0]; + this.scores[i] = piece[1]; + } + + this.unk_token_id = config.unk_id; + this.unk_token = this.vocab[config.unk_id]; + + this.tokens_to_ids = new Map(this.vocab.map((x, i) => [x, i])); + this.bos_token = ' '; // beginning of a sentence token + + this.bos_token_id = this.tokens_to_ids.get(this.bos_token); // NOTE: may be undefined + this.eos_token = moreConfig.eos_token; + + this.eos_token_id = this.tokens_to_ids.get(this.eos_token); + this.unk_token = this.vocab[this.unk_token_id]; + + this.minScore = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.min)(this.scores)[0]; + + this.unk_score = this.minScore - 10.0; + this.scores[this.unk_token_id] = this.unk_score; + + this.trie = new _utils_data_structures_js__WEBPACK_IMPORTED_MODULE_5__.CharTrie(); + this.trie.extend(this.vocab); + + // NOTE: `fuse_unk` is hardcoded to true for Unigram models + // See: https://github.com/huggingface/tokenizers/blob/b58227c7f1ccf8b73ee2268354336da56d91e492/tokenizers/src/models/unigram/model.rs#L119 + this.fuse_unk = true; + } + + /** + * Populates lattice nodes. + * @param {TokenLattice} lattice The token lattice to populate with nodes. + */ + populateNodes(lattice) { + const chars = lattice.chars; + const mblen = 1; + let beginPos = 0; + while (beginPos < chars.length) { + let hasSingleNode = false; + + const tokens = []; + const sliced = chars.slice(beginPos).join(''); + const prefixedTokens = this.trie.commonPrefixSearch(sliced); + for (const token of prefixedTokens) { + tokens.push(token); + const tokenId = this.tokens_to_ids.get(token); + const tokenScore = this.scores[tokenId]; + const n = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.len)(token); + lattice.insert(beginPos, n, tokenScore, tokenId); + if (!hasSingleNode && n === mblen) { + hasSingleNode = true; + } + } + if (!hasSingleNode) { + lattice.insert(beginPos, mblen, this.unk_score, this.unk_token_id); + } + beginPos += mblen; + } + } + + /** + * Encodes an array of tokens into an array of subtokens using the unigram model. + * + * @param {string} normalized The normalized string. + * @returns {string[]} An array of subtokens obtained by encoding the input tokens using the unigram model. + */ + tokenize(normalized) { + const lattice = new _utils_data_structures_js__WEBPACK_IMPORTED_MODULE_5__.TokenLattice(normalized, this.bos_token_id, this.eos_token_id); + this.populateNodes(lattice); + return lattice.tokens(); + } + + /** + * Encodes an array of tokens using Unigram encoding. + * @param {string[]} tokens The tokens to encode. + * @returns {string[]} An array of encoded tokens. + */ + encode(tokens) { + const toReturn = []; + for (const token of tokens) { + const tokenized = this.tokenize(token); + toReturn.push(...tokenized); + } + return toReturn; + } + +} + +/** + * Returns list of utf-8 byte and a mapping to unicode strings. + * Specifically avoids mapping to whitespace/control characters the BPE code barfs on. + * @returns {Object} Object with utf-8 byte keys and unicode string values. + */ +const BYTES_TO_UNICODE = (() => { + // Returns list of utf-8 byte and a mapping to unicode strings. + // We specifically avoids mapping to whitespace/control characters + // the bpe code barfs on. + + const bs = [ + ...Array.from({ length: "~".charCodeAt(0) - "!".charCodeAt(0) + 1 }, (_, i) => i + "!".charCodeAt(0)), + ...Array.from({ length: "¬".charCodeAt(0) - "¡".charCodeAt(0) + 1 }, (_, i) => i + "¡".charCodeAt(0)), + ...Array.from({ length: "ÿ".charCodeAt(0) - "®".charCodeAt(0) + 1 }, (_, i) => i + "®".charCodeAt(0)), + ]; + const cs = bs.slice(); + let n = 0; + for (let b = 0; b < 256; ++b) { + if (!bs.includes(b)) { + bs.push(b); + cs.push(256 + n); + n += 1; + } + } + const ccs = cs.map(n => String.fromCharCode(n)); + return Object.fromEntries(bs.map((b, i) => [b, ccs[i]])); +})(); + +const UNICODE_TO_BYTES = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.reverseDictionary)(BYTES_TO_UNICODE); + + +/** + * @typedef {Object} BPENode + * @property {string} token The token associated with the node + * @property {number} bias A positional bias for the node. + * @property {number} [score] The score of the node. + * @property {BPENode} [prev] The previous node in the linked list. + * @property {BPENode} [next] The next node in the linked list. + */ + +/** + * BPE class for encoding text into Byte-Pair-Encoding (BPE) tokens. + * @extends TokenizerModel + */ +class BPE extends TokenizerModel { + /** + * Create a BPE instance. + * @param {Object} config The configuration object for BPE. + * @param {Object} config.vocab A mapping of tokens to ids. + * @param {string[]|[string, string][]} config.merges An array of BPE merges as strings. + * @param {string} config.unk_token The unknown token used for out of vocabulary words. + * @param {string} config.end_of_word_suffix The suffix to place at the end of each word. + * @param {string} [config.continuing_subword_suffix] The suffix to insert between words. + * @param {boolean} [config.byte_fallback=false] Whether to use spm byte-fallback trick (defaults to False) + * @param {boolean} [config.ignore_merges=false] Whether or not to match tokens with the vocab before using merges. + */ + constructor(config) { + super(config); + + /** @type {Map} */ + this.tokens_to_ids = objectToMap(config.vocab); + + this.unk_token_id = this.tokens_to_ids.get(config.unk_token); + this.unk_token = config.unk_token; + + this.vocab = new Array(this.tokens_to_ids.size); + for (const [key, value] of this.tokens_to_ids) { + this.vocab[value] = key; + } + + // Tokenizers >= 0.20.0 serializes BPE merges as a [string, string][] instead of a string[], + // which resolves the ambiguity for merges containing spaces. + const use_new_merge_format = Array.isArray(config.merges[0]); + + /** @type {[string, string][]} */ + this.merges = use_new_merge_format + ? /** @type {[string, string][]} */(config.merges) + : (/** @type {string[]} */(config.merges)).map(x => /** @type {[string, string]} */(x.split(' ', 2))); + this.bpe_ranks = new Map(this.merges.map((x, i) => [JSON.stringify(x), i])); + + this.end_of_word_suffix = config.end_of_word_suffix; + + // NOTE: `continuing_subword_suffix` is custom (to support `BlenderbotSmallTokenizer`) + this.continuing_subword_suffix = config.continuing_subword_suffix ?? null; + + this.byte_fallback = this.config.byte_fallback ?? false; + + if (this.byte_fallback) { + this.text_encoder = new TextEncoder(); + } + + this.ignore_merges = this.config.ignore_merges ?? false; + + /** @type {Map} */ + this.cache = new Map(); + } + + /** + * Apply Byte-Pair-Encoding (BPE) to a given token. Efficient heap-based priority + * queue implementation adapted from https://github.com/belladoreai/llama-tokenizer-js. + * @param {string} token The token to encode. + * @returns {string[]} The BPE encoded tokens. + */ + bpe(token) { + if (token.length === 0) { + return []; + } + + const cached = this.cache.get(token); + if (cached !== undefined) { + return cached; + } + + const word = Array.from(token); + if (this.end_of_word_suffix) { + word[word.length - 1] += this.end_of_word_suffix; + } + + let result = []; + if (word.length > 1) { + // Create a priority queue to store the nodes that will be merged. + // The comparator function compares the scores of the nodes. + const queue = new _utils_data_structures_js__WEBPACK_IMPORTED_MODULE_5__.PriorityQueue((a, b) => a.score < b.score); + + // Construct a doubly-linked list of nodes that will be inserted into the priority queue, + // starting with the individual characters. We also populate each node with a positional + // bias to break ties in the priority queue. + let startingNode = { + token: word[0], + bias: 0, + prev: null, + next: null, + } + + let previousNode = startingNode + for (let i = 1; i < word.length; ++i) { + const currentNode = { + bias: i / word.length, // Add fractional component to break ties + token: word[i], + prev: previousNode, + next: null, + } + previousNode.next = currentNode + this._add_node(queue, previousNode) + previousNode = currentNode + } + + while (!queue.isEmpty()) { + // Get the next node with the highest priority + const node = queue.pop(); + + // Check that this merge is still possible + if (node.deleted || !node.next || node.next.deleted) continue; + + // Here, we mark the current node (left side of the merge) and the next node (right side of the merge) as deleted. + // This is because they will both be replaced by a new node representing the merge result. + node.deleted = true; + node.next.deleted = true; + + // Next, we fix the node that comes before the current node (i.e., left side of the merge). + if (node.prev) { + + // Make a shallow copy of the previous node + const newPreviousNode = { ...node.prev }; + + // Mark the old previous node as deleted. This avoids erroneous merges later, + // because there may still be references to this node in the priority queue. + node.prev.deleted = true; + node.prev = newPreviousNode; + + // Update the reference of the previous node, by pointing its previous node to this new previous node. + if (newPreviousNode.prev) { + newPreviousNode.prev.next = newPreviousNode; + } else { + // If the previous of the previous node does not exist, it means that + // `newPreviousNode` must be the new `startingNode`. + startingNode = newPreviousNode; + } + } + + // Create a new node which represents the result of the merge. + const merged = { + token: node.token + node.next.token, + bias: node.bias, + prev: node.prev, + next: node.next.next, + } + + // We now consider where we can add the new merged node to the priority queue: + // 1. prev <-> merged + if (merged.prev) { + merged.prev.next = merged; + this._add_node(queue, merged.prev); + } else { + // If `merged.prev` does not exist, then `merged` must be the new `startingNode`. + startingNode = merged; + } + + // 2. merged <-> next + if (merged.next) { + merged.next.prev = merged; + this._add_node(queue, merged); + } + } + + // Traverse the linked list, starting from the `startingNode`, and collect the tokens. + for (let currentNode = startingNode; currentNode !== null; currentNode = currentNode.next) { + result.push(currentNode.token); + } + } else { + result = word; + } + + // Possibly append suffix + if (this.continuing_subword_suffix) { + // Do not append suffix to the last token + for (let i = 0; i < result.length - 1; ++i) { + result[i] += this.continuing_subword_suffix; + } + } + + // Save the result to the cache + this.cache.set(token, result); + + return result; + } + + + /** + * Helper function to add a node to the priority queue. + * @param {PriorityQueue} queue + * @param {BPENode} node + * @private + */ + _add_node(queue, node) { + // `score` is a measure of the merge priority: lower means higher priority + // We use the BPE rank as a measure of priority (i.e., the local of the merge in the merges list) + // We also add a fractional component to the score to break ties (with the earlier character having higher priority) + const rank = this.bpe_ranks.get(JSON.stringify([node.token, node.next.token])); + if (rank !== undefined) { + node.score = rank + node.bias; + queue.push(node); + } + } + + /** + * Encodes the input sequence of tokens using the BPE algorithm and returns the resulting subword tokens. + * @param {string[]} tokens The input sequence of tokens to encode. + * @returns {string[]} The resulting subword tokens after applying the BPE algorithm to the input sequence of tokens. + */ + encode(tokens) { + const outputTokens = []; + + for (const token of tokens) { + if (this.ignore_merges && this.tokens_to_ids.has(token)) { + outputTokens.push(token); + continue; + } + const bpe_token_list = this.bpe(token); + + for (const t of bpe_token_list) { + if (this.tokens_to_ids.has(t)) { + outputTokens.push(t); + } else if (this.byte_fallback) { + const byteTokens = Array.from(this.text_encoder.encode(t)) + .map(x => `<0x${x.toString(16).toUpperCase().padStart(2, '0')}>`); + if (byteTokens.every(x => this.tokens_to_ids.has(x))) { + // Ensure the byte tokens are actually in the vocabulary, otherwise + // we fall back to the unknown token. For more information, see + // https://github.com/huggingface/transformers/issues/28096. + outputTokens.push(...byteTokens); + } else { + outputTokens.push(this.unk_token); + } + } else { + outputTokens.push(this.unk_token); + } + } + } + + return outputTokens; + } + +} + +/** + * Legacy tokenizer class for tokenizers with only a vocabulary. + */ +class LegacyTokenizerModel extends TokenizerModel { + /** + * Create a LegacyTokenizerModel instance. + * @param {Object} config The configuration object for LegacyTokenizerModel. + * @param {Object} config.vocab A (possibly nested) mapping of tokens to ids. + * @param {Object} moreConfig Additional configuration object for the LegacyTokenizerModel model. + */ + constructor(config, moreConfig) { + super(config); + + /**@type {Map} */ + this.tokens_to_ids = objectToMap( + moreConfig.target_lang + ? config.vocab[moreConfig.target_lang] + : config.vocab + ); + + this.bos_token = moreConfig.bos_token; + this.bos_token_id = this.tokens_to_ids.get(this.bos_token); + + this.eos_token = moreConfig.eos_token; + this.eos_token_id = this.tokens_to_ids.get(this.eos_token); + + this.pad_token = moreConfig.pad_token; + this.pad_token_id = this.tokens_to_ids.get(this.pad_token); + + this.unk_token = moreConfig.unk_token; + this.unk_token_id = this.tokens_to_ids.get(this.unk_token); + + this.vocab = new Array(this.tokens_to_ids.size); + for (const [key, value] of this.tokens_to_ids) { + this.vocab[value] = key; + } + } + + encode(tokens) { + return tokens; + } +} + + +/** + * A base class for text normalization. + * @abstract + */ +class Normalizer extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * @param {Object} config The configuration object for the normalizer. + */ + constructor(config) { + super(); + this.config = config; + } + + /** + * Factory method for creating normalizers from config objects. + * @static + * @param {Object} config The configuration object for the normalizer. + * @returns {Normalizer} A Normalizer object. + * @throws {Error} If an unknown Normalizer type is specified in the config. + */ + static fromConfig(config) { + if (config === null) return null; + switch (config.type) { + case 'BertNormalizer': + return new BertNormalizer(config); + case 'Precompiled': + return new Precompiled(config); + case 'Sequence': + return new NormalizerSequence(config); + case 'Replace': + return new Replace(config); + case 'NFC': + return new NFC(config); + case 'NFKC': + return new NFKC(config); + case 'NFKD': + return new NFKD(config); + case 'Strip': + return new StripNormalizer(config); + case 'StripAccents': + return new StripAccents(config); + case 'Lowercase': + return new Lowercase(config); + case 'Prepend': + return new Prepend(config); + default: + throw new Error(`Unknown Normalizer type: ${config.type}`); + } + } + + /** + * Normalize the input text. + * @abstract + * @param {string} text The text to normalize. + * @returns {string} The normalized text. + * @throws {Error} If this method is not implemented in a subclass. + */ + normalize(text) { + throw Error("normalize should be implemented in subclass.") + } + + /** + * Alias for {@link Normalizer#normalize}. + * @param {string} text The text to normalize. + * @returns {string} The normalized text. + */ + _call(text) { + return this.normalize(text); + } + +} + +/** + * Replace normalizer that replaces occurrences of a pattern with a given string or regular expression. + * @extends Normalizer + */ +class Replace extends Normalizer { + /** + * Normalize the input text by replacing the pattern with the content. + * @param {string} text The input text to be normalized. + * @returns {string} The normalized text after replacing the pattern with the content. + */ + normalize(text) { + const pattern = createPattern(this.config.pattern); + return pattern === null + ? text + : text.replaceAll(pattern, this.config.content); + } +} + +/** + * A normalizer that applies Unicode normalization form C (NFC) to the input text. + * @extends Normalizer + */ +class NFC extends Normalizer { + /** + * Normalize the input text by applying Unicode normalization form C (NFC). + * @param {string} text The input text to be normalized. + * @returns {string} The normalized text. + */ + normalize(text) { + text = text.normalize('NFC') + return text; + } +} + +/** + * NFKC Normalizer. + * @extends Normalizer + */ +class NFKC extends Normalizer { + /** + * Normalize text using NFKC normalization. + * @param {string} text The text to be normalized. + * @returns {string} The normalized text. + */ + normalize(text) { + text = text.normalize('NFKC') + return text; + } +} +/** + * NFKD Normalizer. + * @extends Normalizer + */ +class NFKD extends Normalizer { + /** + * Normalize text using NFKD normalization. + * @param {string} text The text to be normalized. + * @returns {string} The normalized text. + */ + normalize(text) { + text = text.normalize('NFKD') + return text; + } +} + +/** + * A normalizer that strips leading and/or trailing whitespace from the input text. + */ +class StripNormalizer extends Normalizer { + /** + * Strip leading and/or trailing whitespace from the input text. + * @param {string} text The input text. + * @returns {string} The normalized text. + */ + normalize(text) { + if (this.config.strip_left && this.config.strip_right) { + // Fast path to avoid an extra trim call + text = text.trim(); + } else { + if (this.config.strip_left) { + text = text.trimStart(); + } + if (this.config.strip_right) { + text = text.trimEnd(); + } + } + return text; + } +} + +/** + * StripAccents normalizer removes all accents from the text. + * @extends Normalizer + */ +class StripAccents extends Normalizer { + /** + * Remove all accents from the text. + * @param {string} text The input text. + * @returns {string} The normalized text without accents. + */ + normalize(text) { + text = remove_accents(text); + return text; + } +} + +/** + * A Normalizer that lowercases the input string. + * @extends Normalizer + */ +class Lowercase extends Normalizer { + /** + * Lowercases the input string. + * @param {string} text The text to normalize. + * @returns {string} The normalized text. + */ + normalize(text) { + text = text.toLowerCase(); + return text; + } +} + +/** + * A Normalizer that prepends a string to the input string. + * @extends Normalizer + */ +class Prepend extends Normalizer { + /** + * Prepends the input string. + * @param {string} text The text to normalize. + * @returns {string} The normalized text. + */ + normalize(text) { + text = this.config.prepend + text; + return text; + } +} + +/** + * A Normalizer that applies a sequence of Normalizers. + * @extends Normalizer + */ +class NormalizerSequence extends Normalizer { + /** + * Create a new instance of NormalizerSequence. + * @param {Object} config The configuration object. + * @param {Object[]} config.normalizers An array of Normalizer configuration objects. + */ + constructor(config) { + super(config); + this.normalizers = config.normalizers.map(x => Normalizer.fromConfig(x)); + } + /** + * Apply a sequence of Normalizers to the input text. + * @param {string} text The text to normalize. + * @returns {string} The normalized text. + */ + normalize(text) { + return this.normalizers.reduce((t, normalizer) => { + return normalizer.normalize(t); + }, text); + } +} + +/** + * A class representing a normalizer used in BERT tokenization. + * @extends Normalizer + */ +class BertNormalizer extends Normalizer { + /** + * Adds whitespace around any CJK (Chinese, Japanese, or Korean) character in the input text. + * + * @param {string} text The input text to tokenize. + * @returns {string} The tokenized text with whitespace added around CJK characters. + */ + _tokenize_chinese_chars(text) { + /* Adds whitespace around any CJK character. */ + const output = []; + for (let i = 0; i < text.length; ++i) { + const char = text[i]; + const cp = char.charCodeAt(0); + if (is_chinese_char(cp)) { + output.push(" "); + output.push(char); + output.push(" "); + } else { + output.push(char); + } + } + return output.join(""); + } + + /** + * Strips accents from the given text. + * @param {string} text The text to strip accents from. + * @returns {string} The text with accents removed. + */ + stripAccents(text) { + // "Mark, Nonspacing" (Mn) + return text.normalize('NFD').replace(/\p{Mn}/gu, ''); + } + + + /** + * Checks whether `char` is a control character. + * @param {string} char The character to check. + * @returns {boolean} Whether `char` is a control character. + * @private + */ + _is_control(char) { + switch (char) { + case '\t': + case '\n': + case '\r': + // These are technically control characters but we count them as whitespace characters. + return false; + + default: + // Check if unicode category starts with C: + // Cc - Control + // Cf - Format + // Co - Private Use + // Cs - Surrogate + return /^\p{Cc}|\p{Cf}|\p{Co}|\p{Cs}$/u.test(char); + } + } + + /** + * Performs invalid character removal and whitespace cleanup on text. + * @param {string} text The text to clean. + * @returns {string} The cleaned text. + * @private + */ + _clean_text(text) { + const output = []; + for (const char of text) { + const cp = char.charCodeAt(0); + if (cp === 0 || cp === 0xFFFD || this._is_control(char)) { + continue; + } + if (/^\s$/.test(char)) { // is whitespace + output.push(" "); + } else { + output.push(char); + } + } + return output.join(""); + } + /** + * Normalizes the given text based on the configuration. + * @param {string} text The text to normalize. + * @returns {string} The normalized text. + */ + normalize(text) { + if (this.config.clean_text) { + text = this._clean_text(text); + } + + if (this.config.handle_chinese_chars) { + text = this._tokenize_chinese_chars(text); + } + + if (this.config.lowercase) { + text = text.toLowerCase(); + + if (this.config.strip_accents !== false) { + text = this.stripAccents(text); + } + } else if (this.config.strip_accents) { + text = this.stripAccents(text); + } + + return text; + } +} + +/** + * A callable class representing a pre-tokenizer used in tokenization. Subclasses + * should implement the `pre_tokenize_text` method to define the specific pre-tokenization logic. + * @extends Callable + */ +class PreTokenizer extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * Factory method that returns an instance of a subclass of `PreTokenizer` based on the provided configuration. + * + * @static + * @param {Object} config A configuration object for the pre-tokenizer. + * @returns {PreTokenizer} An instance of a subclass of `PreTokenizer`. + * @throws {Error} If the provided configuration object does not correspond to any known pre-tokenizer. + */ + static fromConfig(config) { + if (config === null) return null; + + switch (config.type) { + case 'BertPreTokenizer': + return new BertPreTokenizer(config); + case 'Sequence': + return new PreTokenizerSequence(config); + case 'Whitespace': + return new WhitespacePreTokenizer(config); + case 'WhitespaceSplit': + return new WhitespaceSplit(config); + case 'Metaspace': + return new MetaspacePreTokenizer(config); + + case 'ByteLevel': + return new ByteLevelPreTokenizer(config); + case 'Split': + return new SplitPreTokenizer(config); + case 'Punctuation': + return new PunctuationPreTokenizer(config); + case 'Digits': + return new DigitsPreTokenizer(config); + case 'Replace': + return new ReplacePreTokenizer(config); + default: + throw new Error(`Unknown PreTokenizer type: ${config.type}`); + } + } + + /** + * Method that should be implemented by subclasses to define the specific pre-tokenization logic. + * + * @abstract + * @param {string} text The text to pre-tokenize. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} The pre-tokenized text. + * @throws {Error} If the method is not implemented in the subclass. + */ + pre_tokenize_text(text, options) { + throw Error("pre_tokenize_text should be implemented in subclass.") + } + + /** + * Tokenizes the given text into pre-tokens. + * @param {string|string[]} text The text or array of texts to pre-tokenize. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} An array of pre-tokens. + */ + pre_tokenize(text, options) { + return (Array.isArray(text) + ? text.map(x => this.pre_tokenize_text(x, options)) + : this.pre_tokenize_text(text, options) + ).flat(); + } + + /** + * Alias for {@link PreTokenizer#pre_tokenize}. + * @param {string|string[]} text The text or array of texts to pre-tokenize. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} An array of pre-tokens. + */ + _call(text, options) { + return this.pre_tokenize(text, options); + } +} + +/** + * @extends PreTokenizer + */ +class BertPreTokenizer extends PreTokenizer { + /** + * A PreTokenizer that splits text into wordpieces using a basic tokenization scheme + * similar to that used in the original implementation of BERT. + * + * @param {Object} config The configuration object. + */ + constructor(config) { + super(); + // Construct a pattern which matches the rust implementation: + // https://github.com/huggingface/tokenizers/blob/b4fcc9ce6e4ad5806e82826f816acfdfdc4fcc67/tokenizers/src/pre_tokenizers/bert.rs#L11 + // Equivalent to removing whitespace and splitting on punctuation (both \p{P} and other ascii characters) + this.pattern = new RegExp(`[^\\s${PUNCTUATION_REGEX}]+|[${PUNCTUATION_REGEX}]`, 'gu'); + } + /** + * Tokenizes a single text using the BERT pre-tokenization scheme. + * + * @param {string} text The text to tokenize. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} An array of tokens. + */ + pre_tokenize_text(text, options) { + return text.trim().match(this.pattern) || []; + } +} + +/** + * A pre-tokenizer that splits text into Byte-Pair-Encoding (BPE) subwords. + * @extends PreTokenizer + */ +class ByteLevelPreTokenizer extends PreTokenizer { + /** + * Creates a new instance of the `ByteLevelPreTokenizer` class. + * @param {Object} config The configuration object. + */ + constructor(config) { + super(); + this.config = config; + + /** + * @type {boolean} Whether to add a leading space to the first word. + * This allows to treat the leading word just as any other word. + */ + this.add_prefix_space = this.config.add_prefix_space; + + /** + * @type {boolean} Whether the post processing step should trim offsets + * to avoid including whitespaces. + * @todo Use this in the pretokenization step. + */ + this.trim_offsets = this.config.trim_offsets; + + /** + * @type {boolean} Whether to use the standard GPT2 regex for whitespace splitting. + * Set it to False if you want to use your own splitting. Defaults to true. + */ + this.use_regex = this.config.use_regex ?? true; + this.pattern = /'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+/gu; + + this.byte_encoder = BYTES_TO_UNICODE; + this.text_encoder = new TextEncoder(); + } + + /** + * Tokenizes a single piece of text using byte-level tokenization. + * @param {string} text The text to tokenize. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} An array of tokens. + */ + pre_tokenize_text(text, options) { + // Add a leading space if the option is enabled + if (this.add_prefix_space && !text.startsWith(' ')) { + text = ' ' + text; + } + + // Split on whitespace and punctuation + const tokens = this.use_regex ? (text.match(this.pattern) || []) : [text]; + + // Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case) + return tokens.map( + token => Array.from(this.text_encoder.encode(token), byte => this.byte_encoder[byte]).join('') + ); + } +} + +/** + * @typedef {'removed'|'isolated'|'mergedWithPrevious'|'mergedWithNext'|'contiguous'} SplitDelimiterBehavior + */ + +/** + * Splits text using a given pattern. + * @extends PreTokenizer + */ +class SplitPreTokenizer extends PreTokenizer { + /** + * @param {Object} config The configuration options for the pre-tokenizer. + * @param {Object} config.pattern The pattern used to split the text. Can be a string or a regex object. + * @param {string|undefined} config.pattern.String The string to use for splitting. Only defined if the pattern is a string. + * @param {string|undefined} config.pattern.Regex The regex to use for splitting. Only defined if the pattern is a regex. + * @param {SplitDelimiterBehavior} config.behavior The behavior to use when splitting. + * @param {boolean} config.invert Whether to split (invert=false) or match (invert=true) the pattern. + */ + constructor(config) { + super(); + this.config = config; + // TODO support all behaviours (config.behavior) + + this.pattern = createPattern(this.config.pattern, this.config.invert); + } + + /** + * Tokenizes text by splitting it using the given pattern. + * @param {string} text The text to tokenize. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} An array of tokens. + */ + pre_tokenize_text(text, options) { + if (this.pattern === null) { + return []; + } + + if (this.config.invert) { + return text.match(this.pattern) || []; + } else { + return regexSplit(text, this.pattern); + } + } +} + +/** + * Splits text based on punctuation. + * @extends PreTokenizer + */ +class PunctuationPreTokenizer extends PreTokenizer { + /** + * @param {Object} config The configuration options for the pre-tokenizer. + * @param {SplitDelimiterBehavior} config.behavior The behavior to use when splitting. + */ + constructor(config) { + super(); + this.config = config; + this.pattern = new RegExp(`[^${PUNCTUATION_REGEX}]+|[${PUNCTUATION_REGEX}]+`, 'gu'); + } + + /** + * Tokenizes text by splitting it using the given pattern. + * @param {string} text The text to tokenize. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} An array of tokens. + */ + pre_tokenize_text(text, options) { + return text.match(this.pattern) || []; + } +} + + +/** + * Splits text based on digits. + * @extends PreTokenizer + */ +class DigitsPreTokenizer extends PreTokenizer { + /** + * @param {Object} config The configuration options for the pre-tokenizer. + * @param {boolean} config.individual_digits Whether to split on individual digits. + */ + constructor(config) { + super(); + this.config = config; + + // Construct a pattern which matches the rust implementation: + const digit_pattern = `[^\\d]+|\\d${this.config.individual_digits ? '' : '+'}`; + this.pattern = new RegExp(digit_pattern, 'gu'); + } + + /** + * Tokenizes text by splitting it using the given pattern. + * @param {string} text The text to tokenize. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} An array of tokens. + */ + pre_tokenize_text(text, options) { + return text.match(this.pattern) || []; + } +} + +/** + * @typedef {Object} PostProcessedOutput + * @property {string[]} tokens List of token produced by the post-processor. + * @property {number[]} [token_type_ids] List of token type ids produced by the post-processor. + */ + + +/** + * @typedef {Object} EncodingSingle + * @property {number[]} input_ids List of token ids to be fed to a model. + * @property {number[]} attention_mask List of token type ids to be fed to a model + * @property {number[]} [token_type_ids] List of indices specifying which tokens should be attended to by the model + */ + + +/** + * @extends Callable + */ +class PostProcessor extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + + /** + * @param {Object} config The configuration for the post-processor. + */ + constructor(config) { + super(); + this.config = config; + } + + /** + * Factory method to create a PostProcessor object from a configuration object. + * + * @param {Object} config Configuration object representing a PostProcessor. + * @returns {PostProcessor} A PostProcessor object created from the given configuration. + * @throws {Error} If an unknown PostProcessor type is encountered. + */ + static fromConfig(config) { + if (config === null) return null; + switch (config.type) { + case 'TemplateProcessing': + return new TemplateProcessing(config); + + case 'ByteLevel': + return new ByteLevelPostProcessor(config); + + case 'RobertaProcessing': + return new RobertaProcessing(config); + case 'BertProcessing': + return new BertProcessing(config); + + case 'Sequence': + return new PostProcessorSequence(config); + default: + throw new Error(`Unknown PostProcessor type: ${config.type}`); + } + } + + /** + * Method to be implemented in subclass to apply post-processing on the given tokens. + * + * @param {Array} tokens The input tokens to be post-processed. + * @param {...*} args Additional arguments required by the post-processing logic. + * @returns {PostProcessedOutput} The post-processed tokens. + * @throws {Error} If the method is not implemented in subclass. + */ + post_process(tokens, ...args) { + throw Error("post_process should be implemented in subclass.") + } + + /** + * Alias for {@link PostProcessor#post_process}. + * @param {Array} tokens The text or array of texts to post-process. + * @param {...*} args Additional arguments required by the post-processing logic. + * @returns {PostProcessedOutput} The post-processed tokens. + */ + _call(tokens, ...args) { + return this.post_process(tokens, ...args); + } +} + +/** + * A post-processor that adds special tokens to the beginning and end of the input. + */ +class BertProcessing extends PostProcessor { + /** + * @param {Object} config The configuration for the post-processor. + * @param {string[]} config.cls The special tokens to add to the beginning of the input. + * @param {string[]} config.sep The special tokens to add to the end of the input. + */ + constructor(config) { + super(config); + // TODO use all of config: add_prefix_space, trim_offsets + + this.cls = config.cls[0]; + this.sep = config.sep[0]; + } + + /** + * Adds the special tokens to the beginning and end of the input. + * @param {string[]} tokens The input tokens. + * @param {string[]} [tokens_pair=null] An optional second set of input tokens. + * @returns {PostProcessedOutput} The post-processed tokens with the special tokens added to the beginning and end. + */ + post_process(tokens, tokens_pair = null, { + add_special_tokens = true, + } = {}) { + if (add_special_tokens) { + tokens = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)([this.cls], tokens, [this.sep]); + } + + let token_type_ids = new Array(tokens.length).fill(0); + if (tokens_pair !== null) { + // NOTE: It is intended to add 2 EOS tokens after the first set of tokens + // https://github.com/huggingface/tokenizers/issues/983 + const middle = (add_special_tokens && this instanceof RobertaProcessing) + ? [this.sep] + : []; + const after = add_special_tokens ? [this.sep] : []; + + tokens = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(tokens, middle, tokens_pair, after); + token_type_ids = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(token_type_ids, new Array(tokens_pair.length + middle.length + after.length).fill(1)); + } + return { tokens, token_type_ids }; + } +} +class RobertaProcessing extends BertProcessing { } // NOTE: extends BertProcessing + +/** + * Post processor that replaces special tokens in a template with actual tokens. + * @extends PostProcessor + */ +class TemplateProcessing extends PostProcessor { + /** + * Creates a new instance of `TemplateProcessing`. + * @param {Object} config The configuration options for the post processor. + * @param {Array} config.single The template for a single sequence of tokens. + * @param {Array} config.pair The template for a pair of sequences of tokens. + */ + constructor(config) { + super(config); + + this.single = config.single; + this.pair = config.pair; + } + + /** + * Replaces special tokens in the template with actual tokens. + * @param {string[]} tokens The list of tokens for the first sequence. + * @param {string[]} [tokens_pair=null] The list of tokens for the second sequence (optional). + * @returns {PostProcessedOutput} An object containing the list of tokens with the special tokens replaced with actual tokens. + */ + post_process(tokens, tokens_pair = null, { + add_special_tokens = true, + } = {}) { + const type = tokens_pair === null ? this.single : this.pair + + let processedTokens = []; + let types = []; + for (const item of type) { + if ('SpecialToken' in item) { + if (add_special_tokens) { + processedTokens.push(item.SpecialToken.id); + types.push(item.SpecialToken.type_id); + } + } else if ('Sequence' in item) { + if (item.Sequence.id === 'A') { + processedTokens = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(processedTokens, tokens); + types = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(types, new Array(tokens.length).fill(item.Sequence.type_id)); + + } else if (item.Sequence.id === 'B') { + processedTokens = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(processedTokens, tokens_pair); + types = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(types, new Array(tokens_pair.length).fill(item.Sequence.type_id)); + } + } + } + return { tokens: processedTokens, token_type_ids: types }; + } +} + +/** + * A PostProcessor that returns the given tokens as is. + * @extends PostProcessor + */ +class ByteLevelPostProcessor extends PostProcessor { + /** + * Post process the given tokens. + * @param {string[]} tokens The list of tokens for the first sequence. + * @param {string[]} [tokens_pair=null] The list of tokens for the second sequence (optional). + * @returns {PostProcessedOutput} An object containing the post-processed tokens. + */ + post_process(tokens, tokens_pair = null) { + if (tokens_pair) { + tokens = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(tokens, tokens_pair); + } + return { tokens }; + } +} + + +/** + * A post-processor that applies multiple post-processors in sequence. + */ +class PostProcessorSequence extends PostProcessor { + + /** + * Creates a new instance of PostProcessorSequence. + * @param {Object} config The configuration object. + * @param {Object[]} config.processors The list of post-processors to apply. + */ + constructor(config) { + super(config); + + this.processors = config.processors.map(x => PostProcessor.fromConfig(x)); + } + + /** + * Post process the given tokens. + * @param {string[]} tokens The list of tokens for the first sequence. + * @param {string[]} [tokens_pair=null] The list of tokens for the second sequence (optional). + * @returns {PostProcessedOutput} An object containing the post-processed tokens. + */ + post_process(tokens, tokens_pair = null, options = {}) { + let token_type_ids; + for (const processor of this.processors) { + if (processor instanceof ByteLevelPostProcessor) { + // Special case where we need to pass the tokens_pair to the post-processor + const output = processor.post_process(tokens); + tokens = output.tokens; + if (tokens_pair) { + const pair_output = processor.post_process(tokens_pair); + tokens_pair = pair_output.tokens; + } + } else { + const output = processor.post_process(tokens, tokens_pair, options); + tokens = output.tokens; + token_type_ids = output.token_type_ids; + } + } + return { tokens, token_type_ids }; + } +} + +/** + * The base class for token decoders. + * @extends Callable + */ +class Decoder extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + + /** + * Creates an instance of `Decoder`. + * + * @param {Object} config The configuration object. + */ + constructor(config) { + super(); + this.config = config; + + /** @type {AddedToken[]} */ + this.added_tokens = []; + this.end_of_word_suffix = null; + this.trim_offsets = config.trim_offsets; + } + + /** + * Creates a decoder instance based on the provided configuration. + * + * @param {Object} config The configuration object. + * @returns {Decoder} A decoder instance. + * @throws {Error} If an unknown decoder type is provided. + */ + static fromConfig(config) { + if (config === null) return null; + switch (config.type) { + case 'WordPiece': + return new WordPieceDecoder(config); + case 'Metaspace': + return new MetaspaceDecoder(config); + case 'ByteLevel': + return new ByteLevelDecoder(config); + + case 'Replace': + return new ReplaceDecoder(config); + case 'ByteFallback': + return new ByteFallback(config); + case 'Fuse': + return new FuseDecoder(config); + case 'Strip': + return new StripDecoder(config); + + case 'Sequence': + return new DecoderSequence(config); + + case 'CTC': + return new CTCDecoder(config); + case 'BPEDecoder': + return new BPEDecoder(config); + default: + throw new Error(`Unknown Decoder type: ${config.type}`); + } + } + + /** + * Calls the `decode` method. + * + * @param {string[]} tokens The list of tokens. + * @returns {string} The decoded string. + */ + _call(tokens) { + return this.decode(tokens); + } + + /** + * Decodes a list of tokens. + * @param {string[]} tokens The list of tokens. + * @returns {string} The decoded string. + */ + decode(tokens) { + return this.decode_chain(tokens).join(''); + } + + /** + * Apply the decoder to a list of tokens. + * + * @param {string[]} tokens The list of tokens. + * @returns {string[]} The decoded list of tokens. + * @throws {Error} If the `decode_chain` method is not implemented in the subclass. + */ + decode_chain(tokens) { + throw Error("`decode_chain` should be implemented in subclass.") + } + +} + +class ReplaceDecoder extends Decoder { + + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + const pattern = createPattern(this.config.pattern); + return pattern === null + ? tokens + : tokens.map(token => token.replaceAll(pattern, this.config.content)) + } +} + + +class ByteFallback extends Decoder { + constructor(config) { + super(config); + + this.text_decoder = new TextDecoder(); + } + + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + + const new_tokens = []; + let previous_byte_tokens = []; + + for (const token of tokens) { + let bytes = null; + if (token.length === 6 && token.startsWith('<0x') && token.endsWith('>')) { + const byte = parseInt(token.slice(3, 5), 16); + if (!isNaN(byte)) { + bytes = byte; + } + } + if (bytes !== null) { + previous_byte_tokens.push(bytes); + } else { + if (previous_byte_tokens.length > 0) { + const string = this.text_decoder.decode(Uint8Array.from(previous_byte_tokens)); + new_tokens.push(string); + previous_byte_tokens = []; + } + new_tokens.push(token); + } + } + if (previous_byte_tokens.length > 0) { + const string = this.text_decoder.decode(Uint8Array.from(previous_byte_tokens)); + new_tokens.push(string); + previous_byte_tokens = []; + } + + return new_tokens; + } +} + +/** + * Fuse simply fuses all tokens into one big string. + * It's usually the last decoding step anyway, but this decoder + * exists incase some decoders need to happen after that step + */ +class FuseDecoder extends Decoder { + + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + return [tokens.join('')]; + } +} + + +class StripDecoder extends Decoder { + constructor(config) { + super(config); + + this.content = this.config.content; + this.start = this.config.start; + this.stop = this.config.stop; + } + + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + return tokens.map(token => { + let start_cut = 0; + for (let i = 0; i < this.start; ++i) { + if (token[i] === this.content) { + start_cut = i + 1; + continue; + } else { + break; + } + } + + let stop_cut = token.length; + for (let i = 0; i < this.stop; ++i) { + const index = token.length - i - 1; + if (token[index] === this.content) { + stop_cut = index; + continue; + } else { + break; + } + } + + return token.slice(start_cut, stop_cut) + }); + } +} + +/** + * A decoder that decodes a list of WordPiece tokens into a single string. + * @extends Decoder + */ +class WordPieceDecoder extends Decoder { + + /** + * Creates a new instance of WordPieceDecoder. + * @param {Object} config The configuration object. + * @param {string} config.prefix The prefix used for WordPiece encoding. + * @param {boolean} config.cleanup Whether to cleanup the decoded string. + */ + constructor(config) { + super(config); + this.cleanup = config.cleanup; + } + + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + return tokens.map((token, i) => { + if (i !== 0) { + if (token.startsWith(this.config.prefix)) { + // NOTE: .replace() is intended; only replace first occurrence + token = token.replace(this.config.prefix, ''); + } else { + token = ' ' + token; + } + } + if (this.cleanup) { + token = clean_up_tokenization(token) + } + + return token; + }); + } +} + +/** + * Byte-level decoder for tokenization output. Inherits from the `Decoder` class. + * @extends Decoder + */ +class ByteLevelDecoder extends Decoder { + + /** + * Create a `ByteLevelDecoder` object. + * @param {Object} config Configuration object. + */ + constructor(config) { + super(config); + + this.byte_decoder = UNICODE_TO_BYTES; + this.text_decoder = new TextDecoder("utf-8", { + fatal: false, + ignoreBOM: true, + }); + + this.end_of_word_suffix = null; + } + + /** + * Convert an array of tokens to string by decoding each byte. + * @param {string[]} tokens Array of tokens to be decoded. + * @returns {string} The decoded string. + */ + convert_tokens_to_string(tokens) { + const text = tokens.join(''); + const byteArray = new Uint8Array([...text].map(c => this.byte_decoder[c])); + const decoded_text = this.text_decoder.decode(byteArray); + return decoded_text; + } + + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + // TODO move to base class (like HF) + // tokens === filtered_tokens + + // To avoid mixing byte-level and unicode for byte-level BPT + // we need to build string separately for added tokens and byte-level tokens + // cf. https://github.com/huggingface/transformers/issues/1133 + const sub_texts = []; + let current_sub_text = []; + for (const token of tokens) { + // tokens sent here are already filtered, so we don't need to do this + // if (skip_special_tokens && this.all_special_ids.includes(token)) { + // continue; + // } + + if (this.added_tokens.find(x => x.content === token) !== undefined) { + if (current_sub_text.length > 0) { + sub_texts.push(this.convert_tokens_to_string(current_sub_text)); + current_sub_text = []; + } + sub_texts.push(token); + } else { + current_sub_text.push(token); + } + } + if (current_sub_text.length > 0) { + sub_texts.push(this.convert_tokens_to_string(current_sub_text)); + } + + // TODO add spaces_between_special_tokens and clean_up_tokenization_spaces options + + return sub_texts; + } +} + +/** + * The CTC (Connectionist Temporal Classification) decoder. + * See https://github.com/huggingface/tokenizers/blob/bb38f390a61883fc2f29d659af696f428d1cda6b/tokenizers/src/decoders/ctc.rs + */ +class CTCDecoder extends Decoder { + + constructor(config) { + super(config); + + this.pad_token = this.config.pad_token; + this.word_delimiter_token = this.config.word_delimiter_token; + this.cleanup = this.config.cleanup; + } + /** + * Converts a connectionist-temporal-classification (CTC) output tokens into a single string. + * @param {string[]} tokens Array of tokens to be decoded. + * @returns {string} The decoded string. + */ + convert_tokens_to_string(tokens) { + if (tokens.length === 0) return ''; + + // group same tokens into non-repeating tokens in CTC style decoding + const grouped_tokens = [tokens[0]]; + for (let i = 1; i < tokens.length; ++i) { + if (tokens[i] !== grouped_tokens.at(-1)) { + grouped_tokens.push(tokens[i]); + } + } + + // filter self.pad_token which is used as CTC-blank token + const filtered_tokens = grouped_tokens.filter(token => token !== this.pad_token); + + let text = filtered_tokens.join(''); + if (this.cleanup) { + // cleanup and replace delimiter token + text = clean_up_tokenization(text) + .replaceAll(this.word_delimiter_token, ' ') + .trim(); + } + return text; + } + + + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + return [this.convert_tokens_to_string(tokens)]; + } +} + +/** + * Apply a sequence of decoders. + * @extends Decoder + */ +class DecoderSequence extends Decoder { + + /** + * Creates a new instance of DecoderSequence. + * @param {Object} config The configuration object. + * @param {Object[]} config.decoders The list of decoders to apply. + */ + constructor(config) { + super(config); + this.decoders = config.decoders.map(x => Decoder.fromConfig(x)); + } + + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + // Use reduce to apply each decoder to the tokens + return this.decoders.reduce((toks, decoder) => { + return decoder.decode_chain(toks); + }, tokens); + } + +} + +class BPEDecoder extends Decoder { + constructor(config) { + super(config); + + this.suffix = this.config.suffix; + } + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + return tokens.map((token, i) => { + return token.replaceAll(this.suffix, (i === tokens.length - 1) ? '' : ' ') + }); + } +} + +// Custom decoder for VITS +class VitsDecoder extends Decoder { + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + let decoded = ''; + for (let i = 1; i < tokens.length; i += 2) { + decoded += tokens[i]; + } + return [decoded]; + } +} + + +/** + * This PreTokenizer replaces spaces with the given replacement character, adds a prefix space if requested, + * and returns a list of tokens. + * @extends PreTokenizer + */ +class MetaspacePreTokenizer extends PreTokenizer { + /** + * @param {Object} config The configuration object for the MetaspacePreTokenizer. + * @param {boolean} config.add_prefix_space Whether to add a prefix space to the first token. + * @param {string} config.replacement The character to replace spaces with. + * @param {string} [config.str_rep=config.replacement] An optional string representation of the replacement character. + * @param {'first'|'never'|'always'} [config.prepend_scheme='always'] The metaspace prepending scheme. + */ + constructor(config) { + super(); + + this.addPrefixSpace = config.add_prefix_space; + this.replacement = config.replacement; + this.strRep = config.str_rep || this.replacement; + this.prepend_scheme = config.prepend_scheme ?? 'always'; + } + + /** + * This method takes a string, replaces spaces with the replacement character, + * adds a prefix space if requested, and returns a new list of tokens. + * @param {string} text The text to pre-tokenize. + * @param {Object} [options] The options for the pre-tokenization. + * @param {number} [options.section_index] The index of the section to pre-tokenize. + * @returns {string[]} A new list of pre-tokenized tokens. + */ + pre_tokenize_text(text, { + section_index = undefined, + } = {}) { + + let normalized = text.replaceAll(' ', this.strRep); + + if ( + // We add a prefix space if: + // (1) The addPrefixSpace option is enabled and the normalized + // token does not already start with the replacement character. + (this.addPrefixSpace && !normalized.startsWith(this.replacement)) + + // and (2) either: + // (a) prepend_scheme is 'always' + // (b) prepend_scheme is 'first' and this is the first section + && ( + this.prepend_scheme === 'always' || + (this.prepend_scheme === 'first' && section_index === 0) + ) + ) { + normalized = this.strRep + normalized; + } + return [normalized]; + } +} + +/** + * MetaspaceDecoder class extends the Decoder class and decodes Metaspace tokenization. + * @extends Decoder + */ +class MetaspaceDecoder extends Decoder { + /** + * Constructs a new MetaspaceDecoder object. + * @param {Object} config The configuration object for the MetaspaceDecoder. + * @param {boolean} config.add_prefix_space Whether to add a prefix space to the decoded string. + * @param {string} config.replacement The string to replace spaces with. + */ + constructor(config) { + super(config); + + this.addPrefixSpace = config.add_prefix_space; + this.replacement = config.replacement; + } + + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + const result = []; + for (let i = 0; i < tokens.length; ++i) { + let normalized = tokens[i].replaceAll(this.replacement, ' '); + if (this.addPrefixSpace && i == 0 && normalized.startsWith(' ')) { + normalized = normalized.substring(1); + } + result.push(normalized); + } + return result; + } +} + +/** + * A normalizer that applies a precompiled charsmap. + * This is useful for applying complex normalizations in C++ and exposing them to JavaScript. + * @extends Normalizer + * @param {Object} config The configuration object for the Precompiled normalizer. + * @param {Object} config.precompiled_charsmap The precompiled charsmap object. + */ +class Precompiled extends Normalizer { + /** + * Create a new instance of Precompiled normalizer. + * @param {Object} config The configuration object. + * @param {any} config.precompiled_charsmap Precompiled chars mapping. + */ + constructor(config) { + super(config); + this.charsmap = config.precompiled_charsmap; + } + + /** + * Normalizes the given text by applying the precompiled charsmap. + * @param {string} text The text to normalize. + * @returns {string} The normalized text. + */ + normalize(text) { + // As stated in the sentencepiece normalization docs (https://github.com/google/sentencepiece/blob/master/doc/normalization.md#use-pre-defined-normalization-rule), + // there are 5 pre-defined normalization rules: + // 1. nmt_nfkc: NFKC normalization with some additional normalization around spaces. (default) + // 2. nfkc: original NFKC normalization. + // 3. nmt_nfkc_cf: nmt_nfkc + Unicode case folding (mostly lower casing) + // 4. nfkc_cf: nfkc + Unicode case folding. + // 5. identity: no normalization + // + // For now, we only implement the default (nmt_nfkc). + // See https://raw.githubusercontent.com/google/sentencepiece/master/data/nmt_nfkc.tsv for the full list of rules. + // TODO: detect when a different `this.charsmap` is used. + + text = text.replace(/[\u0001-\u0008\u000B\u000E-\u001F\u007F\u008F\u009F]/gm, ''); // Remove control characters + text = text.replace(/[\u0009\u000A\u000C\u000D\u00A0\u1680\u2000-\u200F\u2028\u2029\u202F\u205F\u2581\u3000\uFEFF\uFFFD]/gm, '\u0020'); // Replace certain characters with a space + + if (text.includes('\uFF5E')) { + // To match the sentencepiece implementation 100%, we must handle a very strange edge-case. + // For some reason, the "Fullwidth Tilde" character (\uFF5E) should not be converted to the standard Tilde character (\u007E). + // However, NFKC normalization does do this conversion. As a result, we split the string on the Fullwidth Tilde character, + // perform NFKC normalization on each substring, and then join them back together with the Fullwidth Tilde character. + const parts = text.split('\uFF5E'); + text = parts.map(part => part.normalize('NFKC')).join('\uFF5E'); + } else { + text = text.normalize('NFKC'); + } + + return text; + } +} + +/** + * A pre-tokenizer that applies a sequence of pre-tokenizers to the input text. + * @extends PreTokenizer + */ +class PreTokenizerSequence extends PreTokenizer { + /** + * Creates an instance of PreTokenizerSequence. + * @param {Object} config The configuration object for the pre-tokenizer sequence. + * @param {Object[]} config.pretokenizers An array of pre-tokenizer configurations. + */ + constructor(config) { + super(); + this.tokenizers = config.pretokenizers.map(x => PreTokenizer.fromConfig(x)); + } + + /** + * Applies each pre-tokenizer in the sequence to the input text in turn. + * @param {string} text The text to pre-tokenize. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} The pre-tokenized text. + */ + pre_tokenize_text(text, options) { + // Use reduce to apply each tokenizer to the text + return this.tokenizers.reduce((preTokenizedText, tokenizer) => { + return tokenizer.pre_tokenize(preTokenizedText, options); + }, [text]); + } +} + +/** + * Splits on word boundaries (using the following regular expression: `\w+|[^\w\s]+`). + */ +class WhitespacePreTokenizer extends PreTokenizer { + /** + * Creates an instance of WhitespacePreTokenizer. + * @param {Object} config The configuration object for the pre-tokenizer. + */ + constructor(config) { + super(); + } + /** + * Pre-tokenizes the input text by splitting it on word boundaries. + * @param {string} text The text to be pre-tokenized. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} An array of tokens produced by splitting the input text on whitespace. + */ + pre_tokenize_text(text, options) { + return text.match(/\w+|[^\w\s]+/g) || []; + } +} + +/** + * Splits a string of text by whitespace characters into individual tokens. + * @extends PreTokenizer + */ +class WhitespaceSplit extends PreTokenizer { + /** + * Creates an instance of WhitespaceSplit. + * @param {Object} config The configuration object for the pre-tokenizer. + */ + constructor(config) { + super(); + } + /** + * Pre-tokenizes the input text by splitting it on whitespace characters. + * @param {string} text The text to be pre-tokenized. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} An array of tokens produced by splitting the input text on whitespace. + */ + pre_tokenize_text(text, options) { + return whitespace_split(text); + } +} + +// NOTE: `ReplacePreTokenizer` is custom (to support `BlenderbotSmallTokenizer`) +class ReplacePreTokenizer extends PreTokenizer { + /** + * @param {Object} config The configuration options for the pre-tokenizer. + * @param {Object} config.pattern The pattern used to split the text. Can be a string or a regex object. + * @param {string} config.content What to replace the pattern with. + */ + constructor(config) { + super(); + this.config = config; + this.pattern = createPattern(this.config.pattern); + this.content = this.config.content; + } + + /** + * Pre-tokenizes the input text by replacing certain characters. + * @param {string} text The text to be pre-tokenized. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} An array of tokens produced by replacing certain characters. + */ + pre_tokenize_text(text, options) { + if (this.pattern === null) { + return [text]; + } + return [text.replaceAll(this.pattern, this.config.content)]; + } +} + +const SPECIAL_TOKEN_ATTRIBUTES = [ + 'bos_token', + 'eos_token', + 'unk_token', + 'sep_token', + 'pad_token', + 'cls_token', + 'mask_token', + // additional_special_tokens (TODO) +] + +/** + * + * Helper function for padding values of an object, which are each arrays. + * NOTE: No additional checks are made here for validity of arguments. + * @param {Record} item The input object. + * @param {number} length The length to pad to. + * @param {(key: string) => any} value_fn Determine the value to fill the array, based on its key. + * @param {string} side Which side to pad the array. + * @private + */ +function padHelper(item, length, value_fn, side) { + for (const key of Object.keys(item)) { + const diff = length - item[key].length; + const value = value_fn(key); + + const padData = new Array(diff).fill(value); + item[key] = side === 'right' + ? (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(item[key], padData) + : (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(padData, item[key]); + } +} + +/** + * Helper function for truncating values of an object, which are each arrays. + * NOTE: No additional checks are made here for validity of arguments. + * @param {Record} item The input object. + * @param {number} length The length to truncate to. + * @private + */ +function truncateHelper(item, length) { + // Setting .length to a lower value truncates the array in-place: + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length + for (const key of Object.keys(item)) { + item[key].length = length; + } +} + + +/** + * @typedef {Object} Message + * @property {string} role The role of the message (e.g., "user" or "assistant" or "system"). + * @property {string} content The content of the message. + */ + +class PreTrainedTokenizer extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + return_token_type_ids = false; + + padding_side = 'right'; + /** + * Create a new PreTrainedTokenizer instance. + * @param {Object} tokenizerJSON The JSON of the tokenizer. + * @param {Object} tokenizerConfig The config of the tokenizer. + */ + constructor(tokenizerJSON, tokenizerConfig) { + super(); + + this._tokenizer_config = tokenizerConfig; + + // Construct parts of the tokenizer from the JSON + this.normalizer = Normalizer.fromConfig(tokenizerJSON.normalizer); + this.pre_tokenizer = PreTokenizer.fromConfig(tokenizerJSON.pre_tokenizer); + this.model = TokenizerModel.fromConfig(tokenizerJSON.model, tokenizerConfig); + this.post_processor = PostProcessor.fromConfig(tokenizerJSON.post_processor); + this.decoder = Decoder.fromConfig(tokenizerJSON.decoder); + + // Add added_tokens to model + this.special_tokens = []; + this.all_special_ids = []; + + /** @type {AddedToken[]} */ + this.added_tokens = []; + for (const addedToken of tokenizerJSON.added_tokens) { + const token = new AddedToken(addedToken); + this.added_tokens.push(token); + + this.model.tokens_to_ids.set(token.content, token.id); + this.model.vocab[token.id] = token.content; + + if (token.special) { + this.special_tokens.push(token.content); + this.all_special_ids.push(token.id); + } + } + + // Update additional_special_tokens + this.additional_special_tokens = tokenizerConfig.additional_special_tokens ?? []; + this.special_tokens.push(...this.additional_special_tokens); + this.special_tokens = [...new Set(this.special_tokens)]; // Remove duplicates + + if (this.decoder) { + // Slight hack, but it prevents code duplication: + this.decoder.added_tokens = this.added_tokens; + + // Another slight hack to add `end_of_word_suffix` (if present) to the decoder + // This is needed for cases where BPE model and ByteLevel decoder are used + // For more information, see https://github.com/huggingface/transformers.js/issues/74 + // TODO: save this to the decoder when exporting? + this.decoder.end_of_word_suffix = this.model.end_of_word_suffix; + } + + this.added_tokens_regex = this.added_tokens.length > 0 ? new RegExp( + this.added_tokens.slice() + // Sort by length (desc) to avoid early partial matches + .sort((a, b) => b.content.length - a.content.length) + .map(x => `${x.lstrip ? '\\s*' : ''}(${(0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.escapeRegExp)(x.content)})${x.rstrip ? '\\s*' : ''}`) + .join('|') + ) : null; + + // Set mask token if present (otherwise will be undefined, which is fine) + this.mask_token = this.getToken('mask_token'); + this.mask_token_id = this.model.tokens_to_ids.get(this.mask_token); + + this.pad_token = this.getToken('pad_token', 'eos_token'); + this.pad_token_id = this.model.tokens_to_ids.get(this.pad_token); + + this.sep_token = this.getToken('sep_token'); + this.sep_token_id = this.model.tokens_to_ids.get(this.sep_token); + + this.unk_token = this.getToken('unk_token'); + this.unk_token_id = this.model.tokens_to_ids.get(this.unk_token); + + this.model_max_length = tokenizerConfig.model_max_length; + + /** @type {boolean} Whether or not to strip the text when tokenizing (removing excess spaces before and after the string). */ + this.remove_space = tokenizerConfig.remove_space; + + this.clean_up_tokenization_spaces = tokenizerConfig.clean_up_tokenization_spaces ?? true; + this.do_lowercase_and_remove_accent = tokenizerConfig.do_lowercase_and_remove_accent ?? false; + + if (tokenizerConfig.padding_side) { + this.padding_side = tokenizerConfig.padding_side; + } + + this.legacy = false; + + this.chat_template = tokenizerConfig.chat_template ?? null; + if (Array.isArray(this.chat_template)) { + // Chat templates are stored as lists of dicts with fixed key names, + // we reconstruct that into a single dict while loading them. + const chat_template = Object.create(null); + for (const { name, template } of this.chat_template) { + if (typeof name !== 'string' || typeof template !== 'string') { + throw new Error('Chat template must be a list of objects with "name" and "template" properties'); + } + chat_template[name] = template; + } + this.chat_template = chat_template; + } + this._compiled_template_cache = new Map(); + } + + /** + * Returns the value of the first matching key in the tokenizer config object. + * @param {...string} keys One or more keys to search for in the tokenizer config object. + * @returns {string|null} The value associated with the first matching key, or null if no match is found. + * @throws {Error} If an object is found for a matching key and its __type property is not "AddedToken". + * @private + */ + getToken(...keys) { + for (const key of keys) { + const item = this._tokenizer_config[key]; + + if (!item) continue; + + if (typeof item === 'object') { + if (item.__type === 'AddedToken') { + return item.content; + } else { + throw Error(`Unknown token: ${item}`); + } + } else { + return item; + } + } + return null; + } + + /** + * Loads a pre-trained tokenizer from the given `pretrained_model_name_or_path`. + * + * @param {string} pretrained_model_name_or_path The path to the pre-trained tokenizer. + * @param {PretrainedTokenizerOptions} options Additional options for loading the tokenizer. + * + * @throws {Error} Throws an error if the tokenizer.json or tokenizer_config.json files are not found in the `pretrained_model_name_or_path`. + * @returns {Promise} A new instance of the `PreTrainedTokenizer` class. + */ + static async from_pretrained(pretrained_model_name_or_path, { + progress_callback = null, + config = null, + cache_dir = null, + local_files_only = false, + revision = 'main', + legacy = null, + } = {}) { + + const info = await loadTokenizer(pretrained_model_name_or_path, { + progress_callback, + config, + cache_dir, + local_files_only, + revision, + legacy, + }) + + // @ts-ignore + return new this(...info); + } + + /** + * @typedef {number[]|number[][]|Tensor} BatchEncodingItem + * + * @typedef {Object} BatchEncoding Holds the output of the tokenizer's call function. + * @property {BatchEncodingItem} input_ids List of token ids to be fed to a model. + * @property {BatchEncodingItem} attention_mask List of indices specifying which tokens should be attended to by the model. + * @property {BatchEncodingItem} [token_type_ids] List of token type ids to be fed to a model. + */ + + /** + * Encode/tokenize the given text(s). + * @param {string|string[]} text The text to tokenize. + * @param {Object} options An optional object containing the following properties: + * @param {string|string[]} [options.text_pair=null] Optional second sequence to be encoded. If set, must be the same type as text. + * @param {boolean|'max_length'} [options.padding=false] Whether to pad the input sequences. + * @param {boolean} [options.add_special_tokens=true] Whether or not to add the special tokens associated with the corresponding model. + * @param {boolean} [options.truncation=null] Whether to truncate the input sequences. + * @param {number} [options.max_length=null] Maximum length of the returned list and optionally padding length. + * @param {boolean} [options.return_tensor=true] Whether to return the results as Tensors or arrays. + * @param {boolean} [options.return_token_type_ids=null] Whether to return the token type ids. + * @returns {BatchEncoding} Object to be passed to the model. + */ + _call( + // Required positional arguments + text, + + // Optional keyword arguments + { + text_pair = null, + add_special_tokens = true, + padding = false, + truncation = null, + max_length = null, + return_tensor = true, // Different to HF + return_token_type_ids = null, + } = {}, + ) { + + const isBatched = Array.isArray(text); + + /** @type {EncodingSingle[]} */ + let encodedTokens; + + if (isBatched) { + if (text.length === 0) { + throw Error('text array must be non-empty') + } + + if (text_pair !== null) { + if (!Array.isArray(text_pair)) { + throw Error('text_pair must also be an array') + + } else if (text.length !== text_pair.length) { + throw Error('text and text_pair must have the same length') + } + + encodedTokens = text.map( + (t, i) => this._encode_plus(t, { text_pair: text_pair[i], add_special_tokens, return_token_type_ids }) + ) + + } else { + encodedTokens = text.map(x => this._encode_plus(x, { add_special_tokens, return_token_type_ids })); + } + + } else { + if (text === null || text === undefined) { + throw Error('text may not be null or undefined') + } + + if (Array.isArray(text_pair)) { + throw Error('When specifying `text_pair`, since `text` is a string, `text_pair` must also be a string (i.e., not an array).') + } + + // For single input, we just wrap in an array, and then unwrap later. + encodedTokens = [this._encode_plus(text, { text_pair, add_special_tokens, return_token_type_ids })]; + } + // At this point, tokens is batched: [batch_size, tokens] + // However, array may be jagged. So, we pad to max_length + + if (max_length === null) { + if (padding === 'max_length') { + max_length = this.model_max_length; + } else { + // Calculate max length from sequences + max_length = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.max)(encodedTokens.map(x => x.input_ids.length))[0]; + } + } else { + if (!truncation) { + console.warn(`Truncation was not explicitly activated but \`max_length\` is provided a specific value, please use \`truncation=true\` to explicitly truncate examples to max length.`) + } + } + + // Ensure it is less than model max length + max_length = Math.min(max_length, this.model_max_length ?? Infinity); + + if (padding || truncation) { + + // Perform padding and/or truncation + for (let i = 0; i < encodedTokens.length; ++i) { + if (encodedTokens[i].input_ids.length === max_length) { + continue; + + } else if (encodedTokens[i].input_ids.length > max_length) { + // possibly truncate + if (truncation) { + truncateHelper(encodedTokens[i], max_length); + } + + } else { // t.length < max_length + // possibly pad + if (padding) { + padHelper( + encodedTokens[i], + max_length, + key => key === 'input_ids' ? this.pad_token_id : 0, + this.padding_side + ); + } + } + } + } + + const result = {}; + + if (return_tensor) { + if (!(padding && truncation)) { + // Not, guaranteed that all items have same length, so + // we perform additional check + + if ( + encodedTokens.some(x => { + for (const key of Object.keys(x)) { + if (x[key].length !== encodedTokens[0][key]?.length) { + return true; + } + } + return false; + }) + ) { + throw Error( + "Unable to create tensor, you should probably activate truncation and/or padding " + + "with 'padding=true' and 'truncation=true' to have batched tensors with the same length." + ) + } + } + + // Now we actually convert to tensor + // NOTE: In the same way as the python library, we return a batched tensor, regardless of + // whether we have a single input or multiple inputs. + const dims = [encodedTokens.length, encodedTokens[0].input_ids.length]; + + for (const key of Object.keys(encodedTokens[0])) { + result[key] = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor('int64', + BigInt64Array.from(encodedTokens.flatMap(x => x[key]).map(BigInt)), + dims + ); + } + + } else { + for (const key of Object.keys(encodedTokens[0])) { + result[key] = encodedTokens.map(x => x[key]); + } + + // If not returning a tensor, we match the input type + if (!isBatched) { + // Input was not batched, so we unwrap + for (const key of Object.keys(result)) { + result[key] = result[key][0]; + } + } + } + + return /** @type {BatchEncoding} */(result); + } + + /** + * Encodes a single text using the preprocessor pipeline of the tokenizer. + * + * @param {string|null} text The text to encode. + * @returns {string[]|null} The encoded tokens. + */ + _encode_text(text) { + if (text === null) return null; + + // Actual function which does encoding, for a single text + // First, we take care of special tokens. Needed to avoid issues arising from + // normalization and/or pretokenization (which may not preserve special tokens) + const sections = this.added_tokens_regex ? text.split(this.added_tokens_regex).filter(x => x) : [text]; + + const tokens = sections.map((x, section_index) => { + const addedToken = this.added_tokens.find(t => t.content === x); + if (addedToken !== undefined) { + // Ignore added tokens + return x + } else { + if (this.remove_space === true) { + x = x.trim().split(/\s+/).join(' '); + } + if (this.do_lowercase_and_remove_accent) { + x = lowercase_and_remove_accent(x); + } + + if (this.normalizer !== null) { + x = this.normalizer(x); + } + + // If, after normalization, this section is empty (e.g., trimming whitespace), + // we return an empty array + if (x.length === 0) { + return []; + } + + const sectionTokens = (this.pre_tokenizer !== null) ? this.pre_tokenizer(x, { + section_index, + }) : [x]; + + const tokens = this.model(sectionTokens); + + return tokens; + } + }).flat(); + + return tokens; + } + + /** + * Encodes a single text or a pair of texts using the model's tokenizer. + * + * @param {string} text The text to encode. + * @param {Object} options An optional object containing the following properties: + * @param {string} [options.text_pair=null] The optional second text to encode. + * @param {boolean} [options.add_special_tokens=true] Whether or not to add the special tokens associated with the corresponding model. + * @param {boolean} [options.return_token_type_ids=null] Whether to return token_type_ids. + * @returns {EncodingSingle} An object containing the encoded text. + * @private + */ + _encode_plus(text, { + text_pair = null, + add_special_tokens = true, + return_token_type_ids = null, + } = {}) { + + const { tokens, token_type_ids } = this._tokenize_helper(text, { pair: text_pair, add_special_tokens }); + + const input_ids = this.model.convert_tokens_to_ids(tokens); + + const result = { + input_ids, + attention_mask: new Array(input_ids.length).fill(1), + } + if ((return_token_type_ids ?? this.return_token_type_ids) && token_type_ids) { + result.token_type_ids = token_type_ids; + } + return result; + } + + /** + * Internal helper function to tokenize a text, and optionally a pair of texts. + * @param {string} text The text to tokenize. + * @param {Object} options An optional object containing the following properties: + * @param {string} [options.pair=null] The optional second text to tokenize. + * @param {boolean} [options.add_special_tokens=false] Whether or not to add the special tokens associated with the corresponding model. + * @returns {{tokens: string[], token_type_ids?: number[]}} An object containing the tokens and optionally the token type IDs. + */ + _tokenize_helper(text, { + pair = null, + add_special_tokens = false, + } = {}) { + const tokens = this._encode_text(text); + const tokens2 = this._encode_text(pair); + + return this.post_processor + ? this.post_processor(tokens, tokens2, { add_special_tokens }) + : { tokens: (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(tokens ?? [], tokens2 ?? []) }; + } + + /** + * Converts a string into a sequence of tokens. + * @param {string} text The sequence to be encoded. + * @param {Object} options An optional object containing the following properties: + * @param {string} [options.pair] A second sequence to be encoded with the first. + * @param {boolean} [options.add_special_tokens=false] Whether or not to add the special tokens associated with the corresponding model. + * @returns {string[]} The list of tokens. + */ + tokenize(text, { + pair = null, + add_special_tokens = false, + } = {}) { + return this._tokenize_helper(text, { pair, add_special_tokens }).tokens; + } + + /** + * Encodes a single text or a pair of texts using the model's tokenizer. + * + * @param {string} text The text to encode. + * @param {Object} options An optional object containing the following properties: + * @param {string} [options.text_pair=null] The optional second text to encode. + * @param {boolean} [options.add_special_tokens=true] Whether or not to add the special tokens associated with the corresponding model. + * @param {boolean} [options.return_token_type_ids=null] Whether to return token_type_ids. + * @returns {number[]} An array of token IDs representing the encoded text(s). + */ + encode(text, { + text_pair = null, + add_special_tokens = true, + return_token_type_ids = null, + } = {}) { + return this._encode_plus(text, { + text_pair, + add_special_tokens, + return_token_type_ids, + }).input_ids; + } + + /** + * Decode a batch of tokenized sequences. + * @param {number[][]|Tensor} batch List/Tensor of tokenized input sequences. + * @param {Object} decode_args (Optional) Object with decoding arguments. + * @returns {string[]} List of decoded sequences. + */ + batch_decode(batch, decode_args = {}) { + if (batch instanceof _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor) { + batch = batch.tolist(); + } + return batch.map(x => this.decode(x, decode_args)); + } + + /** + * Decodes a sequence of token IDs back to a string. + * + * @param {number[]|bigint[]|Tensor} token_ids List/Tensor of token IDs to decode. + * @param {Object} [decode_args={}] + * @param {boolean} [decode_args.skip_special_tokens=false] If true, special tokens are removed from the output string. + * @param {boolean} [decode_args.clean_up_tokenization_spaces=true] If true, spaces before punctuations and abbreviated forms are removed. + * + * @returns {string} The decoded string. + * @throws {Error} If `token_ids` is not a non-empty array of integers. + */ + decode( + token_ids, + decode_args = {}, + ) { + if (token_ids instanceof _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor) { + token_ids = prepareTensorForDecode(token_ids); + } + + if (!Array.isArray(token_ids) || token_ids.length === 0 || !(0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.isIntegralNumber)(token_ids[0])) { + throw Error("token_ids must be a non-empty array of integers."); + } + + return this.decode_single(token_ids, decode_args) + } + + /** + * Decode a single list of token ids to a string. + * @param {number[]|bigint[]} token_ids List of token ids to decode + * @param {Object} decode_args Optional arguments for decoding + * @param {boolean} [decode_args.skip_special_tokens=false] Whether to skip special tokens during decoding + * @param {boolean} [decode_args.clean_up_tokenization_spaces=null] Whether to clean up tokenization spaces during decoding. + * If null, the value is set to `this.decoder.cleanup` if it exists, falling back to `this.clean_up_tokenization_spaces` if it exists, falling back to `true`. + * @returns {string} The decoded string + */ + decode_single( + token_ids, + { + skip_special_tokens = false, + clean_up_tokenization_spaces = null, + } + ) { + let tokens = this.model.convert_ids_to_tokens(token_ids); + if (skip_special_tokens) { + tokens = tokens.filter(x => !this.special_tokens.includes(x)); + } + + // If `this.decoder` is null, we just join tokens with a space: + // https://github.com/huggingface/tokenizers/blob/8edec536a737cb04494b454805be16c020abb14f/tokenizers/src/tokenizer/mod.rs#L835 + /** @type {string} */ + let decoded = this.decoder ? this.decoder(tokens) : tokens.join(' '); + + // Slight hack, but prevents having to pass `skip_special_tokens` to + // each call to `decode`, which would lead to code duplication. + if (this.decoder && this.decoder.end_of_word_suffix) { + decoded = decoded.replaceAll(this.decoder.end_of_word_suffix, ' '); + if (skip_special_tokens) { + decoded = decoded.trim(); + } + } + + if (clean_up_tokenization_spaces ?? this.clean_up_tokenization_spaces) { + decoded = clean_up_tokenization(decoded); + } + + return decoded; + } + + /** + * Retrieve the chat template string used for tokenizing chat messages. This template is used + * internally by the `apply_chat_template` method and can also be used externally to retrieve the model's chat + * template for better generation tracking. + * + * @param {Object} options An optional object containing the following properties: + * @param {string} [options.chat_template=null] + * A Jinja template or the name of a template to use for this conversion. + * It is usually not necessary to pass anything to this argument, + * as the model's template will be used by default. + * @param {Object[]} [options.tools=null] + * A list of tools (callable functions) that will be accessible to the model. If the template does not + * support function calling, this argument will have no effect. Each tool should be passed as a JSON Schema, + * giving the name, description and argument types for the tool. See our + * [chat templating guide](https://huggingface.co/docs/transformers/main/en/chat_templating#automated-function-conversion-for-tool-use) + * for more information. + * @returns {string} The chat template string. + */ + get_chat_template({ + chat_template = null, + tools = null, + } = {}) { + + // First, handle the cases when the model has a dict of multiple templates + if (this.chat_template && typeof this.chat_template === 'object') { + const template_dict = this.chat_template; + + if (chat_template !== null && Object.hasOwn(template_dict, chat_template)) { + // The user can pass the name of a template to the chat template argument instead of an entire template + chat_template = template_dict[chat_template]; + } else if (chat_template === null) { + if (tools !== null && 'tool_use' in template_dict) { + chat_template = template_dict['tool_use']; + } else if ('default' in template_dict) { + chat_template = template_dict['default']; + } else { + throw Error( + `This model has multiple chat templates with no default specified! Please either pass a chat ` + + `template or the name of the template you wish to use to the 'chat_template' argument. Available ` + + `template names are ${Object.keys(template_dict).sort()}.` + ) + } + } + } else if (chat_template === null) { + // These are the cases when the model has a single template + // priority: `chat_template` argument > `tokenizer.chat_template` + if (this.chat_template) { + chat_template = this.chat_template; + } else { + throw Error( + "Cannot use apply_chat_template() because tokenizer.chat_template is not set and no template " + + "argument was passed! For information about writing templates and setting the " + + "tokenizer.chat_template attribute, please see the documentation at " + + "https://huggingface.co/docs/transformers/main/en/chat_templating" + ) + } + } + return chat_template; + } + + /** + * Converts a list of message objects with `"role"` and `"content"` keys to a list of token + * ids. This method is intended for use with chat models, and will read the tokenizer's chat_template attribute to + * determine the format and control tokens to use when converting. + * + * See [here](https://huggingface.co/docs/transformers/chat_templating) for more information. + * + * **Example:** Applying a chat template to a conversation. + * + * ```javascript + * import { AutoTokenizer } from "@huggingface/transformers"; + * + * const tokenizer = await AutoTokenizer.from_pretrained("Xenova/mistral-tokenizer-v1"); + * + * const chat = [ + * { "role": "user", "content": "Hello, how are you?" }, + * { "role": "assistant", "content": "I'm doing great. How can I help you today?" }, + * { "role": "user", "content": "I'd like to show off how chat templating works!" }, + * ] + * + * const text = tokenizer.apply_chat_template(chat, { tokenize: false }); + * // "[INST] Hello, how are you? [/INST]I'm doing great. How can I help you today? [INST] I'd like to show off how chat templating works! [/INST]" + * + * const input_ids = tokenizer.apply_chat_template(chat, { tokenize: true, return_tensor: false }); + * // [1, 733, 16289, 28793, 22557, 28725, 910, 460, 368, 28804, 733, 28748, 16289, 28793, 28737, 28742, 28719, 2548, 1598, 28723, 1602, 541, 315, 1316, 368, 3154, 28804, 2, 28705, 733, 16289, 28793, 315, 28742, 28715, 737, 298, 1347, 805, 910, 10706, 5752, 1077, 3791, 28808, 733, 28748, 16289, 28793] + * ``` + * + * @param {Message[]} conversation A list of message objects with `"role"` and `"content"` keys, + * representing the chat history so far. + * @param {Object} options An optional object containing the following properties: + * @param {string} [options.chat_template=null] A Jinja template to use for this conversion. If + * this is not passed, the model's chat template will be used instead. + * @param {Object[]} [options.tools=null] + * A list of tools (callable functions) that will be accessible to the model. If the template does not + * support function calling, this argument will have no effect. Each tool should be passed as a JSON Schema, + * giving the name, description and argument types for the tool. See our + * [chat templating guide](https://huggingface.co/docs/transformers/main/en/chat_templating#automated-function-conversion-for-tool-use) + * for more information. + * @param {Record[]} [options.documents=null] + * A list of dicts representing documents that will be accessible to the model if it is performing RAG + * (retrieval-augmented generation). If the template does not support RAG, this argument will have no + * effect. We recommend that each document should be a dict containing "title" and "text" keys. Please + * see the RAG section of the [chat templating guide](https://huggingface.co/docs/transformers/main/en/chat_templating#arguments-for-RAG) + * for examples of passing documents with chat templates. + * @param {boolean} [options.add_generation_prompt=false] Whether to end the prompt with the token(s) that indicate + * the start of an assistant message. This is useful when you want to generate a response from the model. + * Note that this argument will be passed to the chat template, and so it must be supported in the + * template for this argument to have any effect. + * @param {boolean} [options.tokenize=true] Whether to tokenize the output. If false, the output will be a string. + * @param {boolean} [options.padding=false] Whether to pad sequences to the maximum length. Has no effect if tokenize is false. + * @param {boolean} [options.truncation=false] Whether to truncate sequences to the maximum length. Has no effect if tokenize is false. + * @param {number} [options.max_length=null] Maximum length (in tokens) to use for padding or truncation. Has no effect if tokenize is false. + * If not specified, the tokenizer's `max_length` attribute will be used as a default. + * @param {boolean} [options.return_tensor=true] Whether to return the output as a Tensor or an Array. Has no effect if tokenize is false. + * @param {boolean} [options.return_dict=true] Whether to return a dictionary with named outputs. Has no effect if tokenize is false. + * @param {Object} [options.tokenizer_kwargs={}] Additional options to pass to the tokenizer. + * @returns {string | Tensor | number[]| number[][]|BatchEncoding} The tokenized output. + */ + apply_chat_template(conversation, { + tools = null, + documents = null, + chat_template = null, + add_generation_prompt = false, + tokenize = true, + padding = false, + truncation = false, + max_length = null, + return_tensor = true, + return_dict = false, + tokenizer_kwargs = {}, + ...kwargs + } = {}) { + + chat_template = this.get_chat_template({ chat_template, tools }); + + if (typeof chat_template !== 'string') { + throw Error(`chat_template must be a string, but got ${typeof chat_template}`); + } + + // Compilation function uses a cache to avoid recompiling the same template + let compiledTemplate = this._compiled_template_cache.get(chat_template); + if (compiledTemplate === undefined) { + compiledTemplate = new _huggingface_jinja__WEBPACK_IMPORTED_MODULE_6__.Template(chat_template); + this._compiled_template_cache.set(chat_template, compiledTemplate); + } + + const special_tokens_map = Object.create(null); + for (const key of SPECIAL_TOKEN_ATTRIBUTES) { + const value = this.getToken(key); + if (value) { + special_tokens_map[key] = value; + } + } + + const rendered = compiledTemplate.render({ + messages: conversation, + add_generation_prompt, + tools, + documents, + ...special_tokens_map, + ...kwargs, + }); + + if (tokenize) { + const out = this._call(rendered, { + add_special_tokens: false, + padding, + truncation, + max_length, + return_tensor, + ...tokenizer_kwargs, + }); + return return_dict ? out : out.input_ids; + } + + return rendered; + } +} + +/** + * BertTokenizer is a class used to tokenize text for BERT models. + * @extends PreTrainedTokenizer + */ +class BertTokenizer extends PreTrainedTokenizer { + return_token_type_ids = true; +} +/** + * Albert tokenizer + * @extends PreTrainedTokenizer + */ +class AlbertTokenizer extends PreTrainedTokenizer { + return_token_type_ids = true; +} +class MobileBertTokenizer extends PreTrainedTokenizer { + return_token_type_ids = true; +} +class SqueezeBertTokenizer extends PreTrainedTokenizer { + return_token_type_ids = true; +} +class DebertaTokenizer extends PreTrainedTokenizer { + return_token_type_ids = true; +} +class DebertaV2Tokenizer extends PreTrainedTokenizer { + return_token_type_ids = true; +} +class HerbertTokenizer extends PreTrainedTokenizer { + return_token_type_ids = true; +} +class ConvBertTokenizer extends PreTrainedTokenizer { + return_token_type_ids = true; +} +class RoFormerTokenizer extends PreTrainedTokenizer { + return_token_type_ids = true; +} +class DistilBertTokenizer extends PreTrainedTokenizer { } +class CamembertTokenizer extends PreTrainedTokenizer { } +class XLMTokenizer extends PreTrainedTokenizer { + return_token_type_ids = true; + + constructor(tokenizerJSON, tokenizerConfig) { + super(tokenizerJSON, tokenizerConfig); + console.warn('WARNING: `XLMTokenizer` is not yet supported by Hugging Face\'s "fast" tokenizers library. Therefore, you may experience slightly inaccurate results.') + } +} +class ElectraTokenizer extends PreTrainedTokenizer { + return_token_type_ids = true; +} + +class T5Tokenizer extends PreTrainedTokenizer { } +class GPT2Tokenizer extends PreTrainedTokenizer { } +class BartTokenizer extends PreTrainedTokenizer { } +class MBartTokenizer extends PreTrainedTokenizer { + constructor(tokenizerJSON, tokenizerConfig) { + super(tokenizerJSON, tokenizerConfig); + + this.languageRegex = /^[a-z]{2}_[A-Z]{2}$/; + this.language_codes = this.special_tokens.filter(x => this.languageRegex.test(x)); + this.lang_to_token = x => x; // Identity function + } + + /** + * Helper function to build translation inputs for an `MBartTokenizer`. + * @param {string|string[]} raw_inputs The text to tokenize. + * @param {Object} tokenizer_options Options to be sent to the tokenizer + * @param {Object} generate_kwargs Generation options. + * @returns {Object} Object to be passed to the model. + */ + _build_translation_inputs(raw_inputs, tokenizer_options, generate_kwargs) { + return _build_translation_inputs(this, raw_inputs, tokenizer_options, generate_kwargs); + } +} +class MBart50Tokenizer extends MBartTokenizer { } // NOTE: extends MBartTokenizer + +class RobertaTokenizer extends PreTrainedTokenizer { } + +class BloomTokenizer extends PreTrainedTokenizer { } + +const SPIECE_UNDERLINE = "▁"; + +class LlamaTokenizer extends PreTrainedTokenizer { + + padding_side = 'left'; + + constructor(tokenizerJSON, tokenizerConfig) { + super(tokenizerJSON, tokenizerConfig); + + this.legacy = tokenizerConfig.legacy ?? true; + if (!this.legacy) { + // See https://github.com/huggingface/transformers/pull/24565 for more information + this.normalizer = null; + this.pre_tokenizer = new MetaspacePreTokenizer({ + replacement: SPIECE_UNDERLINE, + add_prefix_space: true, + prepend_scheme: "first", + }); + } + } + + /** + * Helper function to handle legacy encoding of SPM tokenizers. + * Adapted from https://github.com/huggingface/transformers/blob/e6dcf8abd6f65bb4b6dfc1831b20d9ba49ce00e2/src/transformers/models/t5/tokenization_t5.py#L374-L387 + * @param {string} text The text to encode. + * @returns {string[]} The encoded tokens. + */ + _encode_text(text) { + if (text === null) return null; + + if (this.legacy || text.length === 0) { + return super._encode_text(text); + } + + let tokens = super._encode_text(SPIECE_UNDERLINE + text.replaceAll(SPIECE_UNDERLINE, " ")); + if (tokens.length > 1 && tokens[0] === SPIECE_UNDERLINE && this.special_tokens.includes(tokens[1])) { + tokens = tokens.slice(1); + } + return tokens; + } +} +class CodeLlamaTokenizer extends PreTrainedTokenizer { } + +class XLMRobertaTokenizer extends PreTrainedTokenizer { } +class MPNetTokenizer extends PreTrainedTokenizer { } + +class FalconTokenizer extends PreTrainedTokenizer { } + +class GPTNeoXTokenizer extends PreTrainedTokenizer { } + +class EsmTokenizer extends PreTrainedTokenizer { } + +class Qwen2Tokenizer extends PreTrainedTokenizer { } + +class GemmaTokenizer extends PreTrainedTokenizer { } + +class Grok1Tokenizer extends PreTrainedTokenizer { } + +/** + * Helper function to build translation inputs for an `NllbTokenizer` or `M2M100Tokenizer`. + * @param {PreTrainedTokenizer} self The tokenizer instance. + * @param {string|string[]} raw_inputs The text to tokenize. + * @param {Object} tokenizer_options Options to be sent to the tokenizer + * @param {Object} generate_kwargs Generation options. + * @returns {Object} Object to be passed to the model. + * @private + */ +function _build_translation_inputs(self, raw_inputs, tokenizer_options, generate_kwargs) { + if (!('language_codes' in self) || !Array.isArray(self.language_codes)) { + throw new Error('Tokenizer must have `language_codes` attribute set and it should be an array of language ids.') + } + if (!('languageRegex' in self) || !(self.languageRegex instanceof RegExp)) { + throw new Error('Tokenizer must have `languageRegex` attribute set and it should be a regular expression.') + } + if (!('lang_to_token' in self) || typeof self.lang_to_token !== 'function') { + throw new Error('Tokenizer must have `lang_to_token` attribute set and it should be a function.') + } + const src_lang_token = generate_kwargs.src_lang; + const tgt_lang_token = generate_kwargs.tgt_lang; + + // Check that the target language is valid: + if (!self.language_codes.includes(tgt_lang_token)) { + throw new Error(`Target language code "${tgt_lang_token}" is not valid. Must be one of: {${self.language_codes.join(', ')}}`); + } + + // Allow `src_lang` to be optional. If not set, we'll use the tokenizer's default. + if (src_lang_token !== undefined) { + // Check that the source language is valid: + if (!self.language_codes.includes(src_lang_token)) { + throw new Error(`Source language code "${src_lang_token}" is not valid. Must be one of: {${self.language_codes.join(', ')}}`); + } + + // In the same way as the Python library, we override the post-processor + // to force the source language to be first: + for (const item of self.post_processor.config.single) { + if ('SpecialToken' in item && self.languageRegex.test(item.SpecialToken.id)) { + item.SpecialToken.id = self.lang_to_token(src_lang_token); + break; + } + } + // TODO: Do the same for pair? + } + + // Override the `forced_bos_token_id` to force the correct language + generate_kwargs.forced_bos_token_id = self.model.convert_tokens_to_ids([self.lang_to_token(tgt_lang_token)])[0]; + + return self._call(raw_inputs, tokenizer_options); +} + +/** + * The NllbTokenizer class is used to tokenize text for NLLB ("No Language Left Behind") models. + * + * No Language Left Behind (NLLB) is a first-of-its-kind, AI breakthrough project + * that open-sources models capable of delivering high-quality translations directly + * between any pair of 200+ languages — including low-resource languages like Asturian, + * Luganda, Urdu and more. It aims to help people communicate with anyone, anywhere, + * regardless of their language preferences. For more information, check out their + * [paper](https://arxiv.org/abs/2207.04672). + * + * For a list of supported languages (along with their language codes), + * @see {@link https://github.com/facebookresearch/flores/blob/main/flores200/README.md#languages-in-flores-200} + */ +class NllbTokenizer extends PreTrainedTokenizer { + + constructor(tokenizerJSON, tokenizerConfig) { + super(tokenizerJSON, tokenizerConfig); + + this.languageRegex = /^[a-z]{3}_[A-Z][a-z]{3}$/; + this.language_codes = this.special_tokens.filter(x => this.languageRegex.test(x)); + this.lang_to_token = x => x; // Identity function + } + + /** + * Helper function to build translation inputs for an `NllbTokenizer`. + * @param {string|string[]} raw_inputs The text to tokenize. + * @param {Object} tokenizer_options Options to be sent to the tokenizer + * @param {Object} generate_kwargs Generation options. + * @returns {Object} Object to be passed to the model. + */ + _build_translation_inputs(raw_inputs, tokenizer_options, generate_kwargs) { + return _build_translation_inputs(this, raw_inputs, tokenizer_options, generate_kwargs); + } +} + +/** + * The M2M100Tokenizer class is used to tokenize text for M2M100 ("Many-to-Many") models. + * + * M2M100 is a multilingual encoder-decoder (seq-to-seq) model trained for Many-to-Many + * multilingual translation. It was introduced in this [paper](https://arxiv.org/abs/2010.11125) + * and first released in [this](https://github.com/pytorch/fairseq/tree/master/examples/m2m_100) repository. + * + * For a list of supported languages (along with their language codes), + * @see {@link https://huggingface.co/facebook/m2m100_418M#languages-covered} + */ +class M2M100Tokenizer extends PreTrainedTokenizer { + constructor(tokenizerJSON, tokenizerConfig) { + super(tokenizerJSON, tokenizerConfig); + + this.languageRegex = /^__[a-z]{2,3}__$/; + this.language_codes = this.special_tokens + .filter(x => this.languageRegex.test(x)) + .map(x => x.slice(2, -2)); + this.lang_to_token = x => `__${x}__`; + } + + /** + * Helper function to build translation inputs for an `M2M100Tokenizer`. + * @param {string|string[]} raw_inputs The text to tokenize. + * @param {Object} tokenizer_options Options to be sent to the tokenizer + * @param {Object} generate_kwargs Generation options. + * @returns {Object} Object to be passed to the model. + */ + _build_translation_inputs(raw_inputs, tokenizer_options, generate_kwargs) { + return _build_translation_inputs(this, raw_inputs, tokenizer_options, generate_kwargs); + } +} + +/** + * WhisperTokenizer tokenizer + * @extends PreTrainedTokenizer + */ +class WhisperTokenizer extends PreTrainedTokenizer { + + get timestamp_begin() { + return this.model.convert_tokens_to_ids(["<|notimestamps|>"])[0] + 1; + } + + /** + * Decodes automatic speech recognition (ASR) sequences. + * @param {Array<{tokens: bigint[], token_timestamps?: number[], stride: number[]}>} sequences The sequences to decode. + * @param {Object} options The options to use for decoding. + * @returns {Array, text: string}>}>} The decoded sequences. + */ + _decode_asr(sequences, { + return_timestamps = false, + return_language = false, + time_precision = null, + force_full_sequences = true + } = {}) { + // Set force_full_sequences=false if you want streaming + // TODO add support for `return_language` + + // Internal method meant to only be used by asr pipeline. + // Handles all the little quirks specific to whisper to handle + // the various options not allowed in other seq2seq models + + // =========== Overview ============ + // - iterate over all outputs + // - all tokens within output + // - Each token can be + // - language token + // - special token + // - timestamp token + // - text token + // - We accumulate the text tokens. + // - We split on end timestamps + // - Lots of complexity comes from stride and timestamps + + if (time_precision === null) { + throw Error("Must specify time_precision") + } + let last_language = null; + + const returnWordTimestamps = return_timestamps === "word"; + + function new_chunk() { + return { "language": last_language, "timestamp": [null, null], "text": "" }; + } + + // Welcome to the state machine! + const chunks = []; + let chunk = new_chunk(); + let time_offset = 0.0; + const timestamp_begin = this.timestamp_begin; + + let previous_tokens = []; + let previous_token_timestamps = []; + + let skip = false; + let right_stride_start = null; + + + const all_special_ids = new Set(this.all_special_ids); + + for (const output of sequences) { + // NOTE: python version has batches, so it uses [0] + const token_ids = output.tokens; + const token_timestamps = returnWordTimestamps ? output.token_timestamps : null; + + // These keep track of timestamps within strides, which need + // to be skipped and resolve all tokens in a single chunk. + let last_timestamp = null; + let first_timestamp = timestamp_begin; + + if ("stride" in output) { + const [chunk_len, stride_left, stride_right] = output.stride; + + // Offset the timings to account for the other `model_outputs`. + time_offset -= stride_left; + right_stride_start = chunk_len - stride_right; + + // Keeping track of timestamps within strides + // We're going to NOT split on those, and delay until we're + // out of BOTH stride. Otherwise lots of issues occur and + // corner cases + if (stride_left) { + first_timestamp = stride_left / time_precision + timestamp_begin; + } + + if (stride_right) { + for (let i = token_ids.length - 1; i >= 0; --i) { + const token = Number(token_ids[i]); + if (token >= timestamp_begin) { + // There can be several token in the right stride + // But the last one is ALWAYS going to be skipped + if (last_timestamp !== null && (token - timestamp_begin) * time_precision < right_stride_start) { + break; + } + last_timestamp = token; + } + } + } + } + + let current_tokens = []; + let current_token_timestamps = []; + + // - all tokens within output + for (let i = 0; i < token_ids.length; ++i) { + const token = Number(token_ids[i]); + // 4 possible states for each token + // - 1/ Language code + // - 2/ all other special tokens (which we ignore) + // - 3/ Timestamp + // - 4/ Regular text + + if (all_special_ids.has(token)) { + const text = this.decode([token]); + const language = _models_whisper_common_whisper_js__WEBPACK_IMPORTED_MODULE_7__.WHISPER_LANGUAGE_MAPPING.get(text.slice(2, -2)); + + if (language !== undefined) { + // 1/ Indeed some language + // TODO Handle when language is different from the previous + // one, and we cannot use timestamped tokens to create chunks + if (last_language !== null && language !== last_language && !return_timestamps) { + previous_tokens.push(current_tokens); + const resolved_tokens = this.findLongestCommonSequence(previous_tokens)[0]; + const resolved_text = this.decode(resolved_tokens); + chunk.text = resolved_text; + chunks.push(chunk); + + // Flush all our temporary context + previous_tokens = []; + current_tokens = []; + chunk = new_chunk(); + } + + last_language = chunk.language = language; + } else { + // 2/ This is a regular special token, ignoring it + } + } else if (token >= timestamp_begin) { + // 3/ Timestamp token + const time = (token - timestamp_begin) * time_precision + time_offset; + const rounded_time = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.round)(time, 2); + + if (last_timestamp !== null && token >= last_timestamp) { + // Whisper outputted a timestamp token, but it falls within + // our stride, so we're going to skip it for the time being + // and resolve this later + // Skip is necessary because timestamp tokens always come + // by pair, so we need to skip the next one too (which would mark the start of another chunk). + skip = true; + } else if (skip || (previous_tokens.length > 0 && token < first_timestamp)) { + skip = false; + } else if (chunk.timestamp[0] === null) { + chunk.timestamp[0] = rounded_time; + } else { + // This is the end of the timestamp chunk + if (rounded_time === chunk.timestamp[0]) { + // This is a bug in timestamp token output + // where we're taking the duplicate token + // as a stop where it should be a start. + // This is an issue in the underlying model output + // Let's just skip it so it becomes de-factor a start agin + } else { + chunk.timestamp[1] = rounded_time; + + // Handling merges + previous_tokens.push(current_tokens) + + if (returnWordTimestamps) { + previous_token_timestamps.push(current_token_timestamps); + } + const [resolved_tokens, resolved_token_timestamps] = this.findLongestCommonSequence( + previous_tokens, previous_token_timestamps + ) + + const resolved_text = this.decode(resolved_tokens) + chunk.text = resolved_text + + if (returnWordTimestamps) { + chunk.words = this.collateWordTimestamps( + resolved_tokens, resolved_token_timestamps, last_language, + ) + } + + chunks.push(chunk) + + // Flush all our temporary context + previous_tokens = [] + current_tokens = [] + previous_token_timestamps = [] + current_token_timestamps = [] + chunk = new_chunk() + } + } + + } else { + // 4/ Regular token + // We just append to the list of all tokens so we can handle + // merges later and decode into text. + current_tokens.push(token) + + if (returnWordTimestamps) { + let start_time = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.round)(token_timestamps[i] + time_offset, 2); + + let end_time; + if (i + 1 < token_timestamps.length) { + end_time = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.round)(token_timestamps[i + 1] + time_offset, 2); + + // Do not allow punctuation-only tokens to have a duration. + // This prevents long pauses from messing up the timestamps. + const decoded_text = this.decode([token]); + if (PUNCTUATION_ONLY_REGEX.test(decoded_text)) { + // Add `time_precision` to avoid overlapping timestamps + end_time = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.round)(Math.min(start_time + time_precision, end_time), 2); + } + } else { + // should never happen + end_time = null; + } + current_token_timestamps.push([start_time, end_time]); + } + + } + } + + if ('stride' in output) { + const [chunk_len, stride_left, stride_right] = output.stride; + time_offset += chunk_len - stride_right + } + + // Leftover tokens + if (current_tokens.length > 0) { + previous_tokens.push(current_tokens) + if (returnWordTimestamps) { + previous_token_timestamps.push(current_token_timestamps); + } + } else if (previous_tokens.every(p => p.length === 0)) { + // Flushing previous tokens (END)" + chunk = new_chunk() + previous_tokens = [] + current_tokens = [] + previous_token_timestamps = []; + current_token_timestamps = []; + } + + } + + if (previous_tokens.length > 0) { + if (force_full_sequences && return_timestamps) { + // Last token should always be timestamps, so there shouldn't be + // leftover + throw new Error( + "Whisper did not predict an ending timestamp, which can happen if audio is cut off in the middle of a word. " + + "Also make sure WhisperTimeStampLogitsProcessor was used during generation." + ); + } + + // Happens when we don't use timestamps + const [resolved_tokens, resolved_token_timestamps] = this.findLongestCommonSequence(previous_tokens, previous_token_timestamps); + + // Flushing previous tokens (FINAL) + const resolved_text = this.decode(resolved_tokens); + chunk.text = resolved_text; + if (returnWordTimestamps) { + chunk.words = this.collateWordTimestamps( + resolved_tokens, resolved_token_timestamps, last_language, + ) + } + chunks.push(chunk); + } + + let optional = Object.create(null); + + // Preparing and cleaning up the pipeline output + const full_text = chunks.map(chunk => chunk.text).join(''); + if (return_timestamps || return_language) { + for (let i = 0; i < chunks.length; ++i) { + const chunk = chunks[i]; + if (!return_timestamps) { + delete chunk["timestamp"]; + } + + if (!return_language) { + delete chunk["language"]; + } + } + if (returnWordTimestamps) { + const new_chunks = []; + for (const chunk of chunks) { + for (const word of chunk.words) { + new_chunks.push(word); + } + } + optional = { "chunks": new_chunks }; + } else { + optional = { "chunks": chunks }; + } + } + return [full_text, optional]; + + } + + /** + * Finds the longest common sequence among the provided sequences. + * @param {number[][]} sequences An array of sequences of token ids to compare. + * @returns {number[][]} The longest common sequence found. + * @throws {Error} If there is a bug within the function. + * @private + */ + findLongestCommonSequence(sequences, token_timestamp_sequences = null) { + // It would be much harder to do O(n) because of fault tolerance. + // We actually have a really good property which is that the total sequence + // MUST be those subsequences in order. + // If token_timestamp_sequences is provided, will split those sequences in + // exactly the same way. + let leftSequence = sequences[0]; + let leftLength = leftSequence.length; + let totalSequence = []; + + const use_token_timestamp_sequences = Array.isArray(token_timestamp_sequences) && token_timestamp_sequences.length > 0; + let total_token_timestamp_sequence = use_token_timestamp_sequences ? [] : null; + let left_token_timestamp_sequence = use_token_timestamp_sequences ? token_timestamp_sequences[0] : null; + for (let i = 1; i < sequences.length; ++i) { + const rightSequence = sequences[i]; + let max = 0.0; + let maxIndices = [leftLength, leftLength, 0, 0]; + // Here we're sliding matches + // [a, b, c, d] + // [c, d, f] + // = [c] == [d] + + // [a, b, c, d] + // [c, d, f] + // = [c, d] == [c, d] + + + // [a, b, c, d] + // [c, d, f] + + // = [b, c, d] == [c, d, f] + + // [a, b, c, d] + // [c, d, f] + + // [a, b, c] == [c, d, f] + + // [a, b, c, d] + // [d, f] + + // [a, b] == [d, f] + + // [a, b, c, d] + // [f] + + // [a] == [f] + + const rightLength = rightSequence.length; + for (let j = 1; j < leftLength + rightLength; ++j) { + // Slightly convoluted because we don't want out of bound indices + // This will be necessary for a small conflict resolution optimization + // later + const leftStart = Math.max(0, leftLength - j); + const leftStop = Math.min(leftLength, leftLength + rightLength - j); + const left = leftSequence.slice(leftStart, leftStop); + const rightStart = Math.max(0, j - leftLength); + const rightStop = Math.min(rightLength, j); + const right = rightSequence.slice(rightStart, rightStop); + if (left.length !== right.length) { + throw new Error("There is a bug within whisper `decode_asr` function, please report it. Dropping to prevent bad inference."); + } + + let matches; + if (use_token_timestamp_sequences) { + // Get length of longest subsequence of tokens that match + // and have timestamps that are in order + matches = left.filter((elem, idx) => ( + elem === right[idx] + && left_token_timestamp_sequence[leftStart + idx] <= token_timestamp_sequences[i][rightStart + idx] + )).length; + } else { + matches = left.filter((elem, idx) => elem === right[idx]).length; + } + + // epsilon to favor long perfect matches + const eps = j / 10000.0; + const matching = matches / j + eps; + if (matches > 1 && matching > max) { + max = matching; + maxIndices = [leftStart, leftStop, rightStart, rightStop]; + } + } + const [leftStart, leftStop, rightStart, rightStop] = maxIndices; + const leftMid = Math.floor((leftStop + leftStart) / 2); + const rightMid = Math.floor((rightStop + rightStart) / 2); + totalSequence.push(...leftSequence.slice(0, leftMid)); + leftSequence = rightSequence.slice(rightMid); + leftLength = leftSequence.length; + + if (use_token_timestamp_sequences) { + total_token_timestamp_sequence.push(...left_token_timestamp_sequence.slice(0, leftMid)); + left_token_timestamp_sequence = token_timestamp_sequences[i].slice(rightMid); + } + } + totalSequence.push(...leftSequence); + + if (use_token_timestamp_sequences) { + total_token_timestamp_sequence.push(...left_token_timestamp_sequence); + return [totalSequence, total_token_timestamp_sequence]; + } else { + return [totalSequence, []]; + } + } + + /** @private */ + collateWordTimestamps(tokens, token_timestamps, language) { + + const [words, _, token_indices] = this.combineTokensIntoWords(tokens, language); + + const timings = []; + for (let i = 0; i < words.length; ++i) { + const indices = token_indices[i]; + timings.push({ + text: words[i], + timestamp: [ + token_timestamps[indices.at(0)][0], + token_timestamps[indices.at(-1)][1], + ], + }); + } + return timings; + } + + /** + * Groups tokens by word. Returns a tuple containing a list of strings with the words, + * and a list of `token_id` sequences with the tokens making up each word. + * @param {number[]} tokens + * @param {string} [language] + * @param {string} prepend_punctionations + * @param {string} append_punctuations + * + * @private + */ + combineTokensIntoWords(tokens, language, prepend_punctionations = "\"'“¡¿([{-", append_punctuations = "\"'.。,,!!??::”)]}、") { + language = language ?? 'english'; + + let words, word_tokens, token_indices; + + if (["chinese", "japanese", "thai", "lao", "myanmar"].includes(language)) { + // These languages don't typically use spaces. + [words, word_tokens, token_indices] = this.splitTokensOnUnicode(tokens) + } else { + [words, word_tokens, token_indices] = this.splitTokensOnSpaces(tokens) + } + + return this.mergePunctuations(words, word_tokens, token_indices, prepend_punctionations, append_punctuations); + } + + /** @type {PreTrainedTokenizer['decode']} */ + decode( + token_ids, + decode_args, + ) { + let text; + // @ts-ignore + if (decode_args?.decode_with_timestamps) { + if (token_ids instanceof _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor) { + token_ids = prepareTensorForDecode(token_ids); + } + text = this.decodeWithTimestamps(token_ids, decode_args); + } else { + text = super.decode(token_ids, decode_args); + } + // TODO: implement offsets + // if (decode_args.output_offsets) { + // let offsets = this.computeOffsets + // } + return text; + } + + /** + * @param {number[]|bigint[]} token_ids List of token IDs to decode. + * @param {Object} decode_args Optional arguments for decoding + * @private + */ + decodeWithTimestamps(token_ids, decode_args) { + const time_precision = decode_args?.time_precision ?? 0.02; + + const timestamp_begin = Array.from(this.all_special_ids).at(-1) + 1; + /**@type {Array} */ + let outputs = [[]]; + for (let token of token_ids) { + token = Number(token); + if (token >= timestamp_begin) { + const timestamp = ((token - timestamp_begin) * time_precision).toFixed(2); + outputs.push(`<|${timestamp}|>`); + outputs.push([]); + } else { + outputs[outputs.length - 1].push(token); + } + } + outputs = outputs.map( + s => typeof s === 'string' ? s : super.decode(s, decode_args) + ) + + return outputs.join(''); + } + + /** + * Combine tokens into words by splitting at any position where the tokens are decoded as valid unicode points. + * @param {number[]} tokens + * @returns {*} + * @private + */ + splitTokensOnUnicode(tokens) { + const decoded_full = this.decode(tokens, { + // @ts-ignore + decode_with_timestamps: true, + }); + const replacement_char = '\uFFFD'; + + const words = [] + const word_tokens = [] + const token_indices = [] + let current_tokens = [] + let current_indices = [] + let unicode_offset = 0 + + for (let token_idx = 0; token_idx < tokens.length; ++token_idx) { + const token = tokens[token_idx]; + + current_tokens.push(token); + current_indices.push(token_idx); + + const decoded = this.decode(current_tokens, { + // @ts-ignore + decode_with_timestamps: true, + }); + + if (!decoded.includes(replacement_char) || decoded_full[unicode_offset + decoded.indexOf(replacement_char)] === replacement_char) { + words.push(decoded) + word_tokens.push(current_tokens) + token_indices.push(current_indices) + current_tokens = [] + current_indices = [] + unicode_offset += decoded.length; + } + + } + + return [words, word_tokens, token_indices] + } + + /** + * Combine tokens into words by splitting at whitespace and punctuation tokens. + * @param {number[]} tokens + * @private + */ + splitTokensOnSpaces(tokens) { + + const [subwords, subword_tokens_list, subword_indices_list] = this.splitTokensOnUnicode(tokens); + + const words = [] + const word_tokens = [] + const token_indices = [] + + const punctuationRegex = new RegExp(`^[${PUNCTUATION_REGEX}]$`, 'gu'); + + for (let i = 0; i < subwords.length; ++i) { + + const subword = subwords[i]; + const subword_tokens = subword_tokens_list[i]; + const subword_indices = subword_indices_list[i]; + + // @ts-ignore + const special = subword_tokens[0] >= this.model.tokens_to_ids.get('<|endoftext|>'); + const with_space = subword.startsWith(' '); + const trimmed = subword.trim(); + const punctuation = punctuationRegex.test(trimmed); + + if (special || with_space || punctuation || words.length === 0) { + words.push(subword); + word_tokens.push(subword_tokens); + token_indices.push(subword_indices); + } else { + const ix = words.length - 1; + words[ix] += subword; + word_tokens[ix].push(...subword_tokens); + token_indices[ix].push(...subword_indices); + } + } + + return [words, word_tokens, token_indices]; + + } + + /** + * Merges punctuation tokens with neighboring words. + * @param {string[]} words + * @param {number[][]} tokens + * @param {number[][]} indices + * @param {string} prepended + * @param {string} appended + * @private + */ + mergePunctuations(words, tokens, indices, prepended, appended) { + + const newWords = structuredClone(words); + const newTokens = structuredClone(tokens); + const newIndices = structuredClone(indices); + + + // prepend punctuations + let i = newWords.length - 2; + let j = newWords.length - 1; + + while (i >= 0) { + if (newWords[i].startsWith(' ') && prepended.includes(newWords[i].trim())) { + newWords[j] = newWords[i] + newWords[j]; + newTokens[j] = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(newTokens[i], newTokens[j]); + newIndices[j] = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(newIndices[i], newIndices[j]); + newWords[i] = ''; + newTokens[i] = []; + newIndices[i] = []; + } else { + j = i; + } + --i; + } + + // append punctuations + i = 0; + j = 1; + while (j < newWords.length) { + if (!newWords[i].endsWith(' ') && appended.includes(newWords[j])) { + newWords[i] += newWords[j]; + newTokens[i] = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(newTokens[i], newTokens[j]); + newIndices[i] = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(newIndices[i], newIndices[j]); + newWords[j] = ''; + newTokens[j] = []; + newIndices[j] = []; + } else { + i = j; + } + ++j; + } + + return [ + newWords.filter(x => x), + newTokens.filter(x => x.length > 0), + newIndices.filter(x => x.length > 0), + ] + } +} +class CodeGenTokenizer extends PreTrainedTokenizer { } +class CLIPTokenizer extends PreTrainedTokenizer { } +class SiglipTokenizer extends PreTrainedTokenizer { } + +/** + * @todo This model is not yet supported by Hugging Face's "fast" tokenizers library (https://github.com/huggingface/tokenizers). + * Therefore, this implementation (which is based on fast tokenizers) may produce slightly inaccurate results. + */ +class MarianTokenizer extends PreTrainedTokenizer { + /** + * Create a new MarianTokenizer instance. + * @param {Object} tokenizerJSON The JSON of the tokenizer. + * @param {Object} tokenizerConfig The config of the tokenizer. + */ + constructor(tokenizerJSON, tokenizerConfig) { + super(tokenizerJSON, tokenizerConfig); + + this.languageRegex = /^(>>\w+<<)\s*/g; + + this.supported_language_codes = this.model.vocab.filter( + x => this.languageRegex.test(x) + ); + + console.warn('WARNING: `MarianTokenizer` is not yet supported by Hugging Face\'s "fast" tokenizers library. Therefore, you may experience slightly inaccurate results.') + } + + /** + * Encodes a single text. Overriding this method is necessary since the language codes + * must be removed before encoding with sentencepiece model. + * @see https://github.com/huggingface/transformers/blob/12d51db243a00726a548a43cc333390ebae731e3/src/transformers/models/marian/tokenization_marian.py#L204-L213 + * + * @param {string|null} text The text to encode. + * @returns {Array} The encoded tokens. + */ + _encode_text(text) { + if (text === null) return null; + + // Check if text starts with language code: + const [matchInfo, ...remainder] = text.trim().split(this.languageRegex); + + if (remainder.length === 0) { + // No language code, encode normally + return super._encode_text(matchInfo); + + } else if (remainder.length === 2) { + // Text starts with language code, so we do not encode it with sentencepiece. + const [language, text] = remainder; + + if (!this.supported_language_codes.includes(language)) { + console.warn(`Unsupported language code "${language}" detected, which may lead to unexpected behavior. Should be one of: ${JSON.stringify(this.supported_language_codes)}`) + } + return (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)([language], super._encode_text(text)); + } + } + +} + +class Wav2Vec2CTCTokenizer extends PreTrainedTokenizer { } + +class BlenderbotTokenizer extends PreTrainedTokenizer { } +class BlenderbotSmallTokenizer extends PreTrainedTokenizer { } + +class SpeechT5Tokenizer extends PreTrainedTokenizer { } + +class NougatTokenizer extends PreTrainedTokenizer { } + +class VitsTokenizer extends PreTrainedTokenizer { + + constructor(tokenizerJSON, tokenizerConfig) { + super(tokenizerJSON, tokenizerConfig); + + // Custom decoder function + this.decoder = new VitsDecoder({}); + } +} + +class CohereTokenizer extends PreTrainedTokenizer { } + +/** + * Helper class which is used to instantiate pretrained tokenizers with the `from_pretrained` function. + * The chosen tokenizer class is determined by the type specified in the tokenizer config. + * + * @example + * const tokenizer = await AutoTokenizer.from_pretrained('Xenova/bert-base-uncased'); + */ +class AutoTokenizer { + static TOKENIZER_CLASS_MAPPING = { + T5Tokenizer, + DistilBertTokenizer, + CamembertTokenizer, + DebertaTokenizer, + DebertaV2Tokenizer, + BertTokenizer, + HerbertTokenizer, + ConvBertTokenizer, + RoFormerTokenizer, + XLMTokenizer, + ElectraTokenizer, + MobileBertTokenizer, + SqueezeBertTokenizer, + AlbertTokenizer, + GPT2Tokenizer, + BartTokenizer, + MBartTokenizer, + MBart50Tokenizer, + RobertaTokenizer, + WhisperTokenizer, + CodeGenTokenizer, + CLIPTokenizer, + SiglipTokenizer, + MarianTokenizer, + BloomTokenizer, + NllbTokenizer, + M2M100Tokenizer, + LlamaTokenizer, + CodeLlamaTokenizer, + XLMRobertaTokenizer, + MPNetTokenizer, + FalconTokenizer, + GPTNeoXTokenizer, + EsmTokenizer, + Wav2Vec2CTCTokenizer, + BlenderbotTokenizer, + BlenderbotSmallTokenizer, + SpeechT5Tokenizer, + NougatTokenizer, + VitsTokenizer, + Qwen2Tokenizer, + GemmaTokenizer, + Grok1Tokenizer, + CohereTokenizer, + + // Base case: + PreTrainedTokenizer, + } + + + /** + * Instantiate one of the tokenizer classes of the library from a pretrained model. + * + * The tokenizer class to instantiate is selected based on the `tokenizer_class` property of the config object + * (either passed as an argument or loaded from `pretrained_model_name_or_path` if possible) + * + * @param {string} pretrained_model_name_or_path The name or path of the pretrained model. Can be either: + * - A string, the *model id* of a pretrained tokenizer hosted inside a model repo on huggingface.co. + * Valid model ids can be located at the root-level, like `bert-base-uncased`, or namespaced under a + * user or organization name, like `dbmdz/bert-base-german-cased`. + * - A path to a *directory* containing tokenizer files, e.g., `./my_model_directory/`. + * @param {PretrainedTokenizerOptions} options Additional options for loading the tokenizer. + * + * @returns {Promise} A new instance of the PreTrainedTokenizer class. + */ + static async from_pretrained(pretrained_model_name_or_path, { + progress_callback = null, + config = null, + cache_dir = null, + local_files_only = false, + revision = 'main', + legacy = null, + } = {}) { + + const [tokenizerJSON, tokenizerConfig] = await loadTokenizer(pretrained_model_name_or_path, { + progress_callback, + config, + cache_dir, + local_files_only, + revision, + legacy, + }) + + // Some tokenizers are saved with the "Fast" suffix, so we remove that if present. + const tokenizerName = tokenizerConfig.tokenizer_class?.replace(/Fast$/, '') ?? 'PreTrainedTokenizer'; + + let cls = this.TOKENIZER_CLASS_MAPPING[tokenizerName]; + if (!cls) { + console.warn(`Unknown tokenizer class "${tokenizerName}", attempting to construct from base class.`); + cls = PreTrainedTokenizer; + } + return new cls(tokenizerJSON, tokenizerConfig); + } +} + + +/***/ }), + +/***/ "./src/utils/audio.js": +/*!****************************!*\ + !*** ./src/utils/audio.js ***! + \****************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ hamming: () => (/* binding */ hamming), +/* harmony export */ hanning: () => (/* binding */ hanning), +/* harmony export */ mel_filter_bank: () => (/* binding */ mel_filter_bank), +/* harmony export */ read_audio: () => (/* binding */ read_audio), +/* harmony export */ spectrogram: () => (/* binding */ spectrogram), +/* harmony export */ window_function: () => (/* binding */ window_function) +/* harmony export */ }); +/* harmony import */ var _hub_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./hub.js */ "./src/utils/hub.js"); +/* harmony import */ var _maths_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./maths.js */ "./src/utils/maths.js"); +/* harmony import */ var _core_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./core.js */ "./src/utils/core.js"); +/* harmony import */ var _tensor_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./tensor.js */ "./src/utils/tensor.js"); +/** + * @file Helper module for audio processing. + * + * These functions and classes are only used internally, + * meaning an end-user shouldn't need to access anything here. + * + * @module utils/audio + */ + + + + + + + +/** + * Helper function to read audio from a path/URL. + * @param {string|URL} url The path/URL to load the audio from. + * @param {number} sampling_rate The sampling rate to use when decoding the audio. + * @returns {Promise} The decoded audio as a `Float32Array`. + */ +async function read_audio(url, sampling_rate) { + if (typeof AudioContext === 'undefined') { + // Running in node or an environment without AudioContext + throw Error( + "Unable to load audio from path/URL since `AudioContext` is not available in your environment. " + + "Instead, audio data should be passed directly to the pipeline/processor. " + + "For more information and some example code, see https://huggingface.co/docs/transformers.js/guides/node-audio-processing." + ) + } + + const response = await (await (0,_hub_js__WEBPACK_IMPORTED_MODULE_0__.getFile)(url)).arrayBuffer(); + const audioCTX = new AudioContext({ sampleRate: sampling_rate }); + if (typeof sampling_rate === 'undefined') { + console.warn(`No sampling rate provided, using default of ${audioCTX.sampleRate}Hz.`) + } + const decoded = await audioCTX.decodeAudioData(response); + + /** @type {Float32Array} */ + let audio; + + // We now replicate HuggingFace's `ffmpeg_read` method: + if (decoded.numberOfChannels === 2) { + // When downmixing a stereo audio file to mono using the -ac 1 option in FFmpeg, + // the audio signal is summed across both channels to create a single mono channel. + // However, if the audio is at full scale (i.e. the highest possible volume level), + // the summing of the two channels can cause the audio signal to clip or distort. + + // To prevent this clipping, FFmpeg applies a scaling factor of 1/sqrt(2) (~ 0.707) + // to the audio signal before summing the two channels. This scaling factor ensures + // that the combined audio signal will not exceed the maximum possible level, even + // if both channels are at full scale. + + // After applying this scaling factor, the audio signal from both channels is summed + // to create a single mono channel. It's worth noting that this scaling factor is + // only applied when downmixing stereo audio to mono using the -ac 1 option in FFmpeg. + // If you're using a different downmixing method, or if you're not downmixing the + // audio at all, this scaling factor may not be needed. + const SCALING_FACTOR = Math.sqrt(2); + + const left = decoded.getChannelData(0); + const right = decoded.getChannelData(1); + + audio = new Float32Array(left.length); + for (let i = 0; i < decoded.length; ++i) { + audio[i] = SCALING_FACTOR * (left[i] + right[i]) / 2; + } + + } else { + // If the audio is not stereo, we can just use the first channel: + audio = decoded.getChannelData(0); + } + + return audio; +} + +/** + * Helper function to generate windows that are special cases of the generalized cosine window. + * See https://www.mathworks.com/help/signal/ug/generalized-cosine-windows.html for more information. + * @param {number} M Number of points in the output window. If zero or less, an empty array is returned. + * @param {number} a_0 Offset for the generalized cosine window. + * @returns {Float64Array} The generated window. + */ +function generalized_cosine_window(M, a_0) { + if (M < 1) { + return new Float64Array(); + } + if (M === 1) { + return new Float64Array([1]); + } + + const a_1 = 1 - a_0; + const factor = 2 * Math.PI / (M - 1); + + const cos_vals = new Float64Array(M); + for (let i = 0; i < M; ++i) { + cos_vals[i] = a_0 - a_1 * Math.cos(i * factor); + } + return cos_vals; +} + +/** + * Generates a Hanning window of length M. + * See https://numpy.org/doc/stable/reference/generated/numpy.hanning.html for more information. + * + * @param {number} M The length of the Hanning window to generate. + * @returns {Float64Array} The generated Hanning window. + */ +function hanning(M) { + return generalized_cosine_window(M, 0.5); +} + + +/** + * Generates a Hamming window of length M. + * See https://numpy.org/doc/stable/reference/generated/numpy.hamming.html for more information. + * + * @param {number} M The length of the Hamming window to generate. + * @returns {Float64Array} The generated Hamming window. + */ +function hamming(M) { + return generalized_cosine_window(M, 0.54); +} + + +const HERTZ_TO_MEL_MAPPING = { + "htk": (/** @type {number} */ freq) => 2595.0 * Math.log10(1.0 + (freq / 700.0)), + "kaldi": (/** @type {number} */ freq) => 1127.0 * Math.log(1.0 + (freq / 700.0)), + "slaney": (/** @type {number} */ freq, min_log_hertz = 1000.0, min_log_mel = 15.0, logstep = 27.0 / Math.log(6.4)) => + freq >= min_log_hertz + ? min_log_mel + Math.log(freq / min_log_hertz) * logstep + : 3.0 * freq / 200.0, +} + +/** + * @template {Float32Array|Float64Array|number} T + * @param {T} freq + * @param {string} [mel_scale] + * @returns {T} + */ +function hertz_to_mel(freq, mel_scale = "htk") { + const fn = HERTZ_TO_MEL_MAPPING[mel_scale]; + if (!fn) { + throw new Error('mel_scale should be one of "htk", "slaney" or "kaldi".'); + } + + return typeof freq === 'number' ? fn(freq) : freq.map(x => fn(x)); +} + +const MEL_TO_HERTZ_MAPPING = { + "htk": (/** @type {number} */ mels) => 700.0 * (10.0 ** (mels / 2595.0) - 1.0), + "kaldi": (/** @type {number} */ mels) => 700.0 * (Math.exp(mels / 1127.0) - 1.0), + "slaney": (/** @type {number} */ mels, min_log_hertz = 1000.0, min_log_mel = 15.0, logstep = Math.log(6.4) / 27.0) => mels >= min_log_mel + ? min_log_hertz * Math.exp(logstep * (mels - min_log_mel)) + : 200.0 * mels / 3.0, +} + +/** + * @template {Float32Array|Float64Array|number} T + * @param {T} mels + * @param {string} [mel_scale] + * @returns {T} + */ +function mel_to_hertz(mels, mel_scale = "htk") { + const fn = MEL_TO_HERTZ_MAPPING[mel_scale]; + if (!fn) { + throw new Error('mel_scale should be one of "htk", "slaney" or "kaldi".'); + } + + return typeof mels === 'number' ? fn(mels) : mels.map(x => fn(x)); +} + +/** +* Creates a triangular filter bank. +* +* Adapted from torchaudio and librosa. +* +* @param {Float64Array} fft_freqs Discrete frequencies of the FFT bins in Hz, of shape `(num_frequency_bins,)`. +* @param {Float64Array} filter_freqs Center frequencies of the triangular filters to create, in Hz, of shape `(num_mel_filters,)`. +* @returns {number[][]} of shape `(num_frequency_bins, num_mel_filters)`. +*/ +function _create_triangular_filter_bank(fft_freqs, filter_freqs) { + const filter_diff = Float64Array.from( + { length: filter_freqs.length - 1 }, + (_, i) => filter_freqs[i + 1] - filter_freqs[i] + ); + + const slopes = Array.from({ + length: fft_freqs.length + }, () => new Array(filter_freqs.length)); + + for (let j = 0; j < fft_freqs.length; ++j) { + const slope = slopes[j]; + for (let i = 0; i < filter_freqs.length; ++i) { + slope[i] = filter_freqs[i] - fft_freqs[j]; + } + } + + const numFreqs = filter_freqs.length - 2; + const ret = Array.from({ length: numFreqs }, () => new Array(fft_freqs.length)); + + for (let j = 0; j < fft_freqs.length; ++j) { // 201 + const slope = slopes[j]; + for (let i = 0; i < numFreqs; ++i) { // 80 + const down = -slope[i] / filter_diff[i]; + const up = slope[i + 2] / filter_diff[i + 1]; + ret[i][j] = Math.max(0, Math.min(down, up)); + } + } + return ret; +} + +/** + * Return evenly spaced numbers over a specified interval. + * @param {number} start The starting value of the sequence. + * @param {number} end The end value of the sequence. + * @param {number} num Number of samples to generate. + * @returns `num` evenly spaced samples, calculated over the interval `[start, stop]`. + */ +function linspace(start, end, num) { + const step = (end - start) / (num - 1); + return Float64Array.from({ length: num }, (_, i) => start + step * i); +} + +/** + * Creates a frequency bin conversion matrix used to obtain a mel spectrogram. This is called a *mel filter bank*, and + * various implementation exist, which differ in the number of filters, the shape of the filters, the way the filters + * are spaced, the bandwidth of the filters, and the manner in which the spectrum is warped. The goal of these + * features is to approximate the non-linear human perception of the variation in pitch with respect to the frequency. + * @param {number} num_frequency_bins Number of frequencies used to compute the spectrogram (should be the same as in `stft`). + * @param {number} num_mel_filters Number of mel filters to generate. + * @param {number} min_frequency Lowest frequency of interest in Hz. + * @param {number} max_frequency Highest frequency of interest in Hz. This should not exceed `sampling_rate / 2`. + * @param {number} sampling_rate Sample rate of the audio waveform. + * @param {string} [norm] If `"slaney"`, divide the triangular mel weights by the width of the mel band (area normalization). + * @param {string} [mel_scale] The mel frequency scale to use, `"htk"` or `"slaney"`. + * @param {boolean} [triangularize_in_mel_space] If this option is enabled, the triangular filter is applied in mel space rather than frequency space. + * This should be set to `true` in order to get the same results as `torchaudio` when computing mel filters. + * @returns {number[][]} Triangular filter bank matrix, which is a 2D array of shape (`num_frequency_bins`, `num_mel_filters`). + * This is a projection matrix to go from a spectrogram to a mel spectrogram. + */ +function mel_filter_bank( + num_frequency_bins, + num_mel_filters, + min_frequency, + max_frequency, + sampling_rate, + norm = null, + mel_scale = "htk", + triangularize_in_mel_space = false, +) { + if (norm !== null && norm !== "slaney") { + throw new Error('norm must be one of null or "slaney"'); + } + + const mel_min = hertz_to_mel(min_frequency, mel_scale); + const mel_max = hertz_to_mel(max_frequency, mel_scale); + const mel_freqs = linspace(mel_min, mel_max, num_mel_filters + 2); + + let filter_freqs = mel_to_hertz(mel_freqs, mel_scale); + let fft_freqs; // frequencies of FFT bins in Hz + + if (triangularize_in_mel_space) { + const fft_bin_width = sampling_rate / (num_frequency_bins * 2); + fft_freqs = hertz_to_mel(Float64Array.from({ length: num_frequency_bins }, (_, i) => i * fft_bin_width), mel_scale); + filter_freqs = mel_freqs; + } else { + fft_freqs = linspace(0, Math.floor(sampling_rate / 2), num_frequency_bins); + } + + const mel_filters = _create_triangular_filter_bank(fft_freqs, filter_freqs); + + if (norm !== null && norm === "slaney") { + // Slaney-style mel is scaled to be approx constant energy per channel + for (let i = 0; i < num_mel_filters; ++i) { + const filter = mel_filters[i]; + const enorm = 2.0 / (filter_freqs[i + 2] - filter_freqs[i]); + for (let j = 0; j < num_frequency_bins; ++j) { + // Apply this enorm to all frequency bins + filter[j] *= enorm; + } + } + } + + // TODO warn if there is a zero row + + return mel_filters; + +} + +/** + * @template {Float32Array|Float64Array} T + * Pads an array with a reflected version of itself on both ends. + * @param {T} array The array to pad. + * @param {number} left The amount of padding to add to the left. + * @param {number} right The amount of padding to add to the right. + * @returns {T} The padded array. + */ +function padReflect(array, left, right) { + // @ts-ignore + const padded = new array.constructor(array.length + left + right); + const w = array.length - 1; + + for (let i = 0; i < array.length; ++i) { + padded[left + i] = array[i]; + } + + for (let i = 1; i <= left; ++i) { + padded[left - i] = array[(0,_core_js__WEBPACK_IMPORTED_MODULE_2__.calculateReflectOffset)(i, w)]; + } + + for (let i = 1; i <= right; ++i) { + padded[w + left + i] = array[(0,_core_js__WEBPACK_IMPORTED_MODULE_2__.calculateReflectOffset)(w - i, w)]; + } + + return padded; +} + +/** + * Helper function to compute `amplitude_to_db` and `power_to_db`. + * @template {Float32Array|Float64Array} T + * @param {T} spectrogram + * @param {number} factor + * @param {number} reference + * @param {number} min_value + * @param {number} db_range + * @returns {T} + */ +function _db_conversion_helper(spectrogram, factor, reference, min_value, db_range) { + if (reference <= 0) { + throw new Error('reference must be greater than zero'); + } + + if (min_value <= 0) { + throw new Error('min_value must be greater than zero'); + } + + reference = Math.max(min_value, reference); + + const logReference = Math.log10(reference); + for (let i = 0; i < spectrogram.length; ++i) { + spectrogram[i] = factor * Math.log10(Math.max(min_value, spectrogram[i]) - logReference) + } + + if (db_range !== null) { + if (db_range <= 0) { + throw new Error('db_range must be greater than zero'); + } + const maxValue = (0,_maths_js__WEBPACK_IMPORTED_MODULE_1__.max)(spectrogram)[0] - db_range; + for (let i = 0; i < spectrogram.length; ++i) { + spectrogram[i] = Math.max(spectrogram[i], maxValue); + } + } + + return spectrogram; +} + +/** + * Converts an amplitude spectrogram to the decibel scale. This computes `20 * log10(spectrogram / reference)`, + * using basic logarithm properties for numerical stability. NOTE: Operates in-place. + * + * The motivation behind applying the log function on the (mel) spectrogram is that humans do not hear loudness on a + * linear scale. Generally to double the perceived volume of a sound we need to put 8 times as much energy into it. + * This means that large variations in energy may not sound all that different if the sound is loud to begin with. + * This compression operation makes the (mel) spectrogram features match more closely what humans actually hear. + * + * @template {Float32Array|Float64Array} T + * @param {T} spectrogram The input amplitude (mel) spectrogram. + * @param {number} [reference=1.0] Sets the input spectrogram value that corresponds to 0 dB. + * For example, use `np.max(spectrogram)` to set the loudest part to 0 dB. Must be greater than zero. + * @param {number} [min_value=1e-5] The spectrogram will be clipped to this minimum value before conversion to decibels, + * to avoid taking `log(0)`. The default of `1e-5` corresponds to a minimum of -100 dB. Must be greater than zero. + * @param {number} [db_range=null] Sets the maximum dynamic range in decibels. For example, if `db_range = 80`, the + * difference between the peak value and the smallest value will never be more than 80 dB. Must be greater than zero. + * @returns {T} The modified spectrogram in decibels. + */ +function amplitude_to_db(spectrogram, reference = 1.0, min_value = 1e-5, db_range = null) { + return _db_conversion_helper(spectrogram, 20.0, reference, min_value, db_range); +} + +/** + * Converts a power spectrogram to the decibel scale. This computes `10 * log10(spectrogram / reference)`, + * using basic logarithm properties for numerical stability. NOTE: Operates in-place. + * + * The motivation behind applying the log function on the (mel) spectrogram is that humans do not hear loudness on a + * linear scale. Generally to double the perceived volume of a sound we need to put 8 times as much energy into it. + * This means that large variations in energy may not sound all that different if the sound is loud to begin with. + * This compression operation makes the (mel) spectrogram features match more closely what humans actually hear. + * + * Based on the implementation of `librosa.power_to_db`. + * + * @template {Float32Array|Float64Array} T + * @param {T} spectrogram The input power (mel) spectrogram. Note that a power spectrogram has the amplitudes squared! + * @param {number} [reference=1.0] Sets the input spectrogram value that corresponds to 0 dB. + * For example, use `np.max(spectrogram)` to set the loudest part to 0 dB. Must be greater than zero. + * @param {number} [min_value=1e-10] The spectrogram will be clipped to this minimum value before conversion to decibels, + * to avoid taking `log(0)`. The default of `1e-10` corresponds to a minimum of -100 dB. Must be greater than zero. + * @param {number} [db_range=null] Sets the maximum dynamic range in decibels. For example, if `db_range = 80`, the + * difference between the peak value and the smallest value will never be more than 80 dB. Must be greater than zero. + * @returns {T} The modified spectrogram in decibels. + */ +function power_to_db(spectrogram, reference = 1.0, min_value = 1e-10, db_range = null) { + return _db_conversion_helper(spectrogram, 10.0, reference, min_value, db_range); +} + +/** + * Calculates a spectrogram over one waveform using the Short-Time Fourier Transform. + * + * This function can create the following kinds of spectrograms: + * - amplitude spectrogram (`power = 1.0`) + * - power spectrogram (`power = 2.0`) + * - complex-valued spectrogram (`power = None`) + * - log spectrogram (use `log_mel` argument) + * - mel spectrogram (provide `mel_filters`) + * - log-mel spectrogram (provide `mel_filters` and `log_mel`) + * + * In this implementation, the window is assumed to be zero-padded to have the same size as the analysis frame. + * A padded window can be obtained from `window_function()`. The FFT input buffer may be larger than the analysis frame, + * typically the next power of two. + * + * @param {Float32Array|Float64Array} waveform The input waveform of shape `(length,)`. This must be a single real-valued, mono waveform. + * @param {Float32Array|Float64Array} window The windowing function to apply of shape `(frame_length,)`, including zero-padding if necessary. The actual window length may be + * shorter than `frame_length`, but we're assuming the array has already been zero-padded. + * @param {number} frame_length The length of the analysis frames in samples (a.k.a., `fft_length`). + * @param {number} hop_length The stride between successive analysis frames in samples. + * @param {Object} options + * @param {number} [options.fft_length=null] The size of the FFT buffer in samples. This determines how many frequency bins the spectrogram will have. + * For optimal speed, this should be a power of two. If `null`, uses `frame_length`. + * @param {number} [options.power=1.0] If 1.0, returns the amplitude spectrogram. If 2.0, returns the power spectrogram. If `null`, returns complex numbers. + * @param {boolean} [options.center=true] Whether to pad the waveform so that frame `t` is centered around time `t * hop_length`. If `false`, frame + * `t` will start at time `t * hop_length`. + * @param {string} [options.pad_mode="reflect"] Padding mode used when `center` is `true`. Possible values are: `"constant"` (pad with zeros), + * `"edge"` (pad with edge values), `"reflect"` (pads with mirrored values). + * @param {boolean} [options.onesided=true] If `true`, only computes the positive frequencies and returns a spectrogram containing `fft_length // 2 + 1` + * frequency bins. If `false`, also computes the negative frequencies and returns `fft_length` frequency bins. + * @param {number} [options.preemphasis=null] Coefficient for a low-pass filter that applies pre-emphasis before the DFT. + * @param {number[][]} [options.mel_filters=null] The mel filter bank of shape `(num_freq_bins, num_mel_filters)`. + * If supplied, applies this filter bank to create a mel spectrogram. + * @param {number} [options.mel_floor=1e-10] Minimum value of mel frequency banks. + * @param {string} [options.log_mel=null] How to convert the spectrogram to log scale. Possible options are: + * `null` (don't convert), `"log"` (take the natural logarithm) `"log10"` (take the base-10 logarithm), `"dB"` (convert to decibels). + * Can only be used when `power` is not `null`. + * @param {number} [options.reference=1.0] Sets the input spectrogram value that corresponds to 0 dB. For example, use `max(spectrogram)[0]` to set + * the loudest part to 0 dB. Must be greater than zero. + * @param {number} [options.min_value=1e-10] The spectrogram will be clipped to this minimum value before conversion to decibels, to avoid taking `log(0)`. + * For a power spectrogram, the default of `1e-10` corresponds to a minimum of -100 dB. For an amplitude spectrogram, the value `1e-5` corresponds to -100 dB. + * Must be greater than zero. + * @param {number} [options.db_range=null] Sets the maximum dynamic range in decibels. For example, if `db_range = 80`, the difference between the + * peak value and the smallest value will never be more than 80 dB. Must be greater than zero. + * @param {boolean} [options.remove_dc_offset=null] Subtract mean from waveform on each frame, applied before pre-emphasis. This should be set to `true` in + * order to get the same results as `torchaudio.compliance.kaldi.fbank` when computing mel filters. + * @param {number} [options.max_num_frames=null] If provided, limits the number of frames to compute to this value. + * @param {number} [options.min_num_frames=null] If provided, ensures the number of frames to compute is at least this value. + * @param {boolean} [options.do_pad=true] If `true`, pads the output spectrogram to have `max_num_frames` frames. + * @param {boolean} [options.transpose=false] If `true`, the returned spectrogram will have shape `(num_frames, num_frequency_bins/num_mel_filters)`. If `false`, the returned spectrogram will have shape `(num_frequency_bins/num_mel_filters, num_frames)`. + * @returns {Promise} Spectrogram of shape `(num_frequency_bins, length)` (regular spectrogram) or shape `(num_mel_filters, length)` (mel spectrogram). + */ +async function spectrogram( + waveform, + window, + frame_length, + hop_length, + { + fft_length = null, + power = 1.0, + center = true, + pad_mode = "reflect", + onesided = true, + preemphasis = null, + mel_filters = null, + mel_floor = 1e-10, + log_mel = null, + reference = 1.0, + min_value = 1e-10, + db_range = null, + remove_dc_offset = null, + + // Custom parameters for efficiency reasons + min_num_frames = null, + max_num_frames = null, + do_pad = true, + transpose = false, + } = {} +) { + const window_length = window.length; + if (fft_length === null) { + fft_length = frame_length; + } + if (frame_length > fft_length) { + throw Error(`frame_length (${frame_length}) may not be larger than fft_length (${fft_length})`) + } + + if (window_length !== frame_length) { + throw new Error(`Length of the window (${window_length}) must equal frame_length (${frame_length})`); + } + + if (hop_length <= 0) { + throw new Error("hop_length must be greater than zero"); + } + + if (power === null && mel_filters !== null) { + throw new Error( + "You have provided `mel_filters` but `power` is `None`. Mel spectrogram computation is not yet supported for complex-valued spectrogram. " + + "Specify `power` to fix this issue." + ); + } + + if (center) { + if (pad_mode !== 'reflect') { + throw new Error(`pad_mode="${pad_mode}" not implemented yet.`) + } + const half_window = Math.floor((fft_length - 1) / 2) + 1; + waveform = padReflect(waveform, half_window, half_window); + } + + // split waveform into frames of frame_length size + let num_frames = Math.floor(1 + Math.floor((waveform.length - frame_length) / hop_length)) + if (min_num_frames !== null && num_frames < min_num_frames) { + num_frames = min_num_frames + } + const num_frequency_bins = onesided ? Math.floor(fft_length / 2) + 1 : fft_length + + let d1 = num_frames; + let d1Max = num_frames; + + // If maximum number of frames is provided, we must either pad or truncate + if (max_num_frames !== null) { + if (max_num_frames > num_frames) { // input is too short, so we pad + if (do_pad) { + d1Max = max_num_frames; + } + } else { // input is too long, so we truncate + d1Max = d1 = max_num_frames; + } + } + + // Preallocate arrays to store output. + const fft = new _maths_js__WEBPACK_IMPORTED_MODULE_1__.FFT(fft_length); + const inputBuffer = new Float64Array(fft_length); + const outputBuffer = new Float64Array(fft.outputBufferSize); + const transposedMagnitudeData = new Float32Array(num_frequency_bins * d1Max); + + for (let i = 0; i < d1; ++i) { + // Populate buffer with waveform data + const offset = i * hop_length; + const buffer_size = Math.min(waveform.length - offset, frame_length); + if (buffer_size !== frame_length) { + // The full buffer is not needed, so we need to reset it (avoid overflow from previous iterations) + // NOTE: We don't need to reset the buffer if it's full since we overwrite the first + // `frame_length` values and the rest (`fft_length - frame_length`) remains zero. + inputBuffer.fill(0, 0, frame_length); + } + + for (let j = 0; j < buffer_size; ++j) { + inputBuffer[j] = waveform[offset + j]; + } + + if (remove_dc_offset) { + let sum = 0; + for (let j = 0; j < buffer_size; ++j) { + sum += inputBuffer[j]; + } + const mean = sum / buffer_size; + for (let j = 0; j < buffer_size; ++j) { + inputBuffer[j] -= mean; + } + } + + if (preemphasis !== null) { + // Done in reverse to avoid copies and distructive modification + for (let j = buffer_size - 1; j >= 1; --j) { + inputBuffer[j] -= preemphasis * inputBuffer[j - 1]; + } + inputBuffer[0] *= 1 - preemphasis; + } + + // Apply window function + for (let j = 0; j < window.length; ++j) { + inputBuffer[j] *= window[j]; + } + + fft.realTransform(outputBuffer, inputBuffer); + + // compute magnitudes + for (let j = 0; j < num_frequency_bins; ++j) { + const j2 = j << 1; + + // NOTE: We transpose the data here to avoid doing it later + transposedMagnitudeData[j * d1Max + i] = outputBuffer[j2] ** 2 + outputBuffer[j2 + 1] ** 2; + } + } + + if (power !== null && power !== 2) { + // slight optimization to not sqrt + const pow = 2 / power; // we use 2 since we already squared + for (let i = 0; i < transposedMagnitudeData.length; ++i) { + transposedMagnitudeData[i] **= pow; + } + } + + // TODO: What if `mel_filters` is null? + const num_mel_filters = mel_filters.length; + + // Perform matrix muliplication: + // mel_spec = mel_filters @ magnitudes.T + // - mel_filters.shape=(80, 201) + // - magnitudes.shape=(3000, 201) => magnitudes.T.shape=(201, 3000) + // - mel_spec.shape=(80, 3000) + let mel_spec = await (0,_tensor_js__WEBPACK_IMPORTED_MODULE_3__.matmul)( + // TODO: Make `mel_filters` a Tensor during initialization + new _tensor_js__WEBPACK_IMPORTED_MODULE_3__.Tensor('float32', mel_filters.flat(), [num_mel_filters, num_frequency_bins]), + new _tensor_js__WEBPACK_IMPORTED_MODULE_3__.Tensor('float32', transposedMagnitudeData, [num_frequency_bins, d1Max]), + ); + if (transpose) { + mel_spec = mel_spec.transpose(1, 0); + } + + const mel_spec_data = /** @type {Float32Array} */(mel_spec.data); + for (let i = 0; i < mel_spec_data.length; ++i) { + mel_spec_data[i] = Math.max(mel_floor, mel_spec_data[i]); + } + + if (power !== null && log_mel !== null) { + const o = Math.min(mel_spec_data.length, d1 * num_mel_filters); + // NOTE: operates in-place + switch (log_mel) { + case 'log': + for (let i = 0; i < o; ++i) { + mel_spec_data[i] = Math.log(mel_spec_data[i]); + } + break; + case 'log10': + for (let i = 0; i < o; ++i) { + mel_spec_data[i] = Math.log10(mel_spec_data[i]); + } + break; + case 'dB': + if (power === 1.0) { + amplitude_to_db(mel_spec_data, reference, min_value, db_range); + } else if (power === 2.0) { + power_to_db(mel_spec_data, reference, min_value, db_range); + } else { + throw new Error(`Cannot use log_mel option '${log_mel}' with power ${power}`) + } + break; + default: + throw new Error(`log_mel must be one of null, 'log', 'log10' or 'dB'. Got '${log_mel}'`); + } + } + + return mel_spec; +} + +/** + * Returns an array containing the specified window. + * @param {number} window_length The length of the window in samples. + * @param {string} name The name of the window function. + * @param {Object} options Additional options. + * @param {boolean} [options.periodic=true] Whether the window is periodic or symmetric. + * @param {number} [options.frame_length=null] The length of the analysis frames in samples. + * Provide a value for `frame_length` if the window is smaller than the frame length, so that it will be zero-padded. + * @param {boolean} [options.center=true] Whether to center the window inside the FFT buffer. Only used when `frame_length` is provided. + * @returns {Float64Array} The window of shape `(window_length,)` or `(frame_length,)`. + */ +function window_function(window_length, name, { + periodic = true, + frame_length = null, + center = true, +} = {}) { + const length = periodic ? window_length + 1 : window_length; + let window; + switch (name) { + case 'boxcar': + window = new Float64Array(length).fill(1.0); + break; + case 'hann': + case 'hann_window': + window = hanning(length); + break; + case 'hamming': + window = hamming(length); + break; + case 'povey': + window = hanning(length).map(x => Math.pow(x, 0.85)); + break; + default: + throw new Error(`Unknown window type ${name}.`); + } + if (periodic) { + window = window.subarray(0, window_length); + } + if (frame_length === null) { + return window; + } + if (window_length > frame_length) { + throw new Error(`Length of the window (${window_length}) may not be larger than frame_length (${frame_length})`); + } + + return window; +} + + +/***/ }), + +/***/ "./src/utils/constants.js": +/*!********************************!*\ + !*** ./src/utils/constants.js ***! + \********************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ GITHUB_ISSUE_URL: () => (/* binding */ GITHUB_ISSUE_URL) +/* harmony export */ }); + +const GITHUB_ISSUE_URL = 'https://github.com/huggingface/transformers.js/issues/new/choose'; + +/***/ }), + +/***/ "./src/utils/core.js": +/*!***************************!*\ + !*** ./src/utils/core.js ***! + \***************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ calculateDimensions: () => (/* binding */ calculateDimensions), +/* harmony export */ calculateReflectOffset: () => (/* binding */ calculateReflectOffset), +/* harmony export */ dispatchCallback: () => (/* binding */ dispatchCallback), +/* harmony export */ escapeRegExp: () => (/* binding */ escapeRegExp), +/* harmony export */ isIntegralNumber: () => (/* binding */ isIntegralNumber), +/* harmony export */ isTypedArray: () => (/* binding */ isTypedArray), +/* harmony export */ len: () => (/* binding */ len), +/* harmony export */ mergeArrays: () => (/* binding */ mergeArrays), +/* harmony export */ pick: () => (/* binding */ pick), +/* harmony export */ pop: () => (/* binding */ pop), +/* harmony export */ product: () => (/* binding */ product), +/* harmony export */ reverseDictionary: () => (/* binding */ reverseDictionary) +/* harmony export */ }); + +/** + * @file Core utility functions/classes for Transformers.js. + * + * These are only used internally, meaning an end-user shouldn't + * need to access anything here. + * + * @module utils/core + */ + +/** + * Helper function to dispatch progress callbacks. + * + * @param {Function} progress_callback The progress callback function to dispatch. + * @param {any} data The data to pass to the progress callback function. + * @returns {void} + * @private + */ +function dispatchCallback(progress_callback, data) { + if (progress_callback) progress_callback(data); +} + +/** + * Reverses the keys and values of an object. + * + * @param {Object} data The object to reverse. + * @returns {Object} The reversed object. + * @see https://ultimatecourses.com/blog/reverse-object-keys-and-values-in-javascript + */ +function reverseDictionary(data) { + // https://ultimatecourses.com/blog/reverse-object-keys-and-values-in-javascript + return Object.fromEntries(Object.entries(data).map(([key, value]) => [value, key])); +} + +/** + * Escapes regular expression special characters from a string by replacing them with their escaped counterparts. + * + * @param {string} string The string to escape. + * @returns {string} The escaped string. + */ +function escapeRegExp(string) { + return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string +} + +/** + * Check if a value is a typed array. + * @param {*} val The value to check. + * @returns {boolean} True if the value is a `TypedArray`, false otherwise. + * + * Adapted from https://stackoverflow.com/a/71091338/13989043 + */ +function isTypedArray(val) { + return val?.prototype?.__proto__?.constructor?.name === 'TypedArray'; +} + + +/** + * Check if a value is an integer. + * @param {*} x The value to check. + * @returns {boolean} True if the value is a string, false otherwise. + */ +function isIntegralNumber(x) { + return Number.isInteger(x) || typeof x === 'bigint' +} + +/** + * Calculates the dimensions of a nested array. + * + * @param {any[]} arr The nested array to calculate dimensions for. + * @returns {number[]} An array containing the dimensions of the input array. + */ +function calculateDimensions(arr) { + const dimensions = []; + let current = arr; + while (Array.isArray(current)) { + dimensions.push(current.length); + current = current[0]; + } + return dimensions; +} + +/** + * Replicate python's .pop() method for objects. + * @param {Object} obj The object to pop from. + * @param {string} key The key to pop. + * @param {*} defaultValue The default value to return if the key does not exist. + * @returns {*} The value of the popped key. + * @throws {Error} If the key does not exist and no default value is provided. + */ +function pop(obj, key, defaultValue = undefined) { + const value = obj[key]; + if (value !== undefined) { + delete obj[key]; + return value; + } + if (defaultValue === undefined) { + throw Error(`Key ${key} does not exist in object.`) + } + return defaultValue; +} + +/** + * Efficiently merge arrays, creating a new copy. + * Adapted from https://stackoverflow.com/a/6768642/13989043 + * @param {Array[]} arrs Arrays to merge. + * @returns {Array} The merged array. + */ +function mergeArrays(...arrs) { + return Array.prototype.concat.apply([], arrs); +} + +/** + * Compute the Cartesian product of given arrays + * @param {...Array} a Arrays to compute the product + * @returns {Array} Returns the computed Cartesian product as an array + * @private + */ +function product(...a) { + // Cartesian product of items + // Adapted from https://stackoverflow.com/a/43053803 + return a.reduce((a, b) => a.flatMap(d => b.map(e => [d, e]))); +} + +/** + * Calculates the index offset for a given index and window size. + * @param {number} i The index. + * @param {number} w The window size. + * @returns {number} The index offset. + */ +function calculateReflectOffset(i, w) { + return Math.abs((i + w) % (2 * w) - w); +} + +/** + * + * @param {Object} o + * @param {string[]} props + * @returns {Object} + */ +function pick(o, props) { + return Object.assign( + {}, + ...props.map((prop) => { + if (o[prop] !== undefined) { + return { [prop]: o[prop] }; + } + }) + ); +} + +/** + * Calculate the length of a string, taking multi-byte characters into account. + * This mimics the behavior of Python's `len` function. + * @param {string} s The string to calculate the length of. + * @returns {number} The length of the string. + */ +function len(s) { + let length = 0; + for (const c of s) ++length; + return length; +} + + +/***/ }), + +/***/ "./src/utils/data-structures.js": +/*!**************************************!*\ + !*** ./src/utils/data-structures.js ***! + \**************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ CharTrie: () => (/* binding */ CharTrie), +/* harmony export */ PriorityQueue: () => (/* binding */ PriorityQueue), +/* harmony export */ TokenLattice: () => (/* binding */ TokenLattice) +/* harmony export */ }); + +/** + * @file Custom data structures. + * + * These are only used internally, meaning an end-user shouldn't + * need to access anything here. + * + * @module utils/data-structures + */ + + +/** + * Efficient Heap-based Implementation of a Priority Queue. + * It uses an array-based binary heap, where the root is at index `0`, and the + * children of node `i` are located at indices `2i + 1` and `2i + 2`, respectively. + * + * Adapted from the following sources: + * - https://stackoverflow.com/a/42919752/13989043 (original) + * - https://github.com/belladoreai/llama-tokenizer-js (minor improvements) + */ +class PriorityQueue { + + /** + * Create a new PriorityQueue. + * @param {function(any, any): boolean} comparator Comparator function to determine priority. Defaults to a MaxHeap. + */ + constructor(comparator = (a, b) => a > b, maxSize = Infinity) { + this._heap = []; + this._comparator = comparator; + this._maxSize = maxSize; + } + + /** + * The size of the queue + */ + get size() { + return this._heap.length; + } + + /** + * Check if the queue is empty. + * @returns {boolean} `true` if the queue is empty, `false` otherwise. + */ + isEmpty() { + return this.size === 0; + } + + /** + * Return the element with the highest priority in the queue. + * @returns {any} The highest priority element in the queue. + */ + peek() { + return this._heap[0]; + } + + /** + * Add one or more elements to the queue. + * @param {...any} values The values to push into the queue. + * @returns {number} The new size of the queue. + */ + push(...values) { + return this.extend(values); + } + + /** + * Add multiple elements to the queue. + * @param {any[]} values The values to push into the queue. + * @returns {number} The new size of the queue. + */ + extend(values) { + for (const value of values) { + if (this.size < this._maxSize) { + this._heap.push(value); + this._siftUp(); + } else { + // Get index of value with the lowest priority + const smallest = this._smallest(); + + // If the new value has higher priority than the smallest value in the heap + // then replace the smallest value with the new value and update the heap + if (this._comparator(value, this._heap[smallest])) { + this._heap[smallest] = value; + this._siftUpFrom(smallest); + } + } + } + return this.size; + } + + /** + * Remove and return the element with the highest priority in the queue. + * @returns {any} The element with the highest priority in the queue. + */ + pop() { + const poppedValue = this.peek(); + const bottom = this.size - 1; + if (bottom > 0) { + this._swap(0, bottom); + } + this._heap.pop(); + this._siftDown(); + return poppedValue; + } + + /** + * Replace the element with the highest priority in the queue with a new value. + * @param {*} value The new value. + * @returns {*} The replaced value. + */ + replace(value) { + const replacedValue = this.peek(); + this._heap[0] = value; + this._siftDown(); + return replacedValue; + } + + /** + * Compute the index for the parent of the node at index `i`. + * @param {number} i The index of the node to get the parent of. + * @returns {number} The index of the parent node. + * @private + */ + _parent(i) { + return ((i + 1) >>> 1) - 1; + } + + /** + * Compute the index for the left child of the node at index `i`. + * @param {number} i The index of the node to get the left child of. + * @returns {number} The index of the left child. + * @private + */ + _left(i) { + return (i << 1) + 1; + } + + /** + * Compute the index for the right child of the node at index `i`. + * @param {number} i The index of the node to get the right child of. + * @returns {number} The index of the right child. + * @private + */ + _right(i) { + return (i + 1) << 1; + } + + /** + * Check if the element at index `i` is greater than the element at index `j`. + * @param {number} i The index of the first element to compare. + * @param {number} j The index of the second element to compare. + * @returns {boolean} `true` if the element at index `i` is greater than the element at index `j`, `false` otherwise. + * @private + */ + _greater(i, j) { + return this._comparator(this._heap[i], this._heap[j]); + } + + /** + * Swap the elements at indices `i` and `j`. + * @param {number} i The index of the first element to swap. + * @param {number} j The index of the second element to swap. + * @private + */ + _swap(i, j) { + const temp = this._heap[i]; + this._heap[i] = this._heap[j]; + this._heap[j] = temp; + } + + /** + * Maintain the heap property by updating positions in the heap, + * starting at the last element and moving up the heap. + * @private + */ + _siftUp() { + this._siftUpFrom(this.size - 1); + } + + /** + * Helper function to sift up from a given node. + * @param {number} node The index of the node to start sifting up from. + */ + _siftUpFrom(node) { + while (node > 0 && this._greater(node, this._parent(node))) { + this._swap(node, this._parent(node)); + node = this._parent(node); + } + } + + /** + * Maintain the heap property by updating positions in the heap, + * starting at the first element and moving down the heap. + * @private + */ + _siftDown() { + let node = 0; + while ( + (this._left(node) < this.size && this._greater(this._left(node), node)) || + (this._right(node) < this.size && this._greater(this._right(node), node)) + ) { + const maxChild = (this._right(node) < this.size && this._greater(this._right(node), this._left(node))) + ? this._right(node) + : this._left(node); + this._swap(node, maxChild); + node = maxChild; + } + } + + /** + * Get the index of the smallest element in the heap. Since we use an array-based heap, + * the index can be computed without needing to traverse the heap. + * @private + */ + _smallest() { + return (2 ** (Math.floor(Math.log2(this.size))) - 1); + } +} + +/** + * A trie structure to efficiently store and search for strings. + */ +class CharTrie { + constructor() { + this.root = CharTrieNode.default(); + } + + /** + * Adds one or more `texts` to the trie. + * @param {string[]} texts The strings to add to the trie. + */ + extend(texts) { + for (const text of texts) { + this.push(text); + } + } + + /** + * Adds text to the trie. + * @param {string} text The string to add to the trie. + */ + push(text) { + let node = this.root; + for (const ch of text) { + let child = node.children.get(ch); + if (child === undefined) { + child = CharTrieNode.default(); + node.children.set(ch, child); + } + node = child; + } + node.isLeaf = true; + } + + /** + * Searches the trie for all strings with a common prefix of `text`. + * @param {string} text The common prefix to search for. + * @yields {string} Each string in the trie that has `text` as a prefix. + */ + *commonPrefixSearch(text) { + let node = this.root; + if (node === undefined) return; + + let prefix = ""; + for (const ch of text) { + prefix += ch; + node = node.children.get(ch); + if (node === undefined) return; + if (node.isLeaf) { + yield prefix; + } + } + } +} + +/** + * Represents a node in a character trie. + */ +class CharTrieNode { + /** + * Create a new CharTrieNode. + * @param {boolean} isLeaf Whether the node is a leaf node or not. + * @param {Map} children A map containing the node's children, where the key is a character and the value is a `CharTrieNode`. + */ + constructor(isLeaf, children) { + this.isLeaf = isLeaf; + this.children = children; + } + + /** + * Returns a new `CharTrieNode` instance with default values. + * @returns {CharTrieNode} A new `CharTrieNode` instance with `isLeaf` set to `false` and an empty `children` map. + */ + static default() { + return new CharTrieNode(false, new Map()); + } +} + +/** + * A lattice data structure to be used for tokenization. + */ +class TokenLattice { + /** + * Creates a new TokenLattice instance. + * + * @param {string} sentence The input sentence to be tokenized. + * @param {number} bosTokenId The beginning-of-sequence token ID. + * @param {number} eosTokenId The end-of-sequence token ID. + */ + constructor(sentence, bosTokenId, eosTokenId) { + this.chars = Array.from(sentence); + this.len = this.chars.length; + this.bosTokenId = bosTokenId; + this.eosTokenId = eosTokenId; + this.nodes = []; + this.beginNodes = Array.from({ length: this.len + 1 }, () => []); + this.endNodes = Array.from({ length: this.len + 1 }, () => []); + + const bos = new TokenLatticeNode(this.bosTokenId, 0, 0, 0, 0.0); + const eos = new TokenLatticeNode(this.eosTokenId, 1, this.len, 0, 0.0); + this.nodes.push(bos.clone()); + this.nodes.push(eos.clone()); + this.beginNodes[this.len].push(eos); + this.endNodes[0].push(bos); + } + + /** + * Inserts a new token node into the token lattice. + * + * @param {number} pos The starting position of the token. + * @param {number} length The length of the token. + * @param {number} score The score of the token. + * @param {number} tokenId The token ID of the token. + */ + insert(pos, length, score, tokenId) { + const nodeId = this.nodes.length; + const node = new TokenLatticeNode(tokenId, nodeId, pos, length, score); + this.beginNodes[pos].push(node); + this.endNodes[pos + length].push(node); + this.nodes.push(node); + } + + /** + * Implements the Viterbi algorithm to compute the most likely sequence of tokens. + * + * @returns {TokenLatticeNode[]} The most likely sequence of tokens. + */ + viterbi() { + const len = this.len; + let pos = 0; + while (pos <= len) { + if (this.beginNodes[pos].length == 0) { + return []; + } + for (let rnode of this.beginNodes[pos]) { + rnode.prev = null; + let bestScore = 0.0; + let bestNode = null; + for (let lnode of this.endNodes[pos]) { + const score = lnode.backtraceScore + rnode.score; + if (bestNode === null || score > bestScore) { + bestNode = lnode.clone(); + bestScore = score; + } + } + + if (bestNode !== null) { + rnode.prev = bestNode; + rnode.backtraceScore = bestScore; + } else { + return []; + } + } + ++pos; + } + + const results = []; + const root = this.beginNodes[len][0]; + const prev = root.prev; + if (prev === null) { + return []; + } + + let node = prev.clone(); + while (node.prev !== null) { + results.push(node.clone()); + const n = node.clone(); + node = n.prev.clone(); + } + + results.reverse(); + return results; + } + + /** + * @param {TokenLatticeNode} node + * @returns {string} The array of nodes representing the most likely sequence of tokens. + */ + piece(node) { + return this.chars.slice(node.pos, node.pos + node.length).join(''); + } + + /** + * @returns {string[]} The most likely sequence of tokens. + */ + tokens() { + const nodes = this.viterbi(); + return nodes.map(x => this.piece(x)); + } + + /** + * @returns {number[]} The most likely sequence of token ids. + */ + tokenIds() { + const nodes = this.viterbi(); + return nodes.map(x => x.tokenId); + } +} +class TokenLatticeNode { + /** + * Represents a node in a token lattice for a given sentence. + * @param {number} tokenId The ID of the token associated with this node. + * @param {number} nodeId The ID of this node. + * @param {number} pos The starting position of the token in the sentence. + * @param {number} length The length of the token. + * @param {number} score The score associated with the token. + */ + constructor(tokenId, nodeId, pos, length, score) { + this.tokenId = tokenId; + this.nodeId = nodeId; + this.pos = pos; + this.length = length; + this.score = score; + this.prev = null; + this.backtraceScore = 0.0; + } + + /** + * Returns a clone of this node. + * @returns {TokenLatticeNode} A clone of this node. + */ + clone() { + const n = new TokenLatticeNode(this.tokenId, this.nodeId, this.pos, this.length, this.score); + n.prev = this.prev; + n.backtraceScore = this.backtraceScore; + return n; + } +} + + +/***/ }), + +/***/ "./src/utils/devices.js": +/*!******************************!*\ + !*** ./src/utils/devices.js ***! + \******************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ DEVICE_TYPES: () => (/* binding */ DEVICE_TYPES) +/* harmony export */ }); + +/** + * The list of devices supported by Transformers.js + */ +const DEVICE_TYPES = Object.freeze({ + auto: 'auto', // Auto-detect based on device and environment + gpu: 'gpu', // Auto-detect GPU + cpu: 'cpu', // CPU + wasm: 'wasm', // WebAssembly + webgpu: 'webgpu', // WebGPU + cuda: 'cuda', // CUDA + dml: 'dml', // DirectML + + webnn: 'webnn', // WebNN (default) + 'webnn-npu': 'webnn-npu', // WebNN NPU + 'webnn-gpu': 'webnn-gpu', // WebNN GPU + 'webnn-cpu': 'webnn-cpu', // WebNN CPU +}); + +/** + * @typedef {keyof typeof DEVICE_TYPES} DeviceType + */ + + +/***/ }), + +/***/ "./src/utils/dtypes.js": +/*!*****************************!*\ + !*** ./src/utils/dtypes.js ***! + \*****************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ DATA_TYPES: () => (/* binding */ DATA_TYPES), +/* harmony export */ DEFAULT_DEVICE_DTYPE_MAPPING: () => (/* binding */ DEFAULT_DEVICE_DTYPE_MAPPING), +/* harmony export */ DEFAULT_DTYPE_SUFFIX_MAPPING: () => (/* binding */ DEFAULT_DTYPE_SUFFIX_MAPPING), +/* harmony export */ isWebGpuFp16Supported: () => (/* binding */ isWebGpuFp16Supported) +/* harmony export */ }); +/* harmony import */ var _env_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../env.js */ "./src/env.js"); +/* harmony import */ var _devices_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./devices.js */ "./src/utils/devices.js"); + + + + +// TODO: Use the adapter from `env.backends.onnx.webgpu.adapter` to check for `shader-f16` support, +// when available in https://github.com/microsoft/onnxruntime/pull/19940. +// For more information, see https://github.com/microsoft/onnxruntime/pull/19857#issuecomment-1999984753 + +/** + * Checks if WebGPU fp16 support is available in the current environment. + */ +const isWebGpuFp16Supported = (function () { + /** @type {boolean} */ + let cachedResult; + + return async function () { + if (cachedResult === undefined) { + if (!_env_js__WEBPACK_IMPORTED_MODULE_0__.apis.IS_WEBGPU_AVAILABLE) { + cachedResult = false; + } else { + try { + const adapter = await navigator.gpu.requestAdapter(); + cachedResult = adapter.features.has('shader-f16'); + } catch (e) { + cachedResult = false; + } + } + } + return cachedResult; + }; +})(); + +const DATA_TYPES = Object.freeze({ + fp32: 'fp32', + fp16: 'fp16', + q8: 'q8', + int8: 'int8', + uint8: 'uint8', + q4: 'q4', + bnb4: 'bnb4', + q4f16: 'q4f16', // fp16 model with int4 block weight quantization +}); +/** @typedef {keyof typeof DATA_TYPES} DataType */ + +const DEFAULT_DEVICE_DTYPE_MAPPING = Object.freeze({ + // NOTE: If not specified, will default to fp32 + [_devices_js__WEBPACK_IMPORTED_MODULE_1__.DEVICE_TYPES.wasm]: DATA_TYPES.q8, +}); + +/** @type {Record} */ +const DEFAULT_DTYPE_SUFFIX_MAPPING = Object.freeze({ + [DATA_TYPES.fp32]: '', + [DATA_TYPES.fp16]: '_fp16', + [DATA_TYPES.int8]: '_int8', + [DATA_TYPES.uint8]: '_uint8', + [DATA_TYPES.q8]: '_quantized', + [DATA_TYPES.q4]: '_q4', + [DATA_TYPES.q4f16]: '_q4f16', + [DATA_TYPES.bnb4]: '_bnb4', +}); + + +/***/ }), + +/***/ "./src/utils/generic.js": +/*!******************************!*\ + !*** ./src/utils/generic.js ***! + \******************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ Callable: () => (/* binding */ Callable) +/* harmony export */ }); + +/** + * A base class for creating callable objects. + * See [here](https://stackoverflow.com/q/76073890) for more information. + * + * @type {new () => {(...args: any[]): any, _call(...args: any[]): any}} + */ +const Callable = /** @type {any} */ (class { + /** + * Creates a new instance of the Callable class. + */ + constructor() { + /** + * Creates a closure that delegates to a private method '_call' with the given arguments. + * @type {any} + * @param {...any} args Zero or more arguments to pass to the '_call' method. + * @returns {*} The result of calling the '_call' method. + */ + let closure = function (...args) { + return closure._call(...args) + } + return Object.setPrototypeOf(closure, new.target.prototype) + } + + /** + * This method should be implemented in subclasses to provide the + * functionality of the callable object. + * + * @param {any[]} args + * @throws {Error} If the subclass does not implement the `_call` method. + */ + _call(...args) { + throw Error('Must implement _call method in subclass') + } +}); + + +/***/ }), + +/***/ "./src/utils/hub.js": +/*!**************************!*\ + !*** ./src/utils/hub.js ***! + \**************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ getFile: () => (/* binding */ getFile), +/* harmony export */ getModelFile: () => (/* binding */ getModelFile), +/* harmony export */ getModelJSON: () => (/* binding */ getModelJSON) +/* harmony export */ }); +/* harmony import */ var fs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! fs */ "?7a2c"); +/* harmony import */ var path__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! path */ "?a42a"); +/* harmony import */ var _env_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../env.js */ "./src/env.js"); +/* harmony import */ var _core_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./core.js */ "./src/utils/core.js"); + +/** + * @file Utility functions to interact with the Hugging Face Hub (https://huggingface.co/models) + * + * @module utils/hub + */ + + + + + + + +/** + * @typedef {Object} PretrainedOptions Options for loading a pretrained model. + * @property {function} [progress_callback=null] If specified, this function will be called during model construction, to provide the user with progress updates. + * @property {import('../configs.js').PretrainedConfig} [config=null] Configuration for the model to use instead of an automatically loaded configuration. Configuration can be automatically loaded when: + * - The model is a model provided by the library (loaded with the *model id* string of a pretrained model). + * - The model is loaded by supplying a local directory as `pretrained_model_name_or_path` and a configuration JSON file named *config.json* is found in the directory. + * @property {string} [cache_dir=null] Path to a directory in which a downloaded pretrained model configuration should be cached if the standard cache should not be used. + * @property {boolean} [local_files_only=false] Whether or not to only look at local files (e.g., not try downloading the model). + * @property {string} [revision='main'] The specific model version to use. It can be a branch name, a tag name, or a commit id, + * since we use a git-based system for storing models and other artifacts on huggingface.co, so `revision` can be any identifier allowed by git. + * NOTE: This setting is ignored for local requests. + */ + +/** + * @typedef {Object} ModelSpecificPretrainedOptions Options for loading a pretrained model. + * @property {string} [subfolder='onnx'] In case the relevant files are located inside a subfolder of the model repo on huggingface.co, + * you can specify the folder name here. + * @property {string} [model_file_name=null] If specified, load the model with this name (excluding the .onnx suffix). Currently only valid for encoder- or decoder-only models. + * @property {import("./devices.js").DeviceType|Record} [device=null] The device to run the model on. If not specified, the device will be chosen from the environment settings. + * @property {import("./dtypes.js").DataType|Record} [dtype=null] The data type to use for the model. If not specified, the data type will be chosen from the environment settings. + * @property {boolean|Record} [use_external_data_format=false] Whether to load the model using the external data format (used for models >= 2GB in size). + * @property {import('onnxruntime-common').InferenceSession.SessionOptions} [session_options] (Optional) User-specified session options passed to the runtime. If not provided, suitable defaults will be chosen. + */ + +/** + * @typedef {PretrainedOptions & ModelSpecificPretrainedOptions} PretrainedModelOptions Options for loading a pretrained model. + */ + +/** + * Mapping from file extensions to MIME types. + */ +const CONTENT_TYPE_MAP = { + 'txt': 'text/plain', + 'html': 'text/html', + 'css': 'text/css', + 'js': 'text/javascript', + 'json': 'application/json', + 'png': 'image/png', + 'jpg': 'image/jpeg', + 'jpeg': 'image/jpeg', + 'gif': 'image/gif', +} +class FileResponse { + + /** + * Creates a new `FileResponse` object. + * @param {string|URL} filePath + */ + constructor(filePath) { + this.filePath = filePath; + this.headers = new Headers(); + + this.exists = fs__WEBPACK_IMPORTED_MODULE_0__.existsSync(filePath); + if (this.exists) { + this.status = 200; + this.statusText = 'OK'; + + let stats = fs__WEBPACK_IMPORTED_MODULE_0__.statSync(filePath); + this.headers.set('content-length', stats.size.toString()); + + this.updateContentType(); + + let self = this; + this.body = new ReadableStream({ + start(controller) { + self.arrayBuffer().then(buffer => { + controller.enqueue(new Uint8Array(buffer)); + controller.close(); + }) + } + }); + } else { + this.status = 404; + this.statusText = 'Not Found'; + this.body = null; + } + } + + /** + * Updates the 'content-type' header property of the response based on the extension of + * the file specified by the filePath property of the current object. + * @returns {void} + */ + updateContentType() { + // Set content-type header based on file extension + const extension = this.filePath.toString().split('.').pop().toLowerCase(); + this.headers.set('content-type', CONTENT_TYPE_MAP[extension] ?? 'application/octet-stream'); + } + + /** + * Clone the current FileResponse object. + * @returns {FileResponse} A new FileResponse object with the same properties as the current object. + */ + clone() { + let response = new FileResponse(this.filePath); + response.exists = this.exists; + response.status = this.status; + response.statusText = this.statusText; + response.headers = new Headers(this.headers); + return response; + } + + /** + * Reads the contents of the file specified by the filePath property and returns a Promise that + * resolves with an ArrayBuffer containing the file's contents. + * @returns {Promise} A Promise that resolves with an ArrayBuffer containing the file's contents. + * @throws {Error} If the file cannot be read. + */ + async arrayBuffer() { + const data = await fs__WEBPACK_IMPORTED_MODULE_0__.promises.readFile(this.filePath); + return data.buffer; + } + + /** + * Reads the contents of the file specified by the filePath property and returns a Promise that + * resolves with a Blob containing the file's contents. + * @returns {Promise} A Promise that resolves with a Blob containing the file's contents. + * @throws {Error} If the file cannot be read. + */ + async blob() { + const data = await fs__WEBPACK_IMPORTED_MODULE_0__.promises.readFile(this.filePath); + return new Blob([data], { type: this.headers.get('content-type') }); + } + + /** + * Reads the contents of the file specified by the filePath property and returns a Promise that + * resolves with a string containing the file's contents. + * @returns {Promise} A Promise that resolves with a string containing the file's contents. + * @throws {Error} If the file cannot be read. + */ + async text() { + const data = await fs__WEBPACK_IMPORTED_MODULE_0__.promises.readFile(this.filePath, 'utf8'); + return data; + } + + /** + * Reads the contents of the file specified by the filePath property and returns a Promise that + * resolves with a parsed JavaScript object containing the file's contents. + * + * @returns {Promise} A Promise that resolves with a parsed JavaScript object containing the file's contents. + * @throws {Error} If the file cannot be read. + */ + async json() { + return JSON.parse(await this.text()); + } +} + +/** + * Determines whether the given string is a valid URL. + * @param {string|URL} string The string to test for validity as an URL. + * @param {string[]} [protocols=null] A list of valid protocols. If specified, the protocol must be in this list. + * @param {string[]} [validHosts=null] A list of valid hostnames. If specified, the URL's hostname must be in this list. + * @returns {boolean} True if the string is a valid URL, false otherwise. + */ +function isValidUrl(string, protocols = null, validHosts = null) { + let url; + try { + url = new URL(string); + } catch (_) { + return false; + } + if (protocols && !protocols.includes(url.protocol)) { + return false; + } + if (validHosts && !validHosts.includes(url.hostname)) { + return false; + } + return true; +} + +/** + * Helper function to get a file, using either the Fetch API or FileSystem API. + * + * @param {URL|string} urlOrPath The URL/path of the file to get. + * @returns {Promise} A promise that resolves to a FileResponse object (if the file is retrieved using the FileSystem API), or a Response object (if the file is retrieved using the Fetch API). + */ +async function getFile(urlOrPath) { + + if (_env_js__WEBPACK_IMPORTED_MODULE_2__.env.useFS && !isValidUrl(urlOrPath, ['http:', 'https:', 'blob:'])) { + return new FileResponse(urlOrPath); + + } else if (typeof process !== 'undefined' && process?.release?.name === 'node') { + const IS_CI = !!process.env?.TESTING_REMOTELY; + const version = _env_js__WEBPACK_IMPORTED_MODULE_2__.env.version; + + const headers = new Headers(); + headers.set('User-Agent', `transformers.js/${version}; is_ci/${IS_CI};`); + + // Check whether we are making a request to the Hugging Face Hub. + const isHFURL = isValidUrl(urlOrPath, ['http:', 'https:'], ['huggingface.co', 'hf.co']); + if (isHFURL) { + // If an access token is present in the environment variables, + // we add it to the request headers. + // NOTE: We keep `HF_ACCESS_TOKEN` for backwards compatibility (as a fallback). + const token = process.env?.HF_TOKEN ?? process.env?.HF_ACCESS_TOKEN; + if (token) { + headers.set('Authorization', `Bearer ${token}`); + } + } + return fetch(urlOrPath, { headers }); + } else { + // Running in a browser-environment, so we use default headers + // NOTE: We do not allow passing authorization headers in the browser, + // since this would require exposing the token to the client. + return fetch(urlOrPath); + } +} + +const ERROR_MAPPING = { + // 4xx errors (https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#client_error_responses) + 400: 'Bad request error occurred while trying to load file', + 401: 'Unauthorized access to file', + 403: 'Forbidden access to file', + 404: 'Could not locate file', + 408: 'Request timeout error occurred while trying to load file', + + // 5xx errors (https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#server_error_responses) + 500: 'Internal server error error occurred while trying to load file', + 502: 'Bad gateway error occurred while trying to load file', + 503: 'Service unavailable error occurred while trying to load file', + 504: 'Gateway timeout error occurred while trying to load file', +} +/** + * Helper method to handle fatal errors that occur while trying to load a file from the Hugging Face Hub. + * @param {number} status The HTTP status code of the error. + * @param {string} remoteURL The URL of the file that could not be loaded. + * @param {boolean} fatal Whether to raise an error if the file could not be loaded. + * @returns {null} Returns `null` if `fatal = true`. + * @throws {Error} If `fatal = false`. + */ +function handleError(status, remoteURL, fatal) { + if (!fatal) { + // File was not loaded correctly, but it is optional. + // TODO in future, cache the response? + return null; + } + + const message = ERROR_MAPPING[status] ?? `Error (${status}) occurred while trying to load file`; + throw Error(`${message}: "${remoteURL}".`); +} + +class FileCache { + /** + * Instantiate a `FileCache` object. + * @param {string} path + */ + constructor(path) { + this.path = path; + } + + /** + * Checks whether the given request is in the cache. + * @param {string} request + * @returns {Promise} + */ + async match(request) { + + let filePath = path__WEBPACK_IMPORTED_MODULE_1__.join(this.path, request); + let file = new FileResponse(filePath); + + if (file.exists) { + return file; + } else { + return undefined; + } + } + + /** + * Adds the given response to the cache. + * @param {string} request + * @param {Response|FileResponse} response + * @returns {Promise} + */ + async put(request, response) { + const buffer = Buffer.from(await response.arrayBuffer()); + + let outputPath = path__WEBPACK_IMPORTED_MODULE_1__.join(this.path, request); + + try { + await fs__WEBPACK_IMPORTED_MODULE_0__.promises.mkdir(path__WEBPACK_IMPORTED_MODULE_1__.dirname(outputPath), { recursive: true }); + await fs__WEBPACK_IMPORTED_MODULE_0__.promises.writeFile(outputPath, buffer); + + } catch (err) { + console.warn('An error occurred while writing the file to cache:', err) + } + } + + // TODO add the rest? + // addAll(requests: RequestInfo[]): Promise; + // delete(request: RequestInfo | URL, options?: CacheQueryOptions): Promise; + // keys(request?: RequestInfo | URL, options?: CacheQueryOptions): Promise>; + // match(request: RequestInfo | URL, options?: CacheQueryOptions): Promise; + // matchAll(request?: RequestInfo | URL, options?: CacheQueryOptions): Promise>; +} + +/** + * + * @param {FileCache|Cache} cache The cache to search + * @param {string[]} names The names of the item to search for + * @returns {Promise} The item from the cache, or undefined if not found. + */ +async function tryCache(cache, ...names) { + for (let name of names) { + try { + let result = await cache.match(name); + if (result) return result; + } catch (e) { + continue; + } + } + return undefined; +} + +/** + * + * Retrieves a file from either a remote URL using the Fetch API or from the local file system using the FileSystem API. + * If the filesystem is available and `env.useCache = true`, the file will be downloaded and cached. + * + * @param {string} path_or_repo_id This can be either: + * - a string, the *model id* of a model repo on huggingface.co. + * - a path to a *directory* potentially containing the file. + * @param {string} filename The name of the file to locate in `path_or_repo`. + * @param {boolean} [fatal=true] Whether to throw an error if the file is not found. + * @param {PretrainedOptions} [options] An object containing optional parameters. + * + * @throws Will throw an error if the file is not found and `fatal` is true. + * @returns {Promise} A Promise that resolves with the file content as a buffer. + */ +async function getModelFile(path_or_repo_id, filename, fatal = true, options = {}) { + + if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.allowLocalModels) { + // User has disabled local models, so we just make sure other settings are correct. + + if (options.local_files_only) { + throw Error("Invalid configuration detected: local models are disabled (`env.allowLocalModels=false`) but you have requested to only use local models (`local_files_only=true`).") + } else if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.allowRemoteModels) { + throw Error("Invalid configuration detected: both local and remote models are disabled. Fix by setting `env.allowLocalModels` or `env.allowRemoteModels` to `true`.") + } + } + + // Initiate file retrieval + (0,_core_js__WEBPACK_IMPORTED_MODULE_3__.dispatchCallback)(options.progress_callback, { + status: 'initiate', + name: path_or_repo_id, + file: filename + }) + + // First, check if the a caching backend is available + // If no caching mechanism available, will download the file every time + let cache; + if (!cache && _env_js__WEBPACK_IMPORTED_MODULE_2__.env.useBrowserCache) { + if (typeof caches === 'undefined') { + throw Error('Browser cache is not available in this environment.') + } + try { + // In some cases, the browser cache may be visible, but not accessible due to security restrictions. + // For example, when running an application in an iframe, if a user attempts to load the page in + // incognito mode, the following error is thrown: `DOMException: Failed to execute 'open' on 'CacheStorage': + // An attempt was made to break through the security policy of the user agent.` + // So, instead of crashing, we just ignore the error and continue without using the cache. + cache = await caches.open('transformers-cache'); + } catch (e) { + console.warn('An error occurred while opening the browser cache:', e); + } + } + + if (!cache && _env_js__WEBPACK_IMPORTED_MODULE_2__.env.useFSCache) { + // TODO throw error if not available + + // If `cache_dir` is not specified, use the default cache directory + cache = new FileCache(options.cache_dir ?? _env_js__WEBPACK_IMPORTED_MODULE_2__.env.cacheDir); + } + + if (!cache && _env_js__WEBPACK_IMPORTED_MODULE_2__.env.useCustomCache) { + // Allow the user to specify a custom cache system. + if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache) { + throw Error('`env.useCustomCache=true`, but `env.customCache` is not defined.') + } + + // Check that the required methods are defined: + if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache.match || !_env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache.put) { + throw new Error( + "`env.customCache` must be an object which implements the `match` and `put` functions of the Web Cache API. " + + "For more information, see https://developer.mozilla.org/en-US/docs/Web/API/Cache" + ) + } + cache = _env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache; + } + + const revision = options.revision ?? 'main'; + + let requestURL = pathJoin(path_or_repo_id, filename); + let localPath = pathJoin(_env_js__WEBPACK_IMPORTED_MODULE_2__.env.localModelPath, requestURL); + + let remoteURL = pathJoin( + _env_js__WEBPACK_IMPORTED_MODULE_2__.env.remoteHost, + _env_js__WEBPACK_IMPORTED_MODULE_2__.env.remotePathTemplate + .replaceAll('{model}', path_or_repo_id) + .replaceAll('{revision}', encodeURIComponent(revision)), + filename + ); + + // Choose cache key for filesystem cache + // When using the main revision (default), we use the request URL as the cache key. + // If a specific revision is requested, we account for this in the cache key. + let fsCacheKey = revision === 'main' ? requestURL : pathJoin(path_or_repo_id, revision, filename); + + /** @type {string} */ + let cacheKey; + let proposedCacheKey = cache instanceof FileCache ? fsCacheKey : remoteURL; + + // Whether to cache the final response in the end. + let toCacheResponse = false; + + /** @type {Response|FileResponse|undefined} */ + let response; + + if (cache) { + // A caching system is available, so we try to get the file from it. + // 1. We first try to get from cache using the local path. In some environments (like deno), + // non-URL cache keys are not allowed. In these cases, `response` will be undefined. + // 2. If no response is found, we try to get from cache using the remote URL or file system cache. + response = await tryCache(cache, localPath, proposedCacheKey); + } + + const cacheHit = response !== undefined; + + if (response === undefined) { + // Caching not available, or file is not cached, so we perform the request + + if (_env_js__WEBPACK_IMPORTED_MODULE_2__.env.allowLocalModels) { + // Accessing local models is enabled, so we try to get the file locally. + // If request is a valid HTTP URL, we skip the local file check. Otherwise, we try to get the file locally. + const isURL = isValidUrl(requestURL, ['http:', 'https:']); + if (!isURL) { + try { + response = await getFile(localPath); + cacheKey = localPath; // Update the cache key to be the local path + } catch (e) { + // Something went wrong while trying to get the file locally. + // NOTE: error handling is done in the next step (since `response` will be undefined) + console.warn(`Unable to load from local path "${localPath}": "${e}"`); + } + } else if (options.local_files_only) { + throw new Error(`\`local_files_only=true\`, but attempted to load a remote file from: ${requestURL}.`); + } else if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.allowRemoteModels) { + throw new Error(`\`env.allowRemoteModels=false\`, but attempted to load a remote file from: ${requestURL}.`); + } + } + + if (response === undefined || response.status === 404) { + // File not found locally. This means either: + // - The user has disabled local file access (`env.allowLocalModels=false`) + // - the path is a valid HTTP url (`response === undefined`) + // - the path is not a valid HTTP url and the file is not present on the file system or local server (`response.status === 404`) + + if (options.local_files_only || !_env_js__WEBPACK_IMPORTED_MODULE_2__.env.allowRemoteModels) { + // User requested local files only, but the file is not found locally. + if (fatal) { + throw Error(`\`local_files_only=true\` or \`env.allowRemoteModels=false\` and file was not found locally at "${localPath}".`); + } else { + // File not found, but this file is optional. + // TODO in future, cache the response? + return null; + } + } + + // File not found locally, so we try to download it from the remote server + response = await getFile(remoteURL); + + if (response.status !== 200) { + return handleError(response.status, remoteURL, fatal); + } + + // Success! We use the proposed cache key from earlier + cacheKey = proposedCacheKey; + } + + // Only cache the response if: + toCacheResponse = + cache // 1. A caching system is available + && typeof Response !== 'undefined' // 2. `Response` is defined (i.e., we are in a browser-like environment) + && response instanceof Response // 3. result is a `Response` object (i.e., not a `FileResponse`) + && response.status === 200 // 4. request was successful (status code 200) + } + + // Start downloading + (0,_core_js__WEBPACK_IMPORTED_MODULE_3__.dispatchCallback)(options.progress_callback, { + status: 'download', + name: path_or_repo_id, + file: filename + }) + + const progressInfo = { + status: 'progress', + name: path_or_repo_id, + file: filename + } + + /** @type {Uint8Array} */ + let buffer; + + if (!options.progress_callback) { + // If no progress callback is specified, we can use the `.arrayBuffer()` + // method to read the response. + buffer = new Uint8Array(await response.arrayBuffer()); + + } else if ( + cacheHit // The item is being read from the cache + && + typeof navigator !== 'undefined' && /firefox/i.test(navigator.userAgent) // We are in Firefox + ) { + // Due to bug in Firefox, we cannot display progress when loading from cache. + // Fortunately, since this should be instantaneous, this should not impact users too much. + buffer = new Uint8Array(await response.arrayBuffer()); + + // For completeness, we still fire the final progress callback + (0,_core_js__WEBPACK_IMPORTED_MODULE_3__.dispatchCallback)(options.progress_callback, { + ...progressInfo, + progress: 100, + loaded: buffer.length, + total: buffer.length, + }) + } else { + buffer = await readResponse(response, data => { + (0,_core_js__WEBPACK_IMPORTED_MODULE_3__.dispatchCallback)(options.progress_callback, { + ...progressInfo, + ...data, + }) + }) + } + + if ( + // Only cache web responses + // i.e., do not cache FileResponses (prevents duplication) + toCacheResponse && cacheKey + && + // Check again whether request is in cache. If not, we add the response to the cache + (await cache.match(cacheKey) === undefined) + ) { + // NOTE: We use `new Response(buffer, ...)` instead of `response.clone()` to handle LFS files + await cache.put(cacheKey, new Response(buffer, { + headers: response.headers + })) + .catch(err => { + // Do not crash if unable to add to cache (e.g., QuotaExceededError). + // Rather, log a warning and proceed with execution. + console.warn(`Unable to add response to browser cache: ${err}.`); + }); + + } + + (0,_core_js__WEBPACK_IMPORTED_MODULE_3__.dispatchCallback)(options.progress_callback, { + status: 'done', + name: path_or_repo_id, + file: filename + }); + + return buffer; +} + +/** + * Fetches a JSON file from a given path and file name. + * + * @param {string} modelPath The path to the directory containing the file. + * @param {string} fileName The name of the file to fetch. + * @param {boolean} [fatal=true] Whether to throw an error if the file is not found. + * @param {PretrainedOptions} [options] An object containing optional parameters. + * @returns {Promise} The JSON data parsed into a JavaScript object. + * @throws Will throw an error if the file is not found and `fatal` is true. + */ +async function getModelJSON(modelPath, fileName, fatal = true, options = {}) { + let buffer = await getModelFile(modelPath, fileName, fatal, options); + if (buffer === null) { + // Return empty object + return {} + } + + let decoder = new TextDecoder('utf-8'); + let jsonData = decoder.decode(buffer); + + return JSON.parse(jsonData); +} + +/** + * Read and track progress when reading a Response object + * + * @param {any} response The Response object to read + * @param {function} progress_callback The function to call with progress updates + * @returns {Promise} A Promise that resolves with the Uint8Array buffer + */ +async function readResponse(response, progress_callback) { + + const contentLength = response.headers.get('Content-Length'); + if (contentLength === null) { + console.warn('Unable to determine content-length from response headers. Will expand buffer when needed.') + } + let total = parseInt(contentLength ?? '0'); + let buffer = new Uint8Array(total); + let loaded = 0; + + const reader = response.body.getReader(); + async function read() { + const { done, value } = await reader.read(); + if (done) return; + + let newLoaded = loaded + value.length; + if (newLoaded > total) { + total = newLoaded; + + // Adding the new data will overflow buffer. + // In this case, we extend the buffer + let newBuffer = new Uint8Array(total); + + // copy contents + newBuffer.set(buffer); + + buffer = newBuffer; + } + buffer.set(value, loaded) + loaded = newLoaded; + + const progress = (loaded / total) * 100; + + // Call your function here + progress_callback({ + progress: progress, + loaded: loaded, + total: total, + }) + + return read(); + } + + // Actually read + await read(); + + return buffer; +} + +/** + * Joins multiple parts of a path into a single path, while handling leading and trailing slashes. + * + * @param {...string} parts Multiple parts of a path. + * @returns {string} A string representing the joined path. + */ +function pathJoin(...parts) { + // https://stackoverflow.com/a/55142565 + parts = parts.map((part, index) => { + if (index) { + part = part.replace(new RegExp('^/'), ''); + } + if (index !== parts.length - 1) { + part = part.replace(new RegExp('/$'), ''); + } + return part; + }) + return parts.join('/'); +} + + +/***/ }), + +/***/ "./src/utils/image.js": +/*!****************************!*\ + !*** ./src/utils/image.js ***! + \****************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ RawImage: () => (/* binding */ RawImage) +/* harmony export */ }); +/* harmony import */ var _hub_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./hub.js */ "./src/utils/hub.js"); +/* harmony import */ var _env_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../env.js */ "./src/env.js"); +/* harmony import */ var _tensor_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./tensor.js */ "./src/utils/tensor.js"); +/* harmony import */ var sharp__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! sharp */ "?2b25"); + +/** + * @file Helper module for image processing. + * + * These functions and classes are only used internally, + * meaning an end-user shouldn't need to access anything here. + * + * @module utils/image + */ + + + + + +// Will be empty (or not used) if running in browser or web-worker + + +const BROWSER_ENV = typeof self !== 'undefined'; +const WEBWORKER_ENV = BROWSER_ENV && self.constructor.name === 'DedicatedWorkerGlobalScope'; + +let createCanvasFunction; +let ImageDataClass; +let loadImageFunction; +if (BROWSER_ENV) { + // Running in browser or web-worker + createCanvasFunction = (/** @type {number} */ width, /** @type {number} */ height) => { + if (!self.OffscreenCanvas) { + throw new Error('OffscreenCanvas not supported by this browser.'); + } + return new self.OffscreenCanvas(width, height) + }; + loadImageFunction = self.createImageBitmap; + ImageDataClass = self.ImageData; + +} else if (sharp__WEBPACK_IMPORTED_MODULE_3__) { + // Running in Node.js, electron, or other non-browser environment + + loadImageFunction = async (/**@type {sharp.Sharp}*/img) => { + const metadata = await img.metadata(); + const rawChannels = metadata.channels; + + const { data, info } = await img.rotate().raw().toBuffer({ resolveWithObject: true }); + + const newImage = new RawImage(new Uint8ClampedArray(data), info.width, info.height, info.channels); + if (rawChannels !== undefined && rawChannels !== info.channels) { + // Make sure the new image has the same number of channels as the input image. + // This is necessary for grayscale images. + newImage.convert(rawChannels); + } + return newImage; + } + +} else { + throw new Error('Unable to load image processing library.'); +} + + +// Defined here: https://github.com/python-pillow/Pillow/blob/a405e8406b83f8bfb8916e93971edc7407b8b1ff/src/libImaging/Imaging.h#L262-L268 +const RESAMPLING_MAPPING = { + 0: 'nearest', + 1: 'lanczos', + 2: 'bilinear', + 3: 'bicubic', + 4: 'box', + 5: 'hamming', +} + +/** + * Mapping from file extensions to MIME types. + */ +const CONTENT_TYPE_MAP = new Map([ + ['png', 'image/png'], + ['jpg', 'image/jpeg'], + ['jpeg', 'image/jpeg'], + ['gif', 'image/gif'], +]); + +class RawImage { + + /** + * Create a new `RawImage` object. + * @param {Uint8ClampedArray|Uint8Array} data The pixel data. + * @param {number} width The width of the image. + * @param {number} height The height of the image. + * @param {1|2|3|4} channels The number of channels. + */ + constructor(data, width, height, channels) { + this.data = data; + this.width = width; + this.height = height; + this.channels = channels; + } + + /** + * Returns the size of the image (width, height). + * @returns {[number, number]} The size of the image (width, height). + */ + get size() { + return [this.width, this.height]; + } + + /** + * Helper method for reading an image from a variety of input types. + * @param {RawImage|string|URL} input + * @returns The image object. + * + * **Example:** Read image from a URL. + * ```javascript + * let image = await RawImage.read('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/football-match.jpg'); + * // RawImage { + * // "data": Uint8ClampedArray [ 25, 25, 25, 19, 19, 19, ... ], + * // "width": 800, + * // "height": 533, + * // "channels": 3 + * // } + * ``` + */ + static async read(input) { + if (input instanceof RawImage) { + return input; + } else if (typeof input === 'string' || input instanceof URL) { + return await this.fromURL(input); + } else { + throw new Error(`Unsupported input type: ${typeof input}`); + } + } + + /** + * Read an image from a canvas. + * @param {HTMLCanvasElement|OffscreenCanvas} canvas The canvas to read the image from. + * @returns {RawImage} The image object. + */ + static fromCanvas(canvas) { + if (!BROWSER_ENV) { + throw new Error('fromCanvas() is only supported in browser environments.') + } + + const ctx = canvas.getContext('2d'); + const data = ctx.getImageData(0, 0, canvas.width, canvas.height).data; + return new RawImage(data, canvas.width, canvas.height, 4); + } + + /** + * Read an image from a URL or file path. + * @param {string|URL} url The URL or file path to read the image from. + * @returns {Promise} The image object. + */ + static async fromURL(url) { + const response = await (0,_hub_js__WEBPACK_IMPORTED_MODULE_0__.getFile)(url); + if (response.status !== 200) { + throw new Error(`Unable to read image from "${url}" (${response.status} ${response.statusText})`); + } + const blob = await response.blob(); + return this.fromBlob(blob); + } + + /** + * Helper method to create a new Image from a blob. + * @param {Blob} blob The blob to read the image from. + * @returns {Promise} The image object. + */ + static async fromBlob(blob) { + if (BROWSER_ENV) { + // Running in environment with canvas + const img = await loadImageFunction(blob); + + const ctx = createCanvasFunction(img.width, img.height).getContext('2d'); + + // Draw image to context + ctx.drawImage(img, 0, 0); + + return new this(ctx.getImageData(0, 0, img.width, img.height).data, img.width, img.height, 4); + + } else { + // Use sharp.js to read (and possible resize) the image. + const img = sharp__WEBPACK_IMPORTED_MODULE_3__(await blob.arrayBuffer()); + + return await loadImageFunction(img); + } + } + + /** + * Helper method to create a new Image from a tensor + * @param {Tensor} tensor + */ + static fromTensor(tensor, channel_format = 'CHW') { + if (tensor.dims.length !== 3) { + throw new Error(`Tensor should have 3 dimensions, but has ${tensor.dims.length} dimensions.`); + } + + if (channel_format === 'CHW') { + tensor = tensor.transpose(1, 2, 0); + } else if (channel_format === 'HWC') { + // Do nothing + } else { + throw new Error(`Unsupported channel format: ${channel_format}`); + } + if (!(tensor.data instanceof Uint8ClampedArray || tensor.data instanceof Uint8Array)) { + throw new Error(`Unsupported tensor type: ${tensor.type}`); + } + switch (tensor.dims[2]) { + case 1: + case 2: + case 3: + case 4: + return new RawImage(tensor.data, tensor.dims[1], tensor.dims[0], tensor.dims[2]); + default: + throw new Error(`Unsupported number of channels: ${tensor.dims[2]}`); + } + } + + /** + * Convert the image to grayscale format. + * @returns {RawImage} `this` to support chaining. + */ + grayscale() { + if (this.channels === 1) { + return this; + } + + const newData = new Uint8ClampedArray(this.width * this.height * 1); + switch (this.channels) { + case 3: // rgb to grayscale + case 4: // rgba to grayscale + for (let i = 0, offset = 0; i < this.data.length; i += this.channels) { + const red = this.data[i]; + const green = this.data[i + 1]; + const blue = this.data[i + 2]; + + newData[offset++] = Math.round(0.2989 * red + 0.5870 * green + 0.1140 * blue); + } + break; + default: + throw new Error(`Conversion failed due to unsupported number of channels: ${this.channels}`); + } + return this._update(newData, this.width, this.height, 1); + } + + /** + * Convert the image to RGB format. + * @returns {RawImage} `this` to support chaining. + */ + rgb() { + if (this.channels === 3) { + return this; + } + + const newData = new Uint8ClampedArray(this.width * this.height * 3); + + switch (this.channels) { + case 1: // grayscale to rgb + for (let i = 0, offset = 0; i < this.data.length; ++i) { + newData[offset++] = this.data[i]; + newData[offset++] = this.data[i]; + newData[offset++] = this.data[i]; + } + break; + case 4: // rgba to rgb + for (let i = 0, offset = 0; i < this.data.length; i += 4) { + newData[offset++] = this.data[i]; + newData[offset++] = this.data[i + 1]; + newData[offset++] = this.data[i + 2]; + } + break; + default: + throw new Error(`Conversion failed due to unsupported number of channels: ${this.channels}`); + } + return this._update(newData, this.width, this.height, 3); + + } + + /** + * Convert the image to RGBA format. + * @returns {RawImage} `this` to support chaining. + */ + rgba() { + if (this.channels === 4) { + return this; + } + + const newData = new Uint8ClampedArray(this.width * this.height * 4); + + switch (this.channels) { + case 1: // grayscale to rgba + for (let i = 0, offset = 0; i < this.data.length; ++i) { + newData[offset++] = this.data[i]; + newData[offset++] = this.data[i]; + newData[offset++] = this.data[i]; + newData[offset++] = 255; + } + break; + case 3: // rgb to rgba + for (let i = 0, offset = 0; i < this.data.length; i += 3) { + newData[offset++] = this.data[i]; + newData[offset++] = this.data[i + 1]; + newData[offset++] = this.data[i + 2]; + newData[offset++] = 255; + } + break; + default: + throw new Error(`Conversion failed due to unsupported number of channels: ${this.channels}`); + } + + return this._update(newData, this.width, this.height, 4); + } + + /** + * Resize the image to the given dimensions. This method uses the canvas API to perform the resizing. + * @param {number} width The width of the new image. + * @param {number} height The height of the new image. + * @param {Object} options Additional options for resizing. + * @param {0|1|2|3|4|5|string} [options.resample] The resampling method to use. + * @returns {Promise} `this` to support chaining. + */ + async resize(width, height, { + resample = 2, + } = {}) { + + // Ensure resample method is a string + let resampleMethod = RESAMPLING_MAPPING[resample] ?? resample; + + if (BROWSER_ENV) { + // TODO use `resample` in browser environment + + // Store number of channels before resizing + const numChannels = this.channels; + + // Create canvas object for this image + const canvas = this.toCanvas(); + + // Actually perform resizing using the canvas API + const ctx = createCanvasFunction(width, height).getContext('2d'); + + // Draw image to context, resizing in the process + ctx.drawImage(canvas, 0, 0, width, height); + + // Create image from the resized data + const resizedImage = new RawImage(ctx.getImageData(0, 0, width, height).data, width, height, 4); + + // Convert back so that image has the same number of channels as before + return resizedImage.convert(numChannels); + + } else { + // Create sharp image from raw data, and resize + let img = this.toSharp(); + + switch (resampleMethod) { + case 'box': + case 'hamming': + if (resampleMethod === 'box' || resampleMethod === 'hamming') { + console.warn(`Resampling method ${resampleMethod} is not yet supported. Using bilinear instead.`); + resampleMethod = 'bilinear'; + } + + case 'nearest': + case 'bilinear': + case 'bicubic': + // Perform resizing using affine transform. + // This matches how the python Pillow library does it. + img = img.affine([width / this.width, 0, 0, height / this.height], { + interpolator: resampleMethod + }); + break; + + case 'lanczos': + // https://github.com/python-pillow/Pillow/discussions/5519 + // https://github.com/lovell/sharp/blob/main/docs/api-resize.md + img = img.resize({ + width, height, + fit: 'fill', + kernel: 'lanczos3', // PIL Lanczos uses a kernel size of 3 + }); + break; + + default: + throw new Error(`Resampling method ${resampleMethod} is not supported.`); + } + + return await loadImageFunction(img); + } + + } + + async pad([left, right, top, bottom]) { + left = Math.max(left, 0); + right = Math.max(right, 0); + top = Math.max(top, 0); + bottom = Math.max(bottom, 0); + + if (left === 0 && right === 0 && top === 0 && bottom === 0) { + // No padding needed + return this; + } + + if (BROWSER_ENV) { + // Store number of channels before padding + const numChannels = this.channels; + + // Create canvas object for this image + const canvas = this.toCanvas(); + + const newWidth = this.width + left + right; + const newHeight = this.height + top + bottom; + + // Create a new canvas of the desired size. + const ctx = createCanvasFunction(newWidth, newHeight).getContext('2d'); + + // Draw image to context, padding in the process + ctx.drawImage(canvas, + 0, 0, this.width, this.height, + left, top, newWidth, newHeight + ); + + // Create image from the padded data + const paddedImage = new RawImage( + ctx.getImageData(0, 0, newWidth, newHeight).data, + newWidth, newHeight, 4); + + // Convert back so that image has the same number of channels as before + return paddedImage.convert(numChannels); + + } else { + const img = this.toSharp().extend({ left, right, top, bottom }); + return await loadImageFunction(img); + } + } + + async crop([x_min, y_min, x_max, y_max]) { + // Ensure crop bounds are within the image + x_min = Math.max(x_min, 0); + y_min = Math.max(y_min, 0); + x_max = Math.min(x_max, this.width - 1); + y_max = Math.min(y_max, this.height - 1); + + // Do nothing if the crop is the entire image + if (x_min === 0 && y_min === 0 && x_max === this.width - 1 && y_max === this.height - 1) { + return this; + } + + const crop_width = x_max - x_min + 1; + const crop_height = y_max - y_min + 1; + + if (BROWSER_ENV) { + // Store number of channels before resizing + const numChannels = this.channels; + + // Create canvas object for this image + const canvas = this.toCanvas(); + + // Create a new canvas of the desired size. This is needed since if the + // image is too small, we need to pad it with black pixels. + const ctx = createCanvasFunction(crop_width, crop_height).getContext('2d'); + + // Draw image to context, cropping in the process + ctx.drawImage(canvas, + x_min, y_min, crop_width, crop_height, + 0, 0, crop_width, crop_height + ); + + // Create image from the resized data + const resizedImage = new RawImage(ctx.getImageData(0, 0, crop_width, crop_height).data, crop_width, crop_height, 4); + + // Convert back so that image has the same number of channels as before + return resizedImage.convert(numChannels); + + } else { + // Create sharp image from raw data + const img = this.toSharp().extract({ + left: x_min, + top: y_min, + width: crop_width, + height: crop_height, + }); + + return await loadImageFunction(img); + } + + } + + async center_crop(crop_width, crop_height) { + // If the image is already the desired size, return it + if (this.width === crop_width && this.height === crop_height) { + return this; + } + + // Determine bounds of the image in the new canvas + const width_offset = (this.width - crop_width) / 2; + const height_offset = (this.height - crop_height) / 2; + + + if (BROWSER_ENV) { + // Store number of channels before resizing + const numChannels = this.channels; + + // Create canvas object for this image + const canvas = this.toCanvas(); + + // Create a new canvas of the desired size. This is needed since if the + // image is too small, we need to pad it with black pixels. + const ctx = createCanvasFunction(crop_width, crop_height).getContext('2d'); + + let sourceX = 0; + let sourceY = 0; + let destX = 0; + let destY = 0; + + if (width_offset >= 0) { + sourceX = width_offset; + } else { + destX = -width_offset; + } + + if (height_offset >= 0) { + sourceY = height_offset; + } else { + destY = -height_offset; + } + + // Draw image to context, cropping in the process + ctx.drawImage(canvas, + sourceX, sourceY, crop_width, crop_height, + destX, destY, crop_width, crop_height + ); + + // Create image from the resized data + const resizedImage = new RawImage(ctx.getImageData(0, 0, crop_width, crop_height).data, crop_width, crop_height, 4); + + // Convert back so that image has the same number of channels as before + return resizedImage.convert(numChannels); + + } else { + // Create sharp image from raw data + let img = this.toSharp(); + + if (width_offset >= 0 && height_offset >= 0) { + // Cropped image lies entirely within the original image + img = img.extract({ + left: Math.floor(width_offset), + top: Math.floor(height_offset), + width: crop_width, + height: crop_height, + }) + } else if (width_offset <= 0 && height_offset <= 0) { + // Cropped image lies entirely outside the original image, + // so we add padding + const top = Math.floor(-height_offset); + const left = Math.floor(-width_offset); + img = img.extend({ + top: top, + left: left, + + // Ensures the resulting image has the desired dimensions + right: crop_width - this.width - left, + bottom: crop_height - this.height - top, + }); + } else { + // Cropped image lies partially outside the original image. + // We first pad, then crop. + + let y_padding = [0, 0]; + let y_extract = 0; + if (height_offset < 0) { + y_padding[0] = Math.floor(-height_offset); + y_padding[1] = crop_height - this.height - y_padding[0]; + } else { + y_extract = Math.floor(height_offset); + } + + let x_padding = [0, 0]; + let x_extract = 0; + if (width_offset < 0) { + x_padding[0] = Math.floor(-width_offset); + x_padding[1] = crop_width - this.width - x_padding[0]; + } else { + x_extract = Math.floor(width_offset); + } + + img = img.extend({ + top: y_padding[0], + bottom: y_padding[1], + left: x_padding[0], + right: x_padding[1], + }).extract({ + left: x_extract, + top: y_extract, + width: crop_width, + height: crop_height, + }) + } + + return await loadImageFunction(img); + } + } + + async toBlob(type = 'image/png', quality = 1) { + if (!BROWSER_ENV) { + throw new Error('toBlob() is only supported in browser environments.') + } + + const canvas = this.toCanvas(); + return await canvas.convertToBlob({ type, quality }); + } + + toTensor(channel_format = 'CHW') { + let tensor = new _tensor_js__WEBPACK_IMPORTED_MODULE_2__.Tensor( + 'uint8', + new Uint8Array(this.data), + [this.height, this.width, this.channels] + ); + + if (channel_format === 'HWC') { + // Do nothing + } else if (channel_format === 'CHW') { // hwc -> chw + tensor = tensor.permute(2, 0, 1); + } else { + throw new Error(`Unsupported channel format: ${channel_format}`); + } + return tensor; + } + + toCanvas() { + if (!BROWSER_ENV) { + throw new Error('toCanvas() is only supported in browser environments.') + } + + // Clone, and convert data to RGBA before drawing to canvas. + // This is because the canvas API only supports RGBA + const cloned = this.clone().rgba(); + + // Create canvas object for the cloned image + const clonedCanvas = createCanvasFunction(cloned.width, cloned.height); + + // Draw image to context + const data = new ImageDataClass(cloned.data, cloned.width, cloned.height); + clonedCanvas.getContext('2d').putImageData(data, 0, 0); + + return clonedCanvas; + } + + /** + * Helper method to update the image data. + * @param {Uint8ClampedArray} data The new image data. + * @param {number} width The new width of the image. + * @param {number} height The new height of the image. + * @param {1|2|3|4|null} [channels] The new number of channels of the image. + * @private + */ + _update(data, width, height, channels = null) { + this.data = data; + this.width = width; + this.height = height; + if (channels !== null) { + this.channels = channels; + } + return this; + } + + /** + * Clone the image + * @returns {RawImage} The cloned image + */ + clone() { + return new RawImage(this.data.slice(), this.width, this.height, this.channels); + } + + /** + * Helper method for converting image to have a certain number of channels + * @param {number} numChannels The number of channels. Must be 1, 3, or 4. + * @returns {RawImage} `this` to support chaining. + */ + convert(numChannels) { + if (this.channels === numChannels) return this; // Already correct number of channels + + switch (numChannels) { + case 1: + this.grayscale(); + break; + case 3: + this.rgb(); + break; + case 4: + this.rgba(); + break; + default: + throw new Error(`Conversion failed due to unsupported number of channels: ${this.channels}`); + } + return this; + } + + /** + * Save the image to the given path. + * @param {string} path The path to save the image to. + */ + async save(path) { + + if (BROWSER_ENV) { + if (WEBWORKER_ENV) { + throw new Error('Unable to save an image from a Web Worker.') + } + + const extension = path.split('.').pop().toLowerCase(); + const mime = CONTENT_TYPE_MAP.get(extension) ?? 'image/png'; + + // Convert image to Blob + const blob = await this.toBlob(mime); + + // Convert the canvas content to a data URL + const dataURL = URL.createObjectURL(blob); + + // Create an anchor element with the data URL as the href attribute + const downloadLink = document.createElement('a'); + downloadLink.href = dataURL; + + // Set the download attribute to specify the desired filename for the downloaded image + downloadLink.download = path; + + // Trigger the download + downloadLink.click(); + + // Clean up: remove the anchor element from the DOM + downloadLink.remove(); + + } else if (!_env_js__WEBPACK_IMPORTED_MODULE_1__.env.useFS) { + throw new Error('Unable to save the image because filesystem is disabled in this environment.') + + } else { + const img = this.toSharp(); + return await img.toFile(path); + } + } + + toSharp() { + if (BROWSER_ENV) { + throw new Error('toSharp() is only supported in server-side environments.') + } + + return sharp__WEBPACK_IMPORTED_MODULE_3__(this.data, { + raw: { + width: this.width, + height: this.height, + channels: this.channels + } + }); + } +} + +/***/ }), + +/***/ "./src/utils/maths.js": +/*!****************************!*\ + !*** ./src/utils/maths.js ***! + \****************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ FFT: () => (/* binding */ FFT), +/* harmony export */ bankers_round: () => (/* binding */ bankers_round), +/* harmony export */ cos_sim: () => (/* binding */ cos_sim), +/* harmony export */ dot: () => (/* binding */ dot), +/* harmony export */ dynamic_time_warping: () => (/* binding */ dynamic_time_warping), +/* harmony export */ interpolate_data: () => (/* binding */ interpolate_data), +/* harmony export */ log_softmax: () => (/* binding */ log_softmax), +/* harmony export */ magnitude: () => (/* binding */ magnitude), +/* harmony export */ max: () => (/* binding */ max), +/* harmony export */ medianFilter: () => (/* binding */ medianFilter), +/* harmony export */ min: () => (/* binding */ min), +/* harmony export */ permute_data: () => (/* binding */ permute_data), +/* harmony export */ round: () => (/* binding */ round), +/* harmony export */ softmax: () => (/* binding */ softmax) +/* harmony export */ }); + +/** + * @file Helper module for mathematical processing. + * + * These functions and classes are only used internally, + * meaning an end-user shouldn't need to access anything here. + * + * @module utils/maths + */ + +/** + * @typedef {Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array} TypedArray + * @typedef {BigInt64Array | BigUint64Array} BigTypedArray + * @typedef {TypedArray | BigTypedArray} AnyTypedArray + */ + +/** + * @param {TypedArray} input + */ +function interpolate_data(input, [in_channels, in_height, in_width], [out_height, out_width], mode = 'bilinear', align_corners = false) { + // TODO use mode and align_corners + + // Output image dimensions + const x_scale = out_width / in_width; + const y_scale = out_height / in_height; + + // Output image + // @ts-ignore + const out_img = new input.constructor(out_height * out_width * in_channels); + + // Pre-calculate strides + const inStride = in_height * in_width; + const outStride = out_height * out_width; + + for (let i = 0; i < out_height; ++i) { + for (let j = 0; j < out_width; ++j) { + // Calculate output offset + const outOffset = i * out_width + j; + + // Calculate input pixel coordinates + const x = (j + 0.5) / x_scale - 0.5; + const y = (i + 0.5) / y_scale - 0.5; + + // Calculate the four nearest input pixels + // We also check if the input pixel coordinates are within the image bounds + let x1 = Math.floor(x); + let y1 = Math.floor(y); + const x2 = Math.min(x1 + 1, in_width - 1); + const y2 = Math.min(y1 + 1, in_height - 1); + + x1 = Math.max(x1, 0); + y1 = Math.max(y1, 0); + + + // Calculate the fractional distances between the input pixel and the four nearest pixels + const s = x - x1; + const t = y - y1; + + // Perform bilinear interpolation + const w1 = (1 - s) * (1 - t); + const w2 = s * (1 - t); + const w3 = (1 - s) * t; + const w4 = s * t; + + // Calculate the four nearest input pixel indices + const yStride = y1 * in_width; + const xStride = y2 * in_width; + const idx1 = yStride + x1; + const idx2 = yStride + x2; + const idx3 = xStride + x1; + const idx4 = xStride + x2; + + for (let k = 0; k < in_channels; ++k) { + // Calculate channel offset + const cOffset = k * inStride; + + out_img[k * outStride + outOffset] = + w1 * input[cOffset + idx1] + + w2 * input[cOffset + idx2] + + w3 * input[cOffset + idx3] + + w4 * input[cOffset + idx4]; + } + } + } + + return out_img; +} + + +/** + * Helper method to permute a `AnyTypedArray` directly + * @template {AnyTypedArray} T + * @param {T} array + * @param {number[]} dims + * @param {number[]} axes + * @returns {[T, number[]]} The permuted array and the new shape. + */ +function permute_data(array, dims, axes) { + // Calculate the new shape of the permuted array + // and the stride of the original array + const shape = new Array(axes.length); + const stride = new Array(axes.length); + + for (let i = axes.length - 1, s = 1; i >= 0; --i) { + stride[i] = s; + shape[i] = dims[axes[i]]; + s *= shape[i]; + } + + // Precompute inverse mapping of stride + const invStride = axes.map((_, i) => stride[axes.indexOf(i)]); + + // Create the permuted array with the new shape + // @ts-ignore + const permutedData = new array.constructor(array.length); + + // Permute the original array to the new array + for (let i = 0; i < array.length; ++i) { + let newIndex = 0; + for (let j = dims.length - 1, k = i; j >= 0; --j) { + newIndex += (k % dims[j]) * invStride[j]; + k = Math.floor(k / dims[j]); + } + permutedData[newIndex] = array[i]; + } + + return [permutedData, shape]; +} + + +/** + * Compute the softmax of an array of numbers. + * @template {TypedArray|number[]} T + * @param {T} arr The array of numbers to compute the softmax of. + * @returns {T} The softmax array. + */ +function softmax(arr) { + // Compute the maximum value in the array + const maxVal = max(arr)[0]; + + // Compute the exponentials of the array values + const exps = arr.map(x => Math.exp(x - maxVal)); + + // Compute the sum of the exponentials + // @ts-ignore + const sumExps = exps.reduce((acc, val) => acc + val, 0); + + // Compute the softmax values + const softmaxArr = exps.map(x => x / sumExps); + + return /** @type {T} */(softmaxArr); +} + +/** + * Calculates the logarithm of the softmax function for the input array. + * @template {TypedArray|number[]} T + * @param {T} arr The input array to calculate the log_softmax function for. + * @returns {T} The resulting log_softmax array. + */ +function log_softmax(arr) { + // Compute the maximum value in the array + const maxVal = max(arr)[0]; + + // Compute the sum of the exponentials + let sumExps = 0; + for(let i = 0; i < arr.length; ++i) { + sumExps += Math.exp(arr[i] - maxVal); + } + + // Compute the log of the sum + const logSum = Math.log(sumExps); + + // Compute the softmax values + const logSoftmaxArr = arr.map(x => x - maxVal - logSum); + + return /** @type {T} */(logSoftmaxArr); +} + +/** + * Calculates the dot product of two arrays. + * @param {number[]} arr1 The first array. + * @param {number[]} arr2 The second array. + * @returns {number} The dot product of arr1 and arr2. + */ +function dot(arr1, arr2) { + let result = 0; + for (let i = 0; i < arr1.length; ++i) { + result += arr1[i] * arr2[i]; + } + return result; +} + +/** + * Computes the cosine similarity between two arrays. + * + * @param {number[]} arr1 The first array. + * @param {number[]} arr2 The second array. + * @returns {number} The cosine similarity between the two arrays. + */ +function cos_sim(arr1, arr2) { + // Calculate dot product of the two arrays + const dotProduct = dot(arr1, arr2); + + // Calculate the magnitude of the first array + const magnitudeA = magnitude(arr1); + + // Calculate the magnitude of the second array + const magnitudeB = magnitude(arr2); + + // Calculate the cosine similarity + const cosineSimilarity = dotProduct / (magnitudeA * magnitudeB); + + return cosineSimilarity; +} + +/** + * Calculates the magnitude of a given array. + * @param {number[]} arr The array to calculate the magnitude of. + * @returns {number} The magnitude of the array. + */ +function magnitude(arr) { + return Math.sqrt(arr.reduce((acc, val) => acc + val * val, 0)); +} + + +/** + * Returns the value and index of the minimum element in an array. + * @param {number[]|TypedArray} arr array of numbers. + * @returns {[number, number]} the value and index of the minimum element, of the form: [valueOfMin, indexOfMin] + * @throws {Error} If array is empty. + */ +function min(arr) { + if (arr.length === 0) throw Error('Array must not be empty'); + let min = arr[0]; + let indexOfMin = 0; + for (let i = 1; i < arr.length; ++i) { + if (arr[i] < min) { + min = arr[i]; + indexOfMin = i; + } + } + return [min, indexOfMin]; +} + + +/** + * Returns the value and index of the maximum element in an array. + * @param {number[]|AnyTypedArray} arr array of numbers. + * @returns {[number, number]} the value and index of the maximum element, of the form: [valueOfMax, indexOfMax] + * @throws {Error} If array is empty. + */ +function max(arr) { + if (arr.length === 0) throw Error('Array must not be empty'); + let max = arr[0]; + let indexOfMax = 0; + for (let i = 1; i < arr.length; ++i) { + if (arr[i] > max) { + max = arr[i]; + indexOfMax = i; + } + } + return [Number(max), indexOfMax]; +} + +function isPowerOfTwo(number) { + // Check if the number is greater than 0 and has only one bit set to 1 + return (number > 0) && ((number & (number - 1)) === 0); +} + +/** + * Implementation of Radix-4 FFT. + * + * P2FFT class provides functionality for performing Fast Fourier Transform on arrays + * which are a power of two in length. + * Code adapted from https://www.npmjs.com/package/fft.js + */ +class P2FFT { + /** + * @param {number} size The size of the input array. Must be a power of two larger than 1. + * @throws {Error} FFT size must be a power of two larger than 1. + */ + constructor(size) { + this.size = size | 0; // convert to a 32-bit signed integer + if (this.size <= 1 || !isPowerOfTwo(this.size)) + throw new Error('FFT size must be a power of two larger than 1'); + + this._csize = size << 1; + + this.table = new Float64Array(this.size * 2); + for (let i = 0; i < this.table.length; i += 2) { + const angle = Math.PI * i / this.size; + this.table[i] = Math.cos(angle); + this.table[i + 1] = -Math.sin(angle); + } + + // Find size's power of two + let power = 0; + for (let t = 1; this.size > t; t <<= 1) + ++power; + + // Calculate initial step's width: + // * If we are full radix-4, it is 2x smaller to give inital len=8 + // * Otherwise it is the same as `power` to give len=4 + this._width = power % 2 === 0 ? power - 1 : power; + + // Pre-compute bit-reversal patterns + this._bitrev = new Int32Array(1 << this._width); + for (let j = 0; j < this._bitrev.length; ++j) { + this._bitrev[j] = 0; + for (let shift = 0; shift < this._width; shift += 2) { + const revShift = this._width - shift - 2; + this._bitrev[j] |= ((j >>> shift) & 3) << revShift; + } + } + } + + /** + * Create a complex number array with size `2 * size` + * + * @returns {Float64Array} A complex number array with size `2 * size` + */ + createComplexArray() { + return new Float64Array(this._csize); + } + + /** + * Converts a complex number representation stored in a Float64Array to an array of real numbers. + * + * @param {Float64Array} complex The complex number representation to be converted. + * @param {number[]} [storage] An optional array to store the result in. + * @returns {number[]} An array of real numbers representing the input complex number representation. + */ + fromComplexArray(complex, storage) { + const res = storage || new Array(complex.length >>> 1); + for (let i = 0; i < complex.length; i += 2) + res[i >>> 1] = complex[i]; + return res; + } + + /** + * Convert a real-valued input array to a complex-valued output array. + * @param {Float64Array} input The real-valued input array. + * @param {Float64Array} [storage] Optional buffer to store the output array. + * @returns {Float64Array} The complex-valued output array. + */ + toComplexArray(input, storage) { + const res = storage || this.createComplexArray(); + for (let i = 0; i < res.length; i += 2) { + res[i] = input[i >>> 1]; + res[i + 1] = 0; + } + return res; + } + + /** + * Performs a Fast Fourier Transform (FFT) on the given input data and stores the result in the output buffer. + * + * @param {Float64Array} out The output buffer to store the result. + * @param {Float64Array} data The input data to transform. + * + * @throws {Error} Input and output buffers must be different. + * + * @returns {void} + */ + transform(out, data) { + if (out === data) + throw new Error('Input and output buffers must be different'); + + this._transform4(out, data, 1 /* DONE */); + } + + /** + * Performs a real-valued forward FFT on the given input buffer and stores the result in the given output buffer. + * The input buffer must contain real values only, while the output buffer will contain complex values. The input and + * output buffers must be different. + * + * @param {Float64Array} out The output buffer. + * @param {Float64Array} data The input buffer containing real values. + * + * @throws {Error} If the input and output buffers are the same. + */ + realTransform(out, data) { + if (out === data) + throw new Error('Input and output buffers must be different'); + + this._realTransform4(out, data, 1 /* DONE */); + } + + /** + * Performs an inverse FFT transformation on the given `data` array, and stores the result in `out`. + * The `out` array must be a different buffer than the `data` array. The `out` array will contain the + * result of the transformation. The `data` array will not be modified. + * + * @param {Float64Array} out The output buffer for the transformed data. + * @param {Float64Array} data The input data to transform. + * @throws {Error} If `out` and `data` refer to the same buffer. + * @returns {void} + */ + inverseTransform(out, data) { + if (out === data) + throw new Error('Input and output buffers must be different'); + + this._transform4(out, data, -1 /* DONE */); + for (let i = 0; i < out.length; ++i) + out[i] /= this.size; + } + + /** + * Performs a radix-4 implementation of a discrete Fourier transform on a given set of data. + * + * @param {Float64Array} out The output buffer for the transformed data. + * @param {Float64Array} data The input buffer of data to be transformed. + * @param {number} inv A scaling factor to apply to the transform. + * @returns {void} + */ + _transform4(out, data, inv) { + // radix-4 implementation + + const size = this._csize; + + // Initial step (permute and transform) + const width = this._width; + let step = 1 << width; + let len = (size / step) << 1; + + let outOff; + let t; + const bitrev = this._bitrev; + if (len === 4) { + for (outOff = 0, t = 0; outOff < size; outOff += len, ++t) { + const off = bitrev[t]; + this._singleTransform2(data, out, outOff, off, step); + } + } else { + // len === 8 + for (outOff = 0, t = 0; outOff < size; outOff += len, ++t) { + const off = bitrev[t]; + this._singleTransform4(data, out, outOff, off, step, inv); + } + } + + // Loop through steps in decreasing order + const table = this.table; + for (step >>= 2; step >= 2; step >>= 2) { + len = (size / step) << 1; + const quarterLen = len >>> 2; + + // Loop through offsets in the data + for (outOff = 0; outOff < size; outOff += len) { + // Full case + const limit = outOff + quarterLen - 1; + for (let i = outOff, k = 0; i < limit; i += 2, k += step) { + const A = i; + const B = A + quarterLen; + const C = B + quarterLen; + const D = C + quarterLen; + + // Original values + const Ar = out[A]; + const Ai = out[A + 1]; + const Br = out[B]; + const Bi = out[B + 1]; + const Cr = out[C]; + const Ci = out[C + 1]; + const Dr = out[D]; + const Di = out[D + 1]; + + const tableBr = table[k]; + const tableBi = inv * table[k + 1]; + const MBr = Br * tableBr - Bi * tableBi; + const MBi = Br * tableBi + Bi * tableBr; + + const tableCr = table[2 * k]; + const tableCi = inv * table[2 * k + 1]; + const MCr = Cr * tableCr - Ci * tableCi; + const MCi = Cr * tableCi + Ci * tableCr; + + const tableDr = table[3 * k]; + const tableDi = inv * table[3 * k + 1]; + const MDr = Dr * tableDr - Di * tableDi; + const MDi = Dr * tableDi + Di * tableDr; + + // Pre-Final values + const T0r = Ar + MCr; + const T0i = Ai + MCi; + const T1r = Ar - MCr; + const T1i = Ai - MCi; + const T2r = MBr + MDr; + const T2i = MBi + MDi; + const T3r = inv * (MBr - MDr); + const T3i = inv * (MBi - MDi); + + // Final values + out[A] = T0r + T2r; + out[A + 1] = T0i + T2i; + out[B] = T1r + T3i; + out[B + 1] = T1i - T3r; + out[C] = T0r - T2r; + out[C + 1] = T0i - T2i; + out[D] = T1r - T3i; + out[D + 1] = T1i + T3r; + } + } + } + } + + /** + * Performs a radix-2 implementation of a discrete Fourier transform on a given set of data. + * + * @param {Float64Array} data The input buffer of data to be transformed. + * @param {Float64Array} out The output buffer for the transformed data. + * @param {number} outOff The offset at which to write the output data. + * @param {number} off The offset at which to begin reading the input data. + * @param {number} step The step size for indexing the input data. + * @returns {void} + */ + _singleTransform2(data, out, outOff, off, step) { + // radix-2 implementation + // NOTE: Only called for len=4 + + const evenR = data[off]; + const evenI = data[off + 1]; + const oddR = data[off + step]; + const oddI = data[off + step + 1]; + + out[outOff] = evenR + oddR; + out[outOff + 1] = evenI + oddI; + out[outOff + 2] = evenR - oddR; + out[outOff + 3] = evenI - oddI; + } + + /** + * Performs radix-4 transformation on input data of length 8 + * + * @param {Float64Array} data Input data array of length 8 + * @param {Float64Array} out Output data array of length 8 + * @param {number} outOff Index of output array to start writing from + * @param {number} off Index of input array to start reading from + * @param {number} step Step size between elements in input array + * @param {number} inv Scaling factor for inverse transform + * + * @returns {void} + */ + _singleTransform4(data, out, outOff, off, step, inv) { + // radix-4 + // NOTE: Only called for len=8 + const step2 = step * 2; + const step3 = step * 3; + + // Original values + const Ar = data[off]; + const Ai = data[off + 1]; + const Br = data[off + step]; + const Bi = data[off + step + 1]; + const Cr = data[off + step2]; + const Ci = data[off + step2 + 1]; + const Dr = data[off + step3]; + const Di = data[off + step3 + 1]; + + // Pre-Final values + const T0r = Ar + Cr; + const T0i = Ai + Ci; + const T1r = Ar - Cr; + const T1i = Ai - Ci; + const T2r = Br + Dr; + const T2i = Bi + Di; + const T3r = inv * (Br - Dr); + const T3i = inv * (Bi - Di); + + // Final values + out[outOff] = T0r + T2r; + out[outOff + 1] = T0i + T2i; + out[outOff + 2] = T1r + T3i; + out[outOff + 3] = T1i - T3r; + out[outOff + 4] = T0r - T2r; + out[outOff + 5] = T0i - T2i; + out[outOff + 6] = T1r - T3i; + out[outOff + 7] = T1i + T3r; + } + + /** + * Real input radix-4 implementation + * @param {Float64Array} out Output array for the transformed data + * @param {Float64Array} data Input array of real data to be transformed + * @param {number} inv The scale factor used to normalize the inverse transform + */ + _realTransform4(out, data, inv) { + // Real input radix-4 implementation + const size = this._csize; + + // Initial step (permute and transform) + const width = this._width; + let step = 1 << width; + let len = (size / step) << 1; + + let outOff; + let t; + const bitrev = this._bitrev; + if (len === 4) { + for (outOff = 0, t = 0; outOff < size; outOff += len, ++t) { + const off = bitrev[t]; + this._singleRealTransform2(data, out, outOff, off >>> 1, step >>> 1); + } + } else { + // len === 8 + for (outOff = 0, t = 0; outOff < size; outOff += len, ++t) { + const off = bitrev[t]; + this._singleRealTransform4(data, out, outOff, off >>> 1, step >>> 1, inv); + } + } + + // Loop through steps in decreasing order + const table = this.table; + for (step >>= 2; step >= 2; step >>= 2) { + len = (size / step) << 1; + const halfLen = len >>> 1; + const quarterLen = halfLen >>> 1; + const hquarterLen = quarterLen >>> 1; + + // Loop through offsets in the data + for (outOff = 0; outOff < size; outOff += len) { + for (let i = 0, k = 0; i <= hquarterLen; i += 2, k += step) { + const A = outOff + i; + const B = A + quarterLen; + const C = B + quarterLen; + const D = C + quarterLen; + + // Original values + const Ar = out[A]; + const Ai = out[A + 1]; + const Br = out[B]; + const Bi = out[B + 1]; + const Cr = out[C]; + const Ci = out[C + 1]; + const Dr = out[D]; + const Di = out[D + 1]; + + // Middle values + const MAr = Ar; + const MAi = Ai; + + const tableBr = table[k]; + const tableBi = inv * table[k + 1]; + const MBr = Br * tableBr - Bi * tableBi; + const MBi = Br * tableBi + Bi * tableBr; + + const tableCr = table[2 * k]; + const tableCi = inv * table[2 * k + 1]; + const MCr = Cr * tableCr - Ci * tableCi; + const MCi = Cr * tableCi + Ci * tableCr; + + const tableDr = table[3 * k]; + const tableDi = inv * table[3 * k + 1]; + const MDr = Dr * tableDr - Di * tableDi; + const MDi = Dr * tableDi + Di * tableDr; + + // Pre-Final values + const T0r = MAr + MCr; + const T0i = MAi + MCi; + const T1r = MAr - MCr; + const T1i = MAi - MCi; + const T2r = MBr + MDr; + const T2i = MBi + MDi; + const T3r = inv * (MBr - MDr); + const T3i = inv * (MBi - MDi); + + // Final values + out[A] = T0r + T2r; + out[A + 1] = T0i + T2i; + out[B] = T1r + T3i; + out[B + 1] = T1i - T3r; + + // Output final middle point + if (i === 0) { + out[C] = T0r - T2r; + out[C + 1] = T0i - T2i; + continue; + } + + // Do not overwrite ourselves + if (i === hquarterLen) + continue; + + const SA = outOff + quarterLen - i; + const SB = outOff + halfLen - i; + + out[SA] = T1r - inv * T3i; + out[SA + 1] = -T1i - inv * T3r; + out[SB] = T0r - inv * T2r; + out[SB + 1] = -T0i + inv * T2i; + } + } + } + + // Complete the spectrum by adding its mirrored negative frequency components. + const half = size >>> 1; + for (let i = 2; i < half; i += 2) { + out[size - i] = out[i]; + out[size - i + 1] = -out[i + 1]; + } + } + + /** + * Performs a single real input radix-2 transformation on the provided data + * + * @param {Float64Array} data The input data array + * @param {Float64Array} out The output data array + * @param {number} outOff The output offset + * @param {number} off The input offset + * @param {number} step The step + * + * @returns {void} + */ + _singleRealTransform2(data, out, outOff, off, step) { + // radix-2 implementation + // NOTE: Only called for len=4 + + const evenR = data[off]; + const oddR = data[off + step]; + + out[outOff] = evenR + oddR; + out[outOff + 1] = 0; + out[outOff + 2] = evenR - oddR; + out[outOff + 3] = 0; + } + + /** + * Computes a single real-valued transform using radix-4 algorithm. + * This method is only called for len=8. + * + * @param {Float64Array} data The input data array. + * @param {Float64Array} out The output data array. + * @param {number} outOff The offset into the output array. + * @param {number} off The offset into the input array. + * @param {number} step The step size for the input array. + * @param {number} inv The value of inverse. + */ + _singleRealTransform4(data, out, outOff, off, step, inv) { + // radix-4 + // NOTE: Only called for len=8 + const step2 = step * 2; + const step3 = step * 3; + + // Original values + const Ar = data[off]; + const Br = data[off + step]; + const Cr = data[off + step2]; + const Dr = data[off + step3]; + + // Pre-Final values + const T0r = Ar + Cr; + const T1r = Ar - Cr; + const T2r = Br + Dr; + const T3r = inv * (Br - Dr); + + // Final values + out[outOff] = T0r + T2r; + out[outOff + 1] = 0; + out[outOff + 2] = T1r; + out[outOff + 3] = -T3r; + out[outOff + 4] = T0r - T2r; + out[outOff + 5] = 0; + out[outOff + 6] = T1r; + out[outOff + 7] = T3r; + } +} + +/** + * NP2FFT class provides functionality for performing Fast Fourier Transform on arrays + * which are not a power of two in length. In such cases, the chirp-z transform is used. + * + * For more information, see: https://math.stackexchange.com/questions/77118/non-power-of-2-ffts/77156#77156 + */ +class NP2FFT { + + /** + * Constructs a new NP2FFT object. + * @param {number} fft_length The length of the FFT + */ + constructor(fft_length) { + // Helper variables + const a = 2 * (fft_length - 1); + const b = 2 * (2 * fft_length - 1); + const nextP2 = 2 ** (Math.ceil(Math.log2(b))) + this.bufferSize = nextP2; + this._a = a; + + // Define buffers + // Compute chirp for transform + const chirp = new Float64Array(b); + const ichirp = new Float64Array(nextP2); + this._chirpBuffer = new Float64Array(nextP2); + this._buffer1 = new Float64Array(nextP2); + this._buffer2 = new Float64Array(nextP2); + this._outBuffer1 = new Float64Array(nextP2); + this._outBuffer2 = new Float64Array(nextP2); + + // Compute complex exponentiation + const theta = -2 * Math.PI / fft_length; + const baseR = Math.cos(theta); + const baseI = Math.sin(theta); + + // Precompute helper for chirp-z transform + for (let i = 0; i < b >> 1; ++i) { + // Compute complex power: + const e = (i + 1 - fft_length) ** 2 / 2.0; + + // Compute the modulus and argument of the result + const result_mod = Math.sqrt(baseR ** 2 + baseI ** 2) ** e; + const result_arg = e * Math.atan2(baseI, baseR); + + // Convert the result back to rectangular form + // and assign to chirp and ichirp + const i2 = 2 * i; + chirp[i2] = result_mod * Math.cos(result_arg); + chirp[i2 + 1] = result_mod * Math.sin(result_arg); + + // conjugate + ichirp[i2] = chirp[i2]; + ichirp[i2 + 1] = - chirp[i2 + 1]; + } + this._slicedChirpBuffer = chirp.subarray(a, b); + + // create object to perform Fast Fourier Transforms + // with `nextP2` complex numbers + this._f = new P2FFT(nextP2 >> 1); + this._f.transform(this._chirpBuffer, ichirp); + } + + _transform(output, input, real) { + const ib1 = this._buffer1; + const ib2 = this._buffer2; + const ob2 = this._outBuffer1; + const ob3 = this._outBuffer2; + const cb = this._chirpBuffer; + const sb = this._slicedChirpBuffer; + const a = this._a; + + if (real) { + // Real multiplication + for (let j = 0; j < sb.length; j += 2) { + const j2 = j + 1 + const j3 = j >> 1; + + const a_real = input[j3]; + ib1[j] = a_real * sb[j]; + ib1[j2] = a_real * sb[j2]; + } + } else { + // Complex multiplication + for (let j = 0; j < sb.length; j += 2) { + const j2 = j + 1 + ib1[j] = input[j] * sb[j] - input[j2] * sb[j2]; + ib1[j2] = input[j] * sb[j2] + input[j2] * sb[j]; + } + } + this._f.transform(ob2, ib1); + + for (let j = 0; j < cb.length; j += 2) { + const j2 = j + 1; + + ib2[j] = ob2[j] * cb[j] - ob2[j2] * cb[j2]; + ib2[j2] = ob2[j] * cb[j2] + ob2[j2] * cb[j]; + } + this._f.inverseTransform(ob3, ib2); + + for (let j = 0; j < ob3.length; j += 2) { + const a_real = ob3[j + a]; + const a_imag = ob3[j + a + 1]; + const b_real = sb[j]; + const b_imag = sb[j + 1]; + + output[j] = a_real * b_real - a_imag * b_imag; + output[j + 1] = a_real * b_imag + a_imag * b_real; + } + } + + transform(output, input) { + this._transform(output, input, false); + } + + realTransform(output, input) { + this._transform(output, input, true); + } +} + +class FFT { + constructor(fft_length) { + this.fft_length = fft_length; + this.isPowerOfTwo = isPowerOfTwo(fft_length); + if (this.isPowerOfTwo) { + this.fft = new P2FFT(fft_length); + this.outputBufferSize = 2 * fft_length; + } else { + this.fft = new NP2FFT(fft_length); + this.outputBufferSize = this.fft.bufferSize; + } + } + + realTransform(out, input) { + this.fft.realTransform(out, input); + } + + transform(out, input) { + this.fft.transform(out, input); + } +} + + +/** + * Performs median filter on the provided data. Padding is done by mirroring the data. + * @param {AnyTypedArray} data The input array + * @param {number} windowSize The window size + */ +function medianFilter(data, windowSize) { + + if (windowSize % 2 === 0 || windowSize <= 0) { + throw new Error('Window size must be a positive odd number'); + } + + // @ts-ignore + const outputArray = new data.constructor(data.length); + + // @ts-ignore + const buffer = new data.constructor(windowSize); // Reusable array for storing values + + const halfWindowSize = Math.floor(windowSize / 2); + + for (let i = 0; i < data.length; ++i) { + let valuesIndex = 0; + + for (let j = -halfWindowSize; j <= halfWindowSize; ++j) { + let index = i + j; + if (index < 0) { + index = Math.abs(index); + } else if (index >= data.length) { + index = 2 * (data.length - 1) - index; + } + + buffer[valuesIndex++] = data[index]; + } + + buffer.sort(); + outputArray[i] = buffer[halfWindowSize]; + } + + return outputArray; +} + +/** + * Helper function to round a number to a given number of decimals + * @param {number} num The number to round + * @param {number} decimals The number of decimals + * @returns {number} The rounded number + */ +function round(num, decimals) { + const pow = Math.pow(10, decimals); + return Math.round(num * pow) / pow; +} + +/** + * Helper function to round a number to the nearest integer, with ties rounded to the nearest even number. + * Also known as "bankers' rounding". This is the default rounding mode in python. For example: + * 1.5 rounds to 2 and 2.5 rounds to 2. + * + * @param {number} x The number to round + * @returns {number} The rounded number + */ +function bankers_round(x) { + const r = Math.round(x); + const br = Math.abs(x) % 1 === 0.5 ? (r % 2 === 0 ? r : r - 1) : r; + return br; +} + + +/** + * Measures similarity between two temporal sequences (e.g., input audio and output tokens + * to generate token-level timestamps). + * @param {number[][]} matrix + * @returns {number[][]} + */ +function dynamic_time_warping(matrix) { + const output_length = matrix.length; + const input_length = matrix[0].length; + + const outputShape = [output_length + 1, input_length + 1]; + + const cost = Array.from( + { length: outputShape[0] }, + () => Array(outputShape[1]).fill(Infinity) + ); + cost[0][0] = 0; + + const trace = Array.from( + { length: outputShape[0] }, + () => Array(outputShape[1]).fill(-1) + ); + + for (let j = 1; j < outputShape[1]; ++j) { + for (let i = 1; i < outputShape[0]; ++i) { + const c0 = cost[i - 1][j - 1]; + const c1 = cost[i - 1][j]; + const c2 = cost[i][j - 1]; + + let c, t; + if (c0 < c1 && c0 < c2) { + c = c0; + t = 0; + } else if (c1 < c0 && c1 < c2) { + c = c1; + t = 1; + } else { + c = c2; + t = 2; + } + cost[i][j] = matrix[i - 1][j - 1] + c; + trace[i][j] = t; + } + } + + for (let i = 0; i < outputShape[1]; ++i) { // trace[0, :] = 2 + trace[0][i] = 2; + } + for (let i = 0; i < outputShape[0]; ++i) { // trace[:, 0] = 1 + trace[i][0] = 1; + } + + // backtrace + let i = output_length; + let j = input_length; + let text_indices = []; + let time_indices = []; + while (i > 0 || j > 0) { + text_indices.push(i - 1); + time_indices.push(j - 1); + + switch (trace[i][j]) { + case 0: + --i; --j; + break; + case 1: + --i; + break; + case 2: + --j; + break; + default: + throw new Error( + `Internal error in dynamic time warping. Unexpected trace[${i}, ${j}]. Please file a bug report.` + ) + } + } + + text_indices.reverse(); + time_indices.reverse(); + + return [text_indices, time_indices]; + +} + + +/***/ }), + +/***/ "./src/utils/tensor.js": +/*!*****************************!*\ + !*** ./src/utils/tensor.js ***! + \*****************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ Tensor: () => (/* binding */ Tensor), +/* harmony export */ cat: () => (/* binding */ cat), +/* harmony export */ full: () => (/* binding */ full), +/* harmony export */ full_like: () => (/* binding */ full_like), +/* harmony export */ interpolate: () => (/* binding */ interpolate), +/* harmony export */ interpolate_4d: () => (/* binding */ interpolate_4d), +/* harmony export */ layer_norm: () => (/* binding */ layer_norm), +/* harmony export */ matmul: () => (/* binding */ matmul), +/* harmony export */ mean: () => (/* binding */ mean), +/* harmony export */ mean_pooling: () => (/* binding */ mean_pooling), +/* harmony export */ ones: () => (/* binding */ ones), +/* harmony export */ ones_like: () => (/* binding */ ones_like), +/* harmony export */ permute: () => (/* binding */ permute), +/* harmony export */ quantize_embeddings: () => (/* binding */ quantize_embeddings), +/* harmony export */ rfft: () => (/* binding */ rfft), +/* harmony export */ stack: () => (/* binding */ stack), +/* harmony export */ std_mean: () => (/* binding */ std_mean), +/* harmony export */ topk: () => (/* binding */ topk), +/* harmony export */ zeros: () => (/* binding */ zeros), +/* harmony export */ zeros_like: () => (/* binding */ zeros_like) +/* harmony export */ }); +/* harmony import */ var _maths_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./maths.js */ "./src/utils/maths.js"); +/* harmony import */ var _backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../backends/onnx.js */ "./src/backends/onnx.js"); +/* harmony import */ var _ops_registry_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../ops/registry.js */ "./src/ops/registry.js"); +/** + * @file Helper module for `Tensor` processing. + * + * These functions and classes are only used internally, + * meaning an end-user shouldn't need to access anything here. + * + * @module utils/tensor + */ + + + + + + + +const DataTypeMap = Object.freeze({ + float32: Float32Array, + float16: Uint16Array, + float64: Float64Array, + string: Array, // string[] + int8: Int8Array, + uint8: Uint8Array, + int16: Int16Array, + uint16: Uint16Array, + int32: Int32Array, + uint32: Uint32Array, + int64: BigInt64Array, + uint64: BigUint64Array, + bool: Uint8Array, +}); + +/** + * @typedef {keyof typeof DataTypeMap} DataType + * @typedef {import('./maths.js').AnyTypedArray | any[]} DataArray + */ + + +class Tensor { + /** @type {number[]} Dimensions of the tensor. */ + get dims() { + // @ts-ignore + return this.ort_tensor.dims; + } + set dims(value) { + // FIXME: ONNXTensor declares dims as readonly so one needs to use the constructor() if dims change. + // @ts-ignore + this.ort_tensor.dims = value; + } + + /** @type {DataType} Type of the tensor. */ + get type() { + return this.ort_tensor.type; + }; + + /** @type {DataArray} The data stored in the tensor. */ + get data() { + return this.ort_tensor.data; + } + + /** @type {number} The number of elements in the tensor. */ + get size() { + return this.ort_tensor.size; + }; + + /** @type {string} The location of the tensor data. */ + get location() { + return this.ort_tensor.location; + }; + + ort_tensor; + + /** + * Create a new Tensor or copy an existing Tensor. + * @param {[DataType, DataArray, number[]]|[ONNXTensor]} args + */ + constructor(...args) { + if ((0,_backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.isONNXTensor)(args[0])) { + this.ort_tensor = /** @type {ONNXTensor} */ (args[0]); + } else { + // Create new tensor + this.ort_tensor = new _backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.Tensor( + /** @type {DataType} */(args[0]), + /** @type {Exclude} */(args[1]), + args[2] + ); + } + + return new Proxy(this, { + get: (obj, key) => { + if (typeof key === 'string') { + let index = Number(key); + if (Number.isInteger(index)) { + // key is an integer (i.e., index) + return obj._getitem(index); + } + } + // @ts-ignore + return obj[key]; + }, + set: (obj, key, value) => { + // TODO allow setting of data + + // @ts-ignore + return obj[key] = value; + } + }); + } + + dispose() { + this.ort_tensor.dispose(); + // this.ort_tensor = undefined; + } + + /** + * Returns an iterator object for iterating over the tensor data in row-major order. + * If the tensor has more than one dimension, the iterator will yield subarrays. + * @returns {Iterator} An iterator object for iterating over the tensor data in row-major order. + */ + *[Symbol.iterator]() { + const [iterLength, ...iterDims] = this.dims; + + if (iterDims.length > 0) { + const iterSize = iterDims.reduce((a, b) => a * b); + for (let i = 0; i < iterLength; ++i) { + yield this._subarray(i, iterSize, iterDims); + } + } else { + yield* this.data + } + + } + + /** + * Index into a Tensor object. + * @param {number} index The index to access. + * @returns {Tensor} The data at the specified index. + */ + _getitem(index) { + const [iterLength, ...iterDims] = this.dims; + + index = safeIndex(index, iterLength); + + if (iterDims.length > 0) { + const iterSize = iterDims.reduce((a, b) => a * b); + return this._subarray(index, iterSize, iterDims); + } else { + return new Tensor(this.type, [this.data[index]], iterDims); + } + } + + /** + * @param {number|bigint} item The item to search for in the tensor + * @returns {number} The index of the first occurrence of item in the tensor data. + */ + indexOf(item) { + const this_data = this.data; + for (let index = 0; index < this_data.length; ++index) { + // Note: == instead of === so we can match Ints with BigInts + if (this_data[index] == item) { + return index; + } + } + return -1; + } + + /** + * @param {number} index + * @param {number} iterSize + * @param {any} iterDims + * @returns {Tensor} + */ + _subarray(index, iterSize, iterDims) { + const o1 = index * iterSize; + const o2 = (index + 1) * iterSize; + + // We use subarray if available (typed array), otherwise we use slice (normal array) + const data = + ('subarray' in this.data) + ? this.data.subarray(o1, o2) + : this.data.slice(o1, o2); + return new Tensor(this.type, data, iterDims); + } + + /** + * Returns the value of this tensor as a standard JavaScript Number. This only works + * for tensors with one element. For other cases, see `Tensor.tolist()`. + * @returns {number|bigint} The value of this tensor as a standard JavaScript Number. + * @throws {Error} If the tensor has more than one element. + */ + item() { + const this_data = this.data; + if (this_data.length !== 1) { + throw new Error(`a Tensor with ${this_data.length} elements cannot be converted to Scalar`); + } + return this_data[0]; + } + + /** + * Convert tensor data to a n-dimensional JS list + * @returns {Array} + */ + tolist() { + return reshape(this.data, this.dims) + } + + /** + * Return a new Tensor with the sigmoid function applied to each element. + * @returns {Tensor} The tensor with the sigmoid function applied. + */ + sigmoid() { + return this.clone().sigmoid_(); + } + + /** + * Applies the sigmoid function to the tensor in place. + * @returns {Tensor} Returns `this`. + */ + sigmoid_() { + const this_data = this.data; + for (let i = 0; i < this_data.length; ++i) { + this_data[i] = 1 / (1 + Math.exp(-this_data[i])); + } + return this; + } + + /** + * Return a new Tensor with a callback function applied to each element. + * @param {Function} callback - The function to apply to each element. It should take three arguments: + * the current element, its index, and the tensor's data array. + * @returns {Tensor} A new Tensor with the callback function applied to each element. + */ + map(callback) { + return this.clone().map_(callback); + } + + /** + * Apply a callback function to each element of the tensor in place. + * @param {Function} callback - The function to apply to each element. It should take three arguments: + * the current element, its index, and the tensor's data array. + * @returns {Tensor} Returns `this`. + */ + map_(callback) { + const this_data = this.data; + for (let i = 0; i < this_data.length; ++i) { + this_data[i] = callback(this_data[i], i, this_data); + } + return this; + } + + /** + * Return a new Tensor with every element multiplied by a constant. + * @param {number} val The value to multiply by. + * @returns {Tensor} The new tensor. + */ + mul(val) { + return this.clone().mul_(val); + } + + /** + * Multiply the tensor by a constant in place. + * @param {number} val The value to multiply by. + * @returns {Tensor} Returns `this`. + */ + mul_(val) { + const this_data = this.data; + for (let i = 0; i < this_data.length; ++i) { + this_data[i] *= val; + } + return this; + } + + /** + * Return a new Tensor with every element divided by a constant. + * @param {number} val The value to divide by. + * @returns {Tensor} The new tensor. + */ + div(val) { + return this.clone().div_(val); + } + + /** + * Divide the tensor by a constant in place. + * @param {number} val The value to divide by. + * @returns {Tensor} Returns `this`. + */ + div_(val) { + const this_data = this.data; + for (let i = 0; i < this_data.length; ++i) { + this_data[i] /= val; + } + return this; + } + + /** + * Return a new Tensor with every element added by a constant. + * @param {number} val The value to add by. + * @returns {Tensor} The new tensor. + */ + add(val) { + return this.clone().add_(val); + } + + /** + * Add the tensor by a constant in place. + * @param {number} val The value to add by. + * @returns {Tensor} Returns `this`. + */ + add_(val) { + const this_data = this.data; + for (let i = 0; i < this_data.length; ++i) { + this_data[i] += val; + } + return this; + } + + /** + * Return a new Tensor with every element subtracted by a constant. + * @param {number} val The value to subtract by. + * @returns {Tensor} The new tensor. + */ + sub(val) { + return this.clone().sub_(val); + } + + /** + * Subtract the tensor by a constant in place. + * @param {number} val The value to subtract by. + * @returns {Tensor} Returns `this`. + */ + sub_(val) { + const this_data = this.data; + for (let i = 0; i < this_data.length; ++i) { + this_data[i] -= val; + } + return this; + } + + clone() { + return new Tensor(this.type, this.data.slice(), this.dims.slice()); + } + + slice(...slices) { + // This allows for slicing with ranges and numbers + const newTensorDims = []; + const newOffsets = []; + + // slices is an array of numbers or arrays of numbers + // e.g., slices = [0, [1, 3], null, [0, 3]] + for (let sliceIndex = 0; sliceIndex < this.dims.length; ++sliceIndex) { + let slice = slices[sliceIndex]; + + if (slice === null || slice === undefined) { + // null or undefined means take the whole dimension + newOffsets.push([0, this.dims[sliceIndex]]); + newTensorDims.push(this.dims[sliceIndex]); + + } else if (typeof slice === 'number') { + slice = safeIndex(slice, this.dims[sliceIndex], sliceIndex); + + // A number means take a single element + newOffsets.push([slice, slice + 1]); + + } else if (Array.isArray(slice) && slice.length === 2) { + // An array of length 2 means take a range of elements + let [start, end] = slice; + start = start === null + ? 0 + : safeIndex(start, this.dims[sliceIndex], sliceIndex, false); + end = end === null + ? this.dims[sliceIndex] + : safeIndex(end, this.dims[sliceIndex], sliceIndex, false); + + if (start > end) { + throw new Error(`Invalid slice: ${slice}`); + } + + const offsets = [ + Math.max(start, 0), + Math.min(end, this.dims[sliceIndex]) + ]; + + newOffsets.push(offsets); + newTensorDims.push(offsets[1] - offsets[0]); + + } else { + throw new Error(`Invalid slice: ${slice}`); + } + } + + const newDims = newOffsets.map(([start, end]) => end - start); + const newBufferSize = newDims.reduce((a, b) => a * b); + + const this_data = this.data; + // Allocate memory + // @ts-ignore + const data = new this_data.constructor(newBufferSize); + + // Precompute strides + const stride = this.stride(); + + for (let i = 0; i < newBufferSize; ++i) { + let originalIndex = 0; + for (let j = newDims.length - 1, num = i; j >= 0; --j) { + const size = newDims[j]; + originalIndex += ((num % size) + newOffsets[j][0]) * stride[j]; + num = Math.floor(num / size); + } + data[i] = this_data[originalIndex]; + } + return new Tensor(this.type, data, newTensorDims); + + } + + /** + * Return a permuted version of this Tensor, according to the provided dimensions. + * @param {...number} dims Dimensions to permute. + * @returns {Tensor} The permuted tensor. + */ + permute(...dims) { + return permute(this, dims); + } + + // TODO: implement transpose. For now (backwards compatibility), it's just an alias for permute() + transpose(...dims) { + return this.permute(...dims); + } + + // TODO add .max() and .min() methods + + /** + * Returns the sum of each row of the input tensor in the given dimension dim. + * + * @param {number} [dim=null] The dimension or dimensions to reduce. If `null`, all dimensions are reduced. + * @param {boolean} keepdim Whether the output tensor has `dim` retained or not. + * @returns The summed tensor + */ + sum(dim = null, keepdim = false) { + return this.norm(1, dim, keepdim); + } + + /** + * Returns the matrix norm or vector norm of a given tensor. + * @param {number|string} [p='fro'] The order of norm + * @param {number} [dim=null] Specifies which dimension of the tensor to calculate the norm across. + * If dim is None, the norm will be calculated across all dimensions of input. + * @param {boolean} [keepdim=false] Whether the output tensors have dim retained or not. + * @returns {Tensor} The norm of the tensor. + */ + norm(p = 'fro', dim = null, keepdim = false) { + if (p === 'fro') { + // NOTE: Since we only support integer dims, Frobenius norm produces the same result as p=2. + p = 2; + } else if (typeof p === 'string') { + throw Error(`Unsupported norm: ${p}`); + } + + const this_data = this.data; + + if (dim === null) { + // @ts-ignore + let val = this_data.reduce((a, b) => a + (b ** p), 0) ** (1 / p); + return new Tensor(this.type, [val], []); + } + + // Negative indexing + dim = safeIndex(dim, this.dims.length); + + // Calculate the shape of the resulting array after summation + const resultDims = this.dims.slice(); // Copy the original dimensions + resultDims[dim] = 1; // Remove the specified axis + + // Create a new array to store the accumulated values + // @ts-ignore + const result = new this_data.constructor(this_data.length / this.dims[dim]); + + // Iterate over the data array + for (let i = 0; i < this_data.length; ++i) { + + // Calculate the index in the resulting array + let resultIndex = 0; + + for (let j = this.dims.length - 1, num = i, resultMultiplier = 1; j >= 0; --j) { + const size = this.dims[j]; + if (j !== dim) { + const index = num % size; + resultIndex += index * resultMultiplier; + resultMultiplier *= resultDims[j]; + } + num = Math.floor(num / size); + } + + // Accumulate the value at the current index + result[resultIndex] += (this_data[i]) ** p; + } + + if (p !== 1) { + for (let i = 0; i < result.length; ++i) { + result[i] = result[i] ** (1 / p); + } + } + + if (!keepdim) { + resultDims.splice(dim, 1); + } + + return new Tensor(this.type, result, resultDims); + } + + /** + * Performs `L_p` normalization of inputs over specified dimension. Operates in place. + * @param {number} [p=2] The exponent value in the norm formulation + * @param {number} [dim=1] The dimension to reduce + * @returns {Tensor} `this` for operation chaining. + */ + normalize_(p = 2.0, dim = 1) { + dim = safeIndex(dim, this.dims.length); + + const norm = this.norm(p, dim, true); + + const this_data = this.data; + const norm_data = norm.data; + for (let i = 0; i < this_data.length; ++i) { + + // Calculate the index in the resulting array + let resultIndex = 0; + + for (let j = this.dims.length - 1, num = i, resultMultiplier = 1; j >= 0; --j) { + const size = this.dims[j]; + if (j !== dim) { + const index = num % size; + resultIndex += index * resultMultiplier; + resultMultiplier *= this.dims[j]; + } + num = Math.floor(num / size); + } + + // Divide by normalized value + this_data[i] /= norm_data[resultIndex]; + } + + return this; + } + + /** + * Performs `L_p` normalization of inputs over specified dimension. + * @param {number} [p=2] The exponent value in the norm formulation + * @param {number} [dim=1] The dimension to reduce + * @returns {Tensor} The normalized tensor. + */ + normalize(p = 2.0, dim = 1) { + return this.clone().normalize_(p, dim); + } + + /** + * Compute and return the stride of this tensor. + * Stride is the jump necessary to go from one element to the next one in the specified dimension dim. + * @returns {number[]} The stride of this tensor. + */ + stride() { + return dimsToStride(this.dims); + } + + /** + * Returns a tensor with all specified dimensions of input of size 1 removed. + * + * NOTE: The returned tensor shares the storage with the input tensor, so changing the contents of one will change the contents of the other. + * If you would like a copy, use `tensor.clone()` before squeezing. + * + * @param {number} [dim=null] If given, the input will be squeezed only in the specified dimensions. + * @returns {Tensor} The squeezed tensor + */ + squeeze(dim = null) { + return new Tensor( + this.type, + this.data, + calc_squeeze_dims(this.dims, dim) + ) + } + + /** + * In-place version of @see {@link Tensor.squeeze} + */ + squeeze_(dim = null) { + this.dims = calc_squeeze_dims(this.dims, dim); + return this; + } + + /** + * Returns a new tensor with a dimension of size one inserted at the specified position. + * + * NOTE: The returned tensor shares the same underlying data with this tensor. + * + * @param {number} dim The index at which to insert the singleton dimension + * @returns {Tensor} The unsqueezed tensor + */ + unsqueeze(dim = null) { + return new Tensor( + this.type, + this.data, + calc_unsqueeze_dims(this.dims, dim) + ); + } + + /** + * In-place version of @see {@link Tensor.unsqueeze} + */ + unsqueeze_(dim = null) { + this.dims = calc_unsqueeze_dims(this.dims, dim); + return this; + } + + /** + * In-place version of @see {@link Tensor.flatten} + */ + flatten_(start_dim = 0, end_dim = -1) { + // TODO validate inputs + end_dim = (end_dim + this.dims.length) % this.dims.length; + + let dimsToKeepBefore = this.dims.slice(0, start_dim); + let dimsToFlatten = this.dims.slice(start_dim, end_dim + 1); + let dimsToKeepAfter = this.dims.slice(end_dim + 1); + + this.dims = [...dimsToKeepBefore, dimsToFlatten.reduce((a, b) => a * b, 1), ...dimsToKeepAfter] + return this; + } + + /** + * Flattens input by reshaping it into a one-dimensional tensor. + * If `start_dim` or `end_dim` are passed, only dimensions starting with `start_dim` + * and ending with `end_dim` are flattened. The order of elements in input is unchanged. + * @param {number} start_dim the first dim to flatten + * @param {number} end_dim the last dim to flatten + * @returns {Tensor} The flattened tensor. + */ + flatten(start_dim = 0, end_dim = -1) { + return this.clone().flatten_(start_dim, end_dim); + } + + /** + * Returns a new tensor with the same data as the `self` tensor but of a different `shape`. + * @param {...number} dims the desired size + * @returns {Tensor} The tensor with the same data but different shape + */ + view(...dims) { + // TODO: validate dims + let inferredIndex = -1; + for (let i = 0; i < dims.length; ++i) { + if (dims[i] === -1) { + if (inferredIndex !== -1) { + throw new Error("Only one dimension can be inferred"); + } + inferredIndex = i; + } + } + + const this_data = this.data; + if (inferredIndex !== -1) { + // Some dimension must be inferred + const productOther = dims.reduce((product, curr, index) => { + return index !== inferredIndex ? product * curr : product + }, 1); + + dims[inferredIndex] = this_data.length / productOther; + } + return new Tensor(this.type, this_data, dims); // NOTE: uses same underlying storage + } + + neg_() { + const this_data = this.data; + for (let i = 0; i < this_data.length; ++i) { + this_data[i] = -this_data[i]; + } + return this; + } + neg() { + return this.clone().neg_(); + } + + /** + * In-place version of @see {@link Tensor.clamp} + */ + clamp_(min, max) { + const this_data = this.data; + for (let i = 0; i < this_data.length; ++i) { + this_data[i] = Math.min(Math.max(this_data[i], min), max); + } + return this; + } + + /** + * Clamps all elements in input into the range [ min, max ] + * @param {number} min lower-bound of the range to be clamped to + * @param {number} max upper-bound of the range to be clamped to + * @returns {Tensor} the output tensor. + */ + clamp(min, max) { + return this.clone().clamp_(min, max); + } + + /** + * In-place version of @see {@link Tensor.round} + */ + round_() { + const this_data = this.data; + for (let i = 0; i < this_data.length; ++i) { + this_data[i] = Math.round(this_data[i]); + } + return this; + } + + /** + * Rounds elements of input to the nearest integer. + * @returns {Tensor} the output tensor. + */ + round() { + return this.clone().round_(); + } + + mean(dim = null, keepdim = false) { + return mean(this, dim, keepdim); + } + + /** + * Performs Tensor dtype conversion. + * @param {DataType} type The desired data type. + * @returns {Tensor} The converted tensor. + */ + to(type) { + // If the self Tensor already has the correct dtype, then self is returned. + if (this.type === type) return this; + + // Otherwise, the returned tensor is a copy of self with the desired dtype. + if (!DataTypeMap.hasOwnProperty(type)) { + throw new Error(`Unsupported type: ${type}`); + } + // @ts-ignore + return new Tensor(type, DataTypeMap[type].from(this.data), this.dims); + } +} + +/** + * This creates a nested array of a given type and depth (see examples). + * + * @example + * NestArray; // string[] + * @example + * NestArray; // number[][] + * @example + * NestArray; // string[][][] etc. + * @template T + * @template {number} Depth + * @template {never[]} [Acc=[]] + * @typedef {Acc['length'] extends Depth ? T : NestArray} NestArray + */ + +/** + * Reshapes a 1-dimensional array into an n-dimensional array, according to the provided dimensions. + * + * @example + * reshape([10 ], [1 ]); // Type: number[] Value: [10] + * reshape([1, 2, 3, 4 ], [2, 2 ]); // Type: number[][] Value: [[1, 2], [3, 4]] + * reshape([1, 2, 3, 4, 5, 6, 7, 8], [2, 2, 2]); // Type: number[][][] Value: [[[1, 2], [3, 4]], [[5, 6], [7, 8]]] + * reshape([1, 2, 3, 4, 5, 6, 7, 8], [4, 2 ]); // Type: number[][] Value: [[1, 2], [3, 4], [5, 6], [7, 8]] + * @param {T[]|DataArray} data The input array to reshape. + * @param {DIM} dimensions The target shape/dimensions. + * @template T + * @template {[number]|number[]} DIM + * @returns {NestArray} The reshaped array. + */ +function reshape(data, dimensions) { + + const totalElements = data.length; + const dimensionSize = dimensions.reduce((a, b) => a * b); + + if (totalElements !== dimensionSize) { + throw Error(`cannot reshape array of size ${totalElements} into shape (${dimensions})`); + } + + /** @type {any} */ + let reshapedArray = data; + + for (let i = dimensions.length - 1; i >= 0; i--) { + reshapedArray = reshapedArray.reduce((acc, val) => { + let lastArray = acc[acc.length - 1]; + + if (lastArray.length < dimensions[i]) { + lastArray.push(val); + } else { + acc.push([val]); + } + + return acc; + }, [[]]); + } + + return reshapedArray[0]; +} + +/** + * Permutes a tensor according to the provided axes. + * @param {any} tensor The input tensor to permute. + * @param {Array} axes The axes to permute the tensor along. + * @returns {Tensor} The permuted tensor. + */ +function permute(tensor, axes) { + const [permutedData, shape] = (0,_maths_js__WEBPACK_IMPORTED_MODULE_0__.permute_data)(tensor.data, tensor.dims, axes); + return new Tensor(tensor.type, permutedData, shape); +} + + +/** + * Interpolates an Tensor to the given size. + * @param {Tensor} input The input tensor to interpolate. Data must be channel-first (i.e., [c, h, w]) + * @param {number[]} size The output size of the image + * @param {string} mode The interpolation mode + * @param {boolean} align_corners Whether to align corners. + * @returns {Tensor} The interpolated tensor. + */ +function interpolate(input, [out_height, out_width], mode = 'bilinear', align_corners = false) { + + // Input image dimensions + const in_channels = input.dims.at(-3) ?? 1; + const in_height = input.dims.at(-2); + const in_width = input.dims.at(-1); + + let output = (0,_maths_js__WEBPACK_IMPORTED_MODULE_0__.interpolate_data)( + /** @type {import('./maths.js').TypedArray}*/(input.data), + [in_channels, in_height, in_width], + [out_height, out_width], + mode, + align_corners + ); + return new Tensor(input.type, output, [in_channels, out_height, out_width]); +} + + +/** + * Down/up samples the input. + * Inspired by https://pytorch.org/docs/stable/generated/torch.nn.functional.interpolate.html. + * @param {Tensor} input the input tensor + * @param {Object} options the options for the interpolation + * @param {[number, number]|[number, number, number]|[number, number, number, number]} [options.size=null] output spatial size. + * @param {"bilinear"|"bicubic"} [options.mode='bilinear'] algorithm used for upsampling + * @returns {Promise} The interpolated tensor. + */ +async function interpolate_4d(input, { + size = null, + mode = 'bilinear', +} = {}) { + + // Error checking + if (input.dims.length !== 4) { + throw new Error('`interpolate_4d` currently only supports 4D input.'); + } + if (!size) { + // TODO: support scale_factor + throw new Error('`interpolate_4d` requires a `size` argument.'); + } + + // Fill in missing dimensions + let targetDims; + if (size.length === 2) { + targetDims = [...input.dims.slice(0, 2), ...size]; + } else if (size.length === 3) { + targetDims = [input.dims[0], ...size]; + } else if (size.length === 4) { + targetDims = size; + } else { + throw new Error('`size` must be of length 2, 3, or 4.'); + } + + let op; + if (mode === 'bilinear') { + op = await _ops_registry_js__WEBPACK_IMPORTED_MODULE_2__.TensorOpRegistry.bilinear_interpolate_4d; + } else if (mode === 'bicubic') { + op = await _ops_registry_js__WEBPACK_IMPORTED_MODULE_2__.TensorOpRegistry.bicubic_interpolate_4d; + } else { + throw new Error(`Unsupported mode: ${mode}`); + } + + const sizeTensor = new Tensor('int64', new BigInt64Array(targetDims.map(BigInt)), [targetDims.length]); + return await op({ x: input, s: sizeTensor }); +} + +/** + * Matrix product of two tensors. + * Inspired by https://pytorch.org/docs/stable/generated/torch.matmul.html + * @param {Tensor} a the first tensor to be multiplied + * @param {Tensor} b the second tensor to be multiplied + * @returns {Promise} The matrix product of the two tensors. + */ +async function matmul(a, b) { + const op = await _ops_registry_js__WEBPACK_IMPORTED_MODULE_2__.TensorOpRegistry.matmul; + return await op({ a, b }); +} + +/** + * Computes the one dimensional Fourier transform of real-valued input. + * Inspired by https://pytorch.org/docs/stable/generated/torch.fft.rfft.html + * @param {Tensor} x the real input tensor + * @param {Tensor} a The dimension along which to take the one dimensional real FFT. + * @returns {Promise} the output tensor. + */ +async function rfft(x, a) { + const op = await _ops_registry_js__WEBPACK_IMPORTED_MODULE_2__.TensorOpRegistry.rfft; + return await op({ x, a }); +} + + +/** + * Returns the k largest elements of the given input tensor. + * Inspired by https://pytorch.org/docs/stable/generated/torch.topk.html + * @param {Tensor} x the input tensor + * @param {number} k the k in "top-k" + * @returns {Promise<[Tensor, Tensor]>} the output tuple of (Tensor, LongTensor) of top-k elements and their indices. + */ +async function topk(x, k) { + const op = await _ops_registry_js__WEBPACK_IMPORTED_MODULE_2__.TensorOpRegistry.top_k; + + if (k === null) { + k = x.dims.at(-1); + } else { + k = Math.min(k, x.dims.at(-1)); + } + return await op({ + x, + k: new Tensor( + 'int64', + [BigInt(k)], + [1] + ) + }); +} + +/** + * Perform mean pooling of the last hidden state followed by a normalization step. + * @param {Tensor} last_hidden_state Tensor of shape [batchSize, seqLength, embedDim] + * @param {Tensor} attention_mask Tensor of shape [batchSize, seqLength] + * @returns {Tensor} Returns a new Tensor of shape [batchSize, embedDim]. + */ +function mean_pooling(last_hidden_state, attention_mask) { + // last_hidden_state: [batchSize, seqLength, embedDim] + // attention_mask: [batchSize, seqLength] + const lastHiddenStateData = last_hidden_state.data; + const attentionMaskData = attention_mask.data; + + const shape = [last_hidden_state.dims[0], last_hidden_state.dims[2]]; + + // @ts-ignore + const returnedData = new lastHiddenStateData.constructor(shape[0] * shape[1]); + const [batchSize, seqLength, embedDim] = last_hidden_state.dims; + + let outIndex = 0; + for (let i = 0; i < batchSize; ++i) { + const offset = i * embedDim * seqLength; + + for (let k = 0; k < embedDim; ++k) { + let sum = 0; + let count = 0; + + const attnMaskOffset = i * seqLength; + const offset2 = offset + k; + // Pool over all words in sequence + for (let j = 0; j < seqLength; ++j) { + // index into attention mask + const attn = Number(attentionMaskData[attnMaskOffset + j]); + + count += attn; + sum += lastHiddenStateData[offset2 + j * embedDim] * attn; + } + + const avg = sum / count; + returnedData[outIndex++] = avg; + } + } + + return new Tensor( + last_hidden_state.type, + returnedData, + shape + ) +} + +/** + * Apply Layer Normalization for last certain number of dimensions. + * @param {Tensor} input The input tensor + * @param {number[]} normalized_shape input shape from an expected input of size + * @param {Object} options The options for the layer normalization + * @param {number} [options.eps=1e-5] A value added to the denominator for numerical stability. + * @returns {Tensor} The normalized tensor. + */ +function layer_norm(input, normalized_shape, { + eps = 1e-5, +} = {}) { + if (input.dims.length !== 2) { + throw new Error('`layer_norm` currently only supports 2D input.'); + } + + const [batchSize, featureDim] = input.dims; + + if (normalized_shape.length !== 1 && normalized_shape[0] !== featureDim) { + throw new Error('`normalized_shape` must be a 1D array with shape `[input.dims[1]]`.'); + } + + const [std, mean] = std_mean(input, 1, 0, true); + const stdData = /** @type {Float32Array} */(std.data); + const meanData = /** @type {Float32Array} */(mean.data); + + const inputData = /** @type {Float32Array} */(input.data); + + // @ts-ignore + const returnedData = new inputData.constructor(inputData.length); + + for (let i = 0; i < batchSize; ++i) { + const offset = i * featureDim; + for (let j = 0; j < featureDim; ++j) { + const offset2 = offset + j; + returnedData[offset2] = (inputData[offset2] - meanData[i]) / (stdData[i] + eps); + } + } + return new Tensor(input.type, returnedData, input.dims); +} + +/** + * Helper function to calculate new dimensions when performing a squeeze operation. + * @param {number[]} dims The dimensions of the tensor. + * @param {number|number[]|null} dim The dimension(s) to squeeze. + * @returns {number[]} The new dimensions. + * @private + */ +function calc_squeeze_dims(dims, dim) { + dims = dims.slice(); + if (dim === null) { + dims = dims.filter((d) => d !== 1); + } else if (typeof dim === 'number') { + if (dims[dim] === 1) { + dims.splice(dim, 1); + } + } else if (Array.isArray(dim)) { + dims = dims.filter((x, i) => { + return x !== 1 || !dim.includes(i); + }); + } + return dims; +} + +/** + * Helper function to calculate new dimensions when performing an unsqueeze operation. + * @param {number[]} dims The dimensions of the tensor. + * @param {number} dim The dimension to unsqueeze. + * @returns {number[]} The new dimensions. + * @private + */ +function calc_unsqueeze_dims(dims, dim) { + // Dimension out of range (e.g., "expected to be in range of [-4, 3], but got 4") + // + 1 since we allow inserting at the end (i.e. dim = -1) + dim = safeIndex(dim, dims.length + 1); + dims = dims.slice(); + // Insert 1 into specified dimension + dims.splice(dim, 0, 1); + return dims; +} + +/** + * Safely calculate the index for an array of a given size, allowing negative indexing. + * @param {number} index The index that will be used. + * @param {number} size The size of the array. + * @param {number} [dimension=null] The dimension that the index is for (optional). + * @returns {number} The index, guaranteed to be non-negative and less than `arrayLength`. + * + * @throws {Error} If the index is out of range. + * @private + */ +function safeIndex(index, size, dimension = null, boundsCheck = true) { + if (boundsCheck && (index < -size || index >= size)) { + throw new Error(`IndexError: index ${index} is out of bounds for dimension${dimension === null ? '' : ' ' + dimension} with size ${size}`); + } + + if (index < 0) { + // Negative indexing, ensuring positive index + index = ((index % size) + size) % size; + } + return index; +} + +/** + * Concatenates an array of tensors along a specified dimension. + * @param {Tensor[]} tensors The array of tensors to concatenate. + * @param {number} dim The dimension to concatenate along. + * @returns {Tensor} The concatenated tensor. + */ +function cat(tensors, dim = 0) { + dim = safeIndex(dim, tensors[0].dims.length); + + // TODO do validation of shapes + + const resultDims = tensors[0].dims.slice(); + resultDims[dim] = tensors.reduce((a, b) => a + b.dims[dim], 0); + + // Create a new array to store the accumulated values + const resultSize = resultDims.reduce((a, b) => a * b, 1); + // @ts-ignore + const result = new tensors[0].data.constructor(resultSize); + + // Create output tensor of same type as first + const resultType = tensors[0].type; + + if (dim === 0) { + // Handle special case for performance reasons + + let offset = 0; + for (const tensor of tensors) { + const tensorData = tensor.data; + result.set(tensorData, offset); + offset += tensorData.length; + } + + } else { + + let currentDim = 0; + + for (let t = 0; t < tensors.length; ++t) { + const { data, dims } = tensors[t]; + + // Iterate over the data array + for (let i = 0; i < data.length; ++i) { + // Calculate the index in the resulting array + let resultIndex = 0; + + for (let j = dims.length - 1, num = i, resultMultiplier = 1; j >= 0; --j) { + const size = dims[j]; + let index = num % size; + if (j === dim) { + index += currentDim; + } + resultIndex += index * resultMultiplier; + resultMultiplier *= resultDims[j]; + num = Math.floor(num / size); + } + // Accumulate the value at the current index + result[resultIndex] = data[i]; + } + + currentDim += dims[dim]; + } + } + return new Tensor(resultType, result, resultDims); +} + +/** + * Stack an array of tensors along a specified dimension. + * @param {Tensor[]} tensors The array of tensors to stack. + * @param {number} dim The dimension to stack along. + * @returns {Tensor} The stacked tensor. + */ +function stack(tensors, dim = 0) { + // TODO do validation of shapes + // NOTE: stack expects each tensor to be equal size + return cat(tensors.map(t => t.unsqueeze(dim)), dim); +} + + +/** + * Calculates the standard deviation and mean over the dimensions specified by dim. dim can be a single dimension or `null` to reduce over all dimensions. + * @param {Tensor} input the input tenso + * @param {number|null} dim the dimension to reduce. If None, all dimensions are reduced. + * @param {number} correction difference between the sample size and sample degrees of freedom. Defaults to Bessel's correction, correction=1. + * @param {boolean} keepdim whether the output tensor has dim retained or not. + * @returns {Tensor[]} A tuple of (std, mean) tensors. + */ +function std_mean(input, dim = null, correction = 1, keepdim = false) { + const inputData = /** @type {Float32Array} */(input.data); + const inputDims = input.dims; + + if (dim === null) { + // None to reduce over all dimensions. + const sum = inputData.reduce((a, b) => a + b, 0); + const mean = sum / inputData.length; + const std = Math.sqrt(inputData.reduce((a, b) => a + (b - mean) ** 2, 0) / (inputData.length - correction)); + + const meanTensor = new Tensor(input.type, [mean], [/* scalar */]); + const stdTensor = new Tensor(input.type, [std], [/* scalar */]); + + return [stdTensor, meanTensor]; + } + + // Negative indexing + dim = safeIndex(dim, inputDims.length); + + const meanTensor = mean(input, dim, keepdim); + const meanTensorData = meanTensor.data; + + // Calculate the shape of the resulting array after summation + const resultDims = inputDims.slice(); // Copy the original dimensions + resultDims[dim] = 1; // Remove the specified axis + + // Create a new array to store the accumulated values + // @ts-ignore + const result = new inputData.constructor(inputData.length / inputDims[dim]); + + // Iterate over the data array + for (let i = 0; i < inputData.length; ++i) { + + // Calculate the index in the resulting array + let resultIndex = 0; + + for (let j = inputDims.length - 1, num = i, resultMultiplier = 1; j >= 0; --j) { + const size = inputDims[j]; + if (j !== dim) { + const index = num % size; + resultIndex += index * resultMultiplier; + resultMultiplier *= resultDims[j]; + } + num = Math.floor(num / size); + } + + // Accumulate the value at the current index + result[resultIndex] += (inputData[i] - meanTensorData[resultIndex]) ** 2; + } + + for (let i = 0; i < result.length; ++i) { + result[i] = Math.sqrt(result[i] / (inputDims[dim] - correction)); + } + + if (!keepdim) { + resultDims.splice(dim, 1); + } + + const stdTensor = new Tensor(input.type, result, resultDims); + + return [stdTensor, meanTensor]; +} + + +/** + * Returns the mean value of each row of the input tensor in the given dimension dim. + * @param {Tensor} input the input tensor. + * @param {number|null} dim the dimension to reduce. + * @param {boolean} keepdim whether the output tensor has dim retained or not. + * @returns {Tensor} A new tensor with means taken along the specified dimension. + */ +function mean(input, dim = null, keepdim = false) { + const inputData = /** @type {Float32Array} */(input.data); + + if (dim === null) { + // None to reduce over all dimensions. + // @ts-ignore + const val = inputData.reduce((a, b) => a + b, 0); + return new Tensor(input.type, [val / inputData.length], [/* scalar */]); + } + const inputDims = input.dims; + + // Negative indexing + dim = safeIndex(dim, inputDims.length); + + // Calculate the shape of the resulting array after summation + const resultDims = inputDims.slice(); // Copy the original dimensions + resultDims[dim] = 1; // Remove the specified axis + + // Create a new array to store the accumulated values + // @ts-ignore + const result = new inputData.constructor(inputData.length / inputDims[dim]); + + // Iterate over the data array + for (let i = 0; i < inputData.length; ++i) { + + // Calculate the index in the resulting array + let resultIndex = 0; + + for (let j = inputDims.length - 1, num = i, resultMultiplier = 1; j >= 0; --j) { + const size = inputDims[j]; + if (j !== dim) { + const index = num % size; + resultIndex += index * resultMultiplier; + resultMultiplier *= resultDims[j]; + } + num = Math.floor(num / size); + } + + // Accumulate the value at the current index + result[resultIndex] += inputData[i]; + } + + if (inputDims[dim] !== 1) { + for (let i = 0; i < result.length; ++i) { + result[i] = result[i] / inputDims[dim]; + } + } + + if (!keepdim) { + resultDims.splice(dim, 1); + } + + return new Tensor(input.type, result, resultDims); +} + + +function dimsToStride(dims) { + const stride = new Array(dims.length); + for (let i = dims.length - 1, s2 = 1; i >= 0; --i) { + stride[i] = s2; + s2 *= dims[i]; + } + return stride; +} + +function fullHelper(size, fill_value, dtype, cls) { + const numElements = size.reduce((a, b) => a * b, 1); + return new Tensor( + dtype, + new cls(numElements).fill(fill_value), + size + ) +} + +/** + * Creates a tensor of size size filled with fill_value. The tensor's dtype is inferred from fill_value. + * @param {number[]} size A sequence of integers defining the shape of the output tensor. + * @param {number|bigint} fill_value The value to fill the output tensor with. + * @returns {Tensor} The filled tensor. + */ +function full(size, fill_value) { + let dtype; + let typedArrayCls; + if (typeof fill_value === 'number') { + dtype = 'float32'; + typedArrayCls = Float32Array; + } else if (typeof fill_value === 'bigint') { + dtype = 'int64'; + typedArrayCls = BigInt64Array; + } else { + // TODO: support other dtypes + throw new Error(`Unsupported data type: ${typeof fill_value}`); + } + return fullHelper(size, fill_value, dtype, typedArrayCls); +} + +function full_like(tensor, fill_value) { + return full(tensor.dims, fill_value); +} + +/** + * Returns a tensor filled with the scalar value 1, with the shape defined by the variable argument size. + * @param {number[]} size A sequence of integers defining the shape of the output tensor. + * @returns {Tensor} The ones tensor. + */ +function ones(size) { + return fullHelper(size, 1n, 'int64', BigInt64Array); +} + +/** + * Returns a tensor filled with the scalar value 1, with the same size as input. + * @param {Tensor} tensor The size of input will determine size of the output tensor. + * @returns {Tensor} The ones tensor. + */ +function ones_like(tensor) { + return ones(tensor.dims); +} + +/** + * Returns a tensor filled with the scalar value 0, with the shape defined by the variable argument size. + * @param {number[]} size A sequence of integers defining the shape of the output tensor. + * @returns {Tensor} The zeros tensor. + */ +function zeros(size) { + return fullHelper(size, 0n, 'int64', BigInt64Array); +} + +/** + * Returns a tensor filled with the scalar value 0, with the same size as input. + * @param {Tensor} tensor The size of input will determine size of the output tensor. + * @returns {Tensor} The zeros tensor. + */ +function zeros_like(tensor) { + return zeros(tensor.dims); +} + +/** + * Quantizes the embeddings tensor to binary or unsigned binary precision. + * @param {Tensor} tensor The tensor to quantize. + * @param {'binary'|'ubinary'} precision The precision to use for quantization. + * @returns {Tensor} The quantized tensor. + */ +function quantize_embeddings(tensor, precision) { + if (tensor.dims.length !== 2) { + throw new Error("The tensor must have 2 dimensions"); + } + if (tensor.dims.at(-1) % 8 !== 0) { + throw new Error("The last dimension of the tensor must be a multiple of 8"); + } + if (!['binary', 'ubinary'].includes(precision)) { + throw new Error("The precision must be either 'binary' or 'ubinary'"); + } + + const signed = precision === 'binary'; + const dtype = signed ? 'int8' : 'uint8'; + + // Create a typed array to store the packed bits + const cls = signed ? Int8Array : Uint8Array; + const inputData = tensor.data; + const outputData = new cls(inputData.length / 8); + + // Iterate over each number in the array + for (let i = 0; i < inputData.length; ++i) { + // Determine if the number is greater than 0 + const bit = inputData[i] > 0 ? 1 : 0; + + // Calculate the index in the typed array and the position within the byte + const arrayIndex = Math.floor(i / 8); + const bitPosition = i % 8; + + // Pack the bit into the typed array + outputData[arrayIndex] |= bit << (7 - bitPosition); + if (signed && bitPosition === 0) { + outputData[arrayIndex] -= 128; + } + }; + + return new Tensor(dtype, outputData, [tensor.dims[0], tensor.dims[1] / 8]); +} + + +/***/ }) + +/******/ }); +/************************************************************************/ +/******/ // The module cache +/******/ var __webpack_module_cache__ = {}; +/******/ +/******/ // The require function +/******/ function __webpack_require__(moduleId) { +/******/ // Check if module is in cache +/******/ var cachedModule = __webpack_module_cache__[moduleId]; +/******/ if (cachedModule !== undefined) { +/******/ return cachedModule.exports; +/******/ } +/******/ // Create a new module (and put it into the cache) +/******/ var module = __webpack_module_cache__[moduleId] = { +/******/ // no module.id needed +/******/ // no module.loaded needed +/******/ exports: {} +/******/ }; +/******/ +/******/ // Execute the module function +/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__); +/******/ +/******/ // Return the exports of the module +/******/ return module.exports; +/******/ } +/******/ +/******/ // expose the modules object (__webpack_modules__) +/******/ __webpack_require__.m = __webpack_modules__; +/******/ +/************************************************************************/ +/******/ /* webpack/runtime/create fake namespace object */ +/******/ (() => { +/******/ var getProto = Object.getPrototypeOf ? (obj) => (Object.getPrototypeOf(obj)) : (obj) => (obj.__proto__); +/******/ var leafPrototypes; +/******/ // create a fake namespace object +/******/ // mode & 1: value is a module id, require it +/******/ // mode & 2: merge all properties of value into the ns +/******/ // mode & 4: return value when already ns object +/******/ // mode & 16: return value when it's Promise-like +/******/ // mode & 8|1: behave like require +/******/ __webpack_require__.t = function(value, mode) { +/******/ if(mode & 1) value = this(value); +/******/ if(mode & 8) return value; +/******/ if(typeof value === 'object' && value) { +/******/ if((mode & 4) && value.__esModule) return value; +/******/ if((mode & 16) && typeof value.then === 'function') return value; +/******/ } +/******/ var ns = Object.create(null); +/******/ __webpack_require__.r(ns); +/******/ var def = {}; +/******/ leafPrototypes = leafPrototypes || [null, getProto({}), getProto([]), getProto(getProto)]; +/******/ for(var current = mode & 2 && value; typeof current == 'object' && !~leafPrototypes.indexOf(current); current = getProto(current)) { +/******/ Object.getOwnPropertyNames(current).forEach((key) => (def[key] = () => (value[key]))); +/******/ } +/******/ def['default'] = () => (value); +/******/ __webpack_require__.d(ns, def); +/******/ return ns; +/******/ }; +/******/ })(); +/******/ +/******/ /* webpack/runtime/define property getters */ +/******/ (() => { +/******/ // define getter functions for harmony exports +/******/ __webpack_require__.d = (exports, definition) => { +/******/ for(var key in definition) { +/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) { +/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] }); +/******/ } +/******/ } +/******/ }; +/******/ })(); +/******/ +/******/ /* webpack/runtime/hasOwnProperty shorthand */ +/******/ (() => { +/******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop)) +/******/ })(); +/******/ +/******/ /* webpack/runtime/make namespace object */ +/******/ (() => { +/******/ // define __esModule on exports +/******/ __webpack_require__.r = (exports) => { +/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { +/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); +/******/ } +/******/ Object.defineProperty(exports, '__esModule', { value: true }); +/******/ }; +/******/ })(); +/******/ +/******/ /* webpack/runtime/publicPath */ +/******/ (() => { +/******/ var scriptUrl; +/******/ if (typeof import.meta.url === "string") scriptUrl = import.meta.url +/******/ // When supporting browsers where an automatic publicPath is not supported you must specify an output.publicPath manually via configuration +/******/ // or pass an empty string ("") and set the __webpack_public_path__ variable from your code to use your own logic. +/******/ if (!scriptUrl) throw new Error("Automatic publicPath is not supported in this browser"); +/******/ scriptUrl = scriptUrl.replace(/#.*$/, "").replace(/\?.*$/, "").replace(/\/[^\/]+$/, "/"); +/******/ __webpack_require__.p = scriptUrl; +/******/ })(); +/******/ +/******/ /* webpack/runtime/import chunk loading */ +/******/ (() => { +/******/ __webpack_require__.b = new URL("./", import.meta.url); +/******/ +/******/ // object to store loaded and loading chunks +/******/ // undefined = chunk not loaded, null = chunk preloaded/prefetched +/******/ // [resolve, Promise] = chunk loading, 0 = chunk loaded +/******/ var installedChunks = { +/******/ "transformers": 0 +/******/ }; +/******/ +/******/ // no install chunk +/******/ +/******/ // no chunk on demand loading +/******/ +/******/ // no prefetching +/******/ +/******/ // no preloaded +/******/ +/******/ // no external install chunk +/******/ +/******/ // no on chunks loaded +/******/ })(); +/******/ +/************************************************************************/ +var __webpack_exports__ = {}; +/*!*****************************!*\ + !*** ./src/transformers.js ***! + \*****************************/ +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ ASTFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.ASTFeatureExtractor), +/* harmony export */ ASTForAudioClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ASTForAudioClassification), +/* harmony export */ ASTModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ASTModel), +/* harmony export */ ASTPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ASTPreTrainedModel), +/* harmony export */ AlbertForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AlbertForMaskedLM), +/* harmony export */ AlbertForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AlbertForQuestionAnswering), +/* harmony export */ AlbertForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AlbertForSequenceClassification), +/* harmony export */ AlbertModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AlbertModel), +/* harmony export */ AlbertPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AlbertPreTrainedModel), +/* harmony export */ AlbertTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.AlbertTokenizer), +/* harmony export */ AudioClassificationPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.AudioClassificationPipeline), +/* harmony export */ AutoConfig: () => (/* reexport safe */ _configs_js__WEBPACK_IMPORTED_MODULE_5__.AutoConfig), +/* harmony export */ AutoModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModel), +/* harmony export */ AutoModelForAudioClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForAudioClassification), +/* harmony export */ AutoModelForAudioFrameClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForAudioFrameClassification), +/* harmony export */ AutoModelForCTC: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForCTC), +/* harmony export */ AutoModelForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForCausalLM), +/* harmony export */ AutoModelForDepthEstimation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForDepthEstimation), +/* harmony export */ AutoModelForDocumentQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForDocumentQuestionAnswering), +/* harmony export */ AutoModelForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForImageClassification), +/* harmony export */ AutoModelForImageFeatureExtraction: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForImageFeatureExtraction), +/* harmony export */ AutoModelForImageMatting: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForImageMatting), +/* harmony export */ AutoModelForImageSegmentation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForImageSegmentation), +/* harmony export */ AutoModelForImageToImage: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForImageToImage), +/* harmony export */ AutoModelForMaskGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForMaskGeneration), +/* harmony export */ AutoModelForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForMaskedLM), +/* harmony export */ AutoModelForNormalEstimation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForNormalEstimation), +/* harmony export */ AutoModelForObjectDetection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForObjectDetection), +/* harmony export */ AutoModelForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForQuestionAnswering), +/* harmony export */ AutoModelForSemanticSegmentation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForSemanticSegmentation), +/* harmony export */ AutoModelForSeq2SeqLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForSeq2SeqLM), +/* harmony export */ AutoModelForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForSequenceClassification), +/* harmony export */ AutoModelForSpeechSeq2Seq: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForSpeechSeq2Seq), +/* harmony export */ AutoModelForTextToSpectrogram: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForTextToSpectrogram), +/* harmony export */ AutoModelForTextToWaveform: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForTextToWaveform), +/* harmony export */ AutoModelForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForTokenClassification), +/* harmony export */ AutoModelForUniversalSegmentation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForUniversalSegmentation), +/* harmony export */ AutoModelForVision2Seq: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForVision2Seq), +/* harmony export */ AutoModelForXVector: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForXVector), +/* harmony export */ AutoModelForZeroShotObjectDetection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForZeroShotObjectDetection), +/* harmony export */ AutoProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.AutoProcessor), +/* harmony export */ AutoTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.AutoTokenizer), +/* harmony export */ AutomaticSpeechRecognitionPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.AutomaticSpeechRecognitionPipeline), +/* harmony export */ BartForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BartForConditionalGeneration), +/* harmony export */ BartForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BartForSequenceClassification), +/* harmony export */ BartModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BartModel), +/* harmony export */ BartPretrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BartPretrainedModel), +/* harmony export */ BartTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.BartTokenizer), +/* harmony export */ BaseModelOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BaseModelOutput), +/* harmony export */ BaseStreamer: () => (/* reexport safe */ _generation_streamers_js__WEBPACK_IMPORTED_MODULE_10__.BaseStreamer), +/* harmony export */ BeitFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.BeitFeatureExtractor), +/* harmony export */ BeitForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BeitForImageClassification), +/* harmony export */ BeitModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BeitModel), +/* harmony export */ BeitPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BeitPreTrainedModel), +/* harmony export */ BertForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BertForMaskedLM), +/* harmony export */ BertForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BertForQuestionAnswering), +/* harmony export */ BertForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BertForSequenceClassification), +/* harmony export */ BertForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BertForTokenClassification), +/* harmony export */ BertModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BertModel), +/* harmony export */ BertPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BertPreTrainedModel), +/* harmony export */ BertTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.BertTokenizer), +/* harmony export */ BitImageProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.BitImageProcessor), +/* harmony export */ BlenderbotForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BlenderbotForConditionalGeneration), +/* harmony export */ BlenderbotModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BlenderbotModel), +/* harmony export */ BlenderbotPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BlenderbotPreTrainedModel), +/* harmony export */ BlenderbotSmallForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BlenderbotSmallForConditionalGeneration), +/* harmony export */ BlenderbotSmallModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BlenderbotSmallModel), +/* harmony export */ BlenderbotSmallPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BlenderbotSmallPreTrainedModel), +/* harmony export */ BlenderbotSmallTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.BlenderbotSmallTokenizer), +/* harmony export */ BlenderbotTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.BlenderbotTokenizer), +/* harmony export */ BloomForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BloomForCausalLM), +/* harmony export */ BloomModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BloomModel), +/* harmony export */ BloomPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BloomPreTrainedModel), +/* harmony export */ BloomTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.BloomTokenizer), +/* harmony export */ CLIPFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.CLIPFeatureExtractor), +/* harmony export */ CLIPImageProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.CLIPImageProcessor), +/* harmony export */ CLIPModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPModel), +/* harmony export */ CLIPPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPPreTrainedModel), +/* harmony export */ CLIPSegForImageSegmentation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPSegForImageSegmentation), +/* harmony export */ CLIPSegModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPSegModel), +/* harmony export */ CLIPSegPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPSegPreTrainedModel), +/* harmony export */ CLIPTextModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPTextModel), +/* harmony export */ CLIPTextModelWithProjection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPTextModelWithProjection), +/* harmony export */ CLIPTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.CLIPTokenizer), +/* harmony export */ CLIPVisionModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPVisionModel), +/* harmony export */ CLIPVisionModelWithProjection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPVisionModelWithProjection), +/* harmony export */ CamembertForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CamembertForMaskedLM), +/* harmony export */ CamembertForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CamembertForQuestionAnswering), +/* harmony export */ CamembertForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CamembertForSequenceClassification), +/* harmony export */ CamembertForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CamembertForTokenClassification), +/* harmony export */ CamembertModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CamembertModel), +/* harmony export */ CamembertPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CamembertPreTrainedModel), +/* harmony export */ CamembertTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.CamembertTokenizer), +/* harmony export */ CausalLMOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CausalLMOutput), +/* harmony export */ CausalLMOutputWithPast: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CausalLMOutputWithPast), +/* harmony export */ ChineseCLIPFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.ChineseCLIPFeatureExtractor), +/* harmony export */ ChineseCLIPModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ChineseCLIPModel), +/* harmony export */ ChineseCLIPPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ChineseCLIPPreTrainedModel), +/* harmony export */ ClapAudioModelWithProjection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ClapAudioModelWithProjection), +/* harmony export */ ClapFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.ClapFeatureExtractor), +/* harmony export */ ClapModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ClapModel), +/* harmony export */ ClapPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ClapPreTrainedModel), +/* harmony export */ ClapTextModelWithProjection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ClapTextModelWithProjection), +/* harmony export */ CodeGenForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CodeGenForCausalLM), +/* harmony export */ CodeGenModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CodeGenModel), +/* harmony export */ CodeGenPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CodeGenPreTrainedModel), +/* harmony export */ CodeGenTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.CodeGenTokenizer), +/* harmony export */ CodeLlamaTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.CodeLlamaTokenizer), +/* harmony export */ CohereForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CohereForCausalLM), +/* harmony export */ CohereModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CohereModel), +/* harmony export */ CoherePreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CoherePreTrainedModel), +/* harmony export */ CohereTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.CohereTokenizer), +/* harmony export */ ConvBertForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvBertForMaskedLM), +/* harmony export */ ConvBertForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvBertForQuestionAnswering), +/* harmony export */ ConvBertForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvBertForSequenceClassification), +/* harmony export */ ConvBertForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvBertForTokenClassification), +/* harmony export */ ConvBertModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvBertModel), +/* harmony export */ ConvBertPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvBertPreTrainedModel), +/* harmony export */ ConvBertTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.ConvBertTokenizer), +/* harmony export */ ConvNextFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.ConvNextFeatureExtractor), +/* harmony export */ ConvNextForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvNextForImageClassification), +/* harmony export */ ConvNextImageProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.ConvNextImageProcessor), +/* harmony export */ ConvNextModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvNextModel), +/* harmony export */ ConvNextPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvNextPreTrainedModel), +/* harmony export */ ConvNextV2ForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvNextV2ForImageClassification), +/* harmony export */ ConvNextV2Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvNextV2Model), +/* harmony export */ ConvNextV2PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvNextV2PreTrainedModel), +/* harmony export */ DPTFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.DPTFeatureExtractor), +/* harmony export */ DPTForDepthEstimation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DPTForDepthEstimation), +/* harmony export */ DPTImageProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.DPTImageProcessor), +/* harmony export */ DPTModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DPTModel), +/* harmony export */ DPTPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DPTPreTrainedModel), +/* harmony export */ DebertaForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaForMaskedLM), +/* harmony export */ DebertaForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaForQuestionAnswering), +/* harmony export */ DebertaForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaForSequenceClassification), +/* harmony export */ DebertaForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaForTokenClassification), +/* harmony export */ DebertaModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaModel), +/* harmony export */ DebertaPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaPreTrainedModel), +/* harmony export */ DebertaTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.DebertaTokenizer), +/* harmony export */ DebertaV2ForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaV2ForMaskedLM), +/* harmony export */ DebertaV2ForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaV2ForQuestionAnswering), +/* harmony export */ DebertaV2ForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaV2ForSequenceClassification), +/* harmony export */ DebertaV2ForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaV2ForTokenClassification), +/* harmony export */ DebertaV2Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaV2Model), +/* harmony export */ DebertaV2PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaV2PreTrainedModel), +/* harmony export */ DebertaV2Tokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.DebertaV2Tokenizer), +/* harmony export */ DecisionTransformerModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DecisionTransformerModel), +/* harmony export */ DecisionTransformerPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DecisionTransformerPreTrainedModel), +/* harmony export */ DeiTFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.DeiTFeatureExtractor), +/* harmony export */ DeiTForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DeiTForImageClassification), +/* harmony export */ DeiTModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DeiTModel), +/* harmony export */ DeiTPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DeiTPreTrainedModel), +/* harmony export */ DepthAnythingForDepthEstimation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DepthAnythingForDepthEstimation), +/* harmony export */ DepthAnythingPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DepthAnythingPreTrainedModel), +/* harmony export */ DepthEstimationPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.DepthEstimationPipeline), +/* harmony export */ DepthProForDepthEstimation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DepthProForDepthEstimation), +/* harmony export */ DepthProPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DepthProPreTrainedModel), +/* harmony export */ DetrFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.DetrFeatureExtractor), +/* harmony export */ DetrForObjectDetection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DetrForObjectDetection), +/* harmony export */ DetrForSegmentation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DetrForSegmentation), +/* harmony export */ DetrModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DetrModel), +/* harmony export */ DetrObjectDetectionOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DetrObjectDetectionOutput), +/* harmony export */ DetrPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DetrPreTrainedModel), +/* harmony export */ DetrSegmentationOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DetrSegmentationOutput), +/* harmony export */ Dinov2ForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Dinov2ForImageClassification), +/* harmony export */ Dinov2Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Dinov2Model), +/* harmony export */ Dinov2PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Dinov2PreTrainedModel), +/* harmony export */ DistilBertForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DistilBertForMaskedLM), +/* harmony export */ DistilBertForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DistilBertForQuestionAnswering), +/* harmony export */ DistilBertForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DistilBertForSequenceClassification), +/* harmony export */ DistilBertForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DistilBertForTokenClassification), +/* harmony export */ DistilBertModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DistilBertModel), +/* harmony export */ DistilBertPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DistilBertPreTrainedModel), +/* harmony export */ DistilBertTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.DistilBertTokenizer), +/* harmony export */ DocumentQuestionAnsweringPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.DocumentQuestionAnsweringPipeline), +/* harmony export */ DonutFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.DonutFeatureExtractor), +/* harmony export */ DonutImageProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.DonutImageProcessor), +/* harmony export */ DonutSwinModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DonutSwinModel), +/* harmony export */ DonutSwinPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DonutSwinPreTrainedModel), +/* harmony export */ EfficientNetForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.EfficientNetForImageClassification), +/* harmony export */ EfficientNetImageProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.EfficientNetImageProcessor), +/* harmony export */ EfficientNetModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.EfficientNetModel), +/* harmony export */ EfficientNetPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.EfficientNetPreTrainedModel), +/* harmony export */ ElectraForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ElectraForMaskedLM), +/* harmony export */ ElectraForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ElectraForQuestionAnswering), +/* harmony export */ ElectraForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ElectraForSequenceClassification), +/* harmony export */ ElectraForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ElectraForTokenClassification), +/* harmony export */ ElectraModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ElectraModel), +/* harmony export */ ElectraPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ElectraPreTrainedModel), +/* harmony export */ ElectraTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.ElectraTokenizer), +/* harmony export */ EosTokenCriteria: () => (/* reexport safe */ _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_11__.EosTokenCriteria), +/* harmony export */ EsmForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.EsmForMaskedLM), +/* harmony export */ EsmForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.EsmForSequenceClassification), +/* harmony export */ EsmForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.EsmForTokenClassification), +/* harmony export */ EsmModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.EsmModel), +/* harmony export */ EsmPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.EsmPreTrainedModel), +/* harmony export */ EsmTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.EsmTokenizer), +/* harmony export */ FFT: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.FFT), +/* harmony export */ FalconForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.FalconForCausalLM), +/* harmony export */ FalconModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.FalconModel), +/* harmony export */ FalconPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.FalconPreTrainedModel), +/* harmony export */ FalconTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.FalconTokenizer), +/* harmony export */ FastViTForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.FastViTForImageClassification), +/* harmony export */ FastViTModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.FastViTModel), +/* harmony export */ FastViTPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.FastViTPreTrainedModel), +/* harmony export */ FeatureExtractionPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.FeatureExtractionPipeline), +/* harmony export */ FeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.FeatureExtractor), +/* harmony export */ FillMaskPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.FillMaskPipeline), +/* harmony export */ Florence2ForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Florence2ForConditionalGeneration), +/* harmony export */ Florence2PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Florence2PreTrainedModel), +/* harmony export */ Florence2Processor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.Florence2Processor), +/* harmony export */ GLPNFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.GLPNFeatureExtractor), +/* harmony export */ GLPNForDepthEstimation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GLPNForDepthEstimation), +/* harmony export */ GLPNModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GLPNModel), +/* harmony export */ GLPNPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GLPNPreTrainedModel), +/* harmony export */ GPT2LMHeadModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPT2LMHeadModel), +/* harmony export */ GPT2Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPT2Model), +/* harmony export */ GPT2PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPT2PreTrainedModel), +/* harmony export */ GPT2Tokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.GPT2Tokenizer), +/* harmony export */ GPTBigCodeForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTBigCodeForCausalLM), +/* harmony export */ GPTBigCodeModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTBigCodeModel), +/* harmony export */ GPTBigCodePreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTBigCodePreTrainedModel), +/* harmony export */ GPTJForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTJForCausalLM), +/* harmony export */ GPTJModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTJModel), +/* harmony export */ GPTJPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTJPreTrainedModel), +/* harmony export */ GPTNeoForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTNeoForCausalLM), +/* harmony export */ GPTNeoModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTNeoModel), +/* harmony export */ GPTNeoPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTNeoPreTrainedModel), +/* harmony export */ GPTNeoXForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTNeoXForCausalLM), +/* harmony export */ GPTNeoXModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTNeoXModel), +/* harmony export */ GPTNeoXPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTNeoXPreTrainedModel), +/* harmony export */ GPTNeoXTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.GPTNeoXTokenizer), +/* harmony export */ Gemma2ForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Gemma2ForCausalLM), +/* harmony export */ Gemma2Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Gemma2Model), +/* harmony export */ Gemma2PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Gemma2PreTrainedModel), +/* harmony export */ GemmaForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GemmaForCausalLM), +/* harmony export */ GemmaModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GemmaModel), +/* harmony export */ GemmaPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GemmaPreTrainedModel), +/* harmony export */ GemmaTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.GemmaTokenizer), +/* harmony export */ GraniteForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GraniteForCausalLM), +/* harmony export */ GraniteModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GraniteModel), +/* harmony export */ GranitePreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GranitePreTrainedModel), +/* harmony export */ Grok1Tokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.Grok1Tokenizer), +/* harmony export */ GroupViTModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GroupViTModel), +/* harmony export */ GroupViTPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GroupViTPreTrainedModel), +/* harmony export */ HerbertTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.HerbertTokenizer), +/* harmony export */ HieraForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.HieraForImageClassification), +/* harmony export */ HieraModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.HieraModel), +/* harmony export */ HieraPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.HieraPreTrainedModel), +/* harmony export */ HubertForCTC: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.HubertForCTC), +/* harmony export */ HubertForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.HubertForSequenceClassification), +/* harmony export */ HubertModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.HubertModel), +/* harmony export */ HubertPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.HubertPreTrainedModel), +/* harmony export */ ImageClassificationPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ImageClassificationPipeline), +/* harmony export */ ImageFeatureExtractionPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ImageFeatureExtractionPipeline), +/* harmony export */ ImageFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.ImageFeatureExtractor), +/* harmony export */ ImageMattingOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ImageMattingOutput), +/* harmony export */ ImageSegmentationPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ImageSegmentationPipeline), +/* harmony export */ ImageToImagePipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ImageToImagePipeline), +/* harmony export */ ImageToTextPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ImageToTextPipeline), +/* harmony export */ InterruptableStoppingCriteria: () => (/* reexport safe */ _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_11__.InterruptableStoppingCriteria), +/* harmony export */ JAISLMHeadModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.JAISLMHeadModel), +/* harmony export */ JAISModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.JAISModel), +/* harmony export */ JAISPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.JAISPreTrainedModel), +/* harmony export */ LlamaForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.LlamaForCausalLM), +/* harmony export */ LlamaModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.LlamaModel), +/* harmony export */ LlamaPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.LlamaPreTrainedModel), +/* harmony export */ LlamaTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.LlamaTokenizer), +/* harmony export */ LlavaForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.LlavaForConditionalGeneration), +/* harmony export */ LlavaPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.LlavaPreTrainedModel), +/* harmony export */ LongT5ForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.LongT5ForConditionalGeneration), +/* harmony export */ LongT5Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.LongT5Model), +/* harmony export */ LongT5PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.LongT5PreTrainedModel), +/* harmony export */ M2M100ForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.M2M100ForConditionalGeneration), +/* harmony export */ M2M100Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.M2M100Model), +/* harmony export */ M2M100PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.M2M100PreTrainedModel), +/* harmony export */ M2M100Tokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.M2M100Tokenizer), +/* harmony export */ MBart50Tokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.MBart50Tokenizer), +/* harmony export */ MBartForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MBartForCausalLM), +/* harmony export */ MBartForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MBartForConditionalGeneration), +/* harmony export */ MBartForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MBartForSequenceClassification), +/* harmony export */ MBartModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MBartModel), +/* harmony export */ MBartPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MBartPreTrainedModel), +/* harmony export */ MBartTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.MBartTokenizer), +/* harmony export */ MPNetForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MPNetForMaskedLM), +/* harmony export */ MPNetForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MPNetForQuestionAnswering), +/* harmony export */ MPNetForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MPNetForSequenceClassification), +/* harmony export */ MPNetForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MPNetForTokenClassification), +/* harmony export */ MPNetModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MPNetModel), +/* harmony export */ MPNetPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MPNetPreTrainedModel), +/* harmony export */ MPNetTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.MPNetTokenizer), +/* harmony export */ MT5ForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MT5ForConditionalGeneration), +/* harmony export */ MT5Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MT5Model), +/* harmony export */ MT5PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MT5PreTrainedModel), +/* harmony export */ MarianMTModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MarianMTModel), +/* harmony export */ MarianModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MarianModel), +/* harmony export */ MarianPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MarianPreTrainedModel), +/* harmony export */ MarianTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.MarianTokenizer), +/* harmony export */ MaskFormerFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.MaskFormerFeatureExtractor), +/* harmony export */ MaskFormerForInstanceSegmentation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MaskFormerForInstanceSegmentation), +/* harmony export */ MaskFormerModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MaskFormerModel), +/* harmony export */ MaskFormerPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MaskFormerPreTrainedModel), +/* harmony export */ MaskedLMOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MaskedLMOutput), +/* harmony export */ MaxLengthCriteria: () => (/* reexport safe */ _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_11__.MaxLengthCriteria), +/* harmony export */ MistralForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MistralForCausalLM), +/* harmony export */ MistralModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MistralModel), +/* harmony export */ MistralPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MistralPreTrainedModel), +/* harmony export */ MobileBertForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileBertForMaskedLM), +/* harmony export */ MobileBertForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileBertForQuestionAnswering), +/* harmony export */ MobileBertForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileBertForSequenceClassification), +/* harmony export */ MobileBertModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileBertModel), +/* harmony export */ MobileBertPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileBertPreTrainedModel), +/* harmony export */ MobileBertTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.MobileBertTokenizer), +/* harmony export */ MobileLLMForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileLLMForCausalLM), +/* harmony export */ MobileLLMModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileLLMModel), +/* harmony export */ MobileLLMPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileLLMPreTrainedModel), +/* harmony export */ MobileNetV1FeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.MobileNetV1FeatureExtractor), +/* harmony export */ MobileNetV1ForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV1ForImageClassification), +/* harmony export */ MobileNetV1Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV1Model), +/* harmony export */ MobileNetV1PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV1PreTrainedModel), +/* harmony export */ MobileNetV2FeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.MobileNetV2FeatureExtractor), +/* harmony export */ MobileNetV2ForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV2ForImageClassification), +/* harmony export */ MobileNetV2Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV2Model), +/* harmony export */ MobileNetV2PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV2PreTrainedModel), +/* harmony export */ MobileNetV3FeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.MobileNetV3FeatureExtractor), +/* harmony export */ MobileNetV3ForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV3ForImageClassification), +/* harmony export */ MobileNetV3Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV3Model), +/* harmony export */ MobileNetV3PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV3PreTrainedModel), +/* harmony export */ MobileNetV4FeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.MobileNetV4FeatureExtractor), +/* harmony export */ MobileNetV4ForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV4ForImageClassification), +/* harmony export */ MobileNetV4Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV4Model), +/* harmony export */ MobileNetV4PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV4PreTrainedModel), +/* harmony export */ MobileViTFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.MobileViTFeatureExtractor), +/* harmony export */ MobileViTForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileViTForImageClassification), +/* harmony export */ MobileViTImageProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.MobileViTImageProcessor), +/* harmony export */ MobileViTModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileViTModel), +/* harmony export */ MobileViTPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileViTPreTrainedModel), +/* harmony export */ MobileViTV2ForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileViTV2ForImageClassification), +/* harmony export */ MobileViTV2Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileViTV2Model), +/* harmony export */ MobileViTV2PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileViTV2PreTrainedModel), +/* harmony export */ ModelOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ModelOutput), +/* harmony export */ Moondream1ForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Moondream1ForConditionalGeneration), +/* harmony export */ MptForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MptForCausalLM), +/* harmony export */ MptModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MptModel), +/* harmony export */ MptPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MptPreTrainedModel), +/* harmony export */ MusicgenForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MusicgenForCausalLM), +/* harmony export */ MusicgenForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MusicgenForConditionalGeneration), +/* harmony export */ MusicgenModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MusicgenModel), +/* harmony export */ MusicgenPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MusicgenPreTrainedModel), +/* harmony export */ NllbTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.NllbTokenizer), +/* harmony export */ NomicBertModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.NomicBertModel), +/* harmony export */ NomicBertPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.NomicBertPreTrainedModel), +/* harmony export */ NougatImageProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.NougatImageProcessor), +/* harmony export */ NougatTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.NougatTokenizer), +/* harmony export */ OPTForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OPTForCausalLM), +/* harmony export */ OPTModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OPTModel), +/* harmony export */ OPTPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OPTPreTrainedModel), +/* harmony export */ ObjectDetectionPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ObjectDetectionPipeline), +/* harmony export */ OlmoForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OlmoForCausalLM), +/* harmony export */ OlmoModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OlmoModel), +/* harmony export */ OlmoPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OlmoPreTrainedModel), +/* harmony export */ OpenELMForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OpenELMForCausalLM), +/* harmony export */ OpenELMModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OpenELMModel), +/* harmony export */ OpenELMPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OpenELMPreTrainedModel), +/* harmony export */ OwlViTFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.OwlViTFeatureExtractor), +/* harmony export */ OwlViTForObjectDetection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OwlViTForObjectDetection), +/* harmony export */ OwlViTModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OwlViTModel), +/* harmony export */ OwlViTPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OwlViTPreTrainedModel), +/* harmony export */ OwlViTProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.OwlViTProcessor), +/* harmony export */ Owlv2ForObjectDetection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Owlv2ForObjectDetection), +/* harmony export */ Owlv2ImageProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.Owlv2ImageProcessor), +/* harmony export */ Owlv2Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Owlv2Model), +/* harmony export */ Owlv2PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Owlv2PreTrainedModel), +/* harmony export */ Phi3ForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Phi3ForCausalLM), +/* harmony export */ Phi3Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Phi3Model), +/* harmony export */ Phi3PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Phi3PreTrainedModel), +/* harmony export */ PhiForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PhiForCausalLM), +/* harmony export */ PhiModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PhiModel), +/* harmony export */ PhiPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PhiPreTrainedModel), +/* harmony export */ Pipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.Pipeline), +/* harmony export */ PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PreTrainedModel), +/* harmony export */ PreTrainedTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.PreTrainedTokenizer), +/* harmony export */ PretrainedConfig: () => (/* reexport safe */ _configs_js__WEBPACK_IMPORTED_MODULE_5__.PretrainedConfig), +/* harmony export */ PretrainedMixin: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PretrainedMixin), +/* harmony export */ Processor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.Processor), +/* harmony export */ PvtForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PvtForImageClassification), +/* harmony export */ PvtImageProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.PvtImageProcessor), +/* harmony export */ PvtModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PvtModel), +/* harmony export */ PvtPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PvtPreTrainedModel), +/* harmony export */ PyAnnoteFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.PyAnnoteFeatureExtractor), +/* harmony export */ PyAnnoteForAudioFrameClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PyAnnoteForAudioFrameClassification), +/* harmony export */ PyAnnoteModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PyAnnoteModel), +/* harmony export */ PyAnnotePreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PyAnnotePreTrainedModel), +/* harmony export */ PyAnnoteProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.PyAnnoteProcessor), +/* harmony export */ QuestionAnsweringModelOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.QuestionAnsweringModelOutput), +/* harmony export */ QuestionAnsweringPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.QuestionAnsweringPipeline), +/* harmony export */ Qwen2ForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Qwen2ForCausalLM), +/* harmony export */ Qwen2Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Qwen2Model), +/* harmony export */ Qwen2PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Qwen2PreTrainedModel), +/* harmony export */ Qwen2Tokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.Qwen2Tokenizer), +/* harmony export */ RTDetrForObjectDetection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RTDetrForObjectDetection), +/* harmony export */ RTDetrImageProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.RTDetrImageProcessor), +/* harmony export */ RTDetrModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RTDetrModel), +/* harmony export */ RTDetrObjectDetectionOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RTDetrObjectDetectionOutput), +/* harmony export */ RTDetrPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RTDetrPreTrainedModel), +/* harmony export */ RawImage: () => (/* reexport safe */ _utils_image_js__WEBPACK_IMPORTED_MODULE_7__.RawImage), +/* harmony export */ ResNetForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ResNetForImageClassification), +/* harmony export */ ResNetModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ResNetModel), +/* harmony export */ ResNetPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ResNetPreTrainedModel), +/* harmony export */ RoFormerForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RoFormerForMaskedLM), +/* harmony export */ RoFormerForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RoFormerForQuestionAnswering), +/* harmony export */ RoFormerForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RoFormerForSequenceClassification), +/* harmony export */ RoFormerForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RoFormerForTokenClassification), +/* harmony export */ RoFormerModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RoFormerModel), +/* harmony export */ RoFormerPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RoFormerPreTrainedModel), +/* harmony export */ RoFormerTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.RoFormerTokenizer), +/* harmony export */ RobertaForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RobertaForMaskedLM), +/* harmony export */ RobertaForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RobertaForQuestionAnswering), +/* harmony export */ RobertaForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RobertaForSequenceClassification), +/* harmony export */ RobertaForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RobertaForTokenClassification), +/* harmony export */ RobertaModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RobertaModel), +/* harmony export */ RobertaPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RobertaPreTrainedModel), +/* harmony export */ RobertaTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.RobertaTokenizer), +/* harmony export */ SamImageProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.SamImageProcessor), +/* harmony export */ SamImageSegmentationOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SamImageSegmentationOutput), +/* harmony export */ SamModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SamModel), +/* harmony export */ SamPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SamPreTrainedModel), +/* harmony export */ SamProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.SamProcessor), +/* harmony export */ SapiensFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.SapiensFeatureExtractor), +/* harmony export */ SapiensForDepthEstimation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SapiensForDepthEstimation), +/* harmony export */ SapiensForNormalEstimation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SapiensForNormalEstimation), +/* harmony export */ SapiensForSemanticSegmentation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SapiensForSemanticSegmentation), +/* harmony export */ SapiensPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SapiensPreTrainedModel), +/* harmony export */ SeamlessM4TFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.SeamlessM4TFeatureExtractor), +/* harmony export */ SegformerFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.SegformerFeatureExtractor), +/* harmony export */ SegformerForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SegformerForImageClassification), +/* harmony export */ SegformerForSemanticSegmentation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SegformerForSemanticSegmentation), +/* harmony export */ SegformerModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SegformerModel), +/* harmony export */ SegformerPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SegformerPreTrainedModel), +/* harmony export */ Seq2SeqLMOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Seq2SeqLMOutput), +/* harmony export */ SequenceClassifierOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SequenceClassifierOutput), +/* harmony export */ SiglipImageProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.SiglipImageProcessor), +/* harmony export */ SiglipModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SiglipModel), +/* harmony export */ SiglipPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SiglipPreTrainedModel), +/* harmony export */ SiglipTextModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SiglipTextModel), +/* harmony export */ SiglipTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.SiglipTokenizer), +/* harmony export */ SiglipVisionModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SiglipVisionModel), +/* harmony export */ SpeechT5FeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.SpeechT5FeatureExtractor), +/* harmony export */ SpeechT5ForSpeechToText: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SpeechT5ForSpeechToText), +/* harmony export */ SpeechT5ForTextToSpeech: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SpeechT5ForTextToSpeech), +/* harmony export */ SpeechT5HifiGan: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SpeechT5HifiGan), +/* harmony export */ SpeechT5Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SpeechT5Model), +/* harmony export */ SpeechT5PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SpeechT5PreTrainedModel), +/* harmony export */ SpeechT5Processor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.SpeechT5Processor), +/* harmony export */ SpeechT5Tokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.SpeechT5Tokenizer), +/* harmony export */ SqueezeBertForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SqueezeBertForMaskedLM), +/* harmony export */ SqueezeBertForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SqueezeBertForQuestionAnswering), +/* harmony export */ SqueezeBertForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SqueezeBertForSequenceClassification), +/* harmony export */ SqueezeBertModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SqueezeBertModel), +/* harmony export */ SqueezeBertPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SqueezeBertPreTrainedModel), +/* harmony export */ SqueezeBertTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.SqueezeBertTokenizer), +/* harmony export */ StableLmForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.StableLmForCausalLM), +/* harmony export */ StableLmModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.StableLmModel), +/* harmony export */ StableLmPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.StableLmPreTrainedModel), +/* harmony export */ Starcoder2ForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Starcoder2ForCausalLM), +/* harmony export */ Starcoder2Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Starcoder2Model), +/* harmony export */ Starcoder2PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Starcoder2PreTrainedModel), +/* harmony export */ StoppingCriteria: () => (/* reexport safe */ _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_11__.StoppingCriteria), +/* harmony export */ StoppingCriteriaList: () => (/* reexport safe */ _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_11__.StoppingCriteriaList), +/* harmony export */ SummarizationPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.SummarizationPipeline), +/* harmony export */ Swin2SRForImageSuperResolution: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Swin2SRForImageSuperResolution), +/* harmony export */ Swin2SRImageProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.Swin2SRImageProcessor), +/* harmony export */ Swin2SRModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Swin2SRModel), +/* harmony export */ Swin2SRPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Swin2SRPreTrainedModel), +/* harmony export */ SwinForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SwinForImageClassification), +/* harmony export */ SwinModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SwinModel), +/* harmony export */ SwinPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SwinPreTrainedModel), +/* harmony export */ T5ForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.T5ForConditionalGeneration), +/* harmony export */ T5Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.T5Model), +/* harmony export */ T5PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.T5PreTrainedModel), +/* harmony export */ T5Tokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.T5Tokenizer), +/* harmony export */ TableTransformerForObjectDetection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.TableTransformerForObjectDetection), +/* harmony export */ TableTransformerModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.TableTransformerModel), +/* harmony export */ TableTransformerObjectDetectionOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.TableTransformerObjectDetectionOutput), +/* harmony export */ TableTransformerPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.TableTransformerPreTrainedModel), +/* harmony export */ Tensor: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.Tensor), +/* harmony export */ Text2TextGenerationPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.Text2TextGenerationPipeline), +/* harmony export */ TextClassificationPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.TextClassificationPipeline), +/* harmony export */ TextGenerationPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.TextGenerationPipeline), +/* harmony export */ TextStreamer: () => (/* reexport safe */ _generation_streamers_js__WEBPACK_IMPORTED_MODULE_10__.TextStreamer), +/* harmony export */ TextToAudioPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.TextToAudioPipeline), +/* harmony export */ TokenClassificationPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.TokenClassificationPipeline), +/* harmony export */ TokenClassifierOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.TokenClassifierOutput), +/* harmony export */ TokenizerModel: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.TokenizerModel), +/* harmony export */ TrOCRForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.TrOCRForCausalLM), +/* harmony export */ TrOCRPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.TrOCRPreTrainedModel), +/* harmony export */ TranslationPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.TranslationPipeline), +/* harmony export */ UniSpeechForCTC: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechForCTC), +/* harmony export */ UniSpeechForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechForSequenceClassification), +/* harmony export */ UniSpeechModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechModel), +/* harmony export */ UniSpeechPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechPreTrainedModel), +/* harmony export */ UniSpeechSatForAudioFrameClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechSatForAudioFrameClassification), +/* harmony export */ UniSpeechSatForCTC: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechSatForCTC), +/* harmony export */ UniSpeechSatForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechSatForSequenceClassification), +/* harmony export */ UniSpeechSatModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechSatModel), +/* harmony export */ UniSpeechSatPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechSatPreTrainedModel), +/* harmony export */ ViTFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.ViTFeatureExtractor), +/* harmony export */ ViTForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTForImageClassification), +/* harmony export */ ViTImageProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.ViTImageProcessor), +/* harmony export */ ViTMAEModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTMAEModel), +/* harmony export */ ViTMAEPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTMAEPreTrainedModel), +/* harmony export */ ViTMSNForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTMSNForImageClassification), +/* harmony export */ ViTMSNModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTMSNModel), +/* harmony export */ ViTMSNPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTMSNPreTrainedModel), +/* harmony export */ ViTModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTModel), +/* harmony export */ ViTPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTPreTrainedModel), +/* harmony export */ VisionEncoderDecoderModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.VisionEncoderDecoderModel), +/* harmony export */ VitMatteForImageMatting: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.VitMatteForImageMatting), +/* harmony export */ VitMatteImageProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.VitMatteImageProcessor), +/* harmony export */ VitMattePreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.VitMattePreTrainedModel), +/* harmony export */ VitsModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.VitsModel), +/* harmony export */ VitsModelOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.VitsModelOutput), +/* harmony export */ VitsPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.VitsPreTrainedModel), +/* harmony export */ VitsTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.VitsTokenizer), +/* harmony export */ Wav2Vec2BertForCTC: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2BertForCTC), +/* harmony export */ Wav2Vec2BertForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2BertForSequenceClassification), +/* harmony export */ Wav2Vec2BertModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2BertModel), +/* harmony export */ Wav2Vec2BertPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2BertPreTrainedModel), +/* harmony export */ Wav2Vec2CTCTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.Wav2Vec2CTCTokenizer), +/* harmony export */ Wav2Vec2FeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.Wav2Vec2FeatureExtractor), +/* harmony export */ Wav2Vec2ForAudioFrameClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2ForAudioFrameClassification), +/* harmony export */ Wav2Vec2ForCTC: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2ForCTC), +/* harmony export */ Wav2Vec2ForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2ForSequenceClassification), +/* harmony export */ Wav2Vec2Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2Model), +/* harmony export */ Wav2Vec2PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2PreTrainedModel), +/* harmony export */ Wav2Vec2ProcessorWithLM: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.Wav2Vec2ProcessorWithLM), +/* harmony export */ WavLMForAudioFrameClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WavLMForAudioFrameClassification), +/* harmony export */ WavLMForCTC: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WavLMForCTC), +/* harmony export */ WavLMForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WavLMForSequenceClassification), +/* harmony export */ WavLMForXVector: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WavLMForXVector), +/* harmony export */ WavLMModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WavLMModel), +/* harmony export */ WavLMPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WavLMPreTrainedModel), +/* harmony export */ WeSpeakerFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.WeSpeakerFeatureExtractor), +/* harmony export */ WeSpeakerResNetModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WeSpeakerResNetModel), +/* harmony export */ WeSpeakerResNetPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WeSpeakerResNetPreTrainedModel), +/* harmony export */ WhisperFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.WhisperFeatureExtractor), +/* harmony export */ WhisperForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WhisperForConditionalGeneration), +/* harmony export */ WhisperModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WhisperModel), +/* harmony export */ WhisperPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WhisperPreTrainedModel), +/* harmony export */ WhisperProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.WhisperProcessor), +/* harmony export */ WhisperTextStreamer: () => (/* reexport safe */ _generation_streamers_js__WEBPACK_IMPORTED_MODULE_10__.WhisperTextStreamer), +/* harmony export */ WhisperTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.WhisperTokenizer), +/* harmony export */ XLMForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMForQuestionAnswering), +/* harmony export */ XLMForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMForSequenceClassification), +/* harmony export */ XLMForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMForTokenClassification), +/* harmony export */ XLMModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMModel), +/* harmony export */ XLMPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMPreTrainedModel), +/* harmony export */ XLMRobertaForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMRobertaForMaskedLM), +/* harmony export */ XLMRobertaForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMRobertaForQuestionAnswering), +/* harmony export */ XLMRobertaForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMRobertaForSequenceClassification), +/* harmony export */ XLMRobertaForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMRobertaForTokenClassification), +/* harmony export */ XLMRobertaModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMRobertaModel), +/* harmony export */ XLMRobertaPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMRobertaPreTrainedModel), +/* harmony export */ XLMRobertaTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.XLMRobertaTokenizer), +/* harmony export */ XLMTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.XLMTokenizer), +/* harmony export */ XLMWithLMHeadModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMWithLMHeadModel), +/* harmony export */ XVectorOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XVectorOutput), +/* harmony export */ YolosFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.YolosFeatureExtractor), +/* harmony export */ YolosForObjectDetection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.YolosForObjectDetection), +/* harmony export */ YolosModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.YolosModel), +/* harmony export */ YolosObjectDetectionOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.YolosObjectDetectionOutput), +/* harmony export */ YolosPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.YolosPreTrainedModel), +/* harmony export */ ZeroShotAudioClassificationPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ZeroShotAudioClassificationPipeline), +/* harmony export */ ZeroShotClassificationPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ZeroShotClassificationPipeline), +/* harmony export */ ZeroShotImageClassificationPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ZeroShotImageClassificationPipeline), +/* harmony export */ ZeroShotObjectDetectionPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ZeroShotObjectDetectionPipeline), +/* harmony export */ bankers_round: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.bankers_round), +/* harmony export */ cat: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.cat), +/* harmony export */ cos_sim: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.cos_sim), +/* harmony export */ dot: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.dot), +/* harmony export */ dynamic_time_warping: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.dynamic_time_warping), +/* harmony export */ env: () => (/* reexport safe */ _env_js__WEBPACK_IMPORTED_MODULE_0__.env), +/* harmony export */ full: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.full), +/* harmony export */ full_like: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.full_like), +/* harmony export */ getKeyValueShapes: () => (/* reexport safe */ _configs_js__WEBPACK_IMPORTED_MODULE_5__.getKeyValueShapes), +/* harmony export */ hamming: () => (/* reexport safe */ _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.hamming), +/* harmony export */ hanning: () => (/* reexport safe */ _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.hanning), +/* harmony export */ interpolate: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.interpolate), +/* harmony export */ interpolate_4d: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.interpolate_4d), +/* harmony export */ interpolate_data: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.interpolate_data), +/* harmony export */ is_chinese_char: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.is_chinese_char), +/* harmony export */ layer_norm: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.layer_norm), +/* harmony export */ log_softmax: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.log_softmax), +/* harmony export */ magnitude: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.magnitude), +/* harmony export */ matmul: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.matmul), +/* harmony export */ max: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.max), +/* harmony export */ mean: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.mean), +/* harmony export */ mean_pooling: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.mean_pooling), +/* harmony export */ medianFilter: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.medianFilter), +/* harmony export */ mel_filter_bank: () => (/* reexport safe */ _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.mel_filter_bank), +/* harmony export */ min: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.min), +/* harmony export */ ones: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.ones), +/* harmony export */ ones_like: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.ones_like), +/* harmony export */ permute: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.permute), +/* harmony export */ permute_data: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.permute_data), +/* harmony export */ pipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.pipeline), +/* harmony export */ quantize_embeddings: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.quantize_embeddings), +/* harmony export */ read_audio: () => (/* reexport safe */ _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.read_audio), +/* harmony export */ rfft: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.rfft), +/* harmony export */ round: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.round), +/* harmony export */ softmax: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.softmax), +/* harmony export */ spectrogram: () => (/* reexport safe */ _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.spectrogram), +/* harmony export */ stack: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.stack), +/* harmony export */ std_mean: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.std_mean), +/* harmony export */ topk: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.topk), +/* harmony export */ window_function: () => (/* reexport safe */ _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.window_function), +/* harmony export */ zeros: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.zeros), +/* harmony export */ zeros_like: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.zeros_like) +/* harmony export */ }); +/* harmony import */ var _env_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./env.js */ "./src/env.js"); +/* harmony import */ var _pipelines_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./pipelines.js */ "./src/pipelines.js"); +/* harmony import */ var _models_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./models.js */ "./src/models.js"); +/* harmony import */ var _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./tokenizers.js */ "./src/tokenizers.js"); +/* harmony import */ var _processors_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ./processors.js */ "./src/processors.js"); +/* harmony import */ var _configs_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ./configs.js */ "./src/configs.js"); +/* harmony import */ var _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! ./utils/audio.js */ "./src/utils/audio.js"); +/* harmony import */ var _utils_image_js__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__(/*! ./utils/image.js */ "./src/utils/image.js"); +/* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__(/*! ./utils/tensor.js */ "./src/utils/tensor.js"); +/* harmony import */ var _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__ = __webpack_require__(/*! ./utils/maths.js */ "./src/utils/maths.js"); +/* harmony import */ var _generation_streamers_js__WEBPACK_IMPORTED_MODULE_10__ = __webpack_require__(/*! ./generation/streamers.js */ "./src/generation/streamers.js"); +/* harmony import */ var _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_11__ = __webpack_require__(/*! ./generation/stopping_criteria.js */ "./src/generation/stopping_criteria.js"); +/** + * @file Entry point for the Transformers.js library. Only the exports from this file + * are available to the end user, and are grouped as follows: + * + * 1. [Pipelines](./pipelines) + * 2. [Environment variables](./env) + * 3. [Models](./models) + * 4. [Tokenizers](./tokenizers) + * 5. [Processors](./processors) + * + * @module transformers + */ + + + + + + + + + + + + + + + + + +var __webpack_exports__ASTFeatureExtractor = __webpack_exports__.ASTFeatureExtractor; +var __webpack_exports__ASTForAudioClassification = __webpack_exports__.ASTForAudioClassification; +var __webpack_exports__ASTModel = __webpack_exports__.ASTModel; +var __webpack_exports__ASTPreTrainedModel = __webpack_exports__.ASTPreTrainedModel; +var __webpack_exports__AlbertForMaskedLM = __webpack_exports__.AlbertForMaskedLM; +var __webpack_exports__AlbertForQuestionAnswering = __webpack_exports__.AlbertForQuestionAnswering; +var __webpack_exports__AlbertForSequenceClassification = __webpack_exports__.AlbertForSequenceClassification; +var __webpack_exports__AlbertModel = __webpack_exports__.AlbertModel; +var __webpack_exports__AlbertPreTrainedModel = __webpack_exports__.AlbertPreTrainedModel; +var __webpack_exports__AlbertTokenizer = __webpack_exports__.AlbertTokenizer; +var __webpack_exports__AudioClassificationPipeline = __webpack_exports__.AudioClassificationPipeline; +var __webpack_exports__AutoConfig = __webpack_exports__.AutoConfig; +var __webpack_exports__AutoModel = __webpack_exports__.AutoModel; +var __webpack_exports__AutoModelForAudioClassification = __webpack_exports__.AutoModelForAudioClassification; +var __webpack_exports__AutoModelForAudioFrameClassification = __webpack_exports__.AutoModelForAudioFrameClassification; +var __webpack_exports__AutoModelForCTC = __webpack_exports__.AutoModelForCTC; +var __webpack_exports__AutoModelForCausalLM = __webpack_exports__.AutoModelForCausalLM; +var __webpack_exports__AutoModelForDepthEstimation = __webpack_exports__.AutoModelForDepthEstimation; +var __webpack_exports__AutoModelForDocumentQuestionAnswering = __webpack_exports__.AutoModelForDocumentQuestionAnswering; +var __webpack_exports__AutoModelForImageClassification = __webpack_exports__.AutoModelForImageClassification; +var __webpack_exports__AutoModelForImageFeatureExtraction = __webpack_exports__.AutoModelForImageFeatureExtraction; +var __webpack_exports__AutoModelForImageMatting = __webpack_exports__.AutoModelForImageMatting; +var __webpack_exports__AutoModelForImageSegmentation = __webpack_exports__.AutoModelForImageSegmentation; +var __webpack_exports__AutoModelForImageToImage = __webpack_exports__.AutoModelForImageToImage; +var __webpack_exports__AutoModelForMaskGeneration = __webpack_exports__.AutoModelForMaskGeneration; +var __webpack_exports__AutoModelForMaskedLM = __webpack_exports__.AutoModelForMaskedLM; +var __webpack_exports__AutoModelForNormalEstimation = __webpack_exports__.AutoModelForNormalEstimation; +var __webpack_exports__AutoModelForObjectDetection = __webpack_exports__.AutoModelForObjectDetection; +var __webpack_exports__AutoModelForQuestionAnswering = __webpack_exports__.AutoModelForQuestionAnswering; +var __webpack_exports__AutoModelForSemanticSegmentation = __webpack_exports__.AutoModelForSemanticSegmentation; +var __webpack_exports__AutoModelForSeq2SeqLM = __webpack_exports__.AutoModelForSeq2SeqLM; +var __webpack_exports__AutoModelForSequenceClassification = __webpack_exports__.AutoModelForSequenceClassification; +var __webpack_exports__AutoModelForSpeechSeq2Seq = __webpack_exports__.AutoModelForSpeechSeq2Seq; +var __webpack_exports__AutoModelForTextToSpectrogram = __webpack_exports__.AutoModelForTextToSpectrogram; +var __webpack_exports__AutoModelForTextToWaveform = __webpack_exports__.AutoModelForTextToWaveform; +var __webpack_exports__AutoModelForTokenClassification = __webpack_exports__.AutoModelForTokenClassification; +var __webpack_exports__AutoModelForUniversalSegmentation = __webpack_exports__.AutoModelForUniversalSegmentation; +var __webpack_exports__AutoModelForVision2Seq = __webpack_exports__.AutoModelForVision2Seq; +var __webpack_exports__AutoModelForXVector = __webpack_exports__.AutoModelForXVector; +var __webpack_exports__AutoModelForZeroShotObjectDetection = __webpack_exports__.AutoModelForZeroShotObjectDetection; +var __webpack_exports__AutoProcessor = __webpack_exports__.AutoProcessor; +var __webpack_exports__AutoTokenizer = __webpack_exports__.AutoTokenizer; +var __webpack_exports__AutomaticSpeechRecognitionPipeline = __webpack_exports__.AutomaticSpeechRecognitionPipeline; +var __webpack_exports__BartForConditionalGeneration = __webpack_exports__.BartForConditionalGeneration; +var __webpack_exports__BartForSequenceClassification = __webpack_exports__.BartForSequenceClassification; +var __webpack_exports__BartModel = __webpack_exports__.BartModel; +var __webpack_exports__BartPretrainedModel = __webpack_exports__.BartPretrainedModel; +var __webpack_exports__BartTokenizer = __webpack_exports__.BartTokenizer; +var __webpack_exports__BaseModelOutput = __webpack_exports__.BaseModelOutput; +var __webpack_exports__BaseStreamer = __webpack_exports__.BaseStreamer; +var __webpack_exports__BeitFeatureExtractor = __webpack_exports__.BeitFeatureExtractor; +var __webpack_exports__BeitForImageClassification = __webpack_exports__.BeitForImageClassification; +var __webpack_exports__BeitModel = __webpack_exports__.BeitModel; +var __webpack_exports__BeitPreTrainedModel = __webpack_exports__.BeitPreTrainedModel; +var __webpack_exports__BertForMaskedLM = __webpack_exports__.BertForMaskedLM; +var __webpack_exports__BertForQuestionAnswering = __webpack_exports__.BertForQuestionAnswering; +var __webpack_exports__BertForSequenceClassification = __webpack_exports__.BertForSequenceClassification; +var __webpack_exports__BertForTokenClassification = __webpack_exports__.BertForTokenClassification; +var __webpack_exports__BertModel = __webpack_exports__.BertModel; +var __webpack_exports__BertPreTrainedModel = __webpack_exports__.BertPreTrainedModel; +var __webpack_exports__BertTokenizer = __webpack_exports__.BertTokenizer; +var __webpack_exports__BitImageProcessor = __webpack_exports__.BitImageProcessor; +var __webpack_exports__BlenderbotForConditionalGeneration = __webpack_exports__.BlenderbotForConditionalGeneration; +var __webpack_exports__BlenderbotModel = __webpack_exports__.BlenderbotModel; +var __webpack_exports__BlenderbotPreTrainedModel = __webpack_exports__.BlenderbotPreTrainedModel; +var __webpack_exports__BlenderbotSmallForConditionalGeneration = __webpack_exports__.BlenderbotSmallForConditionalGeneration; +var __webpack_exports__BlenderbotSmallModel = __webpack_exports__.BlenderbotSmallModel; +var __webpack_exports__BlenderbotSmallPreTrainedModel = __webpack_exports__.BlenderbotSmallPreTrainedModel; +var __webpack_exports__BlenderbotSmallTokenizer = __webpack_exports__.BlenderbotSmallTokenizer; +var __webpack_exports__BlenderbotTokenizer = __webpack_exports__.BlenderbotTokenizer; +var __webpack_exports__BloomForCausalLM = __webpack_exports__.BloomForCausalLM; +var __webpack_exports__BloomModel = __webpack_exports__.BloomModel; +var __webpack_exports__BloomPreTrainedModel = __webpack_exports__.BloomPreTrainedModel; +var __webpack_exports__BloomTokenizer = __webpack_exports__.BloomTokenizer; +var __webpack_exports__CLIPFeatureExtractor = __webpack_exports__.CLIPFeatureExtractor; +var __webpack_exports__CLIPImageProcessor = __webpack_exports__.CLIPImageProcessor; +var __webpack_exports__CLIPModel = __webpack_exports__.CLIPModel; +var __webpack_exports__CLIPPreTrainedModel = __webpack_exports__.CLIPPreTrainedModel; +var __webpack_exports__CLIPSegForImageSegmentation = __webpack_exports__.CLIPSegForImageSegmentation; +var __webpack_exports__CLIPSegModel = __webpack_exports__.CLIPSegModel; +var __webpack_exports__CLIPSegPreTrainedModel = __webpack_exports__.CLIPSegPreTrainedModel; +var __webpack_exports__CLIPTextModel = __webpack_exports__.CLIPTextModel; +var __webpack_exports__CLIPTextModelWithProjection = __webpack_exports__.CLIPTextModelWithProjection; +var __webpack_exports__CLIPTokenizer = __webpack_exports__.CLIPTokenizer; +var __webpack_exports__CLIPVisionModel = __webpack_exports__.CLIPVisionModel; +var __webpack_exports__CLIPVisionModelWithProjection = __webpack_exports__.CLIPVisionModelWithProjection; +var __webpack_exports__CamembertForMaskedLM = __webpack_exports__.CamembertForMaskedLM; +var __webpack_exports__CamembertForQuestionAnswering = __webpack_exports__.CamembertForQuestionAnswering; +var __webpack_exports__CamembertForSequenceClassification = __webpack_exports__.CamembertForSequenceClassification; +var __webpack_exports__CamembertForTokenClassification = __webpack_exports__.CamembertForTokenClassification; +var __webpack_exports__CamembertModel = __webpack_exports__.CamembertModel; +var __webpack_exports__CamembertPreTrainedModel = __webpack_exports__.CamembertPreTrainedModel; +var __webpack_exports__CamembertTokenizer = __webpack_exports__.CamembertTokenizer; +var __webpack_exports__CausalLMOutput = __webpack_exports__.CausalLMOutput; +var __webpack_exports__CausalLMOutputWithPast = __webpack_exports__.CausalLMOutputWithPast; +var __webpack_exports__ChineseCLIPFeatureExtractor = __webpack_exports__.ChineseCLIPFeatureExtractor; +var __webpack_exports__ChineseCLIPModel = __webpack_exports__.ChineseCLIPModel; +var __webpack_exports__ChineseCLIPPreTrainedModel = __webpack_exports__.ChineseCLIPPreTrainedModel; +var __webpack_exports__ClapAudioModelWithProjection = __webpack_exports__.ClapAudioModelWithProjection; +var __webpack_exports__ClapFeatureExtractor = __webpack_exports__.ClapFeatureExtractor; +var __webpack_exports__ClapModel = __webpack_exports__.ClapModel; +var __webpack_exports__ClapPreTrainedModel = __webpack_exports__.ClapPreTrainedModel; +var __webpack_exports__ClapTextModelWithProjection = __webpack_exports__.ClapTextModelWithProjection; +var __webpack_exports__CodeGenForCausalLM = __webpack_exports__.CodeGenForCausalLM; +var __webpack_exports__CodeGenModel = __webpack_exports__.CodeGenModel; +var __webpack_exports__CodeGenPreTrainedModel = __webpack_exports__.CodeGenPreTrainedModel; +var __webpack_exports__CodeGenTokenizer = __webpack_exports__.CodeGenTokenizer; +var __webpack_exports__CodeLlamaTokenizer = __webpack_exports__.CodeLlamaTokenizer; +var __webpack_exports__CohereForCausalLM = __webpack_exports__.CohereForCausalLM; +var __webpack_exports__CohereModel = __webpack_exports__.CohereModel; +var __webpack_exports__CoherePreTrainedModel = __webpack_exports__.CoherePreTrainedModel; +var __webpack_exports__CohereTokenizer = __webpack_exports__.CohereTokenizer; +var __webpack_exports__ConvBertForMaskedLM = __webpack_exports__.ConvBertForMaskedLM; +var __webpack_exports__ConvBertForQuestionAnswering = __webpack_exports__.ConvBertForQuestionAnswering; +var __webpack_exports__ConvBertForSequenceClassification = __webpack_exports__.ConvBertForSequenceClassification; +var __webpack_exports__ConvBertForTokenClassification = __webpack_exports__.ConvBertForTokenClassification; +var __webpack_exports__ConvBertModel = __webpack_exports__.ConvBertModel; +var __webpack_exports__ConvBertPreTrainedModel = __webpack_exports__.ConvBertPreTrainedModel; +var __webpack_exports__ConvBertTokenizer = __webpack_exports__.ConvBertTokenizer; +var __webpack_exports__ConvNextFeatureExtractor = __webpack_exports__.ConvNextFeatureExtractor; +var __webpack_exports__ConvNextForImageClassification = __webpack_exports__.ConvNextForImageClassification; +var __webpack_exports__ConvNextImageProcessor = __webpack_exports__.ConvNextImageProcessor; +var __webpack_exports__ConvNextModel = __webpack_exports__.ConvNextModel; +var __webpack_exports__ConvNextPreTrainedModel = __webpack_exports__.ConvNextPreTrainedModel; +var __webpack_exports__ConvNextV2ForImageClassification = __webpack_exports__.ConvNextV2ForImageClassification; +var __webpack_exports__ConvNextV2Model = __webpack_exports__.ConvNextV2Model; +var __webpack_exports__ConvNextV2PreTrainedModel = __webpack_exports__.ConvNextV2PreTrainedModel; +var __webpack_exports__DPTFeatureExtractor = __webpack_exports__.DPTFeatureExtractor; +var __webpack_exports__DPTForDepthEstimation = __webpack_exports__.DPTForDepthEstimation; +var __webpack_exports__DPTImageProcessor = __webpack_exports__.DPTImageProcessor; +var __webpack_exports__DPTModel = __webpack_exports__.DPTModel; +var __webpack_exports__DPTPreTrainedModel = __webpack_exports__.DPTPreTrainedModel; +var __webpack_exports__DebertaForMaskedLM = __webpack_exports__.DebertaForMaskedLM; +var __webpack_exports__DebertaForQuestionAnswering = __webpack_exports__.DebertaForQuestionAnswering; +var __webpack_exports__DebertaForSequenceClassification = __webpack_exports__.DebertaForSequenceClassification; +var __webpack_exports__DebertaForTokenClassification = __webpack_exports__.DebertaForTokenClassification; +var __webpack_exports__DebertaModel = __webpack_exports__.DebertaModel; +var __webpack_exports__DebertaPreTrainedModel = __webpack_exports__.DebertaPreTrainedModel; +var __webpack_exports__DebertaTokenizer = __webpack_exports__.DebertaTokenizer; +var __webpack_exports__DebertaV2ForMaskedLM = __webpack_exports__.DebertaV2ForMaskedLM; +var __webpack_exports__DebertaV2ForQuestionAnswering = __webpack_exports__.DebertaV2ForQuestionAnswering; +var __webpack_exports__DebertaV2ForSequenceClassification = __webpack_exports__.DebertaV2ForSequenceClassification; +var __webpack_exports__DebertaV2ForTokenClassification = __webpack_exports__.DebertaV2ForTokenClassification; +var __webpack_exports__DebertaV2Model = __webpack_exports__.DebertaV2Model; +var __webpack_exports__DebertaV2PreTrainedModel = __webpack_exports__.DebertaV2PreTrainedModel; +var __webpack_exports__DebertaV2Tokenizer = __webpack_exports__.DebertaV2Tokenizer; +var __webpack_exports__DecisionTransformerModel = __webpack_exports__.DecisionTransformerModel; +var __webpack_exports__DecisionTransformerPreTrainedModel = __webpack_exports__.DecisionTransformerPreTrainedModel; +var __webpack_exports__DeiTFeatureExtractor = __webpack_exports__.DeiTFeatureExtractor; +var __webpack_exports__DeiTForImageClassification = __webpack_exports__.DeiTForImageClassification; +var __webpack_exports__DeiTModel = __webpack_exports__.DeiTModel; +var __webpack_exports__DeiTPreTrainedModel = __webpack_exports__.DeiTPreTrainedModel; +var __webpack_exports__DepthAnythingForDepthEstimation = __webpack_exports__.DepthAnythingForDepthEstimation; +var __webpack_exports__DepthAnythingPreTrainedModel = __webpack_exports__.DepthAnythingPreTrainedModel; +var __webpack_exports__DepthEstimationPipeline = __webpack_exports__.DepthEstimationPipeline; +var __webpack_exports__DepthProForDepthEstimation = __webpack_exports__.DepthProForDepthEstimation; +var __webpack_exports__DepthProPreTrainedModel = __webpack_exports__.DepthProPreTrainedModel; +var __webpack_exports__DetrFeatureExtractor = __webpack_exports__.DetrFeatureExtractor; +var __webpack_exports__DetrForObjectDetection = __webpack_exports__.DetrForObjectDetection; +var __webpack_exports__DetrForSegmentation = __webpack_exports__.DetrForSegmentation; +var __webpack_exports__DetrModel = __webpack_exports__.DetrModel; +var __webpack_exports__DetrObjectDetectionOutput = __webpack_exports__.DetrObjectDetectionOutput; +var __webpack_exports__DetrPreTrainedModel = __webpack_exports__.DetrPreTrainedModel; +var __webpack_exports__DetrSegmentationOutput = __webpack_exports__.DetrSegmentationOutput; +var __webpack_exports__Dinov2ForImageClassification = __webpack_exports__.Dinov2ForImageClassification; +var __webpack_exports__Dinov2Model = __webpack_exports__.Dinov2Model; +var __webpack_exports__Dinov2PreTrainedModel = __webpack_exports__.Dinov2PreTrainedModel; +var __webpack_exports__DistilBertForMaskedLM = __webpack_exports__.DistilBertForMaskedLM; +var __webpack_exports__DistilBertForQuestionAnswering = __webpack_exports__.DistilBertForQuestionAnswering; +var __webpack_exports__DistilBertForSequenceClassification = __webpack_exports__.DistilBertForSequenceClassification; +var __webpack_exports__DistilBertForTokenClassification = __webpack_exports__.DistilBertForTokenClassification; +var __webpack_exports__DistilBertModel = __webpack_exports__.DistilBertModel; +var __webpack_exports__DistilBertPreTrainedModel = __webpack_exports__.DistilBertPreTrainedModel; +var __webpack_exports__DistilBertTokenizer = __webpack_exports__.DistilBertTokenizer; +var __webpack_exports__DocumentQuestionAnsweringPipeline = __webpack_exports__.DocumentQuestionAnsweringPipeline; +var __webpack_exports__DonutFeatureExtractor = __webpack_exports__.DonutFeatureExtractor; +var __webpack_exports__DonutImageProcessor = __webpack_exports__.DonutImageProcessor; +var __webpack_exports__DonutSwinModel = __webpack_exports__.DonutSwinModel; +var __webpack_exports__DonutSwinPreTrainedModel = __webpack_exports__.DonutSwinPreTrainedModel; +var __webpack_exports__EfficientNetForImageClassification = __webpack_exports__.EfficientNetForImageClassification; +var __webpack_exports__EfficientNetImageProcessor = __webpack_exports__.EfficientNetImageProcessor; +var __webpack_exports__EfficientNetModel = __webpack_exports__.EfficientNetModel; +var __webpack_exports__EfficientNetPreTrainedModel = __webpack_exports__.EfficientNetPreTrainedModel; +var __webpack_exports__ElectraForMaskedLM = __webpack_exports__.ElectraForMaskedLM; +var __webpack_exports__ElectraForQuestionAnswering = __webpack_exports__.ElectraForQuestionAnswering; +var __webpack_exports__ElectraForSequenceClassification = __webpack_exports__.ElectraForSequenceClassification; +var __webpack_exports__ElectraForTokenClassification = __webpack_exports__.ElectraForTokenClassification; +var __webpack_exports__ElectraModel = __webpack_exports__.ElectraModel; +var __webpack_exports__ElectraPreTrainedModel = __webpack_exports__.ElectraPreTrainedModel; +var __webpack_exports__ElectraTokenizer = __webpack_exports__.ElectraTokenizer; +var __webpack_exports__EosTokenCriteria = __webpack_exports__.EosTokenCriteria; +var __webpack_exports__EsmForMaskedLM = __webpack_exports__.EsmForMaskedLM; +var __webpack_exports__EsmForSequenceClassification = __webpack_exports__.EsmForSequenceClassification; +var __webpack_exports__EsmForTokenClassification = __webpack_exports__.EsmForTokenClassification; +var __webpack_exports__EsmModel = __webpack_exports__.EsmModel; +var __webpack_exports__EsmPreTrainedModel = __webpack_exports__.EsmPreTrainedModel; +var __webpack_exports__EsmTokenizer = __webpack_exports__.EsmTokenizer; +var __webpack_exports__FFT = __webpack_exports__.FFT; +var __webpack_exports__FalconForCausalLM = __webpack_exports__.FalconForCausalLM; +var __webpack_exports__FalconModel = __webpack_exports__.FalconModel; +var __webpack_exports__FalconPreTrainedModel = __webpack_exports__.FalconPreTrainedModel; +var __webpack_exports__FalconTokenizer = __webpack_exports__.FalconTokenizer; +var __webpack_exports__FastViTForImageClassification = __webpack_exports__.FastViTForImageClassification; +var __webpack_exports__FastViTModel = __webpack_exports__.FastViTModel; +var __webpack_exports__FastViTPreTrainedModel = __webpack_exports__.FastViTPreTrainedModel; +var __webpack_exports__FeatureExtractionPipeline = __webpack_exports__.FeatureExtractionPipeline; +var __webpack_exports__FeatureExtractor = __webpack_exports__.FeatureExtractor; +var __webpack_exports__FillMaskPipeline = __webpack_exports__.FillMaskPipeline; +var __webpack_exports__Florence2ForConditionalGeneration = __webpack_exports__.Florence2ForConditionalGeneration; +var __webpack_exports__Florence2PreTrainedModel = __webpack_exports__.Florence2PreTrainedModel; +var __webpack_exports__Florence2Processor = __webpack_exports__.Florence2Processor; +var __webpack_exports__GLPNFeatureExtractor = __webpack_exports__.GLPNFeatureExtractor; +var __webpack_exports__GLPNForDepthEstimation = __webpack_exports__.GLPNForDepthEstimation; +var __webpack_exports__GLPNModel = __webpack_exports__.GLPNModel; +var __webpack_exports__GLPNPreTrainedModel = __webpack_exports__.GLPNPreTrainedModel; +var __webpack_exports__GPT2LMHeadModel = __webpack_exports__.GPT2LMHeadModel; +var __webpack_exports__GPT2Model = __webpack_exports__.GPT2Model; +var __webpack_exports__GPT2PreTrainedModel = __webpack_exports__.GPT2PreTrainedModel; +var __webpack_exports__GPT2Tokenizer = __webpack_exports__.GPT2Tokenizer; +var __webpack_exports__GPTBigCodeForCausalLM = __webpack_exports__.GPTBigCodeForCausalLM; +var __webpack_exports__GPTBigCodeModel = __webpack_exports__.GPTBigCodeModel; +var __webpack_exports__GPTBigCodePreTrainedModel = __webpack_exports__.GPTBigCodePreTrainedModel; +var __webpack_exports__GPTJForCausalLM = __webpack_exports__.GPTJForCausalLM; +var __webpack_exports__GPTJModel = __webpack_exports__.GPTJModel; +var __webpack_exports__GPTJPreTrainedModel = __webpack_exports__.GPTJPreTrainedModel; +var __webpack_exports__GPTNeoForCausalLM = __webpack_exports__.GPTNeoForCausalLM; +var __webpack_exports__GPTNeoModel = __webpack_exports__.GPTNeoModel; +var __webpack_exports__GPTNeoPreTrainedModel = __webpack_exports__.GPTNeoPreTrainedModel; +var __webpack_exports__GPTNeoXForCausalLM = __webpack_exports__.GPTNeoXForCausalLM; +var __webpack_exports__GPTNeoXModel = __webpack_exports__.GPTNeoXModel; +var __webpack_exports__GPTNeoXPreTrainedModel = __webpack_exports__.GPTNeoXPreTrainedModel; +var __webpack_exports__GPTNeoXTokenizer = __webpack_exports__.GPTNeoXTokenizer; +var __webpack_exports__Gemma2ForCausalLM = __webpack_exports__.Gemma2ForCausalLM; +var __webpack_exports__Gemma2Model = __webpack_exports__.Gemma2Model; +var __webpack_exports__Gemma2PreTrainedModel = __webpack_exports__.Gemma2PreTrainedModel; +var __webpack_exports__GemmaForCausalLM = __webpack_exports__.GemmaForCausalLM; +var __webpack_exports__GemmaModel = __webpack_exports__.GemmaModel; +var __webpack_exports__GemmaPreTrainedModel = __webpack_exports__.GemmaPreTrainedModel; +var __webpack_exports__GemmaTokenizer = __webpack_exports__.GemmaTokenizer; +var __webpack_exports__GraniteForCausalLM = __webpack_exports__.GraniteForCausalLM; +var __webpack_exports__GraniteModel = __webpack_exports__.GraniteModel; +var __webpack_exports__GranitePreTrainedModel = __webpack_exports__.GranitePreTrainedModel; +var __webpack_exports__Grok1Tokenizer = __webpack_exports__.Grok1Tokenizer; +var __webpack_exports__GroupViTModel = __webpack_exports__.GroupViTModel; +var __webpack_exports__GroupViTPreTrainedModel = __webpack_exports__.GroupViTPreTrainedModel; +var __webpack_exports__HerbertTokenizer = __webpack_exports__.HerbertTokenizer; +var __webpack_exports__HieraForImageClassification = __webpack_exports__.HieraForImageClassification; +var __webpack_exports__HieraModel = __webpack_exports__.HieraModel; +var __webpack_exports__HieraPreTrainedModel = __webpack_exports__.HieraPreTrainedModel; +var __webpack_exports__HubertForCTC = __webpack_exports__.HubertForCTC; +var __webpack_exports__HubertForSequenceClassification = __webpack_exports__.HubertForSequenceClassification; +var __webpack_exports__HubertModel = __webpack_exports__.HubertModel; +var __webpack_exports__HubertPreTrainedModel = __webpack_exports__.HubertPreTrainedModel; +var __webpack_exports__ImageClassificationPipeline = __webpack_exports__.ImageClassificationPipeline; +var __webpack_exports__ImageFeatureExtractionPipeline = __webpack_exports__.ImageFeatureExtractionPipeline; +var __webpack_exports__ImageFeatureExtractor = __webpack_exports__.ImageFeatureExtractor; +var __webpack_exports__ImageMattingOutput = __webpack_exports__.ImageMattingOutput; +var __webpack_exports__ImageSegmentationPipeline = __webpack_exports__.ImageSegmentationPipeline; +var __webpack_exports__ImageToImagePipeline = __webpack_exports__.ImageToImagePipeline; +var __webpack_exports__ImageToTextPipeline = __webpack_exports__.ImageToTextPipeline; +var __webpack_exports__InterruptableStoppingCriteria = __webpack_exports__.InterruptableStoppingCriteria; +var __webpack_exports__JAISLMHeadModel = __webpack_exports__.JAISLMHeadModel; +var __webpack_exports__JAISModel = __webpack_exports__.JAISModel; +var __webpack_exports__JAISPreTrainedModel = __webpack_exports__.JAISPreTrainedModel; +var __webpack_exports__LlamaForCausalLM = __webpack_exports__.LlamaForCausalLM; +var __webpack_exports__LlamaModel = __webpack_exports__.LlamaModel; +var __webpack_exports__LlamaPreTrainedModel = __webpack_exports__.LlamaPreTrainedModel; +var __webpack_exports__LlamaTokenizer = __webpack_exports__.LlamaTokenizer; +var __webpack_exports__LlavaForConditionalGeneration = __webpack_exports__.LlavaForConditionalGeneration; +var __webpack_exports__LlavaPreTrainedModel = __webpack_exports__.LlavaPreTrainedModel; +var __webpack_exports__LongT5ForConditionalGeneration = __webpack_exports__.LongT5ForConditionalGeneration; +var __webpack_exports__LongT5Model = __webpack_exports__.LongT5Model; +var __webpack_exports__LongT5PreTrainedModel = __webpack_exports__.LongT5PreTrainedModel; +var __webpack_exports__M2M100ForConditionalGeneration = __webpack_exports__.M2M100ForConditionalGeneration; +var __webpack_exports__M2M100Model = __webpack_exports__.M2M100Model; +var __webpack_exports__M2M100PreTrainedModel = __webpack_exports__.M2M100PreTrainedModel; +var __webpack_exports__M2M100Tokenizer = __webpack_exports__.M2M100Tokenizer; +var __webpack_exports__MBart50Tokenizer = __webpack_exports__.MBart50Tokenizer; +var __webpack_exports__MBartForCausalLM = __webpack_exports__.MBartForCausalLM; +var __webpack_exports__MBartForConditionalGeneration = __webpack_exports__.MBartForConditionalGeneration; +var __webpack_exports__MBartForSequenceClassification = __webpack_exports__.MBartForSequenceClassification; +var __webpack_exports__MBartModel = __webpack_exports__.MBartModel; +var __webpack_exports__MBartPreTrainedModel = __webpack_exports__.MBartPreTrainedModel; +var __webpack_exports__MBartTokenizer = __webpack_exports__.MBartTokenizer; +var __webpack_exports__MPNetForMaskedLM = __webpack_exports__.MPNetForMaskedLM; +var __webpack_exports__MPNetForQuestionAnswering = __webpack_exports__.MPNetForQuestionAnswering; +var __webpack_exports__MPNetForSequenceClassification = __webpack_exports__.MPNetForSequenceClassification; +var __webpack_exports__MPNetForTokenClassification = __webpack_exports__.MPNetForTokenClassification; +var __webpack_exports__MPNetModel = __webpack_exports__.MPNetModel; +var __webpack_exports__MPNetPreTrainedModel = __webpack_exports__.MPNetPreTrainedModel; +var __webpack_exports__MPNetTokenizer = __webpack_exports__.MPNetTokenizer; +var __webpack_exports__MT5ForConditionalGeneration = __webpack_exports__.MT5ForConditionalGeneration; +var __webpack_exports__MT5Model = __webpack_exports__.MT5Model; +var __webpack_exports__MT5PreTrainedModel = __webpack_exports__.MT5PreTrainedModel; +var __webpack_exports__MarianMTModel = __webpack_exports__.MarianMTModel; +var __webpack_exports__MarianModel = __webpack_exports__.MarianModel; +var __webpack_exports__MarianPreTrainedModel = __webpack_exports__.MarianPreTrainedModel; +var __webpack_exports__MarianTokenizer = __webpack_exports__.MarianTokenizer; +var __webpack_exports__MaskFormerFeatureExtractor = __webpack_exports__.MaskFormerFeatureExtractor; +var __webpack_exports__MaskFormerForInstanceSegmentation = __webpack_exports__.MaskFormerForInstanceSegmentation; +var __webpack_exports__MaskFormerModel = __webpack_exports__.MaskFormerModel; +var __webpack_exports__MaskFormerPreTrainedModel = __webpack_exports__.MaskFormerPreTrainedModel; +var __webpack_exports__MaskedLMOutput = __webpack_exports__.MaskedLMOutput; +var __webpack_exports__MaxLengthCriteria = __webpack_exports__.MaxLengthCriteria; +var __webpack_exports__MistralForCausalLM = __webpack_exports__.MistralForCausalLM; +var __webpack_exports__MistralModel = __webpack_exports__.MistralModel; +var __webpack_exports__MistralPreTrainedModel = __webpack_exports__.MistralPreTrainedModel; +var __webpack_exports__MobileBertForMaskedLM = __webpack_exports__.MobileBertForMaskedLM; +var __webpack_exports__MobileBertForQuestionAnswering = __webpack_exports__.MobileBertForQuestionAnswering; +var __webpack_exports__MobileBertForSequenceClassification = __webpack_exports__.MobileBertForSequenceClassification; +var __webpack_exports__MobileBertModel = __webpack_exports__.MobileBertModel; +var __webpack_exports__MobileBertPreTrainedModel = __webpack_exports__.MobileBertPreTrainedModel; +var __webpack_exports__MobileBertTokenizer = __webpack_exports__.MobileBertTokenizer; +var __webpack_exports__MobileLLMForCausalLM = __webpack_exports__.MobileLLMForCausalLM; +var __webpack_exports__MobileLLMModel = __webpack_exports__.MobileLLMModel; +var __webpack_exports__MobileLLMPreTrainedModel = __webpack_exports__.MobileLLMPreTrainedModel; +var __webpack_exports__MobileNetV1FeatureExtractor = __webpack_exports__.MobileNetV1FeatureExtractor; +var __webpack_exports__MobileNetV1ForImageClassification = __webpack_exports__.MobileNetV1ForImageClassification; +var __webpack_exports__MobileNetV1Model = __webpack_exports__.MobileNetV1Model; +var __webpack_exports__MobileNetV1PreTrainedModel = __webpack_exports__.MobileNetV1PreTrainedModel; +var __webpack_exports__MobileNetV2FeatureExtractor = __webpack_exports__.MobileNetV2FeatureExtractor; +var __webpack_exports__MobileNetV2ForImageClassification = __webpack_exports__.MobileNetV2ForImageClassification; +var __webpack_exports__MobileNetV2Model = __webpack_exports__.MobileNetV2Model; +var __webpack_exports__MobileNetV2PreTrainedModel = __webpack_exports__.MobileNetV2PreTrainedModel; +var __webpack_exports__MobileNetV3FeatureExtractor = __webpack_exports__.MobileNetV3FeatureExtractor; +var __webpack_exports__MobileNetV3ForImageClassification = __webpack_exports__.MobileNetV3ForImageClassification; +var __webpack_exports__MobileNetV3Model = __webpack_exports__.MobileNetV3Model; +var __webpack_exports__MobileNetV3PreTrainedModel = __webpack_exports__.MobileNetV3PreTrainedModel; +var __webpack_exports__MobileNetV4FeatureExtractor = __webpack_exports__.MobileNetV4FeatureExtractor; +var __webpack_exports__MobileNetV4ForImageClassification = __webpack_exports__.MobileNetV4ForImageClassification; +var __webpack_exports__MobileNetV4Model = __webpack_exports__.MobileNetV4Model; +var __webpack_exports__MobileNetV4PreTrainedModel = __webpack_exports__.MobileNetV4PreTrainedModel; +var __webpack_exports__MobileViTFeatureExtractor = __webpack_exports__.MobileViTFeatureExtractor; +var __webpack_exports__MobileViTForImageClassification = __webpack_exports__.MobileViTForImageClassification; +var __webpack_exports__MobileViTImageProcessor = __webpack_exports__.MobileViTImageProcessor; +var __webpack_exports__MobileViTModel = __webpack_exports__.MobileViTModel; +var __webpack_exports__MobileViTPreTrainedModel = __webpack_exports__.MobileViTPreTrainedModel; +var __webpack_exports__MobileViTV2ForImageClassification = __webpack_exports__.MobileViTV2ForImageClassification; +var __webpack_exports__MobileViTV2Model = __webpack_exports__.MobileViTV2Model; +var __webpack_exports__MobileViTV2PreTrainedModel = __webpack_exports__.MobileViTV2PreTrainedModel; +var __webpack_exports__ModelOutput = __webpack_exports__.ModelOutput; +var __webpack_exports__Moondream1ForConditionalGeneration = __webpack_exports__.Moondream1ForConditionalGeneration; +var __webpack_exports__MptForCausalLM = __webpack_exports__.MptForCausalLM; +var __webpack_exports__MptModel = __webpack_exports__.MptModel; +var __webpack_exports__MptPreTrainedModel = __webpack_exports__.MptPreTrainedModel; +var __webpack_exports__MusicgenForCausalLM = __webpack_exports__.MusicgenForCausalLM; +var __webpack_exports__MusicgenForConditionalGeneration = __webpack_exports__.MusicgenForConditionalGeneration; +var __webpack_exports__MusicgenModel = __webpack_exports__.MusicgenModel; +var __webpack_exports__MusicgenPreTrainedModel = __webpack_exports__.MusicgenPreTrainedModel; +var __webpack_exports__NllbTokenizer = __webpack_exports__.NllbTokenizer; +var __webpack_exports__NomicBertModel = __webpack_exports__.NomicBertModel; +var __webpack_exports__NomicBertPreTrainedModel = __webpack_exports__.NomicBertPreTrainedModel; +var __webpack_exports__NougatImageProcessor = __webpack_exports__.NougatImageProcessor; +var __webpack_exports__NougatTokenizer = __webpack_exports__.NougatTokenizer; +var __webpack_exports__OPTForCausalLM = __webpack_exports__.OPTForCausalLM; +var __webpack_exports__OPTModel = __webpack_exports__.OPTModel; +var __webpack_exports__OPTPreTrainedModel = __webpack_exports__.OPTPreTrainedModel; +var __webpack_exports__ObjectDetectionPipeline = __webpack_exports__.ObjectDetectionPipeline; +var __webpack_exports__OlmoForCausalLM = __webpack_exports__.OlmoForCausalLM; +var __webpack_exports__OlmoModel = __webpack_exports__.OlmoModel; +var __webpack_exports__OlmoPreTrainedModel = __webpack_exports__.OlmoPreTrainedModel; +var __webpack_exports__OpenELMForCausalLM = __webpack_exports__.OpenELMForCausalLM; +var __webpack_exports__OpenELMModel = __webpack_exports__.OpenELMModel; +var __webpack_exports__OpenELMPreTrainedModel = __webpack_exports__.OpenELMPreTrainedModel; +var __webpack_exports__OwlViTFeatureExtractor = __webpack_exports__.OwlViTFeatureExtractor; +var __webpack_exports__OwlViTForObjectDetection = __webpack_exports__.OwlViTForObjectDetection; +var __webpack_exports__OwlViTModel = __webpack_exports__.OwlViTModel; +var __webpack_exports__OwlViTPreTrainedModel = __webpack_exports__.OwlViTPreTrainedModel; +var __webpack_exports__OwlViTProcessor = __webpack_exports__.OwlViTProcessor; +var __webpack_exports__Owlv2ForObjectDetection = __webpack_exports__.Owlv2ForObjectDetection; +var __webpack_exports__Owlv2ImageProcessor = __webpack_exports__.Owlv2ImageProcessor; +var __webpack_exports__Owlv2Model = __webpack_exports__.Owlv2Model; +var __webpack_exports__Owlv2PreTrainedModel = __webpack_exports__.Owlv2PreTrainedModel; +var __webpack_exports__Phi3ForCausalLM = __webpack_exports__.Phi3ForCausalLM; +var __webpack_exports__Phi3Model = __webpack_exports__.Phi3Model; +var __webpack_exports__Phi3PreTrainedModel = __webpack_exports__.Phi3PreTrainedModel; +var __webpack_exports__PhiForCausalLM = __webpack_exports__.PhiForCausalLM; +var __webpack_exports__PhiModel = __webpack_exports__.PhiModel; +var __webpack_exports__PhiPreTrainedModel = __webpack_exports__.PhiPreTrainedModel; +var __webpack_exports__Pipeline = __webpack_exports__.Pipeline; +var __webpack_exports__PreTrainedModel = __webpack_exports__.PreTrainedModel; +var __webpack_exports__PreTrainedTokenizer = __webpack_exports__.PreTrainedTokenizer; +var __webpack_exports__PretrainedConfig = __webpack_exports__.PretrainedConfig; +var __webpack_exports__PretrainedMixin = __webpack_exports__.PretrainedMixin; +var __webpack_exports__Processor = __webpack_exports__.Processor; +var __webpack_exports__PvtForImageClassification = __webpack_exports__.PvtForImageClassification; +var __webpack_exports__PvtImageProcessor = __webpack_exports__.PvtImageProcessor; +var __webpack_exports__PvtModel = __webpack_exports__.PvtModel; +var __webpack_exports__PvtPreTrainedModel = __webpack_exports__.PvtPreTrainedModel; +var __webpack_exports__PyAnnoteFeatureExtractor = __webpack_exports__.PyAnnoteFeatureExtractor; +var __webpack_exports__PyAnnoteForAudioFrameClassification = __webpack_exports__.PyAnnoteForAudioFrameClassification; +var __webpack_exports__PyAnnoteModel = __webpack_exports__.PyAnnoteModel; +var __webpack_exports__PyAnnotePreTrainedModel = __webpack_exports__.PyAnnotePreTrainedModel; +var __webpack_exports__PyAnnoteProcessor = __webpack_exports__.PyAnnoteProcessor; +var __webpack_exports__QuestionAnsweringModelOutput = __webpack_exports__.QuestionAnsweringModelOutput; +var __webpack_exports__QuestionAnsweringPipeline = __webpack_exports__.QuestionAnsweringPipeline; +var __webpack_exports__Qwen2ForCausalLM = __webpack_exports__.Qwen2ForCausalLM; +var __webpack_exports__Qwen2Model = __webpack_exports__.Qwen2Model; +var __webpack_exports__Qwen2PreTrainedModel = __webpack_exports__.Qwen2PreTrainedModel; +var __webpack_exports__Qwen2Tokenizer = __webpack_exports__.Qwen2Tokenizer; +var __webpack_exports__RTDetrForObjectDetection = __webpack_exports__.RTDetrForObjectDetection; +var __webpack_exports__RTDetrImageProcessor = __webpack_exports__.RTDetrImageProcessor; +var __webpack_exports__RTDetrModel = __webpack_exports__.RTDetrModel; +var __webpack_exports__RTDetrObjectDetectionOutput = __webpack_exports__.RTDetrObjectDetectionOutput; +var __webpack_exports__RTDetrPreTrainedModel = __webpack_exports__.RTDetrPreTrainedModel; +var __webpack_exports__RawImage = __webpack_exports__.RawImage; +var __webpack_exports__ResNetForImageClassification = __webpack_exports__.ResNetForImageClassification; +var __webpack_exports__ResNetModel = __webpack_exports__.ResNetModel; +var __webpack_exports__ResNetPreTrainedModel = __webpack_exports__.ResNetPreTrainedModel; +var __webpack_exports__RoFormerForMaskedLM = __webpack_exports__.RoFormerForMaskedLM; +var __webpack_exports__RoFormerForQuestionAnswering = __webpack_exports__.RoFormerForQuestionAnswering; +var __webpack_exports__RoFormerForSequenceClassification = __webpack_exports__.RoFormerForSequenceClassification; +var __webpack_exports__RoFormerForTokenClassification = __webpack_exports__.RoFormerForTokenClassification; +var __webpack_exports__RoFormerModel = __webpack_exports__.RoFormerModel; +var __webpack_exports__RoFormerPreTrainedModel = __webpack_exports__.RoFormerPreTrainedModel; +var __webpack_exports__RoFormerTokenizer = __webpack_exports__.RoFormerTokenizer; +var __webpack_exports__RobertaForMaskedLM = __webpack_exports__.RobertaForMaskedLM; +var __webpack_exports__RobertaForQuestionAnswering = __webpack_exports__.RobertaForQuestionAnswering; +var __webpack_exports__RobertaForSequenceClassification = __webpack_exports__.RobertaForSequenceClassification; +var __webpack_exports__RobertaForTokenClassification = __webpack_exports__.RobertaForTokenClassification; +var __webpack_exports__RobertaModel = __webpack_exports__.RobertaModel; +var __webpack_exports__RobertaPreTrainedModel = __webpack_exports__.RobertaPreTrainedModel; +var __webpack_exports__RobertaTokenizer = __webpack_exports__.RobertaTokenizer; +var __webpack_exports__SamImageProcessor = __webpack_exports__.SamImageProcessor; +var __webpack_exports__SamImageSegmentationOutput = __webpack_exports__.SamImageSegmentationOutput; +var __webpack_exports__SamModel = __webpack_exports__.SamModel; +var __webpack_exports__SamPreTrainedModel = __webpack_exports__.SamPreTrainedModel; +var __webpack_exports__SamProcessor = __webpack_exports__.SamProcessor; +var __webpack_exports__SapiensFeatureExtractor = __webpack_exports__.SapiensFeatureExtractor; +var __webpack_exports__SapiensForDepthEstimation = __webpack_exports__.SapiensForDepthEstimation; +var __webpack_exports__SapiensForNormalEstimation = __webpack_exports__.SapiensForNormalEstimation; +var __webpack_exports__SapiensForSemanticSegmentation = __webpack_exports__.SapiensForSemanticSegmentation; +var __webpack_exports__SapiensPreTrainedModel = __webpack_exports__.SapiensPreTrainedModel; +var __webpack_exports__SeamlessM4TFeatureExtractor = __webpack_exports__.SeamlessM4TFeatureExtractor; +var __webpack_exports__SegformerFeatureExtractor = __webpack_exports__.SegformerFeatureExtractor; +var __webpack_exports__SegformerForImageClassification = __webpack_exports__.SegformerForImageClassification; +var __webpack_exports__SegformerForSemanticSegmentation = __webpack_exports__.SegformerForSemanticSegmentation; +var __webpack_exports__SegformerModel = __webpack_exports__.SegformerModel; +var __webpack_exports__SegformerPreTrainedModel = __webpack_exports__.SegformerPreTrainedModel; +var __webpack_exports__Seq2SeqLMOutput = __webpack_exports__.Seq2SeqLMOutput; +var __webpack_exports__SequenceClassifierOutput = __webpack_exports__.SequenceClassifierOutput; +var __webpack_exports__SiglipImageProcessor = __webpack_exports__.SiglipImageProcessor; +var __webpack_exports__SiglipModel = __webpack_exports__.SiglipModel; +var __webpack_exports__SiglipPreTrainedModel = __webpack_exports__.SiglipPreTrainedModel; +var __webpack_exports__SiglipTextModel = __webpack_exports__.SiglipTextModel; +var __webpack_exports__SiglipTokenizer = __webpack_exports__.SiglipTokenizer; +var __webpack_exports__SiglipVisionModel = __webpack_exports__.SiglipVisionModel; +var __webpack_exports__SpeechT5FeatureExtractor = __webpack_exports__.SpeechT5FeatureExtractor; +var __webpack_exports__SpeechT5ForSpeechToText = __webpack_exports__.SpeechT5ForSpeechToText; +var __webpack_exports__SpeechT5ForTextToSpeech = __webpack_exports__.SpeechT5ForTextToSpeech; +var __webpack_exports__SpeechT5HifiGan = __webpack_exports__.SpeechT5HifiGan; +var __webpack_exports__SpeechT5Model = __webpack_exports__.SpeechT5Model; +var __webpack_exports__SpeechT5PreTrainedModel = __webpack_exports__.SpeechT5PreTrainedModel; +var __webpack_exports__SpeechT5Processor = __webpack_exports__.SpeechT5Processor; +var __webpack_exports__SpeechT5Tokenizer = __webpack_exports__.SpeechT5Tokenizer; +var __webpack_exports__SqueezeBertForMaskedLM = __webpack_exports__.SqueezeBertForMaskedLM; +var __webpack_exports__SqueezeBertForQuestionAnswering = __webpack_exports__.SqueezeBertForQuestionAnswering; +var __webpack_exports__SqueezeBertForSequenceClassification = __webpack_exports__.SqueezeBertForSequenceClassification; +var __webpack_exports__SqueezeBertModel = __webpack_exports__.SqueezeBertModel; +var __webpack_exports__SqueezeBertPreTrainedModel = __webpack_exports__.SqueezeBertPreTrainedModel; +var __webpack_exports__SqueezeBertTokenizer = __webpack_exports__.SqueezeBertTokenizer; +var __webpack_exports__StableLmForCausalLM = __webpack_exports__.StableLmForCausalLM; +var __webpack_exports__StableLmModel = __webpack_exports__.StableLmModel; +var __webpack_exports__StableLmPreTrainedModel = __webpack_exports__.StableLmPreTrainedModel; +var __webpack_exports__Starcoder2ForCausalLM = __webpack_exports__.Starcoder2ForCausalLM; +var __webpack_exports__Starcoder2Model = __webpack_exports__.Starcoder2Model; +var __webpack_exports__Starcoder2PreTrainedModel = __webpack_exports__.Starcoder2PreTrainedModel; +var __webpack_exports__StoppingCriteria = __webpack_exports__.StoppingCriteria; +var __webpack_exports__StoppingCriteriaList = __webpack_exports__.StoppingCriteriaList; +var __webpack_exports__SummarizationPipeline = __webpack_exports__.SummarizationPipeline; +var __webpack_exports__Swin2SRForImageSuperResolution = __webpack_exports__.Swin2SRForImageSuperResolution; +var __webpack_exports__Swin2SRImageProcessor = __webpack_exports__.Swin2SRImageProcessor; +var __webpack_exports__Swin2SRModel = __webpack_exports__.Swin2SRModel; +var __webpack_exports__Swin2SRPreTrainedModel = __webpack_exports__.Swin2SRPreTrainedModel; +var __webpack_exports__SwinForImageClassification = __webpack_exports__.SwinForImageClassification; +var __webpack_exports__SwinModel = __webpack_exports__.SwinModel; +var __webpack_exports__SwinPreTrainedModel = __webpack_exports__.SwinPreTrainedModel; +var __webpack_exports__T5ForConditionalGeneration = __webpack_exports__.T5ForConditionalGeneration; +var __webpack_exports__T5Model = __webpack_exports__.T5Model; +var __webpack_exports__T5PreTrainedModel = __webpack_exports__.T5PreTrainedModel; +var __webpack_exports__T5Tokenizer = __webpack_exports__.T5Tokenizer; +var __webpack_exports__TableTransformerForObjectDetection = __webpack_exports__.TableTransformerForObjectDetection; +var __webpack_exports__TableTransformerModel = __webpack_exports__.TableTransformerModel; +var __webpack_exports__TableTransformerObjectDetectionOutput = __webpack_exports__.TableTransformerObjectDetectionOutput; +var __webpack_exports__TableTransformerPreTrainedModel = __webpack_exports__.TableTransformerPreTrainedModel; +var __webpack_exports__Tensor = __webpack_exports__.Tensor; +var __webpack_exports__Text2TextGenerationPipeline = __webpack_exports__.Text2TextGenerationPipeline; +var __webpack_exports__TextClassificationPipeline = __webpack_exports__.TextClassificationPipeline; +var __webpack_exports__TextGenerationPipeline = __webpack_exports__.TextGenerationPipeline; +var __webpack_exports__TextStreamer = __webpack_exports__.TextStreamer; +var __webpack_exports__TextToAudioPipeline = __webpack_exports__.TextToAudioPipeline; +var __webpack_exports__TokenClassificationPipeline = __webpack_exports__.TokenClassificationPipeline; +var __webpack_exports__TokenClassifierOutput = __webpack_exports__.TokenClassifierOutput; +var __webpack_exports__TokenizerModel = __webpack_exports__.TokenizerModel; +var __webpack_exports__TrOCRForCausalLM = __webpack_exports__.TrOCRForCausalLM; +var __webpack_exports__TrOCRPreTrainedModel = __webpack_exports__.TrOCRPreTrainedModel; +var __webpack_exports__TranslationPipeline = __webpack_exports__.TranslationPipeline; +var __webpack_exports__UniSpeechForCTC = __webpack_exports__.UniSpeechForCTC; +var __webpack_exports__UniSpeechForSequenceClassification = __webpack_exports__.UniSpeechForSequenceClassification; +var __webpack_exports__UniSpeechModel = __webpack_exports__.UniSpeechModel; +var __webpack_exports__UniSpeechPreTrainedModel = __webpack_exports__.UniSpeechPreTrainedModel; +var __webpack_exports__UniSpeechSatForAudioFrameClassification = __webpack_exports__.UniSpeechSatForAudioFrameClassification; +var __webpack_exports__UniSpeechSatForCTC = __webpack_exports__.UniSpeechSatForCTC; +var __webpack_exports__UniSpeechSatForSequenceClassification = __webpack_exports__.UniSpeechSatForSequenceClassification; +var __webpack_exports__UniSpeechSatModel = __webpack_exports__.UniSpeechSatModel; +var __webpack_exports__UniSpeechSatPreTrainedModel = __webpack_exports__.UniSpeechSatPreTrainedModel; +var __webpack_exports__ViTFeatureExtractor = __webpack_exports__.ViTFeatureExtractor; +var __webpack_exports__ViTForImageClassification = __webpack_exports__.ViTForImageClassification; +var __webpack_exports__ViTImageProcessor = __webpack_exports__.ViTImageProcessor; +var __webpack_exports__ViTMAEModel = __webpack_exports__.ViTMAEModel; +var __webpack_exports__ViTMAEPreTrainedModel = __webpack_exports__.ViTMAEPreTrainedModel; +var __webpack_exports__ViTMSNForImageClassification = __webpack_exports__.ViTMSNForImageClassification; +var __webpack_exports__ViTMSNModel = __webpack_exports__.ViTMSNModel; +var __webpack_exports__ViTMSNPreTrainedModel = __webpack_exports__.ViTMSNPreTrainedModel; +var __webpack_exports__ViTModel = __webpack_exports__.ViTModel; +var __webpack_exports__ViTPreTrainedModel = __webpack_exports__.ViTPreTrainedModel; +var __webpack_exports__VisionEncoderDecoderModel = __webpack_exports__.VisionEncoderDecoderModel; +var __webpack_exports__VitMatteForImageMatting = __webpack_exports__.VitMatteForImageMatting; +var __webpack_exports__VitMatteImageProcessor = __webpack_exports__.VitMatteImageProcessor; +var __webpack_exports__VitMattePreTrainedModel = __webpack_exports__.VitMattePreTrainedModel; +var __webpack_exports__VitsModel = __webpack_exports__.VitsModel; +var __webpack_exports__VitsModelOutput = __webpack_exports__.VitsModelOutput; +var __webpack_exports__VitsPreTrainedModel = __webpack_exports__.VitsPreTrainedModel; +var __webpack_exports__VitsTokenizer = __webpack_exports__.VitsTokenizer; +var __webpack_exports__Wav2Vec2BertForCTC = __webpack_exports__.Wav2Vec2BertForCTC; +var __webpack_exports__Wav2Vec2BertForSequenceClassification = __webpack_exports__.Wav2Vec2BertForSequenceClassification; +var __webpack_exports__Wav2Vec2BertModel = __webpack_exports__.Wav2Vec2BertModel; +var __webpack_exports__Wav2Vec2BertPreTrainedModel = __webpack_exports__.Wav2Vec2BertPreTrainedModel; +var __webpack_exports__Wav2Vec2CTCTokenizer = __webpack_exports__.Wav2Vec2CTCTokenizer; +var __webpack_exports__Wav2Vec2FeatureExtractor = __webpack_exports__.Wav2Vec2FeatureExtractor; +var __webpack_exports__Wav2Vec2ForAudioFrameClassification = __webpack_exports__.Wav2Vec2ForAudioFrameClassification; +var __webpack_exports__Wav2Vec2ForCTC = __webpack_exports__.Wav2Vec2ForCTC; +var __webpack_exports__Wav2Vec2ForSequenceClassification = __webpack_exports__.Wav2Vec2ForSequenceClassification; +var __webpack_exports__Wav2Vec2Model = __webpack_exports__.Wav2Vec2Model; +var __webpack_exports__Wav2Vec2PreTrainedModel = __webpack_exports__.Wav2Vec2PreTrainedModel; +var __webpack_exports__Wav2Vec2ProcessorWithLM = __webpack_exports__.Wav2Vec2ProcessorWithLM; +var __webpack_exports__WavLMForAudioFrameClassification = __webpack_exports__.WavLMForAudioFrameClassification; +var __webpack_exports__WavLMForCTC = __webpack_exports__.WavLMForCTC; +var __webpack_exports__WavLMForSequenceClassification = __webpack_exports__.WavLMForSequenceClassification; +var __webpack_exports__WavLMForXVector = __webpack_exports__.WavLMForXVector; +var __webpack_exports__WavLMModel = __webpack_exports__.WavLMModel; +var __webpack_exports__WavLMPreTrainedModel = __webpack_exports__.WavLMPreTrainedModel; +var __webpack_exports__WeSpeakerFeatureExtractor = __webpack_exports__.WeSpeakerFeatureExtractor; +var __webpack_exports__WeSpeakerResNetModel = __webpack_exports__.WeSpeakerResNetModel; +var __webpack_exports__WeSpeakerResNetPreTrainedModel = __webpack_exports__.WeSpeakerResNetPreTrainedModel; +var __webpack_exports__WhisperFeatureExtractor = __webpack_exports__.WhisperFeatureExtractor; +var __webpack_exports__WhisperForConditionalGeneration = __webpack_exports__.WhisperForConditionalGeneration; +var __webpack_exports__WhisperModel = __webpack_exports__.WhisperModel; +var __webpack_exports__WhisperPreTrainedModel = __webpack_exports__.WhisperPreTrainedModel; +var __webpack_exports__WhisperProcessor = __webpack_exports__.WhisperProcessor; +var __webpack_exports__WhisperTextStreamer = __webpack_exports__.WhisperTextStreamer; +var __webpack_exports__WhisperTokenizer = __webpack_exports__.WhisperTokenizer; +var __webpack_exports__XLMForQuestionAnswering = __webpack_exports__.XLMForQuestionAnswering; +var __webpack_exports__XLMForSequenceClassification = __webpack_exports__.XLMForSequenceClassification; +var __webpack_exports__XLMForTokenClassification = __webpack_exports__.XLMForTokenClassification; +var __webpack_exports__XLMModel = __webpack_exports__.XLMModel; +var __webpack_exports__XLMPreTrainedModel = __webpack_exports__.XLMPreTrainedModel; +var __webpack_exports__XLMRobertaForMaskedLM = __webpack_exports__.XLMRobertaForMaskedLM; +var __webpack_exports__XLMRobertaForQuestionAnswering = __webpack_exports__.XLMRobertaForQuestionAnswering; +var __webpack_exports__XLMRobertaForSequenceClassification = __webpack_exports__.XLMRobertaForSequenceClassification; +var __webpack_exports__XLMRobertaForTokenClassification = __webpack_exports__.XLMRobertaForTokenClassification; +var __webpack_exports__XLMRobertaModel = __webpack_exports__.XLMRobertaModel; +var __webpack_exports__XLMRobertaPreTrainedModel = __webpack_exports__.XLMRobertaPreTrainedModel; +var __webpack_exports__XLMRobertaTokenizer = __webpack_exports__.XLMRobertaTokenizer; +var __webpack_exports__XLMTokenizer = __webpack_exports__.XLMTokenizer; +var __webpack_exports__XLMWithLMHeadModel = __webpack_exports__.XLMWithLMHeadModel; +var __webpack_exports__XVectorOutput = __webpack_exports__.XVectorOutput; +var __webpack_exports__YolosFeatureExtractor = __webpack_exports__.YolosFeatureExtractor; +var __webpack_exports__YolosForObjectDetection = __webpack_exports__.YolosForObjectDetection; +var __webpack_exports__YolosModel = __webpack_exports__.YolosModel; +var __webpack_exports__YolosObjectDetectionOutput = __webpack_exports__.YolosObjectDetectionOutput; +var __webpack_exports__YolosPreTrainedModel = __webpack_exports__.YolosPreTrainedModel; +var __webpack_exports__ZeroShotAudioClassificationPipeline = __webpack_exports__.ZeroShotAudioClassificationPipeline; +var __webpack_exports__ZeroShotClassificationPipeline = __webpack_exports__.ZeroShotClassificationPipeline; +var __webpack_exports__ZeroShotImageClassificationPipeline = __webpack_exports__.ZeroShotImageClassificationPipeline; +var __webpack_exports__ZeroShotObjectDetectionPipeline = __webpack_exports__.ZeroShotObjectDetectionPipeline; +var __webpack_exports__bankers_round = __webpack_exports__.bankers_round; +var __webpack_exports__cat = __webpack_exports__.cat; +var __webpack_exports__cos_sim = __webpack_exports__.cos_sim; +var __webpack_exports__dot = __webpack_exports__.dot; +var __webpack_exports__dynamic_time_warping = __webpack_exports__.dynamic_time_warping; +var __webpack_exports__env = __webpack_exports__.env; +var __webpack_exports__full = __webpack_exports__.full; +var __webpack_exports__full_like = __webpack_exports__.full_like; +var __webpack_exports__getKeyValueShapes = __webpack_exports__.getKeyValueShapes; +var __webpack_exports__hamming = __webpack_exports__.hamming; +var __webpack_exports__hanning = __webpack_exports__.hanning; +var __webpack_exports__interpolate = __webpack_exports__.interpolate; +var __webpack_exports__interpolate_4d = __webpack_exports__.interpolate_4d; +var __webpack_exports__interpolate_data = __webpack_exports__.interpolate_data; +var __webpack_exports__is_chinese_char = __webpack_exports__.is_chinese_char; +var __webpack_exports__layer_norm = __webpack_exports__.layer_norm; +var __webpack_exports__log_softmax = __webpack_exports__.log_softmax; +var __webpack_exports__magnitude = __webpack_exports__.magnitude; +var __webpack_exports__matmul = __webpack_exports__.matmul; +var __webpack_exports__max = __webpack_exports__.max; +var __webpack_exports__mean = __webpack_exports__.mean; +var __webpack_exports__mean_pooling = __webpack_exports__.mean_pooling; +var __webpack_exports__medianFilter = __webpack_exports__.medianFilter; +var __webpack_exports__mel_filter_bank = __webpack_exports__.mel_filter_bank; +var __webpack_exports__min = __webpack_exports__.min; +var __webpack_exports__ones = __webpack_exports__.ones; +var __webpack_exports__ones_like = __webpack_exports__.ones_like; +var __webpack_exports__permute = __webpack_exports__.permute; +var __webpack_exports__permute_data = __webpack_exports__.permute_data; +var __webpack_exports__pipeline = __webpack_exports__.pipeline; +var __webpack_exports__quantize_embeddings = __webpack_exports__.quantize_embeddings; +var __webpack_exports__read_audio = __webpack_exports__.read_audio; +var __webpack_exports__rfft = __webpack_exports__.rfft; +var __webpack_exports__round = __webpack_exports__.round; +var __webpack_exports__softmax = __webpack_exports__.softmax; +var __webpack_exports__spectrogram = __webpack_exports__.spectrogram; +var __webpack_exports__stack = __webpack_exports__.stack; +var __webpack_exports__std_mean = __webpack_exports__.std_mean; +var __webpack_exports__topk = __webpack_exports__.topk; +var __webpack_exports__window_function = __webpack_exports__.window_function; +var __webpack_exports__zeros = __webpack_exports__.zeros; +var __webpack_exports__zeros_like = __webpack_exports__.zeros_like; +export { __webpack_exports__ASTFeatureExtractor as ASTFeatureExtractor, __webpack_exports__ASTForAudioClassification as ASTForAudioClassification, __webpack_exports__ASTModel as ASTModel, __webpack_exports__ASTPreTrainedModel as ASTPreTrainedModel, __webpack_exports__AlbertForMaskedLM as AlbertForMaskedLM, __webpack_exports__AlbertForQuestionAnswering as AlbertForQuestionAnswering, __webpack_exports__AlbertForSequenceClassification as AlbertForSequenceClassification, __webpack_exports__AlbertModel as AlbertModel, __webpack_exports__AlbertPreTrainedModel as AlbertPreTrainedModel, __webpack_exports__AlbertTokenizer as AlbertTokenizer, __webpack_exports__AudioClassificationPipeline as AudioClassificationPipeline, __webpack_exports__AutoConfig as AutoConfig, __webpack_exports__AutoModel as AutoModel, __webpack_exports__AutoModelForAudioClassification as AutoModelForAudioClassification, __webpack_exports__AutoModelForAudioFrameClassification as AutoModelForAudioFrameClassification, __webpack_exports__AutoModelForCTC as AutoModelForCTC, __webpack_exports__AutoModelForCausalLM as AutoModelForCausalLM, __webpack_exports__AutoModelForDepthEstimation as AutoModelForDepthEstimation, __webpack_exports__AutoModelForDocumentQuestionAnswering as AutoModelForDocumentQuestionAnswering, __webpack_exports__AutoModelForImageClassification as AutoModelForImageClassification, __webpack_exports__AutoModelForImageFeatureExtraction as AutoModelForImageFeatureExtraction, __webpack_exports__AutoModelForImageMatting as AutoModelForImageMatting, __webpack_exports__AutoModelForImageSegmentation as AutoModelForImageSegmentation, __webpack_exports__AutoModelForImageToImage as AutoModelForImageToImage, __webpack_exports__AutoModelForMaskGeneration as AutoModelForMaskGeneration, __webpack_exports__AutoModelForMaskedLM as AutoModelForMaskedLM, __webpack_exports__AutoModelForNormalEstimation as AutoModelForNormalEstimation, __webpack_exports__AutoModelForObjectDetection as AutoModelForObjectDetection, __webpack_exports__AutoModelForQuestionAnswering as AutoModelForQuestionAnswering, __webpack_exports__AutoModelForSemanticSegmentation as AutoModelForSemanticSegmentation, __webpack_exports__AutoModelForSeq2SeqLM as AutoModelForSeq2SeqLM, __webpack_exports__AutoModelForSequenceClassification as AutoModelForSequenceClassification, __webpack_exports__AutoModelForSpeechSeq2Seq as AutoModelForSpeechSeq2Seq, __webpack_exports__AutoModelForTextToSpectrogram as AutoModelForTextToSpectrogram, __webpack_exports__AutoModelForTextToWaveform as AutoModelForTextToWaveform, __webpack_exports__AutoModelForTokenClassification as AutoModelForTokenClassification, __webpack_exports__AutoModelForUniversalSegmentation as AutoModelForUniversalSegmentation, __webpack_exports__AutoModelForVision2Seq as AutoModelForVision2Seq, __webpack_exports__AutoModelForXVector as AutoModelForXVector, __webpack_exports__AutoModelForZeroShotObjectDetection as AutoModelForZeroShotObjectDetection, __webpack_exports__AutoProcessor as AutoProcessor, __webpack_exports__AutoTokenizer as AutoTokenizer, __webpack_exports__AutomaticSpeechRecognitionPipeline as AutomaticSpeechRecognitionPipeline, __webpack_exports__BartForConditionalGeneration as BartForConditionalGeneration, __webpack_exports__BartForSequenceClassification as BartForSequenceClassification, __webpack_exports__BartModel as BartModel, __webpack_exports__BartPretrainedModel as BartPretrainedModel, __webpack_exports__BartTokenizer as BartTokenizer, __webpack_exports__BaseModelOutput as BaseModelOutput, __webpack_exports__BaseStreamer as BaseStreamer, __webpack_exports__BeitFeatureExtractor as BeitFeatureExtractor, __webpack_exports__BeitForImageClassification as BeitForImageClassification, __webpack_exports__BeitModel as BeitModel, __webpack_exports__BeitPreTrainedModel as BeitPreTrainedModel, __webpack_exports__BertForMaskedLM as BertForMaskedLM, __webpack_exports__BertForQuestionAnswering as BertForQuestionAnswering, __webpack_exports__BertForSequenceClassification as BertForSequenceClassification, __webpack_exports__BertForTokenClassification as BertForTokenClassification, __webpack_exports__BertModel as BertModel, __webpack_exports__BertPreTrainedModel as BertPreTrainedModel, __webpack_exports__BertTokenizer as BertTokenizer, __webpack_exports__BitImageProcessor as BitImageProcessor, __webpack_exports__BlenderbotForConditionalGeneration as BlenderbotForConditionalGeneration, __webpack_exports__BlenderbotModel as BlenderbotModel, __webpack_exports__BlenderbotPreTrainedModel as BlenderbotPreTrainedModel, __webpack_exports__BlenderbotSmallForConditionalGeneration as BlenderbotSmallForConditionalGeneration, __webpack_exports__BlenderbotSmallModel as BlenderbotSmallModel, __webpack_exports__BlenderbotSmallPreTrainedModel as BlenderbotSmallPreTrainedModel, __webpack_exports__BlenderbotSmallTokenizer as BlenderbotSmallTokenizer, __webpack_exports__BlenderbotTokenizer as BlenderbotTokenizer, __webpack_exports__BloomForCausalLM as BloomForCausalLM, __webpack_exports__BloomModel as BloomModel, __webpack_exports__BloomPreTrainedModel as BloomPreTrainedModel, __webpack_exports__BloomTokenizer as BloomTokenizer, __webpack_exports__CLIPFeatureExtractor as CLIPFeatureExtractor, __webpack_exports__CLIPImageProcessor as CLIPImageProcessor, __webpack_exports__CLIPModel as CLIPModel, __webpack_exports__CLIPPreTrainedModel as CLIPPreTrainedModel, __webpack_exports__CLIPSegForImageSegmentation as CLIPSegForImageSegmentation, __webpack_exports__CLIPSegModel as CLIPSegModel, __webpack_exports__CLIPSegPreTrainedModel as CLIPSegPreTrainedModel, __webpack_exports__CLIPTextModel as CLIPTextModel, __webpack_exports__CLIPTextModelWithProjection as CLIPTextModelWithProjection, __webpack_exports__CLIPTokenizer as CLIPTokenizer, __webpack_exports__CLIPVisionModel as CLIPVisionModel, __webpack_exports__CLIPVisionModelWithProjection as CLIPVisionModelWithProjection, __webpack_exports__CamembertForMaskedLM as CamembertForMaskedLM, __webpack_exports__CamembertForQuestionAnswering as CamembertForQuestionAnswering, __webpack_exports__CamembertForSequenceClassification as CamembertForSequenceClassification, __webpack_exports__CamembertForTokenClassification as CamembertForTokenClassification, __webpack_exports__CamembertModel as CamembertModel, __webpack_exports__CamembertPreTrainedModel as CamembertPreTrainedModel, __webpack_exports__CamembertTokenizer as CamembertTokenizer, __webpack_exports__CausalLMOutput as CausalLMOutput, __webpack_exports__CausalLMOutputWithPast as CausalLMOutputWithPast, __webpack_exports__ChineseCLIPFeatureExtractor as ChineseCLIPFeatureExtractor, __webpack_exports__ChineseCLIPModel as ChineseCLIPModel, __webpack_exports__ChineseCLIPPreTrainedModel as ChineseCLIPPreTrainedModel, __webpack_exports__ClapAudioModelWithProjection as ClapAudioModelWithProjection, __webpack_exports__ClapFeatureExtractor as ClapFeatureExtractor, __webpack_exports__ClapModel as ClapModel, __webpack_exports__ClapPreTrainedModel as ClapPreTrainedModel, __webpack_exports__ClapTextModelWithProjection as ClapTextModelWithProjection, __webpack_exports__CodeGenForCausalLM as CodeGenForCausalLM, __webpack_exports__CodeGenModel as CodeGenModel, __webpack_exports__CodeGenPreTrainedModel as CodeGenPreTrainedModel, __webpack_exports__CodeGenTokenizer as CodeGenTokenizer, __webpack_exports__CodeLlamaTokenizer as CodeLlamaTokenizer, __webpack_exports__CohereForCausalLM as CohereForCausalLM, __webpack_exports__CohereModel as CohereModel, __webpack_exports__CoherePreTrainedModel as CoherePreTrainedModel, __webpack_exports__CohereTokenizer as CohereTokenizer, __webpack_exports__ConvBertForMaskedLM as ConvBertForMaskedLM, __webpack_exports__ConvBertForQuestionAnswering as ConvBertForQuestionAnswering, __webpack_exports__ConvBertForSequenceClassification as ConvBertForSequenceClassification, __webpack_exports__ConvBertForTokenClassification as ConvBertForTokenClassification, __webpack_exports__ConvBertModel as ConvBertModel, __webpack_exports__ConvBertPreTrainedModel as ConvBertPreTrainedModel, __webpack_exports__ConvBertTokenizer as ConvBertTokenizer, __webpack_exports__ConvNextFeatureExtractor as ConvNextFeatureExtractor, __webpack_exports__ConvNextForImageClassification as ConvNextForImageClassification, __webpack_exports__ConvNextImageProcessor as ConvNextImageProcessor, __webpack_exports__ConvNextModel as ConvNextModel, __webpack_exports__ConvNextPreTrainedModel as ConvNextPreTrainedModel, __webpack_exports__ConvNextV2ForImageClassification as ConvNextV2ForImageClassification, __webpack_exports__ConvNextV2Model as ConvNextV2Model, __webpack_exports__ConvNextV2PreTrainedModel as ConvNextV2PreTrainedModel, __webpack_exports__DPTFeatureExtractor as DPTFeatureExtractor, __webpack_exports__DPTForDepthEstimation as DPTForDepthEstimation, __webpack_exports__DPTImageProcessor as DPTImageProcessor, __webpack_exports__DPTModel as DPTModel, __webpack_exports__DPTPreTrainedModel as DPTPreTrainedModel, __webpack_exports__DebertaForMaskedLM as DebertaForMaskedLM, __webpack_exports__DebertaForQuestionAnswering as DebertaForQuestionAnswering, __webpack_exports__DebertaForSequenceClassification as DebertaForSequenceClassification, __webpack_exports__DebertaForTokenClassification as DebertaForTokenClassification, __webpack_exports__DebertaModel as DebertaModel, __webpack_exports__DebertaPreTrainedModel as DebertaPreTrainedModel, __webpack_exports__DebertaTokenizer as DebertaTokenizer, __webpack_exports__DebertaV2ForMaskedLM as DebertaV2ForMaskedLM, __webpack_exports__DebertaV2ForQuestionAnswering as DebertaV2ForQuestionAnswering, __webpack_exports__DebertaV2ForSequenceClassification as DebertaV2ForSequenceClassification, __webpack_exports__DebertaV2ForTokenClassification as DebertaV2ForTokenClassification, __webpack_exports__DebertaV2Model as DebertaV2Model, __webpack_exports__DebertaV2PreTrainedModel as DebertaV2PreTrainedModel, __webpack_exports__DebertaV2Tokenizer as DebertaV2Tokenizer, __webpack_exports__DecisionTransformerModel as DecisionTransformerModel, __webpack_exports__DecisionTransformerPreTrainedModel as DecisionTransformerPreTrainedModel, __webpack_exports__DeiTFeatureExtractor as DeiTFeatureExtractor, __webpack_exports__DeiTForImageClassification as DeiTForImageClassification, __webpack_exports__DeiTModel as DeiTModel, __webpack_exports__DeiTPreTrainedModel as DeiTPreTrainedModel, __webpack_exports__DepthAnythingForDepthEstimation as DepthAnythingForDepthEstimation, __webpack_exports__DepthAnythingPreTrainedModel as DepthAnythingPreTrainedModel, __webpack_exports__DepthEstimationPipeline as DepthEstimationPipeline, __webpack_exports__DepthProForDepthEstimation as DepthProForDepthEstimation, __webpack_exports__DepthProPreTrainedModel as DepthProPreTrainedModel, __webpack_exports__DetrFeatureExtractor as DetrFeatureExtractor, __webpack_exports__DetrForObjectDetection as DetrForObjectDetection, __webpack_exports__DetrForSegmentation as DetrForSegmentation, __webpack_exports__DetrModel as DetrModel, __webpack_exports__DetrObjectDetectionOutput as DetrObjectDetectionOutput, __webpack_exports__DetrPreTrainedModel as DetrPreTrainedModel, __webpack_exports__DetrSegmentationOutput as DetrSegmentationOutput, __webpack_exports__Dinov2ForImageClassification as Dinov2ForImageClassification, __webpack_exports__Dinov2Model as Dinov2Model, __webpack_exports__Dinov2PreTrainedModel as Dinov2PreTrainedModel, __webpack_exports__DistilBertForMaskedLM as DistilBertForMaskedLM, __webpack_exports__DistilBertForQuestionAnswering as DistilBertForQuestionAnswering, __webpack_exports__DistilBertForSequenceClassification as DistilBertForSequenceClassification, __webpack_exports__DistilBertForTokenClassification as DistilBertForTokenClassification, __webpack_exports__DistilBertModel as DistilBertModel, __webpack_exports__DistilBertPreTrainedModel as DistilBertPreTrainedModel, __webpack_exports__DistilBertTokenizer as DistilBertTokenizer, __webpack_exports__DocumentQuestionAnsweringPipeline as DocumentQuestionAnsweringPipeline, __webpack_exports__DonutFeatureExtractor as DonutFeatureExtractor, __webpack_exports__DonutImageProcessor as DonutImageProcessor, __webpack_exports__DonutSwinModel as DonutSwinModel, __webpack_exports__DonutSwinPreTrainedModel as DonutSwinPreTrainedModel, __webpack_exports__EfficientNetForImageClassification as EfficientNetForImageClassification, __webpack_exports__EfficientNetImageProcessor as EfficientNetImageProcessor, __webpack_exports__EfficientNetModel as EfficientNetModel, __webpack_exports__EfficientNetPreTrainedModel as EfficientNetPreTrainedModel, __webpack_exports__ElectraForMaskedLM as ElectraForMaskedLM, __webpack_exports__ElectraForQuestionAnswering as ElectraForQuestionAnswering, __webpack_exports__ElectraForSequenceClassification as ElectraForSequenceClassification, __webpack_exports__ElectraForTokenClassification as ElectraForTokenClassification, __webpack_exports__ElectraModel as ElectraModel, __webpack_exports__ElectraPreTrainedModel as ElectraPreTrainedModel, __webpack_exports__ElectraTokenizer as ElectraTokenizer, __webpack_exports__EosTokenCriteria as EosTokenCriteria, __webpack_exports__EsmForMaskedLM as EsmForMaskedLM, __webpack_exports__EsmForSequenceClassification as EsmForSequenceClassification, __webpack_exports__EsmForTokenClassification as EsmForTokenClassification, __webpack_exports__EsmModel as EsmModel, __webpack_exports__EsmPreTrainedModel as EsmPreTrainedModel, __webpack_exports__EsmTokenizer as EsmTokenizer, __webpack_exports__FFT as FFT, __webpack_exports__FalconForCausalLM as FalconForCausalLM, __webpack_exports__FalconModel as FalconModel, __webpack_exports__FalconPreTrainedModel as FalconPreTrainedModel, __webpack_exports__FalconTokenizer as FalconTokenizer, __webpack_exports__FastViTForImageClassification as FastViTForImageClassification, __webpack_exports__FastViTModel as FastViTModel, __webpack_exports__FastViTPreTrainedModel as FastViTPreTrainedModel, __webpack_exports__FeatureExtractionPipeline as FeatureExtractionPipeline, __webpack_exports__FeatureExtractor as FeatureExtractor, __webpack_exports__FillMaskPipeline as FillMaskPipeline, __webpack_exports__Florence2ForConditionalGeneration as Florence2ForConditionalGeneration, __webpack_exports__Florence2PreTrainedModel as Florence2PreTrainedModel, __webpack_exports__Florence2Processor as Florence2Processor, __webpack_exports__GLPNFeatureExtractor as GLPNFeatureExtractor, __webpack_exports__GLPNForDepthEstimation as GLPNForDepthEstimation, __webpack_exports__GLPNModel as GLPNModel, __webpack_exports__GLPNPreTrainedModel as GLPNPreTrainedModel, __webpack_exports__GPT2LMHeadModel as GPT2LMHeadModel, __webpack_exports__GPT2Model as GPT2Model, __webpack_exports__GPT2PreTrainedModel as GPT2PreTrainedModel, __webpack_exports__GPT2Tokenizer as GPT2Tokenizer, __webpack_exports__GPTBigCodeForCausalLM as GPTBigCodeForCausalLM, __webpack_exports__GPTBigCodeModel as GPTBigCodeModel, __webpack_exports__GPTBigCodePreTrainedModel as GPTBigCodePreTrainedModel, __webpack_exports__GPTJForCausalLM as GPTJForCausalLM, __webpack_exports__GPTJModel as GPTJModel, __webpack_exports__GPTJPreTrainedModel as GPTJPreTrainedModel, __webpack_exports__GPTNeoForCausalLM as GPTNeoForCausalLM, __webpack_exports__GPTNeoModel as GPTNeoModel, __webpack_exports__GPTNeoPreTrainedModel as GPTNeoPreTrainedModel, __webpack_exports__GPTNeoXForCausalLM as GPTNeoXForCausalLM, __webpack_exports__GPTNeoXModel as GPTNeoXModel, __webpack_exports__GPTNeoXPreTrainedModel as GPTNeoXPreTrainedModel, __webpack_exports__GPTNeoXTokenizer as GPTNeoXTokenizer, __webpack_exports__Gemma2ForCausalLM as Gemma2ForCausalLM, __webpack_exports__Gemma2Model as Gemma2Model, __webpack_exports__Gemma2PreTrainedModel as Gemma2PreTrainedModel, __webpack_exports__GemmaForCausalLM as GemmaForCausalLM, __webpack_exports__GemmaModel as GemmaModel, __webpack_exports__GemmaPreTrainedModel as GemmaPreTrainedModel, __webpack_exports__GemmaTokenizer as GemmaTokenizer, __webpack_exports__GraniteForCausalLM as GraniteForCausalLM, __webpack_exports__GraniteModel as GraniteModel, __webpack_exports__GranitePreTrainedModel as GranitePreTrainedModel, __webpack_exports__Grok1Tokenizer as Grok1Tokenizer, __webpack_exports__GroupViTModel as GroupViTModel, __webpack_exports__GroupViTPreTrainedModel as GroupViTPreTrainedModel, __webpack_exports__HerbertTokenizer as HerbertTokenizer, __webpack_exports__HieraForImageClassification as HieraForImageClassification, __webpack_exports__HieraModel as HieraModel, __webpack_exports__HieraPreTrainedModel as HieraPreTrainedModel, __webpack_exports__HubertForCTC as HubertForCTC, __webpack_exports__HubertForSequenceClassification as HubertForSequenceClassification, __webpack_exports__HubertModel as HubertModel, __webpack_exports__HubertPreTrainedModel as HubertPreTrainedModel, __webpack_exports__ImageClassificationPipeline as ImageClassificationPipeline, __webpack_exports__ImageFeatureExtractionPipeline as ImageFeatureExtractionPipeline, __webpack_exports__ImageFeatureExtractor as ImageFeatureExtractor, __webpack_exports__ImageMattingOutput as ImageMattingOutput, __webpack_exports__ImageSegmentationPipeline as ImageSegmentationPipeline, __webpack_exports__ImageToImagePipeline as ImageToImagePipeline, __webpack_exports__ImageToTextPipeline as ImageToTextPipeline, __webpack_exports__InterruptableStoppingCriteria as InterruptableStoppingCriteria, __webpack_exports__JAISLMHeadModel as JAISLMHeadModel, __webpack_exports__JAISModel as JAISModel, __webpack_exports__JAISPreTrainedModel as JAISPreTrainedModel, __webpack_exports__LlamaForCausalLM as LlamaForCausalLM, __webpack_exports__LlamaModel as LlamaModel, __webpack_exports__LlamaPreTrainedModel as LlamaPreTrainedModel, __webpack_exports__LlamaTokenizer as LlamaTokenizer, __webpack_exports__LlavaForConditionalGeneration as LlavaForConditionalGeneration, __webpack_exports__LlavaPreTrainedModel as LlavaPreTrainedModel, __webpack_exports__LongT5ForConditionalGeneration as LongT5ForConditionalGeneration, __webpack_exports__LongT5Model as LongT5Model, __webpack_exports__LongT5PreTrainedModel as LongT5PreTrainedModel, __webpack_exports__M2M100ForConditionalGeneration as M2M100ForConditionalGeneration, __webpack_exports__M2M100Model as M2M100Model, __webpack_exports__M2M100PreTrainedModel as M2M100PreTrainedModel, __webpack_exports__M2M100Tokenizer as M2M100Tokenizer, __webpack_exports__MBart50Tokenizer as MBart50Tokenizer, __webpack_exports__MBartForCausalLM as MBartForCausalLM, __webpack_exports__MBartForConditionalGeneration as MBartForConditionalGeneration, __webpack_exports__MBartForSequenceClassification as MBartForSequenceClassification, __webpack_exports__MBartModel as MBartModel, __webpack_exports__MBartPreTrainedModel as MBartPreTrainedModel, __webpack_exports__MBartTokenizer as MBartTokenizer, __webpack_exports__MPNetForMaskedLM as MPNetForMaskedLM, __webpack_exports__MPNetForQuestionAnswering as MPNetForQuestionAnswering, __webpack_exports__MPNetForSequenceClassification as MPNetForSequenceClassification, __webpack_exports__MPNetForTokenClassification as MPNetForTokenClassification, __webpack_exports__MPNetModel as MPNetModel, __webpack_exports__MPNetPreTrainedModel as MPNetPreTrainedModel, __webpack_exports__MPNetTokenizer as MPNetTokenizer, __webpack_exports__MT5ForConditionalGeneration as MT5ForConditionalGeneration, __webpack_exports__MT5Model as MT5Model, __webpack_exports__MT5PreTrainedModel as MT5PreTrainedModel, __webpack_exports__MarianMTModel as MarianMTModel, __webpack_exports__MarianModel as MarianModel, __webpack_exports__MarianPreTrainedModel as MarianPreTrainedModel, __webpack_exports__MarianTokenizer as MarianTokenizer, __webpack_exports__MaskFormerFeatureExtractor as MaskFormerFeatureExtractor, __webpack_exports__MaskFormerForInstanceSegmentation as MaskFormerForInstanceSegmentation, __webpack_exports__MaskFormerModel as MaskFormerModel, __webpack_exports__MaskFormerPreTrainedModel as MaskFormerPreTrainedModel, __webpack_exports__MaskedLMOutput as MaskedLMOutput, __webpack_exports__MaxLengthCriteria as MaxLengthCriteria, __webpack_exports__MistralForCausalLM as MistralForCausalLM, __webpack_exports__MistralModel as MistralModel, __webpack_exports__MistralPreTrainedModel as MistralPreTrainedModel, __webpack_exports__MobileBertForMaskedLM as MobileBertForMaskedLM, __webpack_exports__MobileBertForQuestionAnswering as MobileBertForQuestionAnswering, __webpack_exports__MobileBertForSequenceClassification as MobileBertForSequenceClassification, __webpack_exports__MobileBertModel as MobileBertModel, __webpack_exports__MobileBertPreTrainedModel as MobileBertPreTrainedModel, __webpack_exports__MobileBertTokenizer as MobileBertTokenizer, __webpack_exports__MobileLLMForCausalLM as MobileLLMForCausalLM, __webpack_exports__MobileLLMModel as MobileLLMModel, __webpack_exports__MobileLLMPreTrainedModel as MobileLLMPreTrainedModel, __webpack_exports__MobileNetV1FeatureExtractor as MobileNetV1FeatureExtractor, __webpack_exports__MobileNetV1ForImageClassification as MobileNetV1ForImageClassification, __webpack_exports__MobileNetV1Model as MobileNetV1Model, __webpack_exports__MobileNetV1PreTrainedModel as MobileNetV1PreTrainedModel, __webpack_exports__MobileNetV2FeatureExtractor as MobileNetV2FeatureExtractor, __webpack_exports__MobileNetV2ForImageClassification as MobileNetV2ForImageClassification, __webpack_exports__MobileNetV2Model as MobileNetV2Model, __webpack_exports__MobileNetV2PreTrainedModel as MobileNetV2PreTrainedModel, __webpack_exports__MobileNetV3FeatureExtractor as MobileNetV3FeatureExtractor, __webpack_exports__MobileNetV3ForImageClassification as MobileNetV3ForImageClassification, __webpack_exports__MobileNetV3Model as MobileNetV3Model, __webpack_exports__MobileNetV3PreTrainedModel as MobileNetV3PreTrainedModel, __webpack_exports__MobileNetV4FeatureExtractor as MobileNetV4FeatureExtractor, __webpack_exports__MobileNetV4ForImageClassification as MobileNetV4ForImageClassification, __webpack_exports__MobileNetV4Model as MobileNetV4Model, __webpack_exports__MobileNetV4PreTrainedModel as MobileNetV4PreTrainedModel, __webpack_exports__MobileViTFeatureExtractor as MobileViTFeatureExtractor, __webpack_exports__MobileViTForImageClassification as MobileViTForImageClassification, __webpack_exports__MobileViTImageProcessor as MobileViTImageProcessor, __webpack_exports__MobileViTModel as MobileViTModel, __webpack_exports__MobileViTPreTrainedModel as MobileViTPreTrainedModel, __webpack_exports__MobileViTV2ForImageClassification as MobileViTV2ForImageClassification, __webpack_exports__MobileViTV2Model as MobileViTV2Model, __webpack_exports__MobileViTV2PreTrainedModel as MobileViTV2PreTrainedModel, __webpack_exports__ModelOutput as ModelOutput, __webpack_exports__Moondream1ForConditionalGeneration as Moondream1ForConditionalGeneration, __webpack_exports__MptForCausalLM as MptForCausalLM, __webpack_exports__MptModel as MptModel, __webpack_exports__MptPreTrainedModel as MptPreTrainedModel, __webpack_exports__MusicgenForCausalLM as MusicgenForCausalLM, __webpack_exports__MusicgenForConditionalGeneration as MusicgenForConditionalGeneration, __webpack_exports__MusicgenModel as MusicgenModel, __webpack_exports__MusicgenPreTrainedModel as MusicgenPreTrainedModel, __webpack_exports__NllbTokenizer as NllbTokenizer, __webpack_exports__NomicBertModel as NomicBertModel, __webpack_exports__NomicBertPreTrainedModel as NomicBertPreTrainedModel, __webpack_exports__NougatImageProcessor as NougatImageProcessor, __webpack_exports__NougatTokenizer as NougatTokenizer, __webpack_exports__OPTForCausalLM as OPTForCausalLM, __webpack_exports__OPTModel as OPTModel, __webpack_exports__OPTPreTrainedModel as OPTPreTrainedModel, __webpack_exports__ObjectDetectionPipeline as ObjectDetectionPipeline, __webpack_exports__OlmoForCausalLM as OlmoForCausalLM, __webpack_exports__OlmoModel as OlmoModel, __webpack_exports__OlmoPreTrainedModel as OlmoPreTrainedModel, __webpack_exports__OpenELMForCausalLM as OpenELMForCausalLM, __webpack_exports__OpenELMModel as OpenELMModel, __webpack_exports__OpenELMPreTrainedModel as OpenELMPreTrainedModel, __webpack_exports__OwlViTFeatureExtractor as OwlViTFeatureExtractor, __webpack_exports__OwlViTForObjectDetection as OwlViTForObjectDetection, __webpack_exports__OwlViTModel as OwlViTModel, __webpack_exports__OwlViTPreTrainedModel as OwlViTPreTrainedModel, __webpack_exports__OwlViTProcessor as OwlViTProcessor, __webpack_exports__Owlv2ForObjectDetection as Owlv2ForObjectDetection, __webpack_exports__Owlv2ImageProcessor as Owlv2ImageProcessor, __webpack_exports__Owlv2Model as Owlv2Model, __webpack_exports__Owlv2PreTrainedModel as Owlv2PreTrainedModel, __webpack_exports__Phi3ForCausalLM as Phi3ForCausalLM, __webpack_exports__Phi3Model as Phi3Model, __webpack_exports__Phi3PreTrainedModel as Phi3PreTrainedModel, __webpack_exports__PhiForCausalLM as PhiForCausalLM, __webpack_exports__PhiModel as PhiModel, __webpack_exports__PhiPreTrainedModel as PhiPreTrainedModel, __webpack_exports__Pipeline as Pipeline, __webpack_exports__PreTrainedModel as PreTrainedModel, __webpack_exports__PreTrainedTokenizer as PreTrainedTokenizer, __webpack_exports__PretrainedConfig as PretrainedConfig, __webpack_exports__PretrainedMixin as PretrainedMixin, __webpack_exports__Processor as Processor, __webpack_exports__PvtForImageClassification as PvtForImageClassification, __webpack_exports__PvtImageProcessor as PvtImageProcessor, __webpack_exports__PvtModel as PvtModel, __webpack_exports__PvtPreTrainedModel as PvtPreTrainedModel, __webpack_exports__PyAnnoteFeatureExtractor as PyAnnoteFeatureExtractor, __webpack_exports__PyAnnoteForAudioFrameClassification as PyAnnoteForAudioFrameClassification, __webpack_exports__PyAnnoteModel as PyAnnoteModel, __webpack_exports__PyAnnotePreTrainedModel as PyAnnotePreTrainedModel, __webpack_exports__PyAnnoteProcessor as PyAnnoteProcessor, __webpack_exports__QuestionAnsweringModelOutput as QuestionAnsweringModelOutput, __webpack_exports__QuestionAnsweringPipeline as QuestionAnsweringPipeline, __webpack_exports__Qwen2ForCausalLM as Qwen2ForCausalLM, __webpack_exports__Qwen2Model as Qwen2Model, __webpack_exports__Qwen2PreTrainedModel as Qwen2PreTrainedModel, __webpack_exports__Qwen2Tokenizer as Qwen2Tokenizer, __webpack_exports__RTDetrForObjectDetection as RTDetrForObjectDetection, __webpack_exports__RTDetrImageProcessor as RTDetrImageProcessor, __webpack_exports__RTDetrModel as RTDetrModel, __webpack_exports__RTDetrObjectDetectionOutput as RTDetrObjectDetectionOutput, __webpack_exports__RTDetrPreTrainedModel as RTDetrPreTrainedModel, __webpack_exports__RawImage as RawImage, __webpack_exports__ResNetForImageClassification as ResNetForImageClassification, __webpack_exports__ResNetModel as ResNetModel, __webpack_exports__ResNetPreTrainedModel as ResNetPreTrainedModel, __webpack_exports__RoFormerForMaskedLM as RoFormerForMaskedLM, __webpack_exports__RoFormerForQuestionAnswering as RoFormerForQuestionAnswering, __webpack_exports__RoFormerForSequenceClassification as RoFormerForSequenceClassification, __webpack_exports__RoFormerForTokenClassification as RoFormerForTokenClassification, __webpack_exports__RoFormerModel as RoFormerModel, __webpack_exports__RoFormerPreTrainedModel as RoFormerPreTrainedModel, __webpack_exports__RoFormerTokenizer as RoFormerTokenizer, __webpack_exports__RobertaForMaskedLM as RobertaForMaskedLM, __webpack_exports__RobertaForQuestionAnswering as RobertaForQuestionAnswering, __webpack_exports__RobertaForSequenceClassification as RobertaForSequenceClassification, __webpack_exports__RobertaForTokenClassification as RobertaForTokenClassification, __webpack_exports__RobertaModel as RobertaModel, __webpack_exports__RobertaPreTrainedModel as RobertaPreTrainedModel, __webpack_exports__RobertaTokenizer as RobertaTokenizer, __webpack_exports__SamImageProcessor as SamImageProcessor, __webpack_exports__SamImageSegmentationOutput as SamImageSegmentationOutput, __webpack_exports__SamModel as SamModel, __webpack_exports__SamPreTrainedModel as SamPreTrainedModel, __webpack_exports__SamProcessor as SamProcessor, __webpack_exports__SapiensFeatureExtractor as SapiensFeatureExtractor, __webpack_exports__SapiensForDepthEstimation as SapiensForDepthEstimation, __webpack_exports__SapiensForNormalEstimation as SapiensForNormalEstimation, __webpack_exports__SapiensForSemanticSegmentation as SapiensForSemanticSegmentation, __webpack_exports__SapiensPreTrainedModel as SapiensPreTrainedModel, __webpack_exports__SeamlessM4TFeatureExtractor as SeamlessM4TFeatureExtractor, __webpack_exports__SegformerFeatureExtractor as SegformerFeatureExtractor, __webpack_exports__SegformerForImageClassification as SegformerForImageClassification, __webpack_exports__SegformerForSemanticSegmentation as SegformerForSemanticSegmentation, __webpack_exports__SegformerModel as SegformerModel, __webpack_exports__SegformerPreTrainedModel as SegformerPreTrainedModel, __webpack_exports__Seq2SeqLMOutput as Seq2SeqLMOutput, __webpack_exports__SequenceClassifierOutput as SequenceClassifierOutput, __webpack_exports__SiglipImageProcessor as SiglipImageProcessor, __webpack_exports__SiglipModel as SiglipModel, __webpack_exports__SiglipPreTrainedModel as SiglipPreTrainedModel, __webpack_exports__SiglipTextModel as SiglipTextModel, __webpack_exports__SiglipTokenizer as SiglipTokenizer, __webpack_exports__SiglipVisionModel as SiglipVisionModel, __webpack_exports__SpeechT5FeatureExtractor as SpeechT5FeatureExtractor, __webpack_exports__SpeechT5ForSpeechToText as SpeechT5ForSpeechToText, __webpack_exports__SpeechT5ForTextToSpeech as SpeechT5ForTextToSpeech, __webpack_exports__SpeechT5HifiGan as SpeechT5HifiGan, __webpack_exports__SpeechT5Model as SpeechT5Model, __webpack_exports__SpeechT5PreTrainedModel as SpeechT5PreTrainedModel, __webpack_exports__SpeechT5Processor as SpeechT5Processor, __webpack_exports__SpeechT5Tokenizer as SpeechT5Tokenizer, __webpack_exports__SqueezeBertForMaskedLM as SqueezeBertForMaskedLM, __webpack_exports__SqueezeBertForQuestionAnswering as SqueezeBertForQuestionAnswering, __webpack_exports__SqueezeBertForSequenceClassification as SqueezeBertForSequenceClassification, __webpack_exports__SqueezeBertModel as SqueezeBertModel, __webpack_exports__SqueezeBertPreTrainedModel as SqueezeBertPreTrainedModel, __webpack_exports__SqueezeBertTokenizer as SqueezeBertTokenizer, __webpack_exports__StableLmForCausalLM as StableLmForCausalLM, __webpack_exports__StableLmModel as StableLmModel, __webpack_exports__StableLmPreTrainedModel as StableLmPreTrainedModel, __webpack_exports__Starcoder2ForCausalLM as Starcoder2ForCausalLM, __webpack_exports__Starcoder2Model as Starcoder2Model, __webpack_exports__Starcoder2PreTrainedModel as Starcoder2PreTrainedModel, __webpack_exports__StoppingCriteria as StoppingCriteria, __webpack_exports__StoppingCriteriaList as StoppingCriteriaList, __webpack_exports__SummarizationPipeline as SummarizationPipeline, __webpack_exports__Swin2SRForImageSuperResolution as Swin2SRForImageSuperResolution, __webpack_exports__Swin2SRImageProcessor as Swin2SRImageProcessor, __webpack_exports__Swin2SRModel as Swin2SRModel, __webpack_exports__Swin2SRPreTrainedModel as Swin2SRPreTrainedModel, __webpack_exports__SwinForImageClassification as SwinForImageClassification, __webpack_exports__SwinModel as SwinModel, __webpack_exports__SwinPreTrainedModel as SwinPreTrainedModel, __webpack_exports__T5ForConditionalGeneration as T5ForConditionalGeneration, __webpack_exports__T5Model as T5Model, __webpack_exports__T5PreTrainedModel as T5PreTrainedModel, __webpack_exports__T5Tokenizer as T5Tokenizer, __webpack_exports__TableTransformerForObjectDetection as TableTransformerForObjectDetection, __webpack_exports__TableTransformerModel as TableTransformerModel, __webpack_exports__TableTransformerObjectDetectionOutput as TableTransformerObjectDetectionOutput, __webpack_exports__TableTransformerPreTrainedModel as TableTransformerPreTrainedModel, __webpack_exports__Tensor as Tensor, __webpack_exports__Text2TextGenerationPipeline as Text2TextGenerationPipeline, __webpack_exports__TextClassificationPipeline as TextClassificationPipeline, __webpack_exports__TextGenerationPipeline as TextGenerationPipeline, __webpack_exports__TextStreamer as TextStreamer, __webpack_exports__TextToAudioPipeline as TextToAudioPipeline, __webpack_exports__TokenClassificationPipeline as TokenClassificationPipeline, __webpack_exports__TokenClassifierOutput as TokenClassifierOutput, __webpack_exports__TokenizerModel as TokenizerModel, __webpack_exports__TrOCRForCausalLM as TrOCRForCausalLM, __webpack_exports__TrOCRPreTrainedModel as TrOCRPreTrainedModel, __webpack_exports__TranslationPipeline as TranslationPipeline, __webpack_exports__UniSpeechForCTC as UniSpeechForCTC, __webpack_exports__UniSpeechForSequenceClassification as UniSpeechForSequenceClassification, __webpack_exports__UniSpeechModel as UniSpeechModel, __webpack_exports__UniSpeechPreTrainedModel as UniSpeechPreTrainedModel, __webpack_exports__UniSpeechSatForAudioFrameClassification as UniSpeechSatForAudioFrameClassification, __webpack_exports__UniSpeechSatForCTC as UniSpeechSatForCTC, __webpack_exports__UniSpeechSatForSequenceClassification as UniSpeechSatForSequenceClassification, __webpack_exports__UniSpeechSatModel as UniSpeechSatModel, __webpack_exports__UniSpeechSatPreTrainedModel as UniSpeechSatPreTrainedModel, __webpack_exports__ViTFeatureExtractor as ViTFeatureExtractor, __webpack_exports__ViTForImageClassification as ViTForImageClassification, __webpack_exports__ViTImageProcessor as ViTImageProcessor, __webpack_exports__ViTMAEModel as ViTMAEModel, __webpack_exports__ViTMAEPreTrainedModel as ViTMAEPreTrainedModel, __webpack_exports__ViTMSNForImageClassification as ViTMSNForImageClassification, __webpack_exports__ViTMSNModel as ViTMSNModel, __webpack_exports__ViTMSNPreTrainedModel as ViTMSNPreTrainedModel, __webpack_exports__ViTModel as ViTModel, __webpack_exports__ViTPreTrainedModel as ViTPreTrainedModel, __webpack_exports__VisionEncoderDecoderModel as VisionEncoderDecoderModel, __webpack_exports__VitMatteForImageMatting as VitMatteForImageMatting, __webpack_exports__VitMatteImageProcessor as VitMatteImageProcessor, __webpack_exports__VitMattePreTrainedModel as VitMattePreTrainedModel, __webpack_exports__VitsModel as VitsModel, __webpack_exports__VitsModelOutput as VitsModelOutput, __webpack_exports__VitsPreTrainedModel as VitsPreTrainedModel, __webpack_exports__VitsTokenizer as VitsTokenizer, __webpack_exports__Wav2Vec2BertForCTC as Wav2Vec2BertForCTC, __webpack_exports__Wav2Vec2BertForSequenceClassification as Wav2Vec2BertForSequenceClassification, __webpack_exports__Wav2Vec2BertModel as Wav2Vec2BertModel, __webpack_exports__Wav2Vec2BertPreTrainedModel as Wav2Vec2BertPreTrainedModel, __webpack_exports__Wav2Vec2CTCTokenizer as Wav2Vec2CTCTokenizer, __webpack_exports__Wav2Vec2FeatureExtractor as Wav2Vec2FeatureExtractor, __webpack_exports__Wav2Vec2ForAudioFrameClassification as Wav2Vec2ForAudioFrameClassification, __webpack_exports__Wav2Vec2ForCTC as Wav2Vec2ForCTC, __webpack_exports__Wav2Vec2ForSequenceClassification as Wav2Vec2ForSequenceClassification, __webpack_exports__Wav2Vec2Model as Wav2Vec2Model, __webpack_exports__Wav2Vec2PreTrainedModel as Wav2Vec2PreTrainedModel, __webpack_exports__Wav2Vec2ProcessorWithLM as Wav2Vec2ProcessorWithLM, __webpack_exports__WavLMForAudioFrameClassification as WavLMForAudioFrameClassification, __webpack_exports__WavLMForCTC as WavLMForCTC, __webpack_exports__WavLMForSequenceClassification as WavLMForSequenceClassification, __webpack_exports__WavLMForXVector as WavLMForXVector, __webpack_exports__WavLMModel as WavLMModel, __webpack_exports__WavLMPreTrainedModel as WavLMPreTrainedModel, __webpack_exports__WeSpeakerFeatureExtractor as WeSpeakerFeatureExtractor, __webpack_exports__WeSpeakerResNetModel as WeSpeakerResNetModel, __webpack_exports__WeSpeakerResNetPreTrainedModel as WeSpeakerResNetPreTrainedModel, __webpack_exports__WhisperFeatureExtractor as WhisperFeatureExtractor, __webpack_exports__WhisperForConditionalGeneration as WhisperForConditionalGeneration, __webpack_exports__WhisperModel as WhisperModel, __webpack_exports__WhisperPreTrainedModel as WhisperPreTrainedModel, __webpack_exports__WhisperProcessor as WhisperProcessor, __webpack_exports__WhisperTextStreamer as WhisperTextStreamer, __webpack_exports__WhisperTokenizer as WhisperTokenizer, __webpack_exports__XLMForQuestionAnswering as XLMForQuestionAnswering, __webpack_exports__XLMForSequenceClassification as XLMForSequenceClassification, __webpack_exports__XLMForTokenClassification as XLMForTokenClassification, __webpack_exports__XLMModel as XLMModel, __webpack_exports__XLMPreTrainedModel as XLMPreTrainedModel, __webpack_exports__XLMRobertaForMaskedLM as XLMRobertaForMaskedLM, __webpack_exports__XLMRobertaForQuestionAnswering as XLMRobertaForQuestionAnswering, __webpack_exports__XLMRobertaForSequenceClassification as XLMRobertaForSequenceClassification, __webpack_exports__XLMRobertaForTokenClassification as XLMRobertaForTokenClassification, __webpack_exports__XLMRobertaModel as XLMRobertaModel, __webpack_exports__XLMRobertaPreTrainedModel as XLMRobertaPreTrainedModel, __webpack_exports__XLMRobertaTokenizer as XLMRobertaTokenizer, __webpack_exports__XLMTokenizer as XLMTokenizer, __webpack_exports__XLMWithLMHeadModel as XLMWithLMHeadModel, __webpack_exports__XVectorOutput as XVectorOutput, __webpack_exports__YolosFeatureExtractor as YolosFeatureExtractor, __webpack_exports__YolosForObjectDetection as YolosForObjectDetection, __webpack_exports__YolosModel as YolosModel, __webpack_exports__YolosObjectDetectionOutput as YolosObjectDetectionOutput, __webpack_exports__YolosPreTrainedModel as YolosPreTrainedModel, __webpack_exports__ZeroShotAudioClassificationPipeline as ZeroShotAudioClassificationPipeline, __webpack_exports__ZeroShotClassificationPipeline as ZeroShotClassificationPipeline, __webpack_exports__ZeroShotImageClassificationPipeline as ZeroShotImageClassificationPipeline, __webpack_exports__ZeroShotObjectDetectionPipeline as ZeroShotObjectDetectionPipeline, __webpack_exports__bankers_round as bankers_round, __webpack_exports__cat as cat, __webpack_exports__cos_sim as cos_sim, __webpack_exports__dot as dot, __webpack_exports__dynamic_time_warping as dynamic_time_warping, __webpack_exports__env as env, __webpack_exports__full as full, __webpack_exports__full_like as full_like, __webpack_exports__getKeyValueShapes as getKeyValueShapes, __webpack_exports__hamming as hamming, __webpack_exports__hanning as hanning, __webpack_exports__interpolate as interpolate, __webpack_exports__interpolate_4d as interpolate_4d, __webpack_exports__interpolate_data as interpolate_data, __webpack_exports__is_chinese_char as is_chinese_char, __webpack_exports__layer_norm as layer_norm, __webpack_exports__log_softmax as log_softmax, __webpack_exports__magnitude as magnitude, __webpack_exports__matmul as matmul, __webpack_exports__max as max, __webpack_exports__mean as mean, __webpack_exports__mean_pooling as mean_pooling, __webpack_exports__medianFilter as medianFilter, __webpack_exports__mel_filter_bank as mel_filter_bank, __webpack_exports__min as min, __webpack_exports__ones as ones, __webpack_exports__ones_like as ones_like, __webpack_exports__permute as permute, __webpack_exports__permute_data as permute_data, __webpack_exports__pipeline as pipeline, __webpack_exports__quantize_embeddings as quantize_embeddings, __webpack_exports__read_audio as read_audio, __webpack_exports__rfft as rfft, __webpack_exports__round as round, __webpack_exports__softmax as softmax, __webpack_exports__spectrogram as spectrogram, __webpack_exports__stack as stack, __webpack_exports__std_mean as std_mean, __webpack_exports__topk as topk, __webpack_exports__window_function as window_function, __webpack_exports__zeros as zeros, __webpack_exports__zeros_like as zeros_like }; + +//# sourceMappingURL=transformers.js.map \ No newline at end of file diff --git a/_shared/voice/vendor/transformers/transformers.mjs b/_shared/voice/vendor/transformers/transformers.mjs new file mode 100644 index 0000000000000000000000000000000000000000..1005a7dc5c72e11d508abbb61bc4d48275f3deff --- /dev/null +++ b/_shared/voice/vendor/transformers/transformers.mjs @@ -0,0 +1,31404 @@ +import * as __WEBPACK_EXTERNAL_MODULE_fs__ from "fs"; +import * as __WEBPACK_EXTERNAL_MODULE_onnxruntime_node_6a60201e__ from "onnxruntime-node"; +import * as __WEBPACK_EXTERNAL_MODULE_path__ from "path"; +import * as __WEBPACK_EXTERNAL_MODULE_sharp__ from "sharp"; +import * as __WEBPACK_EXTERNAL_MODULE_url__ from "url"; +/******/ var __webpack_modules__ = ({ + +/***/ "fs": +/*!*********************!*\ + !*** external "fs" ***! + \*********************/ +/***/ ((module) => { + +module.exports = __WEBPACK_EXTERNAL_MODULE_fs__; + +/***/ }), + +/***/ "onnxruntime-node": +/*!***********************************!*\ + !*** external "onnxruntime-node" ***! + \***********************************/ +/***/ ((module) => { + +module.exports = __WEBPACK_EXTERNAL_MODULE_onnxruntime_node_6a60201e__; + +/***/ }), + +/***/ "path": +/*!***********************!*\ + !*** external "path" ***! + \***********************/ +/***/ ((module) => { + +module.exports = __WEBPACK_EXTERNAL_MODULE_path__; + +/***/ }), + +/***/ "sharp": +/*!************************!*\ + !*** external "sharp" ***! + \************************/ +/***/ ((module) => { + +module.exports = __WEBPACK_EXTERNAL_MODULE_sharp__; + +/***/ }), + +/***/ "url": +/*!**********************!*\ + !*** external "url" ***! + \**********************/ +/***/ ((module) => { + +module.exports = __WEBPACK_EXTERNAL_MODULE_url__; + +/***/ }), + +/***/ "?cb4d": +/*!*************************************!*\ + !*** #onnxruntime-webgpu (ignored) ***! + \*************************************/ +/***/ (() => { + +/* (ignored) */ + +/***/ }), + +/***/ "./node_modules/@huggingface/jinja/dist/index.js": +/*!*******************************************************!*\ + !*** ./node_modules/@huggingface/jinja/dist/index.js ***! + \*******************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ Environment: () => (/* binding */ Environment), +/* harmony export */ Interpreter: () => (/* binding */ Interpreter), +/* harmony export */ Template: () => (/* binding */ Template), +/* harmony export */ parse: () => (/* binding */ parse), +/* harmony export */ tokenize: () => (/* binding */ tokenize) +/* harmony export */ }); +// src/lexer.ts +var TOKEN_TYPES = Object.freeze({ + Text: "Text", + // The text between Jinja statements or expressions + NumericLiteral: "NumericLiteral", + // e.g., 123 + BooleanLiteral: "BooleanLiteral", + // true or false + StringLiteral: "StringLiteral", + // 'string' + Identifier: "Identifier", + // Variables, functions, etc. + Equals: "Equals", + // = + OpenParen: "OpenParen", + // ( + CloseParen: "CloseParen", + // ) + OpenStatement: "OpenStatement", + // {% + CloseStatement: "CloseStatement", + // %} + OpenExpression: "OpenExpression", + // {{ + CloseExpression: "CloseExpression", + // }} + OpenSquareBracket: "OpenSquareBracket", + // [ + CloseSquareBracket: "CloseSquareBracket", + // ] + OpenCurlyBracket: "OpenCurlyBracket", + // { + CloseCurlyBracket: "CloseCurlyBracket", + // } + Comma: "Comma", + // , + Dot: "Dot", + // . + Colon: "Colon", + // : + Pipe: "Pipe", + // | + CallOperator: "CallOperator", + // () + AdditiveBinaryOperator: "AdditiveBinaryOperator", + // + - + MultiplicativeBinaryOperator: "MultiplicativeBinaryOperator", + // * / % + ComparisonBinaryOperator: "ComparisonBinaryOperator", + // < > <= >= == != + UnaryOperator: "UnaryOperator", + // ! - + + // Keywords + Set: "Set", + If: "If", + For: "For", + In: "In", + Is: "Is", + NotIn: "NotIn", + Else: "Else", + EndIf: "EndIf", + ElseIf: "ElseIf", + EndFor: "EndFor", + And: "And", + Or: "Or", + Not: "UnaryOperator", + Macro: "Macro", + EndMacro: "EndMacro" +}); +var KEYWORDS = Object.freeze({ + set: TOKEN_TYPES.Set, + for: TOKEN_TYPES.For, + in: TOKEN_TYPES.In, + is: TOKEN_TYPES.Is, + if: TOKEN_TYPES.If, + else: TOKEN_TYPES.Else, + endif: TOKEN_TYPES.EndIf, + elif: TOKEN_TYPES.ElseIf, + endfor: TOKEN_TYPES.EndFor, + and: TOKEN_TYPES.And, + or: TOKEN_TYPES.Or, + not: TOKEN_TYPES.Not, + "not in": TOKEN_TYPES.NotIn, + macro: TOKEN_TYPES.Macro, + endmacro: TOKEN_TYPES.EndMacro, + // Literals + true: TOKEN_TYPES.BooleanLiteral, + false: TOKEN_TYPES.BooleanLiteral, + // NOTE: According to the Jinja docs: The special constants true, false, and none are indeed lowercase. + // Because that caused confusion in the past, (True used to expand to an undefined variable that was considered false), + // all three can now also be written in title case (True, False, and None). However, for consistency, (all Jinja identifiers are lowercase) + // you should use the lowercase versions. + True: TOKEN_TYPES.BooleanLiteral, + False: TOKEN_TYPES.BooleanLiteral +}); +var Token = class { + /** + * Constructs a new Token. + * @param {string} value The raw value as seen inside the source code. + * @param {TokenType} type The type of token. + */ + constructor(value, type) { + this.value = value; + this.type = type; + } +}; +function isWord(char) { + return /\w/.test(char); +} +function isInteger(char) { + return /[0-9]/.test(char); +} +var ORDERED_MAPPING_TABLE = [ + // Control sequences + ["{%", TOKEN_TYPES.OpenStatement], + ["%}", TOKEN_TYPES.CloseStatement], + ["{{", TOKEN_TYPES.OpenExpression], + ["}}", TOKEN_TYPES.CloseExpression], + // Single character tokens + ["(", TOKEN_TYPES.OpenParen], + [")", TOKEN_TYPES.CloseParen], + ["{", TOKEN_TYPES.OpenCurlyBracket], + ["}", TOKEN_TYPES.CloseCurlyBracket], + ["[", TOKEN_TYPES.OpenSquareBracket], + ["]", TOKEN_TYPES.CloseSquareBracket], + [",", TOKEN_TYPES.Comma], + [".", TOKEN_TYPES.Dot], + [":", TOKEN_TYPES.Colon], + ["|", TOKEN_TYPES.Pipe], + // Comparison operators + ["<=", TOKEN_TYPES.ComparisonBinaryOperator], + [">=", TOKEN_TYPES.ComparisonBinaryOperator], + ["==", TOKEN_TYPES.ComparisonBinaryOperator], + ["!=", TOKEN_TYPES.ComparisonBinaryOperator], + ["<", TOKEN_TYPES.ComparisonBinaryOperator], + [">", TOKEN_TYPES.ComparisonBinaryOperator], + // Arithmetic operators + ["+", TOKEN_TYPES.AdditiveBinaryOperator], + ["-", TOKEN_TYPES.AdditiveBinaryOperator], + ["*", TOKEN_TYPES.MultiplicativeBinaryOperator], + ["/", TOKEN_TYPES.MultiplicativeBinaryOperator], + ["%", TOKEN_TYPES.MultiplicativeBinaryOperator], + // Assignment operator + ["=", TOKEN_TYPES.Equals] +]; +var ESCAPE_CHARACTERS = /* @__PURE__ */ new Map([ + ["n", "\n"], + // New line + ["t", " "], + // Horizontal tab + ["r", "\r"], + // Carriage return + ["b", "\b"], + // Backspace + ["f", "\f"], + // Form feed + ["v", "\v"], + // Vertical tab + ["'", "'"], + // Single quote + ['"', '"'], + // Double quote + ["\\", "\\"] + // Backslash +]); +function preprocess(template, options = {}) { + if (template.endsWith("\n")) { + template = template.slice(0, -1); + } + template = template.replace(/{#.*?#}/gs, "{##}"); + if (options.lstrip_blocks) { + template = template.replace(/^[ \t]*({[#%])/gm, "$1"); + } + if (options.trim_blocks) { + template = template.replace(/([#%]})\n/g, "$1"); + } + return template.replace(/{##}/g, "").replace(/-%}\s*/g, "%}").replace(/\s*{%-/g, "{%").replace(/-}}\s*/g, "}}").replace(/\s*{{-/g, "{{"); +} +function tokenize(source, options = {}) { + const tokens = []; + const src = preprocess(source, options); + let cursorPosition = 0; + const consumeWhile = (predicate) => { + let str = ""; + while (predicate(src[cursorPosition])) { + if (src[cursorPosition] === "\\") { + ++cursorPosition; + if (cursorPosition >= src.length) + throw new SyntaxError("Unexpected end of input"); + const escaped = src[cursorPosition++]; + const unescaped = ESCAPE_CHARACTERS.get(escaped); + if (unescaped === void 0) { + throw new SyntaxError(`Unexpected escaped character: ${escaped}`); + } + str += unescaped; + continue; + } + str += src[cursorPosition++]; + if (cursorPosition >= src.length) + throw new SyntaxError("Unexpected end of input"); + } + return str; + }; + main: + while (cursorPosition < src.length) { + const lastTokenType = tokens.at(-1)?.type; + if (lastTokenType === void 0 || lastTokenType === TOKEN_TYPES.CloseStatement || lastTokenType === TOKEN_TYPES.CloseExpression) { + let text = ""; + while (cursorPosition < src.length && // Keep going until we hit the next Jinja statement or expression + !(src[cursorPosition] === "{" && (src[cursorPosition + 1] === "%" || src[cursorPosition + 1] === "{"))) { + text += src[cursorPosition++]; + } + if (text.length > 0) { + tokens.push(new Token(text, TOKEN_TYPES.Text)); + continue; + } + } + consumeWhile((char2) => /\s/.test(char2)); + const char = src[cursorPosition]; + if (char === "-" || char === "+") { + const lastTokenType2 = tokens.at(-1)?.type; + if (lastTokenType2 === TOKEN_TYPES.Text || lastTokenType2 === void 0) { + throw new SyntaxError(`Unexpected character: ${char}`); + } + switch (lastTokenType2) { + case TOKEN_TYPES.Identifier: + case TOKEN_TYPES.NumericLiteral: + case TOKEN_TYPES.BooleanLiteral: + case TOKEN_TYPES.StringLiteral: + case TOKEN_TYPES.CloseParen: + case TOKEN_TYPES.CloseSquareBracket: + break; + default: { + ++cursorPosition; + const num = consumeWhile(isInteger); + tokens.push( + new Token(`${char}${num}`, num.length > 0 ? TOKEN_TYPES.NumericLiteral : TOKEN_TYPES.UnaryOperator) + ); + continue; + } + } + } + for (const [char2, token] of ORDERED_MAPPING_TABLE) { + const slice2 = src.slice(cursorPosition, cursorPosition + char2.length); + if (slice2 === char2) { + tokens.push(new Token(char2, token)); + cursorPosition += char2.length; + continue main; + } + } + if (char === "'" || char === '"') { + ++cursorPosition; + const str = consumeWhile((c) => c !== char); + tokens.push(new Token(str, TOKEN_TYPES.StringLiteral)); + ++cursorPosition; + continue; + } + if (isInteger(char)) { + const num = consumeWhile(isInteger); + tokens.push(new Token(num, TOKEN_TYPES.NumericLiteral)); + continue; + } + if (isWord(char)) { + const word = consumeWhile(isWord); + const type = Object.hasOwn(KEYWORDS, word) ? KEYWORDS[word] : TOKEN_TYPES.Identifier; + if (type === TOKEN_TYPES.In && tokens.at(-1)?.type === TOKEN_TYPES.Not) { + tokens.pop(); + tokens.push(new Token("not in", TOKEN_TYPES.NotIn)); + } else { + tokens.push(new Token(word, type)); + } + continue; + } + throw new SyntaxError(`Unexpected character: ${char}`); + } + return tokens; +} + +// src/ast.ts +var Statement = class { + type = "Statement"; +}; +var Program = class extends Statement { + constructor(body) { + super(); + this.body = body; + } + type = "Program"; +}; +var If = class extends Statement { + constructor(test, body, alternate) { + super(); + this.test = test; + this.body = body; + this.alternate = alternate; + } + type = "If"; +}; +var For = class extends Statement { + constructor(loopvar, iterable, body, defaultBlock) { + super(); + this.loopvar = loopvar; + this.iterable = iterable; + this.body = body; + this.defaultBlock = defaultBlock; + } + type = "For"; +}; +var SetStatement = class extends Statement { + constructor(assignee, value) { + super(); + this.assignee = assignee; + this.value = value; + } + type = "Set"; +}; +var Macro = class extends Statement { + constructor(name, args, body) { + super(); + this.name = name; + this.args = args; + this.body = body; + } + type = "Macro"; +}; +var Expression = class extends Statement { + type = "Expression"; +}; +var MemberExpression = class extends Expression { + constructor(object, property, computed) { + super(); + this.object = object; + this.property = property; + this.computed = computed; + } + type = "MemberExpression"; +}; +var CallExpression = class extends Expression { + constructor(callee, args) { + super(); + this.callee = callee; + this.args = args; + } + type = "CallExpression"; +}; +var Identifier = class extends Expression { + /** + * @param {string} value The name of the identifier + */ + constructor(value) { + super(); + this.value = value; + } + type = "Identifier"; +}; +var Literal = class extends Expression { + constructor(value) { + super(); + this.value = value; + } + type = "Literal"; +}; +var NumericLiteral = class extends Literal { + type = "NumericLiteral"; +}; +var StringLiteral = class extends Literal { + type = "StringLiteral"; +}; +var BooleanLiteral = class extends Literal { + type = "BooleanLiteral"; +}; +var ArrayLiteral = class extends Literal { + type = "ArrayLiteral"; +}; +var TupleLiteral = class extends Literal { + type = "TupleLiteral"; +}; +var ObjectLiteral = class extends Literal { + type = "ObjectLiteral"; +}; +var BinaryExpression = class extends Expression { + constructor(operator, left, right) { + super(); + this.operator = operator; + this.left = left; + this.right = right; + } + type = "BinaryExpression"; +}; +var FilterExpression = class extends Expression { + constructor(operand, filter) { + super(); + this.operand = operand; + this.filter = filter; + } + type = "FilterExpression"; +}; +var SelectExpression = class extends Expression { + constructor(iterable, test) { + super(); + this.iterable = iterable; + this.test = test; + } + type = "SelectExpression"; +}; +var TestExpression = class extends Expression { + constructor(operand, negate, test) { + super(); + this.operand = operand; + this.negate = negate; + this.test = test; + } + type = "TestExpression"; +}; +var UnaryExpression = class extends Expression { + constructor(operator, argument) { + super(); + this.operator = operator; + this.argument = argument; + } + type = "UnaryExpression"; +}; +var SliceExpression = class extends Expression { + constructor(start = void 0, stop = void 0, step = void 0) { + super(); + this.start = start; + this.stop = stop; + this.step = step; + } + type = "SliceExpression"; +}; +var KeywordArgumentExpression = class extends Expression { + constructor(key, value) { + super(); + this.key = key; + this.value = value; + } + type = "KeywordArgumentExpression"; +}; + +// src/parser.ts +function parse(tokens) { + const program = new Program([]); + let current = 0; + function expect(type, error) { + const prev = tokens[current++]; + if (!prev || prev.type !== type) { + throw new Error(`Parser Error: ${error}. ${prev.type} !== ${type}.`); + } + return prev; + } + function parseAny() { + switch (tokens[current].type) { + case TOKEN_TYPES.Text: + return parseText(); + case TOKEN_TYPES.OpenStatement: + return parseJinjaStatement(); + case TOKEN_TYPES.OpenExpression: + return parseJinjaExpression(); + default: + throw new SyntaxError(`Unexpected token type: ${tokens[current].type}`); + } + } + function not(...types) { + return current + types.length <= tokens.length && types.some((type, i) => type !== tokens[current + i].type); + } + function is(...types) { + return current + types.length <= tokens.length && types.every((type, i) => type === tokens[current + i].type); + } + function parseText() { + return new StringLiteral(expect(TOKEN_TYPES.Text, "Expected text token").value); + } + function parseJinjaStatement() { + expect(TOKEN_TYPES.OpenStatement, "Expected opening statement token"); + let result; + switch (tokens[current].type) { + case TOKEN_TYPES.Set: + ++current; + result = parseSetStatement(); + expect(TOKEN_TYPES.CloseStatement, "Expected closing statement token"); + break; + case TOKEN_TYPES.If: + ++current; + result = parseIfStatement(); + expect(TOKEN_TYPES.OpenStatement, "Expected {% token"); + expect(TOKEN_TYPES.EndIf, "Expected endif token"); + expect(TOKEN_TYPES.CloseStatement, "Expected %} token"); + break; + case TOKEN_TYPES.Macro: + ++current; + result = parseMacroStatement(); + expect(TOKEN_TYPES.OpenStatement, "Expected {% token"); + expect(TOKEN_TYPES.EndMacro, "Expected endmacro token"); + expect(TOKEN_TYPES.CloseStatement, "Expected %} token"); + break; + case TOKEN_TYPES.For: + ++current; + result = parseForStatement(); + expect(TOKEN_TYPES.OpenStatement, "Expected {% token"); + expect(TOKEN_TYPES.EndFor, "Expected endfor token"); + expect(TOKEN_TYPES.CloseStatement, "Expected %} token"); + break; + default: + throw new SyntaxError(`Unknown statement type: ${tokens[current].type}`); + } + return result; + } + function parseJinjaExpression() { + expect(TOKEN_TYPES.OpenExpression, "Expected opening expression token"); + const result = parseExpression(); + expect(TOKEN_TYPES.CloseExpression, "Expected closing expression token"); + return result; + } + function parseSetStatement() { + const left = parseExpression(); + if (is(TOKEN_TYPES.Equals)) { + ++current; + const value = parseSetStatement(); + return new SetStatement(left, value); + } + return left; + } + function parseIfStatement() { + const test = parseExpression(); + expect(TOKEN_TYPES.CloseStatement, "Expected closing statement token"); + const body = []; + const alternate = []; + while (!(tokens[current]?.type === TOKEN_TYPES.OpenStatement && (tokens[current + 1]?.type === TOKEN_TYPES.ElseIf || tokens[current + 1]?.type === TOKEN_TYPES.Else || tokens[current + 1]?.type === TOKEN_TYPES.EndIf))) { + body.push(parseAny()); + } + if (tokens[current]?.type === TOKEN_TYPES.OpenStatement && tokens[current + 1]?.type !== TOKEN_TYPES.EndIf) { + ++current; + if (is(TOKEN_TYPES.ElseIf)) { + expect(TOKEN_TYPES.ElseIf, "Expected elseif token"); + alternate.push(parseIfStatement()); + } else { + expect(TOKEN_TYPES.Else, "Expected else token"); + expect(TOKEN_TYPES.CloseStatement, "Expected closing statement token"); + while (!(tokens[current]?.type === TOKEN_TYPES.OpenStatement && tokens[current + 1]?.type === TOKEN_TYPES.EndIf)) { + alternate.push(parseAny()); + } + } + } + return new If(test, body, alternate); + } + function parseMacroStatement() { + const name = parsePrimaryExpression(); + if (name.type !== "Identifier") { + throw new SyntaxError(`Expected identifier following macro statement`); + } + const args = parseArgs(); + expect(TOKEN_TYPES.CloseStatement, "Expected closing statement token"); + const body = []; + while (not(TOKEN_TYPES.OpenStatement, TOKEN_TYPES.EndMacro)) { + body.push(parseAny()); + } + return new Macro(name, args, body); + } + function parseExpressionSequence(primary = false) { + const fn = primary ? parsePrimaryExpression : parseExpression; + const expressions = [fn()]; + const isTuple = is(TOKEN_TYPES.Comma); + while (isTuple) { + ++current; + expressions.push(fn()); + if (!is(TOKEN_TYPES.Comma)) { + break; + } + } + return isTuple ? new TupleLiteral(expressions) : expressions[0]; + } + function parseForStatement() { + const loopVariable = parseExpressionSequence(true); + if (!(loopVariable instanceof Identifier || loopVariable instanceof TupleLiteral)) { + throw new SyntaxError(`Expected identifier/tuple for the loop variable, got ${loopVariable.type} instead`); + } + expect(TOKEN_TYPES.In, "Expected `in` keyword following loop variable"); + const iterable = parseExpression(); + expect(TOKEN_TYPES.CloseStatement, "Expected closing statement token"); + const body = []; + while (not(TOKEN_TYPES.OpenStatement, TOKEN_TYPES.EndFor) && not(TOKEN_TYPES.OpenStatement, TOKEN_TYPES.Else)) { + body.push(parseAny()); + } + const alternative = []; + if (is(TOKEN_TYPES.OpenStatement, TOKEN_TYPES.Else)) { + ++current; + ++current; + expect(TOKEN_TYPES.CloseStatement, "Expected closing statement token"); + while (not(TOKEN_TYPES.OpenStatement, TOKEN_TYPES.EndFor)) { + alternative.push(parseAny()); + } + } + return new For(loopVariable, iterable, body, alternative); + } + function parseExpression() { + return parseIfExpression(); + } + function parseIfExpression() { + const a = parseLogicalOrExpression(); + if (is(TOKEN_TYPES.If)) { + ++current; + const predicate = parseLogicalOrExpression(); + if (is(TOKEN_TYPES.Else)) { + ++current; + const b = parseLogicalOrExpression(); + return new If(predicate, [a], [b]); + } else { + return new SelectExpression(a, predicate); + } + } + return a; + } + function parseLogicalOrExpression() { + let left = parseLogicalAndExpression(); + while (is(TOKEN_TYPES.Or)) { + const operator = tokens[current]; + ++current; + const right = parseLogicalAndExpression(); + left = new BinaryExpression(operator, left, right); + } + return left; + } + function parseLogicalAndExpression() { + let left = parseLogicalNegationExpression(); + while (is(TOKEN_TYPES.And)) { + const operator = tokens[current]; + ++current; + const right = parseLogicalNegationExpression(); + left = new BinaryExpression(operator, left, right); + } + return left; + } + function parseLogicalNegationExpression() { + let right; + while (is(TOKEN_TYPES.Not)) { + const operator = tokens[current]; + ++current; + const arg = parseLogicalNegationExpression(); + right = new UnaryExpression(operator, arg); + } + return right ?? parseComparisonExpression(); + } + function parseComparisonExpression() { + let left = parseAdditiveExpression(); + while (is(TOKEN_TYPES.ComparisonBinaryOperator) || is(TOKEN_TYPES.In) || is(TOKEN_TYPES.NotIn)) { + const operator = tokens[current]; + ++current; + const right = parseAdditiveExpression(); + left = new BinaryExpression(operator, left, right); + } + return left; + } + function parseAdditiveExpression() { + let left = parseMultiplicativeExpression(); + while (is(TOKEN_TYPES.AdditiveBinaryOperator)) { + const operator = tokens[current]; + ++current; + const right = parseMultiplicativeExpression(); + left = new BinaryExpression(operator, left, right); + } + return left; + } + function parseCallMemberExpression() { + const member = parseMemberExpression(); + if (is(TOKEN_TYPES.OpenParen)) { + return parseCallExpression(member); + } + return member; + } + function parseCallExpression(callee) { + let callExpression = new CallExpression(callee, parseArgs()); + if (is(TOKEN_TYPES.OpenParen)) { + callExpression = parseCallExpression(callExpression); + } + return callExpression; + } + function parseArgs() { + expect(TOKEN_TYPES.OpenParen, "Expected opening parenthesis for arguments list"); + const args = parseArgumentsList(); + expect(TOKEN_TYPES.CloseParen, "Expected closing parenthesis for arguments list"); + return args; + } + function parseArgumentsList() { + const args = []; + while (!is(TOKEN_TYPES.CloseParen)) { + let argument = parseExpression(); + if (is(TOKEN_TYPES.Equals)) { + ++current; + if (!(argument instanceof Identifier)) { + throw new SyntaxError(`Expected identifier for keyword argument`); + } + const value = parseExpression(); + argument = new KeywordArgumentExpression(argument, value); + } + args.push(argument); + if (is(TOKEN_TYPES.Comma)) { + ++current; + } + } + return args; + } + function parseMemberExpressionArgumentsList() { + const slices = []; + let isSlice = false; + while (!is(TOKEN_TYPES.CloseSquareBracket)) { + if (is(TOKEN_TYPES.Colon)) { + slices.push(void 0); + ++current; + isSlice = true; + } else { + slices.push(parseExpression()); + if (is(TOKEN_TYPES.Colon)) { + ++current; + isSlice = true; + } + } + } + if (slices.length === 0) { + throw new SyntaxError(`Expected at least one argument for member/slice expression`); + } + if (isSlice) { + if (slices.length > 3) { + throw new SyntaxError(`Expected 0-3 arguments for slice expression`); + } + return new SliceExpression(...slices); + } + return slices[0]; + } + function parseMemberExpression() { + let object = parsePrimaryExpression(); + while (is(TOKEN_TYPES.Dot) || is(TOKEN_TYPES.OpenSquareBracket)) { + const operator = tokens[current]; + ++current; + let property; + const computed = operator.type !== TOKEN_TYPES.Dot; + if (computed) { + property = parseMemberExpressionArgumentsList(); + expect(TOKEN_TYPES.CloseSquareBracket, "Expected closing square bracket"); + } else { + property = parsePrimaryExpression(); + if (property.type !== "Identifier") { + throw new SyntaxError(`Expected identifier following dot operator`); + } + } + object = new MemberExpression(object, property, computed); + } + return object; + } + function parseMultiplicativeExpression() { + let left = parseTestExpression(); + while (is(TOKEN_TYPES.MultiplicativeBinaryOperator)) { + const operator = tokens[current]; + ++current; + const right = parseTestExpression(); + left = new BinaryExpression(operator, left, right); + } + return left; + } + function parseTestExpression() { + let operand = parseFilterExpression(); + while (is(TOKEN_TYPES.Is)) { + ++current; + const negate = is(TOKEN_TYPES.Not); + if (negate) { + ++current; + } + let filter = parsePrimaryExpression(); + if (filter instanceof BooleanLiteral) { + filter = new Identifier(filter.value.toString()); + } + if (!(filter instanceof Identifier)) { + throw new SyntaxError(`Expected identifier for the test`); + } + operand = new TestExpression(operand, negate, filter); + } + return operand; + } + function parseFilterExpression() { + let operand = parseCallMemberExpression(); + while (is(TOKEN_TYPES.Pipe)) { + ++current; + let filter = parsePrimaryExpression(); + if (!(filter instanceof Identifier)) { + throw new SyntaxError(`Expected identifier for the filter`); + } + if (is(TOKEN_TYPES.OpenParen)) { + filter = parseCallExpression(filter); + } + operand = new FilterExpression(operand, filter); + } + return operand; + } + function parsePrimaryExpression() { + const token = tokens[current]; + switch (token.type) { + case TOKEN_TYPES.NumericLiteral: + ++current; + return new NumericLiteral(Number(token.value)); + case TOKEN_TYPES.StringLiteral: + ++current; + return new StringLiteral(token.value); + case TOKEN_TYPES.BooleanLiteral: + ++current; + return new BooleanLiteral(token.value.toLowerCase() === "true"); + case TOKEN_TYPES.Identifier: + ++current; + return new Identifier(token.value); + case TOKEN_TYPES.OpenParen: { + ++current; + const expression = parseExpressionSequence(); + if (tokens[current].type !== TOKEN_TYPES.CloseParen) { + throw new SyntaxError(`Expected closing parenthesis, got ${tokens[current].type} instead`); + } + ++current; + return expression; + } + case TOKEN_TYPES.OpenSquareBracket: { + ++current; + const values = []; + while (!is(TOKEN_TYPES.CloseSquareBracket)) { + values.push(parseExpression()); + if (is(TOKEN_TYPES.Comma)) { + ++current; + } + } + ++current; + return new ArrayLiteral(values); + } + case TOKEN_TYPES.OpenCurlyBracket: { + ++current; + const values = /* @__PURE__ */ new Map(); + while (!is(TOKEN_TYPES.CloseCurlyBracket)) { + const key = parseExpression(); + expect(TOKEN_TYPES.Colon, "Expected colon between key and value in object literal"); + const value = parseExpression(); + values.set(key, value); + if (is(TOKEN_TYPES.Comma)) { + ++current; + } + } + ++current; + return new ObjectLiteral(values); + } + default: + throw new SyntaxError(`Unexpected token: ${token.type}`); + } + } + while (current < tokens.length) { + program.body.push(parseAny()); + } + return program; +} + +// src/utils.ts +function range(start, stop, step = 1) { + if (stop === void 0) { + stop = start; + start = 0; + } + const result = []; + for (let i = start; i < stop; i += step) { + result.push(i); + } + return result; +} +function slice(array, start, stop, step = 1) { + const direction = Math.sign(step); + if (direction >= 0) { + start = (start ??= 0) < 0 ? Math.max(array.length + start, 0) : Math.min(start, array.length); + stop = (stop ??= array.length) < 0 ? Math.max(array.length + stop, 0) : Math.min(stop, array.length); + } else { + start = (start ??= array.length - 1) < 0 ? Math.max(array.length + start, -1) : Math.min(start, array.length - 1); + stop = (stop ??= -1) < -1 ? Math.max(array.length + stop, -1) : Math.min(stop, array.length - 1); + } + const result = []; + for (let i = start; direction * i < direction * stop; i += step) { + result.push(array[i]); + } + return result; +} +function titleCase(value) { + return value.replace(/\b\w/g, (c) => c.toUpperCase()); +} + +// src/runtime.ts +var RuntimeValue = class { + type = "RuntimeValue"; + value; + /** + * A collection of built-in functions for this type. + */ + builtins = /* @__PURE__ */ new Map(); + /** + * Creates a new RuntimeValue. + */ + constructor(value = void 0) { + this.value = value; + } + /** + * Determines truthiness or falsiness of the runtime value. + * This function should be overridden by subclasses if it has custom truthiness criteria. + * @returns {BooleanValue} BooleanValue(true) if the value is truthy, BooleanValue(false) otherwise. + */ + __bool__() { + return new BooleanValue(!!this.value); + } +}; +var NumericValue = class extends RuntimeValue { + type = "NumericValue"; +}; +var StringValue = class extends RuntimeValue { + type = "StringValue"; + builtins = /* @__PURE__ */ new Map([ + [ + "upper", + new FunctionValue(() => { + return new StringValue(this.value.toUpperCase()); + }) + ], + [ + "lower", + new FunctionValue(() => { + return new StringValue(this.value.toLowerCase()); + }) + ], + [ + "strip", + new FunctionValue(() => { + return new StringValue(this.value.trim()); + }) + ], + [ + "title", + new FunctionValue(() => { + return new StringValue(titleCase(this.value)); + }) + ], + ["length", new NumericValue(this.value.length)] + ]); +}; +var BooleanValue = class extends RuntimeValue { + type = "BooleanValue"; +}; +var ObjectValue = class extends RuntimeValue { + type = "ObjectValue"; + /** + * NOTE: necessary to override since all JavaScript arrays are considered truthy, + * while only non-empty Python arrays are consider truthy. + * + * e.g., + * - JavaScript: {} && 5 -> 5 + * - Python: {} and 5 -> {} + */ + __bool__() { + return new BooleanValue(this.value.size > 0); + } + builtins = /* @__PURE__ */ new Map([ + [ + "get", + new FunctionValue(([key, defaultValue]) => { + if (!(key instanceof StringValue)) { + throw new Error(`Object key must be a string: got ${key.type}`); + } + return this.value.get(key.value) ?? defaultValue ?? new NullValue(); + }) + ], + [ + "items", + new FunctionValue(() => { + return new ArrayValue( + Array.from(this.value.entries()).map(([key, value]) => new ArrayValue([new StringValue(key), value])) + ); + }) + ] + ]); +}; +var KeywordArgumentsValue = class extends ObjectValue { + type = "KeywordArgumentsValue"; +}; +var ArrayValue = class extends RuntimeValue { + type = "ArrayValue"; + builtins = /* @__PURE__ */ new Map([["length", new NumericValue(this.value.length)]]); + /** + * NOTE: necessary to override since all JavaScript arrays are considered truthy, + * while only non-empty Python arrays are consider truthy. + * + * e.g., + * - JavaScript: [] && 5 -> 5 + * - Python: [] and 5 -> [] + */ + __bool__() { + return new BooleanValue(this.value.length > 0); + } +}; +var TupleValue = class extends ArrayValue { + type = "TupleValue"; +}; +var FunctionValue = class extends RuntimeValue { + type = "FunctionValue"; +}; +var NullValue = class extends RuntimeValue { + type = "NullValue"; +}; +var UndefinedValue = class extends RuntimeValue { + type = "UndefinedValue"; +}; +var Environment = class { + constructor(parent) { + this.parent = parent; + } + /** + * The variables declared in this environment. + */ + variables = /* @__PURE__ */ new Map([ + [ + "namespace", + new FunctionValue((args) => { + if (args.length === 0) { + return new ObjectValue(/* @__PURE__ */ new Map()); + } + if (args.length !== 1 || !(args[0] instanceof ObjectValue)) { + throw new Error("`namespace` expects either zero arguments or a single object argument"); + } + return args[0]; + }) + ] + ]); + /** + * The tests available in this environment. + */ + tests = /* @__PURE__ */ new Map([ + ["boolean", (operand) => operand.type === "BooleanValue"], + ["callable", (operand) => operand instanceof FunctionValue], + [ + "odd", + (operand) => { + if (operand.type !== "NumericValue") { + throw new Error(`Cannot apply test "odd" to type: ${operand.type}`); + } + return operand.value % 2 !== 0; + } + ], + [ + "even", + (operand) => { + if (operand.type !== "NumericValue") { + throw new Error(`Cannot apply test "even" to type: ${operand.type}`); + } + return operand.value % 2 === 0; + } + ], + ["false", (operand) => operand.type === "BooleanValue" && !operand.value], + ["true", (operand) => operand.type === "BooleanValue" && operand.value], + ["string", (operand) => operand.type === "StringValue"], + ["number", (operand) => operand.type === "NumericValue"], + ["integer", (operand) => operand.type === "NumericValue" && Number.isInteger(operand.value)], + ["iterable", (operand) => operand instanceof ArrayValue || operand instanceof StringValue], + [ + "lower", + (operand) => { + const str = operand.value; + return operand.type === "StringValue" && str === str.toLowerCase(); + } + ], + [ + "upper", + (operand) => { + const str = operand.value; + return operand.type === "StringValue" && str === str.toUpperCase(); + } + ], + ["none", (operand) => operand.type === "NullValue"], + ["defined", (operand) => operand.type !== "UndefinedValue"], + ["undefined", (operand) => operand.type === "UndefinedValue"], + ["equalto", (a, b) => a.value === b.value], + ["eq", (a, b) => a.value === b.value] + ]); + /** + * Set the value of a variable in the current environment. + */ + set(name, value) { + return this.declareVariable(name, convertToRuntimeValues(value)); + } + declareVariable(name, value) { + if (this.variables.has(name)) { + throw new SyntaxError(`Variable already declared: ${name}`); + } + this.variables.set(name, value); + return value; + } + // private assignVariable(name: string, value: AnyRuntimeValue): AnyRuntimeValue { + // const env = this.resolve(name); + // env.variables.set(name, value); + // return value; + // } + /** + * Set variable in the current scope. + * See https://jinja.palletsprojects.com/en/3.0.x/templates/#assignments for more information. + */ + setVariable(name, value) { + this.variables.set(name, value); + return value; + } + /** + * Resolve the environment in which the variable is declared. + * @param {string} name The name of the variable. + * @returns {Environment} The environment in which the variable is declared. + */ + resolve(name) { + if (this.variables.has(name)) { + return this; + } + if (this.parent) { + return this.parent.resolve(name); + } + throw new Error(`Unknown variable: ${name}`); + } + lookupVariable(name) { + try { + return this.resolve(name).variables.get(name) ?? new UndefinedValue(); + } catch { + return new UndefinedValue(); + } + } +}; +var Interpreter = class { + global; + constructor(env) { + this.global = env ?? new Environment(); + } + /** + * Run the program. + */ + run(program) { + return this.evaluate(program, this.global); + } + /** + * Evaluates expressions following the binary operation type. + */ + evaluateBinaryExpression(node, environment) { + const left = this.evaluate(node.left, environment); + switch (node.operator.value) { + case "and": + return left.__bool__().value ? this.evaluate(node.right, environment) : left; + case "or": + return left.__bool__().value ? left : this.evaluate(node.right, environment); + } + const right = this.evaluate(node.right, environment); + switch (node.operator.value) { + case "==": + return new BooleanValue(left.value == right.value); + case "!=": + return new BooleanValue(left.value != right.value); + } + if (left instanceof UndefinedValue || right instanceof UndefinedValue) { + throw new Error("Cannot perform operation on undefined values"); + } else if (left instanceof NullValue || right instanceof NullValue) { + throw new Error("Cannot perform operation on null values"); + } else if (left instanceof NumericValue && right instanceof NumericValue) { + switch (node.operator.value) { + case "+": + return new NumericValue(left.value + right.value); + case "-": + return new NumericValue(left.value - right.value); + case "*": + return new NumericValue(left.value * right.value); + case "/": + return new NumericValue(left.value / right.value); + case "%": + return new NumericValue(left.value % right.value); + case "<": + return new BooleanValue(left.value < right.value); + case ">": + return new BooleanValue(left.value > right.value); + case ">=": + return new BooleanValue(left.value >= right.value); + case "<=": + return new BooleanValue(left.value <= right.value); + } + } else if (left instanceof ArrayValue && right instanceof ArrayValue) { + switch (node.operator.value) { + case "+": + return new ArrayValue(left.value.concat(right.value)); + } + } else if (right instanceof ArrayValue) { + const member = right.value.find((x) => x.value === left.value) !== void 0; + switch (node.operator.value) { + case "in": + return new BooleanValue(member); + case "not in": + return new BooleanValue(!member); + } + } + if (left instanceof StringValue || right instanceof StringValue) { + switch (node.operator.value) { + case "+": + return new StringValue(left.value.toString() + right.value.toString()); + } + } + if (left instanceof StringValue && right instanceof StringValue) { + switch (node.operator.value) { + case "in": + return new BooleanValue(right.value.includes(left.value)); + case "not in": + return new BooleanValue(!right.value.includes(left.value)); + } + } + if (left instanceof StringValue && right instanceof ObjectValue) { + switch (node.operator.value) { + case "in": + return new BooleanValue(right.value.has(left.value)); + case "not in": + return new BooleanValue(!right.value.has(left.value)); + } + } + throw new SyntaxError(`Unknown operator "${node.operator.value}" between ${left.type} and ${right.type}`); + } + evaluateArguments(args, environment) { + const positionalArguments = []; + const keywordArguments = /* @__PURE__ */ new Map(); + for (const argument of args) { + if (argument.type === "KeywordArgumentExpression") { + const kwarg = argument; + keywordArguments.set(kwarg.key.value, this.evaluate(kwarg.value, environment)); + } else { + if (keywordArguments.size > 0) { + throw new Error("Positional arguments must come before keyword arguments"); + } + positionalArguments.push(this.evaluate(argument, environment)); + } + } + return [positionalArguments, keywordArguments]; + } + /** + * Evaluates expressions following the filter operation type. + */ + evaluateFilterExpression(node, environment) { + const operand = this.evaluate(node.operand, environment); + if (node.filter.type === "Identifier") { + const filter = node.filter; + if (filter.value === "tojson") { + return new StringValue(toJSON(operand)); + } + if (operand instanceof ArrayValue) { + switch (filter.value) { + case "list": + return operand; + case "first": + return operand.value[0]; + case "last": + return operand.value[operand.value.length - 1]; + case "length": + return new NumericValue(operand.value.length); + case "reverse": + return new ArrayValue(operand.value.reverse()); + case "sort": + return new ArrayValue( + operand.value.sort((a, b) => { + if (a.type !== b.type) { + throw new Error(`Cannot compare different types: ${a.type} and ${b.type}`); + } + switch (a.type) { + case "NumericValue": + return a.value - b.value; + case "StringValue": + return a.value.localeCompare(b.value); + default: + throw new Error(`Cannot compare type: ${a.type}`); + } + }) + ); + default: + throw new Error(`Unknown ArrayValue filter: ${filter.value}`); + } + } else if (operand instanceof StringValue) { + switch (filter.value) { + case "length": + return new NumericValue(operand.value.length); + case "upper": + return new StringValue(operand.value.toUpperCase()); + case "lower": + return new StringValue(operand.value.toLowerCase()); + case "title": + return new StringValue(titleCase(operand.value)); + case "capitalize": + return new StringValue(operand.value.charAt(0).toUpperCase() + operand.value.slice(1)); + case "trim": + return new StringValue(operand.value.trim()); + case "indent": + return new StringValue( + operand.value.split("\n").map( + (x, i) => ( + // By default, don't indent the first line or empty lines + i === 0 || x.length === 0 ? x : " " + x + ) + ).join("\n") + ); + case "string": + return operand; + default: + throw new Error(`Unknown StringValue filter: ${filter.value}`); + } + } else if (operand instanceof NumericValue) { + switch (filter.value) { + case "abs": + return new NumericValue(Math.abs(operand.value)); + default: + throw new Error(`Unknown NumericValue filter: ${filter.value}`); + } + } else if (operand instanceof ObjectValue) { + switch (filter.value) { + case "items": + return new ArrayValue( + Array.from(operand.value.entries()).map(([key, value]) => new ArrayValue([new StringValue(key), value])) + ); + case "length": + return new NumericValue(operand.value.size); + default: + throw new Error(`Unknown ObjectValue filter: ${filter.value}`); + } + } + throw new Error(`Cannot apply filter "${filter.value}" to type: ${operand.type}`); + } else if (node.filter.type === "CallExpression") { + const filter = node.filter; + if (filter.callee.type !== "Identifier") { + throw new Error(`Unknown filter: ${filter.callee.type}`); + } + const filterName = filter.callee.value; + if (filterName === "tojson") { + const [, kwargs] = this.evaluateArguments(filter.args, environment); + const indent = kwargs.get("indent") ?? new NullValue(); + if (!(indent instanceof NumericValue || indent instanceof NullValue)) { + throw new Error("If set, indent must be a number"); + } + return new StringValue(toJSON(operand, indent.value)); + } + if (operand instanceof ArrayValue) { + switch (filterName) { + case "selectattr": { + if (operand.value.some((x) => !(x instanceof ObjectValue))) { + throw new Error("`selectattr` can only be applied to array of objects"); + } + if (filter.args.some((x) => x.type !== "StringLiteral")) { + throw new Error("arguments of `selectattr` must be strings"); + } + const [attr, testName, value] = filter.args.map((x) => this.evaluate(x, environment)); + let testFunction; + if (testName) { + const test = environment.tests.get(testName.value); + if (!test) { + throw new Error(`Unknown test: ${testName.value}`); + } + testFunction = test; + } else { + testFunction = (...x) => x[0].__bool__().value; + } + const filtered = operand.value.filter((item) => { + const a = item.value.get(attr.value); + if (a) { + return testFunction(a, value); + } + return false; + }); + return new ArrayValue(filtered); + } + case "map": { + const [, kwargs] = this.evaluateArguments(filter.args, environment); + if (kwargs.has("attribute")) { + const attr = kwargs.get("attribute"); + if (!(attr instanceof StringValue)) { + throw new Error("attribute must be a string"); + } + const defaultValue = kwargs.get("default"); + const mapped = operand.value.map((item) => { + if (!(item instanceof ObjectValue)) { + throw new Error("items in map must be an object"); + } + return item.value.get(attr.value) ?? defaultValue ?? new UndefinedValue(); + }); + return new ArrayValue(mapped); + } else { + throw new Error("`map` expressions without `attribute` set are not currently supported."); + } + } + } + throw new Error(`Unknown ArrayValue filter: ${filterName}`); + } else if (operand instanceof StringValue) { + switch (filterName) { + case "indent": { + const [args, kwargs] = this.evaluateArguments(filter.args, environment); + const width = args.at(0) ?? kwargs.get("width") ?? new NumericValue(4); + if (!(width instanceof NumericValue)) { + throw new Error("width must be a number"); + } + const first = args.at(1) ?? kwargs.get("first") ?? new BooleanValue(false); + const blank = args.at(2) ?? kwargs.get("blank") ?? new BooleanValue(false); + const lines = operand.value.split("\n"); + const indent = " ".repeat(width.value); + const indented = lines.map( + (x, i) => !first.value && i === 0 || !blank.value && x.length === 0 ? x : indent + x + ); + return new StringValue(indented.join("\n")); + } + } + throw new Error(`Unknown StringValue filter: ${filterName}`); + } else { + throw new Error(`Cannot apply filter "${filterName}" to type: ${operand.type}`); + } + } + throw new Error(`Unknown filter: ${node.filter.type}`); + } + /** + * Evaluates expressions following the test operation type. + */ + evaluateTestExpression(node, environment) { + const operand = this.evaluate(node.operand, environment); + const test = environment.tests.get(node.test.value); + if (!test) { + throw new Error(`Unknown test: ${node.test.value}`); + } + const result = test(operand); + return new BooleanValue(node.negate ? !result : result); + } + /** + * Evaluates expressions following the unary operation type. + */ + evaluateUnaryExpression(node, environment) { + const argument = this.evaluate(node.argument, environment); + switch (node.operator.value) { + case "not": + return new BooleanValue(!argument.value); + default: + throw new SyntaxError(`Unknown operator: ${node.operator.value}`); + } + } + evalProgram(program, environment) { + return this.evaluateBlock(program.body, environment); + } + evaluateBlock(statements, environment) { + let result = ""; + for (const statement of statements) { + const lastEvaluated = this.evaluate(statement, environment); + if (lastEvaluated.type !== "NullValue" && lastEvaluated.type !== "UndefinedValue") { + result += lastEvaluated.value; + } + } + return new StringValue(result); + } + evaluateIdentifier(node, environment) { + return environment.lookupVariable(node.value); + } + evaluateCallExpression(expr, environment) { + const [args, kwargs] = this.evaluateArguments(expr.args, environment); + if (kwargs.size > 0) { + args.push(new KeywordArgumentsValue(kwargs)); + } + const fn = this.evaluate(expr.callee, environment); + if (fn.type !== "FunctionValue") { + throw new Error(`Cannot call something that is not a function: got ${fn.type}`); + } + return fn.value(args, environment); + } + evaluateSliceExpression(object, expr, environment) { + if (!(object instanceof ArrayValue || object instanceof StringValue)) { + throw new Error("Slice object must be an array or string"); + } + const start = this.evaluate(expr.start, environment); + const stop = this.evaluate(expr.stop, environment); + const step = this.evaluate(expr.step, environment); + if (!(start instanceof NumericValue || start instanceof UndefinedValue)) { + throw new Error("Slice start must be numeric or undefined"); + } + if (!(stop instanceof NumericValue || stop instanceof UndefinedValue)) { + throw new Error("Slice stop must be numeric or undefined"); + } + if (!(step instanceof NumericValue || step instanceof UndefinedValue)) { + throw new Error("Slice step must be numeric or undefined"); + } + if (object instanceof ArrayValue) { + return new ArrayValue(slice(object.value, start.value, stop.value, step.value)); + } else { + return new StringValue(slice(Array.from(object.value), start.value, stop.value, step.value).join("")); + } + } + evaluateMemberExpression(expr, environment) { + const object = this.evaluate(expr.object, environment); + let property; + if (expr.computed) { + if (expr.property.type === "SliceExpression") { + return this.evaluateSliceExpression(object, expr.property, environment); + } else { + property = this.evaluate(expr.property, environment); + } + } else { + property = new StringValue(expr.property.value); + } + let value; + if (object instanceof ObjectValue) { + if (!(property instanceof StringValue)) { + throw new Error(`Cannot access property with non-string: got ${property.type}`); + } + value = object.value.get(property.value) ?? object.builtins.get(property.value); + } else if (object instanceof ArrayValue || object instanceof StringValue) { + if (property instanceof NumericValue) { + value = object.value.at(property.value); + if (object instanceof StringValue) { + value = new StringValue(object.value.at(property.value)); + } + } else if (property instanceof StringValue) { + value = object.builtins.get(property.value); + } else { + throw new Error(`Cannot access property with non-string/non-number: got ${property.type}`); + } + } else { + if (!(property instanceof StringValue)) { + throw new Error(`Cannot access property with non-string: got ${property.type}`); + } + value = object.builtins.get(property.value); + } + return value instanceof RuntimeValue ? value : new UndefinedValue(); + } + evaluateSet(node, environment) { + const rhs = this.evaluate(node.value, environment); + if (node.assignee.type === "Identifier") { + const variableName = node.assignee.value; + environment.setVariable(variableName, rhs); + } else if (node.assignee.type === "MemberExpression") { + const member = node.assignee; + const object = this.evaluate(member.object, environment); + if (!(object instanceof ObjectValue)) { + throw new Error("Cannot assign to member of non-object"); + } + if (member.property.type !== "Identifier") { + throw new Error("Cannot assign to member with non-identifier property"); + } + object.value.set(member.property.value, rhs); + } else { + throw new Error(`Invalid LHS inside assignment expression: ${JSON.stringify(node.assignee)}`); + } + return new NullValue(); + } + evaluateIf(node, environment) { + const test = this.evaluate(node.test, environment); + return this.evaluateBlock(test.__bool__().value ? node.body : node.alternate, environment); + } + evaluateFor(node, environment) { + const scope = new Environment(environment); + let test, iterable; + if (node.iterable.type === "SelectExpression") { + const select = node.iterable; + iterable = this.evaluate(select.iterable, scope); + test = select.test; + } else { + iterable = this.evaluate(node.iterable, scope); + } + if (!(iterable instanceof ArrayValue)) { + throw new Error(`Expected iterable type in for loop: got ${iterable.type}`); + } + const items = []; + const scopeUpdateFunctions = []; + for (let i = 0; i < iterable.value.length; ++i) { + const loopScope = new Environment(scope); + const current = iterable.value[i]; + let scopeUpdateFunction; + if (node.loopvar.type === "Identifier") { + scopeUpdateFunction = (scope2) => scope2.setVariable(node.loopvar.value, current); + } else if (node.loopvar.type === "TupleLiteral") { + const loopvar = node.loopvar; + if (current.type !== "ArrayValue") { + throw new Error(`Cannot unpack non-iterable type: ${current.type}`); + } + const c = current; + if (loopvar.value.length !== c.value.length) { + throw new Error(`Too ${loopvar.value.length > c.value.length ? "few" : "many"} items to unpack`); + } + scopeUpdateFunction = (scope2) => { + for (let j = 0; j < loopvar.value.length; ++j) { + if (loopvar.value[j].type !== "Identifier") { + throw new Error(`Cannot unpack non-identifier type: ${loopvar.value[j].type}`); + } + scope2.setVariable(loopvar.value[j].value, c.value[j]); + } + }; + } else { + throw new Error(`Invalid loop variable(s): ${node.loopvar.type}`); + } + if (test) { + scopeUpdateFunction(loopScope); + const testValue = this.evaluate(test, loopScope); + if (!testValue.__bool__().value) { + continue; + } + } + items.push(current); + scopeUpdateFunctions.push(scopeUpdateFunction); + } + let result = ""; + let noIteration = true; + for (let i = 0; i < items.length; ++i) { + const loop = /* @__PURE__ */ new Map([ + ["index", new NumericValue(i + 1)], + ["index0", new NumericValue(i)], + ["revindex", new NumericValue(items.length - i)], + ["revindex0", new NumericValue(items.length - i - 1)], + ["first", new BooleanValue(i === 0)], + ["last", new BooleanValue(i === items.length - 1)], + ["length", new NumericValue(items.length)], + ["previtem", i > 0 ? items[i - 1] : new UndefinedValue()], + ["nextitem", i < items.length - 1 ? items[i + 1] : new UndefinedValue()] + ]); + scope.setVariable("loop", new ObjectValue(loop)); + scopeUpdateFunctions[i](scope); + const evaluated = this.evaluateBlock(node.body, scope); + result += evaluated.value; + noIteration = false; + } + if (noIteration) { + const defaultEvaluated = this.evaluateBlock(node.defaultBlock, scope); + result += defaultEvaluated.value; + } + return new StringValue(result); + } + /** + * See https://jinja.palletsprojects.com/en/3.1.x/templates/#macros for more information. + */ + evaluateMacro(node, environment) { + environment.setVariable( + node.name.value, + new FunctionValue((args, scope) => { + const macroScope = new Environment(scope); + args = args.slice(); + let kwargs; + if (args.at(-1)?.type === "KeywordArgumentsValue") { + kwargs = args.pop(); + } + for (let i = 0; i < node.args.length; ++i) { + const nodeArg = node.args[i]; + const passedArg = args[i]; + if (nodeArg.type === "Identifier") { + const identifier = nodeArg; + if (!passedArg) { + throw new Error(`Missing positional argument: ${identifier.value}`); + } + macroScope.setVariable(identifier.value, passedArg); + } else if (nodeArg.type === "KeywordArgumentExpression") { + const kwarg = nodeArg; + const value = passedArg ?? // Try positional arguments first + kwargs?.value.get(kwarg.key.value) ?? // Look in user-passed kwargs + this.evaluate(kwarg.value, macroScope); + macroScope.setVariable(kwarg.key.value, value); + } else { + throw new Error(`Unknown argument type: ${nodeArg.type}`); + } + } + return this.evaluateBlock(node.body, macroScope); + }) + ); + return new NullValue(); + } + evaluate(statement, environment) { + if (statement === void 0) + return new UndefinedValue(); + switch (statement.type) { + case "Program": + return this.evalProgram(statement, environment); + case "Set": + return this.evaluateSet(statement, environment); + case "If": + return this.evaluateIf(statement, environment); + case "For": + return this.evaluateFor(statement, environment); + case "Macro": + return this.evaluateMacro(statement, environment); + case "NumericLiteral": + return new NumericValue(Number(statement.value)); + case "StringLiteral": + return new StringValue(statement.value); + case "BooleanLiteral": + return new BooleanValue(statement.value); + case "ArrayLiteral": + return new ArrayValue(statement.value.map((x) => this.evaluate(x, environment))); + case "TupleLiteral": + return new TupleValue(statement.value.map((x) => this.evaluate(x, environment))); + case "ObjectLiteral": { + const mapping = /* @__PURE__ */ new Map(); + for (const [key, value] of statement.value) { + const evaluatedKey = this.evaluate(key, environment); + if (!(evaluatedKey instanceof StringValue)) { + throw new Error(`Object keys must be strings: got ${evaluatedKey.type}`); + } + mapping.set(evaluatedKey.value, this.evaluate(value, environment)); + } + return new ObjectValue(mapping); + } + case "Identifier": + return this.evaluateIdentifier(statement, environment); + case "CallExpression": + return this.evaluateCallExpression(statement, environment); + case "MemberExpression": + return this.evaluateMemberExpression(statement, environment); + case "UnaryExpression": + return this.evaluateUnaryExpression(statement, environment); + case "BinaryExpression": + return this.evaluateBinaryExpression(statement, environment); + case "FilterExpression": + return this.evaluateFilterExpression(statement, environment); + case "TestExpression": + return this.evaluateTestExpression(statement, environment); + default: + throw new SyntaxError(`Unknown node type: ${statement.type}`); + } + } +}; +function convertToRuntimeValues(input) { + switch (typeof input) { + case "number": + return new NumericValue(input); + case "string": + return new StringValue(input); + case "boolean": + return new BooleanValue(input); + case "undefined": + return new UndefinedValue(); + case "object": + if (input === null) { + return new NullValue(); + } else if (Array.isArray(input)) { + return new ArrayValue(input.map(convertToRuntimeValues)); + } else { + return new ObjectValue( + new Map(Object.entries(input).map(([key, value]) => [key, convertToRuntimeValues(value)])) + ); + } + case "function": + return new FunctionValue((args, _scope) => { + const result = input(...args.map((x) => x.value)) ?? null; + return convertToRuntimeValues(result); + }); + default: + throw new Error(`Cannot convert to runtime value: ${input}`); + } +} +function toJSON(input, indent, depth) { + const currentDepth = depth ?? 0; + switch (input.type) { + case "NullValue": + case "UndefinedValue": + return "null"; + case "NumericValue": + case "StringValue": + case "BooleanValue": + return JSON.stringify(input.value); + case "ArrayValue": + case "ObjectValue": { + const indentValue = indent ? " ".repeat(indent) : ""; + const basePadding = "\n" + indentValue.repeat(currentDepth); + const childrenPadding = basePadding + indentValue; + if (input.type === "ArrayValue") { + const core = input.value.map((x) => toJSON(x, indent, currentDepth + 1)); + return indent ? `[${childrenPadding}${core.join(`,${childrenPadding}`)}${basePadding}]` : `[${core.join(", ")}]`; + } else { + const core = Array.from(input.value.entries()).map(([key, value]) => { + const v = `"${key}": ${toJSON(value, indent, currentDepth + 1)}`; + return indent ? `${childrenPadding}${v}` : v; + }); + return indent ? `{${core.join(",")}${basePadding}}` : `{${core.join(", ")}}`; + } + } + default: + throw new Error(`Cannot convert to JSON: ${input.type}`); + } +} + +// src/index.ts +var Template = class { + parsed; + /** + * @param {string} template The template string + */ + constructor(template) { + const tokens = tokenize(template, { + lstrip_blocks: true, + trim_blocks: true + }); + this.parsed = parse(tokens); + } + render(items) { + const env = new Environment(); + env.set("false", false); + env.set("true", true); + env.set("raise_exception", (args) => { + throw new Error(args); + }); + env.set("range", range); + for (const [key, value] of Object.entries(items)) { + env.set(key, value); + } + const interpreter = new Interpreter(env); + const result = interpreter.run(this.parsed); + return result.value; + } +}; + + + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/backend-impl.js": +/*!******************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/backend-impl.js ***! + \******************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ registerBackend: () => (/* binding */ registerBackend), +/* harmony export */ resolveBackendAndExecutionProviders: () => (/* binding */ resolveBackendAndExecutionProviders) +/* harmony export */ }); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +const backends = new Map(); +const backendsSortedByPriority = []; +/** + * Register a backend. + * + * @param name - the name as a key to lookup as an execution provider. + * @param backend - the backend object. + * @param priority - an integer indicating the priority of the backend. Higher number means higher priority. if priority + * < 0, it will be considered as a 'beta' version and will not be used as a fallback backend by default. + * + * @ignore + */ +const registerBackend = (name, backend, priority) => { + if (backend && typeof backend.init === 'function' && typeof backend.createInferenceSessionHandler === 'function') { + const currentBackend = backends.get(name); + if (currentBackend === undefined) { + backends.set(name, { backend, priority }); + } + else if (currentBackend.priority > priority) { + // same name is already registered with a higher priority. skip registeration. + return; + } + else if (currentBackend.priority === priority) { + if (currentBackend.backend !== backend) { + throw new Error(`cannot register backend "${name}" using priority ${priority}`); + } + } + if (priority >= 0) { + const i = backendsSortedByPriority.indexOf(name); + if (i !== -1) { + backendsSortedByPriority.splice(i, 1); + } + for (let i = 0; i < backendsSortedByPriority.length; i++) { + if (backends.get(backendsSortedByPriority[i]).priority <= priority) { + backendsSortedByPriority.splice(i, 0, name); + return; + } + } + backendsSortedByPriority.push(name); + } + return; + } + throw new TypeError('not a valid backend'); +}; +/** + * Try to resolve and initialize a backend. + * + * @param backendName - the name of the backend. + * @returns the backend instance if resolved and initialized successfully, or an error message if failed. + */ +const tryResolveAndInitializeBackend = async (backendName) => { + const backendInfo = backends.get(backendName); + if (!backendInfo) { + return 'backend not found.'; + } + if (backendInfo.initialized) { + return backendInfo.backend; + } + else if (backendInfo.aborted) { + return backendInfo.error; + } + else { + const isInitializing = !!backendInfo.initPromise; + try { + if (!isInitializing) { + backendInfo.initPromise = backendInfo.backend.init(backendName); + } + await backendInfo.initPromise; + backendInfo.initialized = true; + return backendInfo.backend; + } + catch (e) { + if (!isInitializing) { + backendInfo.error = `${e}`; + backendInfo.aborted = true; + } + return backendInfo.error; + } + finally { + delete backendInfo.initPromise; + } + } +}; +/** + * Resolve execution providers from the specific session options. + * + * @param options - the session options object. + * @returns a promise that resolves to a tuple of an initialized backend instance and a session options object with + * filtered EP list. + * + * @ignore + */ +const resolveBackendAndExecutionProviders = async (options) => { + // extract backend hints from session options + const eps = options.executionProviders || []; + const backendHints = eps.map(i => typeof i === 'string' ? i : i.name); + const backendNames = backendHints.length === 0 ? backendsSortedByPriority : backendHints; + // try to resolve and initialize all requested backends + let backend; + const errors = []; + const availableBackendNames = new Set(); + for (const backendName of backendNames) { + const resolveResult = await tryResolveAndInitializeBackend(backendName); + if (typeof resolveResult === 'string') { + errors.push({ name: backendName, err: resolveResult }); + } + else { + if (!backend) { + backend = resolveResult; + } + if (backend === resolveResult) { + availableBackendNames.add(backendName); + } + } + } + // if no backend is available, throw error. + if (!backend) { + throw new Error(`no available backend found. ERR: ${errors.map(e => `[${e.name}] ${e.err}`).join(', ')}`); + } + // for each explicitly requested backend, if it's not available, output warning message. + for (const { name, err } of errors) { + if (backendHints.includes(name)) { + // eslint-disable-next-line no-console + console.warn(`removing requested execution provider "${name}" from session options because it is not available: ${err}`); + } + } + const filteredEps = eps.filter(i => availableBackendNames.has(typeof i === 'string' ? i : i.name)); + return [ + backend, new Proxy(options, { + get: (target, prop) => { + if (prop === 'executionProviders') { + return filteredEps; + } + return Reflect.get(target, prop); + } + }) + ]; +}; +//# sourceMappingURL=backend-impl.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/backend.js": +/*!*************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/backend.js ***! + \*************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ registerBackend: () => (/* reexport safe */ _backend_impl_js__WEBPACK_IMPORTED_MODULE_0__.registerBackend) +/* harmony export */ }); +/* harmony import */ var _backend_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./backend-impl.js */ "./node_modules/onnxruntime-common/dist/esm/backend-impl.js"); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +//# sourceMappingURL=backend.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/env-impl.js": +/*!**************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/env-impl.js ***! + \**************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ env: () => (/* binding */ env) +/* harmony export */ }); +/* harmony import */ var _version_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./version.js */ "./node_modules/onnxruntime-common/dist/esm/version.js"); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +let logLevelValue = 'warning'; +const env = { + wasm: {}, + webgl: {}, + webgpu: {}, + versions: { common: _version_js__WEBPACK_IMPORTED_MODULE_0__.version }, + set logLevel(value) { + if (value === undefined) { + return; + } + if (typeof value !== 'string' || ['verbose', 'info', 'warning', 'error', 'fatal'].indexOf(value) === -1) { + throw new Error(`Unsupported logging level: ${value}`); + } + logLevelValue = value; + }, + get logLevel() { + return logLevelValue; + }, +}; +// set property 'logLevel' so that they can be correctly transferred to worker by `postMessage()`. +Object.defineProperty(env, 'logLevel', { enumerable: true }); +//# sourceMappingURL=env-impl.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/env.js": +/*!*********************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/env.js ***! + \*********************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ env: () => (/* binding */ env) +/* harmony export */ }); +/* harmony import */ var _env_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./env-impl.js */ "./node_modules/onnxruntime-common/dist/esm/env-impl.js"); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +/** + * Represent a set of flags as a global singleton. + */ +const env = _env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env; +//# sourceMappingURL=env.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/index.js": +/*!***********************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/index.js ***! + \***********************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ InferenceSession: () => (/* reexport safe */ _inference_session_js__WEBPACK_IMPORTED_MODULE_2__.InferenceSession), +/* harmony export */ TRACE: () => (/* reexport safe */ _trace_js__WEBPACK_IMPORTED_MODULE_6__.TRACE), +/* harmony export */ TRACE_FUNC_BEGIN: () => (/* reexport safe */ _trace_js__WEBPACK_IMPORTED_MODULE_6__.TRACE_FUNC_BEGIN), +/* harmony export */ TRACE_FUNC_END: () => (/* reexport safe */ _trace_js__WEBPACK_IMPORTED_MODULE_6__.TRACE_FUNC_END), +/* harmony export */ Tensor: () => (/* reexport safe */ _tensor_js__WEBPACK_IMPORTED_MODULE_3__.Tensor), +/* harmony export */ TrainingSession: () => (/* reexport safe */ _training_session_js__WEBPACK_IMPORTED_MODULE_9__.TrainingSession), +/* harmony export */ env: () => (/* reexport safe */ _env_js__WEBPACK_IMPORTED_MODULE_1__.env), +/* harmony export */ registerBackend: () => (/* reexport safe */ _backend_js__WEBPACK_IMPORTED_MODULE_0__.registerBackend) +/* harmony export */ }); +/* harmony import */ var _backend_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./backend.js */ "./node_modules/onnxruntime-common/dist/esm/backend.js"); +/* harmony import */ var _env_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./env.js */ "./node_modules/onnxruntime-common/dist/esm/env.js"); +/* harmony import */ var _inference_session_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./inference-session.js */ "./node_modules/onnxruntime-common/dist/esm/inference-session.js"); +/* harmony import */ var _tensor_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./tensor.js */ "./node_modules/onnxruntime-common/dist/esm/tensor.js"); +/* harmony import */ var _tensor_conversion_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ./tensor-conversion.js */ "./node_modules/onnxruntime-common/dist/esm/tensor-conversion.js"); +/* harmony import */ var _tensor_factory_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ./tensor-factory.js */ "./node_modules/onnxruntime-common/dist/esm/tensor-factory.js"); +/* harmony import */ var _trace_js__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! ./trace.js */ "./node_modules/onnxruntime-common/dist/esm/trace.js"); +/* harmony import */ var _onnx_model_js__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__(/*! ./onnx-model.js */ "./node_modules/onnxruntime-common/dist/esm/onnx-model.js"); +/* harmony import */ var _onnx_value_js__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__(/*! ./onnx-value.js */ "./node_modules/onnxruntime-common/dist/esm/onnx-value.js"); +/* harmony import */ var _training_session_js__WEBPACK_IMPORTED_MODULE_9__ = __webpack_require__(/*! ./training-session.js */ "./node_modules/onnxruntime-common/dist/esm/training-session.js"); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +/** + * # ONNX Runtime JavaScript API + * + * ONNX Runtime JavaScript API is a unified API for all JavaScript usages, including the following NPM packages: + * + * - [onnxruntime-node](https://www.npmjs.com/package/onnxruntime-node) + * - [onnxruntime-web](https://www.npmjs.com/package/onnxruntime-web) + * - [onnxruntime-react-native](https://www.npmjs.com/package/onnxruntime-react-native) + * + * See also: + * - [Get Started](https://onnxruntime.ai/docs/get-started/with-javascript/) + * - [Inference examples](https://github.com/microsoft/onnxruntime-inference-examples/tree/main/js) + * + * @packageDocumentation + */ + + + + + + + + + + +//# sourceMappingURL=index.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/inference-session-impl.js": +/*!****************************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/inference-session-impl.js ***! + \****************************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ InferenceSession: () => (/* binding */ InferenceSession) +/* harmony export */ }); +/* harmony import */ var _backend_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./backend-impl.js */ "./node_modules/onnxruntime-common/dist/esm/backend-impl.js"); +/* harmony import */ var _tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./tensor.js */ "./node_modules/onnxruntime-common/dist/esm/tensor.js"); +/* harmony import */ var _trace_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./trace.js */ "./node_modules/onnxruntime-common/dist/esm/trace.js"); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + + + +class InferenceSession { + constructor(handler) { + this.handler = handler; + } + async run(feeds, arg1, arg2) { + (0,_trace_js__WEBPACK_IMPORTED_MODULE_2__.TRACE_FUNC_BEGIN)(); + const fetches = {}; + let options = {}; + // check inputs + if (typeof feeds !== 'object' || feeds === null || feeds instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor || Array.isArray(feeds)) { + throw new TypeError('\'feeds\' must be an object that use input names as keys and OnnxValue as corresponding values.'); + } + let isFetchesEmpty = true; + // determine which override is being used + if (typeof arg1 === 'object') { + if (arg1 === null) { + throw new TypeError('Unexpected argument[1]: cannot be null.'); + } + if (arg1 instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor) { + throw new TypeError('\'fetches\' cannot be a Tensor'); + } + if (Array.isArray(arg1)) { + if (arg1.length === 0) { + throw new TypeError('\'fetches\' cannot be an empty array.'); + } + isFetchesEmpty = false; + // output names + for (const name of arg1) { + if (typeof name !== 'string') { + throw new TypeError('\'fetches\' must be a string array or an object.'); + } + if (this.outputNames.indexOf(name) === -1) { + throw new RangeError(`'fetches' contains invalid output name: ${name}.`); + } + fetches[name] = null; + } + if (typeof arg2 === 'object' && arg2 !== null) { + options = arg2; + } + else if (typeof arg2 !== 'undefined') { + throw new TypeError('\'options\' must be an object.'); + } + } + else { + // decide whether arg1 is fetches or options + // if any output name is present and its value is valid OnnxValue, we consider it fetches + let isFetches = false; + const arg1Keys = Object.getOwnPropertyNames(arg1); + for (const name of this.outputNames) { + if (arg1Keys.indexOf(name) !== -1) { + const v = arg1[name]; + if (v === null || v instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor) { + isFetches = true; + isFetchesEmpty = false; + fetches[name] = v; + } + } + } + if (isFetches) { + if (typeof arg2 === 'object' && arg2 !== null) { + options = arg2; + } + else if (typeof arg2 !== 'undefined') { + throw new TypeError('\'options\' must be an object.'); + } + } + else { + options = arg1; + } + } + } + else if (typeof arg1 !== 'undefined') { + throw new TypeError('Unexpected argument[1]: must be \'fetches\' or \'options\'.'); + } + // check if all inputs are in feed + for (const name of this.inputNames) { + if (typeof feeds[name] === 'undefined') { + throw new Error(`input '${name}' is missing in 'feeds'.`); + } + } + // if no fetches is specified, we use the full output names list + if (isFetchesEmpty) { + for (const name of this.outputNames) { + fetches[name] = null; + } + } + // feeds, fetches and options are prepared + const results = await this.handler.run(feeds, fetches, options); + const returnValue = {}; + for (const key in results) { + if (Object.hasOwnProperty.call(results, key)) { + const result = results[key]; + if (result instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor) { + returnValue[key] = result; + } + else { + returnValue[key] = new _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor(result.type, result.data, result.dims); + } + } + } + (0,_trace_js__WEBPACK_IMPORTED_MODULE_2__.TRACE_FUNC_END)(); + return returnValue; + } + async release() { + return this.handler.dispose(); + } + static async create(arg0, arg1, arg2, arg3) { + (0,_trace_js__WEBPACK_IMPORTED_MODULE_2__.TRACE_FUNC_BEGIN)(); + // either load from a file or buffer + let filePathOrUint8Array; + let options = {}; + if (typeof arg0 === 'string') { + filePathOrUint8Array = arg0; + if (typeof arg1 === 'object' && arg1 !== null) { + options = arg1; + } + else if (typeof arg1 !== 'undefined') { + throw new TypeError('\'options\' must be an object.'); + } + } + else if (arg0 instanceof Uint8Array) { + filePathOrUint8Array = arg0; + if (typeof arg1 === 'object' && arg1 !== null) { + options = arg1; + } + else if (typeof arg1 !== 'undefined') { + throw new TypeError('\'options\' must be an object.'); + } + } + else if (arg0 instanceof ArrayBuffer || + (typeof SharedArrayBuffer !== 'undefined' && arg0 instanceof SharedArrayBuffer)) { + const buffer = arg0; + let byteOffset = 0; + let byteLength = arg0.byteLength; + if (typeof arg1 === 'object' && arg1 !== null) { + options = arg1; + } + else if (typeof arg1 === 'number') { + byteOffset = arg1; + if (!Number.isSafeInteger(byteOffset)) { + throw new RangeError('\'byteOffset\' must be an integer.'); + } + if (byteOffset < 0 || byteOffset >= buffer.byteLength) { + throw new RangeError(`'byteOffset' is out of range [0, ${buffer.byteLength}).`); + } + byteLength = arg0.byteLength - byteOffset; + if (typeof arg2 === 'number') { + byteLength = arg2; + if (!Number.isSafeInteger(byteLength)) { + throw new RangeError('\'byteLength\' must be an integer.'); + } + if (byteLength <= 0 || byteOffset + byteLength > buffer.byteLength) { + throw new RangeError(`'byteLength' is out of range (0, ${buffer.byteLength - byteOffset}].`); + } + if (typeof arg3 === 'object' && arg3 !== null) { + options = arg3; + } + else if (typeof arg3 !== 'undefined') { + throw new TypeError('\'options\' must be an object.'); + } + } + else if (typeof arg2 !== 'undefined') { + throw new TypeError('\'byteLength\' must be a number.'); + } + } + else if (typeof arg1 !== 'undefined') { + throw new TypeError('\'options\' must be an object.'); + } + filePathOrUint8Array = new Uint8Array(buffer, byteOffset, byteLength); + } + else { + throw new TypeError('Unexpected argument[0]: must be \'path\' or \'buffer\'.'); + } + // resolve backend, update session options with validated EPs, and create session handler + const [backend, optionsWithValidatedEPs] = await (0,_backend_impl_js__WEBPACK_IMPORTED_MODULE_0__.resolveBackendAndExecutionProviders)(options); + const handler = await backend.createInferenceSessionHandler(filePathOrUint8Array, optionsWithValidatedEPs); + (0,_trace_js__WEBPACK_IMPORTED_MODULE_2__.TRACE_FUNC_END)(); + return new InferenceSession(handler); + } + startProfiling() { + this.handler.startProfiling(); + } + endProfiling() { + this.handler.endProfiling(); + } + get inputNames() { + return this.handler.inputNames; + } + get outputNames() { + return this.handler.outputNames; + } +} +//# sourceMappingURL=inference-session-impl.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/inference-session.js": +/*!***********************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/inference-session.js ***! + \***********************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ InferenceSession: () => (/* binding */ InferenceSession) +/* harmony export */ }); +/* harmony import */ var _inference_session_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./inference-session-impl.js */ "./node_modules/onnxruntime-common/dist/esm/inference-session-impl.js"); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// eslint-disable-next-line @typescript-eslint/naming-convention +const InferenceSession = _inference_session_impl_js__WEBPACK_IMPORTED_MODULE_0__.InferenceSession; +//# sourceMappingURL=inference-session.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/onnx-model.js": +/*!****************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/onnx-model.js ***! + \****************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +//# sourceMappingURL=onnx-model.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/onnx-value.js": +/*!****************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/onnx-value.js ***! + \****************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +//# sourceMappingURL=onnx-value.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/tensor-conversion-impl.js": +/*!****************************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/tensor-conversion-impl.js ***! + \****************************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ tensorToDataURL: () => (/* binding */ tensorToDataURL), +/* harmony export */ tensorToImageData: () => (/* binding */ tensorToImageData) +/* harmony export */ }); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +/** + * implementation of Tensor.toDataURL() + */ +const tensorToDataURL = (tensor, options) => { + const canvas = typeof document !== 'undefined' ? document.createElement('canvas') : (new OffscreenCanvas(1, 1)); + canvas.width = tensor.dims[3]; + canvas.height = tensor.dims[2]; + const pixels2DContext = canvas.getContext('2d'); + if (pixels2DContext != null) { + // Default values for height and width & format + let width; + let height; + if (options?.tensorLayout !== undefined && options.tensorLayout === 'NHWC') { + width = tensor.dims[2]; + height = tensor.dims[3]; + } + else { // Default layout is NCWH + width = tensor.dims[3]; + height = tensor.dims[2]; + } + const inputformat = options?.format !== undefined ? options.format : 'RGB'; + const norm = options?.norm; + let normMean; + let normBias; + if (norm === undefined || norm.mean === undefined) { + normMean = [255, 255, 255, 255]; + } + else { + if (typeof (norm.mean) === 'number') { + normMean = [norm.mean, norm.mean, norm.mean, norm.mean]; + } + else { + normMean = [norm.mean[0], norm.mean[1], norm.mean[2], 0]; + if (norm.mean[3] !== undefined) { + normMean[3] = norm.mean[3]; + } + } + } + if (norm === undefined || norm.bias === undefined) { + normBias = [0, 0, 0, 0]; + } + else { + if (typeof (norm.bias) === 'number') { + normBias = [norm.bias, norm.bias, norm.bias, norm.bias]; + } + else { + normBias = [norm.bias[0], norm.bias[1], norm.bias[2], 0]; + if (norm.bias[3] !== undefined) { + normBias[3] = norm.bias[3]; + } + } + } + const stride = height * width; + // Default pointer assignments + let rTensorPointer = 0, gTensorPointer = stride, bTensorPointer = stride * 2, aTensorPointer = -1; + // Updating the pointer assignments based on the input image format + if (inputformat === 'RGBA') { + rTensorPointer = 0; + gTensorPointer = stride; + bTensorPointer = stride * 2; + aTensorPointer = stride * 3; + } + else if (inputformat === 'RGB') { + rTensorPointer = 0; + gTensorPointer = stride; + bTensorPointer = stride * 2; + } + else if (inputformat === 'RBG') { + rTensorPointer = 0; + bTensorPointer = stride; + gTensorPointer = stride * 2; + } + for (let i = 0; i < height; i++) { + for (let j = 0; j < width; j++) { + const R = (tensor.data[rTensorPointer++] - normBias[0]) * normMean[0]; // R value + const G = (tensor.data[gTensorPointer++] - normBias[1]) * normMean[1]; // G value + const B = (tensor.data[bTensorPointer++] - normBias[2]) * normMean[2]; // B value + const A = aTensorPointer === -1 ? + 255 : + (tensor.data[aTensorPointer++] - normBias[3]) * normMean[3]; // A value + // eslint-disable-next-line @typescript-eslint/restrict-plus-operands + pixels2DContext.fillStyle = 'rgba(' + R + ',' + G + ',' + B + ',' + A + ')'; + pixels2DContext.fillRect(j, i, 1, 1); + } + } + if ('toDataURL' in canvas) { + return canvas.toDataURL(); + } + else { + throw new Error('toDataURL is not supported'); + } + } + else { + throw new Error('Can not access image data'); + } +}; +/** + * implementation of Tensor.toImageData() + */ +const tensorToImageData = (tensor, options) => { + const pixels2DContext = typeof document !== 'undefined' ? + document.createElement('canvas').getContext('2d') : + new OffscreenCanvas(1, 1).getContext('2d'); + let image; + if (pixels2DContext != null) { + // Default values for height and width & format + let width; + let height; + let channels; + if (options?.tensorLayout !== undefined && options.tensorLayout === 'NHWC') { + width = tensor.dims[2]; + height = tensor.dims[1]; + channels = tensor.dims[3]; + } + else { // Default layout is NCWH + width = tensor.dims[3]; + height = tensor.dims[2]; + channels = tensor.dims[1]; + } + const inputformat = options !== undefined ? (options.format !== undefined ? options.format : 'RGB') : 'RGB'; + const norm = options?.norm; + let normMean; + let normBias; + if (norm === undefined || norm.mean === undefined) { + normMean = [255, 255, 255, 255]; + } + else { + if (typeof (norm.mean) === 'number') { + normMean = [norm.mean, norm.mean, norm.mean, norm.mean]; + } + else { + normMean = [norm.mean[0], norm.mean[1], norm.mean[2], 255]; + if (norm.mean[3] !== undefined) { + normMean[3] = norm.mean[3]; + } + } + } + if (norm === undefined || norm.bias === undefined) { + normBias = [0, 0, 0, 0]; + } + else { + if (typeof (norm.bias) === 'number') { + normBias = [norm.bias, norm.bias, norm.bias, norm.bias]; + } + else { + normBias = [norm.bias[0], norm.bias[1], norm.bias[2], 0]; + if (norm.bias[3] !== undefined) { + normBias[3] = norm.bias[3]; + } + } + } + const stride = height * width; + if (options !== undefined) { + if (options.format !== undefined && (channels === 4 && options.format !== 'RGBA') || + (channels === 3 && (options.format !== 'RGB' && options.format !== 'BGR'))) { + throw new Error('Tensor format doesn\'t match input tensor dims'); + } + } + // Default pointer assignments + const step = 4; + let rImagePointer = 0, gImagePointer = 1, bImagePointer = 2, aImagePointer = 3; + let rTensorPointer = 0, gTensorPointer = stride, bTensorPointer = stride * 2, aTensorPointer = -1; + // Updating the pointer assignments based on the input image format + if (inputformat === 'RGBA') { + rTensorPointer = 0; + gTensorPointer = stride; + bTensorPointer = stride * 2; + aTensorPointer = stride * 3; + } + else if (inputformat === 'RGB') { + rTensorPointer = 0; + gTensorPointer = stride; + bTensorPointer = stride * 2; + } + else if (inputformat === 'RBG') { + rTensorPointer = 0; + bTensorPointer = stride; + gTensorPointer = stride * 2; + } + image = pixels2DContext.createImageData(width, height); + for (let i = 0; i < height * width; rImagePointer += step, gImagePointer += step, bImagePointer += step, aImagePointer += step, i++) { + image.data[rImagePointer] = (tensor.data[rTensorPointer++] - normBias[0]) * normMean[0]; // R value + image.data[gImagePointer] = (tensor.data[gTensorPointer++] - normBias[1]) * normMean[1]; // G value + image.data[bImagePointer] = (tensor.data[bTensorPointer++] - normBias[2]) * normMean[2]; // B value + image.data[aImagePointer] = aTensorPointer === -1 ? + 255 : + (tensor.data[aTensorPointer++] - normBias[3]) * normMean[3]; // A value + } + } + else { + throw new Error('Can not access image data'); + } + return image; +}; +//# sourceMappingURL=tensor-conversion-impl.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/tensor-conversion.js": +/*!***********************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/tensor-conversion.js ***! + \***********************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +//# sourceMappingURL=tensor-conversion.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/tensor-factory-impl.js": +/*!*************************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/tensor-factory-impl.js ***! + \*************************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ bufferToTensor: () => (/* binding */ bufferToTensor), +/* harmony export */ tensorFromGpuBuffer: () => (/* binding */ tensorFromGpuBuffer), +/* harmony export */ tensorFromImage: () => (/* binding */ tensorFromImage), +/* harmony export */ tensorFromPinnedBuffer: () => (/* binding */ tensorFromPinnedBuffer), +/* harmony export */ tensorFromTexture: () => (/* binding */ tensorFromTexture) +/* harmony export */ }); +/* harmony import */ var _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./tensor-impl.js */ "./node_modules/onnxruntime-common/dist/esm/tensor-impl.js"); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +/** + * Create a new tensor object from image object + * + * @param buffer - Extracted image buffer data - assuming RGBA format + * @param imageFormat - input image configuration - required configurations height, width, format + * @param tensorFormat - output tensor configuration - Default is RGB format + */ +const bufferToTensor = (buffer, options) => { + if (buffer === undefined) { + throw new Error('Image buffer must be defined'); + } + if (options.height === undefined || options.width === undefined) { + throw new Error('Image height and width must be defined'); + } + if (options.tensorLayout === 'NHWC') { + throw new Error('NHWC Tensor layout is not supported yet'); + } + const { height, width } = options; + const norm = options.norm ?? { mean: 255, bias: 0 }; + let normMean; + let normBias; + if (typeof (norm.mean) === 'number') { + normMean = [norm.mean, norm.mean, norm.mean, norm.mean]; + } + else { + normMean = [norm.mean[0], norm.mean[1], norm.mean[2], norm.mean[3] ?? 255]; + } + if (typeof (norm.bias) === 'number') { + normBias = [norm.bias, norm.bias, norm.bias, norm.bias]; + } + else { + normBias = [norm.bias[0], norm.bias[1], norm.bias[2], norm.bias[3] ?? 0]; + } + const inputformat = options.format !== undefined ? options.format : 'RGBA'; + // default value is RGBA since imagedata and HTMLImageElement uses it + const outputformat = options.tensorFormat !== undefined ? (options.tensorFormat !== undefined ? options.tensorFormat : 'RGB') : 'RGB'; + const stride = height * width; + const float32Data = outputformat === 'RGBA' ? new Float32Array(stride * 4) : new Float32Array(stride * 3); + // Default pointer assignments + let step = 4, rImagePointer = 0, gImagePointer = 1, bImagePointer = 2, aImagePointer = 3; + let rTensorPointer = 0, gTensorPointer = stride, bTensorPointer = stride * 2, aTensorPointer = -1; + // Updating the pointer assignments based on the input image format + if (inputformat === 'RGB') { + step = 3; + rImagePointer = 0; + gImagePointer = 1; + bImagePointer = 2; + aImagePointer = -1; + } + // Updating the pointer assignments based on the output tensor format + if (outputformat === 'RGBA') { + aTensorPointer = stride * 3; + } + else if (outputformat === 'RBG') { + rTensorPointer = 0; + bTensorPointer = stride; + gTensorPointer = stride * 2; + } + else if (outputformat === 'BGR') { + bTensorPointer = 0; + gTensorPointer = stride; + rTensorPointer = stride * 2; + } + for (let i = 0; i < stride; i++, rImagePointer += step, bImagePointer += step, gImagePointer += step, aImagePointer += step) { + float32Data[rTensorPointer++] = (buffer[rImagePointer] + normBias[0]) / normMean[0]; + float32Data[gTensorPointer++] = (buffer[gImagePointer] + normBias[1]) / normMean[1]; + float32Data[bTensorPointer++] = (buffer[bImagePointer] + normBias[2]) / normMean[2]; + if (aTensorPointer !== -1 && aImagePointer !== -1) { + float32Data[aTensorPointer++] = (buffer[aImagePointer] + normBias[3]) / normMean[3]; + } + } + // Float32Array -> ort.Tensor + const outputTensor = outputformat === 'RGBA' ? new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor('float32', float32Data, [1, 4, height, width]) : + new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor('float32', float32Data, [1, 3, height, width]); + return outputTensor; +}; +/** + * implementation of Tensor.fromImage(). + */ +const tensorFromImage = async (image, options) => { + // checking the type of image object + const isHTMLImageEle = typeof (HTMLImageElement) !== 'undefined' && image instanceof HTMLImageElement; + const isImageDataEle = typeof (ImageData) !== 'undefined' && image instanceof ImageData; + const isImageBitmap = typeof (ImageBitmap) !== 'undefined' && image instanceof ImageBitmap; + const isString = typeof image === 'string'; + let data; + let bufferToTensorOptions = options ?? {}; + const createCanvas = () => { + if (typeof document !== 'undefined') { + return document.createElement('canvas'); + } + else if (typeof OffscreenCanvas !== 'undefined') { + return new OffscreenCanvas(1, 1); + } + else { + throw new Error('Canvas is not supported'); + } + }; + const createCanvasContext = (canvas) => { + if (canvas instanceof HTMLCanvasElement) { + return canvas.getContext('2d'); + } + else if (canvas instanceof OffscreenCanvas) { + return canvas.getContext('2d'); + } + else { + return null; + } + }; + // filling and checking image configuration options + if (isHTMLImageEle) { + // HTMLImageElement - image object - format is RGBA by default + const canvas = createCanvas(); + canvas.width = image.width; + canvas.height = image.height; + const pixels2DContext = createCanvasContext(canvas); + if (pixels2DContext != null) { + let height = image.height; + let width = image.width; + if (options !== undefined && options.resizedHeight !== undefined && options.resizedWidth !== undefined) { + height = options.resizedHeight; + width = options.resizedWidth; + } + if (options !== undefined) { + bufferToTensorOptions = options; + if (options.tensorFormat !== undefined) { + throw new Error('Image input config format must be RGBA for HTMLImageElement'); + } + else { + bufferToTensorOptions.tensorFormat = 'RGBA'; + } + bufferToTensorOptions.height = height; + bufferToTensorOptions.width = width; + } + else { + bufferToTensorOptions.tensorFormat = 'RGBA'; + bufferToTensorOptions.height = height; + bufferToTensorOptions.width = width; + } + pixels2DContext.drawImage(image, 0, 0); + data = pixels2DContext.getImageData(0, 0, width, height).data; + } + else { + throw new Error('Can not access image data'); + } + } + else if (isImageDataEle) { + let height; + let width; + if (options !== undefined && options.resizedWidth !== undefined && options.resizedHeight !== undefined) { + height = options.resizedHeight; + width = options.resizedWidth; + } + else { + height = image.height; + width = image.width; + } + if (options !== undefined) { + bufferToTensorOptions = options; + } + bufferToTensorOptions.format = 'RGBA'; + bufferToTensorOptions.height = height; + bufferToTensorOptions.width = width; + if (options !== undefined) { + const tempCanvas = createCanvas(); + tempCanvas.width = width; + tempCanvas.height = height; + const pixels2DContext = createCanvasContext(tempCanvas); + if (pixels2DContext != null) { + pixels2DContext.putImageData(image, 0, 0); + data = pixels2DContext.getImageData(0, 0, width, height).data; + } + else { + throw new Error('Can not access image data'); + } + } + else { + data = image.data; + } + } + else if (isImageBitmap) { + // ImageBitmap - image object - format must be provided by user + if (options === undefined) { + throw new Error('Please provide image config with format for Imagebitmap'); + } + const canvas = createCanvas(); + canvas.width = image.width; + canvas.height = image.height; + const pixels2DContext = createCanvasContext(canvas); + if (pixels2DContext != null) { + const height = image.height; + const width = image.width; + pixels2DContext.drawImage(image, 0, 0, width, height); + data = pixels2DContext.getImageData(0, 0, width, height).data; + bufferToTensorOptions.height = height; + bufferToTensorOptions.width = width; + return bufferToTensor(data, bufferToTensorOptions); + } + else { + throw new Error('Can not access image data'); + } + } + else if (isString) { + return new Promise((resolve, reject) => { + const canvas = createCanvas(); + const context = createCanvasContext(canvas); + if (!image || !context) { + return reject(); + } + const newImage = new Image(); + newImage.crossOrigin = 'Anonymous'; + newImage.src = image; + newImage.onload = () => { + canvas.width = newImage.width; + canvas.height = newImage.height; + context.drawImage(newImage, 0, 0, canvas.width, canvas.height); + const img = context.getImageData(0, 0, canvas.width, canvas.height); + bufferToTensorOptions.height = canvas.height; + bufferToTensorOptions.width = canvas.width; + resolve(bufferToTensor(img.data, bufferToTensorOptions)); + }; + }); + } + else { + throw new Error('Input data provided is not supported - aborted tensor creation'); + } + if (data !== undefined) { + return bufferToTensor(data, bufferToTensorOptions); + } + else { + throw new Error('Input data provided is not supported - aborted tensor creation'); + } +}; +/** + * implementation of Tensor.fromTexture(). + */ +const tensorFromTexture = (texture, options) => { + const { width, height, download, dispose } = options; + // Always assume RGBAF32. TODO: support different texture format + const dims = [1, height, width, 4]; + return new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor({ location: 'texture', type: 'float32', texture, dims, download, dispose }); +}; +/** + * implementation of Tensor.fromGpuBuffer(). + */ +const tensorFromGpuBuffer = (gpuBuffer, options) => { + const { dataType, dims, download, dispose } = options; + return new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor({ location: 'gpu-buffer', type: dataType ?? 'float32', gpuBuffer, dims, download, dispose }); +}; +/** + * implementation of Tensor.fromPinnedBuffer(). + */ +const tensorFromPinnedBuffer = (type, buffer, dims) => new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor({ location: 'cpu-pinned', type, data: buffer, dims: dims ?? [buffer.length] }); +//# sourceMappingURL=tensor-factory-impl.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/tensor-factory.js": +/*!********************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/tensor-factory.js ***! + \********************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +//# sourceMappingURL=tensor-factory.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/tensor-impl-type-mapping.js": +/*!******************************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/tensor-impl-type-mapping.js ***! + \******************************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP: () => (/* binding */ NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP), +/* harmony export */ NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP: () => (/* binding */ NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP), +/* harmony export */ checkTypedArray: () => (/* binding */ checkTypedArray) +/* harmony export */ }); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// a runtime map that maps type string to TypedArray constructor. Should match Tensor.DataTypeMap. +const NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP = new Map([ + ['float32', Float32Array], + ['uint8', Uint8Array], + ['int8', Int8Array], + ['uint16', Uint16Array], + ['int16', Int16Array], + ['int32', Int32Array], + ['bool', Uint8Array], + ['float64', Float64Array], + ['uint32', Uint32Array], +]); +// a runtime map that maps type string to TypedArray constructor. Should match Tensor.DataTypeMap. +const NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP = new Map([ + [Float32Array, 'float32'], + [Uint8Array, 'uint8'], + [Int8Array, 'int8'], + [Uint16Array, 'uint16'], + [Int16Array, 'int16'], + [Int32Array, 'int32'], + [Float64Array, 'float64'], + [Uint32Array, 'uint32'], +]); +// the following code allows delaying execution of BigInt/Float16Array checking. This allows lazy initialization for +// NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP and NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP, which allows BigInt/Float16Array +// polyfill if available. +let isTypedArrayChecked = false; +const checkTypedArray = () => { + if (!isTypedArrayChecked) { + isTypedArrayChecked = true; + const isBigInt64ArrayAvailable = typeof BigInt64Array !== 'undefined' && BigInt64Array.from; + const isBigUint64ArrayAvailable = typeof BigUint64Array !== 'undefined' && BigUint64Array.from; + const isFloat16ArrayAvailable = typeof Float16Array !== 'undefined' && Float16Array.from; + if (isBigInt64ArrayAvailable) { + NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.set('int64', BigInt64Array); + NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.set(BigInt64Array, 'int64'); + } + if (isBigUint64ArrayAvailable) { + NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.set('uint64', BigUint64Array); + NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.set(BigUint64Array, 'uint64'); + } + if (isFloat16ArrayAvailable) { + NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.set('float16', Float16Array); + NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.set(Float16Array, 'float16'); + } + else { + // if Float16Array is not available, use 'Uint16Array' to store the data. + NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.set('float16', Uint16Array); + } + } +}; +//# sourceMappingURL=tensor-impl-type-mapping.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/tensor-impl.js": +/*!*****************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/tensor-impl.js ***! + \*****************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ Tensor: () => (/* binding */ Tensor) +/* harmony export */ }); +/* harmony import */ var _tensor_conversion_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./tensor-conversion-impl.js */ "./node_modules/onnxruntime-common/dist/esm/tensor-conversion-impl.js"); +/* harmony import */ var _tensor_factory_impl_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./tensor-factory-impl.js */ "./node_modules/onnxruntime-common/dist/esm/tensor-factory-impl.js"); +/* harmony import */ var _tensor_impl_type_mapping_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./tensor-impl-type-mapping.js */ "./node_modules/onnxruntime-common/dist/esm/tensor-impl-type-mapping.js"); +/* harmony import */ var _tensor_utils_impl_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./tensor-utils-impl.js */ "./node_modules/onnxruntime-common/dist/esm/tensor-utils-impl.js"); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + + + + +/** + * the implementation of Tensor interface. + * + * @ignore + */ +class Tensor { + /** + * implementation. + */ + constructor(arg0, arg1, arg2) { + // perform one-time check for BigInt/Float16Array support + (0,_tensor_impl_type_mapping_js__WEBPACK_IMPORTED_MODULE_2__.checkTypedArray)(); + let type; + let dims; + if (typeof arg0 === 'object' && 'location' in arg0) { + // + // constructing tensor from specific location + // + this.dataLocation = arg0.location; + type = arg0.type; + dims = arg0.dims; + switch (arg0.location) { + case 'cpu-pinned': { + const expectedTypedArrayConstructor = _tensor_impl_type_mapping_js__WEBPACK_IMPORTED_MODULE_2__.NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.get(type); + if (!expectedTypedArrayConstructor) { + throw new TypeError(`unsupported type "${type}" to create tensor from pinned buffer`); + } + if (!(arg0.data instanceof expectedTypedArrayConstructor)) { + throw new TypeError(`buffer should be of type ${expectedTypedArrayConstructor.name}`); + } + this.cpuData = arg0.data; + break; + } + case 'texture': { + if (type !== 'float32') { + throw new TypeError(`unsupported type "${type}" to create tensor from texture`); + } + this.gpuTextureData = arg0.texture; + this.downloader = arg0.download; + this.disposer = arg0.dispose; + break; + } + case 'gpu-buffer': { + if ((type !== 'float32' && type !== 'float16' && type !== 'int32' && type !== 'int64' && type !== 'uint32' && + type !== 'uint8' && type !== 'bool')) { + throw new TypeError(`unsupported type "${type}" to create tensor from gpu buffer`); + } + this.gpuBufferData = arg0.gpuBuffer; + this.downloader = arg0.download; + this.disposer = arg0.dispose; + break; + } + default: + throw new Error(`Tensor constructor: unsupported location '${this.dataLocation}'`); + } + } + else { + // + // constructing tensor of location 'cpu' + // + let data; + let maybeDims; + // check whether arg0 is type or data + if (typeof arg0 === 'string') { + // + // Override: constructor(type, data, ...) + // + type = arg0; + maybeDims = arg2; + if (arg0 === 'string') { + // string tensor + if (!Array.isArray(arg1)) { + throw new TypeError('A string tensor\'s data must be a string array.'); + } + // we don't check whether every element in the array is string; this is too slow. we assume it's correct and + // error will be populated at inference + data = arg1; + } + else { + // numeric tensor + const typedArrayConstructor = _tensor_impl_type_mapping_js__WEBPACK_IMPORTED_MODULE_2__.NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.get(arg0); + if (typedArrayConstructor === undefined) { + throw new TypeError(`Unsupported tensor type: ${arg0}.`); + } + if (Array.isArray(arg1)) { + if (arg0 === 'float16' && typedArrayConstructor === Uint16Array) { + // When no Float16Array polyfill is used, we cannot create 'float16' tensor from number array. + // + // Throw error here because when user try to use number array as data, + // e.g. new Tensor('float16', [1, 2, 3, 4], dims)), it will actually call + // Uint16Array.from(arg1) which generates wrong data. + throw new TypeError('Creating a float16 tensor from number array is not supported. Please use Uint16Array as data.'); + } + else if (arg0 === 'uint64' || arg0 === 'int64') { + // use 'as any' here because: + // 1. TypeScript's check on type of 'Array.isArray()' does not work with readonly arrays. + // see https://github.com/microsoft/TypeScript/issues/17002 + // 2. TypeScript's check on union type of '(BigInt64ArrayConstructor|BigUint64ArrayConstructor).from()' + // does not accept parameter mapFn. + // 3. parameters of 'SupportedTypedArrayConstructors.from()' does not match the requirement of the union + // type. + // assume 'arg1' is of type "readonly number[]|readonly bigint[]" here. + // eslint-disable-next-line @typescript-eslint/no-explicit-any + data = typedArrayConstructor.from(arg1, BigInt); + } + else { + // assume 'arg1' is of type "readonly number[]" here. + // eslint-disable-next-line @typescript-eslint/no-explicit-any + data = typedArrayConstructor.from(arg1); + } + } + else if (arg1 instanceof typedArrayConstructor) { + data = arg1; + } + else { + throw new TypeError(`A ${type} tensor's data must be type of ${typedArrayConstructor}`); + } + } + } + else { + // + // Override: constructor(data, ...) + // + maybeDims = arg1; + if (Array.isArray(arg0)) { + // only boolean[] and string[] is supported + if (arg0.length === 0) { + throw new TypeError('Tensor type cannot be inferred from an empty array.'); + } + const firstElementType = typeof arg0[0]; + if (firstElementType === 'string') { + type = 'string'; + data = arg0; + } + else if (firstElementType === 'boolean') { + type = 'bool'; + // 'arg0' is of type 'boolean[]'. Uint8Array.from(boolean[]) actually works, but typescript thinks this is + // wrong type. We use 'as any' to make it happy. + // eslint-disable-next-line @typescript-eslint/no-explicit-any + data = Uint8Array.from(arg0); + } + else { + throw new TypeError(`Invalid element type of data array: ${firstElementType}.`); + } + } + else { + // get tensor type from TypedArray + const mappedType = _tensor_impl_type_mapping_js__WEBPACK_IMPORTED_MODULE_2__.NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.get(arg0.constructor); + if (mappedType === undefined) { + throw new TypeError(`Unsupported type for tensor data: ${arg0.constructor}.`); + } + type = mappedType; + data = arg0; + } + } + // type and data is processed, now processing dims + if (maybeDims === undefined) { + // assume 1-D tensor if dims omitted + maybeDims = [data.length]; + } + else if (!Array.isArray(maybeDims)) { + throw new TypeError('A tensor\'s dims must be a number array'); + } + dims = maybeDims; + this.cpuData = data; + this.dataLocation = 'cpu'; + } + // perform check on dims + const size = (0,_tensor_utils_impl_js__WEBPACK_IMPORTED_MODULE_3__.calculateSize)(dims); + // if data is on CPU, check whether data length matches tensor size + if (this.cpuData && size !== this.cpuData.length) { + throw new Error(`Tensor's size(${size}) does not match data length(${this.cpuData.length}).`); + } + this.type = type; + this.dims = dims; + this.size = size; + } + // #endregion + // #region factory + static async fromImage(image, options) { + return (0,_tensor_factory_impl_js__WEBPACK_IMPORTED_MODULE_1__.tensorFromImage)(image, options); + } + static fromTexture(texture, options) { + return (0,_tensor_factory_impl_js__WEBPACK_IMPORTED_MODULE_1__.tensorFromTexture)(texture, options); + } + static fromGpuBuffer(gpuBuffer, options) { + return (0,_tensor_factory_impl_js__WEBPACK_IMPORTED_MODULE_1__.tensorFromGpuBuffer)(gpuBuffer, options); + } + static fromPinnedBuffer(type, buffer, dims) { + return (0,_tensor_factory_impl_js__WEBPACK_IMPORTED_MODULE_1__.tensorFromPinnedBuffer)(type, buffer, dims); + } + // #endregion + // #region conversions + toDataURL(options) { + return (0,_tensor_conversion_impl_js__WEBPACK_IMPORTED_MODULE_0__.tensorToDataURL)(this, options); + } + toImageData(options) { + return (0,_tensor_conversion_impl_js__WEBPACK_IMPORTED_MODULE_0__.tensorToImageData)(this, options); + } + // #endregion + // #region properties + get data() { + this.ensureValid(); + if (!this.cpuData) { + throw new Error('The data is not on CPU. Use `getData()` to download GPU data to CPU, ' + + 'or use `texture` or `gpuBuffer` property to access the GPU data directly.'); + } + return this.cpuData; + } + get location() { + return this.dataLocation; + } + get texture() { + this.ensureValid(); + if (!this.gpuTextureData) { + throw new Error('The data is not stored as a WebGL texture.'); + } + return this.gpuTextureData; + } + get gpuBuffer() { + this.ensureValid(); + if (!this.gpuBufferData) { + throw new Error('The data is not stored as a WebGPU buffer.'); + } + return this.gpuBufferData; + } + // #endregion + // #region methods + async getData(releaseData) { + this.ensureValid(); + switch (this.dataLocation) { + case 'cpu': + case 'cpu-pinned': + return this.data; + case 'texture': + case 'gpu-buffer': { + if (!this.downloader) { + throw new Error('The current tensor is not created with a specified data downloader.'); + } + if (this.isDownloading) { + throw new Error('The current tensor is being downloaded.'); + } + try { + this.isDownloading = true; + const data = await this.downloader(); + this.downloader = undefined; + this.dataLocation = 'cpu'; + this.cpuData = data; + if (releaseData && this.disposer) { + this.disposer(); + this.disposer = undefined; + } + return data; + } + finally { + this.isDownloading = false; + } + } + default: + throw new Error(`cannot get data from location: ${this.dataLocation}`); + } + } + dispose() { + if (this.isDownloading) { + throw new Error('The current tensor is being downloaded.'); + } + if (this.disposer) { + this.disposer(); + this.disposer = undefined; + } + this.cpuData = undefined; + this.gpuTextureData = undefined; + this.gpuBufferData = undefined; + this.downloader = undefined; + this.isDownloading = undefined; + this.dataLocation = 'none'; + } + // #endregion + // #region tensor utilities + ensureValid() { + if (this.dataLocation === 'none') { + throw new Error('The tensor is disposed.'); + } + } + reshape(dims) { + this.ensureValid(); + if (this.downloader || this.disposer) { + throw new Error('Cannot reshape a tensor that owns GPU resource.'); + } + return (0,_tensor_utils_impl_js__WEBPACK_IMPORTED_MODULE_3__.tensorReshape)(this, dims); + } +} +//# sourceMappingURL=tensor-impl.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/tensor-utils-impl.js": +/*!***********************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/tensor-utils-impl.js ***! + \***********************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ calculateSize: () => (/* binding */ calculateSize), +/* harmony export */ tensorReshape: () => (/* binding */ tensorReshape) +/* harmony export */ }); +/* harmony import */ var _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./tensor-impl.js */ "./node_modules/onnxruntime-common/dist/esm/tensor-impl.js"); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +/** + * calculate size from dims. + * + * @param dims the dims array. May be an illegal input. + */ +const calculateSize = (dims) => { + let size = 1; + for (let i = 0; i < dims.length; i++) { + const dim = dims[i]; + if (typeof dim !== 'number' || !Number.isSafeInteger(dim)) { + throw new TypeError(`dims[${i}] must be an integer, got: ${dim}`); + } + if (dim < 0) { + throw new RangeError(`dims[${i}] must be a non-negative integer, got: ${dim}`); + } + size *= dim; + } + return size; +}; +/** + * implementation of Tensor.reshape() + */ +const tensorReshape = (tensor, dims) => { + switch (tensor.location) { + case 'cpu': + return new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor(tensor.type, tensor.data, dims); + case 'cpu-pinned': + return new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor({ + location: 'cpu-pinned', + data: tensor.data, + type: tensor.type, + dims, + }); + case 'texture': + return new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor({ + location: 'texture', + texture: tensor.texture, + type: tensor.type, + dims, + }); + case 'gpu-buffer': + return new _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor({ + location: 'gpu-buffer', + gpuBuffer: tensor.gpuBuffer, + type: tensor.type, + dims, + }); + default: + throw new Error(`tensorReshape: tensor location ${tensor.location} is not supported`); + } +}; +//# sourceMappingURL=tensor-utils-impl.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/tensor.js": +/*!************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/tensor.js ***! + \************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ Tensor: () => (/* binding */ Tensor) +/* harmony export */ }); +/* harmony import */ var _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./tensor-impl.js */ "./node_modules/onnxruntime-common/dist/esm/tensor-impl.js"); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// eslint-disable-next-line @typescript-eslint/naming-convention +const Tensor = _tensor_impl_js__WEBPACK_IMPORTED_MODULE_0__.Tensor; +//# sourceMappingURL=tensor.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/trace.js": +/*!***********************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/trace.js ***! + \***********************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ TRACE: () => (/* binding */ TRACE), +/* harmony export */ TRACE_FUNC_BEGIN: () => (/* binding */ TRACE_FUNC_BEGIN), +/* harmony export */ TRACE_FUNC_END: () => (/* binding */ TRACE_FUNC_END) +/* harmony export */ }); +/* harmony import */ var _env_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./env-impl.js */ "./node_modules/onnxruntime-common/dist/esm/env-impl.js"); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +/** + * @ignore + */ +const TRACE = (deviceType, label) => { + if (typeof _env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.trace === 'undefined' ? !_env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.wasm.trace : !_env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.trace) { + return; + } + // eslint-disable-next-line no-console + console.timeStamp(`${deviceType}::ORT::${label}`); +}; +const TRACE_FUNC = (msg, extraMsg) => { + const stack = new Error().stack?.split(/\r\n|\r|\n/g) || []; + let hasTraceFunc = false; + for (let i = 0; i < stack.length; i++) { + if (hasTraceFunc && !stack[i].includes('TRACE_FUNC')) { + let label = `FUNC_${msg}::${stack[i].trim().split(' ')[1]}`; + if (extraMsg) { + label += `::${extraMsg}`; + } + TRACE('CPU', label); + return; + } + if (stack[i].includes('TRACE_FUNC')) { + hasTraceFunc = true; + } + } +}; +/** + * @ignore + */ +const TRACE_FUNC_BEGIN = (extraMsg) => { + if (typeof _env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.trace === 'undefined' ? !_env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.wasm.trace : !_env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.trace) { + return; + } + TRACE_FUNC('BEGIN', extraMsg); +}; +/** + * @ignore + */ +const TRACE_FUNC_END = (extraMsg) => { + if (typeof _env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.trace === 'undefined' ? !_env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.wasm.trace : !_env_impl_js__WEBPACK_IMPORTED_MODULE_0__.env.trace) { + return; + } + TRACE_FUNC('END', extraMsg); +}; +//# sourceMappingURL=trace.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/training-session-impl.js": +/*!***************************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/training-session-impl.js ***! + \***************************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ TrainingSession: () => (/* binding */ TrainingSession) +/* harmony export */ }); +/* harmony import */ var _backend_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./backend-impl.js */ "./node_modules/onnxruntime-common/dist/esm/backend-impl.js"); +/* harmony import */ var _tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./tensor.js */ "./node_modules/onnxruntime-common/dist/esm/tensor.js"); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + + +const noBackendErrMsg = 'Training backend could not be resolved. ' + + 'Make sure you\'re using the correct configuration & WebAssembly files.'; +class TrainingSession { + constructor(handler, hasOptimizerModel, hasEvalModel) { + this.handler = handler; + this.hasOptimizerModel = hasOptimizerModel; + this.hasEvalModel = hasEvalModel; + } + get trainingInputNames() { + return this.handler.inputNames; + } + get trainingOutputNames() { + return this.handler.outputNames; + } + get evalInputNames() { + if (this.hasEvalModel) { + return this.handler.evalInputNames; + } + else { + throw new Error('This training session has no evalModel loaded.'); + } + } + get evalOutputNames() { + if (this.hasEvalModel) { + return this.handler.evalOutputNames; + } + else { + throw new Error('This training session has no evalModel loaded.'); + } + } + static async create(trainingOptions, sessionOptions) { + const evalModel = trainingOptions.evalModel || ''; + const optimizerModel = trainingOptions.optimizerModel || ''; + const options = sessionOptions || {}; + // resolve backend, update session options with validated EPs, and create session handler + const [backend, optionsWithValidatedEPs] = await (0,_backend_impl_js__WEBPACK_IMPORTED_MODULE_0__.resolveBackendAndExecutionProviders)(options); + if (backend.createTrainingSessionHandler) { + const handler = await backend.createTrainingSessionHandler(trainingOptions.checkpointState, trainingOptions.trainModel, evalModel, optimizerModel, optionsWithValidatedEPs); + return new TrainingSession(handler, !!trainingOptions.optimizerModel, !!trainingOptions.evalModel); + } + else { + throw new Error(noBackendErrMsg); + } + } + /** + * Helper function for runTrainStep and future runStep methods that handles the type-narrowing conversion from + * the given parameters to SessionHandler.FetchesType and RunOptions. + * + * @param inputNames the feeds object is checked that they contain all input names in the provided list of input + * names. + * @param outputNames the fetches object is checked that their keys match up with valid names in the list of output + * names. + * @param feeds the required input + * @param arg1 narrowed & converted into the SessionHandler.FetchesType or RunOptions object + * @param arg2 optional RunOptions object. + * @returns + */ + typeNarrowingForRunStep(inputNames, outputNames, feeds, arg1, arg2) { + const fetches = {}; + let options = {}; + // check inputs + if (typeof feeds !== 'object' || feeds === null || feeds instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor || Array.isArray(feeds)) { + throw new TypeError('\'feeds\' must be an object that use input names as keys and OnnxValue as corresponding values.'); + } + let isFetchesEmpty = true; + // determine which override is being used + if (typeof arg1 === 'object') { + if (arg1 === null) { + throw new TypeError('Unexpected argument[1]: cannot be null.'); + } + if (arg1 instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor) { + throw new TypeError('\'fetches\' cannot be a Tensor'); + } + if (Array.isArray(arg1)) { + if (arg1.length === 0) { + throw new TypeError('\'fetches\' cannot be an empty array.'); + } + isFetchesEmpty = false; + // output names + for (const name of arg1) { + if (typeof name !== 'string') { + throw new TypeError('\'fetches\' must be a string array or an object.'); + } + if (outputNames.indexOf(name) === -1) { + throw new RangeError(`'fetches' contains invalid output name: ${name}.`); + } + fetches[name] = null; + } + if (typeof arg2 === 'object' && arg2 !== null) { + options = arg2; + } + else if (typeof arg2 !== 'undefined') { + throw new TypeError('\'options\' must be an object.'); + } + } + else { + // decide whether arg1 is fetches or options + // if any output name is present and its value is valid OnnxValue, we consider it fetches + let isFetches = false; + const arg1Keys = Object.getOwnPropertyNames(arg1); + for (const name of outputNames) { + if (arg1Keys.indexOf(name) !== -1) { + const v = arg1[name]; + if (v === null || v instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor) { + isFetches = true; + isFetchesEmpty = false; + fetches[name] = v; + } + } + } + if (isFetches) { + if (typeof arg2 === 'object' && arg2 !== null) { + options = arg2; + } + else if (typeof arg2 !== 'undefined') { + throw new TypeError('\'options\' must be an object.'); + } + } + else { + options = arg1; + } + } + } + else if (typeof arg1 !== 'undefined') { + throw new TypeError('Unexpected argument[1]: must be \'fetches\' or \'options\'.'); + } + // check if all inputs are in feed + for (const name of inputNames) { + if (typeof feeds[name] === 'undefined') { + throw new Error(`input '${name}' is missing in 'feeds'.`); + } + } + // if no fetches is specified, we use the full output names list + if (isFetchesEmpty) { + for (const name of outputNames) { + fetches[name] = null; + } + } + return [fetches, options]; + } + /** + * Helper method for runTrainStep and any other runStep methods. Takes the ReturnType result from the SessionHandler + * and changes it into a map of Tensors. + * + * @param results + * @returns + */ + convertHandlerReturnTypeToMapOfTensors(results) { + const returnValue = {}; + for (const key in results) { + if (Object.hasOwnProperty.call(results, key)) { + const result = results[key]; + if (result instanceof _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor) { + returnValue[key] = result; + } + else { + returnValue[key] = new _tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor(result.type, result.data, result.dims); + } + } + } + return returnValue; + } + async lazyResetGrad() { + await this.handler.lazyResetGrad(); + } + async runTrainStep(feeds, arg1, arg2) { + const [fetches, options] = this.typeNarrowingForRunStep(this.trainingInputNames, this.trainingOutputNames, feeds, arg1, arg2); + const results = await this.handler.runTrainStep(feeds, fetches, options); + return this.convertHandlerReturnTypeToMapOfTensors(results); + } + async runOptimizerStep(options) { + if (this.hasOptimizerModel) { + await this.handler.runOptimizerStep(options || {}); + } + else { + throw new Error('This TrainingSession has no OptimizerModel loaded.'); + } + } + async runEvalStep(feeds, arg1, arg2) { + if (this.hasEvalModel) { + const [fetches, options] = this.typeNarrowingForRunStep(this.evalInputNames, this.evalOutputNames, feeds, arg1, arg2); + const results = await this.handler.runEvalStep(feeds, fetches, options); + return this.convertHandlerReturnTypeToMapOfTensors(results); + } + else { + throw new Error('This TrainingSession has no EvalModel loaded.'); + } + } + async getParametersSize(trainableOnly = true) { + return this.handler.getParametersSize(trainableOnly); + } + async loadParametersBuffer(array, trainableOnly = true) { + const paramsSize = await this.getParametersSize(trainableOnly); + // checking that the size of the Uint8Array is equivalent to the byte length of a Float32Array of the number + // of parameters + if (array.length !== 4 * paramsSize) { + throw new Error('Size of the buffer passed into loadParametersBuffer must match the number of parameters in ' + + 'the model. Please use getParametersSize method to check.'); + } + return this.handler.loadParametersBuffer(array, trainableOnly); + } + async getContiguousParameters(trainableOnly = true) { + return this.handler.getContiguousParameters(trainableOnly); + } + async release() { + return this.handler.dispose(); + } +} +//# sourceMappingURL=training-session-impl.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/training-session.js": +/*!**********************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/training-session.js ***! + \**********************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ TrainingSession: () => (/* binding */ TrainingSession) +/* harmony export */ }); +/* harmony import */ var _training_session_impl_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./training-session-impl.js */ "./node_modules/onnxruntime-common/dist/esm/training-session-impl.js"); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +// eslint-disable-next-line @typescript-eslint/naming-convention +const TrainingSession = _training_session_impl_js__WEBPACK_IMPORTED_MODULE_0__.TrainingSession; +//# sourceMappingURL=training-session.js.map + +/***/ }), + +/***/ "./node_modules/onnxruntime-common/dist/esm/version.js": +/*!*************************************************************!*\ + !*** ./node_modules/onnxruntime-common/dist/esm/version.js ***! + \*************************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ version: () => (/* binding */ version) +/* harmony export */ }); +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +// This file is generated by /js/scripts/update-version.ts +// Do not modify file content manually. +const version = '1.19.2'; +//# sourceMappingURL=version.js.map + +/***/ }), + +/***/ "./src/backends/onnx.js": +/*!******************************!*\ + !*** ./src/backends/onnx.js ***! + \******************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +var _onnxruntime_webgpu__WEBPACK_IMPORTED_MODULE_2___namespace_cache; +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ Tensor: () => (/* reexport safe */ onnxruntime_common__WEBPACK_IMPORTED_MODULE_3__.Tensor), +/* harmony export */ createInferenceSession: () => (/* binding */ createInferenceSession), +/* harmony export */ deviceToExecutionProviders: () => (/* binding */ deviceToExecutionProviders), +/* harmony export */ isONNXProxy: () => (/* binding */ isONNXProxy), +/* harmony export */ isONNXTensor: () => (/* binding */ isONNXTensor) +/* harmony export */ }); +/* harmony import */ var _env_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../env.js */ "./src/env.js"); +/* harmony import */ var onnxruntime_node__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! onnxruntime-node */ "onnxruntime-node"); +/* harmony import */ var _onnxruntime_webgpu__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! #onnxruntime-webgpu */ "?cb4d"); +/* harmony import */ var onnxruntime_common__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! onnxruntime-common */ "./node_modules/onnxruntime-common/dist/esm/index.js"); +/** + * @file Handler file for choosing the correct version of ONNX Runtime, based on the environment. + * Ideally, we could import the `onnxruntime-web` and `onnxruntime-node` packages only when needed, + * but dynamic imports don't seem to work with the current webpack version and/or configuration. + * This is possibly due to the experimental nature of top-level await statements. + * So, we just import both packages, and use the appropriate one based on the environment: + * - When running in node, we use `onnxruntime-node`. + * - When running in the browser, we use `onnxruntime-web` (`onnxruntime-node` is not bundled). + * + * This module is not directly exported, but can be accessed through the environment variables: + * ```javascript + * import { env } from '@huggingface/transformers'; + * console.log(env.backends.onnx); + * ``` + * + * @module backends/onnx + */ + + + +// NOTE: Import order matters here. We need to import `onnxruntime-node` before `onnxruntime-web`. +// In either case, we select the default export if it exists, otherwise we use the named export. + + +// Use subpath-imports to ensure Node.js and browser interoperability. +// See package.json and https://nodejs.org/api/packages.html#subpath-imports +// for more information. +// @ts-ignore + + + + +/** + * @typedef {import('onnxruntime-common').InferenceSession.ExecutionProviderConfig} ONNXExecutionProviders + */ + +/** @type {Record} */ +const DEVICE_TO_EXECUTION_PROVIDER_MAPPING = Object.freeze({ + auto: null, // Auto-detect based on device and environment + gpu: null, // Auto-detect GPU + cpu: 'cpu', // CPU + wasm: 'wasm', // WebAssembly + webgpu: 'webgpu', // WebGPU + cuda: 'cuda', // CUDA + dml: 'dml', // DirectML + + webnn: { name: 'webnn', deviceType: 'cpu' }, // WebNN (default) + 'webnn-npu': { name: 'webnn', deviceType: 'npu' }, // WebNN NPU + 'webnn-gpu': { name: 'webnn', deviceType: 'gpu' }, // WebNN GPU + 'webnn-cpu': { name: 'webnn', deviceType: 'cpu' }, // WebNN CPU +}); + +/** + * The list of supported devices, sorted by priority/performance. + * @type {import("../utils/devices.js").DeviceType[]} + */ +const supportedDevices = []; + +/** @type {ONNXExecutionProviders[]} */ +let defaultDevices; +let ONNX; +const ORT_SYMBOL = Symbol.for('onnxruntime'); + +if (ORT_SYMBOL in globalThis) { + // If the JS runtime exposes their own ONNX runtime, use it + ONNX = globalThis[ORT_SYMBOL]; + +} else if (_env_js__WEBPACK_IMPORTED_MODULE_0__.apis.IS_NODE_ENV) { + ONNX = onnxruntime_node__WEBPACK_IMPORTED_MODULE_1__["default"] ?? onnxruntime_node__WEBPACK_IMPORTED_MODULE_1__; + + // Updated as of ONNX Runtime 1.18.0 + // The following table lists the supported versions of ONNX Runtime Node.js binding provided with pre-built binaries. + // | EPs/Platforms | Windows x64 | Windows arm64 | Linux x64 | Linux arm64 | MacOS x64 | MacOS arm64 | + // | ------------- | ----------- | ------------- | ----------------- | ----------- | --------- | ----------- | + // | CPU | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | + // | DirectML | ✔️ | ✔️ | ❌ | ❌ | ❌ | ❌ | + // | CUDA | ❌ | ❌ | ✔️ (CUDA v11.8) | ❌ | ❌ | ❌ | + switch (process.platform) { + case 'win32': // Windows x64 and Windows arm64 + supportedDevices.push('dml'); + break; + case 'linux': // Linux x64 and Linux arm64 + if (process.arch === 'x64') { + supportedDevices.push('cuda'); + } + break; + case 'darwin': // MacOS x64 and MacOS arm64 + break; + } + + supportedDevices.push('cpu'); + defaultDevices = ['cpu']; +} else { + ONNX = /*#__PURE__*/ (_onnxruntime_webgpu__WEBPACK_IMPORTED_MODULE_2___namespace_cache || (_onnxruntime_webgpu__WEBPACK_IMPORTED_MODULE_2___namespace_cache = __webpack_require__.t(_onnxruntime_webgpu__WEBPACK_IMPORTED_MODULE_2__, 2))); + + if (_env_js__WEBPACK_IMPORTED_MODULE_0__.apis.IS_WEBNN_AVAILABLE) { + // TODO: Only push supported providers (depending on available hardware) + supportedDevices.push('webnn-npu', 'webnn-gpu', 'webnn-cpu', 'webnn'); + } + + if (_env_js__WEBPACK_IMPORTED_MODULE_0__.apis.IS_WEBGPU_AVAILABLE) { + supportedDevices.push('webgpu'); + } + + supportedDevices.push('wasm'); + defaultDevices = ['wasm']; +} + +// @ts-ignore +const InferenceSession = ONNX.InferenceSession; + +/** + * Map a device to the execution providers to use for the given device. + * @param {import("../utils/devices.js").DeviceType|"auto"|null} [device=null] (Optional) The device to run the inference on. + * @returns {ONNXExecutionProviders[]} The execution providers to use for the given device. + */ +function deviceToExecutionProviders(device = null) { + // Use the default execution providers if the user hasn't specified anything + if (!device) return defaultDevices; + + // Handle overloaded cases + switch (device) { + case "auto": + return supportedDevices; + case "gpu": + return supportedDevices.filter(x => + ["webgpu", "cuda", "dml", "webnn-gpu"].includes(x), + ); + } + + if (supportedDevices.includes(device)) { + return [DEVICE_TO_EXECUTION_PROVIDER_MAPPING[device] ?? device]; + } + + throw new Error(`Unsupported device: "${device}". Should be one of: ${supportedDevices.join(', ')}.`) +} + + +/** + * To prevent multiple calls to `initWasm()`, we store the first call in a Promise + * that is resolved when the first InferenceSession is created. Subsequent calls + * will wait for this Promise to resolve before creating their own InferenceSession. + * @type {Promise|null} + */ +let wasmInitPromise = null; + +/** + * Create an ONNX inference session. + * @param {Uint8Array} buffer The ONNX model buffer. + * @param {import('onnxruntime-common').InferenceSession.SessionOptions} session_options ONNX inference session options. + * @param {Object} session_config ONNX inference session configuration. + * @returns {Promise} The ONNX inference session. + */ +async function createInferenceSession(buffer, session_options, session_config) { + if (wasmInitPromise) { + // A previous session has already initialized the WASM runtime + // so we wait for it to resolve before creating this new session. + await wasmInitPromise; + } + + const sessionPromise = InferenceSession.create(buffer, session_options); + wasmInitPromise ??= sessionPromise; + const session = await sessionPromise; + session.config = session_config; + return session; +} + +/** + * Check if an object is an ONNX tensor. + * @param {any} x The object to check + * @returns {boolean} Whether the object is an ONNX tensor. + */ +function isONNXTensor(x) { + return x instanceof ONNX.Tensor; +} + +/** @type {import('onnxruntime-common').Env} */ +// @ts-ignore +const ONNX_ENV = ONNX?.env; +if (ONNX_ENV?.wasm) { + // Initialize wasm backend with suitable default settings. + + // (Optional) Set path to wasm files. This is needed when running in a web worker. + // https://onnxruntime.ai/docs/api/js/interfaces/Env.WebAssemblyFlags.html#wasmPaths + // We use remote wasm files by default to make it easier for newer users. + // In practice, users should probably self-host the necessary .wasm files. + ONNX_ENV.wasm.wasmPaths = `https://cdn.jsdelivr.net/npm/@huggingface/transformers@${_env_js__WEBPACK_IMPORTED_MODULE_0__.env.version}/dist/`; + + // TODO: Add support for loading WASM files from cached buffer when we upgrade to onnxruntime-web@1.19.0 + // https://github.com/microsoft/onnxruntime/pull/21534 + + // Users may wish to proxy the WASM backend to prevent the UI from freezing, + // However, this is not necessary when using WebGPU, so we default to false. + ONNX_ENV.wasm.proxy = false; + + // https://developer.mozilla.org/en-US/docs/Web/API/crossOriginIsolated + if (typeof crossOriginIsolated === 'undefined' || !crossOriginIsolated) { + ONNX_ENV.wasm.numThreads = 1; + } +} + +if (ONNX_ENV?.webgpu) { + ONNX_ENV.webgpu.powerPreference = 'high-performance'; +} + +/** + * Check if ONNX's WASM backend is being proxied. + * @returns {boolean} Whether ONNX's WASM backend is being proxied. + */ +function isONNXProxy() { + // TODO: Update this when allowing non-WASM backends. + return ONNX_ENV?.wasm?.proxy; +} + +// Expose ONNX environment variables to `env.backends.onnx` +_env_js__WEBPACK_IMPORTED_MODULE_0__.env.backends.onnx = ONNX_ENV; + + +/***/ }), + +/***/ "./src/configs.js": +/*!************************!*\ + !*** ./src/configs.js ***! + \************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ AutoConfig: () => (/* binding */ AutoConfig), +/* harmony export */ PretrainedConfig: () => (/* binding */ PretrainedConfig), +/* harmony export */ getKeyValueShapes: () => (/* binding */ getKeyValueShapes) +/* harmony export */ }); +/* harmony import */ var _utils_core_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./utils/core.js */ "./src/utils/core.js"); +/* harmony import */ var _utils_hub_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./utils/hub.js */ "./src/utils/hub.js"); + +/** + * @file Helper module for using model configs. For more information, see the corresponding + * [Python documentation](https://huggingface.co/docs/transformers/main/en/model_doc/auto#transformers.AutoConfig). + * + * **Example:** Load an `AutoConfig`. + * + * ```javascript + * import { AutoConfig } from '@huggingface/transformers'; + * const config = await AutoConfig.from_pretrained('bert-base-uncased'); + * console.log(config); + * // PretrainedConfig { + * // "model_type": "bert", + * // "is_encoder_decoder": false, + * // "architectures": [ + * // "BertForMaskedLM" + * // ], + * // "vocab_size": 30522 + * // "num_attention_heads": 12, + * // "num_hidden_layers": 12, + * // "hidden_size": 768, + * // "max_position_embeddings": 512, + * // ... + * // } + * ``` + * + * @module configs + */ + + + + +/** + * @typedef {import('./utils/hub.js').PretrainedOptions} PretrainedOptions + */ + + +/** + * Loads a config from the specified path. + * @param {string} pretrained_model_name_or_path The path to the config directory. + * @param {PretrainedOptions} options Additional options for loading the config. + * @returns {Promise} A promise that resolves with information about the loaded config. + */ +async function loadConfig(pretrained_model_name_or_path, options) { + return await (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_1__.getModelJSON)(pretrained_model_name_or_path, 'config.json', true, options); +} + +/** + * + * @param {PretrainedConfig} config + * @returns {Object} The normalized configuration. + */ +function getNormalizedConfig(config) { + const mapping = {}; + + let init_normalized_config = {}; + switch (config.model_type) { + // Sub-configs + case 'llava': + case 'paligemma': + case 'florence2': + init_normalized_config = getNormalizedConfig(config.text_config); + break; + case 'moondream1': + init_normalized_config = getNormalizedConfig(config.phi_config); + break; + case 'musicgen': + init_normalized_config = getNormalizedConfig(config.decoder); + break; + + // Decoder-only models + case 'gpt2': + case 'gptj': + case 'jais': + case 'codegen': + case 'gpt_bigcode': + mapping['num_heads'] = 'n_head'; + mapping['num_layers'] = 'n_layer'; + mapping['hidden_size'] = 'n_embd'; + break; + case 'gpt_neox': + case 'stablelm': + case 'opt': + case 'phi': + case 'phi3': + case 'falcon': + mapping['num_heads'] = 'num_attention_heads'; + mapping['num_layers'] = 'num_hidden_layers'; + mapping['hidden_size'] = 'hidden_size'; + break; + case 'llama': + case 'olmo': + case 'mobilellm': + case 'granite': + case 'cohere': + case 'mistral': + case 'starcoder2': + case 'qwen2': + mapping['num_heads'] = 'num_key_value_heads'; + mapping['num_layers'] = 'num_hidden_layers'; + mapping['hidden_size'] = 'hidden_size'; + mapping['num_attention_heads'] = 'num_attention_heads'; + break; + case 'gemma': + case 'gemma2': + mapping['num_heads'] = 'num_key_value_heads'; + mapping['num_layers'] = 'num_hidden_layers'; + mapping['dim_kv'] = 'head_dim'; + break; + case 'openelm': + mapping['num_heads'] = 'num_kv_heads'; + mapping['num_layers'] = 'num_transformer_layers'; + mapping['dim_kv'] = 'head_dim'; + break; + case 'gpt_neo': + case 'donut-swin': + mapping['num_heads'] = 'num_heads'; + mapping['num_layers'] = 'num_layers'; + mapping['hidden_size'] = 'hidden_size'; + break; + case 'bloom': + mapping['num_heads'] = 'n_head'; + mapping['num_layers'] = 'n_layer'; + mapping['hidden_size'] = 'hidden_size'; + break; + case 'mpt': + mapping['num_heads'] = 'n_heads'; + mapping['num_layers'] = 'n_layers'; + mapping['hidden_size'] = 'd_model'; + break; + + // Encoder-decoder models + case 't5': + case 'mt5': + case 'longt5': + mapping['num_decoder_layers'] = 'num_decoder_layers'; + mapping['num_decoder_heads'] = 'num_heads'; + mapping['decoder_dim_kv'] = 'd_kv'; + mapping['num_encoder_layers'] = 'num_layers'; + mapping['num_encoder_heads'] = 'num_heads'; + mapping['encoder_dim_kv'] = 'd_kv'; + break; + case 'bart': + case 'mbart': + case 'marian': + case 'whisper': + case 'm2m_100': + case 'blenderbot': + case 'blenderbot-small': + case 'florence2_language': + mapping['num_decoder_layers'] = 'decoder_layers'; + mapping['num_decoder_heads'] = 'decoder_attention_heads'; + mapping['decoder_hidden_size'] = 'd_model'; + mapping['num_encoder_layers'] = 'encoder_layers'; + mapping['num_encoder_heads'] = 'encoder_attention_heads'; + mapping['encoder_hidden_size'] = 'd_model'; + break; + case 'speecht5': + mapping['num_decoder_layers'] = 'decoder_layers'; + mapping['num_decoder_heads'] = 'decoder_attention_heads'; + mapping['decoder_hidden_size'] = 'hidden_size'; + mapping['num_encoder_layers'] = 'encoder_layers'; + mapping['num_encoder_heads'] = 'encoder_attention_heads'; + mapping['encoder_hidden_size'] = 'hidden_size'; + break; + case 'trocr': + mapping['num_encoder_layers'] = mapping['num_decoder_layers'] = 'decoder_layers'; + mapping['num_encoder_heads'] = mapping['num_decoder_heads'] = 'decoder_attention_heads'; + mapping['encoder_hidden_size'] = mapping['decoder_hidden_size'] = 'd_model'; + break; + case 'musicgen_decoder': + mapping['num_encoder_layers'] = mapping['num_decoder_layers'] = 'num_hidden_layers'; + mapping['num_encoder_heads'] = mapping['num_decoder_heads'] = 'num_attention_heads'; + mapping['encoder_hidden_size'] = mapping['decoder_hidden_size'] = 'hidden_size'; + break; + + case 'vision-encoder-decoder': + const decoderConfig = getNormalizedConfig(config.decoder); + + const add_encoder_pkv = 'num_decoder_layers' in decoderConfig; + const result = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_0__.pick)(config, ['model_type', 'is_encoder_decoder']); + if (add_encoder_pkv) { + // Decoder is part of an encoder-decoder model + result.num_decoder_layers = decoderConfig.num_decoder_layers; + result.num_decoder_heads = decoderConfig.num_decoder_heads; + result.decoder_hidden_size = decoderConfig.decoder_hidden_size; + + result.num_encoder_layers = decoderConfig.num_encoder_layers; + result.num_encoder_heads = decoderConfig.num_encoder_heads; + result.encoder_hidden_size = decoderConfig.encoder_hidden_size; + } else { + // Decoder is a decoder-only model + result.num_layers = decoderConfig.num_layers; + result.num_heads = decoderConfig.num_heads; + result.hidden_size = decoderConfig.hidden_size; + } + return result; + + } + + // NOTE: If `num_attention_heads` is not set, it is assumed to be equal to `num_heads` + const normalized_config = { + ...init_normalized_config, + ...(0,_utils_core_js__WEBPACK_IMPORTED_MODULE_0__.pick)(config, ['model_type', 'multi_query', 'is_encoder_decoder']), + }; + for (const key in mapping) { + normalized_config[key] = config[mapping[key]]; + } + return normalized_config; +} + +/** + * + * @param {PretrainedConfig} config + * @returns {Record} + */ +function getKeyValueShapes(config, { + prefix = 'past_key_values', +} = {}) { + /** @type {Record} */ + const decoderFeeds = {}; + const normalized_config = config.normalized_config; + + // TODO support batches (i.e., batch_size > 1) + const batch_size = 1; + + if (normalized_config.is_encoder_decoder && ( + 'num_encoder_heads' in normalized_config && 'num_decoder_heads' in normalized_config + )) { + const encoder_dim_kv = normalized_config.encoder_dim_kv ?? ( + normalized_config.encoder_hidden_size / normalized_config.num_encoder_heads + ); + const decoder_dim_kv = normalized_config.decoder_dim_kv ?? ( + normalized_config.decoder_hidden_size / normalized_config.num_decoder_heads + ); + + const encoder_dims = [batch_size, normalized_config.num_encoder_heads, 0, encoder_dim_kv]; + const decoder_dims = [batch_size, normalized_config.num_decoder_heads, 0, decoder_dim_kv]; + for (let i = 0; i < normalized_config.num_decoder_layers; ++i) { + decoderFeeds[`${prefix}.${i}.encoder.key`] = encoder_dims; + decoderFeeds[`${prefix}.${i}.encoder.value`] = encoder_dims; + decoderFeeds[`${prefix}.${i}.decoder.key`] = decoder_dims; + decoderFeeds[`${prefix}.${i}.decoder.value`] = decoder_dims; + } + } else { // Decoders + const num_heads = normalized_config.num_heads; + const num_layers = normalized_config.num_layers; + const dim_kv = normalized_config.dim_kv ?? ( + normalized_config.hidden_size / + (normalized_config.num_attention_heads ?? num_heads) + ); + + if (normalized_config.model_type === 'falcon') { + // NOTE: Custom implementation for Falcon + const dims = [batch_size * num_heads, 0, dim_kv] + for (let i = 0; i < num_layers; ++i) { + decoderFeeds[`${prefix}.${i}.key`] = dims; + decoderFeeds[`${prefix}.${i}.value`] = dims; + } + } else if (normalized_config.multi_query) { // e.g., for `gpt_bigcode` + const dims = [batch_size * num_heads, 0, 2 * dim_kv] + + for (let i = 0; i < num_layers; ++i) { + decoderFeeds[`${prefix}.${i}.key_value`] = dims; + } + } else if (normalized_config.model_type === 'bloom') { + // NOTE: Custom implementation for Bloom + + const keyDims = [batch_size * num_heads, dim_kv, 0] // [batch_size x num_heads,64,past_sequence_length] + const valueDims = [batch_size * num_heads, 0, dim_kv] // [batch_size x num_heads,past_sequence_length,64] + for (let i = 0; i < num_layers; ++i) { + decoderFeeds[`${prefix}.${i}.key`] = keyDims; + decoderFeeds[`${prefix}.${i}.value`] = valueDims; + } + } else if (normalized_config.model_type === 'openelm') { + for (let i = 0; i < num_layers; ++i) { + const dims = [batch_size, num_heads[i], 0, dim_kv] + + decoderFeeds[`${prefix}.${i}.key`] = dims; + decoderFeeds[`${prefix}.${i}.value`] = dims; + } + } else { // Decoder-only + const dims = [batch_size, num_heads, 0, dim_kv] + for (let i = 0; i < num_layers; ++i) { + decoderFeeds[`${prefix}.${i}.key`] = dims; + decoderFeeds[`${prefix}.${i}.value`] = dims; + } + } + } + + return decoderFeeds; +} +/** + * Base class for all configuration classes. For more information, see the corresponding + * [Python documentation](https://huggingface.co/docs/transformers/main/en/main_classes/configuration#transformers.PretrainedConfig). + */ +class PretrainedConfig { + // NOTE: Typo in original + + /** @type {string|null} */ + model_type = null; + + /** @type {boolean} */ + is_encoder_decoder = false; + + /** @type {number} */ + max_position_embeddings; + + /** @type {TransformersJSConfig} */ + 'transformers.js_config'; + + /** + * Create a new PreTrainedTokenizer instance. + * @param {Object} configJSON The JSON of the config. + */ + constructor(configJSON) { + Object.assign(this, configJSON); + this.normalized_config = getNormalizedConfig(this); + } + + /** + * Loads a pre-trained config from the given `pretrained_model_name_or_path`. + * + * @param {string} pretrained_model_name_or_path The path to the pre-trained config. + * @param {PretrainedOptions} options Additional options for loading the config. + * @throws {Error} Throws an error if the config.json is not found in the `pretrained_model_name_or_path`. + * + * @returns {Promise} A new instance of the `PretrainedConfig` class. + */ + static async from_pretrained(pretrained_model_name_or_path, { + progress_callback = null, + config = null, + cache_dir = null, + local_files_only = false, + revision = 'main', + } = {}) { + if (config && !(config instanceof PretrainedConfig)) { + config = new PretrainedConfig(config); + } + + const data = config ?? await loadConfig(pretrained_model_name_or_path, { + progress_callback, + config, + cache_dir, + local_files_only, + revision, + }) + return new this(data); + } +} + +/** + * Helper class which is used to instantiate pretrained configs with the `from_pretrained` function. + * + * @example + * const config = await AutoConfig.from_pretrained('Xenova/bert-base-uncased'); + */ +class AutoConfig { + /** @type {typeof PretrainedConfig.from_pretrained} */ + static async from_pretrained(...args) { + return PretrainedConfig.from_pretrained(...args); + } +} + +/** + * Transformers.js-specific configuration, possibly present in config.json under the key `transformers.js_config`. + * @typedef {Object} TransformersJSConfig + * @property {import('./utils/tensor.js').DataType|Record} [kv_cache_dtype] The data type of the key-value cache. + * @property {Record} [free_dimension_overrides] Override the free dimensions of the model. + * See https://onnxruntime.ai/docs/tutorials/web/env-flags-and-session-options.html#freedimensionoverrides + * for more information. + * @property {import('./utils/devices.js').DeviceType} [device] The default device to use for the model. + * @property {import('./utils/dtypes.js').DataType} [dtype] The default data type to use for the model. + * @property {boolean|Record} [use_external_data_format=false] Whether to load the model using the external data format (used for models >= 2GB in size). + */ + + +/***/ }), + +/***/ "./src/env.js": +/*!********************!*\ + !*** ./src/env.js ***! + \********************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ apis: () => (/* binding */ apis), +/* harmony export */ env: () => (/* binding */ env) +/* harmony export */ }); +/* harmony import */ var fs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! fs */ "fs"); +/* harmony import */ var path__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! path */ "path"); +/* harmony import */ var url__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! url */ "url"); +/** + * @file Module used to configure Transformers.js. + * + * **Example:** Disable remote models. + * ```javascript + * import { env } from '@huggingface/transformers'; + * env.allowRemoteModels = false; + * ``` + * + * **Example:** Set local model path. + * ```javascript + * import { env } from '@huggingface/transformers'; + * env.localModelPath = '/path/to/local/models/'; + * ``` + * + * **Example:** Set cache directory. + * ```javascript + * import { env } from '@huggingface/transformers'; + * env.cacheDir = '/path/to/cache/directory/'; + * ``` + * + * @module env + */ + + + + + +const VERSION = '3.0.2'; + +// Check if various APIs are available (depends on environment) +const IS_BROWSER_ENV = typeof self !== 'undefined'; +const IS_WEBWORKER_ENV = IS_BROWSER_ENV && self.constructor.name === 'DedicatedWorkerGlobalScope'; +const IS_WEB_CACHE_AVAILABLE = IS_BROWSER_ENV && 'caches' in self; +const IS_WEBGPU_AVAILABLE = typeof navigator !== 'undefined' && 'gpu' in navigator; +const IS_WEBNN_AVAILABLE = typeof navigator !== 'undefined' && 'ml' in navigator; + +const IS_PROCESS_AVAILABLE = typeof process !== 'undefined'; +const IS_NODE_ENV = IS_PROCESS_AVAILABLE && process?.release?.name === 'node'; +const IS_FS_AVAILABLE = !isEmpty(fs__WEBPACK_IMPORTED_MODULE_0__["default"]); +const IS_PATH_AVAILABLE = !isEmpty(path__WEBPACK_IMPORTED_MODULE_1__["default"]); + +/** + * A read-only object containing information about the APIs available in the current environment. + */ +const apis = Object.freeze({ + /** Whether we are running in a browser environment */ + IS_BROWSER_ENV, + + /** Whether we are running in a web worker environment */ + IS_WEBWORKER_ENV, + + /** Whether the Cache API is available */ + IS_WEB_CACHE_AVAILABLE, + + /** Whether the WebGPU API is available */ + IS_WEBGPU_AVAILABLE, + + /** Whether the WebNN API is available */ + IS_WEBNN_AVAILABLE, + + /** Whether the Node.js process API is available */ + IS_PROCESS_AVAILABLE, + + /** Whether we are running in a Node.js environment */ + IS_NODE_ENV, + + /** Whether the filesystem API is available */ + IS_FS_AVAILABLE, + + /** Whether the path API is available */ + IS_PATH_AVAILABLE, +}); + +const RUNNING_LOCALLY = IS_FS_AVAILABLE && IS_PATH_AVAILABLE; + +let dirname__ = './'; +if (RUNNING_LOCALLY) { + // NOTE: We wrap `import.meta` in a call to `Object` to prevent Webpack from trying to bundle it in CommonJS. + // Although we get the warning: "Accessing import.meta directly is unsupported (only property access or destructuring is supported)", + // it is safe to ignore since the bundled value (`{}`) isn't used for CommonJS environments (we use __dirname instead). + const _import_meta_url = Object(import.meta).url; + + if (_import_meta_url) { + dirname__ = path__WEBPACK_IMPORTED_MODULE_1__["default"].dirname(path__WEBPACK_IMPORTED_MODULE_1__["default"].dirname(url__WEBPACK_IMPORTED_MODULE_2__["default"].fileURLToPath(_import_meta_url))) // ESM + } else if (typeof __dirname !== 'undefined') { + dirname__ = path__WEBPACK_IMPORTED_MODULE_1__["default"].dirname(__dirname) // CommonJS + } +} + +// Only used for environments with access to file system +const DEFAULT_CACHE_DIR = RUNNING_LOCALLY + ? path__WEBPACK_IMPORTED_MODULE_1__["default"].join(dirname__, '/.cache/') + : null; + +// Set local model path, based on available APIs +const DEFAULT_LOCAL_MODEL_PATH = '/models/'; +const localModelPath = RUNNING_LOCALLY + ? path__WEBPACK_IMPORTED_MODULE_1__["default"].join(dirname__, DEFAULT_LOCAL_MODEL_PATH) + : DEFAULT_LOCAL_MODEL_PATH; + +/** + * Global variable given visible to users to control execution. This provides users a simple way to configure Transformers.js. + * @typedef {Object} TransformersEnvironment + * @property {string} version This version of Transformers.js. + * @property {{onnx: Partial}} backends Expose environment variables of different backends, + * allowing users to set these variables if they want to. + * @property {boolean} allowRemoteModels Whether to allow loading of remote files, defaults to `true`. + * If set to `false`, it will have the same effect as setting `local_files_only=true` when loading pipelines, models, tokenizers, processors, etc. + * @property {string} remoteHost Host URL to load models from. Defaults to the Hugging Face Hub. + * @property {string} remotePathTemplate Path template to fill in and append to `remoteHost` when loading models. + * @property {boolean} allowLocalModels Whether to allow loading of local files, defaults to `false` if running in-browser, and `true` otherwise. + * If set to `false`, it will skip the local file check and try to load the model from the remote host. + * @property {string} localModelPath Path to load local models from. Defaults to `/models/`. + * @property {boolean} useFS Whether to use the file system to load files. By default, it is `true` if available. + * @property {boolean} useBrowserCache Whether to use Cache API to cache models. By default, it is `true` if available. + * @property {boolean} useFSCache Whether to use the file system to cache files. By default, it is `true` if available. + * @property {string} cacheDir The directory to use for caching files with the file system. By default, it is `./.cache`. + * @property {boolean} useCustomCache Whether to use a custom cache system (defined by `customCache`), defaults to `false`. + * @property {Object} customCache The custom cache to use. Defaults to `null`. Note: this must be an object which + * implements the `match` and `put` functions of the Web Cache API. For more information, see https://developer.mozilla.org/en-US/docs/Web/API/Cache + */ + +/** @type {TransformersEnvironment} */ +const env = { + version: VERSION, + + /////////////////// Backends settings /////////////////// + // NOTE: These will be populated later by the backends themselves. + backends: { + // onnxruntime-web/onnxruntime-node + onnx: {}, + }, + + /////////////////// Model settings /////////////////// + allowRemoteModels: true, + remoteHost: 'https://huggingface.co/', + remotePathTemplate: '{model}/resolve/{revision}/', + + allowLocalModels: !IS_BROWSER_ENV, + localModelPath: localModelPath, + useFS: IS_FS_AVAILABLE, + + /////////////////// Cache settings /////////////////// + useBrowserCache: IS_WEB_CACHE_AVAILABLE, + + useFSCache: IS_FS_AVAILABLE, + cacheDir: DEFAULT_CACHE_DIR, + + useCustomCache: false, + customCache: null, + ////////////////////////////////////////////////////// +} + + +/** + * @param {Object} obj + * @private + */ +function isEmpty(obj) { + return Object.keys(obj).length === 0; +} + + + +/***/ }), + +/***/ "./src/generation/configuration_utils.js": +/*!***********************************************!*\ + !*** ./src/generation/configuration_utils.js ***! + \***********************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ GenerationConfig: () => (/* binding */ GenerationConfig) +/* harmony export */ }); +/* harmony import */ var _utils_core_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../utils/core.js */ "./src/utils/core.js"); + +/** + * @module generation/configuration_utils + */ + + + +/** + * Class that holds a configuration for a generation task. + */ +class GenerationConfig { + // Parameters that control the length of the output + /** + * The maximum length the generated tokens can have. + * Corresponds to the length of the input prompt + `max_new_tokens`. + * Its effect is overridden by `max_new_tokens`, if also set. + * @type {number} + * @default 20 + */ + max_length = 20; + + /** + * The maximum numbers of tokens to generate, ignoring the number of tokens in the prompt. + * @type {number} + * @default null + */ + max_new_tokens = null; + + /** + * The minimum length of the sequence to be generated. + * Corresponds to the length of the input prompt + `min_new_tokens`. + * Its effect is overridden by `min_new_tokens`, if also set. + * @type {number} + * @default 0 + */ + min_length = 0; + + /** + * The minimum numbers of tokens to generate, ignoring the number of tokens in the prompt. + * @type {number} + * @default null + */ + min_new_tokens = null; + + /** + * Controls the stopping condition for beam-based methods, like beam-search. It accepts the following values: + * - `true`, where the generation stops as soon as there are `num_beams` complete candidates; + * - `false`, where an heuristic is applied and the generation stops when is it very unlikely to find better candidates; + * - `"never"`, where the beam search procedure only stops when there cannot be better candidates (canonical beam search algorithm). + * @type {boolean|"never"} + * @default false + */ + early_stopping = false; + + /** + * The maximum amount of time you allow the computation to run for in seconds. + * Generation will still finish the current pass after allocated time has been passed. + * @type {number} + * @default null + */ + max_time = null; + + // Parameters that control the generation strategy used + /** + * Whether or not to use sampling; use greedy decoding otherwise. + * @type {boolean} + * @default false + */ + do_sample = false; + + /** + * Number of beams for beam search. 1 means no beam search. + * @type {number} + * @default 1 + */ + num_beams = 1; + + /** + * Number of groups to divide `num_beams` into in order to ensure diversity among different groups of beams. + * See [this paper](https://arxiv.org/pdf/1610.02424.pdf) for more details. + * @type {number} + * @default 1 + */ + num_beam_groups = 1; + + /** + * The values balance the model confidence and the degeneration penalty in contrastive search decoding. + * @type {number} + * @default null + */ + penalty_alpha = null; + + /** + * Whether or not the model should use the past last key/values attentions (if applicable to the model) to speed up decoding. + * @type {boolean} + * @default true + */ + use_cache = true; + + // Parameters for manipulation of the model output logits + /** + * The value used to modulate the next token probabilities. + * @type {number} + * @default 1.0 + */ + temperature = 1.0; + + /** + * The number of highest probability vocabulary tokens to keep for top-k-filtering. + * @type {number} + * @default 50 + */ + top_k = 50; + + /** + * If set to float < 1, only the smallest set of most probable tokens with probabilities that add up to `top_p` or higher are kept for generation. + * @type {number} + * @default 1.0 + */ + top_p = 1.0; + + /** + * Local typicality measures how similar the conditional probability of predicting a target token next is to the expected conditional probability of predicting a random token next, given the partial text already generated. + * If set to float < 1, the smallest set of the most locally typical tokens with probabilities that add up to `typical_p` or higher are kept for generation. + * See [this paper](https://arxiv.org/pdf/2202.00666.pdf) for more details. + * @type {number} + * @default 1.0 + */ + typical_p = 1.0; + + /** + * If set to float strictly between 0 and 1, only tokens with a conditional probability greater than `epsilon_cutoff` will be sampled. + * In the paper, suggested values range from 3e-4 to 9e-4, depending on the size of the model. + * See [Truncation Sampling as Language Model Desmoothing](https://arxiv.org/abs/2210.15191) for more details. + * @type {number} + * @default 0.0 + */ + epsilon_cutoff = 0.0; + + /** + * Eta sampling is a hybrid of locally typical sampling and epsilon sampling. + * If set to float strictly between 0 and 1, a token is only considered if it is greater than either `eta_cutoff` or `sqrt(eta_cutoff) * exp(-entropy(softmax(next_token_logits)))`. + * The latter term is intuitively the expected next token probability, scaled by `sqrt(eta_cutoff)`. In the paper, suggested values range from 3e-4 to 2e-3, depending on the size of the model. + * See [Truncation Sampling as Language Model Desmoothing](https://arxiv.org/abs/2210.15191) for more details. + * @type {number} + * @default 0.0 + */ + eta_cutoff = 0.0; + + /** + * This value is subtracted from a beam's score if it generates a token same as any beam from other group at a particular time. + * Note that `diversity_penalty` is only effective if `group beam search` is enabled. + * @type {number} + * @default 0.0 + */ + diversity_penalty = 0.0; + + /** + * The parameter for repetition penalty. 1.0 means no penalty. + * See [this paper](https://arxiv.org/pdf/1909.05858.pdf) for more details. + * @type {number} + * @default 1.0 + */ + repetition_penalty = 1.0; + + /** + * The paramater for encoder_repetition_penalty. + * An exponential penalty on sequences that are not in the original input. + * 1.0 means no penalty. + * @type {number} + * @default 1.0 + */ + encoder_repetition_penalty = 1.0; + + /** + * Exponential penalty to the length that is used with beam-based generation. + * It is applied as an exponent to the sequence length, which in turn is used to divide the score of the sequence. + * Since the score is the log likelihood of the sequence (i.e. negative), `length_penalty` > 0.0 promotes longer sequences, while `length_penalty` < 0.0 encourages shorter sequences. + * @type {number} + * @default 1.0 + */ + length_penalty = 1.0; + + /** + * If set to int > 0, all ngrams of that size can only occur once. + * @type {number} + * @default 0 + */ + no_repeat_ngram_size = 0; + + /** + * List of token ids that are not allowed to be generated. + * In order to get the token ids of the words that should not appear in the generated text, use + * `tokenizer(bad_words, { add_prefix_space: true, add_special_tokens: false }).input_ids`. + * @type {number[][]} + * @default null + */ + bad_words_ids = null; + + /** + * List of token ids that must be generated. + * If given a `number[][]`, this is treated as a simple list of words that must be included, the opposite to `bad_words_ids`. + * If given `number[][][]`, this triggers a [disjunctive constraint](https://github.com/huggingface/transformers/issues/14081), where one can allow different forms of each word. + * @type {number[][]|number[][][]} + * @default null + */ + force_words_ids = null; + + /** + * Whether to renormalize the logits after applying all the logits processors or warpers (including the custom ones). + * It's highly recommended to set this flag to `true` as the search algorithms suppose the score logits are normalized but some logit processors or warpers break the normalization. + * @type {boolean} + * @default false + */ + renormalize_logits = false; + + /** + * Custom constraints that can be added to the generation to ensure that the output will contain the use of certain tokens as defined by `Constraint` objects, in the most sensible way possible. + * @type {Object[]} + * @default null + */ + constraints = null; + + /** + * The id of the token to force as the first generated token after the `decoder_start_token_id`. + * Useful for multilingual models like mBART where the first generated token needs to be the target language token. + * @type {number} + * @default null + */ + forced_bos_token_id = null; + + /** + * The id of the token to force as the last generated token when `max_length` is reached. + * Optionally, use a list to set multiple *end-of-sequence* tokens. + * @type {number|number[]} + * @default null + */ + forced_eos_token_id = null; + + /** + * Whether to remove possible *nan* and *inf* outputs of the model to prevent the generation method to crash. Note that using `remove_invalid_values` can slow down generation. + * @type {boolean} + */ + remove_invalid_values = false; + + /** + * This Tuple adds an exponentially increasing length penalty, after a certain amount of tokens have been generated. + * The tuple shall consist of: `(start_index, decay_factor)` where `start_index` indicates where penalty starts and `decay_factor` represents the factor of exponential decay. + * @type {[number, number]} + * @default null + */ + exponential_decay_length_penalty = null; + + /** + * A list of tokens that will be suppressed at generation. + * The `SuppressTokens` logit processor will set their log probs to `-inf` so that they are not sampled. + * @type {number[]} + * @default null + */ + suppress_tokens = null; + + /** + * A list of tokens that will be suppressed at the beginning of the generation. + * The `SuppressBeginTokens` logit processor will set their log probs to `-inf` so that they are not sampled. + * @type {number[]} + * @default null + */ + begin_suppress_tokens = null; + + /** + * A list of pairs of integers which indicates a mapping from generation indices to token indices that will be forced before sampling. + * For example, `[[1, 123]]` means the second generated token will always be a token of index 123. + * @type {[number, number][]} + * @default null + */ + forced_decoder_ids = null; + + /** + * The guidance scale for classifier free guidance (CFG). CFG is enabled by setting `guidance_scale > 1`. + * Higher guidance scale encourages the model to generate samples that are more closely linked to the input + * prompt, usually at the expense of poorer quality. + * @type {number} + * @default null + */ + guidance_scale = null; + + // Parameters that define the output variables of `generate` + /** + * The number of independently computed returned sequences for each element in the batch. + * @type {number} + * @default 1 + */ + num_return_sequences = 1; + + /** + * Whether or not to return the attentions tensors of all attention layers. + * See `attentions` under returned tensors for more details. + * @type {boolean} + * @default false + */ + output_attentions = false; + + /** + * Whether or not to return the hidden states of all layers. + * See `hidden_states` under returned tensors for more details. + * @type {boolean} + * @default false + */ + output_hidden_states = false; + + /** + * Whether or not to return the prediction scores. + * See `scores` under returned tensors for more details. + * @type {boolean} + * @default false + */ + output_scores = false; + + /** + * Whether or not to return a `ModelOutput` instead of a plain tuple. + * @type {boolean} + * @default false + */ + return_dict_in_generate = false; + + // Special tokens that can be used at generation time + /** + * The id of the *padding* token. + * @type {number} + * @default null + */ + pad_token_id = null; + + /** + * The id of the *beginning-of-sequence* token. + * @type {number} + * @default null + */ + bos_token_id = null; + + /** + * The id of the *end-of-sequence* token. + * Optionally, use a list to set multiple *end-of-sequence* tokens. + * @type {number|number[]} + * @default null + */ + eos_token_id = null; + + // Generation parameters exclusive to encoder-decoder models + /** + * If set to int > 0, all ngrams of that size that occur in the `encoder_input_ids` cannot occur in the `decoder_input_ids`. + * @type {number} + * @default 0 + */ + encoder_no_repeat_ngram_size = 0; + + /** + * If an encoder-decoder model starts decoding with a different token than *bos*, the id of that token. + * @type {number} + * @default null + */ + decoder_start_token_id = null; + + // Wild card + /** + * Additional generation kwargs will be forwarded to the `generate` function of the model. + * Kwargs that are not present in `generate`'s signature will be used in the model forward pass. + * @type {Object} + * @default {} + */ + generation_kwargs = {}; + + /** + * + * @param {GenerationConfig|import('../configs.js').PretrainedConfig} config + */ + constructor(config) { + Object.assign(this, (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_0__.pick)(config, Object.getOwnPropertyNames(this))); + } +} + + + +/***/ }), + +/***/ "./src/generation/logits_process.js": +/*!******************************************!*\ + !*** ./src/generation/logits_process.js ***! + \******************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ ClassifierFreeGuidanceLogitsProcessor: () => (/* binding */ ClassifierFreeGuidanceLogitsProcessor), +/* harmony export */ ForcedBOSTokenLogitsProcessor: () => (/* binding */ ForcedBOSTokenLogitsProcessor), +/* harmony export */ ForcedEOSTokenLogitsProcessor: () => (/* binding */ ForcedEOSTokenLogitsProcessor), +/* harmony export */ LogitsProcessor: () => (/* binding */ LogitsProcessor), +/* harmony export */ LogitsProcessorList: () => (/* binding */ LogitsProcessorList), +/* harmony export */ LogitsWarper: () => (/* binding */ LogitsWarper), +/* harmony export */ MinLengthLogitsProcessor: () => (/* binding */ MinLengthLogitsProcessor), +/* harmony export */ MinNewTokensLengthLogitsProcessor: () => (/* binding */ MinNewTokensLengthLogitsProcessor), +/* harmony export */ NoBadWordsLogitsProcessor: () => (/* binding */ NoBadWordsLogitsProcessor), +/* harmony export */ NoRepeatNGramLogitsProcessor: () => (/* binding */ NoRepeatNGramLogitsProcessor), +/* harmony export */ RepetitionPenaltyLogitsProcessor: () => (/* binding */ RepetitionPenaltyLogitsProcessor), +/* harmony export */ SuppressTokensAtBeginLogitsProcessor: () => (/* binding */ SuppressTokensAtBeginLogitsProcessor), +/* harmony export */ TemperatureLogitsWarper: () => (/* binding */ TemperatureLogitsWarper), +/* harmony export */ TopKLogitsWarper: () => (/* binding */ TopKLogitsWarper), +/* harmony export */ TopPLogitsWarper: () => (/* binding */ TopPLogitsWarper), +/* harmony export */ WhisperTimeStampLogitsProcessor: () => (/* binding */ WhisperTimeStampLogitsProcessor) +/* harmony export */ }); +/* harmony import */ var _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../utils/generic.js */ "./src/utils/generic.js"); +/* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../utils/tensor.js */ "./src/utils/tensor.js"); +/* harmony import */ var _utils_maths_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../utils/maths.js */ "./src/utils/maths.js"); + +/** + * @module generation/logits_process + */ + + + + + + +/** + * Abstract base class for all logit processors that can be applied during generation. + */ +class LogitsProcessor extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * Apply the processor to the input logits. + * + * @abstract + * @param {bigint[][]} input_ids The input ids. + * @param {Tensor} logits The logits to process. + * @throws {Error} Throws an error if `_call` is not implemented in the subclass. + */ + _call(input_ids, logits) { + throw Error("`_call` should be implemented in a subclass") + } +} + + +/** + * Abstract base class for all logit warpers that can be applied during generation with multinomial sampling. + */ +class LogitsWarper extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * Apply the processor to the input logits. + * + * @abstract + * @param {bigint[][]} input_ids The input ids. + * @param {Tensor} logits The logits to process. + * @throws {Error} Throws an error if `_call` is not implemented in the subclass. + */ + _call(input_ids, logits) { + throw Error("`_call` should be implemented in a subclass") + } +} + + +/** + * A class representing a list of logits processors. A logits processor is a function that modifies the logits + * output of a language model. This class provides methods for adding new processors and applying all processors to a + * batch of logits. + */ +class LogitsProcessorList extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * Constructs a new instance of `LogitsProcessorList`. + */ + constructor() { + super(); + this.processors = []; + } + + /** + * Adds a new logits processor to the list. + * + * @param {LogitsProcessor} item The logits processor function to add. + */ + push(item) { + this.processors.push(item); + } + + /** + * Adds multiple logits processors to the list. + * + * @param {LogitsProcessor[]} items The logits processor functions to add. + */ + extend(items) { + this.processors.push(...items); + } + + /** + * Applies all logits processors in the list to a batch of logits, modifying them in-place. + * + * @param {bigint[][]} input_ids The input IDs for the language model. + * @param {Tensor} logits + */ + _call(input_ids, logits) { + let toReturn = logits; + // NOTE: Most processors modify logits inplace + for (const processor of this.processors) { + toReturn = processor(input_ids, toReturn); + } + return toReturn; + } + + [Symbol.iterator]() { + return this.processors.values(); + } +} + +// DEPRECATED: https://github.com/huggingface/transformers/pull/29485 +// /** +// * A logits processor that forces a specific token to be generated by the decoder. +// */ +// export class ForceTokensLogitsProcessor extends LogitsProcessor { +// /** +// * Constructs a new instance of `ForceTokensLogitsProcessor`. +// * +// * @param {[number, number][]} forced_decoder_ids The ids of tokens that should be forced. +// */ +// constructor(forced_decoder_ids) { +// super(); +// // TODO: convert to `new Map(forced_decoder_ids)` +// this.force_token_map = Object.fromEntries(forced_decoder_ids ?? []); +// } + +// /** +// * Apply the processor to the input logits. +// * +// * @param {bigint[][]} input_ids The input ids. +// * @param {Tensor} logits The logits to process. +// * @returns {Tensor} The processed logits. +// */ +// _call(input_ids, logits) { +// console.log('this.force_token_map', this.force_token_map) +// console.log('call ForceTokensLogitsProcessor', input_ids, logits) +// console.log('input_ids.length', input_ids.length) +// let map = this.force_token_map[input_ids.length]; +// if (map) { // There exists a mapping +// logits.data.fill(-Infinity) +// logits.data[map] = 0; +// } +// console.log('map', map) +// // throw Error("Not implemented") +// return logits; +// } +// } + +/** + * A LogitsProcessor that forces a BOS token at the beginning of the generated sequence. + */ +class ForcedBOSTokenLogitsProcessor extends LogitsProcessor { + /** + * Create a ForcedBOSTokenLogitsProcessor. + * @param {number} bos_token_id The ID of the beginning-of-sequence token to be forced. + */ + constructor(bos_token_id) { + super(); + this.bos_token_id = bos_token_id; + } + + /** + * Apply the BOS token forcing to the logits. + * @param {bigint[][]} input_ids The input IDs. + * @param {Tensor} logits The logits. + * @returns {Object} The logits with BOS token forcing. + */ + _call(input_ids, logits) { + for (let i = 0; i < input_ids.length; ++i) { + if (input_ids[i].length === 1) { + const batch_logits_data = /** @type {Float32Array} */(logits[i].data); + batch_logits_data.fill(-Infinity); + batch_logits_data[this.bos_token_id] = 0; + } + } + return logits; + } +} + +/** + * A logits processor that enforces the specified token as the last generated token when `max_length` is reached. + */ +class ForcedEOSTokenLogitsProcessor extends LogitsProcessor { + /** + * Create a ForcedEOSTokenLogitsProcessor. + * @param {number} max_length The maximum length of the sequence to be generated. + * @param {number|number[]} eos_token_id The id(s) of the *end-of-sequence* token. + */ + constructor(max_length, eos_token_id) { + super(); + this.max_length = max_length; + this.eos_token_id = Array.isArray(eos_token_id) ? eos_token_id : [eos_token_id]; + } + + /** + * Apply the processor to input_ids and logits. + * + * @param {bigint[][]} input_ids The input ids. + * @param {Tensor} logits The logits tensor. + */ + _call(input_ids, logits) { + for (let i = 0; i < input_ids.length; ++i) { + if (input_ids[i].length === this.max_length - 1) { + const batch_logits_data = /** @type {Float32Array} */(logits[i].data); + batch_logits_data.fill(-Infinity); + for (const eos_token of this.eos_token_id) { + batch_logits_data[eos_token] = 0; + } + } + } + return logits; + } +} + +/** + * A LogitsProcessor that suppresses a list of tokens as soon as the `generate` function starts + * generating using `begin_index` tokens. This should ensure that the tokens defined by + * `begin_suppress_tokens` at not sampled at the begining of the generation. + */ +class SuppressTokensAtBeginLogitsProcessor extends LogitsProcessor { + /** + * Create a SuppressTokensAtBeginLogitsProcessor. + * @param {number[]} begin_suppress_tokens The IDs of the tokens to suppress. + * @param {number} begin_index The number of tokens to generate before suppressing tokens. + */ + constructor(begin_suppress_tokens, begin_index) { + super(); + this.begin_suppress_tokens = begin_suppress_tokens; + this.begin_index = begin_index; + } + + /** + * Apply the BOS token forcing to the logits. + * @param {bigint[][]} input_ids The input IDs. + * @param {Tensor} logits The logits. + * @returns {Object} The logits with BOS token forcing. + */ + _call(input_ids, logits) { + for (let i = 0; i < input_ids.length; ++i) { + if (input_ids[i].length === this.begin_index) { + const batch_logits_data = /** @type {Float32Array} */(logits[i].data); + for (const token_id of this.begin_suppress_tokens) { + batch_logits_data[token_id] = -Infinity; + } + } + } + return logits; + } +} + +/** + * A LogitsProcessor that handles adding timestamps to generated text. + */ +class WhisperTimeStampLogitsProcessor extends LogitsProcessor { + /** + * Constructs a new WhisperTimeStampLogitsProcessor. + * @param {import('../models/whisper/generation_whisper.js').WhisperGenerationConfig} generate_config The config object passed to the `generate()` method of a transformer model. + * @param {number[]} init_tokens The initial tokens of the input sequence. + */ + constructor(generate_config, init_tokens) { + super(); + this.eos_token_id = + Array.isArray(generate_config.eos_token_id) + ? generate_config.eos_token_id[0] + : generate_config.eos_token_id; + + this.no_timestamps_token_id = generate_config.no_timestamps_token_id; + this.timestamp_begin = this.no_timestamps_token_id + 1; + + this.begin_index = init_tokens.length; + if (init_tokens.at(-1) === this.no_timestamps_token_id) { + this.begin_index -= 1; + } + this.max_initial_timestamp_index = generate_config.max_initial_timestamp_index; + } + + /** + * Modify the logits to handle timestamp tokens. + * @param {bigint[][]} input_ids The input sequence of tokens. + * @param {Tensor} logits The logits output by the model. + * @returns {Tensor} The modified logits. + */ + _call(input_ids, logits) { + for (let i = 0; i < input_ids.length; ++i) { + const batch_logits_data = /** @type {Float32Array} */(logits[i].data); + + // suppress <|notimestamps|> which is handled by without_timestamps + batch_logits_data[this.no_timestamps_token_id] = -Infinity; + + if (input_ids[i].length === this.begin_index - 1) { + batch_logits_data.fill(-Infinity); + batch_logits_data[this.timestamp_begin] = 0; + continue; + } + + // timestamps have to appear in pairs, except directly before eos_token; mask logits accordingly + const seq = input_ids[i].slice(this.begin_index); + const last_was_timestamp = seq.length >= 1 && seq[seq.length - 1] >= this.timestamp_begin; + const penultimate_was_timestamp = seq.length < 2 || seq[seq.length - 2] >= this.timestamp_begin; + + if (last_was_timestamp) { + if (penultimate_was_timestamp) { // has to be non-timestamp + batch_logits_data.subarray(this.timestamp_begin).fill(-Infinity); + } else { // cannot be normal text tokens + batch_logits_data.subarray(0, this.eos_token_id).fill(-Infinity); + } + } + + // apply the `max_initial_timestamp` option + if (input_ids[i].length === this.begin_index && this.max_initial_timestamp_index !== null) { + const last_allowed = this.timestamp_begin + this.max_initial_timestamp_index; + batch_logits_data.subarray(last_allowed + 1).fill(-Infinity); + } + + // if sum of probability over timestamps is above any other token, sample timestamp + const logprobs = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_2__.log_softmax)(batch_logits_data); + const timestamp_logprob = Math.log(logprobs.subarray(this.timestamp_begin).map(Math.exp).reduce((a, b) => a + b)); + const max_text_token_logprob = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_2__.max)(logprobs.subarray(0, this.timestamp_begin))[0]; + + if (timestamp_logprob > max_text_token_logprob) { + batch_logits_data.subarray(0, this.timestamp_begin).fill(-Infinity); + } + } + + return logits; + } +} + +/** + * A logits processor that disallows ngrams of a certain size to be repeated. + */ +class NoRepeatNGramLogitsProcessor extends LogitsProcessor { + /** + * Create a NoRepeatNGramLogitsProcessor. + * @param {number} no_repeat_ngram_size The no-repeat-ngram size. All ngrams of this size can only occur once. + */ + constructor(no_repeat_ngram_size) { + super(); + this.no_repeat_ngram_size = no_repeat_ngram_size; + } + + /** + * Generate n-grams from a sequence of token ids. + * @param {bigint[]} prevInputIds List of previous input ids + * @returns {Map} Map of generated n-grams + */ + getNgrams(prevInputIds) { + const curLen = prevInputIds.length; + + /**@type {number[][]} */ + const ngrams = []; + for (let j = 0; j < curLen + 1 - this.no_repeat_ngram_size; ++j) { + const ngram = []; + for (let k = 0; k < this.no_repeat_ngram_size; ++k) { + ngram.push(prevInputIds[j + k]); + } + ngrams.push(ngram.map(Number)); + } + + /** @type {Map} */ + const generatedNgram = new Map(); + for (const ngram of ngrams) { + const prevNgram = ngram.slice(0, ngram.length - 1); + const prevNgramKey = JSON.stringify(prevNgram); + const prevNgramValue = generatedNgram.get(prevNgramKey) ?? []; + prevNgramValue.push(ngram[ngram.length - 1]); + generatedNgram.set(prevNgramKey, prevNgramValue); + } + return generatedNgram; + } + + /** + * Generate n-grams from a sequence of token ids. + * @param {Map} bannedNgrams Map of banned n-grams + * @param {bigint[]} prevInputIds List of previous input ids + * @returns {number[]} Map of generated n-grams + */ + getGeneratedNgrams(bannedNgrams, prevInputIds) { + const ngramIdx = prevInputIds.slice(prevInputIds.length + 1 - this.no_repeat_ngram_size, prevInputIds.length); + const banned = bannedNgrams.get(JSON.stringify(ngramIdx.map(Number))) ?? []; + return banned; + } + + /** + * Calculate banned n-gram tokens + * @param {bigint[]} prevInputIds List of previous input ids + * @returns {number[]} Map of generated n-grams + */ + calcBannedNgramTokens(prevInputIds) { + const bannedTokens = []; + if (prevInputIds.length + 1 < this.no_repeat_ngram_size) { + // return no banned tokens if we haven't generated no_repeat_ngram_size tokens yet + return bannedTokens; + + } else { + const generatedNgrams = this.getNgrams(prevInputIds); + const bannedTokens = this.getGeneratedNgrams(generatedNgrams, prevInputIds); + return bannedTokens; + } + } + + /** + * Apply the no-repeat-ngram processor to the logits. + * @param {bigint[][]} input_ids The input IDs. + * @param {Tensor} logits The logits. + * @returns {Object} The logits with no-repeat-ngram processing. + */ + _call(input_ids, logits) { + for (let i = 0; i < input_ids.length; ++i) { + const batch_logits_data = /** @type {Float32Array} */(logits[i].data); + const bannedTokens = this.calcBannedNgramTokens(input_ids[i]); + for (const token of bannedTokens) { + batch_logits_data[token] = -Infinity; + } + } + return logits; + } +} + +/** + * A logits processor that penalises repeated output tokens. + */ +class RepetitionPenaltyLogitsProcessor extends LogitsProcessor { + /** + * Create a RepetitionPenaltyLogitsProcessor. + * @param {number} penalty The penalty to apply for repeated tokens. + */ + constructor(penalty) { + super(); + this.penalty = penalty; + } + + /** + * Apply the repetition penalty to the logits. + * @param {bigint[][]} input_ids The input IDs. + * @param {Tensor} logits The logits. + * @returns {Object} The logits with repetition penalty processing. + */ + _call(input_ids, logits) { + // Modify the logits corresponding to each element in `input_ids`. + // As a consequence, the logits corresponding to tokens that appear + // many times in the output will be penalised more. + + for (let i = 0; i < input_ids.length; ++i) { + const batch_logits_data = /** @type {Float32Array} */(logits[i].data); + for (const input_id of input_ids[i]) { + const token = Number(input_id); + if (batch_logits_data[token] < 0) { + batch_logits_data[token] *= this.penalty; + } else { + batch_logits_data[token] /= this.penalty; + } + } + } + + return logits + } +} + +/** + * A logits processor that enforces a minimum number of tokens. + */ +class MinLengthLogitsProcessor extends LogitsProcessor { + /** + * Create a MinLengthLogitsProcessor. + * @param {number} min_length The minimum length below which the score of `eos_token_id` is set to negative infinity. + * @param {number|number[]} eos_token_id The ID/IDs of the end-of-sequence token. + */ + constructor(min_length, eos_token_id) { + super(); + this.min_length = min_length; + this.eos_token_id = Array.isArray(eos_token_id) ? eos_token_id : [eos_token_id]; + } + + /** + * Apply logit processor. + * @param {bigint[][]} input_ids The input IDs. + * @param {Tensor} logits The logits. + * @returns {Object} The processed logits. + */ + _call(input_ids, logits) { + for (let i = 0; i < input_ids.length; ++i) { + if (input_ids[i].length < this.min_length) { + const batch_logits_data = /** @type {Float32Array} */(logits[i].data); + + for (const eos_token of this.eos_token_id) { + batch_logits_data[eos_token] = -Infinity; + } + } + } + + return logits + } +} + +/** + * A logits processor that enforces a minimum number of new tokens. + */ +class MinNewTokensLengthLogitsProcessor extends LogitsProcessor { + /** + * Create a MinNewTokensLengthLogitsProcessor. + * @param {number} prompt_length_to_skip The input tokens length. + * @param {number} min_new_tokens The minimum *new* tokens length below which the score of `eos_token_id` is set to negative infinity. + * @param {number|number[]} eos_token_id The ID/IDs of the end-of-sequence token. + */ + constructor(prompt_length_to_skip, min_new_tokens, eos_token_id) { + super(); + this.prompt_length_to_skip = prompt_length_to_skip; + this.min_new_tokens = min_new_tokens; + this.eos_token_id = Array.isArray(eos_token_id) ? eos_token_id : [eos_token_id]; + } + + /** + * Apply logit processor. + * @param {bigint[][]} input_ids The input IDs. + * @param {Tensor} logits The logits. + * @returns {Object} The processed logits. + */ + _call(input_ids, logits) { + for (let i = 0; i < input_ids.length; ++i) { + const new_tokens_length = input_ids[i].length - this.prompt_length_to_skip; + if (new_tokens_length < this.min_new_tokens) { + const batch_logits_data = /** @type {Float32Array} */(logits[i].data); + + for (const eos_token of this.eos_token_id) { + batch_logits_data[eos_token] = -Infinity; + } + } + } + return logits + } +} + +class NoBadWordsLogitsProcessor extends LogitsProcessor { + /** + * Create a `NoBadWordsLogitsProcessor`. + * @param {number[][]} bad_words_ids List of list of token ids that are not allowed to be generated. + * @param {number|number[]} eos_token_id The id of the *end-of-sequence* token. Optionally, use a list to set multiple *end-of-sequence* tokens. + */ + constructor(bad_words_ids, eos_token_id) { + super(); + this.bad_words_ids = bad_words_ids; + this.eos_token_id = Array.isArray(eos_token_id) ? eos_token_id : [eos_token_id]; + } + + /** + * Apply logit processor. + * @param {bigint[][]} input_ids The input IDs. + * @param {Tensor} logits The logits. + * @returns {Object} The processed logits. + */ + _call(input_ids, logits) { + for (let i = 0; i < input_ids.length; ++i) { + const batch_logits_data = /** @type {Float32Array} */(logits[i].data); + const ids = input_ids[i]; + for (const bad_word_ids of this.bad_words_ids) { + // Whether to modify the logits of the last token in the bad word id sequence + let mark = true; + + // For each bad word in the list, if the current sequence of input ids ends with this sequence (excluding the last), + // then we set the logits of the last bad word id to -Infinity. + for (let j = 1; j <= bad_word_ids.length - 1 && bad_word_ids.length < ids.length; ++j) { + + // NOTE: We use != instead of !== to compare bigint and number + // @ts-ignore + if (bad_word_ids.at(-j - 1) != ids.at(-j)) { + // We have found a mismatch + mark = false; + break; + } + } + if (mark) { + batch_logits_data[bad_word_ids.at(-1)] = -Infinity; + } + } + } + return logits + } +} + +/** + * [`LogitsProcessor`] for classifier free guidance (CFG). The scores are split over the batch dimension, + * where the first half correspond to the conditional logits (predicted from the input prompt) and the second half + * correspond to the unconditional logits (predicted from an empty or 'null' prompt). The processor computes a + * weighted average across the conditional and unconditional logits, parameterised by the `guidance_scale`. + * + * See [the paper](https://arxiv.org/abs/2306.05284) for more information. + */ +class ClassifierFreeGuidanceLogitsProcessor extends LogitsProcessor { + + /** + * Create a `ClassifierFreeGuidanceLogitsProcessor`. + * @param {number} guidance_scale The guidance scale for classifier free guidance (CFG). CFG is enabled by setting `guidance_scale > 1`. + * Higher guidance scale encourages the model to generate samples that are more closely linked to the input + * prompt, usually at the expense of poorer quality. + */ + constructor(guidance_scale) { + super(); + if (guidance_scale <= 1) { + throw new Error( + `Require guidance scale >1 to use the classifier free guidance processor, got guidance scale ${guidance_scale}.` + ) + } + this.guidance_scale = guidance_scale; + } + + /** + * Apply logit processor. + * @param {bigint[][]} input_ids The input IDs. + * @param {Tensor} logits The logits. + * @returns {Object} The processed logits. + */ + _call(input_ids, logits) { + if (logits.dims[0] !== 2 * input_ids.length) { + throw new Error( + `Logits should have twice the batch size of the input ids, the first half of batches corresponding to ` + + `the conditional inputs, and the second half of batches corresponding to the unconditional inputs. Got ` + + `batch size ${logits.dims[0]} for the logits and ${input_ids.length} for the input ids.` + ) + } + + const unguided_bsz = input_ids.length; + const cond_logits = logits.slice([0, unguided_bsz], null); + const uncond_logits = logits.slice([unguided_bsz, logits.dims[0]], null); + + // Merge into uncond_logits (to save memory). This is equivalent to the following: + // scores = uncond_logits + (cond_logits - uncond_logits) * guidance_scale + for (let i = 0; i < uncond_logits.data.length; ++i) { + uncond_logits.data[i] += (cond_logits.data[i] - uncond_logits.data[i]) * this.guidance_scale; + } + + return uncond_logits; + } +} + +/** + * [`LogitsWarper`] for temperature (exponential scaling output probability distribution), which effectively means + * that it can control the randomness of the predicted tokens. Often used together with [`TopPLogitsWarper`] and [`TopKLogitsWarper`]. + */ +class TemperatureLogitsWarper extends LogitsWarper { + /** + * Create a `TemperatureLogitsWarper`. + * @param {number} temperature Strictly positive float value used to modulate the logits distribution. + * A value smaller than `1` decreases randomness (and vice versa), with `0` being equivalent to shifting + * all probability mass to the most likely token. + */ + constructor(temperature) { + super(); + + if (typeof temperature !== 'number' || temperature <= 0) { + let errorMessage = + `\`temperature\` (=${temperature}) must be a strictly positive float, otherwise your next token scores will be invalid.`; + + if (temperature === 0) { + errorMessage += " If you're looking for greedy decoding strategies, set `do_sample=false`." + } + } + this.temperature = temperature; + } + + /** + * Apply logit warper. + * @param {bigint[][]} input_ids The input IDs. + * @param {Tensor} logits The logits. + * @returns {Object} The processed logits. + */ + _call(input_ids, logits) { + const batch_logits_data = /** @type {Float32Array} */(logits.data); + for (let i = 0; i < batch_logits_data.length; ++i) { + batch_logits_data[i] /= this.temperature; + } + return logits; + } +} + +/** + * [`LogitsWarper`] that performs top-p, i.e. restricting to top tokens summing to prob_cut_off <= prob_cut_off. + * Often used together with [`TemperatureLogitsWarper`] and [`TopKLogitsWarper`]. + */ +class TopPLogitsWarper extends LogitsWarper { + /** + * Create a `TopPLogitsWarper`. + * @param {number} top_p If set to < 1, only the smallest set of most probable tokens with + * probabilities that add up to `top_p` or higher are kept for generation. + * @param {Object} options Additional options for the top-p sampling. + * @param {number} [options.filter_value=-Infinity] All filtered values will be set to this float value. + * @param {number} [options.min_tokens_to_keep=1] Minimum number of tokens that cannot be filtered. + */ + constructor(top_p, { + filter_value = -Infinity, + min_tokens_to_keep = 1, + } = {}) { + super(); + if (top_p < 0 || top_p > 1.0) { + throw new Error(`\`top_p\` must be a float > 0 and < 1, but is ${top_p}`) + } + if (!Number.isInteger(min_tokens_to_keep) || min_tokens_to_keep < 1) { + throw new Error(`\`min_tokens_to_keep\` must be a positive integer, but is ${min_tokens_to_keep}`) + } + + this.top_p = top_p + this.filter_value = filter_value + this.min_tokens_to_keep = min_tokens_to_keep + } +} + +/** + * [`LogitsWarper`] that performs top-k, i.e. restricting to the k highest probability elements. + * Often used together with [`TemperatureLogitsWarper`] and [`TopPLogitsWarper`]. + */ +class TopKLogitsWarper extends LogitsWarper { + /** + * Create a `TopKLogitsWarper`. + * @param {number} top_k If set to > 0, only the top `top_k` tokens are kept for generation. + * @param {Object} options Additional options for the top-k sampling. + * @param {number} [options.filter_value=-Infinity] All filtered values will be set to this float value. + * @param {number} [options.min_tokens_to_keep=1] Minimum number of tokens that cannot be filtered. + */ + constructor(top_k, { + filter_value = -Infinity, + min_tokens_to_keep = 1, + } = {}) { + super(); + if (!Number.isInteger(top_k) || top_k < 0) { + throw new Error(`\`top_k\` must be a positive integer, but is ${top_k}`) + } + + this.top_k = Math.max(top_k, min_tokens_to_keep) + this.filter_value = filter_value + } +} + +/***/ }), + +/***/ "./src/generation/logits_sampler.js": +/*!******************************************!*\ + !*** ./src/generation/logits_sampler.js ***! + \******************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ LogitsSampler: () => (/* binding */ LogitsSampler) +/* harmony export */ }); +/* harmony import */ var _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../utils/generic.js */ "./src/utils/generic.js"); +/* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../utils/tensor.js */ "./src/utils/tensor.js"); +/* harmony import */ var _utils_maths_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../utils/maths.js */ "./src/utils/maths.js"); +/* harmony import */ var _generation_configuration_utils_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ../generation/configuration_utils.js */ "./src/generation/configuration_utils.js"); + +/** + * @module generation/logits_sampler + */ + + + + + + + +/** + * Sampler is a base class for all sampling methods used for text generation. + */ +class LogitsSampler extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * Creates a new Sampler object with the specified generation config. + * @param {GenerationConfig} generation_config The generation config. + */ + constructor(generation_config) { + super(); + this.generation_config = generation_config; + } + + /** + * Executes the sampler, using the specified logits. + * @param {Tensor} logits + * @returns {Promise<[bigint, number][]>} + */ + async _call(logits) { + // Sample from logits, of dims [batch, sequence_length, vocab_size]. + // If index is specified, sample from [batch, index, vocab_size]. + return this.sample(logits); + } + + /** + * Abstract method for sampling the logits. + * @param {Tensor} logits + * @throws {Error} If not implemented in subclass. + * @returns {Promise<[bigint, number][]>} + */ + async sample(logits) { + throw Error("sample should be implemented in subclasses.") + } + + /** + * Returns the specified logits as an array, with temperature applied. + * @param {Tensor} logits + * @param {number} index + * @returns {Float32Array} + */ + getLogits(logits, index) { + let vocabSize = logits.dims.at(-1); + + let logs = /** @type {Float32Array} */(logits.data); + + if (index === -1) { + logs = logs.slice(-vocabSize); + } else { + let startIndex = index * vocabSize; + logs = logs.slice(startIndex, startIndex + vocabSize); + } + return logs; + } + + /** + * Selects an item randomly based on the specified probabilities. + * @param {import("../transformers.js").DataArray} probabilities An array of probabilities to use for selection. + * @returns {number} The index of the selected item. + */ + randomSelect(probabilities) { + // Return index of chosen item + let sumProbabilities = 0; + for (let i = 0; i < probabilities.length; ++i) { + sumProbabilities += probabilities[i]; + } + + let r = Math.random() * sumProbabilities; + for (let i = 0; i < probabilities.length; ++i) { + r -= probabilities[i]; + if (r <= 0) { + return i; + } + } + return 0; // return first (most probable) as a fallback + } + + /** + * Returns a Sampler object based on the specified options. + * @param {GenerationConfig} generation_config An object containing options for the sampler. + * @returns {LogitsSampler} A Sampler object. + */ + static getSampler(generation_config) { + // - *greedy decoding*: `num_beams=1` and `do_sample=False` + // - *contrastive search*: `penalty_alpha>0` and `top_k>1` + // - *multinomial sampling*: `num_beams=1` and `do_sample=True` + // - *beam-search decoding*: `num_beams>1` and `do_sample=False` + // - *beam-search multinomial sampling*: `num_beams>1` and `do_sample=True` + // - *diverse beam-search decoding*: `num_beams>1` and `num_beam_groups>1` + // - *constrained beam-search decoding*: `constraints!=None` or `force_words_ids!=None` + + // NOTE: beam search is implemented directly into the generation function + if (generation_config.do_sample) { + return new MultinomialSampler(generation_config); + + } else if (generation_config.num_beams > 1) { + return new BeamSearchSampler(generation_config); + + } else { + if (generation_config.num_return_sequences > 1) { + throw Error(`num_return_sequences has to be 1 when doing greedy search, but is ${generation_config.num_return_sequences}.`) + } + return new GreedySampler(generation_config); + } + } +} + +/** + * Class representing a Greedy Sampler. + */ +class GreedySampler extends LogitsSampler { + /** + * Sample the maximum probability of a given logits tensor. + * @param {Tensor} logits + * @returns {Promise<[bigint, number][]>} An array with a single tuple, containing the index of the maximum value and a meaningless score (since this is a greedy search). + */ + async sample(logits) { + // NOTE: no need to do log_softmax here since we only take the maximum + const argmax = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_2__.max)(logits.data)[1]; + + // Note: score is meaningless in this context, since we are performing + // greedy search (p = 1 => log(p) = 0) + return [ + [BigInt(argmax), 0] + ]; + } +} + +/** + * Class representing a MultinomialSampler. + */ +class MultinomialSampler extends LogitsSampler { + + /** + * Sample from the logits. + * @param {Tensor} logits + * @returns {Promise<[bigint, number][]>} + */ + async sample(logits) { + let k = logits.dims.at(-1); // defaults to vocab size + if (this.generation_config.top_k > 0) { + k = Math.min(this.generation_config.top_k, k); + } + + // Get top k tokens + const [v, i] = await (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.topk)(logits, k); + + // Compute softmax over logits + const probabilities = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_2__.softmax)(/** @type {Float32Array} */(v.data)); + + return Array.from({ length: this.generation_config.num_beams }, () => { + const sampledIndex = this.randomSelect(probabilities); + return [ + i.data[sampledIndex], // token id + Math.log(probabilities[sampledIndex]), // score + ]; + }); + } +} + + +/** + * Class representing a BeamSearchSampler. + */ +class BeamSearchSampler extends LogitsSampler { + + /** + * Sample from the logits. + * @param {Tensor} logits + * @returns {Promise<[bigint, number][]>} + */ + async sample(logits) { + let k = logits.dims.at(-1); // defaults to vocab size + if (this.generation_config.top_k > 0) { + k = Math.min(this.generation_config.top_k, k); + } + + // Get top k tokens + const [v, i] = await (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.topk)(logits, k); + + // Compute softmax over logits + const probabilities = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_2__.softmax)(/** @type {Float32Array} */(v.data)); + + return Array.from({ length: this.generation_config.num_beams }, (_, x) => { + return [ + i.data[x], // token id + Math.log(probabilities[x]), // score + ]; + }); + } +} + + +/***/ }), + +/***/ "./src/generation/stopping_criteria.js": +/*!*********************************************!*\ + !*** ./src/generation/stopping_criteria.js ***! + \*********************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ EosTokenCriteria: () => (/* binding */ EosTokenCriteria), +/* harmony export */ InterruptableStoppingCriteria: () => (/* binding */ InterruptableStoppingCriteria), +/* harmony export */ MaxLengthCriteria: () => (/* binding */ MaxLengthCriteria), +/* harmony export */ StoppingCriteria: () => (/* binding */ StoppingCriteria), +/* harmony export */ StoppingCriteriaList: () => (/* binding */ StoppingCriteriaList) +/* harmony export */ }); +/* harmony import */ var _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../utils/generic.js */ "./src/utils/generic.js"); + +/** + * @module generation/stopping_criteria + */ + + + +// NOTE: +// Stopping Criteria returns a list of `batch_size` booleans, indicating whether each sequence in the batch should be stopped. + +/** + * Abstract base class for all stopping criteria that can be applied during generation. + */ +class StoppingCriteria extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * + * @param {number[][]} input_ids (`number[][]` of shape `(batch_size, sequence_length)`): + * Indices of input sequence tokens in the vocabulary. + * @param {number[][]} scores scores (`number[][]` of shape `(batch_size, config.vocab_size)`): + * Prediction scores of a language modeling head. These can be scores for each vocabulary token before SoftMax + * or scores for each vocabulary token after SoftMax. + * @returns {boolean[]} A list of booleans indicating whether each sequence should be stopped. + */ + _call(input_ids, scores) { + throw Error("StoppingCriteria needs to be subclassed"); + } +} +/** + */ +class StoppingCriteriaList extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * Constructs a new instance of `StoppingCriteriaList`. + */ + constructor() { + super(); + this.criteria = []; + } + + /** + * Adds a new stopping criterion to the list. + * + * @param {StoppingCriteria} item The stopping criterion to add. + */ + push(item) { + this.criteria.push(item); + } + + /** + * Adds multiple stopping criteria to the list. + * + * @param {StoppingCriteria|StoppingCriteriaList|StoppingCriteria[]} items The stopping criteria to add. + */ + extend(items) { + if (items instanceof StoppingCriteriaList) { + items = items.criteria; + } else if (items instanceof StoppingCriteria) { + items = [items]; + } + this.criteria.push(...items); + } + + _call(input_ids, scores) { + const is_done = new Array(input_ids.length).fill(false); + for (const criterion of this.criteria) { + const criterion_done = criterion(input_ids, scores); + for (let i = 0; i < is_done.length; ++i) { + is_done[i] ||= criterion_done[i]; + } + } + return is_done; + } + + [Symbol.iterator]() { + return this.criteria.values(); + } +} + +/** + * This class can be used to stop generation whenever the full generated number of tokens exceeds `max_length`. + * Keep in mind for decoder-only type of transformers, this will include the initial prompted tokens. + */ +class MaxLengthCriteria extends StoppingCriteria { + + /** + * + * @param {number} max_length The maximum length that the output sequence can have in number of tokens. + * @param {number} [max_position_embeddings=null] The maximum model length, as defined by the model's `config.max_position_embeddings` attribute. + */ + constructor(max_length, max_position_embeddings = null) { + super(); + this.max_length = max_length; + this.max_position_embeddings = max_position_embeddings; + } + + _call(input_ids) { + return input_ids.map(ids => ids.length >= this.max_length); + } +} + +// TODO: add MaxTimeCriteria + +/** + * This class can be used to stop generation whenever the "end-of-sequence" token is generated. + * By default, it uses the `model.generation_config.eos_token_id`. + */ +class EosTokenCriteria extends StoppingCriteria { + + /** + * + * @param {number|number[]} eos_token_id The id of the *end-of-sequence* token. + * Optionally, use a list to set multiple *end-of-sequence* tokens. + */ + constructor(eos_token_id) { + super(); + if (!Array.isArray(eos_token_id)) { + eos_token_id = [eos_token_id]; + } + this.eos_token_id = eos_token_id; + } + + /** + * + * @param {number[][]} input_ids + * @param {number[][]} scores + * @returns {boolean[]} + */ + _call(input_ids, scores) { + return input_ids.map(ids => { + const last = ids.at(-1); + // NOTE: We use == instead of === to allow for number/bigint comparison + return this.eos_token_id.some(eos_id => last == eos_id); + }); + } +} + +/** + * This class can be used to stop generation whenever the user interrupts the process. + */ +class InterruptableStoppingCriteria extends StoppingCriteria { + constructor() { + super(); + this.interrupted = false; + } + + interrupt() { + this.interrupted = true; + } + + reset() { + this.interrupted = false; + } + + _call(input_ids, scores) { + return new Array(input_ids.length).fill(this.interrupted); + } +} + + +/***/ }), + +/***/ "./src/generation/streamers.js": +/*!*************************************!*\ + !*** ./src/generation/streamers.js ***! + \*************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ BaseStreamer: () => (/* binding */ BaseStreamer), +/* harmony export */ TextStreamer: () => (/* binding */ TextStreamer), +/* harmony export */ WhisperTextStreamer: () => (/* binding */ WhisperTextStreamer) +/* harmony export */ }); +/* harmony import */ var _utils_core_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../utils/core.js */ "./src/utils/core.js"); +/* harmony import */ var _tokenizers_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../tokenizers.js */ "./src/tokenizers.js"); +/* harmony import */ var _env_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../env.js */ "./src/env.js"); + +/** + * @module generation/streamers + */ + + + + + +class BaseStreamer { + /** + * Function that is called by `.generate()` to push new tokens + * @param {bigint[][]} value + */ + put(value) { + throw Error('Not implemented'); + } + + /** + * Function that is called by `.generate()` to signal the end of generation + */ + end() { + throw Error('Not implemented'); + } +} + +const stdout_write = _env_js__WEBPACK_IMPORTED_MODULE_2__.apis.IS_PROCESS_AVAILABLE + ? x => process.stdout.write(x) + : x => console.log(x); + +/** + * Simple text streamer that prints the token(s) to stdout as soon as entire words are formed. + */ +class TextStreamer extends BaseStreamer { + /** + * + * @param {import('../tokenizers.js').PreTrainedTokenizer} tokenizer + */ + constructor(tokenizer, { + skip_prompt = false, + callback_function = null, + token_callback_function = null, + decode_kwargs = {}, + ...kwargs + } = {}) { + super(); + this.tokenizer = tokenizer; + this.skip_prompt = skip_prompt; + this.callback_function = callback_function ?? stdout_write; + this.token_callback_function = token_callback_function; + this.decode_kwargs = { ...decode_kwargs, ...kwargs }; + + // variables used in the streaming process + this.token_cache = []; + this.print_len = 0; + this.next_tokens_are_prompt = true; + } + + /** + * Receives tokens, decodes them, and prints them to stdout as soon as they form entire words. + * @param {bigint[][]} value + */ + put(value) { + if (value.length > 1) { + throw Error('TextStreamer only supports batch size of 1'); + } + + if (this.skip_prompt && this.next_tokens_are_prompt) { + this.next_tokens_are_prompt = false; + return; + } + + const tokens = value[0]; + this.token_callback_function?.(tokens) + + // Add the new token to the cache and decodes the entire thing. + this.token_cache = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_0__.mergeArrays)(this.token_cache, tokens); + const text = this.tokenizer.decode(this.token_cache, this.decode_kwargs); + + let printable_text; + if (text.endsWith('\n')) { + // After the symbol for a new line, we flush the cache. + printable_text = text.slice(this.print_len); + this.token_cache = []; + this.print_len = 0; + } else if (text.length > 0 && (0,_tokenizers_js__WEBPACK_IMPORTED_MODULE_1__.is_chinese_char)(text.charCodeAt(text.length - 1))) { + // If the last token is a CJK character, we print the characters. + printable_text = text.slice(this.print_len); + this.print_len += printable_text.length; + } else { + // Otherwise, prints until the last space char (simple heuristic to avoid printing incomplete words, + // which may change with the subsequent token -- there are probably smarter ways to do this!) + printable_text = text.slice(this.print_len, text.lastIndexOf(' ') + 1); + this.print_len += printable_text.length; + } + + this.on_finalized_text(printable_text, false); + } + + /** + * Flushes any remaining cache and prints a newline to stdout. + */ + end() { + let printable_text; + if (this.token_cache.length > 0) { + const text = this.tokenizer.decode(this.token_cache, this.decode_kwargs); + printable_text = text.slice(this.print_len); + this.token_cache = []; + this.print_len = 0; + } else { + printable_text = ''; + } + this.next_tokens_are_prompt = true; + this.on_finalized_text(printable_text, true); + } + + /** + * Prints the new text to stdout. If the stream is ending, also prints a newline. + * @param {string} text + * @param {boolean} stream_end + */ + on_finalized_text(text, stream_end) { + if (text.length > 0) { + this.callback_function?.(text); + } + if (stream_end && this.callback_function === stdout_write && _env_js__WEBPACK_IMPORTED_MODULE_2__.apis.IS_PROCESS_AVAILABLE) { + this.callback_function?.('\n'); + } + } +} + +/** + * Utility class to handle streaming of tokens generated by whisper speech-to-text models. + * Callback functions are invoked when each of the following events occur: + * - A new chunk starts (on_chunk_start) + * - A new token is generated (callback_function) + * - A chunk ends (on_chunk_end) + * - The stream is finalized (on_finalize) + */ +class WhisperTextStreamer extends TextStreamer { + /** + * @param {import('../tokenizers.js').WhisperTokenizer} tokenizer + * @param {Object} options + * @param {boolean} [options.skip_prompt=false] Whether to skip the prompt tokens + * @param {function(string): void} [options.callback_function=null] Function to call when a piece of text is ready to display + * @param {function(string): void} [options.token_callback_function=null] Function to call when a new token is generated + * @param {function(number): void} [options.on_chunk_start=null] Function to call when a new chunk starts + * @param {function(number): void} [options.on_chunk_end=null] Function to call when a chunk ends + * @param {function(): void} [options.on_finalize=null] Function to call when the stream is finalized + * @param {number} [options.time_precision=0.02] Precision of the timestamps + * @param {boolean} [options.skip_special_tokens=true] Whether to skip special tokens when decoding + * @param {Object} [options.decode_kwargs={}] Additional keyword arguments to pass to the tokenizer's decode method + */ + constructor(tokenizer, { + skip_prompt = false, + callback_function = null, + token_callback_function = null, + on_chunk_start = null, + on_chunk_end = null, + on_finalize = null, + time_precision = 0.02, + skip_special_tokens = true, + decode_kwargs = {}, + } = {}) { + super(tokenizer, { + skip_prompt, + callback_function, + token_callback_function, + decode_kwargs: { skip_special_tokens, ...decode_kwargs }, + }); + this.timestamp_begin = tokenizer.timestamp_begin; + + this.on_chunk_start = on_chunk_start; + this.on_chunk_end = on_chunk_end; + this.on_finalize = on_finalize; + + this.time_precision = time_precision; + + this.waiting_for_timestamp = false; + } + + /** + * @param {bigint[][]} value + */ + put(value) { + if (value.length > 1) { + throw Error('WhisperTextStreamer only supports batch size of 1'); + } + const tokens = value[0]; + + // Check if the token is a timestamp + if (tokens.length === 1) { + const offset = Number(tokens[0]) - this.timestamp_begin; + if (offset >= 0) { + const time = offset * this.time_precision; + if (this.waiting_for_timestamp) { + this.on_chunk_end?.(time); + } else { + this.on_chunk_start?.(time); + } + this.waiting_for_timestamp = !this.waiting_for_timestamp; // Toggle + value = [[]]; // Skip timestamp + } + } + return super.put(value); + } + + end() { + super.end(); + this.on_finalize?.(); + } +} + + +/***/ }), + +/***/ "./src/models.js": +/*!***********************!*\ + !*** ./src/models.js ***! + \***********************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ ASTForAudioClassification: () => (/* binding */ ASTForAudioClassification), +/* harmony export */ ASTModel: () => (/* binding */ ASTModel), +/* harmony export */ ASTPreTrainedModel: () => (/* binding */ ASTPreTrainedModel), +/* harmony export */ AlbertForMaskedLM: () => (/* binding */ AlbertForMaskedLM), +/* harmony export */ AlbertForQuestionAnswering: () => (/* binding */ AlbertForQuestionAnswering), +/* harmony export */ AlbertForSequenceClassification: () => (/* binding */ AlbertForSequenceClassification), +/* harmony export */ AlbertModel: () => (/* binding */ AlbertModel), +/* harmony export */ AlbertPreTrainedModel: () => (/* binding */ AlbertPreTrainedModel), +/* harmony export */ AutoModel: () => (/* binding */ AutoModel), +/* harmony export */ AutoModelForAudioClassification: () => (/* binding */ AutoModelForAudioClassification), +/* harmony export */ AutoModelForAudioFrameClassification: () => (/* binding */ AutoModelForAudioFrameClassification), +/* harmony export */ AutoModelForCTC: () => (/* binding */ AutoModelForCTC), +/* harmony export */ AutoModelForCausalLM: () => (/* binding */ AutoModelForCausalLM), +/* harmony export */ AutoModelForDepthEstimation: () => (/* binding */ AutoModelForDepthEstimation), +/* harmony export */ AutoModelForDocumentQuestionAnswering: () => (/* binding */ AutoModelForDocumentQuestionAnswering), +/* harmony export */ AutoModelForImageClassification: () => (/* binding */ AutoModelForImageClassification), +/* harmony export */ AutoModelForImageFeatureExtraction: () => (/* binding */ AutoModelForImageFeatureExtraction), +/* harmony export */ AutoModelForImageMatting: () => (/* binding */ AutoModelForImageMatting), +/* harmony export */ AutoModelForImageSegmentation: () => (/* binding */ AutoModelForImageSegmentation), +/* harmony export */ AutoModelForImageToImage: () => (/* binding */ AutoModelForImageToImage), +/* harmony export */ AutoModelForMaskGeneration: () => (/* binding */ AutoModelForMaskGeneration), +/* harmony export */ AutoModelForMaskedLM: () => (/* binding */ AutoModelForMaskedLM), +/* harmony export */ AutoModelForNormalEstimation: () => (/* binding */ AutoModelForNormalEstimation), +/* harmony export */ AutoModelForObjectDetection: () => (/* binding */ AutoModelForObjectDetection), +/* harmony export */ AutoModelForQuestionAnswering: () => (/* binding */ AutoModelForQuestionAnswering), +/* harmony export */ AutoModelForSemanticSegmentation: () => (/* binding */ AutoModelForSemanticSegmentation), +/* harmony export */ AutoModelForSeq2SeqLM: () => (/* binding */ AutoModelForSeq2SeqLM), +/* harmony export */ AutoModelForSequenceClassification: () => (/* binding */ AutoModelForSequenceClassification), +/* harmony export */ AutoModelForSpeechSeq2Seq: () => (/* binding */ AutoModelForSpeechSeq2Seq), +/* harmony export */ AutoModelForTextToSpectrogram: () => (/* binding */ AutoModelForTextToSpectrogram), +/* harmony export */ AutoModelForTextToWaveform: () => (/* binding */ AutoModelForTextToWaveform), +/* harmony export */ AutoModelForTokenClassification: () => (/* binding */ AutoModelForTokenClassification), +/* harmony export */ AutoModelForUniversalSegmentation: () => (/* binding */ AutoModelForUniversalSegmentation), +/* harmony export */ AutoModelForVision2Seq: () => (/* binding */ AutoModelForVision2Seq), +/* harmony export */ AutoModelForXVector: () => (/* binding */ AutoModelForXVector), +/* harmony export */ AutoModelForZeroShotObjectDetection: () => (/* binding */ AutoModelForZeroShotObjectDetection), +/* harmony export */ BartForConditionalGeneration: () => (/* binding */ BartForConditionalGeneration), +/* harmony export */ BartForSequenceClassification: () => (/* binding */ BartForSequenceClassification), +/* harmony export */ BartModel: () => (/* binding */ BartModel), +/* harmony export */ BartPretrainedModel: () => (/* binding */ BartPretrainedModel), +/* harmony export */ BaseModelOutput: () => (/* binding */ BaseModelOutput), +/* harmony export */ BeitForImageClassification: () => (/* binding */ BeitForImageClassification), +/* harmony export */ BeitModel: () => (/* binding */ BeitModel), +/* harmony export */ BeitPreTrainedModel: () => (/* binding */ BeitPreTrainedModel), +/* harmony export */ BertForMaskedLM: () => (/* binding */ BertForMaskedLM), +/* harmony export */ BertForQuestionAnswering: () => (/* binding */ BertForQuestionAnswering), +/* harmony export */ BertForSequenceClassification: () => (/* binding */ BertForSequenceClassification), +/* harmony export */ BertForTokenClassification: () => (/* binding */ BertForTokenClassification), +/* harmony export */ BertModel: () => (/* binding */ BertModel), +/* harmony export */ BertPreTrainedModel: () => (/* binding */ BertPreTrainedModel), +/* harmony export */ BlenderbotForConditionalGeneration: () => (/* binding */ BlenderbotForConditionalGeneration), +/* harmony export */ BlenderbotModel: () => (/* binding */ BlenderbotModel), +/* harmony export */ BlenderbotPreTrainedModel: () => (/* binding */ BlenderbotPreTrainedModel), +/* harmony export */ BlenderbotSmallForConditionalGeneration: () => (/* binding */ BlenderbotSmallForConditionalGeneration), +/* harmony export */ BlenderbotSmallModel: () => (/* binding */ BlenderbotSmallModel), +/* harmony export */ BlenderbotSmallPreTrainedModel: () => (/* binding */ BlenderbotSmallPreTrainedModel), +/* harmony export */ BloomForCausalLM: () => (/* binding */ BloomForCausalLM), +/* harmony export */ BloomModel: () => (/* binding */ BloomModel), +/* harmony export */ BloomPreTrainedModel: () => (/* binding */ BloomPreTrainedModel), +/* harmony export */ CLIPModel: () => (/* binding */ CLIPModel), +/* harmony export */ CLIPPreTrainedModel: () => (/* binding */ CLIPPreTrainedModel), +/* harmony export */ CLIPSegForImageSegmentation: () => (/* binding */ CLIPSegForImageSegmentation), +/* harmony export */ CLIPSegModel: () => (/* binding */ CLIPSegModel), +/* harmony export */ CLIPSegPreTrainedModel: () => (/* binding */ CLIPSegPreTrainedModel), +/* harmony export */ CLIPTextModel: () => (/* binding */ CLIPTextModel), +/* harmony export */ CLIPTextModelWithProjection: () => (/* binding */ CLIPTextModelWithProjection), +/* harmony export */ CLIPVisionModel: () => (/* binding */ CLIPVisionModel), +/* harmony export */ CLIPVisionModelWithProjection: () => (/* binding */ CLIPVisionModelWithProjection), +/* harmony export */ CamembertForMaskedLM: () => (/* binding */ CamembertForMaskedLM), +/* harmony export */ CamembertForQuestionAnswering: () => (/* binding */ CamembertForQuestionAnswering), +/* harmony export */ CamembertForSequenceClassification: () => (/* binding */ CamembertForSequenceClassification), +/* harmony export */ CamembertForTokenClassification: () => (/* binding */ CamembertForTokenClassification), +/* harmony export */ CamembertModel: () => (/* binding */ CamembertModel), +/* harmony export */ CamembertPreTrainedModel: () => (/* binding */ CamembertPreTrainedModel), +/* harmony export */ CausalLMOutput: () => (/* binding */ CausalLMOutput), +/* harmony export */ CausalLMOutputWithPast: () => (/* binding */ CausalLMOutputWithPast), +/* harmony export */ ChineseCLIPModel: () => (/* binding */ ChineseCLIPModel), +/* harmony export */ ChineseCLIPPreTrainedModel: () => (/* binding */ ChineseCLIPPreTrainedModel), +/* harmony export */ ClapAudioModelWithProjection: () => (/* binding */ ClapAudioModelWithProjection), +/* harmony export */ ClapModel: () => (/* binding */ ClapModel), +/* harmony export */ ClapPreTrainedModel: () => (/* binding */ ClapPreTrainedModel), +/* harmony export */ ClapTextModelWithProjection: () => (/* binding */ ClapTextModelWithProjection), +/* harmony export */ CodeGenForCausalLM: () => (/* binding */ CodeGenForCausalLM), +/* harmony export */ CodeGenModel: () => (/* binding */ CodeGenModel), +/* harmony export */ CodeGenPreTrainedModel: () => (/* binding */ CodeGenPreTrainedModel), +/* harmony export */ CohereForCausalLM: () => (/* binding */ CohereForCausalLM), +/* harmony export */ CohereModel: () => (/* binding */ CohereModel), +/* harmony export */ CoherePreTrainedModel: () => (/* binding */ CoherePreTrainedModel), +/* harmony export */ ConvBertForMaskedLM: () => (/* binding */ ConvBertForMaskedLM), +/* harmony export */ ConvBertForQuestionAnswering: () => (/* binding */ ConvBertForQuestionAnswering), +/* harmony export */ ConvBertForSequenceClassification: () => (/* binding */ ConvBertForSequenceClassification), +/* harmony export */ ConvBertForTokenClassification: () => (/* binding */ ConvBertForTokenClassification), +/* harmony export */ ConvBertModel: () => (/* binding */ ConvBertModel), +/* harmony export */ ConvBertPreTrainedModel: () => (/* binding */ ConvBertPreTrainedModel), +/* harmony export */ ConvNextForImageClassification: () => (/* binding */ ConvNextForImageClassification), +/* harmony export */ ConvNextModel: () => (/* binding */ ConvNextModel), +/* harmony export */ ConvNextPreTrainedModel: () => (/* binding */ ConvNextPreTrainedModel), +/* harmony export */ ConvNextV2ForImageClassification: () => (/* binding */ ConvNextV2ForImageClassification), +/* harmony export */ ConvNextV2Model: () => (/* binding */ ConvNextV2Model), +/* harmony export */ ConvNextV2PreTrainedModel: () => (/* binding */ ConvNextV2PreTrainedModel), +/* harmony export */ DPTForDepthEstimation: () => (/* binding */ DPTForDepthEstimation), +/* harmony export */ DPTModel: () => (/* binding */ DPTModel), +/* harmony export */ DPTPreTrainedModel: () => (/* binding */ DPTPreTrainedModel), +/* harmony export */ DebertaForMaskedLM: () => (/* binding */ DebertaForMaskedLM), +/* harmony export */ DebertaForQuestionAnswering: () => (/* binding */ DebertaForQuestionAnswering), +/* harmony export */ DebertaForSequenceClassification: () => (/* binding */ DebertaForSequenceClassification), +/* harmony export */ DebertaForTokenClassification: () => (/* binding */ DebertaForTokenClassification), +/* harmony export */ DebertaModel: () => (/* binding */ DebertaModel), +/* harmony export */ DebertaPreTrainedModel: () => (/* binding */ DebertaPreTrainedModel), +/* harmony export */ DebertaV2ForMaskedLM: () => (/* binding */ DebertaV2ForMaskedLM), +/* harmony export */ DebertaV2ForQuestionAnswering: () => (/* binding */ DebertaV2ForQuestionAnswering), +/* harmony export */ DebertaV2ForSequenceClassification: () => (/* binding */ DebertaV2ForSequenceClassification), +/* harmony export */ DebertaV2ForTokenClassification: () => (/* binding */ DebertaV2ForTokenClassification), +/* harmony export */ DebertaV2Model: () => (/* binding */ DebertaV2Model), +/* harmony export */ DebertaV2PreTrainedModel: () => (/* binding */ DebertaV2PreTrainedModel), +/* harmony export */ DecisionTransformerModel: () => (/* binding */ DecisionTransformerModel), +/* harmony export */ DecisionTransformerPreTrainedModel: () => (/* binding */ DecisionTransformerPreTrainedModel), +/* harmony export */ DeiTForImageClassification: () => (/* binding */ DeiTForImageClassification), +/* harmony export */ DeiTModel: () => (/* binding */ DeiTModel), +/* harmony export */ DeiTPreTrainedModel: () => (/* binding */ DeiTPreTrainedModel), +/* harmony export */ DepthAnythingForDepthEstimation: () => (/* binding */ DepthAnythingForDepthEstimation), +/* harmony export */ DepthAnythingPreTrainedModel: () => (/* binding */ DepthAnythingPreTrainedModel), +/* harmony export */ DepthProForDepthEstimation: () => (/* binding */ DepthProForDepthEstimation), +/* harmony export */ DepthProPreTrainedModel: () => (/* binding */ DepthProPreTrainedModel), +/* harmony export */ DetrForObjectDetection: () => (/* binding */ DetrForObjectDetection), +/* harmony export */ DetrForSegmentation: () => (/* binding */ DetrForSegmentation), +/* harmony export */ DetrModel: () => (/* binding */ DetrModel), +/* harmony export */ DetrObjectDetectionOutput: () => (/* binding */ DetrObjectDetectionOutput), +/* harmony export */ DetrPreTrainedModel: () => (/* binding */ DetrPreTrainedModel), +/* harmony export */ DetrSegmentationOutput: () => (/* binding */ DetrSegmentationOutput), +/* harmony export */ Dinov2ForImageClassification: () => (/* binding */ Dinov2ForImageClassification), +/* harmony export */ Dinov2Model: () => (/* binding */ Dinov2Model), +/* harmony export */ Dinov2PreTrainedModel: () => (/* binding */ Dinov2PreTrainedModel), +/* harmony export */ DistilBertForMaskedLM: () => (/* binding */ DistilBertForMaskedLM), +/* harmony export */ DistilBertForQuestionAnswering: () => (/* binding */ DistilBertForQuestionAnswering), +/* harmony export */ DistilBertForSequenceClassification: () => (/* binding */ DistilBertForSequenceClassification), +/* harmony export */ DistilBertForTokenClassification: () => (/* binding */ DistilBertForTokenClassification), +/* harmony export */ DistilBertModel: () => (/* binding */ DistilBertModel), +/* harmony export */ DistilBertPreTrainedModel: () => (/* binding */ DistilBertPreTrainedModel), +/* harmony export */ DonutSwinModel: () => (/* binding */ DonutSwinModel), +/* harmony export */ DonutSwinPreTrainedModel: () => (/* binding */ DonutSwinPreTrainedModel), +/* harmony export */ EfficientNetForImageClassification: () => (/* binding */ EfficientNetForImageClassification), +/* harmony export */ EfficientNetModel: () => (/* binding */ EfficientNetModel), +/* harmony export */ EfficientNetPreTrainedModel: () => (/* binding */ EfficientNetPreTrainedModel), +/* harmony export */ ElectraForMaskedLM: () => (/* binding */ ElectraForMaskedLM), +/* harmony export */ ElectraForQuestionAnswering: () => (/* binding */ ElectraForQuestionAnswering), +/* harmony export */ ElectraForSequenceClassification: () => (/* binding */ ElectraForSequenceClassification), +/* harmony export */ ElectraForTokenClassification: () => (/* binding */ ElectraForTokenClassification), +/* harmony export */ ElectraModel: () => (/* binding */ ElectraModel), +/* harmony export */ ElectraPreTrainedModel: () => (/* binding */ ElectraPreTrainedModel), +/* harmony export */ EsmForMaskedLM: () => (/* binding */ EsmForMaskedLM), +/* harmony export */ EsmForSequenceClassification: () => (/* binding */ EsmForSequenceClassification), +/* harmony export */ EsmForTokenClassification: () => (/* binding */ EsmForTokenClassification), +/* harmony export */ EsmModel: () => (/* binding */ EsmModel), +/* harmony export */ EsmPreTrainedModel: () => (/* binding */ EsmPreTrainedModel), +/* harmony export */ FalconForCausalLM: () => (/* binding */ FalconForCausalLM), +/* harmony export */ FalconModel: () => (/* binding */ FalconModel), +/* harmony export */ FalconPreTrainedModel: () => (/* binding */ FalconPreTrainedModel), +/* harmony export */ FastViTForImageClassification: () => (/* binding */ FastViTForImageClassification), +/* harmony export */ FastViTModel: () => (/* binding */ FastViTModel), +/* harmony export */ FastViTPreTrainedModel: () => (/* binding */ FastViTPreTrainedModel), +/* harmony export */ Florence2ForConditionalGeneration: () => (/* binding */ Florence2ForConditionalGeneration), +/* harmony export */ Florence2PreTrainedModel: () => (/* binding */ Florence2PreTrainedModel), +/* harmony export */ GLPNForDepthEstimation: () => (/* binding */ GLPNForDepthEstimation), +/* harmony export */ GLPNModel: () => (/* binding */ GLPNModel), +/* harmony export */ GLPNPreTrainedModel: () => (/* binding */ GLPNPreTrainedModel), +/* harmony export */ GPT2LMHeadModel: () => (/* binding */ GPT2LMHeadModel), +/* harmony export */ GPT2Model: () => (/* binding */ GPT2Model), +/* harmony export */ GPT2PreTrainedModel: () => (/* binding */ GPT2PreTrainedModel), +/* harmony export */ GPTBigCodeForCausalLM: () => (/* binding */ GPTBigCodeForCausalLM), +/* harmony export */ GPTBigCodeModel: () => (/* binding */ GPTBigCodeModel), +/* harmony export */ GPTBigCodePreTrainedModel: () => (/* binding */ GPTBigCodePreTrainedModel), +/* harmony export */ GPTJForCausalLM: () => (/* binding */ GPTJForCausalLM), +/* harmony export */ GPTJModel: () => (/* binding */ GPTJModel), +/* harmony export */ GPTJPreTrainedModel: () => (/* binding */ GPTJPreTrainedModel), +/* harmony export */ GPTNeoForCausalLM: () => (/* binding */ GPTNeoForCausalLM), +/* harmony export */ GPTNeoModel: () => (/* binding */ GPTNeoModel), +/* harmony export */ GPTNeoPreTrainedModel: () => (/* binding */ GPTNeoPreTrainedModel), +/* harmony export */ GPTNeoXForCausalLM: () => (/* binding */ GPTNeoXForCausalLM), +/* harmony export */ GPTNeoXModel: () => (/* binding */ GPTNeoXModel), +/* harmony export */ GPTNeoXPreTrainedModel: () => (/* binding */ GPTNeoXPreTrainedModel), +/* harmony export */ Gemma2ForCausalLM: () => (/* binding */ Gemma2ForCausalLM), +/* harmony export */ Gemma2Model: () => (/* binding */ Gemma2Model), +/* harmony export */ Gemma2PreTrainedModel: () => (/* binding */ Gemma2PreTrainedModel), +/* harmony export */ GemmaForCausalLM: () => (/* binding */ GemmaForCausalLM), +/* harmony export */ GemmaModel: () => (/* binding */ GemmaModel), +/* harmony export */ GemmaPreTrainedModel: () => (/* binding */ GemmaPreTrainedModel), +/* harmony export */ GraniteForCausalLM: () => (/* binding */ GraniteForCausalLM), +/* harmony export */ GraniteModel: () => (/* binding */ GraniteModel), +/* harmony export */ GranitePreTrainedModel: () => (/* binding */ GranitePreTrainedModel), +/* harmony export */ GroupViTModel: () => (/* binding */ GroupViTModel), +/* harmony export */ GroupViTPreTrainedModel: () => (/* binding */ GroupViTPreTrainedModel), +/* harmony export */ HieraForImageClassification: () => (/* binding */ HieraForImageClassification), +/* harmony export */ HieraModel: () => (/* binding */ HieraModel), +/* harmony export */ HieraPreTrainedModel: () => (/* binding */ HieraPreTrainedModel), +/* harmony export */ HubertForCTC: () => (/* binding */ HubertForCTC), +/* harmony export */ HubertForSequenceClassification: () => (/* binding */ HubertForSequenceClassification), +/* harmony export */ HubertModel: () => (/* binding */ HubertModel), +/* harmony export */ HubertPreTrainedModel: () => (/* binding */ HubertPreTrainedModel), +/* harmony export */ ImageMattingOutput: () => (/* binding */ ImageMattingOutput), +/* harmony export */ JAISLMHeadModel: () => (/* binding */ JAISLMHeadModel), +/* harmony export */ JAISModel: () => (/* binding */ JAISModel), +/* harmony export */ JAISPreTrainedModel: () => (/* binding */ JAISPreTrainedModel), +/* harmony export */ LlamaForCausalLM: () => (/* binding */ LlamaForCausalLM), +/* harmony export */ LlamaModel: () => (/* binding */ LlamaModel), +/* harmony export */ LlamaPreTrainedModel: () => (/* binding */ LlamaPreTrainedModel), +/* harmony export */ LlavaForConditionalGeneration: () => (/* binding */ LlavaForConditionalGeneration), +/* harmony export */ LlavaPreTrainedModel: () => (/* binding */ LlavaPreTrainedModel), +/* harmony export */ LongT5ForConditionalGeneration: () => (/* binding */ LongT5ForConditionalGeneration), +/* harmony export */ LongT5Model: () => (/* binding */ LongT5Model), +/* harmony export */ LongT5PreTrainedModel: () => (/* binding */ LongT5PreTrainedModel), +/* harmony export */ M2M100ForConditionalGeneration: () => (/* binding */ M2M100ForConditionalGeneration), +/* harmony export */ M2M100Model: () => (/* binding */ M2M100Model), +/* harmony export */ M2M100PreTrainedModel: () => (/* binding */ M2M100PreTrainedModel), +/* harmony export */ MBartForCausalLM: () => (/* binding */ MBartForCausalLM), +/* harmony export */ MBartForConditionalGeneration: () => (/* binding */ MBartForConditionalGeneration), +/* harmony export */ MBartForSequenceClassification: () => (/* binding */ MBartForSequenceClassification), +/* harmony export */ MBartModel: () => (/* binding */ MBartModel), +/* harmony export */ MBartPreTrainedModel: () => (/* binding */ MBartPreTrainedModel), +/* harmony export */ MPNetForMaskedLM: () => (/* binding */ MPNetForMaskedLM), +/* harmony export */ MPNetForQuestionAnswering: () => (/* binding */ MPNetForQuestionAnswering), +/* harmony export */ MPNetForSequenceClassification: () => (/* binding */ MPNetForSequenceClassification), +/* harmony export */ MPNetForTokenClassification: () => (/* binding */ MPNetForTokenClassification), +/* harmony export */ MPNetModel: () => (/* binding */ MPNetModel), +/* harmony export */ MPNetPreTrainedModel: () => (/* binding */ MPNetPreTrainedModel), +/* harmony export */ MT5ForConditionalGeneration: () => (/* binding */ MT5ForConditionalGeneration), +/* harmony export */ MT5Model: () => (/* binding */ MT5Model), +/* harmony export */ MT5PreTrainedModel: () => (/* binding */ MT5PreTrainedModel), +/* harmony export */ MarianMTModel: () => (/* binding */ MarianMTModel), +/* harmony export */ MarianModel: () => (/* binding */ MarianModel), +/* harmony export */ MarianPreTrainedModel: () => (/* binding */ MarianPreTrainedModel), +/* harmony export */ MaskFormerForInstanceSegmentation: () => (/* binding */ MaskFormerForInstanceSegmentation), +/* harmony export */ MaskFormerModel: () => (/* binding */ MaskFormerModel), +/* harmony export */ MaskFormerPreTrainedModel: () => (/* binding */ MaskFormerPreTrainedModel), +/* harmony export */ MaskedLMOutput: () => (/* binding */ MaskedLMOutput), +/* harmony export */ MistralForCausalLM: () => (/* binding */ MistralForCausalLM), +/* harmony export */ MistralModel: () => (/* binding */ MistralModel), +/* harmony export */ MistralPreTrainedModel: () => (/* binding */ MistralPreTrainedModel), +/* harmony export */ MobileBertForMaskedLM: () => (/* binding */ MobileBertForMaskedLM), +/* harmony export */ MobileBertForQuestionAnswering: () => (/* binding */ MobileBertForQuestionAnswering), +/* harmony export */ MobileBertForSequenceClassification: () => (/* binding */ MobileBertForSequenceClassification), +/* harmony export */ MobileBertModel: () => (/* binding */ MobileBertModel), +/* harmony export */ MobileBertPreTrainedModel: () => (/* binding */ MobileBertPreTrainedModel), +/* harmony export */ MobileLLMForCausalLM: () => (/* binding */ MobileLLMForCausalLM), +/* harmony export */ MobileLLMModel: () => (/* binding */ MobileLLMModel), +/* harmony export */ MobileLLMPreTrainedModel: () => (/* binding */ MobileLLMPreTrainedModel), +/* harmony export */ MobileNetV1ForImageClassification: () => (/* binding */ MobileNetV1ForImageClassification), +/* harmony export */ MobileNetV1Model: () => (/* binding */ MobileNetV1Model), +/* harmony export */ MobileNetV1PreTrainedModel: () => (/* binding */ MobileNetV1PreTrainedModel), +/* harmony export */ MobileNetV2ForImageClassification: () => (/* binding */ MobileNetV2ForImageClassification), +/* harmony export */ MobileNetV2Model: () => (/* binding */ MobileNetV2Model), +/* harmony export */ MobileNetV2PreTrainedModel: () => (/* binding */ MobileNetV2PreTrainedModel), +/* harmony export */ MobileNetV3ForImageClassification: () => (/* binding */ MobileNetV3ForImageClassification), +/* harmony export */ MobileNetV3Model: () => (/* binding */ MobileNetV3Model), +/* harmony export */ MobileNetV3PreTrainedModel: () => (/* binding */ MobileNetV3PreTrainedModel), +/* harmony export */ MobileNetV4ForImageClassification: () => (/* binding */ MobileNetV4ForImageClassification), +/* harmony export */ MobileNetV4Model: () => (/* binding */ MobileNetV4Model), +/* harmony export */ MobileNetV4PreTrainedModel: () => (/* binding */ MobileNetV4PreTrainedModel), +/* harmony export */ MobileViTForImageClassification: () => (/* binding */ MobileViTForImageClassification), +/* harmony export */ MobileViTModel: () => (/* binding */ MobileViTModel), +/* harmony export */ MobileViTPreTrainedModel: () => (/* binding */ MobileViTPreTrainedModel), +/* harmony export */ MobileViTV2ForImageClassification: () => (/* binding */ MobileViTV2ForImageClassification), +/* harmony export */ MobileViTV2Model: () => (/* binding */ MobileViTV2Model), +/* harmony export */ MobileViTV2PreTrainedModel: () => (/* binding */ MobileViTV2PreTrainedModel), +/* harmony export */ ModelOutput: () => (/* binding */ ModelOutput), +/* harmony export */ Moondream1ForConditionalGeneration: () => (/* binding */ Moondream1ForConditionalGeneration), +/* harmony export */ MptForCausalLM: () => (/* binding */ MptForCausalLM), +/* harmony export */ MptModel: () => (/* binding */ MptModel), +/* harmony export */ MptPreTrainedModel: () => (/* binding */ MptPreTrainedModel), +/* harmony export */ MusicgenForCausalLM: () => (/* binding */ MusicgenForCausalLM), +/* harmony export */ MusicgenForConditionalGeneration: () => (/* binding */ MusicgenForConditionalGeneration), +/* harmony export */ MusicgenModel: () => (/* binding */ MusicgenModel), +/* harmony export */ MusicgenPreTrainedModel: () => (/* binding */ MusicgenPreTrainedModel), +/* harmony export */ NomicBertModel: () => (/* binding */ NomicBertModel), +/* harmony export */ NomicBertPreTrainedModel: () => (/* binding */ NomicBertPreTrainedModel), +/* harmony export */ OPTForCausalLM: () => (/* binding */ OPTForCausalLM), +/* harmony export */ OPTModel: () => (/* binding */ OPTModel), +/* harmony export */ OPTPreTrainedModel: () => (/* binding */ OPTPreTrainedModel), +/* harmony export */ OlmoForCausalLM: () => (/* binding */ OlmoForCausalLM), +/* harmony export */ OlmoModel: () => (/* binding */ OlmoModel), +/* harmony export */ OlmoPreTrainedModel: () => (/* binding */ OlmoPreTrainedModel), +/* harmony export */ OpenELMForCausalLM: () => (/* binding */ OpenELMForCausalLM), +/* harmony export */ OpenELMModel: () => (/* binding */ OpenELMModel), +/* harmony export */ OpenELMPreTrainedModel: () => (/* binding */ OpenELMPreTrainedModel), +/* harmony export */ OwlViTForObjectDetection: () => (/* binding */ OwlViTForObjectDetection), +/* harmony export */ OwlViTModel: () => (/* binding */ OwlViTModel), +/* harmony export */ OwlViTPreTrainedModel: () => (/* binding */ OwlViTPreTrainedModel), +/* harmony export */ Owlv2ForObjectDetection: () => (/* binding */ Owlv2ForObjectDetection), +/* harmony export */ Owlv2Model: () => (/* binding */ Owlv2Model), +/* harmony export */ Owlv2PreTrainedModel: () => (/* binding */ Owlv2PreTrainedModel), +/* harmony export */ Phi3ForCausalLM: () => (/* binding */ Phi3ForCausalLM), +/* harmony export */ Phi3Model: () => (/* binding */ Phi3Model), +/* harmony export */ Phi3PreTrainedModel: () => (/* binding */ Phi3PreTrainedModel), +/* harmony export */ PhiForCausalLM: () => (/* binding */ PhiForCausalLM), +/* harmony export */ PhiModel: () => (/* binding */ PhiModel), +/* harmony export */ PhiPreTrainedModel: () => (/* binding */ PhiPreTrainedModel), +/* harmony export */ PreTrainedModel: () => (/* binding */ PreTrainedModel), +/* harmony export */ PretrainedMixin: () => (/* binding */ PretrainedMixin), +/* harmony export */ PvtForImageClassification: () => (/* binding */ PvtForImageClassification), +/* harmony export */ PvtModel: () => (/* binding */ PvtModel), +/* harmony export */ PvtPreTrainedModel: () => (/* binding */ PvtPreTrainedModel), +/* harmony export */ PyAnnoteForAudioFrameClassification: () => (/* binding */ PyAnnoteForAudioFrameClassification), +/* harmony export */ PyAnnoteModel: () => (/* binding */ PyAnnoteModel), +/* harmony export */ PyAnnotePreTrainedModel: () => (/* binding */ PyAnnotePreTrainedModel), +/* harmony export */ QuestionAnsweringModelOutput: () => (/* binding */ QuestionAnsweringModelOutput), +/* harmony export */ Qwen2ForCausalLM: () => (/* binding */ Qwen2ForCausalLM), +/* harmony export */ Qwen2Model: () => (/* binding */ Qwen2Model), +/* harmony export */ Qwen2PreTrainedModel: () => (/* binding */ Qwen2PreTrainedModel), +/* harmony export */ RTDetrForObjectDetection: () => (/* binding */ RTDetrForObjectDetection), +/* harmony export */ RTDetrModel: () => (/* binding */ RTDetrModel), +/* harmony export */ RTDetrObjectDetectionOutput: () => (/* binding */ RTDetrObjectDetectionOutput), +/* harmony export */ RTDetrPreTrainedModel: () => (/* binding */ RTDetrPreTrainedModel), +/* harmony export */ ResNetForImageClassification: () => (/* binding */ ResNetForImageClassification), +/* harmony export */ ResNetModel: () => (/* binding */ ResNetModel), +/* harmony export */ ResNetPreTrainedModel: () => (/* binding */ ResNetPreTrainedModel), +/* harmony export */ RoFormerForMaskedLM: () => (/* binding */ RoFormerForMaskedLM), +/* harmony export */ RoFormerForQuestionAnswering: () => (/* binding */ RoFormerForQuestionAnswering), +/* harmony export */ RoFormerForSequenceClassification: () => (/* binding */ RoFormerForSequenceClassification), +/* harmony export */ RoFormerForTokenClassification: () => (/* binding */ RoFormerForTokenClassification), +/* harmony export */ RoFormerModel: () => (/* binding */ RoFormerModel), +/* harmony export */ RoFormerPreTrainedModel: () => (/* binding */ RoFormerPreTrainedModel), +/* harmony export */ RobertaForMaskedLM: () => (/* binding */ RobertaForMaskedLM), +/* harmony export */ RobertaForQuestionAnswering: () => (/* binding */ RobertaForQuestionAnswering), +/* harmony export */ RobertaForSequenceClassification: () => (/* binding */ RobertaForSequenceClassification), +/* harmony export */ RobertaForTokenClassification: () => (/* binding */ RobertaForTokenClassification), +/* harmony export */ RobertaModel: () => (/* binding */ RobertaModel), +/* harmony export */ RobertaPreTrainedModel: () => (/* binding */ RobertaPreTrainedModel), +/* harmony export */ SamImageSegmentationOutput: () => (/* binding */ SamImageSegmentationOutput), +/* harmony export */ SamModel: () => (/* binding */ SamModel), +/* harmony export */ SamPreTrainedModel: () => (/* binding */ SamPreTrainedModel), +/* harmony export */ SapiensForDepthEstimation: () => (/* binding */ SapiensForDepthEstimation), +/* harmony export */ SapiensForNormalEstimation: () => (/* binding */ SapiensForNormalEstimation), +/* harmony export */ SapiensForSemanticSegmentation: () => (/* binding */ SapiensForSemanticSegmentation), +/* harmony export */ SapiensPreTrainedModel: () => (/* binding */ SapiensPreTrainedModel), +/* harmony export */ SegformerForImageClassification: () => (/* binding */ SegformerForImageClassification), +/* harmony export */ SegformerForSemanticSegmentation: () => (/* binding */ SegformerForSemanticSegmentation), +/* harmony export */ SegformerModel: () => (/* binding */ SegformerModel), +/* harmony export */ SegformerPreTrainedModel: () => (/* binding */ SegformerPreTrainedModel), +/* harmony export */ Seq2SeqLMOutput: () => (/* binding */ Seq2SeqLMOutput), +/* harmony export */ SequenceClassifierOutput: () => (/* binding */ SequenceClassifierOutput), +/* harmony export */ SiglipModel: () => (/* binding */ SiglipModel), +/* harmony export */ SiglipPreTrainedModel: () => (/* binding */ SiglipPreTrainedModel), +/* harmony export */ SiglipTextModel: () => (/* binding */ SiglipTextModel), +/* harmony export */ SiglipVisionModel: () => (/* binding */ SiglipVisionModel), +/* harmony export */ SpeechT5ForSpeechToText: () => (/* binding */ SpeechT5ForSpeechToText), +/* harmony export */ SpeechT5ForTextToSpeech: () => (/* binding */ SpeechT5ForTextToSpeech), +/* harmony export */ SpeechT5HifiGan: () => (/* binding */ SpeechT5HifiGan), +/* harmony export */ SpeechT5Model: () => (/* binding */ SpeechT5Model), +/* harmony export */ SpeechT5PreTrainedModel: () => (/* binding */ SpeechT5PreTrainedModel), +/* harmony export */ SqueezeBertForMaskedLM: () => (/* binding */ SqueezeBertForMaskedLM), +/* harmony export */ SqueezeBertForQuestionAnswering: () => (/* binding */ SqueezeBertForQuestionAnswering), +/* harmony export */ SqueezeBertForSequenceClassification: () => (/* binding */ SqueezeBertForSequenceClassification), +/* harmony export */ SqueezeBertModel: () => (/* binding */ SqueezeBertModel), +/* harmony export */ SqueezeBertPreTrainedModel: () => (/* binding */ SqueezeBertPreTrainedModel), +/* harmony export */ StableLmForCausalLM: () => (/* binding */ StableLmForCausalLM), +/* harmony export */ StableLmModel: () => (/* binding */ StableLmModel), +/* harmony export */ StableLmPreTrainedModel: () => (/* binding */ StableLmPreTrainedModel), +/* harmony export */ Starcoder2ForCausalLM: () => (/* binding */ Starcoder2ForCausalLM), +/* harmony export */ Starcoder2Model: () => (/* binding */ Starcoder2Model), +/* harmony export */ Starcoder2PreTrainedModel: () => (/* binding */ Starcoder2PreTrainedModel), +/* harmony export */ Swin2SRForImageSuperResolution: () => (/* binding */ Swin2SRForImageSuperResolution), +/* harmony export */ Swin2SRModel: () => (/* binding */ Swin2SRModel), +/* harmony export */ Swin2SRPreTrainedModel: () => (/* binding */ Swin2SRPreTrainedModel), +/* harmony export */ SwinForImageClassification: () => (/* binding */ SwinForImageClassification), +/* harmony export */ SwinModel: () => (/* binding */ SwinModel), +/* harmony export */ SwinPreTrainedModel: () => (/* binding */ SwinPreTrainedModel), +/* harmony export */ T5ForConditionalGeneration: () => (/* binding */ T5ForConditionalGeneration), +/* harmony export */ T5Model: () => (/* binding */ T5Model), +/* harmony export */ T5PreTrainedModel: () => (/* binding */ T5PreTrainedModel), +/* harmony export */ TableTransformerForObjectDetection: () => (/* binding */ TableTransformerForObjectDetection), +/* harmony export */ TableTransformerModel: () => (/* binding */ TableTransformerModel), +/* harmony export */ TableTransformerObjectDetectionOutput: () => (/* binding */ TableTransformerObjectDetectionOutput), +/* harmony export */ TableTransformerPreTrainedModel: () => (/* binding */ TableTransformerPreTrainedModel), +/* harmony export */ TokenClassifierOutput: () => (/* binding */ TokenClassifierOutput), +/* harmony export */ TrOCRForCausalLM: () => (/* binding */ TrOCRForCausalLM), +/* harmony export */ TrOCRPreTrainedModel: () => (/* binding */ TrOCRPreTrainedModel), +/* harmony export */ UniSpeechForCTC: () => (/* binding */ UniSpeechForCTC), +/* harmony export */ UniSpeechForSequenceClassification: () => (/* binding */ UniSpeechForSequenceClassification), +/* harmony export */ UniSpeechModel: () => (/* binding */ UniSpeechModel), +/* harmony export */ UniSpeechPreTrainedModel: () => (/* binding */ UniSpeechPreTrainedModel), +/* harmony export */ UniSpeechSatForAudioFrameClassification: () => (/* binding */ UniSpeechSatForAudioFrameClassification), +/* harmony export */ UniSpeechSatForCTC: () => (/* binding */ UniSpeechSatForCTC), +/* harmony export */ UniSpeechSatForSequenceClassification: () => (/* binding */ UniSpeechSatForSequenceClassification), +/* harmony export */ UniSpeechSatModel: () => (/* binding */ UniSpeechSatModel), +/* harmony export */ UniSpeechSatPreTrainedModel: () => (/* binding */ UniSpeechSatPreTrainedModel), +/* harmony export */ ViTForImageClassification: () => (/* binding */ ViTForImageClassification), +/* harmony export */ ViTMAEModel: () => (/* binding */ ViTMAEModel), +/* harmony export */ ViTMAEPreTrainedModel: () => (/* binding */ ViTMAEPreTrainedModel), +/* harmony export */ ViTMSNForImageClassification: () => (/* binding */ ViTMSNForImageClassification), +/* harmony export */ ViTMSNModel: () => (/* binding */ ViTMSNModel), +/* harmony export */ ViTMSNPreTrainedModel: () => (/* binding */ ViTMSNPreTrainedModel), +/* harmony export */ ViTModel: () => (/* binding */ ViTModel), +/* harmony export */ ViTPreTrainedModel: () => (/* binding */ ViTPreTrainedModel), +/* harmony export */ VisionEncoderDecoderModel: () => (/* binding */ VisionEncoderDecoderModel), +/* harmony export */ VitMatteForImageMatting: () => (/* binding */ VitMatteForImageMatting), +/* harmony export */ VitMattePreTrainedModel: () => (/* binding */ VitMattePreTrainedModel), +/* harmony export */ VitsModel: () => (/* binding */ VitsModel), +/* harmony export */ VitsModelOutput: () => (/* binding */ VitsModelOutput), +/* harmony export */ VitsPreTrainedModel: () => (/* binding */ VitsPreTrainedModel), +/* harmony export */ Wav2Vec2BertForCTC: () => (/* binding */ Wav2Vec2BertForCTC), +/* harmony export */ Wav2Vec2BertForSequenceClassification: () => (/* binding */ Wav2Vec2BertForSequenceClassification), +/* harmony export */ Wav2Vec2BertModel: () => (/* binding */ Wav2Vec2BertModel), +/* harmony export */ Wav2Vec2BertPreTrainedModel: () => (/* binding */ Wav2Vec2BertPreTrainedModel), +/* harmony export */ Wav2Vec2ForAudioFrameClassification: () => (/* binding */ Wav2Vec2ForAudioFrameClassification), +/* harmony export */ Wav2Vec2ForCTC: () => (/* binding */ Wav2Vec2ForCTC), +/* harmony export */ Wav2Vec2ForSequenceClassification: () => (/* binding */ Wav2Vec2ForSequenceClassification), +/* harmony export */ Wav2Vec2Model: () => (/* binding */ Wav2Vec2Model), +/* harmony export */ Wav2Vec2PreTrainedModel: () => (/* binding */ Wav2Vec2PreTrainedModel), +/* harmony export */ WavLMForAudioFrameClassification: () => (/* binding */ WavLMForAudioFrameClassification), +/* harmony export */ WavLMForCTC: () => (/* binding */ WavLMForCTC), +/* harmony export */ WavLMForSequenceClassification: () => (/* binding */ WavLMForSequenceClassification), +/* harmony export */ WavLMForXVector: () => (/* binding */ WavLMForXVector), +/* harmony export */ WavLMModel: () => (/* binding */ WavLMModel), +/* harmony export */ WavLMPreTrainedModel: () => (/* binding */ WavLMPreTrainedModel), +/* harmony export */ WeSpeakerResNetModel: () => (/* binding */ WeSpeakerResNetModel), +/* harmony export */ WeSpeakerResNetPreTrainedModel: () => (/* binding */ WeSpeakerResNetPreTrainedModel), +/* harmony export */ WhisperForConditionalGeneration: () => (/* binding */ WhisperForConditionalGeneration), +/* harmony export */ WhisperModel: () => (/* binding */ WhisperModel), +/* harmony export */ WhisperPreTrainedModel: () => (/* binding */ WhisperPreTrainedModel), +/* harmony export */ XLMForQuestionAnswering: () => (/* binding */ XLMForQuestionAnswering), +/* harmony export */ XLMForSequenceClassification: () => (/* binding */ XLMForSequenceClassification), +/* harmony export */ XLMForTokenClassification: () => (/* binding */ XLMForTokenClassification), +/* harmony export */ XLMModel: () => (/* binding */ XLMModel), +/* harmony export */ XLMPreTrainedModel: () => (/* binding */ XLMPreTrainedModel), +/* harmony export */ XLMRobertaForMaskedLM: () => (/* binding */ XLMRobertaForMaskedLM), +/* harmony export */ XLMRobertaForQuestionAnswering: () => (/* binding */ XLMRobertaForQuestionAnswering), +/* harmony export */ XLMRobertaForSequenceClassification: () => (/* binding */ XLMRobertaForSequenceClassification), +/* harmony export */ XLMRobertaForTokenClassification: () => (/* binding */ XLMRobertaForTokenClassification), +/* harmony export */ XLMRobertaModel: () => (/* binding */ XLMRobertaModel), +/* harmony export */ XLMRobertaPreTrainedModel: () => (/* binding */ XLMRobertaPreTrainedModel), +/* harmony export */ XLMWithLMHeadModel: () => (/* binding */ XLMWithLMHeadModel), +/* harmony export */ XVectorOutput: () => (/* binding */ XVectorOutput), +/* harmony export */ YolosForObjectDetection: () => (/* binding */ YolosForObjectDetection), +/* harmony export */ YolosModel: () => (/* binding */ YolosModel), +/* harmony export */ YolosObjectDetectionOutput: () => (/* binding */ YolosObjectDetectionOutput), +/* harmony export */ YolosPreTrainedModel: () => (/* binding */ YolosPreTrainedModel) +/* harmony export */ }); +/* harmony import */ var _configs_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./configs.js */ "./src/configs.js"); +/* harmony import */ var _backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./backends/onnx.js */ "./src/backends/onnx.js"); +/* harmony import */ var _utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./utils/dtypes.js */ "./src/utils/dtypes.js"); +/* harmony import */ var _utils_generic_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./utils/generic.js */ "./src/utils/generic.js"); +/* harmony import */ var _utils_core_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ./utils/core.js */ "./src/utils/core.js"); +/* harmony import */ var _utils_hub_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ./utils/hub.js */ "./src/utils/hub.js"); +/* harmony import */ var _utils_constants_js__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! ./utils/constants.js */ "./src/utils/constants.js"); +/* harmony import */ var _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__(/*! ./generation/logits_process.js */ "./src/generation/logits_process.js"); +/* harmony import */ var _generation_configuration_utils_js__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__(/*! ./generation/configuration_utils.js */ "./src/generation/configuration_utils.js"); +/* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__ = __webpack_require__(/*! ./utils/tensor.js */ "./src/utils/tensor.js"); +/* harmony import */ var _utils_maths_js__WEBPACK_IMPORTED_MODULE_10__ = __webpack_require__(/*! ./utils/maths.js */ "./src/utils/maths.js"); +/* harmony import */ var _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_11__ = __webpack_require__(/*! ./generation/stopping_criteria.js */ "./src/generation/stopping_criteria.js"); +/* harmony import */ var _generation_logits_sampler_js__WEBPACK_IMPORTED_MODULE_12__ = __webpack_require__(/*! ./generation/logits_sampler.js */ "./src/generation/logits_sampler.js"); +/* harmony import */ var _env_js__WEBPACK_IMPORTED_MODULE_13__ = __webpack_require__(/*! ./env.js */ "./src/env.js"); +/* harmony import */ var _models_whisper_generation_whisper_js__WEBPACK_IMPORTED_MODULE_14__ = __webpack_require__(/*! ./models/whisper/generation_whisper.js */ "./src/models/whisper/generation_whisper.js"); +/* harmony import */ var _models_whisper_common_whisper_js__WEBPACK_IMPORTED_MODULE_15__ = __webpack_require__(/*! ./models/whisper/common_whisper.js */ "./src/models/whisper/common_whisper.js"); + +/** + * @file Definitions of all models available in Transformers.js. + * + * **Example:** Load and run an `AutoModel`. + * + * ```javascript + * import { AutoModel, AutoTokenizer } from '@huggingface/transformers'; + * + * let tokenizer = await AutoTokenizer.from_pretrained('Xenova/bert-base-uncased'); + * let model = await AutoModel.from_pretrained('Xenova/bert-base-uncased'); + * + * let inputs = await tokenizer('I love transformers!'); + * let { logits } = await model(inputs); + * // Tensor { + * // data: Float32Array(183132) [-7.117443084716797, -7.107812881469727, -7.092104911804199, ...] + * // dims: (3) [1, 6, 30522], + * // type: "float32", + * // size: 183132, + * // } + * ``` + * + * We also provide other `AutoModel`s (listed below), which you can use in the same way as the Python library. For example: + * + * **Example:** Load and run an `AutoModelForSeq2SeqLM`. + * ```javascript + * import { AutoModelForSeq2SeqLM, AutoTokenizer } from '@huggingface/transformers'; + * + * let tokenizer = await AutoTokenizer.from_pretrained('Xenova/t5-small'); + * let model = await AutoModelForSeq2SeqLM.from_pretrained('Xenova/t5-small'); + * + * let { input_ids } = await tokenizer('translate English to German: I love transformers!'); + * let outputs = await model.generate(input_ids); + * let decoded = tokenizer.decode(outputs[0], { skip_special_tokens: true }); + * // 'Ich liebe Transformatoren!' + * ``` + * + * @module models + */ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +////////////////////////////////////////////////// +// Model types: used internally +const MODEL_TYPES = { + EncoderOnly: 0, + EncoderDecoder: 1, + Seq2Seq: 2, + Vision2Seq: 3, + DecoderOnly: 4, + MaskGeneration: 5, + ImageTextToText: 6, + Musicgen: 7, +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Helper functions + +// NOTE: These will be populated fully later +const MODEL_TYPE_MAPPING = new Map(); +const MODEL_NAME_TO_CLASS_MAPPING = new Map(); +const MODEL_CLASS_TO_NAME_MAPPING = new Map(); + + +/** + * Constructs an InferenceSession using a model file located at the specified path. + * @param {string} pretrained_model_name_or_path The path to the directory containing the model file. + * @param {string} fileName The name of the model file. + * @param {import('./utils/hub.js').PretrainedModelOptions} options Additional options for loading the model. + * @returns {Promise<{buffer: Uint8Array, session_options: Object, session_config: Object}>} A Promise that resolves to the data needed to create an InferenceSession object. + * @private + */ +async function getSession(pretrained_model_name_or_path, fileName, options) { + const custom_config = options.config?.['transformers.js_config'] ?? {}; + let device = options.device ?? custom_config.device; + if (device && typeof device !== 'string') { + if (device.hasOwnProperty(fileName)) { + device = device[fileName]; + } else { + console.warn(`device not specified for "${fileName}". Using the default device.`); + device = null; + } + } + + // If the device is not specified, we use the default (supported) execution providers. + const selectedDevice = /** @type {import("./utils/devices.js").DeviceType} */( + device ?? (_env_js__WEBPACK_IMPORTED_MODULE_13__.apis.IS_NODE_ENV ? 'cpu' : 'wasm') + ); + const executionProviders = (0,_backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.deviceToExecutionProviders)(selectedDevice); + + // If options.dtype is specified, we use it to choose the suffix for the model file. + // Otherwise, we use the default dtype for the device. + let dtype = options.dtype ?? custom_config.dtype; + if (typeof dtype !== 'string') { + if (dtype && dtype.hasOwnProperty(fileName)) { + dtype = dtype[fileName]; + } else { + dtype = _utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DEFAULT_DEVICE_DTYPE_MAPPING[selectedDevice] ?? _utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DATA_TYPES.fp32; + console.warn(`dtype not specified for "${fileName}". Using the default dtype (${dtype}) for this device (${selectedDevice}).`); + } + } + + const selectedDtype = /** @type {import("./utils/dtypes.js").DataType} */(dtype); + + if (!_utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DEFAULT_DTYPE_SUFFIX_MAPPING.hasOwnProperty(selectedDtype)) { + throw new Error(`Invalid dtype: ${selectedDtype}. Should be one of: ${Object.keys(_utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DATA_TYPES).join(', ')}`); + } else if (selectedDtype === _utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DATA_TYPES.fp16 && selectedDevice === 'webgpu' && !(await (0,_utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.isWebGpuFp16Supported)())) { + throw new Error(`The device (${selectedDevice}) does not support fp16.`); + } + + // Only valid for models with a decoder + const kv_cache_dtype = custom_config.kv_cache_dtype + ? (typeof custom_config.kv_cache_dtype === 'string' + ? custom_config.kv_cache_dtype + : custom_config.kv_cache_dtype[selectedDtype] ?? 'float32') + : undefined; + + if (kv_cache_dtype && !['float32', 'float16'].includes(kv_cache_dtype)) { + throw new Error(`Invalid kv_cache_dtype: ${kv_cache_dtype}. Should be one of: float32, float16`); + } + + const session_config = { + dtype: selectedDtype, + kv_cache_dtype, + } + + // Construct the model file name + const suffix = _utils_dtypes_js__WEBPACK_IMPORTED_MODULE_2__.DEFAULT_DTYPE_SUFFIX_MAPPING[selectedDtype]; + const modelFileName = `${options.subfolder ?? ''}/${fileName}${suffix}.onnx`; + + const session_options = { ...options.session_options }; + + // Overwrite `executionProviders` if not specified + session_options.executionProviders ??= executionProviders; + + // Overwrite `freeDimensionOverrides` if specified in config and not set in session options + const free_dimension_overrides = custom_config.free_dimension_overrides; + if (free_dimension_overrides) { + session_options.freeDimensionOverrides ??= free_dimension_overrides; + } else if (selectedDevice.startsWith('webnn') && !session_options.freeDimensionOverrides) { + console.warn( + 'WebNN does not currently support dynamic shapes and requires `free_dimension_overrides` to be set in config.json as a field within "transformers.js_config". ' + + 'When `free_dimension_overrides` is not set, you may experience significant performance degradation.' + ); + } + + const bufferPromise = (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_5__.getModelFile)(pretrained_model_name_or_path, modelFileName, true, options); + + // handle onnx external data files + const use_external_data_format = options.use_external_data_format ?? custom_config.use_external_data_format; + /** @type {Promise<{path: string, data: Uint8Array}>[]} */ + let externalDataPromises = []; + if (use_external_data_format && ( + use_external_data_format === true || + ( + typeof use_external_data_format === 'object' && + use_external_data_format.hasOwnProperty(fileName) && + use_external_data_format[fileName] === true + ) + )) { + if (_env_js__WEBPACK_IMPORTED_MODULE_13__.apis.IS_NODE_ENV) { + throw new Error('External data format is not yet supported in Node.js'); + } + const path = `${fileName}${suffix}.onnx_data`; + const fullPath = `${options.subfolder ?? ''}/${path}`; + externalDataPromises.push(new Promise(async (resolve, reject) => { + const data = await (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_5__.getModelFile)(pretrained_model_name_or_path, fullPath, true, options); + resolve({ path, data }) + })); + + } else if (session_options.externalData !== undefined) { + externalDataPromises = session_options.externalData.map(async (ext) => { + // if the external data is a string, fetch the file and replace the string with its content + if (typeof ext.data === "string") { + const ext_buffer = await (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_5__.getModelFile)(pretrained_model_name_or_path, ext.data, true, options); + return { ...ext, data: ext_buffer }; + } + return ext; + }); + } + + if (externalDataPromises.length > 0) { + session_options.externalData = await Promise.all(externalDataPromises); + } + + if (selectedDevice === 'webgpu') { + const shapes = (0,_configs_js__WEBPACK_IMPORTED_MODULE_0__.getKeyValueShapes)(options.config, { + prefix: 'present', + }); + if (Object.keys(shapes).length > 0 && !(0,_backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.isONNXProxy)()) { + // Only set preferredOutputLocation if shapes are present and we aren't proxying ONNX + /** @type {Record} */ + const preferredOutputLocation = {}; + for (const key in shapes) { + preferredOutputLocation[key] = 'gpu-buffer'; + } + session_options.preferredOutputLocation = preferredOutputLocation; + } + } + + const buffer = await bufferPromise; + + return { buffer, session_options, session_config }; +} + +/** + * Helper function to create multiple InferenceSession objects. + * + * @param {string} pretrained_model_name_or_path The path to the directory containing the model file. + * @param {Record} names The names of the model files to load. + * @param {import('./utils/hub.js').PretrainedModelOptions} options Additional options for loading the model. + * @returns {Promise>} A Promise that resolves to a dictionary of InferenceSession objects. + * @private + */ +async function constructSessions(pretrained_model_name_or_path, names, options) { + return Object.fromEntries(await Promise.all( + Object.keys(names).map(async (name) => { + const { buffer, session_options, session_config } = await getSession(pretrained_model_name_or_path, names[name], options); + const session = await (0,_backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.createInferenceSession)(buffer, session_options, session_config); + return [name, session]; + }) + )); +} + +/** + * Helper function to load multiple optional configuration files + * @param {string} pretrained_model_name_or_path The path to the directory containing the config file. + * @param {Record} names The names of the config files to load. + * @param {import('./utils/hub.js').PretrainedModelOptions} options Additional options for loading the configs. + * @returns {Promise>} A Promise that resolves to a dictionary of configuration objects. + * @private + */ +async function getOptionalConfigs(pretrained_model_name_or_path, names, options) { + return Object.fromEntries(await Promise.all( + Object.keys(names).map(async (name) => { + const config = await (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_5__.getModelJSON)(pretrained_model_name_or_path, names[name], false, options); + return [name, config]; + }) + )); +} + +/** + * Validate model inputs + * @param {Object} session The InferenceSession object that will be run. + * @param {Object} inputs The inputs to check. + * @returns {Record} The checked inputs. + * @throws {Error} If any inputs are missing. + * @private + */ +function validateInputs(session, inputs) { + /** + * NOTE: Create either a shallow or deep copy based on `onnx.wasm.proxy` + * @type {Record} + */ + const checkedInputs = Object.create(null); + const missingInputs = []; + for (const inputName of session.inputNames) { + const tensor = inputs[inputName]; + // Rare case where one of the model's input names corresponds to a built-in + // object name (e.g., toString), which would cause a simple (!tensor) check to fail, + // because it's not undefined but a function. + if (!(tensor instanceof _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor)) { + missingInputs.push(inputName); + continue; + } + // NOTE: When `env.wasm.proxy is true` the tensor is moved across the Worker + // boundary, transferring ownership to the worker and invalidating the tensor. + // So, in this case, we simply sacrifice a clone for it. + checkedInputs[inputName] = (0,_backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.isONNXProxy)() ? tensor.clone() : tensor; + } + if (missingInputs.length > 0) { + throw new Error( + `An error occurred during model execution: "Missing the following inputs: ${missingInputs.join(', ')}.`); + } + + const numInputsProvided = Object.keys(inputs).length; + const numInputsNeeded = session.inputNames.length; + if (numInputsProvided > numInputsNeeded) { + // No missing inputs, but too many inputs were provided. + // Warn the user and ignore the extra inputs. + let ignored = Object.keys(inputs).filter(inputName => !session.inputNames.includes(inputName)); + console.warn(`WARNING: Too many inputs were provided (${numInputsProvided} > ${numInputsNeeded}). The following inputs will be ignored: "${ignored.join(', ')}".`); + } + + return checkedInputs; +} + +/** + * Executes an InferenceSession using the specified inputs. + * NOTE: `inputs` must contain at least the input names of the model. + * - If additional inputs are passed, they will be ignored. + * - If inputs are missing, an error will be thrown. + * + * @param {Object} session The InferenceSession object to run. + * @param {Object} inputs An object that maps input names to input tensors. + * @returns {Promise} A Promise that resolves to an object that maps output names to output tensors. + * @private + */ +async function sessionRun(session, inputs) { + const checkedInputs = validateInputs(session, inputs); + try { + // pass the original ort tensor + const ortFeed = Object.fromEntries(Object.entries(checkedInputs).map(([k, v]) => [k, v.ort_tensor])); + let output = await session.run(ortFeed); + output = replaceTensors(output); + return output; + } catch (e) { + // This usually occurs when the inputs are of the wrong type. + console.error(`An error occurred during model execution: "${e}".`); + console.error('Inputs given to model:', checkedInputs); + throw e; + } +} + +/** + * Replaces ONNX Tensor objects with custom Tensor objects to support additional functions. + * @param {Object} obj The object to replace tensor objects in. + * @returns {Object} The object with tensor objects replaced by custom Tensor objects. + * @private + */ +function replaceTensors(obj) { + for (let prop in obj) { + if ((0,_backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.isONNXTensor)(obj[prop])) { + obj[prop] = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor(obj[prop]); + } else if (typeof obj[prop] === 'object') { + replaceTensors(obj[prop]); + } + } + return obj; +} + + +/** + * Converts an array or Tensor of integers to an int64 Tensor. + * @param {any[]|Tensor} items The input integers to be converted. + * @returns {Tensor} The int64 Tensor with the converted values. + * @throws {Error} If the input array is empty or the input is a batched Tensor and not all sequences have the same length. + * @private + */ +function toI64Tensor(items) { + if (items instanceof _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor) { + return items; + } + // items is an array + if (items.length === 0) { + throw Error("items must be non-empty"); + } + + if (Array.isArray(items[0])) { + // batched + if (items.some(x => x.length !== items[0].length)) { + throw Error("Unable to create tensor, you should probably activate truncation and/or padding with 'padding=True' and/or 'truncation=True' to have batched tensors with the same length.") + } + + return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor('int64', + BigInt64Array.from(items.flat().map(x => BigInt(x))), + [items.length, items[0].length] + ); + } else { + //flat + return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor('int64', + BigInt64Array.from(items.map(x => BigInt(x))), + [1, items.length] + ); + } +} + +/** + * Creates a boolean tensor with a single value. + * @param {boolean} value The value of the tensor. + * @returns {Tensor} The boolean tensor. + * @private + */ +function boolTensor(value) { + return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor('bool', [value], [1]); +} + +// JS doesn't support mixins, so we define some reused functions here, and allow "this" to be passed in +/** + * Perform forward pass on the seq2seq model (both encoder and decoder). + * @param {Object} self The seq2seq model object. + * @param {Object} model_inputs The input object for the model containing encoder and decoder inputs. + * @returns {Promise} Promise that resolves with the output of the seq2seq model. + * @private + */ +async function seq2seqForward(self, model_inputs) { + let { encoder_outputs, input_ids, decoder_input_ids, ...other_decoder_inputs } = model_inputs; + // Encode if needed + if (!encoder_outputs) { + const encoder_inputs = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.pick)(model_inputs, self.sessions['model'].inputNames); + // Encoder outputs are not given, so we must compute them. + encoder_outputs = (await encoderForward(self, encoder_inputs)).last_hidden_state; + } + + other_decoder_inputs.input_ids = decoder_input_ids; + other_decoder_inputs.encoder_hidden_states = encoder_outputs; + + if (self.sessions['decoder_model_merged'].inputNames.includes('encoder_attention_mask')) { + other_decoder_inputs.encoder_attention_mask = model_inputs.attention_mask + } + + const decoderResults = await decoderForward(self, other_decoder_inputs, true); + + return decoderResults; +} + +/** + * Forward pass of an encoder model. + * @param {Object} self The encoder model. + * @param {Object} model_inputs The input data to be used for the forward pass. + * @returns {Promise} The model's outputs. + * @private + */ +async function encoderForward(self, model_inputs) { + const session = self.sessions['model']; + const encoderFeeds = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.pick)(model_inputs, session.inputNames); + + if (session.inputNames.includes('inputs_embeds') && !encoderFeeds.inputs_embeds) { + if (!model_inputs.input_ids) { + throw new Error('Both `input_ids` and `inputs_embeds` are missing in the model inputs.'); + } + encoderFeeds.inputs_embeds = await self.encode_text({ input_ids: model_inputs.input_ids }); + } + if (session.inputNames.includes('token_type_ids') && !encoderFeeds.token_type_ids) { + // Assign default `token_type_ids` (all zeroes) to the `encoderFeeds` if the model expects it, + // but they weren't created by the tokenizer. + encoderFeeds.token_type_ids = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor( + 'int64', + new BigInt64Array(encoderFeeds.input_ids.data.length), + encoderFeeds.input_ids.dims + ) + } + return await sessionRun(session, encoderFeeds); +} + +/** + * Forward pass of a decoder model. + * @param {Object} self The decoder model. + * @param {Object} model_inputs The input data to be used for the forward pass. + * @returns {Promise} The logits and past key values. + * @private + */ +async function decoderForward(self, model_inputs, is_encoder_decoder = false) { + + const session = self.sessions[ + is_encoder_decoder ? 'decoder_model_merged' : 'model' + ] + + const { past_key_values, ...new_model_inputs } = model_inputs; + + if (session.inputNames.includes('use_cache_branch')) { + new_model_inputs.use_cache_branch = boolTensor(!!past_key_values); + } + if (session.inputNames.includes('position_ids') && new_model_inputs.attention_mask && !new_model_inputs.position_ids) { + new_model_inputs.position_ids = createPositionIds(new_model_inputs, past_key_values); + } + + // Unpack the `past_key_values` object into model inputs + self.addPastKeyValues(new_model_inputs, past_key_values); + + // Select only the inputs that are needed for the current session + const fixed = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.pick)(new_model_inputs, session.inputNames); + return await sessionRun(session, fixed); +} + + +/** + * Forward pass of an image-text-to-text model. + * @param {Object} self The image-text-to-text model model. + * @param {Object} model_inputs The input data to be used for the forward pass. + * @param {Tensor} [model_inputs.input_ids=null] + * @param {Tensor} [model_inputs.attention_mask=null] + * @param {Tensor} [model_inputs.pixel_values=null] + * @param {Tensor} [model_inputs.position_ids=null] + * @param {Tensor} [model_inputs.inputs_embeds=null] + * @param {Tensor} [model_inputs.past_key_values=null] + * @param {Object} [model_inputs.generation_config=null] + * @param {Object} [model_inputs.logits_processor=null] + * @returns {Promise} The model's output tensor + * @private + */ +async function imageTextToTextForward(self, { + // Produced by the tokenizer/processor: + input_ids = null, + attention_mask = null, + pixel_values = null, + + // Used during generation: + position_ids = null, + inputs_embeds = null, + past_key_values = null, + + // Generic generation parameters + generation_config = null, + logits_processor = null, + + // TODO: needed? + ...kwargs +}) { + + if (!inputs_embeds) { + // 1. Extract the input embeddings + inputs_embeds = await self.encode_text({ input_ids }); + + // 2. Possibly, merge text and images + if (pixel_values && input_ids.dims[1] !== 1) { + const image_features = await self.encode_image({ pixel_values }); + + ({ inputs_embeds, attention_mask } = self._merge_input_ids_with_image_features({ + image_features, + inputs_embeds, + input_ids, + attention_mask, + })); + + } else if (past_key_values && pixel_values && input_ids.dims[1] === 1) { + // This is the case when we are generating with cache + const target_length = input_ids.dims[1]; // always 1 + const past_length = Object.values(past_key_values)[0].dims.at(-2); + + attention_mask = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)([ + (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.ones)([input_ids.dims[0], past_length]), + attention_mask.slice(null, [attention_mask.dims[1] - target_length, attention_mask.dims[1]]), + ], 1); + } + } + + const outputs = await decoderForward(self, { + inputs_embeds, + past_key_values, + attention_mask, + position_ids, + generation_config, + logits_processor, + }, true); + return outputs; +} + +function createPositionIds(model_inputs, past_key_values = null) { + // If the model supports providing position_ids, we create position_ids on the fly for batch generation, + // by computing the cumulative sum of the attention mask along the sequence length dimension. + // + // Equivalent to: + // position_ids = attention_mask.long().cumsum(-1) - 1 + // position_ids.masked_fill_(attention_mask == 0, 1) + // if past_key_values: + // position_ids = position_ids[:, -input_ids.shape[1] :] + const { input_ids, inputs_embeds, attention_mask } = model_inputs; + const [bz, seq_len] = attention_mask.dims; + + const data = new BigInt64Array(attention_mask.data.length); + for (let i = 0; i < bz; ++i) { + const start = i * seq_len; + let sum = BigInt(0); + for (let j = 0; j < seq_len; ++j) { + const index = start + j; + if (attention_mask.data[index] === 0n) { + data[index] = BigInt(1); + } else { // === 1n + data[index] = sum; + sum += attention_mask.data[index]; + } + } + } + + let position_ids = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor('int64', data, attention_mask.dims); + if (past_key_values) { + const offset = -(input_ids ?? inputs_embeds).dims.at(1); + position_ids = position_ids.slice(null, [offset, null]); + } + return position_ids; +} + +function decoder_prepare_inputs_for_generation(self, input_ids, model_inputs, generation_config) { + if (model_inputs.past_key_values) { + const past_length = Object.values(model_inputs.past_key_values)[0].dims.at(-2); + const { input_ids, attention_mask } = model_inputs; + + // Keep only the unprocessed tokens: + // 1 - If the length of the attention_mask exceeds the length of input_ids, then we are in a setting where + // some of the inputs are exclusively passed as part of the cache (e.g. when passing input_embeds as + // input) + if (attention_mask && attention_mask.dims[1] > input_ids.dims[1]) { + // NOTE: not needed since we only pass the generated tokens to the next forward pass + // const offset = -(attention_mask.dims[1] - past_length); + // model_inputs.input_ids = input_ids.slice(null, [offset, null]); + } + // 2 - If the past_length is smaller than input_ids', then input_ids holds all input tokens. + // We can discard input_ids based on the past_length. + else if (past_length < input_ids.dims[1]) { + // NOTE: Required for phi models. + // See https://github.com/huggingface/transformers/issues/30809#issuecomment-2111918479 for more information. + model_inputs.input_ids = input_ids.slice(null, [past_length, null]); + } + // 3 - Otherwise (past_length >= input_ids.shape[1]), let's assume input_ids only has unprocessed tokens. + else { + if ( + // NOTE: Only used by VLMs (!= so that null matches undefined) + self.config.image_token_index != null && + // Equivalent to `self.config.image_token_index in input_ids` (== so that int matches bigint) + input_ids.data.some(x => x == self.config.image_token_index) + ) { + // TODO: Support multiple image tokens + const num_image_tokens = self.config.num_image_tokens; + if (!num_image_tokens) { + throw new Error('`num_image_tokens` is missing in the model configuration.'); + } + + const num_new_tokens = input_ids.dims[1] - (past_length - num_image_tokens); + model_inputs.input_ids = input_ids.slice(null, [-num_new_tokens, null]); + + // TODO: The attention mask should be formed from the attention mask passed in model_inputs + model_inputs.attention_mask = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.ones)([1, past_length + num_new_tokens]); + } + } + } + + return model_inputs; +} + +function encoder_decoder_prepare_inputs_for_generation(self, input_ids, model_inputs, generation_config) { + if (model_inputs.past_key_values) { + input_ids = input_ids.map(x => [x.at(-1)]); + } + + return { + ...model_inputs, + decoder_input_ids: toI64Tensor(input_ids), + }; +} + +function image_text_to_text_prepare_inputs_for_generation(self, ...args) { + if (self.config.is_encoder_decoder) { + return encoder_decoder_prepare_inputs_for_generation(self, ...args); + } else { + return decoder_prepare_inputs_for_generation(self, ...args); + } +} + +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +/** + * A base class for pre-trained models that provides the model configuration and an ONNX session. + */ +class PreTrainedModel extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_3__.Callable { + main_input_name = 'input_ids'; + forward_params = ['input_ids', 'attention_mask']; + /** + * Creates a new instance of the `PreTrainedModel` class. + * @param {import('./configs.js').PretrainedConfig} config The model configuration. + * @param {Record} sessions The inference sessions for the model. + * @param {Record} configs Additional configuration files (e.g., generation_config.json). + */ + constructor(config, sessions, configs) { + super(); + + this.config = config; + this.sessions = sessions; + this.configs = configs; + + const modelName = MODEL_CLASS_TO_NAME_MAPPING.get(this.constructor); + const modelType = MODEL_TYPE_MAPPING.get(modelName); + + this.can_generate = false; + this._forward = null; + + this._prepare_inputs_for_generation = null; + switch (modelType) { + case MODEL_TYPES.DecoderOnly: + this.can_generate = true; + this._forward = decoderForward; + this._prepare_inputs_for_generation = decoder_prepare_inputs_for_generation; + break; + case MODEL_TYPES.Seq2Seq: + case MODEL_TYPES.Vision2Seq: + case MODEL_TYPES.Musicgen: + this.can_generate = true; + + this._forward = seq2seqForward; + this._prepare_inputs_for_generation = encoder_decoder_prepare_inputs_for_generation; + break; + + case MODEL_TYPES.EncoderDecoder: + this._forward = seq2seqForward; + break; + case MODEL_TYPES.ImageTextToText: + this.can_generate = true; + this._forward = imageTextToTextForward; + this._prepare_inputs_for_generation = image_text_to_text_prepare_inputs_for_generation; + break; + + default: + // should be MODEL_TYPES.EncoderOnly + this._forward = encoderForward; + break; + } + + if (this.can_generate) { + this.forward_params.push('past_key_values'); + } + + /** @type {import('./configs.js').TransformersJSConfig} */ + this.custom_config = this.config['transformers.js_config'] ?? {}; + } + + /** + * Disposes of all the ONNX sessions that were created during inference. + * @returns {Promise} An array of promises, one for each ONNX session that is being disposed. + * @todo Use https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/FinalizationRegistry + */ + async dispose() { + const promises = []; + for (const session of Object.values(this.sessions)) { + if (session?.handler?.dispose) { + promises.push(session.handler.dispose()) + } + } + return await Promise.all(promises); + } + + /** + * Instantiate one of the model classes of the library from a pretrained model. + * + * The model class to instantiate is selected based on the `model_type` property of the config object + * (either passed as an argument or loaded from `pretrained_model_name_or_path` if possible) + * + * @param {string} pretrained_model_name_or_path The name or path of the pretrained model. Can be either: + * - A string, the *model id* of a pretrained model hosted inside a model repo on huggingface.co. + * Valid model ids can be located at the root-level, like `bert-base-uncased`, or namespaced under a + * user or organization name, like `dbmdz/bert-base-german-cased`. + * - A path to a *directory* containing model weights, e.g., `./my_model_directory/`. + * @param {import('./utils/hub.js').PretrainedModelOptions} options Additional options for loading the model. + * + * @returns {Promise} A new instance of the `PreTrainedModel` class. + */ + static async from_pretrained(pretrained_model_name_or_path, { + progress_callback = null, + config = null, + cache_dir = null, + local_files_only = false, + revision = 'main', + model_file_name = null, + subfolder = 'onnx', + device = null, + dtype = null, + use_external_data_format = null, + session_options = {}, + } = {}) { + + let options = { + progress_callback, + config, + cache_dir, + local_files_only, + revision, + model_file_name, + subfolder, + device, + dtype, + use_external_data_format, + session_options, + } + + const modelName = MODEL_CLASS_TO_NAME_MAPPING.get(this); + const modelType = MODEL_TYPE_MAPPING.get(modelName); + + config = options.config = await _configs_js__WEBPACK_IMPORTED_MODULE_0__.AutoConfig.from_pretrained(pretrained_model_name_or_path, options); + + let info; + if (modelType === MODEL_TYPES.DecoderOnly) { + info = await Promise.all([ + constructSessions(pretrained_model_name_or_path, { + model: options.model_file_name ?? 'model', + }, options), + getOptionalConfigs(pretrained_model_name_or_path, { + generation_config: 'generation_config.json', + }, options), + ]); + + } else if (modelType === MODEL_TYPES.Seq2Seq || modelType === MODEL_TYPES.Vision2Seq) { + info = await Promise.all([ + constructSessions(pretrained_model_name_or_path, { + model: 'encoder_model', + decoder_model_merged: 'decoder_model_merged', + }, options), + getOptionalConfigs(pretrained_model_name_or_path, { + generation_config: 'generation_config.json', + }, options), + ]); + + } else if (modelType === MODEL_TYPES.MaskGeneration) { + info = await Promise.all([ + constructSessions(pretrained_model_name_or_path, { + model: 'vision_encoder', + prompt_encoder_mask_decoder: 'prompt_encoder_mask_decoder', + }, options), + ]); + + } else if (modelType === MODEL_TYPES.EncoderDecoder) { + info = await Promise.all([ + constructSessions(pretrained_model_name_or_path, { + model: 'encoder_model', + decoder_model_merged: 'decoder_model_merged', + }, options), + ]); + + } else if (modelType === MODEL_TYPES.ImageTextToText) { + const sessions = { + embed_tokens: 'embed_tokens', + vision_encoder: 'vision_encoder', + decoder_model_merged: 'decoder_model_merged', + } + if (config.is_encoder_decoder) { + sessions['model'] = 'encoder_model'; + } + info = await Promise.all([ + constructSessions(pretrained_model_name_or_path, sessions, options), + getOptionalConfigs(pretrained_model_name_or_path, { + generation_config: 'generation_config.json', + }, options), + ]); + + } else if (modelType === MODEL_TYPES.Musicgen) { + info = await Promise.all([ + constructSessions(pretrained_model_name_or_path, { + model: 'text_encoder', + decoder_model_merged: 'decoder_model_merged', + encodec_decode: 'encodec_decode', + }, options), + getOptionalConfigs(pretrained_model_name_or_path, { + generation_config: 'generation_config.json', + }, options), + ]); + + } else { // should be MODEL_TYPES.EncoderOnly + if (modelType !== MODEL_TYPES.EncoderOnly) { + console.warn(`Model type for '${modelName ?? config?.model_type}' not found, assuming encoder-only architecture. Please report this at ${_utils_constants_js__WEBPACK_IMPORTED_MODULE_6__.GITHUB_ISSUE_URL}.`) + } + info = await Promise.all([ + constructSessions(pretrained_model_name_or_path, { + model: options.model_file_name ?? 'model', + }, options), + ]); + } + + // @ts-ignore + return new this(config, ...info); + } + + /** + * Runs the model with the provided inputs + * @param {Object} model_inputs Object containing input tensors + * @returns {Promise} Object containing output tensors + */ + async _call(model_inputs) { + return await this.forward(model_inputs); + } + + /** + * Forward method for a pretrained model. If not overridden by a subclass, the correct forward method + * will be chosen based on the model type. + * @param {Object} model_inputs The input data to the model in the format specified in the ONNX model. + * @returns {Promise} The output data from the model in the format specified in the ONNX model. + * @throws {Error} This method must be implemented in subclasses. + */ + async forward(model_inputs) { + return await this._forward(this, model_inputs); + } + + /** + * Get the model's generation config, if it exists. + * @returns {GenerationConfig|null} The model's generation config if it exists, otherwise `null`. + */ + get generation_config() { + return this.configs?.generation_config ?? null; + } + + /** + * This function returns a [`LogitsProcessorList`] list object that contains all relevant [`LogitsWarper`] + * instances used for multinomial sampling. + * @param {GenerationConfig} generation_config The generation config. + * @returns {LogitsProcessorList} generation_config + */ + _get_logits_warper(generation_config) { + + // instantiate warpers list + const warpers = new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.LogitsProcessorList(); + + if (generation_config.temperature !== null && generation_config.temperature !== 1.0) { + warpers.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.TemperatureLogitsWarper(generation_config.temperature)); + } + if (generation_config.top_k !== null && generation_config.top_k !== 0) { + // TODO: add min_tokens_to_keep + warpers.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.TopKLogitsWarper(generation_config.top_k)); + } + if (generation_config.top_p !== null && generation_config.top_p < 1.0) { + // TODO: add min_tokens_to_keep + warpers.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.TopPLogitsWarper(generation_config.top_p)); + } + + return warpers; + } + + /** + * @param {GenerationConfig} generation_config + * @param {number} input_ids_seq_length The starting sequence length for the input ids. + * @returns {LogitsProcessorList} + * @private + */ + _get_logits_processor( + generation_config, + input_ids_seq_length, + // encoder_input_ids, TODO + // prefix_allowed_tokens_fn, TODO + logits_processor = null + ) { + const processors = new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.LogitsProcessorList(); + + // if (generation_config.diversity_penalty !== null && generation_config.diversity_penalty > 0.0) { + // processors.push(new HammingDiversityLogitsProcessor( + // generation_config.diversity_penalty, + // generation_config.num_beams, + // generation_config.num_beam_groups + // )); + // } + + // if (generation_config.encoder_repetition_penalty !== null && generation_config.encoder_repetition_penalty !== 1.0) { + // processors.push(new EncoderRepetitionPenaltyLogitsProcessor( + // generation_config.encoder_repetition_penalty, + // encoder_input_ids + // )); + // } + + if (generation_config.repetition_penalty !== null && generation_config.repetition_penalty !== 1.0) { + processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.RepetitionPenaltyLogitsProcessor(generation_config.repetition_penalty)); + } + + if (generation_config.no_repeat_ngram_size !== null && generation_config.no_repeat_ngram_size > 0) { + processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.NoRepeatNGramLogitsProcessor(generation_config.no_repeat_ngram_size)); + } + + // if (generation_config.encoder_no_repeat_ngram_size !== null && generation_config.encoder_no_repeat_ngram_size > 0) { + // if (this.config.is_encoder_decoder) { + // processors.push(new EncoderNoRepeatNGramLogitsProcessor( + // generation_config.encoder_no_repeat_ngram_size, + // encoder_input_ids + // )); + // } else { + // throw new Error("It's impossible to use `encoder_no_repeat_ngram_size` with decoder-only architecture"); + // } + // } + + if (generation_config.bad_words_ids !== null) { + processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.NoBadWordsLogitsProcessor(generation_config.bad_words_ids, generation_config.eos_token_id)); + } + + if (generation_config.min_length !== null && generation_config.eos_token_id !== null && generation_config.min_length > 0) { + processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.MinLengthLogitsProcessor(generation_config.min_length, generation_config.eos_token_id)); + } + + if (generation_config.min_new_tokens !== null && generation_config.eos_token_id !== null && generation_config.min_new_tokens > 0) { + processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.MinNewTokensLengthLogitsProcessor( + input_ids_seq_length, + generation_config.min_new_tokens, + generation_config.eos_token_id + )); + } + + // if (prefix_allowed_tokens_fn !== null) { + // processors.push(new PrefixConstrainedLogitsProcessor( + // prefix_allowed_tokens_fn, + // generation_config.num_beams / generation_config.num_beam_groups + // )); + // } + + + if (generation_config.forced_bos_token_id !== null) { + processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.ForcedBOSTokenLogitsProcessor(generation_config.forced_bos_token_id)); + } + + if (generation_config.forced_eos_token_id !== null) { + processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.ForcedEOSTokenLogitsProcessor( + generation_config.max_length, + generation_config.forced_eos_token_id + )); + } + + // if (generation_config.remove_invalid_values === true) { + // processors.push(new InfNanRemoveLogitsProcessor()); + // } + + // if (generation_config.exponential_decay_length_penalty !== null) { + // processors.push(new ExponentialDecayLengthPenalty( + // generation_config.exponential_decay_length_penalty, + // generation_config.eos_token_id, + // input_ids_seq_length + // )); + // } + + // if (generation_config.suppress_tokens !== null) { + // processors.push(new SuppressTokensLogitsProcessor(generation_config.suppress_tokens)); + // } + + if (generation_config.begin_suppress_tokens !== null) { + const begin_index = (input_ids_seq_length > 1 || generation_config.forced_bos_token_id === null) + ? input_ids_seq_length + : input_ids_seq_length + 1; + + processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.SuppressTokensAtBeginLogitsProcessor(generation_config.begin_suppress_tokens, begin_index)); + } + + // DEPRECATED: https://github.com/huggingface/transformers/pull/29485 + // if (generation_config.forced_decoder_ids !== null) { + // processors.push(new ForceTokensLogitsProcessor(generation_config.forced_decoder_ids)); + // } + + + // 8. prepare batched CFG externally + if (generation_config.guidance_scale !== null && generation_config.guidance_scale > 1) { + processors.push(new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.ClassifierFreeGuidanceLogitsProcessor(generation_config.guidance_scale)); + } + + if (logits_processor !== null) { + processors.extend(logits_processor) + } + + // `LogitNormalization` should always be the last logit processor, when present + // if (generation_config.renormalize_logits === true) { + // processors.push(new LogitNormalization()); + // } + + return processors; + } + + /** + * This function merges multiple generation configs together to form a final generation config to be used by the model for text generation. + * It first creates an empty `GenerationConfig` object, then it applies the model's own `generation_config` property to it. Finally, if a `generation_config` object was passed in the arguments, it overwrites the corresponding properties in the final config with those of the passed config object. + * @param {GenerationConfig|null} generation_config A `GenerationConfig` object containing generation parameters. + * @param {Object} kwargs Additional generation parameters to be used in place of those in the `generation_config` object. + * @returns {GenerationConfig} The final generation config object to be used by the model for text generation. + */ + _prepare_generation_config(generation_config, kwargs, cls = _generation_configuration_utils_js__WEBPACK_IMPORTED_MODULE_8__.GenerationConfig) { + // Create empty generation config (contains defaults) + // We pass `this.config` so that if `eos_token_id` or `bos_token_id` exist in the model's config, we will use them + const config = { ...this.config }; + for (const key of ["decoder", "generator", "text_config"]) { + // Special case: some models have generation attributes set in the decoder. + // Use them if still unset in the generation config. + if (key in config) { + Object.assign(config, config[key]); + } + } + + const gen_config = new cls(config); + + // Apply model's generation config, if it exists + Object.assign(gen_config, this.generation_config ?? {}); + + // Next, use any generation config specified by the user + // when calling `generate` + if (generation_config) { + Object.assign(gen_config, generation_config); + } + + // Finally, if any kwargs were passed, use them to overwrite + if (kwargs) { + Object.assign(gen_config, (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.pick)(kwargs, Object.getOwnPropertyNames(gen_config))); + } + + return gen_config; + } + + /** + * + * @param {GenerationConfig} generation_config + * @param {StoppingCriteriaList} [stopping_criteria=null] + */ + _get_stopping_criteria(generation_config, stopping_criteria = null) { + const criteria = new _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_11__.StoppingCriteriaList(); + + if (generation_config.max_length !== null) { + criteria.push(new _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_11__.MaxLengthCriteria( + generation_config.max_length, + this.config.max_position_embeddings ?? null, + )); + } + // if (generation_config.max_time !== null) { + // criteria.push(new MaxTimeCriteria(generation_config.max_time)); + // } + if (generation_config.eos_token_id !== null) { + criteria.push(new _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_11__.EosTokenCriteria(generation_config.eos_token_id)); + } + + if (stopping_criteria) { + criteria.extend(stopping_criteria); + } + return criteria; + + } + + /** + * Confirms that the model class is compatible with generation. + * If not, raises an exception that points to the right class to use. + */ + _validate_model_class() { + if (!this.can_generate) { + const generate_compatible_mappings = [ + MODEL_FOR_CAUSAL_LM_MAPPING_NAMES, + // MODEL_FOR_CAUSAL_IMAGE_MODELING_MAPPING, // TODO + MODEL_FOR_VISION_2_SEQ_MAPPING_NAMES, + MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES, + MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES, + ]; + + const modelName = MODEL_CLASS_TO_NAME_MAPPING.get(this.constructor); + + const generate_compatible_classes = new Set(); + const modelType = this.config.model_type; + for (const model_mapping of generate_compatible_mappings) { + const supported_models = model_mapping.get(modelType); + if (supported_models) { + generate_compatible_classes.add(supported_models[0]); + } + } + + let errorMessage = `The current model class (${modelName}) is not compatible with \`.generate()\`, as it doesn't have a language model head.` + if (generate_compatible_classes.size > 0) { + errorMessage += ` Please use the following class instead: ${[...generate_compatible_classes].join(', ')}`; + } + throw Error(errorMessage); + } + } + + prepare_inputs_for_generation(...args) { + return this._prepare_inputs_for_generation(this, ...args); + } + + /** + * + * @param {Object} inputs + * @param {bigint[][]} inputs.generated_input_ids + * @param {Object} inputs.outputs + * @param {Object} inputs.model_inputs + * @param {boolean} inputs.is_encoder_decoder + * @returns {Object} The updated model inputs for the next generation iteration. + */ + _update_model_kwargs_for_generation({ generated_input_ids, outputs, model_inputs, is_encoder_decoder }) { + // update past_key_values + model_inputs['past_key_values'] = this.getPastKeyValues(outputs, model_inputs.past_key_values); + + // update inputs for next run + model_inputs['input_ids'] = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor('int64', generated_input_ids.flat(), [generated_input_ids.length, 1]); + + if (!is_encoder_decoder) { + // update attention mask + model_inputs.attention_mask = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)( + [ + model_inputs.attention_mask, + (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.ones)([model_inputs.attention_mask.dims[0], 1]), + ], 1 + ); + } else if ('decoder_attention_mask' in model_inputs) { + // TODO: update decoder attention mask if the model requires it + } + + // force recreate position_ids in next iteration + model_inputs['position_ids'] = null; + + return model_inputs; + } + + /** + * This function extracts the model-specific `inputs` for generation. + * @param {Object} params + * @param {Tensor} [params.inputs=null] + * @param {number} [params.bos_token_id=null] + * @param {Record} [params.model_kwargs] + * @returns {{inputs_tensor: Tensor, model_inputs: Record, model_input_name: string}} The model-specific inputs for generation. + */ + _prepare_model_inputs({ inputs, bos_token_id, model_kwargs }) { + const model_inputs = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.pick)(model_kwargs, this.forward_params); + const input_name = this.main_input_name; + if (input_name in model_inputs) { + if (inputs) { + throw new Error( + "`inputs`: {inputs}` were passed alongside {input_name} which is not allowed. " + + "Make sure to either pass {inputs} or {input_name}=..." + ); + } + } else { + model_inputs[input_name] = inputs; + } + + const inputs_tensor = model_inputs[input_name]; + + return { inputs_tensor, model_inputs, model_input_name: input_name }; + } + + async _prepare_encoder_decoder_kwargs_for_generation({ inputs_tensor, model_inputs, model_input_name, generation_config }) { + if ( + this.sessions['model'].inputNames.includes('inputs_embeds') + && !model_inputs.inputs_embeds + && '_prepare_inputs_embeds' in this + ) { + // Encoder expects `inputs_embeds` instead of `input_ids` + const { input_ids, pixel_values, attention_mask, ...kwargs } = model_inputs; + // @ts-ignore + const prepared_inputs = await this._prepare_inputs_embeds(model_inputs); + model_inputs = { + ...kwargs, + ...(0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.pick)(prepared_inputs, ['inputs_embeds', 'attention_mask']), + }; + } + let { last_hidden_state } = await encoderForward(this, model_inputs); + + // for classifier free guidance we need to add a 'null' input to our encoder hidden states + if (generation_config.guidance_scale !== null && generation_config.guidance_scale > 1) { + + last_hidden_state = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)([ + last_hidden_state, + (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.full_like)(last_hidden_state, 0.0), + ], 0); + + if ('attention_mask' in model_inputs) { + model_inputs['attention_mask'] = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)([ + model_inputs['attention_mask'], + (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.zeros_like)(model_inputs['attention_mask']), + ], 0); + } + + } else if (model_inputs.decoder_input_ids) { + // Ensure that the encoder outputs have the same batch size as the decoder inputs, + // allowing for more efficient batched generation for single inputs + const decoder_input_ids_batch_size = toI64Tensor(model_inputs.decoder_input_ids).dims[0]; + if (decoder_input_ids_batch_size !== last_hidden_state.dims[0]) { + if (last_hidden_state.dims[0] !== 1) { + throw new Error( + `The encoder outputs have a different batch size (${last_hidden_state.dims[0]}) than the decoder inputs (${decoder_input_ids_batch_size}).` + ) + } + last_hidden_state = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)(Array.from({ length: decoder_input_ids_batch_size }, () => last_hidden_state), 0); + } + } + model_inputs['encoder_outputs'] = last_hidden_state; + + return model_inputs; + } + + /** + * Prepares `decoder_input_ids` for generation with encoder-decoder models + * @param {*} param0 + */ + _prepare_decoder_input_ids_for_generation({ batch_size, model_input_name, model_kwargs, decoder_start_token_id, bos_token_id, generation_config }) { + let { decoder_input_ids, ...model_inputs } = model_kwargs; + + // Prepare input ids if the user has not defined `decoder_input_ids` manually. + if (!(decoder_input_ids instanceof _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor)) { + if (!decoder_input_ids) { + decoder_start_token_id ??= bos_token_id; + + if (this.config.model_type === 'musicgen') { + // Custom logic (TODO: move to Musicgen class) + decoder_input_ids = Array.from({ + length: batch_size * this.config.decoder.num_codebooks + }, () => [decoder_start_token_id]); + + } else if (Array.isArray(decoder_start_token_id)) { + if (decoder_start_token_id.length !== batch_size) { + throw new Error( + `\`decoder_start_token_id\` expcted to have length ${batch_size} but got ${decoder_start_token_id.length}` + ) + } + decoder_input_ids = decoder_start_token_id; + } else { + decoder_input_ids = Array.from({ + length: batch_size, + }, () => [decoder_start_token_id]); + } + } else if (!Array.isArray(decoder_input_ids[0])) { + // Correct batch size + decoder_input_ids = Array.from({ + length: batch_size, + }, () => decoder_input_ids); + } + decoder_input_ids = toI64Tensor(decoder_input_ids); + } + + model_kwargs['decoder_attention_mask'] = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.ones_like)(decoder_input_ids); + + return { input_ids: decoder_input_ids, model_inputs }; + } + + /** + * Generates sequences of token ids for models with a language modeling head. + * @param {import('./generation/parameters.js').GenerationFunctionParameters} options + * @returns {Promise} The output of the model, which can contain the generated token ids, attentions, and scores. + */ + async generate({ + inputs = null, + generation_config = null, + logits_processor = null, + stopping_criteria = null, + streamer = null, + + // inputs_attention_mask = null, + ...kwargs + }) { + this._validate_model_class(); + + // Update generation config with defaults and kwargs + generation_config = this._prepare_generation_config(generation_config, kwargs); + + // 3. Define model inputs + let { inputs_tensor, model_inputs, model_input_name } = this._prepare_model_inputs({ + inputs, + model_kwargs: kwargs, + }); + + const is_encoder_decoder = this.config.is_encoder_decoder; + + // 4. Define other model kwargs + if (!is_encoder_decoder) { + // decoder-only models should use left-padding for generation + } else if (!('encoder_outputs' in model_inputs)) { + // if model is encoder decoder encoder_outputs are created + // and added to `model_kwargs` + model_inputs = await this._prepare_encoder_decoder_kwargs_for_generation( + { inputs_tensor, model_inputs, model_input_name, generation_config } + ) + } + + // 5. Prepare `input_ids` which will be used for auto-regressive generation + // TODO: Update to align with HF transformers' implementation + let input_ids; + if (is_encoder_decoder) { + // Generating from the encoder outputs + ({ input_ids, model_inputs } = this._prepare_decoder_input_ids_for_generation({ + batch_size: model_inputs[model_input_name].dims.at(0), + model_input_name, + model_kwargs: model_inputs, + decoder_start_token_id: generation_config.decoder_start_token_id, + bos_token_id: generation_config.bos_token_id, + generation_config, + })); + } else { + input_ids = model_inputs[model_input_name] + } + + // 6. Prepare `max_length` depending on other stopping criteria. + let input_ids_length = input_ids.dims.at(-1); + + if (generation_config.max_new_tokens !== null) { + generation_config.max_length = input_ids_length + generation_config.max_new_tokens; + } + + // input_ids_length = model_inputs[model_input_name].dims.at(1); + // // inputs instanceof Tensor ? : inputs.length; + + // // decoder-only + // if (input_ids_length === 0) { + // throw Error("Must supply a non-empty array of input token ids.") + // } + + // let decoder_input_ids = + // generation_config.decoder_input_ids + // ?? generation_config.decoder_start_token_id + // ?? generation_config.bos_token_id + // ?? generation_config.eos_token_id; + + // Update logits processor + // 8. prepare distribution pre_processing samplers + const prepared_logits_processor = this._get_logits_processor( + generation_config, + input_ids_length, + logits_processor, + ) + + // 9. prepare stopping criteria + const prepared_stopping_criteria = this._get_stopping_criteria( + generation_config, stopping_criteria + ) + + // /** @type {number[]} */ + // let eos_token_ids = generation_config.eos_token_id; + // if (eos_token_ids !== null && !Array.isArray(eos_token_ids)) { + // eos_token_ids = [eos_token_ids]; + // } + + const numInputs = model_inputs[model_input_name].dims.at(0); + + // TODO: + // done is a list of booleans to keep track of which inputs are done + // const done = new Array(numInputs).fill(false); + // For efficiency purposes, we remove completed rows from model_inputs + // when the beam is complete, and we keep track of the row index + // const rowIndexToBatchIndex = new Map(); + + const sampler = _generation_logits_sampler_js__WEBPACK_IMPORTED_MODULE_12__.LogitsSampler.getSampler(generation_config); + + // TODO make > numInputs + const scores = new Array(numInputs).fill(0); + /** @type {bigint[][]} */ + const all_input_ids = input_ids.tolist(); + if (streamer) { + streamer.put(all_input_ids); + } + // const all_generated_input_ids = Array.from({ length: numInputs }, () => []); + + // NOTE: For now, we don't support spawning new beams + // TODO: when we do, we simply copy past key values and accumulate into single large tensor + + //////////////////////////////////////////////////// + // Generic search which handles 4 generation modes: + // - GenerationMode.GREEDY_SEARCH + // - GenerationMode.SAMPLE + // - GenerationMode.BEAM_SEARCH + // - GenerationMode.BEAM_SAMPLE + //////////////////////////////////////////////////// + let outputs; + let attentions = {}; + while (true) { + // prepare model inputs + model_inputs = this.prepare_inputs_for_generation(all_input_ids, model_inputs, generation_config); + outputs = await this.forward(model_inputs); + + if (generation_config.output_attentions && generation_config.return_dict_in_generate) { + // Get attentions if they are present + const token_attentions = this.getAttentions(outputs); + for (const key in token_attentions) { + if (!(key in attentions)) { + attentions[key] = []; + } + attentions[key].push(token_attentions[key]); + } + } + + // Logits are of the form [batch_size, out_seq_length, vocab_size] + // In most cases, this will be [batch_size, 1, vocab_size] + // So, we select the last token's logits: + // (equivalent to `logits = outputs.logits[:, -1, :]`) + const logits = outputs.logits.slice(null, -1, null); + + const next_tokens_scores = prepared_logits_processor(all_input_ids, logits); + + /** @type {[bigint][]} */ + const generated_input_ids = []; + // const new_kv_cache = [];// NOTE: Only used for beam search when concatenating new kv + // Loop over each batch + for (let batch_idx = 0; batch_idx < next_tokens_scores.dims.at(0); ++batch_idx) { + const logs = next_tokens_scores[batch_idx]; + + const sampledTokens = await sampler(logs); + for (const [newTokenId, logProb] of sampledTokens) { + const bigint = BigInt(newTokenId); + // TODO: If branching, use previous beam as a starting point + // update generated ids, model inputs, and length for next step + scores[batch_idx] += logProb; + all_input_ids[batch_idx].push(bigint); + generated_input_ids.push([bigint]); + + // TODO: Support beam search + break; + } + } + if (streamer) { + streamer.put(generated_input_ids); + } + + const stop = prepared_stopping_criteria(all_input_ids); + if (stop.every(x => x)) { + break; + } + + model_inputs = this._update_model_kwargs_for_generation({ + generated_input_ids, outputs, model_inputs, is_encoder_decoder, + }); + } + + if (streamer) { + streamer.end(); + } + + // Retrieve and dispose all final past key values (including encoder attentions) + const past_key_values = this.getPastKeyValues(outputs, model_inputs.past_key_values, true); + + // TODO: ensure all_input_ids is padded correctly... + const sequences = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor('int64', all_input_ids.flat(), [all_input_ids.length, all_input_ids[0].length]); + + if (generation_config.return_dict_in_generate) { + return { + sequences, + past_key_values, + ...attentions, + // TODO: + // scores, + // logits, + } + } else { + // Dispose all remaining tensors + for (const tensor of Object.values(outputs)) { + if (tensor.location === 'gpu-buffer') { + tensor.dispose(); + } + } + return sequences; + } + } + + /** + * Returns an object containing past key values from the given decoder results object. + * + * @param {Object} decoderResults The decoder results object. + * @param {Object} pastKeyValues The previous past key values. + * @returns {Object} An object containing past key values. + */ + getPastKeyValues(decoderResults, pastKeyValues, disposeEncoderPKVs = false) { + const pkvs = Object.create(null); + + for (const name in decoderResults) { + if (name.startsWith('present')) { + const newName = name.replace('present', 'past_key_values'); + const is_encoder_pkv = name.includes('encoder'); + if (is_encoder_pkv && pastKeyValues) { + // Optimization introduced by optimum to reuse past key values. + // So, we just replace the constant outputs (`decoderResults[name]`) with the previous past key values. + // https://github.com/huggingface/optimum/blob/0bf2c05fb7e1182b52d21b703cfc95fd9e4ea3dc/optimum/onnxruntime/base.py#L677-L704 + pkvs[newName] = pastKeyValues[newName]; + } else { // decoder or using first encoder PKVs + pkvs[newName] = decoderResults[name]; + } + + if (pastKeyValues && (!is_encoder_pkv || disposeEncoderPKVs)) { + // - Always dispose decoder PKVs + // - Only dispose encoder past key values when requested (after generation) + const t = pastKeyValues[newName]; + if (t.location === 'gpu-buffer') { + t.dispose(); + } + } + } + } + return pkvs; + } + + /** + * Returns an object containing attentions from the given model output object. + * + * @param {Object} model_output The output of the model. + * @returns {{cross_attentions?: Tensor[]}} An object containing attentions. + */ + getAttentions(model_output) { + const attentions = {}; + + for (const attnName of ['cross_attentions', 'encoder_attentions', 'decoder_attentions']) { + for (const name in model_output) { + if (name.startsWith(attnName)) { + if (!(attnName in attentions)) { + attentions[attnName] = []; + } + attentions[attnName].push(model_output[name]); + } + } + } + return attentions; + } + + /** + * Adds past key values to the decoder feeds object. If pastKeyValues is null, creates new tensors for past key values. + * + * @param {Object} decoderFeeds The decoder feeds object to add past key values to. + * @param {Object} pastKeyValues An object containing past key values. + */ + addPastKeyValues(decoderFeeds, pastKeyValues) { + if (pastKeyValues) { + Object.assign(decoderFeeds, pastKeyValues) + } else { + const session = this.sessions['decoder_model_merged'] ?? this.sessions['model']; + const dtype = session?.config?.kv_cache_dtype ?? 'float32'; + const empty = (dtype === 'float16') ? new Uint16Array() : []; + + const shapes = (0,_configs_js__WEBPACK_IMPORTED_MODULE_0__.getKeyValueShapes)(this.config); + + for (const name in shapes) { + decoderFeeds[name] = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor(dtype, empty, shapes[name]); + } + } + } + + async encode_image({ pixel_values }) { + // image_inputs === { pixel_values } + const features = (await sessionRun(this.sessions['vision_encoder'], { pixel_values })).image_features; + if (!this.config.num_image_tokens) { + console.warn( + 'The number of image tokens was not set in the model configuration. ' + + `Setting it to the number of features detected by the vision encoder (${features.dims[1]}).` + ) + this.config.num_image_tokens = features.dims[1]; + } + return features; + } + + async encode_text({ input_ids }) { + // text_inputs === { input_ids, attention_mask } + return (await sessionRun(this.sessions['embed_tokens'], { input_ids })).inputs_embeds; + } +} + +////////////////////////////////////////////////// +// Base model output class +class ModelOutput { } + +/** + * Base class for model's outputs, with potential hidden states and attentions. + */ +class BaseModelOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.last_hidden_state Sequence of hidden-states at the output of the last layer of the model. + * @param {Tensor} [output.hidden_states] Hidden-states of the model at the output of each layer plus the optional initial embedding outputs. + * @param {Tensor} [output.attentions] Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads. + */ + constructor({ last_hidden_state, hidden_states = null, attentions = null }) { + super(); + this.last_hidden_state = last_hidden_state; + this.hidden_states = hidden_states; + this.attentions = attentions; + } +} +////////////////////////////////////////////////// +// Bert models +class BertPreTrainedModel extends PreTrainedModel { } +class BertModel extends BertPreTrainedModel { } + +/** + * BertForMaskedLM is a class representing a BERT model for masked language modeling. + */ +class BertForMaskedLM extends BertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for masked language modeling. + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} + +/** + * BertForSequenceClassification is a class representing a BERT model for sequence classification. + */ +class BertForSequenceClassification extends BertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * BertForTokenClassification is a class representing a BERT model for token classification. + */ +class BertForTokenClassification extends BertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * BertForQuestionAnswering is a class representing a BERT model for question answering. + */ +class BertForQuestionAnswering extends BertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for question answering. + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// NomicBert models +class NomicBertPreTrainedModel extends PreTrainedModel { } +class NomicBertModel extends NomicBertPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// RoFormer models +class RoFormerPreTrainedModel extends PreTrainedModel { } + +/** + * The bare RoFormer Model transformer outputting raw hidden-states without any specific head on top. + */ +class RoFormerModel extends RoFormerPreTrainedModel { } + +/** + * RoFormer Model with a `language modeling` head on top. + */ +class RoFormerForMaskedLM extends RoFormerPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for masked language modeling. + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} + +/** + * RoFormer Model transformer with a sequence classification/regression head on top (a linear layer on top of the pooled output) + */ +class RoFormerForSequenceClassification extends RoFormerPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * RoFormer Model with a token classification head on top (a linear layer on top of the hidden-states output) + * e.g. for Named-Entity-Recognition (NER) tasks. + */ +class RoFormerForTokenClassification extends RoFormerPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * RoFormer Model with a span classification head on top for extractive question-answering tasks like SQuAD + * (a linear layers on top of the hidden-states output to compute `span start logits` and `span end logits`). + */ +class RoFormerForQuestionAnswering extends RoFormerPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for question answering. + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +// TODO: Add RoFormerForCausalLM and RoFormerForMultipleChoice +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// ConvBert models +class ConvBertPreTrainedModel extends PreTrainedModel { } + +/** + * The bare ConvBERT Model transformer outputting raw hidden-states without any specific head on top. + */ +class ConvBertModel extends ConvBertPreTrainedModel { } + +/** + * ConvBERT Model with a language modeling head on top. + */ +class ConvBertForMaskedLM extends ConvBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for masked language modeling. + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} + +/** + * ConvBERT Model transformer with a sequence classification/regression head on top (a linear layer on top of the pooled output) + */ +class ConvBertForSequenceClassification extends ConvBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * ConvBERT Model with a token classification head on top (a linear layer on top of the hidden-states output) + * e.g. for Named-Entity-Recognition (NER) tasks. + */ +class ConvBertForTokenClassification extends ConvBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * ConvBERT Model with a span classification head on top for extractive question-answering tasks like SQuAD + * (a linear layers on top of the hidden-states output to compute `span start logits` and `span end logits`) + */ +class ConvBertForQuestionAnswering extends ConvBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for question answering. + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Electra models +class ElectraPreTrainedModel extends PreTrainedModel { } + +/** + * The bare Electra Model transformer outputting raw hidden-states without any specific head on top. + * Identical to the BERT model except that it uses an additional linear layer between the embedding + * layer and the encoder if the hidden size and embedding size are different. + */ +class ElectraModel extends ElectraPreTrainedModel { } +// TODO add ElectraForPreTraining +/** + * Electra model with a language modeling head on top. + */ +class ElectraForMaskedLM extends ElectraPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for masked language modeling. + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} + +/** + * ELECTRA Model transformer with a sequence classification/regression head on top (a linear layer on top of the pooled output) + */ +class ElectraForSequenceClassification extends ElectraPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * Electra model with a token classification head on top. + */ +class ElectraForTokenClassification extends ElectraPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * LECTRA Model with a span classification head on top for extractive question-answering tasks like SQuAD + * (a linear layers on top of the hidden-states output to compute `span start logits` and `span end logits`). + */ +class ElectraForQuestionAnswering extends ElectraPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for question answering. + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// CamemBERT models +class CamembertPreTrainedModel extends PreTrainedModel { } + +/** + * The bare CamemBERT Model transformer outputting raw hidden-states without any specific head on top. + */ +class CamembertModel extends CamembertPreTrainedModel { } + +/** + * CamemBERT Model with a `language modeling` head on top. + */ +class CamembertForMaskedLM extends CamembertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for masked language modeling. + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} + +/** + * CamemBERT Model transformer with a sequence classification/regression head on top (a linear layer on top of the pooled output) e.g. for GLUE tasks. + */ +class CamembertForSequenceClassification extends CamembertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * CamemBERT Model with a token classification head on top (a linear layer on top of the hidden-states output) e.g. for Named-Entity-Recognition (NER) tasks. + */ +class CamembertForTokenClassification extends CamembertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * CamemBERT Model with a span classification head on top for extractive question-answering tasks + */ +class CamembertForQuestionAnswering extends CamembertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for question answering. + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// DeBERTa models +class DebertaPreTrainedModel extends PreTrainedModel { } + +/** + * The bare DeBERTa Model transformer outputting raw hidden-states without any specific head on top. + */ +class DebertaModel extends DebertaPreTrainedModel { } + +/** + * DeBERTa Model with a `language modeling` head on top. + */ +class DebertaForMaskedLM extends DebertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for masked language modeling. + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} + +/** + * DeBERTa Model transformer with a sequence classification/regression head on top (a linear layer on top of the pooled output) + */ +class DebertaForSequenceClassification extends DebertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * DeBERTa Model with a token classification head on top (a linear layer on top of the hidden-states output) e.g. for Named-Entity-Recognition (NER) tasks. + */ +class DebertaForTokenClassification extends DebertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * DeBERTa Model with a span classification head on top for extractive question-answering tasks like SQuAD (a linear + * layers on top of the hidden-states output to compute `span start logits` and `span end logits`). + */ +class DebertaForQuestionAnswering extends DebertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for question answering. + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// DeBERTa-v2 models +class DebertaV2PreTrainedModel extends PreTrainedModel { } + +/** + * The bare DeBERTa-V2 Model transformer outputting raw hidden-states without any specific head on top. + */ +class DebertaV2Model extends DebertaV2PreTrainedModel { } + +/** + * DeBERTa-V2 Model with a `language modeling` head on top. + */ +class DebertaV2ForMaskedLM extends DebertaV2PreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for masked language modeling. + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} + +/** + * DeBERTa-V2 Model transformer with a sequence classification/regression head on top (a linear layer on top of the pooled output) + */ +class DebertaV2ForSequenceClassification extends DebertaV2PreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * DeBERTa-V2 Model with a token classification head on top (a linear layer on top of the hidden-states output) e.g. for Named-Entity-Recognition (NER) tasks. + */ +class DebertaV2ForTokenClassification extends DebertaV2PreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * DeBERTa-V2 Model with a span classification head on top for extractive question-answering tasks like SQuAD (a linear + * layers on top of the hidden-states output to compute `span start logits` and `span end logits`). + */ +class DebertaV2ForQuestionAnswering extends DebertaV2PreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for question answering. + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// DistilBert models +class DistilBertPreTrainedModel extends PreTrainedModel { } +class DistilBertModel extends DistilBertPreTrainedModel { } + +/** + * DistilBertForSequenceClassification is a class representing a DistilBERT model for sequence classification. + */ +class DistilBertForSequenceClassification extends DistilBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * DistilBertForTokenClassification is a class representing a DistilBERT model for token classification. + */ +class DistilBertForTokenClassification extends DistilBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} + + +/** + * DistilBertForQuestionAnswering is a class representing a DistilBERT model for question answering. + */ +class DistilBertForQuestionAnswering extends DistilBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for question answering. + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} + +/** + * DistilBertForMaskedLM is a class representing a DistilBERT model for masking task. + */ +class DistilBertForMaskedLM extends DistilBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// ESM models +class EsmPreTrainedModel extends PreTrainedModel { } + +/** + * The bare ESM Model transformer outputting raw hidden-states without any specific head on top. + */ +class EsmModel extends EsmPreTrainedModel { } + +/** + * ESM Model with a `language modeling` head on top. + */ +class EsmForMaskedLM extends EsmPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for masked language modeling. + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} + +/** + * ESM Model transformer with a sequence classification/regression head on top (a linear layer on top of the pooled output) + */ +class EsmForSequenceClassification extends EsmPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * ESM Model with a token classification head on top (a linear layer on top of the hidden-states output) + * e.g. for Named-Entity-Recognition (NER) tasks. + */ +class EsmForTokenClassification extends EsmPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// MobileBert models +class MobileBertPreTrainedModel extends PreTrainedModel { } +class MobileBertModel extends MobileBertPreTrainedModel { } + +/** + * MobileBertForMaskedLM is a class representing a MobileBERT model for masking task. + */ +class MobileBertForMaskedLM extends MobileBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} + +/** + * MobileBert Model transformer with a sequence classification/regression head on top (a linear layer on top of the pooled output) + */ +class MobileBertForSequenceClassification extends MobileBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * MobileBert Model with a span classification head on top for extractive question-answering tasks + */ +class MobileBertForQuestionAnswering extends MobileBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// MPNet models +class MPNetPreTrainedModel extends PreTrainedModel { } + +/** + * The bare MPNet Model transformer outputting raw hidden-states without any specific head on top. + */ +class MPNetModel extends MPNetPreTrainedModel { } + +/** + * MPNetForMaskedLM is a class representing a MPNet model for masked language modeling. + */ +class MPNetForMaskedLM extends MPNetPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for masked language modeling. + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} + +/** + * MPNetForSequenceClassification is a class representing a MPNet model for sequence classification. + */ +class MPNetForSequenceClassification extends MPNetPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * MPNetForTokenClassification is a class representing a MPNet model for token classification. + */ +class MPNetForTokenClassification extends MPNetPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * MPNetForQuestionAnswering is a class representing a MPNet model for question answering. + */ +class MPNetForQuestionAnswering extends MPNetPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for question answering. + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// SqueezeBert models +class SqueezeBertPreTrainedModel extends PreTrainedModel { } +class SqueezeBertModel extends SqueezeBertPreTrainedModel { } +class SqueezeBertForMaskedLM extends SqueezeBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} +class SqueezeBertForSequenceClassification extends SqueezeBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +class SqueezeBertForQuestionAnswering extends SqueezeBertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Albert models +class AlbertPreTrainedModel extends PreTrainedModel { } +class AlbertModel extends AlbertPreTrainedModel { } +class AlbertForSequenceClassification extends AlbertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +class AlbertForQuestionAnswering extends AlbertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +class AlbertForMaskedLM extends AlbertPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// T5 models +class T5PreTrainedModel extends PreTrainedModel { + forward_params = [ + 'input_ids', + 'attention_mask', + 'encoder_outputs', + 'decoder_input_ids', + 'decoder_attention_mask', + 'past_key_values', + ]; +}; + +class T5Model extends T5PreTrainedModel { } + +/** + * T5Model is a class representing a T5 model for conditional generation. + */ +class T5ForConditionalGeneration extends T5PreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// LONGT5 models +/** + * An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained models. + */ +class LongT5PreTrainedModel extends PreTrainedModel { }; + +/** + * The bare LONGT5 Model transformer outputting raw hidden-states without any specific head on top. + */ +class LongT5Model extends LongT5PreTrainedModel { } + +/** + * LONGT5 Model with a `language modeling` head on top. + */ +class LongT5ForConditionalGeneration extends LongT5PreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// MT5 models +class MT5PreTrainedModel extends PreTrainedModel { }; + +class MT5Model extends MT5PreTrainedModel { } + +/** + * A class representing a conditional sequence-to-sequence model based on the MT5 architecture. + */ +class MT5ForConditionalGeneration extends MT5PreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Bart models +class BartPretrainedModel extends PreTrainedModel { }; + +/** + * The bare BART Model outputting raw hidden-states without any specific head on top. + */ +class BartModel extends BartPretrainedModel { } + +/** + * The BART Model with a language modeling head. Can be used for summarization. + */ +class BartForConditionalGeneration extends BartPretrainedModel { } + +/** + * Bart model with a sequence classification/head on top (a linear layer on top of the pooled output) + */ +class BartForSequenceClassification extends BartPretrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// MBart models +class MBartPreTrainedModel extends PreTrainedModel { }; + +/** + * The bare MBART Model outputting raw hidden-states without any specific head on top. + */ +class MBartModel extends MBartPreTrainedModel { } + +/** + * The MBART Model with a language modeling head. Can be used for summarization, after fine-tuning the pretrained models. + */ +class MBartForConditionalGeneration extends MBartPreTrainedModel { } + +/** + * MBart model with a sequence classification/head on top (a linear layer on top of the pooled output). + */ +class MBartForSequenceClassification extends MBartPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + + +class MBartForCausalLM extends MBartPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Blenderbot models +class BlenderbotPreTrainedModel extends PreTrainedModel { }; + +/** + * The bare Blenderbot Model outputting raw hidden-states without any specific head on top. + */ +class BlenderbotModel extends BlenderbotPreTrainedModel { } + +/** + * The Blenderbot Model with a language modeling head. Can be used for summarization. + */ +class BlenderbotForConditionalGeneration extends BlenderbotPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Blenderbot models +class BlenderbotSmallPreTrainedModel extends PreTrainedModel { }; + +/** + * The bare BlenderbotSmall Model outputting raw hidden-states without any specific head on top. + */ +class BlenderbotSmallModel extends BlenderbotSmallPreTrainedModel { } + +/** + * The BlenderbotSmall Model with a language modeling head. Can be used for summarization. + */ +class BlenderbotSmallForConditionalGeneration extends BlenderbotSmallPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Roberta models +class RobertaPreTrainedModel extends PreTrainedModel { } +class RobertaModel extends RobertaPreTrainedModel { } + +/** + * RobertaForMaskedLM class for performing masked language modeling on Roberta models. + */ +class RobertaForMaskedLM extends RobertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} + +/** + * RobertaForSequenceClassification class for performing sequence classification on Roberta models. + */ +class RobertaForSequenceClassification extends RobertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * RobertaForTokenClassification class for performing token classification on Roberta models. + */ +class RobertaForTokenClassification extends RobertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * RobertaForQuestionAnswering class for performing question answering on Roberta models. + */ +class RobertaForQuestionAnswering extends RobertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// XLM models +/** + * An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained models. + */ +class XLMPreTrainedModel extends PreTrainedModel { } + +/** + * The bare XLM Model transformer outputting raw hidden-states without any specific head on top. + */ +class XLMModel extends XLMPreTrainedModel { } + +/** + * The XLM Model transformer with a language modeling head on top (linear layer with weights tied to the input embeddings). + */ +class XLMWithLMHeadModel extends XLMPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} + +/** + * XLM Model with a sequence classification/regression head on top (a linear layer on top of the pooled output) + */ +class XLMForSequenceClassification extends XLMPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * XLM Model with a token classification head on top (a linear layer on top of the hidden-states output) + */ +class XLMForTokenClassification extends XLMPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * XLM Model with a span classification head on top for extractive question-answering tasks + */ +class XLMForQuestionAnswering extends XLMPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// XLMRoberta models +class XLMRobertaPreTrainedModel extends PreTrainedModel { } +class XLMRobertaModel extends XLMRobertaPreTrainedModel { } + +/** + * XLMRobertaForMaskedLM class for performing masked language modeling on XLMRoberta models. + */ +class XLMRobertaForMaskedLM extends XLMRobertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new MaskedLMOutput(await super._call(model_inputs)); + } +} + +/** + * XLMRobertaForSequenceClassification class for performing sequence classification on XLMRoberta models. + */ +class XLMRobertaForSequenceClassification extends XLMRobertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * XLMRobertaForTokenClassification class for performing token classification on XLMRoberta models. + */ +class XLMRobertaForTokenClassification extends XLMRobertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for token classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * XLMRobertaForQuestionAnswering class for performing question answering on XLMRoberta models. + */ +class XLMRobertaForQuestionAnswering extends XLMRobertaPreTrainedModel { + /** + * Calls the model on new inputs. + * + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} returned object + */ + async _call(model_inputs) { + return new QuestionAnsweringModelOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Audio Spectrogram Transformer (AST) models +class ASTPreTrainedModel extends PreTrainedModel { }; + +/** + * The bare AST Model transformer outputting raw hidden-states without any specific head on top. + */ +class ASTModel extends ASTPreTrainedModel { } + +/** + * Audio Spectrogram Transformer model with an audio classification head on top + * (a linear layer on top of the pooled output) e.g. for datasets like AudioSet, Speech Commands v2. + */ +class ASTForAudioClassification extends ASTPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Whisper models +class WhisperPreTrainedModel extends PreTrainedModel { + + requires_attention_mask = false; + main_input_name = 'input_features'; + forward_params = [ + 'input_features', + 'attention_mask', + 'decoder_input_ids', + 'decoder_attention_mask', + 'past_key_values', + ]; +}; + +/** + * WhisperModel class for training Whisper models without a language model head. + */ +class WhisperModel extends WhisperPreTrainedModel { } + + +/** + * WhisperForConditionalGeneration class for generating conditional outputs from Whisper models. + */ +class WhisperForConditionalGeneration extends WhisperPreTrainedModel { + + _prepare_generation_config(generation_config, kwargs) { + return /** @type {WhisperGenerationConfig} */ (super._prepare_generation_config(generation_config, kwargs, _models_whisper_generation_whisper_js__WEBPACK_IMPORTED_MODULE_14__.WhisperGenerationConfig)); + } + + /** + * + * @param {WhisperGenerationConfig} generation_config + */ + _retrieve_init_tokens(generation_config) { + // prefix tokens are of the form: + // - Multilingual: <|startoftranscript|> <|lang_id|> <|task|> [<|notimestamps|>] + // - English-only: <|startoftranscript|> [<|notimestamps|>] + + // 1. Handle <|startoftranscript|> token + const init_tokens = [generation_config.decoder_start_token_id]; + + // 2. Handle <|lang_id|> and <|task> tokens + let language = generation_config.language; + const task = generation_config.task; + if (generation_config.is_multilingual) { + if (!language) { + // TODO: Implement language detection + console.warn('No language specified - defaulting to English (en).'); + language = 'en'; + } + + // Add language token + const language_code = (0,_models_whisper_common_whisper_js__WEBPACK_IMPORTED_MODULE_15__.whisper_language_to_code)(language); + const language_token = `<|${language_code}|>`; + init_tokens.push(generation_config.lang_to_id[language_token]) + + // Add task token + // NOTE: Defaults to 'transcribe' if no task is specified + init_tokens.push(generation_config.task_to_id[task ?? 'transcribe']); + + } else if (language || task) { + throw new Error( + "Cannot specify `task` or `language` for an English-only model. If the model is intended to be multilingual, pass `is_multilingual=true` to generate, or update the generation config." + ) + } + + // 3. Handle <|notimestamps|> token + if ( + !generation_config.return_timestamps + && generation_config.no_timestamps_token_id + && init_tokens.at(-1) !== generation_config.no_timestamps_token_id + ) { + init_tokens.push(generation_config.no_timestamps_token_id); + } else if ( + generation_config.return_timestamps + && + init_tokens.at(-1) === generation_config.no_timestamps_token_id + ) { + console.warn("<|notimestamps|> prompt token is removed from generation_config since `return_timestamps` is set to `true`."); + init_tokens.pop(); + } + + // let's make sure we don't pass `null` tokens as prompt tokens + return init_tokens.filter(token => token != null); + } + + /** + * Transcribes or translates log-mel input features to a sequence of auto-regressively generated token ids. + * @param {import('./models/whisper/generation_whisper.js').WhisperGenerationFunctionParameters} options + * @returns {Promise} The output of the model, which can contain the generated token ids, attentions, and scores. + */ + async generate({ + inputs = null, + generation_config = null, + logits_processor = null, + stopping_criteria = null, + + // Whisper-specific options (passed to kwargs) + // prompt_ids = null, + // language = null, + // task = null, + + ...kwargs + }) { + generation_config = this._prepare_generation_config(generation_config, kwargs); + + const init_tokens = kwargs.decoder_input_ids ?? this._retrieve_init_tokens(generation_config); + + if (generation_config.return_timestamps) { + logits_processor ??= new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.LogitsProcessorList(); + logits_processor.push( + new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.WhisperTimeStampLogitsProcessor(generation_config, init_tokens) + ); + } + + if (generation_config.begin_suppress_tokens) { + logits_processor ??= new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.LogitsProcessorList(); + logits_processor.push( + new _generation_logits_process_js__WEBPACK_IMPORTED_MODULE_7__.SuppressTokensAtBeginLogitsProcessor(generation_config.begin_suppress_tokens, init_tokens.length) + ); + } + + if (generation_config.return_token_timestamps) { + if (!generation_config.alignment_heads) { + throw new Error( + "Model generation config has no `alignment_heads`, token-level timestamps not available. " + + "See https://gist.github.com/hollance/42e32852f24243b748ae6bc1f985b13a on how to add this property to the generation config." + ) + } + + if (generation_config.task === 'translate') { + console.warn("Token-level timestamps may not be reliable for task 'translate'.") + } + + generation_config.output_attentions = true; + generation_config.return_dict_in_generate = true; + } + + const outputs = await super.generate({ + inputs, + generation_config, + logits_processor, + decoder_input_ids: init_tokens, + ...kwargs + }); + + if (generation_config.return_token_timestamps) { + outputs["token_timestamps"] = this._extract_token_timestamps( + outputs, + generation_config.alignment_heads, + generation_config.num_frames, + ); + } + + return outputs; + } + + /** + * Calculates token-level timestamps using the encoder-decoder cross-attentions and + * dynamic time-warping (DTW) to map each output token to a position in the input audio. + * If `num_frames` is specified, the encoder-decoder cross-attentions will be cropped before applying DTW. + * @param {Object} generate_outputs Outputs generated by the model + * @param {Tensor[][]} generate_outputs.cross_attentions The cross attentions output by the model + * @param {Tensor} generate_outputs.sequences The sequences output by the model + * @param {number[][]} alignment_heads Alignment heads of the model + * @param {number} [num_frames=null] Number of frames in the input audio. + * @param {number} [time_precision=0.02] Precision of the timestamps in seconds + * @returns {Tensor} tensor containing the timestamps in seconds for each predicted token + */ + _extract_token_timestamps(generate_outputs, alignment_heads, num_frames = null, time_precision = 0.02) { + if (!generate_outputs.cross_attentions) { + throw new Error( + "Model outputs must contain cross attentions to extract timestamps. " + + "This is most likely because the model was not exported with `output_attentions=True`." + ) + } + if (num_frames == null) { + console.warn( + "`num_frames` has not been set, meaning the entire audio will be analyzed. " + + "This may lead to inaccurate token-level timestamps for short audios (< 30 seconds)." + ); + } + + let median_filter_width = this.config.median_filter_width; + if (median_filter_width === undefined) { + console.warn("Model config has no `median_filter_width`, using default value of 7.") + median_filter_width = 7; + } + + // TODO: Improve batch processing + const batch = generate_outputs.cross_attentions; + // Create a list with `decoder_layers` elements, each a tensor of shape + // (batch size, attention_heads, output length, input length). + const cross_attentions = Array.from({ length: this.config.decoder_layers }, + // Concatenate the cross attentions for each layer across sequence length dimension. + (_, i) => (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)(batch.map(x => x[i]), 2) + ); + + const weights = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.stack)(alignment_heads.map(([l, h]) => { + if (l >= cross_attentions.length) { + throw new Error(`Layer index ${l} is out of bounds for cross attentions (length ${cross_attentions.length}).`) + } + return num_frames + ? cross_attentions[l].slice(null, h, null, [0, num_frames]) + : cross_attentions[l].slice(null, h); + })).transpose(1, 0, 2, 3); + + const [std, calculatedMean] = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.std_mean)(weights, -2, 0, true); + + // Normalize and smoothen the weights. + const smoothedWeights = weights.clone(); // [1, 8, seqLength, 1500] + + for (let a = 0; a < smoothedWeights.dims[0]; ++a) { + const aTensor = smoothedWeights[a]; // [8, seqLength, 1500] + + for (let b = 0; b < aTensor.dims[0]; ++b) { + const bTensor = aTensor[b]; // [seqLength, 1500] + + const stdTensorData = std[a][b][0].data; // [1500] + const meanTensorData = calculatedMean[a][b][0].data; // [1500] + + for (let c = 0; c < bTensor.dims[0]; ++c) { + + let cTensorData = bTensor[c].data; // [1500] + for (let d = 0; d < cTensorData.length; ++d) { + cTensorData[d] = (cTensorData[d] - meanTensorData[d]) / stdTensorData[d] + } + + // Apply median filter. + cTensorData.set((0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_10__.medianFilter)(cTensorData, median_filter_width)) + } + } + } + + // Average the different cross-attention heads. + const batchedMatrices = [(0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.mean)(smoothedWeights, 1)]; + + const timestampsShape = generate_outputs.sequences.dims; + + const timestamps = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor( + 'float32', + new Float32Array(timestampsShape[0] * timestampsShape[1]), + timestampsShape + ); + + // Perform dynamic time warping on each element of the batch. + for (let batch_idx = 0; batch_idx < timestampsShape[0]; ++batch_idx) { + // NOTE: Since we run only one batch at a time, we can squeeze to get the same dimensions + // as the python implementation + const matrix = batchedMatrices[batch_idx].neg().squeeze_(0); + const [text_indices, time_indices] = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_10__.dynamic_time_warping)(matrix.tolist()); + + const diffs = Array.from({ length: text_indices.length - 1 }, (v, i) => text_indices[i + 1] - text_indices[i]); + const jumps = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.mergeArrays)([1], diffs).map(x => !!x); // convert to boolean + + const jump_times = []; + for (let i = 0; i < jumps.length; ++i) { + if (jumps[i]) { + // NOTE: No point in rounding here, since we set to Float32Array later + jump_times.push(time_indices[i] * time_precision); + } + } + timestamps[batch_idx].data.set(jump_times, 1) + } + + return timestamps; + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +/** + * Vision Encoder-Decoder model based on OpenAI's GPT architecture for image captioning and other vision tasks + */ +class VisionEncoderDecoderModel extends PreTrainedModel { + main_input_name = 'pixel_values'; + forward_params = [ + // Encoder inputs + 'pixel_values', + + // Decoder inpputs + 'decoder_input_ids', + 'encoder_hidden_states', + 'past_key_values', + ]; +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// LLaVa Models +class LlavaPreTrainedModel extends PreTrainedModel { + forward_params = [ + 'input_ids', + 'pixel_values', + 'attention_mask', + 'position_ids', + 'past_key_values', + ]; +} + +/** + * The LLAVA model which consists of a vision backbone and a language model. + */ +class LlavaForConditionalGeneration extends LlavaPreTrainedModel { + + _merge_input_ids_with_image_features({ + inputs_embeds, + image_features, + input_ids, + attention_mask, + }) { + + const image_token_index = this.config.image_token_index; + + const idsList = input_ids.tolist(); + + // NOTE: we use .findIndex instead of .indexOf to perform weak comparison (==) between BigInt and Number + const indexOfImage = idsList.map(x => x.findIndex(x => x == image_token_index)); + + const noImages = indexOfImage.every(x => x === -1); + const allImages = indexOfImage.every(x => x !== -1); + if (!noImages && !allImages) { + // Check for padding reasons + throw new Error('Every input should contain either 0 or 1 image token.'); + } + + if (noImages) { + return { + inputs_embeds, + attention_mask, + } + } + + const stacked = []; + const stacked_attention_mask = []; + for (let i = 0; i < indexOfImage.length; ++i) { + const index = indexOfImage[i]; + + const e = inputs_embeds[i]; + const im = image_features[i]; + const am = attention_mask[i]; + stacked.push( + (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)([ + e.slice([0, index]), + im, + e.slice([index + 1, e.dims[0]]), + ], 0) + ); + + stacked_attention_mask.push( + (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)([ + am.slice([0, index]), + (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.ones)([im.dims[0]]), + am.slice([index + 1, am.dims[0]]) + ], 0) + ) + } + + return { + inputs_embeds: (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.stack)(stacked, 0), + attention_mask: (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.stack)(stacked_attention_mask, 0), + } + } +} +////////////////////////////////////////////////// + +class Moondream1ForConditionalGeneration extends LlavaForConditionalGeneration { } // NOTE: extends LlavaForConditionalGeneration + +class Florence2PreTrainedModel extends PreTrainedModel { + forward_params = [ + // Encoder inputs + 'input_ids', + 'inputs_embeds', + 'attention_mask', + 'pixel_values', + + // Decoder inputs + 'encoder_outputs', + 'decoder_input_ids', + 'decoder_inputs_embeds', + 'decoder_attention_mask', + 'past_key_values', + ]; + main_input_name = 'inputs_embeds'; +} + +class Florence2ForConditionalGeneration extends Florence2PreTrainedModel { + + _merge_input_ids_with_image_features({ + inputs_embeds, + image_features, + input_ids, + attention_mask, + }) { + return { + inputs_embeds: (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)([ + image_features, // image embeds + inputs_embeds, // task prefix embeds + ], 1), + attention_mask: (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)([ + (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.ones)(image_features.dims.slice(0, 2)), // image attention mask + attention_mask, // task prefix attention mask + ], 1), + } + } + + async _prepare_inputs_embeds({ input_ids, pixel_values, inputs_embeds, attention_mask }) { + if (!input_ids && !pixel_values) { + throw new Error('Either `input_ids` or `pixel_values` should be provided.'); + } + + // 1. Possibly, extract the input embeddings + let text_features, image_features; + if (input_ids) { + text_features = await this.encode_text({ input_ids }); + } + if (pixel_values) { + image_features = await this.encode_image({ pixel_values }); + } + + // 2. Possibly, merge text and images + if (text_features && image_features) { + ({ inputs_embeds, attention_mask } = this._merge_input_ids_with_image_features({ + inputs_embeds: text_features, + image_features, + input_ids, + attention_mask, + })); + } else { + inputs_embeds = text_features || image_features; + } + + return { inputs_embeds, attention_mask }; + } + + async forward({ + input_ids, + pixel_values, + attention_mask, + decoder_input_ids, + decoder_attention_mask, + encoder_outputs, + past_key_values, + + inputs_embeds, + decoder_inputs_embeds, + }) { + if (!inputs_embeds) { + ({ inputs_embeds, attention_mask } = await this._prepare_inputs_embeds({ input_ids, pixel_values, inputs_embeds, attention_mask })); + } + + if (!encoder_outputs) { + // Must compute encoder outputs + let { last_hidden_state } = await encoderForward(this, { inputs_embeds, attention_mask }); + encoder_outputs = last_hidden_state; + } + + if (!decoder_inputs_embeds) { + if (!decoder_input_ids) { + throw new Error('Either `decoder_input_ids` or `decoder_inputs_embeds` should be provided.'); + } + decoder_inputs_embeds = await this.encode_text({ input_ids: decoder_input_ids }); + } + + const decoderFeeds = { + inputs_embeds: decoder_inputs_embeds, + attention_mask: decoder_attention_mask, + encoder_attention_mask: attention_mask, + encoder_hidden_states: encoder_outputs, + past_key_values, + }; + const decoder_outputs = await decoderForward(this, decoderFeeds, true); + return decoder_outputs; + } +} +class CLIPPreTrainedModel extends PreTrainedModel { } + +/** + * CLIP Text and Vision Model with a projection layers on top + * + * **Example:** Perform zero-shot image classification with a `CLIPModel`. + * + * ```javascript + * import { AutoTokenizer, AutoProcessor, CLIPModel, RawImage } from '@huggingface/transformers'; + * + * // Load tokenizer, processor, and model + * let tokenizer = await AutoTokenizer.from_pretrained('Xenova/clip-vit-base-patch16'); + * let processor = await AutoProcessor.from_pretrained('Xenova/clip-vit-base-patch16'); + * let model = await CLIPModel.from_pretrained('Xenova/clip-vit-base-patch16'); + * + * // Run tokenization + * let texts = ['a photo of a car', 'a photo of a football match'] + * let text_inputs = tokenizer(texts, { padding: true, truncation: true }); + * + * // Read image and run processor + * let image = await RawImage.read('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/football-match.jpg'); + * let image_inputs = await processor(image); + * + * // Run model with both text and pixel inputs + * let output = await model({ ...text_inputs, ...image_inputs }); + * // { + * // logits_per_image: Tensor { + * // dims: [ 1, 2 ], + * // data: Float32Array(2) [ 18.579734802246094, 24.31830596923828 ], + * // }, + * // logits_per_text: Tensor { + * // dims: [ 2, 1 ], + * // data: Float32Array(2) [ 18.579734802246094, 24.31830596923828 ], + * // }, + * // text_embeds: Tensor { + * // dims: [ 2, 512 ], + * // data: Float32Array(1024) [ ... ], + * // }, + * // image_embeds: Tensor { + * // dims: [ 1, 512 ], + * // data: Float32Array(512) [ ... ], + * // } + * // } + * ``` + */ +class CLIPModel extends CLIPPreTrainedModel { } + +/** + * The text model from CLIP without any head or projection on top. + */ +class CLIPTextModel extends CLIPPreTrainedModel { + /** @type {PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options = {}) { + // Update default model file name if not provided + options.model_file_name ??= 'text_model'; + return super.from_pretrained(pretrained_model_name_or_path, options); + } +} + +/** + * CLIP Text Model with a projection layer on top (a linear layer on top of the pooled output) + * + * **Example:** Compute text embeddings with `CLIPTextModelWithProjection`. + * + * ```javascript + * import { AutoTokenizer, CLIPTextModelWithProjection } from '@huggingface/transformers'; + * + * // Load tokenizer and text model + * const tokenizer = await AutoTokenizer.from_pretrained('Xenova/clip-vit-base-patch16'); + * const text_model = await CLIPTextModelWithProjection.from_pretrained('Xenova/clip-vit-base-patch16'); + * + * // Run tokenization + * let texts = ['a photo of a car', 'a photo of a football match']; + * let text_inputs = tokenizer(texts, { padding: true, truncation: true }); + * + * // Compute embeddings + * const { text_embeds } = await text_model(text_inputs); + * // Tensor { + * // dims: [ 2, 512 ], + * // type: 'float32', + * // data: Float32Array(1024) [ ... ], + * // size: 1024 + * // } + * ``` + */ +class CLIPTextModelWithProjection extends CLIPPreTrainedModel { + /** @type {PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options = {}) { + // Update default model file name if not provided + options.model_file_name ??= 'text_model'; + return super.from_pretrained(pretrained_model_name_or_path, options); + } +} + +/** + * The vision model from CLIP without any head or projection on top. + */ +class CLIPVisionModel extends CLIPPreTrainedModel { + /** @type {PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options = {}) { + // Update default model file name if not provided + options.model_file_name ??= 'vision_model'; + return super.from_pretrained(pretrained_model_name_or_path, options); + } +} + +/** + * CLIP Vision Model with a projection layer on top (a linear layer on top of the pooled output) + * + * **Example:** Compute vision embeddings with `CLIPVisionModelWithProjection`. + * + * ```javascript + * import { AutoProcessor, CLIPVisionModelWithProjection, RawImage} from '@huggingface/transformers'; + * + * // Load processor and vision model + * const processor = await AutoProcessor.from_pretrained('Xenova/clip-vit-base-patch16'); + * const vision_model = await CLIPVisionModelWithProjection.from_pretrained('Xenova/clip-vit-base-patch16'); + * + * // Read image and run processor + * let image = await RawImage.read('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/football-match.jpg'); + * let image_inputs = await processor(image); + * + * // Compute embeddings + * const { image_embeds } = await vision_model(image_inputs); + * // Tensor { + * // dims: [ 1, 512 ], + * // type: 'float32', + * // data: Float32Array(512) [ ... ], + * // size: 512 + * // } + * ``` + */ +class CLIPVisionModelWithProjection extends CLIPPreTrainedModel { + /** @type {PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options = {}) { + // Update default model file name if not provided + options.model_file_name ??= 'vision_model'; + return super.from_pretrained(pretrained_model_name_or_path, options); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// SigLIP models +class SiglipPreTrainedModel extends PreTrainedModel { } + +/** + * SigLIP Text and Vision Model with a projection layers on top + * + * **Example:** Perform zero-shot image classification with a `SiglipModel`. + * + * ```javascript + * import { AutoTokenizer, AutoProcessor, SiglipModel, RawImage } from '@huggingface/transformers'; + * + * // Load tokenizer, processor, and model + * const tokenizer = await AutoTokenizer.from_pretrained('Xenova/siglip-base-patch16-224'); + * const processor = await AutoProcessor.from_pretrained('Xenova/siglip-base-patch16-224'); + * const model = await SiglipModel.from_pretrained('Xenova/siglip-base-patch16-224'); + * + * // Run tokenization + * const texts = ['a photo of 2 cats', 'a photo of 2 dogs']; + * const text_inputs = tokenizer(texts, { padding: 'max_length', truncation: true }); + * + * // Read image and run processor + * const image = await RawImage.read('http://images.cocodataset.org/val2017/000000039769.jpg'); + * const image_inputs = await processor(image); + * + * // Run model with both text and pixel inputs + * const output = await model({ ...text_inputs, ...image_inputs }); + * // { + * // logits_per_image: Tensor { + * // dims: [ 1, 2 ], + * // data: Float32Array(2) [ -1.6019744873046875, -10.720091819763184 ], + * // }, + * // logits_per_text: Tensor { + * // dims: [ 2, 1 ], + * // data: Float32Array(2) [ -1.6019744873046875, -10.720091819763184 ], + * // }, + * // text_embeds: Tensor { + * // dims: [ 2, 768 ], + * // data: Float32Array(1536) [ ... ], + * // }, + * // image_embeds: Tensor { + * // dims: [ 1, 768 ], + * // data: Float32Array(768) [ ... ], + * // } + * // } + * ``` + */ +class SiglipModel extends SiglipPreTrainedModel { } + +/** + * The text model from SigLIP without any head or projection on top. + * + * **Example:** Compute text embeddings with `SiglipTextModel`. + * + * ```javascript + * import { AutoTokenizer, SiglipTextModel } from '@huggingface/transformers'; + * + * // Load tokenizer and text model + * const tokenizer = await AutoTokenizer.from_pretrained('Xenova/siglip-base-patch16-224'); + * const text_model = await SiglipTextModel.from_pretrained('Xenova/siglip-base-patch16-224'); + * + * // Run tokenization + * const texts = ['a photo of 2 cats', 'a photo of 2 dogs']; + * const text_inputs = tokenizer(texts, { padding: 'max_length', truncation: true }); + * + * // Compute embeddings + * const { pooler_output } = await text_model(text_inputs); + * // Tensor { + * // dims: [ 2, 768 ], + * // type: 'float32', + * // data: Float32Array(1536) [ ... ], + * // size: 1536 + * // } + * ``` + */ +class SiglipTextModel extends SiglipPreTrainedModel { + + /** @type {PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options = {}) { + // Update default model file name if not provided + options.model_file_name ??= 'text_model'; + return super.from_pretrained(pretrained_model_name_or_path, options); + } +} + +/** + * The vision model from SigLIP without any head or projection on top. + * + * **Example:** Compute vision embeddings with `SiglipVisionModel`. + * + * ```javascript + * import { AutoProcessor, SiglipVisionModel, RawImage} from '@huggingface/transformers'; + * + * // Load processor and vision model + * const processor = await AutoProcessor.from_pretrained('Xenova/siglip-base-patch16-224'); + * const vision_model = await SiglipVisionModel.from_pretrained('Xenova/siglip-base-patch16-224'); + * + * // Read image and run processor + * const image = await RawImage.read('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/football-match.jpg'); + * const image_inputs = await processor(image); + * + * // Compute embeddings + * const { pooler_output } = await vision_model(image_inputs); + * // Tensor { + * // dims: [ 1, 768 ], + * // type: 'float32', + * // data: Float32Array(768) [ ... ], + * // size: 768 + * // } + * ``` + */ +class SiglipVisionModel extends CLIPPreTrainedModel { + /** @type {PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options = {}) { + // Update default model file name if not provided + options.model_file_name ??= 'vision_model'; + return super.from_pretrained(pretrained_model_name_or_path, options); + } +} +////////////////////////////////////////////////// +// ChineseCLIP models +class ChineseCLIPPreTrainedModel extends PreTrainedModel { } + +class ChineseCLIPModel extends ChineseCLIPPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// CLIPSeg models +class CLIPSegPreTrainedModel extends PreTrainedModel { } + +class CLIPSegModel extends CLIPSegPreTrainedModel { } + +/** + * CLIPSeg model with a Transformer-based decoder on top for zero-shot and one-shot image segmentation. + * + * **Example:** Perform zero-shot image segmentation with a `CLIPSegForImageSegmentation` model. + * + * ```javascript + * import { AutoTokenizer, AutoProcessor, CLIPSegForImageSegmentation, RawImage } from '@huggingface/transformers'; + * + * // Load tokenizer, processor, and model + * const tokenizer = await AutoTokenizer.from_pretrained('Xenova/clipseg-rd64-refined'); + * const processor = await AutoProcessor.from_pretrained('Xenova/clipseg-rd64-refined'); + * const model = await CLIPSegForImageSegmentation.from_pretrained('Xenova/clipseg-rd64-refined'); + * + * // Run tokenization + * const texts = ['a glass', 'something to fill', 'wood', 'a jar']; + * const text_inputs = tokenizer(texts, { padding: true, truncation: true }); + * + * // Read image and run processor + * const image = await RawImage.read('https://github.com/timojl/clipseg/blob/master/example_image.jpg?raw=true'); + * const image_inputs = await processor(image); + * + * // Run model with both text and pixel inputs + * const { logits } = await model({ ...text_inputs, ...image_inputs }); + * // logits: Tensor { + * // dims: [4, 352, 352], + * // type: 'float32', + * // data: Float32Array(495616) [ ... ], + * // size: 495616 + * // } + * ``` + * + * You can visualize the predictions as follows: + * ```javascript + * const preds = logits + * .unsqueeze_(1) + * .sigmoid_() + * .mul_(255) + * .round_() + * .to('uint8'); + * + * for (let i = 0; i < preds.dims[0]; ++i) { + * const img = RawImage.fromTensor(preds[i]); + * img.save(`prediction_${i}.png`); + * } + * ``` + */ +class CLIPSegForImageSegmentation extends CLIPSegPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// GPT2 models +class GPT2PreTrainedModel extends PreTrainedModel { } + +class GPT2Model extends GPT2PreTrainedModel { } + +/** + * GPT-2 language model head on top of the GPT-2 base model. This model is suitable for text generation tasks. + */ +class GPT2LMHeadModel extends GPT2PreTrainedModel { } +// export class GPT2ForSequenceClassification extends GPT2PreTrainedModel { +// TODO +// } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// JAIS models +class JAISPreTrainedModel extends PreTrainedModel { } + +/** + * The bare JAIS Model transformer outputting raw hidden-states without any specific head on top. + */ +class JAISModel extends JAISPreTrainedModel { } + +/** + * The JAIS Model transformer with a language modeling head on top (linear layer with weights tied to the input embeddings). + */ +class JAISLMHeadModel extends JAISPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// GPTNeo models +class GPTNeoPreTrainedModel extends PreTrainedModel { } +class GPTNeoModel extends GPTNeoPreTrainedModel { } + +class GPTNeoForCausalLM extends GPTNeoPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// GPTNeoX models +class GPTNeoXPreTrainedModel extends PreTrainedModel { } +class GPTNeoXModel extends GPTNeoXPreTrainedModel { } + +class GPTNeoXForCausalLM extends GPTNeoXPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// GPT-J models +class GPTJPreTrainedModel extends PreTrainedModel { } + +class GPTJModel extends GPTJPreTrainedModel { } + +class GPTJForCausalLM extends GPTJPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// GPTBigCode models +class GPTBigCodePreTrainedModel extends PreTrainedModel { } + +class GPTBigCodeModel extends GPTBigCodePreTrainedModel { } + +class GPTBigCodeForCausalLM extends GPTBigCodePreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// CodeGen models +class CodeGenPreTrainedModel extends PreTrainedModel { } +/** + * CodeGenModel is a class representing a code generation model without a language model head. + */ +class CodeGenModel extends CodeGenPreTrainedModel { } + +/** + * CodeGenForCausalLM is a class that represents a code generation model based on the GPT-2 architecture. It extends the `CodeGenPreTrainedModel` class. + */ +class CodeGenForCausalLM extends CodeGenPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// LLama models + +/** + * The bare LLama Model outputting raw hidden-states without any specific head on top. + */ +class LlamaPreTrainedModel extends PreTrainedModel { } +/** + * The bare LLaMA Model outputting raw hidden-states without any specific head on top. + */ +class LlamaModel extends LlamaPreTrainedModel { } + +class LlamaForCausalLM extends LlamaPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// MobileLLM models +class MobileLLMPreTrainedModel extends PreTrainedModel { } +class MobileLLMModel extends MobileLLMPreTrainedModel { } +class MobileLLMForCausalLM extends MobileLLMPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// OLMo models +class OlmoPreTrainedModel extends PreTrainedModel { } +class OlmoModel extends OlmoPreTrainedModel { } +class OlmoForCausalLM extends OlmoPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Granite models +class GranitePreTrainedModel extends PreTrainedModel { } +class GraniteModel extends GranitePreTrainedModel { } +class GraniteForCausalLM extends GranitePreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Cohere models + +/** + * The bare Cohere Model outputting raw hidden-states without any specific head on top. + */ +class CoherePreTrainedModel extends PreTrainedModel { } +class CohereModel extends CoherePreTrainedModel { } + +class CohereForCausalLM extends CoherePreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Gemma models + +/** + * The bare Gemma Model outputting raw hidden-states without any specific head on top. + */ +class GemmaPreTrainedModel extends PreTrainedModel { } +/** + * The bare Gemma Model outputting raw hidden-states without any specific head on top. + */ +class GemmaModel extends GemmaPreTrainedModel { } + +class GemmaForCausalLM extends GemmaPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Gemma2 models + +/** + * The bare Gemma2 Model outputting raw hidden-states without any specific head on top. + */ +class Gemma2PreTrainedModel extends PreTrainedModel { } +/** + * The bare Gemma2 Model outputting raw hidden-states without any specific head on top. + */ +class Gemma2Model extends Gemma2PreTrainedModel { } + +class Gemma2ForCausalLM extends Gemma2PreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class OpenELMPreTrainedModel extends PreTrainedModel { } +class OpenELMModel extends OpenELMPreTrainedModel { } + +class OpenELMForCausalLM extends OpenELMPreTrainedModel { } + + +////////////////////////////////////////////////// +// Qwen2 models + +/** + * The bare Qwen2 Model outputting raw hidden-states without any specific head on top. + */ +class Qwen2PreTrainedModel extends PreTrainedModel { } +/** + * The bare Qwen2 Model outputting raw hidden-states without any specific head on top. + */ +class Qwen2Model extends Qwen2PreTrainedModel { } + +class Qwen2ForCausalLM extends Qwen2PreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Phi models +class PhiPreTrainedModel extends PreTrainedModel { } +/** + * The bare Phi Model outputting raw hidden-states without any specific head on top. + */ +class PhiModel extends PhiPreTrainedModel { } + +class PhiForCausalLM extends PhiPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Phi3 models +class Phi3PreTrainedModel extends PreTrainedModel { } + +/** + * The bare Phi3 Model outputting raw hidden-states without any specific head on top. + */ +class Phi3Model extends Phi3PreTrainedModel { } + +class Phi3ForCausalLM extends Phi3PreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Bloom models +/** + * The Bloom Model transformer with a language modeling head on top (linear layer with weights tied to the input embeddings). + */ +class BloomPreTrainedModel extends PreTrainedModel { } + +/** + * The bare Bloom Model transformer outputting raw hidden-states without any specific head on top. + */ +class BloomModel extends BloomPreTrainedModel { } + +/** + * The Bloom Model transformer with a language modeling head on top (linear layer with weights tied to the input embeddings). + */ +class BloomForCausalLM extends BloomPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// MPT models +class MptPreTrainedModel extends PreTrainedModel { } + +/** + * The bare Mpt Model transformer outputting raw hidden-states without any specific head on top. + */ +class MptModel extends MptPreTrainedModel { } + +/** + * The MPT Model transformer with a language modeling head on top (linear layer with weights tied to the input embeddings). + */ +class MptForCausalLM extends MptPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// OPT models +class OPTPreTrainedModel extends PreTrainedModel { } + +/** + * The bare OPT Model outputting raw hidden-states without any specific head on top. + */ +class OPTModel extends OPTPreTrainedModel { } + +/** + * The OPT Model transformer with a language modeling head on top (linear layer with weights tied to the input embeddings). + */ +class OPTForCausalLM extends OPTPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class ViTPreTrainedModel extends PreTrainedModel { } +class ViTModel extends ViTPreTrainedModel { } +class ViTForImageClassification extends ViTPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class PvtPreTrainedModel extends PreTrainedModel { } +class PvtModel extends PvtPreTrainedModel { } +class PvtForImageClassification extends PvtPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class ViTMAEPreTrainedModel extends PreTrainedModel { } +class ViTMAEModel extends ViTMAEPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +class ViTMSNPreTrainedModel extends PreTrainedModel { } +class ViTMSNModel extends ViTMSNPreTrainedModel { } +class ViTMSNForImageClassification extends ViTMSNPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class GroupViTPreTrainedModel extends PreTrainedModel { } +class GroupViTModel extends GroupViTPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +class FastViTPreTrainedModel extends PreTrainedModel { } +class FastViTModel extends FastViTPreTrainedModel { } +class FastViTForImageClassification extends FastViTPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class VitMattePreTrainedModel extends PreTrainedModel { } + +/** + * ViTMatte framework leveraging any vision backbone e.g. for ADE20k, CityScapes. + * + * **Example:** Perform image matting with a `VitMatteForImageMatting` model. + * ```javascript + * import { AutoProcessor, VitMatteForImageMatting, RawImage } from '@huggingface/transformers'; + * + * // Load processor and model + * const processor = await AutoProcessor.from_pretrained('Xenova/vitmatte-small-distinctions-646'); + * const model = await VitMatteForImageMatting.from_pretrained('Xenova/vitmatte-small-distinctions-646'); + * + * // Load image and trimap + * const image = await RawImage.fromURL('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/vitmatte_image.png'); + * const trimap = await RawImage.fromURL('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/vitmatte_trimap.png'); + * + * // Prepare image + trimap for the model + * const inputs = await processor(image, trimap); + * + * // Predict alpha matte + * const { alphas } = await model(inputs); + * // Tensor { + * // dims: [ 1, 1, 640, 960 ], + * // type: 'float32', + * // size: 614400, + * // data: Float32Array(614400) [ 0.9894027709960938, 0.9970508813858032, ... ] + * // } + * ``` + * + * You can visualize the alpha matte as follows: + * ```javascript + * import { Tensor, cat } from '@huggingface/transformers'; + * + * // Visualize predicted alpha matte + * const imageTensor = image.toTensor(); + * + * // Convert float (0-1) alpha matte to uint8 (0-255) + * const alphaChannel = alphas + * .squeeze(0) + * .mul_(255) + * .clamp_(0, 255) + * .round_() + * .to('uint8'); + * + * // Concatenate original image with predicted alpha + * const imageData = cat([imageTensor, alphaChannel], 0); + * + * // Save output image + * const outputImage = RawImage.fromTensor(imageData); + * outputImage.save('output.png'); + * ``` + */ +class VitMatteForImageMatting extends VitMattePreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new ImageMattingOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class MobileViTPreTrainedModel extends PreTrainedModel { } +class MobileViTModel extends MobileViTPreTrainedModel { } +class MobileViTForImageClassification extends MobileViTPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +// TODO: MobileViTForSemanticSegmentation + +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class MobileViTV2PreTrainedModel extends PreTrainedModel { } +class MobileViTV2Model extends MobileViTV2PreTrainedModel { } +class MobileViTV2ForImageClassification extends MobileViTV2PreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +// TODO: MobileViTV2ForSemanticSegmentation + +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class OwlViTPreTrainedModel extends PreTrainedModel { } +class OwlViTModel extends OwlViTPreTrainedModel { } +class OwlViTForObjectDetection extends OwlViTPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class Owlv2PreTrainedModel extends PreTrainedModel { } +class Owlv2Model extends Owlv2PreTrainedModel { } +class Owlv2ForObjectDetection extends Owlv2PreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Beit Models +class BeitPreTrainedModel extends PreTrainedModel { } +class BeitModel extends BeitPreTrainedModel { } +class BeitForImageClassification extends BeitPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +class DetrPreTrainedModel extends PreTrainedModel { } +class DetrModel extends DetrPreTrainedModel { } +class DetrForObjectDetection extends DetrPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new DetrObjectDetectionOutput(await super._call(model_inputs)); + } +} + +class DetrForSegmentation extends DetrPreTrainedModel { + /** + * Runs the model with the provided inputs + * @param {Object} model_inputs Model inputs + * @returns {Promise} Object containing segmentation outputs + */ + async _call(model_inputs) { + return new DetrSegmentationOutput(await super._call(model_inputs)); + } +} + +class DetrObjectDetectionOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits Classification logits (including no-object) for all queries. + * @param {Tensor} output.pred_boxes Normalized boxes coordinates for all queries, represented as (center_x, center_y, width, height). + * These values are normalized in [0, 1], relative to the size of each individual image in the batch (disregarding possible padding). + */ + constructor({ logits, pred_boxes }) { + super(); + this.logits = logits; + this.pred_boxes = pred_boxes; + } +} + +class DetrSegmentationOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits The output logits of the model. + * @param {Tensor} output.pred_boxes Predicted boxes. + * @param {Tensor} output.pred_masks Predicted masks. + */ + constructor({ logits, pred_boxes, pred_masks }) { + super(); + this.logits = logits; + this.pred_boxes = pred_boxes; + this.pred_masks = pred_masks; + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class RTDetrPreTrainedModel extends PreTrainedModel { } +class RTDetrModel extends RTDetrPreTrainedModel { } +class RTDetrForObjectDetection extends RTDetrPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new RTDetrObjectDetectionOutput(await super._call(model_inputs)); + } +} + +class RTDetrObjectDetectionOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits Classification logits (including no-object) for all queries. + * @param {Tensor} output.pred_boxes Normalized boxes coordinates for all queries, represented as (center_x, center_y, width, height). + * These values are normalized in [0, 1], relative to the size of each individual image in the batch (disregarding possible padding). + */ + constructor({ logits, pred_boxes }) { + super(); + this.logits = logits; + this.pred_boxes = pred_boxes; + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class TableTransformerPreTrainedModel extends PreTrainedModel { } + +/** + * The bare Table Transformer Model (consisting of a backbone and encoder-decoder Transformer) + * outputting raw hidden-states without any specific head on top. + */ +class TableTransformerModel extends TableTransformerPreTrainedModel { } + +/** + * Table Transformer Model (consisting of a backbone and encoder-decoder Transformer) + * with object detection heads on top, for tasks such as COCO detection. + */ +class TableTransformerForObjectDetection extends TableTransformerPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new TableTransformerObjectDetectionOutput(await super._call(model_inputs)); + } +} +class TableTransformerObjectDetectionOutput extends DetrObjectDetectionOutput { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +class DeiTPreTrainedModel extends PreTrainedModel { } +class DeiTModel extends DeiTPreTrainedModel { } +class DeiTForImageClassification extends DeiTPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class HieraPreTrainedModel extends PreTrainedModel { } +class HieraModel extends HieraPreTrainedModel { } +class HieraForImageClassification extends HieraPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +/** + * An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained models. + */ +class ResNetPreTrainedModel extends PreTrainedModel { } + +/** + * The bare ResNet model outputting raw features without any specific head on top. + */ +class ResNetModel extends ResNetPreTrainedModel { } + +/** + * ResNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for ImageNet. + */ +class ResNetForImageClassification extends ResNetPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +class SwinPreTrainedModel extends PreTrainedModel { } +class SwinModel extends SwinPreTrainedModel { } +class SwinForImageClassification extends SwinPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class Swin2SRPreTrainedModel extends PreTrainedModel { } + +/** + * The bare Swin2SR Model transformer outputting raw hidden-states without any specific head on top. + */ +class Swin2SRModel extends Swin2SRPreTrainedModel { } + +/** + * Swin2SR Model transformer with an upsampler head on top for image super resolution and restoration. + * + * **Example:** Super-resolution w/ `Xenova/swin2SR-classical-sr-x2-64`. + * + * ```javascript + * import { AutoProcessor, Swin2SRForImageSuperResolution, RawImage } from '@huggingface/transformers'; + * + * // Load processor and model + * const model_id = 'Xenova/swin2SR-classical-sr-x2-64'; + * const processor = await AutoProcessor.from_pretrained(model_id); + * const model = await Swin2SRForImageSuperResolution.from_pretrained(model_id); + * + * // Prepare model inputs + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/butterfly.jpg'; + * const image = await RawImage.fromURL(url); + * const inputs = await processor(image); + * + * // Run model + * const outputs = await model(inputs); + * + * // Convert Tensor to RawImage + * const output = outputs.reconstruction.squeeze().clamp_(0, 1).mul_(255).round_().to('uint8'); + * const outputImage = RawImage.fromTensor(output); + * // RawImage { + * // data: Uint8Array(786432) [ 41, 31, 24, ... ], + * // width: 512, + * // height: 512, + * // channels: 3 + * // } + * ``` + */ +class Swin2SRForImageSuperResolution extends Swin2SRPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class DPTPreTrainedModel extends PreTrainedModel { } + +/** + * The bare DPT Model transformer outputting raw hidden-states without any specific head on top. + */ +class DPTModel extends DPTPreTrainedModel { } + +/** + * DPT Model with a depth estimation head on top (consisting of 3 convolutional layers) e.g. for KITTI, NYUv2. + * + * **Example:** Depth estimation w/ `Xenova/dpt-hybrid-midas`. + * ```javascript + * import { DPTForDepthEstimation, AutoProcessor, RawImage, interpolate, max } from '@huggingface/transformers'; + * + * // Load model and processor + * const model_id = 'Xenova/dpt-hybrid-midas'; + * const model = await DPTForDepthEstimation.from_pretrained(model_id); + * const processor = await AutoProcessor.from_pretrained(model_id); + * + * // Load image from URL + * const url = 'http://images.cocodataset.org/val2017/000000039769.jpg'; + * const image = await RawImage.fromURL(url); + * + * // Prepare image for the model + * const inputs = await processor(image); + * + * // Run model + * const { predicted_depth } = await model(inputs); + * + * // Interpolate to original size + * const prediction = interpolate(predicted_depth, image.size.reverse(), 'bilinear', false); + * + * // Visualize the prediction + * const formatted = prediction.mul_(255 / max(prediction.data)[0]).to('uint8'); + * const depth = RawImage.fromTensor(formatted); + * // RawImage { + * // data: Uint8Array(307200) [ 85, 85, 84, ... ], + * // width: 640, + * // height: 480, + * // channels: 1 + * // } + * ``` + */ +class DPTForDepthEstimation extends DPTPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class DepthAnythingPreTrainedModel extends PreTrainedModel { } + +/** + * Depth Anything Model with a depth estimation head on top (consisting of 3 convolutional layers) e.g. for KITTI, NYUv2. + */ +class DepthAnythingForDepthEstimation extends DepthAnythingPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +class SapiensPreTrainedModel extends PreTrainedModel { } +class SapiensForSemanticSegmentation extends SapiensPreTrainedModel { } +class SapiensForDepthEstimation extends SapiensPreTrainedModel { } +class SapiensForNormalEstimation extends SapiensPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class DepthProPreTrainedModel extends PreTrainedModel { } +class DepthProForDepthEstimation extends DepthProPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class MaskFormerPreTrainedModel extends PreTrainedModel { } +class MaskFormerModel extends MaskFormerPreTrainedModel { } +class MaskFormerForInstanceSegmentation extends MaskFormerPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class GLPNPreTrainedModel extends PreTrainedModel { } + +/** + * The bare GLPN encoder (Mix-Transformer) outputting raw hidden-states without any specific head on top. + */ +class GLPNModel extends GLPNPreTrainedModel { } + +/** + * GLPN Model transformer with a lightweight depth estimation head on top e.g. for KITTI, NYUv2. + * + * **Example:** Depth estimation w/ `Xenova/glpn-kitti`. + * ```javascript + * import { GLPNForDepthEstimation, AutoProcessor, RawImage, interpolate, max } from '@huggingface/transformers'; + * + * // Load model and processor + * const model_id = 'Xenova/glpn-kitti'; + * const model = await GLPNForDepthEstimation.from_pretrained(model_id); + * const processor = await AutoProcessor.from_pretrained(model_id); + * + * // Load image from URL + * const url = 'http://images.cocodataset.org/val2017/000000039769.jpg'; + * const image = await RawImage.fromURL(url); + * + * // Prepare image for the model + * const inputs = await processor(image); + * + * // Run model + * const { predicted_depth } = await model(inputs); + * + * // Interpolate to original size + * const prediction = interpolate(predicted_depth, image.size.reverse(), 'bilinear', false); + * + * // Visualize the prediction + * const formatted = prediction.mul_(255 / max(prediction.data)[0]).to('uint8'); + * const depth = RawImage.fromTensor(formatted); + * // RawImage { + * // data: Uint8Array(307200) [ 207, 169, 154, ... ], + * // width: 640, + * // height: 480, + * // channels: 1 + * // } + * ``` + */ +class GLPNForDepthEstimation extends GLPNPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class DonutSwinPreTrainedModel extends PreTrainedModel { } + +/** + * The bare Donut Swin Model transformer outputting raw hidden-states without any specific head on top. + * + * **Example:** Step-by-step Document Parsing. + * + * ```javascript + * import { AutoProcessor, AutoTokenizer, AutoModelForVision2Seq, RawImage } from '@huggingface/transformers'; + * + * // Choose model to use + * const model_id = 'Xenova/donut-base-finetuned-cord-v2'; + * + * // Prepare image inputs + * const processor = await AutoProcessor.from_pretrained(model_id); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/receipt.png'; + * const image = await RawImage.read(url); + * const image_inputs = await processor(image); + * + * // Prepare decoder inputs + * const tokenizer = await AutoTokenizer.from_pretrained(model_id); + * const task_prompt = ''; + * const decoder_input_ids = tokenizer(task_prompt, { + * add_special_tokens: false, + * }).input_ids; + * + * // Create the model + * const model = await AutoModelForVision2Seq.from_pretrained(model_id); + * + * // Run inference + * const output = await model.generate(image_inputs.pixel_values, { + * decoder_input_ids, + * max_length: model.config.decoder.max_position_embeddings, + * }); + * + * // Decode output + * const decoded = tokenizer.batch_decode(output)[0]; + * // CINNAMON SUGAR 17,000 1 x 17,000 17,000 17,000 20,000 3,000 + * ``` + * + * **Example:** Step-by-step Document Visual Question Answering (DocVQA) + * + * ```javascript + * import { AutoProcessor, AutoTokenizer, AutoModelForVision2Seq, RawImage } from '@huggingface/transformers'; + * + * // Choose model to use + * const model_id = 'Xenova/donut-base-finetuned-docvqa'; + * + * // Prepare image inputs + * const processor = await AutoProcessor.from_pretrained(model_id); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/invoice.png'; + * const image = await RawImage.read(url); + * const image_inputs = await processor(image); + * + * // Prepare decoder inputs + * const tokenizer = await AutoTokenizer.from_pretrained(model_id); + * const question = 'What is the invoice number?'; + * const task_prompt = `${question}`; + * const decoder_input_ids = tokenizer(task_prompt, { + * add_special_tokens: false, + * }).input_ids; + * + * // Create the model + * const model = await AutoModelForVision2Seq.from_pretrained(model_id); + * + * // Run inference + * const output = await model.generate(image_inputs.pixel_values, { + * decoder_input_ids, + * max_length: model.config.decoder.max_position_embeddings, + * }); + * + * // Decode output + * const decoded = tokenizer.batch_decode(output)[0]; + * // What is the invoice number? us-001 + * ``` + */ +class DonutSwinModel extends DonutSwinPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +class ConvNextPreTrainedModel extends PreTrainedModel { } + +/** + * The bare ConvNext model outputting raw features without any specific head on top. + */ +class ConvNextModel extends ConvNextPreTrainedModel { } + +/** + * ConvNext Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for ImageNet. + */ +class ConvNextForImageClassification extends ConvNextPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +class ConvNextV2PreTrainedModel extends PreTrainedModel { } + +/** + * The bare ConvNextV2 model outputting raw features without any specific head on top. + */ +class ConvNextV2Model extends ConvNextV2PreTrainedModel { } + +/** + * ConvNextV2 Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for ImageNet. + */ +class ConvNextV2ForImageClassification extends ConvNextV2PreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class Dinov2PreTrainedModel extends PreTrainedModel { } + +/** + * The bare DINOv2 Model transformer outputting raw hidden-states without any specific head on top. + */ +class Dinov2Model extends Dinov2PreTrainedModel { } + +/** + * Dinov2 Model transformer with an image classification head on top (a linear layer on top of the final hidden state of the [CLS] token) e.g. for ImageNet. + */ +class Dinov2ForImageClassification extends Dinov2PreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +class YolosPreTrainedModel extends PreTrainedModel { } +class YolosModel extends YolosPreTrainedModel { } +class YolosForObjectDetection extends YolosPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new YolosObjectDetectionOutput(await super._call(model_inputs)); + } +} + +class YolosObjectDetectionOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits Classification logits (including no-object) for all queries. + * @param {Tensor} output.pred_boxes Normalized boxes coordinates for all queries, represented as (center_x, center_y, width, height). + * These values are normalized in [0, 1], relative to the size of each individual image in the batch (disregarding possible padding). + */ + constructor({ logits, pred_boxes }) { + super(); + this.logits = logits; + this.pred_boxes = pred_boxes; + } +} +////////////////////////////////////////////////// + + + + +////////////////////////////////////////////////// +class SamPreTrainedModel extends PreTrainedModel { } + +/** + * Segment Anything Model (SAM) for generating segmentation masks, given an input image + * and optional 2D location and bounding boxes. + * + * **Example:** Perform mask generation w/ `Xenova/sam-vit-base`. + * ```javascript + * import { SamModel, AutoProcessor, RawImage } from '@huggingface/transformers'; + * + * const model = await SamModel.from_pretrained('Xenova/sam-vit-base'); + * const processor = await AutoProcessor.from_pretrained('Xenova/sam-vit-base'); + * + * const img_url = 'https://huggingface.co/ybelkada/segment-anything/resolve/main/assets/car.png'; + * const raw_image = await RawImage.read(img_url); + * const input_points = [[[450, 600]]] // 2D localization of a window + * + * const inputs = await processor(raw_image, { input_points }); + * const outputs = await model(inputs); + * + * const masks = await processor.post_process_masks(outputs.pred_masks, inputs.original_sizes, inputs.reshaped_input_sizes); + * // [ + * // Tensor { + * // dims: [ 1, 3, 1764, 2646 ], + * // type: 'bool', + * // data: Uint8Array(14002632) [ ... ], + * // size: 14002632 + * // } + * // ] + * const scores = outputs.iou_scores; + * // Tensor { + * // dims: [ 1, 1, 3 ], + * // type: 'float32', + * // data: Float32Array(3) [ + * // 0.8892380595207214, + * // 0.9311248064041138, + * // 0.983696699142456 + * // ], + * // size: 3 + * // } + * ``` + */ +class SamModel extends SamPreTrainedModel { + + /** + * Compute image embeddings and positional image embeddings, given the pixel values of an image. + * @param {Object} model_inputs Object containing the model inputs. + * @param {Tensor} model_inputs.pixel_values Pixel values obtained using a `SamProcessor`. + * @returns {Promise<{ image_embeddings: Tensor, image_positional_embeddings: Tensor }>} The image embeddings and positional image embeddings. + */ + async get_image_embeddings({ pixel_values }) { + // in: + // - pixel_values: tensor.float32[batch_size,3,1024,1024] + // + // out: + // - image_embeddings: tensor.float32[batch_size,256,64,64] + // - image_positional_embeddings: tensor.float32[batch_size,256,64,64] + return await encoderForward(this, { pixel_values }) + } + + /** + * @typedef {Object} SamModelInputs Object containing the model inputs. + * @property {Tensor} pixel_values Pixel values as a Tensor with shape `(batch_size, num_channels, height, width)`. + * These can be obtained using a `SamProcessor`. + * @property {Tensor} [input_points] Input 2D spatial points with shape `(batch_size, num_points, 2)`. + * This is used by the prompt encoder to encode the prompt. + * @property {Tensor} [input_labels] Input labels for the points, as a Tensor of shape `(batch_size, point_batch_size, num_points)`. + * This is used by the prompt encoder to encode the prompt. There are 4 types of labels: + * - `1`: the point is a point that contains the object of interest + * - `0`: the point is a point that does not contain the object of interest + * - `-1`: the point corresponds to the background + * - `-10`: the point is a padding point, thus should be ignored by the prompt encoder + * @property {Tensor} [input_boxes] Input bounding boxes with shape `(batch_size, num_boxes, 4)`. + * @property {Tensor} [image_embeddings] Image embeddings used by the mask decoder. + * @property {Tensor} [image_positional_embeddings] Image positional embeddings used by the mask decoder. + */ + + /** + * @param {SamModelInputs} model_inputs Object containing the model inputs. + * @returns {Promise} The output of the model. + */ + async forward(model_inputs) { + if (!model_inputs.image_embeddings || !model_inputs.image_positional_embeddings) { + // Compute the image embeddings if they are missing + model_inputs = { + ...model_inputs, + ...(await this.get_image_embeddings(model_inputs)) + } + } + + if (!model_inputs.input_labels && model_inputs.input_points) { + // Set default input labels if they are missing + const shape = model_inputs.input_points.dims.slice(0, -1); + const numElements = shape.reduce((a, b) => a * b, 1); + model_inputs.input_labels = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor( + 'int64', + new BigInt64Array(numElements).fill(1n), + shape + ); + } + + const decoder_inputs = { + image_embeddings: model_inputs.image_embeddings, + image_positional_embeddings: model_inputs.image_positional_embeddings, + }; + if (model_inputs.input_points) { + decoder_inputs.input_points = model_inputs.input_points; + } + if (model_inputs.input_labels) { + decoder_inputs.input_labels = model_inputs.input_labels; + } + if (model_inputs.input_boxes) { + decoder_inputs.input_boxes = model_inputs.input_boxes; + } + + // Returns: + // - iou_scores: tensor.float32[batch_size,point_batch_size,3] + // - pred_masks: tensor.float32[batch_size,point_batch_size,3,256,256] + return await sessionRun(this.sessions['prompt_encoder_mask_decoder'], decoder_inputs); + } + + /** + * Runs the model with the provided inputs + * @param {Object} model_inputs Model inputs + * @returns {Promise} Object containing segmentation outputs + */ + async _call(model_inputs) { + return new SamImageSegmentationOutput(await super._call(model_inputs)); + } +} + + +/** + * Base class for Segment-Anything model's output. + */ +class SamImageSegmentationOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.iou_scores The output logits of the model. + * @param {Tensor} output.pred_masks Predicted boxes. + */ + constructor({ iou_scores, pred_masks }) { + super(); + this.iou_scores = iou_scores; + this.pred_masks = pred_masks; + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// MarianMT models +class MarianPreTrainedModel extends PreTrainedModel { }; + +class MarianModel extends MarianPreTrainedModel { } + +class MarianMTModel extends MarianPreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// M2M100 models +class M2M100PreTrainedModel extends PreTrainedModel { }; + +class M2M100Model extends M2M100PreTrainedModel { } + +class M2M100ForConditionalGeneration extends M2M100PreTrainedModel { } +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Wav2Vec2 models +class Wav2Vec2PreTrainedModel extends PreTrainedModel { }; + +/** + * The bare Wav2Vec2 Model transformer outputting raw hidden-states without any specific head on top. + * + * **Example:** Load and run a `Wav2Vec2Model` for feature extraction. + * + * ```javascript + * import { AutoProcessor, AutoModel, read_audio } from '@huggingface/transformers'; + * + * // Read and preprocess audio + * const processor = await AutoProcessor.from_pretrained('Xenova/mms-300m'); + * const audio = await read_audio('https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac', 16000); + * const inputs = await processor(audio); + * + * // Run model with inputs + * const model = await AutoModel.from_pretrained('Xenova/mms-300m'); + * const output = await model(inputs); + * // { + * // last_hidden_state: Tensor { + * // dims: [ 1, 1144, 1024 ], + * // type: 'float32', + * // data: Float32Array(1171456) [ ... ], + * // size: 1171456 + * // } + * // } + * ``` + */ +class Wav2Vec2Model extends Wav2Vec2PreTrainedModel { } + +class Wav2Vec2ForCTC extends Wav2Vec2PreTrainedModel { + /** + * @param {Object} model_inputs + * @param {Tensor} model_inputs.input_values Float values of input raw speech waveform. + * @param {Tensor} model_inputs.attention_mask Mask to avoid performing convolution and attention on padding token indices. Mask values selected in [0, 1] + */ + async _call(model_inputs) { + return new CausalLMOutput(await super._call(model_inputs)); + } +} + +class Wav2Vec2ForSequenceClassification extends Wav2Vec2PreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * Wav2Vec2 Model with a frame classification head on top for tasks like Speaker Diarization. + */ +class Wav2Vec2ForAudioFrameClassification extends Wav2Vec2PreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// PyAnnote models +class PyAnnotePreTrainedModel extends PreTrainedModel { }; + +/** + * The bare PyAnnote Model transformer outputting raw hidden-states without any specific head on top. + */ +class PyAnnoteModel extends PyAnnotePreTrainedModel { } + +/** + * PyAnnote Model with a frame classification head on top for tasks like Speaker Diarization. + * + * **Example:** Load and run a `PyAnnoteForAudioFrameClassification` for speaker diarization. + * + * ```javascript + * import { AutoProcessor, AutoModelForAudioFrameClassification, read_audio } from '@huggingface/transformers'; + * + * // Load model and processor + * const model_id = 'onnx-community/pyannote-segmentation-3.0'; + * const model = await AutoModelForAudioFrameClassification.from_pretrained(model_id); + * const processor = await AutoProcessor.from_pretrained(model_id); + * + * // Read and preprocess audio + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/mlk.wav'; + * const audio = await read_audio(url, processor.feature_extractor.config.sampling_rate); + * const inputs = await processor(audio); + * + * // Run model with inputs + * const { logits } = await model(inputs); + * // { + * // logits: Tensor { + * // dims: [ 1, 767, 7 ], // [batch_size, num_frames, num_classes] + * // type: 'float32', + * // data: Float32Array(5369) [ ... ], + * // size: 5369 + * // } + * // } + * + * const result = processor.post_process_speaker_diarization(logits, audio.length); + * // [ + * // [ + * // { id: 0, start: 0, end: 1.0512535626298245, confidence: 0.8220156481664611 }, + * // { id: 2, start: 1.0512535626298245, end: 2.3398869619825127, confidence: 0.9008811707860472 }, + * // ... + * // ] + * // ] + * + * // Display result + * console.table(result[0], ['start', 'end', 'id', 'confidence']); + * // ┌─────────┬────────────────────┬────────────────────┬────┬─────────────────────┐ + * // │ (index) │ start │ end │ id │ confidence │ + * // ├─────────┼────────────────────┼────────────────────┼────┼─────────────────────┤ + * // │ 0 │ 0 │ 1.0512535626298245 │ 0 │ 0.8220156481664611 │ + * // │ 1 │ 1.0512535626298245 │ 2.3398869619825127 │ 2 │ 0.9008811707860472 │ + * // │ 2 │ 2.3398869619825127 │ 3.5946089560890773 │ 0 │ 0.7521651315796233 │ + * // │ 3 │ 3.5946089560890773 │ 4.578039708226655 │ 2 │ 0.8491978128022479 │ + * // │ 4 │ 4.578039708226655 │ 4.594995410849717 │ 0 │ 0.2935352600416393 │ + * // │ 5 │ 4.594995410849717 │ 6.121008646925269 │ 3 │ 0.6788051309866024 │ + * // │ 6 │ 6.121008646925269 │ 6.256654267909762 │ 0 │ 0.37125512393851134 │ + * // │ 7 │ 6.256654267909762 │ 8.630452635138397 │ 2 │ 0.7467035186353542 │ + * // │ 8 │ 8.630452635138397 │ 10.088643060721703 │ 0 │ 0.7689364814666032 │ + * // │ 9 │ 10.088643060721703 │ 12.58113134631177 │ 2 │ 0.9123324509131324 │ + * // │ 10 │ 12.58113134631177 │ 13.005023911888312 │ 0 │ 0.4828358177572041 │ + * // └─────────┴────────────────────┴────────────────────┴────┴─────────────────────┘ + * ``` + */ +class PyAnnoteForAudioFrameClassification extends PyAnnotePreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// WeSpeakerResNet models +class WeSpeakerResNetPreTrainedModel extends PreTrainedModel { }; +class WeSpeakerResNetModel extends WeSpeakerResNetPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// UniSpeech models +class UniSpeechPreTrainedModel extends PreTrainedModel { }; + +/** + * The bare UniSpeech Model transformer outputting raw hidden-states without any specific head on top. + */ +class UniSpeechModel extends UniSpeechPreTrainedModel { } + +/** + * UniSpeech Model with a `language modeling` head on top for Connectionist Temporal Classification (CTC). + */ +class UniSpeechForCTC extends UniSpeechPreTrainedModel { + /** + * @param {Object} model_inputs + * @param {Tensor} model_inputs.input_values Float values of input raw speech waveform. + * @param {Tensor} model_inputs.attention_mask Mask to avoid performing convolution and attention on padding token indices. Mask values selected in [0, 1] + */ + async _call(model_inputs) { + return new CausalLMOutput(await super._call(model_inputs)); + } +} + +/** + * UniSpeech Model with a sequence classification head on top (a linear layer over the pooled output). + */ +class UniSpeechForSequenceClassification extends UniSpeechPreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// UniSpeechSat models +class UniSpeechSatPreTrainedModel extends PreTrainedModel { }; + +/** + * The bare UniSpeechSat Model transformer outputting raw hidden-states without any specific head on top. + */ +class UniSpeechSatModel extends UniSpeechSatPreTrainedModel { } + +/** + * UniSpeechSat Model with a `language modeling` head on top for Connectionist Temporal Classification (CTC). + */ +class UniSpeechSatForCTC extends UniSpeechSatPreTrainedModel { + /** + * @param {Object} model_inputs + * @param {Tensor} model_inputs.input_values Float values of input raw speech waveform. + * @param {Tensor} model_inputs.attention_mask Mask to avoid performing convolution and attention on padding token indices. Mask values selected in [0, 1] + */ + async _call(model_inputs) { + return new CausalLMOutput(await super._call(model_inputs)); + } +} + +/** + * UniSpeechSat Model with a sequence classification head on top (a linear layer over the pooled output). + */ +class UniSpeechSatForSequenceClassification extends UniSpeechSatPreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * UniSpeechSat Model with a frame classification head on top for tasks like Speaker Diarization. + */ +class UniSpeechSatForAudioFrameClassification extends UniSpeechSatPreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Wav2Vec2Bert models +class Wav2Vec2BertPreTrainedModel extends PreTrainedModel { }; + +/** + * The bare Wav2Vec2Bert Model transformer outputting raw hidden-states without any specific head on top. + */ +class Wav2Vec2BertModel extends Wav2Vec2BertPreTrainedModel { } + +/** + * Wav2Vec2Bert Model with a `language modeling` head on top for Connectionist Temporal Classification (CTC). + */ +class Wav2Vec2BertForCTC extends Wav2Vec2BertPreTrainedModel { + /** + * @param {Object} model_inputs + * @param {Tensor} model_inputs.input_features Float values of input mel-spectrogram. + * @param {Tensor} model_inputs.attention_mask Mask to avoid performing convolution and attention on padding token indices. Mask values selected in [0, 1] + */ + async _call(model_inputs) { + return new CausalLMOutput(await super._call(model_inputs)); + } +} + +/** + * Wav2Vec2Bert Model with a sequence classification head on top (a linear layer over the pooled output). + */ +class Wav2Vec2BertForSequenceClassification extends Wav2Vec2BertPreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Hubert models +class HubertPreTrainedModel extends PreTrainedModel { } + +/** + * The bare Hubert Model transformer outputting raw hidden-states without any specific head on top. + * + * **Example:** Load and run a `HubertModel` for feature extraction. + * + * ```javascript + * import { AutoProcessor, AutoModel, read_audio } from '@huggingface/transformers'; + * + * // Read and preprocess audio + * const processor = await AutoProcessor.from_pretrained('Xenova/hubert-base-ls960'); + * const audio = await read_audio('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav', 16000); + * const inputs = await processor(audio); + * + * // Load and run model with inputs + * const model = await AutoModel.from_pretrained('Xenova/hubert-base-ls960'); + * const output = await model(inputs); + * // { + * // last_hidden_state: Tensor { + * // dims: [ 1, 549, 768 ], + * // type: 'float32', + * // data: Float32Array(421632) [0.0682469978928566, 0.08104046434164047, -0.4975186586380005, ...], + * // size: 421632 + * // } + * // } + * ``` + */ +class HubertModel extends Wav2Vec2PreTrainedModel { } + +/** + * Hubert Model with a `language modeling` head on top for Connectionist Temporal Classification (CTC). + */ +class HubertForCTC extends Wav2Vec2PreTrainedModel { + /** + * @param {Object} model_inputs + * @param {Tensor} model_inputs.input_values Float values of input raw speech waveform. + * @param {Tensor} model_inputs.attention_mask Mask to avoid performing convolution and attention on padding token indices. Mask values selected in [0, 1] + */ + async _call(model_inputs) { + return new CausalLMOutput(await super._call(model_inputs)); + } +} + +/** + * Hubert Model with a sequence classification head on top (a linear layer over the pooled output) for tasks like SUPERB Keyword Spotting. + */ +class HubertForSequenceClassification extends Wav2Vec2PreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// WavLM models +/** + * An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained models. + */ +class WavLMPreTrainedModel extends PreTrainedModel { }; + +/** + * The bare WavLM Model transformer outputting raw hidden-states without any specific head on top. + * + * **Example:** Load and run a `WavLMModel` for feature extraction. + * + * ```javascript + * import { AutoProcessor, AutoModel, read_audio } from '@huggingface/transformers'; + * + * // Read and preprocess audio + * const processor = await AutoProcessor.from_pretrained('Xenova/wavlm-base'); + * const audio = await read_audio('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav', 16000); + * const inputs = await processor(audio); + * + * // Run model with inputs + * const model = await AutoModel.from_pretrained('Xenova/wavlm-base'); + * const output = await model(inputs); + * // { + * // last_hidden_state: Tensor { + * // dims: [ 1, 549, 768 ], + * // type: 'float32', + * // data: Float32Array(421632) [-0.349443256855011, -0.39341306686401367, 0.022836603224277496, ...], + * // size: 421632 + * // } + * // } + * ``` + */ +class WavLMModel extends WavLMPreTrainedModel { } + +/** + * WavLM Model with a `language modeling` head on top for Connectionist Temporal Classification (CTC). + */ +class WavLMForCTC extends WavLMPreTrainedModel { + /** + * @param {Object} model_inputs + * @param {Tensor} model_inputs.input_values Float values of input raw speech waveform. + * @param {Tensor} model_inputs.attention_mask Mask to avoid performing convolution and attention on padding token indices. Mask values selected in [0, 1] + */ + async _call(model_inputs) { + return new CausalLMOutput(await super._call(model_inputs)); + } +} + +/** + * WavLM Model with a sequence classification head on top (a linear layer over the pooled output). + */ +class WavLMForSequenceClassification extends WavLMPreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} + +/** + * WavLM Model with an XVector feature extraction head on top for tasks like Speaker Verification. + * + * **Example:** Extract speaker embeddings with `WavLMForXVector`. + * ```javascript + * import { AutoProcessor, AutoModel, read_audio } from '@huggingface/transformers'; + * + * // Read and preprocess audio + * const processor = await AutoProcessor.from_pretrained('Xenova/wavlm-base-plus-sv'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav'; + * const audio = await read_audio(url, 16000); + * const inputs = await processor(audio); + * + * // Run model with inputs + * const model = await AutoModel.from_pretrained('Xenova/wavlm-base-plus-sv'); + * const outputs = await model(inputs); + * // { + * // logits: Tensor { + * // dims: [ 1, 512 ], + * // type: 'float32', + * // data: Float32Array(512) [0.5847219228744507, ...], + * // size: 512 + * // }, + * // embeddings: Tensor { + * // dims: [ 1, 512 ], + * // type: 'float32', + * // data: Float32Array(512) [-0.09079201519489288, ...], + * // size: 512 + * // } + * // } + * ``` + */ +class WavLMForXVector extends WavLMPreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits and speaker embeddings. + */ + async _call(model_inputs) { + return new XVectorOutput(await super._call(model_inputs)); + } +} + +/** + * WavLM Model with a frame classification head on top for tasks like Speaker Diarization. + * + * **Example:** Perform speaker diarization with `WavLMForAudioFrameClassification`. + * ```javascript + * import { AutoProcessor, AutoModelForAudioFrameClassification, read_audio } from '@huggingface/transformers'; + * + * // Read and preprocess audio + * const processor = await AutoProcessor.from_pretrained('Xenova/wavlm-base-plus-sd'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav'; + * const audio = await read_audio(url, 16000); + * const inputs = await processor(audio); + * + * // Run model with inputs + * const model = await AutoModelForAudioFrameClassification.from_pretrained('Xenova/wavlm-base-plus-sd'); + * const { logits } = await model(inputs); + * // { + * // logits: Tensor { + * // dims: [ 1, 549, 2 ], // [batch_size, num_frames, num_speakers] + * // type: 'float32', + * // data: Float32Array(1098) [-3.5301010608673096, ...], + * // size: 1098 + * // } + * // } + * + * const labels = logits[0].sigmoid().tolist().map( + * frames => frames.map(speaker => speaker > 0.5 ? 1 : 0) + * ); + * console.log(labels); // labels is a one-hot array of shape (num_frames, num_speakers) + * // [ + * // [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], + * // [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], + * // [0, 0], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1], + * // ... + * // ] + * ``` + */ +class WavLMForAudioFrameClassification extends WavLMPreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} An object containing the model's output logits for sequence classification. + */ + async _call(model_inputs) { + return new TokenClassifierOutput(await super._call(model_inputs)); + } +} + +////////////////////////////////////////////////// +// SpeechT5 models +/** + * An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained models. + */ +class SpeechT5PreTrainedModel extends PreTrainedModel { }; + +/** + * The bare SpeechT5 Encoder-Decoder Model outputting raw hidden-states without any specific pre- or post-nets. + */ +class SpeechT5Model extends SpeechT5PreTrainedModel { }; + +/** + * SpeechT5 Model with a speech encoder and a text decoder. + * + * **Example:** Generate speech from text with `SpeechT5ForSpeechToText`. + * ```javascript + * import { AutoTokenizer, AutoProcessor, SpeechT5ForTextToSpeech, SpeechT5HifiGan, Tensor } from '@huggingface/transformers'; + * + * // Load the tokenizer and processor + * const tokenizer = await AutoTokenizer.from_pretrained('Xenova/speecht5_tts'); + * const processor = await AutoProcessor.from_pretrained('Xenova/speecht5_tts'); + * + * // Load the models + * // NOTE: We use the full-precision versions as they are more accurate + * const model = await SpeechT5ForTextToSpeech.from_pretrained('Xenova/speecht5_tts', { dtype: 'fp32' }); + * const vocoder = await SpeechT5HifiGan.from_pretrained('Xenova/speecht5_hifigan', { dtype: 'fp32' }); + * + * // Load speaker embeddings from URL + * const speaker_embeddings_data = new Float32Array( + * await (await fetch('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/speaker_embeddings.bin')).arrayBuffer() + * ); + * const speaker_embeddings = new Tensor( + * 'float32', + * speaker_embeddings_data, + * [1, speaker_embeddings_data.length] + * ) + * + * // Run tokenization + * const { input_ids } = tokenizer('Hello, my dog is cute'); + * + * // Generate waveform + * const { waveform } = await model.generate_speech(input_ids, speaker_embeddings, { vocoder }); + * console.log(waveform) + * // Tensor { + * // dims: [ 26112 ], + * // type: 'float32', + * // size: 26112, + * // data: Float32Array(26112) [ -0.00043630177970044315, -0.00018082228780258447, ... ], + * // } + * ``` + */ +class SpeechT5ForSpeechToText extends SpeechT5PreTrainedModel { } + +/** + * SpeechT5 Model with a text encoder and a speech decoder. + */ +class SpeechT5ForTextToSpeech extends SpeechT5PreTrainedModel { + + /** + * @typedef {Object} SpeechOutput + * @property {Tensor} [spectrogram] The predicted log-mel spectrogram of shape + * `(output_sequence_length, config.num_mel_bins)`. Returned when no `vocoder` is provided + * @property {Tensor} [waveform] The predicted waveform of shape `(num_frames,)`. Returned when a `vocoder` is provided. + * @property {Tensor} [cross_attentions] The outputs of the decoder's cross-attention layers of shape + * `(config.decoder_layers, config.decoder_attention_heads, output_sequence_length, input_sequence_length)`. returned when `output_cross_attentions` is `true`. + */ + + /** + * Converts a sequence of input tokens into a sequence of mel spectrograms, which are subsequently turned into a speech waveform using a vocoder. + * @param {Tensor} input_values Indices of input sequence tokens in the vocabulary. + * @param {Tensor} speaker_embeddings Tensor containing the speaker embeddings. + * @param {Object} options Optional parameters for generating speech. + * @param {number} [options.threshold=0.5] The generated sequence ends when the predicted stop token probability exceeds this value. + * @param {number} [options.minlenratio=0.0] Used to calculate the minimum required length for the output sequence. + * @param {number} [options.maxlenratio=20.0] Used to calculate the maximum allowed length for the output sequence. + * @param {Object} [options.vocoder=null] The vocoder that converts the mel spectrogram into a speech waveform. If `null`, the output is the mel spectrogram. + * @param {boolean} [options.output_cross_attentions=false] Whether or not to return the attentions tensors of the decoder's cross-attention layers. + * @returns {Promise} A promise which resolves to an object containing the spectrogram, waveform, and cross-attention tensors. + */ + async generate_speech(input_values, speaker_embeddings, { + threshold = 0.5, + minlenratio = 0.0, + maxlenratio = 20.0, + vocoder = null, + // output_cross_attentions = false, // TODO add + } = {}) { + + const model_inputs = { + input_ids: input_values + } + + const { encoder_outputs, encoder_attention_mask } = await encoderForward(this, model_inputs); + + const r = encoder_outputs.dims[1] / this.config.reduction_factor; + const maxlen = Math.floor(r * maxlenratio); + const minlen = Math.floor(r * minlenratio); + + const num_mel_bins = this.config.num_mel_bins; + + let spectrogramParts = []; + let past_key_values = null; + let decoder_outputs = null; + let idx = 0; + + while (true) { + ++idx; + + const use_cache_branch = boolTensor(!!decoder_outputs); + let output_sequence; + if (decoder_outputs) { + output_sequence = decoder_outputs.output_sequence_out; + } else { + output_sequence = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor( + 'float32', + new Float32Array(num_mel_bins), + [1, 1, num_mel_bins], + ) + } + let decoderFeeds = { + use_cache_branch, + output_sequence, + encoder_attention_mask: encoder_attention_mask, + speaker_embeddings: speaker_embeddings, + encoder_hidden_states: encoder_outputs, + }; + + this.addPastKeyValues(decoderFeeds, past_key_values); + decoder_outputs = await sessionRun(this.sessions['decoder_model_merged'], decoderFeeds); + past_key_values = this.getPastKeyValues(decoder_outputs, past_key_values); + + const { prob, spectrum } = decoder_outputs; + spectrogramParts.push(spectrum); + + if (idx >= minlen && ( + // Finished when stop token or maximum length is reached. + Array.from(prob.data).filter(p => p >= threshold).length > 0 || idx >= maxlen + )) { + break; + } + } + + const spectrogram = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.cat)(spectrogramParts); + const { waveform } = await sessionRun(vocoder.sessions['model'], { spectrogram }); + + return { + spectrogram, + waveform, + // cross_attentions: null, // TODO add + } + } +} + +/** + * HiFi-GAN vocoder. + * + * See [SpeechT5ForSpeechToText](./models#module_models.SpeechT5ForSpeechToText) for example usage. + */ +class SpeechT5HifiGan extends PreTrainedModel { + main_input_name = 'spectrogram'; +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// TrOCR models +class TrOCRPreTrainedModel extends PreTrainedModel { } + +/** + * The TrOCR Decoder with a language modeling head. + */ +class TrOCRForCausalLM extends TrOCRPreTrainedModel { } + +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Mistral models +/** + * The bare Mistral Model outputting raw hidden-states without any specific head on top. + */ +class MistralPreTrainedModel extends PreTrainedModel { } + +class MistralModel extends MistralPreTrainedModel { } + +class MistralForCausalLM extends MistralPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Starcoder2 models +/** + * The bare Starcoder2 Model outputting raw hidden-states without any specific head on top. + */ +class Starcoder2PreTrainedModel extends PreTrainedModel { } + +class Starcoder2Model extends Starcoder2PreTrainedModel { } + +class Starcoder2ForCausalLM extends Starcoder2PreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// Falcon models +/** + * The bare Falcon Model outputting raw hidden-states without any specific head on top. + */ +class FalconPreTrainedModel extends PreTrainedModel { } + +class FalconModel extends FalconPreTrainedModel { } + +class FalconForCausalLM extends FalconPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// CLAP models +class ClapPreTrainedModel extends PreTrainedModel { } + +class ClapModel extends ClapPreTrainedModel { } + +/** + * CLAP Text Model with a projection layer on top (a linear layer on top of the pooled output). + * + * **Example:** Compute text embeddings with `ClapTextModelWithProjection`. + * + * ```javascript + * import { AutoTokenizer, ClapTextModelWithProjection } from '@huggingface/transformers'; + * + * // Load tokenizer and text model + * const tokenizer = await AutoTokenizer.from_pretrained('Xenova/clap-htsat-unfused'); + * const text_model = await ClapTextModelWithProjection.from_pretrained('Xenova/clap-htsat-unfused'); + * + * // Run tokenization + * const texts = ['a sound of a cat', 'a sound of a dog']; + * const text_inputs = tokenizer(texts, { padding: true, truncation: true }); + * + * // Compute embeddings + * const { text_embeds } = await text_model(text_inputs); + * // Tensor { + * // dims: [ 2, 512 ], + * // type: 'float32', + * // data: Float32Array(1024) [ ... ], + * // size: 1024 + * // } + * ``` + */ +class ClapTextModelWithProjection extends ClapPreTrainedModel { + + /** @type {PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options = {}) { + // Update default model file name if not provided + options.model_file_name ??= 'text_model'; + return super.from_pretrained(pretrained_model_name_or_path, options); + } +} + +/** + * CLAP Audio Model with a projection layer on top (a linear layer on top of the pooled output). + * + * **Example:** Compute audio embeddings with `ClapAudioModelWithProjection`. + * + * ```javascript + * import { AutoProcessor, ClapAudioModelWithProjection, read_audio } from '@huggingface/transformers'; + * + * // Load processor and audio model + * const processor = await AutoProcessor.from_pretrained('Xenova/clap-htsat-unfused'); + * const audio_model = await ClapAudioModelWithProjection.from_pretrained('Xenova/clap-htsat-unfused'); + * + * // Read audio and run processor + * const audio = await read_audio('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/cat_meow.wav'); + * const audio_inputs = await processor(audio); + * + * // Compute embeddings + * const { audio_embeds } = await audio_model(audio_inputs); + * // Tensor { + * // dims: [ 1, 512 ], + * // type: 'float32', + * // data: Float32Array(512) [ ... ], + * // size: 512 + * // } + * ``` + */ +class ClapAudioModelWithProjection extends ClapPreTrainedModel { + /** @type {PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, options = {}) { + // Update default model file name if not provided + options.model_file_name ??= 'audio_model'; + return super.from_pretrained(pretrained_model_name_or_path, options); + } +} +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +// VITS models +class VitsPreTrainedModel extends PreTrainedModel { } + +/** + * The complete VITS model, for text-to-speech synthesis. + * + * **Example:** Generate speech from text with `VitsModel`. + * ```javascript + * import { AutoTokenizer, VitsModel } from '@huggingface/transformers'; + * + * // Load the tokenizer and model + * const tokenizer = await AutoTokenizer.from_pretrained('Xenova/mms-tts-eng'); + * const model = await VitsModel.from_pretrained('Xenova/mms-tts-eng'); + * + * // Run tokenization + * const inputs = tokenizer('I love transformers'); + * + * // Generate waveform + * const { waveform } = await model(inputs); + * // Tensor { + * // dims: [ 1, 35328 ], + * // type: 'float32', + * // data: Float32Array(35328) [ ... ], + * // size: 35328, + * // } + * ``` + */ +class VitsModel extends VitsPreTrainedModel { + /** + * Calls the model on new inputs. + * @param {Object} model_inputs The inputs to the model. + * @returns {Promise} The outputs for the VITS model. + */ + async _call(model_inputs) { + return new VitsModelOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Segformer models +class SegformerPreTrainedModel extends PreTrainedModel { } + +/** + * The bare SegFormer encoder (Mix-Transformer) outputting raw hidden-states without any specific head on top. + */ +class SegformerModel extends SegformerPreTrainedModel { } + +/** + * SegFormer Model transformer with an image classification head on top (a linear layer on top of the final hidden states) e.g. for ImageNet. + */ +class SegformerForImageClassification extends SegformerPreTrainedModel { } + +/** + * SegFormer Model transformer with an all-MLP decode head on top e.g. for ADE20k, CityScapes. + */ +class SegformerForSemanticSegmentation extends SegformerPreTrainedModel { } + +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// StableLm models +class StableLmPreTrainedModel extends PreTrainedModel { } + +/** + * The bare StableLm Model transformer outputting raw hidden-states without any specific head on top. + */ +class StableLmModel extends StableLmPreTrainedModel { } + +/** + * StableLm Model with a `language modeling` head on top for Causal Language Modeling (with past). + */ +class StableLmForCausalLM extends StableLmPreTrainedModel { } +////////////////////////////////////////////////// + + +////////////////////////////////////////////////// +class EfficientNetPreTrainedModel extends PreTrainedModel { } + +/** + * The bare EfficientNet model outputting raw features without any specific head on top. + */ +class EfficientNetModel extends EfficientNetPreTrainedModel { } + +/** + * EfficientNet Model with an image classification head on top (a linear layer on top of the pooled features). + */ +class EfficientNetForImageClassification extends EfficientNetPreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Musicgen models +class MusicgenPreTrainedModel extends PreTrainedModel { } + +/** + * The bare Musicgen decoder model outputting raw hidden-states without any specific head on top. + */ +class MusicgenModel extends MusicgenPreTrainedModel { } + +/** + * The MusicGen decoder model with a language modelling head on top. + */ +class MusicgenForCausalLM extends MusicgenPreTrainedModel { } + +/** + * The composite MusicGen model with a text encoder, audio encoder and Musicgen decoder, + * for music generation tasks with one or both of text and audio prompts. + * + * **Example:** Generate music from text with `Xenova/musicgen-small`. + * ```javascript + * import { AutoTokenizer, MusicgenForConditionalGeneration } from '@huggingface/transformers'; + * + * // Load tokenizer and model + * const tokenizer = await AutoTokenizer.from_pretrained('Xenova/musicgen-small'); + * const model = await MusicgenForConditionalGeneration.from_pretrained( + * 'Xenova/musicgen-small', { dtype: 'fp32' } + * ); + * + * // Prepare text input + * const prompt = '80s pop track with bassy drums and synth'; + * const inputs = tokenizer(prompt); + * + * // Generate audio + * const audio_values = await model.generate({ + * ...inputs, + * max_new_tokens: 512, + * do_sample: true, + * guidance_scale: 3, + * }); + * + * // (Optional) Write the output to a WAV file + * import wavefile from 'wavefile'; + * import fs from 'fs'; + * + * const wav = new wavefile.WaveFile(); + * wav.fromScratch(1, model.config.audio_encoder.sampling_rate, '32f', audio_values.data); + * fs.writeFileSync('musicgen_out.wav', wav.toBuffer()); + * ``` + */ +class MusicgenForConditionalGeneration extends PreTrainedModel { // NOTE: not MusicgenPreTrainedModel + forward_params = [ + 'input_ids', + 'attention_mask', + 'encoder_outputs', + 'decoder_input_ids', + 'decoder_attention_mask', + 'past_key_values', + ]; + + /** + * Apply the pattern mask to the final ids, + * then revert the pattern delay mask by filtering the pad token id in a single step. + * @param {Tensor} outputs The output tensor from the model. + * @returns {Tensor} The filtered output tensor. + */ + _apply_and_filter_by_delay_pattern_mask(outputs) { + const [bs_x_codebooks, seqLength] = outputs.dims; + const num_codebooks = this.config.decoder.num_codebooks; + const upperBound = (seqLength - num_codebooks); + + let newDataSize = 0; + for (let i = 0; i < outputs.size; ++i) { + if (outputs.data[i] === this.config.decoder.pad_token_id) { + continue; + } + + const row = (i % seqLength); + const col = Math.floor(i / seqLength) % num_codebooks; + + const diff = row - col; + if (diff > 0 && diff <= upperBound) { + outputs.data[newDataSize++] = outputs.data[i]; + } + } + + const batch_size = Math.floor(bs_x_codebooks / num_codebooks); + const inferred = newDataSize / (batch_size * num_codebooks); + // TODO: assert `inferred` is an integer + return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_9__.Tensor( + outputs.type, + outputs.data.slice(0, newDataSize), + [batch_size, num_codebooks, inferred] + ); + } + + + prepare_inputs_for_generation(input_ids, model_inputs, generation_config) { + // apply the delay pattern mask + let clonedInputIds = structuredClone(input_ids); + for (let i = 0; i < clonedInputIds.length; ++i) { + for (let j = 0; j < clonedInputIds[i].length; ++j) { + if ((i % this.config.decoder.num_codebooks) >= j) { + clonedInputIds[i][j] = BigInt(this.config.decoder.pad_token_id); + } + } + } + // for classifier free guidance we need to replicate the decoder args across the batch dim + // (we'll split these before sampling) + if (generation_config.guidance_scale !== null && generation_config.guidance_scale > 1) { + // [batch, seqLength] -> [2 * batch, seqLength] + clonedInputIds = clonedInputIds.concat(clonedInputIds); + } + + const prepped = super.prepare_inputs_for_generation(clonedInputIds, model_inputs, generation_config); + return prepped; + } + + /** + * Generates sequences of token ids for models with a language modeling head. + * @param {import('./generation/parameters.js').GenerationFunctionParameters} options + * @returns {Promise} The output of the model, which can contain the generated token ids, attentions, and scores. + */ + async generate(options) { + + const output_ids = await super.generate(options); + + // apply the pattern mask to the final ids + // tensor: int64[1,batch_size,4,chunk_length] + const audio_codes = this._apply_and_filter_by_delay_pattern_mask( + /** @type {Tensor} */(output_ids) + ).unsqueeze_(0); // append the frame dimension back to the audio codes + + const { audio_values } = await sessionRun(this.sessions['encodec_decode'], { audio_codes }) + + return audio_values; + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// MobileNetV1 models +class MobileNetV1PreTrainedModel extends PreTrainedModel { } + +/** + * The bare MobileNetV1 model outputting raw hidden-states without any specific head on top. + */ +class MobileNetV1Model extends MobileNetV1PreTrainedModel { } + +/** + * MobileNetV1 model with an image classification head on top (a linear layer on top of the pooled features), + * e.g. for ImageNet. + */ +class MobileNetV1ForImageClassification extends MobileNetV1PreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// MobileNetV2 models +class MobileNetV2PreTrainedModel extends PreTrainedModel { } + +/** + * The bare MobileNetV2 model outputting raw hidden-states without any specific head on top. + */ +class MobileNetV2Model extends MobileNetV2PreTrainedModel { } + +/** + * MobileNetV2 model with an image classification head on top (a linear layer on top of the pooled features), + * e.g. for ImageNet. + */ +class MobileNetV2ForImageClassification extends MobileNetV2PreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// MobileNetV3 models +class MobileNetV3PreTrainedModel extends PreTrainedModel { } + +/** + * The bare MobileNetV3 model outputting raw hidden-states without any specific head on top. + */ +class MobileNetV3Model extends MobileNetV3PreTrainedModel { } + +/** + * MobileNetV3 model with an image classification head on top (a linear layer on top of the pooled features), + * e.g. for ImageNet. + */ +class MobileNetV3ForImageClassification extends MobileNetV3PreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// MobileNetV4 models +class MobileNetV4PreTrainedModel extends PreTrainedModel { } + +/** + * The bare MobileNetV4 model outputting raw hidden-states without any specific head on top. + */ +class MobileNetV4Model extends MobileNetV4PreTrainedModel { } + +/** + * MobileNetV4 model with an image classification head on top (a linear layer on top of the pooled features), + * e.g. for ImageNet. + */ +class MobileNetV4ForImageClassification extends MobileNetV4PreTrainedModel { + /** + * @param {any} model_inputs + */ + async _call(model_inputs) { + return new SequenceClassifierOutput(await super._call(model_inputs)); + } +} +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// Decision Transformer models +class DecisionTransformerPreTrainedModel extends PreTrainedModel { } + +/** + * The model builds upon the GPT2 architecture to perform autoregressive prediction of actions in an offline RL setting. + * Refer to the paper for more details: https://arxiv.org/abs/2106.01345 + */ +class DecisionTransformerModel extends DecisionTransformerPreTrainedModel { } + +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +// AutoModels, used to simplify construction of PreTrainedModels +// (uses config to instantiate correct class) + +/** + * Base class of all AutoModels. Contains the `from_pretrained` function + * which is used to instantiate pretrained models. + */ +class PretrainedMixin { + /** + * Mapping from model type to model class. + * @type {Map[]} + */ + static MODEL_CLASS_MAPPINGS = null; + + /** + * Whether to attempt to instantiate the base class (`PretrainedModel`) if + * the model type is not found in the mapping. + */ + static BASE_IF_FAIL = false; + + + /** @type {typeof PreTrainedModel.from_pretrained} */ + static async from_pretrained(pretrained_model_name_or_path, { + progress_callback = null, + config = null, + cache_dir = null, + local_files_only = false, + revision = 'main', + model_file_name = null, + subfolder = 'onnx', + device = null, + dtype = null, + use_external_data_format = null, + session_options = {}, + } = {}) { + + const options = { + progress_callback, + config, + cache_dir, + local_files_only, + revision, + model_file_name, + subfolder, + device, + dtype, + use_external_data_format, + session_options, + } + options.config = await _configs_js__WEBPACK_IMPORTED_MODULE_0__.AutoConfig.from_pretrained(pretrained_model_name_or_path, options); + + if (!this.MODEL_CLASS_MAPPINGS) { + throw new Error("`MODEL_CLASS_MAPPINGS` not implemented for this type of `AutoClass`: " + this.name); + } + + for (const MODEL_CLASS_MAPPING of this.MODEL_CLASS_MAPPINGS) { + const modelInfo = MODEL_CLASS_MAPPING.get(options.config.model_type); + if (!modelInfo) { + continue; // Item not found in this mapping + } + return await modelInfo[1].from_pretrained(pretrained_model_name_or_path, options); + } + + if (this.BASE_IF_FAIL) { + console.warn(`Unknown model class "${options.config.model_type}", attempting to construct from base class.`); + return await PreTrainedModel.from_pretrained(pretrained_model_name_or_path, options); + } else { + throw Error(`Unsupported model type: ${options.config.model_type}`) + } + } +} + +const MODEL_MAPPING_NAMES_ENCODER_ONLY = new Map([ + ['bert', ['BertModel', BertModel]], + ['nomic_bert', ['NomicBertModel', NomicBertModel]], + ['roformer', ['RoFormerModel', RoFormerModel]], + ['electra', ['ElectraModel', ElectraModel]], + ['esm', ['EsmModel', EsmModel]], + ['convbert', ['ConvBertModel', ConvBertModel]], + ['camembert', ['CamembertModel', CamembertModel]], + ['deberta', ['DebertaModel', DebertaModel]], + ['deberta-v2', ['DebertaV2Model', DebertaV2Model]], + ['mpnet', ['MPNetModel', MPNetModel]], + ['albert', ['AlbertModel', AlbertModel]], + ['distilbert', ['DistilBertModel', DistilBertModel]], + ['roberta', ['RobertaModel', RobertaModel]], + ['xlm', ['XLMModel', XLMModel]], + ['xlm-roberta', ['XLMRobertaModel', XLMRobertaModel]], + ['clap', ['ClapModel', ClapModel]], + ['clip', ['CLIPModel', CLIPModel]], + ['clipseg', ['CLIPSegModel', CLIPSegModel]], + ['chinese_clip', ['ChineseCLIPModel', ChineseCLIPModel]], + ['siglip', ['SiglipModel', SiglipModel]], + ['mobilebert', ['MobileBertModel', MobileBertModel]], + ['squeezebert', ['SqueezeBertModel', SqueezeBertModel]], + ['wav2vec2', ['Wav2Vec2Model', Wav2Vec2Model]], + ['wav2vec2-bert', ['Wav2Vec2BertModel', Wav2Vec2BertModel]], + ['unispeech', ['UniSpeechModel', UniSpeechModel]], + ['unispeech-sat', ['UniSpeechSatModel', UniSpeechSatModel]], + ['hubert', ['HubertModel', HubertModel]], + ['wavlm', ['WavLMModel', WavLMModel]], + ['audio-spectrogram-transformer', ['ASTModel', ASTModel]], + ['vits', ['VitsModel', VitsModel]], + ['pyannote', ['PyAnnoteModel', PyAnnoteModel]], + ['wespeaker-resnet', ['WeSpeakerResNetModel', WeSpeakerResNetModel]], + + ['detr', ['DetrModel', DetrModel]], + ['rt_detr', ['RTDetrModel', RTDetrModel]], + ['table-transformer', ['TableTransformerModel', TableTransformerModel]], + ['vit', ['ViTModel', ViTModel]], + ['pvt', ['PvtModel', PvtModel]], + ['vit_msn', ['ViTMSNModel', ViTMSNModel]], + ['vit_mae', ['ViTMAEModel', ViTMAEModel]], + ['groupvit', ['GroupViTModel', GroupViTModel]], + ['fastvit', ['FastViTModel', FastViTModel]], + ['mobilevit', ['MobileViTModel', MobileViTModel]], + ['mobilevitv2', ['MobileViTV2Model', MobileViTV2Model]], + ['owlvit', ['OwlViTModel', OwlViTModel]], + ['owlv2', ['Owlv2Model', Owlv2Model]], + ['beit', ['BeitModel', BeitModel]], + ['deit', ['DeiTModel', DeiTModel]], + ['hiera', ['HieraModel', HieraModel]], + ['convnext', ['ConvNextModel', ConvNextModel]], + ['convnextv2', ['ConvNextV2Model', ConvNextV2Model]], + ['dinov2', ['Dinov2Model', Dinov2Model]], + ['resnet', ['ResNetModel', ResNetModel]], + ['swin', ['SwinModel', SwinModel]], + ['swin2sr', ['Swin2SRModel', Swin2SRModel]], + ['donut-swin', ['DonutSwinModel', DonutSwinModel]], + ['yolos', ['YolosModel', YolosModel]], + ['dpt', ['DPTModel', DPTModel]], + ['glpn', ['GLPNModel', GLPNModel]], + + ['hifigan', ['SpeechT5HifiGan', SpeechT5HifiGan]], + ['efficientnet', ['EfficientNetModel', EfficientNetModel]], + + ['decision_transformer', ['DecisionTransformerModel', DecisionTransformerModel]], + + ['mobilenet_v1', ['MobileNetV1Model', MobileNetV1Model]], + ['mobilenet_v2', ['MobileNetV2Model', MobileNetV2Model]], + ['mobilenet_v3', ['MobileNetV3Model', MobileNetV3Model]], + ['mobilenet_v4', ['MobileNetV4Model', MobileNetV4Model]], + + ['maskformer', ['MaskFormerModel', MaskFormerModel]], +]); + +const MODEL_MAPPING_NAMES_ENCODER_DECODER = new Map([ + ['t5', ['T5Model', T5Model]], + ['longt5', ['LongT5Model', LongT5Model]], + ['mt5', ['MT5Model', MT5Model]], + ['bart', ['BartModel', BartModel]], + ['mbart', ['MBartModel', MBartModel]], + ['marian', ['MarianModel', MarianModel]], + ['whisper', ['WhisperModel', WhisperModel]], + ['m2m_100', ['M2M100Model', M2M100Model]], + ['blenderbot', ['BlenderbotModel', BlenderbotModel]], + ['blenderbot-small', ['BlenderbotSmallModel', BlenderbotSmallModel]], +]); + + +const MODEL_MAPPING_NAMES_DECODER_ONLY = new Map([ + ['bloom', ['BloomModel', BloomModel]], + ['jais', ['JAISModel', JAISModel]], + ['gpt2', ['GPT2Model', GPT2Model]], + ['gptj', ['GPTJModel', GPTJModel]], + ['gpt_bigcode', ['GPTBigCodeModel', GPTBigCodeModel]], + ['gpt_neo', ['GPTNeoModel', GPTNeoModel]], + ['gpt_neox', ['GPTNeoXModel', GPTNeoXModel]], + ['codegen', ['CodeGenModel', CodeGenModel]], + ['llama', ['LlamaModel', LlamaModel]], + ['olmo', ['OlmoModel', OlmoModel]], + ['mobilellm', ['MobileLLMModel', MobileLLMModel]], + ['granite', ['GraniteModel', GraniteModel]], + ['cohere', ['CohereModel', CohereModel]], + ['gemma', ['GemmaModel', GemmaModel]], + ['gemma2', ['Gemma2Model', Gemma2Model]], + ['openelm', ['OpenELMModel', OpenELMModel]], + ['qwen2', ['Qwen2Model', Qwen2Model]], + ['phi', ['PhiModel', PhiModel]], + ['phi3', ['Phi3Model', Phi3Model]], + ['mpt', ['MptModel', MptModel]], + ['opt', ['OPTModel', OPTModel]], + ['mistral', ['MistralModel', MistralModel]], + ['starcoder2', ['Starcoder2Model', Starcoder2Model]], + ['falcon', ['FalconModel', FalconModel]], + ['stablelm', ['StableLmModel', StableLmModel]], +]); + +const MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES = new Map([ + ['speecht5', ['SpeechT5ForSpeechToText', SpeechT5ForSpeechToText]], + ['whisper', ['WhisperForConditionalGeneration', WhisperForConditionalGeneration]], +]); + +const MODEL_FOR_TEXT_TO_SPECTROGRAM_MAPPING_NAMES = new Map([ + ['speecht5', ['SpeechT5ForTextToSpeech', SpeechT5ForTextToSpeech]], +]); + +const MODEL_FOR_TEXT_TO_WAVEFORM_MAPPING_NAMES = new Map([ + ['vits', ['VitsModel', VitsModel]], + ['musicgen', ['MusicgenForConditionalGeneration', MusicgenForConditionalGeneration]], +]); + +const MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES = new Map([ + ['bert', ['BertForSequenceClassification', BertForSequenceClassification]], + ['roformer', ['RoFormerForSequenceClassification', RoFormerForSequenceClassification]], + ['electra', ['ElectraForSequenceClassification', ElectraForSequenceClassification]], + ['esm', ['EsmForSequenceClassification', EsmForSequenceClassification]], + ['convbert', ['ConvBertForSequenceClassification', ConvBertForSequenceClassification]], + ['camembert', ['CamembertForSequenceClassification', CamembertForSequenceClassification]], + ['deberta', ['DebertaForSequenceClassification', DebertaForSequenceClassification]], + ['deberta-v2', ['DebertaV2ForSequenceClassification', DebertaV2ForSequenceClassification]], + ['mpnet', ['MPNetForSequenceClassification', MPNetForSequenceClassification]], + ['albert', ['AlbertForSequenceClassification', AlbertForSequenceClassification]], + ['distilbert', ['DistilBertForSequenceClassification', DistilBertForSequenceClassification]], + ['roberta', ['RobertaForSequenceClassification', RobertaForSequenceClassification]], + ['xlm', ['XLMForSequenceClassification', XLMForSequenceClassification]], + ['xlm-roberta', ['XLMRobertaForSequenceClassification', XLMRobertaForSequenceClassification]], + ['bart', ['BartForSequenceClassification', BartForSequenceClassification]], + ['mbart', ['MBartForSequenceClassification', MBartForSequenceClassification]], + ['mobilebert', ['MobileBertForSequenceClassification', MobileBertForSequenceClassification]], + ['squeezebert', ['SqueezeBertForSequenceClassification', SqueezeBertForSequenceClassification]], +]); + +const MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES = new Map([ + ['bert', ['BertForTokenClassification', BertForTokenClassification]], + ['roformer', ['RoFormerForTokenClassification', RoFormerForTokenClassification]], + ['electra', ['ElectraForTokenClassification', ElectraForTokenClassification]], + ['esm', ['EsmForTokenClassification', EsmForTokenClassification]], + ['convbert', ['ConvBertForTokenClassification', ConvBertForTokenClassification]], + ['camembert', ['CamembertForTokenClassification', CamembertForTokenClassification]], + ['deberta', ['DebertaForTokenClassification', DebertaForTokenClassification]], + ['deberta-v2', ['DebertaV2ForTokenClassification', DebertaV2ForTokenClassification]], + ['mpnet', ['MPNetForTokenClassification', MPNetForTokenClassification]], + ['distilbert', ['DistilBertForTokenClassification', DistilBertForTokenClassification]], + ['roberta', ['RobertaForTokenClassification', RobertaForTokenClassification]], + ['xlm', ['XLMForTokenClassification', XLMForTokenClassification]], + ['xlm-roberta', ['XLMRobertaForTokenClassification', XLMRobertaForTokenClassification]], +]); + +const MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES = new Map([ + ['t5', ['T5ForConditionalGeneration', T5ForConditionalGeneration]], + ['longt5', ['LongT5ForConditionalGeneration', LongT5ForConditionalGeneration]], + ['mt5', ['MT5ForConditionalGeneration', MT5ForConditionalGeneration]], + ['bart', ['BartForConditionalGeneration', BartForConditionalGeneration]], + ['mbart', ['MBartForConditionalGeneration', MBartForConditionalGeneration]], + ['marian', ['MarianMTModel', MarianMTModel]], + ['m2m_100', ['M2M100ForConditionalGeneration', M2M100ForConditionalGeneration]], + ['blenderbot', ['BlenderbotForConditionalGeneration', BlenderbotForConditionalGeneration]], + ['blenderbot-small', ['BlenderbotSmallForConditionalGeneration', BlenderbotSmallForConditionalGeneration]], +]); + +const MODEL_FOR_CAUSAL_LM_MAPPING_NAMES = new Map([ + ['bloom', ['BloomForCausalLM', BloomForCausalLM]], + ['gpt2', ['GPT2LMHeadModel', GPT2LMHeadModel]], + ['jais', ['JAISLMHeadModel', JAISLMHeadModel]], + ['gptj', ['GPTJForCausalLM', GPTJForCausalLM]], + ['gpt_bigcode', ['GPTBigCodeForCausalLM', GPTBigCodeForCausalLM]], + ['gpt_neo', ['GPTNeoForCausalLM', GPTNeoForCausalLM]], + ['gpt_neox', ['GPTNeoXForCausalLM', GPTNeoXForCausalLM]], + ['codegen', ['CodeGenForCausalLM', CodeGenForCausalLM]], + ['llama', ['LlamaForCausalLM', LlamaForCausalLM]], + ['olmo', ['OlmoForCausalLM', OlmoForCausalLM]], + ['mobilellm', ['MobileLLMForCausalLM', MobileLLMForCausalLM]], + ['granite', ['GraniteForCausalLM', GraniteForCausalLM]], + ['cohere', ['CohereForCausalLM', CohereForCausalLM]], + ['gemma', ['GemmaForCausalLM', GemmaForCausalLM]], + ['gemma2', ['Gemma2ForCausalLM', Gemma2ForCausalLM]], + ['openelm', ['OpenELMForCausalLM', OpenELMForCausalLM]], + ['qwen2', ['Qwen2ForCausalLM', Qwen2ForCausalLM]], + ['phi', ['PhiForCausalLM', PhiForCausalLM]], + ['phi3', ['Phi3ForCausalLM', Phi3ForCausalLM]], + ['mpt', ['MptForCausalLM', MptForCausalLM]], + ['opt', ['OPTForCausalLM', OPTForCausalLM]], + ['mbart', ['MBartForCausalLM', MBartForCausalLM]], + ['mistral', ['MistralForCausalLM', MistralForCausalLM]], + ['starcoder2', ['Starcoder2ForCausalLM', Starcoder2ForCausalLM]], + ['falcon', ['FalconForCausalLM', FalconForCausalLM]], + ['trocr', ['TrOCRForCausalLM', TrOCRForCausalLM]], + ['stablelm', ['StableLmForCausalLM', StableLmForCausalLM]], +]); + +const MODEL_FOR_MASKED_LM_MAPPING_NAMES = new Map([ + ['bert', ['BertForMaskedLM', BertForMaskedLM]], + ['roformer', ['RoFormerForMaskedLM', RoFormerForMaskedLM]], + ['electra', ['ElectraForMaskedLM', ElectraForMaskedLM]], + ['esm', ['EsmForMaskedLM', EsmForMaskedLM]], + ['convbert', ['ConvBertForMaskedLM', ConvBertForMaskedLM]], + ['camembert', ['CamembertForMaskedLM', CamembertForMaskedLM]], + ['deberta', ['DebertaForMaskedLM', DebertaForMaskedLM]], + ['deberta-v2', ['DebertaV2ForMaskedLM', DebertaV2ForMaskedLM]], + ['mpnet', ['MPNetForMaskedLM', MPNetForMaskedLM]], + ['albert', ['AlbertForMaskedLM', AlbertForMaskedLM]], + ['distilbert', ['DistilBertForMaskedLM', DistilBertForMaskedLM]], + ['roberta', ['RobertaForMaskedLM', RobertaForMaskedLM]], + ['xlm', ['XLMWithLMHeadModel', XLMWithLMHeadModel]], + ['xlm-roberta', ['XLMRobertaForMaskedLM', XLMRobertaForMaskedLM]], + ['mobilebert', ['MobileBertForMaskedLM', MobileBertForMaskedLM]], + ['squeezebert', ['SqueezeBertForMaskedLM', SqueezeBertForMaskedLM]], +]); + +const MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES = new Map([ + ['bert', ['BertForQuestionAnswering', BertForQuestionAnswering]], + ['roformer', ['RoFormerForQuestionAnswering', RoFormerForQuestionAnswering]], + ['electra', ['ElectraForQuestionAnswering', ElectraForQuestionAnswering]], + ['convbert', ['ConvBertForQuestionAnswering', ConvBertForQuestionAnswering]], + ['camembert', ['CamembertForQuestionAnswering', CamembertForQuestionAnswering]], + ['deberta', ['DebertaForQuestionAnswering', DebertaForQuestionAnswering]], + ['deberta-v2', ['DebertaV2ForQuestionAnswering', DebertaV2ForQuestionAnswering]], + ['mpnet', ['MPNetForQuestionAnswering', MPNetForQuestionAnswering]], + ['albert', ['AlbertForQuestionAnswering', AlbertForQuestionAnswering]], + ['distilbert', ['DistilBertForQuestionAnswering', DistilBertForQuestionAnswering]], + ['roberta', ['RobertaForQuestionAnswering', RobertaForQuestionAnswering]], + ['xlm', ['XLMForQuestionAnswering', XLMForQuestionAnswering]], + ['xlm-roberta', ['XLMRobertaForQuestionAnswering', XLMRobertaForQuestionAnswering]], + ['mobilebert', ['MobileBertForQuestionAnswering', MobileBertForQuestionAnswering]], + ['squeezebert', ['SqueezeBertForQuestionAnswering', SqueezeBertForQuestionAnswering]], +]); + +const MODEL_FOR_VISION_2_SEQ_MAPPING_NAMES = new Map([ + ['vision-encoder-decoder', ['VisionEncoderDecoderModel', VisionEncoderDecoderModel]], +]); + +const MODEL_FOR_IMAGE_TEXT_TO_TEXT_MAPPING_NAMES = new Map([ + ['llava', ['LlavaForConditionalGeneration', LlavaForConditionalGeneration]], + ['moondream1', ['Moondream1ForConditionalGeneration', Moondream1ForConditionalGeneration]], + ['florence2', ['Florence2ForConditionalGeneration', Florence2ForConditionalGeneration]], +]); + +const MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING_NAMES = new Map([ + ['vision-encoder-decoder', ['VisionEncoderDecoderModel', VisionEncoderDecoderModel]], +]); + +const MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES = new Map([ + ['vit', ['ViTForImageClassification', ViTForImageClassification]], + ['pvt', ['PvtForImageClassification', PvtForImageClassification]], + ['vit_msn', ['ViTMSNForImageClassification', ViTMSNForImageClassification]], + ['fastvit', ['FastViTForImageClassification', FastViTForImageClassification]], + ['mobilevit', ['MobileViTForImageClassification', MobileViTForImageClassification]], + ['mobilevitv2', ['MobileViTV2ForImageClassification', MobileViTV2ForImageClassification]], + ['beit', ['BeitForImageClassification', BeitForImageClassification]], + ['deit', ['DeiTForImageClassification', DeiTForImageClassification]], + ['hiera', ['HieraForImageClassification', HieraForImageClassification]], + ['convnext', ['ConvNextForImageClassification', ConvNextForImageClassification]], + ['convnextv2', ['ConvNextV2ForImageClassification', ConvNextV2ForImageClassification]], + ['dinov2', ['Dinov2ForImageClassification', Dinov2ForImageClassification]], + ['resnet', ['ResNetForImageClassification', ResNetForImageClassification]], + ['swin', ['SwinForImageClassification', SwinForImageClassification]], + ['segformer', ['SegformerForImageClassification', SegformerForImageClassification]], + ['efficientnet', ['EfficientNetForImageClassification', EfficientNetForImageClassification]], + ['mobilenet_v1', ['MobileNetV1ForImageClassification', MobileNetV1ForImageClassification]], + ['mobilenet_v2', ['MobileNetV2ForImageClassification', MobileNetV2ForImageClassification]], + ['mobilenet_v3', ['MobileNetV3ForImageClassification', MobileNetV3ForImageClassification]], + ['mobilenet_v4', ['MobileNetV4ForImageClassification', MobileNetV4ForImageClassification]], +]); + +const MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES = new Map([ + ['detr', ['DetrForObjectDetection', DetrForObjectDetection]], + ['rt_detr', ['RTDetrForObjectDetection', RTDetrForObjectDetection]], + ['table-transformer', ['TableTransformerForObjectDetection', TableTransformerForObjectDetection]], + ['yolos', ['YolosForObjectDetection', YolosForObjectDetection]], +]); + +const MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING_NAMES = new Map([ + ['owlvit', ['OwlViTForObjectDetection', OwlViTForObjectDetection]], + ['owlv2', ['Owlv2ForObjectDetection', Owlv2ForObjectDetection]], +]); + +const MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES = new Map([ + // TODO: Do not add new models here + ['detr', ['DetrForSegmentation', DetrForSegmentation]], + ['clipseg', ['CLIPSegForImageSegmentation', CLIPSegForImageSegmentation]], +]); + +const MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING_NAMES = new Map([ + ['segformer', ['SegformerForSemanticSegmentation', SegformerForSemanticSegmentation]], + ['sapiens', ['SapiensForSemanticSegmentation', SapiensForSemanticSegmentation]], +]); + +const MODEL_FOR_UNIVERSAL_SEGMENTATION_MAPPING_NAMES = new Map([ + ['detr', ['DetrForSegmentation', DetrForSegmentation]], + ['maskformer', ['MaskFormerForInstanceSegmentation', MaskFormerForInstanceSegmentation]], +]); + +const MODEL_FOR_MASK_GENERATION_MAPPING_NAMES = new Map([ + ['sam', ['SamModel', SamModel]], +]); + +const MODEL_FOR_CTC_MAPPING_NAMES = new Map([ + ['wav2vec2', ['Wav2Vec2ForCTC', Wav2Vec2ForCTC]], + ['wav2vec2-bert', ['Wav2Vec2BertForCTC', Wav2Vec2BertForCTC]], + ['unispeech', ['UniSpeechForCTC', UniSpeechForCTC]], + ['unispeech-sat', ['UniSpeechSatForCTC', UniSpeechSatForCTC]], + ['wavlm', ['WavLMForCTC', WavLMForCTC]], + ['hubert', ['HubertForCTC', HubertForCTC]], +]); + +const MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES = new Map([ + ['wav2vec2', ['Wav2Vec2ForSequenceClassification', Wav2Vec2ForSequenceClassification]], + ['wav2vec2-bert', ['Wav2Vec2BertForSequenceClassification', Wav2Vec2BertForSequenceClassification]], + ['unispeech', ['UniSpeechForSequenceClassification', UniSpeechForSequenceClassification]], + ['unispeech-sat', ['UniSpeechSatForSequenceClassification', UniSpeechSatForSequenceClassification]], + ['wavlm', ['WavLMForSequenceClassification', WavLMForSequenceClassification]], + ['hubert', ['HubertForSequenceClassification', HubertForSequenceClassification]], + ['audio-spectrogram-transformer', ['ASTForAudioClassification', ASTForAudioClassification]], +]); + +const MODEL_FOR_AUDIO_XVECTOR_MAPPING_NAMES = new Map([ + ['wavlm', ['WavLMForXVector', WavLMForXVector]], +]); + +const MODEL_FOR_AUDIO_FRAME_CLASSIFICATION_MAPPING_NAMES = new Map([ + ['unispeech-sat', ['UniSpeechSatForAudioFrameClassification', UniSpeechSatForAudioFrameClassification]], + ['wavlm', ['WavLMForAudioFrameClassification', WavLMForAudioFrameClassification]], + ['wav2vec2', ['Wav2Vec2ForAudioFrameClassification', Wav2Vec2ForAudioFrameClassification]], + ['pyannote', ['PyAnnoteForAudioFrameClassification', PyAnnoteForAudioFrameClassification]], +]); + +const MODEL_FOR_IMAGE_MATTING_MAPPING_NAMES = new Map([ + ['vitmatte', ['VitMatteForImageMatting', VitMatteForImageMatting]], +]); + +const MODEL_FOR_IMAGE_TO_IMAGE_MAPPING_NAMES = new Map([ + ['swin2sr', ['Swin2SRForImageSuperResolution', Swin2SRForImageSuperResolution]], +]) + +const MODEL_FOR_DEPTH_ESTIMATION_MAPPING_NAMES = new Map([ + ['dpt', ['DPTForDepthEstimation', DPTForDepthEstimation]], + ['depth_anything', ['DepthAnythingForDepthEstimation', DepthAnythingForDepthEstimation]], + ['glpn', ['GLPNForDepthEstimation', GLPNForDepthEstimation]], + ['sapiens', ['SapiensForDepthEstimation', SapiensForDepthEstimation]], + ['depth_pro', ['DepthProForDepthEstimation', DepthProForDepthEstimation]], +]) + +const MODEL_FOR_NORMAL_ESTIMATION_MAPPING_NAMES = new Map([ + ['sapiens', ['SapiensForNormalEstimation', SapiensForNormalEstimation]], +]) + +// NOTE: This is custom to Transformers.js, and is necessary because certain models +// (e.g., CLIP) are split into vision and text components +const MODEL_FOR_IMAGE_FEATURE_EXTRACTION_MAPPING_NAMES = new Map([ + ['clip', ['CLIPVisionModelWithProjection', CLIPVisionModelWithProjection]], + ['siglip', ['SiglipVisionModel', SiglipVisionModel]], +]) + +const MODEL_CLASS_TYPE_MAPPING = [ + [MODEL_MAPPING_NAMES_ENCODER_ONLY, MODEL_TYPES.EncoderOnly], + [MODEL_MAPPING_NAMES_ENCODER_DECODER, MODEL_TYPES.EncoderDecoder], + [MODEL_MAPPING_NAMES_DECODER_ONLY, MODEL_TYPES.DecoderOnly], + [MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES, MODEL_TYPES.Seq2Seq], + [MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES, MODEL_TYPES.Seq2Seq], + [MODEL_FOR_CAUSAL_LM_MAPPING_NAMES, MODEL_TYPES.DecoderOnly], + [MODEL_FOR_MASKED_LM_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_VISION_2_SEQ_MAPPING_NAMES, MODEL_TYPES.Vision2Seq], + [MODEL_FOR_IMAGE_TEXT_TO_TEXT_MAPPING_NAMES, MODEL_TYPES.ImageTextToText], + [MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_UNIVERSAL_SEGMENTATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_IMAGE_MATTING_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_IMAGE_TO_IMAGE_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_DEPTH_ESTIMATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_NORMAL_ESTIMATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_MASK_GENERATION_MAPPING_NAMES, MODEL_TYPES.MaskGeneration], + [MODEL_FOR_CTC_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_TEXT_TO_SPECTROGRAM_MAPPING_NAMES, MODEL_TYPES.Seq2Seq], + [MODEL_FOR_TEXT_TO_WAVEFORM_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_AUDIO_XVECTOR_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + [MODEL_FOR_AUDIO_FRAME_CLASSIFICATION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], + + // Custom: + [MODEL_FOR_IMAGE_FEATURE_EXTRACTION_MAPPING_NAMES, MODEL_TYPES.EncoderOnly], +]; + +for (const [mappings, type] of MODEL_CLASS_TYPE_MAPPING) { + // @ts-ignore + for (const [name, model] of mappings.values()) { + MODEL_TYPE_MAPPING.set(name, type); + MODEL_CLASS_TO_NAME_MAPPING.set(model, name); + MODEL_NAME_TO_CLASS_MAPPING.set(name, model); + } +} + +const CUSTOM_MAPPING = [ + // OVERRIDE: + // TODO: Refactor to allow class to specify model + ['MusicgenForConditionalGeneration', MusicgenForConditionalGeneration, MODEL_TYPES.Musicgen], + + ['CLIPTextModelWithProjection', CLIPTextModelWithProjection, MODEL_TYPES.EncoderOnly], + ['SiglipTextModel', SiglipTextModel, MODEL_TYPES.EncoderOnly], + ['ClapTextModelWithProjection', ClapTextModelWithProjection, MODEL_TYPES.EncoderOnly], + ['ClapAudioModelWithProjection', ClapAudioModelWithProjection, MODEL_TYPES.EncoderOnly], +] +for (const [name, model, type] of CUSTOM_MAPPING) { + MODEL_TYPE_MAPPING.set(name, type); + MODEL_CLASS_TO_NAME_MAPPING.set(model, name); + MODEL_NAME_TO_CLASS_MAPPING.set(name, model); +} + + +/** + * Helper class which is used to instantiate pretrained models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModel.from_pretrained('Xenova/bert-base-uncased'); + */ +class AutoModel extends PretrainedMixin { + /** @type {Map[]} */ + // @ts-ignore + static MODEL_CLASS_MAPPINGS = MODEL_CLASS_TYPE_MAPPING.map(x => x[0]); + static BASE_IF_FAIL = true; +} + +/** + * Helper class which is used to instantiate pretrained sequence classification models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForSequenceClassification.from_pretrained('Xenova/distilbert-base-uncased-finetuned-sst-2-english'); + */ +class AutoModelForSequenceClassification extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained token classification models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForTokenClassification.from_pretrained('Xenova/distilbert-base-multilingual-cased-ner-hrl'); + */ +class AutoModelForTokenClassification extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained sequence-to-sequence models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForSeq2SeqLM.from_pretrained('Xenova/t5-small'); + */ +class AutoModelForSeq2SeqLM extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained sequence-to-sequence speech-to-text models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForSpeechSeq2Seq.from_pretrained('openai/whisper-tiny.en'); + */ +class AutoModelForSpeechSeq2Seq extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained sequence-to-sequence text-to-spectrogram models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForTextToSpectrogram.from_pretrained('microsoft/speecht5_tts'); + */ +class AutoModelForTextToSpectrogram extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_TEXT_TO_SPECTROGRAM_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained text-to-waveform models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForTextToSpectrogram.from_pretrained('facebook/mms-tts-eng'); + */ +class AutoModelForTextToWaveform extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_TEXT_TO_WAVEFORM_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained causal language models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForCausalLM.from_pretrained('Xenova/gpt2'); + */ +class AutoModelForCausalLM extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_CAUSAL_LM_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained masked language models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForMaskedLM.from_pretrained('Xenova/bert-base-uncased'); + */ +class AutoModelForMaskedLM extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_MASKED_LM_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained question answering models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForQuestionAnswering.from_pretrained('Xenova/distilbert-base-cased-distilled-squad'); + */ +class AutoModelForQuestionAnswering extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained vision-to-sequence models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForVision2Seq.from_pretrained('Xenova/vit-gpt2-image-captioning'); + */ +class AutoModelForVision2Seq extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_VISION_2_SEQ_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained image classification models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForImageClassification.from_pretrained('Xenova/vit-base-patch16-224'); + */ +class AutoModelForImageClassification extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained image segmentation models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForImageSegmentation.from_pretrained('Xenova/detr-resnet-50-panoptic'); + */ +class AutoModelForImageSegmentation extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained image segmentation models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForSemanticSegmentation.from_pretrained('nvidia/segformer-b3-finetuned-cityscapes-1024-1024'); + */ +class AutoModelForSemanticSegmentation extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained universal image segmentation models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForUniversalSegmentation.from_pretrained('hf-internal-testing/tiny-random-MaskFormerForInstanceSegmentation'); + */ +class AutoModelForUniversalSegmentation extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_UNIVERSAL_SEGMENTATION_MAPPING_NAMES]; +} + +/** + * Helper class which is used to instantiate pretrained object detection models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForObjectDetection.from_pretrained('Xenova/detr-resnet-50'); + */ +class AutoModelForObjectDetection extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES]; +} + +class AutoModelForZeroShotObjectDetection extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING_NAMES]; +} + + +/** + * Helper class which is used to instantiate pretrained mask generation models with the `from_pretrained` function. + * The chosen model class is determined by the type specified in the model config. + * + * @example + * let model = await AutoModelForMaskGeneration.from_pretrained('Xenova/sam-vit-base'); + */ +class AutoModelForMaskGeneration extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_MASK_GENERATION_MAPPING_NAMES]; +} + +class AutoModelForCTC extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_CTC_MAPPING_NAMES]; +} + +class AutoModelForAudioClassification extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES]; +} + +class AutoModelForXVector extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_AUDIO_XVECTOR_MAPPING_NAMES]; +} + +class AutoModelForAudioFrameClassification extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_AUDIO_FRAME_CLASSIFICATION_MAPPING_NAMES]; +} + +class AutoModelForDocumentQuestionAnswering extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING_NAMES]; +} + +class AutoModelForImageMatting extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_IMAGE_MATTING_MAPPING_NAMES]; +} + +class AutoModelForImageToImage extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_IMAGE_TO_IMAGE_MAPPING_NAMES]; +} + +class AutoModelForDepthEstimation extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_DEPTH_ESTIMATION_MAPPING_NAMES]; +} + +class AutoModelForNormalEstimation extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_NORMAL_ESTIMATION_MAPPING_NAMES]; +} + +class AutoModelForImageFeatureExtraction extends PretrainedMixin { + static MODEL_CLASS_MAPPINGS = [MODEL_FOR_IMAGE_FEATURE_EXTRACTION_MAPPING_NAMES]; +} + +////////////////////////////////////////////////// + +////////////////////////////////////////////////// +class Seq2SeqLMOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits The output logits of the model. + * @param {Tensor} output.past_key_values An tensor of key/value pairs that represent the previous state of the model. + * @param {Tensor} output.encoder_outputs The output of the encoder in a sequence-to-sequence model. + * @param {Tensor} [output.decoder_attentions] Attentions weights of the decoder, after the attention softmax, used to compute the weighted average in the self-attention heads. + * @param {Tensor} [output.cross_attentions] Attentions weights of the decoder's cross-attention layer, after the attention softmax, used to compute the weighted average in the cross-attention heads. + */ + constructor({ logits, past_key_values, encoder_outputs, decoder_attentions = null, cross_attentions = null }) { + super(); + this.logits = logits; + this.past_key_values = past_key_values; + this.encoder_outputs = encoder_outputs; + this.decoder_attentions = decoder_attentions; + this.cross_attentions = cross_attentions; + } +} + +/** + * Base class for outputs of sentence classification models. + */ +class SequenceClassifierOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits classification (or regression if config.num_labels==1) scores (before SoftMax). + */ + constructor({ logits }) { + super(); + this.logits = logits; + } +} + +/** + * Base class for outputs of XVector models. + */ +class XVectorOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits Classification hidden states before AMSoftmax, of shape `(batch_size, config.xvector_output_dim)`. + * @param {Tensor} output.embeddings Utterance embeddings used for vector similarity-based retrieval, of shape `(batch_size, config.xvector_output_dim)`. + */ + constructor({ logits, embeddings }) { + super(); + this.logits = logits; + this.embeddings = embeddings; + } +} + +/** + * Base class for outputs of token classification models. + */ +class TokenClassifierOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits Classification scores (before SoftMax). + */ + constructor({ logits }) { + super(); + this.logits = logits; + } +} + +/** + * Base class for masked language models outputs. + */ +class MaskedLMOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax). + */ + constructor({ logits }) { + super(); + this.logits = logits; + } +} + +/** + * Base class for outputs of question answering models. + */ +class QuestionAnsweringModelOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.start_logits Span-start scores (before SoftMax). + * @param {Tensor} output.end_logits Span-end scores (before SoftMax). + */ + constructor({ start_logits, end_logits }) { + super(); + this.start_logits = start_logits; + this.end_logits = end_logits; + } +} + + +/** + * Base class for causal language model (or autoregressive) outputs. + */ +class CausalLMOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits Prediction scores of the language modeling head (scores for each vocabulary token before softmax). + */ + constructor({ logits }) { + super(); + this.logits = logits; + } +} + +/** + * Base class for causal language model (or autoregressive) outputs. + */ +class CausalLMOutputWithPast extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.logits Prediction scores of the language modeling head (scores for each vocabulary token before softmax). + * @param {Tensor} output.past_key_values Contains pre-computed hidden-states (key and values in the self-attention blocks) + * that can be used (see `past_key_values` input) to speed up sequential decoding. + */ + constructor({ logits, past_key_values }) { + super(); + this.logits = logits; + this.past_key_values = past_key_values; + } +} + +class ImageMattingOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.alphas Estimated alpha values, of shape `(batch_size, num_channels, height, width)`. + */ + constructor({ alphas }) { + super(); + this.alphas = alphas; + } +} + +/** + * Describes the outputs for the VITS model. + */ +class VitsModelOutput extends ModelOutput { + /** + * @param {Object} output The output of the model. + * @param {Tensor} output.waveform The final audio waveform predicted by the model, of shape `(batch_size, sequence_length)`. + * @param {Tensor} output.spectrogram The log-mel spectrogram predicted at the output of the flow model. + * This spectrogram is passed to the Hi-Fi GAN decoder model to obtain the final audio waveform. + */ + constructor({ waveform, spectrogram }) { + super(); + this.waveform = waveform; + this.spectrogram = spectrogram; + } +} + + +/***/ }), + +/***/ "./src/models/whisper/common_whisper.js": +/*!**********************************************!*\ + !*** ./src/models/whisper/common_whisper.js ***! + \**********************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ WHISPER_LANGUAGE_MAPPING: () => (/* binding */ WHISPER_LANGUAGE_MAPPING), +/* harmony export */ WHISPER_TO_LANGUAGE_CODE_MAPPING: () => (/* binding */ WHISPER_TO_LANGUAGE_CODE_MAPPING), +/* harmony export */ whisper_language_to_code: () => (/* binding */ whisper_language_to_code) +/* harmony export */ }); + + +const WHISPER_LANGUAGES = [ + ["en", "english"], + ["zh", "chinese"], + ["de", "german"], + ["es", "spanish"], + ["ru", "russian"], + ["ko", "korean"], + ["fr", "french"], + ["ja", "japanese"], + ["pt", "portuguese"], + ["tr", "turkish"], + ["pl", "polish"], + ["ca", "catalan"], + ["nl", "dutch"], + ["ar", "arabic"], + ["sv", "swedish"], + ["it", "italian"], + ["id", "indonesian"], + ["hi", "hindi"], + ["fi", "finnish"], + ["vi", "vietnamese"], + ["he", "hebrew"], + ["uk", "ukrainian"], + ["el", "greek"], + ["ms", "malay"], + ["cs", "czech"], + ["ro", "romanian"], + ["da", "danish"], + ["hu", "hungarian"], + ["ta", "tamil"], + ["no", "norwegian"], + ["th", "thai"], + ["ur", "urdu"], + ["hr", "croatian"], + ["bg", "bulgarian"], + ["lt", "lithuanian"], + ["la", "latin"], + ["mi", "maori"], + ["ml", "malayalam"], + ["cy", "welsh"], + ["sk", "slovak"], + ["te", "telugu"], + ["fa", "persian"], + ["lv", "latvian"], + ["bn", "bengali"], + ["sr", "serbian"], + ["az", "azerbaijani"], + ["sl", "slovenian"], + ["kn", "kannada"], + ["et", "estonian"], + ["mk", "macedonian"], + ["br", "breton"], + ["eu", "basque"], + ["is", "icelandic"], + ["hy", "armenian"], + ["ne", "nepali"], + ["mn", "mongolian"], + ["bs", "bosnian"], + ["kk", "kazakh"], + ["sq", "albanian"], + ["sw", "swahili"], + ["gl", "galician"], + ["mr", "marathi"], + ["pa", "punjabi"], + ["si", "sinhala"], + ["km", "khmer"], + ["sn", "shona"], + ["yo", "yoruba"], + ["so", "somali"], + ["af", "afrikaans"], + ["oc", "occitan"], + ["ka", "georgian"], + ["be", "belarusian"], + ["tg", "tajik"], + ["sd", "sindhi"], + ["gu", "gujarati"], + ["am", "amharic"], + ["yi", "yiddish"], + ["lo", "lao"], + ["uz", "uzbek"], + ["fo", "faroese"], + ["ht", "haitian creole"], + ["ps", "pashto"], + ["tk", "turkmen"], + ["nn", "nynorsk"], + ["mt", "maltese"], + ["sa", "sanskrit"], + ["lb", "luxembourgish"], + ["my", "myanmar"], + ["bo", "tibetan"], + ["tl", "tagalog"], + ["mg", "malagasy"], + ["as", "assamese"], + ["tt", "tatar"], + ["haw", "hawaiian"], + ["ln", "lingala"], + ["ha", "hausa"], + ["ba", "bashkir"], + ["jw", "javanese"], + ["su", "sundanese"], +] + +// @ts-ignore +const WHISPER_LANGUAGE_MAPPING = new Map(WHISPER_LANGUAGES); +// @ts-ignore +const WHISPER_TO_LANGUAGE_CODE_MAPPING = new Map([ + ...WHISPER_LANGUAGES.map(([k, v]) => [v, k]), + ...[ + ["burmese", "my"], + ["valencian", "ca"], + ["flemish", "nl"], + ["haitian", "ht"], + ["letzeburgesch", "lb"], + ["pushto", "ps"], + ["panjabi", "pa"], + ["moldavian", "ro"], + ["moldovan", "ro"], + ["sinhalese", "si"], + ["castilian", "es"], + ] +]); + +/** + * @param {string} language The language name or code + * @returns {string} The language code + */ +function whisper_language_to_code(language) { + language = language.toLowerCase(); + + // Map to code from user-friendly name (e.g., "english" -> "en") + let language_code = WHISPER_TO_LANGUAGE_CODE_MAPPING.get(language); + + if (language_code === undefined) { + // User provided something that is not a language name + + if (WHISPER_LANGUAGE_MAPPING.has(language)) { + // User provided the language code directly (e.g., "en") + language_code = language; + + } else { + // User provided something that is not a language code or name + const is_language_code = language.length === 2; + const langs = is_language_code ? WHISPER_LANGUAGE_MAPPING.keys() : WHISPER_LANGUAGE_MAPPING.values(); + + throw new Error(`Language "${language}" is not supported. Must be one of: ${JSON.stringify(langs)}`); + } + } + return language_code; +} + + +/***/ }), + +/***/ "./src/models/whisper/generation_whisper.js": +/*!**************************************************!*\ + !*** ./src/models/whisper/generation_whisper.js ***! + \**************************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ WhisperGenerationConfig: () => (/* binding */ WhisperGenerationConfig) +/* harmony export */ }); +/* harmony import */ var _generation_configuration_utils_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../generation/configuration_utils.js */ "./src/generation/configuration_utils.js"); + + +class WhisperGenerationConfig extends _generation_configuration_utils_js__WEBPACK_IMPORTED_MODULE_0__.GenerationConfig { + + /** + * Whether to return the timestamps with the text. This enables the `WhisperTimestampsLogitsProcessor`. + * @type {boolean} + */ + return_timestamps = null; + + /** + * Whether to return token-level timestamps + * with the text. This can be used with or without the `return_timestamps` option. To get word-level + * timestamps, use the tokenizer to group the tokens into words. + * @type {boolean} + */ + return_token_timestamps = null; + + /** + * The number of audio frames available in this chunk. This is only used generating word-level timestamps. + * @type {number} + */ + num_frames = null; + + /** + * Alignment heads to predict word-level timestamps. This is a list of [layer, head] pairs that + * select the cross-attention heads that are highly correlated to word-level timing. + * @type {[number, number][]} + */ + alignment_heads = null; + + /** + * Task to use for generation, either "translate" or "transcribe". + * @type {string} + */ + task = null; + + /** + * Language token to use for generation, can be either in the form of `<|en|>`, `en` or `english`. + * You can find all the possible language tokens in the `model.generation_config.lang_to_id` dictionary. + * @type {string} + */ + language = null; + + /** + * The id of the `"<|notimestamps|>"` token. + * @type {number} + */ + no_timestamps_token_id = null; + + /** + * Rank-1 list of token IDs created by passing text to [`~WhisperProcessor.get_prompt_ids`] that is + * provided as a prompt to each chunk. This can be used to provide or "prompt-engineer" a context for + * transcription, e.g. custom vocabularies or proper nouns to make it more likely to predict those words + * correctly. It cannot be used in conjunction with `decoder_start_token_id` as it overwrites this value. + * @type {number[]} + */ + prompt_ids = null; + + /** + * Whether the model is multilingual or not. + * @type {boolean} + */ + is_multilingual = null; + + /** + * (Optional) A mapping from language tokens to their corresponding IDs. + * Only required if the model is multilingual. + * @type {Record|null} + */ + lang_to_id = null; + + /** + * (Optional) A mapping from task tokens to their corresponding IDs. + * @type {Record|null} + */ + task_to_id = null; + + /** + * Used to set the maximum value of the initial timestamp. This is used to prevent the model from + * predicting timestamps that are too far in the future. + * @type {number} + */ + max_initial_timestamp_index = 1; +} + +/** + * @typedef {import('../../generation/parameters.js').GenerationFunctionParameters & {generation_config: WhisperGenerationConfig} & WhisperGenerationConfig} WhisperGenerationFunctionParameters + */ + + +/***/ }), + +/***/ "./src/ops/registry.js": +/*!*****************************!*\ + !*** ./src/ops/registry.js ***! + \*****************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ TensorOpRegistry: () => (/* binding */ TensorOpRegistry) +/* harmony export */ }); +/* harmony import */ var _backends_onnx_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../backends/onnx.js */ "./src/backends/onnx.js"); +/* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../utils/tensor.js */ "./src/utils/tensor.js"); + + + +/** + * Asynchronously creates a wrapper function for running an ONNX inference session. + * + * @param {number[]} session_bytes The session data in bytes. + * @param {import('onnxruntime-common').InferenceSession.SessionOptions} session_options The options for the ONNX session. + * @template {string | [string] | string[]} T + * @param {T} names The name(s) of the output tensor(s). + * + * @returns {Promise): Promise>} + * The wrapper function for running the ONNX inference session. + */ +const wrap = async (session_bytes, session_options, names) => { + const session = await (0,_backends_onnx_js__WEBPACK_IMPORTED_MODULE_0__.createInferenceSession)( + new Uint8Array(session_bytes), session_options, + ); + return /** @type {any} */(async (/** @type {Record} */ inputs) => { + const ortFeed = Object.fromEntries(Object.entries(inputs).map(([k, v]) => [k, v.ort_tensor])); + const outputs = await session.run(ortFeed); + + if (Array.isArray(names)) { + return names.map((n) => new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor(outputs[n])); + } else { + return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_1__.Tensor(outputs[/** @type {string} */(names)]); + } + }) +} + +// In-memory registry of initialized ONNX operators +class TensorOpRegistry { + static session_options = { + // TODO: Allow for multiple execution providers + // executionProviders: ['webgpu'], + }; + + static get bilinear_interpolate_4d() { + if (!this._bilinear_interpolate_4d) { + this._bilinear_interpolate_4d = wrap( + [8, 9, 18, 0, 58, 128, 1, 10, 40, 10, 1, 120, 10, 0, 10, 0, 10, 1, 115, 18, 1, 121, 34, 6, 82, 101, 115, 105, 122, 101, 42, 17, 10, 4, 109, 111, 100, 101, 34, 6, 108, 105, 110, 101, 97, 114, 160, 1, 3, 18, 1, 114, 90, 31, 10, 1, 120, 18, 26, 10, 24, 8, 1, 18, 20, 10, 3, 18, 1, 98, 10, 3, 18, 1, 99, 10, 3, 18, 1, 104, 10, 3, 18, 1, 119, 90, 15, 10, 1, 115, 18, 10, 10, 8, 8, 7, 18, 4, 10, 2, 8, 4, 98, 31, 10, 1, 121, 18, 26, 10, 24, 8, 1, 18, 20, 10, 3, 18, 1, 98, 10, 3, 18, 1, 99, 10, 3, 18, 1, 104, 10, 3, 18, 1, 119, 66, 2, 16, 20], + this.session_options, + 'y', + ); + } + return this._bilinear_interpolate_4d; + } + + static get bicubic_interpolate_4d() { + if (!this._bicubic_interpolate_4d) { + this._bicubic_interpolate_4d = wrap( + [8, 9, 18, 0, 58, 127, 10, 39, 10, 1, 120, 10, 0, 10, 0, 10, 1, 115, 18, 1, 121, 34, 6, 82, 101, 115, 105, 122, 101, 42, 16, 10, 4, 109, 111, 100, 101, 34, 5, 99, 117, 98, 105, 99, 160, 1, 3, 18, 1, 114, 90, 31, 10, 1, 120, 18, 26, 10, 24, 8, 1, 18, 20, 10, 3, 18, 1, 98, 10, 3, 18, 1, 99, 10, 3, 18, 1, 104, 10, 3, 18, 1, 119, 90, 15, 10, 1, 115, 18, 10, 10, 8, 8, 7, 18, 4, 10, 2, 8, 4, 98, 31, 10, 1, 121, 18, 26, 10, 24, 8, 1, 18, 20, 10, 3, 18, 1, 98, 10, 3, 18, 1, 99, 10, 3, 18, 1, 104, 10, 3, 18, 1, 119, 66, 2, 16, 20], + this.session_options, + 'y', + ); + } + return this._bicubic_interpolate_4d; + } + + static get matmul() { + if (!this._matmul) { + this._matmul = wrap( + [8, 9, 18, 0, 58, 55, 10, 17, 10, 1, 97, 10, 1, 98, 18, 1, 99, 34, 6, 77, 97, 116, 77, 117, 108, 18, 1, 114, 90, 9, 10, 1, 97, 18, 4, 10, 2, 8, 1, 90, 9, 10, 1, 98, 18, 4, 10, 2, 8, 1, 98, 9, 10, 1, 99, 18, 4, 10, 2, 8, 1, 66, 2, 16, 20], + this.session_options, + 'c', + ); + } + return this._matmul; + } + + static get stft() { + if (!this._stft) { + this._stft = wrap( + [8, 7, 18, 0, 58, 148, 1, 10, 38, 10, 1, 115, 10, 1, 106, 10, 1, 119, 10, 1, 108, 18, 1, 111, 34, 4, 83, 84, 70, 84, 42, 15, 10, 8, 111, 110, 101, 115, 105, 100, 101, 100, 24, 1, 160, 1, 2, 18, 1, 115, 90, 26, 10, 1, 115, 18, 21, 10, 19, 8, 1, 18, 15, 10, 3, 18, 1, 98, 10, 3, 18, 1, 115, 10, 3, 18, 1, 99, 90, 11, 10, 1, 106, 18, 6, 10, 4, 8, 7, 18, 0, 90, 16, 10, 1, 119, 18, 11, 10, 9, 8, 1, 18, 5, 10, 3, 18, 1, 119, 90, 11, 10, 1, 108, 18, 6, 10, 4, 8, 7, 18, 0, 98, 31, 10, 1, 111, 18, 26, 10, 24, 8, 1, 18, 20, 10, 3, 18, 1, 98, 10, 3, 18, 1, 102, 10, 3, 18, 1, 100, 10, 3, 18, 1, 99, 66, 2, 16, 17], + this.session_options, + 'o', + ) + } + return this._stft; + } + + static get rfft() { + if (!this._rfft) { + this._rfft = wrap( + [8, 9, 18, 0, 58, 97, 10, 33, 10, 1, 120, 10, 0, 10, 1, 97, 18, 1, 121, 34, 3, 68, 70, 84, 42, 15, 10, 8, 111, 110, 101, 115, 105, 100, 101, 100, 24, 1, 160, 1, 2, 18, 1, 100, 90, 21, 10, 1, 120, 18, 16, 10, 14, 8, 1, 18, 10, 10, 3, 18, 1, 115, 10, 3, 18, 1, 99, 90, 11, 10, 1, 97, 18, 6, 10, 4, 8, 7, 18, 0, 98, 21, 10, 1, 121, 18, 16, 10, 14, 8, 1, 18, 10, 10, 3, 18, 1, 115, 10, 3, 18, 1, 99, 66, 2, 16, 20], + this.session_options, + 'y', + ) + } + return this._rfft; + } + + static get top_k() { + if (!this._top_k) { + this._top_k = wrap( + [8, 10, 18, 0, 58, 73, 10, 18, 10, 1, 120, 10, 1, 107, 18, 1, 118, 18, 1, 105, 34, 4, 84, 111, 112, 75, 18, 1, 116, 90, 9, 10, 1, 120, 18, 4, 10, 2, 8, 1, 90, 15, 10, 1, 107, 18, 10, 10, 8, 8, 7, 18, 4, 10, 2, 8, 1, 98, 9, 10, 1, 118, 18, 4, 10, 2, 8, 1, 98, 9, 10, 1, 105, 18, 4, 10, 2, 8, 7, 66, 2, 16, 21], + this.session_options, + [ /* Values */ 'v', /* Indices */ 'i'] + ) + } + return this._top_k; + } +} + + +/***/ }), + +/***/ "./src/pipelines.js": +/*!**************************!*\ + !*** ./src/pipelines.js ***! + \**************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ AudioClassificationPipeline: () => (/* binding */ AudioClassificationPipeline), +/* harmony export */ AutomaticSpeechRecognitionPipeline: () => (/* binding */ AutomaticSpeechRecognitionPipeline), +/* harmony export */ DepthEstimationPipeline: () => (/* binding */ DepthEstimationPipeline), +/* harmony export */ DocumentQuestionAnsweringPipeline: () => (/* binding */ DocumentQuestionAnsweringPipeline), +/* harmony export */ FeatureExtractionPipeline: () => (/* binding */ FeatureExtractionPipeline), +/* harmony export */ FillMaskPipeline: () => (/* binding */ FillMaskPipeline), +/* harmony export */ ImageClassificationPipeline: () => (/* binding */ ImageClassificationPipeline), +/* harmony export */ ImageFeatureExtractionPipeline: () => (/* binding */ ImageFeatureExtractionPipeline), +/* harmony export */ ImageSegmentationPipeline: () => (/* binding */ ImageSegmentationPipeline), +/* harmony export */ ImageToImagePipeline: () => (/* binding */ ImageToImagePipeline), +/* harmony export */ ImageToTextPipeline: () => (/* binding */ ImageToTextPipeline), +/* harmony export */ ObjectDetectionPipeline: () => (/* binding */ ObjectDetectionPipeline), +/* harmony export */ Pipeline: () => (/* binding */ Pipeline), +/* harmony export */ QuestionAnsweringPipeline: () => (/* binding */ QuestionAnsweringPipeline), +/* harmony export */ SummarizationPipeline: () => (/* binding */ SummarizationPipeline), +/* harmony export */ Text2TextGenerationPipeline: () => (/* binding */ Text2TextGenerationPipeline), +/* harmony export */ TextClassificationPipeline: () => (/* binding */ TextClassificationPipeline), +/* harmony export */ TextGenerationPipeline: () => (/* binding */ TextGenerationPipeline), +/* harmony export */ TextToAudioPipeline: () => (/* binding */ TextToAudioPipeline), +/* harmony export */ TokenClassificationPipeline: () => (/* binding */ TokenClassificationPipeline), +/* harmony export */ TranslationPipeline: () => (/* binding */ TranslationPipeline), +/* harmony export */ ZeroShotAudioClassificationPipeline: () => (/* binding */ ZeroShotAudioClassificationPipeline), +/* harmony export */ ZeroShotClassificationPipeline: () => (/* binding */ ZeroShotClassificationPipeline), +/* harmony export */ ZeroShotImageClassificationPipeline: () => (/* binding */ ZeroShotImageClassificationPipeline), +/* harmony export */ ZeroShotObjectDetectionPipeline: () => (/* binding */ ZeroShotObjectDetectionPipeline), +/* harmony export */ pipeline: () => (/* binding */ pipeline) +/* harmony export */ }); +/* harmony import */ var _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./tokenizers.js */ "./src/tokenizers.js"); +/* harmony import */ var _models_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./models.js */ "./src/models.js"); +/* harmony import */ var _processors_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./processors.js */ "./src/processors.js"); +/* harmony import */ var _utils_generic_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./utils/generic.js */ "./src/utils/generic.js"); +/* harmony import */ var _utils_core_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ./utils/core.js */ "./src/utils/core.js"); +/* harmony import */ var _utils_maths_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ./utils/maths.js */ "./src/utils/maths.js"); +/* harmony import */ var _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! ./utils/audio.js */ "./src/utils/audio.js"); +/* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__(/*! ./utils/tensor.js */ "./src/utils/tensor.js"); +/* harmony import */ var _utils_image_js__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__(/*! ./utils/image.js */ "./src/utils/image.js"); +/** + * @file Pipelines provide a high-level, easy to use, API for running machine learning models. + * + * **Example:** Instantiate pipeline using the `pipeline` function. + * ```javascript + * import { pipeline } from '@huggingface/transformers'; + * + * const classifier = await pipeline('sentiment-analysis'); + * const output = await classifier('I love transformers!'); + * // [{'label': 'POSITIVE', 'score': 0.999817686}] + * ``` + * + * @module pipelines + */ + + + + + + + + + + + + + + +/** + * @typedef {string | RawImage | URL} ImageInput + * @typedef {ImageInput|ImageInput[]} ImagePipelineInputs + */ + +/** + * Prepare images for further tasks. + * @param {ImagePipelineInputs} images images to prepare. + * @returns {Promise} returns processed images. + * @private + */ +async function prepareImages(images) { + if (!Array.isArray(images)) { + images = [images]; + } + + // Possibly convert any non-images to images + return await Promise.all(images.map(x => _utils_image_js__WEBPACK_IMPORTED_MODULE_8__.RawImage.read(x))); +} + +/** + * @typedef {string | URL | Float32Array | Float64Array} AudioInput + * @typedef {AudioInput|AudioInput[]} AudioPipelineInputs + */ + +/** + * Prepare audios for further tasks. + * @param {AudioPipelineInputs} audios audios to prepare. + * @param {number} sampling_rate sampling rate of the audios. + * @returns {Promise} The preprocessed audio data. + * @private + */ +async function prepareAudios(audios, sampling_rate) { + if (!Array.isArray(audios)) { + audios = [audios]; + } + + return await Promise.all(audios.map(x => { + if (typeof x === 'string' || x instanceof URL) { + return (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.read_audio)(x, sampling_rate); + } else if (x instanceof Float64Array) { + return new Float32Array(x); + } + return x; + })); +} + +/** + * @typedef {Object} BoundingBox + * @property {number} xmin The minimum x coordinate of the bounding box. + * @property {number} ymin The minimum y coordinate of the bounding box. + * @property {number} xmax The maximum x coordinate of the bounding box. + * @property {number} ymax The maximum y coordinate of the bounding box. + */ + +/** + * Helper function to convert list [xmin, xmax, ymin, ymax] into object { "xmin": xmin, ... } + * @param {number[]} box The bounding box as a list. + * @param {boolean} asInteger Whether to cast to integers. + * @returns {BoundingBox} The bounding box as an object. + * @private + */ +function get_bounding_box(box, asInteger) { + if (asInteger) { + box = box.map(x => x | 0); + } + const [xmin, ymin, xmax, ymax] = box; + + return { xmin, ymin, xmax, ymax }; +} + + +/** + * @callback DisposeType Disposes the item. + * @returns {Promise} A promise that resolves when the item has been disposed. + * + * @typedef {Object} Disposable + * @property {DisposeType} dispose A promise that resolves when the pipeline has been disposed. + */ + +/** + * The Pipeline class is the class from which all pipelines inherit. + * Refer to this class for methods shared across different pipelines. + * @extends Callable + */ +class Pipeline extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_3__.Callable { + /** + * Create a new Pipeline. + * @param {Object} options An object containing the following properties: + * @param {string} [options.task] The task of the pipeline. Useful for specifying subtasks. + * @param {PreTrainedModel} [options.model] The model used by the pipeline. + * @param {PreTrainedTokenizer} [options.tokenizer=null] The tokenizer used by the pipeline (if any). + * @param {Processor} [options.processor=null] The processor used by the pipeline (if any). + */ + constructor({ task, model, tokenizer = null, processor = null }) { + super(); + this.task = task; + this.model = model; + this.tokenizer = tokenizer; + this.processor = processor; + } + + /** @type {DisposeType} */ + async dispose() { + await this.model.dispose(); + } +} + +/** + * @typedef {Object} ModelTokenizerConstructorArgs + * @property {string} task The task of the pipeline. Useful for specifying subtasks. + * @property {PreTrainedModel} model The model used by the pipeline. + * @property {PreTrainedTokenizer} tokenizer The tokenizer used by the pipeline. + * + * @typedef {ModelTokenizerConstructorArgs} TextPipelineConstructorArgs An object used to instantiate a text-based pipeline. + */ + +/** + * @typedef {Object} ModelProcessorConstructorArgs + * @property {string} task The task of the pipeline. Useful for specifying subtasks. + * @property {PreTrainedModel} model The model used by the pipeline. + * @property {Processor} processor The processor used by the pipeline. + * + * @typedef {ModelProcessorConstructorArgs} AudioPipelineConstructorArgs An object used to instantiate an audio-based pipeline. + * @typedef {ModelProcessorConstructorArgs} ImagePipelineConstructorArgs An object used to instantiate an image-based pipeline. + */ + + +/** + * @typedef {Object} ModelTokenizerProcessorConstructorArgs + * @property {string} task The task of the pipeline. Useful for specifying subtasks. + * @property {PreTrainedModel} model The model used by the pipeline. + * @property {PreTrainedTokenizer} tokenizer The tokenizer used by the pipeline. + * @property {Processor} processor The processor used by the pipeline. + * + * @typedef {ModelTokenizerProcessorConstructorArgs} TextAudioPipelineConstructorArgs An object used to instantiate a text- and audio-based pipeline. + * @typedef {ModelTokenizerProcessorConstructorArgs} TextImagePipelineConstructorArgs An object used to instantiate a text- and image-based pipeline. + */ + +/** + * @typedef {Object} TextClassificationSingle + * @property {string} label The label predicted. + * @property {number} score The corresponding probability. + * @typedef {TextClassificationSingle[]} TextClassificationOutput + * + * @typedef {Object} TextClassificationPipelineOptions Parameters specific to text classification pipelines. + * @property {number} [top_k=1] The number of top predictions to be returned. + * + * @callback TextClassificationPipelineCallback Classify the text(s) given as inputs. + * @param {string|string[]} texts The input text(s) to be classified. + * @param {TextClassificationPipelineOptions} [options] The options to use for text classification. + * @returns {Promise} An array or object containing the predicted labels and scores. + * + * @typedef {TextPipelineConstructorArgs & TextClassificationPipelineCallback & Disposable} TextClassificationPipelineType + */ + +/** + * Text classification pipeline using any `ModelForSequenceClassification`. + * + * **Example:** Sentiment-analysis w/ `Xenova/distilbert-base-uncased-finetuned-sst-2-english`. + * ```javascript + * const classifier = await pipeline('sentiment-analysis', 'Xenova/distilbert-base-uncased-finetuned-sst-2-english'); + * const output = await classifier('I love transformers!'); + * // [{ label: 'POSITIVE', score: 0.999788761138916 }] + * ``` + * + * **Example:** Multilingual sentiment-analysis w/ `Xenova/bert-base-multilingual-uncased-sentiment` (and return top 5 classes). + * ```javascript + * const classifier = await pipeline('sentiment-analysis', 'Xenova/bert-base-multilingual-uncased-sentiment'); + * const output = await classifier('Le meilleur film de tous les temps.', { top_k: 5 }); + * // [ + * // { label: '5 stars', score: 0.9610759615898132 }, + * // { label: '4 stars', score: 0.03323351591825485 }, + * // { label: '3 stars', score: 0.0036155181005597115 }, + * // { label: '1 star', score: 0.0011325967498123646 }, + * // { label: '2 stars', score: 0.0009423971059732139 } + * // ] + * ``` + * + * **Example:** Toxic comment classification w/ `Xenova/toxic-bert` (and return all classes). + * ```javascript + * const classifier = await pipeline('text-classification', 'Xenova/toxic-bert'); + * const output = await classifier('I hate you!', { top_k: null }); + * // [ + * // { label: 'toxic', score: 0.9593140482902527 }, + * // { label: 'insult', score: 0.16187334060668945 }, + * // { label: 'obscene', score: 0.03452680632472038 }, + * // { label: 'identity_hate', score: 0.0223250575363636 }, + * // { label: 'threat', score: 0.019197041168808937 }, + * // { label: 'severe_toxic', score: 0.005651099607348442 } + * // ] + * ``` + */ +class TextClassificationPipeline extends (/** @type {new (options: TextPipelineConstructorArgs) => TextClassificationPipelineType} */ (Pipeline)) { + + /** + * Create a new TextClassificationPipeline. + * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {TextClassificationPipelineCallback} */ + async _call(texts, { + top_k = 1 + } = {}) { + + // Run tokenization + const model_inputs = this.tokenizer(texts, { + padding: true, + truncation: true, + }); + + // Run model + const outputs = await this.model(model_inputs) + + // TODO: Use softmax tensor function + const function_to_apply = + this.model.config.problem_type === 'multi_label_classification' + ? batch => batch.sigmoid() + : batch => new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.Tensor( + 'float32', + (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(batch.data), + batch.dims, + ); // single_label_classification (default) + + const id2label = this.model.config.id2label; + + const toReturn = []; + for (const batch of outputs.logits) { + const output = function_to_apply(batch); + + const scores = await (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.topk)(output, top_k); + + const values = scores[0].tolist(); + const indices = scores[1].tolist(); + const vals = indices.map((x, i) => ({ + label: id2label ? id2label[x] : `LABEL_${x}`, + score: values[i], + })); + if (top_k === 1) { + toReturn.push(...vals); + } else { + toReturn.push(vals); + } + } + + return Array.isArray(texts) || top_k === 1 ? /** @type {TextClassificationOutput} */ (toReturn) : /** @type {TextClassificationOutput[]} */ (toReturn)[0]; + } +} + +/** + * @typedef {Object} TokenClassificationSingle + * @property {string} word The token/word classified. This is obtained by decoding the selected tokens. + * @property {number} score The corresponding probability for `entity`. + * @property {string} entity The entity predicted for that token/word. + * @property {number} index The index of the corresponding token in the sentence. + * @property {number} [start] The index of the start of the corresponding entity in the sentence. + * @property {number} [end] The index of the end of the corresponding entity in the sentence. + * @typedef {TokenClassificationSingle[]} TokenClassificationOutput + * + * @typedef {Object} TokenClassificationPipelineOptions Parameters specific to token classification pipelines. + * @property {string[]} [ignore_labels] A list of labels to ignore. + * + * @callback TokenClassificationPipelineCallback Classify each token of the text(s) given as inputs. + * @param {string|string[]} texts One or several texts (or one list of texts) for token classification. + * @param {TokenClassificationPipelineOptions} [options] The options to use for token classification. + * @returns {Promise} The result. + * + * @typedef {TextPipelineConstructorArgs & TokenClassificationPipelineCallback & Disposable} TokenClassificationPipelineType + */ + +/** + * Named Entity Recognition pipeline using any `ModelForTokenClassification`. + * + * **Example:** Perform named entity recognition with `Xenova/bert-base-NER`. + * ```javascript + * const classifier = await pipeline('token-classification', 'Xenova/bert-base-NER'); + * const output = await classifier('My name is Sarah and I live in London'); + * // [ + * // { entity: 'B-PER', score: 0.9980202913284302, index: 4, word: 'Sarah' }, + * // { entity: 'B-LOC', score: 0.9994474053382874, index: 9, word: 'London' } + * // ] + * ``` + * + * **Example:** Perform named entity recognition with `Xenova/bert-base-NER` (and return all labels). + * ```javascript + * const classifier = await pipeline('token-classification', 'Xenova/bert-base-NER'); + * const output = await classifier('Sarah lives in the United States of America', { ignore_labels: [] }); + * // [ + * // { entity: 'B-PER', score: 0.9966587424278259, index: 1, word: 'Sarah' }, + * // { entity: 'O', score: 0.9987385869026184, index: 2, word: 'lives' }, + * // { entity: 'O', score: 0.9990072846412659, index: 3, word: 'in' }, + * // { entity: 'O', score: 0.9988298416137695, index: 4, word: 'the' }, + * // { entity: 'B-LOC', score: 0.9995510578155518, index: 5, word: 'United' }, + * // { entity: 'I-LOC', score: 0.9990395307540894, index: 6, word: 'States' }, + * // { entity: 'I-LOC', score: 0.9986724853515625, index: 7, word: 'of' }, + * // { entity: 'I-LOC', score: 0.9975294470787048, index: 8, word: 'America' } + * // ] + * ``` + */ +class TokenClassificationPipeline extends (/** @type {new (options: TextPipelineConstructorArgs) => TokenClassificationPipelineType} */ (Pipeline)) { + + /** + * Create a new TokenClassificationPipeline. + * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {TokenClassificationPipelineCallback} */ + async _call(texts, { + ignore_labels = ['O'], + } = {}) { + + const isBatched = Array.isArray(texts); + + // Run tokenization + const model_inputs = this.tokenizer(isBatched ? texts : [texts], { + padding: true, + truncation: true, + }); + + // Run model + const outputs = await this.model(model_inputs) + + const logits = outputs.logits; + const id2label = this.model.config.id2label; + + const toReturn = []; + for (let i = 0; i < logits.dims[0]; ++i) { + const ids = model_inputs.input_ids[i]; + const batch = logits[i]; + + // List of tokens that aren't ignored + const tokens = []; + for (let j = 0; j < batch.dims[0]; ++j) { + const tokenData = batch[j]; + const topScoreIndex = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.max)(tokenData.data)[1]; + + const entity = id2label ? id2label[topScoreIndex] : `LABEL_${topScoreIndex}`; + if (ignore_labels.includes(entity)) { + // We predicted a token that should be ignored. So, we skip it. + continue; + } + + // TODO add option to keep special tokens? + const word = this.tokenizer.decode([ids[j].item()], { skip_special_tokens: true }); + if (word === '') { + // Was a special token. So, we skip it. + continue; + } + + const scores = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(tokenData.data); + + tokens.push({ + entity: entity, + score: scores[topScoreIndex], + index: j, + word: word, + + // TODO: Add support for start and end + // start: null, + // end: null, + }); + } + toReturn.push(tokens); + } + return isBatched ? toReturn : toReturn[0]; + } +} + +/** + * @typedef {Object} QuestionAnsweringOutput + * @property {number} score The probability associated to the answer. + * @property {number} [start] The character start index of the answer (in the tokenized version of the input). + * @property {number} [end] The character end index of the answer (in the tokenized version of the input). + * @property {string} answer The answer to the question. + * + * @typedef {Object} QuestionAnsweringPipelineOptions Parameters specific to question answering pipelines. + * @property {number} [top_k=1] The number of top answer predictions to be returned. + * + * @callback QuestionAnsweringPipelineCallback Answer the question(s) given as inputs by using the context(s). + * @param {string|string[]} question One or several question(s) (must be used in conjunction with the `context` argument). + * @param {string|string[]} context One or several context(s) associated with the question(s) (must be used in conjunction with the `question` argument). + * @param {QuestionAnsweringPipelineOptions} [options] The options to use for question answering. + * @returns {Promise} An array or object containing the predicted answers and scores. + * + * @typedef {TextPipelineConstructorArgs & QuestionAnsweringPipelineCallback & Disposable} QuestionAnsweringPipelineType + */ + +/** + * Question Answering pipeline using any `ModelForQuestionAnswering`. + * + * **Example:** Run question answering with `Xenova/distilbert-base-uncased-distilled-squad`. + * ```javascript + * const answerer = await pipeline('question-answering', 'Xenova/distilbert-base-uncased-distilled-squad'); + * const question = 'Who was Jim Henson?'; + * const context = 'Jim Henson was a nice puppet.'; + * const output = await answerer(question, context); + * // { + * // answer: "a nice puppet", + * // score: 0.5768911502526741 + * // } + * ``` + */ +class QuestionAnsweringPipeline extends (/** @type {new (options: TextPipelineConstructorArgs) => QuestionAnsweringPipelineType} */ (Pipeline)) { + + /** + * Create a new QuestionAnsweringPipeline. + * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {QuestionAnsweringPipelineCallback} */ + async _call(question, context, { + top_k = 1 + } = {}) { + + // Run tokenization + const inputs = this.tokenizer(question, { + text_pair: context, + padding: true, + truncation: true, + }); + + const { start_logits, end_logits } = await this.model(inputs); + const input_ids = inputs.input_ids.tolist(); + const attention_mask = inputs.attention_mask.tolist(); + + // TODO: add support for `return_special_tokens_mask` + const special_tokens = this.tokenizer.all_special_ids; + + /** @type {QuestionAnsweringOutput[]} */ + const toReturn = []; + for (let j = 0; j < start_logits.dims[0]; ++j) { + const ids = input_ids[j]; + const sepIndex = ids.findIndex(x => + // We use == to match bigint with number + // @ts-ignore + x == this.tokenizer.sep_token_id + ); + + + const valid_mask = attention_mask[j].map((y, ix) => ( + y == 1 + && ( + ix === 0 // is cls_token + || ( + ix > sepIndex + && special_tokens.findIndex(x => x == ids[ix]) === -1 // token is not a special token (special_tokens_mask == 0) + ) + ) + )); + + const start = start_logits[j].tolist(); + const end = end_logits[j].tolist(); + + // Now, we mask out values that can't be in the answer + // NOTE: We keep the cls_token unmasked (some models use it to indicate unanswerable questions) + for (let i = 1; i < start.length; ++i) { + if ( + attention_mask[j] == 0 // is part of padding + || i <= sepIndex // is before the sep_token + || special_tokens.findIndex(x => x == ids[i]) !== -1 // Is a special token + ) { + // Make sure non-context indexes in the tensor cannot contribute to the softmax + start[i] = -Infinity; + end[i] = -Infinity; + } + } + + // Normalize logits and spans to retrieve the answer + const start_scores = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(start).map((x, i) => [x, i]); + const end_scores = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(end).map((x, i) => [x, i]); + + // Mask CLS + start_scores[0][0] = 0; + end_scores[0][0] = 0; + + // Generate all valid spans and select best ones + const options = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.product)(start_scores, end_scores) + .filter(x => x[0][1] <= x[1][1]) + .map(x => [x[0][1], x[1][1], x[0][0] * x[1][0]]) + .sort((a, b) => b[2] - a[2]); + + for (let k = 0; k < Math.min(options.length, top_k); ++k) { + const [start, end, score] = options[k]; + + const answer_tokens = ids.slice(start, end + 1) + + const answer = this.tokenizer.decode(answer_tokens, { + skip_special_tokens: true, + }); + + // TODO add start and end? + // NOTE: HF returns character index + toReturn.push({ + answer, score + }); + } + } + + // Mimic HF's return type based on top_k + return (top_k === 1) ? toReturn[0] : toReturn; + } +} + + +/** + * @typedef {Object} FillMaskSingle + * @property {string} sequence The corresponding input with the mask token prediction. + * @property {number} score The corresponding probability. + * @property {number} token The predicted token id (to replace the masked one). + * @property {string} token_str The predicted token (to replace the masked one). + * @typedef {FillMaskSingle[]} FillMaskOutput + * + * @typedef {Object} FillMaskPipelineOptions Parameters specific to fill mask pipelines. + * @property {number} [top_k=5] When passed, overrides the number of predictions to return. + * + * @callback FillMaskPipelineCallback Fill the masked token in the text(s) given as inputs. + * @param {string|string[]} texts One or several texts (or one list of prompts) with masked tokens. + * @param {FillMaskPipelineOptions} [options] The options to use for masked language modelling. + * @returns {Promise} An array of objects containing the score, predicted token, predicted token string, + * and the sequence with the predicted token filled in, or an array of such arrays (one for each input text). + * If only one input text is given, the output will be an array of objects. + * @throws {Error} When the mask token is not found in the input text. + * + * @typedef {TextPipelineConstructorArgs & FillMaskPipelineCallback & Disposable} FillMaskPipelineType + */ + +/** + * Masked language modeling prediction pipeline using any `ModelWithLMHead`. + * + * **Example:** Perform masked language modelling (a.k.a. "fill-mask") with `Xenova/bert-base-uncased`. + * ```javascript + * const unmasker = await pipeline('fill-mask', 'Xenova/bert-base-cased'); + * const output = await unmasker('The goal of life is [MASK].'); + * // [ + * // { token_str: 'survival', score: 0.06137419492006302, token: 8115, sequence: 'The goal of life is survival.' }, + * // { token_str: 'love', score: 0.03902450203895569, token: 1567, sequence: 'The goal of life is love.' }, + * // { token_str: 'happiness', score: 0.03253183513879776, token: 9266, sequence: 'The goal of life is happiness.' }, + * // { token_str: 'freedom', score: 0.018736306577920914, token: 4438, sequence: 'The goal of life is freedom.' }, + * // { token_str: 'life', score: 0.01859794743359089, token: 1297, sequence: 'The goal of life is life.' } + * // ] + * ``` + * + * **Example:** Perform masked language modelling (a.k.a. "fill-mask") with `Xenova/bert-base-cased` (and return top result). + * ```javascript + * const unmasker = await pipeline('fill-mask', 'Xenova/bert-base-cased'); + * const output = await unmasker('The Milky Way is a [MASK] galaxy.', { top_k: 1 }); + * // [{ token_str: 'spiral', score: 0.6299987435340881, token: 14061, sequence: 'The Milky Way is a spiral galaxy.' }] + * ``` + */ +class FillMaskPipeline extends (/** @type {new (options: TextPipelineConstructorArgs) => FillMaskPipelineType} */ (Pipeline)) { + + /** + * Create a new FillMaskPipeline. + * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {FillMaskPipelineCallback} */ + async _call(texts, { + top_k = 5 + } = {}) { + + // Run tokenization + const model_inputs = this.tokenizer(texts, { + padding: true, + truncation: true, + }); + + // Run model + const { logits } = await this.model(model_inputs) + + const toReturn = []; + + /** @type {bigint[][]} */ + const input_ids = model_inputs.input_ids.tolist(); + for (let i = 0; i < input_ids.length; ++i) { + const ids = input_ids[i]; + const mask_token_index = ids.findIndex(x => + // We use == to match bigint with number + // @ts-ignore + x == this.tokenizer.mask_token_id + ); + if (mask_token_index === -1) { + throw Error(`Mask token (${this.tokenizer.mask_token}) not found in text.`) + } + const itemLogits = logits[i][mask_token_index]; + + const scores = await (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.topk)(new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.Tensor( + 'float32', + (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(itemLogits.data), + itemLogits.dims, + ), top_k); + const values = scores[0].tolist(); + const indices = scores[1].tolist(); + + toReturn.push(indices.map((x, i) => { + const sequence = ids.slice(); + sequence[mask_token_index] = x; + + return { + score: values[i], + token: Number(x), + token_str: this.tokenizer.model.vocab[x], + sequence: this.tokenizer.decode(sequence, { skip_special_tokens: true }), + } + })); + } + return Array.isArray(texts) ? toReturn : toReturn[0]; + } +} + + +/** + * @typedef {Object} Text2TextGenerationSingle + * @property {string} generated_text The generated text. + * @typedef {Text2TextGenerationSingle[]} Text2TextGenerationOutput + * + * @callback Text2TextGenerationPipelineCallback Generate the output text(s) using text(s) given as inputs. + * @param {string|string[]} texts Input text for the encoder. + * @param {Partial} [options] Additional keyword arguments to pass along to the generate method of the model. + * @returns {Promise} + * + * @typedef {TextPipelineConstructorArgs & Text2TextGenerationPipelineCallback & Disposable} Text2TextGenerationPipelineType + */ + +/** + * Text2TextGenerationPipeline class for generating text using a model that performs text-to-text generation tasks. + * + * **Example:** Text-to-text generation w/ `Xenova/LaMini-Flan-T5-783M`. + * ```javascript + * const generator = await pipeline('text2text-generation', 'Xenova/LaMini-Flan-T5-783M'); + * const output = await generator('how can I become more healthy?', { + * max_new_tokens: 100, + * }); + * // [{ generated_text: "To become more healthy, you can: 1. Eat a balanced diet with plenty of fruits, vegetables, whole grains, lean proteins, and healthy fats. 2. Stay hydrated by drinking plenty of water. 3. Get enough sleep and manage stress levels. 4. Avoid smoking and excessive alcohol consumption. 5. Regularly exercise and maintain a healthy weight. 6. Practice good hygiene and sanitation. 7. Seek medical attention if you experience any health issues." }] + * ``` + */ +class Text2TextGenerationPipeline extends (/** @type {new (options: TextPipelineConstructorArgs) => Text2TextGenerationPipelineType} */ (Pipeline)) { + /** @type {'generated_text'} */ + _key = 'generated_text'; + + /** + * Create a new Text2TextGenerationPipeline. + * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {Text2TextGenerationPipelineCallback} */ + async _call(texts, generate_kwargs = {}) { + if (!Array.isArray(texts)) { + texts = [texts]; + } + + + // Add global prefix, if present + if (this.model.config.prefix) { + texts = texts.map(x => this.model.config.prefix + x) + } + + // Handle task specific params: + const task_specific_params = this.model.config.task_specific_params + if (task_specific_params && task_specific_params[this.task]) { + // Add prefixes, if present + if (task_specific_params[this.task].prefix) { + texts = texts.map(x => task_specific_params[this.task].prefix + x) + } + + // TODO update generation config + } + + const tokenizer = this.tokenizer; + const tokenizer_options = { + padding: true, + truncation: true, + } + let inputs; + if (this instanceof TranslationPipeline && '_build_translation_inputs' in tokenizer) { + // TODO: move to Translation pipeline? + // Currently put here to avoid code duplication + // @ts-ignore + inputs = tokenizer._build_translation_inputs(texts, tokenizer_options, generate_kwargs); + + } else { + inputs = tokenizer(texts, tokenizer_options); + } + + const outputTokenIds = await this.model.generate({ ...inputs, ...generate_kwargs }); + return tokenizer.batch_decode(/** @type {Tensor} */(outputTokenIds), { + skip_special_tokens: true, + }).map(text => ({ [this._key]: text })); + } +} + + +/** + * @typedef {Object} SummarizationSingle + * @property {string} summary_text The summary text. + * @typedef {SummarizationSingle[]} SummarizationOutput + * + * @callback SummarizationPipelineCallback Summarize the text(s) given as inputs. + * @param {string|string[]} texts One or several articles (or one list of articles) to summarize. + * @param {import('./generation/configuration_utils.js').GenerationConfig} [options] Additional keyword arguments to pass along to the generate method of the model. + * @returns {Promise} + * + * @typedef {TextPipelineConstructorArgs & SummarizationPipelineCallback & Disposable} SummarizationPipelineType + */ + +/** + * A pipeline for summarization tasks, inheriting from Text2TextGenerationPipeline. + * + * **Example:** Summarization w/ `Xenova/distilbart-cnn-6-6`. + * ```javascript + * const generator = await pipeline('summarization', 'Xenova/distilbart-cnn-6-6'); + * const text = 'The tower is 324 metres (1,063 ft) tall, about the same height as an 81-storey building, ' + + * 'and the tallest structure in Paris. Its base is square, measuring 125 metres (410 ft) on each side. ' + + * 'During its construction, the Eiffel Tower surpassed the Washington Monument to become the tallest ' + + * 'man-made structure in the world, a title it held for 41 years until the Chrysler Building in New ' + + * 'York City was finished in 1930. It was the first structure to reach a height of 300 metres. Due to ' + + * 'the addition of a broadcasting aerial at the top of the tower in 1957, it is now taller than the ' + + * 'Chrysler Building by 5.2 metres (17 ft). Excluding transmitters, the Eiffel Tower is the second ' + + * 'tallest free-standing structure in France after the Millau Viaduct.'; + * const output = await generator(text, { + * max_new_tokens: 100, + * }); + * // [{ summary_text: ' The Eiffel Tower is about the same height as an 81-storey building and the tallest structure in Paris. It is the second tallest free-standing structure in France after the Millau Viaduct.' }] + * ``` + */ +class SummarizationPipeline extends (/** @type {new (options: TextPipelineConstructorArgs) => SummarizationPipelineType} */ (/** @type {any} */ (Text2TextGenerationPipeline))) { + /** @type {'summary_text'} */ + _key = 'summary_text'; + + /** + * Create a new SummarizationPipeline. + * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } +} + + +/** + * @typedef {Object} TranslationSingle + * @property {string} translation_text The translated text. + * @typedef {TranslationSingle[]} TranslationOutput + * + * @callback TranslationPipelineCallback Translate the text(s) given as inputs. + * @param {string|string[]} texts Texts to be translated. + * @param {import('./generation/configuration_utils.js').GenerationConfig} [options] Additional keyword arguments to pass along to the generate method of the model. + * @returns {Promise} + * + * @typedef {TextPipelineConstructorArgs & TranslationPipelineCallback & Disposable} TranslationPipelineType + */ + +/** + * Translates text from one language to another. + * + * **Example:** Multilingual translation w/ `Xenova/nllb-200-distilled-600M`. + * + * See [here](https://github.com/facebookresearch/flores/blob/main/flores200/README.md#languages-in-flores-200) + * for the full list of languages and their corresponding codes. + * + * ```javascript + * const translator = await pipeline('translation', 'Xenova/nllb-200-distilled-600M'); + * const output = await translator('जीवन एक चॉकलेट बॉक्स की तरह है।', { + * src_lang: 'hin_Deva', // Hindi + * tgt_lang: 'fra_Latn', // French + * }); + * // [{ translation_text: 'La vie est comme une boîte à chocolat.' }] + * ``` + * + * **Example:** Multilingual translation w/ `Xenova/m2m100_418M`. + * + * See [here](https://huggingface.co/facebook/m2m100_418M#languages-covered) + * for the full list of languages and their corresponding codes. + * + * ```javascript + * const translator = await pipeline('translation', 'Xenova/m2m100_418M'); + * const output = await translator('生活就像一盒巧克力。', { + * src_lang: 'zh', // Chinese + * tgt_lang: 'en', // English + * }); + * // [{ translation_text: 'Life is like a box of chocolate.' }] + * ``` + * + * **Example:** Multilingual translation w/ `Xenova/mbart-large-50-many-to-many-mmt`. + * + * See [here](https://huggingface.co/facebook/mbart-large-50-many-to-many-mmt#languages-covered) + * for the full list of languages and their corresponding codes. + * + * ```javascript + * const translator = await pipeline('translation', 'Xenova/mbart-large-50-many-to-many-mmt'); + * const output = await translator('संयुक्त राष्ट्र के प्रमुख का कहना है कि सीरिया में कोई सैन्य समाधान नहीं है', { + * src_lang: 'hi_IN', // Hindi + * tgt_lang: 'fr_XX', // French + * }); + * // [{ translation_text: 'Le chef des Nations affirme qu 'il n 'y a military solution in Syria.' }] + * ``` + */ +class TranslationPipeline extends (/** @type {new (options: TextPipelineConstructorArgs) => TranslationPipelineType} */ (/** @type {any} */ (Text2TextGenerationPipeline))) { + /** @type {'translation_text'} */ + _key = 'translation_text'; + + /** + * Create a new TranslationPipeline. + * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } +} + +function isChat(x) { + return Array.isArray(x) && x.every(x => 'role' in x && 'content' in x); +} + +/** + * @typedef {import('./tokenizers.js').Message[]} Chat + * + * @typedef {Object} TextGenerationSingle + * @property {string|Chat} generated_text The generated text. + * @typedef {TextGenerationSingle[]} TextGenerationOutput + * + * @typedef {Object} TextGenerationSpecificParams Parameters specific to text-generation pipelines. + * @property {boolean} [add_special_tokens] Whether or not to add special tokens when tokenizing the sequences. + * @property {boolean} [return_full_text=true] If set to `false` only added text is returned, otherwise the full text is returned. + * @typedef {import('./generation/configuration_utils.js').GenerationConfig & TextGenerationSpecificParams} TextGenerationConfig + * + * @callback TextGenerationPipelineCallback Complete the prompt(s) given as inputs. + * @param {string|string[]|Chat|Chat[]} texts One or several prompts (or one list of prompts) to complete. + * @param {Partial} [options] Additional keyword arguments to pass along to the generate method of the model. + * @returns {Promise} An array or object containing the generated texts. + * + * @typedef {TextPipelineConstructorArgs & TextGenerationPipelineCallback & Disposable} TextGenerationPipelineType + */ + +/** + * Language generation pipeline using any `ModelWithLMHead` or `ModelForCausalLM`. + * This pipeline predicts the words that will follow a specified text prompt. + * NOTE: For the full list of generation parameters, see [`GenerationConfig`](./utils/generation#module_utils/generation.GenerationConfig). + * + * **Example:** Text generation with `Xenova/distilgpt2` (default settings). + * ```javascript + * const generator = await pipeline('text-generation', 'Xenova/distilgpt2'); + * const text = 'I enjoy walking with my cute dog,'; + * const output = await generator(text); + * // [{ generated_text: "I enjoy walking with my cute dog, and I love to play with the other dogs." }] + * ``` + * + * **Example:** Text generation with `Xenova/distilgpt2` (custom settings). + * ```javascript + * const generator = await pipeline('text-generation', 'Xenova/distilgpt2'); + * const text = 'Once upon a time, there was'; + * const output = await generator(text, { + * temperature: 2, + * max_new_tokens: 10, + * repetition_penalty: 1.5, + * no_repeat_ngram_size: 2, + * num_beams: 2, + * num_return_sequences: 2, + * }); + * // [{ + * // "generated_text": "Once upon a time, there was an abundance of information about the history and activities that" + * // }, { + * // "generated_text": "Once upon a time, there was an abundance of information about the most important and influential" + * // }] + * ``` + * + * **Example:** Run code generation with `Xenova/codegen-350M-mono`. + * ```javascript + * const generator = await pipeline('text-generation', 'Xenova/codegen-350M-mono'); + * const text = 'def fib(n):'; + * const output = await generator(text, { + * max_new_tokens: 44, + * }); + * // [{ + * // generated_text: 'def fib(n):\n' + + * // ' if n == 0:\n' + + * // ' return 0\n' + + * // ' elif n == 1:\n' + + * // ' return 1\n' + + * // ' else:\n' + + * // ' return fib(n-1) + fib(n-2)\n' + * // }] + * ``` + */ +class TextGenerationPipeline extends (/** @type {new (options: TextPipelineConstructorArgs) => TextGenerationPipelineType} */ (Pipeline)) { + + /** + * Create a new TextGenerationPipeline. + * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {TextGenerationPipelineCallback} */ + async _call(texts, generate_kwargs = {}) { + let isBatched = false; + let isChatInput = false; + + // Normalize inputs + /** @type {string[]} */ + let inputs; + if (typeof texts === 'string') { + inputs = texts = [texts]; + } else if (Array.isArray(texts) && texts.every(x => typeof x === 'string')) { + isBatched = true; + inputs = /** @type {string[]} */(texts); + } else { + if (isChat(texts)) { + texts = [/** @type {Chat} */(texts)]; + } else if (Array.isArray(texts) && texts.every(isChat)) { + isBatched = true; + } else { + throw new Error('Input must be a string, an array of strings, a Chat, or an array of Chats'); + } + isChatInput = true; + + // If the input is a chat, we need to apply the chat template + inputs = /** @type {string[]} */(/** @type {Chat[]} */ (texts).map( + x => this.tokenizer.apply_chat_template(x, { + tokenize: false, + add_generation_prompt: true, + }) + )); + } + + // By default, do not add special tokens + const add_special_tokens = generate_kwargs.add_special_tokens ?? false; + + // By default, return full text + const return_full_text = isChatInput + ? false + : generate_kwargs.return_full_text ?? true; + + this.tokenizer.padding_side = 'left'; + const text_inputs = this.tokenizer(inputs, { + add_special_tokens, + padding: true, + truncation: true, + }); + + const outputTokenIds = /** @type {Tensor} */(await this.model.generate({ + ...text_inputs, + ...generate_kwargs + })); + + const decoded = this.tokenizer.batch_decode(outputTokenIds, { + skip_special_tokens: true, + }); + + let promptLengths; + if (!return_full_text && text_inputs.input_ids.dims.at(-1) > 0) { + promptLengths = this.tokenizer.batch_decode(text_inputs.input_ids, { + skip_special_tokens: true, + }).map(x => x.length); + } + + /** @type {TextGenerationOutput[]} */ + const toReturn = Array.from({ length: texts.length }, _ => []); + for (let i = 0; i < decoded.length; ++i) { + const textIndex = Math.floor(i / outputTokenIds.dims[0] * texts.length); + + if (promptLengths) { + // Trim the decoded text to only include the generated part + decoded[i] = decoded[i].slice(promptLengths[textIndex]); + } + toReturn[textIndex].push({ + generated_text: isChatInput + ? [ + ...((/** @type {Chat[]} */(texts)[textIndex])), + { role: 'assistant', content: decoded[i] }, + ] + : decoded[i] + }); + } + return (!isBatched && toReturn.length === 1) ? toReturn[0] : toReturn; + } +} + +/** + * @typedef {Object} ZeroShotClassificationOutput + * @property {string} sequence The sequence for which this is the output. + * @property {string[]} labels The labels sorted by order of likelihood. + * @property {number[]} scores The probabilities for each of the labels. + * + * @typedef {Object} ZeroShotClassificationPipelineOptions Parameters specific to zero-shot classification pipelines. + * @property {string} [hypothesis_template="This example is {}."] The template used to turn each + * candidate label into an NLI-style hypothesis. The candidate label will replace the {} placeholder. + * @property {boolean} [multi_label=false] Whether or not multiple candidate labels can be true. + * If `false`, the scores are normalized such that the sum of the label likelihoods for each sequence + * is 1. If `true`, the labels are considered independent and probabilities are normalized for each + * candidate by doing a softmax of the entailment score vs. the contradiction score. + * + * @callback ZeroShotClassificationPipelineCallback Classify the sequence(s) given as inputs. + * @param {string|string[]} texts The sequence(s) to classify, will be truncated if the model input is too large. + * @param {string|string[]} candidate_labels The set of possible class labels to classify each sequence into. + * Can be a single label, a string of comma-separated labels, or a list of labels. + * @param {ZeroShotClassificationPipelineOptions} [options] The options to use for zero-shot classification. + * @returns {Promise} An array or object containing the predicted labels and scores. + * + * @typedef {TextPipelineConstructorArgs & ZeroShotClassificationPipelineCallback & Disposable} ZeroShotClassificationPipelineType + */ + +/** + * NLI-based zero-shot classification pipeline using a `ModelForSequenceClassification` + * trained on NLI (natural language inference) tasks. Equivalent of `text-classification` + * pipelines, but these models don't require a hardcoded number of potential classes, they + * can be chosen at runtime. It usually means it's slower but it is **much** more flexible. + * + * **Example:** Zero shot classification with `Xenova/mobilebert-uncased-mnli`. + * ```javascript + * const classifier = await pipeline('zero-shot-classification', 'Xenova/mobilebert-uncased-mnli'); + * const text = 'Last week I upgraded my iOS version and ever since then my phone has been overheating whenever I use your app.'; + * const labels = [ 'mobile', 'billing', 'website', 'account access' ]; + * const output = await classifier(text, labels); + * // { + * // sequence: 'Last week I upgraded my iOS version and ever since then my phone has been overheating whenever I use your app.', + * // labels: [ 'mobile', 'website', 'billing', 'account access' ], + * // scores: [ 0.5562091040482018, 0.1843621307860853, 0.13942646639336376, 0.12000229877234923 ] + * // } + * ``` + * + * **Example:** Zero shot classification with `Xenova/nli-deberta-v3-xsmall` (multi-label). + * ```javascript + * const classifier = await pipeline('zero-shot-classification', 'Xenova/nli-deberta-v3-xsmall'); + * const text = 'I have a problem with my iphone that needs to be resolved asap!'; + * const labels = [ 'urgent', 'not urgent', 'phone', 'tablet', 'computer' ]; + * const output = await classifier(text, labels, { multi_label: true }); + * // { + * // sequence: 'I have a problem with my iphone that needs to be resolved asap!', + * // labels: [ 'urgent', 'phone', 'computer', 'tablet', 'not urgent' ], + * // scores: [ 0.9958870956360275, 0.9923963400697035, 0.002333537946160235, 0.0015134138567598765, 0.0010699384208377163 ] + * // } + * ``` + */ +class ZeroShotClassificationPipeline extends (/** @type {new (options: TextPipelineConstructorArgs) => ZeroShotClassificationPipelineType} */ (Pipeline)) { + /** + * Create a new ZeroShotClassificationPipeline. + * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + + // Use model config to get label2id mapping + this.label2id = Object.fromEntries( + Object.entries((/** @type {any} */(this).model).config.label2id).map( + ([k, v]) => [k.toLowerCase(), v] + ) + ); + + this.entailment_id = this.label2id['entailment']; + if (this.entailment_id === undefined) { + console.warn("Could not find 'entailment' in label2id mapping. Using 2 as entailment_id."); + this.entailment_id = 2; + } + + this.contradiction_id = this.label2id['contradiction'] ?? this.label2id['not_entailment']; + if (this.contradiction_id === undefined) { + console.warn("Could not find 'contradiction' in label2id mapping. Using 0 as contradiction_id."); + this.contradiction_id = 0; + } + } + + /** @type {ZeroShotClassificationPipelineCallback} */ + async _call(texts, candidate_labels, { + hypothesis_template = "This example is {}.", + multi_label = false, + } = {}) { + + const isBatched = Array.isArray(texts); + if (!isBatched) { + texts = [/** @type {string} */ (texts)]; + } + if (!Array.isArray(candidate_labels)) { + candidate_labels = [candidate_labels]; + } + + // Insert labels into hypothesis template + const hypotheses = candidate_labels.map( + x => hypothesis_template.replace('{}', x) + ); + + // How to perform the softmax over the logits: + // - true: softmax over the entailment vs. contradiction dim for each label independently + // - false: softmax the "entailment" logits over all candidate labels + const softmaxEach = multi_label || candidate_labels.length === 1; + + /** @type {ZeroShotClassificationOutput[]} */ + const toReturn = []; + for (const premise of texts) { + const entails_logits = []; + + for (const hypothesis of hypotheses) { + const inputs = this.tokenizer(premise, { + text_pair: hypothesis, + padding: true, + truncation: true, + }) + const outputs = await this.model(inputs) + + if (softmaxEach) { + entails_logits.push([ + outputs.logits.data[this.contradiction_id], + outputs.logits.data[this.entailment_id] + ]) + } else { + entails_logits.push(outputs.logits.data[this.entailment_id]) + } + } + + /** @type {number[]} */ + const scores = softmaxEach + ? entails_logits.map(x => (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(x)[1]) + : (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(entails_logits); + + // Sort by scores (desc) and return scores with indices + const scores_sorted = scores + .map((x, i) => [x, i]) + .sort((a, b) => (b[0] - a[0])); + + toReturn.push({ + sequence: premise, + labels: scores_sorted.map(x => candidate_labels[x[1]]), + scores: scores_sorted.map(x => x[0]), + }); + } + return isBatched ? toReturn : toReturn[0]; + } +} + +/** + * @typedef {Object} FeatureExtractionPipelineOptions Parameters specific to feature extraction pipelines. + * @property {'none'|'mean'|'cls'} [pooling="none"] The pooling method to use. + * @property {boolean} [normalize=false] Whether or not to normalize the embeddings in the last dimension. + * @property {boolean} [quantize=false] Whether or not to quantize the embeddings. + * @property {'binary'|'ubinary'} [precision='binary'] The precision to use for quantization. + * + * @callback FeatureExtractionPipelineCallback Extract the features of the input(s). + * @param {string|string[]} texts One or several texts (or one list of texts) to get the features of. + * @param {FeatureExtractionPipelineOptions} [options] The options to use for feature extraction. + * @returns {Promise} The features computed by the model. + * + * @typedef {TextPipelineConstructorArgs & FeatureExtractionPipelineCallback & Disposable} FeatureExtractionPipelineType + */ + +/** + * Feature extraction pipeline using no model head. This pipeline extracts the hidden + * states from the base transformer, which can be used as features in downstream tasks. + * + * **Example:** Run feature extraction with `bert-base-uncased` (without pooling/normalization). + * ```javascript + * const extractor = await pipeline('feature-extraction', 'Xenova/bert-base-uncased', { revision: 'default' }); + * const output = await extractor('This is a simple test.'); + * // Tensor { + * // type: 'float32', + * // data: Float32Array [0.05939924716949463, 0.021655935794115067, ...], + * // dims: [1, 8, 768] + * // } + * ``` + * + * **Example:** Run feature extraction with `bert-base-uncased` (with pooling/normalization). + * ```javascript + * const extractor = await pipeline('feature-extraction', 'Xenova/bert-base-uncased', { revision: 'default' }); + * const output = await extractor('This is a simple test.', { pooling: 'mean', normalize: true }); + * // Tensor { + * // type: 'float32', + * // data: Float32Array [0.03373778983950615, -0.010106077417731285, ...], + * // dims: [1, 768] + * // } + * ``` + * + * **Example:** Calculating embeddings with `sentence-transformers` models. + * ```javascript + * const extractor = await pipeline('feature-extraction', 'Xenova/all-MiniLM-L6-v2'); + * const output = await extractor('This is a simple test.', { pooling: 'mean', normalize: true }); + * // Tensor { + * // type: 'float32', + * // data: Float32Array [0.09094982594251633, -0.014774246141314507, ...], + * // dims: [1, 384] + * // } + * ``` + * **Example:** Calculating binary embeddings with `sentence-transformers` models. + * ```javascript + * const extractor = await pipeline('feature-extraction', 'Xenova/all-MiniLM-L6-v2'); + * const output = await extractor('This is a simple test.', { pooling: 'mean', quantize: true, precision: 'binary' }); + * // Tensor { + * // type: 'int8', + * // data: Int8Array [49, 108, 24, ...], + * // dims: [1, 48] + * // } + * ``` + */ +class FeatureExtractionPipeline extends (/** @type {new (options: TextPipelineConstructorArgs) => FeatureExtractionPipelineType} */ (Pipeline)) { + /** + * Create a new FeatureExtractionPipeline. + * @param {TextPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {FeatureExtractionPipelineCallback} */ + async _call(texts, { + pooling = /** @type {'none'} */('none'), + normalize = false, + quantize = false, + precision = /** @type {'binary'} */('binary'), + } = {}) { + + // Run tokenization + const model_inputs = this.tokenizer(texts, { + padding: true, + truncation: true, + }); + + // Run model + const outputs = await this.model(model_inputs) + + // TODO: Provide warning to the user that they might be using model which was not exported + // specifically for feature extraction + // console.log(this.model.config) + // console.log(outputs) + + /** @type {Tensor} */ + let result = outputs.last_hidden_state ?? outputs.logits ?? outputs.token_embeddings; + if (pooling === 'none') { + // Skip pooling + } else if (pooling === 'mean') { + result = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.mean_pooling)(result, model_inputs.attention_mask); + } else if (pooling === 'cls') { + result = result.slice(null, 0); + } else { + throw Error(`Pooling method '${pooling}' not supported.`); + } + + if (normalize) { + result = result.normalize(2, -1); + } + + if (quantize) { + result = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.quantize_embeddings)(result, precision); + } + + return result; + } +} + + +/** + * @typedef {Object} ImageFeatureExtractionPipelineOptions Parameters specific to image feature extraction pipelines. + * @property {boolean} [pool=null] Whether or not to return the pooled output. If set to `false`, the model will return the raw hidden states. + * + * @callback ImageFeatureExtractionPipelineCallback Extract the features of the input(s). + * @param {ImagePipelineInputs} images One or several images (or one list of images) to get the features of. + * @param {ImageFeatureExtractionPipelineOptions} [options] The options to use for image feature extraction. + * @returns {Promise} The image features computed by the model. + * + * @typedef {ImagePipelineConstructorArgs & ImageFeatureExtractionPipelineCallback & Disposable} ImageFeatureExtractionPipelineType + */ + +/** + * Image feature extraction pipeline using no model head. This pipeline extracts the hidden + * states from the base transformer, which can be used as features in downstream tasks. + * + * **Example:** Perform image feature extraction with `Xenova/vit-base-patch16-224-in21k`. + * ```javascript + * const image_feature_extractor = await pipeline('image-feature-extraction', 'Xenova/vit-base-patch16-224-in21k'); + * const url = 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/cats.png'; + * const features = await image_feature_extractor(url); + * // Tensor { + * // dims: [ 1, 197, 768 ], + * // type: 'float32', + * // data: Float32Array(151296) [ ... ], + * // size: 151296 + * // } + * ``` + * + * **Example:** Compute image embeddings with `Xenova/clip-vit-base-patch32`. + * ```javascript + * const image_feature_extractor = await pipeline('image-feature-extraction', 'Xenova/clip-vit-base-patch32'); + * const url = 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/cats.png'; + * const features = await image_feature_extractor(url); + * // Tensor { + * // dims: [ 1, 512 ], + * // type: 'float32', + * // data: Float32Array(512) [ ... ], + * // size: 512 + * // } + * ``` + */ +class ImageFeatureExtractionPipeline extends (/** @type {new (options: ImagePipelineConstructorArgs) => ImageFeatureExtractionPipelineType} */ (Pipeline)) { + /** + * Create a new ImageFeatureExtractionPipeline. + * @param {ImagePipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {ImageFeatureExtractionPipelineCallback} */ + async _call(images, { + pool = null, + } = {}) { + + const preparedImages = await prepareImages(images); + const { pixel_values } = await this.processor(preparedImages); + const outputs = await this.model({ pixel_values }); + + /** @type {Tensor} */ + let result; + if (pool) { + if (!('pooler_output' in outputs)) { + throw Error(`No pooled output was returned. Make sure the model has a 'pooler' layer when using the 'pool' option.`); + } + result = outputs.pooler_output; + + } else { + result = outputs.last_hidden_state ?? outputs.logits ?? outputs.image_embeds; + } + return result; + } +} + +// TODO +// export class SentenceSimilarityPipeline extends Pipeline { +// } + +/** + * @typedef {Object} AudioClassificationSingle + * @property {string} label The label predicted. + * @property {number} score The corresponding probability. + * @typedef {AudioClassificationSingle[]} AudioClassificationOutput + * + * @typedef {Object} AudioClassificationPipelineOptions Parameters specific to audio classification pipelines. + * @property {number} [top_k=5] The number of top labels that will be returned by the pipeline. + * If the provided number is `null` or higher than the number of labels available in the model configuration, + * it will default to the number of labels. + * + * @callback AudioClassificationPipelineCallback Classify the sequence(s) given as inputs. + * @param {AudioPipelineInputs} audio The input audio file(s) to be classified. The input is either: + * - `string` or `URL` that is the filename/URL of the audio file, the file will be read at the processor's sampling rate + * to get the waveform using the [`AudioContext`](https://developer.mozilla.org/en-US/docs/Web/API/AudioContext) API. + * If `AudioContext` is not available, you should pass the raw waveform in as a Float32Array of shape `(n, )`. + * - `Float32Array` or `Float64Array` of shape `(n, )`, representing the raw audio at the correct sampling rate (no further check will be done). + * @param {AudioClassificationPipelineOptions} [options] The options to use for audio classification. + * @returns {Promise} An array or object containing the predicted labels and scores. + * + * @typedef {AudioPipelineConstructorArgs & AudioClassificationPipelineCallback & Disposable} AudioClassificationPipelineType + */ + +/** + * Audio classification pipeline using any `AutoModelForAudioClassification`. + * This pipeline predicts the class of a raw waveform or an audio file. + * + * **Example:** Perform audio classification with `Xenova/wav2vec2-large-xlsr-53-gender-recognition-librispeech`. + * ```javascript + * const classifier = await pipeline('audio-classification', 'Xenova/wav2vec2-large-xlsr-53-gender-recognition-librispeech'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav'; + * const output = await classifier(url); + * // [ + * // { label: 'male', score: 0.9981542229652405 }, + * // { label: 'female', score: 0.001845747814513743 } + * // ] + * ``` + * + * **Example:** Perform audio classification with `Xenova/ast-finetuned-audioset-10-10-0.4593` and return top 4 results. + * ```javascript + * const classifier = await pipeline('audio-classification', 'Xenova/ast-finetuned-audioset-10-10-0.4593'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/cat_meow.wav'; + * const output = await classifier(url, { top_k: 4 }); + * // [ + * // { label: 'Meow', score: 0.5617874264717102 }, + * // { label: 'Cat', score: 0.22365376353263855 }, + * // { label: 'Domestic animals, pets', score: 0.1141069084405899 }, + * // { label: 'Animal', score: 0.08985692262649536 }, + * // ] + * ``` + */ +class AudioClassificationPipeline extends (/** @type {new (options: AudioPipelineConstructorArgs) => AudioClassificationPipelineType} */ (Pipeline)) { + + /** + * Create a new AudioClassificationPipeline. + * @param {AudioPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {AudioClassificationPipelineCallback} */ + async _call(audio, { + top_k = 5 + } = {}) { + + const sampling_rate = this.processor.feature_extractor.config.sampling_rate; + const preparedAudios = await prepareAudios(audio, sampling_rate); + + const id2label = this.model.config.id2label; + + const toReturn = []; + for (const aud of preparedAudios) { + const inputs = await this.processor(aud); + const output = await this.model(inputs); + const logits = output.logits[0]; + + const scores = await (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.topk)(new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.Tensor( + 'float32', + (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(logits.data), + logits.dims, + ), top_k); + + const values = scores[0].tolist(); + const indices = scores[1].tolist(); + + const vals = indices.map((x, i) => ({ + label: /** @type {string} */ (id2label ? id2label[x] : `LABEL_${x}`), + score: /** @type {number} */ (values[i]), + })); + + toReturn.push(vals); + }; + return Array.isArray(audio) ? toReturn : toReturn[0]; + } +} + +/** + * @typedef {Object} ZeroShotAudioClassificationOutput + * @property {string} label The label identified by the model. It is one of the suggested `candidate_label`. + * @property {number} score The score attributed by the model for that label (between 0 and 1). + * + * @typedef {Object} ZeroShotAudioClassificationPipelineOptions Parameters specific to zero-shot audio classification pipelines. + * @property {string} [hypothesis_template="This is a sound of {}."] The sentence used in conjunction with `candidate_labels` + * to attempt the audio classification by replacing the placeholder with the candidate_labels. + * Then likelihood is estimated by using `logits_per_audio`. + * + * @callback ZeroShotAudioClassificationPipelineCallback Classify the sequence(s) given as inputs. + * @param {AudioPipelineInputs} audio The input audio file(s) to be classified. The input is either: + * - `string` or `URL` that is the filename/URL of the audio file, the file will be read at the processor's sampling rate + * to get the waveform using the [`AudioContext`](https://developer.mozilla.org/en-US/docs/Web/API/AudioContext) API. + * If `AudioContext` is not available, you should pass the raw waveform in as a Float32Array of shape `(n, )`. + * - `Float32Array` or `Float64Array` of shape `(n, )`, representing the raw audio at the correct sampling rate (no further check will be done). + * @param {string[]} candidate_labels The candidate labels for this audio. + * @param {ZeroShotAudioClassificationPipelineOptions} [options] The options to use for zero-shot audio classification. + * @returns {Promise} An array of objects containing the predicted labels and scores. + * + * @typedef {TextAudioPipelineConstructorArgs & ZeroShotAudioClassificationPipelineCallback & Disposable} ZeroShotAudioClassificationPipelineType + */ + +/** + * Zero shot audio classification pipeline using `ClapModel`. This pipeline predicts the class of an audio when you + * provide an audio and a set of `candidate_labels`. + * + * **Example**: Perform zero-shot audio classification with `Xenova/clap-htsat-unfused`. + * ```javascript + * const classifier = await pipeline('zero-shot-audio-classification', 'Xenova/clap-htsat-unfused'); + * const audio = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/dog_barking.wav'; + * const candidate_labels = ['dog', 'vaccum cleaner']; + * const scores = await classifier(audio, candidate_labels); + * // [ + * // { score: 0.9993992447853088, label: 'dog' }, + * // { score: 0.0006007603369653225, label: 'vaccum cleaner' } + * // ] + * ``` + */ +class ZeroShotAudioClassificationPipeline extends (/** @type {new (options: TextAudioPipelineConstructorArgs) => ZeroShotAudioClassificationPipelineType} */ (Pipeline)) { + + /** + * Create a new ZeroShotAudioClassificationPipeline. + * @param {TextAudioPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {ZeroShotAudioClassificationPipelineCallback} */ + async _call(audio, candidate_labels, { + hypothesis_template = "This is a sound of {}." + } = {}) { + + const single = !Array.isArray(audio); + if (single) { + audio = [/** @type {AudioInput} */ (audio)]; + } + + // Insert label into hypothesis template + const texts = candidate_labels.map( + x => hypothesis_template.replace('{}', x) + ); + + // Run tokenization + const text_inputs = this.tokenizer(texts, { + padding: true, + truncation: true, + }); + + const sampling_rate = this.processor.feature_extractor.config.sampling_rate; + const preparedAudios = await prepareAudios(audio, sampling_rate); + + const toReturn = []; + for (const aud of preparedAudios) { + const audio_inputs = await this.processor(aud); + + // Run model with both text and audio inputs + const output = await this.model({ ...text_inputs, ...audio_inputs }); + + // Compute softmax per audio + const probs = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(output.logits_per_audio.data); + + toReturn.push([...probs].map((x, i) => ({ + score: x, + label: candidate_labels[i] + }))); + } + return single ? toReturn[0] : toReturn; + } +} + +/** + * @typedef {Object} Chunk + * @property {[number, number]} timestamp The start and end timestamp of the chunk in seconds. + * @property {string} text The recognized text. + */ + +/** + * @typedef {Object} AutomaticSpeechRecognitionOutput + * @property {string} text The recognized text. + * @property {Chunk[]} [chunks] When using `return_timestamps`, the `chunks` will become a list + * containing all the various text chunks identified by the model. + * + * @typedef {Object} AutomaticSpeechRecognitionSpecificParams Parameters specific to automatic-speech-recognition pipelines. + * @property {boolean|'word'} [return_timestamps] Whether to return timestamps or not. Default is `false`. + * @property {number} [chunk_length_s] The length of audio chunks to process in seconds. Default is 0 (no chunking). + * @property {number} [stride_length_s] The length of overlap between consecutive audio chunks in seconds. If not provided, defaults to `chunk_length_s / 6`. + * @property {boolean} [force_full_sequences] Whether to force outputting full sequences or not. Default is `false`. + * @property {string} [language] The source language. Default is `null`, meaning it should be auto-detected. Use this to potentially improve performance if the source language is known. + * @property {string} [task] The task to perform. Default is `null`, meaning it should be auto-detected. + * @property {number} [num_frames] The number of frames in the input audio. + * @typedef {import('./generation/configuration_utils.js').GenerationConfig & AutomaticSpeechRecognitionSpecificParams} AutomaticSpeechRecognitionConfig + * + * @callback AutomaticSpeechRecognitionPipelineCallback Transcribe the audio sequence(s) given as inputs to text. + * @param {AudioPipelineInputs} audio The input audio file(s) to be transcribed. The input is either: + * - `string` or `URL` that is the filename/URL of the audio file, the file will be read at the processor's sampling rate + * to get the waveform using the [`AudioContext`](https://developer.mozilla.org/en-US/docs/Web/API/AudioContext) API. + * If `AudioContext` is not available, you should pass the raw waveform in as a Float32Array of shape `(n, )`. + * - `Float32Array` or `Float64Array` of shape `(n, )`, representing the raw audio at the correct sampling rate (no further check will be done). + * @param {Partial} [options] Additional keyword arguments to pass along to the generate method of the model. + * @returns {Promise} An object containing the transcription text and optionally timestamps if `return_timestamps` is `true`. + * + * @typedef {TextAudioPipelineConstructorArgs & AutomaticSpeechRecognitionPipelineCallback & Disposable} AutomaticSpeechRecognitionPipelineType + */ + +/** + * Pipeline that aims at extracting spoken text contained within some audio. + * + * **Example:** Transcribe English. + * ```javascript + * const transcriber = await pipeline('automatic-speech-recognition', 'Xenova/whisper-tiny.en'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav'; + * const output = await transcriber(url); + * // { text: " And so my fellow Americans ask not what your country can do for you, ask what you can do for your country." } + * ``` + * + * **Example:** Transcribe English w/ timestamps. + * ```javascript + * const transcriber = await pipeline('automatic-speech-recognition', 'Xenova/whisper-tiny.en'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav'; + * const output = await transcriber(url, { return_timestamps: true }); + * // { + * // text: " And so my fellow Americans ask not what your country can do for you, ask what you can do for your country." + * // chunks: [ + * // { timestamp: [0, 8], text: " And so my fellow Americans ask not what your country can do for you" } + * // { timestamp: [8, 11], text: " ask what you can do for your country." } + * // ] + * // } + * ``` + * + * **Example:** Transcribe English w/ word-level timestamps. + * ```javascript + * const transcriber = await pipeline('automatic-speech-recognition', 'Xenova/whisper-tiny.en'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav'; + * const output = await transcriber(url, { return_timestamps: 'word' }); + * // { + * // "text": " And so my fellow Americans ask not what your country can do for you ask what you can do for your country.", + * // "chunks": [ + * // { "text": " And", "timestamp": [0, 0.78] }, + * // { "text": " so", "timestamp": [0.78, 1.06] }, + * // { "text": " my", "timestamp": [1.06, 1.46] }, + * // ... + * // { "text": " for", "timestamp": [9.72, 9.92] }, + * // { "text": " your", "timestamp": [9.92, 10.22] }, + * // { "text": " country.", "timestamp": [10.22, 13.5] } + * // ] + * // } + * ``` + * + * **Example:** Transcribe French. + * ```javascript + * const transcriber = await pipeline('automatic-speech-recognition', 'Xenova/whisper-small'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/french-audio.mp3'; + * const output = await transcriber(url, { language: 'french', task: 'transcribe' }); + * // { text: " J'adore, j'aime, je n'aime pas, je déteste." } + * ``` + * + * **Example:** Translate French to English. + * ```javascript + * const transcriber = await pipeline('automatic-speech-recognition', 'Xenova/whisper-small'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/french-audio.mp3'; + * const output = await transcriber(url, { language: 'french', task: 'translate' }); + * // { text: " I love, I like, I don't like, I hate." } + * ``` + * + * **Example:** Transcribe/translate audio longer than 30 seconds. + * ```javascript + * const transcriber = await pipeline('automatic-speech-recognition', 'Xenova/whisper-tiny.en'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/ted_60.wav'; + * const output = await transcriber(url, { chunk_length_s: 30, stride_length_s: 5 }); + * // { text: " So in college, I was a government major, which means [...] So I'd start off light and I'd bump it up" } + * ``` + */ +class AutomaticSpeechRecognitionPipeline extends (/** @type {new (options: TextAudioPipelineConstructorArgs) => AutomaticSpeechRecognitionPipelineType} */ (Pipeline)) { + + /** + * Create a new AutomaticSpeechRecognitionPipeline. + * @param {TextAudioPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {AutomaticSpeechRecognitionPipelineCallback} */ + async _call(audio, kwargs = {}) { + switch (this.model.config.model_type) { + case 'whisper': + return this._call_whisper(audio, kwargs) + case 'wav2vec2': + case 'wav2vec2-bert': + case 'unispeech': + case 'unispeech-sat': + case 'hubert': + return this._call_wav2vec2(audio, kwargs) + default: + throw new Error(`AutomaticSpeechRecognitionPipeline does not support model type '${this.model.config.model_type}'.`) + } + } + + /** + * @type {AutomaticSpeechRecognitionPipelineCallback} + * @private + */ + async _call_wav2vec2(audio, kwargs) { + // TODO use kwargs + + if (kwargs.language) { + console.warn('`language` parameter is not yet supported for `wav2vec2` models, defaulting to "English".'); + } + if (kwargs.task) { + console.warn('`task` parameter is not yet supported for `wav2vec2` models, defaulting to "transcribe".'); + } + + const single = !Array.isArray(audio); + if (single) { + audio = [/** @type {AudioInput} */ (audio)]; + } + + const sampling_rate = this.processor.feature_extractor.config.sampling_rate; + const preparedAudios = await prepareAudios(audio, sampling_rate); + + const toReturn = []; + for (const aud of preparedAudios) { + const inputs = await this.processor(aud); + const output = await this.model(inputs); + const logits = output.logits[0]; + + const predicted_ids = []; + for (const item of logits) { + predicted_ids.push((0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.max)(item.data)[1]) + } + const predicted_sentences = this.tokenizer.decode(predicted_ids) + toReturn.push({ text: predicted_sentences }) + } + return single ? toReturn[0] : toReturn; + } + + /** + * @type {AutomaticSpeechRecognitionPipelineCallback} + * @private + */ + async _call_whisper(audio, kwargs) { + const return_timestamps = kwargs.return_timestamps ?? false; + const chunk_length_s = kwargs.chunk_length_s ?? 0; + const force_full_sequences = kwargs.force_full_sequences ?? false; + let stride_length_s = kwargs.stride_length_s ?? null; + + const generation_config = { ...kwargs } + + if (return_timestamps === 'word') { + generation_config['return_token_timestamps'] = true; + generation_config['return_timestamps'] = false; // Do not predict timestamp tokens + } + + const single = !Array.isArray(audio); + if (single) { + audio = [/** @type {AudioInput} */ (audio)]; + } + + const time_precision = this.processor.feature_extractor.config.chunk_length / this.model.config.max_source_positions; + const hop_length = this.processor.feature_extractor.config.hop_length; + + const sampling_rate = this.processor.feature_extractor.config.sampling_rate; + const preparedAudios = await prepareAudios(audio, sampling_rate); + + const toReturn = []; + for (const aud of preparedAudios) { + /** @type {{stride: number[], input_features: Tensor, is_last: boolean, tokens?: bigint[], token_timestamps?: number[]}[]} */ + let chunks = []; + if (chunk_length_s > 0) { + if (stride_length_s === null) { + stride_length_s = chunk_length_s / 6; + } else if (chunk_length_s <= stride_length_s) { + throw Error("`chunk_length_s` must be larger than `stride_length_s`.") + } + + // TODO support different stride_length_s (for left and right) + + const window = sampling_rate * chunk_length_s; + const stride = sampling_rate * stride_length_s; + const jump = window - 2 * stride; + let offset = 0; + + // Create subarrays of audio with overlaps + while (true) { + const offset_end = offset + window; + const subarr = aud.subarray(offset, offset_end); + const feature = await this.processor(subarr); + + const is_first = offset === 0; + const is_last = offset_end >= aud.length; + chunks.push({ + stride: [ + subarr.length, + is_first ? 0 : stride, + is_last ? 0 : stride + ], + input_features: feature.input_features, + is_last, + }) + if (is_last) break; + offset += jump; + } + + } else { + chunks = [{ + stride: [aud.length, 0, 0], + input_features: (await this.processor(aud)).input_features, + is_last: true + }] + } + + // Generate for each set of input features + for (const chunk of chunks) { + generation_config.num_frames = Math.floor(chunk.stride[0] / hop_length); + + // NOTE: doing sequentially for now + const data = await this.model.generate({ + inputs: chunk.input_features, + ...generation_config + }); + + // TODO: Right now we only get top beam + if (return_timestamps === 'word') { + chunk.tokens = data.sequences.tolist()[0]; + chunk.token_timestamps = data.token_timestamps.tolist()[0].map( + (/** @type {number} */ x) => (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.round)(x, 2) + ); + + } else { + chunk.tokens = (/** @type {Tensor} */(data))[0].tolist(); + } + + // convert stride to seconds + chunk.stride = chunk.stride.map(x => x / sampling_rate); + } + + // Merge text chunks + // @ts-ignore + const [full_text, optional] = this.tokenizer._decode_asr(chunks, { + time_precision, return_timestamps, force_full_sequences + }); + + toReturn.push({ text: full_text, ...optional }) + } + return single ? toReturn[0] : toReturn; + } +} + +/** + * @typedef {Object} ImageToTextSingle + * @property {string} generated_text The generated text. + * @typedef {ImageToTextSingle[]} ImageToTextOutput + * + * @callback ImageToTextPipelineCallback Assign labels to the image(s) passed as inputs. + * @param {ImagePipelineInputs} texts The images to be captioned. + * @param {Partial} [options] Additional keyword arguments to pass along to the generate method of the model. + * @returns {Promise} An object (or array of objects) containing the generated text(s). + * + * @typedef {TextImagePipelineConstructorArgs & ImageToTextPipelineCallback & Disposable} ImageToTextPipelineType + */ + +/** + * Image To Text pipeline using a `AutoModelForVision2Seq`. This pipeline predicts a caption for a given image. + * + * **Example:** Generate a caption for an image w/ `Xenova/vit-gpt2-image-captioning`. + * ```javascript + * const captioner = await pipeline('image-to-text', 'Xenova/vit-gpt2-image-captioning'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/cats.jpg'; + * const output = await captioner(url); + * // [{ generated_text: 'a cat laying on a couch with another cat' }] + * ``` + * + * **Example:** Optical Character Recognition (OCR) w/ `Xenova/trocr-small-handwritten`. + * ```javascript + * const captioner = await pipeline('image-to-text', 'Xenova/trocr-small-handwritten'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/handwriting.jpg'; + * const output = await captioner(url); + * // [{ generated_text: 'Mr. Brown commented icily.' }] + * ``` + */ +class ImageToTextPipeline extends (/** @type {new (options: TextImagePipelineConstructorArgs) => ImageToTextPipelineType} */ (Pipeline)) { + + /** + * Create a new ImageToTextPipeline. + * @param {TextImagePipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {ImageToTextPipelineCallback} */ + async _call(images, generate_kwargs = {}) { + + const isBatched = Array.isArray(images); + const preparedImages = await prepareImages(images); + + const { pixel_values } = await this.processor(preparedImages); + + const toReturn = []; + for (const batch of pixel_values) { + batch.dims = [1, ...batch.dims] + const output = await this.model.generate({ inputs: batch, ...generate_kwargs }); + const decoded = this.tokenizer.batch_decode(/** @type {Tensor} */(output), { + skip_special_tokens: true, + }).map(x => ({ generated_text: x.trim() })) + toReturn.push(decoded); + } + + return isBatched ? toReturn : toReturn[0]; + } +} + +/** + * @typedef {Object} ImageClassificationSingle + * @property {string} label The label identified by the model. + * @property {number} score The score attributed by the model for that label. + * @typedef {ImageClassificationSingle[]} ImageClassificationOutput + * + * @typedef {Object} ImageClassificationPipelineOptions Parameters specific to image classification pipelines. + * @property {number} [top_k=1] The number of top labels that will be returned by the pipeline. + * + * @callback ImageClassificationPipelineCallback Assign labels to the image(s) passed as inputs. + * @param {ImagePipelineInputs} images The input images(s) to be classified. + * @param {ImageClassificationPipelineOptions} [options] The options to use for image classification. + * @returns {Promise} An array or object containing the predicted labels and scores. + * + * @typedef {ImagePipelineConstructorArgs & ImageClassificationPipelineCallback & Disposable} ImageClassificationPipelineType + */ + +/** + * Image classification pipeline using any `AutoModelForImageClassification`. + * This pipeline predicts the class of an image. + * + * **Example:** Classify an image. + * ```javascript + * const classifier = await pipeline('image-classification', 'Xenova/vit-base-patch16-224'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/tiger.jpg'; + * const output = await classifier(url); + * // [ + * // { label: 'tiger, Panthera tigris', score: 0.632695734500885 }, + * // ] + * ``` + * + * **Example:** Classify an image and return top `n` classes. + * ```javascript + * const classifier = await pipeline('image-classification', 'Xenova/vit-base-patch16-224'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/tiger.jpg'; + * const output = await classifier(url, { top_k: 3 }); + * // [ + * // { label: 'tiger, Panthera tigris', score: 0.632695734500885 }, + * // { label: 'tiger cat', score: 0.3634825646877289 }, + * // { label: 'lion, king of beasts, Panthera leo', score: 0.00045060308184474707 }, + * // ] + * ``` + * + * **Example:** Classify an image and return all classes. + * ```javascript + * const classifier = await pipeline('image-classification', 'Xenova/vit-base-patch16-224'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/tiger.jpg'; + * const output = await classifier(url, { top_k: 0 }); + * // [ + * // { label: 'tiger, Panthera tigris', score: 0.632695734500885 }, + * // { label: 'tiger cat', score: 0.3634825646877289 }, + * // { label: 'lion, king of beasts, Panthera leo', score: 0.00045060308184474707 }, + * // { label: 'jaguar, panther, Panthera onca, Felis onca', score: 0.00035465499968267977 }, + * // ... + * // ] + * ``` + */ +class ImageClassificationPipeline extends (/** @type {new (options: ImagePipelineConstructorArgs) => ImageClassificationPipelineType} */ (Pipeline)) { + + /** + * Create a new ImageClassificationPipeline. + * @param {ImagePipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {ImageClassificationPipelineCallback} */ + async _call(images, { + top_k = 5 + } = {}) { + + const preparedImages = await prepareImages(images); + + const { pixel_values } = await this.processor(preparedImages); + const output = await this.model({ pixel_values }); + + const id2label = this.model.config.id2label; + + /** @type {ImageClassificationOutput[]} */ + const toReturn = []; + for (const batch of output.logits) { + const scores = await (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.topk)(new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.Tensor( + 'float32', + (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(batch.data), + batch.dims, + ), top_k); + + const values = scores[0].tolist(); + const indices = scores[1].tolist(); + + const vals = indices.map((x, i) => ({ + label: /** @type {string} */ (id2label ? id2label[x] : `LABEL_${x}`), + score: /** @type {number} */ (values[i]), + })); + toReturn.push(vals); + } + + return Array.isArray(images) ? toReturn : toReturn[0]; + } + +} + +/** + * @typedef {Object} ImageSegmentationPipelineOutput + * @property {string} label The label of the segment. + * @property {number|null} score The score of the segment. + * @property {RawImage} mask The mask of the segment. + * + * @typedef {Object} ImageSegmentationPipelineOptions Parameters specific to image segmentation pipelines. + * @property {number} [threshold=0.5] Probability threshold to filter out predicted masks. + * @property {number} [mask_threshold=0.5] Threshold to use when turning the predicted masks into binary values. + * @property {number} [overlap_mask_area_threshold=0.8] Mask overlap threshold to eliminate small, disconnected segments. + * @property {null|string} [subtask=null] Segmentation task to be performed. One of [`panoptic`, `instance`, and `semantic`], + * depending on model capabilities. If not set, the pipeline will attempt to resolve (in that order). + * @property {number[]} [label_ids_to_fuse=null] List of label ids to fuse. If not set, do not fuse any labels. + * @property {number[][]} [target_sizes=null] List of target sizes for the input images. If not set, use the original image sizes. + * + * @callback ImageSegmentationPipelineCallback Segment the input images. + * @param {ImagePipelineInputs} images The input images. + * @param {ImageSegmentationPipelineOptions} [options] The options to use for image segmentation. + * @returns {Promise} The annotated segments. + * + * @typedef {ImagePipelineConstructorArgs & ImageSegmentationPipelineCallback & Disposable} ImageSegmentationPipelineType + */ + +/** + * Image segmentation pipeline using any `AutoModelForXXXSegmentation`. + * This pipeline predicts masks of objects and their classes. + * + * **Example:** Perform image segmentation with `Xenova/detr-resnet-50-panoptic`. + * ```javascript + * const segmenter = await pipeline('image-segmentation', 'Xenova/detr-resnet-50-panoptic'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/cats.jpg'; + * const output = await segmenter(url); + * // [ + * // { label: 'remote', score: 0.9984649419784546, mask: RawImage { ... } }, + * // { label: 'cat', score: 0.9994316101074219, mask: RawImage { ... } } + * // ] + * ``` + */ +class ImageSegmentationPipeline extends (/** @type {new (options: ImagePipelineConstructorArgs) => ImageSegmentationPipelineType} */ (Pipeline)) { + /** + * Create a new ImageSegmentationPipeline. + * @param {ImagePipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + + this.subtasks_mapping = { + // Mapping of subtasks to their corresponding post-processing function names. + panoptic: 'post_process_panoptic_segmentation', + instance: 'post_process_instance_segmentation', + semantic: 'post_process_semantic_segmentation' + } + } + + /** @type {ImageSegmentationPipelineCallback} */ + async _call(images, { + threshold = 0.5, + mask_threshold = 0.5, + overlap_mask_area_threshold = 0.8, + label_ids_to_fuse = null, + target_sizes = null, + subtask = null, + } = {}) { + const isBatched = Array.isArray(images); + + if (isBatched && images.length !== 1) { + throw Error("Image segmentation pipeline currently only supports a batch size of 1."); + } + + const preparedImages = await prepareImages(images); + const imageSizes = preparedImages.map(x => [x.height, x.width]); + + const { pixel_values, pixel_mask } = await this.processor(preparedImages); + const output = await this.model({ pixel_values, pixel_mask }); + + let fn = null; + if (subtask !== null) { + fn = this.subtasks_mapping[subtask]; + } else { + for (let [task, func] of Object.entries(this.subtasks_mapping)) { + if (func in this.processor.feature_extractor) { + fn = this.processor.feature_extractor[func].bind(this.processor.feature_extractor); + subtask = task; + break; + } + } + } + + const id2label = this.model.config.id2label; + + /** @type {ImageSegmentationPipelineOutput[]} */ + const annotation = []; + if (subtask === 'panoptic' || subtask === 'instance') { + const processed = fn( + output, + threshold, + mask_threshold, + overlap_mask_area_threshold, + label_ids_to_fuse, + target_sizes ?? imageSizes, // TODO FIX? + )[0]; + + const segmentation = processed.segmentation; + + for (const segment of processed.segments_info) { + const maskData = new Uint8ClampedArray(segmentation.data.length); + for (let i = 0; i < segmentation.data.length; ++i) { + if (segmentation.data[i] === segment.id) { + maskData[i] = 255; + } + } + + const mask = new _utils_image_js__WEBPACK_IMPORTED_MODULE_8__.RawImage(maskData, segmentation.dims[1], segmentation.dims[0], 1) + + annotation.push({ + score: segment.score, + label: id2label[segment.label_id], + mask: mask + }) + } + + } else if (subtask === 'semantic') { + const { segmentation, labels } = fn(output, target_sizes ?? imageSizes)[0]; + + for (const label of labels) { + const maskData = new Uint8ClampedArray(segmentation.data.length); + for (let i = 0; i < segmentation.data.length; ++i) { + if (segmentation.data[i] === label) { + maskData[i] = 255; + } + } + + const mask = new _utils_image_js__WEBPACK_IMPORTED_MODULE_8__.RawImage(maskData, segmentation.dims[1], segmentation.dims[0], 1); + + annotation.push({ + score: null, + label: id2label[label], + mask: mask + }); + } + } else { + throw Error(`Subtask ${subtask} not supported.`); + } + + return annotation; + } +} + +/** + * @typedef {Object} ZeroShotImageClassificationOutput + * @property {string} label The label identified by the model. It is one of the suggested `candidate_label`. + * @property {number} score The score attributed by the model for that label (between 0 and 1). + * + * @typedef {Object} ZeroShotImageClassificationPipelineOptions Parameters specific to zero-shot image classification pipelines. + * @property {string} [hypothesis_template="This is a photo of {}"] The sentence used in conjunction with `candidate_labels` + * to attempt the image classification by replacing the placeholder with the candidate_labels. + * Then likelihood is estimated by using `logits_per_image`. + * + * @callback ZeroShotImageClassificationPipelineCallback Assign labels to the image(s) passed as inputs. + * @param {ImagePipelineInputs} images The input images. + * @param {string[]} candidate_labels The candidate labels for this image. + * @param {ZeroShotImageClassificationPipelineOptions} [options] The options to use for zero-shot image classification. + * @returns {Promise} An array of objects containing the predicted labels and scores. + * + * @typedef {TextImagePipelineConstructorArgs & ZeroShotImageClassificationPipelineCallback & Disposable} ZeroShotImageClassificationPipelineType + */ + +/** + * Zero shot image classification pipeline. This pipeline predicts the class of + * an image when you provide an image and a set of `candidate_labels`. + * + * **Example:** Zero shot image classification w/ `Xenova/clip-vit-base-patch32`. + * ```javascript + * const classifier = await pipeline('zero-shot-image-classification', 'Xenova/clip-vit-base-patch32'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/tiger.jpg'; + * const output = await classifier(url, ['tiger', 'horse', 'dog']); + * // [ + * // { score: 0.9993917942047119, label: 'tiger' }, + * // { score: 0.0003519294841680676, label: 'horse' }, + * // { score: 0.0002562698791734874, label: 'dog' } + * // ] + * ``` + */ +class ZeroShotImageClassificationPipeline extends (/** @type {new (options: TextImagePipelineConstructorArgs) => ZeroShotImageClassificationPipelineType} */ (Pipeline)) { + /** + * Create a new ZeroShotImageClassificationPipeline. + * @param {TextImagePipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {ZeroShotImageClassificationPipelineCallback} */ + async _call(images, candidate_labels, { + hypothesis_template = "This is a photo of {}" + } = {}) { + + const isBatched = Array.isArray(images); + const preparedImages = await prepareImages(images); + + // Insert label into hypothesis template + const texts = candidate_labels.map( + x => hypothesis_template.replace('{}', x) + ); + + // Run tokenization + const text_inputs = this.tokenizer(texts, { + padding: this.model.config.model_type === 'siglip' ? 'max_length' : true, + truncation: true, + }); + + // Run processor + const { pixel_values } = await this.processor(preparedImages); + + // Run model with both text and pixel inputs + const output = await this.model({ ...text_inputs, pixel_values }); + + const function_to_apply = + this.model.config.model_type === 'siglip' + ? batch => batch.sigmoid().data + : batch => (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.softmax)(batch.data); + + // Compare each image with each candidate label + const toReturn = []; + for (const batch of output.logits_per_image) { + // Compute softmax per image + const probs = function_to_apply(batch); + + const result = [...probs].map((x, i) => ({ + score: x, + label: candidate_labels[i] + })); + result.sort((a, b) => b.score - a.score); // sort by score in descending order + toReturn.push(result); + } + + return isBatched ? toReturn : toReturn[0]; + } +} + + +/** + * @typedef {Object} ObjectDetectionPipelineSingle + * @property {string} label The class label identified by the model. + * @property {number} score The score attributed by the model for that label. + * @property {BoundingBox} box The bounding box of detected object in image's original size, or as a percentage if `percentage` is set to true. + * @typedef {ObjectDetectionPipelineSingle[]} ObjectDetectionPipelineOutput + * + * @typedef {Object} ObjectDetectionPipelineOptions Parameters specific to object detection pipelines. + * @property {number} [threshold=0.9] The threshold used to filter boxes by score. + * @property {boolean} [percentage=false] Whether to return the boxes coordinates in percentage (true) or in pixels (false). + * + * @callback ObjectDetectionPipelineCallback Detect objects (bounding boxes & classes) in the image(s) passed as inputs. + * @param {ImagePipelineInputs} images The input images. + * @param {ObjectDetectionPipelineOptions} [options] The options to use for object detection. + * @returns {Promise} A list of objects or a list of list of objects. + * + * @typedef {ImagePipelineConstructorArgs & ObjectDetectionPipelineCallback & Disposable} ObjectDetectionPipelineType + */ + +/** + * Object detection pipeline using any `AutoModelForObjectDetection`. + * This pipeline predicts bounding boxes of objects and their classes. + * + * **Example:** Run object-detection with `Xenova/detr-resnet-50`. + * ```javascript + * const detector = await pipeline('object-detection', 'Xenova/detr-resnet-50'); + * const img = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/cats.jpg'; + * const output = await detector(img, { threshold: 0.9 }); + * // [{ + * // score: 0.9976370930671692, + * // label: "remote", + * // box: { xmin: 31, ymin: 68, xmax: 190, ymax: 118 } + * // }, + * // ... + * // { + * // score: 0.9984092116355896, + * // label: "cat", + * // box: { xmin: 331, ymin: 19, xmax: 649, ymax: 371 } + * // }] + * ``` + */ +class ObjectDetectionPipeline extends (/** @type {new (options: ImagePipelineConstructorArgs) => ObjectDetectionPipelineType} */ (Pipeline)) { + + /** + * Create a new ObjectDetectionPipeline. + * @param {ImagePipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {ObjectDetectionPipelineCallback} */ + async _call(images, { + threshold = 0.9, + percentage = false, + } = {}) { + + const isBatched = Array.isArray(images); + + if (isBatched && images.length !== 1) { + throw Error("Object detection pipeline currently only supports a batch size of 1."); + } + const preparedImages = await prepareImages(images); + + const imageSizes = percentage ? null : preparedImages.map(x => [x.height, x.width]); + + const { pixel_values, pixel_mask } = await this.processor(preparedImages); + const output = await this.model({ pixel_values, pixel_mask }); + + // @ts-ignore + const processed = this.processor.feature_extractor.post_process_object_detection(output, threshold, imageSizes); + + // Add labels + const id2label = this.model.config.id2label; + + // Format output + /** @type {ObjectDetectionPipelineOutput[]} */ + const result = processed.map(batch => ( + batch.boxes.map((box, i) => ({ + score: batch.scores[i], + label: id2label[batch.classes[i]], + box: get_bounding_box(box, !percentage), + })) + )) + + return isBatched ? result : result[0]; + } +} + + +/** + * @typedef {Object} ZeroShotObjectDetectionOutput + * @property {string} label Text query corresponding to the found object. + * @property {number} score Score corresponding to the object (between 0 and 1). + * @property {BoundingBox} box Bounding box of the detected object in image's original size, or as a percentage if `percentage` is set to true. + * + * @typedef {Object} ZeroShotObjectDetectionPipelineOptions Parameters specific to zero-shot object detection pipelines. + * @property {number} [threshold=0.1] The probability necessary to make a prediction. + * @property {number} [top_k=null] The number of top predictions that will be returned by the pipeline. + * If the provided number is `null` or higher than the number of predictions available, it will default + * to the number of predictions. + * @property {boolean} [percentage=false] Whether to return the boxes coordinates in percentage (true) or in pixels (false). + * + * @callback ZeroShotObjectDetectionPipelineCallback Detect objects (bounding boxes & classes) in the image(s) passed as inputs. + * @param {ImagePipelineInputs} images The input images. + * @param {string[]} candidate_labels What the model should recognize in the image. + * @param {ZeroShotObjectDetectionPipelineOptions} [options] The options to use for zero-shot object detection. + * @returns {Promise} An array of objects containing the predicted labels, scores, and bounding boxes. + * + * @typedef {TextImagePipelineConstructorArgs & ZeroShotObjectDetectionPipelineCallback & Disposable} ZeroShotObjectDetectionPipelineType + */ + +/** + * Zero-shot object detection pipeline. This pipeline predicts bounding boxes of + * objects when you provide an image and a set of `candidate_labels`. + * + * **Example:** Zero-shot object detection w/ `Xenova/owlvit-base-patch32`. + * ```javascript + * const detector = await pipeline('zero-shot-object-detection', 'Xenova/owlvit-base-patch32'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/astronaut.png'; + * const candidate_labels = ['human face', 'rocket', 'helmet', 'american flag']; + * const output = await detector(url, candidate_labels); + * // [ + * // { + * // score: 0.24392342567443848, + * // label: 'human face', + * // box: { xmin: 180, ymin: 67, xmax: 274, ymax: 175 } + * // }, + * // { + * // score: 0.15129457414150238, + * // label: 'american flag', + * // box: { xmin: 0, ymin: 4, xmax: 106, ymax: 513 } + * // }, + * // { + * // score: 0.13649864494800568, + * // label: 'helmet', + * // box: { xmin: 277, ymin: 337, xmax: 511, ymax: 511 } + * // }, + * // { + * // score: 0.10262022167444229, + * // label: 'rocket', + * // box: { xmin: 352, ymin: -1, xmax: 463, ymax: 287 } + * // } + * // ] + * ``` + * + * **Example:** Zero-shot object detection w/ `Xenova/owlvit-base-patch32` (returning top 4 matches and setting a threshold). + * ```javascript + * const detector = await pipeline('zero-shot-object-detection', 'Xenova/owlvit-base-patch32'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/beach.png'; + * const candidate_labels = ['hat', 'book', 'sunglasses', 'camera']; + * const output = await detector(url, candidate_labels, { top_k: 4, threshold: 0.05 }); + * // [ + * // { + * // score: 0.1606510728597641, + * // label: 'sunglasses', + * // box: { xmin: 347, ymin: 229, xmax: 429, ymax: 264 } + * // }, + * // { + * // score: 0.08935828506946564, + * // label: 'hat', + * // box: { xmin: 38, ymin: 174, xmax: 258, ymax: 364 } + * // }, + * // { + * // score: 0.08530698716640472, + * // label: 'camera', + * // box: { xmin: 187, ymin: 350, xmax: 260, ymax: 411 } + * // }, + * // { + * // score: 0.08349756896495819, + * // label: 'book', + * // box: { xmin: 261, ymin: 280, xmax: 494, ymax: 425 } + * // } + * // ] + * ``` + */ +class ZeroShotObjectDetectionPipeline extends (/** @type {new (options: TextImagePipelineConstructorArgs) => ZeroShotObjectDetectionPipelineType} */ (Pipeline)) { + + /** + * Create a new ZeroShotObjectDetectionPipeline. + * @param {TextImagePipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {ZeroShotObjectDetectionPipelineCallback} */ + async _call(images, candidate_labels, { + threshold = 0.1, + top_k = null, + percentage = false, + } = {}) { + + const isBatched = Array.isArray(images); + const preparedImages = await prepareImages(images); + + // Run tokenization + const text_inputs = this.tokenizer(candidate_labels, { + padding: true, + truncation: true, + }); + + // Run processor + const model_inputs = await this.processor(preparedImages); + + // Since non-maximum suppression is performed for exporting, we need to + // process each image separately. For more information, see: + // https://github.com/huggingface/optimum/blob/e3b7efb1257c011db907ef40ab340e795cc5684c/optimum/exporters/onnx/model_configs.py#L1028-L1032 + const toReturn = []; + for (let i = 0; i < preparedImages.length; ++i) { + const image = preparedImages[i]; + const imageSize = percentage ? null : [[image.height, image.width]]; + const pixel_values = model_inputs.pixel_values[i].unsqueeze_(0); + + // Run model with both text and pixel inputs + const output = await this.model({ ...text_inputs, pixel_values }); + + // @ts-ignore + const processed = this.processor.feature_extractor.post_process_object_detection(output, threshold, imageSize, true)[0]; + let result = processed.boxes.map((box, i) => ({ + score: processed.scores[i], + label: candidate_labels[processed.classes[i]], + box: get_bounding_box(box, !percentage), + })).sort((a, b) => b.score - a.score); + if (top_k !== null) { + result = result.slice(0, top_k); + } + toReturn.push(result) + } + + return isBatched ? toReturn : toReturn[0]; + } +} + +/** + * @typedef {Object} DocumentQuestionAnsweringSingle + * @property {string} answer The generated text. + * @typedef {DocumentQuestionAnsweringSingle[]} DocumentQuestionAnsweringOutput + * + * @callback DocumentQuestionAnsweringPipelineCallback Answer the question given as input by using the document. + * @param {ImageInput} image The image of the document to use. + * @param {string} question A question to ask of the document. + * @param {Partial} [options] Additional keyword arguments to pass along to the generate method of the model. + * @returns {Promise} An object (or array of objects) containing the answer(s). + * + * @typedef {TextImagePipelineConstructorArgs & DocumentQuestionAnsweringPipelineCallback & Disposable} DocumentQuestionAnsweringPipelineType + */ + +/** + * Document Question Answering pipeline using any `AutoModelForDocumentQuestionAnswering`. + * The inputs/outputs are similar to the (extractive) question answering pipeline; however, + * the pipeline takes an image (and optional OCR'd words/boxes) as input instead of text context. + * + * **Example:** Answer questions about a document with `Xenova/donut-base-finetuned-docvqa`. + * ```javascript + * const qa_pipeline = await pipeline('document-question-answering', 'Xenova/donut-base-finetuned-docvqa'); + * const image = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/invoice.png'; + * const question = 'What is the invoice number?'; + * const output = await qa_pipeline(image, question); + * // [{ answer: 'us-001' }] + * ``` + */ +class DocumentQuestionAnsweringPipeline extends (/** @type {new (options: TextImagePipelineConstructorArgs) => DocumentQuestionAnsweringPipelineType} */ (Pipeline)) { + + /** + * Create a new DocumentQuestionAnsweringPipeline. + * @param {TextImagePipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {DocumentQuestionAnsweringPipelineCallback} */ + async _call(image, question, generate_kwargs = {}) { + + // NOTE: For now, we only support a batch size of 1 + + // Preprocess image + const preparedImage = (await prepareImages(image))[0]; + const { pixel_values } = await this.processor(preparedImage); + + // Run tokenization + const task_prompt = `${question}`; + const decoder_input_ids = this.tokenizer(task_prompt, { + add_special_tokens: false, + padding: true, + truncation: true, + }).input_ids; + + // Run model + const output = await this.model.generate({ + inputs: pixel_values, + max_length: this.model.config.decoder.max_position_embeddings, + decoder_input_ids, + ...generate_kwargs, + }); + + // Decode output + const decoded = this.tokenizer.batch_decode(/** @type {Tensor} */(output))[0]; + + // Parse answer + const match = decoded.match(/(.*?)<\/s_answer>/); + let answer = null; + if (match && match.length >= 2) { + answer = match[1].trim(); + } + return [{ answer }]; + } +} + + +/** + * @typedef {Object} VocoderOptions + * @property {PreTrainedModel} [vocoder] The vocoder used by the pipeline (if the model uses one). If not provided, use the default HifiGan vocoder. + * @typedef {TextAudioPipelineConstructorArgs & VocoderOptions} TextToAudioPipelineConstructorArgs + */ + +/** + * @typedef {Object} TextToAudioOutput + * @property {Float32Array} audio The generated audio waveform. + * @property {number} sampling_rate The sampling rate of the generated audio waveform. + * + * @typedef {Object} TextToAudioPipelineOptions Parameters specific to text-to-audio pipelines. + * @property {Tensor|Float32Array|string|URL} [speaker_embeddings=null] The speaker embeddings (if the model requires it). + * + * @callback TextToAudioPipelineCallback Generates speech/audio from the inputs. + * @param {string|string[]} texts The text(s) to generate. + * @param {TextToAudioPipelineOptions} options Parameters passed to the model generation/forward method. + * @returns {Promise} An object containing the generated audio and sampling rate. + * + * @typedef {TextToAudioPipelineConstructorArgs & TextToAudioPipelineCallback & Disposable} TextToAudioPipelineType + */ + +/** + * Text-to-audio generation pipeline using any `AutoModelForTextToWaveform` or `AutoModelForTextToSpectrogram`. + * This pipeline generates an audio file from an input text and optional other conditional inputs. + * + * **Example:** Generate audio from text with `Xenova/speecht5_tts`. + * ```javascript + * const synthesizer = await pipeline('text-to-speech', 'Xenova/speecht5_tts', { quantized: false }); + * const speaker_embeddings = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/speaker_embeddings.bin'; + * const out = await synthesizer('Hello, my dog is cute', { speaker_embeddings }); + * // { + * // audio: Float32Array(26112) [-0.00005657337896991521, 0.00020583874720614403, ...], + * // sampling_rate: 16000 + * // } + * ``` + * + * You can then save the audio to a .wav file with the `wavefile` package: + * ```javascript + * import wavefile from 'wavefile'; + * import fs from 'fs'; + * + * const wav = new wavefile.WaveFile(); + * wav.fromScratch(1, out.sampling_rate, '32f', out.audio); + * fs.writeFileSync('out.wav', wav.toBuffer()); + * ``` + * + * **Example:** Multilingual speech generation with `Xenova/mms-tts-fra`. See [here](https://huggingface.co/models?pipeline_tag=text-to-speech&other=vits&sort=trending) for the full list of available languages (1107). + * ```javascript + * const synthesizer = await pipeline('text-to-speech', 'Xenova/mms-tts-fra'); + * const out = await synthesizer('Bonjour'); + * // { + * // audio: Float32Array(23808) [-0.00037693005288019776, 0.0003325853613205254, ...], + * // sampling_rate: 16000 + * // } + * ``` + */ +class TextToAudioPipeline extends (/** @type {new (options: TextToAudioPipelineConstructorArgs) => TextToAudioPipelineType} */ (Pipeline)) { + DEFAULT_VOCODER_ID = "Xenova/speecht5_hifigan" + + /** + * Create a new TextToAudioPipeline. + * @param {TextToAudioPipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + + // TODO: Find a better way for `pipeline` to set the default vocoder + this.vocoder = options.vocoder ?? null; + } + + + /** @type {TextToAudioPipelineCallback} */ + async _call(text_inputs, { + speaker_embeddings = null, + } = {}) { + + // If this.processor is not set, we are using a `AutoModelForTextToWaveform` model + if (this.processor) { + return this._call_text_to_spectrogram(text_inputs, { speaker_embeddings }); + } else { + return this._call_text_to_waveform(text_inputs); + } + } + + async _call_text_to_waveform(text_inputs) { + + // Run tokenization + const inputs = this.tokenizer(text_inputs, { + padding: true, + truncation: true, + }); + + // Generate waveform + const { waveform } = await this.model(inputs); + + const sampling_rate = this.model.config.sampling_rate; + return { + audio: waveform.data, + sampling_rate, + } + } + + async _call_text_to_spectrogram(text_inputs, { speaker_embeddings }) { + + // Load vocoder, if not provided + if (!this.vocoder) { + console.log('No vocoder specified, using default HifiGan vocoder.'); + this.vocoder = await _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModel.from_pretrained(this.DEFAULT_VOCODER_ID, { dtype: 'fp32' }); + } + + // Load speaker embeddings as Float32Array from path/URL + if (typeof speaker_embeddings === 'string' || speaker_embeddings instanceof URL) { + // Load from URL with fetch + speaker_embeddings = new Float32Array( + await (await fetch(speaker_embeddings)).arrayBuffer() + ); + } + + if (speaker_embeddings instanceof Float32Array) { + speaker_embeddings = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.Tensor( + 'float32', + speaker_embeddings, + [1, speaker_embeddings.length] + ) + } else if (!(speaker_embeddings instanceof _utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.Tensor)) { + throw new Error("Speaker embeddings must be a `Tensor`, `Float32Array`, `string`, or `URL`.") + } + + // Run tokenization + const { input_ids } = this.tokenizer(text_inputs, { + padding: true, + truncation: true, + }); + + // NOTE: At this point, we are guaranteed that `speaker_embeddings` is a `Tensor` + // @ts-ignore + const { waveform } = await this.model.generate_speech(input_ids, speaker_embeddings, { vocoder: this.vocoder }); + + const sampling_rate = this.processor.feature_extractor.config.sampling_rate; + return { + audio: waveform.data, + sampling_rate, + } + } +} + +/** + * @callback ImageToImagePipelineCallback Transform the image(s) passed as inputs. + * @param {ImagePipelineInputs} images The images to transform. + * @returns {Promise} The transformed image or list of images. + * + * @typedef {ImagePipelineConstructorArgs & ImageToImagePipelineCallback & Disposable} ImageToImagePipelineType + */ + +/** + * Image to Image pipeline using any `AutoModelForImageToImage`. This pipeline generates an image based on a previous image input. + * + * **Example:** Super-resolution w/ `Xenova/swin2SR-classical-sr-x2-64` + * ```javascript + * const upscaler = await pipeline('image-to-image', 'Xenova/swin2SR-classical-sr-x2-64'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/butterfly.jpg'; + * const output = await upscaler(url); + * // RawImage { + * // data: Uint8Array(786432) [ 41, 31, 24, 43, ... ], + * // width: 512, + * // height: 512, + * // channels: 3 + * // } + * ``` + */ +class ImageToImagePipeline extends (/** @type {new (options: ImagePipelineConstructorArgs) => ImageToImagePipelineType} */ (Pipeline)) { + /** + * Create a new ImageToImagePipeline. + * @param {ImagePipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {ImageToImagePipelineCallback} */ + async _call(images) { + + const preparedImages = await prepareImages(images); + const inputs = await this.processor(preparedImages); + const outputs = await this.model(inputs); + + /** @type {RawImage[]} */ + const toReturn = []; + for (const batch of outputs.reconstruction) { + const output = batch.squeeze().clamp_(0, 1).mul_(255).round_().to('uint8'); + toReturn.push(_utils_image_js__WEBPACK_IMPORTED_MODULE_8__.RawImage.fromTensor(output)); + } + + return toReturn.length > 1 ? toReturn : toReturn[0]; + } +} + +/** + * @typedef {Object} DepthEstimationPipelineOutput + * @property {Tensor} predicted_depth The raw depth map predicted by the model. + * @property {RawImage} depth The processed depth map as an image (with the same size as the input image). + * + * @callback DepthEstimationPipelineCallback Predicts the depth for the image(s) passed as inputs. + * @param {ImagePipelineInputs} images The images to compute depth for. + * @returns {Promise} An image or a list of images containing result(s). + * + * @typedef {ImagePipelineConstructorArgs & DepthEstimationPipelineCallback & Disposable} DepthEstimationPipelineType + */ + +/** + * Depth estimation pipeline using any `AutoModelForDepthEstimation`. This pipeline predicts the depth of an image. + * + * **Example:** Depth estimation w/ `Xenova/dpt-hybrid-midas` + * ```javascript + * const depth_estimator = await pipeline('depth-estimation', 'Xenova/dpt-hybrid-midas'); + * const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/cats.jpg'; + * const out = await depth_estimator(url); + * // { + * // predicted_depth: Tensor { + * // dims: [ 384, 384 ], + * // type: 'float32', + * // data: Float32Array(147456) [ 542.859130859375, 545.2833862304688, 546.1649169921875, ... ], + * // size: 147456 + * // }, + * // depth: RawImage { + * // data: Uint8Array(307200) [ 86, 86, 86, ... ], + * // width: 640, + * // height: 480, + * // channels: 1 + * // } + * // } + * ``` + */ +class DepthEstimationPipeline extends (/** @type {new (options: ImagePipelineConstructorArgs) => DepthEstimationPipelineType} */ (Pipeline)) { + /** + * Create a new DepthEstimationPipeline. + * @param {ImagePipelineConstructorArgs} options An object used to instantiate the pipeline. + */ + constructor(options) { + super(options); + } + + /** @type {DepthEstimationPipelineCallback} */ + async _call(images) { + + const preparedImages = await prepareImages(images); + + const inputs = await this.processor(preparedImages); + const { predicted_depth } = await this.model(inputs); + + const toReturn = []; + for (let i = 0; i < preparedImages.length; ++i) { + const prediction = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_7__.interpolate)(predicted_depth[i], preparedImages[i].size.reverse(), 'bilinear', false); + const formatted = prediction.mul_(255 / (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_5__.max)(prediction.data)[0]).to('uint8'); + toReturn.push({ + predicted_depth: predicted_depth[i], + depth: _utils_image_js__WEBPACK_IMPORTED_MODULE_8__.RawImage.fromTensor(formatted), + }); + } + + return toReturn.length > 1 ? toReturn : toReturn[0]; + } +} + +const SUPPORTED_TASKS = Object.freeze({ + "text-classification": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": TextClassificationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSequenceClassification, + "default": { + // TODO: replace with original + // "model": "distilbert-base-uncased-finetuned-sst-2-english", + "model": "Xenova/distilbert-base-uncased-finetuned-sst-2-english", + }, + "type": "text", + }, + "token-classification": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": TokenClassificationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForTokenClassification, + "default": { + // TODO: replace with original + // "model": "Davlan/bert-base-multilingual-cased-ner-hrl", + "model": "Xenova/bert-base-multilingual-cased-ner-hrl", + }, + "type": "text", + }, + "question-answering": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": QuestionAnsweringPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForQuestionAnswering, + "default": { + // TODO: replace with original + // "model": "distilbert-base-cased-distilled-squad", + "model": "Xenova/distilbert-base-cased-distilled-squad", + }, + "type": "text", + }, + + "fill-mask": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": FillMaskPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForMaskedLM, + "default": { + // TODO: replace with original + // "model": "bert-base-uncased", + "model": "Xenova/bert-base-uncased", + }, + "type": "text", + }, + "summarization": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": SummarizationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSeq2SeqLM, + "default": { + // TODO: replace with original + // "model": "sshleifer/distilbart-cnn-6-6", + "model": "Xenova/distilbart-cnn-6-6", + }, + "type": "text", + }, + "translation": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": TranslationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSeq2SeqLM, + "default": { + // TODO: replace with original + // "model": "t5-small", + "model": "Xenova/t5-small", + }, + "type": "text", + }, + "text2text-generation": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": Text2TextGenerationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSeq2SeqLM, + "default": { + // TODO: replace with original + // "model": "google/flan-t5-small", + "model": "Xenova/flan-t5-small", + }, + "type": "text", + }, + "text-generation": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": TextGenerationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForCausalLM, + "default": { + // TODO: replace with original + // "model": "gpt2", + "model": "Xenova/gpt2", + }, + "type": "text", + }, + "zero-shot-classification": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": ZeroShotClassificationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSequenceClassification, + "default": { + // TODO: replace with original + // "model": "typeform/distilbert-base-uncased-mnli", + "model": "Xenova/distilbert-base-uncased-mnli", + }, + "type": "text", + }, + "audio-classification": { + "pipeline": AudioClassificationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForAudioClassification, + "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "superb/wav2vec2-base-superb-ks", + "model": "Xenova/wav2vec2-base-superb-ks", + }, + "type": "audio", + }, + "zero-shot-audio-classification": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": ZeroShotAudioClassificationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModel, + "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "laion/clap-htsat-fused", + "model": "Xenova/clap-htsat-unfused", + }, + "type": "multimodal", + }, + "automatic-speech-recognition": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": AutomaticSpeechRecognitionPipeline, + "model": [_models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSpeechSeq2Seq, _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForCTC], + "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "openai/whisper-tiny.en", + "model": "Xenova/whisper-tiny.en", + }, + "type": "multimodal", + }, + "text-to-audio": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": TextToAudioPipeline, + "model": [_models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForTextToWaveform, _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForTextToSpectrogram], + "processor": [_processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, /* Some don't use a processor */ null], + "default": { + // TODO: replace with original + // "model": "microsoft/speecht5_tts", + "model": "Xenova/speecht5_tts", + }, + "type": "text", + }, + "image-to-text": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": ImageToTextPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForVision2Seq, + "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "nlpconnect/vit-gpt2-image-captioning", + "model": "Xenova/vit-gpt2-image-captioning", + }, + "type": "multimodal", + }, + + "image-classification": { + // no tokenizer + "pipeline": ImageClassificationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForImageClassification, + "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "google/vit-base-patch16-224", + "model": "Xenova/vit-base-patch16-224", + }, + "type": "multimodal", + }, + + "image-segmentation": { + // no tokenizer + "pipeline": ImageSegmentationPipeline, + "model": [_models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForImageSegmentation, _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForSemanticSegmentation, _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForUniversalSegmentation], + "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "facebook/detr-resnet-50-panoptic", + "model": "Xenova/detr-resnet-50-panoptic", + }, + "type": "multimodal", + }, + + "zero-shot-image-classification": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": ZeroShotImageClassificationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModel, + "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "openai/clip-vit-base-patch32", + "model": "Xenova/clip-vit-base-patch32", + }, + "type": "multimodal", + }, + + "object-detection": { + // no tokenizer + "pipeline": ObjectDetectionPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForObjectDetection, + "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "facebook/detr-resnet-50", + "model": "Xenova/detr-resnet-50", + }, + "type": "multimodal", + }, + "zero-shot-object-detection": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": ZeroShotObjectDetectionPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForZeroShotObjectDetection, + "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "google/owlvit-base-patch32", + "model": "Xenova/owlvit-base-patch32", + }, + "type": "multimodal", + }, + "document-question-answering": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": DocumentQuestionAnsweringPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForDocumentQuestionAnswering, + "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "naver-clova-ix/donut-base-finetuned-docvqa", + "model": "Xenova/donut-base-finetuned-docvqa", + }, + "type": "multimodal", + }, + "image-to-image": { + // no tokenizer + "pipeline": ImageToImagePipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForImageToImage, + "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "caidas/swin2SR-classical-sr-x2-64", + "model": "Xenova/swin2SR-classical-sr-x2-64", + }, + "type": "image", + }, + "depth-estimation": { + // no tokenizer + "pipeline": DepthEstimationPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForDepthEstimation, + "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "default": { + // TODO: replace with original + // "model": "Intel/dpt-large", + "model": "Xenova/dpt-large", + }, + "type": "image", + }, + + // This task serves as a useful interface for dealing with sentence-transformers (https://huggingface.co/sentence-transformers). + "feature-extraction": { + "tokenizer": _tokenizers_js__WEBPACK_IMPORTED_MODULE_0__.AutoTokenizer, + "pipeline": FeatureExtractionPipeline, + "model": _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModel, + "default": { + // TODO: replace with original + // "model": "sentence-transformers/all-MiniLM-L6-v2", + "model": "Xenova/all-MiniLM-L6-v2", + }, + "type": "text", + }, + "image-feature-extraction": { + "processor": _processors_js__WEBPACK_IMPORTED_MODULE_2__.AutoProcessor, + "pipeline": ImageFeatureExtractionPipeline, + "model": [_models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModelForImageFeatureExtraction, _models_js__WEBPACK_IMPORTED_MODULE_1__.AutoModel], + "default": { + // TODO: replace with original + // "model": "google/vit-base-patch16-224", + "model": "Xenova/vit-base-patch16-224-in21k", + }, + "type": "image", + }, +}) + + +// TODO: Add types for TASK_ALIASES +const TASK_ALIASES = Object.freeze({ + "sentiment-analysis": "text-classification", + "ner": "token-classification", + // "vqa": "visual-question-answering", // TODO: Add + "asr": "automatic-speech-recognition", + "text-to-speech": "text-to-audio", + + // Add for backwards compatibility + "embeddings": "feature-extraction", +}); + +/** + * @typedef {keyof typeof SUPPORTED_TASKS} TaskType + * @typedef {keyof typeof TASK_ALIASES} AliasType + * @typedef {TaskType | AliasType} PipelineType All possible pipeline types. + * @typedef {{[K in TaskType]: InstanceType}} SupportedTasks A mapping of pipeline names to their corresponding pipeline classes. + * @typedef {{[K in AliasType]: InstanceType}} AliasTasks A mapping from pipeline aliases to their corresponding pipeline classes. + * @typedef {SupportedTasks & AliasTasks} AllTasks A mapping from all pipeline names and aliases to their corresponding pipeline classes. + */ + +/** + * Utility factory method to build a `Pipeline` object. + * + * @template {PipelineType} T The type of pipeline to return. + * @param {T} task The task defining which pipeline will be returned. Currently accepted tasks are: + * - `"audio-classification"`: will return a `AudioClassificationPipeline`. + * - `"automatic-speech-recognition"`: will return a `AutomaticSpeechRecognitionPipeline`. + * - `"depth-estimation"`: will return a `DepthEstimationPipeline`. + * - `"document-question-answering"`: will return a `DocumentQuestionAnsweringPipeline`. + * - `"feature-extraction"`: will return a `FeatureExtractionPipeline`. + * - `"fill-mask"`: will return a `FillMaskPipeline`. + * - `"image-classification"`: will return a `ImageClassificationPipeline`. + * - `"image-segmentation"`: will return a `ImageSegmentationPipeline`. + * - `"image-to-text"`: will return a `ImageToTextPipeline`. + * - `"object-detection"`: will return a `ObjectDetectionPipeline`. + * - `"question-answering"`: will return a `QuestionAnsweringPipeline`. + * - `"summarization"`: will return a `SummarizationPipeline`. + * - `"text2text-generation"`: will return a `Text2TextGenerationPipeline`. + * - `"text-classification"` (alias "sentiment-analysis" available): will return a `TextClassificationPipeline`. + * - `"text-generation"`: will return a `TextGenerationPipeline`. + * - `"token-classification"` (alias "ner" available): will return a `TokenClassificationPipeline`. + * - `"translation"`: will return a `TranslationPipeline`. + * - `"translation_xx_to_yy"`: will return a `TranslationPipeline`. + * - `"zero-shot-classification"`: will return a `ZeroShotClassificationPipeline`. + * - `"zero-shot-audio-classification"`: will return a `ZeroShotAudioClassificationPipeline`. + * - `"zero-shot-image-classification"`: will return a `ZeroShotImageClassificationPipeline`. + * - `"zero-shot-object-detection"`: will return a `ZeroShotObjectDetectionPipeline`. + * @param {string} [model=null] The name of the pre-trained model to use. If not specified, the default model for the task will be used. + * @param {import('./utils/hub.js').PretrainedModelOptions} [options] Optional parameters for the pipeline. + * @returns {Promise} A Pipeline object for the specified task. + * @throws {Error} If an unsupported pipeline is requested. + */ +async function pipeline( + task, + model = null, + { + progress_callback = null, + config = null, + cache_dir = null, + local_files_only = false, + revision = 'main', + device = null, + dtype = null, + model_file_name = null, + session_options = {}, + } = {} +) { + // Helper method to construct pipeline + + // Apply aliases + // @ts-ignore + task = TASK_ALIASES[task] ?? task; + + // Get pipeline info + const pipelineInfo = SUPPORTED_TASKS[task.split('_', 1)[0]]; + if (!pipelineInfo) { + throw Error(`Unsupported pipeline: ${task}. Must be one of [${Object.keys(SUPPORTED_TASKS)}]`) + } + + // Use model if specified, otherwise, use default + if (!model) { + model = pipelineInfo.default.model + console.log(`No model specified. Using default model: "${model}".`); + } + + const pretrainedOptions = { + progress_callback, + config, + cache_dir, + local_files_only, + revision, + device, + dtype, + model_file_name, + session_options, + } + + const classes = new Map([ + ['tokenizer', pipelineInfo.tokenizer], + ['model', pipelineInfo.model], + ['processor', pipelineInfo.processor], + ]); + + // Load model, tokenizer, and processor (if they exist) + const results = await loadItems(classes, model, pretrainedOptions); + results.task = task; + + (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_4__.dispatchCallback)(progress_callback, { + 'status': 'ready', + 'task': task, + 'model': model, + }); + + const pipelineClass = pipelineInfo.pipeline; + return new pipelineClass(results); +} + + +/** + * Helper function to get applicable model, tokenizer, or processor classes for a given model. + * @param {Map} mapping The mapping of names to classes, arrays of classes, or null. + * @param {string} model The name of the model to load. + * @param {import('./utils/hub.js').PretrainedOptions} pretrainedOptions The options to pass to the `from_pretrained` method. + * @private + */ +async function loadItems(mapping, model, pretrainedOptions) { + + const result = Object.create(null); + + /**@type {Promise[]} */ + const promises = []; + for (const [name, cls] of mapping.entries()) { + if (!cls) continue; + + /**@type {Promise} */ + let promise; + if (Array.isArray(cls)) { + promise = new Promise(async (resolve, reject) => { + let e; + for (const c of cls) { + if (c === null) { + // If null, we resolve it immediately, meaning the relevant + // class was not found, but it is optional. + resolve(null); + return; + } + try { + resolve(await c.from_pretrained(model, pretrainedOptions)); + return; + } catch (err) { + if (err.message?.includes('Unsupported model type')) { + // If the error is due to an unsupported model type, we + // save the error and try the next class. + e = err; + } else if (err.message?.includes('Could not locate file')) { + e = err; + } else { + reject(err); + return; + } + + } + } + reject(e); + }) + } else { + promise = cls.from_pretrained(model, pretrainedOptions); + } + + result[name] = promise; + promises.push(promise); + } + + // Wait for all promises to resolve (in parallel) + await Promise.all(promises); + + // Then assign to result + for (const [name, promise] of Object.entries(result)) { + result[name] = await promise; + } + + return result; +} + +/***/ }), + +/***/ "./src/processors.js": +/*!***************************!*\ + !*** ./src/processors.js ***! + \***************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ ASTFeatureExtractor: () => (/* binding */ ASTFeatureExtractor), +/* harmony export */ AutoProcessor: () => (/* binding */ AutoProcessor), +/* harmony export */ BeitFeatureExtractor: () => (/* binding */ BeitFeatureExtractor), +/* harmony export */ BitImageProcessor: () => (/* binding */ BitImageProcessor), +/* harmony export */ CLIPFeatureExtractor: () => (/* binding */ CLIPFeatureExtractor), +/* harmony export */ CLIPImageProcessor: () => (/* binding */ CLIPImageProcessor), +/* harmony export */ ChineseCLIPFeatureExtractor: () => (/* binding */ ChineseCLIPFeatureExtractor), +/* harmony export */ ClapFeatureExtractor: () => (/* binding */ ClapFeatureExtractor), +/* harmony export */ ConvNextFeatureExtractor: () => (/* binding */ ConvNextFeatureExtractor), +/* harmony export */ ConvNextImageProcessor: () => (/* binding */ ConvNextImageProcessor), +/* harmony export */ DPTFeatureExtractor: () => (/* binding */ DPTFeatureExtractor), +/* harmony export */ DPTImageProcessor: () => (/* binding */ DPTImageProcessor), +/* harmony export */ DeiTFeatureExtractor: () => (/* binding */ DeiTFeatureExtractor), +/* harmony export */ DetrFeatureExtractor: () => (/* binding */ DetrFeatureExtractor), +/* harmony export */ DonutFeatureExtractor: () => (/* binding */ DonutFeatureExtractor), +/* harmony export */ DonutImageProcessor: () => (/* binding */ DonutImageProcessor), +/* harmony export */ EfficientNetImageProcessor: () => (/* binding */ EfficientNetImageProcessor), +/* harmony export */ FeatureExtractor: () => (/* binding */ FeatureExtractor), +/* harmony export */ Florence2Processor: () => (/* binding */ Florence2Processor), +/* harmony export */ GLPNFeatureExtractor: () => (/* binding */ GLPNFeatureExtractor), +/* harmony export */ ImageFeatureExtractor: () => (/* binding */ ImageFeatureExtractor), +/* harmony export */ MaskFormerFeatureExtractor: () => (/* binding */ MaskFormerFeatureExtractor), +/* harmony export */ MobileNetV1FeatureExtractor: () => (/* binding */ MobileNetV1FeatureExtractor), +/* harmony export */ MobileNetV2FeatureExtractor: () => (/* binding */ MobileNetV2FeatureExtractor), +/* harmony export */ MobileNetV3FeatureExtractor: () => (/* binding */ MobileNetV3FeatureExtractor), +/* harmony export */ MobileNetV4FeatureExtractor: () => (/* binding */ MobileNetV4FeatureExtractor), +/* harmony export */ MobileViTFeatureExtractor: () => (/* binding */ MobileViTFeatureExtractor), +/* harmony export */ MobileViTImageProcessor: () => (/* binding */ MobileViTImageProcessor), +/* harmony export */ NougatImageProcessor: () => (/* binding */ NougatImageProcessor), +/* harmony export */ OwlViTFeatureExtractor: () => (/* binding */ OwlViTFeatureExtractor), +/* harmony export */ OwlViTProcessor: () => (/* binding */ OwlViTProcessor), +/* harmony export */ Owlv2ImageProcessor: () => (/* binding */ Owlv2ImageProcessor), +/* harmony export */ Processor: () => (/* binding */ Processor), +/* harmony export */ PvtImageProcessor: () => (/* binding */ PvtImageProcessor), +/* harmony export */ PyAnnoteFeatureExtractor: () => (/* binding */ PyAnnoteFeatureExtractor), +/* harmony export */ PyAnnoteProcessor: () => (/* binding */ PyAnnoteProcessor), +/* harmony export */ RTDetrImageProcessor: () => (/* binding */ RTDetrImageProcessor), +/* harmony export */ SamImageProcessor: () => (/* binding */ SamImageProcessor), +/* harmony export */ SamProcessor: () => (/* binding */ SamProcessor), +/* harmony export */ SapiensFeatureExtractor: () => (/* binding */ SapiensFeatureExtractor), +/* harmony export */ SeamlessM4TFeatureExtractor: () => (/* binding */ SeamlessM4TFeatureExtractor), +/* harmony export */ SegformerFeatureExtractor: () => (/* binding */ SegformerFeatureExtractor), +/* harmony export */ SiglipImageProcessor: () => (/* binding */ SiglipImageProcessor), +/* harmony export */ SpeechT5FeatureExtractor: () => (/* binding */ SpeechT5FeatureExtractor), +/* harmony export */ SpeechT5Processor: () => (/* binding */ SpeechT5Processor), +/* harmony export */ Swin2SRImageProcessor: () => (/* binding */ Swin2SRImageProcessor), +/* harmony export */ ViTFeatureExtractor: () => (/* binding */ ViTFeatureExtractor), +/* harmony export */ ViTImageProcessor: () => (/* binding */ ViTImageProcessor), +/* harmony export */ VitMatteImageProcessor: () => (/* binding */ VitMatteImageProcessor), +/* harmony export */ Wav2Vec2FeatureExtractor: () => (/* binding */ Wav2Vec2FeatureExtractor), +/* harmony export */ Wav2Vec2ProcessorWithLM: () => (/* binding */ Wav2Vec2ProcessorWithLM), +/* harmony export */ WeSpeakerFeatureExtractor: () => (/* binding */ WeSpeakerFeatureExtractor), +/* harmony export */ WhisperFeatureExtractor: () => (/* binding */ WhisperFeatureExtractor), +/* harmony export */ WhisperProcessor: () => (/* binding */ WhisperProcessor), +/* harmony export */ YolosFeatureExtractor: () => (/* binding */ YolosFeatureExtractor) +/* harmony export */ }); +/* harmony import */ var _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./utils/generic.js */ "./src/utils/generic.js"); +/* harmony import */ var _utils_core_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./utils/core.js */ "./src/utils/core.js"); +/* harmony import */ var _utils_hub_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./utils/hub.js */ "./src/utils/hub.js"); +/* harmony import */ var _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./utils/maths.js */ "./src/utils/maths.js"); +/* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ./utils/tensor.js */ "./src/utils/tensor.js"); +/* harmony import */ var _utils_image_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ./utils/image.js */ "./src/utils/image.js"); +/* harmony import */ var _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! ./utils/audio.js */ "./src/utils/audio.js"); + +/** + * @file Processors are used to prepare non-textual inputs (e.g., image or audio) for a model. + * + * **Example:** Using a `WhisperProcessor` to prepare an audio input for a model. + * ```javascript + * import { AutoProcessor, read_audio } from '@huggingface/transformers'; + * + * let processor = await AutoProcessor.from_pretrained('openai/whisper-tiny.en'); + * let audio = await read_audio('https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac', 16000); + * let { input_features } = await processor(audio); + * // Tensor { + * // data: Float32Array(240000) [0.4752984642982483, 0.5597258806228638, 0.56434166431427, ...], + * // dims: [1, 80, 3000], + * // type: 'float32', + * // size: 240000, + * // } + * ``` + * + * @module processors + */ + + + + + + + + + + + + + + + +// Helper functions + +/** + * Converts bounding boxes from center format to corners format. + * + * @param {number[]} arr The coordinate for the center of the box and its width, height dimensions (center_x, center_y, width, height) + * @returns {number[]} The coodinates for the top-left and bottom-right corners of the box (top_left_x, top_left_y, bottom_right_x, bottom_right_y) + */ +function center_to_corners_format([centerX, centerY, width, height]) { + return [ + centerX - width / 2, + centerY - height / 2, + centerX + width / 2, + centerY + height / 2 + ]; +} + +/** + * Post-processes the outputs of the model (for object detection). + * @param {Object} outputs The outputs of the model that must be post-processed + * @param {Tensor} outputs.logits The logits + * @param {Tensor} outputs.pred_boxes The predicted boxes. + * @param {number} [threshold=0.5] The threshold to use for the scores. + * @param {[number, number][]} [target_sizes=null] The sizes of the original images. + * @param {boolean} [is_zero_shot=false] Whether zero-shot object detection was performed. + * @return {Object[]} An array of objects containing the post-processed outputs. + * @private + */ +function post_process_object_detection(outputs, threshold = 0.5, target_sizes = null, is_zero_shot = false) { + const out_logits = outputs.logits; + const out_bbox = outputs.pred_boxes; + const [batch_size, num_boxes, num_classes] = out_logits.dims; + + if (target_sizes !== null && target_sizes.length !== batch_size) { + throw Error("Make sure that you pass in as many target sizes as the batch dimension of the logits") + } + let toReturn = []; + for (let i = 0; i < batch_size; ++i) { + let target_size = target_sizes !== null ? target_sizes[i] : null; + let info = { + boxes: [], + classes: [], + scores: [] + } + let logits = out_logits[i]; + let bbox = out_bbox[i]; + + for (let j = 0; j < num_boxes; ++j) { + let logit = logits[j]; + + let indices = []; + let probs; + if (is_zero_shot) { + // Get indices of classes with high enough probability + probs = logit.sigmoid().data; + for (let k = 0; k < probs.length; ++k) { + if (probs[k] > threshold) { + indices.push(k); + } + } + + } else { + // Get most probable class + let maxIndex = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.max)(logit.data)[1]; + + if (maxIndex === num_classes - 1) { + // This is the background class, skip it + continue; + } + // Compute softmax over classes + probs = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.softmax)(logit.data); + + if (probs[maxIndex] < threshold) { + continue; + } + indices.push(maxIndex); + } + + for (const index of indices) { + + // Some class has a high enough probability + /** @type {number[]} */ + let box = bbox[j].data; + + // convert to [x0, y0, x1, y1] format + box = center_to_corners_format(box) + if (target_size !== null) { + box = box.map((x, i) => x * target_size[(i + 1) % 2]) + } + + info.boxes.push(box); + info.classes.push(index); + info.scores.push(probs[index]); + } + } + toReturn.push(info); + } + return toReturn; +} + + +/** + * Post-processes the outputs of the model (for semantic segmentation). + * @param {*} outputs Raw outputs of the model. + * @param {[number, number][]} [target_sizes=null] List of tuples corresponding to the requested final size + * (height, width) of each prediction. If unset, predictions will not be resized. + * @returns {{segmentation: Tensor; labels: number[]}[]} The semantic segmentation maps. + */ +function post_process_semantic_segmentation(outputs, target_sizes = null) { + + const logits = outputs.logits; + const batch_size = logits.dims[0]; + + if (target_sizes !== null && target_sizes.length !== batch_size) { + throw Error("Make sure that you pass in as many target sizes as the batch dimension of the logits") + } + + const toReturn = []; + for (let i = 0; i < batch_size; ++i) { + const target_size = target_sizes !== null ? target_sizes[i] : null; + + let data = logits[i]; + + // 1. If target_size is not null, we need to resize the masks to the target size + if (target_size !== null) { + // resize the masks to the target size + data = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.interpolate)(data, target_size, 'bilinear', false); + } + const [height, width] = target_size ?? data.dims.slice(-2); + + const segmentation = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( + 'int32', + new Int32Array(height * width), + [height, width] + ); + + // Buffer to store current largest value + const buffer = data[0].data; + const segmentation_data = segmentation.data; + for (let j = 1; j < data.dims[0]; ++j) { + const row = data[j].data; + for (let k = 0; k < row.length; ++k) { + if (row[k] > buffer[k]) { + buffer[k] = row[k]; + segmentation_data[k] = j; + } + } + } + + // Store which objects have labels + // This is much more efficient that creating a set of the final values + const hasLabel = new Array(data.dims[0]); + for (let j = 0; j < segmentation_data.length; ++j) { + const index = segmentation_data[j]; + hasLabel[index] = index; + } + /** @type {number[]} The unique list of labels that were detected */ + const labels = hasLabel.filter(x => x !== undefined); + + toReturn.push({ segmentation, labels }); + } + return toReturn; +} + + +/** + * Binarize the given masks using `object_mask_threshold`, it returns the associated values of `masks`, `scores` and `labels`. + * @param {Tensor} class_logits The class logits. + * @param {Tensor} mask_logits The mask logits. + * @param {number} object_mask_threshold A number between 0 and 1 used to binarize the masks. + * @param {number} num_labels The number of labels. + * @returns {[Tensor[], number[], number[]]} The binarized masks, the scores, and the labels. + * @private + */ +function remove_low_and_no_objects(class_logits, mask_logits, object_mask_threshold, num_labels) { + + const mask_probs_item = []; + const pred_scores_item = []; + const pred_labels_item = []; + + for (let j = 0; j < class_logits.dims[0]; ++j) { + const cls = class_logits[j]; + const mask = mask_logits[j]; + + const pred_label = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.max)(cls.data)[1]; + if (pred_label === num_labels) { + // Is the background, so we ignore it + continue; + } + + const scores = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.softmax)(cls.data); + const pred_score = scores[pred_label]; + if (pred_score > object_mask_threshold) { + mask_probs_item.push(mask); + pred_scores_item.push(pred_score); + pred_labels_item.push(pred_label); + } + } + + return [mask_probs_item, pred_scores_item, pred_labels_item]; +} + +/** + * Checks whether the segment is valid or not. + * @param {Int32Array} mask_labels Labels for each pixel in the mask. + * @param {Tensor[]} mask_probs Probabilities for each pixel in the masks. + * @param {number} k The class id of the segment. + * @param {number} mask_threshold The mask threshold. + * @param {number} overlap_mask_area_threshold The overlap mask area threshold. + * @returns {[boolean, number[]]} Whether the segment is valid or not, and the indices of the valid labels. + * @private + */ +function check_segment_validity( + mask_labels, + mask_probs, + k, + mask_threshold = 0.5, + overlap_mask_area_threshold = 0.8 +) { + // mask_k is a 1D array of indices, indicating where the mask is equal to k + const mask_k = []; + let mask_k_area = 0; + let original_area = 0; + + const mask_probs_k_data = mask_probs[k].data; + + // Compute the area of all the stuff in query k + for (let i = 0; i < mask_labels.length; ++i) { + if (mask_labels[i] === k) { + mask_k.push(i); + ++mask_k_area; + } + + if (mask_probs_k_data[i] >= mask_threshold) { + ++original_area; + } + } + let mask_exists = mask_k_area > 0 && original_area > 0; + + // Eliminate disconnected tiny segments + if (mask_exists) { + // Perform additional check + let area_ratio = mask_k_area / original_area; + mask_exists = area_ratio > overlap_mask_area_threshold; + } + + return [mask_exists, mask_k] +} + +/** + * Computes the segments. + * @param {Tensor[]} mask_probs The mask probabilities. + * @param {number[]} pred_scores The predicted scores. + * @param {number[]} pred_labels The predicted labels. + * @param {number} mask_threshold The mask threshold. + * @param {number} overlap_mask_area_threshold The overlap mask area threshold. + * @param {Set} label_ids_to_fuse The label ids to fuse. + * @param {number[]} target_size The target size of the image. + * @returns {[Tensor, Array<{id: number, label_id: number, score: number}>]} The computed segments. + * @private + */ +function compute_segments( + mask_probs, + pred_scores, + pred_labels, + mask_threshold, + overlap_mask_area_threshold, + label_ids_to_fuse = null, + target_size = null, +) { + const [height, width] = target_size ?? mask_probs[0].dims; + + const segmentation = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( + 'int32', + new Int32Array(height * width), + [height, width] + ); + const segments = []; + + // 1. If target_size is not null, we need to resize the masks to the target size + if (target_size !== null) { + // resize the masks to the target size + for (let i = 0; i < mask_probs.length; ++i) { + mask_probs[i] = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.interpolate)(mask_probs[i], target_size, 'bilinear', false); + } + } + + // 2. Weigh each mask by its prediction score + // NOTE: `mask_probs` is updated in-place + // + // Temporary storage for the best label/scores for each pixel ([height, width]): + const mask_labels = new Int32Array(mask_probs[0].data.length); + const bestScores = new Float32Array(mask_probs[0].data.length); + + for (let i = 0; i < mask_probs.length; ++i) { + let score = pred_scores[i]; + + const mask_probs_i_data = mask_probs[i].data; + + for (let j = 0; j < mask_probs_i_data.length; ++j) { + mask_probs_i_data[j] *= score + if (mask_probs_i_data[j] > bestScores[j]) { + mask_labels[j] = i; + bestScores[j] = mask_probs_i_data[j]; + } + } + } + + let current_segment_id = 0; + + // let stuff_memory_list = {} + const segmentation_data = segmentation.data; + for (let k = 0; k < pred_labels.length; ++k) { + const pred_class = pred_labels[k]; + + // TODO add `should_fuse` + // let should_fuse = pred_class in label_ids_to_fuse + + // Check if mask exists and large enough to be a segment + const [mask_exists, mask_k] = check_segment_validity( + mask_labels, + mask_probs, + k, + mask_threshold, + overlap_mask_area_threshold + ) + + if (!mask_exists) { + // Nothing to see here + continue; + } + + // TODO + // if (pred_class in stuff_memory_list) { + // current_segment_id = stuff_memory_list[pred_class] + // } else { + // current_segment_id += 1; + // } + ++current_segment_id; + + + // Add current object segment to final segmentation map + for (const index of mask_k) { + segmentation_data[index] = current_segment_id; + } + + segments.push({ + id: current_segment_id, + label_id: pred_class, + // was_fused: should_fuse, TODO + score: pred_scores[k], + }) + + // TODO + // if(should_fuse){ + // stuff_memory_list[pred_class] = current_segment_id + // } + } + + return [segmentation, segments]; +} + + +/** + * Post-process the model output to generate the final panoptic segmentation. + * @param {*} outputs The model output to post process + * @param {number} [threshold=0.5] The probability score threshold to keep predicted instance masks. + * @param {number} [mask_threshold=0.5] Threshold to use when turning the predicted masks into binary values. + * @param {number} [overlap_mask_area_threshold=0.8] The overlap mask area threshold to merge or discard small disconnected parts within each binary instance mask. + * @param {Set} [label_ids_to_fuse=null] The labels in this state will have all their instances be fused together. + * @param {[number, number][]} [target_sizes=null] The target sizes to resize the masks to. + * @returns {Array<{ segmentation: Tensor, segments_info: Array<{id: number, label_id: number, score: number}>}>} + */ +function post_process_panoptic_segmentation( + outputs, + threshold = 0.5, + mask_threshold = 0.5, + overlap_mask_area_threshold = 0.8, + label_ids_to_fuse = null, + target_sizes = null, +) { + if (label_ids_to_fuse === null) { + console.warn("`label_ids_to_fuse` unset. No instance will be fused.") + label_ids_to_fuse = new Set(); + } + + const class_queries_logits = outputs.class_queries_logits ?? outputs.logits; // [batch_size, num_queries, num_classes+1] + const masks_queries_logits = outputs.masks_queries_logits ?? outputs.pred_masks; // [batch_size, num_queries, height, width] + + const mask_probs = masks_queries_logits.sigmoid() // [batch_size, num_queries, height, width] + + let [batch_size, num_queries, num_labels] = class_queries_logits.dims; + num_labels -= 1; // Remove last class (background) + + if (target_sizes !== null && target_sizes.length !== batch_size) { + throw Error("Make sure that you pass in as many target sizes as the batch dimension of the logits") + } + + let toReturn = []; + for (let i = 0; i < batch_size; ++i) { + let target_size = target_sizes !== null ? target_sizes[i] : null; + + let class_logits = class_queries_logits[i]; + let mask_logits = mask_probs[i]; + + let [mask_probs_item, pred_scores_item, pred_labels_item] = remove_low_and_no_objects(class_logits, mask_logits, threshold, num_labels); + + if (pred_labels_item.length === 0) { + // No mask found + let [height, width] = target_size ?? mask_logits.dims.slice(-2); + + let segmentation = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( + 'int32', + new Int32Array(height * width).fill(-1), + [height, width] + ) + toReturn.push({ + segmentation: segmentation, + segments_info: [] + }); + continue; + } + + + // Get segmentation map and segment information of batch item + let [segmentation, segments] = compute_segments( + mask_probs_item, + pred_scores_item, + pred_labels_item, + mask_threshold, + overlap_mask_area_threshold, + label_ids_to_fuse, + target_size, + ) + + toReturn.push({ + segmentation: segmentation, + segments_info: segments + }) + } + + return toReturn; +} + + +/** + * Post-processes the outputs of the model (for instance segmentation). + * @param {*} outputs Raw outputs of the model. + * @param {number} [threshold=0.5] The probability score threshold to keep predicted instance masks. + * @param {[number, number][]} [target_sizes=null] List of tuples corresponding to the requested final size + * (height, width) of each prediction. If unset, predictions will not be resized. + * @returns {Array<{ segmentation: Tensor, segments_info: Array<{id: number, label_id: number, score: number}>}>} + */ +function post_process_instance_segmentation(outputs, threshold = 0.5, target_sizes = null) { + throw new Error('Not implemented yet'); + return []; +} + +/** + * Named tuple to indicate the order we are using is (height x width), even though + * the Graphics’ industry standard is (width x height). + * @typedef {[height: number, width: number]} HeightWidth + */ + +/** + * Helper function to validate audio inputs. + * @param {any} audio The audio data. + * @param {string} feature_extractor The name of the feature extractor. + * @private + */ +function validate_audio_inputs(audio, feature_extractor) { + if (!(audio instanceof Float32Array || audio instanceof Float64Array)) { + throw new Error( + `${feature_extractor} expects input to be a Float32Array or a Float64Array, but got ${audio?.constructor?.name ?? typeof audio} instead. ` + + `If using the feature extractor directly, remember to use \`read_audio(url, sampling_rate)\` to obtain the raw audio data of the file/url.` + ) + } +} + +/** + * Helper function to constrain a value to be a multiple of a number. + * @param {number} val The value to constrain. + * @param {number} multiple The number to constrain to. + * @param {number} [minVal=0] The minimum value to constrain to. + * @param {number} [maxVal=null] The maximum value to constrain to. + * @returns {number} The constrained value. + * @private + */ +function constraint_to_multiple_of(val, multiple, minVal = 0, maxVal = null) { + const a = val / multiple; + let x = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.bankers_round)(a) * multiple; + + if (maxVal !== null && x > maxVal) { + x = Math.floor(a) * multiple; + } + + if (x < minVal) { + x = Math.ceil(a) * multiple; + } + + return x; +} + +/** + * Rounds the height and width down to the closest multiple of size_divisibility + * @param {[number, number]} size The size of the image + * @param {number} divisor The divisor to use. + * @returns {[number, number]} The rounded size. + */ +function enforce_size_divisibility([width, height], divisor) { + return [ + Math.max(Math.floor(width / divisor), 1) * divisor, + Math.max(Math.floor(height / divisor), 1) * divisor + ]; +} + + +/** + * Base class for feature extractors. + * + * @extends Callable + */ +class FeatureExtractor extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * Constructs a new FeatureExtractor instance. + * + * @param {Object} config The configuration for the feature extractor. + */ + constructor(config) { + super(); + this.config = config + } +} + +/** + * @typedef {object} ImageFeatureExtractorResult + * @property {Tensor} pixel_values The pixel values of the batched preprocessed images. + * @property {HeightWidth[]} original_sizes Array of two-dimensional tuples like [[480, 640]]. + * @property {HeightWidth[]} reshaped_input_sizes Array of two-dimensional tuples like [[1000, 1330]]. + */ + +/** + * Feature extractor for image models. + * + * @extends FeatureExtractor + */ +class ImageFeatureExtractor extends FeatureExtractor { + + /** + * Constructs a new ImageFeatureExtractor instance. + * + * @param {Object} config The configuration for the feature extractor. + * @param {number[]} config.image_mean The mean values for image normalization. + * @param {number[]} config.image_std The standard deviation values for image normalization. + * @param {boolean} config.do_rescale Whether to rescale the image pixel values to the [0,1] range. + * @param {number} config.rescale_factor The factor to use for rescaling the image pixel values. + * @param {boolean} config.do_normalize Whether to normalize the image pixel values. + * @param {boolean} config.do_resize Whether to resize the image. + * @param {number} config.resample What method to use for resampling. + * @param {number|Object} config.size The size to resize the image to. + * @param {boolean} [config.do_flip_channel_order=false] Whether to flip the color channels from RGB to BGR. + * Can be overridden by the `do_flip_channel_order` parameter in the `preprocess` method. + */ + constructor(config) { + super(config); + + this.image_mean = this.config.image_mean ?? this.config.mean; + this.image_std = this.config.image_std ?? this.config.std; + + this.resample = this.config.resample ?? 2; // 2 => bilinear + this.do_rescale = this.config.do_rescale ?? true; + this.rescale_factor = this.config.rescale_factor ?? (1 / 255); + this.do_normalize = this.config.do_normalize; + + this.do_resize = this.config.do_resize; + this.do_thumbnail = this.config.do_thumbnail; + this.size = this.config.size; + this.size_divisibility = this.config.size_divisibility ?? this.config.size_divisor; + + this.do_center_crop = this.config.do_center_crop; + this.crop_size = this.config.crop_size; + this.do_convert_rgb = this.config.do_convert_rgb ?? true; + this.do_crop_margin = this.config.do_crop_margin; + + this.pad_size = this.config.pad_size; + this.do_pad = this.config.do_pad; + + if (this.do_pad && !this.pad_size && this.size && this.size.width !== undefined && this.size.height !== undefined) { + // Should pad, but no pad size specified + // We infer the pad size from the resize size + this.pad_size = this.size + } + + this.do_flip_channel_order = this.config.do_flip_channel_order ?? false; + } + + /** + * Resize the image to make a thumbnail. The image is resized so that no dimension is larger than any + * corresponding dimension of the specified size. + * @param {RawImage} image The image to be resized. + * @param {{height:number, width:number}} size The size `{"height": h, "width": w}` to resize the image to. + * @param {string | 0 | 1 | 2 | 3 | 4 | 5} [resample=2] The resampling filter to use. + * @returns {Promise} The resized image. + */ + async thumbnail(image, size, resample = 2) { + const input_height = image.height; + const input_width = image.width; + + const output_height = size.height; + const output_width = size.width; + + // We always resize to the smallest of either the input or output size. + let height = Math.min(input_height, output_height) + let width = Math.min(input_width, output_width) + + if (height === input_height && width === input_width) { + return image; + } + if (input_height > input_width) { + width = Math.floor(input_width * height / input_height); + } else if (input_width > input_height) { + height = Math.floor(input_height * width / input_width); + } + return await image.resize(width, height, { resample }); + } + + + /** + * Crops the margin of the image. Gray pixels are considered margin (i.e., pixels with a value below the threshold). + * @param {RawImage} image The image to be cropped. + * @param {number} gray_threshold Value below which pixels are considered to be gray. + * @returns {Promise} The cropped image. + */ + async crop_margin(image, gray_threshold = 200) { + + const gray_image = image.clone().grayscale(); + + const minValue = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.min)(gray_image.data)[0]; + const maxValue = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.max)(gray_image.data)[0]; + const diff = maxValue - minValue; + + if (diff === 0) { + return image; + } + + const threshold = gray_threshold / 255; + + let x_min = gray_image.width, y_min = gray_image.height, x_max = 0, y_max = 0; + const gray_image_data = gray_image.data; + for (let j = 0; j < gray_image.height; ++j) { + const row = j * gray_image.width; + for (let i = 0; i < gray_image.width; ++i) { + if ((gray_image_data[row + i] - minValue) / diff < threshold) { + // We have a non-zero pixel, so we update the min/max values accordingly + x_min = Math.min(x_min, i); + y_min = Math.min(y_min, j); + x_max = Math.max(x_max, i); + y_max = Math.max(y_max, j); + } + } + } + + image = await image.crop([x_min, y_min, x_max, y_max]); + return image; + } + + /** + * Pad the image by a certain amount. + * @param {Float32Array} pixelData The pixel data to pad. + * @param {number[]} imgDims The dimensions of the image (height, width, channels). + * @param {{width:number; height:number}|number} padSize The dimensions of the padded image. + * @param {Object} options The options for padding. + * @param {'constant'|'symmetric'} [options.mode='constant'] The type of padding to add. + * @param {boolean} [options.center=false] Whether to center the image. + * @param {number} [options.constant_values=0] The constant value to use for padding. + * @returns {[Float32Array, number[]]} The padded pixel data and image dimensions. + */ + pad_image(pixelData, imgDims, padSize, { + mode = 'constant', + center = false, + constant_values = 0, + } = {}) { + const [imageHeight, imageWidth, imageChannels] = imgDims; + + let paddedImageWidth, paddedImageHeight; + if (typeof padSize === 'number') { + paddedImageWidth = padSize; + paddedImageHeight = padSize; + } else { + paddedImageWidth = padSize.width; + paddedImageHeight = padSize.height; + } + + // Only add padding if there is a difference in size + if (paddedImageWidth !== imageWidth || paddedImageHeight !== imageHeight) { + const paddedPixelData = new Float32Array(paddedImageWidth * paddedImageHeight * imageChannels); + if (Array.isArray(constant_values)) { + // Fill with constant values, cycling through the array + for (let i = 0; i < paddedPixelData.length; ++i) { + paddedPixelData[i] = constant_values[i % imageChannels]; + } + } else if (constant_values !== 0) { + paddedPixelData.fill(constant_values); + } + + const [left, top] = center + ? [Math.floor((paddedImageWidth - imageWidth) / 2), Math.floor((paddedImageHeight - imageHeight) / 2)] + : [0, 0]; + + // Copy the original image into the padded image + for (let i = 0; i < imageHeight; ++i) { + const a = (i + top) * paddedImageWidth; + const b = i * imageWidth; + for (let j = 0; j < imageWidth; ++j) { + const c = (a + j + left) * imageChannels; + const d = (b + j) * imageChannels; + for (let k = 0; k < imageChannels; ++k) { + paddedPixelData[c + k] = pixelData[d + k]; + } + } + } + + if (mode === 'symmetric') { + if (center) { + throw new Error('`center` padding is not supported when `mode` is set to `symmetric`.'); + // TODO: Implement this + } + const h1 = imageHeight - 1; + const w1 = imageWidth - 1; + for (let i = 0; i < paddedImageHeight; ++i) { + const a = i * paddedImageWidth; + const b = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.calculateReflectOffset)(i, h1) * imageWidth; + + for (let j = 0; j < paddedImageWidth; ++j) { + if (i < imageHeight && j < imageWidth) continue; // Do not overwrite original image + const c = (a + j) * imageChannels; + const d = (b + (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.calculateReflectOffset)(j, w1)) * imageChannels; + + // Copy channel-wise + for (let k = 0; k < imageChannels; ++k) { + paddedPixelData[c + k] = pixelData[d + k]; + } + } + } + } + + + // Update pixel data and image dimensions + pixelData = paddedPixelData; + imgDims = [paddedImageHeight, paddedImageWidth, imageChannels] + } + return [pixelData, imgDims]; + } + + /** + * Rescale the image' pixel values by `this.rescale_factor`. + * @param {Float32Array} pixelData The pixel data to rescale. + * @returns {void} + */ + rescale(pixelData) { + for (let i = 0; i < pixelData.length; ++i) { + pixelData[i] = this.rescale_factor * pixelData[i]; + } + } + + /** + * Find the target (width, height) dimension of the output image after + * resizing given the input image and the desired size. + * @param {RawImage} image The image to resize. + * @param {any} size The size to use for resizing the image. + * @returns {[number, number]} The target (width, height) dimension of the output image after resizing. + */ + get_resize_output_image_size(image, size) { + // `size` comes in many forms, so we need to handle them all here: + // 1. `size` is an integer, in which case we resize the image to be a square + + const [srcWidth, srcHeight] = image.size; + + let shortest_edge; + let longest_edge; + + if (this.do_thumbnail) { + // NOTE: custom logic for `Donut` models + const { height, width } = size; + shortest_edge = Math.min(height, width) + } + // Support both formats for backwards compatibility + else if (Number.isInteger(size)) { + shortest_edge = size; + longest_edge = this.config.max_size ?? shortest_edge; + + } else if (size !== undefined) { + // Extract known properties from `size` + shortest_edge = size.shortest_edge; + longest_edge = size.longest_edge; + } + + // If `longest_edge` and `shortest_edge` are set, maintain aspect ratio and resize to `shortest_edge` + // while keeping the largest dimension <= `longest_edge` + if (shortest_edge !== undefined || longest_edge !== undefined) { + // http://opensourcehacker.com/2011/12/01/calculate-aspect-ratio-conserving-resize-for-images-in-javascript/ + // Try resize so that shortest edge is `shortest_edge` (target) + const shortResizeFactor = shortest_edge === undefined + ? 1 // If `shortest_edge` is not set, don't upscale + : Math.max(shortest_edge / srcWidth, shortest_edge / srcHeight); + + const newWidth = srcWidth * shortResizeFactor; + const newHeight = srcHeight * shortResizeFactor; + + // The new width and height might be greater than `longest_edge`, so + // we downscale again to ensure the largest dimension is `longest_edge` + const longResizeFactor = longest_edge === undefined + ? 1 // If `longest_edge` is not set, don't downscale + : Math.min(longest_edge / newWidth, longest_edge / newHeight); + + // To avoid certain floating point precision issues, we round to 2 decimal places + let finalWidth = Math.floor(Number((newWidth * longResizeFactor).toFixed(2))); + let finalHeight = Math.floor(Number((newHeight * longResizeFactor).toFixed(2))); + + if (this.size_divisibility !== undefined) { + [finalWidth, finalHeight] = enforce_size_divisibility([finalWidth, finalHeight], this.size_divisibility) + } + return [finalWidth, finalHeight]; + + } else if (size !== undefined && size.width !== undefined && size.height !== undefined) { + // If `width` and `height` are set, resize to those dimensions + + let newWidth = size.width; + let newHeight = size.height; + + // Custom for DPT models + if (this.config.keep_aspect_ratio && this.config.ensure_multiple_of) { + + // determine new height and width + let scale_height = newHeight / srcHeight; + let scale_width = newWidth / srcWidth; + + // scale as little as possible + if (Math.abs(1 - scale_width) < Math.abs(1 - scale_height)) { + // fit width + scale_height = scale_width; + } else { + // fit height + scale_width = scale_height; + } + + newHeight = constraint_to_multiple_of(scale_height * srcHeight, this.config.ensure_multiple_of); + newWidth = constraint_to_multiple_of(scale_width * srcWidth, this.config.ensure_multiple_of); + } + + return [newWidth, newHeight]; + + } else if (this.size_divisibility !== undefined) { + return enforce_size_divisibility([srcWidth, srcHeight], this.size_divisibility); + } else { + throw new Error(`Could not resize image due to unsupported \`this.size\` option in config: ${JSON.stringify(size)}`); + } + } + + /** + * Resizes the image. + * @param {RawImage} image The image to resize. + * @returns {Promise} The resized image. + */ + async resize(image) { + const [newWidth, newHeight] = this.get_resize_output_image_size(image, this.size); + return await image.resize(newWidth, newHeight, { + resample: this.resample, + }); + } + + /** + * @typedef {object} PreprocessedImage + * @property {HeightWidth} original_size The original size of the image. + * @property {HeightWidth} reshaped_input_size The reshaped input size of the image. + * @property {Tensor} pixel_values The pixel values of the preprocessed image. + */ + + /** + * Preprocesses the given image. + * + * @param {RawImage} image The image to preprocess. + * @param {Object} overrides The overrides for the preprocessing options. + * @returns {Promise} The preprocessed image. + */ + async preprocess(image, { + do_normalize = null, + do_pad = null, + do_convert_rgb = null, + do_convert_grayscale = null, + do_flip_channel_order = null, + } = {}) { + if (this.do_crop_margin) { + // NOTE: Specific to nougat processors. This is done before resizing, + // and can be interpreted as a pre-preprocessing step. + image = await this.crop_margin(image); + } + + const [srcWidth, srcHeight] = image.size; // original image size + + // Convert image to RGB if specified in config. + if (do_convert_rgb ?? this.do_convert_rgb) { + image = image.rgb(); + } else if (do_convert_grayscale) { + image = image.grayscale(); + } + + // TODO: + // For efficiency reasons, it might be best to merge the resize and center crop operations into one. + + // Resize all images + if (this.do_resize) { + image = await this.resize(image); + } + + // Resize the image using thumbnail method. + if (this.do_thumbnail) { + image = await this.thumbnail(image, this.size, this.resample); + } + + if (this.do_center_crop) { + + let crop_width; + let crop_height; + if (Number.isInteger(this.crop_size)) { + crop_width = this.crop_size; + crop_height = this.crop_size; + } else { + crop_width = this.crop_size.width; + crop_height = this.crop_size.height; + } + + image = await image.center_crop(crop_width, crop_height); + } + + /** @type {HeightWidth} */ + const reshaped_input_size = [image.height, image.width]; + + // NOTE: All pixel-level manipulation (i.e., modifying `pixelData`) + // occurs with data in the hwc format (height, width, channels), + // to emulate the behavior of the original Python code (w/ numpy). + let pixelData = Float32Array.from(image.data); + let imgDims = [image.height, image.width, image.channels]; + + if (this.do_rescale) { + this.rescale(pixelData); + } + + if (do_normalize ?? this.do_normalize) { + let image_mean = this.image_mean; + if (!Array.isArray(this.image_mean)) { + image_mean = new Array(image.channels).fill(image_mean); + } + + let image_std = this.image_std; + if (!Array.isArray(this.image_std)) { + image_std = new Array(image.channels).fill(image_mean); + } + + if (image_mean.length !== image.channels || image_std.length !== image.channels) { + throw new Error(`When set to arrays, the length of \`image_mean\` (${image_mean.length}) and \`image_std\` (${image_std.length}) must match the number of channels in the image (${image.channels}).`); + } + + for (let i = 0; i < pixelData.length; i += image.channels) { + for (let j = 0; j < image.channels; ++j) { + pixelData[i + j] = (pixelData[i + j] - image_mean[j]) / image_std[j]; + } + } + } + + // do padding after rescaling/normalizing + if (do_pad ?? this.do_pad) { + if (this.pad_size) { + const padded = this.pad_image(pixelData, [image.height, image.width, image.channels], this.pad_size); + [pixelData, imgDims] = padded; // Update pixel data and image dimensions + } else if (this.size_divisibility) { + const [paddedWidth, paddedHeight] = enforce_size_divisibility([imgDims[1], imgDims[0]], this.size_divisibility); + [pixelData, imgDims] = this.pad_image(pixelData, imgDims, { width: paddedWidth, height: paddedHeight }); + } + } + + if (do_flip_channel_order ?? this.do_flip_channel_order) { + if (imgDims[2] !== 3) { + throw new Error('Flipping channel order is only supported for RGB images.'); + } + // Convert RGB to BGR + for (let i = 0; i < pixelData.length; i += 3) { + const temp = pixelData[i]; + pixelData[i] = pixelData[i + 2]; + pixelData[i + 2] = temp; + } + } + + const pixel_values = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor('float32', pixelData, imgDims) + .permute(2, 0, 1); // convert to channel dimension format (hwc -> chw) + + return { + original_size: [srcHeight, srcWidth], + reshaped_input_size: reshaped_input_size, + pixel_values, + } + } + + /** + * Calls the feature extraction process on an array of images, + * preprocesses each image, and concatenates the resulting + * features into a single Tensor. + * @param {RawImage[]} images The image(s) to extract features from. + * @param {...any} args Additional arguments. + * @returns {Promise} An object containing the concatenated pixel values (and other metadata) of the preprocessed images. + */ + async _call(images, ...args) { + if (!Array.isArray(images)) { + images = [images]; + } + /** @type {PreprocessedImage[]} */ + const imageData = await Promise.all(images.map(x => this.preprocess(x))); + + // Stack pixel values + const pixel_values = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.stack)(imageData.map(x => x.pixel_values), 0); + + return { + pixel_values, + + // Original sizes of images + original_sizes: imageData.map(x => x.original_size), + + // Reshaped sizes of images, before padding or cropping + reshaped_input_sizes: imageData.map(x => x.reshaped_input_size), + } + } + +} + +class SapiensFeatureExtractor extends ImageFeatureExtractor { + /** @type {typeof post_process_semantic_segmentation} */ + post_process_semantic_segmentation(...args) { + return post_process_semantic_segmentation(...args); + } +} +class SegformerFeatureExtractor extends ImageFeatureExtractor { + /** @type {typeof post_process_semantic_segmentation} */ + post_process_semantic_segmentation(...args) { + return post_process_semantic_segmentation(...args); + } +} +class PvtImageProcessor extends ImageFeatureExtractor { } +class DPTFeatureExtractor extends ImageFeatureExtractor { } +class DPTImageProcessor extends DPTFeatureExtractor { } // NOTE: extends DPTFeatureExtractor +class BitImageProcessor extends ImageFeatureExtractor { } +class GLPNFeatureExtractor extends ImageFeatureExtractor { } +class CLIPFeatureExtractor extends ImageFeatureExtractor { } +class CLIPImageProcessor extends CLIPFeatureExtractor { } // NOTE: extends CLIPFeatureExtractor +class ChineseCLIPFeatureExtractor extends ImageFeatureExtractor { } +class SiglipImageProcessor extends ImageFeatureExtractor { } +class ConvNextFeatureExtractor extends ImageFeatureExtractor { + constructor(config) { + super(config); + + /** + * Percentage of the image to crop. Only has an effect if this.size < 384. + */ + this.crop_pct = this.config.crop_pct ?? (224 / 256); + } + + async resize(image) { + const shortest_edge = this.size?.shortest_edge; + if (shortest_edge === undefined) { + throw new Error(`Size dictionary must contain 'shortest_edge' key.`); + } + + if (shortest_edge < 384) { + // maintain same ratio, resizing shortest edge to shortest_edge/crop_pct + const resize_shortest_edge = Math.floor(shortest_edge / this.crop_pct); + + const [newWidth, newHeight] = this.get_resize_output_image_size(image, { + shortest_edge: resize_shortest_edge, + }); + + image = await image.resize(newWidth, newHeight, { + resample: this.resample, + }); + + // then crop to (shortest_edge, shortest_edge) + image = await image.center_crop(shortest_edge, shortest_edge); + } else { + // warping (no cropping) when evaluated at 384 or larger + image = await image.resize(shortest_edge, shortest_edge, { + resample: this.resample, + }); + } + + return image; + } +} +class ConvNextImageProcessor extends ConvNextFeatureExtractor { } // NOTE extends ConvNextFeatureExtractor +class ViTFeatureExtractor extends ImageFeatureExtractor { } +class ViTImageProcessor extends ImageFeatureExtractor { } + +class EfficientNetImageProcessor extends ImageFeatureExtractor { + constructor(config) { + super(config); + this.include_top = this.config.include_top ?? true; + if (this.include_top) { + this.image_std = this.image_std.map(x => x * x); + } + } +} + +class MobileNetV1FeatureExtractor extends ImageFeatureExtractor { } +class MobileNetV2FeatureExtractor extends ImageFeatureExtractor { } +class MobileNetV3FeatureExtractor extends ImageFeatureExtractor { } +class MobileNetV4FeatureExtractor extends ImageFeatureExtractor { } + +class MobileViTFeatureExtractor extends ImageFeatureExtractor { } +class MobileViTImageProcessor extends MobileViTFeatureExtractor { } // NOTE extends MobileViTFeatureExtractor +class OwlViTFeatureExtractor extends ImageFeatureExtractor { + /** @type {typeof post_process_object_detection} */ + post_process_object_detection(...args) { + return post_process_object_detection(...args); + } +} +class Owlv2ImageProcessor extends OwlViTFeatureExtractor { } // NOTE extends OwlViTFeatureExtractor + +class RTDetrImageProcessor extends ImageFeatureExtractor { + /** @type {typeof post_process_object_detection} */ + post_process_object_detection(...args) { + return post_process_object_detection(...args); + } +} + +class DeiTFeatureExtractor extends ImageFeatureExtractor { } +class BeitFeatureExtractor extends ImageFeatureExtractor { } +class DonutFeatureExtractor extends ImageFeatureExtractor { + pad_image(pixelData, imgDims, padSize, options = {}) { + const [imageHeight, imageWidth, imageChannels] = imgDims; + + let image_mean = this.image_mean; + if (!Array.isArray(this.image_mean)) { + image_mean = new Array(imageChannels).fill(image_mean); + } + + let image_std = this.image_std; + if (!Array.isArray(image_std)) { + image_std = new Array(imageChannels).fill(image_mean); + } + + const constant_values = image_mean.map((x, i) => - x / image_std[i]); + + return super.pad_image(pixelData, imgDims, padSize, { + center: true, + + // Since normalization is done after padding, we need to use certain constant values to ensure the same behaviour is observed. + // For more information, see https://github.com/huggingface/transformers/blob/main/src/transformers/models/donut/image_processing_donut.py#L433-L451 + constant_values: constant_values, + ...options, + }); + } +} +class DonutImageProcessor extends DonutFeatureExtractor { } // NOTE extends DonutFeatureExtractor +class NougatImageProcessor extends DonutFeatureExtractor { } // NOTE extends DonutFeatureExtractor + +/** + * @typedef {object} DetrFeatureExtractorResultProps + * @property {Tensor} pixel_mask + * @typedef {ImageFeatureExtractorResult & DetrFeatureExtractorResultProps} DetrFeatureExtractorResult + */ + +/** + * Detr Feature Extractor. + * + * @extends ImageFeatureExtractor + */ +class DetrFeatureExtractor extends ImageFeatureExtractor { + /** + * Calls the feature extraction process on an array of images, preprocesses + * each image, and concatenates the resulting features into a single Tensor. + * @param {RawImage[]} images The image(s) to extract features from. + * @returns {Promise} An object containing the concatenated pixel values of the preprocessed images. + */ + async _call(images) { + const result = await super._call(images); + + // TODO support differently-sized images, for now assume all images are the same size. + // TODO support different mask sizes (not just 64x64) + // Currently, just fill pixel mask with 1s + const maskSize = [result.pixel_values.dims[0], 64, 64]; + const pixel_mask = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.full)(maskSize, 1n); + + return { ...result, pixel_mask }; + } + + /** @type {typeof post_process_object_detection} */ + post_process_object_detection(...args) { + return post_process_object_detection(...args); + } + + /** @type {typeof post_process_panoptic_segmentation} */ + post_process_panoptic_segmentation(...args) { + return post_process_panoptic_segmentation(...args); + } + + post_process_instance_segmentation() { + // TODO + throw Error("Not implemented yet"); + } +} + +class MaskFormerFeatureExtractor extends ImageFeatureExtractor { + + /** @type {typeof post_process_panoptic_segmentation} */ + post_process_panoptic_segmentation(...args) { + return post_process_panoptic_segmentation(...args); + } + + post_process_instance_segmentation() { + // TODO + throw Error("Not implemented yet"); + } +} + + +class YolosFeatureExtractor extends ImageFeatureExtractor { + /** @type {typeof post_process_object_detection} */ + post_process_object_detection(...args) { + return post_process_object_detection(...args); + } +} + +/** + * @typedef {object} SamImageProcessorResult + * @property {Tensor} pixel_values + * @property {HeightWidth[]} original_sizes + * @property {HeightWidth[]} reshaped_input_sizes + * @property {Tensor} [input_points] + * @property {Tensor} [input_labels] + * @property {Tensor} [input_boxes] + */ + +class SamImageProcessor extends ImageFeatureExtractor { + + /** + * + * @param {any} input_points + * @param {HeightWidth[]} original_sizes + * @param {HeightWidth[]} reshaped_input_sizes + * @returns {Tensor} + */ + reshape_input_points(input_points, original_sizes, reshaped_input_sizes, is_bounding_box = false) { + + // Make deep copy to avoid altering user's input + input_points = structuredClone(input_points); + let shape = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.calculateDimensions)(input_points); + + // TODO: add support for 2D input_points + if (shape.length === 3) { + // Correct user's input + if (!is_bounding_box) { + shape = [1, ...shape]; + } + input_points = [input_points]; + } else if (shape.length !== 4) { + throw Error("The input_points must be a 4D tensor of shape `batch_size`, `point_batch_size`, `nb_points_per_image`, `2`.") + } + + // Reshape input points + for (let i = 0; i < input_points.length; ++i) { // batch_size + let originalImageSize = original_sizes[i]; + let reshapedImageSize = reshaped_input_sizes[i]; + + let resizeFactors = [ + reshapedImageSize[0] / originalImageSize[0], + reshapedImageSize[1] / originalImageSize[1] + ] + + for (let j = 0; j < input_points[i].length; ++j) { // point_batch_size + for (let k = 0; k < input_points[i][j].length; ++k) { // nb_points_per_image + for (let w = 0; w < input_points[i][j][k].length; ++w) { // 2 or 4 + input_points[i][j][k][w] *= resizeFactors[w % 2]; + } + } + } + } + + return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( + 'float32', + Float32Array.from(input_points.flat(Infinity)), + shape + ) + + } + + /** + * + * @param {any} input_labels + * @param {Tensor} input_points + * @returns {Tensor} + */ + add_input_labels(input_labels, input_points) { + let shape = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.calculateDimensions)(input_labels); + if (shape.length === 2) { + // Correct user's input + shape = [1, ...shape]; + input_labels = [input_labels]; + } else if (shape.length !== 3) { + throw Error("The input_points must be a 4D tensor of shape `batch_size`, `point_batch_size`, `nb_points_per_image`, `2`.") + } + + if (shape.some((x, i) => x !== input_points.dims[i])) { + throw Error(`The first ${shape.length} dimensions of 'input_points' and 'input_labels' must be the same.`) + } + return new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( + 'int64', + input_labels.flat(Infinity).map(BigInt), + shape, + ) + } + /** + * @param {any[]} images The URL(s) of the image(s) to extract features from. + * @param {Object} [options] Additional options for the processor. + * @param {any} [options.input_points=null] A 3D or 4D array, representing the input points provided by the user. + * - 3D: `[point_batch_size, nb_points_per_image, 2]`. In this case, `batch_size` is assumed to be 1. + * - 4D: `[batch_size, point_batch_size, nb_points_per_image, 2]`. + * @param {any} [options.input_labels=null] A 2D or 3D array, representing the input labels for the points, used by the prompt encoder to encode the prompt. + * - 2D: `[point_batch_size, nb_points_per_image]`. In this case, `batch_size` is assumed to be 1. + * - 3D: `[batch_size, point_batch_size, nb_points_per_image]`. + * @param {number[][][]} [options.input_boxes=null] A 3D array of shape `(batch_size, num_boxes, 4)`, representing the input boxes provided by the user. + * This is used by the prompt encoder to encode the prompt. Generally yields to much better generated masks. + * The processor will generate a tensor, with each dimension corresponding respectively to the image batch size, + * the number of boxes per image and the coordinates of the top left and botton right point of the box. + * In the order (`x1`, `y1`, `x2`, `y2`): + * - `x1`: the x coordinate of the top left point of the input box + * - `y1`: the y coordinate of the top left point of the input box + * - `x2`: the x coordinate of the bottom right point of the input box + * - `y2`: the y coordinate of the bottom right point of the input box + * @returns {Promise} + */ + async _call(images, { + input_points = null, + input_labels = null, + input_boxes = null + } = {}) { + // TODO allow user to use preprocessed images + /** @type {SamImageProcessorResult} */ + const processed = await super._call(images); + + if (input_points) { + processed.input_points = this.reshape_input_points( + input_points, processed.original_sizes, processed.reshaped_input_sizes + ); + } + + if (input_labels) { + if (!processed.input_points) { + throw Error("`input_points` must be provided if `input_labels` are provided.") + } + processed.input_labels = this.add_input_labels(input_labels, processed.input_points); + } + + if (input_boxes) { + processed.input_boxes = this.reshape_input_points( + input_boxes, processed.original_sizes, processed.reshaped_input_sizes, true, + ); + } + + return processed; + } + + /** + * Remove padding and upscale masks to the original image size. + * @param {Tensor} masks Batched masks from the mask_decoder in (batch_size, num_channels, height, width) format. + * @param {[number, number][]} original_sizes The original sizes of each image before it was resized to the model's expected input shape, in (height, width) format. + * @param {[number, number][]} reshaped_input_sizes The size of each image as it is fed to the model, in (height, width) format. Used to remove padding. + * @param {Object} options Optional parameters for post-processing. + * @param {number} [options.mask_threshold] The threshold to use for binarizing the masks. + * @param {boolean} [options.binarize] Whether to binarize the masks. + * @param {Object} [options.pad_size] The target size the images were padded to before being passed to the model. If `null`, the target size is assumed to be the processor's `pad_size`. + * @param {number} [options.pad_size.height] The height the images were padded to. + * @param {number} [options.pad_size.width] The width the images were padded to. + * @returns {Promise} Batched masks in batch_size, num_channels, height, width) format, where (height, width) is given by original_size. + */ + async post_process_masks(masks, original_sizes, reshaped_input_sizes, { + mask_threshold = 0.0, + binarize = true, + pad_size = null, + } = {}) { + // masks: [1, 1, 3, 256, 256] + + const output_masks = []; + + pad_size = pad_size ?? this.pad_size; + + /** @type {[number, number]} */ + const target_image_size = [pad_size.height, pad_size.width]; + + for (let i = 0; i < original_sizes.length; ++i) { + const original_size = original_sizes[i]; + const reshaped_input_size = reshaped_input_sizes[i]; + + // Upscale mask to padded size + let interpolated_mask = (await (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.interpolate_4d)( + masks[i], + { mode: 'bilinear', size: target_image_size } + )); + + // Crop mask + interpolated_mask = interpolated_mask.slice(null, null, [0, reshaped_input_size[0]], [0, reshaped_input_size[1]]); + + // Downscale mask + interpolated_mask = (await (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.interpolate_4d)( + interpolated_mask, + { mode: 'bilinear', size: original_size } + )); + + if (binarize) { + const data = interpolated_mask.data; + const binarizedMaskData = new Uint8Array(data.length); + for (let i = 0; i < data.length; ++i) { + if (data[i] > mask_threshold) { + binarizedMaskData[i] = 1; + } + } + interpolated_mask = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( + 'bool', + binarizedMaskData, + interpolated_mask.dims + ) + } + + output_masks.push(interpolated_mask); + } + + return output_masks; + } + + /** + * Generates a list of crop boxes of different sizes. Each layer has (2**i)**2 boxes for the ith layer. + * @param {RawImage} image Input original image + * @param {number} target_size Target size of the resized image + * @param {Object} options Options for generating crop boxes + * @param {number} [options.crop_n_layers] If >0, mask prediction will be run again on crops of the image. + * Sets the number of layers to run, where each layer has 2**i_layer number of image crops. + * @param {number} [options.overlap_ratio] Sets the degree to which crops overlap. In the first crop layer, + * crops will overlap by this fraction of the image length. Later layers with more crops scale down this overlap. + * @param {number} [options.points_per_crop] Number of points to sample from each crop. + * @param {number} [options.crop_n_points_downscale_factor] The number of points-per-side sampled in layer n is + * scaled down by crop_n_points_downscale_factor**n. + * @returns {Object} An object containing the crop boxes, number of points per crop, cropped images, and input labels. + */ + generate_crop_boxes(image, target_size, { + crop_n_layers = 0, + overlap_ratio = 512 / 1500, + points_per_crop = 32, + crop_n_points_downscale_factor = 1, + } = {}) { + // TODO: Implement + // return { crop_boxes, points_per_crop, cropped_images, input_labels } + } +} + +class Swin2SRImageProcessor extends ImageFeatureExtractor { + pad_image(pixelData, imgDims, padSize, options = {}) { + // NOTE: In this case, `padSize` represents the size of the sliding window for the local attention. + // In other words, the image is padded so that its width and height are multiples of `padSize`. + const [imageHeight, imageWidth, imageChannels] = imgDims; + + return super.pad_image(pixelData, imgDims, { + // NOTE: For Swin2SR models, the original python implementation adds padding even when the image's width/height is already + // a multiple of `pad_size`. However, this is most likely a bug (PR: https://github.com/mv-lab/swin2sr/pull/19). + // For this reason, we only add padding when the image's width/height is not a multiple of `pad_size`. + width: imageWidth + (padSize - imageWidth % padSize) % padSize, + height: imageHeight + (padSize - imageHeight % padSize) % padSize, + }, { + mode: 'symmetric', + center: false, + constant_values: -1, + ...options, + }) + } +} + +class VitMatteImageProcessor extends ImageFeatureExtractor { + /** + * Calls the feature extraction process on an array of images, preprocesses + * each image, and concatenates the resulting features into a single Tensor. + * @param {RawImage[]} images The image(s) to extract features from. + * @param {RawImage[]} trimaps The trimaps(s) to extract features from. + * @returns {Promise} An object containing the concatenated pixel values of the preprocessed images. + */ + async _call(images, trimaps) { + if (!Array.isArray(images)) { + images = [images]; + } + if (!Array.isArray(trimaps)) { + trimaps = [trimaps]; + } + + const imageData = await Promise.all(images.map(x => this.preprocess(x))); + const trimapData = await Promise.all(trimaps.map(x => this.preprocess(x, { + do_normalize: false, + do_convert_rgb: false, + do_convert_grayscale: true, + }))); + + + // Stack pixel values + const pixel_values = (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.stack)(imageData.map( + // Concatenate images and trimaps + (x, i) => (0,_utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.cat)([x.pixel_values, trimapData[i].pixel_values], 0) + ), 0); + + return { + pixel_values, + + // Original sizes of images + original_sizes: imageData.map(x => x.original_size), + + // Reshaped sizes of images, before padding or cropping + reshaped_input_sizes: imageData.map(x => x.reshaped_input_size), + } + } +} + +class WhisperFeatureExtractor extends FeatureExtractor { + + constructor(config) { + super(config); + + // Prefer given `mel_filters` from preprocessor_config.json, or calculate them if they don't exist. + this.config.mel_filters ??= (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.mel_filter_bank)( + Math.floor(1 + this.config.n_fft / 2), // num_frequency_bins + this.config.feature_size, // num_mel_filters + 0.0, // min_frequency + 8000.0, // max_frequency + this.config.sampling_rate, // sampling_rate + "slaney", // norm + "slaney", // mel_scale + ); + + this.window = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.window_function)(this.config.n_fft, 'hann'); + } + + /** + * Computes the log-Mel spectrogram of the provided audio waveform. + * @param {Float32Array|Float64Array} waveform The audio waveform to process. + * @returns {Promise} An object containing the log-Mel spectrogram data as a Float32Array and its dimensions as an array of numbers. + */ + async _extract_fbank_features(waveform) { + const features = await (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.spectrogram)( + waveform, + this.window, // window + this.config.n_fft, // frame_length + this.config.hop_length, // hop_length + { + power: 2.0, + mel_filters: this.config.mel_filters, + log_mel: 'log10', + + // Custom + max_num_frames: this.config.nb_max_frames, // 3000 + } + ) + + const data = features.data; + const maxValue = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.max)(data)[0]; + + for (let i = 0; i < data.length; ++i) { + data[i] = (Math.max(data[i], maxValue - 8.0) + 4.0) / 4.0; + } + + return features; + } + + /** + * Asynchronously extracts features from a given audio using the provided configuration. + * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. + * @returns {Promise<{ input_features: Tensor }>} A Promise resolving to an object containing the extracted input features as a Tensor. + */ + async _call(audio) { + validate_audio_inputs(audio, 'WhisperFeatureExtractor'); + + let waveform; + if (audio.length > this.config.n_samples) { + console.warn( + "Attempting to extract features for audio longer than 30 seconds. " + + "If using a pipeline to extract transcript from a long audio clip, " + + "remember to specify `chunk_length_s` and/or `stride_length_s`." + ); + waveform = audio.slice(0, this.config.n_samples); + } else { + // pad with zeros + waveform = new Float32Array(this.config.n_samples); + waveform.set(audio); + } + + const features = await this._extract_fbank_features(waveform); + + return { + input_features: features.unsqueeze_(0) + }; + } +} + +class Wav2Vec2FeatureExtractor extends FeatureExtractor { + + /** + * @param {Float32Array} input_values + * @returns {Float32Array} + */ + _zero_mean_unit_var_norm(input_values) { + // TODO support batch? + const sum = input_values.reduce((a, b) => a + b, 0); + const mean = sum / input_values.length; + const variance = input_values.reduce((a, b) => a + (b - mean) ** 2, 0) / input_values.length; + return input_values.map(x => (x - mean) / Math.sqrt(variance + 1e-7)); + } + + /** + * Asynchronously extracts features from a given audio using the provided configuration. + * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. + * @returns {Promise<{ input_values: Tensor; attention_mask: Tensor }>} A Promise resolving to an object containing the extracted input features and attention mask as Tensors. + */ + async _call(audio) { + validate_audio_inputs(audio, 'Wav2Vec2FeatureExtractor'); + + if (audio instanceof Float64Array) { + audio = new Float32Array(audio); + } + + let input_values = audio; + + // zero-mean and unit-variance normalization + if (this.config.do_normalize) { + input_values = this._zero_mean_unit_var_norm(input_values); + } + + // TODO: allow user to pass in attention mask + const shape = [1, input_values.length]; + return { + input_values: new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor('float32', input_values, shape), + attention_mask: new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor('int64', new BigInt64Array(input_values.length).fill(1n), shape) + }; + } +} + +class SeamlessM4TFeatureExtractor extends FeatureExtractor { + + constructor(config) { + super(config); + + const sampling_rate = this.config.sampling_rate; + const mel_filters = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.mel_filter_bank)( + 256, // num_frequency_bins + this.config.num_mel_bins, // num_mel_filters + 20, // min_frequency + Math.floor(sampling_rate / 2), // max_frequency + sampling_rate, // sampling_rate + null, // norm + "kaldi", // mel_scale + true, // triangularize_in_mel_space + ); + + // Do padding: + for (let i = 0; i < mel_filters.length; ++i) { + mel_filters[i].push(0); + } + this.mel_filters = mel_filters; + + this.window = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.window_function)(400, 'povey', { + periodic: false, + }) + } + + /** + * Computes the log-Mel spectrogram of the provided audio waveform. + * @param {Float32Array|Float64Array} waveform The audio waveform to process. + * @param {number} max_length The maximum number of frames to return. + * @returns {Promise} An object containing the log-Mel spectrogram data as a Float32Array and its dimensions as an array of numbers. + */ + async _extract_fbank_features(waveform, max_length) { + // NOTE: We don't pad/truncate since that is passed in as `max_num_frames` + + // Kaldi compliance: 16-bit signed integers + // 32768 == 2 ** 15 + waveform = waveform.map((/** @type {number} */ x) => x * 32768) + + return (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.spectrogram)( + waveform, + this.window, // window + 400, // frame_length + 160, // hop_length + { + fft_length: 512, + power: 2.0, + center: false, + preemphasis: 0.97, + mel_filters: this.mel_filters, + log_mel: 'log', + mel_floor: 1.192092955078125e-07, + remove_dc_offset: true, + + // Custom + max_num_frames: max_length, + transpose: true, + } + ) + } + + /** + * Asynchronously extracts features from a given audio using the provided configuration. + * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. + * @param {Object} options Optional parameters for feature extraction. + * @param {boolean} [options.padding=true] Whether to pad the sequence to a multiple of `pad_to_multiple_of`. + * @param {number} [options.pad_to_multiple_of=2] The number to pad the sequence to a multiple of. + * @param {boolean} [options.do_normalize_per_mel_bins=true] Whether or not to zero-mean unit-variance normalize the input per mel-channel. + * @param {boolean} [options.return_attention_mask=true] Whether to return the attention mask. + * @returns {Promise<{ input_features: Tensor, attention_mask?: Tensor }>} A Promise resolving to an object containing the extracted input features and attention masks as Tensors. + */ + async _call(audio, { + padding = true, + pad_to_multiple_of = 2, + do_normalize_per_mel_bins = true, + return_attention_mask = true, + } = {}) { + validate_audio_inputs(audio, 'SeamlessM4TFeatureExtractor'); + + let features = await this._extract_fbank_features(audio, this.config.max_length); + + if (do_normalize_per_mel_bins) { + const [num_features, feature_size] = features.dims; + const data = features.data; + for (let i = 0; i < feature_size; ++i) { + let sum = 0; + for (let j = 0; j < num_features; ++j) { + sum += data[j * feature_size + i]; + } + + const mean = sum / num_features; + + let variance = 0; + for (let j = 0; j < num_features; ++j) { + variance += (data[j * feature_size + i] - mean) ** 2; + } + variance /= num_features - 1; // NOTE: We use ddof=1 + + const std = Math.sqrt(variance + 1e-7); + for (let j = 0; j < num_features; ++j) { + const index = j * feature_size + i; + data[index] = (data[index] - mean) / std; + } + } + } + + let padded_attention_mask; + if (padding) { + const [num_frames, num_channels] = features.dims; + const data = /** @type {Float32Array} */(features.data); + + const pad_size = num_frames % pad_to_multiple_of; + if (pad_size > 0) { + const padded_data = new Float32Array(num_channels * (num_frames + pad_size)); + padded_data.set(data) + padded_data.fill(this.config.padding_value, data.length) + + const numPaddedFrames = num_frames + pad_size; + features = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( + features.type, + padded_data, + [numPaddedFrames, num_channels], + ) + + if (return_attention_mask) { + padded_attention_mask = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( + 'int64', + new BigInt64Array(numPaddedFrames), + [1, numPaddedFrames], + ) + padded_attention_mask.data.fill(1n, 0, num_frames); + } + } + } + + const [num_frames, num_channels] = features.dims; + + const stride = this.config.stride; + const remainder = num_frames % stride; + if (remainder !== 0) { + throw new Error(`The number of frames (${num_frames}) must be a multiple of the stride (${stride}).`) + } + + const input_features = features.view( + 1, + Math.floor(num_frames / stride), + num_channels * stride, + ); + + const result = { input_features } + + if (return_attention_mask) { + const reshapedNumFrames = input_features.dims[1]; + + const attention_mask_data = new BigInt64Array(reshapedNumFrames); + + if (padded_attention_mask) { + const padded_attention_mask_data = padded_attention_mask.data; + for (let i = 1, j = 0; i < num_frames; i += stride, ++j) { + attention_mask_data[j] = padded_attention_mask_data[i]; + } + } else { + attention_mask_data.fill(1n); + } + result.attention_mask = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor( + 'int64', + attention_mask_data, + [1, reshapedNumFrames], + ); + } + + return result; + } +} + +class ASTFeatureExtractor extends FeatureExtractor { + + + constructor(config) { + super(config); + + const sampling_rate = this.config.sampling_rate; + const mel_filters = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.mel_filter_bank)( + 256, // num_frequency_bins + this.config.num_mel_bins, // num_mel_filters + 20, // min_frequency + Math.floor(sampling_rate / 2), // max_frequency + sampling_rate, // sampling_rate + null, // norm + "kaldi", // mel_scale + true, // triangularize_in_mel_space + ); + + // Do padding: + for (let i = 0; i < mel_filters.length; ++i) { + mel_filters[i].push(0); + } + this.mel_filters = mel_filters; + + this.window = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.window_function)(400, 'hann', { + periodic: false, + }) + + this.mean = this.config.mean; + this.std = this.config.std; + } + + /** + * Computes the log-Mel spectrogram of the provided audio waveform. + * @param {Float32Array|Float64Array} waveform The audio waveform to process. + * @param {number} max_length The maximum number of frames to return. + * @returns {Promise} An object containing the log-Mel spectrogram data as a Float32Array and its dimensions as an array of numbers. + */ + async _extract_fbank_features(waveform, max_length) { + // NOTE: We don't pad/truncate since that is passed in as `max_num_frames` + return (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.spectrogram)( + waveform, + this.window, // window + 400, // frame_length + 160, // hop_length + { + fft_length: 512, + power: 2.0, + center: false, + preemphasis: 0.97, + mel_filters: this.mel_filters, + log_mel: 'log', + mel_floor: 1.192092955078125e-07, + remove_dc_offset: true, + + // Custom + max_num_frames: max_length, + transpose: true, + } + ) + } + + + /** + * Asynchronously extracts features from a given audio using the provided configuration. + * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. + * @returns {Promise<{ input_values: Tensor }>} A Promise resolving to an object containing the extracted input features as a Tensor. + */ + async _call(audio) { + validate_audio_inputs(audio, 'ASTFeatureExtractor'); + + const features = await this._extract_fbank_features(audio, this.config.max_length); + if (this.config.do_normalize) { + // Normalize the input audio spectrogram to have mean=0, std=0.5 + const denom = this.std * 2; + const features_data = features.data; + for (let i = 0; i < features_data.length; ++i) { + features_data[i] = (features_data[i] - this.mean) / denom; + } + } + + return { + input_values: features.unsqueeze_(0) + }; + } +} + +class ClapFeatureExtractor extends FeatureExtractor { + + constructor(config) { + super(config); + + this.mel_filters = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.mel_filter_bank)( + this.config.nb_frequency_bins, // num_frequency_bins + this.config.feature_size, // num_mel_filters + this.config.frequency_min, // min_frequency + this.config.frequency_max, // max_frequency + this.config.sampling_rate, // sampling_rate + null, // norm + "htk", // mel_scale + ); + + this.mel_filters_slaney = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.mel_filter_bank)( + this.config.nb_frequency_bins, // num_frequency_bins + this.config.feature_size, // num_mel_filters + this.config.frequency_min, // min_frequency + this.config.frequency_max, // max_frequency + this.config.sampling_rate, // sampling_rate + "slaney", // norm + "slaney", // mel_scale + ); + + this.window = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.window_function)(this.config.fft_window_size, 'hann') + + } + + + /** + * Extracts the mel spectrogram and prepares it for the mode based on the `truncation` and `padding` arguments. + * + * Four different path are possible: + * - `truncation="fusion"` and the length of the waveform is greater than the max length: the mel spectrogram + * will be computed on the entire audio. 3 random crops and a dowsampled version of the full mel spectrogram + * are then stacked together. They will later be used for `feature_fusion`. + * - `truncation="rand_trunc"` and the length of the waveform is smaller than the max length: the audio is + * padded based on `padding`. + * - `truncation="fusion"` and the length of the waveform is smaller than the max length: the audio is padded + * based on `padding`, and is repeated `4` times. + * - `truncation="rand_trunc"` and the length of the waveform is greater than the max length: the mel + * spectrogram will be computed on a random crop of the waveform. + * + * @param {Float32Array|Float64Array} waveform The input waveform. + * @param {number} max_length The maximum length of the waveform. + * @param {string} truncation The truncation strategy to use. + * @param {string} padding The padding strategy to use. + * @returns {Promise} An object containing the mel spectrogram data as a Float32Array, its dimensions as an array of numbers, and a boolean indicating whether the waveform was longer than the max length. + * @private + */ + async _get_input_mel(waveform, max_length, truncation, padding) { + + /** @type {Tensor} */ + let input_mel; + let longer = false; + const diff = waveform.length - max_length; + if (diff > 0) { + if (truncation === 'rand_trunc') { + longer = true; + const idx = Math.floor(Math.random() * (diff + 1)); + waveform = waveform.subarray(idx, idx + max_length); + + input_mel = await this._extract_fbank_features(waveform, this.mel_filters_slaney, this.config.nb_max_samples); + } else { + // TODO implement fusion strategy + throw new Error(`Truncation strategy "${truncation}" not implemented`) + } + } else { + if (diff < 0) { + let padded = new Float64Array(max_length); // already padded with zeros + padded.set(waveform); + + if (padding === 'repeat') { + for (let i = waveform.length; i < max_length; i += waveform.length) { + padded.set(waveform.subarray(0, Math.min(waveform.length, max_length - i)), i); + } + } else if (padding === 'repeatpad') { + for (let i = waveform.length; i < -diff; i += waveform.length) { + padded.set(waveform, i); + } + } + waveform = padded; + } + + if (truncation === 'fusion') { + throw new Error(`Truncation strategy "${truncation}" not implemented`) + } + + input_mel = await this._extract_fbank_features(waveform, this.mel_filters_slaney, this.config.nb_max_samples); + } + + return input_mel.unsqueeze_(0); + } + + /** + * Compute the log-mel spectrogram of the provided `waveform` using the Hann window. + * In CLAP, two different filter banks are used depending on the truncation pattern: + * - `self.mel_filters`: they correspond to the default parameters of `torchaudio` which can be obtained from + * calling `torchaudio.transforms.MelSpectrogram().mel_scale.fb`. These filters are used when `truncation` + * is set to `"fusion"`. + * - `self.mel_filteres_slaney` : they correspond to the default parameters of `librosa` which used + * `librosa.filters.mel` when computing the mel spectrogram. These filters were only used in the original + * implementation when the truncation mode is not `"fusion"`. + * + * @param {Float32Array|Float64Array} waveform The audio waveform to process. + * @param {number[][]} mel_filters The mel filters to use. + * @param {number} [max_length=null] The maximum number of frames to return. + * @returns {Promise} An object containing the log-Mel spectrogram data as a Float32Array and its dimensions as an array of numbers. + */ + async _extract_fbank_features(waveform, mel_filters, max_length = null) { + // NOTE: We don't pad/truncate since that is passed in as `max_num_frames` + return (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.spectrogram)( + waveform, + this.window, // window + this.config.fft_window_size, // frame_length + this.config.hop_length, // hop_length + { + power: 2.0, + mel_filters, + log_mel: 'dB', + + // Custom + max_num_frames: max_length, + do_pad: false, + transpose: true, + } + ) + } + + + /** + * Asynchronously extracts features from a given audio using the provided configuration. + * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. + * @returns {Promise<{ input_features: Tensor }>} A Promise resolving to an object containing the extracted input features as a Tensor. + */ + async _call(audio, { + max_length = null, + } = {}) { + validate_audio_inputs(audio, 'ClapFeatureExtractor'); + + // convert to mel spectrogram, truncate and pad if needed. + const padded_inputs = await this._get_input_mel( + audio, + max_length ?? this.config.nb_max_samples, + this.config.truncation, + this.config.padding, + ); + + return { + input_features: padded_inputs.unsqueeze_(0), + } + } +} + + +class PyAnnoteFeatureExtractor extends FeatureExtractor { + /** + * Asynchronously extracts features from a given audio using the provided configuration. + * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. + * @returns {Promise<{ input_values: Tensor; }>} The extracted input features. + */ + async _call(audio) { + validate_audio_inputs(audio, 'PyAnnoteFeatureExtractor'); + + if (audio instanceof Float64Array) { + audio = new Float32Array(audio); + } + + const shape = [ + 1, /* batch_size */ + 1, /* num_channels */ + audio.length, /* num_samples */ + ]; + return { + input_values: new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor('float32', audio, shape), + }; + } + + /** + * NOTE: Can return fractional values. `Math.ceil` will ensure correct value. + * @param {number} samples The number of frames in the audio. + * @returns {number} The number of frames in the audio. + */ + samples_to_frames(samples) { + return ((samples - this.config.offset) / this.config.step); + } + + /** + * Post-processes the speaker diarization logits output by the model. + * @param {Tensor} logits The speaker diarization logits output by the model. + * @param {number} num_samples Number of samples in the input audio. + * @returns {Array>} The post-processed speaker diarization results. + */ + post_process_speaker_diarization(logits, num_samples) { + const ratio = ( + num_samples / this.samples_to_frames(num_samples) + ) / this.config.sampling_rate; + + const results = []; + for (const scores of logits.tolist()) { + const accumulated_segments = []; + + let current_speaker = -1; + for (let i = 0; i < scores.length; ++i) { + const probabilities = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.softmax)(scores[i]); + const [score, id] = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.max)(probabilities); + const [start, end] = [i, i + 1]; + + if (id !== current_speaker) { + // Speaker has changed + current_speaker = id; + accumulated_segments.push({ id, start, end, score }); + } else { + // Continue the current segment + accumulated_segments.at(-1).end = end; + accumulated_segments.at(-1).score += score; + } + } + + results.push(accumulated_segments.map( + // Convert frame-space to time-space + // and compute the confidence + ({ id, start, end, score }) => ({ + id, + start: start * ratio, + end: end * ratio, + confidence: score / (end - start), + }) + )); + } + return results; + } + +} + +class WeSpeakerFeatureExtractor extends FeatureExtractor { + + constructor(config) { + super(config); + + const sampling_rate = this.config.sampling_rate; + const mel_filters = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.mel_filter_bank)( + 256, // num_frequency_bins + this.config.num_mel_bins, // num_mel_filters + 20, // min_frequency + Math.floor(sampling_rate / 2), // max_frequency + sampling_rate, // sampling_rate + null, // norm + "kaldi", // mel_scale + true, // triangularize_in_mel_space + ); + + // Do padding: + for (let i = 0; i < mel_filters.length; ++i) { + mel_filters[i].push(0); + } + this.mel_filters = mel_filters; + + this.window = (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.window_function)(400, 'hamming', { + periodic: false, + }) + this.min_num_frames = this.config.min_num_frames; + } + + /** + * Computes the log-Mel spectrogram of the provided audio waveform. + * @param {Float32Array|Float64Array} waveform The audio waveform to process. + * @returns {Promise} An object containing the log-Mel spectrogram data as a Float32Array and its dimensions as an array of numbers. + */ + async _extract_fbank_features(waveform) { + // Kaldi compliance: 16-bit signed integers + // 32768 == 2 ** 15 + waveform = waveform.map((/** @type {number} */ x) => x * 32768) + + return (0,_utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.spectrogram)( + waveform, + this.window, // window + 400, // frame_length + 160, // hop_length + { + fft_length: 512, + power: 2.0, + center: false, + preemphasis: 0.97, + mel_filters: this.mel_filters, + log_mel: 'log', + mel_floor: 1.192092955078125e-07, + remove_dc_offset: true, + + // Custom + transpose: true, + min_num_frames: this.min_num_frames, + } + ) + } + + + /** + * Asynchronously extracts features from a given audio using the provided configuration. + * @param {Float32Array|Float64Array} audio The audio data as a Float32Array/Float64Array. + * @returns {Promise<{ input_features: Tensor }>} A Promise resolving to an object containing the extracted input features as a Tensor. + */ + async _call(audio) { + validate_audio_inputs(audio, 'WeSpeakerFeatureExtractor'); + + const features = (await this._extract_fbank_features(audio)).unsqueeze_(0); + + if (this.config.fbank_centering_span === null) { + // center features with global average + const meanData = /** @type {Float32Array} */ (features.mean(1).data); + const featuresData = /** @type {Float32Array} */(features.data); + const [batch_size, num_frames, feature_size] = features.dims; + + for (let i = 0; i < batch_size; ++i) { + const offset1 = i * num_frames * feature_size; + const offset2 = i * feature_size; + for (let j = 0; j < num_frames; ++j) { + const offset3 = offset1 + j * feature_size; + for (let k = 0; k < feature_size; ++k) { + featuresData[offset3 + k] -= meanData[offset2 + k]; + } + } + } + } + + return { + input_features: features + }; + } +} + +class SpeechT5FeatureExtractor extends FeatureExtractor { } + +/** + * Represents a Processor that extracts features from an input. + * @extends Callable + */ +class Processor extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * Creates a new Processor with the given feature extractor. + * @param {FeatureExtractor} feature_extractor The function used to extract features from the input. + */ + constructor(feature_extractor) { + super(); + this.feature_extractor = feature_extractor; + // TODO use tokenizer here? + } + + /** + * Calls the feature_extractor function with the given input. + * @param {any} input The input to extract features from. + * @param {...any} args Additional arguments. + * @returns {Promise} A Promise that resolves with the extracted features. + */ + async _call(input, ...args) { + return await this.feature_extractor(input, ...args); + } +} + +class SamProcessor extends Processor { + /** + * @borrows SamImageProcessor#_call as _call + */ + async _call(...args) { + return await this.feature_extractor(...args); + } + + /** + * @borrows SamImageProcessor#post_process_masks as post_process_masks + */ + post_process_masks(...args) { + // @ts-ignore + return this.feature_extractor.post_process_masks(...args); + } + /** + * @borrows SamImageProcessor#reshape_input_points as reshape_input_points + */ + reshape_input_points(...args) { + // @ts-ignore + return this.feature_extractor.reshape_input_points(...args); + } +} + +/** + * Represents a WhisperProcessor that extracts features from an audio input. + * @extends Processor + */ +class WhisperProcessor extends Processor { + /** + * Calls the feature_extractor function with the given audio input. + * @param {any} audio The audio input to extract features from. + * @returns {Promise} A Promise that resolves with the extracted features. + */ + async _call(audio) { + return await this.feature_extractor(audio) + } +} + + +class Wav2Vec2ProcessorWithLM extends Processor { + /** + * Calls the feature_extractor function with the given audio input. + * @param {any} audio The audio input to extract features from. + * @returns {Promise} A Promise that resolves with the extracted features. + */ + async _call(audio) { + return await this.feature_extractor(audio) + } +} + +class PyAnnoteProcessor extends Processor { + /** + * Calls the feature_extractor function with the given audio input. + * @param {any} audio The audio input to extract features from. + * @returns {Promise} A Promise that resolves with the extracted features. + */ + async _call(audio) { + return await this.feature_extractor(audio) + } + + post_process_speaker_diarization(...args) { + // @ts-ignore + return this.feature_extractor.post_process_speaker_diarization(...args); + } + +} + +class SpeechT5Processor extends Processor { + /** + * Calls the feature_extractor function with the given input. + * @param {any} input The input to extract features from. + * @returns {Promise} A Promise that resolves with the extracted features. + */ + async _call(input) { + return await this.feature_extractor(input) + } +} + +class OwlViTProcessor extends Processor { } + +class Florence2Processor extends Processor { + constructor(feature_extractor) { + super(feature_extractor); + + const { + tasks_answer_post_processing_type, + task_prompts_without_inputs, + task_prompts_with_input, + } = feature_extractor.config; + + /** @type {Map} */ + this.tasks_answer_post_processing_type = new Map(Object.entries(tasks_answer_post_processing_type ?? {})); + + /** @type {Map} */ + this.task_prompts_without_inputs = new Map(Object.entries(task_prompts_without_inputs ?? {})); + + /** @type {Map} */ + this.task_prompts_with_input = new Map(Object.entries(task_prompts_with_input ?? {})); + + this.regexes = { + quad_boxes: /(.+?)/gm, + bboxes: /([^<]+)?/gm, + } + this.size_per_bin = 1000; + } + + /** + * Helper function to construct prompts from input texts + * @param {string|string[]} text + * @returns {string[]} + */ + construct_prompts(text) { + if (typeof text === 'string') { + text = [text]; + } + + const prompts = []; + for (const t of text) { + // 1. fixed task prompts without additional inputs + if (this.task_prompts_without_inputs.has(t)) { + prompts.push(this.task_prompts_without_inputs.get(t)); + } + // 2. task prompts with additional inputs + else { + for (const [task, prompt] of this.task_prompts_with_input) { + if (t.includes(task)) { + prompts.push(prompt.replaceAll('{input}', t).replaceAll(task, '')); + break; + } + } + + // 3. default prompt + if (prompts.length !== text.length) { + prompts.push(t); + } + } + } + return prompts; + } + + /** + * Post-process the output of the model to each of the task outputs. + * @param {string} text The text to post-process. + * @param {string} task The task to post-process the text for. + * @param {[number, number]} image_size The size of the image. height x width. + */ + post_process_generation(text, task, image_size) { + const task_answer_post_processing_type = this.tasks_answer_post_processing_type.get(task) ?? 'pure_text'; + + // remove the special tokens + text = text.replaceAll('', '').replaceAll('', ''); + + let final_answer; + switch (task_answer_post_processing_type) { + case 'pure_text': + final_answer = text; + break; + + case 'description_with_bboxes': + case 'bboxes': + case 'phrase_grounding': + case 'ocr': + const key = task_answer_post_processing_type === 'ocr' ? 'quad_boxes' : 'bboxes'; + const matches = text.matchAll(this.regexes[key]); + const labels = []; + const items = []; + for (const [_, label, ...locations] of matches) { + // Push new label, or duplicate the last label + labels.push(label ? label.trim() : labels.at(-1) ?? ''); + items.push(locations.map((x, i) => + // NOTE: Add 0.5 to use the center position of the bin as the coordinate. + (Number(x) + 0.5) / this.size_per_bin * image_size[i % 2]) + ); + } + final_answer = { labels, [key]: items }; + break; + + default: + throw new Error(`Task "${task}" (of type "${task_answer_post_processing_type}") not yet implemented.`); + } + + return { [task]: final_answer } + } +} + +////////////////////////////////////////////////// +/** + * Helper class which is used to instantiate pretrained processors with the `from_pretrained` function. + * The chosen processor class is determined by the type specified in the processor config. + * + * **Example:** Load a processor using `from_pretrained`. + * ```javascript + * let processor = await AutoProcessor.from_pretrained('openai/whisper-tiny.en'); + * ``` + * + * **Example:** Run an image through a processor. + * ```javascript + * let processor = await AutoProcessor.from_pretrained('Xenova/clip-vit-base-patch16'); + * let image = await RawImage.read('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/football-match.jpg'); + * let image_inputs = await processor(image); + * // { + * // "pixel_values": { + * // "dims": [ 1, 3, 224, 224 ], + * // "type": "float32", + * // "data": Float32Array [ -1.558687686920166, -1.558687686920166, -1.5440893173217773, ... ], + * // "size": 150528 + * // }, + * // "original_sizes": [ + * // [ 533, 800 ] + * // ], + * // "reshaped_input_sizes": [ + * // [ 224, 224 ] + * // ] + * // } + * ``` + */ +class AutoProcessor { + static FEATURE_EXTRACTOR_CLASS_MAPPING = { + ImageFeatureExtractor, + WhisperFeatureExtractor, + ViTFeatureExtractor, + MobileViTFeatureExtractor, + MobileViTImageProcessor, + MobileNetV1FeatureExtractor, + MobileNetV2FeatureExtractor, + MobileNetV3FeatureExtractor, + MobileNetV4FeatureExtractor, + OwlViTFeatureExtractor, + Owlv2ImageProcessor, + CLIPFeatureExtractor, + CLIPImageProcessor, + Florence2Processor, + ChineseCLIPFeatureExtractor, + SiglipImageProcessor, + ConvNextFeatureExtractor, + ConvNextImageProcessor, + SegformerFeatureExtractor, + SapiensFeatureExtractor, + BitImageProcessor, + DPTImageProcessor, + DPTFeatureExtractor, + PvtImageProcessor, + GLPNFeatureExtractor, + BeitFeatureExtractor, + DeiTFeatureExtractor, + DetrFeatureExtractor, + RTDetrImageProcessor, + MaskFormerFeatureExtractor, + YolosFeatureExtractor, + DonutFeatureExtractor, + DonutImageProcessor, + NougatImageProcessor, + EfficientNetImageProcessor, + + ViTImageProcessor, + VitMatteImageProcessor, + SamImageProcessor, + Swin2SRImageProcessor, + Wav2Vec2FeatureExtractor, + SeamlessM4TFeatureExtractor, + SpeechT5FeatureExtractor, + ASTFeatureExtractor, + ClapFeatureExtractor, + PyAnnoteFeatureExtractor, + WeSpeakerFeatureExtractor, + } + + static PROCESSOR_CLASS_MAPPING = { + WhisperProcessor, + Wav2Vec2ProcessorWithLM, + PyAnnoteProcessor, + SamProcessor, + SpeechT5Processor, + OwlViTProcessor, + Florence2Processor, + } + + /** + * Instantiate one of the processor classes of the library from a pretrained model. + * + * The processor class to instantiate is selected based on the `feature_extractor_type` property of the config object + * (either passed as an argument or loaded from `pretrained_model_name_or_path` if possible) + * + * @param {string} pretrained_model_name_or_path The name or path of the pretrained model. Can be either: + * - A string, the *model id* of a pretrained processor hosted inside a model repo on huggingface.co. + * Valid model ids can be located at the root-level, like `bert-base-uncased`, or namespaced under a + * user or organization name, like `dbmdz/bert-base-german-cased`. + * - A path to a *directory* containing processor files, e.g., `./my_model_directory/`. + * @param {import('./utils/hub.js').PretrainedOptions} options Additional options for loading the processor. + * + * @returns {Promise} A new instance of the Processor class. + */ + static async from_pretrained(pretrained_model_name_or_path, { + progress_callback = null, + config = null, + cache_dir = null, + local_files_only = false, + revision = 'main', + } = {}) { + + let preprocessorConfig = config ?? await (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_2__.getModelJSON)(pretrained_model_name_or_path, 'preprocessor_config.json', true, { + progress_callback, + config, + cache_dir, + local_files_only, + revision, + }) + + // Determine feature extractor class + // TODO: Ensure backwards compatibility with old configs + let key = preprocessorConfig.feature_extractor_type ?? preprocessorConfig.image_processor_type; + let feature_extractor_class = this.FEATURE_EXTRACTOR_CLASS_MAPPING[key]; + + if (!feature_extractor_class) { + if (preprocessorConfig.size !== undefined) { + // Assume ImageFeatureExtractor + console.warn(`Feature extractor type "${key}" not found, assuming ImageFeatureExtractor due to size parameter in config.`); + feature_extractor_class = ImageFeatureExtractor; + } else { + throw new Error(`Unknown Feature Extractor type: ${key}`); + } + } + + // If no associated processor class, use default + let processor_class = this.PROCESSOR_CLASS_MAPPING[preprocessorConfig.processor_class] ?? Processor; + + // Instantiate processor and feature extractor + let feature_extractor = new feature_extractor_class(preprocessorConfig); + return new processor_class(feature_extractor); + } +} +////////////////////////////////////////////////// + + + +/***/ }), + +/***/ "./src/tokenizers.js": +/*!***************************!*\ + !*** ./src/tokenizers.js ***! + \***************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ AlbertTokenizer: () => (/* binding */ AlbertTokenizer), +/* harmony export */ AutoTokenizer: () => (/* binding */ AutoTokenizer), +/* harmony export */ BartTokenizer: () => (/* binding */ BartTokenizer), +/* harmony export */ BertTokenizer: () => (/* binding */ BertTokenizer), +/* harmony export */ BlenderbotSmallTokenizer: () => (/* binding */ BlenderbotSmallTokenizer), +/* harmony export */ BlenderbotTokenizer: () => (/* binding */ BlenderbotTokenizer), +/* harmony export */ BloomTokenizer: () => (/* binding */ BloomTokenizer), +/* harmony export */ CLIPTokenizer: () => (/* binding */ CLIPTokenizer), +/* harmony export */ CamembertTokenizer: () => (/* binding */ CamembertTokenizer), +/* harmony export */ CodeGenTokenizer: () => (/* binding */ CodeGenTokenizer), +/* harmony export */ CodeLlamaTokenizer: () => (/* binding */ CodeLlamaTokenizer), +/* harmony export */ CohereTokenizer: () => (/* binding */ CohereTokenizer), +/* harmony export */ ConvBertTokenizer: () => (/* binding */ ConvBertTokenizer), +/* harmony export */ DebertaTokenizer: () => (/* binding */ DebertaTokenizer), +/* harmony export */ DebertaV2Tokenizer: () => (/* binding */ DebertaV2Tokenizer), +/* harmony export */ DistilBertTokenizer: () => (/* binding */ DistilBertTokenizer), +/* harmony export */ ElectraTokenizer: () => (/* binding */ ElectraTokenizer), +/* harmony export */ EsmTokenizer: () => (/* binding */ EsmTokenizer), +/* harmony export */ FalconTokenizer: () => (/* binding */ FalconTokenizer), +/* harmony export */ GPT2Tokenizer: () => (/* binding */ GPT2Tokenizer), +/* harmony export */ GPTNeoXTokenizer: () => (/* binding */ GPTNeoXTokenizer), +/* harmony export */ GemmaTokenizer: () => (/* binding */ GemmaTokenizer), +/* harmony export */ Grok1Tokenizer: () => (/* binding */ Grok1Tokenizer), +/* harmony export */ HerbertTokenizer: () => (/* binding */ HerbertTokenizer), +/* harmony export */ LlamaTokenizer: () => (/* binding */ LlamaTokenizer), +/* harmony export */ M2M100Tokenizer: () => (/* binding */ M2M100Tokenizer), +/* harmony export */ MBart50Tokenizer: () => (/* binding */ MBart50Tokenizer), +/* harmony export */ MBartTokenizer: () => (/* binding */ MBartTokenizer), +/* harmony export */ MPNetTokenizer: () => (/* binding */ MPNetTokenizer), +/* harmony export */ MarianTokenizer: () => (/* binding */ MarianTokenizer), +/* harmony export */ MobileBertTokenizer: () => (/* binding */ MobileBertTokenizer), +/* harmony export */ NllbTokenizer: () => (/* binding */ NllbTokenizer), +/* harmony export */ NougatTokenizer: () => (/* binding */ NougatTokenizer), +/* harmony export */ PreTrainedTokenizer: () => (/* binding */ PreTrainedTokenizer), +/* harmony export */ Qwen2Tokenizer: () => (/* binding */ Qwen2Tokenizer), +/* harmony export */ RoFormerTokenizer: () => (/* binding */ RoFormerTokenizer), +/* harmony export */ RobertaTokenizer: () => (/* binding */ RobertaTokenizer), +/* harmony export */ SiglipTokenizer: () => (/* binding */ SiglipTokenizer), +/* harmony export */ SpeechT5Tokenizer: () => (/* binding */ SpeechT5Tokenizer), +/* harmony export */ SqueezeBertTokenizer: () => (/* binding */ SqueezeBertTokenizer), +/* harmony export */ T5Tokenizer: () => (/* binding */ T5Tokenizer), +/* harmony export */ TokenizerModel: () => (/* binding */ TokenizerModel), +/* harmony export */ VitsTokenizer: () => (/* binding */ VitsTokenizer), +/* harmony export */ Wav2Vec2CTCTokenizer: () => (/* binding */ Wav2Vec2CTCTokenizer), +/* harmony export */ WhisperTokenizer: () => (/* binding */ WhisperTokenizer), +/* harmony export */ XLMRobertaTokenizer: () => (/* binding */ XLMRobertaTokenizer), +/* harmony export */ XLMTokenizer: () => (/* binding */ XLMTokenizer), +/* harmony export */ is_chinese_char: () => (/* binding */ is_chinese_char) +/* harmony export */ }); +/* harmony import */ var _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./utils/generic.js */ "./src/utils/generic.js"); +/* harmony import */ var _utils_core_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./utils/core.js */ "./src/utils/core.js"); +/* harmony import */ var _utils_hub_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./utils/hub.js */ "./src/utils/hub.js"); +/* harmony import */ var _utils_maths_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./utils/maths.js */ "./src/utils/maths.js"); +/* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ./utils/tensor.js */ "./src/utils/tensor.js"); +/* harmony import */ var _utils_data_structures_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ./utils/data-structures.js */ "./src/utils/data-structures.js"); +/* harmony import */ var _huggingface_jinja__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! @huggingface/jinja */ "./node_modules/@huggingface/jinja/dist/index.js"); +/* harmony import */ var _models_whisper_common_whisper_js__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__(/*! ./models/whisper/common_whisper.js */ "./src/models/whisper/common_whisper.js"); +/* harmony import */ var _utils_constants_js__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__(/*! ./utils/constants.js */ "./src/utils/constants.js"); + +/** + * @file Tokenizers are used to prepare textual inputs for a model. + * + * **Example:** Create an `AutoTokenizer` and use it to tokenize a sentence. + * This will automatically detect the tokenizer type based on the tokenizer class defined in `tokenizer.json`. + * ```javascript + * import { AutoTokenizer } from '@huggingface/transformers'; + * + * const tokenizer = await AutoTokenizer.from_pretrained('Xenova/bert-base-uncased'); + * const { input_ids } = await tokenizer('I love transformers!'); + * // Tensor { + * // data: BigInt64Array(6) [101n, 1045n, 2293n, 19081n, 999n, 102n], + * // dims: [1, 6], + * // type: 'int64', + * // size: 6, + * // } + * ``` + * + * @module tokenizers + */ + + + + + + + + + + + + + + + + +/** + * @typedef {Object} TokenizerProperties Additional tokenizer-specific properties. + * @property {boolean} [legacy=false] Whether or not the `legacy` behavior of the tokenizer should be used. + * @typedef {import('./utils/hub.js').PretrainedOptions & TokenizerProperties} PretrainedTokenizerOptions + */ + +/** + * Loads a tokenizer from the specified path. + * @param {string} pretrained_model_name_or_path The path to the tokenizer directory. + * @param {PretrainedTokenizerOptions} options Additional options for loading the tokenizer. + * @returns {Promise} A promise that resolves with information about the loaded tokenizer. + */ +async function loadTokenizer(pretrained_model_name_or_path, options) { + + const info = await Promise.all([ + (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_2__.getModelJSON)(pretrained_model_name_or_path, 'tokenizer.json', true, options), + (0,_utils_hub_js__WEBPACK_IMPORTED_MODULE_2__.getModelJSON)(pretrained_model_name_or_path, 'tokenizer_config.json', true, options), + ]) + + // Override legacy option if `options.legacy` is not null + if (options.legacy !== null) { + info[1].legacy = options.legacy; + } + return info; +} + + +/** + * Helper function to split a string on a regex, but keep the delimiters. + * This is required, because the JavaScript `.split()` method does not keep the delimiters, + * and wrapping in a capturing group causes issues with existing capturing groups (due to nesting). + * @param {string} text The text to split. + * @param {RegExp} regex The regex to split on. + * @returns {string[]} The split string. + */ +function regexSplit(text, regex) { + const result = []; + let prev = 0; + for (const match of text.matchAll(regex)) { + const fullMatch = match[0]; + if (prev < match.index) { + result.push(text.slice(prev, match.index)); + } + if (fullMatch.length > 0) { + result.push(fullMatch); + } + prev = match.index + fullMatch.length; + } + if (prev < text.length) { + result.push(text.slice(prev)); + } + return result; +} + + +/** + * Helper method to construct a pattern from a config object. + * @param {Object} pattern The pattern object. + * @param {boolean} invert Whether to invert the pattern. + * @returns {RegExp|null} The compiled pattern. + */ +function createPattern(pattern, invert = true) { + + if (pattern.Regex !== undefined) { + // In certain cases, the pattern may contain unnecessary escape sequences (e.g., \# or \& or \~). + // i.e., valid in Python (where the patterns are exported from) but invalid in JavaScript (where the patterns are parsed). + // This isn't an issue when creating the regex w/o the 'u' flag, but it is when the 'u' flag is used. + // For this reason, it is necessary to remove these backslashes before creating the regex. + // See https://stackoverflow.com/a/63007777/13989043 for more information + let regex = pattern.Regex.replace(/\\([#&~])/g, '$1'); // TODO: add more characters to this list if necessary + + // We also handle special cases where the regex contains invalid (non-JS compatible) syntax. + for (const [key, value] of PROBLEMATIC_REGEX_MAP) { + regex = regex.replaceAll(key, value); + } + + return new RegExp(regex, 'gu'); + + } else if (pattern.String !== undefined) { + const escaped = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.escapeRegExp)(pattern.String); + // NOTE: if invert is true, we wrap the pattern in a group so that it is kept when performing .split() + return new RegExp(invert ? escaped : `(${escaped})`, 'gu'); + + } else { + console.warn('Unknown pattern type:', pattern) + return null; + } +} + +/** + * Helper function to convert an Object to a Map + * @param {Object} obj The object to convert. + * @returns {Map} The map. + */ +function objectToMap(obj) { + return new Map(Object.entries(obj)); +} + +/** + * Helper function to convert a tensor to a list before decoding. + * @param {Tensor} tensor The tensor to convert. + * @returns {number[]} The tensor as a list. + */ +function prepareTensorForDecode(tensor) { + const dims = tensor.dims; + switch (dims.length) { + case 1: + return tensor.tolist(); + case 2: + if (dims[0] !== 1) { + throw new Error('Unable to decode tensor with `batch size !== 1`. Use `tokenizer.batch_decode(...)` for batched inputs.'); + } + return tensor.tolist()[0]; + default: + throw new Error(`Expected tensor to have 1-2 dimensions, got ${dims.length}.`) + } +} + +/** + * Clean up a list of simple English tokenization artifacts like spaces before punctuations and abbreviated forms + * @param {string} text The text to clean up. + * @returns {string} The cleaned up text. + */ +function clean_up_tokenization(text) { + // Clean up a list of simple English tokenization artifacts + // like spaces before punctuations and abbreviated forms + return text.replace(/ \./g, '.') + .replace(/ \?/g, '?') + .replace(/ \!/g, '!') + .replace(/ ,/g, ',') + .replace(/ \' /g, "'") + .replace(/ n\'t/g, "n't") + .replace(/ \'m/g, "'m") + .replace(/ \'s/g, "'s") + .replace(/ \'ve/g, "'ve") + .replace(/ \'re/g, "'re"); +} + +/** + * Helper function to remove accents from a string. + * @param {string} text The text to remove accents from. + * @returns {string} The text with accents removed. + */ +function remove_accents(text) { + return text.replace(/\p{M}/gu, ''); +} + +/** + * Helper function to lowercase a string and remove accents. + * @param {string} text The text to lowercase and remove accents from. + * @returns {string} The lowercased text with accents removed. + */ +function lowercase_and_remove_accent(text) { + return remove_accents(text.toLowerCase()); +} + + +/** + * Checks whether the given Unicode codepoint represents a CJK (Chinese, Japanese, or Korean) character. + * + * A "chinese character" is defined as anything in the CJK Unicode block: + * https://en.wikipedia.org/wiki/CJK_Unified_Ideographs_(Unicode_block) + * + * Note that the CJK Unicode block is NOT all Japanese and Korean characters, despite its name. + * The modern Korean Hangul alphabet is a different block, as is Japanese Hiragana and Katakana. + * Those alphabets are used to write space-separated words, so they are not treated specially + * and are handled like all other languages. + * + * @param {number|bigint} cp The Unicode codepoint to check. + * @returns {boolean} True if the codepoint represents a CJK character, false otherwise. + */ +function is_chinese_char(cp) { + return ( + (cp >= 0x4E00 && cp <= 0x9FFF) + || (cp >= 0x3400 && cp <= 0x4DBF) + || (cp >= 0x20000 && cp <= 0x2A6DF) + || (cp >= 0x2A700 && cp <= 0x2B73F) + || (cp >= 0x2B740 && cp <= 0x2B81F) + || (cp >= 0x2B820 && cp <= 0x2CEAF) + || (cp >= 0xF900 && cp <= 0xFAFF) + || (cp >= 0x2F800 && cp <= 0x2FA1F) + ) +} + +/** + * Helper function to fuse consecutive unknown tokens. + * @param {string[]} arr The list of input tokens + * @param {Map} tokens_to_ids The mapping from tokens to token ids. + * @param {number} unk_token_id The value to fuse on. + * @private + */ +function fuse_unk(arr, tokens_to_ids, unk_token_id) { + const fused = []; + let i = 0; + while (i < arr.length) { + fused.push(arr[i]) + if ((tokens_to_ids.get(arr[i]) ?? unk_token_id) !== unk_token_id) { + ++i; + continue; + } + + while (++i < arr.length && (tokens_to_ids.get(arr[i]) ?? unk_token_id) === unk_token_id) { + if (tokens_to_ids.get(fused.at(-1)) !== unk_token_id) { + fused[fused.length - 1] += arr[i]; + } + } + } + + return fused; +} + +/** + * Split a string on whitespace. + * @param {string} text The text to split. + * @returns {string[]} The split string. + */ +function whitespace_split(text) { + return text.match(/\S+/g) || []; +} + +const PUNCTUATION_REGEX = '\\p{P}\\u0021-\\u002F\\u003A-\\u0040\\u005B-\\u0060\\u007B-\\u007E'; +const PUNCTUATION_ONLY_REGEX = new RegExp(`^[${PUNCTUATION_REGEX}]+$`, 'gu'); +const BLOOM_SPLIT_CHARS = '.,!?\u2026\u3002\uff0c\u3001\u0964\u06d4\u060c'; + +// A mapping of regex patterns to their equivalent (but possibly longer) JS-compatible versions. +const PROBLEMATIC_REGEX_MAP = new Map([ + // This uses the case insensitive group modifier, which is not supported in JavaScript. + // When parsing the regex, an "Invalid group" error is thrown. + ["(?i:'s|'t|'re|'ve|'m|'ll|'d)", "(?:'([sS]|[tT]|[rR][eE]|[vV][eE]|[mM]|[lL][lL]|[dD]))"], + + // Used to override the default (invalid) regex of the bloom pretokenizer. + // For more information, see https://github.com/huggingface/transformers.js/issues/94 + [` ?[^(\\s|[${BLOOM_SPLIT_CHARS}])]+`, ` ?[^\\s${BLOOM_SPLIT_CHARS}]+`], +]) + + +/** + * Represent a token added by the user on top of the existing Model vocabulary. + * AddedToken can be configured to specify the behavior they should have in various situations like: + * - Whether they should only match single words + * - Whether to include any whitespace on its left or right + */ +class AddedToken { + /** + * Creates a new instance of AddedToken. + * @param {Object} config Added token configuration object. + * @param {string} config.content The content of the added token. + * @param {number} config.id The id of the added token. + * @param {boolean} [config.single_word=false] Whether this token must be a single word or can break words. + * @param {boolean} [config.lstrip=false] Whether this token should strip whitespaces on its left. + * @param {boolean} [config.rstrip=false] Whether this token should strip whitespaces on its right. + * @param {boolean} [config.normalized=false] Whether this token should be normalized. + * @param {boolean} [config.special=false] Whether this token is special. + */ + constructor(config) { + this.content = config.content; + this.id = config.id; + this.single_word = config.single_word ?? false; + this.lstrip = config.lstrip ?? false; + this.rstrip = config.rstrip ?? false; + this.special = config.special ?? false; + this.normalized = config.normalized ?? null; + } +} + +/** + * Abstract base class for tokenizer models. + * + * @extends Callable + */ +class TokenizerModel extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * Creates a new instance of TokenizerModel. + * @param {Object} config The configuration object for the TokenizerModel. + */ + constructor(config) { + super(); + this.config = config; + + /** @type {string[]} */ + this.vocab = []; + + /** + * A mapping of tokens to ids. + * @type {Map} + */ + this.tokens_to_ids = new Map(); + + this.unk_token_id = undefined; + this.unk_token = undefined; + this.end_of_word_suffix = undefined; + + /** @type {boolean} Whether to fuse unknown tokens when encoding. Defaults to false. */ + this.fuse_unk = this.config.fuse_unk ?? false; + } + + /** + * Instantiates a new TokenizerModel instance based on the configuration object provided. + * @param {Object} config The configuration object for the TokenizerModel. + * @param {...*} args Optional arguments to pass to the specific TokenizerModel constructor. + * @returns {TokenizerModel} A new instance of a TokenizerModel. + * @throws Will throw an error if the TokenizerModel type in the config is not recognized. + */ + static fromConfig(config, ...args) { + switch (config.type) { + case 'WordPiece': + return new WordPieceTokenizer(config); + case 'Unigram': + // @ts-ignore + return new Unigram(config, ...args); + case 'BPE': + return new BPE(config); + + default: + // Some tokenizers, like for google-t5/t5-small, do not have a `type` field. + // In this case, we can infer the tokenizer type based on the structure of the `vocab` field. + if (config.vocab) { + if (Array.isArray(config.vocab)) { + // config.vocab is of type `[string, number][]` + // @ts-ignore + return new Unigram(config, ...args); + } else { + // @ts-ignore + return new LegacyTokenizerModel(config, ...args); + } + } + throw new Error(`Unknown TokenizerModel type: ${config.type}`); + } + } + + /** + * Internal function to call the TokenizerModel instance. + * @param {string[]} tokens The tokens to encode. + * @returns {string[]} The encoded tokens. + */ + _call(tokens) { + tokens = this.encode(tokens); + if (this.fuse_unk) { + // Fuse unknown tokens + tokens = fuse_unk(tokens, this.tokens_to_ids, this.unk_token_id); + } + return tokens; + } + + /** + * Encodes a list of tokens into a list of token IDs. + * @param {string[]} tokens The tokens to encode. + * @returns {string[]} The encoded tokens. + * @throws Will throw an error if not implemented in a subclass. + */ + encode(tokens) { + throw Error("encode should be implemented in subclass.") + } + + /** + * Converts a list of tokens into a list of token IDs. + * @param {string[]} tokens The tokens to convert. + * @returns {number[]} The converted token IDs. + */ + convert_tokens_to_ids(tokens) { + return tokens.map(t => this.tokens_to_ids.get(t) ?? this.unk_token_id); + } + + /** + * Converts a list of token IDs into a list of tokens. + * @param {number[]|bigint[]} ids The token IDs to convert. + * @returns {string[]} The converted tokens. + */ + convert_ids_to_tokens(ids) { + return ids.map(i => this.vocab[i] ?? this.unk_token); + } +} + +/** + * A subclass of TokenizerModel that uses WordPiece encoding to encode tokens. + * @extends TokenizerModel + */ +class WordPieceTokenizer extends TokenizerModel { + /** + * @param {Object} config The configuration object. + * @param {Object} config.vocab A mapping of tokens to ids. + * @param {string} config.unk_token The unknown token string. + * @param {string} config.continuing_subword_prefix The prefix to use for continuing subwords. + * @param {number} [config.max_input_chars_per_word=100] The maximum number of characters per word. + */ + constructor(config) { + super(config); + /** + * A mapping of tokens to ids. + * @type {Map} + */ + this.tokens_to_ids = objectToMap(config.vocab); + + /** + * The id of the unknown token. + * @type {number} + */ + this.unk_token_id = this.tokens_to_ids.get(config.unk_token); + + /** + * The unknown token string. + * @type {string} + */ + this.unk_token = config.unk_token; + + /** + * The maximum number of characters allowed per word. + * @type {number} + */ + this.max_input_chars_per_word = config.max_input_chars_per_word ?? 100; + + /** + * An array of tokens. + * @type {string[]} + */ + this.vocab = new Array(this.tokens_to_ids.size); + for (const [key, value] of this.tokens_to_ids) { + this.vocab[value] = key; + } + } + + /** + * Encodes an array of tokens using WordPiece encoding. + * @param {string[]} tokens The tokens to encode. + * @returns {string[]} An array of encoded tokens. + */ + encode(tokens) { + const outputTokens = []; + for (const token of tokens) { + const chars = [...token]; + if (chars.length > this.max_input_chars_per_word) { + outputTokens.push(this.unk_token); + continue; + } + + let isUnknown = false; + let start = 0; + const subTokens = []; + + while (start < chars.length) { + let end = chars.length; + let currentSubstring = null; + while (start < end) { + let substr = chars.slice(start, end).join(''); + + if (start > 0) { + substr = this.config.continuing_subword_prefix + substr; + } + if (this.tokens_to_ids.has(substr)) { + currentSubstring = substr; + break; + } + + --end; + } + if (currentSubstring === null) { + isUnknown = true; + break; + } + subTokens.push(currentSubstring); + start = end; + } + if (isUnknown) { + outputTokens.push(this.unk_token); + } else { + outputTokens.push(...subTokens); + } + } + + return outputTokens; + } + +} + +/** + * Class representing a Unigram tokenizer model. + * @extends TokenizerModel + */ +class Unigram extends TokenizerModel { + /** + * Create a new Unigram tokenizer model. + * @param {Object} config The configuration object for the Unigram model. + * @param {number} config.unk_id The ID of the unknown token + * @param {any[][]} config.vocab A 2D array representing a mapping of tokens to scores. + * @param {Object} moreConfig Additional configuration object for the Unigram model. + */ + constructor(config, moreConfig) { + super(config); + + const vocabSize = config.vocab.length; + this.vocab = new Array(vocabSize); + this.scores = new Array(vocabSize); + for (let i = 0; i < vocabSize; ++i) { + const piece = config.vocab[i]; + this.vocab[i] = piece[0]; + this.scores[i] = piece[1]; + } + + this.unk_token_id = config.unk_id; + this.unk_token = this.vocab[config.unk_id]; + + this.tokens_to_ids = new Map(this.vocab.map((x, i) => [x, i])); + this.bos_token = ' '; // beginning of a sentence token + + this.bos_token_id = this.tokens_to_ids.get(this.bos_token); // NOTE: may be undefined + this.eos_token = moreConfig.eos_token; + + this.eos_token_id = this.tokens_to_ids.get(this.eos_token); + this.unk_token = this.vocab[this.unk_token_id]; + + this.minScore = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.min)(this.scores)[0]; + + this.unk_score = this.minScore - 10.0; + this.scores[this.unk_token_id] = this.unk_score; + + this.trie = new _utils_data_structures_js__WEBPACK_IMPORTED_MODULE_5__.CharTrie(); + this.trie.extend(this.vocab); + + // NOTE: `fuse_unk` is hardcoded to true for Unigram models + // See: https://github.com/huggingface/tokenizers/blob/b58227c7f1ccf8b73ee2268354336da56d91e492/tokenizers/src/models/unigram/model.rs#L119 + this.fuse_unk = true; + } + + /** + * Populates lattice nodes. + * @param {TokenLattice} lattice The token lattice to populate with nodes. + */ + populateNodes(lattice) { + const chars = lattice.chars; + const mblen = 1; + let beginPos = 0; + while (beginPos < chars.length) { + let hasSingleNode = false; + + const tokens = []; + const sliced = chars.slice(beginPos).join(''); + const prefixedTokens = this.trie.commonPrefixSearch(sliced); + for (const token of prefixedTokens) { + tokens.push(token); + const tokenId = this.tokens_to_ids.get(token); + const tokenScore = this.scores[tokenId]; + const n = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.len)(token); + lattice.insert(beginPos, n, tokenScore, tokenId); + if (!hasSingleNode && n === mblen) { + hasSingleNode = true; + } + } + if (!hasSingleNode) { + lattice.insert(beginPos, mblen, this.unk_score, this.unk_token_id); + } + beginPos += mblen; + } + } + + /** + * Encodes an array of tokens into an array of subtokens using the unigram model. + * + * @param {string} normalized The normalized string. + * @returns {string[]} An array of subtokens obtained by encoding the input tokens using the unigram model. + */ + tokenize(normalized) { + const lattice = new _utils_data_structures_js__WEBPACK_IMPORTED_MODULE_5__.TokenLattice(normalized, this.bos_token_id, this.eos_token_id); + this.populateNodes(lattice); + return lattice.tokens(); + } + + /** + * Encodes an array of tokens using Unigram encoding. + * @param {string[]} tokens The tokens to encode. + * @returns {string[]} An array of encoded tokens. + */ + encode(tokens) { + const toReturn = []; + for (const token of tokens) { + const tokenized = this.tokenize(token); + toReturn.push(...tokenized); + } + return toReturn; + } + +} + +/** + * Returns list of utf-8 byte and a mapping to unicode strings. + * Specifically avoids mapping to whitespace/control characters the BPE code barfs on. + * @returns {Object} Object with utf-8 byte keys and unicode string values. + */ +const BYTES_TO_UNICODE = (() => { + // Returns list of utf-8 byte and a mapping to unicode strings. + // We specifically avoids mapping to whitespace/control characters + // the bpe code barfs on. + + const bs = [ + ...Array.from({ length: "~".charCodeAt(0) - "!".charCodeAt(0) + 1 }, (_, i) => i + "!".charCodeAt(0)), + ...Array.from({ length: "¬".charCodeAt(0) - "¡".charCodeAt(0) + 1 }, (_, i) => i + "¡".charCodeAt(0)), + ...Array.from({ length: "ÿ".charCodeAt(0) - "®".charCodeAt(0) + 1 }, (_, i) => i + "®".charCodeAt(0)), + ]; + const cs = bs.slice(); + let n = 0; + for (let b = 0; b < 256; ++b) { + if (!bs.includes(b)) { + bs.push(b); + cs.push(256 + n); + n += 1; + } + } + const ccs = cs.map(n => String.fromCharCode(n)); + return Object.fromEntries(bs.map((b, i) => [b, ccs[i]])); +})(); + +const UNICODE_TO_BYTES = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.reverseDictionary)(BYTES_TO_UNICODE); + + +/** + * @typedef {Object} BPENode + * @property {string} token The token associated with the node + * @property {number} bias A positional bias for the node. + * @property {number} [score] The score of the node. + * @property {BPENode} [prev] The previous node in the linked list. + * @property {BPENode} [next] The next node in the linked list. + */ + +/** + * BPE class for encoding text into Byte-Pair-Encoding (BPE) tokens. + * @extends TokenizerModel + */ +class BPE extends TokenizerModel { + /** + * Create a BPE instance. + * @param {Object} config The configuration object for BPE. + * @param {Object} config.vocab A mapping of tokens to ids. + * @param {string[]|[string, string][]} config.merges An array of BPE merges as strings. + * @param {string} config.unk_token The unknown token used for out of vocabulary words. + * @param {string} config.end_of_word_suffix The suffix to place at the end of each word. + * @param {string} [config.continuing_subword_suffix] The suffix to insert between words. + * @param {boolean} [config.byte_fallback=false] Whether to use spm byte-fallback trick (defaults to False) + * @param {boolean} [config.ignore_merges=false] Whether or not to match tokens with the vocab before using merges. + */ + constructor(config) { + super(config); + + /** @type {Map} */ + this.tokens_to_ids = objectToMap(config.vocab); + + this.unk_token_id = this.tokens_to_ids.get(config.unk_token); + this.unk_token = config.unk_token; + + this.vocab = new Array(this.tokens_to_ids.size); + for (const [key, value] of this.tokens_to_ids) { + this.vocab[value] = key; + } + + // Tokenizers >= 0.20.0 serializes BPE merges as a [string, string][] instead of a string[], + // which resolves the ambiguity for merges containing spaces. + const use_new_merge_format = Array.isArray(config.merges[0]); + + /** @type {[string, string][]} */ + this.merges = use_new_merge_format + ? /** @type {[string, string][]} */(config.merges) + : (/** @type {string[]} */(config.merges)).map(x => /** @type {[string, string]} */(x.split(' ', 2))); + this.bpe_ranks = new Map(this.merges.map((x, i) => [JSON.stringify(x), i])); + + this.end_of_word_suffix = config.end_of_word_suffix; + + // NOTE: `continuing_subword_suffix` is custom (to support `BlenderbotSmallTokenizer`) + this.continuing_subword_suffix = config.continuing_subword_suffix ?? null; + + this.byte_fallback = this.config.byte_fallback ?? false; + + if (this.byte_fallback) { + this.text_encoder = new TextEncoder(); + } + + this.ignore_merges = this.config.ignore_merges ?? false; + + /** @type {Map} */ + this.cache = new Map(); + } + + /** + * Apply Byte-Pair-Encoding (BPE) to a given token. Efficient heap-based priority + * queue implementation adapted from https://github.com/belladoreai/llama-tokenizer-js. + * @param {string} token The token to encode. + * @returns {string[]} The BPE encoded tokens. + */ + bpe(token) { + if (token.length === 0) { + return []; + } + + const cached = this.cache.get(token); + if (cached !== undefined) { + return cached; + } + + const word = Array.from(token); + if (this.end_of_word_suffix) { + word[word.length - 1] += this.end_of_word_suffix; + } + + let result = []; + if (word.length > 1) { + // Create a priority queue to store the nodes that will be merged. + // The comparator function compares the scores of the nodes. + const queue = new _utils_data_structures_js__WEBPACK_IMPORTED_MODULE_5__.PriorityQueue((a, b) => a.score < b.score); + + // Construct a doubly-linked list of nodes that will be inserted into the priority queue, + // starting with the individual characters. We also populate each node with a positional + // bias to break ties in the priority queue. + let startingNode = { + token: word[0], + bias: 0, + prev: null, + next: null, + } + + let previousNode = startingNode + for (let i = 1; i < word.length; ++i) { + const currentNode = { + bias: i / word.length, // Add fractional component to break ties + token: word[i], + prev: previousNode, + next: null, + } + previousNode.next = currentNode + this._add_node(queue, previousNode) + previousNode = currentNode + } + + while (!queue.isEmpty()) { + // Get the next node with the highest priority + const node = queue.pop(); + + // Check that this merge is still possible + if (node.deleted || !node.next || node.next.deleted) continue; + + // Here, we mark the current node (left side of the merge) and the next node (right side of the merge) as deleted. + // This is because they will both be replaced by a new node representing the merge result. + node.deleted = true; + node.next.deleted = true; + + // Next, we fix the node that comes before the current node (i.e., left side of the merge). + if (node.prev) { + + // Make a shallow copy of the previous node + const newPreviousNode = { ...node.prev }; + + // Mark the old previous node as deleted. This avoids erroneous merges later, + // because there may still be references to this node in the priority queue. + node.prev.deleted = true; + node.prev = newPreviousNode; + + // Update the reference of the previous node, by pointing its previous node to this new previous node. + if (newPreviousNode.prev) { + newPreviousNode.prev.next = newPreviousNode; + } else { + // If the previous of the previous node does not exist, it means that + // `newPreviousNode` must be the new `startingNode`. + startingNode = newPreviousNode; + } + } + + // Create a new node which represents the result of the merge. + const merged = { + token: node.token + node.next.token, + bias: node.bias, + prev: node.prev, + next: node.next.next, + } + + // We now consider where we can add the new merged node to the priority queue: + // 1. prev <-> merged + if (merged.prev) { + merged.prev.next = merged; + this._add_node(queue, merged.prev); + } else { + // If `merged.prev` does not exist, then `merged` must be the new `startingNode`. + startingNode = merged; + } + + // 2. merged <-> next + if (merged.next) { + merged.next.prev = merged; + this._add_node(queue, merged); + } + } + + // Traverse the linked list, starting from the `startingNode`, and collect the tokens. + for (let currentNode = startingNode; currentNode !== null; currentNode = currentNode.next) { + result.push(currentNode.token); + } + } else { + result = word; + } + + // Possibly append suffix + if (this.continuing_subword_suffix) { + // Do not append suffix to the last token + for (let i = 0; i < result.length - 1; ++i) { + result[i] += this.continuing_subword_suffix; + } + } + + // Save the result to the cache + this.cache.set(token, result); + + return result; + } + + + /** + * Helper function to add a node to the priority queue. + * @param {PriorityQueue} queue + * @param {BPENode} node + * @private + */ + _add_node(queue, node) { + // `score` is a measure of the merge priority: lower means higher priority + // We use the BPE rank as a measure of priority (i.e., the local of the merge in the merges list) + // We also add a fractional component to the score to break ties (with the earlier character having higher priority) + const rank = this.bpe_ranks.get(JSON.stringify([node.token, node.next.token])); + if (rank !== undefined) { + node.score = rank + node.bias; + queue.push(node); + } + } + + /** + * Encodes the input sequence of tokens using the BPE algorithm and returns the resulting subword tokens. + * @param {string[]} tokens The input sequence of tokens to encode. + * @returns {string[]} The resulting subword tokens after applying the BPE algorithm to the input sequence of tokens. + */ + encode(tokens) { + const outputTokens = []; + + for (const token of tokens) { + if (this.ignore_merges && this.tokens_to_ids.has(token)) { + outputTokens.push(token); + continue; + } + const bpe_token_list = this.bpe(token); + + for (const t of bpe_token_list) { + if (this.tokens_to_ids.has(t)) { + outputTokens.push(t); + } else if (this.byte_fallback) { + const byteTokens = Array.from(this.text_encoder.encode(t)) + .map(x => `<0x${x.toString(16).toUpperCase().padStart(2, '0')}>`); + if (byteTokens.every(x => this.tokens_to_ids.has(x))) { + // Ensure the byte tokens are actually in the vocabulary, otherwise + // we fall back to the unknown token. For more information, see + // https://github.com/huggingface/transformers/issues/28096. + outputTokens.push(...byteTokens); + } else { + outputTokens.push(this.unk_token); + } + } else { + outputTokens.push(this.unk_token); + } + } + } + + return outputTokens; + } + +} + +/** + * Legacy tokenizer class for tokenizers with only a vocabulary. + */ +class LegacyTokenizerModel extends TokenizerModel { + /** + * Create a LegacyTokenizerModel instance. + * @param {Object} config The configuration object for LegacyTokenizerModel. + * @param {Object} config.vocab A (possibly nested) mapping of tokens to ids. + * @param {Object} moreConfig Additional configuration object for the LegacyTokenizerModel model. + */ + constructor(config, moreConfig) { + super(config); + + /**@type {Map} */ + this.tokens_to_ids = objectToMap( + moreConfig.target_lang + ? config.vocab[moreConfig.target_lang] + : config.vocab + ); + + this.bos_token = moreConfig.bos_token; + this.bos_token_id = this.tokens_to_ids.get(this.bos_token); + + this.eos_token = moreConfig.eos_token; + this.eos_token_id = this.tokens_to_ids.get(this.eos_token); + + this.pad_token = moreConfig.pad_token; + this.pad_token_id = this.tokens_to_ids.get(this.pad_token); + + this.unk_token = moreConfig.unk_token; + this.unk_token_id = this.tokens_to_ids.get(this.unk_token); + + this.vocab = new Array(this.tokens_to_ids.size); + for (const [key, value] of this.tokens_to_ids) { + this.vocab[value] = key; + } + } + + encode(tokens) { + return tokens; + } +} + + +/** + * A base class for text normalization. + * @abstract + */ +class Normalizer extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * @param {Object} config The configuration object for the normalizer. + */ + constructor(config) { + super(); + this.config = config; + } + + /** + * Factory method for creating normalizers from config objects. + * @static + * @param {Object} config The configuration object for the normalizer. + * @returns {Normalizer} A Normalizer object. + * @throws {Error} If an unknown Normalizer type is specified in the config. + */ + static fromConfig(config) { + if (config === null) return null; + switch (config.type) { + case 'BertNormalizer': + return new BertNormalizer(config); + case 'Precompiled': + return new Precompiled(config); + case 'Sequence': + return new NormalizerSequence(config); + case 'Replace': + return new Replace(config); + case 'NFC': + return new NFC(config); + case 'NFKC': + return new NFKC(config); + case 'NFKD': + return new NFKD(config); + case 'Strip': + return new StripNormalizer(config); + case 'StripAccents': + return new StripAccents(config); + case 'Lowercase': + return new Lowercase(config); + case 'Prepend': + return new Prepend(config); + default: + throw new Error(`Unknown Normalizer type: ${config.type}`); + } + } + + /** + * Normalize the input text. + * @abstract + * @param {string} text The text to normalize. + * @returns {string} The normalized text. + * @throws {Error} If this method is not implemented in a subclass. + */ + normalize(text) { + throw Error("normalize should be implemented in subclass.") + } + + /** + * Alias for {@link Normalizer#normalize}. + * @param {string} text The text to normalize. + * @returns {string} The normalized text. + */ + _call(text) { + return this.normalize(text); + } + +} + +/** + * Replace normalizer that replaces occurrences of a pattern with a given string or regular expression. + * @extends Normalizer + */ +class Replace extends Normalizer { + /** + * Normalize the input text by replacing the pattern with the content. + * @param {string} text The input text to be normalized. + * @returns {string} The normalized text after replacing the pattern with the content. + */ + normalize(text) { + const pattern = createPattern(this.config.pattern); + return pattern === null + ? text + : text.replaceAll(pattern, this.config.content); + } +} + +/** + * A normalizer that applies Unicode normalization form C (NFC) to the input text. + * @extends Normalizer + */ +class NFC extends Normalizer { + /** + * Normalize the input text by applying Unicode normalization form C (NFC). + * @param {string} text The input text to be normalized. + * @returns {string} The normalized text. + */ + normalize(text) { + text = text.normalize('NFC') + return text; + } +} + +/** + * NFKC Normalizer. + * @extends Normalizer + */ +class NFKC extends Normalizer { + /** + * Normalize text using NFKC normalization. + * @param {string} text The text to be normalized. + * @returns {string} The normalized text. + */ + normalize(text) { + text = text.normalize('NFKC') + return text; + } +} +/** + * NFKD Normalizer. + * @extends Normalizer + */ +class NFKD extends Normalizer { + /** + * Normalize text using NFKD normalization. + * @param {string} text The text to be normalized. + * @returns {string} The normalized text. + */ + normalize(text) { + text = text.normalize('NFKD') + return text; + } +} + +/** + * A normalizer that strips leading and/or trailing whitespace from the input text. + */ +class StripNormalizer extends Normalizer { + /** + * Strip leading and/or trailing whitespace from the input text. + * @param {string} text The input text. + * @returns {string} The normalized text. + */ + normalize(text) { + if (this.config.strip_left && this.config.strip_right) { + // Fast path to avoid an extra trim call + text = text.trim(); + } else { + if (this.config.strip_left) { + text = text.trimStart(); + } + if (this.config.strip_right) { + text = text.trimEnd(); + } + } + return text; + } +} + +/** + * StripAccents normalizer removes all accents from the text. + * @extends Normalizer + */ +class StripAccents extends Normalizer { + /** + * Remove all accents from the text. + * @param {string} text The input text. + * @returns {string} The normalized text without accents. + */ + normalize(text) { + text = remove_accents(text); + return text; + } +} + +/** + * A Normalizer that lowercases the input string. + * @extends Normalizer + */ +class Lowercase extends Normalizer { + /** + * Lowercases the input string. + * @param {string} text The text to normalize. + * @returns {string} The normalized text. + */ + normalize(text) { + text = text.toLowerCase(); + return text; + } +} + +/** + * A Normalizer that prepends a string to the input string. + * @extends Normalizer + */ +class Prepend extends Normalizer { + /** + * Prepends the input string. + * @param {string} text The text to normalize. + * @returns {string} The normalized text. + */ + normalize(text) { + text = this.config.prepend + text; + return text; + } +} + +/** + * A Normalizer that applies a sequence of Normalizers. + * @extends Normalizer + */ +class NormalizerSequence extends Normalizer { + /** + * Create a new instance of NormalizerSequence. + * @param {Object} config The configuration object. + * @param {Object[]} config.normalizers An array of Normalizer configuration objects. + */ + constructor(config) { + super(config); + this.normalizers = config.normalizers.map(x => Normalizer.fromConfig(x)); + } + /** + * Apply a sequence of Normalizers to the input text. + * @param {string} text The text to normalize. + * @returns {string} The normalized text. + */ + normalize(text) { + return this.normalizers.reduce((t, normalizer) => { + return normalizer.normalize(t); + }, text); + } +} + +/** + * A class representing a normalizer used in BERT tokenization. + * @extends Normalizer + */ +class BertNormalizer extends Normalizer { + /** + * Adds whitespace around any CJK (Chinese, Japanese, or Korean) character in the input text. + * + * @param {string} text The input text to tokenize. + * @returns {string} The tokenized text with whitespace added around CJK characters. + */ + _tokenize_chinese_chars(text) { + /* Adds whitespace around any CJK character. */ + const output = []; + for (let i = 0; i < text.length; ++i) { + const char = text[i]; + const cp = char.charCodeAt(0); + if (is_chinese_char(cp)) { + output.push(" "); + output.push(char); + output.push(" "); + } else { + output.push(char); + } + } + return output.join(""); + } + + /** + * Strips accents from the given text. + * @param {string} text The text to strip accents from. + * @returns {string} The text with accents removed. + */ + stripAccents(text) { + // "Mark, Nonspacing" (Mn) + return text.normalize('NFD').replace(/\p{Mn}/gu, ''); + } + + + /** + * Checks whether `char` is a control character. + * @param {string} char The character to check. + * @returns {boolean} Whether `char` is a control character. + * @private + */ + _is_control(char) { + switch (char) { + case '\t': + case '\n': + case '\r': + // These are technically control characters but we count them as whitespace characters. + return false; + + default: + // Check if unicode category starts with C: + // Cc - Control + // Cf - Format + // Co - Private Use + // Cs - Surrogate + return /^\p{Cc}|\p{Cf}|\p{Co}|\p{Cs}$/u.test(char); + } + } + + /** + * Performs invalid character removal and whitespace cleanup on text. + * @param {string} text The text to clean. + * @returns {string} The cleaned text. + * @private + */ + _clean_text(text) { + const output = []; + for (const char of text) { + const cp = char.charCodeAt(0); + if (cp === 0 || cp === 0xFFFD || this._is_control(char)) { + continue; + } + if (/^\s$/.test(char)) { // is whitespace + output.push(" "); + } else { + output.push(char); + } + } + return output.join(""); + } + /** + * Normalizes the given text based on the configuration. + * @param {string} text The text to normalize. + * @returns {string} The normalized text. + */ + normalize(text) { + if (this.config.clean_text) { + text = this._clean_text(text); + } + + if (this.config.handle_chinese_chars) { + text = this._tokenize_chinese_chars(text); + } + + if (this.config.lowercase) { + text = text.toLowerCase(); + + if (this.config.strip_accents !== false) { + text = this.stripAccents(text); + } + } else if (this.config.strip_accents) { + text = this.stripAccents(text); + } + + return text; + } +} + +/** + * A callable class representing a pre-tokenizer used in tokenization. Subclasses + * should implement the `pre_tokenize_text` method to define the specific pre-tokenization logic. + * @extends Callable + */ +class PreTokenizer extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + /** + * Factory method that returns an instance of a subclass of `PreTokenizer` based on the provided configuration. + * + * @static + * @param {Object} config A configuration object for the pre-tokenizer. + * @returns {PreTokenizer} An instance of a subclass of `PreTokenizer`. + * @throws {Error} If the provided configuration object does not correspond to any known pre-tokenizer. + */ + static fromConfig(config) { + if (config === null) return null; + + switch (config.type) { + case 'BertPreTokenizer': + return new BertPreTokenizer(config); + case 'Sequence': + return new PreTokenizerSequence(config); + case 'Whitespace': + return new WhitespacePreTokenizer(config); + case 'WhitespaceSplit': + return new WhitespaceSplit(config); + case 'Metaspace': + return new MetaspacePreTokenizer(config); + + case 'ByteLevel': + return new ByteLevelPreTokenizer(config); + case 'Split': + return new SplitPreTokenizer(config); + case 'Punctuation': + return new PunctuationPreTokenizer(config); + case 'Digits': + return new DigitsPreTokenizer(config); + case 'Replace': + return new ReplacePreTokenizer(config); + default: + throw new Error(`Unknown PreTokenizer type: ${config.type}`); + } + } + + /** + * Method that should be implemented by subclasses to define the specific pre-tokenization logic. + * + * @abstract + * @param {string} text The text to pre-tokenize. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} The pre-tokenized text. + * @throws {Error} If the method is not implemented in the subclass. + */ + pre_tokenize_text(text, options) { + throw Error("pre_tokenize_text should be implemented in subclass.") + } + + /** + * Tokenizes the given text into pre-tokens. + * @param {string|string[]} text The text or array of texts to pre-tokenize. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} An array of pre-tokens. + */ + pre_tokenize(text, options) { + return (Array.isArray(text) + ? text.map(x => this.pre_tokenize_text(x, options)) + : this.pre_tokenize_text(text, options) + ).flat(); + } + + /** + * Alias for {@link PreTokenizer#pre_tokenize}. + * @param {string|string[]} text The text or array of texts to pre-tokenize. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} An array of pre-tokens. + */ + _call(text, options) { + return this.pre_tokenize(text, options); + } +} + +/** + * @extends PreTokenizer + */ +class BertPreTokenizer extends PreTokenizer { + /** + * A PreTokenizer that splits text into wordpieces using a basic tokenization scheme + * similar to that used in the original implementation of BERT. + * + * @param {Object} config The configuration object. + */ + constructor(config) { + super(); + // Construct a pattern which matches the rust implementation: + // https://github.com/huggingface/tokenizers/blob/b4fcc9ce6e4ad5806e82826f816acfdfdc4fcc67/tokenizers/src/pre_tokenizers/bert.rs#L11 + // Equivalent to removing whitespace and splitting on punctuation (both \p{P} and other ascii characters) + this.pattern = new RegExp(`[^\\s${PUNCTUATION_REGEX}]+|[${PUNCTUATION_REGEX}]`, 'gu'); + } + /** + * Tokenizes a single text using the BERT pre-tokenization scheme. + * + * @param {string} text The text to tokenize. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} An array of tokens. + */ + pre_tokenize_text(text, options) { + return text.trim().match(this.pattern) || []; + } +} + +/** + * A pre-tokenizer that splits text into Byte-Pair-Encoding (BPE) subwords. + * @extends PreTokenizer + */ +class ByteLevelPreTokenizer extends PreTokenizer { + /** + * Creates a new instance of the `ByteLevelPreTokenizer` class. + * @param {Object} config The configuration object. + */ + constructor(config) { + super(); + this.config = config; + + /** + * @type {boolean} Whether to add a leading space to the first word. + * This allows to treat the leading word just as any other word. + */ + this.add_prefix_space = this.config.add_prefix_space; + + /** + * @type {boolean} Whether the post processing step should trim offsets + * to avoid including whitespaces. + * @todo Use this in the pretokenization step. + */ + this.trim_offsets = this.config.trim_offsets; + + /** + * @type {boolean} Whether to use the standard GPT2 regex for whitespace splitting. + * Set it to False if you want to use your own splitting. Defaults to true. + */ + this.use_regex = this.config.use_regex ?? true; + this.pattern = /'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+/gu; + + this.byte_encoder = BYTES_TO_UNICODE; + this.text_encoder = new TextEncoder(); + } + + /** + * Tokenizes a single piece of text using byte-level tokenization. + * @param {string} text The text to tokenize. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} An array of tokens. + */ + pre_tokenize_text(text, options) { + // Add a leading space if the option is enabled + if (this.add_prefix_space && !text.startsWith(' ')) { + text = ' ' + text; + } + + // Split on whitespace and punctuation + const tokens = this.use_regex ? (text.match(this.pattern) || []) : [text]; + + // Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case) + return tokens.map( + token => Array.from(this.text_encoder.encode(token), byte => this.byte_encoder[byte]).join('') + ); + } +} + +/** + * @typedef {'removed'|'isolated'|'mergedWithPrevious'|'mergedWithNext'|'contiguous'} SplitDelimiterBehavior + */ + +/** + * Splits text using a given pattern. + * @extends PreTokenizer + */ +class SplitPreTokenizer extends PreTokenizer { + /** + * @param {Object} config The configuration options for the pre-tokenizer. + * @param {Object} config.pattern The pattern used to split the text. Can be a string or a regex object. + * @param {string|undefined} config.pattern.String The string to use for splitting. Only defined if the pattern is a string. + * @param {string|undefined} config.pattern.Regex The regex to use for splitting. Only defined if the pattern is a regex. + * @param {SplitDelimiterBehavior} config.behavior The behavior to use when splitting. + * @param {boolean} config.invert Whether to split (invert=false) or match (invert=true) the pattern. + */ + constructor(config) { + super(); + this.config = config; + // TODO support all behaviours (config.behavior) + + this.pattern = createPattern(this.config.pattern, this.config.invert); + } + + /** + * Tokenizes text by splitting it using the given pattern. + * @param {string} text The text to tokenize. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} An array of tokens. + */ + pre_tokenize_text(text, options) { + if (this.pattern === null) { + return []; + } + + if (this.config.invert) { + return text.match(this.pattern) || []; + } else { + return regexSplit(text, this.pattern); + } + } +} + +/** + * Splits text based on punctuation. + * @extends PreTokenizer + */ +class PunctuationPreTokenizer extends PreTokenizer { + /** + * @param {Object} config The configuration options for the pre-tokenizer. + * @param {SplitDelimiterBehavior} config.behavior The behavior to use when splitting. + */ + constructor(config) { + super(); + this.config = config; + this.pattern = new RegExp(`[^${PUNCTUATION_REGEX}]+|[${PUNCTUATION_REGEX}]+`, 'gu'); + } + + /** + * Tokenizes text by splitting it using the given pattern. + * @param {string} text The text to tokenize. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} An array of tokens. + */ + pre_tokenize_text(text, options) { + return text.match(this.pattern) || []; + } +} + + +/** + * Splits text based on digits. + * @extends PreTokenizer + */ +class DigitsPreTokenizer extends PreTokenizer { + /** + * @param {Object} config The configuration options for the pre-tokenizer. + * @param {boolean} config.individual_digits Whether to split on individual digits. + */ + constructor(config) { + super(); + this.config = config; + + // Construct a pattern which matches the rust implementation: + const digit_pattern = `[^\\d]+|\\d${this.config.individual_digits ? '' : '+'}`; + this.pattern = new RegExp(digit_pattern, 'gu'); + } + + /** + * Tokenizes text by splitting it using the given pattern. + * @param {string} text The text to tokenize. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} An array of tokens. + */ + pre_tokenize_text(text, options) { + return text.match(this.pattern) || []; + } +} + +/** + * @typedef {Object} PostProcessedOutput + * @property {string[]} tokens List of token produced by the post-processor. + * @property {number[]} [token_type_ids] List of token type ids produced by the post-processor. + */ + + +/** + * @typedef {Object} EncodingSingle + * @property {number[]} input_ids List of token ids to be fed to a model. + * @property {number[]} attention_mask List of token type ids to be fed to a model + * @property {number[]} [token_type_ids] List of indices specifying which tokens should be attended to by the model + */ + + +/** + * @extends Callable + */ +class PostProcessor extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + + /** + * @param {Object} config The configuration for the post-processor. + */ + constructor(config) { + super(); + this.config = config; + } + + /** + * Factory method to create a PostProcessor object from a configuration object. + * + * @param {Object} config Configuration object representing a PostProcessor. + * @returns {PostProcessor} A PostProcessor object created from the given configuration. + * @throws {Error} If an unknown PostProcessor type is encountered. + */ + static fromConfig(config) { + if (config === null) return null; + switch (config.type) { + case 'TemplateProcessing': + return new TemplateProcessing(config); + + case 'ByteLevel': + return new ByteLevelPostProcessor(config); + + case 'RobertaProcessing': + return new RobertaProcessing(config); + case 'BertProcessing': + return new BertProcessing(config); + + case 'Sequence': + return new PostProcessorSequence(config); + default: + throw new Error(`Unknown PostProcessor type: ${config.type}`); + } + } + + /** + * Method to be implemented in subclass to apply post-processing on the given tokens. + * + * @param {Array} tokens The input tokens to be post-processed. + * @param {...*} args Additional arguments required by the post-processing logic. + * @returns {PostProcessedOutput} The post-processed tokens. + * @throws {Error} If the method is not implemented in subclass. + */ + post_process(tokens, ...args) { + throw Error("post_process should be implemented in subclass.") + } + + /** + * Alias for {@link PostProcessor#post_process}. + * @param {Array} tokens The text or array of texts to post-process. + * @param {...*} args Additional arguments required by the post-processing logic. + * @returns {PostProcessedOutput} The post-processed tokens. + */ + _call(tokens, ...args) { + return this.post_process(tokens, ...args); + } +} + +/** + * A post-processor that adds special tokens to the beginning and end of the input. + */ +class BertProcessing extends PostProcessor { + /** + * @param {Object} config The configuration for the post-processor. + * @param {string[]} config.cls The special tokens to add to the beginning of the input. + * @param {string[]} config.sep The special tokens to add to the end of the input. + */ + constructor(config) { + super(config); + // TODO use all of config: add_prefix_space, trim_offsets + + this.cls = config.cls[0]; + this.sep = config.sep[0]; + } + + /** + * Adds the special tokens to the beginning and end of the input. + * @param {string[]} tokens The input tokens. + * @param {string[]} [tokens_pair=null] An optional second set of input tokens. + * @returns {PostProcessedOutput} The post-processed tokens with the special tokens added to the beginning and end. + */ + post_process(tokens, tokens_pair = null, { + add_special_tokens = true, + } = {}) { + if (add_special_tokens) { + tokens = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)([this.cls], tokens, [this.sep]); + } + + let token_type_ids = new Array(tokens.length).fill(0); + if (tokens_pair !== null) { + // NOTE: It is intended to add 2 EOS tokens after the first set of tokens + // https://github.com/huggingface/tokenizers/issues/983 + const middle = (add_special_tokens && this instanceof RobertaProcessing) + ? [this.sep] + : []; + const after = add_special_tokens ? [this.sep] : []; + + tokens = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(tokens, middle, tokens_pair, after); + token_type_ids = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(token_type_ids, new Array(tokens_pair.length + middle.length + after.length).fill(1)); + } + return { tokens, token_type_ids }; + } +} +class RobertaProcessing extends BertProcessing { } // NOTE: extends BertProcessing + +/** + * Post processor that replaces special tokens in a template with actual tokens. + * @extends PostProcessor + */ +class TemplateProcessing extends PostProcessor { + /** + * Creates a new instance of `TemplateProcessing`. + * @param {Object} config The configuration options for the post processor. + * @param {Array} config.single The template for a single sequence of tokens. + * @param {Array} config.pair The template for a pair of sequences of tokens. + */ + constructor(config) { + super(config); + + this.single = config.single; + this.pair = config.pair; + } + + /** + * Replaces special tokens in the template with actual tokens. + * @param {string[]} tokens The list of tokens for the first sequence. + * @param {string[]} [tokens_pair=null] The list of tokens for the second sequence (optional). + * @returns {PostProcessedOutput} An object containing the list of tokens with the special tokens replaced with actual tokens. + */ + post_process(tokens, tokens_pair = null, { + add_special_tokens = true, + } = {}) { + const type = tokens_pair === null ? this.single : this.pair + + let processedTokens = []; + let types = []; + for (const item of type) { + if ('SpecialToken' in item) { + if (add_special_tokens) { + processedTokens.push(item.SpecialToken.id); + types.push(item.SpecialToken.type_id); + } + } else if ('Sequence' in item) { + if (item.Sequence.id === 'A') { + processedTokens = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(processedTokens, tokens); + types = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(types, new Array(tokens.length).fill(item.Sequence.type_id)); + + } else if (item.Sequence.id === 'B') { + processedTokens = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(processedTokens, tokens_pair); + types = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(types, new Array(tokens_pair.length).fill(item.Sequence.type_id)); + } + } + } + return { tokens: processedTokens, token_type_ids: types }; + } +} + +/** + * A PostProcessor that returns the given tokens as is. + * @extends PostProcessor + */ +class ByteLevelPostProcessor extends PostProcessor { + /** + * Post process the given tokens. + * @param {string[]} tokens The list of tokens for the first sequence. + * @param {string[]} [tokens_pair=null] The list of tokens for the second sequence (optional). + * @returns {PostProcessedOutput} An object containing the post-processed tokens. + */ + post_process(tokens, tokens_pair = null) { + if (tokens_pair) { + tokens = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(tokens, tokens_pair); + } + return { tokens }; + } +} + + +/** + * A post-processor that applies multiple post-processors in sequence. + */ +class PostProcessorSequence extends PostProcessor { + + /** + * Creates a new instance of PostProcessorSequence. + * @param {Object} config The configuration object. + * @param {Object[]} config.processors The list of post-processors to apply. + */ + constructor(config) { + super(config); + + this.processors = config.processors.map(x => PostProcessor.fromConfig(x)); + } + + /** + * Post process the given tokens. + * @param {string[]} tokens The list of tokens for the first sequence. + * @param {string[]} [tokens_pair=null] The list of tokens for the second sequence (optional). + * @returns {PostProcessedOutput} An object containing the post-processed tokens. + */ + post_process(tokens, tokens_pair = null, options = {}) { + let token_type_ids; + for (const processor of this.processors) { + if (processor instanceof ByteLevelPostProcessor) { + // Special case where we need to pass the tokens_pair to the post-processor + const output = processor.post_process(tokens); + tokens = output.tokens; + if (tokens_pair) { + const pair_output = processor.post_process(tokens_pair); + tokens_pair = pair_output.tokens; + } + } else { + const output = processor.post_process(tokens, tokens_pair, options); + tokens = output.tokens; + token_type_ids = output.token_type_ids; + } + } + return { tokens, token_type_ids }; + } +} + +/** + * The base class for token decoders. + * @extends Callable + */ +class Decoder extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + + /** + * Creates an instance of `Decoder`. + * + * @param {Object} config The configuration object. + */ + constructor(config) { + super(); + this.config = config; + + /** @type {AddedToken[]} */ + this.added_tokens = []; + this.end_of_word_suffix = null; + this.trim_offsets = config.trim_offsets; + } + + /** + * Creates a decoder instance based on the provided configuration. + * + * @param {Object} config The configuration object. + * @returns {Decoder} A decoder instance. + * @throws {Error} If an unknown decoder type is provided. + */ + static fromConfig(config) { + if (config === null) return null; + switch (config.type) { + case 'WordPiece': + return new WordPieceDecoder(config); + case 'Metaspace': + return new MetaspaceDecoder(config); + case 'ByteLevel': + return new ByteLevelDecoder(config); + + case 'Replace': + return new ReplaceDecoder(config); + case 'ByteFallback': + return new ByteFallback(config); + case 'Fuse': + return new FuseDecoder(config); + case 'Strip': + return new StripDecoder(config); + + case 'Sequence': + return new DecoderSequence(config); + + case 'CTC': + return new CTCDecoder(config); + case 'BPEDecoder': + return new BPEDecoder(config); + default: + throw new Error(`Unknown Decoder type: ${config.type}`); + } + } + + /** + * Calls the `decode` method. + * + * @param {string[]} tokens The list of tokens. + * @returns {string} The decoded string. + */ + _call(tokens) { + return this.decode(tokens); + } + + /** + * Decodes a list of tokens. + * @param {string[]} tokens The list of tokens. + * @returns {string} The decoded string. + */ + decode(tokens) { + return this.decode_chain(tokens).join(''); + } + + /** + * Apply the decoder to a list of tokens. + * + * @param {string[]} tokens The list of tokens. + * @returns {string[]} The decoded list of tokens. + * @throws {Error} If the `decode_chain` method is not implemented in the subclass. + */ + decode_chain(tokens) { + throw Error("`decode_chain` should be implemented in subclass.") + } + +} + +class ReplaceDecoder extends Decoder { + + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + const pattern = createPattern(this.config.pattern); + return pattern === null + ? tokens + : tokens.map(token => token.replaceAll(pattern, this.config.content)) + } +} + + +class ByteFallback extends Decoder { + constructor(config) { + super(config); + + this.text_decoder = new TextDecoder(); + } + + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + + const new_tokens = []; + let previous_byte_tokens = []; + + for (const token of tokens) { + let bytes = null; + if (token.length === 6 && token.startsWith('<0x') && token.endsWith('>')) { + const byte = parseInt(token.slice(3, 5), 16); + if (!isNaN(byte)) { + bytes = byte; + } + } + if (bytes !== null) { + previous_byte_tokens.push(bytes); + } else { + if (previous_byte_tokens.length > 0) { + const string = this.text_decoder.decode(Uint8Array.from(previous_byte_tokens)); + new_tokens.push(string); + previous_byte_tokens = []; + } + new_tokens.push(token); + } + } + if (previous_byte_tokens.length > 0) { + const string = this.text_decoder.decode(Uint8Array.from(previous_byte_tokens)); + new_tokens.push(string); + previous_byte_tokens = []; + } + + return new_tokens; + } +} + +/** + * Fuse simply fuses all tokens into one big string. + * It's usually the last decoding step anyway, but this decoder + * exists incase some decoders need to happen after that step + */ +class FuseDecoder extends Decoder { + + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + return [tokens.join('')]; + } +} + + +class StripDecoder extends Decoder { + constructor(config) { + super(config); + + this.content = this.config.content; + this.start = this.config.start; + this.stop = this.config.stop; + } + + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + return tokens.map(token => { + let start_cut = 0; + for (let i = 0; i < this.start; ++i) { + if (token[i] === this.content) { + start_cut = i + 1; + continue; + } else { + break; + } + } + + let stop_cut = token.length; + for (let i = 0; i < this.stop; ++i) { + const index = token.length - i - 1; + if (token[index] === this.content) { + stop_cut = index; + continue; + } else { + break; + } + } + + return token.slice(start_cut, stop_cut) + }); + } +} + +/** + * A decoder that decodes a list of WordPiece tokens into a single string. + * @extends Decoder + */ +class WordPieceDecoder extends Decoder { + + /** + * Creates a new instance of WordPieceDecoder. + * @param {Object} config The configuration object. + * @param {string} config.prefix The prefix used for WordPiece encoding. + * @param {boolean} config.cleanup Whether to cleanup the decoded string. + */ + constructor(config) { + super(config); + this.cleanup = config.cleanup; + } + + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + return tokens.map((token, i) => { + if (i !== 0) { + if (token.startsWith(this.config.prefix)) { + // NOTE: .replace() is intended; only replace first occurrence + token = token.replace(this.config.prefix, ''); + } else { + token = ' ' + token; + } + } + if (this.cleanup) { + token = clean_up_tokenization(token) + } + + return token; + }); + } +} + +/** + * Byte-level decoder for tokenization output. Inherits from the `Decoder` class. + * @extends Decoder + */ +class ByteLevelDecoder extends Decoder { + + /** + * Create a `ByteLevelDecoder` object. + * @param {Object} config Configuration object. + */ + constructor(config) { + super(config); + + this.byte_decoder = UNICODE_TO_BYTES; + this.text_decoder = new TextDecoder("utf-8", { + fatal: false, + ignoreBOM: true, + }); + + this.end_of_word_suffix = null; + } + + /** + * Convert an array of tokens to string by decoding each byte. + * @param {string[]} tokens Array of tokens to be decoded. + * @returns {string} The decoded string. + */ + convert_tokens_to_string(tokens) { + const text = tokens.join(''); + const byteArray = new Uint8Array([...text].map(c => this.byte_decoder[c])); + const decoded_text = this.text_decoder.decode(byteArray); + return decoded_text; + } + + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + // TODO move to base class (like HF) + // tokens === filtered_tokens + + // To avoid mixing byte-level and unicode for byte-level BPT + // we need to build string separately for added tokens and byte-level tokens + // cf. https://github.com/huggingface/transformers/issues/1133 + const sub_texts = []; + let current_sub_text = []; + for (const token of tokens) { + // tokens sent here are already filtered, so we don't need to do this + // if (skip_special_tokens && this.all_special_ids.includes(token)) { + // continue; + // } + + if (this.added_tokens.find(x => x.content === token) !== undefined) { + if (current_sub_text.length > 0) { + sub_texts.push(this.convert_tokens_to_string(current_sub_text)); + current_sub_text = []; + } + sub_texts.push(token); + } else { + current_sub_text.push(token); + } + } + if (current_sub_text.length > 0) { + sub_texts.push(this.convert_tokens_to_string(current_sub_text)); + } + + // TODO add spaces_between_special_tokens and clean_up_tokenization_spaces options + + return sub_texts; + } +} + +/** + * The CTC (Connectionist Temporal Classification) decoder. + * See https://github.com/huggingface/tokenizers/blob/bb38f390a61883fc2f29d659af696f428d1cda6b/tokenizers/src/decoders/ctc.rs + */ +class CTCDecoder extends Decoder { + + constructor(config) { + super(config); + + this.pad_token = this.config.pad_token; + this.word_delimiter_token = this.config.word_delimiter_token; + this.cleanup = this.config.cleanup; + } + /** + * Converts a connectionist-temporal-classification (CTC) output tokens into a single string. + * @param {string[]} tokens Array of tokens to be decoded. + * @returns {string} The decoded string. + */ + convert_tokens_to_string(tokens) { + if (tokens.length === 0) return ''; + + // group same tokens into non-repeating tokens in CTC style decoding + const grouped_tokens = [tokens[0]]; + for (let i = 1; i < tokens.length; ++i) { + if (tokens[i] !== grouped_tokens.at(-1)) { + grouped_tokens.push(tokens[i]); + } + } + + // filter self.pad_token which is used as CTC-blank token + const filtered_tokens = grouped_tokens.filter(token => token !== this.pad_token); + + let text = filtered_tokens.join(''); + if (this.cleanup) { + // cleanup and replace delimiter token + text = clean_up_tokenization(text) + .replaceAll(this.word_delimiter_token, ' ') + .trim(); + } + return text; + } + + + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + return [this.convert_tokens_to_string(tokens)]; + } +} + +/** + * Apply a sequence of decoders. + * @extends Decoder + */ +class DecoderSequence extends Decoder { + + /** + * Creates a new instance of DecoderSequence. + * @param {Object} config The configuration object. + * @param {Object[]} config.decoders The list of decoders to apply. + */ + constructor(config) { + super(config); + this.decoders = config.decoders.map(x => Decoder.fromConfig(x)); + } + + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + // Use reduce to apply each decoder to the tokens + return this.decoders.reduce((toks, decoder) => { + return decoder.decode_chain(toks); + }, tokens); + } + +} + +class BPEDecoder extends Decoder { + constructor(config) { + super(config); + + this.suffix = this.config.suffix; + } + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + return tokens.map((token, i) => { + return token.replaceAll(this.suffix, (i === tokens.length - 1) ? '' : ' ') + }); + } +} + +// Custom decoder for VITS +class VitsDecoder extends Decoder { + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + let decoded = ''; + for (let i = 1; i < tokens.length; i += 2) { + decoded += tokens[i]; + } + return [decoded]; + } +} + + +/** + * This PreTokenizer replaces spaces with the given replacement character, adds a prefix space if requested, + * and returns a list of tokens. + * @extends PreTokenizer + */ +class MetaspacePreTokenizer extends PreTokenizer { + /** + * @param {Object} config The configuration object for the MetaspacePreTokenizer. + * @param {boolean} config.add_prefix_space Whether to add a prefix space to the first token. + * @param {string} config.replacement The character to replace spaces with. + * @param {string} [config.str_rep=config.replacement] An optional string representation of the replacement character. + * @param {'first'|'never'|'always'} [config.prepend_scheme='always'] The metaspace prepending scheme. + */ + constructor(config) { + super(); + + this.addPrefixSpace = config.add_prefix_space; + this.replacement = config.replacement; + this.strRep = config.str_rep || this.replacement; + this.prepend_scheme = config.prepend_scheme ?? 'always'; + } + + /** + * This method takes a string, replaces spaces with the replacement character, + * adds a prefix space if requested, and returns a new list of tokens. + * @param {string} text The text to pre-tokenize. + * @param {Object} [options] The options for the pre-tokenization. + * @param {number} [options.section_index] The index of the section to pre-tokenize. + * @returns {string[]} A new list of pre-tokenized tokens. + */ + pre_tokenize_text(text, { + section_index = undefined, + } = {}) { + + let normalized = text.replaceAll(' ', this.strRep); + + if ( + // We add a prefix space if: + // (1) The addPrefixSpace option is enabled and the normalized + // token does not already start with the replacement character. + (this.addPrefixSpace && !normalized.startsWith(this.replacement)) + + // and (2) either: + // (a) prepend_scheme is 'always' + // (b) prepend_scheme is 'first' and this is the first section + && ( + this.prepend_scheme === 'always' || + (this.prepend_scheme === 'first' && section_index === 0) + ) + ) { + normalized = this.strRep + normalized; + } + return [normalized]; + } +} + +/** + * MetaspaceDecoder class extends the Decoder class and decodes Metaspace tokenization. + * @extends Decoder + */ +class MetaspaceDecoder extends Decoder { + /** + * Constructs a new MetaspaceDecoder object. + * @param {Object} config The configuration object for the MetaspaceDecoder. + * @param {boolean} config.add_prefix_space Whether to add a prefix space to the decoded string. + * @param {string} config.replacement The string to replace spaces with. + */ + constructor(config) { + super(config); + + this.addPrefixSpace = config.add_prefix_space; + this.replacement = config.replacement; + } + + /** @type {Decoder['decode_chain']} */ + decode_chain(tokens) { + const result = []; + for (let i = 0; i < tokens.length; ++i) { + let normalized = tokens[i].replaceAll(this.replacement, ' '); + if (this.addPrefixSpace && i == 0 && normalized.startsWith(' ')) { + normalized = normalized.substring(1); + } + result.push(normalized); + } + return result; + } +} + +/** + * A normalizer that applies a precompiled charsmap. + * This is useful for applying complex normalizations in C++ and exposing them to JavaScript. + * @extends Normalizer + * @param {Object} config The configuration object for the Precompiled normalizer. + * @param {Object} config.precompiled_charsmap The precompiled charsmap object. + */ +class Precompiled extends Normalizer { + /** + * Create a new instance of Precompiled normalizer. + * @param {Object} config The configuration object. + * @param {any} config.precompiled_charsmap Precompiled chars mapping. + */ + constructor(config) { + super(config); + this.charsmap = config.precompiled_charsmap; + } + + /** + * Normalizes the given text by applying the precompiled charsmap. + * @param {string} text The text to normalize. + * @returns {string} The normalized text. + */ + normalize(text) { + // As stated in the sentencepiece normalization docs (https://github.com/google/sentencepiece/blob/master/doc/normalization.md#use-pre-defined-normalization-rule), + // there are 5 pre-defined normalization rules: + // 1. nmt_nfkc: NFKC normalization with some additional normalization around spaces. (default) + // 2. nfkc: original NFKC normalization. + // 3. nmt_nfkc_cf: nmt_nfkc + Unicode case folding (mostly lower casing) + // 4. nfkc_cf: nfkc + Unicode case folding. + // 5. identity: no normalization + // + // For now, we only implement the default (nmt_nfkc). + // See https://raw.githubusercontent.com/google/sentencepiece/master/data/nmt_nfkc.tsv for the full list of rules. + // TODO: detect when a different `this.charsmap` is used. + + text = text.replace(/[\u0001-\u0008\u000B\u000E-\u001F\u007F\u008F\u009F]/gm, ''); // Remove control characters + text = text.replace(/[\u0009\u000A\u000C\u000D\u00A0\u1680\u2000-\u200F\u2028\u2029\u202F\u205F\u2581\u3000\uFEFF\uFFFD]/gm, '\u0020'); // Replace certain characters with a space + + if (text.includes('\uFF5E')) { + // To match the sentencepiece implementation 100%, we must handle a very strange edge-case. + // For some reason, the "Fullwidth Tilde" character (\uFF5E) should not be converted to the standard Tilde character (\u007E). + // However, NFKC normalization does do this conversion. As a result, we split the string on the Fullwidth Tilde character, + // perform NFKC normalization on each substring, and then join them back together with the Fullwidth Tilde character. + const parts = text.split('\uFF5E'); + text = parts.map(part => part.normalize('NFKC')).join('\uFF5E'); + } else { + text = text.normalize('NFKC'); + } + + return text; + } +} + +/** + * A pre-tokenizer that applies a sequence of pre-tokenizers to the input text. + * @extends PreTokenizer + */ +class PreTokenizerSequence extends PreTokenizer { + /** + * Creates an instance of PreTokenizerSequence. + * @param {Object} config The configuration object for the pre-tokenizer sequence. + * @param {Object[]} config.pretokenizers An array of pre-tokenizer configurations. + */ + constructor(config) { + super(); + this.tokenizers = config.pretokenizers.map(x => PreTokenizer.fromConfig(x)); + } + + /** + * Applies each pre-tokenizer in the sequence to the input text in turn. + * @param {string} text The text to pre-tokenize. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} The pre-tokenized text. + */ + pre_tokenize_text(text, options) { + // Use reduce to apply each tokenizer to the text + return this.tokenizers.reduce((preTokenizedText, tokenizer) => { + return tokenizer.pre_tokenize(preTokenizedText, options); + }, [text]); + } +} + +/** + * Splits on word boundaries (using the following regular expression: `\w+|[^\w\s]+`). + */ +class WhitespacePreTokenizer extends PreTokenizer { + /** + * Creates an instance of WhitespacePreTokenizer. + * @param {Object} config The configuration object for the pre-tokenizer. + */ + constructor(config) { + super(); + } + /** + * Pre-tokenizes the input text by splitting it on word boundaries. + * @param {string} text The text to be pre-tokenized. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} An array of tokens produced by splitting the input text on whitespace. + */ + pre_tokenize_text(text, options) { + return text.match(/\w+|[^\w\s]+/g) || []; + } +} + +/** + * Splits a string of text by whitespace characters into individual tokens. + * @extends PreTokenizer + */ +class WhitespaceSplit extends PreTokenizer { + /** + * Creates an instance of WhitespaceSplit. + * @param {Object} config The configuration object for the pre-tokenizer. + */ + constructor(config) { + super(); + } + /** + * Pre-tokenizes the input text by splitting it on whitespace characters. + * @param {string} text The text to be pre-tokenized. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} An array of tokens produced by splitting the input text on whitespace. + */ + pre_tokenize_text(text, options) { + return whitespace_split(text); + } +} + +// NOTE: `ReplacePreTokenizer` is custom (to support `BlenderbotSmallTokenizer`) +class ReplacePreTokenizer extends PreTokenizer { + /** + * @param {Object} config The configuration options for the pre-tokenizer. + * @param {Object} config.pattern The pattern used to split the text. Can be a string or a regex object. + * @param {string} config.content What to replace the pattern with. + */ + constructor(config) { + super(); + this.config = config; + this.pattern = createPattern(this.config.pattern); + this.content = this.config.content; + } + + /** + * Pre-tokenizes the input text by replacing certain characters. + * @param {string} text The text to be pre-tokenized. + * @param {Object} [options] Additional options for the pre-tokenization logic. + * @returns {string[]} An array of tokens produced by replacing certain characters. + */ + pre_tokenize_text(text, options) { + if (this.pattern === null) { + return [text]; + } + return [text.replaceAll(this.pattern, this.config.content)]; + } +} + +const SPECIAL_TOKEN_ATTRIBUTES = [ + 'bos_token', + 'eos_token', + 'unk_token', + 'sep_token', + 'pad_token', + 'cls_token', + 'mask_token', + // additional_special_tokens (TODO) +] + +/** + * + * Helper function for padding values of an object, which are each arrays. + * NOTE: No additional checks are made here for validity of arguments. + * @param {Record} item The input object. + * @param {number} length The length to pad to. + * @param {(key: string) => any} value_fn Determine the value to fill the array, based on its key. + * @param {string} side Which side to pad the array. + * @private + */ +function padHelper(item, length, value_fn, side) { + for (const key of Object.keys(item)) { + const diff = length - item[key].length; + const value = value_fn(key); + + const padData = new Array(diff).fill(value); + item[key] = side === 'right' + ? (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(item[key], padData) + : (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(padData, item[key]); + } +} + +/** + * Helper function for truncating values of an object, which are each arrays. + * NOTE: No additional checks are made here for validity of arguments. + * @param {Record} item The input object. + * @param {number} length The length to truncate to. + * @private + */ +function truncateHelper(item, length) { + // Setting .length to a lower value truncates the array in-place: + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length + for (const key of Object.keys(item)) { + item[key].length = length; + } +} + + +/** + * @typedef {Object} Message + * @property {string} role The role of the message (e.g., "user" or "assistant" or "system"). + * @property {string} content The content of the message. + */ + +class PreTrainedTokenizer extends _utils_generic_js__WEBPACK_IMPORTED_MODULE_0__.Callable { + return_token_type_ids = false; + + padding_side = 'right'; + /** + * Create a new PreTrainedTokenizer instance. + * @param {Object} tokenizerJSON The JSON of the tokenizer. + * @param {Object} tokenizerConfig The config of the tokenizer. + */ + constructor(tokenizerJSON, tokenizerConfig) { + super(); + + this._tokenizer_config = tokenizerConfig; + + // Construct parts of the tokenizer from the JSON + this.normalizer = Normalizer.fromConfig(tokenizerJSON.normalizer); + this.pre_tokenizer = PreTokenizer.fromConfig(tokenizerJSON.pre_tokenizer); + this.model = TokenizerModel.fromConfig(tokenizerJSON.model, tokenizerConfig); + this.post_processor = PostProcessor.fromConfig(tokenizerJSON.post_processor); + this.decoder = Decoder.fromConfig(tokenizerJSON.decoder); + + // Add added_tokens to model + this.special_tokens = []; + this.all_special_ids = []; + + /** @type {AddedToken[]} */ + this.added_tokens = []; + for (const addedToken of tokenizerJSON.added_tokens) { + const token = new AddedToken(addedToken); + this.added_tokens.push(token); + + this.model.tokens_to_ids.set(token.content, token.id); + this.model.vocab[token.id] = token.content; + + if (token.special) { + this.special_tokens.push(token.content); + this.all_special_ids.push(token.id); + } + } + + // Update additional_special_tokens + this.additional_special_tokens = tokenizerConfig.additional_special_tokens ?? []; + this.special_tokens.push(...this.additional_special_tokens); + this.special_tokens = [...new Set(this.special_tokens)]; // Remove duplicates + + if (this.decoder) { + // Slight hack, but it prevents code duplication: + this.decoder.added_tokens = this.added_tokens; + + // Another slight hack to add `end_of_word_suffix` (if present) to the decoder + // This is needed for cases where BPE model and ByteLevel decoder are used + // For more information, see https://github.com/huggingface/transformers.js/issues/74 + // TODO: save this to the decoder when exporting? + this.decoder.end_of_word_suffix = this.model.end_of_word_suffix; + } + + this.added_tokens_regex = this.added_tokens.length > 0 ? new RegExp( + this.added_tokens.slice() + // Sort by length (desc) to avoid early partial matches + .sort((a, b) => b.content.length - a.content.length) + .map(x => `${x.lstrip ? '\\s*' : ''}(${(0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.escapeRegExp)(x.content)})${x.rstrip ? '\\s*' : ''}`) + .join('|') + ) : null; + + // Set mask token if present (otherwise will be undefined, which is fine) + this.mask_token = this.getToken('mask_token'); + this.mask_token_id = this.model.tokens_to_ids.get(this.mask_token); + + this.pad_token = this.getToken('pad_token', 'eos_token'); + this.pad_token_id = this.model.tokens_to_ids.get(this.pad_token); + + this.sep_token = this.getToken('sep_token'); + this.sep_token_id = this.model.tokens_to_ids.get(this.sep_token); + + this.unk_token = this.getToken('unk_token'); + this.unk_token_id = this.model.tokens_to_ids.get(this.unk_token); + + this.model_max_length = tokenizerConfig.model_max_length; + + /** @type {boolean} Whether or not to strip the text when tokenizing (removing excess spaces before and after the string). */ + this.remove_space = tokenizerConfig.remove_space; + + this.clean_up_tokenization_spaces = tokenizerConfig.clean_up_tokenization_spaces ?? true; + this.do_lowercase_and_remove_accent = tokenizerConfig.do_lowercase_and_remove_accent ?? false; + + if (tokenizerConfig.padding_side) { + this.padding_side = tokenizerConfig.padding_side; + } + + this.legacy = false; + + this.chat_template = tokenizerConfig.chat_template ?? null; + if (Array.isArray(this.chat_template)) { + // Chat templates are stored as lists of dicts with fixed key names, + // we reconstruct that into a single dict while loading them. + const chat_template = Object.create(null); + for (const { name, template } of this.chat_template) { + if (typeof name !== 'string' || typeof template !== 'string') { + throw new Error('Chat template must be a list of objects with "name" and "template" properties'); + } + chat_template[name] = template; + } + this.chat_template = chat_template; + } + this._compiled_template_cache = new Map(); + } + + /** + * Returns the value of the first matching key in the tokenizer config object. + * @param {...string} keys One or more keys to search for in the tokenizer config object. + * @returns {string|null} The value associated with the first matching key, or null if no match is found. + * @throws {Error} If an object is found for a matching key and its __type property is not "AddedToken". + * @private + */ + getToken(...keys) { + for (const key of keys) { + const item = this._tokenizer_config[key]; + + if (!item) continue; + + if (typeof item === 'object') { + if (item.__type === 'AddedToken') { + return item.content; + } else { + throw Error(`Unknown token: ${item}`); + } + } else { + return item; + } + } + return null; + } + + /** + * Loads a pre-trained tokenizer from the given `pretrained_model_name_or_path`. + * + * @param {string} pretrained_model_name_or_path The path to the pre-trained tokenizer. + * @param {PretrainedTokenizerOptions} options Additional options for loading the tokenizer. + * + * @throws {Error} Throws an error if the tokenizer.json or tokenizer_config.json files are not found in the `pretrained_model_name_or_path`. + * @returns {Promise} A new instance of the `PreTrainedTokenizer` class. + */ + static async from_pretrained(pretrained_model_name_or_path, { + progress_callback = null, + config = null, + cache_dir = null, + local_files_only = false, + revision = 'main', + legacy = null, + } = {}) { + + const info = await loadTokenizer(pretrained_model_name_or_path, { + progress_callback, + config, + cache_dir, + local_files_only, + revision, + legacy, + }) + + // @ts-ignore + return new this(...info); + } + + /** + * @typedef {number[]|number[][]|Tensor} BatchEncodingItem + * + * @typedef {Object} BatchEncoding Holds the output of the tokenizer's call function. + * @property {BatchEncodingItem} input_ids List of token ids to be fed to a model. + * @property {BatchEncodingItem} attention_mask List of indices specifying which tokens should be attended to by the model. + * @property {BatchEncodingItem} [token_type_ids] List of token type ids to be fed to a model. + */ + + /** + * Encode/tokenize the given text(s). + * @param {string|string[]} text The text to tokenize. + * @param {Object} options An optional object containing the following properties: + * @param {string|string[]} [options.text_pair=null] Optional second sequence to be encoded. If set, must be the same type as text. + * @param {boolean|'max_length'} [options.padding=false] Whether to pad the input sequences. + * @param {boolean} [options.add_special_tokens=true] Whether or not to add the special tokens associated with the corresponding model. + * @param {boolean} [options.truncation=null] Whether to truncate the input sequences. + * @param {number} [options.max_length=null] Maximum length of the returned list and optionally padding length. + * @param {boolean} [options.return_tensor=true] Whether to return the results as Tensors or arrays. + * @param {boolean} [options.return_token_type_ids=null] Whether to return the token type ids. + * @returns {BatchEncoding} Object to be passed to the model. + */ + _call( + // Required positional arguments + text, + + // Optional keyword arguments + { + text_pair = null, + add_special_tokens = true, + padding = false, + truncation = null, + max_length = null, + return_tensor = true, // Different to HF + return_token_type_ids = null, + } = {}, + ) { + + const isBatched = Array.isArray(text); + + /** @type {EncodingSingle[]} */ + let encodedTokens; + + if (isBatched) { + if (text.length === 0) { + throw Error('text array must be non-empty') + } + + if (text_pair !== null) { + if (!Array.isArray(text_pair)) { + throw Error('text_pair must also be an array') + + } else if (text.length !== text_pair.length) { + throw Error('text and text_pair must have the same length') + } + + encodedTokens = text.map( + (t, i) => this._encode_plus(t, { text_pair: text_pair[i], add_special_tokens, return_token_type_ids }) + ) + + } else { + encodedTokens = text.map(x => this._encode_plus(x, { add_special_tokens, return_token_type_ids })); + } + + } else { + if (text === null || text === undefined) { + throw Error('text may not be null or undefined') + } + + if (Array.isArray(text_pair)) { + throw Error('When specifying `text_pair`, since `text` is a string, `text_pair` must also be a string (i.e., not an array).') + } + + // For single input, we just wrap in an array, and then unwrap later. + encodedTokens = [this._encode_plus(text, { text_pair, add_special_tokens, return_token_type_ids })]; + } + // At this point, tokens is batched: [batch_size, tokens] + // However, array may be jagged. So, we pad to max_length + + if (max_length === null) { + if (padding === 'max_length') { + max_length = this.model_max_length; + } else { + // Calculate max length from sequences + max_length = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.max)(encodedTokens.map(x => x.input_ids.length))[0]; + } + } else { + if (!truncation) { + console.warn(`Truncation was not explicitly activated but \`max_length\` is provided a specific value, please use \`truncation=true\` to explicitly truncate examples to max length.`) + } + } + + // Ensure it is less than model max length + max_length = Math.min(max_length, this.model_max_length ?? Infinity); + + if (padding || truncation) { + + // Perform padding and/or truncation + for (let i = 0; i < encodedTokens.length; ++i) { + if (encodedTokens[i].input_ids.length === max_length) { + continue; + + } else if (encodedTokens[i].input_ids.length > max_length) { + // possibly truncate + if (truncation) { + truncateHelper(encodedTokens[i], max_length); + } + + } else { // t.length < max_length + // possibly pad + if (padding) { + padHelper( + encodedTokens[i], + max_length, + key => key === 'input_ids' ? this.pad_token_id : 0, + this.padding_side + ); + } + } + } + } + + const result = {}; + + if (return_tensor) { + if (!(padding && truncation)) { + // Not, guaranteed that all items have same length, so + // we perform additional check + + if ( + encodedTokens.some(x => { + for (const key of Object.keys(x)) { + if (x[key].length !== encodedTokens[0][key]?.length) { + return true; + } + } + return false; + }) + ) { + throw Error( + "Unable to create tensor, you should probably activate truncation and/or padding " + + "with 'padding=true' and 'truncation=true' to have batched tensors with the same length." + ) + } + } + + // Now we actually convert to tensor + // NOTE: In the same way as the python library, we return a batched tensor, regardless of + // whether we have a single input or multiple inputs. + const dims = [encodedTokens.length, encodedTokens[0].input_ids.length]; + + for (const key of Object.keys(encodedTokens[0])) { + result[key] = new _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor('int64', + BigInt64Array.from(encodedTokens.flatMap(x => x[key]).map(BigInt)), + dims + ); + } + + } else { + for (const key of Object.keys(encodedTokens[0])) { + result[key] = encodedTokens.map(x => x[key]); + } + + // If not returning a tensor, we match the input type + if (!isBatched) { + // Input was not batched, so we unwrap + for (const key of Object.keys(result)) { + result[key] = result[key][0]; + } + } + } + + return /** @type {BatchEncoding} */(result); + } + + /** + * Encodes a single text using the preprocessor pipeline of the tokenizer. + * + * @param {string|null} text The text to encode. + * @returns {string[]|null} The encoded tokens. + */ + _encode_text(text) { + if (text === null) return null; + + // Actual function which does encoding, for a single text + // First, we take care of special tokens. Needed to avoid issues arising from + // normalization and/or pretokenization (which may not preserve special tokens) + const sections = this.added_tokens_regex ? text.split(this.added_tokens_regex).filter(x => x) : [text]; + + const tokens = sections.map((x, section_index) => { + const addedToken = this.added_tokens.find(t => t.content === x); + if (addedToken !== undefined) { + // Ignore added tokens + return x + } else { + if (this.remove_space === true) { + x = x.trim().split(/\s+/).join(' '); + } + if (this.do_lowercase_and_remove_accent) { + x = lowercase_and_remove_accent(x); + } + + if (this.normalizer !== null) { + x = this.normalizer(x); + } + + // If, after normalization, this section is empty (e.g., trimming whitespace), + // we return an empty array + if (x.length === 0) { + return []; + } + + const sectionTokens = (this.pre_tokenizer !== null) ? this.pre_tokenizer(x, { + section_index, + }) : [x]; + + const tokens = this.model(sectionTokens); + + return tokens; + } + }).flat(); + + return tokens; + } + + /** + * Encodes a single text or a pair of texts using the model's tokenizer. + * + * @param {string} text The text to encode. + * @param {Object} options An optional object containing the following properties: + * @param {string} [options.text_pair=null] The optional second text to encode. + * @param {boolean} [options.add_special_tokens=true] Whether or not to add the special tokens associated with the corresponding model. + * @param {boolean} [options.return_token_type_ids=null] Whether to return token_type_ids. + * @returns {EncodingSingle} An object containing the encoded text. + * @private + */ + _encode_plus(text, { + text_pair = null, + add_special_tokens = true, + return_token_type_ids = null, + } = {}) { + + const { tokens, token_type_ids } = this._tokenize_helper(text, { pair: text_pair, add_special_tokens }); + + const input_ids = this.model.convert_tokens_to_ids(tokens); + + const result = { + input_ids, + attention_mask: new Array(input_ids.length).fill(1), + } + if ((return_token_type_ids ?? this.return_token_type_ids) && token_type_ids) { + result.token_type_ids = token_type_ids; + } + return result; + } + + /** + * Internal helper function to tokenize a text, and optionally a pair of texts. + * @param {string} text The text to tokenize. + * @param {Object} options An optional object containing the following properties: + * @param {string} [options.pair=null] The optional second text to tokenize. + * @param {boolean} [options.add_special_tokens=false] Whether or not to add the special tokens associated with the corresponding model. + * @returns {{tokens: string[], token_type_ids?: number[]}} An object containing the tokens and optionally the token type IDs. + */ + _tokenize_helper(text, { + pair = null, + add_special_tokens = false, + } = {}) { + const tokens = this._encode_text(text); + const tokens2 = this._encode_text(pair); + + return this.post_processor + ? this.post_processor(tokens, tokens2, { add_special_tokens }) + : { tokens: (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(tokens ?? [], tokens2 ?? []) }; + } + + /** + * Converts a string into a sequence of tokens. + * @param {string} text The sequence to be encoded. + * @param {Object} options An optional object containing the following properties: + * @param {string} [options.pair] A second sequence to be encoded with the first. + * @param {boolean} [options.add_special_tokens=false] Whether or not to add the special tokens associated with the corresponding model. + * @returns {string[]} The list of tokens. + */ + tokenize(text, { + pair = null, + add_special_tokens = false, + } = {}) { + return this._tokenize_helper(text, { pair, add_special_tokens }).tokens; + } + + /** + * Encodes a single text or a pair of texts using the model's tokenizer. + * + * @param {string} text The text to encode. + * @param {Object} options An optional object containing the following properties: + * @param {string} [options.text_pair=null] The optional second text to encode. + * @param {boolean} [options.add_special_tokens=true] Whether or not to add the special tokens associated with the corresponding model. + * @param {boolean} [options.return_token_type_ids=null] Whether to return token_type_ids. + * @returns {number[]} An array of token IDs representing the encoded text(s). + */ + encode(text, { + text_pair = null, + add_special_tokens = true, + return_token_type_ids = null, + } = {}) { + return this._encode_plus(text, { + text_pair, + add_special_tokens, + return_token_type_ids, + }).input_ids; + } + + /** + * Decode a batch of tokenized sequences. + * @param {number[][]|Tensor} batch List/Tensor of tokenized input sequences. + * @param {Object} decode_args (Optional) Object with decoding arguments. + * @returns {string[]} List of decoded sequences. + */ + batch_decode(batch, decode_args = {}) { + if (batch instanceof _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor) { + batch = batch.tolist(); + } + return batch.map(x => this.decode(x, decode_args)); + } + + /** + * Decodes a sequence of token IDs back to a string. + * + * @param {number[]|bigint[]|Tensor} token_ids List/Tensor of token IDs to decode. + * @param {Object} [decode_args={}] + * @param {boolean} [decode_args.skip_special_tokens=false] If true, special tokens are removed from the output string. + * @param {boolean} [decode_args.clean_up_tokenization_spaces=true] If true, spaces before punctuations and abbreviated forms are removed. + * + * @returns {string} The decoded string. + * @throws {Error} If `token_ids` is not a non-empty array of integers. + */ + decode( + token_ids, + decode_args = {}, + ) { + if (token_ids instanceof _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor) { + token_ids = prepareTensorForDecode(token_ids); + } + + if (!Array.isArray(token_ids) || token_ids.length === 0 || !(0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.isIntegralNumber)(token_ids[0])) { + throw Error("token_ids must be a non-empty array of integers."); + } + + return this.decode_single(token_ids, decode_args) + } + + /** + * Decode a single list of token ids to a string. + * @param {number[]|bigint[]} token_ids List of token ids to decode + * @param {Object} decode_args Optional arguments for decoding + * @param {boolean} [decode_args.skip_special_tokens=false] Whether to skip special tokens during decoding + * @param {boolean} [decode_args.clean_up_tokenization_spaces=null] Whether to clean up tokenization spaces during decoding. + * If null, the value is set to `this.decoder.cleanup` if it exists, falling back to `this.clean_up_tokenization_spaces` if it exists, falling back to `true`. + * @returns {string} The decoded string + */ + decode_single( + token_ids, + { + skip_special_tokens = false, + clean_up_tokenization_spaces = null, + } + ) { + let tokens = this.model.convert_ids_to_tokens(token_ids); + if (skip_special_tokens) { + tokens = tokens.filter(x => !this.special_tokens.includes(x)); + } + + // If `this.decoder` is null, we just join tokens with a space: + // https://github.com/huggingface/tokenizers/blob/8edec536a737cb04494b454805be16c020abb14f/tokenizers/src/tokenizer/mod.rs#L835 + /** @type {string} */ + let decoded = this.decoder ? this.decoder(tokens) : tokens.join(' '); + + // Slight hack, but prevents having to pass `skip_special_tokens` to + // each call to `decode`, which would lead to code duplication. + if (this.decoder && this.decoder.end_of_word_suffix) { + decoded = decoded.replaceAll(this.decoder.end_of_word_suffix, ' '); + if (skip_special_tokens) { + decoded = decoded.trim(); + } + } + + if (clean_up_tokenization_spaces ?? this.clean_up_tokenization_spaces) { + decoded = clean_up_tokenization(decoded); + } + + return decoded; + } + + /** + * Retrieve the chat template string used for tokenizing chat messages. This template is used + * internally by the `apply_chat_template` method and can also be used externally to retrieve the model's chat + * template for better generation tracking. + * + * @param {Object} options An optional object containing the following properties: + * @param {string} [options.chat_template=null] + * A Jinja template or the name of a template to use for this conversion. + * It is usually not necessary to pass anything to this argument, + * as the model's template will be used by default. + * @param {Object[]} [options.tools=null] + * A list of tools (callable functions) that will be accessible to the model. If the template does not + * support function calling, this argument will have no effect. Each tool should be passed as a JSON Schema, + * giving the name, description and argument types for the tool. See our + * [chat templating guide](https://huggingface.co/docs/transformers/main/en/chat_templating#automated-function-conversion-for-tool-use) + * for more information. + * @returns {string} The chat template string. + */ + get_chat_template({ + chat_template = null, + tools = null, + } = {}) { + + // First, handle the cases when the model has a dict of multiple templates + if (this.chat_template && typeof this.chat_template === 'object') { + const template_dict = this.chat_template; + + if (chat_template !== null && Object.hasOwn(template_dict, chat_template)) { + // The user can pass the name of a template to the chat template argument instead of an entire template + chat_template = template_dict[chat_template]; + } else if (chat_template === null) { + if (tools !== null && 'tool_use' in template_dict) { + chat_template = template_dict['tool_use']; + } else if ('default' in template_dict) { + chat_template = template_dict['default']; + } else { + throw Error( + `This model has multiple chat templates with no default specified! Please either pass a chat ` + + `template or the name of the template you wish to use to the 'chat_template' argument. Available ` + + `template names are ${Object.keys(template_dict).sort()}.` + ) + } + } + } else if (chat_template === null) { + // These are the cases when the model has a single template + // priority: `chat_template` argument > `tokenizer.chat_template` + if (this.chat_template) { + chat_template = this.chat_template; + } else { + throw Error( + "Cannot use apply_chat_template() because tokenizer.chat_template is not set and no template " + + "argument was passed! For information about writing templates and setting the " + + "tokenizer.chat_template attribute, please see the documentation at " + + "https://huggingface.co/docs/transformers/main/en/chat_templating" + ) + } + } + return chat_template; + } + + /** + * Converts a list of message objects with `"role"` and `"content"` keys to a list of token + * ids. This method is intended for use with chat models, and will read the tokenizer's chat_template attribute to + * determine the format and control tokens to use when converting. + * + * See [here](https://huggingface.co/docs/transformers/chat_templating) for more information. + * + * **Example:** Applying a chat template to a conversation. + * + * ```javascript + * import { AutoTokenizer } from "@huggingface/transformers"; + * + * const tokenizer = await AutoTokenizer.from_pretrained("Xenova/mistral-tokenizer-v1"); + * + * const chat = [ + * { "role": "user", "content": "Hello, how are you?" }, + * { "role": "assistant", "content": "I'm doing great. How can I help you today?" }, + * { "role": "user", "content": "I'd like to show off how chat templating works!" }, + * ] + * + * const text = tokenizer.apply_chat_template(chat, { tokenize: false }); + * // "[INST] Hello, how are you? [/INST]I'm doing great. How can I help you today? [INST] I'd like to show off how chat templating works! [/INST]" + * + * const input_ids = tokenizer.apply_chat_template(chat, { tokenize: true, return_tensor: false }); + * // [1, 733, 16289, 28793, 22557, 28725, 910, 460, 368, 28804, 733, 28748, 16289, 28793, 28737, 28742, 28719, 2548, 1598, 28723, 1602, 541, 315, 1316, 368, 3154, 28804, 2, 28705, 733, 16289, 28793, 315, 28742, 28715, 737, 298, 1347, 805, 910, 10706, 5752, 1077, 3791, 28808, 733, 28748, 16289, 28793] + * ``` + * + * @param {Message[]} conversation A list of message objects with `"role"` and `"content"` keys, + * representing the chat history so far. + * @param {Object} options An optional object containing the following properties: + * @param {string} [options.chat_template=null] A Jinja template to use for this conversion. If + * this is not passed, the model's chat template will be used instead. + * @param {Object[]} [options.tools=null] + * A list of tools (callable functions) that will be accessible to the model. If the template does not + * support function calling, this argument will have no effect. Each tool should be passed as a JSON Schema, + * giving the name, description and argument types for the tool. See our + * [chat templating guide](https://huggingface.co/docs/transformers/main/en/chat_templating#automated-function-conversion-for-tool-use) + * for more information. + * @param {Record[]} [options.documents=null] + * A list of dicts representing documents that will be accessible to the model if it is performing RAG + * (retrieval-augmented generation). If the template does not support RAG, this argument will have no + * effect. We recommend that each document should be a dict containing "title" and "text" keys. Please + * see the RAG section of the [chat templating guide](https://huggingface.co/docs/transformers/main/en/chat_templating#arguments-for-RAG) + * for examples of passing documents with chat templates. + * @param {boolean} [options.add_generation_prompt=false] Whether to end the prompt with the token(s) that indicate + * the start of an assistant message. This is useful when you want to generate a response from the model. + * Note that this argument will be passed to the chat template, and so it must be supported in the + * template for this argument to have any effect. + * @param {boolean} [options.tokenize=true] Whether to tokenize the output. If false, the output will be a string. + * @param {boolean} [options.padding=false] Whether to pad sequences to the maximum length. Has no effect if tokenize is false. + * @param {boolean} [options.truncation=false] Whether to truncate sequences to the maximum length. Has no effect if tokenize is false. + * @param {number} [options.max_length=null] Maximum length (in tokens) to use for padding or truncation. Has no effect if tokenize is false. + * If not specified, the tokenizer's `max_length` attribute will be used as a default. + * @param {boolean} [options.return_tensor=true] Whether to return the output as a Tensor or an Array. Has no effect if tokenize is false. + * @param {boolean} [options.return_dict=true] Whether to return a dictionary with named outputs. Has no effect if tokenize is false. + * @param {Object} [options.tokenizer_kwargs={}] Additional options to pass to the tokenizer. + * @returns {string | Tensor | number[]| number[][]|BatchEncoding} The tokenized output. + */ + apply_chat_template(conversation, { + tools = null, + documents = null, + chat_template = null, + add_generation_prompt = false, + tokenize = true, + padding = false, + truncation = false, + max_length = null, + return_tensor = true, + return_dict = false, + tokenizer_kwargs = {}, + ...kwargs + } = {}) { + + chat_template = this.get_chat_template({ chat_template, tools }); + + if (typeof chat_template !== 'string') { + throw Error(`chat_template must be a string, but got ${typeof chat_template}`); + } + + // Compilation function uses a cache to avoid recompiling the same template + let compiledTemplate = this._compiled_template_cache.get(chat_template); + if (compiledTemplate === undefined) { + compiledTemplate = new _huggingface_jinja__WEBPACK_IMPORTED_MODULE_6__.Template(chat_template); + this._compiled_template_cache.set(chat_template, compiledTemplate); + } + + const special_tokens_map = Object.create(null); + for (const key of SPECIAL_TOKEN_ATTRIBUTES) { + const value = this.getToken(key); + if (value) { + special_tokens_map[key] = value; + } + } + + const rendered = compiledTemplate.render({ + messages: conversation, + add_generation_prompt, + tools, + documents, + ...special_tokens_map, + ...kwargs, + }); + + if (tokenize) { + const out = this._call(rendered, { + add_special_tokens: false, + padding, + truncation, + max_length, + return_tensor, + ...tokenizer_kwargs, + }); + return return_dict ? out : out.input_ids; + } + + return rendered; + } +} + +/** + * BertTokenizer is a class used to tokenize text for BERT models. + * @extends PreTrainedTokenizer + */ +class BertTokenizer extends PreTrainedTokenizer { + return_token_type_ids = true; +} +/** + * Albert tokenizer + * @extends PreTrainedTokenizer + */ +class AlbertTokenizer extends PreTrainedTokenizer { + return_token_type_ids = true; +} +class MobileBertTokenizer extends PreTrainedTokenizer { + return_token_type_ids = true; +} +class SqueezeBertTokenizer extends PreTrainedTokenizer { + return_token_type_ids = true; +} +class DebertaTokenizer extends PreTrainedTokenizer { + return_token_type_ids = true; +} +class DebertaV2Tokenizer extends PreTrainedTokenizer { + return_token_type_ids = true; +} +class HerbertTokenizer extends PreTrainedTokenizer { + return_token_type_ids = true; +} +class ConvBertTokenizer extends PreTrainedTokenizer { + return_token_type_ids = true; +} +class RoFormerTokenizer extends PreTrainedTokenizer { + return_token_type_ids = true; +} +class DistilBertTokenizer extends PreTrainedTokenizer { } +class CamembertTokenizer extends PreTrainedTokenizer { } +class XLMTokenizer extends PreTrainedTokenizer { + return_token_type_ids = true; + + constructor(tokenizerJSON, tokenizerConfig) { + super(tokenizerJSON, tokenizerConfig); + console.warn('WARNING: `XLMTokenizer` is not yet supported by Hugging Face\'s "fast" tokenizers library. Therefore, you may experience slightly inaccurate results.') + } +} +class ElectraTokenizer extends PreTrainedTokenizer { + return_token_type_ids = true; +} + +class T5Tokenizer extends PreTrainedTokenizer { } +class GPT2Tokenizer extends PreTrainedTokenizer { } +class BartTokenizer extends PreTrainedTokenizer { } +class MBartTokenizer extends PreTrainedTokenizer { + constructor(tokenizerJSON, tokenizerConfig) { + super(tokenizerJSON, tokenizerConfig); + + this.languageRegex = /^[a-z]{2}_[A-Z]{2}$/; + this.language_codes = this.special_tokens.filter(x => this.languageRegex.test(x)); + this.lang_to_token = x => x; // Identity function + } + + /** + * Helper function to build translation inputs for an `MBartTokenizer`. + * @param {string|string[]} raw_inputs The text to tokenize. + * @param {Object} tokenizer_options Options to be sent to the tokenizer + * @param {Object} generate_kwargs Generation options. + * @returns {Object} Object to be passed to the model. + */ + _build_translation_inputs(raw_inputs, tokenizer_options, generate_kwargs) { + return _build_translation_inputs(this, raw_inputs, tokenizer_options, generate_kwargs); + } +} +class MBart50Tokenizer extends MBartTokenizer { } // NOTE: extends MBartTokenizer + +class RobertaTokenizer extends PreTrainedTokenizer { } + +class BloomTokenizer extends PreTrainedTokenizer { } + +const SPIECE_UNDERLINE = "▁"; + +class LlamaTokenizer extends PreTrainedTokenizer { + + padding_side = 'left'; + + constructor(tokenizerJSON, tokenizerConfig) { + super(tokenizerJSON, tokenizerConfig); + + this.legacy = tokenizerConfig.legacy ?? true; + if (!this.legacy) { + // See https://github.com/huggingface/transformers/pull/24565 for more information + this.normalizer = null; + this.pre_tokenizer = new MetaspacePreTokenizer({ + replacement: SPIECE_UNDERLINE, + add_prefix_space: true, + prepend_scheme: "first", + }); + } + } + + /** + * Helper function to handle legacy encoding of SPM tokenizers. + * Adapted from https://github.com/huggingface/transformers/blob/e6dcf8abd6f65bb4b6dfc1831b20d9ba49ce00e2/src/transformers/models/t5/tokenization_t5.py#L374-L387 + * @param {string} text The text to encode. + * @returns {string[]} The encoded tokens. + */ + _encode_text(text) { + if (text === null) return null; + + if (this.legacy || text.length === 0) { + return super._encode_text(text); + } + + let tokens = super._encode_text(SPIECE_UNDERLINE + text.replaceAll(SPIECE_UNDERLINE, " ")); + if (tokens.length > 1 && tokens[0] === SPIECE_UNDERLINE && this.special_tokens.includes(tokens[1])) { + tokens = tokens.slice(1); + } + return tokens; + } +} +class CodeLlamaTokenizer extends PreTrainedTokenizer { } + +class XLMRobertaTokenizer extends PreTrainedTokenizer { } +class MPNetTokenizer extends PreTrainedTokenizer { } + +class FalconTokenizer extends PreTrainedTokenizer { } + +class GPTNeoXTokenizer extends PreTrainedTokenizer { } + +class EsmTokenizer extends PreTrainedTokenizer { } + +class Qwen2Tokenizer extends PreTrainedTokenizer { } + +class GemmaTokenizer extends PreTrainedTokenizer { } + +class Grok1Tokenizer extends PreTrainedTokenizer { } + +/** + * Helper function to build translation inputs for an `NllbTokenizer` or `M2M100Tokenizer`. + * @param {PreTrainedTokenizer} self The tokenizer instance. + * @param {string|string[]} raw_inputs The text to tokenize. + * @param {Object} tokenizer_options Options to be sent to the tokenizer + * @param {Object} generate_kwargs Generation options. + * @returns {Object} Object to be passed to the model. + * @private + */ +function _build_translation_inputs(self, raw_inputs, tokenizer_options, generate_kwargs) { + if (!('language_codes' in self) || !Array.isArray(self.language_codes)) { + throw new Error('Tokenizer must have `language_codes` attribute set and it should be an array of language ids.') + } + if (!('languageRegex' in self) || !(self.languageRegex instanceof RegExp)) { + throw new Error('Tokenizer must have `languageRegex` attribute set and it should be a regular expression.') + } + if (!('lang_to_token' in self) || typeof self.lang_to_token !== 'function') { + throw new Error('Tokenizer must have `lang_to_token` attribute set and it should be a function.') + } + const src_lang_token = generate_kwargs.src_lang; + const tgt_lang_token = generate_kwargs.tgt_lang; + + // Check that the target language is valid: + if (!self.language_codes.includes(tgt_lang_token)) { + throw new Error(`Target language code "${tgt_lang_token}" is not valid. Must be one of: {${self.language_codes.join(', ')}}`); + } + + // Allow `src_lang` to be optional. If not set, we'll use the tokenizer's default. + if (src_lang_token !== undefined) { + // Check that the source language is valid: + if (!self.language_codes.includes(src_lang_token)) { + throw new Error(`Source language code "${src_lang_token}" is not valid. Must be one of: {${self.language_codes.join(', ')}}`); + } + + // In the same way as the Python library, we override the post-processor + // to force the source language to be first: + for (const item of self.post_processor.config.single) { + if ('SpecialToken' in item && self.languageRegex.test(item.SpecialToken.id)) { + item.SpecialToken.id = self.lang_to_token(src_lang_token); + break; + } + } + // TODO: Do the same for pair? + } + + // Override the `forced_bos_token_id` to force the correct language + generate_kwargs.forced_bos_token_id = self.model.convert_tokens_to_ids([self.lang_to_token(tgt_lang_token)])[0]; + + return self._call(raw_inputs, tokenizer_options); +} + +/** + * The NllbTokenizer class is used to tokenize text for NLLB ("No Language Left Behind") models. + * + * No Language Left Behind (NLLB) is a first-of-its-kind, AI breakthrough project + * that open-sources models capable of delivering high-quality translations directly + * between any pair of 200+ languages — including low-resource languages like Asturian, + * Luganda, Urdu and more. It aims to help people communicate with anyone, anywhere, + * regardless of their language preferences. For more information, check out their + * [paper](https://arxiv.org/abs/2207.04672). + * + * For a list of supported languages (along with their language codes), + * @see {@link https://github.com/facebookresearch/flores/blob/main/flores200/README.md#languages-in-flores-200} + */ +class NllbTokenizer extends PreTrainedTokenizer { + + constructor(tokenizerJSON, tokenizerConfig) { + super(tokenizerJSON, tokenizerConfig); + + this.languageRegex = /^[a-z]{3}_[A-Z][a-z]{3}$/; + this.language_codes = this.special_tokens.filter(x => this.languageRegex.test(x)); + this.lang_to_token = x => x; // Identity function + } + + /** + * Helper function to build translation inputs for an `NllbTokenizer`. + * @param {string|string[]} raw_inputs The text to tokenize. + * @param {Object} tokenizer_options Options to be sent to the tokenizer + * @param {Object} generate_kwargs Generation options. + * @returns {Object} Object to be passed to the model. + */ + _build_translation_inputs(raw_inputs, tokenizer_options, generate_kwargs) { + return _build_translation_inputs(this, raw_inputs, tokenizer_options, generate_kwargs); + } +} + +/** + * The M2M100Tokenizer class is used to tokenize text for M2M100 ("Many-to-Many") models. + * + * M2M100 is a multilingual encoder-decoder (seq-to-seq) model trained for Many-to-Many + * multilingual translation. It was introduced in this [paper](https://arxiv.org/abs/2010.11125) + * and first released in [this](https://github.com/pytorch/fairseq/tree/master/examples/m2m_100) repository. + * + * For a list of supported languages (along with their language codes), + * @see {@link https://huggingface.co/facebook/m2m100_418M#languages-covered} + */ +class M2M100Tokenizer extends PreTrainedTokenizer { + constructor(tokenizerJSON, tokenizerConfig) { + super(tokenizerJSON, tokenizerConfig); + + this.languageRegex = /^__[a-z]{2,3}__$/; + this.language_codes = this.special_tokens + .filter(x => this.languageRegex.test(x)) + .map(x => x.slice(2, -2)); + this.lang_to_token = x => `__${x}__`; + } + + /** + * Helper function to build translation inputs for an `M2M100Tokenizer`. + * @param {string|string[]} raw_inputs The text to tokenize. + * @param {Object} tokenizer_options Options to be sent to the tokenizer + * @param {Object} generate_kwargs Generation options. + * @returns {Object} Object to be passed to the model. + */ + _build_translation_inputs(raw_inputs, tokenizer_options, generate_kwargs) { + return _build_translation_inputs(this, raw_inputs, tokenizer_options, generate_kwargs); + } +} + +/** + * WhisperTokenizer tokenizer + * @extends PreTrainedTokenizer + */ +class WhisperTokenizer extends PreTrainedTokenizer { + + get timestamp_begin() { + return this.model.convert_tokens_to_ids(["<|notimestamps|>"])[0] + 1; + } + + /** + * Decodes automatic speech recognition (ASR) sequences. + * @param {Array<{tokens: bigint[], token_timestamps?: number[], stride: number[]}>} sequences The sequences to decode. + * @param {Object} options The options to use for decoding. + * @returns {Array, text: string}>}>} The decoded sequences. + */ + _decode_asr(sequences, { + return_timestamps = false, + return_language = false, + time_precision = null, + force_full_sequences = true + } = {}) { + // Set force_full_sequences=false if you want streaming + // TODO add support for `return_language` + + // Internal method meant to only be used by asr pipeline. + // Handles all the little quirks specific to whisper to handle + // the various options not allowed in other seq2seq models + + // =========== Overview ============ + // - iterate over all outputs + // - all tokens within output + // - Each token can be + // - language token + // - special token + // - timestamp token + // - text token + // - We accumulate the text tokens. + // - We split on end timestamps + // - Lots of complexity comes from stride and timestamps + + if (time_precision === null) { + throw Error("Must specify time_precision") + } + let last_language = null; + + const returnWordTimestamps = return_timestamps === "word"; + + function new_chunk() { + return { "language": last_language, "timestamp": [null, null], "text": "" }; + } + + // Welcome to the state machine! + const chunks = []; + let chunk = new_chunk(); + let time_offset = 0.0; + const timestamp_begin = this.timestamp_begin; + + let previous_tokens = []; + let previous_token_timestamps = []; + + let skip = false; + let right_stride_start = null; + + + const all_special_ids = new Set(this.all_special_ids); + + for (const output of sequences) { + // NOTE: python version has batches, so it uses [0] + const token_ids = output.tokens; + const token_timestamps = returnWordTimestamps ? output.token_timestamps : null; + + // These keep track of timestamps within strides, which need + // to be skipped and resolve all tokens in a single chunk. + let last_timestamp = null; + let first_timestamp = timestamp_begin; + + if ("stride" in output) { + const [chunk_len, stride_left, stride_right] = output.stride; + + // Offset the timings to account for the other `model_outputs`. + time_offset -= stride_left; + right_stride_start = chunk_len - stride_right; + + // Keeping track of timestamps within strides + // We're going to NOT split on those, and delay until we're + // out of BOTH stride. Otherwise lots of issues occur and + // corner cases + if (stride_left) { + first_timestamp = stride_left / time_precision + timestamp_begin; + } + + if (stride_right) { + for (let i = token_ids.length - 1; i >= 0; --i) { + const token = Number(token_ids[i]); + if (token >= timestamp_begin) { + // There can be several token in the right stride + // But the last one is ALWAYS going to be skipped + if (last_timestamp !== null && (token - timestamp_begin) * time_precision < right_stride_start) { + break; + } + last_timestamp = token; + } + } + } + } + + let current_tokens = []; + let current_token_timestamps = []; + + // - all tokens within output + for (let i = 0; i < token_ids.length; ++i) { + const token = Number(token_ids[i]); + // 4 possible states for each token + // - 1/ Language code + // - 2/ all other special tokens (which we ignore) + // - 3/ Timestamp + // - 4/ Regular text + + if (all_special_ids.has(token)) { + const text = this.decode([token]); + const language = _models_whisper_common_whisper_js__WEBPACK_IMPORTED_MODULE_7__.WHISPER_LANGUAGE_MAPPING.get(text.slice(2, -2)); + + if (language !== undefined) { + // 1/ Indeed some language + // TODO Handle when language is different from the previous + // one, and we cannot use timestamped tokens to create chunks + if (last_language !== null && language !== last_language && !return_timestamps) { + previous_tokens.push(current_tokens); + const resolved_tokens = this.findLongestCommonSequence(previous_tokens)[0]; + const resolved_text = this.decode(resolved_tokens); + chunk.text = resolved_text; + chunks.push(chunk); + + // Flush all our temporary context + previous_tokens = []; + current_tokens = []; + chunk = new_chunk(); + } + + last_language = chunk.language = language; + } else { + // 2/ This is a regular special token, ignoring it + } + } else if (token >= timestamp_begin) { + // 3/ Timestamp token + const time = (token - timestamp_begin) * time_precision + time_offset; + const rounded_time = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.round)(time, 2); + + if (last_timestamp !== null && token >= last_timestamp) { + // Whisper outputted a timestamp token, but it falls within + // our stride, so we're going to skip it for the time being + // and resolve this later + // Skip is necessary because timestamp tokens always come + // by pair, so we need to skip the next one too (which would mark the start of another chunk). + skip = true; + } else if (skip || (previous_tokens.length > 0 && token < first_timestamp)) { + skip = false; + } else if (chunk.timestamp[0] === null) { + chunk.timestamp[0] = rounded_time; + } else { + // This is the end of the timestamp chunk + if (rounded_time === chunk.timestamp[0]) { + // This is a bug in timestamp token output + // where we're taking the duplicate token + // as a stop where it should be a start. + // This is an issue in the underlying model output + // Let's just skip it so it becomes de-factor a start agin + } else { + chunk.timestamp[1] = rounded_time; + + // Handling merges + previous_tokens.push(current_tokens) + + if (returnWordTimestamps) { + previous_token_timestamps.push(current_token_timestamps); + } + const [resolved_tokens, resolved_token_timestamps] = this.findLongestCommonSequence( + previous_tokens, previous_token_timestamps + ) + + const resolved_text = this.decode(resolved_tokens) + chunk.text = resolved_text + + if (returnWordTimestamps) { + chunk.words = this.collateWordTimestamps( + resolved_tokens, resolved_token_timestamps, last_language, + ) + } + + chunks.push(chunk) + + // Flush all our temporary context + previous_tokens = [] + current_tokens = [] + previous_token_timestamps = [] + current_token_timestamps = [] + chunk = new_chunk() + } + } + + } else { + // 4/ Regular token + // We just append to the list of all tokens so we can handle + // merges later and decode into text. + current_tokens.push(token) + + if (returnWordTimestamps) { + let start_time = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.round)(token_timestamps[i] + time_offset, 2); + + let end_time; + if (i + 1 < token_timestamps.length) { + end_time = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.round)(token_timestamps[i + 1] + time_offset, 2); + + // Do not allow punctuation-only tokens to have a duration. + // This prevents long pauses from messing up the timestamps. + const decoded_text = this.decode([token]); + if (PUNCTUATION_ONLY_REGEX.test(decoded_text)) { + // Add `time_precision` to avoid overlapping timestamps + end_time = (0,_utils_maths_js__WEBPACK_IMPORTED_MODULE_3__.round)(Math.min(start_time + time_precision, end_time), 2); + } + } else { + // should never happen + end_time = null; + } + current_token_timestamps.push([start_time, end_time]); + } + + } + } + + if ('stride' in output) { + const [chunk_len, stride_left, stride_right] = output.stride; + time_offset += chunk_len - stride_right + } + + // Leftover tokens + if (current_tokens.length > 0) { + previous_tokens.push(current_tokens) + if (returnWordTimestamps) { + previous_token_timestamps.push(current_token_timestamps); + } + } else if (previous_tokens.every(p => p.length === 0)) { + // Flushing previous tokens (END)" + chunk = new_chunk() + previous_tokens = [] + current_tokens = [] + previous_token_timestamps = []; + current_token_timestamps = []; + } + + } + + if (previous_tokens.length > 0) { + if (force_full_sequences && return_timestamps) { + // Last token should always be timestamps, so there shouldn't be + // leftover + throw new Error( + "Whisper did not predict an ending timestamp, which can happen if audio is cut off in the middle of a word. " + + "Also make sure WhisperTimeStampLogitsProcessor was used during generation." + ); + } + + // Happens when we don't use timestamps + const [resolved_tokens, resolved_token_timestamps] = this.findLongestCommonSequence(previous_tokens, previous_token_timestamps); + + // Flushing previous tokens (FINAL) + const resolved_text = this.decode(resolved_tokens); + chunk.text = resolved_text; + if (returnWordTimestamps) { + chunk.words = this.collateWordTimestamps( + resolved_tokens, resolved_token_timestamps, last_language, + ) + } + chunks.push(chunk); + } + + let optional = Object.create(null); + + // Preparing and cleaning up the pipeline output + const full_text = chunks.map(chunk => chunk.text).join(''); + if (return_timestamps || return_language) { + for (let i = 0; i < chunks.length; ++i) { + const chunk = chunks[i]; + if (!return_timestamps) { + delete chunk["timestamp"]; + } + + if (!return_language) { + delete chunk["language"]; + } + } + if (returnWordTimestamps) { + const new_chunks = []; + for (const chunk of chunks) { + for (const word of chunk.words) { + new_chunks.push(word); + } + } + optional = { "chunks": new_chunks }; + } else { + optional = { "chunks": chunks }; + } + } + return [full_text, optional]; + + } + + /** + * Finds the longest common sequence among the provided sequences. + * @param {number[][]} sequences An array of sequences of token ids to compare. + * @returns {number[][]} The longest common sequence found. + * @throws {Error} If there is a bug within the function. + * @private + */ + findLongestCommonSequence(sequences, token_timestamp_sequences = null) { + // It would be much harder to do O(n) because of fault tolerance. + // We actually have a really good property which is that the total sequence + // MUST be those subsequences in order. + // If token_timestamp_sequences is provided, will split those sequences in + // exactly the same way. + let leftSequence = sequences[0]; + let leftLength = leftSequence.length; + let totalSequence = []; + + const use_token_timestamp_sequences = Array.isArray(token_timestamp_sequences) && token_timestamp_sequences.length > 0; + let total_token_timestamp_sequence = use_token_timestamp_sequences ? [] : null; + let left_token_timestamp_sequence = use_token_timestamp_sequences ? token_timestamp_sequences[0] : null; + for (let i = 1; i < sequences.length; ++i) { + const rightSequence = sequences[i]; + let max = 0.0; + let maxIndices = [leftLength, leftLength, 0, 0]; + // Here we're sliding matches + // [a, b, c, d] + // [c, d, f] + // = [c] == [d] + + // [a, b, c, d] + // [c, d, f] + // = [c, d] == [c, d] + + + // [a, b, c, d] + // [c, d, f] + + // = [b, c, d] == [c, d, f] + + // [a, b, c, d] + // [c, d, f] + + // [a, b, c] == [c, d, f] + + // [a, b, c, d] + // [d, f] + + // [a, b] == [d, f] + + // [a, b, c, d] + // [f] + + // [a] == [f] + + const rightLength = rightSequence.length; + for (let j = 1; j < leftLength + rightLength; ++j) { + // Slightly convoluted because we don't want out of bound indices + // This will be necessary for a small conflict resolution optimization + // later + const leftStart = Math.max(0, leftLength - j); + const leftStop = Math.min(leftLength, leftLength + rightLength - j); + const left = leftSequence.slice(leftStart, leftStop); + const rightStart = Math.max(0, j - leftLength); + const rightStop = Math.min(rightLength, j); + const right = rightSequence.slice(rightStart, rightStop); + if (left.length !== right.length) { + throw new Error("There is a bug within whisper `decode_asr` function, please report it. Dropping to prevent bad inference."); + } + + let matches; + if (use_token_timestamp_sequences) { + // Get length of longest subsequence of tokens that match + // and have timestamps that are in order + matches = left.filter((elem, idx) => ( + elem === right[idx] + && left_token_timestamp_sequence[leftStart + idx] <= token_timestamp_sequences[i][rightStart + idx] + )).length; + } else { + matches = left.filter((elem, idx) => elem === right[idx]).length; + } + + // epsilon to favor long perfect matches + const eps = j / 10000.0; + const matching = matches / j + eps; + if (matches > 1 && matching > max) { + max = matching; + maxIndices = [leftStart, leftStop, rightStart, rightStop]; + } + } + const [leftStart, leftStop, rightStart, rightStop] = maxIndices; + const leftMid = Math.floor((leftStop + leftStart) / 2); + const rightMid = Math.floor((rightStop + rightStart) / 2); + totalSequence.push(...leftSequence.slice(0, leftMid)); + leftSequence = rightSequence.slice(rightMid); + leftLength = leftSequence.length; + + if (use_token_timestamp_sequences) { + total_token_timestamp_sequence.push(...left_token_timestamp_sequence.slice(0, leftMid)); + left_token_timestamp_sequence = token_timestamp_sequences[i].slice(rightMid); + } + } + totalSequence.push(...leftSequence); + + if (use_token_timestamp_sequences) { + total_token_timestamp_sequence.push(...left_token_timestamp_sequence); + return [totalSequence, total_token_timestamp_sequence]; + } else { + return [totalSequence, []]; + } + } + + /** @private */ + collateWordTimestamps(tokens, token_timestamps, language) { + + const [words, _, token_indices] = this.combineTokensIntoWords(tokens, language); + + const timings = []; + for (let i = 0; i < words.length; ++i) { + const indices = token_indices[i]; + timings.push({ + text: words[i], + timestamp: [ + token_timestamps[indices.at(0)][0], + token_timestamps[indices.at(-1)][1], + ], + }); + } + return timings; + } + + /** + * Groups tokens by word. Returns a tuple containing a list of strings with the words, + * and a list of `token_id` sequences with the tokens making up each word. + * @param {number[]} tokens + * @param {string} [language] + * @param {string} prepend_punctionations + * @param {string} append_punctuations + * + * @private + */ + combineTokensIntoWords(tokens, language, prepend_punctionations = "\"'“¡¿([{-", append_punctuations = "\"'.。,,!!??::”)]}、") { + language = language ?? 'english'; + + let words, word_tokens, token_indices; + + if (["chinese", "japanese", "thai", "lao", "myanmar"].includes(language)) { + // These languages don't typically use spaces. + [words, word_tokens, token_indices] = this.splitTokensOnUnicode(tokens) + } else { + [words, word_tokens, token_indices] = this.splitTokensOnSpaces(tokens) + } + + return this.mergePunctuations(words, word_tokens, token_indices, prepend_punctionations, append_punctuations); + } + + /** @type {PreTrainedTokenizer['decode']} */ + decode( + token_ids, + decode_args, + ) { + let text; + // @ts-ignore + if (decode_args?.decode_with_timestamps) { + if (token_ids instanceof _utils_tensor_js__WEBPACK_IMPORTED_MODULE_4__.Tensor) { + token_ids = prepareTensorForDecode(token_ids); + } + text = this.decodeWithTimestamps(token_ids, decode_args); + } else { + text = super.decode(token_ids, decode_args); + } + // TODO: implement offsets + // if (decode_args.output_offsets) { + // let offsets = this.computeOffsets + // } + return text; + } + + /** + * @param {number[]|bigint[]} token_ids List of token IDs to decode. + * @param {Object} decode_args Optional arguments for decoding + * @private + */ + decodeWithTimestamps(token_ids, decode_args) { + const time_precision = decode_args?.time_precision ?? 0.02; + + const timestamp_begin = Array.from(this.all_special_ids).at(-1) + 1; + /**@type {Array} */ + let outputs = [[]]; + for (let token of token_ids) { + token = Number(token); + if (token >= timestamp_begin) { + const timestamp = ((token - timestamp_begin) * time_precision).toFixed(2); + outputs.push(`<|${timestamp}|>`); + outputs.push([]); + } else { + outputs[outputs.length - 1].push(token); + } + } + outputs = outputs.map( + s => typeof s === 'string' ? s : super.decode(s, decode_args) + ) + + return outputs.join(''); + } + + /** + * Combine tokens into words by splitting at any position where the tokens are decoded as valid unicode points. + * @param {number[]} tokens + * @returns {*} + * @private + */ + splitTokensOnUnicode(tokens) { + const decoded_full = this.decode(tokens, { + // @ts-ignore + decode_with_timestamps: true, + }); + const replacement_char = '\uFFFD'; + + const words = [] + const word_tokens = [] + const token_indices = [] + let current_tokens = [] + let current_indices = [] + let unicode_offset = 0 + + for (let token_idx = 0; token_idx < tokens.length; ++token_idx) { + const token = tokens[token_idx]; + + current_tokens.push(token); + current_indices.push(token_idx); + + const decoded = this.decode(current_tokens, { + // @ts-ignore + decode_with_timestamps: true, + }); + + if (!decoded.includes(replacement_char) || decoded_full[unicode_offset + decoded.indexOf(replacement_char)] === replacement_char) { + words.push(decoded) + word_tokens.push(current_tokens) + token_indices.push(current_indices) + current_tokens = [] + current_indices = [] + unicode_offset += decoded.length; + } + + } + + return [words, word_tokens, token_indices] + } + + /** + * Combine tokens into words by splitting at whitespace and punctuation tokens. + * @param {number[]} tokens + * @private + */ + splitTokensOnSpaces(tokens) { + + const [subwords, subword_tokens_list, subword_indices_list] = this.splitTokensOnUnicode(tokens); + + const words = [] + const word_tokens = [] + const token_indices = [] + + const punctuationRegex = new RegExp(`^[${PUNCTUATION_REGEX}]$`, 'gu'); + + for (let i = 0; i < subwords.length; ++i) { + + const subword = subwords[i]; + const subword_tokens = subword_tokens_list[i]; + const subword_indices = subword_indices_list[i]; + + // @ts-ignore + const special = subword_tokens[0] >= this.model.tokens_to_ids.get('<|endoftext|>'); + const with_space = subword.startsWith(' '); + const trimmed = subword.trim(); + const punctuation = punctuationRegex.test(trimmed); + + if (special || with_space || punctuation || words.length === 0) { + words.push(subword); + word_tokens.push(subword_tokens); + token_indices.push(subword_indices); + } else { + const ix = words.length - 1; + words[ix] += subword; + word_tokens[ix].push(...subword_tokens); + token_indices[ix].push(...subword_indices); + } + } + + return [words, word_tokens, token_indices]; + + } + + /** + * Merges punctuation tokens with neighboring words. + * @param {string[]} words + * @param {number[][]} tokens + * @param {number[][]} indices + * @param {string} prepended + * @param {string} appended + * @private + */ + mergePunctuations(words, tokens, indices, prepended, appended) { + + const newWords = structuredClone(words); + const newTokens = structuredClone(tokens); + const newIndices = structuredClone(indices); + + + // prepend punctuations + let i = newWords.length - 2; + let j = newWords.length - 1; + + while (i >= 0) { + if (newWords[i].startsWith(' ') && prepended.includes(newWords[i].trim())) { + newWords[j] = newWords[i] + newWords[j]; + newTokens[j] = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(newTokens[i], newTokens[j]); + newIndices[j] = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(newIndices[i], newIndices[j]); + newWords[i] = ''; + newTokens[i] = []; + newIndices[i] = []; + } else { + j = i; + } + --i; + } + + // append punctuations + i = 0; + j = 1; + while (j < newWords.length) { + if (!newWords[i].endsWith(' ') && appended.includes(newWords[j])) { + newWords[i] += newWords[j]; + newTokens[i] = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(newTokens[i], newTokens[j]); + newIndices[i] = (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)(newIndices[i], newIndices[j]); + newWords[j] = ''; + newTokens[j] = []; + newIndices[j] = []; + } else { + i = j; + } + ++j; + } + + return [ + newWords.filter(x => x), + newTokens.filter(x => x.length > 0), + newIndices.filter(x => x.length > 0), + ] + } +} +class CodeGenTokenizer extends PreTrainedTokenizer { } +class CLIPTokenizer extends PreTrainedTokenizer { } +class SiglipTokenizer extends PreTrainedTokenizer { } + +/** + * @todo This model is not yet supported by Hugging Face's "fast" tokenizers library (https://github.com/huggingface/tokenizers). + * Therefore, this implementation (which is based on fast tokenizers) may produce slightly inaccurate results. + */ +class MarianTokenizer extends PreTrainedTokenizer { + /** + * Create a new MarianTokenizer instance. + * @param {Object} tokenizerJSON The JSON of the tokenizer. + * @param {Object} tokenizerConfig The config of the tokenizer. + */ + constructor(tokenizerJSON, tokenizerConfig) { + super(tokenizerJSON, tokenizerConfig); + + this.languageRegex = /^(>>\w+<<)\s*/g; + + this.supported_language_codes = this.model.vocab.filter( + x => this.languageRegex.test(x) + ); + + console.warn('WARNING: `MarianTokenizer` is not yet supported by Hugging Face\'s "fast" tokenizers library. Therefore, you may experience slightly inaccurate results.') + } + + /** + * Encodes a single text. Overriding this method is necessary since the language codes + * must be removed before encoding with sentencepiece model. + * @see https://github.com/huggingface/transformers/blob/12d51db243a00726a548a43cc333390ebae731e3/src/transformers/models/marian/tokenization_marian.py#L204-L213 + * + * @param {string|null} text The text to encode. + * @returns {Array} The encoded tokens. + */ + _encode_text(text) { + if (text === null) return null; + + // Check if text starts with language code: + const [matchInfo, ...remainder] = text.trim().split(this.languageRegex); + + if (remainder.length === 0) { + // No language code, encode normally + return super._encode_text(matchInfo); + + } else if (remainder.length === 2) { + // Text starts with language code, so we do not encode it with sentencepiece. + const [language, text] = remainder; + + if (!this.supported_language_codes.includes(language)) { + console.warn(`Unsupported language code "${language}" detected, which may lead to unexpected behavior. Should be one of: ${JSON.stringify(this.supported_language_codes)}`) + } + return (0,_utils_core_js__WEBPACK_IMPORTED_MODULE_1__.mergeArrays)([language], super._encode_text(text)); + } + } + +} + +class Wav2Vec2CTCTokenizer extends PreTrainedTokenizer { } + +class BlenderbotTokenizer extends PreTrainedTokenizer { } +class BlenderbotSmallTokenizer extends PreTrainedTokenizer { } + +class SpeechT5Tokenizer extends PreTrainedTokenizer { } + +class NougatTokenizer extends PreTrainedTokenizer { } + +class VitsTokenizer extends PreTrainedTokenizer { + + constructor(tokenizerJSON, tokenizerConfig) { + super(tokenizerJSON, tokenizerConfig); + + // Custom decoder function + this.decoder = new VitsDecoder({}); + } +} + +class CohereTokenizer extends PreTrainedTokenizer { } + +/** + * Helper class which is used to instantiate pretrained tokenizers with the `from_pretrained` function. + * The chosen tokenizer class is determined by the type specified in the tokenizer config. + * + * @example + * const tokenizer = await AutoTokenizer.from_pretrained('Xenova/bert-base-uncased'); + */ +class AutoTokenizer { + static TOKENIZER_CLASS_MAPPING = { + T5Tokenizer, + DistilBertTokenizer, + CamembertTokenizer, + DebertaTokenizer, + DebertaV2Tokenizer, + BertTokenizer, + HerbertTokenizer, + ConvBertTokenizer, + RoFormerTokenizer, + XLMTokenizer, + ElectraTokenizer, + MobileBertTokenizer, + SqueezeBertTokenizer, + AlbertTokenizer, + GPT2Tokenizer, + BartTokenizer, + MBartTokenizer, + MBart50Tokenizer, + RobertaTokenizer, + WhisperTokenizer, + CodeGenTokenizer, + CLIPTokenizer, + SiglipTokenizer, + MarianTokenizer, + BloomTokenizer, + NllbTokenizer, + M2M100Tokenizer, + LlamaTokenizer, + CodeLlamaTokenizer, + XLMRobertaTokenizer, + MPNetTokenizer, + FalconTokenizer, + GPTNeoXTokenizer, + EsmTokenizer, + Wav2Vec2CTCTokenizer, + BlenderbotTokenizer, + BlenderbotSmallTokenizer, + SpeechT5Tokenizer, + NougatTokenizer, + VitsTokenizer, + Qwen2Tokenizer, + GemmaTokenizer, + Grok1Tokenizer, + CohereTokenizer, + + // Base case: + PreTrainedTokenizer, + } + + + /** + * Instantiate one of the tokenizer classes of the library from a pretrained model. + * + * The tokenizer class to instantiate is selected based on the `tokenizer_class` property of the config object + * (either passed as an argument or loaded from `pretrained_model_name_or_path` if possible) + * + * @param {string} pretrained_model_name_or_path The name or path of the pretrained model. Can be either: + * - A string, the *model id* of a pretrained tokenizer hosted inside a model repo on huggingface.co. + * Valid model ids can be located at the root-level, like `bert-base-uncased`, or namespaced under a + * user or organization name, like `dbmdz/bert-base-german-cased`. + * - A path to a *directory* containing tokenizer files, e.g., `./my_model_directory/`. + * @param {PretrainedTokenizerOptions} options Additional options for loading the tokenizer. + * + * @returns {Promise} A new instance of the PreTrainedTokenizer class. + */ + static async from_pretrained(pretrained_model_name_or_path, { + progress_callback = null, + config = null, + cache_dir = null, + local_files_only = false, + revision = 'main', + legacy = null, + } = {}) { + + const [tokenizerJSON, tokenizerConfig] = await loadTokenizer(pretrained_model_name_or_path, { + progress_callback, + config, + cache_dir, + local_files_only, + revision, + legacy, + }) + + // Some tokenizers are saved with the "Fast" suffix, so we remove that if present. + const tokenizerName = tokenizerConfig.tokenizer_class?.replace(/Fast$/, '') ?? 'PreTrainedTokenizer'; + + let cls = this.TOKENIZER_CLASS_MAPPING[tokenizerName]; + if (!cls) { + console.warn(`Unknown tokenizer class "${tokenizerName}", attempting to construct from base class.`); + cls = PreTrainedTokenizer; + } + return new cls(tokenizerJSON, tokenizerConfig); + } +} + + +/***/ }), + +/***/ "./src/utils/audio.js": +/*!****************************!*\ + !*** ./src/utils/audio.js ***! + \****************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ hamming: () => (/* binding */ hamming), +/* harmony export */ hanning: () => (/* binding */ hanning), +/* harmony export */ mel_filter_bank: () => (/* binding */ mel_filter_bank), +/* harmony export */ read_audio: () => (/* binding */ read_audio), +/* harmony export */ spectrogram: () => (/* binding */ spectrogram), +/* harmony export */ window_function: () => (/* binding */ window_function) +/* harmony export */ }); +/* harmony import */ var _hub_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./hub.js */ "./src/utils/hub.js"); +/* harmony import */ var _maths_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./maths.js */ "./src/utils/maths.js"); +/* harmony import */ var _core_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./core.js */ "./src/utils/core.js"); +/* harmony import */ var _tensor_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./tensor.js */ "./src/utils/tensor.js"); +/** + * @file Helper module for audio processing. + * + * These functions and classes are only used internally, + * meaning an end-user shouldn't need to access anything here. + * + * @module utils/audio + */ + + + + + + + +/** + * Helper function to read audio from a path/URL. + * @param {string|URL} url The path/URL to load the audio from. + * @param {number} sampling_rate The sampling rate to use when decoding the audio. + * @returns {Promise} The decoded audio as a `Float32Array`. + */ +async function read_audio(url, sampling_rate) { + if (typeof AudioContext === 'undefined') { + // Running in node or an environment without AudioContext + throw Error( + "Unable to load audio from path/URL since `AudioContext` is not available in your environment. " + + "Instead, audio data should be passed directly to the pipeline/processor. " + + "For more information and some example code, see https://huggingface.co/docs/transformers.js/guides/node-audio-processing." + ) + } + + const response = await (await (0,_hub_js__WEBPACK_IMPORTED_MODULE_0__.getFile)(url)).arrayBuffer(); + const audioCTX = new AudioContext({ sampleRate: sampling_rate }); + if (typeof sampling_rate === 'undefined') { + console.warn(`No sampling rate provided, using default of ${audioCTX.sampleRate}Hz.`) + } + const decoded = await audioCTX.decodeAudioData(response); + + /** @type {Float32Array} */ + let audio; + + // We now replicate HuggingFace's `ffmpeg_read` method: + if (decoded.numberOfChannels === 2) { + // When downmixing a stereo audio file to mono using the -ac 1 option in FFmpeg, + // the audio signal is summed across both channels to create a single mono channel. + // However, if the audio is at full scale (i.e. the highest possible volume level), + // the summing of the two channels can cause the audio signal to clip or distort. + + // To prevent this clipping, FFmpeg applies a scaling factor of 1/sqrt(2) (~ 0.707) + // to the audio signal before summing the two channels. This scaling factor ensures + // that the combined audio signal will not exceed the maximum possible level, even + // if both channels are at full scale. + + // After applying this scaling factor, the audio signal from both channels is summed + // to create a single mono channel. It's worth noting that this scaling factor is + // only applied when downmixing stereo audio to mono using the -ac 1 option in FFmpeg. + // If you're using a different downmixing method, or if you're not downmixing the + // audio at all, this scaling factor may not be needed. + const SCALING_FACTOR = Math.sqrt(2); + + const left = decoded.getChannelData(0); + const right = decoded.getChannelData(1); + + audio = new Float32Array(left.length); + for (let i = 0; i < decoded.length; ++i) { + audio[i] = SCALING_FACTOR * (left[i] + right[i]) / 2; + } + + } else { + // If the audio is not stereo, we can just use the first channel: + audio = decoded.getChannelData(0); + } + + return audio; +} + +/** + * Helper function to generate windows that are special cases of the generalized cosine window. + * See https://www.mathworks.com/help/signal/ug/generalized-cosine-windows.html for more information. + * @param {number} M Number of points in the output window. If zero or less, an empty array is returned. + * @param {number} a_0 Offset for the generalized cosine window. + * @returns {Float64Array} The generated window. + */ +function generalized_cosine_window(M, a_0) { + if (M < 1) { + return new Float64Array(); + } + if (M === 1) { + return new Float64Array([1]); + } + + const a_1 = 1 - a_0; + const factor = 2 * Math.PI / (M - 1); + + const cos_vals = new Float64Array(M); + for (let i = 0; i < M; ++i) { + cos_vals[i] = a_0 - a_1 * Math.cos(i * factor); + } + return cos_vals; +} + +/** + * Generates a Hanning window of length M. + * See https://numpy.org/doc/stable/reference/generated/numpy.hanning.html for more information. + * + * @param {number} M The length of the Hanning window to generate. + * @returns {Float64Array} The generated Hanning window. + */ +function hanning(M) { + return generalized_cosine_window(M, 0.5); +} + + +/** + * Generates a Hamming window of length M. + * See https://numpy.org/doc/stable/reference/generated/numpy.hamming.html for more information. + * + * @param {number} M The length of the Hamming window to generate. + * @returns {Float64Array} The generated Hamming window. + */ +function hamming(M) { + return generalized_cosine_window(M, 0.54); +} + + +const HERTZ_TO_MEL_MAPPING = { + "htk": (/** @type {number} */ freq) => 2595.0 * Math.log10(1.0 + (freq / 700.0)), + "kaldi": (/** @type {number} */ freq) => 1127.0 * Math.log(1.0 + (freq / 700.0)), + "slaney": (/** @type {number} */ freq, min_log_hertz = 1000.0, min_log_mel = 15.0, logstep = 27.0 / Math.log(6.4)) => + freq >= min_log_hertz + ? min_log_mel + Math.log(freq / min_log_hertz) * logstep + : 3.0 * freq / 200.0, +} + +/** + * @template {Float32Array|Float64Array|number} T + * @param {T} freq + * @param {string} [mel_scale] + * @returns {T} + */ +function hertz_to_mel(freq, mel_scale = "htk") { + const fn = HERTZ_TO_MEL_MAPPING[mel_scale]; + if (!fn) { + throw new Error('mel_scale should be one of "htk", "slaney" or "kaldi".'); + } + + return typeof freq === 'number' ? fn(freq) : freq.map(x => fn(x)); +} + +const MEL_TO_HERTZ_MAPPING = { + "htk": (/** @type {number} */ mels) => 700.0 * (10.0 ** (mels / 2595.0) - 1.0), + "kaldi": (/** @type {number} */ mels) => 700.0 * (Math.exp(mels / 1127.0) - 1.0), + "slaney": (/** @type {number} */ mels, min_log_hertz = 1000.0, min_log_mel = 15.0, logstep = Math.log(6.4) / 27.0) => mels >= min_log_mel + ? min_log_hertz * Math.exp(logstep * (mels - min_log_mel)) + : 200.0 * mels / 3.0, +} + +/** + * @template {Float32Array|Float64Array|number} T + * @param {T} mels + * @param {string} [mel_scale] + * @returns {T} + */ +function mel_to_hertz(mels, mel_scale = "htk") { + const fn = MEL_TO_HERTZ_MAPPING[mel_scale]; + if (!fn) { + throw new Error('mel_scale should be one of "htk", "slaney" or "kaldi".'); + } + + return typeof mels === 'number' ? fn(mels) : mels.map(x => fn(x)); +} + +/** +* Creates a triangular filter bank. +* +* Adapted from torchaudio and librosa. +* +* @param {Float64Array} fft_freqs Discrete frequencies of the FFT bins in Hz, of shape `(num_frequency_bins,)`. +* @param {Float64Array} filter_freqs Center frequencies of the triangular filters to create, in Hz, of shape `(num_mel_filters,)`. +* @returns {number[][]} of shape `(num_frequency_bins, num_mel_filters)`. +*/ +function _create_triangular_filter_bank(fft_freqs, filter_freqs) { + const filter_diff = Float64Array.from( + { length: filter_freqs.length - 1 }, + (_, i) => filter_freqs[i + 1] - filter_freqs[i] + ); + + const slopes = Array.from({ + length: fft_freqs.length + }, () => new Array(filter_freqs.length)); + + for (let j = 0; j < fft_freqs.length; ++j) { + const slope = slopes[j]; + for (let i = 0; i < filter_freqs.length; ++i) { + slope[i] = filter_freqs[i] - fft_freqs[j]; + } + } + + const numFreqs = filter_freqs.length - 2; + const ret = Array.from({ length: numFreqs }, () => new Array(fft_freqs.length)); + + for (let j = 0; j < fft_freqs.length; ++j) { // 201 + const slope = slopes[j]; + for (let i = 0; i < numFreqs; ++i) { // 80 + const down = -slope[i] / filter_diff[i]; + const up = slope[i + 2] / filter_diff[i + 1]; + ret[i][j] = Math.max(0, Math.min(down, up)); + } + } + return ret; +} + +/** + * Return evenly spaced numbers over a specified interval. + * @param {number} start The starting value of the sequence. + * @param {number} end The end value of the sequence. + * @param {number} num Number of samples to generate. + * @returns `num` evenly spaced samples, calculated over the interval `[start, stop]`. + */ +function linspace(start, end, num) { + const step = (end - start) / (num - 1); + return Float64Array.from({ length: num }, (_, i) => start + step * i); +} + +/** + * Creates a frequency bin conversion matrix used to obtain a mel spectrogram. This is called a *mel filter bank*, and + * various implementation exist, which differ in the number of filters, the shape of the filters, the way the filters + * are spaced, the bandwidth of the filters, and the manner in which the spectrum is warped. The goal of these + * features is to approximate the non-linear human perception of the variation in pitch with respect to the frequency. + * @param {number} num_frequency_bins Number of frequencies used to compute the spectrogram (should be the same as in `stft`). + * @param {number} num_mel_filters Number of mel filters to generate. + * @param {number} min_frequency Lowest frequency of interest in Hz. + * @param {number} max_frequency Highest frequency of interest in Hz. This should not exceed `sampling_rate / 2`. + * @param {number} sampling_rate Sample rate of the audio waveform. + * @param {string} [norm] If `"slaney"`, divide the triangular mel weights by the width of the mel band (area normalization). + * @param {string} [mel_scale] The mel frequency scale to use, `"htk"` or `"slaney"`. + * @param {boolean} [triangularize_in_mel_space] If this option is enabled, the triangular filter is applied in mel space rather than frequency space. + * This should be set to `true` in order to get the same results as `torchaudio` when computing mel filters. + * @returns {number[][]} Triangular filter bank matrix, which is a 2D array of shape (`num_frequency_bins`, `num_mel_filters`). + * This is a projection matrix to go from a spectrogram to a mel spectrogram. + */ +function mel_filter_bank( + num_frequency_bins, + num_mel_filters, + min_frequency, + max_frequency, + sampling_rate, + norm = null, + mel_scale = "htk", + triangularize_in_mel_space = false, +) { + if (norm !== null && norm !== "slaney") { + throw new Error('norm must be one of null or "slaney"'); + } + + const mel_min = hertz_to_mel(min_frequency, mel_scale); + const mel_max = hertz_to_mel(max_frequency, mel_scale); + const mel_freqs = linspace(mel_min, mel_max, num_mel_filters + 2); + + let filter_freqs = mel_to_hertz(mel_freqs, mel_scale); + let fft_freqs; // frequencies of FFT bins in Hz + + if (triangularize_in_mel_space) { + const fft_bin_width = sampling_rate / (num_frequency_bins * 2); + fft_freqs = hertz_to_mel(Float64Array.from({ length: num_frequency_bins }, (_, i) => i * fft_bin_width), mel_scale); + filter_freqs = mel_freqs; + } else { + fft_freqs = linspace(0, Math.floor(sampling_rate / 2), num_frequency_bins); + } + + const mel_filters = _create_triangular_filter_bank(fft_freqs, filter_freqs); + + if (norm !== null && norm === "slaney") { + // Slaney-style mel is scaled to be approx constant energy per channel + for (let i = 0; i < num_mel_filters; ++i) { + const filter = mel_filters[i]; + const enorm = 2.0 / (filter_freqs[i + 2] - filter_freqs[i]); + for (let j = 0; j < num_frequency_bins; ++j) { + // Apply this enorm to all frequency bins + filter[j] *= enorm; + } + } + } + + // TODO warn if there is a zero row + + return mel_filters; + +} + +/** + * @template {Float32Array|Float64Array} T + * Pads an array with a reflected version of itself on both ends. + * @param {T} array The array to pad. + * @param {number} left The amount of padding to add to the left. + * @param {number} right The amount of padding to add to the right. + * @returns {T} The padded array. + */ +function padReflect(array, left, right) { + // @ts-ignore + const padded = new array.constructor(array.length + left + right); + const w = array.length - 1; + + for (let i = 0; i < array.length; ++i) { + padded[left + i] = array[i]; + } + + for (let i = 1; i <= left; ++i) { + padded[left - i] = array[(0,_core_js__WEBPACK_IMPORTED_MODULE_2__.calculateReflectOffset)(i, w)]; + } + + for (let i = 1; i <= right; ++i) { + padded[w + left + i] = array[(0,_core_js__WEBPACK_IMPORTED_MODULE_2__.calculateReflectOffset)(w - i, w)]; + } + + return padded; +} + +/** + * Helper function to compute `amplitude_to_db` and `power_to_db`. + * @template {Float32Array|Float64Array} T + * @param {T} spectrogram + * @param {number} factor + * @param {number} reference + * @param {number} min_value + * @param {number} db_range + * @returns {T} + */ +function _db_conversion_helper(spectrogram, factor, reference, min_value, db_range) { + if (reference <= 0) { + throw new Error('reference must be greater than zero'); + } + + if (min_value <= 0) { + throw new Error('min_value must be greater than zero'); + } + + reference = Math.max(min_value, reference); + + const logReference = Math.log10(reference); + for (let i = 0; i < spectrogram.length; ++i) { + spectrogram[i] = factor * Math.log10(Math.max(min_value, spectrogram[i]) - logReference) + } + + if (db_range !== null) { + if (db_range <= 0) { + throw new Error('db_range must be greater than zero'); + } + const maxValue = (0,_maths_js__WEBPACK_IMPORTED_MODULE_1__.max)(spectrogram)[0] - db_range; + for (let i = 0; i < spectrogram.length; ++i) { + spectrogram[i] = Math.max(spectrogram[i], maxValue); + } + } + + return spectrogram; +} + +/** + * Converts an amplitude spectrogram to the decibel scale. This computes `20 * log10(spectrogram / reference)`, + * using basic logarithm properties for numerical stability. NOTE: Operates in-place. + * + * The motivation behind applying the log function on the (mel) spectrogram is that humans do not hear loudness on a + * linear scale. Generally to double the perceived volume of a sound we need to put 8 times as much energy into it. + * This means that large variations in energy may not sound all that different if the sound is loud to begin with. + * This compression operation makes the (mel) spectrogram features match more closely what humans actually hear. + * + * @template {Float32Array|Float64Array} T + * @param {T} spectrogram The input amplitude (mel) spectrogram. + * @param {number} [reference=1.0] Sets the input spectrogram value that corresponds to 0 dB. + * For example, use `np.max(spectrogram)` to set the loudest part to 0 dB. Must be greater than zero. + * @param {number} [min_value=1e-5] The spectrogram will be clipped to this minimum value before conversion to decibels, + * to avoid taking `log(0)`. The default of `1e-5` corresponds to a minimum of -100 dB. Must be greater than zero. + * @param {number} [db_range=null] Sets the maximum dynamic range in decibels. For example, if `db_range = 80`, the + * difference between the peak value and the smallest value will never be more than 80 dB. Must be greater than zero. + * @returns {T} The modified spectrogram in decibels. + */ +function amplitude_to_db(spectrogram, reference = 1.0, min_value = 1e-5, db_range = null) { + return _db_conversion_helper(spectrogram, 20.0, reference, min_value, db_range); +} + +/** + * Converts a power spectrogram to the decibel scale. This computes `10 * log10(spectrogram / reference)`, + * using basic logarithm properties for numerical stability. NOTE: Operates in-place. + * + * The motivation behind applying the log function on the (mel) spectrogram is that humans do not hear loudness on a + * linear scale. Generally to double the perceived volume of a sound we need to put 8 times as much energy into it. + * This means that large variations in energy may not sound all that different if the sound is loud to begin with. + * This compression operation makes the (mel) spectrogram features match more closely what humans actually hear. + * + * Based on the implementation of `librosa.power_to_db`. + * + * @template {Float32Array|Float64Array} T + * @param {T} spectrogram The input power (mel) spectrogram. Note that a power spectrogram has the amplitudes squared! + * @param {number} [reference=1.0] Sets the input spectrogram value that corresponds to 0 dB. + * For example, use `np.max(spectrogram)` to set the loudest part to 0 dB. Must be greater than zero. + * @param {number} [min_value=1e-10] The spectrogram will be clipped to this minimum value before conversion to decibels, + * to avoid taking `log(0)`. The default of `1e-10` corresponds to a minimum of -100 dB. Must be greater than zero. + * @param {number} [db_range=null] Sets the maximum dynamic range in decibels. For example, if `db_range = 80`, the + * difference between the peak value and the smallest value will never be more than 80 dB. Must be greater than zero. + * @returns {T} The modified spectrogram in decibels. + */ +function power_to_db(spectrogram, reference = 1.0, min_value = 1e-10, db_range = null) { + return _db_conversion_helper(spectrogram, 10.0, reference, min_value, db_range); +} + +/** + * Calculates a spectrogram over one waveform using the Short-Time Fourier Transform. + * + * This function can create the following kinds of spectrograms: + * - amplitude spectrogram (`power = 1.0`) + * - power spectrogram (`power = 2.0`) + * - complex-valued spectrogram (`power = None`) + * - log spectrogram (use `log_mel` argument) + * - mel spectrogram (provide `mel_filters`) + * - log-mel spectrogram (provide `mel_filters` and `log_mel`) + * + * In this implementation, the window is assumed to be zero-padded to have the same size as the analysis frame. + * A padded window can be obtained from `window_function()`. The FFT input buffer may be larger than the analysis frame, + * typically the next power of two. + * + * @param {Float32Array|Float64Array} waveform The input waveform of shape `(length,)`. This must be a single real-valued, mono waveform. + * @param {Float32Array|Float64Array} window The windowing function to apply of shape `(frame_length,)`, including zero-padding if necessary. The actual window length may be + * shorter than `frame_length`, but we're assuming the array has already been zero-padded. + * @param {number} frame_length The length of the analysis frames in samples (a.k.a., `fft_length`). + * @param {number} hop_length The stride between successive analysis frames in samples. + * @param {Object} options + * @param {number} [options.fft_length=null] The size of the FFT buffer in samples. This determines how many frequency bins the spectrogram will have. + * For optimal speed, this should be a power of two. If `null`, uses `frame_length`. + * @param {number} [options.power=1.0] If 1.0, returns the amplitude spectrogram. If 2.0, returns the power spectrogram. If `null`, returns complex numbers. + * @param {boolean} [options.center=true] Whether to pad the waveform so that frame `t` is centered around time `t * hop_length`. If `false`, frame + * `t` will start at time `t * hop_length`. + * @param {string} [options.pad_mode="reflect"] Padding mode used when `center` is `true`. Possible values are: `"constant"` (pad with zeros), + * `"edge"` (pad with edge values), `"reflect"` (pads with mirrored values). + * @param {boolean} [options.onesided=true] If `true`, only computes the positive frequencies and returns a spectrogram containing `fft_length // 2 + 1` + * frequency bins. If `false`, also computes the negative frequencies and returns `fft_length` frequency bins. + * @param {number} [options.preemphasis=null] Coefficient for a low-pass filter that applies pre-emphasis before the DFT. + * @param {number[][]} [options.mel_filters=null] The mel filter bank of shape `(num_freq_bins, num_mel_filters)`. + * If supplied, applies this filter bank to create a mel spectrogram. + * @param {number} [options.mel_floor=1e-10] Minimum value of mel frequency banks. + * @param {string} [options.log_mel=null] How to convert the spectrogram to log scale. Possible options are: + * `null` (don't convert), `"log"` (take the natural logarithm) `"log10"` (take the base-10 logarithm), `"dB"` (convert to decibels). + * Can only be used when `power` is not `null`. + * @param {number} [options.reference=1.0] Sets the input spectrogram value that corresponds to 0 dB. For example, use `max(spectrogram)[0]` to set + * the loudest part to 0 dB. Must be greater than zero. + * @param {number} [options.min_value=1e-10] The spectrogram will be clipped to this minimum value before conversion to decibels, to avoid taking `log(0)`. + * For a power spectrogram, the default of `1e-10` corresponds to a minimum of -100 dB. For an amplitude spectrogram, the value `1e-5` corresponds to -100 dB. + * Must be greater than zero. + * @param {number} [options.db_range=null] Sets the maximum dynamic range in decibels. For example, if `db_range = 80`, the difference between the + * peak value and the smallest value will never be more than 80 dB. Must be greater than zero. + * @param {boolean} [options.remove_dc_offset=null] Subtract mean from waveform on each frame, applied before pre-emphasis. This should be set to `true` in + * order to get the same results as `torchaudio.compliance.kaldi.fbank` when computing mel filters. + * @param {number} [options.max_num_frames=null] If provided, limits the number of frames to compute to this value. + * @param {number} [options.min_num_frames=null] If provided, ensures the number of frames to compute is at least this value. + * @param {boolean} [options.do_pad=true] If `true`, pads the output spectrogram to have `max_num_frames` frames. + * @param {boolean} [options.transpose=false] If `true`, the returned spectrogram will have shape `(num_frames, num_frequency_bins/num_mel_filters)`. If `false`, the returned spectrogram will have shape `(num_frequency_bins/num_mel_filters, num_frames)`. + * @returns {Promise} Spectrogram of shape `(num_frequency_bins, length)` (regular spectrogram) or shape `(num_mel_filters, length)` (mel spectrogram). + */ +async function spectrogram( + waveform, + window, + frame_length, + hop_length, + { + fft_length = null, + power = 1.0, + center = true, + pad_mode = "reflect", + onesided = true, + preemphasis = null, + mel_filters = null, + mel_floor = 1e-10, + log_mel = null, + reference = 1.0, + min_value = 1e-10, + db_range = null, + remove_dc_offset = null, + + // Custom parameters for efficiency reasons + min_num_frames = null, + max_num_frames = null, + do_pad = true, + transpose = false, + } = {} +) { + const window_length = window.length; + if (fft_length === null) { + fft_length = frame_length; + } + if (frame_length > fft_length) { + throw Error(`frame_length (${frame_length}) may not be larger than fft_length (${fft_length})`) + } + + if (window_length !== frame_length) { + throw new Error(`Length of the window (${window_length}) must equal frame_length (${frame_length})`); + } + + if (hop_length <= 0) { + throw new Error("hop_length must be greater than zero"); + } + + if (power === null && mel_filters !== null) { + throw new Error( + "You have provided `mel_filters` but `power` is `None`. Mel spectrogram computation is not yet supported for complex-valued spectrogram. " + + "Specify `power` to fix this issue." + ); + } + + if (center) { + if (pad_mode !== 'reflect') { + throw new Error(`pad_mode="${pad_mode}" not implemented yet.`) + } + const half_window = Math.floor((fft_length - 1) / 2) + 1; + waveform = padReflect(waveform, half_window, half_window); + } + + // split waveform into frames of frame_length size + let num_frames = Math.floor(1 + Math.floor((waveform.length - frame_length) / hop_length)) + if (min_num_frames !== null && num_frames < min_num_frames) { + num_frames = min_num_frames + } + const num_frequency_bins = onesided ? Math.floor(fft_length / 2) + 1 : fft_length + + let d1 = num_frames; + let d1Max = num_frames; + + // If maximum number of frames is provided, we must either pad or truncate + if (max_num_frames !== null) { + if (max_num_frames > num_frames) { // input is too short, so we pad + if (do_pad) { + d1Max = max_num_frames; + } + } else { // input is too long, so we truncate + d1Max = d1 = max_num_frames; + } + } + + // Preallocate arrays to store output. + const fft = new _maths_js__WEBPACK_IMPORTED_MODULE_1__.FFT(fft_length); + const inputBuffer = new Float64Array(fft_length); + const outputBuffer = new Float64Array(fft.outputBufferSize); + const transposedMagnitudeData = new Float32Array(num_frequency_bins * d1Max); + + for (let i = 0; i < d1; ++i) { + // Populate buffer with waveform data + const offset = i * hop_length; + const buffer_size = Math.min(waveform.length - offset, frame_length); + if (buffer_size !== frame_length) { + // The full buffer is not needed, so we need to reset it (avoid overflow from previous iterations) + // NOTE: We don't need to reset the buffer if it's full since we overwrite the first + // `frame_length` values and the rest (`fft_length - frame_length`) remains zero. + inputBuffer.fill(0, 0, frame_length); + } + + for (let j = 0; j < buffer_size; ++j) { + inputBuffer[j] = waveform[offset + j]; + } + + if (remove_dc_offset) { + let sum = 0; + for (let j = 0; j < buffer_size; ++j) { + sum += inputBuffer[j]; + } + const mean = sum / buffer_size; + for (let j = 0; j < buffer_size; ++j) { + inputBuffer[j] -= mean; + } + } + + if (preemphasis !== null) { + // Done in reverse to avoid copies and distructive modification + for (let j = buffer_size - 1; j >= 1; --j) { + inputBuffer[j] -= preemphasis * inputBuffer[j - 1]; + } + inputBuffer[0] *= 1 - preemphasis; + } + + // Apply window function + for (let j = 0; j < window.length; ++j) { + inputBuffer[j] *= window[j]; + } + + fft.realTransform(outputBuffer, inputBuffer); + + // compute magnitudes + for (let j = 0; j < num_frequency_bins; ++j) { + const j2 = j << 1; + + // NOTE: We transpose the data here to avoid doing it later + transposedMagnitudeData[j * d1Max + i] = outputBuffer[j2] ** 2 + outputBuffer[j2 + 1] ** 2; + } + } + + if (power !== null && power !== 2) { + // slight optimization to not sqrt + const pow = 2 / power; // we use 2 since we already squared + for (let i = 0; i < transposedMagnitudeData.length; ++i) { + transposedMagnitudeData[i] **= pow; + } + } + + // TODO: What if `mel_filters` is null? + const num_mel_filters = mel_filters.length; + + // Perform matrix muliplication: + // mel_spec = mel_filters @ magnitudes.T + // - mel_filters.shape=(80, 201) + // - magnitudes.shape=(3000, 201) => magnitudes.T.shape=(201, 3000) + // - mel_spec.shape=(80, 3000) + let mel_spec = await (0,_tensor_js__WEBPACK_IMPORTED_MODULE_3__.matmul)( + // TODO: Make `mel_filters` a Tensor during initialization + new _tensor_js__WEBPACK_IMPORTED_MODULE_3__.Tensor('float32', mel_filters.flat(), [num_mel_filters, num_frequency_bins]), + new _tensor_js__WEBPACK_IMPORTED_MODULE_3__.Tensor('float32', transposedMagnitudeData, [num_frequency_bins, d1Max]), + ); + if (transpose) { + mel_spec = mel_spec.transpose(1, 0); + } + + const mel_spec_data = /** @type {Float32Array} */(mel_spec.data); + for (let i = 0; i < mel_spec_data.length; ++i) { + mel_spec_data[i] = Math.max(mel_floor, mel_spec_data[i]); + } + + if (power !== null && log_mel !== null) { + const o = Math.min(mel_spec_data.length, d1 * num_mel_filters); + // NOTE: operates in-place + switch (log_mel) { + case 'log': + for (let i = 0; i < o; ++i) { + mel_spec_data[i] = Math.log(mel_spec_data[i]); + } + break; + case 'log10': + for (let i = 0; i < o; ++i) { + mel_spec_data[i] = Math.log10(mel_spec_data[i]); + } + break; + case 'dB': + if (power === 1.0) { + amplitude_to_db(mel_spec_data, reference, min_value, db_range); + } else if (power === 2.0) { + power_to_db(mel_spec_data, reference, min_value, db_range); + } else { + throw new Error(`Cannot use log_mel option '${log_mel}' with power ${power}`) + } + break; + default: + throw new Error(`log_mel must be one of null, 'log', 'log10' or 'dB'. Got '${log_mel}'`); + } + } + + return mel_spec; +} + +/** + * Returns an array containing the specified window. + * @param {number} window_length The length of the window in samples. + * @param {string} name The name of the window function. + * @param {Object} options Additional options. + * @param {boolean} [options.periodic=true] Whether the window is periodic or symmetric. + * @param {number} [options.frame_length=null] The length of the analysis frames in samples. + * Provide a value for `frame_length` if the window is smaller than the frame length, so that it will be zero-padded. + * @param {boolean} [options.center=true] Whether to center the window inside the FFT buffer. Only used when `frame_length` is provided. + * @returns {Float64Array} The window of shape `(window_length,)` or `(frame_length,)`. + */ +function window_function(window_length, name, { + periodic = true, + frame_length = null, + center = true, +} = {}) { + const length = periodic ? window_length + 1 : window_length; + let window; + switch (name) { + case 'boxcar': + window = new Float64Array(length).fill(1.0); + break; + case 'hann': + case 'hann_window': + window = hanning(length); + break; + case 'hamming': + window = hamming(length); + break; + case 'povey': + window = hanning(length).map(x => Math.pow(x, 0.85)); + break; + default: + throw new Error(`Unknown window type ${name}.`); + } + if (periodic) { + window = window.subarray(0, window_length); + } + if (frame_length === null) { + return window; + } + if (window_length > frame_length) { + throw new Error(`Length of the window (${window_length}) may not be larger than frame_length (${frame_length})`); + } + + return window; +} + + +/***/ }), + +/***/ "./src/utils/constants.js": +/*!********************************!*\ + !*** ./src/utils/constants.js ***! + \********************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ GITHUB_ISSUE_URL: () => (/* binding */ GITHUB_ISSUE_URL) +/* harmony export */ }); + +const GITHUB_ISSUE_URL = 'https://github.com/huggingface/transformers.js/issues/new/choose'; + +/***/ }), + +/***/ "./src/utils/core.js": +/*!***************************!*\ + !*** ./src/utils/core.js ***! + \***************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ calculateDimensions: () => (/* binding */ calculateDimensions), +/* harmony export */ calculateReflectOffset: () => (/* binding */ calculateReflectOffset), +/* harmony export */ dispatchCallback: () => (/* binding */ dispatchCallback), +/* harmony export */ escapeRegExp: () => (/* binding */ escapeRegExp), +/* harmony export */ isIntegralNumber: () => (/* binding */ isIntegralNumber), +/* harmony export */ isTypedArray: () => (/* binding */ isTypedArray), +/* harmony export */ len: () => (/* binding */ len), +/* harmony export */ mergeArrays: () => (/* binding */ mergeArrays), +/* harmony export */ pick: () => (/* binding */ pick), +/* harmony export */ pop: () => (/* binding */ pop), +/* harmony export */ product: () => (/* binding */ product), +/* harmony export */ reverseDictionary: () => (/* binding */ reverseDictionary) +/* harmony export */ }); + +/** + * @file Core utility functions/classes for Transformers.js. + * + * These are only used internally, meaning an end-user shouldn't + * need to access anything here. + * + * @module utils/core + */ + +/** + * Helper function to dispatch progress callbacks. + * + * @param {Function} progress_callback The progress callback function to dispatch. + * @param {any} data The data to pass to the progress callback function. + * @returns {void} + * @private + */ +function dispatchCallback(progress_callback, data) { + if (progress_callback) progress_callback(data); +} + +/** + * Reverses the keys and values of an object. + * + * @param {Object} data The object to reverse. + * @returns {Object} The reversed object. + * @see https://ultimatecourses.com/blog/reverse-object-keys-and-values-in-javascript + */ +function reverseDictionary(data) { + // https://ultimatecourses.com/blog/reverse-object-keys-and-values-in-javascript + return Object.fromEntries(Object.entries(data).map(([key, value]) => [value, key])); +} + +/** + * Escapes regular expression special characters from a string by replacing them with their escaped counterparts. + * + * @param {string} string The string to escape. + * @returns {string} The escaped string. + */ +function escapeRegExp(string) { + return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string +} + +/** + * Check if a value is a typed array. + * @param {*} val The value to check. + * @returns {boolean} True if the value is a `TypedArray`, false otherwise. + * + * Adapted from https://stackoverflow.com/a/71091338/13989043 + */ +function isTypedArray(val) { + return val?.prototype?.__proto__?.constructor?.name === 'TypedArray'; +} + + +/** + * Check if a value is an integer. + * @param {*} x The value to check. + * @returns {boolean} True if the value is a string, false otherwise. + */ +function isIntegralNumber(x) { + return Number.isInteger(x) || typeof x === 'bigint' +} + +/** + * Calculates the dimensions of a nested array. + * + * @param {any[]} arr The nested array to calculate dimensions for. + * @returns {number[]} An array containing the dimensions of the input array. + */ +function calculateDimensions(arr) { + const dimensions = []; + let current = arr; + while (Array.isArray(current)) { + dimensions.push(current.length); + current = current[0]; + } + return dimensions; +} + +/** + * Replicate python's .pop() method for objects. + * @param {Object} obj The object to pop from. + * @param {string} key The key to pop. + * @param {*} defaultValue The default value to return if the key does not exist. + * @returns {*} The value of the popped key. + * @throws {Error} If the key does not exist and no default value is provided. + */ +function pop(obj, key, defaultValue = undefined) { + const value = obj[key]; + if (value !== undefined) { + delete obj[key]; + return value; + } + if (defaultValue === undefined) { + throw Error(`Key ${key} does not exist in object.`) + } + return defaultValue; +} + +/** + * Efficiently merge arrays, creating a new copy. + * Adapted from https://stackoverflow.com/a/6768642/13989043 + * @param {Array[]} arrs Arrays to merge. + * @returns {Array} The merged array. + */ +function mergeArrays(...arrs) { + return Array.prototype.concat.apply([], arrs); +} + +/** + * Compute the Cartesian product of given arrays + * @param {...Array} a Arrays to compute the product + * @returns {Array} Returns the computed Cartesian product as an array + * @private + */ +function product(...a) { + // Cartesian product of items + // Adapted from https://stackoverflow.com/a/43053803 + return a.reduce((a, b) => a.flatMap(d => b.map(e => [d, e]))); +} + +/** + * Calculates the index offset for a given index and window size. + * @param {number} i The index. + * @param {number} w The window size. + * @returns {number} The index offset. + */ +function calculateReflectOffset(i, w) { + return Math.abs((i + w) % (2 * w) - w); +} + +/** + * + * @param {Object} o + * @param {string[]} props + * @returns {Object} + */ +function pick(o, props) { + return Object.assign( + {}, + ...props.map((prop) => { + if (o[prop] !== undefined) { + return { [prop]: o[prop] }; + } + }) + ); +} + +/** + * Calculate the length of a string, taking multi-byte characters into account. + * This mimics the behavior of Python's `len` function. + * @param {string} s The string to calculate the length of. + * @returns {number} The length of the string. + */ +function len(s) { + let length = 0; + for (const c of s) ++length; + return length; +} + + +/***/ }), + +/***/ "./src/utils/data-structures.js": +/*!**************************************!*\ + !*** ./src/utils/data-structures.js ***! + \**************************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ CharTrie: () => (/* binding */ CharTrie), +/* harmony export */ PriorityQueue: () => (/* binding */ PriorityQueue), +/* harmony export */ TokenLattice: () => (/* binding */ TokenLattice) +/* harmony export */ }); + +/** + * @file Custom data structures. + * + * These are only used internally, meaning an end-user shouldn't + * need to access anything here. + * + * @module utils/data-structures + */ + + +/** + * Efficient Heap-based Implementation of a Priority Queue. + * It uses an array-based binary heap, where the root is at index `0`, and the + * children of node `i` are located at indices `2i + 1` and `2i + 2`, respectively. + * + * Adapted from the following sources: + * - https://stackoverflow.com/a/42919752/13989043 (original) + * - https://github.com/belladoreai/llama-tokenizer-js (minor improvements) + */ +class PriorityQueue { + + /** + * Create a new PriorityQueue. + * @param {function(any, any): boolean} comparator Comparator function to determine priority. Defaults to a MaxHeap. + */ + constructor(comparator = (a, b) => a > b, maxSize = Infinity) { + this._heap = []; + this._comparator = comparator; + this._maxSize = maxSize; + } + + /** + * The size of the queue + */ + get size() { + return this._heap.length; + } + + /** + * Check if the queue is empty. + * @returns {boolean} `true` if the queue is empty, `false` otherwise. + */ + isEmpty() { + return this.size === 0; + } + + /** + * Return the element with the highest priority in the queue. + * @returns {any} The highest priority element in the queue. + */ + peek() { + return this._heap[0]; + } + + /** + * Add one or more elements to the queue. + * @param {...any} values The values to push into the queue. + * @returns {number} The new size of the queue. + */ + push(...values) { + return this.extend(values); + } + + /** + * Add multiple elements to the queue. + * @param {any[]} values The values to push into the queue. + * @returns {number} The new size of the queue. + */ + extend(values) { + for (const value of values) { + if (this.size < this._maxSize) { + this._heap.push(value); + this._siftUp(); + } else { + // Get index of value with the lowest priority + const smallest = this._smallest(); + + // If the new value has higher priority than the smallest value in the heap + // then replace the smallest value with the new value and update the heap + if (this._comparator(value, this._heap[smallest])) { + this._heap[smallest] = value; + this._siftUpFrom(smallest); + } + } + } + return this.size; + } + + /** + * Remove and return the element with the highest priority in the queue. + * @returns {any} The element with the highest priority in the queue. + */ + pop() { + const poppedValue = this.peek(); + const bottom = this.size - 1; + if (bottom > 0) { + this._swap(0, bottom); + } + this._heap.pop(); + this._siftDown(); + return poppedValue; + } + + /** + * Replace the element with the highest priority in the queue with a new value. + * @param {*} value The new value. + * @returns {*} The replaced value. + */ + replace(value) { + const replacedValue = this.peek(); + this._heap[0] = value; + this._siftDown(); + return replacedValue; + } + + /** + * Compute the index for the parent of the node at index `i`. + * @param {number} i The index of the node to get the parent of. + * @returns {number} The index of the parent node. + * @private + */ + _parent(i) { + return ((i + 1) >>> 1) - 1; + } + + /** + * Compute the index for the left child of the node at index `i`. + * @param {number} i The index of the node to get the left child of. + * @returns {number} The index of the left child. + * @private + */ + _left(i) { + return (i << 1) + 1; + } + + /** + * Compute the index for the right child of the node at index `i`. + * @param {number} i The index of the node to get the right child of. + * @returns {number} The index of the right child. + * @private + */ + _right(i) { + return (i + 1) << 1; + } + + /** + * Check if the element at index `i` is greater than the element at index `j`. + * @param {number} i The index of the first element to compare. + * @param {number} j The index of the second element to compare. + * @returns {boolean} `true` if the element at index `i` is greater than the element at index `j`, `false` otherwise. + * @private + */ + _greater(i, j) { + return this._comparator(this._heap[i], this._heap[j]); + } + + /** + * Swap the elements at indices `i` and `j`. + * @param {number} i The index of the first element to swap. + * @param {number} j The index of the second element to swap. + * @private + */ + _swap(i, j) { + const temp = this._heap[i]; + this._heap[i] = this._heap[j]; + this._heap[j] = temp; + } + + /** + * Maintain the heap property by updating positions in the heap, + * starting at the last element and moving up the heap. + * @private + */ + _siftUp() { + this._siftUpFrom(this.size - 1); + } + + /** + * Helper function to sift up from a given node. + * @param {number} node The index of the node to start sifting up from. + */ + _siftUpFrom(node) { + while (node > 0 && this._greater(node, this._parent(node))) { + this._swap(node, this._parent(node)); + node = this._parent(node); + } + } + + /** + * Maintain the heap property by updating positions in the heap, + * starting at the first element and moving down the heap. + * @private + */ + _siftDown() { + let node = 0; + while ( + (this._left(node) < this.size && this._greater(this._left(node), node)) || + (this._right(node) < this.size && this._greater(this._right(node), node)) + ) { + const maxChild = (this._right(node) < this.size && this._greater(this._right(node), this._left(node))) + ? this._right(node) + : this._left(node); + this._swap(node, maxChild); + node = maxChild; + } + } + + /** + * Get the index of the smallest element in the heap. Since we use an array-based heap, + * the index can be computed without needing to traverse the heap. + * @private + */ + _smallest() { + return (2 ** (Math.floor(Math.log2(this.size))) - 1); + } +} + +/** + * A trie structure to efficiently store and search for strings. + */ +class CharTrie { + constructor() { + this.root = CharTrieNode.default(); + } + + /** + * Adds one or more `texts` to the trie. + * @param {string[]} texts The strings to add to the trie. + */ + extend(texts) { + for (const text of texts) { + this.push(text); + } + } + + /** + * Adds text to the trie. + * @param {string} text The string to add to the trie. + */ + push(text) { + let node = this.root; + for (const ch of text) { + let child = node.children.get(ch); + if (child === undefined) { + child = CharTrieNode.default(); + node.children.set(ch, child); + } + node = child; + } + node.isLeaf = true; + } + + /** + * Searches the trie for all strings with a common prefix of `text`. + * @param {string} text The common prefix to search for. + * @yields {string} Each string in the trie that has `text` as a prefix. + */ + *commonPrefixSearch(text) { + let node = this.root; + if (node === undefined) return; + + let prefix = ""; + for (const ch of text) { + prefix += ch; + node = node.children.get(ch); + if (node === undefined) return; + if (node.isLeaf) { + yield prefix; + } + } + } +} + +/** + * Represents a node in a character trie. + */ +class CharTrieNode { + /** + * Create a new CharTrieNode. + * @param {boolean} isLeaf Whether the node is a leaf node or not. + * @param {Map} children A map containing the node's children, where the key is a character and the value is a `CharTrieNode`. + */ + constructor(isLeaf, children) { + this.isLeaf = isLeaf; + this.children = children; + } + + /** + * Returns a new `CharTrieNode` instance with default values. + * @returns {CharTrieNode} A new `CharTrieNode` instance with `isLeaf` set to `false` and an empty `children` map. + */ + static default() { + return new CharTrieNode(false, new Map()); + } +} + +/** + * A lattice data structure to be used for tokenization. + */ +class TokenLattice { + /** + * Creates a new TokenLattice instance. + * + * @param {string} sentence The input sentence to be tokenized. + * @param {number} bosTokenId The beginning-of-sequence token ID. + * @param {number} eosTokenId The end-of-sequence token ID. + */ + constructor(sentence, bosTokenId, eosTokenId) { + this.chars = Array.from(sentence); + this.len = this.chars.length; + this.bosTokenId = bosTokenId; + this.eosTokenId = eosTokenId; + this.nodes = []; + this.beginNodes = Array.from({ length: this.len + 1 }, () => []); + this.endNodes = Array.from({ length: this.len + 1 }, () => []); + + const bos = new TokenLatticeNode(this.bosTokenId, 0, 0, 0, 0.0); + const eos = new TokenLatticeNode(this.eosTokenId, 1, this.len, 0, 0.0); + this.nodes.push(bos.clone()); + this.nodes.push(eos.clone()); + this.beginNodes[this.len].push(eos); + this.endNodes[0].push(bos); + } + + /** + * Inserts a new token node into the token lattice. + * + * @param {number} pos The starting position of the token. + * @param {number} length The length of the token. + * @param {number} score The score of the token. + * @param {number} tokenId The token ID of the token. + */ + insert(pos, length, score, tokenId) { + const nodeId = this.nodes.length; + const node = new TokenLatticeNode(tokenId, nodeId, pos, length, score); + this.beginNodes[pos].push(node); + this.endNodes[pos + length].push(node); + this.nodes.push(node); + } + + /** + * Implements the Viterbi algorithm to compute the most likely sequence of tokens. + * + * @returns {TokenLatticeNode[]} The most likely sequence of tokens. + */ + viterbi() { + const len = this.len; + let pos = 0; + while (pos <= len) { + if (this.beginNodes[pos].length == 0) { + return []; + } + for (let rnode of this.beginNodes[pos]) { + rnode.prev = null; + let bestScore = 0.0; + let bestNode = null; + for (let lnode of this.endNodes[pos]) { + const score = lnode.backtraceScore + rnode.score; + if (bestNode === null || score > bestScore) { + bestNode = lnode.clone(); + bestScore = score; + } + } + + if (bestNode !== null) { + rnode.prev = bestNode; + rnode.backtraceScore = bestScore; + } else { + return []; + } + } + ++pos; + } + + const results = []; + const root = this.beginNodes[len][0]; + const prev = root.prev; + if (prev === null) { + return []; + } + + let node = prev.clone(); + while (node.prev !== null) { + results.push(node.clone()); + const n = node.clone(); + node = n.prev.clone(); + } + + results.reverse(); + return results; + } + + /** + * @param {TokenLatticeNode} node + * @returns {string} The array of nodes representing the most likely sequence of tokens. + */ + piece(node) { + return this.chars.slice(node.pos, node.pos + node.length).join(''); + } + + /** + * @returns {string[]} The most likely sequence of tokens. + */ + tokens() { + const nodes = this.viterbi(); + return nodes.map(x => this.piece(x)); + } + + /** + * @returns {number[]} The most likely sequence of token ids. + */ + tokenIds() { + const nodes = this.viterbi(); + return nodes.map(x => x.tokenId); + } +} +class TokenLatticeNode { + /** + * Represents a node in a token lattice for a given sentence. + * @param {number} tokenId The ID of the token associated with this node. + * @param {number} nodeId The ID of this node. + * @param {number} pos The starting position of the token in the sentence. + * @param {number} length The length of the token. + * @param {number} score The score associated with the token. + */ + constructor(tokenId, nodeId, pos, length, score) { + this.tokenId = tokenId; + this.nodeId = nodeId; + this.pos = pos; + this.length = length; + this.score = score; + this.prev = null; + this.backtraceScore = 0.0; + } + + /** + * Returns a clone of this node. + * @returns {TokenLatticeNode} A clone of this node. + */ + clone() { + const n = new TokenLatticeNode(this.tokenId, this.nodeId, this.pos, this.length, this.score); + n.prev = this.prev; + n.backtraceScore = this.backtraceScore; + return n; + } +} + + +/***/ }), + +/***/ "./src/utils/devices.js": +/*!******************************!*\ + !*** ./src/utils/devices.js ***! + \******************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ DEVICE_TYPES: () => (/* binding */ DEVICE_TYPES) +/* harmony export */ }); + +/** + * The list of devices supported by Transformers.js + */ +const DEVICE_TYPES = Object.freeze({ + auto: 'auto', // Auto-detect based on device and environment + gpu: 'gpu', // Auto-detect GPU + cpu: 'cpu', // CPU + wasm: 'wasm', // WebAssembly + webgpu: 'webgpu', // WebGPU + cuda: 'cuda', // CUDA + dml: 'dml', // DirectML + + webnn: 'webnn', // WebNN (default) + 'webnn-npu': 'webnn-npu', // WebNN NPU + 'webnn-gpu': 'webnn-gpu', // WebNN GPU + 'webnn-cpu': 'webnn-cpu', // WebNN CPU +}); + +/** + * @typedef {keyof typeof DEVICE_TYPES} DeviceType + */ + + +/***/ }), + +/***/ "./src/utils/dtypes.js": +/*!*****************************!*\ + !*** ./src/utils/dtypes.js ***! + \*****************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ DATA_TYPES: () => (/* binding */ DATA_TYPES), +/* harmony export */ DEFAULT_DEVICE_DTYPE_MAPPING: () => (/* binding */ DEFAULT_DEVICE_DTYPE_MAPPING), +/* harmony export */ DEFAULT_DTYPE_SUFFIX_MAPPING: () => (/* binding */ DEFAULT_DTYPE_SUFFIX_MAPPING), +/* harmony export */ isWebGpuFp16Supported: () => (/* binding */ isWebGpuFp16Supported) +/* harmony export */ }); +/* harmony import */ var _env_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../env.js */ "./src/env.js"); +/* harmony import */ var _devices_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./devices.js */ "./src/utils/devices.js"); + + + + +// TODO: Use the adapter from `env.backends.onnx.webgpu.adapter` to check for `shader-f16` support, +// when available in https://github.com/microsoft/onnxruntime/pull/19940. +// For more information, see https://github.com/microsoft/onnxruntime/pull/19857#issuecomment-1999984753 + +/** + * Checks if WebGPU fp16 support is available in the current environment. + */ +const isWebGpuFp16Supported = (function () { + /** @type {boolean} */ + let cachedResult; + + return async function () { + if (cachedResult === undefined) { + if (!_env_js__WEBPACK_IMPORTED_MODULE_0__.apis.IS_WEBGPU_AVAILABLE) { + cachedResult = false; + } else { + try { + const adapter = await navigator.gpu.requestAdapter(); + cachedResult = adapter.features.has('shader-f16'); + } catch (e) { + cachedResult = false; + } + } + } + return cachedResult; + }; +})(); + +const DATA_TYPES = Object.freeze({ + fp32: 'fp32', + fp16: 'fp16', + q8: 'q8', + int8: 'int8', + uint8: 'uint8', + q4: 'q4', + bnb4: 'bnb4', + q4f16: 'q4f16', // fp16 model with int4 block weight quantization +}); +/** @typedef {keyof typeof DATA_TYPES} DataType */ + +const DEFAULT_DEVICE_DTYPE_MAPPING = Object.freeze({ + // NOTE: If not specified, will default to fp32 + [_devices_js__WEBPACK_IMPORTED_MODULE_1__.DEVICE_TYPES.wasm]: DATA_TYPES.q8, +}); + +/** @type {Record} */ +const DEFAULT_DTYPE_SUFFIX_MAPPING = Object.freeze({ + [DATA_TYPES.fp32]: '', + [DATA_TYPES.fp16]: '_fp16', + [DATA_TYPES.int8]: '_int8', + [DATA_TYPES.uint8]: '_uint8', + [DATA_TYPES.q8]: '_quantized', + [DATA_TYPES.q4]: '_q4', + [DATA_TYPES.q4f16]: '_q4f16', + [DATA_TYPES.bnb4]: '_bnb4', +}); + + +/***/ }), + +/***/ "./src/utils/generic.js": +/*!******************************!*\ + !*** ./src/utils/generic.js ***! + \******************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ Callable: () => (/* binding */ Callable) +/* harmony export */ }); + +/** + * A base class for creating callable objects. + * See [here](https://stackoverflow.com/q/76073890) for more information. + * + * @type {new () => {(...args: any[]): any, _call(...args: any[]): any}} + */ +const Callable = /** @type {any} */ (class { + /** + * Creates a new instance of the Callable class. + */ + constructor() { + /** + * Creates a closure that delegates to a private method '_call' with the given arguments. + * @type {any} + * @param {...any} args Zero or more arguments to pass to the '_call' method. + * @returns {*} The result of calling the '_call' method. + */ + let closure = function (...args) { + return closure._call(...args) + } + return Object.setPrototypeOf(closure, new.target.prototype) + } + + /** + * This method should be implemented in subclasses to provide the + * functionality of the callable object. + * + * @param {any[]} args + * @throws {Error} If the subclass does not implement the `_call` method. + */ + _call(...args) { + throw Error('Must implement _call method in subclass') + } +}); + + +/***/ }), + +/***/ "./src/utils/hub.js": +/*!**************************!*\ + !*** ./src/utils/hub.js ***! + \**************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ getFile: () => (/* binding */ getFile), +/* harmony export */ getModelFile: () => (/* binding */ getModelFile), +/* harmony export */ getModelJSON: () => (/* binding */ getModelJSON) +/* harmony export */ }); +/* harmony import */ var fs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! fs */ "fs"); +/* harmony import */ var path__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! path */ "path"); +/* harmony import */ var _env_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../env.js */ "./src/env.js"); +/* harmony import */ var _core_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./core.js */ "./src/utils/core.js"); + +/** + * @file Utility functions to interact with the Hugging Face Hub (https://huggingface.co/models) + * + * @module utils/hub + */ + + + + + + + +/** + * @typedef {Object} PretrainedOptions Options for loading a pretrained model. + * @property {function} [progress_callback=null] If specified, this function will be called during model construction, to provide the user with progress updates. + * @property {import('../configs.js').PretrainedConfig} [config=null] Configuration for the model to use instead of an automatically loaded configuration. Configuration can be automatically loaded when: + * - The model is a model provided by the library (loaded with the *model id* string of a pretrained model). + * - The model is loaded by supplying a local directory as `pretrained_model_name_or_path` and a configuration JSON file named *config.json* is found in the directory. + * @property {string} [cache_dir=null] Path to a directory in which a downloaded pretrained model configuration should be cached if the standard cache should not be used. + * @property {boolean} [local_files_only=false] Whether or not to only look at local files (e.g., not try downloading the model). + * @property {string} [revision='main'] The specific model version to use. It can be a branch name, a tag name, or a commit id, + * since we use a git-based system for storing models and other artifacts on huggingface.co, so `revision` can be any identifier allowed by git. + * NOTE: This setting is ignored for local requests. + */ + +/** + * @typedef {Object} ModelSpecificPretrainedOptions Options for loading a pretrained model. + * @property {string} [subfolder='onnx'] In case the relevant files are located inside a subfolder of the model repo on huggingface.co, + * you can specify the folder name here. + * @property {string} [model_file_name=null] If specified, load the model with this name (excluding the .onnx suffix). Currently only valid for encoder- or decoder-only models. + * @property {import("./devices.js").DeviceType|Record} [device=null] The device to run the model on. If not specified, the device will be chosen from the environment settings. + * @property {import("./dtypes.js").DataType|Record} [dtype=null] The data type to use for the model. If not specified, the data type will be chosen from the environment settings. + * @property {boolean|Record} [use_external_data_format=false] Whether to load the model using the external data format (used for models >= 2GB in size). + * @property {import('onnxruntime-common').InferenceSession.SessionOptions} [session_options] (Optional) User-specified session options passed to the runtime. If not provided, suitable defaults will be chosen. + */ + +/** + * @typedef {PretrainedOptions & ModelSpecificPretrainedOptions} PretrainedModelOptions Options for loading a pretrained model. + */ + +/** + * Mapping from file extensions to MIME types. + */ +const CONTENT_TYPE_MAP = { + 'txt': 'text/plain', + 'html': 'text/html', + 'css': 'text/css', + 'js': 'text/javascript', + 'json': 'application/json', + 'png': 'image/png', + 'jpg': 'image/jpeg', + 'jpeg': 'image/jpeg', + 'gif': 'image/gif', +} +class FileResponse { + + /** + * Creates a new `FileResponse` object. + * @param {string|URL} filePath + */ + constructor(filePath) { + this.filePath = filePath; + this.headers = new Headers(); + + this.exists = fs__WEBPACK_IMPORTED_MODULE_0__["default"].existsSync(filePath); + if (this.exists) { + this.status = 200; + this.statusText = 'OK'; + + let stats = fs__WEBPACK_IMPORTED_MODULE_0__["default"].statSync(filePath); + this.headers.set('content-length', stats.size.toString()); + + this.updateContentType(); + + let self = this; + this.body = new ReadableStream({ + start(controller) { + self.arrayBuffer().then(buffer => { + controller.enqueue(new Uint8Array(buffer)); + controller.close(); + }) + } + }); + } else { + this.status = 404; + this.statusText = 'Not Found'; + this.body = null; + } + } + + /** + * Updates the 'content-type' header property of the response based on the extension of + * the file specified by the filePath property of the current object. + * @returns {void} + */ + updateContentType() { + // Set content-type header based on file extension + const extension = this.filePath.toString().split('.').pop().toLowerCase(); + this.headers.set('content-type', CONTENT_TYPE_MAP[extension] ?? 'application/octet-stream'); + } + + /** + * Clone the current FileResponse object. + * @returns {FileResponse} A new FileResponse object with the same properties as the current object. + */ + clone() { + let response = new FileResponse(this.filePath); + response.exists = this.exists; + response.status = this.status; + response.statusText = this.statusText; + response.headers = new Headers(this.headers); + return response; + } + + /** + * Reads the contents of the file specified by the filePath property and returns a Promise that + * resolves with an ArrayBuffer containing the file's contents. + * @returns {Promise} A Promise that resolves with an ArrayBuffer containing the file's contents. + * @throws {Error} If the file cannot be read. + */ + async arrayBuffer() { + const data = await fs__WEBPACK_IMPORTED_MODULE_0__["default"].promises.readFile(this.filePath); + return data.buffer; + } + + /** + * Reads the contents of the file specified by the filePath property and returns a Promise that + * resolves with a Blob containing the file's contents. + * @returns {Promise} A Promise that resolves with a Blob containing the file's contents. + * @throws {Error} If the file cannot be read. + */ + async blob() { + const data = await fs__WEBPACK_IMPORTED_MODULE_0__["default"].promises.readFile(this.filePath); + return new Blob([data], { type: this.headers.get('content-type') }); + } + + /** + * Reads the contents of the file specified by the filePath property and returns a Promise that + * resolves with a string containing the file's contents. + * @returns {Promise} A Promise that resolves with a string containing the file's contents. + * @throws {Error} If the file cannot be read. + */ + async text() { + const data = await fs__WEBPACK_IMPORTED_MODULE_0__["default"].promises.readFile(this.filePath, 'utf8'); + return data; + } + + /** + * Reads the contents of the file specified by the filePath property and returns a Promise that + * resolves with a parsed JavaScript object containing the file's contents. + * + * @returns {Promise} A Promise that resolves with a parsed JavaScript object containing the file's contents. + * @throws {Error} If the file cannot be read. + */ + async json() { + return JSON.parse(await this.text()); + } +} + +/** + * Determines whether the given string is a valid URL. + * @param {string|URL} string The string to test for validity as an URL. + * @param {string[]} [protocols=null] A list of valid protocols. If specified, the protocol must be in this list. + * @param {string[]} [validHosts=null] A list of valid hostnames. If specified, the URL's hostname must be in this list. + * @returns {boolean} True if the string is a valid URL, false otherwise. + */ +function isValidUrl(string, protocols = null, validHosts = null) { + let url; + try { + url = new URL(string); + } catch (_) { + return false; + } + if (protocols && !protocols.includes(url.protocol)) { + return false; + } + if (validHosts && !validHosts.includes(url.hostname)) { + return false; + } + return true; +} + +/** + * Helper function to get a file, using either the Fetch API or FileSystem API. + * + * @param {URL|string} urlOrPath The URL/path of the file to get. + * @returns {Promise} A promise that resolves to a FileResponse object (if the file is retrieved using the FileSystem API), or a Response object (if the file is retrieved using the Fetch API). + */ +async function getFile(urlOrPath) { + + if (_env_js__WEBPACK_IMPORTED_MODULE_2__.env.useFS && !isValidUrl(urlOrPath, ['http:', 'https:', 'blob:'])) { + return new FileResponse(urlOrPath); + + } else if (typeof process !== 'undefined' && process?.release?.name === 'node') { + const IS_CI = !!process.env?.TESTING_REMOTELY; + const version = _env_js__WEBPACK_IMPORTED_MODULE_2__.env.version; + + const headers = new Headers(); + headers.set('User-Agent', `transformers.js/${version}; is_ci/${IS_CI};`); + + // Check whether we are making a request to the Hugging Face Hub. + const isHFURL = isValidUrl(urlOrPath, ['http:', 'https:'], ['huggingface.co', 'hf.co']); + if (isHFURL) { + // If an access token is present in the environment variables, + // we add it to the request headers. + // NOTE: We keep `HF_ACCESS_TOKEN` for backwards compatibility (as a fallback). + const token = process.env?.HF_TOKEN ?? process.env?.HF_ACCESS_TOKEN; + if (token) { + headers.set('Authorization', `Bearer ${token}`); + } + } + return fetch(urlOrPath, { headers }); + } else { + // Running in a browser-environment, so we use default headers + // NOTE: We do not allow passing authorization headers in the browser, + // since this would require exposing the token to the client. + return fetch(urlOrPath); + } +} + +const ERROR_MAPPING = { + // 4xx errors (https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#client_error_responses) + 400: 'Bad request error occurred while trying to load file', + 401: 'Unauthorized access to file', + 403: 'Forbidden access to file', + 404: 'Could not locate file', + 408: 'Request timeout error occurred while trying to load file', + + // 5xx errors (https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#server_error_responses) + 500: 'Internal server error error occurred while trying to load file', + 502: 'Bad gateway error occurred while trying to load file', + 503: 'Service unavailable error occurred while trying to load file', + 504: 'Gateway timeout error occurred while trying to load file', +} +/** + * Helper method to handle fatal errors that occur while trying to load a file from the Hugging Face Hub. + * @param {number} status The HTTP status code of the error. + * @param {string} remoteURL The URL of the file that could not be loaded. + * @param {boolean} fatal Whether to raise an error if the file could not be loaded. + * @returns {null} Returns `null` if `fatal = true`. + * @throws {Error} If `fatal = false`. + */ +function handleError(status, remoteURL, fatal) { + if (!fatal) { + // File was not loaded correctly, but it is optional. + // TODO in future, cache the response? + return null; + } + + const message = ERROR_MAPPING[status] ?? `Error (${status}) occurred while trying to load file`; + throw Error(`${message}: "${remoteURL}".`); +} + +class FileCache { + /** + * Instantiate a `FileCache` object. + * @param {string} path + */ + constructor(path) { + this.path = path; + } + + /** + * Checks whether the given request is in the cache. + * @param {string} request + * @returns {Promise} + */ + async match(request) { + + let filePath = path__WEBPACK_IMPORTED_MODULE_1__["default"].join(this.path, request); + let file = new FileResponse(filePath); + + if (file.exists) { + return file; + } else { + return undefined; + } + } + + /** + * Adds the given response to the cache. + * @param {string} request + * @param {Response|FileResponse} response + * @returns {Promise} + */ + async put(request, response) { + const buffer = Buffer.from(await response.arrayBuffer()); + + let outputPath = path__WEBPACK_IMPORTED_MODULE_1__["default"].join(this.path, request); + + try { + await fs__WEBPACK_IMPORTED_MODULE_0__["default"].promises.mkdir(path__WEBPACK_IMPORTED_MODULE_1__["default"].dirname(outputPath), { recursive: true }); + await fs__WEBPACK_IMPORTED_MODULE_0__["default"].promises.writeFile(outputPath, buffer); + + } catch (err) { + console.warn('An error occurred while writing the file to cache:', err) + } + } + + // TODO add the rest? + // addAll(requests: RequestInfo[]): Promise; + // delete(request: RequestInfo | URL, options?: CacheQueryOptions): Promise; + // keys(request?: RequestInfo | URL, options?: CacheQueryOptions): Promise>; + // match(request: RequestInfo | URL, options?: CacheQueryOptions): Promise; + // matchAll(request?: RequestInfo | URL, options?: CacheQueryOptions): Promise>; +} + +/** + * + * @param {FileCache|Cache} cache The cache to search + * @param {string[]} names The names of the item to search for + * @returns {Promise} The item from the cache, or undefined if not found. + */ +async function tryCache(cache, ...names) { + for (let name of names) { + try { + let result = await cache.match(name); + if (result) return result; + } catch (e) { + continue; + } + } + return undefined; +} + +/** + * + * Retrieves a file from either a remote URL using the Fetch API or from the local file system using the FileSystem API. + * If the filesystem is available and `env.useCache = true`, the file will be downloaded and cached. + * + * @param {string} path_or_repo_id This can be either: + * - a string, the *model id* of a model repo on huggingface.co. + * - a path to a *directory* potentially containing the file. + * @param {string} filename The name of the file to locate in `path_or_repo`. + * @param {boolean} [fatal=true] Whether to throw an error if the file is not found. + * @param {PretrainedOptions} [options] An object containing optional parameters. + * + * @throws Will throw an error if the file is not found and `fatal` is true. + * @returns {Promise} A Promise that resolves with the file content as a buffer. + */ +async function getModelFile(path_or_repo_id, filename, fatal = true, options = {}) { + + if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.allowLocalModels) { + // User has disabled local models, so we just make sure other settings are correct. + + if (options.local_files_only) { + throw Error("Invalid configuration detected: local models are disabled (`env.allowLocalModels=false`) but you have requested to only use local models (`local_files_only=true`).") + } else if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.allowRemoteModels) { + throw Error("Invalid configuration detected: both local and remote models are disabled. Fix by setting `env.allowLocalModels` or `env.allowRemoteModels` to `true`.") + } + } + + // Initiate file retrieval + (0,_core_js__WEBPACK_IMPORTED_MODULE_3__.dispatchCallback)(options.progress_callback, { + status: 'initiate', + name: path_or_repo_id, + file: filename + }) + + // First, check if the a caching backend is available + // If no caching mechanism available, will download the file every time + let cache; + if (!cache && _env_js__WEBPACK_IMPORTED_MODULE_2__.env.useBrowserCache) { + if (typeof caches === 'undefined') { + throw Error('Browser cache is not available in this environment.') + } + try { + // In some cases, the browser cache may be visible, but not accessible due to security restrictions. + // For example, when running an application in an iframe, if a user attempts to load the page in + // incognito mode, the following error is thrown: `DOMException: Failed to execute 'open' on 'CacheStorage': + // An attempt was made to break through the security policy of the user agent.` + // So, instead of crashing, we just ignore the error and continue without using the cache. + cache = await caches.open('transformers-cache'); + } catch (e) { + console.warn('An error occurred while opening the browser cache:', e); + } + } + + if (!cache && _env_js__WEBPACK_IMPORTED_MODULE_2__.env.useFSCache) { + // TODO throw error if not available + + // If `cache_dir` is not specified, use the default cache directory + cache = new FileCache(options.cache_dir ?? _env_js__WEBPACK_IMPORTED_MODULE_2__.env.cacheDir); + } + + if (!cache && _env_js__WEBPACK_IMPORTED_MODULE_2__.env.useCustomCache) { + // Allow the user to specify a custom cache system. + if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache) { + throw Error('`env.useCustomCache=true`, but `env.customCache` is not defined.') + } + + // Check that the required methods are defined: + if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache.match || !_env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache.put) { + throw new Error( + "`env.customCache` must be an object which implements the `match` and `put` functions of the Web Cache API. " + + "For more information, see https://developer.mozilla.org/en-US/docs/Web/API/Cache" + ) + } + cache = _env_js__WEBPACK_IMPORTED_MODULE_2__.env.customCache; + } + + const revision = options.revision ?? 'main'; + + let requestURL = pathJoin(path_or_repo_id, filename); + let localPath = pathJoin(_env_js__WEBPACK_IMPORTED_MODULE_2__.env.localModelPath, requestURL); + + let remoteURL = pathJoin( + _env_js__WEBPACK_IMPORTED_MODULE_2__.env.remoteHost, + _env_js__WEBPACK_IMPORTED_MODULE_2__.env.remotePathTemplate + .replaceAll('{model}', path_or_repo_id) + .replaceAll('{revision}', encodeURIComponent(revision)), + filename + ); + + // Choose cache key for filesystem cache + // When using the main revision (default), we use the request URL as the cache key. + // If a specific revision is requested, we account for this in the cache key. + let fsCacheKey = revision === 'main' ? requestURL : pathJoin(path_or_repo_id, revision, filename); + + /** @type {string} */ + let cacheKey; + let proposedCacheKey = cache instanceof FileCache ? fsCacheKey : remoteURL; + + // Whether to cache the final response in the end. + let toCacheResponse = false; + + /** @type {Response|FileResponse|undefined} */ + let response; + + if (cache) { + // A caching system is available, so we try to get the file from it. + // 1. We first try to get from cache using the local path. In some environments (like deno), + // non-URL cache keys are not allowed. In these cases, `response` will be undefined. + // 2. If no response is found, we try to get from cache using the remote URL or file system cache. + response = await tryCache(cache, localPath, proposedCacheKey); + } + + const cacheHit = response !== undefined; + + if (response === undefined) { + // Caching not available, or file is not cached, so we perform the request + + if (_env_js__WEBPACK_IMPORTED_MODULE_2__.env.allowLocalModels) { + // Accessing local models is enabled, so we try to get the file locally. + // If request is a valid HTTP URL, we skip the local file check. Otherwise, we try to get the file locally. + const isURL = isValidUrl(requestURL, ['http:', 'https:']); + if (!isURL) { + try { + response = await getFile(localPath); + cacheKey = localPath; // Update the cache key to be the local path + } catch (e) { + // Something went wrong while trying to get the file locally. + // NOTE: error handling is done in the next step (since `response` will be undefined) + console.warn(`Unable to load from local path "${localPath}": "${e}"`); + } + } else if (options.local_files_only) { + throw new Error(`\`local_files_only=true\`, but attempted to load a remote file from: ${requestURL}.`); + } else if (!_env_js__WEBPACK_IMPORTED_MODULE_2__.env.allowRemoteModels) { + throw new Error(`\`env.allowRemoteModels=false\`, but attempted to load a remote file from: ${requestURL}.`); + } + } + + if (response === undefined || response.status === 404) { + // File not found locally. This means either: + // - The user has disabled local file access (`env.allowLocalModels=false`) + // - the path is a valid HTTP url (`response === undefined`) + // - the path is not a valid HTTP url and the file is not present on the file system or local server (`response.status === 404`) + + if (options.local_files_only || !_env_js__WEBPACK_IMPORTED_MODULE_2__.env.allowRemoteModels) { + // User requested local files only, but the file is not found locally. + if (fatal) { + throw Error(`\`local_files_only=true\` or \`env.allowRemoteModels=false\` and file was not found locally at "${localPath}".`); + } else { + // File not found, but this file is optional. + // TODO in future, cache the response? + return null; + } + } + + // File not found locally, so we try to download it from the remote server + response = await getFile(remoteURL); + + if (response.status !== 200) { + return handleError(response.status, remoteURL, fatal); + } + + // Success! We use the proposed cache key from earlier + cacheKey = proposedCacheKey; + } + + // Only cache the response if: + toCacheResponse = + cache // 1. A caching system is available + && typeof Response !== 'undefined' // 2. `Response` is defined (i.e., we are in a browser-like environment) + && response instanceof Response // 3. result is a `Response` object (i.e., not a `FileResponse`) + && response.status === 200 // 4. request was successful (status code 200) + } + + // Start downloading + (0,_core_js__WEBPACK_IMPORTED_MODULE_3__.dispatchCallback)(options.progress_callback, { + status: 'download', + name: path_or_repo_id, + file: filename + }) + + const progressInfo = { + status: 'progress', + name: path_or_repo_id, + file: filename + } + + /** @type {Uint8Array} */ + let buffer; + + if (!options.progress_callback) { + // If no progress callback is specified, we can use the `.arrayBuffer()` + // method to read the response. + buffer = new Uint8Array(await response.arrayBuffer()); + + } else if ( + cacheHit // The item is being read from the cache + && + typeof navigator !== 'undefined' && /firefox/i.test(navigator.userAgent) // We are in Firefox + ) { + // Due to bug in Firefox, we cannot display progress when loading from cache. + // Fortunately, since this should be instantaneous, this should not impact users too much. + buffer = new Uint8Array(await response.arrayBuffer()); + + // For completeness, we still fire the final progress callback + (0,_core_js__WEBPACK_IMPORTED_MODULE_3__.dispatchCallback)(options.progress_callback, { + ...progressInfo, + progress: 100, + loaded: buffer.length, + total: buffer.length, + }) + } else { + buffer = await readResponse(response, data => { + (0,_core_js__WEBPACK_IMPORTED_MODULE_3__.dispatchCallback)(options.progress_callback, { + ...progressInfo, + ...data, + }) + }) + } + + if ( + // Only cache web responses + // i.e., do not cache FileResponses (prevents duplication) + toCacheResponse && cacheKey + && + // Check again whether request is in cache. If not, we add the response to the cache + (await cache.match(cacheKey) === undefined) + ) { + // NOTE: We use `new Response(buffer, ...)` instead of `response.clone()` to handle LFS files + await cache.put(cacheKey, new Response(buffer, { + headers: response.headers + })) + .catch(err => { + // Do not crash if unable to add to cache (e.g., QuotaExceededError). + // Rather, log a warning and proceed with execution. + console.warn(`Unable to add response to browser cache: ${err}.`); + }); + + } + + (0,_core_js__WEBPACK_IMPORTED_MODULE_3__.dispatchCallback)(options.progress_callback, { + status: 'done', + name: path_or_repo_id, + file: filename + }); + + return buffer; +} + +/** + * Fetches a JSON file from a given path and file name. + * + * @param {string} modelPath The path to the directory containing the file. + * @param {string} fileName The name of the file to fetch. + * @param {boolean} [fatal=true] Whether to throw an error if the file is not found. + * @param {PretrainedOptions} [options] An object containing optional parameters. + * @returns {Promise} The JSON data parsed into a JavaScript object. + * @throws Will throw an error if the file is not found and `fatal` is true. + */ +async function getModelJSON(modelPath, fileName, fatal = true, options = {}) { + let buffer = await getModelFile(modelPath, fileName, fatal, options); + if (buffer === null) { + // Return empty object + return {} + } + + let decoder = new TextDecoder('utf-8'); + let jsonData = decoder.decode(buffer); + + return JSON.parse(jsonData); +} + +/** + * Read and track progress when reading a Response object + * + * @param {any} response The Response object to read + * @param {function} progress_callback The function to call with progress updates + * @returns {Promise} A Promise that resolves with the Uint8Array buffer + */ +async function readResponse(response, progress_callback) { + + const contentLength = response.headers.get('Content-Length'); + if (contentLength === null) { + console.warn('Unable to determine content-length from response headers. Will expand buffer when needed.') + } + let total = parseInt(contentLength ?? '0'); + let buffer = new Uint8Array(total); + let loaded = 0; + + const reader = response.body.getReader(); + async function read() { + const { done, value } = await reader.read(); + if (done) return; + + let newLoaded = loaded + value.length; + if (newLoaded > total) { + total = newLoaded; + + // Adding the new data will overflow buffer. + // In this case, we extend the buffer + let newBuffer = new Uint8Array(total); + + // copy contents + newBuffer.set(buffer); + + buffer = newBuffer; + } + buffer.set(value, loaded) + loaded = newLoaded; + + const progress = (loaded / total) * 100; + + // Call your function here + progress_callback({ + progress: progress, + loaded: loaded, + total: total, + }) + + return read(); + } + + // Actually read + await read(); + + return buffer; +} + +/** + * Joins multiple parts of a path into a single path, while handling leading and trailing slashes. + * + * @param {...string} parts Multiple parts of a path. + * @returns {string} A string representing the joined path. + */ +function pathJoin(...parts) { + // https://stackoverflow.com/a/55142565 + parts = parts.map((part, index) => { + if (index) { + part = part.replace(new RegExp('^/'), ''); + } + if (index !== parts.length - 1) { + part = part.replace(new RegExp('/$'), ''); + } + return part; + }) + return parts.join('/'); +} + + +/***/ }), + +/***/ "./src/utils/image.js": +/*!****************************!*\ + !*** ./src/utils/image.js ***! + \****************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ RawImage: () => (/* binding */ RawImage) +/* harmony export */ }); +/* harmony import */ var _hub_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./hub.js */ "./src/utils/hub.js"); +/* harmony import */ var _env_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../env.js */ "./src/env.js"); +/* harmony import */ var _tensor_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./tensor.js */ "./src/utils/tensor.js"); +/* harmony import */ var sharp__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! sharp */ "sharp"); + +/** + * @file Helper module for image processing. + * + * These functions and classes are only used internally, + * meaning an end-user shouldn't need to access anything here. + * + * @module utils/image + */ + + + + + +// Will be empty (or not used) if running in browser or web-worker + + +const BROWSER_ENV = typeof self !== 'undefined'; +const WEBWORKER_ENV = BROWSER_ENV && self.constructor.name === 'DedicatedWorkerGlobalScope'; + +let createCanvasFunction; +let ImageDataClass; +let loadImageFunction; +if (BROWSER_ENV) { + // Running in browser or web-worker + createCanvasFunction = (/** @type {number} */ width, /** @type {number} */ height) => { + if (!self.OffscreenCanvas) { + throw new Error('OffscreenCanvas not supported by this browser.'); + } + return new self.OffscreenCanvas(width, height) + }; + loadImageFunction = self.createImageBitmap; + ImageDataClass = self.ImageData; + +} else if (sharp__WEBPACK_IMPORTED_MODULE_3__["default"]) { + // Running in Node.js, electron, or other non-browser environment + + loadImageFunction = async (/**@type {sharp.Sharp}*/img) => { + const metadata = await img.metadata(); + const rawChannels = metadata.channels; + + const { data, info } = await img.rotate().raw().toBuffer({ resolveWithObject: true }); + + const newImage = new RawImage(new Uint8ClampedArray(data), info.width, info.height, info.channels); + if (rawChannels !== undefined && rawChannels !== info.channels) { + // Make sure the new image has the same number of channels as the input image. + // This is necessary for grayscale images. + newImage.convert(rawChannels); + } + return newImage; + } + +} else { + throw new Error('Unable to load image processing library.'); +} + + +// Defined here: https://github.com/python-pillow/Pillow/blob/a405e8406b83f8bfb8916e93971edc7407b8b1ff/src/libImaging/Imaging.h#L262-L268 +const RESAMPLING_MAPPING = { + 0: 'nearest', + 1: 'lanczos', + 2: 'bilinear', + 3: 'bicubic', + 4: 'box', + 5: 'hamming', +} + +/** + * Mapping from file extensions to MIME types. + */ +const CONTENT_TYPE_MAP = new Map([ + ['png', 'image/png'], + ['jpg', 'image/jpeg'], + ['jpeg', 'image/jpeg'], + ['gif', 'image/gif'], +]); + +class RawImage { + + /** + * Create a new `RawImage` object. + * @param {Uint8ClampedArray|Uint8Array} data The pixel data. + * @param {number} width The width of the image. + * @param {number} height The height of the image. + * @param {1|2|3|4} channels The number of channels. + */ + constructor(data, width, height, channels) { + this.data = data; + this.width = width; + this.height = height; + this.channels = channels; + } + + /** + * Returns the size of the image (width, height). + * @returns {[number, number]} The size of the image (width, height). + */ + get size() { + return [this.width, this.height]; + } + + /** + * Helper method for reading an image from a variety of input types. + * @param {RawImage|string|URL} input + * @returns The image object. + * + * **Example:** Read image from a URL. + * ```javascript + * let image = await RawImage.read('https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/football-match.jpg'); + * // RawImage { + * // "data": Uint8ClampedArray [ 25, 25, 25, 19, 19, 19, ... ], + * // "width": 800, + * // "height": 533, + * // "channels": 3 + * // } + * ``` + */ + static async read(input) { + if (input instanceof RawImage) { + return input; + } else if (typeof input === 'string' || input instanceof URL) { + return await this.fromURL(input); + } else { + throw new Error(`Unsupported input type: ${typeof input}`); + } + } + + /** + * Read an image from a canvas. + * @param {HTMLCanvasElement|OffscreenCanvas} canvas The canvas to read the image from. + * @returns {RawImage} The image object. + */ + static fromCanvas(canvas) { + if (!BROWSER_ENV) { + throw new Error('fromCanvas() is only supported in browser environments.') + } + + const ctx = canvas.getContext('2d'); + const data = ctx.getImageData(0, 0, canvas.width, canvas.height).data; + return new RawImage(data, canvas.width, canvas.height, 4); + } + + /** + * Read an image from a URL or file path. + * @param {string|URL} url The URL or file path to read the image from. + * @returns {Promise} The image object. + */ + static async fromURL(url) { + const response = await (0,_hub_js__WEBPACK_IMPORTED_MODULE_0__.getFile)(url); + if (response.status !== 200) { + throw new Error(`Unable to read image from "${url}" (${response.status} ${response.statusText})`); + } + const blob = await response.blob(); + return this.fromBlob(blob); + } + + /** + * Helper method to create a new Image from a blob. + * @param {Blob} blob The blob to read the image from. + * @returns {Promise} The image object. + */ + static async fromBlob(blob) { + if (BROWSER_ENV) { + // Running in environment with canvas + const img = await loadImageFunction(blob); + + const ctx = createCanvasFunction(img.width, img.height).getContext('2d'); + + // Draw image to context + ctx.drawImage(img, 0, 0); + + return new this(ctx.getImageData(0, 0, img.width, img.height).data, img.width, img.height, 4); + + } else { + // Use sharp.js to read (and possible resize) the image. + const img = (0,sharp__WEBPACK_IMPORTED_MODULE_3__["default"])(await blob.arrayBuffer()); + + return await loadImageFunction(img); + } + } + + /** + * Helper method to create a new Image from a tensor + * @param {Tensor} tensor + */ + static fromTensor(tensor, channel_format = 'CHW') { + if (tensor.dims.length !== 3) { + throw new Error(`Tensor should have 3 dimensions, but has ${tensor.dims.length} dimensions.`); + } + + if (channel_format === 'CHW') { + tensor = tensor.transpose(1, 2, 0); + } else if (channel_format === 'HWC') { + // Do nothing + } else { + throw new Error(`Unsupported channel format: ${channel_format}`); + } + if (!(tensor.data instanceof Uint8ClampedArray || tensor.data instanceof Uint8Array)) { + throw new Error(`Unsupported tensor type: ${tensor.type}`); + } + switch (tensor.dims[2]) { + case 1: + case 2: + case 3: + case 4: + return new RawImage(tensor.data, tensor.dims[1], tensor.dims[0], tensor.dims[2]); + default: + throw new Error(`Unsupported number of channels: ${tensor.dims[2]}`); + } + } + + /** + * Convert the image to grayscale format. + * @returns {RawImage} `this` to support chaining. + */ + grayscale() { + if (this.channels === 1) { + return this; + } + + const newData = new Uint8ClampedArray(this.width * this.height * 1); + switch (this.channels) { + case 3: // rgb to grayscale + case 4: // rgba to grayscale + for (let i = 0, offset = 0; i < this.data.length; i += this.channels) { + const red = this.data[i]; + const green = this.data[i + 1]; + const blue = this.data[i + 2]; + + newData[offset++] = Math.round(0.2989 * red + 0.5870 * green + 0.1140 * blue); + } + break; + default: + throw new Error(`Conversion failed due to unsupported number of channels: ${this.channels}`); + } + return this._update(newData, this.width, this.height, 1); + } + + /** + * Convert the image to RGB format. + * @returns {RawImage} `this` to support chaining. + */ + rgb() { + if (this.channels === 3) { + return this; + } + + const newData = new Uint8ClampedArray(this.width * this.height * 3); + + switch (this.channels) { + case 1: // grayscale to rgb + for (let i = 0, offset = 0; i < this.data.length; ++i) { + newData[offset++] = this.data[i]; + newData[offset++] = this.data[i]; + newData[offset++] = this.data[i]; + } + break; + case 4: // rgba to rgb + for (let i = 0, offset = 0; i < this.data.length; i += 4) { + newData[offset++] = this.data[i]; + newData[offset++] = this.data[i + 1]; + newData[offset++] = this.data[i + 2]; + } + break; + default: + throw new Error(`Conversion failed due to unsupported number of channels: ${this.channels}`); + } + return this._update(newData, this.width, this.height, 3); + + } + + /** + * Convert the image to RGBA format. + * @returns {RawImage} `this` to support chaining. + */ + rgba() { + if (this.channels === 4) { + return this; + } + + const newData = new Uint8ClampedArray(this.width * this.height * 4); + + switch (this.channels) { + case 1: // grayscale to rgba + for (let i = 0, offset = 0; i < this.data.length; ++i) { + newData[offset++] = this.data[i]; + newData[offset++] = this.data[i]; + newData[offset++] = this.data[i]; + newData[offset++] = 255; + } + break; + case 3: // rgb to rgba + for (let i = 0, offset = 0; i < this.data.length; i += 3) { + newData[offset++] = this.data[i]; + newData[offset++] = this.data[i + 1]; + newData[offset++] = this.data[i + 2]; + newData[offset++] = 255; + } + break; + default: + throw new Error(`Conversion failed due to unsupported number of channels: ${this.channels}`); + } + + return this._update(newData, this.width, this.height, 4); + } + + /** + * Resize the image to the given dimensions. This method uses the canvas API to perform the resizing. + * @param {number} width The width of the new image. + * @param {number} height The height of the new image. + * @param {Object} options Additional options for resizing. + * @param {0|1|2|3|4|5|string} [options.resample] The resampling method to use. + * @returns {Promise} `this` to support chaining. + */ + async resize(width, height, { + resample = 2, + } = {}) { + + // Ensure resample method is a string + let resampleMethod = RESAMPLING_MAPPING[resample] ?? resample; + + if (BROWSER_ENV) { + // TODO use `resample` in browser environment + + // Store number of channels before resizing + const numChannels = this.channels; + + // Create canvas object for this image + const canvas = this.toCanvas(); + + // Actually perform resizing using the canvas API + const ctx = createCanvasFunction(width, height).getContext('2d'); + + // Draw image to context, resizing in the process + ctx.drawImage(canvas, 0, 0, width, height); + + // Create image from the resized data + const resizedImage = new RawImage(ctx.getImageData(0, 0, width, height).data, width, height, 4); + + // Convert back so that image has the same number of channels as before + return resizedImage.convert(numChannels); + + } else { + // Create sharp image from raw data, and resize + let img = this.toSharp(); + + switch (resampleMethod) { + case 'box': + case 'hamming': + if (resampleMethod === 'box' || resampleMethod === 'hamming') { + console.warn(`Resampling method ${resampleMethod} is not yet supported. Using bilinear instead.`); + resampleMethod = 'bilinear'; + } + + case 'nearest': + case 'bilinear': + case 'bicubic': + // Perform resizing using affine transform. + // This matches how the python Pillow library does it. + img = img.affine([width / this.width, 0, 0, height / this.height], { + interpolator: resampleMethod + }); + break; + + case 'lanczos': + // https://github.com/python-pillow/Pillow/discussions/5519 + // https://github.com/lovell/sharp/blob/main/docs/api-resize.md + img = img.resize({ + width, height, + fit: 'fill', + kernel: 'lanczos3', // PIL Lanczos uses a kernel size of 3 + }); + break; + + default: + throw new Error(`Resampling method ${resampleMethod} is not supported.`); + } + + return await loadImageFunction(img); + } + + } + + async pad([left, right, top, bottom]) { + left = Math.max(left, 0); + right = Math.max(right, 0); + top = Math.max(top, 0); + bottom = Math.max(bottom, 0); + + if (left === 0 && right === 0 && top === 0 && bottom === 0) { + // No padding needed + return this; + } + + if (BROWSER_ENV) { + // Store number of channels before padding + const numChannels = this.channels; + + // Create canvas object for this image + const canvas = this.toCanvas(); + + const newWidth = this.width + left + right; + const newHeight = this.height + top + bottom; + + // Create a new canvas of the desired size. + const ctx = createCanvasFunction(newWidth, newHeight).getContext('2d'); + + // Draw image to context, padding in the process + ctx.drawImage(canvas, + 0, 0, this.width, this.height, + left, top, newWidth, newHeight + ); + + // Create image from the padded data + const paddedImage = new RawImage( + ctx.getImageData(0, 0, newWidth, newHeight).data, + newWidth, newHeight, 4); + + // Convert back so that image has the same number of channels as before + return paddedImage.convert(numChannels); + + } else { + const img = this.toSharp().extend({ left, right, top, bottom }); + return await loadImageFunction(img); + } + } + + async crop([x_min, y_min, x_max, y_max]) { + // Ensure crop bounds are within the image + x_min = Math.max(x_min, 0); + y_min = Math.max(y_min, 0); + x_max = Math.min(x_max, this.width - 1); + y_max = Math.min(y_max, this.height - 1); + + // Do nothing if the crop is the entire image + if (x_min === 0 && y_min === 0 && x_max === this.width - 1 && y_max === this.height - 1) { + return this; + } + + const crop_width = x_max - x_min + 1; + const crop_height = y_max - y_min + 1; + + if (BROWSER_ENV) { + // Store number of channels before resizing + const numChannels = this.channels; + + // Create canvas object for this image + const canvas = this.toCanvas(); + + // Create a new canvas of the desired size. This is needed since if the + // image is too small, we need to pad it with black pixels. + const ctx = createCanvasFunction(crop_width, crop_height).getContext('2d'); + + // Draw image to context, cropping in the process + ctx.drawImage(canvas, + x_min, y_min, crop_width, crop_height, + 0, 0, crop_width, crop_height + ); + + // Create image from the resized data + const resizedImage = new RawImage(ctx.getImageData(0, 0, crop_width, crop_height).data, crop_width, crop_height, 4); + + // Convert back so that image has the same number of channels as before + return resizedImage.convert(numChannels); + + } else { + // Create sharp image from raw data + const img = this.toSharp().extract({ + left: x_min, + top: y_min, + width: crop_width, + height: crop_height, + }); + + return await loadImageFunction(img); + } + + } + + async center_crop(crop_width, crop_height) { + // If the image is already the desired size, return it + if (this.width === crop_width && this.height === crop_height) { + return this; + } + + // Determine bounds of the image in the new canvas + const width_offset = (this.width - crop_width) / 2; + const height_offset = (this.height - crop_height) / 2; + + + if (BROWSER_ENV) { + // Store number of channels before resizing + const numChannels = this.channels; + + // Create canvas object for this image + const canvas = this.toCanvas(); + + // Create a new canvas of the desired size. This is needed since if the + // image is too small, we need to pad it with black pixels. + const ctx = createCanvasFunction(crop_width, crop_height).getContext('2d'); + + let sourceX = 0; + let sourceY = 0; + let destX = 0; + let destY = 0; + + if (width_offset >= 0) { + sourceX = width_offset; + } else { + destX = -width_offset; + } + + if (height_offset >= 0) { + sourceY = height_offset; + } else { + destY = -height_offset; + } + + // Draw image to context, cropping in the process + ctx.drawImage(canvas, + sourceX, sourceY, crop_width, crop_height, + destX, destY, crop_width, crop_height + ); + + // Create image from the resized data + const resizedImage = new RawImage(ctx.getImageData(0, 0, crop_width, crop_height).data, crop_width, crop_height, 4); + + // Convert back so that image has the same number of channels as before + return resizedImage.convert(numChannels); + + } else { + // Create sharp image from raw data + let img = this.toSharp(); + + if (width_offset >= 0 && height_offset >= 0) { + // Cropped image lies entirely within the original image + img = img.extract({ + left: Math.floor(width_offset), + top: Math.floor(height_offset), + width: crop_width, + height: crop_height, + }) + } else if (width_offset <= 0 && height_offset <= 0) { + // Cropped image lies entirely outside the original image, + // so we add padding + const top = Math.floor(-height_offset); + const left = Math.floor(-width_offset); + img = img.extend({ + top: top, + left: left, + + // Ensures the resulting image has the desired dimensions + right: crop_width - this.width - left, + bottom: crop_height - this.height - top, + }); + } else { + // Cropped image lies partially outside the original image. + // We first pad, then crop. + + let y_padding = [0, 0]; + let y_extract = 0; + if (height_offset < 0) { + y_padding[0] = Math.floor(-height_offset); + y_padding[1] = crop_height - this.height - y_padding[0]; + } else { + y_extract = Math.floor(height_offset); + } + + let x_padding = [0, 0]; + let x_extract = 0; + if (width_offset < 0) { + x_padding[0] = Math.floor(-width_offset); + x_padding[1] = crop_width - this.width - x_padding[0]; + } else { + x_extract = Math.floor(width_offset); + } + + img = img.extend({ + top: y_padding[0], + bottom: y_padding[1], + left: x_padding[0], + right: x_padding[1], + }).extract({ + left: x_extract, + top: y_extract, + width: crop_width, + height: crop_height, + }) + } + + return await loadImageFunction(img); + } + } + + async toBlob(type = 'image/png', quality = 1) { + if (!BROWSER_ENV) { + throw new Error('toBlob() is only supported in browser environments.') + } + + const canvas = this.toCanvas(); + return await canvas.convertToBlob({ type, quality }); + } + + toTensor(channel_format = 'CHW') { + let tensor = new _tensor_js__WEBPACK_IMPORTED_MODULE_2__.Tensor( + 'uint8', + new Uint8Array(this.data), + [this.height, this.width, this.channels] + ); + + if (channel_format === 'HWC') { + // Do nothing + } else if (channel_format === 'CHW') { // hwc -> chw + tensor = tensor.permute(2, 0, 1); + } else { + throw new Error(`Unsupported channel format: ${channel_format}`); + } + return tensor; + } + + toCanvas() { + if (!BROWSER_ENV) { + throw new Error('toCanvas() is only supported in browser environments.') + } + + // Clone, and convert data to RGBA before drawing to canvas. + // This is because the canvas API only supports RGBA + const cloned = this.clone().rgba(); + + // Create canvas object for the cloned image + const clonedCanvas = createCanvasFunction(cloned.width, cloned.height); + + // Draw image to context + const data = new ImageDataClass(cloned.data, cloned.width, cloned.height); + clonedCanvas.getContext('2d').putImageData(data, 0, 0); + + return clonedCanvas; + } + + /** + * Helper method to update the image data. + * @param {Uint8ClampedArray} data The new image data. + * @param {number} width The new width of the image. + * @param {number} height The new height of the image. + * @param {1|2|3|4|null} [channels] The new number of channels of the image. + * @private + */ + _update(data, width, height, channels = null) { + this.data = data; + this.width = width; + this.height = height; + if (channels !== null) { + this.channels = channels; + } + return this; + } + + /** + * Clone the image + * @returns {RawImage} The cloned image + */ + clone() { + return new RawImage(this.data.slice(), this.width, this.height, this.channels); + } + + /** + * Helper method for converting image to have a certain number of channels + * @param {number} numChannels The number of channels. Must be 1, 3, or 4. + * @returns {RawImage} `this` to support chaining. + */ + convert(numChannels) { + if (this.channels === numChannels) return this; // Already correct number of channels + + switch (numChannels) { + case 1: + this.grayscale(); + break; + case 3: + this.rgb(); + break; + case 4: + this.rgba(); + break; + default: + throw new Error(`Conversion failed due to unsupported number of channels: ${this.channels}`); + } + return this; + } + + /** + * Save the image to the given path. + * @param {string} path The path to save the image to. + */ + async save(path) { + + if (BROWSER_ENV) { + if (WEBWORKER_ENV) { + throw new Error('Unable to save an image from a Web Worker.') + } + + const extension = path.split('.').pop().toLowerCase(); + const mime = CONTENT_TYPE_MAP.get(extension) ?? 'image/png'; + + // Convert image to Blob + const blob = await this.toBlob(mime); + + // Convert the canvas content to a data URL + const dataURL = URL.createObjectURL(blob); + + // Create an anchor element with the data URL as the href attribute + const downloadLink = document.createElement('a'); + downloadLink.href = dataURL; + + // Set the download attribute to specify the desired filename for the downloaded image + downloadLink.download = path; + + // Trigger the download + downloadLink.click(); + + // Clean up: remove the anchor element from the DOM + downloadLink.remove(); + + } else if (!_env_js__WEBPACK_IMPORTED_MODULE_1__.env.useFS) { + throw new Error('Unable to save the image because filesystem is disabled in this environment.') + + } else { + const img = this.toSharp(); + return await img.toFile(path); + } + } + + toSharp() { + if (BROWSER_ENV) { + throw new Error('toSharp() is only supported in server-side environments.') + } + + return (0,sharp__WEBPACK_IMPORTED_MODULE_3__["default"])(this.data, { + raw: { + width: this.width, + height: this.height, + channels: this.channels + } + }); + } +} + +/***/ }), + +/***/ "./src/utils/maths.js": +/*!****************************!*\ + !*** ./src/utils/maths.js ***! + \****************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ FFT: () => (/* binding */ FFT), +/* harmony export */ bankers_round: () => (/* binding */ bankers_round), +/* harmony export */ cos_sim: () => (/* binding */ cos_sim), +/* harmony export */ dot: () => (/* binding */ dot), +/* harmony export */ dynamic_time_warping: () => (/* binding */ dynamic_time_warping), +/* harmony export */ interpolate_data: () => (/* binding */ interpolate_data), +/* harmony export */ log_softmax: () => (/* binding */ log_softmax), +/* harmony export */ magnitude: () => (/* binding */ magnitude), +/* harmony export */ max: () => (/* binding */ max), +/* harmony export */ medianFilter: () => (/* binding */ medianFilter), +/* harmony export */ min: () => (/* binding */ min), +/* harmony export */ permute_data: () => (/* binding */ permute_data), +/* harmony export */ round: () => (/* binding */ round), +/* harmony export */ softmax: () => (/* binding */ softmax) +/* harmony export */ }); + +/** + * @file Helper module for mathematical processing. + * + * These functions and classes are only used internally, + * meaning an end-user shouldn't need to access anything here. + * + * @module utils/maths + */ + +/** + * @typedef {Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array} TypedArray + * @typedef {BigInt64Array | BigUint64Array} BigTypedArray + * @typedef {TypedArray | BigTypedArray} AnyTypedArray + */ + +/** + * @param {TypedArray} input + */ +function interpolate_data(input, [in_channels, in_height, in_width], [out_height, out_width], mode = 'bilinear', align_corners = false) { + // TODO use mode and align_corners + + // Output image dimensions + const x_scale = out_width / in_width; + const y_scale = out_height / in_height; + + // Output image + // @ts-ignore + const out_img = new input.constructor(out_height * out_width * in_channels); + + // Pre-calculate strides + const inStride = in_height * in_width; + const outStride = out_height * out_width; + + for (let i = 0; i < out_height; ++i) { + for (let j = 0; j < out_width; ++j) { + // Calculate output offset + const outOffset = i * out_width + j; + + // Calculate input pixel coordinates + const x = (j + 0.5) / x_scale - 0.5; + const y = (i + 0.5) / y_scale - 0.5; + + // Calculate the four nearest input pixels + // We also check if the input pixel coordinates are within the image bounds + let x1 = Math.floor(x); + let y1 = Math.floor(y); + const x2 = Math.min(x1 + 1, in_width - 1); + const y2 = Math.min(y1 + 1, in_height - 1); + + x1 = Math.max(x1, 0); + y1 = Math.max(y1, 0); + + + // Calculate the fractional distances between the input pixel and the four nearest pixels + const s = x - x1; + const t = y - y1; + + // Perform bilinear interpolation + const w1 = (1 - s) * (1 - t); + const w2 = s * (1 - t); + const w3 = (1 - s) * t; + const w4 = s * t; + + // Calculate the four nearest input pixel indices + const yStride = y1 * in_width; + const xStride = y2 * in_width; + const idx1 = yStride + x1; + const idx2 = yStride + x2; + const idx3 = xStride + x1; + const idx4 = xStride + x2; + + for (let k = 0; k < in_channels; ++k) { + // Calculate channel offset + const cOffset = k * inStride; + + out_img[k * outStride + outOffset] = + w1 * input[cOffset + idx1] + + w2 * input[cOffset + idx2] + + w3 * input[cOffset + idx3] + + w4 * input[cOffset + idx4]; + } + } + } + + return out_img; +} + + +/** + * Helper method to permute a `AnyTypedArray` directly + * @template {AnyTypedArray} T + * @param {T} array + * @param {number[]} dims + * @param {number[]} axes + * @returns {[T, number[]]} The permuted array and the new shape. + */ +function permute_data(array, dims, axes) { + // Calculate the new shape of the permuted array + // and the stride of the original array + const shape = new Array(axes.length); + const stride = new Array(axes.length); + + for (let i = axes.length - 1, s = 1; i >= 0; --i) { + stride[i] = s; + shape[i] = dims[axes[i]]; + s *= shape[i]; + } + + // Precompute inverse mapping of stride + const invStride = axes.map((_, i) => stride[axes.indexOf(i)]); + + // Create the permuted array with the new shape + // @ts-ignore + const permutedData = new array.constructor(array.length); + + // Permute the original array to the new array + for (let i = 0; i < array.length; ++i) { + let newIndex = 0; + for (let j = dims.length - 1, k = i; j >= 0; --j) { + newIndex += (k % dims[j]) * invStride[j]; + k = Math.floor(k / dims[j]); + } + permutedData[newIndex] = array[i]; + } + + return [permutedData, shape]; +} + + +/** + * Compute the softmax of an array of numbers. + * @template {TypedArray|number[]} T + * @param {T} arr The array of numbers to compute the softmax of. + * @returns {T} The softmax array. + */ +function softmax(arr) { + // Compute the maximum value in the array + const maxVal = max(arr)[0]; + + // Compute the exponentials of the array values + const exps = arr.map(x => Math.exp(x - maxVal)); + + // Compute the sum of the exponentials + // @ts-ignore + const sumExps = exps.reduce((acc, val) => acc + val, 0); + + // Compute the softmax values + const softmaxArr = exps.map(x => x / sumExps); + + return /** @type {T} */(softmaxArr); +} + +/** + * Calculates the logarithm of the softmax function for the input array. + * @template {TypedArray|number[]} T + * @param {T} arr The input array to calculate the log_softmax function for. + * @returns {T} The resulting log_softmax array. + */ +function log_softmax(arr) { + // Compute the maximum value in the array + const maxVal = max(arr)[0]; + + // Compute the sum of the exponentials + let sumExps = 0; + for(let i = 0; i < arr.length; ++i) { + sumExps += Math.exp(arr[i] - maxVal); + } + + // Compute the log of the sum + const logSum = Math.log(sumExps); + + // Compute the softmax values + const logSoftmaxArr = arr.map(x => x - maxVal - logSum); + + return /** @type {T} */(logSoftmaxArr); +} + +/** + * Calculates the dot product of two arrays. + * @param {number[]} arr1 The first array. + * @param {number[]} arr2 The second array. + * @returns {number} The dot product of arr1 and arr2. + */ +function dot(arr1, arr2) { + let result = 0; + for (let i = 0; i < arr1.length; ++i) { + result += arr1[i] * arr2[i]; + } + return result; +} + +/** + * Computes the cosine similarity between two arrays. + * + * @param {number[]} arr1 The first array. + * @param {number[]} arr2 The second array. + * @returns {number} The cosine similarity between the two arrays. + */ +function cos_sim(arr1, arr2) { + // Calculate dot product of the two arrays + const dotProduct = dot(arr1, arr2); + + // Calculate the magnitude of the first array + const magnitudeA = magnitude(arr1); + + // Calculate the magnitude of the second array + const magnitudeB = magnitude(arr2); + + // Calculate the cosine similarity + const cosineSimilarity = dotProduct / (magnitudeA * magnitudeB); + + return cosineSimilarity; +} + +/** + * Calculates the magnitude of a given array. + * @param {number[]} arr The array to calculate the magnitude of. + * @returns {number} The magnitude of the array. + */ +function magnitude(arr) { + return Math.sqrt(arr.reduce((acc, val) => acc + val * val, 0)); +} + + +/** + * Returns the value and index of the minimum element in an array. + * @param {number[]|TypedArray} arr array of numbers. + * @returns {[number, number]} the value and index of the minimum element, of the form: [valueOfMin, indexOfMin] + * @throws {Error} If array is empty. + */ +function min(arr) { + if (arr.length === 0) throw Error('Array must not be empty'); + let min = arr[0]; + let indexOfMin = 0; + for (let i = 1; i < arr.length; ++i) { + if (arr[i] < min) { + min = arr[i]; + indexOfMin = i; + } + } + return [min, indexOfMin]; +} + + +/** + * Returns the value and index of the maximum element in an array. + * @param {number[]|AnyTypedArray} arr array of numbers. + * @returns {[number, number]} the value and index of the maximum element, of the form: [valueOfMax, indexOfMax] + * @throws {Error} If array is empty. + */ +function max(arr) { + if (arr.length === 0) throw Error('Array must not be empty'); + let max = arr[0]; + let indexOfMax = 0; + for (let i = 1; i < arr.length; ++i) { + if (arr[i] > max) { + max = arr[i]; + indexOfMax = i; + } + } + return [Number(max), indexOfMax]; +} + +function isPowerOfTwo(number) { + // Check if the number is greater than 0 and has only one bit set to 1 + return (number > 0) && ((number & (number - 1)) === 0); +} + +/** + * Implementation of Radix-4 FFT. + * + * P2FFT class provides functionality for performing Fast Fourier Transform on arrays + * which are a power of two in length. + * Code adapted from https://www.npmjs.com/package/fft.js + */ +class P2FFT { + /** + * @param {number} size The size of the input array. Must be a power of two larger than 1. + * @throws {Error} FFT size must be a power of two larger than 1. + */ + constructor(size) { + this.size = size | 0; // convert to a 32-bit signed integer + if (this.size <= 1 || !isPowerOfTwo(this.size)) + throw new Error('FFT size must be a power of two larger than 1'); + + this._csize = size << 1; + + this.table = new Float64Array(this.size * 2); + for (let i = 0; i < this.table.length; i += 2) { + const angle = Math.PI * i / this.size; + this.table[i] = Math.cos(angle); + this.table[i + 1] = -Math.sin(angle); + } + + // Find size's power of two + let power = 0; + for (let t = 1; this.size > t; t <<= 1) + ++power; + + // Calculate initial step's width: + // * If we are full radix-4, it is 2x smaller to give inital len=8 + // * Otherwise it is the same as `power` to give len=4 + this._width = power % 2 === 0 ? power - 1 : power; + + // Pre-compute bit-reversal patterns + this._bitrev = new Int32Array(1 << this._width); + for (let j = 0; j < this._bitrev.length; ++j) { + this._bitrev[j] = 0; + for (let shift = 0; shift < this._width; shift += 2) { + const revShift = this._width - shift - 2; + this._bitrev[j] |= ((j >>> shift) & 3) << revShift; + } + } + } + + /** + * Create a complex number array with size `2 * size` + * + * @returns {Float64Array} A complex number array with size `2 * size` + */ + createComplexArray() { + return new Float64Array(this._csize); + } + + /** + * Converts a complex number representation stored in a Float64Array to an array of real numbers. + * + * @param {Float64Array} complex The complex number representation to be converted. + * @param {number[]} [storage] An optional array to store the result in. + * @returns {number[]} An array of real numbers representing the input complex number representation. + */ + fromComplexArray(complex, storage) { + const res = storage || new Array(complex.length >>> 1); + for (let i = 0; i < complex.length; i += 2) + res[i >>> 1] = complex[i]; + return res; + } + + /** + * Convert a real-valued input array to a complex-valued output array. + * @param {Float64Array} input The real-valued input array. + * @param {Float64Array} [storage] Optional buffer to store the output array. + * @returns {Float64Array} The complex-valued output array. + */ + toComplexArray(input, storage) { + const res = storage || this.createComplexArray(); + for (let i = 0; i < res.length; i += 2) { + res[i] = input[i >>> 1]; + res[i + 1] = 0; + } + return res; + } + + /** + * Performs a Fast Fourier Transform (FFT) on the given input data and stores the result in the output buffer. + * + * @param {Float64Array} out The output buffer to store the result. + * @param {Float64Array} data The input data to transform. + * + * @throws {Error} Input and output buffers must be different. + * + * @returns {void} + */ + transform(out, data) { + if (out === data) + throw new Error('Input and output buffers must be different'); + + this._transform4(out, data, 1 /* DONE */); + } + + /** + * Performs a real-valued forward FFT on the given input buffer and stores the result in the given output buffer. + * The input buffer must contain real values only, while the output buffer will contain complex values. The input and + * output buffers must be different. + * + * @param {Float64Array} out The output buffer. + * @param {Float64Array} data The input buffer containing real values. + * + * @throws {Error} If the input and output buffers are the same. + */ + realTransform(out, data) { + if (out === data) + throw new Error('Input and output buffers must be different'); + + this._realTransform4(out, data, 1 /* DONE */); + } + + /** + * Performs an inverse FFT transformation on the given `data` array, and stores the result in `out`. + * The `out` array must be a different buffer than the `data` array. The `out` array will contain the + * result of the transformation. The `data` array will not be modified. + * + * @param {Float64Array} out The output buffer for the transformed data. + * @param {Float64Array} data The input data to transform. + * @throws {Error} If `out` and `data` refer to the same buffer. + * @returns {void} + */ + inverseTransform(out, data) { + if (out === data) + throw new Error('Input and output buffers must be different'); + + this._transform4(out, data, -1 /* DONE */); + for (let i = 0; i < out.length; ++i) + out[i] /= this.size; + } + + /** + * Performs a radix-4 implementation of a discrete Fourier transform on a given set of data. + * + * @param {Float64Array} out The output buffer for the transformed data. + * @param {Float64Array} data The input buffer of data to be transformed. + * @param {number} inv A scaling factor to apply to the transform. + * @returns {void} + */ + _transform4(out, data, inv) { + // radix-4 implementation + + const size = this._csize; + + // Initial step (permute and transform) + const width = this._width; + let step = 1 << width; + let len = (size / step) << 1; + + let outOff; + let t; + const bitrev = this._bitrev; + if (len === 4) { + for (outOff = 0, t = 0; outOff < size; outOff += len, ++t) { + const off = bitrev[t]; + this._singleTransform2(data, out, outOff, off, step); + } + } else { + // len === 8 + for (outOff = 0, t = 0; outOff < size; outOff += len, ++t) { + const off = bitrev[t]; + this._singleTransform4(data, out, outOff, off, step, inv); + } + } + + // Loop through steps in decreasing order + const table = this.table; + for (step >>= 2; step >= 2; step >>= 2) { + len = (size / step) << 1; + const quarterLen = len >>> 2; + + // Loop through offsets in the data + for (outOff = 0; outOff < size; outOff += len) { + // Full case + const limit = outOff + quarterLen - 1; + for (let i = outOff, k = 0; i < limit; i += 2, k += step) { + const A = i; + const B = A + quarterLen; + const C = B + quarterLen; + const D = C + quarterLen; + + // Original values + const Ar = out[A]; + const Ai = out[A + 1]; + const Br = out[B]; + const Bi = out[B + 1]; + const Cr = out[C]; + const Ci = out[C + 1]; + const Dr = out[D]; + const Di = out[D + 1]; + + const tableBr = table[k]; + const tableBi = inv * table[k + 1]; + const MBr = Br * tableBr - Bi * tableBi; + const MBi = Br * tableBi + Bi * tableBr; + + const tableCr = table[2 * k]; + const tableCi = inv * table[2 * k + 1]; + const MCr = Cr * tableCr - Ci * tableCi; + const MCi = Cr * tableCi + Ci * tableCr; + + const tableDr = table[3 * k]; + const tableDi = inv * table[3 * k + 1]; + const MDr = Dr * tableDr - Di * tableDi; + const MDi = Dr * tableDi + Di * tableDr; + + // Pre-Final values + const T0r = Ar + MCr; + const T0i = Ai + MCi; + const T1r = Ar - MCr; + const T1i = Ai - MCi; + const T2r = MBr + MDr; + const T2i = MBi + MDi; + const T3r = inv * (MBr - MDr); + const T3i = inv * (MBi - MDi); + + // Final values + out[A] = T0r + T2r; + out[A + 1] = T0i + T2i; + out[B] = T1r + T3i; + out[B + 1] = T1i - T3r; + out[C] = T0r - T2r; + out[C + 1] = T0i - T2i; + out[D] = T1r - T3i; + out[D + 1] = T1i + T3r; + } + } + } + } + + /** + * Performs a radix-2 implementation of a discrete Fourier transform on a given set of data. + * + * @param {Float64Array} data The input buffer of data to be transformed. + * @param {Float64Array} out The output buffer for the transformed data. + * @param {number} outOff The offset at which to write the output data. + * @param {number} off The offset at which to begin reading the input data. + * @param {number} step The step size for indexing the input data. + * @returns {void} + */ + _singleTransform2(data, out, outOff, off, step) { + // radix-2 implementation + // NOTE: Only called for len=4 + + const evenR = data[off]; + const evenI = data[off + 1]; + const oddR = data[off + step]; + const oddI = data[off + step + 1]; + + out[outOff] = evenR + oddR; + out[outOff + 1] = evenI + oddI; + out[outOff + 2] = evenR - oddR; + out[outOff + 3] = evenI - oddI; + } + + /** + * Performs radix-4 transformation on input data of length 8 + * + * @param {Float64Array} data Input data array of length 8 + * @param {Float64Array} out Output data array of length 8 + * @param {number} outOff Index of output array to start writing from + * @param {number} off Index of input array to start reading from + * @param {number} step Step size between elements in input array + * @param {number} inv Scaling factor for inverse transform + * + * @returns {void} + */ + _singleTransform4(data, out, outOff, off, step, inv) { + // radix-4 + // NOTE: Only called for len=8 + const step2 = step * 2; + const step3 = step * 3; + + // Original values + const Ar = data[off]; + const Ai = data[off + 1]; + const Br = data[off + step]; + const Bi = data[off + step + 1]; + const Cr = data[off + step2]; + const Ci = data[off + step2 + 1]; + const Dr = data[off + step3]; + const Di = data[off + step3 + 1]; + + // Pre-Final values + const T0r = Ar + Cr; + const T0i = Ai + Ci; + const T1r = Ar - Cr; + const T1i = Ai - Ci; + const T2r = Br + Dr; + const T2i = Bi + Di; + const T3r = inv * (Br - Dr); + const T3i = inv * (Bi - Di); + + // Final values + out[outOff] = T0r + T2r; + out[outOff + 1] = T0i + T2i; + out[outOff + 2] = T1r + T3i; + out[outOff + 3] = T1i - T3r; + out[outOff + 4] = T0r - T2r; + out[outOff + 5] = T0i - T2i; + out[outOff + 6] = T1r - T3i; + out[outOff + 7] = T1i + T3r; + } + + /** + * Real input radix-4 implementation + * @param {Float64Array} out Output array for the transformed data + * @param {Float64Array} data Input array of real data to be transformed + * @param {number} inv The scale factor used to normalize the inverse transform + */ + _realTransform4(out, data, inv) { + // Real input radix-4 implementation + const size = this._csize; + + // Initial step (permute and transform) + const width = this._width; + let step = 1 << width; + let len = (size / step) << 1; + + let outOff; + let t; + const bitrev = this._bitrev; + if (len === 4) { + for (outOff = 0, t = 0; outOff < size; outOff += len, ++t) { + const off = bitrev[t]; + this._singleRealTransform2(data, out, outOff, off >>> 1, step >>> 1); + } + } else { + // len === 8 + for (outOff = 0, t = 0; outOff < size; outOff += len, ++t) { + const off = bitrev[t]; + this._singleRealTransform4(data, out, outOff, off >>> 1, step >>> 1, inv); + } + } + + // Loop through steps in decreasing order + const table = this.table; + for (step >>= 2; step >= 2; step >>= 2) { + len = (size / step) << 1; + const halfLen = len >>> 1; + const quarterLen = halfLen >>> 1; + const hquarterLen = quarterLen >>> 1; + + // Loop through offsets in the data + for (outOff = 0; outOff < size; outOff += len) { + for (let i = 0, k = 0; i <= hquarterLen; i += 2, k += step) { + const A = outOff + i; + const B = A + quarterLen; + const C = B + quarterLen; + const D = C + quarterLen; + + // Original values + const Ar = out[A]; + const Ai = out[A + 1]; + const Br = out[B]; + const Bi = out[B + 1]; + const Cr = out[C]; + const Ci = out[C + 1]; + const Dr = out[D]; + const Di = out[D + 1]; + + // Middle values + const MAr = Ar; + const MAi = Ai; + + const tableBr = table[k]; + const tableBi = inv * table[k + 1]; + const MBr = Br * tableBr - Bi * tableBi; + const MBi = Br * tableBi + Bi * tableBr; + + const tableCr = table[2 * k]; + const tableCi = inv * table[2 * k + 1]; + const MCr = Cr * tableCr - Ci * tableCi; + const MCi = Cr * tableCi + Ci * tableCr; + + const tableDr = table[3 * k]; + const tableDi = inv * table[3 * k + 1]; + const MDr = Dr * tableDr - Di * tableDi; + const MDi = Dr * tableDi + Di * tableDr; + + // Pre-Final values + const T0r = MAr + MCr; + const T0i = MAi + MCi; + const T1r = MAr - MCr; + const T1i = MAi - MCi; + const T2r = MBr + MDr; + const T2i = MBi + MDi; + const T3r = inv * (MBr - MDr); + const T3i = inv * (MBi - MDi); + + // Final values + out[A] = T0r + T2r; + out[A + 1] = T0i + T2i; + out[B] = T1r + T3i; + out[B + 1] = T1i - T3r; + + // Output final middle point + if (i === 0) { + out[C] = T0r - T2r; + out[C + 1] = T0i - T2i; + continue; + } + + // Do not overwrite ourselves + if (i === hquarterLen) + continue; + + const SA = outOff + quarterLen - i; + const SB = outOff + halfLen - i; + + out[SA] = T1r - inv * T3i; + out[SA + 1] = -T1i - inv * T3r; + out[SB] = T0r - inv * T2r; + out[SB + 1] = -T0i + inv * T2i; + } + } + } + + // Complete the spectrum by adding its mirrored negative frequency components. + const half = size >>> 1; + for (let i = 2; i < half; i += 2) { + out[size - i] = out[i]; + out[size - i + 1] = -out[i + 1]; + } + } + + /** + * Performs a single real input radix-2 transformation on the provided data + * + * @param {Float64Array} data The input data array + * @param {Float64Array} out The output data array + * @param {number} outOff The output offset + * @param {number} off The input offset + * @param {number} step The step + * + * @returns {void} + */ + _singleRealTransform2(data, out, outOff, off, step) { + // radix-2 implementation + // NOTE: Only called for len=4 + + const evenR = data[off]; + const oddR = data[off + step]; + + out[outOff] = evenR + oddR; + out[outOff + 1] = 0; + out[outOff + 2] = evenR - oddR; + out[outOff + 3] = 0; + } + + /** + * Computes a single real-valued transform using radix-4 algorithm. + * This method is only called for len=8. + * + * @param {Float64Array} data The input data array. + * @param {Float64Array} out The output data array. + * @param {number} outOff The offset into the output array. + * @param {number} off The offset into the input array. + * @param {number} step The step size for the input array. + * @param {number} inv The value of inverse. + */ + _singleRealTransform4(data, out, outOff, off, step, inv) { + // radix-4 + // NOTE: Only called for len=8 + const step2 = step * 2; + const step3 = step * 3; + + // Original values + const Ar = data[off]; + const Br = data[off + step]; + const Cr = data[off + step2]; + const Dr = data[off + step3]; + + // Pre-Final values + const T0r = Ar + Cr; + const T1r = Ar - Cr; + const T2r = Br + Dr; + const T3r = inv * (Br - Dr); + + // Final values + out[outOff] = T0r + T2r; + out[outOff + 1] = 0; + out[outOff + 2] = T1r; + out[outOff + 3] = -T3r; + out[outOff + 4] = T0r - T2r; + out[outOff + 5] = 0; + out[outOff + 6] = T1r; + out[outOff + 7] = T3r; + } +} + +/** + * NP2FFT class provides functionality for performing Fast Fourier Transform on arrays + * which are not a power of two in length. In such cases, the chirp-z transform is used. + * + * For more information, see: https://math.stackexchange.com/questions/77118/non-power-of-2-ffts/77156#77156 + */ +class NP2FFT { + + /** + * Constructs a new NP2FFT object. + * @param {number} fft_length The length of the FFT + */ + constructor(fft_length) { + // Helper variables + const a = 2 * (fft_length - 1); + const b = 2 * (2 * fft_length - 1); + const nextP2 = 2 ** (Math.ceil(Math.log2(b))) + this.bufferSize = nextP2; + this._a = a; + + // Define buffers + // Compute chirp for transform + const chirp = new Float64Array(b); + const ichirp = new Float64Array(nextP2); + this._chirpBuffer = new Float64Array(nextP2); + this._buffer1 = new Float64Array(nextP2); + this._buffer2 = new Float64Array(nextP2); + this._outBuffer1 = new Float64Array(nextP2); + this._outBuffer2 = new Float64Array(nextP2); + + // Compute complex exponentiation + const theta = -2 * Math.PI / fft_length; + const baseR = Math.cos(theta); + const baseI = Math.sin(theta); + + // Precompute helper for chirp-z transform + for (let i = 0; i < b >> 1; ++i) { + // Compute complex power: + const e = (i + 1 - fft_length) ** 2 / 2.0; + + // Compute the modulus and argument of the result + const result_mod = Math.sqrt(baseR ** 2 + baseI ** 2) ** e; + const result_arg = e * Math.atan2(baseI, baseR); + + // Convert the result back to rectangular form + // and assign to chirp and ichirp + const i2 = 2 * i; + chirp[i2] = result_mod * Math.cos(result_arg); + chirp[i2 + 1] = result_mod * Math.sin(result_arg); + + // conjugate + ichirp[i2] = chirp[i2]; + ichirp[i2 + 1] = - chirp[i2 + 1]; + } + this._slicedChirpBuffer = chirp.subarray(a, b); + + // create object to perform Fast Fourier Transforms + // with `nextP2` complex numbers + this._f = new P2FFT(nextP2 >> 1); + this._f.transform(this._chirpBuffer, ichirp); + } + + _transform(output, input, real) { + const ib1 = this._buffer1; + const ib2 = this._buffer2; + const ob2 = this._outBuffer1; + const ob3 = this._outBuffer2; + const cb = this._chirpBuffer; + const sb = this._slicedChirpBuffer; + const a = this._a; + + if (real) { + // Real multiplication + for (let j = 0; j < sb.length; j += 2) { + const j2 = j + 1 + const j3 = j >> 1; + + const a_real = input[j3]; + ib1[j] = a_real * sb[j]; + ib1[j2] = a_real * sb[j2]; + } + } else { + // Complex multiplication + for (let j = 0; j < sb.length; j += 2) { + const j2 = j + 1 + ib1[j] = input[j] * sb[j] - input[j2] * sb[j2]; + ib1[j2] = input[j] * sb[j2] + input[j2] * sb[j]; + } + } + this._f.transform(ob2, ib1); + + for (let j = 0; j < cb.length; j += 2) { + const j2 = j + 1; + + ib2[j] = ob2[j] * cb[j] - ob2[j2] * cb[j2]; + ib2[j2] = ob2[j] * cb[j2] + ob2[j2] * cb[j]; + } + this._f.inverseTransform(ob3, ib2); + + for (let j = 0; j < ob3.length; j += 2) { + const a_real = ob3[j + a]; + const a_imag = ob3[j + a + 1]; + const b_real = sb[j]; + const b_imag = sb[j + 1]; + + output[j] = a_real * b_real - a_imag * b_imag; + output[j + 1] = a_real * b_imag + a_imag * b_real; + } + } + + transform(output, input) { + this._transform(output, input, false); + } + + realTransform(output, input) { + this._transform(output, input, true); + } +} + +class FFT { + constructor(fft_length) { + this.fft_length = fft_length; + this.isPowerOfTwo = isPowerOfTwo(fft_length); + if (this.isPowerOfTwo) { + this.fft = new P2FFT(fft_length); + this.outputBufferSize = 2 * fft_length; + } else { + this.fft = new NP2FFT(fft_length); + this.outputBufferSize = this.fft.bufferSize; + } + } + + realTransform(out, input) { + this.fft.realTransform(out, input); + } + + transform(out, input) { + this.fft.transform(out, input); + } +} + + +/** + * Performs median filter on the provided data. Padding is done by mirroring the data. + * @param {AnyTypedArray} data The input array + * @param {number} windowSize The window size + */ +function medianFilter(data, windowSize) { + + if (windowSize % 2 === 0 || windowSize <= 0) { + throw new Error('Window size must be a positive odd number'); + } + + // @ts-ignore + const outputArray = new data.constructor(data.length); + + // @ts-ignore + const buffer = new data.constructor(windowSize); // Reusable array for storing values + + const halfWindowSize = Math.floor(windowSize / 2); + + for (let i = 0; i < data.length; ++i) { + let valuesIndex = 0; + + for (let j = -halfWindowSize; j <= halfWindowSize; ++j) { + let index = i + j; + if (index < 0) { + index = Math.abs(index); + } else if (index >= data.length) { + index = 2 * (data.length - 1) - index; + } + + buffer[valuesIndex++] = data[index]; + } + + buffer.sort(); + outputArray[i] = buffer[halfWindowSize]; + } + + return outputArray; +} + +/** + * Helper function to round a number to a given number of decimals + * @param {number} num The number to round + * @param {number} decimals The number of decimals + * @returns {number} The rounded number + */ +function round(num, decimals) { + const pow = Math.pow(10, decimals); + return Math.round(num * pow) / pow; +} + +/** + * Helper function to round a number to the nearest integer, with ties rounded to the nearest even number. + * Also known as "bankers' rounding". This is the default rounding mode in python. For example: + * 1.5 rounds to 2 and 2.5 rounds to 2. + * + * @param {number} x The number to round + * @returns {number} The rounded number + */ +function bankers_round(x) { + const r = Math.round(x); + const br = Math.abs(x) % 1 === 0.5 ? (r % 2 === 0 ? r : r - 1) : r; + return br; +} + + +/** + * Measures similarity between two temporal sequences (e.g., input audio and output tokens + * to generate token-level timestamps). + * @param {number[][]} matrix + * @returns {number[][]} + */ +function dynamic_time_warping(matrix) { + const output_length = matrix.length; + const input_length = matrix[0].length; + + const outputShape = [output_length + 1, input_length + 1]; + + const cost = Array.from( + { length: outputShape[0] }, + () => Array(outputShape[1]).fill(Infinity) + ); + cost[0][0] = 0; + + const trace = Array.from( + { length: outputShape[0] }, + () => Array(outputShape[1]).fill(-1) + ); + + for (let j = 1; j < outputShape[1]; ++j) { + for (let i = 1; i < outputShape[0]; ++i) { + const c0 = cost[i - 1][j - 1]; + const c1 = cost[i - 1][j]; + const c2 = cost[i][j - 1]; + + let c, t; + if (c0 < c1 && c0 < c2) { + c = c0; + t = 0; + } else if (c1 < c0 && c1 < c2) { + c = c1; + t = 1; + } else { + c = c2; + t = 2; + } + cost[i][j] = matrix[i - 1][j - 1] + c; + trace[i][j] = t; + } + } + + for (let i = 0; i < outputShape[1]; ++i) { // trace[0, :] = 2 + trace[0][i] = 2; + } + for (let i = 0; i < outputShape[0]; ++i) { // trace[:, 0] = 1 + trace[i][0] = 1; + } + + // backtrace + let i = output_length; + let j = input_length; + let text_indices = []; + let time_indices = []; + while (i > 0 || j > 0) { + text_indices.push(i - 1); + time_indices.push(j - 1); + + switch (trace[i][j]) { + case 0: + --i; --j; + break; + case 1: + --i; + break; + case 2: + --j; + break; + default: + throw new Error( + `Internal error in dynamic time warping. Unexpected trace[${i}, ${j}]. Please file a bug report.` + ) + } + } + + text_indices.reverse(); + time_indices.reverse(); + + return [text_indices, time_indices]; + +} + + +/***/ }), + +/***/ "./src/utils/tensor.js": +/*!*****************************!*\ + !*** ./src/utils/tensor.js ***! + \*****************************/ +/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ Tensor: () => (/* binding */ Tensor), +/* harmony export */ cat: () => (/* binding */ cat), +/* harmony export */ full: () => (/* binding */ full), +/* harmony export */ full_like: () => (/* binding */ full_like), +/* harmony export */ interpolate: () => (/* binding */ interpolate), +/* harmony export */ interpolate_4d: () => (/* binding */ interpolate_4d), +/* harmony export */ layer_norm: () => (/* binding */ layer_norm), +/* harmony export */ matmul: () => (/* binding */ matmul), +/* harmony export */ mean: () => (/* binding */ mean), +/* harmony export */ mean_pooling: () => (/* binding */ mean_pooling), +/* harmony export */ ones: () => (/* binding */ ones), +/* harmony export */ ones_like: () => (/* binding */ ones_like), +/* harmony export */ permute: () => (/* binding */ permute), +/* harmony export */ quantize_embeddings: () => (/* binding */ quantize_embeddings), +/* harmony export */ rfft: () => (/* binding */ rfft), +/* harmony export */ stack: () => (/* binding */ stack), +/* harmony export */ std_mean: () => (/* binding */ std_mean), +/* harmony export */ topk: () => (/* binding */ topk), +/* harmony export */ zeros: () => (/* binding */ zeros), +/* harmony export */ zeros_like: () => (/* binding */ zeros_like) +/* harmony export */ }); +/* harmony import */ var _maths_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./maths.js */ "./src/utils/maths.js"); +/* harmony import */ var _backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../backends/onnx.js */ "./src/backends/onnx.js"); +/* harmony import */ var _ops_registry_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../ops/registry.js */ "./src/ops/registry.js"); +/** + * @file Helper module for `Tensor` processing. + * + * These functions and classes are only used internally, + * meaning an end-user shouldn't need to access anything here. + * + * @module utils/tensor + */ + + + + + + + +const DataTypeMap = Object.freeze({ + float32: Float32Array, + float16: Uint16Array, + float64: Float64Array, + string: Array, // string[] + int8: Int8Array, + uint8: Uint8Array, + int16: Int16Array, + uint16: Uint16Array, + int32: Int32Array, + uint32: Uint32Array, + int64: BigInt64Array, + uint64: BigUint64Array, + bool: Uint8Array, +}); + +/** + * @typedef {keyof typeof DataTypeMap} DataType + * @typedef {import('./maths.js').AnyTypedArray | any[]} DataArray + */ + + +class Tensor { + /** @type {number[]} Dimensions of the tensor. */ + get dims() { + // @ts-ignore + return this.ort_tensor.dims; + } + set dims(value) { + // FIXME: ONNXTensor declares dims as readonly so one needs to use the constructor() if dims change. + // @ts-ignore + this.ort_tensor.dims = value; + } + + /** @type {DataType} Type of the tensor. */ + get type() { + return this.ort_tensor.type; + }; + + /** @type {DataArray} The data stored in the tensor. */ + get data() { + return this.ort_tensor.data; + } + + /** @type {number} The number of elements in the tensor. */ + get size() { + return this.ort_tensor.size; + }; + + /** @type {string} The location of the tensor data. */ + get location() { + return this.ort_tensor.location; + }; + + ort_tensor; + + /** + * Create a new Tensor or copy an existing Tensor. + * @param {[DataType, DataArray, number[]]|[ONNXTensor]} args + */ + constructor(...args) { + if ((0,_backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.isONNXTensor)(args[0])) { + this.ort_tensor = /** @type {ONNXTensor} */ (args[0]); + } else { + // Create new tensor + this.ort_tensor = new _backends_onnx_js__WEBPACK_IMPORTED_MODULE_1__.Tensor( + /** @type {DataType} */(args[0]), + /** @type {Exclude} */(args[1]), + args[2] + ); + } + + return new Proxy(this, { + get: (obj, key) => { + if (typeof key === 'string') { + let index = Number(key); + if (Number.isInteger(index)) { + // key is an integer (i.e., index) + return obj._getitem(index); + } + } + // @ts-ignore + return obj[key]; + }, + set: (obj, key, value) => { + // TODO allow setting of data + + // @ts-ignore + return obj[key] = value; + } + }); + } + + dispose() { + this.ort_tensor.dispose(); + // this.ort_tensor = undefined; + } + + /** + * Returns an iterator object for iterating over the tensor data in row-major order. + * If the tensor has more than one dimension, the iterator will yield subarrays. + * @returns {Iterator} An iterator object for iterating over the tensor data in row-major order. + */ + *[Symbol.iterator]() { + const [iterLength, ...iterDims] = this.dims; + + if (iterDims.length > 0) { + const iterSize = iterDims.reduce((a, b) => a * b); + for (let i = 0; i < iterLength; ++i) { + yield this._subarray(i, iterSize, iterDims); + } + } else { + yield* this.data + } + + } + + /** + * Index into a Tensor object. + * @param {number} index The index to access. + * @returns {Tensor} The data at the specified index. + */ + _getitem(index) { + const [iterLength, ...iterDims] = this.dims; + + index = safeIndex(index, iterLength); + + if (iterDims.length > 0) { + const iterSize = iterDims.reduce((a, b) => a * b); + return this._subarray(index, iterSize, iterDims); + } else { + return new Tensor(this.type, [this.data[index]], iterDims); + } + } + + /** + * @param {number|bigint} item The item to search for in the tensor + * @returns {number} The index of the first occurrence of item in the tensor data. + */ + indexOf(item) { + const this_data = this.data; + for (let index = 0; index < this_data.length; ++index) { + // Note: == instead of === so we can match Ints with BigInts + if (this_data[index] == item) { + return index; + } + } + return -1; + } + + /** + * @param {number} index + * @param {number} iterSize + * @param {any} iterDims + * @returns {Tensor} + */ + _subarray(index, iterSize, iterDims) { + const o1 = index * iterSize; + const o2 = (index + 1) * iterSize; + + // We use subarray if available (typed array), otherwise we use slice (normal array) + const data = + ('subarray' in this.data) + ? this.data.subarray(o1, o2) + : this.data.slice(o1, o2); + return new Tensor(this.type, data, iterDims); + } + + /** + * Returns the value of this tensor as a standard JavaScript Number. This only works + * for tensors with one element. For other cases, see `Tensor.tolist()`. + * @returns {number|bigint} The value of this tensor as a standard JavaScript Number. + * @throws {Error} If the tensor has more than one element. + */ + item() { + const this_data = this.data; + if (this_data.length !== 1) { + throw new Error(`a Tensor with ${this_data.length} elements cannot be converted to Scalar`); + } + return this_data[0]; + } + + /** + * Convert tensor data to a n-dimensional JS list + * @returns {Array} + */ + tolist() { + return reshape(this.data, this.dims) + } + + /** + * Return a new Tensor with the sigmoid function applied to each element. + * @returns {Tensor} The tensor with the sigmoid function applied. + */ + sigmoid() { + return this.clone().sigmoid_(); + } + + /** + * Applies the sigmoid function to the tensor in place. + * @returns {Tensor} Returns `this`. + */ + sigmoid_() { + const this_data = this.data; + for (let i = 0; i < this_data.length; ++i) { + this_data[i] = 1 / (1 + Math.exp(-this_data[i])); + } + return this; + } + + /** + * Return a new Tensor with a callback function applied to each element. + * @param {Function} callback - The function to apply to each element. It should take three arguments: + * the current element, its index, and the tensor's data array. + * @returns {Tensor} A new Tensor with the callback function applied to each element. + */ + map(callback) { + return this.clone().map_(callback); + } + + /** + * Apply a callback function to each element of the tensor in place. + * @param {Function} callback - The function to apply to each element. It should take three arguments: + * the current element, its index, and the tensor's data array. + * @returns {Tensor} Returns `this`. + */ + map_(callback) { + const this_data = this.data; + for (let i = 0; i < this_data.length; ++i) { + this_data[i] = callback(this_data[i], i, this_data); + } + return this; + } + + /** + * Return a new Tensor with every element multiplied by a constant. + * @param {number} val The value to multiply by. + * @returns {Tensor} The new tensor. + */ + mul(val) { + return this.clone().mul_(val); + } + + /** + * Multiply the tensor by a constant in place. + * @param {number} val The value to multiply by. + * @returns {Tensor} Returns `this`. + */ + mul_(val) { + const this_data = this.data; + for (let i = 0; i < this_data.length; ++i) { + this_data[i] *= val; + } + return this; + } + + /** + * Return a new Tensor with every element divided by a constant. + * @param {number} val The value to divide by. + * @returns {Tensor} The new tensor. + */ + div(val) { + return this.clone().div_(val); + } + + /** + * Divide the tensor by a constant in place. + * @param {number} val The value to divide by. + * @returns {Tensor} Returns `this`. + */ + div_(val) { + const this_data = this.data; + for (let i = 0; i < this_data.length; ++i) { + this_data[i] /= val; + } + return this; + } + + /** + * Return a new Tensor with every element added by a constant. + * @param {number} val The value to add by. + * @returns {Tensor} The new tensor. + */ + add(val) { + return this.clone().add_(val); + } + + /** + * Add the tensor by a constant in place. + * @param {number} val The value to add by. + * @returns {Tensor} Returns `this`. + */ + add_(val) { + const this_data = this.data; + for (let i = 0; i < this_data.length; ++i) { + this_data[i] += val; + } + return this; + } + + /** + * Return a new Tensor with every element subtracted by a constant. + * @param {number} val The value to subtract by. + * @returns {Tensor} The new tensor. + */ + sub(val) { + return this.clone().sub_(val); + } + + /** + * Subtract the tensor by a constant in place. + * @param {number} val The value to subtract by. + * @returns {Tensor} Returns `this`. + */ + sub_(val) { + const this_data = this.data; + for (let i = 0; i < this_data.length; ++i) { + this_data[i] -= val; + } + return this; + } + + clone() { + return new Tensor(this.type, this.data.slice(), this.dims.slice()); + } + + slice(...slices) { + // This allows for slicing with ranges and numbers + const newTensorDims = []; + const newOffsets = []; + + // slices is an array of numbers or arrays of numbers + // e.g., slices = [0, [1, 3], null, [0, 3]] + for (let sliceIndex = 0; sliceIndex < this.dims.length; ++sliceIndex) { + let slice = slices[sliceIndex]; + + if (slice === null || slice === undefined) { + // null or undefined means take the whole dimension + newOffsets.push([0, this.dims[sliceIndex]]); + newTensorDims.push(this.dims[sliceIndex]); + + } else if (typeof slice === 'number') { + slice = safeIndex(slice, this.dims[sliceIndex], sliceIndex); + + // A number means take a single element + newOffsets.push([slice, slice + 1]); + + } else if (Array.isArray(slice) && slice.length === 2) { + // An array of length 2 means take a range of elements + let [start, end] = slice; + start = start === null + ? 0 + : safeIndex(start, this.dims[sliceIndex], sliceIndex, false); + end = end === null + ? this.dims[sliceIndex] + : safeIndex(end, this.dims[sliceIndex], sliceIndex, false); + + if (start > end) { + throw new Error(`Invalid slice: ${slice}`); + } + + const offsets = [ + Math.max(start, 0), + Math.min(end, this.dims[sliceIndex]) + ]; + + newOffsets.push(offsets); + newTensorDims.push(offsets[1] - offsets[0]); + + } else { + throw new Error(`Invalid slice: ${slice}`); + } + } + + const newDims = newOffsets.map(([start, end]) => end - start); + const newBufferSize = newDims.reduce((a, b) => a * b); + + const this_data = this.data; + // Allocate memory + // @ts-ignore + const data = new this_data.constructor(newBufferSize); + + // Precompute strides + const stride = this.stride(); + + for (let i = 0; i < newBufferSize; ++i) { + let originalIndex = 0; + for (let j = newDims.length - 1, num = i; j >= 0; --j) { + const size = newDims[j]; + originalIndex += ((num % size) + newOffsets[j][0]) * stride[j]; + num = Math.floor(num / size); + } + data[i] = this_data[originalIndex]; + } + return new Tensor(this.type, data, newTensorDims); + + } + + /** + * Return a permuted version of this Tensor, according to the provided dimensions. + * @param {...number} dims Dimensions to permute. + * @returns {Tensor} The permuted tensor. + */ + permute(...dims) { + return permute(this, dims); + } + + // TODO: implement transpose. For now (backwards compatibility), it's just an alias for permute() + transpose(...dims) { + return this.permute(...dims); + } + + // TODO add .max() and .min() methods + + /** + * Returns the sum of each row of the input tensor in the given dimension dim. + * + * @param {number} [dim=null] The dimension or dimensions to reduce. If `null`, all dimensions are reduced. + * @param {boolean} keepdim Whether the output tensor has `dim` retained or not. + * @returns The summed tensor + */ + sum(dim = null, keepdim = false) { + return this.norm(1, dim, keepdim); + } + + /** + * Returns the matrix norm or vector norm of a given tensor. + * @param {number|string} [p='fro'] The order of norm + * @param {number} [dim=null] Specifies which dimension of the tensor to calculate the norm across. + * If dim is None, the norm will be calculated across all dimensions of input. + * @param {boolean} [keepdim=false] Whether the output tensors have dim retained or not. + * @returns {Tensor} The norm of the tensor. + */ + norm(p = 'fro', dim = null, keepdim = false) { + if (p === 'fro') { + // NOTE: Since we only support integer dims, Frobenius norm produces the same result as p=2. + p = 2; + } else if (typeof p === 'string') { + throw Error(`Unsupported norm: ${p}`); + } + + const this_data = this.data; + + if (dim === null) { + // @ts-ignore + let val = this_data.reduce((a, b) => a + (b ** p), 0) ** (1 / p); + return new Tensor(this.type, [val], []); + } + + // Negative indexing + dim = safeIndex(dim, this.dims.length); + + // Calculate the shape of the resulting array after summation + const resultDims = this.dims.slice(); // Copy the original dimensions + resultDims[dim] = 1; // Remove the specified axis + + // Create a new array to store the accumulated values + // @ts-ignore + const result = new this_data.constructor(this_data.length / this.dims[dim]); + + // Iterate over the data array + for (let i = 0; i < this_data.length; ++i) { + + // Calculate the index in the resulting array + let resultIndex = 0; + + for (let j = this.dims.length - 1, num = i, resultMultiplier = 1; j >= 0; --j) { + const size = this.dims[j]; + if (j !== dim) { + const index = num % size; + resultIndex += index * resultMultiplier; + resultMultiplier *= resultDims[j]; + } + num = Math.floor(num / size); + } + + // Accumulate the value at the current index + result[resultIndex] += (this_data[i]) ** p; + } + + if (p !== 1) { + for (let i = 0; i < result.length; ++i) { + result[i] = result[i] ** (1 / p); + } + } + + if (!keepdim) { + resultDims.splice(dim, 1); + } + + return new Tensor(this.type, result, resultDims); + } + + /** + * Performs `L_p` normalization of inputs over specified dimension. Operates in place. + * @param {number} [p=2] The exponent value in the norm formulation + * @param {number} [dim=1] The dimension to reduce + * @returns {Tensor} `this` for operation chaining. + */ + normalize_(p = 2.0, dim = 1) { + dim = safeIndex(dim, this.dims.length); + + const norm = this.norm(p, dim, true); + + const this_data = this.data; + const norm_data = norm.data; + for (let i = 0; i < this_data.length; ++i) { + + // Calculate the index in the resulting array + let resultIndex = 0; + + for (let j = this.dims.length - 1, num = i, resultMultiplier = 1; j >= 0; --j) { + const size = this.dims[j]; + if (j !== dim) { + const index = num % size; + resultIndex += index * resultMultiplier; + resultMultiplier *= this.dims[j]; + } + num = Math.floor(num / size); + } + + // Divide by normalized value + this_data[i] /= norm_data[resultIndex]; + } + + return this; + } + + /** + * Performs `L_p` normalization of inputs over specified dimension. + * @param {number} [p=2] The exponent value in the norm formulation + * @param {number} [dim=1] The dimension to reduce + * @returns {Tensor} The normalized tensor. + */ + normalize(p = 2.0, dim = 1) { + return this.clone().normalize_(p, dim); + } + + /** + * Compute and return the stride of this tensor. + * Stride is the jump necessary to go from one element to the next one in the specified dimension dim. + * @returns {number[]} The stride of this tensor. + */ + stride() { + return dimsToStride(this.dims); + } + + /** + * Returns a tensor with all specified dimensions of input of size 1 removed. + * + * NOTE: The returned tensor shares the storage with the input tensor, so changing the contents of one will change the contents of the other. + * If you would like a copy, use `tensor.clone()` before squeezing. + * + * @param {number} [dim=null] If given, the input will be squeezed only in the specified dimensions. + * @returns {Tensor} The squeezed tensor + */ + squeeze(dim = null) { + return new Tensor( + this.type, + this.data, + calc_squeeze_dims(this.dims, dim) + ) + } + + /** + * In-place version of @see {@link Tensor.squeeze} + */ + squeeze_(dim = null) { + this.dims = calc_squeeze_dims(this.dims, dim); + return this; + } + + /** + * Returns a new tensor with a dimension of size one inserted at the specified position. + * + * NOTE: The returned tensor shares the same underlying data with this tensor. + * + * @param {number} dim The index at which to insert the singleton dimension + * @returns {Tensor} The unsqueezed tensor + */ + unsqueeze(dim = null) { + return new Tensor( + this.type, + this.data, + calc_unsqueeze_dims(this.dims, dim) + ); + } + + /** + * In-place version of @see {@link Tensor.unsqueeze} + */ + unsqueeze_(dim = null) { + this.dims = calc_unsqueeze_dims(this.dims, dim); + return this; + } + + /** + * In-place version of @see {@link Tensor.flatten} + */ + flatten_(start_dim = 0, end_dim = -1) { + // TODO validate inputs + end_dim = (end_dim + this.dims.length) % this.dims.length; + + let dimsToKeepBefore = this.dims.slice(0, start_dim); + let dimsToFlatten = this.dims.slice(start_dim, end_dim + 1); + let dimsToKeepAfter = this.dims.slice(end_dim + 1); + + this.dims = [...dimsToKeepBefore, dimsToFlatten.reduce((a, b) => a * b, 1), ...dimsToKeepAfter] + return this; + } + + /** + * Flattens input by reshaping it into a one-dimensional tensor. + * If `start_dim` or `end_dim` are passed, only dimensions starting with `start_dim` + * and ending with `end_dim` are flattened. The order of elements in input is unchanged. + * @param {number} start_dim the first dim to flatten + * @param {number} end_dim the last dim to flatten + * @returns {Tensor} The flattened tensor. + */ + flatten(start_dim = 0, end_dim = -1) { + return this.clone().flatten_(start_dim, end_dim); + } + + /** + * Returns a new tensor with the same data as the `self` tensor but of a different `shape`. + * @param {...number} dims the desired size + * @returns {Tensor} The tensor with the same data but different shape + */ + view(...dims) { + // TODO: validate dims + let inferredIndex = -1; + for (let i = 0; i < dims.length; ++i) { + if (dims[i] === -1) { + if (inferredIndex !== -1) { + throw new Error("Only one dimension can be inferred"); + } + inferredIndex = i; + } + } + + const this_data = this.data; + if (inferredIndex !== -1) { + // Some dimension must be inferred + const productOther = dims.reduce((product, curr, index) => { + return index !== inferredIndex ? product * curr : product + }, 1); + + dims[inferredIndex] = this_data.length / productOther; + } + return new Tensor(this.type, this_data, dims); // NOTE: uses same underlying storage + } + + neg_() { + const this_data = this.data; + for (let i = 0; i < this_data.length; ++i) { + this_data[i] = -this_data[i]; + } + return this; + } + neg() { + return this.clone().neg_(); + } + + /** + * In-place version of @see {@link Tensor.clamp} + */ + clamp_(min, max) { + const this_data = this.data; + for (let i = 0; i < this_data.length; ++i) { + this_data[i] = Math.min(Math.max(this_data[i], min), max); + } + return this; + } + + /** + * Clamps all elements in input into the range [ min, max ] + * @param {number} min lower-bound of the range to be clamped to + * @param {number} max upper-bound of the range to be clamped to + * @returns {Tensor} the output tensor. + */ + clamp(min, max) { + return this.clone().clamp_(min, max); + } + + /** + * In-place version of @see {@link Tensor.round} + */ + round_() { + const this_data = this.data; + for (let i = 0; i < this_data.length; ++i) { + this_data[i] = Math.round(this_data[i]); + } + return this; + } + + /** + * Rounds elements of input to the nearest integer. + * @returns {Tensor} the output tensor. + */ + round() { + return this.clone().round_(); + } + + mean(dim = null, keepdim = false) { + return mean(this, dim, keepdim); + } + + /** + * Performs Tensor dtype conversion. + * @param {DataType} type The desired data type. + * @returns {Tensor} The converted tensor. + */ + to(type) { + // If the self Tensor already has the correct dtype, then self is returned. + if (this.type === type) return this; + + // Otherwise, the returned tensor is a copy of self with the desired dtype. + if (!DataTypeMap.hasOwnProperty(type)) { + throw new Error(`Unsupported type: ${type}`); + } + // @ts-ignore + return new Tensor(type, DataTypeMap[type].from(this.data), this.dims); + } +} + +/** + * This creates a nested array of a given type and depth (see examples). + * + * @example + * NestArray; // string[] + * @example + * NestArray; // number[][] + * @example + * NestArray; // string[][][] etc. + * @template T + * @template {number} Depth + * @template {never[]} [Acc=[]] + * @typedef {Acc['length'] extends Depth ? T : NestArray} NestArray + */ + +/** + * Reshapes a 1-dimensional array into an n-dimensional array, according to the provided dimensions. + * + * @example + * reshape([10 ], [1 ]); // Type: number[] Value: [10] + * reshape([1, 2, 3, 4 ], [2, 2 ]); // Type: number[][] Value: [[1, 2], [3, 4]] + * reshape([1, 2, 3, 4, 5, 6, 7, 8], [2, 2, 2]); // Type: number[][][] Value: [[[1, 2], [3, 4]], [[5, 6], [7, 8]]] + * reshape([1, 2, 3, 4, 5, 6, 7, 8], [4, 2 ]); // Type: number[][] Value: [[1, 2], [3, 4], [5, 6], [7, 8]] + * @param {T[]|DataArray} data The input array to reshape. + * @param {DIM} dimensions The target shape/dimensions. + * @template T + * @template {[number]|number[]} DIM + * @returns {NestArray} The reshaped array. + */ +function reshape(data, dimensions) { + + const totalElements = data.length; + const dimensionSize = dimensions.reduce((a, b) => a * b); + + if (totalElements !== dimensionSize) { + throw Error(`cannot reshape array of size ${totalElements} into shape (${dimensions})`); + } + + /** @type {any} */ + let reshapedArray = data; + + for (let i = dimensions.length - 1; i >= 0; i--) { + reshapedArray = reshapedArray.reduce((acc, val) => { + let lastArray = acc[acc.length - 1]; + + if (lastArray.length < dimensions[i]) { + lastArray.push(val); + } else { + acc.push([val]); + } + + return acc; + }, [[]]); + } + + return reshapedArray[0]; +} + +/** + * Permutes a tensor according to the provided axes. + * @param {any} tensor The input tensor to permute. + * @param {Array} axes The axes to permute the tensor along. + * @returns {Tensor} The permuted tensor. + */ +function permute(tensor, axes) { + const [permutedData, shape] = (0,_maths_js__WEBPACK_IMPORTED_MODULE_0__.permute_data)(tensor.data, tensor.dims, axes); + return new Tensor(tensor.type, permutedData, shape); +} + + +/** + * Interpolates an Tensor to the given size. + * @param {Tensor} input The input tensor to interpolate. Data must be channel-first (i.e., [c, h, w]) + * @param {number[]} size The output size of the image + * @param {string} mode The interpolation mode + * @param {boolean} align_corners Whether to align corners. + * @returns {Tensor} The interpolated tensor. + */ +function interpolate(input, [out_height, out_width], mode = 'bilinear', align_corners = false) { + + // Input image dimensions + const in_channels = input.dims.at(-3) ?? 1; + const in_height = input.dims.at(-2); + const in_width = input.dims.at(-1); + + let output = (0,_maths_js__WEBPACK_IMPORTED_MODULE_0__.interpolate_data)( + /** @type {import('./maths.js').TypedArray}*/(input.data), + [in_channels, in_height, in_width], + [out_height, out_width], + mode, + align_corners + ); + return new Tensor(input.type, output, [in_channels, out_height, out_width]); +} + + +/** + * Down/up samples the input. + * Inspired by https://pytorch.org/docs/stable/generated/torch.nn.functional.interpolate.html. + * @param {Tensor} input the input tensor + * @param {Object} options the options for the interpolation + * @param {[number, number]|[number, number, number]|[number, number, number, number]} [options.size=null] output spatial size. + * @param {"bilinear"|"bicubic"} [options.mode='bilinear'] algorithm used for upsampling + * @returns {Promise} The interpolated tensor. + */ +async function interpolate_4d(input, { + size = null, + mode = 'bilinear', +} = {}) { + + // Error checking + if (input.dims.length !== 4) { + throw new Error('`interpolate_4d` currently only supports 4D input.'); + } + if (!size) { + // TODO: support scale_factor + throw new Error('`interpolate_4d` requires a `size` argument.'); + } + + // Fill in missing dimensions + let targetDims; + if (size.length === 2) { + targetDims = [...input.dims.slice(0, 2), ...size]; + } else if (size.length === 3) { + targetDims = [input.dims[0], ...size]; + } else if (size.length === 4) { + targetDims = size; + } else { + throw new Error('`size` must be of length 2, 3, or 4.'); + } + + let op; + if (mode === 'bilinear') { + op = await _ops_registry_js__WEBPACK_IMPORTED_MODULE_2__.TensorOpRegistry.bilinear_interpolate_4d; + } else if (mode === 'bicubic') { + op = await _ops_registry_js__WEBPACK_IMPORTED_MODULE_2__.TensorOpRegistry.bicubic_interpolate_4d; + } else { + throw new Error(`Unsupported mode: ${mode}`); + } + + const sizeTensor = new Tensor('int64', new BigInt64Array(targetDims.map(BigInt)), [targetDims.length]); + return await op({ x: input, s: sizeTensor }); +} + +/** + * Matrix product of two tensors. + * Inspired by https://pytorch.org/docs/stable/generated/torch.matmul.html + * @param {Tensor} a the first tensor to be multiplied + * @param {Tensor} b the second tensor to be multiplied + * @returns {Promise} The matrix product of the two tensors. + */ +async function matmul(a, b) { + const op = await _ops_registry_js__WEBPACK_IMPORTED_MODULE_2__.TensorOpRegistry.matmul; + return await op({ a, b }); +} + +/** + * Computes the one dimensional Fourier transform of real-valued input. + * Inspired by https://pytorch.org/docs/stable/generated/torch.fft.rfft.html + * @param {Tensor} x the real input tensor + * @param {Tensor} a The dimension along which to take the one dimensional real FFT. + * @returns {Promise} the output tensor. + */ +async function rfft(x, a) { + const op = await _ops_registry_js__WEBPACK_IMPORTED_MODULE_2__.TensorOpRegistry.rfft; + return await op({ x, a }); +} + + +/** + * Returns the k largest elements of the given input tensor. + * Inspired by https://pytorch.org/docs/stable/generated/torch.topk.html + * @param {Tensor} x the input tensor + * @param {number} k the k in "top-k" + * @returns {Promise<[Tensor, Tensor]>} the output tuple of (Tensor, LongTensor) of top-k elements and their indices. + */ +async function topk(x, k) { + const op = await _ops_registry_js__WEBPACK_IMPORTED_MODULE_2__.TensorOpRegistry.top_k; + + if (k === null) { + k = x.dims.at(-1); + } else { + k = Math.min(k, x.dims.at(-1)); + } + return await op({ + x, + k: new Tensor( + 'int64', + [BigInt(k)], + [1] + ) + }); +} + +/** + * Perform mean pooling of the last hidden state followed by a normalization step. + * @param {Tensor} last_hidden_state Tensor of shape [batchSize, seqLength, embedDim] + * @param {Tensor} attention_mask Tensor of shape [batchSize, seqLength] + * @returns {Tensor} Returns a new Tensor of shape [batchSize, embedDim]. + */ +function mean_pooling(last_hidden_state, attention_mask) { + // last_hidden_state: [batchSize, seqLength, embedDim] + // attention_mask: [batchSize, seqLength] + const lastHiddenStateData = last_hidden_state.data; + const attentionMaskData = attention_mask.data; + + const shape = [last_hidden_state.dims[0], last_hidden_state.dims[2]]; + + // @ts-ignore + const returnedData = new lastHiddenStateData.constructor(shape[0] * shape[1]); + const [batchSize, seqLength, embedDim] = last_hidden_state.dims; + + let outIndex = 0; + for (let i = 0; i < batchSize; ++i) { + const offset = i * embedDim * seqLength; + + for (let k = 0; k < embedDim; ++k) { + let sum = 0; + let count = 0; + + const attnMaskOffset = i * seqLength; + const offset2 = offset + k; + // Pool over all words in sequence + for (let j = 0; j < seqLength; ++j) { + // index into attention mask + const attn = Number(attentionMaskData[attnMaskOffset + j]); + + count += attn; + sum += lastHiddenStateData[offset2 + j * embedDim] * attn; + } + + const avg = sum / count; + returnedData[outIndex++] = avg; + } + } + + return new Tensor( + last_hidden_state.type, + returnedData, + shape + ) +} + +/** + * Apply Layer Normalization for last certain number of dimensions. + * @param {Tensor} input The input tensor + * @param {number[]} normalized_shape input shape from an expected input of size + * @param {Object} options The options for the layer normalization + * @param {number} [options.eps=1e-5] A value added to the denominator for numerical stability. + * @returns {Tensor} The normalized tensor. + */ +function layer_norm(input, normalized_shape, { + eps = 1e-5, +} = {}) { + if (input.dims.length !== 2) { + throw new Error('`layer_norm` currently only supports 2D input.'); + } + + const [batchSize, featureDim] = input.dims; + + if (normalized_shape.length !== 1 && normalized_shape[0] !== featureDim) { + throw new Error('`normalized_shape` must be a 1D array with shape `[input.dims[1]]`.'); + } + + const [std, mean] = std_mean(input, 1, 0, true); + const stdData = /** @type {Float32Array} */(std.data); + const meanData = /** @type {Float32Array} */(mean.data); + + const inputData = /** @type {Float32Array} */(input.data); + + // @ts-ignore + const returnedData = new inputData.constructor(inputData.length); + + for (let i = 0; i < batchSize; ++i) { + const offset = i * featureDim; + for (let j = 0; j < featureDim; ++j) { + const offset2 = offset + j; + returnedData[offset2] = (inputData[offset2] - meanData[i]) / (stdData[i] + eps); + } + } + return new Tensor(input.type, returnedData, input.dims); +} + +/** + * Helper function to calculate new dimensions when performing a squeeze operation. + * @param {number[]} dims The dimensions of the tensor. + * @param {number|number[]|null} dim The dimension(s) to squeeze. + * @returns {number[]} The new dimensions. + * @private + */ +function calc_squeeze_dims(dims, dim) { + dims = dims.slice(); + if (dim === null) { + dims = dims.filter((d) => d !== 1); + } else if (typeof dim === 'number') { + if (dims[dim] === 1) { + dims.splice(dim, 1); + } + } else if (Array.isArray(dim)) { + dims = dims.filter((x, i) => { + return x !== 1 || !dim.includes(i); + }); + } + return dims; +} + +/** + * Helper function to calculate new dimensions when performing an unsqueeze operation. + * @param {number[]} dims The dimensions of the tensor. + * @param {number} dim The dimension to unsqueeze. + * @returns {number[]} The new dimensions. + * @private + */ +function calc_unsqueeze_dims(dims, dim) { + // Dimension out of range (e.g., "expected to be in range of [-4, 3], but got 4") + // + 1 since we allow inserting at the end (i.e. dim = -1) + dim = safeIndex(dim, dims.length + 1); + dims = dims.slice(); + // Insert 1 into specified dimension + dims.splice(dim, 0, 1); + return dims; +} + +/** + * Safely calculate the index for an array of a given size, allowing negative indexing. + * @param {number} index The index that will be used. + * @param {number} size The size of the array. + * @param {number} [dimension=null] The dimension that the index is for (optional). + * @returns {number} The index, guaranteed to be non-negative and less than `arrayLength`. + * + * @throws {Error} If the index is out of range. + * @private + */ +function safeIndex(index, size, dimension = null, boundsCheck = true) { + if (boundsCheck && (index < -size || index >= size)) { + throw new Error(`IndexError: index ${index} is out of bounds for dimension${dimension === null ? '' : ' ' + dimension} with size ${size}`); + } + + if (index < 0) { + // Negative indexing, ensuring positive index + index = ((index % size) + size) % size; + } + return index; +} + +/** + * Concatenates an array of tensors along a specified dimension. + * @param {Tensor[]} tensors The array of tensors to concatenate. + * @param {number} dim The dimension to concatenate along. + * @returns {Tensor} The concatenated tensor. + */ +function cat(tensors, dim = 0) { + dim = safeIndex(dim, tensors[0].dims.length); + + // TODO do validation of shapes + + const resultDims = tensors[0].dims.slice(); + resultDims[dim] = tensors.reduce((a, b) => a + b.dims[dim], 0); + + // Create a new array to store the accumulated values + const resultSize = resultDims.reduce((a, b) => a * b, 1); + // @ts-ignore + const result = new tensors[0].data.constructor(resultSize); + + // Create output tensor of same type as first + const resultType = tensors[0].type; + + if (dim === 0) { + // Handle special case for performance reasons + + let offset = 0; + for (const tensor of tensors) { + const tensorData = tensor.data; + result.set(tensorData, offset); + offset += tensorData.length; + } + + } else { + + let currentDim = 0; + + for (let t = 0; t < tensors.length; ++t) { + const { data, dims } = tensors[t]; + + // Iterate over the data array + for (let i = 0; i < data.length; ++i) { + // Calculate the index in the resulting array + let resultIndex = 0; + + for (let j = dims.length - 1, num = i, resultMultiplier = 1; j >= 0; --j) { + const size = dims[j]; + let index = num % size; + if (j === dim) { + index += currentDim; + } + resultIndex += index * resultMultiplier; + resultMultiplier *= resultDims[j]; + num = Math.floor(num / size); + } + // Accumulate the value at the current index + result[resultIndex] = data[i]; + } + + currentDim += dims[dim]; + } + } + return new Tensor(resultType, result, resultDims); +} + +/** + * Stack an array of tensors along a specified dimension. + * @param {Tensor[]} tensors The array of tensors to stack. + * @param {number} dim The dimension to stack along. + * @returns {Tensor} The stacked tensor. + */ +function stack(tensors, dim = 0) { + // TODO do validation of shapes + // NOTE: stack expects each tensor to be equal size + return cat(tensors.map(t => t.unsqueeze(dim)), dim); +} + + +/** + * Calculates the standard deviation and mean over the dimensions specified by dim. dim can be a single dimension or `null` to reduce over all dimensions. + * @param {Tensor} input the input tenso + * @param {number|null} dim the dimension to reduce. If None, all dimensions are reduced. + * @param {number} correction difference between the sample size and sample degrees of freedom. Defaults to Bessel's correction, correction=1. + * @param {boolean} keepdim whether the output tensor has dim retained or not. + * @returns {Tensor[]} A tuple of (std, mean) tensors. + */ +function std_mean(input, dim = null, correction = 1, keepdim = false) { + const inputData = /** @type {Float32Array} */(input.data); + const inputDims = input.dims; + + if (dim === null) { + // None to reduce over all dimensions. + const sum = inputData.reduce((a, b) => a + b, 0); + const mean = sum / inputData.length; + const std = Math.sqrt(inputData.reduce((a, b) => a + (b - mean) ** 2, 0) / (inputData.length - correction)); + + const meanTensor = new Tensor(input.type, [mean], [/* scalar */]); + const stdTensor = new Tensor(input.type, [std], [/* scalar */]); + + return [stdTensor, meanTensor]; + } + + // Negative indexing + dim = safeIndex(dim, inputDims.length); + + const meanTensor = mean(input, dim, keepdim); + const meanTensorData = meanTensor.data; + + // Calculate the shape of the resulting array after summation + const resultDims = inputDims.slice(); // Copy the original dimensions + resultDims[dim] = 1; // Remove the specified axis + + // Create a new array to store the accumulated values + // @ts-ignore + const result = new inputData.constructor(inputData.length / inputDims[dim]); + + // Iterate over the data array + for (let i = 0; i < inputData.length; ++i) { + + // Calculate the index in the resulting array + let resultIndex = 0; + + for (let j = inputDims.length - 1, num = i, resultMultiplier = 1; j >= 0; --j) { + const size = inputDims[j]; + if (j !== dim) { + const index = num % size; + resultIndex += index * resultMultiplier; + resultMultiplier *= resultDims[j]; + } + num = Math.floor(num / size); + } + + // Accumulate the value at the current index + result[resultIndex] += (inputData[i] - meanTensorData[resultIndex]) ** 2; + } + + for (let i = 0; i < result.length; ++i) { + result[i] = Math.sqrt(result[i] / (inputDims[dim] - correction)); + } + + if (!keepdim) { + resultDims.splice(dim, 1); + } + + const stdTensor = new Tensor(input.type, result, resultDims); + + return [stdTensor, meanTensor]; +} + + +/** + * Returns the mean value of each row of the input tensor in the given dimension dim. + * @param {Tensor} input the input tensor. + * @param {number|null} dim the dimension to reduce. + * @param {boolean} keepdim whether the output tensor has dim retained or not. + * @returns {Tensor} A new tensor with means taken along the specified dimension. + */ +function mean(input, dim = null, keepdim = false) { + const inputData = /** @type {Float32Array} */(input.data); + + if (dim === null) { + // None to reduce over all dimensions. + // @ts-ignore + const val = inputData.reduce((a, b) => a + b, 0); + return new Tensor(input.type, [val / inputData.length], [/* scalar */]); + } + const inputDims = input.dims; + + // Negative indexing + dim = safeIndex(dim, inputDims.length); + + // Calculate the shape of the resulting array after summation + const resultDims = inputDims.slice(); // Copy the original dimensions + resultDims[dim] = 1; // Remove the specified axis + + // Create a new array to store the accumulated values + // @ts-ignore + const result = new inputData.constructor(inputData.length / inputDims[dim]); + + // Iterate over the data array + for (let i = 0; i < inputData.length; ++i) { + + // Calculate the index in the resulting array + let resultIndex = 0; + + for (let j = inputDims.length - 1, num = i, resultMultiplier = 1; j >= 0; --j) { + const size = inputDims[j]; + if (j !== dim) { + const index = num % size; + resultIndex += index * resultMultiplier; + resultMultiplier *= resultDims[j]; + } + num = Math.floor(num / size); + } + + // Accumulate the value at the current index + result[resultIndex] += inputData[i]; + } + + if (inputDims[dim] !== 1) { + for (let i = 0; i < result.length; ++i) { + result[i] = result[i] / inputDims[dim]; + } + } + + if (!keepdim) { + resultDims.splice(dim, 1); + } + + return new Tensor(input.type, result, resultDims); +} + + +function dimsToStride(dims) { + const stride = new Array(dims.length); + for (let i = dims.length - 1, s2 = 1; i >= 0; --i) { + stride[i] = s2; + s2 *= dims[i]; + } + return stride; +} + +function fullHelper(size, fill_value, dtype, cls) { + const numElements = size.reduce((a, b) => a * b, 1); + return new Tensor( + dtype, + new cls(numElements).fill(fill_value), + size + ) +} + +/** + * Creates a tensor of size size filled with fill_value. The tensor's dtype is inferred from fill_value. + * @param {number[]} size A sequence of integers defining the shape of the output tensor. + * @param {number|bigint} fill_value The value to fill the output tensor with. + * @returns {Tensor} The filled tensor. + */ +function full(size, fill_value) { + let dtype; + let typedArrayCls; + if (typeof fill_value === 'number') { + dtype = 'float32'; + typedArrayCls = Float32Array; + } else if (typeof fill_value === 'bigint') { + dtype = 'int64'; + typedArrayCls = BigInt64Array; + } else { + // TODO: support other dtypes + throw new Error(`Unsupported data type: ${typeof fill_value}`); + } + return fullHelper(size, fill_value, dtype, typedArrayCls); +} + +function full_like(tensor, fill_value) { + return full(tensor.dims, fill_value); +} + +/** + * Returns a tensor filled with the scalar value 1, with the shape defined by the variable argument size. + * @param {number[]} size A sequence of integers defining the shape of the output tensor. + * @returns {Tensor} The ones tensor. + */ +function ones(size) { + return fullHelper(size, 1n, 'int64', BigInt64Array); +} + +/** + * Returns a tensor filled with the scalar value 1, with the same size as input. + * @param {Tensor} tensor The size of input will determine size of the output tensor. + * @returns {Tensor} The ones tensor. + */ +function ones_like(tensor) { + return ones(tensor.dims); +} + +/** + * Returns a tensor filled with the scalar value 0, with the shape defined by the variable argument size. + * @param {number[]} size A sequence of integers defining the shape of the output tensor. + * @returns {Tensor} The zeros tensor. + */ +function zeros(size) { + return fullHelper(size, 0n, 'int64', BigInt64Array); +} + +/** + * Returns a tensor filled with the scalar value 0, with the same size as input. + * @param {Tensor} tensor The size of input will determine size of the output tensor. + * @returns {Tensor} The zeros tensor. + */ +function zeros_like(tensor) { + return zeros(tensor.dims); +} + +/** + * Quantizes the embeddings tensor to binary or unsigned binary precision. + * @param {Tensor} tensor The tensor to quantize. + * @param {'binary'|'ubinary'} precision The precision to use for quantization. + * @returns {Tensor} The quantized tensor. + */ +function quantize_embeddings(tensor, precision) { + if (tensor.dims.length !== 2) { + throw new Error("The tensor must have 2 dimensions"); + } + if (tensor.dims.at(-1) % 8 !== 0) { + throw new Error("The last dimension of the tensor must be a multiple of 8"); + } + if (!['binary', 'ubinary'].includes(precision)) { + throw new Error("The precision must be either 'binary' or 'ubinary'"); + } + + const signed = precision === 'binary'; + const dtype = signed ? 'int8' : 'uint8'; + + // Create a typed array to store the packed bits + const cls = signed ? Int8Array : Uint8Array; + const inputData = tensor.data; + const outputData = new cls(inputData.length / 8); + + // Iterate over each number in the array + for (let i = 0; i < inputData.length; ++i) { + // Determine if the number is greater than 0 + const bit = inputData[i] > 0 ? 1 : 0; + + // Calculate the index in the typed array and the position within the byte + const arrayIndex = Math.floor(i / 8); + const bitPosition = i % 8; + + // Pack the bit into the typed array + outputData[arrayIndex] |= bit << (7 - bitPosition); + if (signed && bitPosition === 0) { + outputData[arrayIndex] -= 128; + } + }; + + return new Tensor(dtype, outputData, [tensor.dims[0], tensor.dims[1] / 8]); +} + + +/***/ }) + +/******/ }); +/************************************************************************/ +/******/ // The module cache +/******/ var __webpack_module_cache__ = {}; +/******/ +/******/ // The require function +/******/ function __webpack_require__(moduleId) { +/******/ // Check if module is in cache +/******/ var cachedModule = __webpack_module_cache__[moduleId]; +/******/ if (cachedModule !== undefined) { +/******/ return cachedModule.exports; +/******/ } +/******/ // Create a new module (and put it into the cache) +/******/ var module = __webpack_module_cache__[moduleId] = { +/******/ // no module.id needed +/******/ // no module.loaded needed +/******/ exports: {} +/******/ }; +/******/ +/******/ // Execute the module function +/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__); +/******/ +/******/ // Return the exports of the module +/******/ return module.exports; +/******/ } +/******/ +/************************************************************************/ +/******/ /* webpack/runtime/create fake namespace object */ +/******/ (() => { +/******/ var getProto = Object.getPrototypeOf ? (obj) => (Object.getPrototypeOf(obj)) : (obj) => (obj.__proto__); +/******/ var leafPrototypes; +/******/ // create a fake namespace object +/******/ // mode & 1: value is a module id, require it +/******/ // mode & 2: merge all properties of value into the ns +/******/ // mode & 4: return value when already ns object +/******/ // mode & 16: return value when it's Promise-like +/******/ // mode & 8|1: behave like require +/******/ __webpack_require__.t = function(value, mode) { +/******/ if(mode & 1) value = this(value); +/******/ if(mode & 8) return value; +/******/ if(typeof value === 'object' && value) { +/******/ if((mode & 4) && value.__esModule) return value; +/******/ if((mode & 16) && typeof value.then === 'function') return value; +/******/ } +/******/ var ns = Object.create(null); +/******/ __webpack_require__.r(ns); +/******/ var def = {}; +/******/ leafPrototypes = leafPrototypes || [null, getProto({}), getProto([]), getProto(getProto)]; +/******/ for(var current = mode & 2 && value; typeof current == 'object' && !~leafPrototypes.indexOf(current); current = getProto(current)) { +/******/ Object.getOwnPropertyNames(current).forEach((key) => (def[key] = () => (value[key]))); +/******/ } +/******/ def['default'] = () => (value); +/******/ __webpack_require__.d(ns, def); +/******/ return ns; +/******/ }; +/******/ })(); +/******/ +/******/ /* webpack/runtime/define property getters */ +/******/ (() => { +/******/ // define getter functions for harmony exports +/******/ __webpack_require__.d = (exports, definition) => { +/******/ for(var key in definition) { +/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) { +/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] }); +/******/ } +/******/ } +/******/ }; +/******/ })(); +/******/ +/******/ /* webpack/runtime/hasOwnProperty shorthand */ +/******/ (() => { +/******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop)) +/******/ })(); +/******/ +/******/ /* webpack/runtime/make namespace object */ +/******/ (() => { +/******/ // define __esModule on exports +/******/ __webpack_require__.r = (exports) => { +/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { +/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); +/******/ } +/******/ Object.defineProperty(exports, '__esModule', { value: true }); +/******/ }; +/******/ })(); +/******/ +/************************************************************************/ +var __webpack_exports__ = {}; +/*!*****************************!*\ + !*** ./src/transformers.js ***! + \*****************************/ +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ ASTFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.ASTFeatureExtractor), +/* harmony export */ ASTForAudioClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ASTForAudioClassification), +/* harmony export */ ASTModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ASTModel), +/* harmony export */ ASTPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ASTPreTrainedModel), +/* harmony export */ AlbertForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AlbertForMaskedLM), +/* harmony export */ AlbertForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AlbertForQuestionAnswering), +/* harmony export */ AlbertForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AlbertForSequenceClassification), +/* harmony export */ AlbertModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AlbertModel), +/* harmony export */ AlbertPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AlbertPreTrainedModel), +/* harmony export */ AlbertTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.AlbertTokenizer), +/* harmony export */ AudioClassificationPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.AudioClassificationPipeline), +/* harmony export */ AutoConfig: () => (/* reexport safe */ _configs_js__WEBPACK_IMPORTED_MODULE_5__.AutoConfig), +/* harmony export */ AutoModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModel), +/* harmony export */ AutoModelForAudioClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForAudioClassification), +/* harmony export */ AutoModelForAudioFrameClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForAudioFrameClassification), +/* harmony export */ AutoModelForCTC: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForCTC), +/* harmony export */ AutoModelForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForCausalLM), +/* harmony export */ AutoModelForDepthEstimation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForDepthEstimation), +/* harmony export */ AutoModelForDocumentQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForDocumentQuestionAnswering), +/* harmony export */ AutoModelForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForImageClassification), +/* harmony export */ AutoModelForImageFeatureExtraction: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForImageFeatureExtraction), +/* harmony export */ AutoModelForImageMatting: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForImageMatting), +/* harmony export */ AutoModelForImageSegmentation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForImageSegmentation), +/* harmony export */ AutoModelForImageToImage: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForImageToImage), +/* harmony export */ AutoModelForMaskGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForMaskGeneration), +/* harmony export */ AutoModelForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForMaskedLM), +/* harmony export */ AutoModelForNormalEstimation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForNormalEstimation), +/* harmony export */ AutoModelForObjectDetection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForObjectDetection), +/* harmony export */ AutoModelForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForQuestionAnswering), +/* harmony export */ AutoModelForSemanticSegmentation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForSemanticSegmentation), +/* harmony export */ AutoModelForSeq2SeqLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForSeq2SeqLM), +/* harmony export */ AutoModelForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForSequenceClassification), +/* harmony export */ AutoModelForSpeechSeq2Seq: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForSpeechSeq2Seq), +/* harmony export */ AutoModelForTextToSpectrogram: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForTextToSpectrogram), +/* harmony export */ AutoModelForTextToWaveform: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForTextToWaveform), +/* harmony export */ AutoModelForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForTokenClassification), +/* harmony export */ AutoModelForUniversalSegmentation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForUniversalSegmentation), +/* harmony export */ AutoModelForVision2Seq: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForVision2Seq), +/* harmony export */ AutoModelForXVector: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForXVector), +/* harmony export */ AutoModelForZeroShotObjectDetection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.AutoModelForZeroShotObjectDetection), +/* harmony export */ AutoProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.AutoProcessor), +/* harmony export */ AutoTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.AutoTokenizer), +/* harmony export */ AutomaticSpeechRecognitionPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.AutomaticSpeechRecognitionPipeline), +/* harmony export */ BartForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BartForConditionalGeneration), +/* harmony export */ BartForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BartForSequenceClassification), +/* harmony export */ BartModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BartModel), +/* harmony export */ BartPretrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BartPretrainedModel), +/* harmony export */ BartTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.BartTokenizer), +/* harmony export */ BaseModelOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BaseModelOutput), +/* harmony export */ BaseStreamer: () => (/* reexport safe */ _generation_streamers_js__WEBPACK_IMPORTED_MODULE_10__.BaseStreamer), +/* harmony export */ BeitFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.BeitFeatureExtractor), +/* harmony export */ BeitForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BeitForImageClassification), +/* harmony export */ BeitModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BeitModel), +/* harmony export */ BeitPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BeitPreTrainedModel), +/* harmony export */ BertForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BertForMaskedLM), +/* harmony export */ BertForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BertForQuestionAnswering), +/* harmony export */ BertForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BertForSequenceClassification), +/* harmony export */ BertForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BertForTokenClassification), +/* harmony export */ BertModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BertModel), +/* harmony export */ BertPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BertPreTrainedModel), +/* harmony export */ BertTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.BertTokenizer), +/* harmony export */ BitImageProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.BitImageProcessor), +/* harmony export */ BlenderbotForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BlenderbotForConditionalGeneration), +/* harmony export */ BlenderbotModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BlenderbotModel), +/* harmony export */ BlenderbotPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BlenderbotPreTrainedModel), +/* harmony export */ BlenderbotSmallForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BlenderbotSmallForConditionalGeneration), +/* harmony export */ BlenderbotSmallModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BlenderbotSmallModel), +/* harmony export */ BlenderbotSmallPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BlenderbotSmallPreTrainedModel), +/* harmony export */ BlenderbotSmallTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.BlenderbotSmallTokenizer), +/* harmony export */ BlenderbotTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.BlenderbotTokenizer), +/* harmony export */ BloomForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BloomForCausalLM), +/* harmony export */ BloomModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BloomModel), +/* harmony export */ BloomPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.BloomPreTrainedModel), +/* harmony export */ BloomTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.BloomTokenizer), +/* harmony export */ CLIPFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.CLIPFeatureExtractor), +/* harmony export */ CLIPImageProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.CLIPImageProcessor), +/* harmony export */ CLIPModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPModel), +/* harmony export */ CLIPPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPPreTrainedModel), +/* harmony export */ CLIPSegForImageSegmentation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPSegForImageSegmentation), +/* harmony export */ CLIPSegModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPSegModel), +/* harmony export */ CLIPSegPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPSegPreTrainedModel), +/* harmony export */ CLIPTextModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPTextModel), +/* harmony export */ CLIPTextModelWithProjection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPTextModelWithProjection), +/* harmony export */ CLIPTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.CLIPTokenizer), +/* harmony export */ CLIPVisionModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPVisionModel), +/* harmony export */ CLIPVisionModelWithProjection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CLIPVisionModelWithProjection), +/* harmony export */ CamembertForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CamembertForMaskedLM), +/* harmony export */ CamembertForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CamembertForQuestionAnswering), +/* harmony export */ CamembertForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CamembertForSequenceClassification), +/* harmony export */ CamembertForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CamembertForTokenClassification), +/* harmony export */ CamembertModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CamembertModel), +/* harmony export */ CamembertPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CamembertPreTrainedModel), +/* harmony export */ CamembertTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.CamembertTokenizer), +/* harmony export */ CausalLMOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CausalLMOutput), +/* harmony export */ CausalLMOutputWithPast: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CausalLMOutputWithPast), +/* harmony export */ ChineseCLIPFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.ChineseCLIPFeatureExtractor), +/* harmony export */ ChineseCLIPModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ChineseCLIPModel), +/* harmony export */ ChineseCLIPPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ChineseCLIPPreTrainedModel), +/* harmony export */ ClapAudioModelWithProjection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ClapAudioModelWithProjection), +/* harmony export */ ClapFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.ClapFeatureExtractor), +/* harmony export */ ClapModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ClapModel), +/* harmony export */ ClapPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ClapPreTrainedModel), +/* harmony export */ ClapTextModelWithProjection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ClapTextModelWithProjection), +/* harmony export */ CodeGenForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CodeGenForCausalLM), +/* harmony export */ CodeGenModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CodeGenModel), +/* harmony export */ CodeGenPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CodeGenPreTrainedModel), +/* harmony export */ CodeGenTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.CodeGenTokenizer), +/* harmony export */ CodeLlamaTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.CodeLlamaTokenizer), +/* harmony export */ CohereForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CohereForCausalLM), +/* harmony export */ CohereModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CohereModel), +/* harmony export */ CoherePreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.CoherePreTrainedModel), +/* harmony export */ CohereTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.CohereTokenizer), +/* harmony export */ ConvBertForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvBertForMaskedLM), +/* harmony export */ ConvBertForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvBertForQuestionAnswering), +/* harmony export */ ConvBertForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvBertForSequenceClassification), +/* harmony export */ ConvBertForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvBertForTokenClassification), +/* harmony export */ ConvBertModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvBertModel), +/* harmony export */ ConvBertPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvBertPreTrainedModel), +/* harmony export */ ConvBertTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.ConvBertTokenizer), +/* harmony export */ ConvNextFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.ConvNextFeatureExtractor), +/* harmony export */ ConvNextForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvNextForImageClassification), +/* harmony export */ ConvNextImageProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.ConvNextImageProcessor), +/* harmony export */ ConvNextModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvNextModel), +/* harmony export */ ConvNextPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvNextPreTrainedModel), +/* harmony export */ ConvNextV2ForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvNextV2ForImageClassification), +/* harmony export */ ConvNextV2Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvNextV2Model), +/* harmony export */ ConvNextV2PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ConvNextV2PreTrainedModel), +/* harmony export */ DPTFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.DPTFeatureExtractor), +/* harmony export */ DPTForDepthEstimation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DPTForDepthEstimation), +/* harmony export */ DPTImageProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.DPTImageProcessor), +/* harmony export */ DPTModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DPTModel), +/* harmony export */ DPTPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DPTPreTrainedModel), +/* harmony export */ DebertaForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaForMaskedLM), +/* harmony export */ DebertaForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaForQuestionAnswering), +/* harmony export */ DebertaForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaForSequenceClassification), +/* harmony export */ DebertaForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaForTokenClassification), +/* harmony export */ DebertaModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaModel), +/* harmony export */ DebertaPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaPreTrainedModel), +/* harmony export */ DebertaTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.DebertaTokenizer), +/* harmony export */ DebertaV2ForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaV2ForMaskedLM), +/* harmony export */ DebertaV2ForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaV2ForQuestionAnswering), +/* harmony export */ DebertaV2ForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaV2ForSequenceClassification), +/* harmony export */ DebertaV2ForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaV2ForTokenClassification), +/* harmony export */ DebertaV2Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaV2Model), +/* harmony export */ DebertaV2PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DebertaV2PreTrainedModel), +/* harmony export */ DebertaV2Tokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.DebertaV2Tokenizer), +/* harmony export */ DecisionTransformerModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DecisionTransformerModel), +/* harmony export */ DecisionTransformerPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DecisionTransformerPreTrainedModel), +/* harmony export */ DeiTFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.DeiTFeatureExtractor), +/* harmony export */ DeiTForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DeiTForImageClassification), +/* harmony export */ DeiTModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DeiTModel), +/* harmony export */ DeiTPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DeiTPreTrainedModel), +/* harmony export */ DepthAnythingForDepthEstimation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DepthAnythingForDepthEstimation), +/* harmony export */ DepthAnythingPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DepthAnythingPreTrainedModel), +/* harmony export */ DepthEstimationPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.DepthEstimationPipeline), +/* harmony export */ DepthProForDepthEstimation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DepthProForDepthEstimation), +/* harmony export */ DepthProPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DepthProPreTrainedModel), +/* harmony export */ DetrFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.DetrFeatureExtractor), +/* harmony export */ DetrForObjectDetection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DetrForObjectDetection), +/* harmony export */ DetrForSegmentation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DetrForSegmentation), +/* harmony export */ DetrModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DetrModel), +/* harmony export */ DetrObjectDetectionOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DetrObjectDetectionOutput), +/* harmony export */ DetrPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DetrPreTrainedModel), +/* harmony export */ DetrSegmentationOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DetrSegmentationOutput), +/* harmony export */ Dinov2ForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Dinov2ForImageClassification), +/* harmony export */ Dinov2Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Dinov2Model), +/* harmony export */ Dinov2PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Dinov2PreTrainedModel), +/* harmony export */ DistilBertForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DistilBertForMaskedLM), +/* harmony export */ DistilBertForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DistilBertForQuestionAnswering), +/* harmony export */ DistilBertForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DistilBertForSequenceClassification), +/* harmony export */ DistilBertForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DistilBertForTokenClassification), +/* harmony export */ DistilBertModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DistilBertModel), +/* harmony export */ DistilBertPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DistilBertPreTrainedModel), +/* harmony export */ DistilBertTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.DistilBertTokenizer), +/* harmony export */ DocumentQuestionAnsweringPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.DocumentQuestionAnsweringPipeline), +/* harmony export */ DonutFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.DonutFeatureExtractor), +/* harmony export */ DonutImageProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.DonutImageProcessor), +/* harmony export */ DonutSwinModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DonutSwinModel), +/* harmony export */ DonutSwinPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.DonutSwinPreTrainedModel), +/* harmony export */ EfficientNetForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.EfficientNetForImageClassification), +/* harmony export */ EfficientNetImageProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.EfficientNetImageProcessor), +/* harmony export */ EfficientNetModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.EfficientNetModel), +/* harmony export */ EfficientNetPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.EfficientNetPreTrainedModel), +/* harmony export */ ElectraForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ElectraForMaskedLM), +/* harmony export */ ElectraForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ElectraForQuestionAnswering), +/* harmony export */ ElectraForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ElectraForSequenceClassification), +/* harmony export */ ElectraForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ElectraForTokenClassification), +/* harmony export */ ElectraModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ElectraModel), +/* harmony export */ ElectraPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ElectraPreTrainedModel), +/* harmony export */ ElectraTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.ElectraTokenizer), +/* harmony export */ EosTokenCriteria: () => (/* reexport safe */ _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_11__.EosTokenCriteria), +/* harmony export */ EsmForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.EsmForMaskedLM), +/* harmony export */ EsmForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.EsmForSequenceClassification), +/* harmony export */ EsmForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.EsmForTokenClassification), +/* harmony export */ EsmModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.EsmModel), +/* harmony export */ EsmPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.EsmPreTrainedModel), +/* harmony export */ EsmTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.EsmTokenizer), +/* harmony export */ FFT: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.FFT), +/* harmony export */ FalconForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.FalconForCausalLM), +/* harmony export */ FalconModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.FalconModel), +/* harmony export */ FalconPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.FalconPreTrainedModel), +/* harmony export */ FalconTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.FalconTokenizer), +/* harmony export */ FastViTForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.FastViTForImageClassification), +/* harmony export */ FastViTModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.FastViTModel), +/* harmony export */ FastViTPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.FastViTPreTrainedModel), +/* harmony export */ FeatureExtractionPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.FeatureExtractionPipeline), +/* harmony export */ FeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.FeatureExtractor), +/* harmony export */ FillMaskPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.FillMaskPipeline), +/* harmony export */ Florence2ForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Florence2ForConditionalGeneration), +/* harmony export */ Florence2PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Florence2PreTrainedModel), +/* harmony export */ Florence2Processor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.Florence2Processor), +/* harmony export */ GLPNFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.GLPNFeatureExtractor), +/* harmony export */ GLPNForDepthEstimation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GLPNForDepthEstimation), +/* harmony export */ GLPNModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GLPNModel), +/* harmony export */ GLPNPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GLPNPreTrainedModel), +/* harmony export */ GPT2LMHeadModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPT2LMHeadModel), +/* harmony export */ GPT2Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPT2Model), +/* harmony export */ GPT2PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPT2PreTrainedModel), +/* harmony export */ GPT2Tokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.GPT2Tokenizer), +/* harmony export */ GPTBigCodeForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTBigCodeForCausalLM), +/* harmony export */ GPTBigCodeModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTBigCodeModel), +/* harmony export */ GPTBigCodePreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTBigCodePreTrainedModel), +/* harmony export */ GPTJForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTJForCausalLM), +/* harmony export */ GPTJModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTJModel), +/* harmony export */ GPTJPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTJPreTrainedModel), +/* harmony export */ GPTNeoForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTNeoForCausalLM), +/* harmony export */ GPTNeoModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTNeoModel), +/* harmony export */ GPTNeoPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTNeoPreTrainedModel), +/* harmony export */ GPTNeoXForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTNeoXForCausalLM), +/* harmony export */ GPTNeoXModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTNeoXModel), +/* harmony export */ GPTNeoXPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GPTNeoXPreTrainedModel), +/* harmony export */ GPTNeoXTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.GPTNeoXTokenizer), +/* harmony export */ Gemma2ForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Gemma2ForCausalLM), +/* harmony export */ Gemma2Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Gemma2Model), +/* harmony export */ Gemma2PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Gemma2PreTrainedModel), +/* harmony export */ GemmaForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GemmaForCausalLM), +/* harmony export */ GemmaModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GemmaModel), +/* harmony export */ GemmaPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GemmaPreTrainedModel), +/* harmony export */ GemmaTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.GemmaTokenizer), +/* harmony export */ GraniteForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GraniteForCausalLM), +/* harmony export */ GraniteModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GraniteModel), +/* harmony export */ GranitePreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GranitePreTrainedModel), +/* harmony export */ Grok1Tokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.Grok1Tokenizer), +/* harmony export */ GroupViTModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GroupViTModel), +/* harmony export */ GroupViTPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.GroupViTPreTrainedModel), +/* harmony export */ HerbertTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.HerbertTokenizer), +/* harmony export */ HieraForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.HieraForImageClassification), +/* harmony export */ HieraModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.HieraModel), +/* harmony export */ HieraPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.HieraPreTrainedModel), +/* harmony export */ HubertForCTC: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.HubertForCTC), +/* harmony export */ HubertForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.HubertForSequenceClassification), +/* harmony export */ HubertModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.HubertModel), +/* harmony export */ HubertPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.HubertPreTrainedModel), +/* harmony export */ ImageClassificationPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ImageClassificationPipeline), +/* harmony export */ ImageFeatureExtractionPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ImageFeatureExtractionPipeline), +/* harmony export */ ImageFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.ImageFeatureExtractor), +/* harmony export */ ImageMattingOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ImageMattingOutput), +/* harmony export */ ImageSegmentationPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ImageSegmentationPipeline), +/* harmony export */ ImageToImagePipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ImageToImagePipeline), +/* harmony export */ ImageToTextPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ImageToTextPipeline), +/* harmony export */ InterruptableStoppingCriteria: () => (/* reexport safe */ _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_11__.InterruptableStoppingCriteria), +/* harmony export */ JAISLMHeadModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.JAISLMHeadModel), +/* harmony export */ JAISModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.JAISModel), +/* harmony export */ JAISPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.JAISPreTrainedModel), +/* harmony export */ LlamaForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.LlamaForCausalLM), +/* harmony export */ LlamaModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.LlamaModel), +/* harmony export */ LlamaPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.LlamaPreTrainedModel), +/* harmony export */ LlamaTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.LlamaTokenizer), +/* harmony export */ LlavaForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.LlavaForConditionalGeneration), +/* harmony export */ LlavaPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.LlavaPreTrainedModel), +/* harmony export */ LongT5ForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.LongT5ForConditionalGeneration), +/* harmony export */ LongT5Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.LongT5Model), +/* harmony export */ LongT5PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.LongT5PreTrainedModel), +/* harmony export */ M2M100ForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.M2M100ForConditionalGeneration), +/* harmony export */ M2M100Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.M2M100Model), +/* harmony export */ M2M100PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.M2M100PreTrainedModel), +/* harmony export */ M2M100Tokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.M2M100Tokenizer), +/* harmony export */ MBart50Tokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.MBart50Tokenizer), +/* harmony export */ MBartForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MBartForCausalLM), +/* harmony export */ MBartForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MBartForConditionalGeneration), +/* harmony export */ MBartForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MBartForSequenceClassification), +/* harmony export */ MBartModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MBartModel), +/* harmony export */ MBartPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MBartPreTrainedModel), +/* harmony export */ MBartTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.MBartTokenizer), +/* harmony export */ MPNetForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MPNetForMaskedLM), +/* harmony export */ MPNetForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MPNetForQuestionAnswering), +/* harmony export */ MPNetForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MPNetForSequenceClassification), +/* harmony export */ MPNetForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MPNetForTokenClassification), +/* harmony export */ MPNetModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MPNetModel), +/* harmony export */ MPNetPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MPNetPreTrainedModel), +/* harmony export */ MPNetTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.MPNetTokenizer), +/* harmony export */ MT5ForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MT5ForConditionalGeneration), +/* harmony export */ MT5Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MT5Model), +/* harmony export */ MT5PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MT5PreTrainedModel), +/* harmony export */ MarianMTModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MarianMTModel), +/* harmony export */ MarianModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MarianModel), +/* harmony export */ MarianPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MarianPreTrainedModel), +/* harmony export */ MarianTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.MarianTokenizer), +/* harmony export */ MaskFormerFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.MaskFormerFeatureExtractor), +/* harmony export */ MaskFormerForInstanceSegmentation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MaskFormerForInstanceSegmentation), +/* harmony export */ MaskFormerModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MaskFormerModel), +/* harmony export */ MaskFormerPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MaskFormerPreTrainedModel), +/* harmony export */ MaskedLMOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MaskedLMOutput), +/* harmony export */ MaxLengthCriteria: () => (/* reexport safe */ _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_11__.MaxLengthCriteria), +/* harmony export */ MistralForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MistralForCausalLM), +/* harmony export */ MistralModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MistralModel), +/* harmony export */ MistralPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MistralPreTrainedModel), +/* harmony export */ MobileBertForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileBertForMaskedLM), +/* harmony export */ MobileBertForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileBertForQuestionAnswering), +/* harmony export */ MobileBertForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileBertForSequenceClassification), +/* harmony export */ MobileBertModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileBertModel), +/* harmony export */ MobileBertPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileBertPreTrainedModel), +/* harmony export */ MobileBertTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.MobileBertTokenizer), +/* harmony export */ MobileLLMForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileLLMForCausalLM), +/* harmony export */ MobileLLMModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileLLMModel), +/* harmony export */ MobileLLMPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileLLMPreTrainedModel), +/* harmony export */ MobileNetV1FeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.MobileNetV1FeatureExtractor), +/* harmony export */ MobileNetV1ForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV1ForImageClassification), +/* harmony export */ MobileNetV1Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV1Model), +/* harmony export */ MobileNetV1PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV1PreTrainedModel), +/* harmony export */ MobileNetV2FeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.MobileNetV2FeatureExtractor), +/* harmony export */ MobileNetV2ForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV2ForImageClassification), +/* harmony export */ MobileNetV2Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV2Model), +/* harmony export */ MobileNetV2PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV2PreTrainedModel), +/* harmony export */ MobileNetV3FeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.MobileNetV3FeatureExtractor), +/* harmony export */ MobileNetV3ForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV3ForImageClassification), +/* harmony export */ MobileNetV3Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV3Model), +/* harmony export */ MobileNetV3PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV3PreTrainedModel), +/* harmony export */ MobileNetV4FeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.MobileNetV4FeatureExtractor), +/* harmony export */ MobileNetV4ForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV4ForImageClassification), +/* harmony export */ MobileNetV4Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV4Model), +/* harmony export */ MobileNetV4PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileNetV4PreTrainedModel), +/* harmony export */ MobileViTFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.MobileViTFeatureExtractor), +/* harmony export */ MobileViTForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileViTForImageClassification), +/* harmony export */ MobileViTImageProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.MobileViTImageProcessor), +/* harmony export */ MobileViTModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileViTModel), +/* harmony export */ MobileViTPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileViTPreTrainedModel), +/* harmony export */ MobileViTV2ForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileViTV2ForImageClassification), +/* harmony export */ MobileViTV2Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileViTV2Model), +/* harmony export */ MobileViTV2PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MobileViTV2PreTrainedModel), +/* harmony export */ ModelOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ModelOutput), +/* harmony export */ Moondream1ForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Moondream1ForConditionalGeneration), +/* harmony export */ MptForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MptForCausalLM), +/* harmony export */ MptModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MptModel), +/* harmony export */ MptPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MptPreTrainedModel), +/* harmony export */ MusicgenForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MusicgenForCausalLM), +/* harmony export */ MusicgenForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MusicgenForConditionalGeneration), +/* harmony export */ MusicgenModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MusicgenModel), +/* harmony export */ MusicgenPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.MusicgenPreTrainedModel), +/* harmony export */ NllbTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.NllbTokenizer), +/* harmony export */ NomicBertModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.NomicBertModel), +/* harmony export */ NomicBertPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.NomicBertPreTrainedModel), +/* harmony export */ NougatImageProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.NougatImageProcessor), +/* harmony export */ NougatTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.NougatTokenizer), +/* harmony export */ OPTForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OPTForCausalLM), +/* harmony export */ OPTModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OPTModel), +/* harmony export */ OPTPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OPTPreTrainedModel), +/* harmony export */ ObjectDetectionPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ObjectDetectionPipeline), +/* harmony export */ OlmoForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OlmoForCausalLM), +/* harmony export */ OlmoModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OlmoModel), +/* harmony export */ OlmoPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OlmoPreTrainedModel), +/* harmony export */ OpenELMForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OpenELMForCausalLM), +/* harmony export */ OpenELMModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OpenELMModel), +/* harmony export */ OpenELMPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OpenELMPreTrainedModel), +/* harmony export */ OwlViTFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.OwlViTFeatureExtractor), +/* harmony export */ OwlViTForObjectDetection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OwlViTForObjectDetection), +/* harmony export */ OwlViTModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OwlViTModel), +/* harmony export */ OwlViTPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.OwlViTPreTrainedModel), +/* harmony export */ OwlViTProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.OwlViTProcessor), +/* harmony export */ Owlv2ForObjectDetection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Owlv2ForObjectDetection), +/* harmony export */ Owlv2ImageProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.Owlv2ImageProcessor), +/* harmony export */ Owlv2Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Owlv2Model), +/* harmony export */ Owlv2PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Owlv2PreTrainedModel), +/* harmony export */ Phi3ForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Phi3ForCausalLM), +/* harmony export */ Phi3Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Phi3Model), +/* harmony export */ Phi3PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Phi3PreTrainedModel), +/* harmony export */ PhiForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PhiForCausalLM), +/* harmony export */ PhiModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PhiModel), +/* harmony export */ PhiPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PhiPreTrainedModel), +/* harmony export */ Pipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.Pipeline), +/* harmony export */ PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PreTrainedModel), +/* harmony export */ PreTrainedTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.PreTrainedTokenizer), +/* harmony export */ PretrainedConfig: () => (/* reexport safe */ _configs_js__WEBPACK_IMPORTED_MODULE_5__.PretrainedConfig), +/* harmony export */ PretrainedMixin: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PretrainedMixin), +/* harmony export */ Processor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.Processor), +/* harmony export */ PvtForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PvtForImageClassification), +/* harmony export */ PvtImageProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.PvtImageProcessor), +/* harmony export */ PvtModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PvtModel), +/* harmony export */ PvtPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PvtPreTrainedModel), +/* harmony export */ PyAnnoteFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.PyAnnoteFeatureExtractor), +/* harmony export */ PyAnnoteForAudioFrameClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PyAnnoteForAudioFrameClassification), +/* harmony export */ PyAnnoteModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PyAnnoteModel), +/* harmony export */ PyAnnotePreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.PyAnnotePreTrainedModel), +/* harmony export */ PyAnnoteProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.PyAnnoteProcessor), +/* harmony export */ QuestionAnsweringModelOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.QuestionAnsweringModelOutput), +/* harmony export */ QuestionAnsweringPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.QuestionAnsweringPipeline), +/* harmony export */ Qwen2ForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Qwen2ForCausalLM), +/* harmony export */ Qwen2Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Qwen2Model), +/* harmony export */ Qwen2PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Qwen2PreTrainedModel), +/* harmony export */ Qwen2Tokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.Qwen2Tokenizer), +/* harmony export */ RTDetrForObjectDetection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RTDetrForObjectDetection), +/* harmony export */ RTDetrImageProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.RTDetrImageProcessor), +/* harmony export */ RTDetrModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RTDetrModel), +/* harmony export */ RTDetrObjectDetectionOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RTDetrObjectDetectionOutput), +/* harmony export */ RTDetrPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RTDetrPreTrainedModel), +/* harmony export */ RawImage: () => (/* reexport safe */ _utils_image_js__WEBPACK_IMPORTED_MODULE_7__.RawImage), +/* harmony export */ ResNetForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ResNetForImageClassification), +/* harmony export */ ResNetModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ResNetModel), +/* harmony export */ ResNetPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ResNetPreTrainedModel), +/* harmony export */ RoFormerForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RoFormerForMaskedLM), +/* harmony export */ RoFormerForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RoFormerForQuestionAnswering), +/* harmony export */ RoFormerForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RoFormerForSequenceClassification), +/* harmony export */ RoFormerForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RoFormerForTokenClassification), +/* harmony export */ RoFormerModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RoFormerModel), +/* harmony export */ RoFormerPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RoFormerPreTrainedModel), +/* harmony export */ RoFormerTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.RoFormerTokenizer), +/* harmony export */ RobertaForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RobertaForMaskedLM), +/* harmony export */ RobertaForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RobertaForQuestionAnswering), +/* harmony export */ RobertaForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RobertaForSequenceClassification), +/* harmony export */ RobertaForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RobertaForTokenClassification), +/* harmony export */ RobertaModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RobertaModel), +/* harmony export */ RobertaPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.RobertaPreTrainedModel), +/* harmony export */ RobertaTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.RobertaTokenizer), +/* harmony export */ SamImageProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.SamImageProcessor), +/* harmony export */ SamImageSegmentationOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SamImageSegmentationOutput), +/* harmony export */ SamModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SamModel), +/* harmony export */ SamPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SamPreTrainedModel), +/* harmony export */ SamProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.SamProcessor), +/* harmony export */ SapiensFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.SapiensFeatureExtractor), +/* harmony export */ SapiensForDepthEstimation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SapiensForDepthEstimation), +/* harmony export */ SapiensForNormalEstimation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SapiensForNormalEstimation), +/* harmony export */ SapiensForSemanticSegmentation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SapiensForSemanticSegmentation), +/* harmony export */ SapiensPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SapiensPreTrainedModel), +/* harmony export */ SeamlessM4TFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.SeamlessM4TFeatureExtractor), +/* harmony export */ SegformerFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.SegformerFeatureExtractor), +/* harmony export */ SegformerForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SegformerForImageClassification), +/* harmony export */ SegformerForSemanticSegmentation: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SegformerForSemanticSegmentation), +/* harmony export */ SegformerModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SegformerModel), +/* harmony export */ SegformerPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SegformerPreTrainedModel), +/* harmony export */ Seq2SeqLMOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Seq2SeqLMOutput), +/* harmony export */ SequenceClassifierOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SequenceClassifierOutput), +/* harmony export */ SiglipImageProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.SiglipImageProcessor), +/* harmony export */ SiglipModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SiglipModel), +/* harmony export */ SiglipPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SiglipPreTrainedModel), +/* harmony export */ SiglipTextModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SiglipTextModel), +/* harmony export */ SiglipTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.SiglipTokenizer), +/* harmony export */ SiglipVisionModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SiglipVisionModel), +/* harmony export */ SpeechT5FeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.SpeechT5FeatureExtractor), +/* harmony export */ SpeechT5ForSpeechToText: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SpeechT5ForSpeechToText), +/* harmony export */ SpeechT5ForTextToSpeech: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SpeechT5ForTextToSpeech), +/* harmony export */ SpeechT5HifiGan: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SpeechT5HifiGan), +/* harmony export */ SpeechT5Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SpeechT5Model), +/* harmony export */ SpeechT5PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SpeechT5PreTrainedModel), +/* harmony export */ SpeechT5Processor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.SpeechT5Processor), +/* harmony export */ SpeechT5Tokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.SpeechT5Tokenizer), +/* harmony export */ SqueezeBertForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SqueezeBertForMaskedLM), +/* harmony export */ SqueezeBertForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SqueezeBertForQuestionAnswering), +/* harmony export */ SqueezeBertForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SqueezeBertForSequenceClassification), +/* harmony export */ SqueezeBertModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SqueezeBertModel), +/* harmony export */ SqueezeBertPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SqueezeBertPreTrainedModel), +/* harmony export */ SqueezeBertTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.SqueezeBertTokenizer), +/* harmony export */ StableLmForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.StableLmForCausalLM), +/* harmony export */ StableLmModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.StableLmModel), +/* harmony export */ StableLmPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.StableLmPreTrainedModel), +/* harmony export */ Starcoder2ForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Starcoder2ForCausalLM), +/* harmony export */ Starcoder2Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Starcoder2Model), +/* harmony export */ Starcoder2PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Starcoder2PreTrainedModel), +/* harmony export */ StoppingCriteria: () => (/* reexport safe */ _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_11__.StoppingCriteria), +/* harmony export */ StoppingCriteriaList: () => (/* reexport safe */ _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_11__.StoppingCriteriaList), +/* harmony export */ SummarizationPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.SummarizationPipeline), +/* harmony export */ Swin2SRForImageSuperResolution: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Swin2SRForImageSuperResolution), +/* harmony export */ Swin2SRImageProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.Swin2SRImageProcessor), +/* harmony export */ Swin2SRModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Swin2SRModel), +/* harmony export */ Swin2SRPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Swin2SRPreTrainedModel), +/* harmony export */ SwinForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SwinForImageClassification), +/* harmony export */ SwinModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SwinModel), +/* harmony export */ SwinPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.SwinPreTrainedModel), +/* harmony export */ T5ForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.T5ForConditionalGeneration), +/* harmony export */ T5Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.T5Model), +/* harmony export */ T5PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.T5PreTrainedModel), +/* harmony export */ T5Tokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.T5Tokenizer), +/* harmony export */ TableTransformerForObjectDetection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.TableTransformerForObjectDetection), +/* harmony export */ TableTransformerModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.TableTransformerModel), +/* harmony export */ TableTransformerObjectDetectionOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.TableTransformerObjectDetectionOutput), +/* harmony export */ TableTransformerPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.TableTransformerPreTrainedModel), +/* harmony export */ Tensor: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.Tensor), +/* harmony export */ Text2TextGenerationPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.Text2TextGenerationPipeline), +/* harmony export */ TextClassificationPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.TextClassificationPipeline), +/* harmony export */ TextGenerationPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.TextGenerationPipeline), +/* harmony export */ TextStreamer: () => (/* reexport safe */ _generation_streamers_js__WEBPACK_IMPORTED_MODULE_10__.TextStreamer), +/* harmony export */ TextToAudioPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.TextToAudioPipeline), +/* harmony export */ TokenClassificationPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.TokenClassificationPipeline), +/* harmony export */ TokenClassifierOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.TokenClassifierOutput), +/* harmony export */ TokenizerModel: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.TokenizerModel), +/* harmony export */ TrOCRForCausalLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.TrOCRForCausalLM), +/* harmony export */ TrOCRPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.TrOCRPreTrainedModel), +/* harmony export */ TranslationPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.TranslationPipeline), +/* harmony export */ UniSpeechForCTC: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechForCTC), +/* harmony export */ UniSpeechForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechForSequenceClassification), +/* harmony export */ UniSpeechModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechModel), +/* harmony export */ UniSpeechPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechPreTrainedModel), +/* harmony export */ UniSpeechSatForAudioFrameClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechSatForAudioFrameClassification), +/* harmony export */ UniSpeechSatForCTC: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechSatForCTC), +/* harmony export */ UniSpeechSatForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechSatForSequenceClassification), +/* harmony export */ UniSpeechSatModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechSatModel), +/* harmony export */ UniSpeechSatPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.UniSpeechSatPreTrainedModel), +/* harmony export */ ViTFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.ViTFeatureExtractor), +/* harmony export */ ViTForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTForImageClassification), +/* harmony export */ ViTImageProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.ViTImageProcessor), +/* harmony export */ ViTMAEModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTMAEModel), +/* harmony export */ ViTMAEPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTMAEPreTrainedModel), +/* harmony export */ ViTMSNForImageClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTMSNForImageClassification), +/* harmony export */ ViTMSNModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTMSNModel), +/* harmony export */ ViTMSNPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTMSNPreTrainedModel), +/* harmony export */ ViTModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTModel), +/* harmony export */ ViTPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.ViTPreTrainedModel), +/* harmony export */ VisionEncoderDecoderModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.VisionEncoderDecoderModel), +/* harmony export */ VitMatteForImageMatting: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.VitMatteForImageMatting), +/* harmony export */ VitMatteImageProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.VitMatteImageProcessor), +/* harmony export */ VitMattePreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.VitMattePreTrainedModel), +/* harmony export */ VitsModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.VitsModel), +/* harmony export */ VitsModelOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.VitsModelOutput), +/* harmony export */ VitsPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.VitsPreTrainedModel), +/* harmony export */ VitsTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.VitsTokenizer), +/* harmony export */ Wav2Vec2BertForCTC: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2BertForCTC), +/* harmony export */ Wav2Vec2BertForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2BertForSequenceClassification), +/* harmony export */ Wav2Vec2BertModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2BertModel), +/* harmony export */ Wav2Vec2BertPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2BertPreTrainedModel), +/* harmony export */ Wav2Vec2CTCTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.Wav2Vec2CTCTokenizer), +/* harmony export */ Wav2Vec2FeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.Wav2Vec2FeatureExtractor), +/* harmony export */ Wav2Vec2ForAudioFrameClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2ForAudioFrameClassification), +/* harmony export */ Wav2Vec2ForCTC: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2ForCTC), +/* harmony export */ Wav2Vec2ForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2ForSequenceClassification), +/* harmony export */ Wav2Vec2Model: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2Model), +/* harmony export */ Wav2Vec2PreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.Wav2Vec2PreTrainedModel), +/* harmony export */ Wav2Vec2ProcessorWithLM: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.Wav2Vec2ProcessorWithLM), +/* harmony export */ WavLMForAudioFrameClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WavLMForAudioFrameClassification), +/* harmony export */ WavLMForCTC: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WavLMForCTC), +/* harmony export */ WavLMForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WavLMForSequenceClassification), +/* harmony export */ WavLMForXVector: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WavLMForXVector), +/* harmony export */ WavLMModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WavLMModel), +/* harmony export */ WavLMPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WavLMPreTrainedModel), +/* harmony export */ WeSpeakerFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.WeSpeakerFeatureExtractor), +/* harmony export */ WeSpeakerResNetModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WeSpeakerResNetModel), +/* harmony export */ WeSpeakerResNetPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WeSpeakerResNetPreTrainedModel), +/* harmony export */ WhisperFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.WhisperFeatureExtractor), +/* harmony export */ WhisperForConditionalGeneration: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WhisperForConditionalGeneration), +/* harmony export */ WhisperModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WhisperModel), +/* harmony export */ WhisperPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.WhisperPreTrainedModel), +/* harmony export */ WhisperProcessor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.WhisperProcessor), +/* harmony export */ WhisperTextStreamer: () => (/* reexport safe */ _generation_streamers_js__WEBPACK_IMPORTED_MODULE_10__.WhisperTextStreamer), +/* harmony export */ WhisperTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.WhisperTokenizer), +/* harmony export */ XLMForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMForQuestionAnswering), +/* harmony export */ XLMForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMForSequenceClassification), +/* harmony export */ XLMForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMForTokenClassification), +/* harmony export */ XLMModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMModel), +/* harmony export */ XLMPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMPreTrainedModel), +/* harmony export */ XLMRobertaForMaskedLM: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMRobertaForMaskedLM), +/* harmony export */ XLMRobertaForQuestionAnswering: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMRobertaForQuestionAnswering), +/* harmony export */ XLMRobertaForSequenceClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMRobertaForSequenceClassification), +/* harmony export */ XLMRobertaForTokenClassification: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMRobertaForTokenClassification), +/* harmony export */ XLMRobertaModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMRobertaModel), +/* harmony export */ XLMRobertaPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMRobertaPreTrainedModel), +/* harmony export */ XLMRobertaTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.XLMRobertaTokenizer), +/* harmony export */ XLMTokenizer: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.XLMTokenizer), +/* harmony export */ XLMWithLMHeadModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XLMWithLMHeadModel), +/* harmony export */ XVectorOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.XVectorOutput), +/* harmony export */ YolosFeatureExtractor: () => (/* reexport safe */ _processors_js__WEBPACK_IMPORTED_MODULE_4__.YolosFeatureExtractor), +/* harmony export */ YolosForObjectDetection: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.YolosForObjectDetection), +/* harmony export */ YolosModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.YolosModel), +/* harmony export */ YolosObjectDetectionOutput: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.YolosObjectDetectionOutput), +/* harmony export */ YolosPreTrainedModel: () => (/* reexport safe */ _models_js__WEBPACK_IMPORTED_MODULE_2__.YolosPreTrainedModel), +/* harmony export */ ZeroShotAudioClassificationPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ZeroShotAudioClassificationPipeline), +/* harmony export */ ZeroShotClassificationPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ZeroShotClassificationPipeline), +/* harmony export */ ZeroShotImageClassificationPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ZeroShotImageClassificationPipeline), +/* harmony export */ ZeroShotObjectDetectionPipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.ZeroShotObjectDetectionPipeline), +/* harmony export */ bankers_round: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.bankers_round), +/* harmony export */ cat: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.cat), +/* harmony export */ cos_sim: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.cos_sim), +/* harmony export */ dot: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.dot), +/* harmony export */ dynamic_time_warping: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.dynamic_time_warping), +/* harmony export */ env: () => (/* reexport safe */ _env_js__WEBPACK_IMPORTED_MODULE_0__.env), +/* harmony export */ full: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.full), +/* harmony export */ full_like: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.full_like), +/* harmony export */ getKeyValueShapes: () => (/* reexport safe */ _configs_js__WEBPACK_IMPORTED_MODULE_5__.getKeyValueShapes), +/* harmony export */ hamming: () => (/* reexport safe */ _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.hamming), +/* harmony export */ hanning: () => (/* reexport safe */ _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.hanning), +/* harmony export */ interpolate: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.interpolate), +/* harmony export */ interpolate_4d: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.interpolate_4d), +/* harmony export */ interpolate_data: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.interpolate_data), +/* harmony export */ is_chinese_char: () => (/* reexport safe */ _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__.is_chinese_char), +/* harmony export */ layer_norm: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.layer_norm), +/* harmony export */ log_softmax: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.log_softmax), +/* harmony export */ magnitude: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.magnitude), +/* harmony export */ matmul: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.matmul), +/* harmony export */ max: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.max), +/* harmony export */ mean: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.mean), +/* harmony export */ mean_pooling: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.mean_pooling), +/* harmony export */ medianFilter: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.medianFilter), +/* harmony export */ mel_filter_bank: () => (/* reexport safe */ _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.mel_filter_bank), +/* harmony export */ min: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.min), +/* harmony export */ ones: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.ones), +/* harmony export */ ones_like: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.ones_like), +/* harmony export */ permute: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.permute), +/* harmony export */ permute_data: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.permute_data), +/* harmony export */ pipeline: () => (/* reexport safe */ _pipelines_js__WEBPACK_IMPORTED_MODULE_1__.pipeline), +/* harmony export */ quantize_embeddings: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.quantize_embeddings), +/* harmony export */ read_audio: () => (/* reexport safe */ _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.read_audio), +/* harmony export */ rfft: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.rfft), +/* harmony export */ round: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.round), +/* harmony export */ softmax: () => (/* reexport safe */ _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__.softmax), +/* harmony export */ spectrogram: () => (/* reexport safe */ _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.spectrogram), +/* harmony export */ stack: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.stack), +/* harmony export */ std_mean: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.std_mean), +/* harmony export */ topk: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.topk), +/* harmony export */ window_function: () => (/* reexport safe */ _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__.window_function), +/* harmony export */ zeros: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.zeros), +/* harmony export */ zeros_like: () => (/* reexport safe */ _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__.zeros_like) +/* harmony export */ }); +/* harmony import */ var _env_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./env.js */ "./src/env.js"); +/* harmony import */ var _pipelines_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./pipelines.js */ "./src/pipelines.js"); +/* harmony import */ var _models_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./models.js */ "./src/models.js"); +/* harmony import */ var _tokenizers_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./tokenizers.js */ "./src/tokenizers.js"); +/* harmony import */ var _processors_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ./processors.js */ "./src/processors.js"); +/* harmony import */ var _configs_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ./configs.js */ "./src/configs.js"); +/* harmony import */ var _utils_audio_js__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! ./utils/audio.js */ "./src/utils/audio.js"); +/* harmony import */ var _utils_image_js__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__(/*! ./utils/image.js */ "./src/utils/image.js"); +/* harmony import */ var _utils_tensor_js__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__(/*! ./utils/tensor.js */ "./src/utils/tensor.js"); +/* harmony import */ var _utils_maths_js__WEBPACK_IMPORTED_MODULE_9__ = __webpack_require__(/*! ./utils/maths.js */ "./src/utils/maths.js"); +/* harmony import */ var _generation_streamers_js__WEBPACK_IMPORTED_MODULE_10__ = __webpack_require__(/*! ./generation/streamers.js */ "./src/generation/streamers.js"); +/* harmony import */ var _generation_stopping_criteria_js__WEBPACK_IMPORTED_MODULE_11__ = __webpack_require__(/*! ./generation/stopping_criteria.js */ "./src/generation/stopping_criteria.js"); +/** + * @file Entry point for the Transformers.js library. Only the exports from this file + * are available to the end user, and are grouped as follows: + * + * 1. [Pipelines](./pipelines) + * 2. [Environment variables](./env) + * 3. [Models](./models) + * 4. [Tokenizers](./tokenizers) + * 5. [Processors](./processors) + * + * @module transformers + */ + + + + + + + + + + + + + + + + + +var __webpack_exports__ASTFeatureExtractor = __webpack_exports__.ASTFeatureExtractor; +var __webpack_exports__ASTForAudioClassification = __webpack_exports__.ASTForAudioClassification; +var __webpack_exports__ASTModel = __webpack_exports__.ASTModel; +var __webpack_exports__ASTPreTrainedModel = __webpack_exports__.ASTPreTrainedModel; +var __webpack_exports__AlbertForMaskedLM = __webpack_exports__.AlbertForMaskedLM; +var __webpack_exports__AlbertForQuestionAnswering = __webpack_exports__.AlbertForQuestionAnswering; +var __webpack_exports__AlbertForSequenceClassification = __webpack_exports__.AlbertForSequenceClassification; +var __webpack_exports__AlbertModel = __webpack_exports__.AlbertModel; +var __webpack_exports__AlbertPreTrainedModel = __webpack_exports__.AlbertPreTrainedModel; +var __webpack_exports__AlbertTokenizer = __webpack_exports__.AlbertTokenizer; +var __webpack_exports__AudioClassificationPipeline = __webpack_exports__.AudioClassificationPipeline; +var __webpack_exports__AutoConfig = __webpack_exports__.AutoConfig; +var __webpack_exports__AutoModel = __webpack_exports__.AutoModel; +var __webpack_exports__AutoModelForAudioClassification = __webpack_exports__.AutoModelForAudioClassification; +var __webpack_exports__AutoModelForAudioFrameClassification = __webpack_exports__.AutoModelForAudioFrameClassification; +var __webpack_exports__AutoModelForCTC = __webpack_exports__.AutoModelForCTC; +var __webpack_exports__AutoModelForCausalLM = __webpack_exports__.AutoModelForCausalLM; +var __webpack_exports__AutoModelForDepthEstimation = __webpack_exports__.AutoModelForDepthEstimation; +var __webpack_exports__AutoModelForDocumentQuestionAnswering = __webpack_exports__.AutoModelForDocumentQuestionAnswering; +var __webpack_exports__AutoModelForImageClassification = __webpack_exports__.AutoModelForImageClassification; +var __webpack_exports__AutoModelForImageFeatureExtraction = __webpack_exports__.AutoModelForImageFeatureExtraction; +var __webpack_exports__AutoModelForImageMatting = __webpack_exports__.AutoModelForImageMatting; +var __webpack_exports__AutoModelForImageSegmentation = __webpack_exports__.AutoModelForImageSegmentation; +var __webpack_exports__AutoModelForImageToImage = __webpack_exports__.AutoModelForImageToImage; +var __webpack_exports__AutoModelForMaskGeneration = __webpack_exports__.AutoModelForMaskGeneration; +var __webpack_exports__AutoModelForMaskedLM = __webpack_exports__.AutoModelForMaskedLM; +var __webpack_exports__AutoModelForNormalEstimation = __webpack_exports__.AutoModelForNormalEstimation; +var __webpack_exports__AutoModelForObjectDetection = __webpack_exports__.AutoModelForObjectDetection; +var __webpack_exports__AutoModelForQuestionAnswering = __webpack_exports__.AutoModelForQuestionAnswering; +var __webpack_exports__AutoModelForSemanticSegmentation = __webpack_exports__.AutoModelForSemanticSegmentation; +var __webpack_exports__AutoModelForSeq2SeqLM = __webpack_exports__.AutoModelForSeq2SeqLM; +var __webpack_exports__AutoModelForSequenceClassification = __webpack_exports__.AutoModelForSequenceClassification; +var __webpack_exports__AutoModelForSpeechSeq2Seq = __webpack_exports__.AutoModelForSpeechSeq2Seq; +var __webpack_exports__AutoModelForTextToSpectrogram = __webpack_exports__.AutoModelForTextToSpectrogram; +var __webpack_exports__AutoModelForTextToWaveform = __webpack_exports__.AutoModelForTextToWaveform; +var __webpack_exports__AutoModelForTokenClassification = __webpack_exports__.AutoModelForTokenClassification; +var __webpack_exports__AutoModelForUniversalSegmentation = __webpack_exports__.AutoModelForUniversalSegmentation; +var __webpack_exports__AutoModelForVision2Seq = __webpack_exports__.AutoModelForVision2Seq; +var __webpack_exports__AutoModelForXVector = __webpack_exports__.AutoModelForXVector; +var __webpack_exports__AutoModelForZeroShotObjectDetection = __webpack_exports__.AutoModelForZeroShotObjectDetection; +var __webpack_exports__AutoProcessor = __webpack_exports__.AutoProcessor; +var __webpack_exports__AutoTokenizer = __webpack_exports__.AutoTokenizer; +var __webpack_exports__AutomaticSpeechRecognitionPipeline = __webpack_exports__.AutomaticSpeechRecognitionPipeline; +var __webpack_exports__BartForConditionalGeneration = __webpack_exports__.BartForConditionalGeneration; +var __webpack_exports__BartForSequenceClassification = __webpack_exports__.BartForSequenceClassification; +var __webpack_exports__BartModel = __webpack_exports__.BartModel; +var __webpack_exports__BartPretrainedModel = __webpack_exports__.BartPretrainedModel; +var __webpack_exports__BartTokenizer = __webpack_exports__.BartTokenizer; +var __webpack_exports__BaseModelOutput = __webpack_exports__.BaseModelOutput; +var __webpack_exports__BaseStreamer = __webpack_exports__.BaseStreamer; +var __webpack_exports__BeitFeatureExtractor = __webpack_exports__.BeitFeatureExtractor; +var __webpack_exports__BeitForImageClassification = __webpack_exports__.BeitForImageClassification; +var __webpack_exports__BeitModel = __webpack_exports__.BeitModel; +var __webpack_exports__BeitPreTrainedModel = __webpack_exports__.BeitPreTrainedModel; +var __webpack_exports__BertForMaskedLM = __webpack_exports__.BertForMaskedLM; +var __webpack_exports__BertForQuestionAnswering = __webpack_exports__.BertForQuestionAnswering; +var __webpack_exports__BertForSequenceClassification = __webpack_exports__.BertForSequenceClassification; +var __webpack_exports__BertForTokenClassification = __webpack_exports__.BertForTokenClassification; +var __webpack_exports__BertModel = __webpack_exports__.BertModel; +var __webpack_exports__BertPreTrainedModel = __webpack_exports__.BertPreTrainedModel; +var __webpack_exports__BertTokenizer = __webpack_exports__.BertTokenizer; +var __webpack_exports__BitImageProcessor = __webpack_exports__.BitImageProcessor; +var __webpack_exports__BlenderbotForConditionalGeneration = __webpack_exports__.BlenderbotForConditionalGeneration; +var __webpack_exports__BlenderbotModel = __webpack_exports__.BlenderbotModel; +var __webpack_exports__BlenderbotPreTrainedModel = __webpack_exports__.BlenderbotPreTrainedModel; +var __webpack_exports__BlenderbotSmallForConditionalGeneration = __webpack_exports__.BlenderbotSmallForConditionalGeneration; +var __webpack_exports__BlenderbotSmallModel = __webpack_exports__.BlenderbotSmallModel; +var __webpack_exports__BlenderbotSmallPreTrainedModel = __webpack_exports__.BlenderbotSmallPreTrainedModel; +var __webpack_exports__BlenderbotSmallTokenizer = __webpack_exports__.BlenderbotSmallTokenizer; +var __webpack_exports__BlenderbotTokenizer = __webpack_exports__.BlenderbotTokenizer; +var __webpack_exports__BloomForCausalLM = __webpack_exports__.BloomForCausalLM; +var __webpack_exports__BloomModel = __webpack_exports__.BloomModel; +var __webpack_exports__BloomPreTrainedModel = __webpack_exports__.BloomPreTrainedModel; +var __webpack_exports__BloomTokenizer = __webpack_exports__.BloomTokenizer; +var __webpack_exports__CLIPFeatureExtractor = __webpack_exports__.CLIPFeatureExtractor; +var __webpack_exports__CLIPImageProcessor = __webpack_exports__.CLIPImageProcessor; +var __webpack_exports__CLIPModel = __webpack_exports__.CLIPModel; +var __webpack_exports__CLIPPreTrainedModel = __webpack_exports__.CLIPPreTrainedModel; +var __webpack_exports__CLIPSegForImageSegmentation = __webpack_exports__.CLIPSegForImageSegmentation; +var __webpack_exports__CLIPSegModel = __webpack_exports__.CLIPSegModel; +var __webpack_exports__CLIPSegPreTrainedModel = __webpack_exports__.CLIPSegPreTrainedModel; +var __webpack_exports__CLIPTextModel = __webpack_exports__.CLIPTextModel; +var __webpack_exports__CLIPTextModelWithProjection = __webpack_exports__.CLIPTextModelWithProjection; +var __webpack_exports__CLIPTokenizer = __webpack_exports__.CLIPTokenizer; +var __webpack_exports__CLIPVisionModel = __webpack_exports__.CLIPVisionModel; +var __webpack_exports__CLIPVisionModelWithProjection = __webpack_exports__.CLIPVisionModelWithProjection; +var __webpack_exports__CamembertForMaskedLM = __webpack_exports__.CamembertForMaskedLM; +var __webpack_exports__CamembertForQuestionAnswering = __webpack_exports__.CamembertForQuestionAnswering; +var __webpack_exports__CamembertForSequenceClassification = __webpack_exports__.CamembertForSequenceClassification; +var __webpack_exports__CamembertForTokenClassification = __webpack_exports__.CamembertForTokenClassification; +var __webpack_exports__CamembertModel = __webpack_exports__.CamembertModel; +var __webpack_exports__CamembertPreTrainedModel = __webpack_exports__.CamembertPreTrainedModel; +var __webpack_exports__CamembertTokenizer = __webpack_exports__.CamembertTokenizer; +var __webpack_exports__CausalLMOutput = __webpack_exports__.CausalLMOutput; +var __webpack_exports__CausalLMOutputWithPast = __webpack_exports__.CausalLMOutputWithPast; +var __webpack_exports__ChineseCLIPFeatureExtractor = __webpack_exports__.ChineseCLIPFeatureExtractor; +var __webpack_exports__ChineseCLIPModel = __webpack_exports__.ChineseCLIPModel; +var __webpack_exports__ChineseCLIPPreTrainedModel = __webpack_exports__.ChineseCLIPPreTrainedModel; +var __webpack_exports__ClapAudioModelWithProjection = __webpack_exports__.ClapAudioModelWithProjection; +var __webpack_exports__ClapFeatureExtractor = __webpack_exports__.ClapFeatureExtractor; +var __webpack_exports__ClapModel = __webpack_exports__.ClapModel; +var __webpack_exports__ClapPreTrainedModel = __webpack_exports__.ClapPreTrainedModel; +var __webpack_exports__ClapTextModelWithProjection = __webpack_exports__.ClapTextModelWithProjection; +var __webpack_exports__CodeGenForCausalLM = __webpack_exports__.CodeGenForCausalLM; +var __webpack_exports__CodeGenModel = __webpack_exports__.CodeGenModel; +var __webpack_exports__CodeGenPreTrainedModel = __webpack_exports__.CodeGenPreTrainedModel; +var __webpack_exports__CodeGenTokenizer = __webpack_exports__.CodeGenTokenizer; +var __webpack_exports__CodeLlamaTokenizer = __webpack_exports__.CodeLlamaTokenizer; +var __webpack_exports__CohereForCausalLM = __webpack_exports__.CohereForCausalLM; +var __webpack_exports__CohereModel = __webpack_exports__.CohereModel; +var __webpack_exports__CoherePreTrainedModel = __webpack_exports__.CoherePreTrainedModel; +var __webpack_exports__CohereTokenizer = __webpack_exports__.CohereTokenizer; +var __webpack_exports__ConvBertForMaskedLM = __webpack_exports__.ConvBertForMaskedLM; +var __webpack_exports__ConvBertForQuestionAnswering = __webpack_exports__.ConvBertForQuestionAnswering; +var __webpack_exports__ConvBertForSequenceClassification = __webpack_exports__.ConvBertForSequenceClassification; +var __webpack_exports__ConvBertForTokenClassification = __webpack_exports__.ConvBertForTokenClassification; +var __webpack_exports__ConvBertModel = __webpack_exports__.ConvBertModel; +var __webpack_exports__ConvBertPreTrainedModel = __webpack_exports__.ConvBertPreTrainedModel; +var __webpack_exports__ConvBertTokenizer = __webpack_exports__.ConvBertTokenizer; +var __webpack_exports__ConvNextFeatureExtractor = __webpack_exports__.ConvNextFeatureExtractor; +var __webpack_exports__ConvNextForImageClassification = __webpack_exports__.ConvNextForImageClassification; +var __webpack_exports__ConvNextImageProcessor = __webpack_exports__.ConvNextImageProcessor; +var __webpack_exports__ConvNextModel = __webpack_exports__.ConvNextModel; +var __webpack_exports__ConvNextPreTrainedModel = __webpack_exports__.ConvNextPreTrainedModel; +var __webpack_exports__ConvNextV2ForImageClassification = __webpack_exports__.ConvNextV2ForImageClassification; +var __webpack_exports__ConvNextV2Model = __webpack_exports__.ConvNextV2Model; +var __webpack_exports__ConvNextV2PreTrainedModel = __webpack_exports__.ConvNextV2PreTrainedModel; +var __webpack_exports__DPTFeatureExtractor = __webpack_exports__.DPTFeatureExtractor; +var __webpack_exports__DPTForDepthEstimation = __webpack_exports__.DPTForDepthEstimation; +var __webpack_exports__DPTImageProcessor = __webpack_exports__.DPTImageProcessor; +var __webpack_exports__DPTModel = __webpack_exports__.DPTModel; +var __webpack_exports__DPTPreTrainedModel = __webpack_exports__.DPTPreTrainedModel; +var __webpack_exports__DebertaForMaskedLM = __webpack_exports__.DebertaForMaskedLM; +var __webpack_exports__DebertaForQuestionAnswering = __webpack_exports__.DebertaForQuestionAnswering; +var __webpack_exports__DebertaForSequenceClassification = __webpack_exports__.DebertaForSequenceClassification; +var __webpack_exports__DebertaForTokenClassification = __webpack_exports__.DebertaForTokenClassification; +var __webpack_exports__DebertaModel = __webpack_exports__.DebertaModel; +var __webpack_exports__DebertaPreTrainedModel = __webpack_exports__.DebertaPreTrainedModel; +var __webpack_exports__DebertaTokenizer = __webpack_exports__.DebertaTokenizer; +var __webpack_exports__DebertaV2ForMaskedLM = __webpack_exports__.DebertaV2ForMaskedLM; +var __webpack_exports__DebertaV2ForQuestionAnswering = __webpack_exports__.DebertaV2ForQuestionAnswering; +var __webpack_exports__DebertaV2ForSequenceClassification = __webpack_exports__.DebertaV2ForSequenceClassification; +var __webpack_exports__DebertaV2ForTokenClassification = __webpack_exports__.DebertaV2ForTokenClassification; +var __webpack_exports__DebertaV2Model = __webpack_exports__.DebertaV2Model; +var __webpack_exports__DebertaV2PreTrainedModel = __webpack_exports__.DebertaV2PreTrainedModel; +var __webpack_exports__DebertaV2Tokenizer = __webpack_exports__.DebertaV2Tokenizer; +var __webpack_exports__DecisionTransformerModel = __webpack_exports__.DecisionTransformerModel; +var __webpack_exports__DecisionTransformerPreTrainedModel = __webpack_exports__.DecisionTransformerPreTrainedModel; +var __webpack_exports__DeiTFeatureExtractor = __webpack_exports__.DeiTFeatureExtractor; +var __webpack_exports__DeiTForImageClassification = __webpack_exports__.DeiTForImageClassification; +var __webpack_exports__DeiTModel = __webpack_exports__.DeiTModel; +var __webpack_exports__DeiTPreTrainedModel = __webpack_exports__.DeiTPreTrainedModel; +var __webpack_exports__DepthAnythingForDepthEstimation = __webpack_exports__.DepthAnythingForDepthEstimation; +var __webpack_exports__DepthAnythingPreTrainedModel = __webpack_exports__.DepthAnythingPreTrainedModel; +var __webpack_exports__DepthEstimationPipeline = __webpack_exports__.DepthEstimationPipeline; +var __webpack_exports__DepthProForDepthEstimation = __webpack_exports__.DepthProForDepthEstimation; +var __webpack_exports__DepthProPreTrainedModel = __webpack_exports__.DepthProPreTrainedModel; +var __webpack_exports__DetrFeatureExtractor = __webpack_exports__.DetrFeatureExtractor; +var __webpack_exports__DetrForObjectDetection = __webpack_exports__.DetrForObjectDetection; +var __webpack_exports__DetrForSegmentation = __webpack_exports__.DetrForSegmentation; +var __webpack_exports__DetrModel = __webpack_exports__.DetrModel; +var __webpack_exports__DetrObjectDetectionOutput = __webpack_exports__.DetrObjectDetectionOutput; +var __webpack_exports__DetrPreTrainedModel = __webpack_exports__.DetrPreTrainedModel; +var __webpack_exports__DetrSegmentationOutput = __webpack_exports__.DetrSegmentationOutput; +var __webpack_exports__Dinov2ForImageClassification = __webpack_exports__.Dinov2ForImageClassification; +var __webpack_exports__Dinov2Model = __webpack_exports__.Dinov2Model; +var __webpack_exports__Dinov2PreTrainedModel = __webpack_exports__.Dinov2PreTrainedModel; +var __webpack_exports__DistilBertForMaskedLM = __webpack_exports__.DistilBertForMaskedLM; +var __webpack_exports__DistilBertForQuestionAnswering = __webpack_exports__.DistilBertForQuestionAnswering; +var __webpack_exports__DistilBertForSequenceClassification = __webpack_exports__.DistilBertForSequenceClassification; +var __webpack_exports__DistilBertForTokenClassification = __webpack_exports__.DistilBertForTokenClassification; +var __webpack_exports__DistilBertModel = __webpack_exports__.DistilBertModel; +var __webpack_exports__DistilBertPreTrainedModel = __webpack_exports__.DistilBertPreTrainedModel; +var __webpack_exports__DistilBertTokenizer = __webpack_exports__.DistilBertTokenizer; +var __webpack_exports__DocumentQuestionAnsweringPipeline = __webpack_exports__.DocumentQuestionAnsweringPipeline; +var __webpack_exports__DonutFeatureExtractor = __webpack_exports__.DonutFeatureExtractor; +var __webpack_exports__DonutImageProcessor = __webpack_exports__.DonutImageProcessor; +var __webpack_exports__DonutSwinModel = __webpack_exports__.DonutSwinModel; +var __webpack_exports__DonutSwinPreTrainedModel = __webpack_exports__.DonutSwinPreTrainedModel; +var __webpack_exports__EfficientNetForImageClassification = __webpack_exports__.EfficientNetForImageClassification; +var __webpack_exports__EfficientNetImageProcessor = __webpack_exports__.EfficientNetImageProcessor; +var __webpack_exports__EfficientNetModel = __webpack_exports__.EfficientNetModel; +var __webpack_exports__EfficientNetPreTrainedModel = __webpack_exports__.EfficientNetPreTrainedModel; +var __webpack_exports__ElectraForMaskedLM = __webpack_exports__.ElectraForMaskedLM; +var __webpack_exports__ElectraForQuestionAnswering = __webpack_exports__.ElectraForQuestionAnswering; +var __webpack_exports__ElectraForSequenceClassification = __webpack_exports__.ElectraForSequenceClassification; +var __webpack_exports__ElectraForTokenClassification = __webpack_exports__.ElectraForTokenClassification; +var __webpack_exports__ElectraModel = __webpack_exports__.ElectraModel; +var __webpack_exports__ElectraPreTrainedModel = __webpack_exports__.ElectraPreTrainedModel; +var __webpack_exports__ElectraTokenizer = __webpack_exports__.ElectraTokenizer; +var __webpack_exports__EosTokenCriteria = __webpack_exports__.EosTokenCriteria; +var __webpack_exports__EsmForMaskedLM = __webpack_exports__.EsmForMaskedLM; +var __webpack_exports__EsmForSequenceClassification = __webpack_exports__.EsmForSequenceClassification; +var __webpack_exports__EsmForTokenClassification = __webpack_exports__.EsmForTokenClassification; +var __webpack_exports__EsmModel = __webpack_exports__.EsmModel; +var __webpack_exports__EsmPreTrainedModel = __webpack_exports__.EsmPreTrainedModel; +var __webpack_exports__EsmTokenizer = __webpack_exports__.EsmTokenizer; +var __webpack_exports__FFT = __webpack_exports__.FFT; +var __webpack_exports__FalconForCausalLM = __webpack_exports__.FalconForCausalLM; +var __webpack_exports__FalconModel = __webpack_exports__.FalconModel; +var __webpack_exports__FalconPreTrainedModel = __webpack_exports__.FalconPreTrainedModel; +var __webpack_exports__FalconTokenizer = __webpack_exports__.FalconTokenizer; +var __webpack_exports__FastViTForImageClassification = __webpack_exports__.FastViTForImageClassification; +var __webpack_exports__FastViTModel = __webpack_exports__.FastViTModel; +var __webpack_exports__FastViTPreTrainedModel = __webpack_exports__.FastViTPreTrainedModel; +var __webpack_exports__FeatureExtractionPipeline = __webpack_exports__.FeatureExtractionPipeline; +var __webpack_exports__FeatureExtractor = __webpack_exports__.FeatureExtractor; +var __webpack_exports__FillMaskPipeline = __webpack_exports__.FillMaskPipeline; +var __webpack_exports__Florence2ForConditionalGeneration = __webpack_exports__.Florence2ForConditionalGeneration; +var __webpack_exports__Florence2PreTrainedModel = __webpack_exports__.Florence2PreTrainedModel; +var __webpack_exports__Florence2Processor = __webpack_exports__.Florence2Processor; +var __webpack_exports__GLPNFeatureExtractor = __webpack_exports__.GLPNFeatureExtractor; +var __webpack_exports__GLPNForDepthEstimation = __webpack_exports__.GLPNForDepthEstimation; +var __webpack_exports__GLPNModel = __webpack_exports__.GLPNModel; +var __webpack_exports__GLPNPreTrainedModel = __webpack_exports__.GLPNPreTrainedModel; +var __webpack_exports__GPT2LMHeadModel = __webpack_exports__.GPT2LMHeadModel; +var __webpack_exports__GPT2Model = __webpack_exports__.GPT2Model; +var __webpack_exports__GPT2PreTrainedModel = __webpack_exports__.GPT2PreTrainedModel; +var __webpack_exports__GPT2Tokenizer = __webpack_exports__.GPT2Tokenizer; +var __webpack_exports__GPTBigCodeForCausalLM = __webpack_exports__.GPTBigCodeForCausalLM; +var __webpack_exports__GPTBigCodeModel = __webpack_exports__.GPTBigCodeModel; +var __webpack_exports__GPTBigCodePreTrainedModel = __webpack_exports__.GPTBigCodePreTrainedModel; +var __webpack_exports__GPTJForCausalLM = __webpack_exports__.GPTJForCausalLM; +var __webpack_exports__GPTJModel = __webpack_exports__.GPTJModel; +var __webpack_exports__GPTJPreTrainedModel = __webpack_exports__.GPTJPreTrainedModel; +var __webpack_exports__GPTNeoForCausalLM = __webpack_exports__.GPTNeoForCausalLM; +var __webpack_exports__GPTNeoModel = __webpack_exports__.GPTNeoModel; +var __webpack_exports__GPTNeoPreTrainedModel = __webpack_exports__.GPTNeoPreTrainedModel; +var __webpack_exports__GPTNeoXForCausalLM = __webpack_exports__.GPTNeoXForCausalLM; +var __webpack_exports__GPTNeoXModel = __webpack_exports__.GPTNeoXModel; +var __webpack_exports__GPTNeoXPreTrainedModel = __webpack_exports__.GPTNeoXPreTrainedModel; +var __webpack_exports__GPTNeoXTokenizer = __webpack_exports__.GPTNeoXTokenizer; +var __webpack_exports__Gemma2ForCausalLM = __webpack_exports__.Gemma2ForCausalLM; +var __webpack_exports__Gemma2Model = __webpack_exports__.Gemma2Model; +var __webpack_exports__Gemma2PreTrainedModel = __webpack_exports__.Gemma2PreTrainedModel; +var __webpack_exports__GemmaForCausalLM = __webpack_exports__.GemmaForCausalLM; +var __webpack_exports__GemmaModel = __webpack_exports__.GemmaModel; +var __webpack_exports__GemmaPreTrainedModel = __webpack_exports__.GemmaPreTrainedModel; +var __webpack_exports__GemmaTokenizer = __webpack_exports__.GemmaTokenizer; +var __webpack_exports__GraniteForCausalLM = __webpack_exports__.GraniteForCausalLM; +var __webpack_exports__GraniteModel = __webpack_exports__.GraniteModel; +var __webpack_exports__GranitePreTrainedModel = __webpack_exports__.GranitePreTrainedModel; +var __webpack_exports__Grok1Tokenizer = __webpack_exports__.Grok1Tokenizer; +var __webpack_exports__GroupViTModel = __webpack_exports__.GroupViTModel; +var __webpack_exports__GroupViTPreTrainedModel = __webpack_exports__.GroupViTPreTrainedModel; +var __webpack_exports__HerbertTokenizer = __webpack_exports__.HerbertTokenizer; +var __webpack_exports__HieraForImageClassification = __webpack_exports__.HieraForImageClassification; +var __webpack_exports__HieraModel = __webpack_exports__.HieraModel; +var __webpack_exports__HieraPreTrainedModel = __webpack_exports__.HieraPreTrainedModel; +var __webpack_exports__HubertForCTC = __webpack_exports__.HubertForCTC; +var __webpack_exports__HubertForSequenceClassification = __webpack_exports__.HubertForSequenceClassification; +var __webpack_exports__HubertModel = __webpack_exports__.HubertModel; +var __webpack_exports__HubertPreTrainedModel = __webpack_exports__.HubertPreTrainedModel; +var __webpack_exports__ImageClassificationPipeline = __webpack_exports__.ImageClassificationPipeline; +var __webpack_exports__ImageFeatureExtractionPipeline = __webpack_exports__.ImageFeatureExtractionPipeline; +var __webpack_exports__ImageFeatureExtractor = __webpack_exports__.ImageFeatureExtractor; +var __webpack_exports__ImageMattingOutput = __webpack_exports__.ImageMattingOutput; +var __webpack_exports__ImageSegmentationPipeline = __webpack_exports__.ImageSegmentationPipeline; +var __webpack_exports__ImageToImagePipeline = __webpack_exports__.ImageToImagePipeline; +var __webpack_exports__ImageToTextPipeline = __webpack_exports__.ImageToTextPipeline; +var __webpack_exports__InterruptableStoppingCriteria = __webpack_exports__.InterruptableStoppingCriteria; +var __webpack_exports__JAISLMHeadModel = __webpack_exports__.JAISLMHeadModel; +var __webpack_exports__JAISModel = __webpack_exports__.JAISModel; +var __webpack_exports__JAISPreTrainedModel = __webpack_exports__.JAISPreTrainedModel; +var __webpack_exports__LlamaForCausalLM = __webpack_exports__.LlamaForCausalLM; +var __webpack_exports__LlamaModel = __webpack_exports__.LlamaModel; +var __webpack_exports__LlamaPreTrainedModel = __webpack_exports__.LlamaPreTrainedModel; +var __webpack_exports__LlamaTokenizer = __webpack_exports__.LlamaTokenizer; +var __webpack_exports__LlavaForConditionalGeneration = __webpack_exports__.LlavaForConditionalGeneration; +var __webpack_exports__LlavaPreTrainedModel = __webpack_exports__.LlavaPreTrainedModel; +var __webpack_exports__LongT5ForConditionalGeneration = __webpack_exports__.LongT5ForConditionalGeneration; +var __webpack_exports__LongT5Model = __webpack_exports__.LongT5Model; +var __webpack_exports__LongT5PreTrainedModel = __webpack_exports__.LongT5PreTrainedModel; +var __webpack_exports__M2M100ForConditionalGeneration = __webpack_exports__.M2M100ForConditionalGeneration; +var __webpack_exports__M2M100Model = __webpack_exports__.M2M100Model; +var __webpack_exports__M2M100PreTrainedModel = __webpack_exports__.M2M100PreTrainedModel; +var __webpack_exports__M2M100Tokenizer = __webpack_exports__.M2M100Tokenizer; +var __webpack_exports__MBart50Tokenizer = __webpack_exports__.MBart50Tokenizer; +var __webpack_exports__MBartForCausalLM = __webpack_exports__.MBartForCausalLM; +var __webpack_exports__MBartForConditionalGeneration = __webpack_exports__.MBartForConditionalGeneration; +var __webpack_exports__MBartForSequenceClassification = __webpack_exports__.MBartForSequenceClassification; +var __webpack_exports__MBartModel = __webpack_exports__.MBartModel; +var __webpack_exports__MBartPreTrainedModel = __webpack_exports__.MBartPreTrainedModel; +var __webpack_exports__MBartTokenizer = __webpack_exports__.MBartTokenizer; +var __webpack_exports__MPNetForMaskedLM = __webpack_exports__.MPNetForMaskedLM; +var __webpack_exports__MPNetForQuestionAnswering = __webpack_exports__.MPNetForQuestionAnswering; +var __webpack_exports__MPNetForSequenceClassification = __webpack_exports__.MPNetForSequenceClassification; +var __webpack_exports__MPNetForTokenClassification = __webpack_exports__.MPNetForTokenClassification; +var __webpack_exports__MPNetModel = __webpack_exports__.MPNetModel; +var __webpack_exports__MPNetPreTrainedModel = __webpack_exports__.MPNetPreTrainedModel; +var __webpack_exports__MPNetTokenizer = __webpack_exports__.MPNetTokenizer; +var __webpack_exports__MT5ForConditionalGeneration = __webpack_exports__.MT5ForConditionalGeneration; +var __webpack_exports__MT5Model = __webpack_exports__.MT5Model; +var __webpack_exports__MT5PreTrainedModel = __webpack_exports__.MT5PreTrainedModel; +var __webpack_exports__MarianMTModel = __webpack_exports__.MarianMTModel; +var __webpack_exports__MarianModel = __webpack_exports__.MarianModel; +var __webpack_exports__MarianPreTrainedModel = __webpack_exports__.MarianPreTrainedModel; +var __webpack_exports__MarianTokenizer = __webpack_exports__.MarianTokenizer; +var __webpack_exports__MaskFormerFeatureExtractor = __webpack_exports__.MaskFormerFeatureExtractor; +var __webpack_exports__MaskFormerForInstanceSegmentation = __webpack_exports__.MaskFormerForInstanceSegmentation; +var __webpack_exports__MaskFormerModel = __webpack_exports__.MaskFormerModel; +var __webpack_exports__MaskFormerPreTrainedModel = __webpack_exports__.MaskFormerPreTrainedModel; +var __webpack_exports__MaskedLMOutput = __webpack_exports__.MaskedLMOutput; +var __webpack_exports__MaxLengthCriteria = __webpack_exports__.MaxLengthCriteria; +var __webpack_exports__MistralForCausalLM = __webpack_exports__.MistralForCausalLM; +var __webpack_exports__MistralModel = __webpack_exports__.MistralModel; +var __webpack_exports__MistralPreTrainedModel = __webpack_exports__.MistralPreTrainedModel; +var __webpack_exports__MobileBertForMaskedLM = __webpack_exports__.MobileBertForMaskedLM; +var __webpack_exports__MobileBertForQuestionAnswering = __webpack_exports__.MobileBertForQuestionAnswering; +var __webpack_exports__MobileBertForSequenceClassification = __webpack_exports__.MobileBertForSequenceClassification; +var __webpack_exports__MobileBertModel = __webpack_exports__.MobileBertModel; +var __webpack_exports__MobileBertPreTrainedModel = __webpack_exports__.MobileBertPreTrainedModel; +var __webpack_exports__MobileBertTokenizer = __webpack_exports__.MobileBertTokenizer; +var __webpack_exports__MobileLLMForCausalLM = __webpack_exports__.MobileLLMForCausalLM; +var __webpack_exports__MobileLLMModel = __webpack_exports__.MobileLLMModel; +var __webpack_exports__MobileLLMPreTrainedModel = __webpack_exports__.MobileLLMPreTrainedModel; +var __webpack_exports__MobileNetV1FeatureExtractor = __webpack_exports__.MobileNetV1FeatureExtractor; +var __webpack_exports__MobileNetV1ForImageClassification = __webpack_exports__.MobileNetV1ForImageClassification; +var __webpack_exports__MobileNetV1Model = __webpack_exports__.MobileNetV1Model; +var __webpack_exports__MobileNetV1PreTrainedModel = __webpack_exports__.MobileNetV1PreTrainedModel; +var __webpack_exports__MobileNetV2FeatureExtractor = __webpack_exports__.MobileNetV2FeatureExtractor; +var __webpack_exports__MobileNetV2ForImageClassification = __webpack_exports__.MobileNetV2ForImageClassification; +var __webpack_exports__MobileNetV2Model = __webpack_exports__.MobileNetV2Model; +var __webpack_exports__MobileNetV2PreTrainedModel = __webpack_exports__.MobileNetV2PreTrainedModel; +var __webpack_exports__MobileNetV3FeatureExtractor = __webpack_exports__.MobileNetV3FeatureExtractor; +var __webpack_exports__MobileNetV3ForImageClassification = __webpack_exports__.MobileNetV3ForImageClassification; +var __webpack_exports__MobileNetV3Model = __webpack_exports__.MobileNetV3Model; +var __webpack_exports__MobileNetV3PreTrainedModel = __webpack_exports__.MobileNetV3PreTrainedModel; +var __webpack_exports__MobileNetV4FeatureExtractor = __webpack_exports__.MobileNetV4FeatureExtractor; +var __webpack_exports__MobileNetV4ForImageClassification = __webpack_exports__.MobileNetV4ForImageClassification; +var __webpack_exports__MobileNetV4Model = __webpack_exports__.MobileNetV4Model; +var __webpack_exports__MobileNetV4PreTrainedModel = __webpack_exports__.MobileNetV4PreTrainedModel; +var __webpack_exports__MobileViTFeatureExtractor = __webpack_exports__.MobileViTFeatureExtractor; +var __webpack_exports__MobileViTForImageClassification = __webpack_exports__.MobileViTForImageClassification; +var __webpack_exports__MobileViTImageProcessor = __webpack_exports__.MobileViTImageProcessor; +var __webpack_exports__MobileViTModel = __webpack_exports__.MobileViTModel; +var __webpack_exports__MobileViTPreTrainedModel = __webpack_exports__.MobileViTPreTrainedModel; +var __webpack_exports__MobileViTV2ForImageClassification = __webpack_exports__.MobileViTV2ForImageClassification; +var __webpack_exports__MobileViTV2Model = __webpack_exports__.MobileViTV2Model; +var __webpack_exports__MobileViTV2PreTrainedModel = __webpack_exports__.MobileViTV2PreTrainedModel; +var __webpack_exports__ModelOutput = __webpack_exports__.ModelOutput; +var __webpack_exports__Moondream1ForConditionalGeneration = __webpack_exports__.Moondream1ForConditionalGeneration; +var __webpack_exports__MptForCausalLM = __webpack_exports__.MptForCausalLM; +var __webpack_exports__MptModel = __webpack_exports__.MptModel; +var __webpack_exports__MptPreTrainedModel = __webpack_exports__.MptPreTrainedModel; +var __webpack_exports__MusicgenForCausalLM = __webpack_exports__.MusicgenForCausalLM; +var __webpack_exports__MusicgenForConditionalGeneration = __webpack_exports__.MusicgenForConditionalGeneration; +var __webpack_exports__MusicgenModel = __webpack_exports__.MusicgenModel; +var __webpack_exports__MusicgenPreTrainedModel = __webpack_exports__.MusicgenPreTrainedModel; +var __webpack_exports__NllbTokenizer = __webpack_exports__.NllbTokenizer; +var __webpack_exports__NomicBertModel = __webpack_exports__.NomicBertModel; +var __webpack_exports__NomicBertPreTrainedModel = __webpack_exports__.NomicBertPreTrainedModel; +var __webpack_exports__NougatImageProcessor = __webpack_exports__.NougatImageProcessor; +var __webpack_exports__NougatTokenizer = __webpack_exports__.NougatTokenizer; +var __webpack_exports__OPTForCausalLM = __webpack_exports__.OPTForCausalLM; +var __webpack_exports__OPTModel = __webpack_exports__.OPTModel; +var __webpack_exports__OPTPreTrainedModel = __webpack_exports__.OPTPreTrainedModel; +var __webpack_exports__ObjectDetectionPipeline = __webpack_exports__.ObjectDetectionPipeline; +var __webpack_exports__OlmoForCausalLM = __webpack_exports__.OlmoForCausalLM; +var __webpack_exports__OlmoModel = __webpack_exports__.OlmoModel; +var __webpack_exports__OlmoPreTrainedModel = __webpack_exports__.OlmoPreTrainedModel; +var __webpack_exports__OpenELMForCausalLM = __webpack_exports__.OpenELMForCausalLM; +var __webpack_exports__OpenELMModel = __webpack_exports__.OpenELMModel; +var __webpack_exports__OpenELMPreTrainedModel = __webpack_exports__.OpenELMPreTrainedModel; +var __webpack_exports__OwlViTFeatureExtractor = __webpack_exports__.OwlViTFeatureExtractor; +var __webpack_exports__OwlViTForObjectDetection = __webpack_exports__.OwlViTForObjectDetection; +var __webpack_exports__OwlViTModel = __webpack_exports__.OwlViTModel; +var __webpack_exports__OwlViTPreTrainedModel = __webpack_exports__.OwlViTPreTrainedModel; +var __webpack_exports__OwlViTProcessor = __webpack_exports__.OwlViTProcessor; +var __webpack_exports__Owlv2ForObjectDetection = __webpack_exports__.Owlv2ForObjectDetection; +var __webpack_exports__Owlv2ImageProcessor = __webpack_exports__.Owlv2ImageProcessor; +var __webpack_exports__Owlv2Model = __webpack_exports__.Owlv2Model; +var __webpack_exports__Owlv2PreTrainedModel = __webpack_exports__.Owlv2PreTrainedModel; +var __webpack_exports__Phi3ForCausalLM = __webpack_exports__.Phi3ForCausalLM; +var __webpack_exports__Phi3Model = __webpack_exports__.Phi3Model; +var __webpack_exports__Phi3PreTrainedModel = __webpack_exports__.Phi3PreTrainedModel; +var __webpack_exports__PhiForCausalLM = __webpack_exports__.PhiForCausalLM; +var __webpack_exports__PhiModel = __webpack_exports__.PhiModel; +var __webpack_exports__PhiPreTrainedModel = __webpack_exports__.PhiPreTrainedModel; +var __webpack_exports__Pipeline = __webpack_exports__.Pipeline; +var __webpack_exports__PreTrainedModel = __webpack_exports__.PreTrainedModel; +var __webpack_exports__PreTrainedTokenizer = __webpack_exports__.PreTrainedTokenizer; +var __webpack_exports__PretrainedConfig = __webpack_exports__.PretrainedConfig; +var __webpack_exports__PretrainedMixin = __webpack_exports__.PretrainedMixin; +var __webpack_exports__Processor = __webpack_exports__.Processor; +var __webpack_exports__PvtForImageClassification = __webpack_exports__.PvtForImageClassification; +var __webpack_exports__PvtImageProcessor = __webpack_exports__.PvtImageProcessor; +var __webpack_exports__PvtModel = __webpack_exports__.PvtModel; +var __webpack_exports__PvtPreTrainedModel = __webpack_exports__.PvtPreTrainedModel; +var __webpack_exports__PyAnnoteFeatureExtractor = __webpack_exports__.PyAnnoteFeatureExtractor; +var __webpack_exports__PyAnnoteForAudioFrameClassification = __webpack_exports__.PyAnnoteForAudioFrameClassification; +var __webpack_exports__PyAnnoteModel = __webpack_exports__.PyAnnoteModel; +var __webpack_exports__PyAnnotePreTrainedModel = __webpack_exports__.PyAnnotePreTrainedModel; +var __webpack_exports__PyAnnoteProcessor = __webpack_exports__.PyAnnoteProcessor; +var __webpack_exports__QuestionAnsweringModelOutput = __webpack_exports__.QuestionAnsweringModelOutput; +var __webpack_exports__QuestionAnsweringPipeline = __webpack_exports__.QuestionAnsweringPipeline; +var __webpack_exports__Qwen2ForCausalLM = __webpack_exports__.Qwen2ForCausalLM; +var __webpack_exports__Qwen2Model = __webpack_exports__.Qwen2Model; +var __webpack_exports__Qwen2PreTrainedModel = __webpack_exports__.Qwen2PreTrainedModel; +var __webpack_exports__Qwen2Tokenizer = __webpack_exports__.Qwen2Tokenizer; +var __webpack_exports__RTDetrForObjectDetection = __webpack_exports__.RTDetrForObjectDetection; +var __webpack_exports__RTDetrImageProcessor = __webpack_exports__.RTDetrImageProcessor; +var __webpack_exports__RTDetrModel = __webpack_exports__.RTDetrModel; +var __webpack_exports__RTDetrObjectDetectionOutput = __webpack_exports__.RTDetrObjectDetectionOutput; +var __webpack_exports__RTDetrPreTrainedModel = __webpack_exports__.RTDetrPreTrainedModel; +var __webpack_exports__RawImage = __webpack_exports__.RawImage; +var __webpack_exports__ResNetForImageClassification = __webpack_exports__.ResNetForImageClassification; +var __webpack_exports__ResNetModel = __webpack_exports__.ResNetModel; +var __webpack_exports__ResNetPreTrainedModel = __webpack_exports__.ResNetPreTrainedModel; +var __webpack_exports__RoFormerForMaskedLM = __webpack_exports__.RoFormerForMaskedLM; +var __webpack_exports__RoFormerForQuestionAnswering = __webpack_exports__.RoFormerForQuestionAnswering; +var __webpack_exports__RoFormerForSequenceClassification = __webpack_exports__.RoFormerForSequenceClassification; +var __webpack_exports__RoFormerForTokenClassification = __webpack_exports__.RoFormerForTokenClassification; +var __webpack_exports__RoFormerModel = __webpack_exports__.RoFormerModel; +var __webpack_exports__RoFormerPreTrainedModel = __webpack_exports__.RoFormerPreTrainedModel; +var __webpack_exports__RoFormerTokenizer = __webpack_exports__.RoFormerTokenizer; +var __webpack_exports__RobertaForMaskedLM = __webpack_exports__.RobertaForMaskedLM; +var __webpack_exports__RobertaForQuestionAnswering = __webpack_exports__.RobertaForQuestionAnswering; +var __webpack_exports__RobertaForSequenceClassification = __webpack_exports__.RobertaForSequenceClassification; +var __webpack_exports__RobertaForTokenClassification = __webpack_exports__.RobertaForTokenClassification; +var __webpack_exports__RobertaModel = __webpack_exports__.RobertaModel; +var __webpack_exports__RobertaPreTrainedModel = __webpack_exports__.RobertaPreTrainedModel; +var __webpack_exports__RobertaTokenizer = __webpack_exports__.RobertaTokenizer; +var __webpack_exports__SamImageProcessor = __webpack_exports__.SamImageProcessor; +var __webpack_exports__SamImageSegmentationOutput = __webpack_exports__.SamImageSegmentationOutput; +var __webpack_exports__SamModel = __webpack_exports__.SamModel; +var __webpack_exports__SamPreTrainedModel = __webpack_exports__.SamPreTrainedModel; +var __webpack_exports__SamProcessor = __webpack_exports__.SamProcessor; +var __webpack_exports__SapiensFeatureExtractor = __webpack_exports__.SapiensFeatureExtractor; +var __webpack_exports__SapiensForDepthEstimation = __webpack_exports__.SapiensForDepthEstimation; +var __webpack_exports__SapiensForNormalEstimation = __webpack_exports__.SapiensForNormalEstimation; +var __webpack_exports__SapiensForSemanticSegmentation = __webpack_exports__.SapiensForSemanticSegmentation; +var __webpack_exports__SapiensPreTrainedModel = __webpack_exports__.SapiensPreTrainedModel; +var __webpack_exports__SeamlessM4TFeatureExtractor = __webpack_exports__.SeamlessM4TFeatureExtractor; +var __webpack_exports__SegformerFeatureExtractor = __webpack_exports__.SegformerFeatureExtractor; +var __webpack_exports__SegformerForImageClassification = __webpack_exports__.SegformerForImageClassification; +var __webpack_exports__SegformerForSemanticSegmentation = __webpack_exports__.SegformerForSemanticSegmentation; +var __webpack_exports__SegformerModel = __webpack_exports__.SegformerModel; +var __webpack_exports__SegformerPreTrainedModel = __webpack_exports__.SegformerPreTrainedModel; +var __webpack_exports__Seq2SeqLMOutput = __webpack_exports__.Seq2SeqLMOutput; +var __webpack_exports__SequenceClassifierOutput = __webpack_exports__.SequenceClassifierOutput; +var __webpack_exports__SiglipImageProcessor = __webpack_exports__.SiglipImageProcessor; +var __webpack_exports__SiglipModel = __webpack_exports__.SiglipModel; +var __webpack_exports__SiglipPreTrainedModel = __webpack_exports__.SiglipPreTrainedModel; +var __webpack_exports__SiglipTextModel = __webpack_exports__.SiglipTextModel; +var __webpack_exports__SiglipTokenizer = __webpack_exports__.SiglipTokenizer; +var __webpack_exports__SiglipVisionModel = __webpack_exports__.SiglipVisionModel; +var __webpack_exports__SpeechT5FeatureExtractor = __webpack_exports__.SpeechT5FeatureExtractor; +var __webpack_exports__SpeechT5ForSpeechToText = __webpack_exports__.SpeechT5ForSpeechToText; +var __webpack_exports__SpeechT5ForTextToSpeech = __webpack_exports__.SpeechT5ForTextToSpeech; +var __webpack_exports__SpeechT5HifiGan = __webpack_exports__.SpeechT5HifiGan; +var __webpack_exports__SpeechT5Model = __webpack_exports__.SpeechT5Model; +var __webpack_exports__SpeechT5PreTrainedModel = __webpack_exports__.SpeechT5PreTrainedModel; +var __webpack_exports__SpeechT5Processor = __webpack_exports__.SpeechT5Processor; +var __webpack_exports__SpeechT5Tokenizer = __webpack_exports__.SpeechT5Tokenizer; +var __webpack_exports__SqueezeBertForMaskedLM = __webpack_exports__.SqueezeBertForMaskedLM; +var __webpack_exports__SqueezeBertForQuestionAnswering = __webpack_exports__.SqueezeBertForQuestionAnswering; +var __webpack_exports__SqueezeBertForSequenceClassification = __webpack_exports__.SqueezeBertForSequenceClassification; +var __webpack_exports__SqueezeBertModel = __webpack_exports__.SqueezeBertModel; +var __webpack_exports__SqueezeBertPreTrainedModel = __webpack_exports__.SqueezeBertPreTrainedModel; +var __webpack_exports__SqueezeBertTokenizer = __webpack_exports__.SqueezeBertTokenizer; +var __webpack_exports__StableLmForCausalLM = __webpack_exports__.StableLmForCausalLM; +var __webpack_exports__StableLmModel = __webpack_exports__.StableLmModel; +var __webpack_exports__StableLmPreTrainedModel = __webpack_exports__.StableLmPreTrainedModel; +var __webpack_exports__Starcoder2ForCausalLM = __webpack_exports__.Starcoder2ForCausalLM; +var __webpack_exports__Starcoder2Model = __webpack_exports__.Starcoder2Model; +var __webpack_exports__Starcoder2PreTrainedModel = __webpack_exports__.Starcoder2PreTrainedModel; +var __webpack_exports__StoppingCriteria = __webpack_exports__.StoppingCriteria; +var __webpack_exports__StoppingCriteriaList = __webpack_exports__.StoppingCriteriaList; +var __webpack_exports__SummarizationPipeline = __webpack_exports__.SummarizationPipeline; +var __webpack_exports__Swin2SRForImageSuperResolution = __webpack_exports__.Swin2SRForImageSuperResolution; +var __webpack_exports__Swin2SRImageProcessor = __webpack_exports__.Swin2SRImageProcessor; +var __webpack_exports__Swin2SRModel = __webpack_exports__.Swin2SRModel; +var __webpack_exports__Swin2SRPreTrainedModel = __webpack_exports__.Swin2SRPreTrainedModel; +var __webpack_exports__SwinForImageClassification = __webpack_exports__.SwinForImageClassification; +var __webpack_exports__SwinModel = __webpack_exports__.SwinModel; +var __webpack_exports__SwinPreTrainedModel = __webpack_exports__.SwinPreTrainedModel; +var __webpack_exports__T5ForConditionalGeneration = __webpack_exports__.T5ForConditionalGeneration; +var __webpack_exports__T5Model = __webpack_exports__.T5Model; +var __webpack_exports__T5PreTrainedModel = __webpack_exports__.T5PreTrainedModel; +var __webpack_exports__T5Tokenizer = __webpack_exports__.T5Tokenizer; +var __webpack_exports__TableTransformerForObjectDetection = __webpack_exports__.TableTransformerForObjectDetection; +var __webpack_exports__TableTransformerModel = __webpack_exports__.TableTransformerModel; +var __webpack_exports__TableTransformerObjectDetectionOutput = __webpack_exports__.TableTransformerObjectDetectionOutput; +var __webpack_exports__TableTransformerPreTrainedModel = __webpack_exports__.TableTransformerPreTrainedModel; +var __webpack_exports__Tensor = __webpack_exports__.Tensor; +var __webpack_exports__Text2TextGenerationPipeline = __webpack_exports__.Text2TextGenerationPipeline; +var __webpack_exports__TextClassificationPipeline = __webpack_exports__.TextClassificationPipeline; +var __webpack_exports__TextGenerationPipeline = __webpack_exports__.TextGenerationPipeline; +var __webpack_exports__TextStreamer = __webpack_exports__.TextStreamer; +var __webpack_exports__TextToAudioPipeline = __webpack_exports__.TextToAudioPipeline; +var __webpack_exports__TokenClassificationPipeline = __webpack_exports__.TokenClassificationPipeline; +var __webpack_exports__TokenClassifierOutput = __webpack_exports__.TokenClassifierOutput; +var __webpack_exports__TokenizerModel = __webpack_exports__.TokenizerModel; +var __webpack_exports__TrOCRForCausalLM = __webpack_exports__.TrOCRForCausalLM; +var __webpack_exports__TrOCRPreTrainedModel = __webpack_exports__.TrOCRPreTrainedModel; +var __webpack_exports__TranslationPipeline = __webpack_exports__.TranslationPipeline; +var __webpack_exports__UniSpeechForCTC = __webpack_exports__.UniSpeechForCTC; +var __webpack_exports__UniSpeechForSequenceClassification = __webpack_exports__.UniSpeechForSequenceClassification; +var __webpack_exports__UniSpeechModel = __webpack_exports__.UniSpeechModel; +var __webpack_exports__UniSpeechPreTrainedModel = __webpack_exports__.UniSpeechPreTrainedModel; +var __webpack_exports__UniSpeechSatForAudioFrameClassification = __webpack_exports__.UniSpeechSatForAudioFrameClassification; +var __webpack_exports__UniSpeechSatForCTC = __webpack_exports__.UniSpeechSatForCTC; +var __webpack_exports__UniSpeechSatForSequenceClassification = __webpack_exports__.UniSpeechSatForSequenceClassification; +var __webpack_exports__UniSpeechSatModel = __webpack_exports__.UniSpeechSatModel; +var __webpack_exports__UniSpeechSatPreTrainedModel = __webpack_exports__.UniSpeechSatPreTrainedModel; +var __webpack_exports__ViTFeatureExtractor = __webpack_exports__.ViTFeatureExtractor; +var __webpack_exports__ViTForImageClassification = __webpack_exports__.ViTForImageClassification; +var __webpack_exports__ViTImageProcessor = __webpack_exports__.ViTImageProcessor; +var __webpack_exports__ViTMAEModel = __webpack_exports__.ViTMAEModel; +var __webpack_exports__ViTMAEPreTrainedModel = __webpack_exports__.ViTMAEPreTrainedModel; +var __webpack_exports__ViTMSNForImageClassification = __webpack_exports__.ViTMSNForImageClassification; +var __webpack_exports__ViTMSNModel = __webpack_exports__.ViTMSNModel; +var __webpack_exports__ViTMSNPreTrainedModel = __webpack_exports__.ViTMSNPreTrainedModel; +var __webpack_exports__ViTModel = __webpack_exports__.ViTModel; +var __webpack_exports__ViTPreTrainedModel = __webpack_exports__.ViTPreTrainedModel; +var __webpack_exports__VisionEncoderDecoderModel = __webpack_exports__.VisionEncoderDecoderModel; +var __webpack_exports__VitMatteForImageMatting = __webpack_exports__.VitMatteForImageMatting; +var __webpack_exports__VitMatteImageProcessor = __webpack_exports__.VitMatteImageProcessor; +var __webpack_exports__VitMattePreTrainedModel = __webpack_exports__.VitMattePreTrainedModel; +var __webpack_exports__VitsModel = __webpack_exports__.VitsModel; +var __webpack_exports__VitsModelOutput = __webpack_exports__.VitsModelOutput; +var __webpack_exports__VitsPreTrainedModel = __webpack_exports__.VitsPreTrainedModel; +var __webpack_exports__VitsTokenizer = __webpack_exports__.VitsTokenizer; +var __webpack_exports__Wav2Vec2BertForCTC = __webpack_exports__.Wav2Vec2BertForCTC; +var __webpack_exports__Wav2Vec2BertForSequenceClassification = __webpack_exports__.Wav2Vec2BertForSequenceClassification; +var __webpack_exports__Wav2Vec2BertModel = __webpack_exports__.Wav2Vec2BertModel; +var __webpack_exports__Wav2Vec2BertPreTrainedModel = __webpack_exports__.Wav2Vec2BertPreTrainedModel; +var __webpack_exports__Wav2Vec2CTCTokenizer = __webpack_exports__.Wav2Vec2CTCTokenizer; +var __webpack_exports__Wav2Vec2FeatureExtractor = __webpack_exports__.Wav2Vec2FeatureExtractor; +var __webpack_exports__Wav2Vec2ForAudioFrameClassification = __webpack_exports__.Wav2Vec2ForAudioFrameClassification; +var __webpack_exports__Wav2Vec2ForCTC = __webpack_exports__.Wav2Vec2ForCTC; +var __webpack_exports__Wav2Vec2ForSequenceClassification = __webpack_exports__.Wav2Vec2ForSequenceClassification; +var __webpack_exports__Wav2Vec2Model = __webpack_exports__.Wav2Vec2Model; +var __webpack_exports__Wav2Vec2PreTrainedModel = __webpack_exports__.Wav2Vec2PreTrainedModel; +var __webpack_exports__Wav2Vec2ProcessorWithLM = __webpack_exports__.Wav2Vec2ProcessorWithLM; +var __webpack_exports__WavLMForAudioFrameClassification = __webpack_exports__.WavLMForAudioFrameClassification; +var __webpack_exports__WavLMForCTC = __webpack_exports__.WavLMForCTC; +var __webpack_exports__WavLMForSequenceClassification = __webpack_exports__.WavLMForSequenceClassification; +var __webpack_exports__WavLMForXVector = __webpack_exports__.WavLMForXVector; +var __webpack_exports__WavLMModel = __webpack_exports__.WavLMModel; +var __webpack_exports__WavLMPreTrainedModel = __webpack_exports__.WavLMPreTrainedModel; +var __webpack_exports__WeSpeakerFeatureExtractor = __webpack_exports__.WeSpeakerFeatureExtractor; +var __webpack_exports__WeSpeakerResNetModel = __webpack_exports__.WeSpeakerResNetModel; +var __webpack_exports__WeSpeakerResNetPreTrainedModel = __webpack_exports__.WeSpeakerResNetPreTrainedModel; +var __webpack_exports__WhisperFeatureExtractor = __webpack_exports__.WhisperFeatureExtractor; +var __webpack_exports__WhisperForConditionalGeneration = __webpack_exports__.WhisperForConditionalGeneration; +var __webpack_exports__WhisperModel = __webpack_exports__.WhisperModel; +var __webpack_exports__WhisperPreTrainedModel = __webpack_exports__.WhisperPreTrainedModel; +var __webpack_exports__WhisperProcessor = __webpack_exports__.WhisperProcessor; +var __webpack_exports__WhisperTextStreamer = __webpack_exports__.WhisperTextStreamer; +var __webpack_exports__WhisperTokenizer = __webpack_exports__.WhisperTokenizer; +var __webpack_exports__XLMForQuestionAnswering = __webpack_exports__.XLMForQuestionAnswering; +var __webpack_exports__XLMForSequenceClassification = __webpack_exports__.XLMForSequenceClassification; +var __webpack_exports__XLMForTokenClassification = __webpack_exports__.XLMForTokenClassification; +var __webpack_exports__XLMModel = __webpack_exports__.XLMModel; +var __webpack_exports__XLMPreTrainedModel = __webpack_exports__.XLMPreTrainedModel; +var __webpack_exports__XLMRobertaForMaskedLM = __webpack_exports__.XLMRobertaForMaskedLM; +var __webpack_exports__XLMRobertaForQuestionAnswering = __webpack_exports__.XLMRobertaForQuestionAnswering; +var __webpack_exports__XLMRobertaForSequenceClassification = __webpack_exports__.XLMRobertaForSequenceClassification; +var __webpack_exports__XLMRobertaForTokenClassification = __webpack_exports__.XLMRobertaForTokenClassification; +var __webpack_exports__XLMRobertaModel = __webpack_exports__.XLMRobertaModel; +var __webpack_exports__XLMRobertaPreTrainedModel = __webpack_exports__.XLMRobertaPreTrainedModel; +var __webpack_exports__XLMRobertaTokenizer = __webpack_exports__.XLMRobertaTokenizer; +var __webpack_exports__XLMTokenizer = __webpack_exports__.XLMTokenizer; +var __webpack_exports__XLMWithLMHeadModel = __webpack_exports__.XLMWithLMHeadModel; +var __webpack_exports__XVectorOutput = __webpack_exports__.XVectorOutput; +var __webpack_exports__YolosFeatureExtractor = __webpack_exports__.YolosFeatureExtractor; +var __webpack_exports__YolosForObjectDetection = __webpack_exports__.YolosForObjectDetection; +var __webpack_exports__YolosModel = __webpack_exports__.YolosModel; +var __webpack_exports__YolosObjectDetectionOutput = __webpack_exports__.YolosObjectDetectionOutput; +var __webpack_exports__YolosPreTrainedModel = __webpack_exports__.YolosPreTrainedModel; +var __webpack_exports__ZeroShotAudioClassificationPipeline = __webpack_exports__.ZeroShotAudioClassificationPipeline; +var __webpack_exports__ZeroShotClassificationPipeline = __webpack_exports__.ZeroShotClassificationPipeline; +var __webpack_exports__ZeroShotImageClassificationPipeline = __webpack_exports__.ZeroShotImageClassificationPipeline; +var __webpack_exports__ZeroShotObjectDetectionPipeline = __webpack_exports__.ZeroShotObjectDetectionPipeline; +var __webpack_exports__bankers_round = __webpack_exports__.bankers_round; +var __webpack_exports__cat = __webpack_exports__.cat; +var __webpack_exports__cos_sim = __webpack_exports__.cos_sim; +var __webpack_exports__dot = __webpack_exports__.dot; +var __webpack_exports__dynamic_time_warping = __webpack_exports__.dynamic_time_warping; +var __webpack_exports__env = __webpack_exports__.env; +var __webpack_exports__full = __webpack_exports__.full; +var __webpack_exports__full_like = __webpack_exports__.full_like; +var __webpack_exports__getKeyValueShapes = __webpack_exports__.getKeyValueShapes; +var __webpack_exports__hamming = __webpack_exports__.hamming; +var __webpack_exports__hanning = __webpack_exports__.hanning; +var __webpack_exports__interpolate = __webpack_exports__.interpolate; +var __webpack_exports__interpolate_4d = __webpack_exports__.interpolate_4d; +var __webpack_exports__interpolate_data = __webpack_exports__.interpolate_data; +var __webpack_exports__is_chinese_char = __webpack_exports__.is_chinese_char; +var __webpack_exports__layer_norm = __webpack_exports__.layer_norm; +var __webpack_exports__log_softmax = __webpack_exports__.log_softmax; +var __webpack_exports__magnitude = __webpack_exports__.magnitude; +var __webpack_exports__matmul = __webpack_exports__.matmul; +var __webpack_exports__max = __webpack_exports__.max; +var __webpack_exports__mean = __webpack_exports__.mean; +var __webpack_exports__mean_pooling = __webpack_exports__.mean_pooling; +var __webpack_exports__medianFilter = __webpack_exports__.medianFilter; +var __webpack_exports__mel_filter_bank = __webpack_exports__.mel_filter_bank; +var __webpack_exports__min = __webpack_exports__.min; +var __webpack_exports__ones = __webpack_exports__.ones; +var __webpack_exports__ones_like = __webpack_exports__.ones_like; +var __webpack_exports__permute = __webpack_exports__.permute; +var __webpack_exports__permute_data = __webpack_exports__.permute_data; +var __webpack_exports__pipeline = __webpack_exports__.pipeline; +var __webpack_exports__quantize_embeddings = __webpack_exports__.quantize_embeddings; +var __webpack_exports__read_audio = __webpack_exports__.read_audio; +var __webpack_exports__rfft = __webpack_exports__.rfft; +var __webpack_exports__round = __webpack_exports__.round; +var __webpack_exports__softmax = __webpack_exports__.softmax; +var __webpack_exports__spectrogram = __webpack_exports__.spectrogram; +var __webpack_exports__stack = __webpack_exports__.stack; +var __webpack_exports__std_mean = __webpack_exports__.std_mean; +var __webpack_exports__topk = __webpack_exports__.topk; +var __webpack_exports__window_function = __webpack_exports__.window_function; +var __webpack_exports__zeros = __webpack_exports__.zeros; +var __webpack_exports__zeros_like = __webpack_exports__.zeros_like; +export { __webpack_exports__ASTFeatureExtractor as ASTFeatureExtractor, __webpack_exports__ASTForAudioClassification as ASTForAudioClassification, __webpack_exports__ASTModel as ASTModel, __webpack_exports__ASTPreTrainedModel as ASTPreTrainedModel, __webpack_exports__AlbertForMaskedLM as AlbertForMaskedLM, __webpack_exports__AlbertForQuestionAnswering as AlbertForQuestionAnswering, __webpack_exports__AlbertForSequenceClassification as AlbertForSequenceClassification, __webpack_exports__AlbertModel as AlbertModel, __webpack_exports__AlbertPreTrainedModel as AlbertPreTrainedModel, __webpack_exports__AlbertTokenizer as AlbertTokenizer, __webpack_exports__AudioClassificationPipeline as AudioClassificationPipeline, __webpack_exports__AutoConfig as AutoConfig, __webpack_exports__AutoModel as AutoModel, __webpack_exports__AutoModelForAudioClassification as AutoModelForAudioClassification, __webpack_exports__AutoModelForAudioFrameClassification as AutoModelForAudioFrameClassification, __webpack_exports__AutoModelForCTC as AutoModelForCTC, __webpack_exports__AutoModelForCausalLM as AutoModelForCausalLM, __webpack_exports__AutoModelForDepthEstimation as AutoModelForDepthEstimation, __webpack_exports__AutoModelForDocumentQuestionAnswering as AutoModelForDocumentQuestionAnswering, __webpack_exports__AutoModelForImageClassification as AutoModelForImageClassification, __webpack_exports__AutoModelForImageFeatureExtraction as AutoModelForImageFeatureExtraction, __webpack_exports__AutoModelForImageMatting as AutoModelForImageMatting, __webpack_exports__AutoModelForImageSegmentation as AutoModelForImageSegmentation, __webpack_exports__AutoModelForImageToImage as AutoModelForImageToImage, __webpack_exports__AutoModelForMaskGeneration as AutoModelForMaskGeneration, __webpack_exports__AutoModelForMaskedLM as AutoModelForMaskedLM, __webpack_exports__AutoModelForNormalEstimation as AutoModelForNormalEstimation, __webpack_exports__AutoModelForObjectDetection as AutoModelForObjectDetection, __webpack_exports__AutoModelForQuestionAnswering as AutoModelForQuestionAnswering, __webpack_exports__AutoModelForSemanticSegmentation as AutoModelForSemanticSegmentation, __webpack_exports__AutoModelForSeq2SeqLM as AutoModelForSeq2SeqLM, __webpack_exports__AutoModelForSequenceClassification as AutoModelForSequenceClassification, __webpack_exports__AutoModelForSpeechSeq2Seq as AutoModelForSpeechSeq2Seq, __webpack_exports__AutoModelForTextToSpectrogram as AutoModelForTextToSpectrogram, __webpack_exports__AutoModelForTextToWaveform as AutoModelForTextToWaveform, __webpack_exports__AutoModelForTokenClassification as AutoModelForTokenClassification, __webpack_exports__AutoModelForUniversalSegmentation as AutoModelForUniversalSegmentation, __webpack_exports__AutoModelForVision2Seq as AutoModelForVision2Seq, __webpack_exports__AutoModelForXVector as AutoModelForXVector, __webpack_exports__AutoModelForZeroShotObjectDetection as AutoModelForZeroShotObjectDetection, __webpack_exports__AutoProcessor as AutoProcessor, __webpack_exports__AutoTokenizer as AutoTokenizer, __webpack_exports__AutomaticSpeechRecognitionPipeline as AutomaticSpeechRecognitionPipeline, __webpack_exports__BartForConditionalGeneration as BartForConditionalGeneration, __webpack_exports__BartForSequenceClassification as BartForSequenceClassification, __webpack_exports__BartModel as BartModel, __webpack_exports__BartPretrainedModel as BartPretrainedModel, __webpack_exports__BartTokenizer as BartTokenizer, __webpack_exports__BaseModelOutput as BaseModelOutput, __webpack_exports__BaseStreamer as BaseStreamer, __webpack_exports__BeitFeatureExtractor as BeitFeatureExtractor, __webpack_exports__BeitForImageClassification as BeitForImageClassification, __webpack_exports__BeitModel as BeitModel, __webpack_exports__BeitPreTrainedModel as BeitPreTrainedModel, __webpack_exports__BertForMaskedLM as BertForMaskedLM, __webpack_exports__BertForQuestionAnswering as BertForQuestionAnswering, __webpack_exports__BertForSequenceClassification as BertForSequenceClassification, __webpack_exports__BertForTokenClassification as BertForTokenClassification, __webpack_exports__BertModel as BertModel, __webpack_exports__BertPreTrainedModel as BertPreTrainedModel, __webpack_exports__BertTokenizer as BertTokenizer, __webpack_exports__BitImageProcessor as BitImageProcessor, __webpack_exports__BlenderbotForConditionalGeneration as BlenderbotForConditionalGeneration, __webpack_exports__BlenderbotModel as BlenderbotModel, __webpack_exports__BlenderbotPreTrainedModel as BlenderbotPreTrainedModel, __webpack_exports__BlenderbotSmallForConditionalGeneration as BlenderbotSmallForConditionalGeneration, __webpack_exports__BlenderbotSmallModel as BlenderbotSmallModel, __webpack_exports__BlenderbotSmallPreTrainedModel as BlenderbotSmallPreTrainedModel, __webpack_exports__BlenderbotSmallTokenizer as BlenderbotSmallTokenizer, __webpack_exports__BlenderbotTokenizer as BlenderbotTokenizer, __webpack_exports__BloomForCausalLM as BloomForCausalLM, __webpack_exports__BloomModel as BloomModel, __webpack_exports__BloomPreTrainedModel as BloomPreTrainedModel, __webpack_exports__BloomTokenizer as BloomTokenizer, __webpack_exports__CLIPFeatureExtractor as CLIPFeatureExtractor, __webpack_exports__CLIPImageProcessor as CLIPImageProcessor, __webpack_exports__CLIPModel as CLIPModel, __webpack_exports__CLIPPreTrainedModel as CLIPPreTrainedModel, __webpack_exports__CLIPSegForImageSegmentation as CLIPSegForImageSegmentation, __webpack_exports__CLIPSegModel as CLIPSegModel, __webpack_exports__CLIPSegPreTrainedModel as CLIPSegPreTrainedModel, __webpack_exports__CLIPTextModel as CLIPTextModel, __webpack_exports__CLIPTextModelWithProjection as CLIPTextModelWithProjection, __webpack_exports__CLIPTokenizer as CLIPTokenizer, __webpack_exports__CLIPVisionModel as CLIPVisionModel, __webpack_exports__CLIPVisionModelWithProjection as CLIPVisionModelWithProjection, __webpack_exports__CamembertForMaskedLM as CamembertForMaskedLM, __webpack_exports__CamembertForQuestionAnswering as CamembertForQuestionAnswering, __webpack_exports__CamembertForSequenceClassification as CamembertForSequenceClassification, __webpack_exports__CamembertForTokenClassification as CamembertForTokenClassification, __webpack_exports__CamembertModel as CamembertModel, __webpack_exports__CamembertPreTrainedModel as CamembertPreTrainedModel, __webpack_exports__CamembertTokenizer as CamembertTokenizer, __webpack_exports__CausalLMOutput as CausalLMOutput, __webpack_exports__CausalLMOutputWithPast as CausalLMOutputWithPast, __webpack_exports__ChineseCLIPFeatureExtractor as ChineseCLIPFeatureExtractor, __webpack_exports__ChineseCLIPModel as ChineseCLIPModel, __webpack_exports__ChineseCLIPPreTrainedModel as ChineseCLIPPreTrainedModel, __webpack_exports__ClapAudioModelWithProjection as ClapAudioModelWithProjection, __webpack_exports__ClapFeatureExtractor as ClapFeatureExtractor, __webpack_exports__ClapModel as ClapModel, __webpack_exports__ClapPreTrainedModel as ClapPreTrainedModel, __webpack_exports__ClapTextModelWithProjection as ClapTextModelWithProjection, __webpack_exports__CodeGenForCausalLM as CodeGenForCausalLM, __webpack_exports__CodeGenModel as CodeGenModel, __webpack_exports__CodeGenPreTrainedModel as CodeGenPreTrainedModel, __webpack_exports__CodeGenTokenizer as CodeGenTokenizer, __webpack_exports__CodeLlamaTokenizer as CodeLlamaTokenizer, __webpack_exports__CohereForCausalLM as CohereForCausalLM, __webpack_exports__CohereModel as CohereModel, __webpack_exports__CoherePreTrainedModel as CoherePreTrainedModel, __webpack_exports__CohereTokenizer as CohereTokenizer, __webpack_exports__ConvBertForMaskedLM as ConvBertForMaskedLM, __webpack_exports__ConvBertForQuestionAnswering as ConvBertForQuestionAnswering, __webpack_exports__ConvBertForSequenceClassification as ConvBertForSequenceClassification, __webpack_exports__ConvBertForTokenClassification as ConvBertForTokenClassification, __webpack_exports__ConvBertModel as ConvBertModel, __webpack_exports__ConvBertPreTrainedModel as ConvBertPreTrainedModel, __webpack_exports__ConvBertTokenizer as ConvBertTokenizer, __webpack_exports__ConvNextFeatureExtractor as ConvNextFeatureExtractor, __webpack_exports__ConvNextForImageClassification as ConvNextForImageClassification, __webpack_exports__ConvNextImageProcessor as ConvNextImageProcessor, __webpack_exports__ConvNextModel as ConvNextModel, __webpack_exports__ConvNextPreTrainedModel as ConvNextPreTrainedModel, __webpack_exports__ConvNextV2ForImageClassification as ConvNextV2ForImageClassification, __webpack_exports__ConvNextV2Model as ConvNextV2Model, __webpack_exports__ConvNextV2PreTrainedModel as ConvNextV2PreTrainedModel, __webpack_exports__DPTFeatureExtractor as DPTFeatureExtractor, __webpack_exports__DPTForDepthEstimation as DPTForDepthEstimation, __webpack_exports__DPTImageProcessor as DPTImageProcessor, __webpack_exports__DPTModel as DPTModel, __webpack_exports__DPTPreTrainedModel as DPTPreTrainedModel, __webpack_exports__DebertaForMaskedLM as DebertaForMaskedLM, __webpack_exports__DebertaForQuestionAnswering as DebertaForQuestionAnswering, __webpack_exports__DebertaForSequenceClassification as DebertaForSequenceClassification, __webpack_exports__DebertaForTokenClassification as DebertaForTokenClassification, __webpack_exports__DebertaModel as DebertaModel, __webpack_exports__DebertaPreTrainedModel as DebertaPreTrainedModel, __webpack_exports__DebertaTokenizer as DebertaTokenizer, __webpack_exports__DebertaV2ForMaskedLM as DebertaV2ForMaskedLM, __webpack_exports__DebertaV2ForQuestionAnswering as DebertaV2ForQuestionAnswering, __webpack_exports__DebertaV2ForSequenceClassification as DebertaV2ForSequenceClassification, __webpack_exports__DebertaV2ForTokenClassification as DebertaV2ForTokenClassification, __webpack_exports__DebertaV2Model as DebertaV2Model, __webpack_exports__DebertaV2PreTrainedModel as DebertaV2PreTrainedModel, __webpack_exports__DebertaV2Tokenizer as DebertaV2Tokenizer, __webpack_exports__DecisionTransformerModel as DecisionTransformerModel, __webpack_exports__DecisionTransformerPreTrainedModel as DecisionTransformerPreTrainedModel, __webpack_exports__DeiTFeatureExtractor as DeiTFeatureExtractor, __webpack_exports__DeiTForImageClassification as DeiTForImageClassification, __webpack_exports__DeiTModel as DeiTModel, __webpack_exports__DeiTPreTrainedModel as DeiTPreTrainedModel, __webpack_exports__DepthAnythingForDepthEstimation as DepthAnythingForDepthEstimation, __webpack_exports__DepthAnythingPreTrainedModel as DepthAnythingPreTrainedModel, __webpack_exports__DepthEstimationPipeline as DepthEstimationPipeline, __webpack_exports__DepthProForDepthEstimation as DepthProForDepthEstimation, __webpack_exports__DepthProPreTrainedModel as DepthProPreTrainedModel, __webpack_exports__DetrFeatureExtractor as DetrFeatureExtractor, __webpack_exports__DetrForObjectDetection as DetrForObjectDetection, __webpack_exports__DetrForSegmentation as DetrForSegmentation, __webpack_exports__DetrModel as DetrModel, __webpack_exports__DetrObjectDetectionOutput as DetrObjectDetectionOutput, __webpack_exports__DetrPreTrainedModel as DetrPreTrainedModel, __webpack_exports__DetrSegmentationOutput as DetrSegmentationOutput, __webpack_exports__Dinov2ForImageClassification as Dinov2ForImageClassification, __webpack_exports__Dinov2Model as Dinov2Model, __webpack_exports__Dinov2PreTrainedModel as Dinov2PreTrainedModel, __webpack_exports__DistilBertForMaskedLM as DistilBertForMaskedLM, __webpack_exports__DistilBertForQuestionAnswering as DistilBertForQuestionAnswering, __webpack_exports__DistilBertForSequenceClassification as DistilBertForSequenceClassification, __webpack_exports__DistilBertForTokenClassification as DistilBertForTokenClassification, __webpack_exports__DistilBertModel as DistilBertModel, __webpack_exports__DistilBertPreTrainedModel as DistilBertPreTrainedModel, __webpack_exports__DistilBertTokenizer as DistilBertTokenizer, __webpack_exports__DocumentQuestionAnsweringPipeline as DocumentQuestionAnsweringPipeline, __webpack_exports__DonutFeatureExtractor as DonutFeatureExtractor, __webpack_exports__DonutImageProcessor as DonutImageProcessor, __webpack_exports__DonutSwinModel as DonutSwinModel, __webpack_exports__DonutSwinPreTrainedModel as DonutSwinPreTrainedModel, __webpack_exports__EfficientNetForImageClassification as EfficientNetForImageClassification, __webpack_exports__EfficientNetImageProcessor as EfficientNetImageProcessor, __webpack_exports__EfficientNetModel as EfficientNetModel, __webpack_exports__EfficientNetPreTrainedModel as EfficientNetPreTrainedModel, __webpack_exports__ElectraForMaskedLM as ElectraForMaskedLM, __webpack_exports__ElectraForQuestionAnswering as ElectraForQuestionAnswering, __webpack_exports__ElectraForSequenceClassification as ElectraForSequenceClassification, __webpack_exports__ElectraForTokenClassification as ElectraForTokenClassification, __webpack_exports__ElectraModel as ElectraModel, __webpack_exports__ElectraPreTrainedModel as ElectraPreTrainedModel, __webpack_exports__ElectraTokenizer as ElectraTokenizer, __webpack_exports__EosTokenCriteria as EosTokenCriteria, __webpack_exports__EsmForMaskedLM as EsmForMaskedLM, __webpack_exports__EsmForSequenceClassification as EsmForSequenceClassification, __webpack_exports__EsmForTokenClassification as EsmForTokenClassification, __webpack_exports__EsmModel as EsmModel, __webpack_exports__EsmPreTrainedModel as EsmPreTrainedModel, __webpack_exports__EsmTokenizer as EsmTokenizer, __webpack_exports__FFT as FFT, __webpack_exports__FalconForCausalLM as FalconForCausalLM, __webpack_exports__FalconModel as FalconModel, __webpack_exports__FalconPreTrainedModel as FalconPreTrainedModel, __webpack_exports__FalconTokenizer as FalconTokenizer, __webpack_exports__FastViTForImageClassification as FastViTForImageClassification, __webpack_exports__FastViTModel as FastViTModel, __webpack_exports__FastViTPreTrainedModel as FastViTPreTrainedModel, __webpack_exports__FeatureExtractionPipeline as FeatureExtractionPipeline, __webpack_exports__FeatureExtractor as FeatureExtractor, __webpack_exports__FillMaskPipeline as FillMaskPipeline, __webpack_exports__Florence2ForConditionalGeneration as Florence2ForConditionalGeneration, __webpack_exports__Florence2PreTrainedModel as Florence2PreTrainedModel, __webpack_exports__Florence2Processor as Florence2Processor, __webpack_exports__GLPNFeatureExtractor as GLPNFeatureExtractor, __webpack_exports__GLPNForDepthEstimation as GLPNForDepthEstimation, __webpack_exports__GLPNModel as GLPNModel, __webpack_exports__GLPNPreTrainedModel as GLPNPreTrainedModel, __webpack_exports__GPT2LMHeadModel as GPT2LMHeadModel, __webpack_exports__GPT2Model as GPT2Model, __webpack_exports__GPT2PreTrainedModel as GPT2PreTrainedModel, __webpack_exports__GPT2Tokenizer as GPT2Tokenizer, __webpack_exports__GPTBigCodeForCausalLM as GPTBigCodeForCausalLM, __webpack_exports__GPTBigCodeModel as GPTBigCodeModel, __webpack_exports__GPTBigCodePreTrainedModel as GPTBigCodePreTrainedModel, __webpack_exports__GPTJForCausalLM as GPTJForCausalLM, __webpack_exports__GPTJModel as GPTJModel, __webpack_exports__GPTJPreTrainedModel as GPTJPreTrainedModel, __webpack_exports__GPTNeoForCausalLM as GPTNeoForCausalLM, __webpack_exports__GPTNeoModel as GPTNeoModel, __webpack_exports__GPTNeoPreTrainedModel as GPTNeoPreTrainedModel, __webpack_exports__GPTNeoXForCausalLM as GPTNeoXForCausalLM, __webpack_exports__GPTNeoXModel as GPTNeoXModel, __webpack_exports__GPTNeoXPreTrainedModel as GPTNeoXPreTrainedModel, __webpack_exports__GPTNeoXTokenizer as GPTNeoXTokenizer, __webpack_exports__Gemma2ForCausalLM as Gemma2ForCausalLM, __webpack_exports__Gemma2Model as Gemma2Model, __webpack_exports__Gemma2PreTrainedModel as Gemma2PreTrainedModel, __webpack_exports__GemmaForCausalLM as GemmaForCausalLM, __webpack_exports__GemmaModel as GemmaModel, __webpack_exports__GemmaPreTrainedModel as GemmaPreTrainedModel, __webpack_exports__GemmaTokenizer as GemmaTokenizer, __webpack_exports__GraniteForCausalLM as GraniteForCausalLM, __webpack_exports__GraniteModel as GraniteModel, __webpack_exports__GranitePreTrainedModel as GranitePreTrainedModel, __webpack_exports__Grok1Tokenizer as Grok1Tokenizer, __webpack_exports__GroupViTModel as GroupViTModel, __webpack_exports__GroupViTPreTrainedModel as GroupViTPreTrainedModel, __webpack_exports__HerbertTokenizer as HerbertTokenizer, __webpack_exports__HieraForImageClassification as HieraForImageClassification, __webpack_exports__HieraModel as HieraModel, __webpack_exports__HieraPreTrainedModel as HieraPreTrainedModel, __webpack_exports__HubertForCTC as HubertForCTC, __webpack_exports__HubertForSequenceClassification as HubertForSequenceClassification, __webpack_exports__HubertModel as HubertModel, __webpack_exports__HubertPreTrainedModel as HubertPreTrainedModel, __webpack_exports__ImageClassificationPipeline as ImageClassificationPipeline, __webpack_exports__ImageFeatureExtractionPipeline as ImageFeatureExtractionPipeline, __webpack_exports__ImageFeatureExtractor as ImageFeatureExtractor, __webpack_exports__ImageMattingOutput as ImageMattingOutput, __webpack_exports__ImageSegmentationPipeline as ImageSegmentationPipeline, __webpack_exports__ImageToImagePipeline as ImageToImagePipeline, __webpack_exports__ImageToTextPipeline as ImageToTextPipeline, __webpack_exports__InterruptableStoppingCriteria as InterruptableStoppingCriteria, __webpack_exports__JAISLMHeadModel as JAISLMHeadModel, __webpack_exports__JAISModel as JAISModel, __webpack_exports__JAISPreTrainedModel as JAISPreTrainedModel, __webpack_exports__LlamaForCausalLM as LlamaForCausalLM, __webpack_exports__LlamaModel as LlamaModel, __webpack_exports__LlamaPreTrainedModel as LlamaPreTrainedModel, __webpack_exports__LlamaTokenizer as LlamaTokenizer, __webpack_exports__LlavaForConditionalGeneration as LlavaForConditionalGeneration, __webpack_exports__LlavaPreTrainedModel as LlavaPreTrainedModel, __webpack_exports__LongT5ForConditionalGeneration as LongT5ForConditionalGeneration, __webpack_exports__LongT5Model as LongT5Model, __webpack_exports__LongT5PreTrainedModel as LongT5PreTrainedModel, __webpack_exports__M2M100ForConditionalGeneration as M2M100ForConditionalGeneration, __webpack_exports__M2M100Model as M2M100Model, __webpack_exports__M2M100PreTrainedModel as M2M100PreTrainedModel, __webpack_exports__M2M100Tokenizer as M2M100Tokenizer, __webpack_exports__MBart50Tokenizer as MBart50Tokenizer, __webpack_exports__MBartForCausalLM as MBartForCausalLM, __webpack_exports__MBartForConditionalGeneration as MBartForConditionalGeneration, __webpack_exports__MBartForSequenceClassification as MBartForSequenceClassification, __webpack_exports__MBartModel as MBartModel, __webpack_exports__MBartPreTrainedModel as MBartPreTrainedModel, __webpack_exports__MBartTokenizer as MBartTokenizer, __webpack_exports__MPNetForMaskedLM as MPNetForMaskedLM, __webpack_exports__MPNetForQuestionAnswering as MPNetForQuestionAnswering, __webpack_exports__MPNetForSequenceClassification as MPNetForSequenceClassification, __webpack_exports__MPNetForTokenClassification as MPNetForTokenClassification, __webpack_exports__MPNetModel as MPNetModel, __webpack_exports__MPNetPreTrainedModel as MPNetPreTrainedModel, __webpack_exports__MPNetTokenizer as MPNetTokenizer, __webpack_exports__MT5ForConditionalGeneration as MT5ForConditionalGeneration, __webpack_exports__MT5Model as MT5Model, __webpack_exports__MT5PreTrainedModel as MT5PreTrainedModel, __webpack_exports__MarianMTModel as MarianMTModel, __webpack_exports__MarianModel as MarianModel, __webpack_exports__MarianPreTrainedModel as MarianPreTrainedModel, __webpack_exports__MarianTokenizer as MarianTokenizer, __webpack_exports__MaskFormerFeatureExtractor as MaskFormerFeatureExtractor, __webpack_exports__MaskFormerForInstanceSegmentation as MaskFormerForInstanceSegmentation, __webpack_exports__MaskFormerModel as MaskFormerModel, __webpack_exports__MaskFormerPreTrainedModel as MaskFormerPreTrainedModel, __webpack_exports__MaskedLMOutput as MaskedLMOutput, __webpack_exports__MaxLengthCriteria as MaxLengthCriteria, __webpack_exports__MistralForCausalLM as MistralForCausalLM, __webpack_exports__MistralModel as MistralModel, __webpack_exports__MistralPreTrainedModel as MistralPreTrainedModel, __webpack_exports__MobileBertForMaskedLM as MobileBertForMaskedLM, __webpack_exports__MobileBertForQuestionAnswering as MobileBertForQuestionAnswering, __webpack_exports__MobileBertForSequenceClassification as MobileBertForSequenceClassification, __webpack_exports__MobileBertModel as MobileBertModel, __webpack_exports__MobileBertPreTrainedModel as MobileBertPreTrainedModel, __webpack_exports__MobileBertTokenizer as MobileBertTokenizer, __webpack_exports__MobileLLMForCausalLM as MobileLLMForCausalLM, __webpack_exports__MobileLLMModel as MobileLLMModel, __webpack_exports__MobileLLMPreTrainedModel as MobileLLMPreTrainedModel, __webpack_exports__MobileNetV1FeatureExtractor as MobileNetV1FeatureExtractor, __webpack_exports__MobileNetV1ForImageClassification as MobileNetV1ForImageClassification, __webpack_exports__MobileNetV1Model as MobileNetV1Model, __webpack_exports__MobileNetV1PreTrainedModel as MobileNetV1PreTrainedModel, __webpack_exports__MobileNetV2FeatureExtractor as MobileNetV2FeatureExtractor, __webpack_exports__MobileNetV2ForImageClassification as MobileNetV2ForImageClassification, __webpack_exports__MobileNetV2Model as MobileNetV2Model, __webpack_exports__MobileNetV2PreTrainedModel as MobileNetV2PreTrainedModel, __webpack_exports__MobileNetV3FeatureExtractor as MobileNetV3FeatureExtractor, __webpack_exports__MobileNetV3ForImageClassification as MobileNetV3ForImageClassification, __webpack_exports__MobileNetV3Model as MobileNetV3Model, __webpack_exports__MobileNetV3PreTrainedModel as MobileNetV3PreTrainedModel, __webpack_exports__MobileNetV4FeatureExtractor as MobileNetV4FeatureExtractor, __webpack_exports__MobileNetV4ForImageClassification as MobileNetV4ForImageClassification, __webpack_exports__MobileNetV4Model as MobileNetV4Model, __webpack_exports__MobileNetV4PreTrainedModel as MobileNetV4PreTrainedModel, __webpack_exports__MobileViTFeatureExtractor as MobileViTFeatureExtractor, __webpack_exports__MobileViTForImageClassification as MobileViTForImageClassification, __webpack_exports__MobileViTImageProcessor as MobileViTImageProcessor, __webpack_exports__MobileViTModel as MobileViTModel, __webpack_exports__MobileViTPreTrainedModel as MobileViTPreTrainedModel, __webpack_exports__MobileViTV2ForImageClassification as MobileViTV2ForImageClassification, __webpack_exports__MobileViTV2Model as MobileViTV2Model, __webpack_exports__MobileViTV2PreTrainedModel as MobileViTV2PreTrainedModel, __webpack_exports__ModelOutput as ModelOutput, __webpack_exports__Moondream1ForConditionalGeneration as Moondream1ForConditionalGeneration, __webpack_exports__MptForCausalLM as MptForCausalLM, __webpack_exports__MptModel as MptModel, __webpack_exports__MptPreTrainedModel as MptPreTrainedModel, __webpack_exports__MusicgenForCausalLM as MusicgenForCausalLM, __webpack_exports__MusicgenForConditionalGeneration as MusicgenForConditionalGeneration, __webpack_exports__MusicgenModel as MusicgenModel, __webpack_exports__MusicgenPreTrainedModel as MusicgenPreTrainedModel, __webpack_exports__NllbTokenizer as NllbTokenizer, __webpack_exports__NomicBertModel as NomicBertModel, __webpack_exports__NomicBertPreTrainedModel as NomicBertPreTrainedModel, __webpack_exports__NougatImageProcessor as NougatImageProcessor, __webpack_exports__NougatTokenizer as NougatTokenizer, __webpack_exports__OPTForCausalLM as OPTForCausalLM, __webpack_exports__OPTModel as OPTModel, __webpack_exports__OPTPreTrainedModel as OPTPreTrainedModel, __webpack_exports__ObjectDetectionPipeline as ObjectDetectionPipeline, __webpack_exports__OlmoForCausalLM as OlmoForCausalLM, __webpack_exports__OlmoModel as OlmoModel, __webpack_exports__OlmoPreTrainedModel as OlmoPreTrainedModel, __webpack_exports__OpenELMForCausalLM as OpenELMForCausalLM, __webpack_exports__OpenELMModel as OpenELMModel, __webpack_exports__OpenELMPreTrainedModel as OpenELMPreTrainedModel, __webpack_exports__OwlViTFeatureExtractor as OwlViTFeatureExtractor, __webpack_exports__OwlViTForObjectDetection as OwlViTForObjectDetection, __webpack_exports__OwlViTModel as OwlViTModel, __webpack_exports__OwlViTPreTrainedModel as OwlViTPreTrainedModel, __webpack_exports__OwlViTProcessor as OwlViTProcessor, __webpack_exports__Owlv2ForObjectDetection as Owlv2ForObjectDetection, __webpack_exports__Owlv2ImageProcessor as Owlv2ImageProcessor, __webpack_exports__Owlv2Model as Owlv2Model, __webpack_exports__Owlv2PreTrainedModel as Owlv2PreTrainedModel, __webpack_exports__Phi3ForCausalLM as Phi3ForCausalLM, __webpack_exports__Phi3Model as Phi3Model, __webpack_exports__Phi3PreTrainedModel as Phi3PreTrainedModel, __webpack_exports__PhiForCausalLM as PhiForCausalLM, __webpack_exports__PhiModel as PhiModel, __webpack_exports__PhiPreTrainedModel as PhiPreTrainedModel, __webpack_exports__Pipeline as Pipeline, __webpack_exports__PreTrainedModel as PreTrainedModel, __webpack_exports__PreTrainedTokenizer as PreTrainedTokenizer, __webpack_exports__PretrainedConfig as PretrainedConfig, __webpack_exports__PretrainedMixin as PretrainedMixin, __webpack_exports__Processor as Processor, __webpack_exports__PvtForImageClassification as PvtForImageClassification, __webpack_exports__PvtImageProcessor as PvtImageProcessor, __webpack_exports__PvtModel as PvtModel, __webpack_exports__PvtPreTrainedModel as PvtPreTrainedModel, __webpack_exports__PyAnnoteFeatureExtractor as PyAnnoteFeatureExtractor, __webpack_exports__PyAnnoteForAudioFrameClassification as PyAnnoteForAudioFrameClassification, __webpack_exports__PyAnnoteModel as PyAnnoteModel, __webpack_exports__PyAnnotePreTrainedModel as PyAnnotePreTrainedModel, __webpack_exports__PyAnnoteProcessor as PyAnnoteProcessor, __webpack_exports__QuestionAnsweringModelOutput as QuestionAnsweringModelOutput, __webpack_exports__QuestionAnsweringPipeline as QuestionAnsweringPipeline, __webpack_exports__Qwen2ForCausalLM as Qwen2ForCausalLM, __webpack_exports__Qwen2Model as Qwen2Model, __webpack_exports__Qwen2PreTrainedModel as Qwen2PreTrainedModel, __webpack_exports__Qwen2Tokenizer as Qwen2Tokenizer, __webpack_exports__RTDetrForObjectDetection as RTDetrForObjectDetection, __webpack_exports__RTDetrImageProcessor as RTDetrImageProcessor, __webpack_exports__RTDetrModel as RTDetrModel, __webpack_exports__RTDetrObjectDetectionOutput as RTDetrObjectDetectionOutput, __webpack_exports__RTDetrPreTrainedModel as RTDetrPreTrainedModel, __webpack_exports__RawImage as RawImage, __webpack_exports__ResNetForImageClassification as ResNetForImageClassification, __webpack_exports__ResNetModel as ResNetModel, __webpack_exports__ResNetPreTrainedModel as ResNetPreTrainedModel, __webpack_exports__RoFormerForMaskedLM as RoFormerForMaskedLM, __webpack_exports__RoFormerForQuestionAnswering as RoFormerForQuestionAnswering, __webpack_exports__RoFormerForSequenceClassification as RoFormerForSequenceClassification, __webpack_exports__RoFormerForTokenClassification as RoFormerForTokenClassification, __webpack_exports__RoFormerModel as RoFormerModel, __webpack_exports__RoFormerPreTrainedModel as RoFormerPreTrainedModel, __webpack_exports__RoFormerTokenizer as RoFormerTokenizer, __webpack_exports__RobertaForMaskedLM as RobertaForMaskedLM, __webpack_exports__RobertaForQuestionAnswering as RobertaForQuestionAnswering, __webpack_exports__RobertaForSequenceClassification as RobertaForSequenceClassification, __webpack_exports__RobertaForTokenClassification as RobertaForTokenClassification, __webpack_exports__RobertaModel as RobertaModel, __webpack_exports__RobertaPreTrainedModel as RobertaPreTrainedModel, __webpack_exports__RobertaTokenizer as RobertaTokenizer, __webpack_exports__SamImageProcessor as SamImageProcessor, __webpack_exports__SamImageSegmentationOutput as SamImageSegmentationOutput, __webpack_exports__SamModel as SamModel, __webpack_exports__SamPreTrainedModel as SamPreTrainedModel, __webpack_exports__SamProcessor as SamProcessor, __webpack_exports__SapiensFeatureExtractor as SapiensFeatureExtractor, __webpack_exports__SapiensForDepthEstimation as SapiensForDepthEstimation, __webpack_exports__SapiensForNormalEstimation as SapiensForNormalEstimation, __webpack_exports__SapiensForSemanticSegmentation as SapiensForSemanticSegmentation, __webpack_exports__SapiensPreTrainedModel as SapiensPreTrainedModel, __webpack_exports__SeamlessM4TFeatureExtractor as SeamlessM4TFeatureExtractor, __webpack_exports__SegformerFeatureExtractor as SegformerFeatureExtractor, __webpack_exports__SegformerForImageClassification as SegformerForImageClassification, __webpack_exports__SegformerForSemanticSegmentation as SegformerForSemanticSegmentation, __webpack_exports__SegformerModel as SegformerModel, __webpack_exports__SegformerPreTrainedModel as SegformerPreTrainedModel, __webpack_exports__Seq2SeqLMOutput as Seq2SeqLMOutput, __webpack_exports__SequenceClassifierOutput as SequenceClassifierOutput, __webpack_exports__SiglipImageProcessor as SiglipImageProcessor, __webpack_exports__SiglipModel as SiglipModel, __webpack_exports__SiglipPreTrainedModel as SiglipPreTrainedModel, __webpack_exports__SiglipTextModel as SiglipTextModel, __webpack_exports__SiglipTokenizer as SiglipTokenizer, __webpack_exports__SiglipVisionModel as SiglipVisionModel, __webpack_exports__SpeechT5FeatureExtractor as SpeechT5FeatureExtractor, __webpack_exports__SpeechT5ForSpeechToText as SpeechT5ForSpeechToText, __webpack_exports__SpeechT5ForTextToSpeech as SpeechT5ForTextToSpeech, __webpack_exports__SpeechT5HifiGan as SpeechT5HifiGan, __webpack_exports__SpeechT5Model as SpeechT5Model, __webpack_exports__SpeechT5PreTrainedModel as SpeechT5PreTrainedModel, __webpack_exports__SpeechT5Processor as SpeechT5Processor, __webpack_exports__SpeechT5Tokenizer as SpeechT5Tokenizer, __webpack_exports__SqueezeBertForMaskedLM as SqueezeBertForMaskedLM, __webpack_exports__SqueezeBertForQuestionAnswering as SqueezeBertForQuestionAnswering, __webpack_exports__SqueezeBertForSequenceClassification as SqueezeBertForSequenceClassification, __webpack_exports__SqueezeBertModel as SqueezeBertModel, __webpack_exports__SqueezeBertPreTrainedModel as SqueezeBertPreTrainedModel, __webpack_exports__SqueezeBertTokenizer as SqueezeBertTokenizer, __webpack_exports__StableLmForCausalLM as StableLmForCausalLM, __webpack_exports__StableLmModel as StableLmModel, __webpack_exports__StableLmPreTrainedModel as StableLmPreTrainedModel, __webpack_exports__Starcoder2ForCausalLM as Starcoder2ForCausalLM, __webpack_exports__Starcoder2Model as Starcoder2Model, __webpack_exports__Starcoder2PreTrainedModel as Starcoder2PreTrainedModel, __webpack_exports__StoppingCriteria as StoppingCriteria, __webpack_exports__StoppingCriteriaList as StoppingCriteriaList, __webpack_exports__SummarizationPipeline as SummarizationPipeline, __webpack_exports__Swin2SRForImageSuperResolution as Swin2SRForImageSuperResolution, __webpack_exports__Swin2SRImageProcessor as Swin2SRImageProcessor, __webpack_exports__Swin2SRModel as Swin2SRModel, __webpack_exports__Swin2SRPreTrainedModel as Swin2SRPreTrainedModel, __webpack_exports__SwinForImageClassification as SwinForImageClassification, __webpack_exports__SwinModel as SwinModel, __webpack_exports__SwinPreTrainedModel as SwinPreTrainedModel, __webpack_exports__T5ForConditionalGeneration as T5ForConditionalGeneration, __webpack_exports__T5Model as T5Model, __webpack_exports__T5PreTrainedModel as T5PreTrainedModel, __webpack_exports__T5Tokenizer as T5Tokenizer, __webpack_exports__TableTransformerForObjectDetection as TableTransformerForObjectDetection, __webpack_exports__TableTransformerModel as TableTransformerModel, __webpack_exports__TableTransformerObjectDetectionOutput as TableTransformerObjectDetectionOutput, __webpack_exports__TableTransformerPreTrainedModel as TableTransformerPreTrainedModel, __webpack_exports__Tensor as Tensor, __webpack_exports__Text2TextGenerationPipeline as Text2TextGenerationPipeline, __webpack_exports__TextClassificationPipeline as TextClassificationPipeline, __webpack_exports__TextGenerationPipeline as TextGenerationPipeline, __webpack_exports__TextStreamer as TextStreamer, __webpack_exports__TextToAudioPipeline as TextToAudioPipeline, __webpack_exports__TokenClassificationPipeline as TokenClassificationPipeline, __webpack_exports__TokenClassifierOutput as TokenClassifierOutput, __webpack_exports__TokenizerModel as TokenizerModel, __webpack_exports__TrOCRForCausalLM as TrOCRForCausalLM, __webpack_exports__TrOCRPreTrainedModel as TrOCRPreTrainedModel, __webpack_exports__TranslationPipeline as TranslationPipeline, __webpack_exports__UniSpeechForCTC as UniSpeechForCTC, __webpack_exports__UniSpeechForSequenceClassification as UniSpeechForSequenceClassification, __webpack_exports__UniSpeechModel as UniSpeechModel, __webpack_exports__UniSpeechPreTrainedModel as UniSpeechPreTrainedModel, __webpack_exports__UniSpeechSatForAudioFrameClassification as UniSpeechSatForAudioFrameClassification, __webpack_exports__UniSpeechSatForCTC as UniSpeechSatForCTC, __webpack_exports__UniSpeechSatForSequenceClassification as UniSpeechSatForSequenceClassification, __webpack_exports__UniSpeechSatModel as UniSpeechSatModel, __webpack_exports__UniSpeechSatPreTrainedModel as UniSpeechSatPreTrainedModel, __webpack_exports__ViTFeatureExtractor as ViTFeatureExtractor, __webpack_exports__ViTForImageClassification as ViTForImageClassification, __webpack_exports__ViTImageProcessor as ViTImageProcessor, __webpack_exports__ViTMAEModel as ViTMAEModel, __webpack_exports__ViTMAEPreTrainedModel as ViTMAEPreTrainedModel, __webpack_exports__ViTMSNForImageClassification as ViTMSNForImageClassification, __webpack_exports__ViTMSNModel as ViTMSNModel, __webpack_exports__ViTMSNPreTrainedModel as ViTMSNPreTrainedModel, __webpack_exports__ViTModel as ViTModel, __webpack_exports__ViTPreTrainedModel as ViTPreTrainedModel, __webpack_exports__VisionEncoderDecoderModel as VisionEncoderDecoderModel, __webpack_exports__VitMatteForImageMatting as VitMatteForImageMatting, __webpack_exports__VitMatteImageProcessor as VitMatteImageProcessor, __webpack_exports__VitMattePreTrainedModel as VitMattePreTrainedModel, __webpack_exports__VitsModel as VitsModel, __webpack_exports__VitsModelOutput as VitsModelOutput, __webpack_exports__VitsPreTrainedModel as VitsPreTrainedModel, __webpack_exports__VitsTokenizer as VitsTokenizer, __webpack_exports__Wav2Vec2BertForCTC as Wav2Vec2BertForCTC, __webpack_exports__Wav2Vec2BertForSequenceClassification as Wav2Vec2BertForSequenceClassification, __webpack_exports__Wav2Vec2BertModel as Wav2Vec2BertModel, __webpack_exports__Wav2Vec2BertPreTrainedModel as Wav2Vec2BertPreTrainedModel, __webpack_exports__Wav2Vec2CTCTokenizer as Wav2Vec2CTCTokenizer, __webpack_exports__Wav2Vec2FeatureExtractor as Wav2Vec2FeatureExtractor, __webpack_exports__Wav2Vec2ForAudioFrameClassification as Wav2Vec2ForAudioFrameClassification, __webpack_exports__Wav2Vec2ForCTC as Wav2Vec2ForCTC, __webpack_exports__Wav2Vec2ForSequenceClassification as Wav2Vec2ForSequenceClassification, __webpack_exports__Wav2Vec2Model as Wav2Vec2Model, __webpack_exports__Wav2Vec2PreTrainedModel as Wav2Vec2PreTrainedModel, __webpack_exports__Wav2Vec2ProcessorWithLM as Wav2Vec2ProcessorWithLM, __webpack_exports__WavLMForAudioFrameClassification as WavLMForAudioFrameClassification, __webpack_exports__WavLMForCTC as WavLMForCTC, __webpack_exports__WavLMForSequenceClassification as WavLMForSequenceClassification, __webpack_exports__WavLMForXVector as WavLMForXVector, __webpack_exports__WavLMModel as WavLMModel, __webpack_exports__WavLMPreTrainedModel as WavLMPreTrainedModel, __webpack_exports__WeSpeakerFeatureExtractor as WeSpeakerFeatureExtractor, __webpack_exports__WeSpeakerResNetModel as WeSpeakerResNetModel, __webpack_exports__WeSpeakerResNetPreTrainedModel as WeSpeakerResNetPreTrainedModel, __webpack_exports__WhisperFeatureExtractor as WhisperFeatureExtractor, __webpack_exports__WhisperForConditionalGeneration as WhisperForConditionalGeneration, __webpack_exports__WhisperModel as WhisperModel, __webpack_exports__WhisperPreTrainedModel as WhisperPreTrainedModel, __webpack_exports__WhisperProcessor as WhisperProcessor, __webpack_exports__WhisperTextStreamer as WhisperTextStreamer, __webpack_exports__WhisperTokenizer as WhisperTokenizer, __webpack_exports__XLMForQuestionAnswering as XLMForQuestionAnswering, __webpack_exports__XLMForSequenceClassification as XLMForSequenceClassification, __webpack_exports__XLMForTokenClassification as XLMForTokenClassification, __webpack_exports__XLMModel as XLMModel, __webpack_exports__XLMPreTrainedModel as XLMPreTrainedModel, __webpack_exports__XLMRobertaForMaskedLM as XLMRobertaForMaskedLM, __webpack_exports__XLMRobertaForQuestionAnswering as XLMRobertaForQuestionAnswering, __webpack_exports__XLMRobertaForSequenceClassification as XLMRobertaForSequenceClassification, __webpack_exports__XLMRobertaForTokenClassification as XLMRobertaForTokenClassification, __webpack_exports__XLMRobertaModel as XLMRobertaModel, __webpack_exports__XLMRobertaPreTrainedModel as XLMRobertaPreTrainedModel, __webpack_exports__XLMRobertaTokenizer as XLMRobertaTokenizer, __webpack_exports__XLMTokenizer as XLMTokenizer, __webpack_exports__XLMWithLMHeadModel as XLMWithLMHeadModel, __webpack_exports__XVectorOutput as XVectorOutput, __webpack_exports__YolosFeatureExtractor as YolosFeatureExtractor, __webpack_exports__YolosForObjectDetection as YolosForObjectDetection, __webpack_exports__YolosModel as YolosModel, __webpack_exports__YolosObjectDetectionOutput as YolosObjectDetectionOutput, __webpack_exports__YolosPreTrainedModel as YolosPreTrainedModel, __webpack_exports__ZeroShotAudioClassificationPipeline as ZeroShotAudioClassificationPipeline, __webpack_exports__ZeroShotClassificationPipeline as ZeroShotClassificationPipeline, __webpack_exports__ZeroShotImageClassificationPipeline as ZeroShotImageClassificationPipeline, __webpack_exports__ZeroShotObjectDetectionPipeline as ZeroShotObjectDetectionPipeline, __webpack_exports__bankers_round as bankers_round, __webpack_exports__cat as cat, __webpack_exports__cos_sim as cos_sim, __webpack_exports__dot as dot, __webpack_exports__dynamic_time_warping as dynamic_time_warping, __webpack_exports__env as env, __webpack_exports__full as full, __webpack_exports__full_like as full_like, __webpack_exports__getKeyValueShapes as getKeyValueShapes, __webpack_exports__hamming as hamming, __webpack_exports__hanning as hanning, __webpack_exports__interpolate as interpolate, __webpack_exports__interpolate_4d as interpolate_4d, __webpack_exports__interpolate_data as interpolate_data, __webpack_exports__is_chinese_char as is_chinese_char, __webpack_exports__layer_norm as layer_norm, __webpack_exports__log_softmax as log_softmax, __webpack_exports__magnitude as magnitude, __webpack_exports__matmul as matmul, __webpack_exports__max as max, __webpack_exports__mean as mean, __webpack_exports__mean_pooling as mean_pooling, __webpack_exports__medianFilter as medianFilter, __webpack_exports__mel_filter_bank as mel_filter_bank, __webpack_exports__min as min, __webpack_exports__ones as ones, __webpack_exports__ones_like as ones_like, __webpack_exports__permute as permute, __webpack_exports__permute_data as permute_data, __webpack_exports__pipeline as pipeline, __webpack_exports__quantize_embeddings as quantize_embeddings, __webpack_exports__read_audio as read_audio, __webpack_exports__rfft as rfft, __webpack_exports__round as round, __webpack_exports__softmax as softmax, __webpack_exports__spectrogram as spectrogram, __webpack_exports__stack as stack, __webpack_exports__std_mean as std_mean, __webpack_exports__topk as topk, __webpack_exports__window_function as window_function, __webpack_exports__zeros as zeros, __webpack_exports__zeros_like as zeros_like }; + +//# sourceMappingURL=transformers.mjs.map \ No newline at end of file diff --git a/apps/q/core/engine.js b/apps/q/core/engine.js new file mode 100644 index 0000000000000000000000000000000000000000..e4c4a35b4e3d6b2bf8fa36056133487b49c786ce --- /dev/null +++ b/apps/q/core/engine.js @@ -0,0 +1,203 @@ +// core/engine.js — the inference ENGINE adapter (the only module that touches the wasm +// tokenizer + the WebGPU `gpu` object). DOM-free. Wraps a model that core/loader.js has +// already loaded onto the GPU and exposes a clean, UI-agnostic API: +// +// const engine = await createEngine(modelEntry, { gpu, info, imageKappa }); +// const { text, outIds } = await engine.generate(ids, { onToken, signal }); +// const rec = await engine.buildReceipt({ ... }); // PROV-O, re-derivable (Law L5) +// +// The token loop, framing, memo and receipt logic are lifted byte-for-byte from the +// original index.html think()/run()/sealReceipt — only the DOM writes are replaced by an +// onToken callback and the running/handedOff flags by an AbortSignal, so output (and the +// receipt κ) is identical to the original app. + +import { qvac_tokenize, qvac_continue, kappa } from "../pkg/holospaces_web.js"; +import { clean, didHolo, kappaTokens, sealReceipt, verifyIntegrity, idBytes, kappaBytes } from "./kappa.js"; + +const _perf = () => (typeof performance !== "undefined" ? performance.now() : 0); +const _sleep = (ms) => new Promise((r) => setTimeout(r, ms)); + +// The engine is itself a content-addressed object — hash the wasm once (lazy). +let _engineK = null; +export async function engineKappa() { + if (_engineK) return _engineK; + try { const b = new Uint8Array(await (await fetch(new URL("../pkg/holospaces_web_bg.wasm", import.meta.url))).arrayBuffer()); _engineK = await kappaBytes(b); } + catch { _engineK = "did:holo:sha256:(engine unavailable)"; } + return _engineK; +} + +export async function createEngine(modelEntry, loaded) { + const { gpu, info, imageKappa } = loaded; + const m = modelEntry; + const engineReady = engineKappa(); + + // model κ: the κ-disk's VERIFIED image_kappa when present (a real content address of the + // weights, every sector re-derived); else the model's declared identity. + const modelKappa = imageKappa + ? "did:holo:sha256:" + String(imageKappa).replace(/^(did:holo:)?sha256:/, "") + : await didHolo({ "@type": "schema:SoftwareSourceCode", name: m.name, size: m.size, fmt: m.fmt || "", family: m.fam || "" }); + + const memo = new Map(); + let _drafter = null; // learned speculative drafter (fn(seq,max)=>ids); null → standard decode. Set via setDrafter(). + let _pinLen = 0; // KV-COMMONS prefix pin: length of the pinned shared prefix (0 = none). See pinPrefix/usePin below. + + const tokenize = (text) => { try { return JSON.parse(qvac_tokenize(text)).ids || []; } catch { return []; } }; + const detokenize = (ids) => { try { return clean(JSON.parse(qvac_continue(JSON.stringify(ids), 0, 0, 0, ids.length)).text || ""); } catch { return ""; } }; + const fingerprint = (ids) => kappa(idBytes(ids)); // live mind κ (blake3, from wasm) + + // Frame one user turn. Qwen2/3 use ChatML (its <|im_*|> markers are atomic BPE tokens); + // other instruction models use a plain Q/A frame. (Verbatim from the original run().) + function frameTurn(prompt, hasHistory) { + if (m.qwen) { + const noThink = m.qwen3 ? "\n\n\n\n" : ""; // Qwen3: skip the thinking block for fast direct answers + return (hasHistory ? "<|im_end|>\n" : "") + `<|im_start|>user\n${prompt}<|im_end|>\n<|im_start|>assistant\n` + noThink; + } + if (m.llama3) // LLaMA-3 header template (BitNet b1.58 etc.) + return (hasHistory ? "<|eot_id|>" : "") + `<|start_header_id|>user<|end_header_id|>\n\n${prompt}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n`; + if (m.olmo) // OLMo/OLMoE: <|user|>/<|assistant|> role tags (each turn self-delimited; leading bos via m.bos) + return `<|user|>\n${prompt}\n<|assistant|>\n`; + if (m.userWord) // word-frame (Falcon-E: its ChatML template stalls EMPIRICALLY; "User:/Falcon:" answers — see q-falcon-templates sweep) + return (hasHistory ? "\n" : "") + "User: " + prompt + "\nFalcon:"; + return "Question: " + prompt + "\nAnswer:"; + } + + function params() { + const temp = m.temp || 0; + return { decode: temp > 0 ? "sampled@t=" + temp : "greedy-argmax", maxTokens: m.cap, repetitionPenalty: m.rep ?? 1.05, template: m.qwen ? "chatml" : m.llama3 ? "llama3" : "qa", thinking: !m.qwen3 }; + } + + // The streaming token loop. `ids` is the whole running conversation (the mind); generation + // appends to it. onToken({ text, ids, outIds, stats }) fires per step; signal aborts. + async function generate(ids, { onToken, signal, repPenalty, maxNew } = {}) { + const rep = repPenalty ?? m.rep ?? 1.3; + const newCap = maxNew ?? m.cap ?? 80; // max NEW tokens this call + const kvCap = (m.ctx || m.cap || 80) + 8; // the engine's KV allocation (loader kvOf) + const promptLen = ids.length; + const tStart = _perf(); + let first = true, decodeStart = 0, decodeTok = 0, ttft = 0, tokps = 0, msExec = 0, err = null, outText = ""; + if (promptLen >= kvCap - 1) err = new Error(`context full: ${promptLen} tokens ≥ ${kvCap} KV positions`); + + // SPECULATIVE PATH (opt-in): when a learned drafter is registered and the engine has the batched-verify + // head, the drafter proposes and the target batch-verifies — output is BYTE-IDENTICAL to greedy decode + // (greedy verify), streamed via onCommit. Any incompatibility/throw falls straight through to the standard + // loop below, so default Q (no drafter) and unsupported models are completely unaffected. + if (!err && _drafter && gpu.specDecode && gpu.setDrafter) { + try { + gpu.setDrafter(_drafter); + const out = []; const t0 = _perf(); let ttft2 = 0, tokps2 = 0; + const seq = await gpu.specDecode(ids.slice(), newCap, rep, (tk) => { + if (signal && signal.aborted) return false; + out.push(tk); + if (!ttft2) ttft2 = _perf() - tStart; + const dt = _perf() - t0; if (dt > 0) tokps2 = out.length / (dt / 1000); + if (onToken) onToken({ text: detokenize(out), ids: ids.slice(0, promptLen).concat(out), outIds: out.slice(), stats: { ttft: ttft2, tokps: tokps2, msExec: gpu.timing ? gpu.timing.exec : 0, gpuBytes: gpu.gpuBytes, spec: gpu.specStats ? gpu.specStats() : null } }); + return out.length < newCap; + }); + const outIds = seq.slice(promptLen); + let text = detokenize(outIds); + if (m.stopText) { const ix = text.indexOf(m.stopText); if (ix >= 0) text = text.slice(0, ix); } + return { text, outIds, ids: seq, stats: { ttft: ttft2, tokps: tokps2, msExec: gpu.timing ? gpu.timing.exec : 0, spec: gpu.specStats ? gpu.specStats() : null }, error: null }; + } catch (e) { try { gpu.setDrafter(null); } catch {} /* fall through to the standard decode loop */ } + } + + while (!err && !(signal && signal.aborted) && ids.length - promptLen < newCap && ids.length < kvCap - 1) { + const prevLen = ids.length; + try { ids = await (gpu.decode || gpu.generate)(ids, first ? 1 : 6, rep); } // batched GPU decode head (4 B/token readback) when the engine has it + catch (e) { err = e; break; } + const dn = ids.length - prevLen; + if (dn > 0) { + msExec = gpu.timing ? gpu.timing.exec : msExec; + if (first) { ttft = _perf() - tStart; decodeStart = _perf(); first = false; } // TTFT = prefill + first token + else { decodeTok += dn; const dt = _perf() - decodeStart; if (dt > 0) tokps = decodeTok / (dt / 1000); } // steady decode rate + } + const di = ids.slice(promptLen); + // incremental detokenize: decode only a bounded window (the dn new tokens + a small left context) + // and append the delta — O(1) per step, not re-detokenizing the whole growing output (was O(n²)). + { const a = Math.max(promptLen, ids.length - (dn + 8)); const wf = detokenize(ids.slice(a)); const wp = a >= ids.length - dn ? "" : detokenize(ids.slice(a, ids.length - dn)); outText += wf.slice(wp.length); } + let text = outText, hitStop = false; + if (m.stopText) { const ix = text.indexOf(m.stopText); if (ix >= 0) { text = text.slice(0, ix); hitStop = true; } } // word-framed models stop on the next "User:" turn + if (onToken) onToken({ text, ids: ids.slice(), outIds: di.slice(), stats: { ttft, tokps, msExec, gpuBytes: gpu.gpuBytes } }); + if (hitStop) break; + if (ids.length <= prevLen) break; // EOS / no progress + // degeneration guard: a long run of one repeated character (the repetition collapse of + // small/experimental quants) will never recover — stop instead of burning the budget. + if (text.length > 80 && /(.)\1{63}$/.test(text)) { err = new Error("degenerate repetition — stopped"); break; } + await _sleep(0); // yield to the event loop so the UI can paint — no artificial throttle (gpu.decode already awaits the GPU) + } + const outIds = ids.slice(promptLen); + let text = detokenize(outIds); + if (m.stopText) { const ix = text.indexOf(m.stopText); if (ix >= 0) text = text.slice(0, ix); } + return { text, outIds, ids, stats: { ttft, tokps, msExec }, error: err }; + } + + // DIFFUSION decode (Dream-class): iterative bidirectional unmasking over a fixed `steps` budget, + // wall-clock fixed by steps not output length. Greedy ⇒ deterministic ⇒ κ-re-derivable (Law L5). + // `ids` is the framed prompt; we diffuse `genLen` masked positions after it. Returns the same shape + // as generate() so callers (and the brain seam) are agnostic. onToken fires ONCE with the final fill + // (diffusion has no left-to-right token stream — the whole block resolves together). + // Two modes: APPEND (genLen masks at the suffix — generation) or FILL (ids ALREADY contain mask ids + // anywhere → infill/surgical edit, conditioning on BOTH sides; diffusion's structural edge over AR). + // `causal` flips the parity gate (causal block=1 must equal the sequential engine — validates the pass). + async function diffuse(ids, { genLen, steps, fill, causal, signal, onToken } = {}) { + if (!gpu || !gpu.diffuse) throw new Error("this model has no diffusion engine (load a diffusion κ-object)"); + const gl = fill ? 0 : (genLen ?? Math.min(m.cap || 64, (m.ctx || 192) - ids.length - 1)); + const S = steps ?? m.steps ?? 12; + const tStart = _perf(); + const seq = await gpu.diffuse(ids, gl, { steps: S, fill: !!fill, causal: !!causal, signal }); + // append → output is the generated suffix; fill → the whole sequence is the answer (a span edited in place) + const outIds = fill ? seq.slice() : seq.slice(ids.length); + let text = detokenize(outIds); + if (m.stopText && !fill) { const ix = text.indexOf(m.stopText); if (ix >= 0) text = text.slice(0, ix); } + const stats = { ttft: _perf() - tStart, tokps: 0, msExec: gpu.timing ? gpu.timing.exec : 0, steps: S, fill: !!fill, diff: gpu.diffStats ? gpu.diffStats() : null }; + if (onToken) onToken({ text, ids: seq.slice(), outIds: outIds.slice(), stats }); + return { text, outIds, ids: seq, stats, error: null }; + } + + // κ-memo: identical (context ⊕ prompt ⊕ model ⊕ params) → replay in O(1), no decode. + const memoKey = async (ctxIds, turnIds, p) => didHolo({ ctx: await kappaTokens(ctxIds.concat(turnIds)), model: modelKappa, params: p || params() }); + + async function buildReceipt({ promptText, ctxIds, turnIds, outIds, fromMemo, evaluateText, paramsPatch, extraUsed }) { + return sealReceipt({ + promptText, ctxIds, turnIds, outIds, text: detokenize(outIds), params: { ...params(), ...(paramsPatch || {}) }, fromMemo, + modelKappa, engineKappa: await engineReady, evaluateText, extraUsed, + }); + } + + // Re-derivation (greedy only): re-run the exact inference and reproduce κ(output) byte-for-byte. + async function reDerive(rec) { + if (!gpu) return { ok: false, reason: "load the model to re-derive" }; + if (/sampled/.test(rec.params.decode)) return { ok: false, reason: "sampled decode — only the κ-binding is verifiable, not re-derivation" }; + try { + let seq = rec.ctxIds.concat(rec.turnIds); const start = seq.length; + gpu.reset(); + seq = await (gpu.decode || gpu.generate)(seq, rec.outIds.length, rec.params.repetitionPenalty); // same head as the live path — replay must match byte-for-byte + const got = await kappaTokens(seq.slice(start)), want = rec.body["prov:generated"]["holo:outputTokens"]; + return { ok: got === want, got, want }; + } finally { try { gpu.reset(); } catch {} } + } + + return { + model: m, dims: gpu.dims, modelKappa, bosId: info?.bos ?? null, get gpuBytes() { return gpu.gpuBytes; }, + tokenize, detokenize, fingerprint, frameTurn, params, + generate, + // register/clear the learned speculative drafter (fn(seq,max)=>ids). Off by default; safe fallback. + setDrafter: (fn) => { _drafter = fn || null; try { gpu.setDrafter && gpu.setDrafter(_drafter); } catch (e) {} }, + specAvailable: !!(gpu.specDecode && gpu.setDrafter), + // ── KV-COMMONS prefix pin (in-session; parity with the standalone Q engine) ────────────────────── + // Prefill a stable shared prefix (the system persona) ONCE and keep it resident, so every following + // generate() that begins with the SAME tokens reuses its K/V and prefills only the new turn (sync() + // matches the common prefix, decodes from divergence). Byte-identical to a cold prefill — the collapse + // is exact (same ids, same positions, same weights). Needs gpu.sync + gpu.truncateTo (present here). + kvPinAvailable: !!(gpu.truncateTo && gpu.sync), + // pinPrefix(ids): prefill `ids` and remember the resident length as the pin. + pinPrefix: async (ids) => { if (!gpu.sync || !gpu.truncateTo) return 0; try { gpu.reset(); await gpu.sync(ids.slice()); _pinLen = gpu.cachedLen; return _pinLen; } catch (e) { _pinLen = 0; return 0; } }, + // pinCurrent(len): pin an already-resident prefix (e.g. the persona just prefilled as a greeting side-effect) — zero extra prefill. + pinCurrent: (len) => { if (!gpu.truncateTo) return 0; try { _pinLen = gpu.truncateTo(len); return _pinLen; } catch (e) { return 0; } }, + // usePin(): rewind the KV cursor to the pinned prefix right before a turn, so sync() reuses it. Returns reused length. + usePin: () => { if (_pinLen > 0 && gpu.truncateTo) { try { return gpu.truncateTo(_pinLen); } catch (e) { return 0; } } return 0; }, + pinLen: () => _pinLen, + memoKey, memoGet: (k) => memo.get(k), memoHas: (k) => memo.has(k), memoSet: (k, v) => memo.set(k, v), + buildReceipt, verify: verifyIntegrity, reDerive, + stats: () => gpu.timing, reset: () => { try { gpu.reset(); } catch {} }, destroy: () => { try { gpu.destroy(); } catch {} }, + }; +} diff --git a/apps/q/core/kappa.js b/apps/q/core/kappa.js new file mode 100644 index 0000000000000000000000000000000000000000..7fd7b8e49c4484cbc542e6f05f68fa3ac7345755 --- /dev/null +++ b/apps/q/core/kappa.js @@ -0,0 +1,76 @@ +// core/kappa.js — the PURE content-addressing + receipt layer (no wasm, no DOM). +// +// Lifted verbatim (behaviour-identical) from the original Holo Q index.html so that +// BOTH the browser app and the pure-Node witness (tools/q-witness.mjs) import the same +// re-derivation logic. Uses only Web Crypto (crypto.subtle), which exists in the browser +// and in Node ≥ 20 as globalThis.crypto.subtle — so a receipt's κ re-derives identically +// in either runtime (Law L5). The PROV-O receipt body and its did:holo are byte-for-byte +// what the original sealed, so existing receipts keep verifying. + +const _enc = new TextEncoder(); +const _td = new TextDecoder(); + +// RFC 8785 JCS — canonical JSON, so a receipt's κ re-derives identically anywhere. +export const jcs = (v) => Array.isArray(v) ? "[" + v.map(jcs).join(",") + "]" + : (v && typeof v === "object") ? "{" + Object.keys(v).sort().map((k) => JSON.stringify(k) + ":" + jcs(v[k])).join(",") + "}" + : JSON.stringify(v); + +export async function sha256hex(u8) { + const d = await crypto.subtle.digest("SHA-256", u8); + return Array.from(new Uint8Array(d), (b) => b.toString(16).padStart(2, "0")).join(""); +} + +export const idBytes = (ids) => new Uint8Array(new Uint32Array(ids).buffer); + +export const didHolo = async (obj) => "did:holo:sha256:" + await sha256hex(_enc.encode(jcs(obj))); +export const kappaText = async (s) => "did:holo:sha256:" + await sha256hex(_enc.encode(s || "")); +export const kappaTokens = async (a) => "did:holo:sha256:" + await sha256hex(idBytes(a)); // the answer's tokens, by content +export const kappaBytes = async (u8) => "did:holo:sha256:" + await sha256hex(u8); + +export const shortK = (k) => { + const s = String(k || ""); const ax = s.split(":").slice(0, 2).join(":"); const h = s.split(":").pop(); + return ax + ":" + (h.length > 18 ? h.slice(0, 12) + "…" + h.slice(-4) : h); +}; + +// SentencePiece byte-fallback tokens (<0xNN>) → their actual bytes, UTF-8 decoded. +export const decodeBytes = (t) => t.replace(/(?:<0x[0-9A-Fa-f]{2}>)+/g, (run) => { + const b = []; run.replace(/<0x([0-9A-Fa-f]{2})>/g, (_, h) => (b.push(parseInt(h, 16)), "")); + try { return _td.decode(new Uint8Array(b)); } catch { return ""; } +}); +export const clean = (t) => decodeBytes( + t.replace(/[\s\S]*?<\/think>/g, "").replace(/<\/?think>/g, "").replace(/<\|[^>]*\|>/g, "").replace(/||<\/s>/g, "") +).replace(/^\s+/, ""); + +// ── verifiable-inference receipt ──────────────────────────────────────────────── +// An answer is a content-addressed, re-derivable transform on the substrate: +// κ(context) ⊕ κ(prompt) ⊕ κ(model) ⊕ κ(params) ⊕ κ(engine) → κ(output) +// sealed as a PROV-O receipt with its OWN did:holo. Greedy decode is deterministic, +// so anyone re-runs it and reproduces κ(output) byte for byte (Law L5). + +// Build the canonical PROV-O receipt body. All κ inputs are already did:holo strings. +// `extraUsed` merges additional provenance (e.g. holo:toolReceipts — the agentic work-trail). +export function receiptBody({ modelKappa, engineKappa, promptKappa, contextKappa, outputKappa, tokenCount, params, conscience, extraUsed }) { + return { + "@context": ["http://www.w3.org/ns/prov#", { holo: "https://hologram.os/ns/q#" }], + "@type": "prov:Activity", "holo:kind": "verifiable-inference", + "prov:used": { "holo:model": modelKappa, "holo:engine": engineKappa, "holo:prompt": promptKappa, "holo:context": contextKappa, "holo:params": params, ...(extraUsed || {}) }, + "prov:generated": { "holo:outputTokens": outputKappa, "holo:tokenCount": tokenCount }, + "holo:conscience": conscience || { outcome: "unverified" }, + }; +} + +// Seal a receipt: compute the κ inputs from raw tokens/text, assemble the body, address it. +// `text` is the already-decoded answer; `evaluateText` (optional) is the conscience judge. +export async function sealReceipt({ promptText, ctxIds, turnIds, outIds, text, params, fromMemo, modelKappa, engineKappa, evaluateText, extraUsed }) { + const [promptKappa, contextKappa, outputKappa] = await Promise.all([ + kappaText(promptText), kappaTokens(ctxIds.concat(turnIds)), kappaTokens(outIds), + ]); + let conscience = { outcome: "unverified" }; + try { if (evaluateText) { const v = evaluateText(text); conscience = { outcome: v.outcome, blocked: v.blocked || [], caveats: v.caveats || [], sealed: v.sealed !== false }; } } catch {} + const body = receiptBody({ modelKappa, engineKappa, promptKappa, contextKappa, outputKappa, tokenCount: outIds.length, params, conscience, extraUsed }); + const id = await didHolo(body); + return { id, body, text, promptText, ctxIds: ctxIds.slice(), turnIds: turnIds.slice(), outIds: outIds.slice(), params, fromMemo: !!fromMemo }; +} + +// Integrity: recompute the receipt's did:holo from its body — tamper any byte and it won't match. +export const verifyIntegrity = async (rec) => { const again = await didHolo(rec.body); return { ok: again === rec.id, again }; }; diff --git a/apps/q/core/loader.js b/apps/q/core/loader.js new file mode 100644 index 0000000000000000000000000000000000000000..ea7b0234f7002dff913bf0143bbc919ea4136821 --- /dev/null +++ b/apps/q/core/loader.js @@ -0,0 +1,205 @@ +// core/loader.js — model LOADING (the 5 substrate paths) + the model catalog + the +// browser-cache manager. Lifted faithfully from the original index.html so a model still +// loads byte-identically; the only change is that DOM status writes become onStatus/onProgress +// callbacks, and each path RETURNS { gpu, info, manifest, imageKappa } instead of mutating +// globals. core/engine.js then wraps the returned gpu. (window.__gpu / window.__kd handles are +// still exposed for the probe + system-monitor panels.) + +import init, { kappa, qvac_load_model, qvac_load_gpu, qvac_tokenize, qvac_continue, qvac_gpu_manifest, qvac_gpu_tensor, qvac_gpu_free, qvac_panic_hook } from "../pkg/holospaces_web.js"; +import { createQvacGPU } from "../qvac-gpu.js?v=63"; +import { modelAsSource } from "./semantic.js"; // C2: a loaded model carries a W3C @type (schema:SoftwareSourceCode) + +// the model κ-object's W3C linked-data view — content-addressed identity (Law L1) + schema.org type. +const modelLinkedData = (m, root) => modelAsSource({ + name: m.name, family: m.fam, params: m.size, format: m.fmt, + kappa: root ? (String(root).startsWith("did:") ? root : "did:holo:" + String(root)) : "did:holo:sha256:0", +}); + +// The compiled κ-objects present on disk (models/, built by compile2bit.mjs). Each loads +// DIRECT off the substrate (verified by re-derivation, no re-quant) via its `kappaUrl`. +// cap = max NEW tokens per turn; ctx = KV-cache positions allocated on the GPU (the context +// window — sized so agentic turns with tool schemas + tool responses fit; KV VRAM scales with it). +export const MODELS = [ + // NATIVELY-TERNARY κ-objects (t2, 1.58 bpw trained-in — see the atlas-bridge witness receipts): + // Falcon-E: its declared ChatML template STALLS empirically (instant <|end_of_text|>); the + // measured working frame is word-style "User:/Falcon:" with a textual stop (q-falcon-templates sweep). + { fam: "Falcon-E", name: "Falcon-E-3B · ternary", kappaUrl: "https://huggingface.co/HOLOGRAMTECH/q-falcon-e-3b/resolve/main", manifestKappa: "did:holo:sha256:6b753fe8186f2b4194424115c36014698580a2aab8427e9b40365893ac6b77ca", size: "0.63 GB", fmt: "t2 1.58-bit κ", cap: 200, ctx: 3000, kv4: true, gpu: true, gpuOnly: true, chat: true, userWord: true, stopText: "\nUser:", tools: false, rep: 1.18, kappa: true }, + { fam: "BitNet", name: "BitNet-2B-4T · ternary", kappaUrl: "https://huggingface.co/HOLOGRAMTECH/q-bitnet-2b/resolve/main", manifestKappa: "did:holo:sha256:fcf835659d88d2fe6f683cf1ab8de6a6ba6214ea0deeee4b1bcf3da1a4c05412", size: "0.69 GB", fmt: "t2 1.58-bit κ", cap: 900, ctx: 3000, kv4: true, gpu: true, gpuOnly: true, chat: true, llama3: true, tools: false, bos: true, eosText: "<|eot_id|>", rep: 1.05, kappa: true }, + // TriLM: the LARGEST natively-ternary-trained model (Spectra 3.9B, ICLR'25); per-row/channel + // scale structure → t2r (trit codes + per-256-block scales, exact). BASE model → QA frame + stop. + { fam: "TriLM", name: "TriLM-3.9B · ternary", kappaUrl: "https://huggingface.co/HOLOGRAMTECH/q-trilm-3.9b/resolve/main", manifestKappa: "did:holo:sha256:499032ceb19c0476345a72cf5fea6caec83054c98486c91a5891dfad0d25ea30", size: "0.87 GB", fmt: "t2r 2.1-bit κ", cap: 200, ctx: 3000, kv4: true, gpu: true, gpuOnly: true, chat: true, stopText: "\nQuestion:", tools: false, rep: 1.18, kappa: true }, + // AGENTIC CODER: Qwen2.5-Coder-7B (q3f) — the Holo Code agent brain. Qwen2.5 arch ⇒ ChatML + + // agentic tool framing work (capability floor for tool use is ~7B; the small ternary models opt out). + // Self-contained κ-object: tokenizer bundled (source="tokenizer.gguf"), no external dependency. + { fam: "Qwen2.5-Coder", name: "Qwen2.5-Coder-7B · agentic", kappaUrl: "https://huggingface.co/HOLOGRAMTECH/q-qwen-coder-7b/resolve/main", manifestKappa: "did:holo:sha256:539941cb060c7dd583e2e86697e53f2c5d511d597c65d09d9c780fbded2c3edf", size: "3.4 GB", fmt: "q3f κ", cap: 900, ctx: 3000, kv4: true, gpu: true, gpuOnly: true, chat: true, code: true, qwen: true, rep: 1.05, kappa: true }, + // MIXTURE-OF-EXPERTS (G5): OLMoE-1B-7B (Allen AI, Apache-2.0) — 64 experts, 8 active/token, ~1.3B + // active of 7B. The first RESIDENT-MoE κ-object: experts RAM-resident + CPU top-k router (softmax + // over all 64, no renorm = OLMoE norm_topk_prob:false). q4 (the engine's resident expert FFN path). + { fam: "OLMoE", name: "OLMoE-1B-7B · MoE (64×8)", kappaUrl: "https://huggingface.co/HOLOGRAMTECH/q-olmoe-1b-7b/resolve/main", manifestKappa: "did:holo:sha256:9cf97ec1c761fd4ef51bc0cd4ac37a0cd8eaa11f1b19b3ae6a141486ad3fe5ad", size: "3.6 GB", fmt: "q4 MoE κ", cap: 400, ctx: 3000, kv4: false, gpu: true, gpuOnly: true, chat: true, olmo: true, bos: true, eosText: "<|endoftext|>", tools: false, rep: 1.1, kappa: true }, + // DIFFUSION (G6): Dream-7B (Dream-org/Dream-v0-Instruct-7B) — masked-diffusion LM on the Qwen2.5-7B + // backbone (same dims ⇒ ChatML). NOT autoregressive: generation is iterative bidirectional unmasking + // over `steps` denoising passes (engine.diffuse / gpu.diffuse), wall-clock fixed by steps not length. + // maskId 151666 rides in the manifest (never tokenized from text). Greedy ⇒ deterministic ⇒ κ-re-derivable. + { fam: "Dream", name: "Dream-7B · diffusion", kappaUrl: "https://huggingface.co/HOLOGRAMTECH/q-dream-7b/resolve/main", manifestKappa: "did:holo:sha256:7b862931ae088f348f1f7e9ea3adbd418924c2e07e6ddd134f926e5681ad760d", size: "2.9 GB", fmt: "q3f diffusion κ", cap: 192, ctx: 192, kv4: false, gpu: true, gpuOnly: true, chat: true, qwen: true, diffusion: true, steps: 12, rep: 1.0, kappa: true }, + // Qwen κ-objects (q3f/q4) were pruned from disk for space — re-derive via compile2bit, then re-list. +]; +const kvOf = (m) => Math.max(96, (m.ctx || m.cap) + 8); + +const _sizeGb = (s) => { const n = parseFloat(s) || 0; return /mb/i.test(s) ? n / 1024 : n; }; +// default to the SMALLEST usable model — lowest latency, fastest first answer. +export const defaultModelIndex = () => (MODELS.map((m, i) => i).filter((i) => !MODELS[i].disabled).sort((a, b) => _sizeGb(MODELS[a].size) - _sizeGb(MODELS[b].size))[0]) ?? 0; + +// ── wasm init (once) + tokenizer re-export so the rest of the app shares this instance ── +let _initOnce = null; +export function ready() { if (!_initOnce) _initOnce = init().then(() => { try { qvac_panic_hook(); } catch {} }); return _initOnce; } +export { qvac_tokenize, qvac_continue, kappa }; + +// ── browser-cache model manager (Cache API) — "Get" downloads + keeps; loading uses the copy ── +export const MCACHE = "holo-q-models"; +const absUrl = (u) => new URL(u, location.href).href; +let _cachedUrls = new Set(); +export async function refreshCached() { try { const c = await caches.open(MCACHE); _cachedUrls = new Set((await c.keys()).map((r) => r.url)); } catch { _cachedUrls = new Set(); } return _cachedUrls; } +export const isCached = (m) => !!m.url && _cachedUrls.has(absUrl(m.url)); +export async function deleteCache(m) { try { const c = await caches.open(MCACHE); await c.delete(m.url); } catch {} await refreshCached(); } +async function modelBytes(m, onStatus) { + try { const c = await caches.open(MCACHE); const hit = await c.match(m.url); if (hit) return new Uint8Array(await hit.arrayBuffer()); } catch {} + onStatus?.(`Downloading ${m.name} (${m.size})…`); + const res = await fetch(m.url); if (!res.ok) { onStatus?.("download failed: HTTP " + res.status); return null; } + return new Uint8Array(await res.arrayBuffer()); +} + +const noop = () => {}; + +// loadModel(entry, { onStatus, onProgress }) → { gpu, info, manifest, imageKappa } | null +// `imageKappa` is the VERIFIED content address of the weights when the path provides one +// (κ-object root, or κ-disk image_kappa); core/engine.js binds it as the receipt's model κ. +export async function loadModel(m, { onStatus = noop, onProgress = noop } = {}) { + await ready(); + onStatus(`Loading ${m.name}…`); + try { + if (m.gpuOnly && !navigator.gpu) { onStatus("This model needs WebGPU (not available here)."); return null; } + if (m.kappaUrl) return await loadKappa(m, onStatus, onProgress); + if (m.kdisk) return await loadModelKDisk(m, onStatus, onProgress); + if (m.remote) return await loadModelRemote(m, onStatus, onProgress); + if (m.diskIngest) return await loadModelDisk(m, onStatus, onProgress); + let gguf = await modelBytes(m, onStatus); if (!gguf) { onStatus("could not load model"); return null; } + const lr = JSON.parse(m.gpuOnly ? qvac_load_gpu(gguf) : qvac_load_model(gguf)); + gguf = null; + if (lr.error) { onStatus("model error: " + lr.error); return null; } + let gpu = null, manifest = null; + if (navigator.gpu && m.gpu) { + try { + onStatus(`Uploading ${m.name} to the GPU…`); + const bits = m.q4 ? 4 : 8; + manifest = JSON.parse(qvac_gpu_manifest(bits)); manifest.twoBit = !!window.__twoBit; + const __qp = new URLSearchParams(location.search).get("stream"); + const __qmode = __qp === null ? undefined : (__qp === "resident" || __qp === "false" ? false : __qp); + const stream = __qmode ?? window.__stream ?? m.stream ?? false; + const __ft = (name) => { const raw = qvac_gpu_tensor(name, bits); return window.__weightHook ? window.__weightHook(name, raw, bits, manifest) : raw; }; + gpu = await createQvacGPU(manifest, __ft, kvOf(m), lr.eos ?? 2, stream); + window.__gpu = gpu; qvac_gpu_free(); + } catch (e) { gpu = null; if (m.gpuOnly) { onStatus("GPU upload failed: " + e); return null; } } + } + onStatus(""); + return { gpu, info: lr, manifest, imageKappa: null }; + } catch (e) { onStatus("could not load model: " + e); return null; } +} + +// LOAD-DIRECT: a pre-compiled 2-bit/Q4 κ-object (compile2bit.mjs output). Weights arrive ALREADY +// quantized (no re-quant at load); the tokenizer comes from the source GGUF's header only. +async function loadKappa(m, onStatus, onProgress) { + onStatus("Loading κ-object manifest…"); + const ld = await import("../holo-load2bit.mjs?v=2"); + // Law L5: pin the manifest κ when the catalog supplies one (m.manifestKappa, or a string m.kappa). + // Until every model carries a pin, unpinned entries load explicitly (allowUnpinned) — the gap is then + // a visible data task (populate manifestKappa), not a silent trust of an unauthenticated root. + const pin = (typeof m.manifestKappa === "string" && m.manifestKappa) || (typeof m.kappa === "string" && m.kappa) || null; + const __b3 = (typeof window !== "undefined" && window.__blake3Map) || undefined; // inject canonical map (test BLAKE3 axis before the HF upload) + const { manifest, fetchTensor, info } = await ld.loadKappaObject(m.kappaUrl.replace(/\/+$/, ""), { ...(pin ? { expectKappa: pin } : { allowUnpinned: true }), blake3Map: __b3 }); + const ing = await import("../qvac-ingest.mjs"); + onStatus("Building tokenizer (source header, no full download)…"); + const hdr = await ing.readHeader(info.source, ing.rangeReader()); + const lr = JSON.parse(qvac_load_gpu(hdr.headerBytes)); + if (lr.error) { onStatus("tokenizer error: " + lr.error); return null; } + if (m.eosText) { try { const e = JSON.parse(qvac_tokenize(m.eosText)).ids; if (e && e.length === 1) lr.eos = e[0]; } catch {} } // chat-stop override (e.g. LLaMA-3 <|eot_id|> ≠ header eos) + qvac_gpu_free(); + manifest.kv4 = !!m.kv4; // int4 KV cache (E6) — catalog opt-in + // MoE forward reads the layer-packed attention (Wb[l]) + RAM-resident experts (readExpert via + // fetchTensor) — i.e. stream="layer": attention JS-resident & paged per token, experts cached. + const sm = manifest.moe ? "layer" : (m.stream || window.__kappaStream || false); + onStatus(`Building engine from κ-object (${info.mode === "q4" ? "native Q4" : info.incoherent ? "incoherent 2-bit" : "LDLQ 2-bit"}, ${sm || "resident"}, no requant)…`); + const prog = (done, total) => onProgress(done, total, "streaming"); + const gpu = await createQvacGPU(manifest, fetchTensor, kvOf(m), lr.eos ?? 2, sm, sm ? prog : null); + window.__gpu = gpu; + onStatus(""); + return { gpu, info: lr, manifest, imageKappa: info.root || null, ld: modelLinkedData(m, info.root) }; +} + +// Very-large-model path: the GGUF never enters wasm; only the header does (tokenizer + manifest), +// then each tensor is streamed off disk (HTTP Range), converted in JS, paged to the GPU per layer. +async function loadModelDisk(m, onStatus, onProgress) { + const bits = m.q4 ? 4 : 8; + const ing = await import("../qvac-ingest.mjs"); + let read = ing.rangeReader(); + try { const cachedResp = await (await caches.open(MCACHE)).match(m.url); if (cachedResp) { const blob = await cachedResp.blob(); read = async (_u, start, len) => new Uint8Array(await blob.slice(start, start + len).arrayBuffer()); } } catch {} + onStatus(`Reading ${m.name} header…`); + const hdr = await ing.readHeader(m.url, read); + const lr = JSON.parse(qvac_load_gpu(hdr.headerBytes)); + if (lr.error) { onStatus("model error: " + lr.error); return null; } + const manifest = JSON.parse(qvac_gpu_manifest(bits)); + qvac_gpu_free(); + const fetchTensor = ing.makeDiskFetcher({ url: m.url, readRange: read, dataOffset: hdr.dataOffset, tensors: hdr.tensors, manifest, bits }); + const mode = m.stream || "layer"; + onStatus(`Preparing ${m.name} (one-time, streamed off disk)…`); + const gpu = await createQvacGPU(manifest, fetchTensor, kvOf(m), lr.eos ?? 2, mode, (d, t) => onProgress(d, t, "layers")); + window.__gpu = gpu; onStatus(""); + return { gpu, info: lr, manifest, imageKappa: null }; +} + +// Out-of-core: stream a PRE-BUILT .qvf frames file from the server, one layer per token via HTTP Range. +async function loadModelRemote(m, onStatus, onProgress) { + onStatus(`Loading ${m.name} index…`); + const index = await (await fetch(m.framesUrl + ".json")).json(); + const url = m.framesUrl; + const rr = async (off, len) => { const r = await fetch(url, { headers: { Range: `bytes=${off}-${off + len - 1}` } }); if (!r.ok && r.status !== 206) throw new Error("HTTP " + r.status); return new Uint8Array(await r.arrayBuffer()); }; + const header = await rr(index.headerOff, index.headerLen); + const lr = JSON.parse(qvac_load_gpu(header)); + if (lr.error) { onStatus("model error: " + lr.error); return null; } + const manifest = index.manifest; qvac_gpu_free(); + const fetchTensor = async (name) => { const s = index.singles[name]; return s ? await rr(s.off, s.len) : new Uint8Array(0); }; + const frameStore = { ready: true, read: (off, len) => rr(index.layersOff + off, len), readExpert: (l, e, role) => { const ri = { gate: 0, up: 1, down: 2 }[role]; const off = index.expertsOff + ((l * index.nExperts + e) * 3 + ri) * index.expertBytes; return rr(off, index.expertBytes); } }; + const layersBytes = (index.packStride || 0) * (index.n_layers || 0) + (manifest.moe ? (index.nExperts * 3 * index.expertBytes * index.n_layers) : 0); + const cacheBudget = window.__cacheGB != null ? window.__cacheGB * 1073741824 : Math.min(layersBytes, 12 * 1073741824); + onStatus(`Preparing ${m.name} (served off disk)…`); + const gpu = await createQvacGPU(manifest, fetchTensor, kvOf(m), lr.eos ?? 2, "remote", (d, t) => onProgress(d, t, "remote"), frameStore, cacheBudget); + window.__gpu = gpu; onStatus(""); + return { gpu, info: lr, manifest, imageKappa: null }; +} + +// HOLOGRAM: load through a content-addressed κ-DISK — every sector VERIFIED by re-derivation (Law L3/L5). +async function loadModelKDisk(m, onStatus, onProgress) { + onStatus(`Resolving ${m.name} κ-disk…`); + const index = await (await fetch(m.kdiskUrl)).json(); + const { makeKDisk } = await import("../qvac-kdisk.mjs"); + const bases = window.__kdiskSources || m.kdiskSources || [location.origin]; + const sources = bases.map((b) => b.replace(/\/$/, "") + "/" + (index.dataFile || (m.dataUrl || "").replace(/^\.\//, ""))); + const kd = makeKDisk({ index, sources }); + window.__kd = kd; + const iv = await kd.verifyImage(); + if (!iv.ok) { onStatus("κ-disk image_kappa mismatch — refusing to load"); return null; } + const rr = kd.rr, qvf = index.qvf; + const header = await rr(qvf.headerOff, qvf.headerLen); + const lr = JSON.parse(qvac_load_gpu(header)); + if (lr.error) { onStatus("model error: " + lr.error); return null; } + const manifest = qvf.manifest; qvac_gpu_free(); + const fetchTensor = async (name) => { const s = qvf.singles[name]; return s ? await rr(s.off, s.len) : new Uint8Array(0); }; + const frameStore = { ready: true, read: (off, len) => rr(qvf.layersOff + off, len), + readExpert: async (l, e, role) => { const ri = { gate: 0, up: 1, down: 2 }[role]; const blkOff = qvf.expertsOff + (l * qvf.nExperts + e) * 3 * qvf.expertBytes; const blk = await rr(blkOff, 3 * qvf.expertBytes); return blk.slice(ri * qvf.expertBytes, (ri + 1) * qvf.expertBytes); } }; + const layersBytes = (qvf.packStride || 0) * (qvf.n_layers || 0) + (manifest.moe ? (qvf.nExperts * 3 * qvf.expertBytes * qvf.n_layers) : 0); + const cacheBudget = window.__cacheGB != null ? window.__cacheGB * 1073741824 : Math.min(layersBytes, 1024 * 1048576); + onStatus(`Realizing ${m.name} (verified off κ-disk)…`); + const gpu = await createQvacGPU(manifest, fetchTensor, kvOf(m), lr.eos ?? 2, "remote", (d, t) => onProgress(d, t, "κ-disk"), frameStore, cacheBudget); + window.__gpu = gpu; + const st = kd.stats(); onStatus(`${index.imageKappa.slice(0, 22)}… · ${st.verified} sectors verified`); + return { gpu, info: lr, manifest, imageKappa: kd.imageKappa || index.imageKappa || null }; +} diff --git a/apps/q/core/q-brain-fast.mjs b/apps/q/core/q-brain-fast.mjs new file mode 100644 index 0000000000000000000000000000000000000000..022f08c4ffdc1077ad8482dedbefc95adb43fc1c --- /dev/null +++ b/apps/q/core/q-brain-fast.mjs @@ -0,0 +1,236 @@ +// core/q-brain-fast.mjs — Q's FAST on-device brain, drop-in for createHoloModelBrain. +// +// Same provider shape (load · generate → text-delta async-iterator · chat · info · setSkill), so +// holo-q-contact.mjs's makeQResponder / makeQGroupResponder bind it with ZERO changes: Q rides the exact +// stream→finalize pipeline, it just sources its bytes from THIS engine (core/loader + core/engine — the +// native-ternary κ-object path with the fixed incremental-detok decode) instead of the qwen holo-brain. +// +// Why this exists: the messenger's default brain (qwen2.5-0.5b via holo-brain-engine) is heavy and its +// app-path decode was O(n²). This one loads a native-ternary BitNet κ-object (0.69 GB, verified per-block), +// decodes at ~70 tok/s warm, and streams byte-identical incremental text. It is 100% on-device at decode +// time — only a ONE-TIME tokenizer-header Range fetch touches the source host at load (the weights are the +// local, L5-verified b/<κ>.gz blocks). Bundle the header to make load fully egress-free (follow-up). +// +// URL discipline (the one gotcha): core/loader's MODELS use a PAGE-relative kappaUrl ("./models/"), +// which is wrong from any page other than /apps/q/. loadKappaObject resolves the manifest, blocks AND the +// bundled tokenizer relative to its baseUrl, so we override kappaUrl to an ABSOLUTE mount ("/apps/q/models/ +// ") — that makes the whole load self-contained from the messenger (or anywhere). + +import { ready, loadModel, MODELS } from "./loader.js"; +import { createEngine } from "./engine.js"; +import { selfFacts, selfPersona, selfIntro } from "./q-self.mjs"; // Q's live, grounded self-knowledge (M0) + +// The default fast brain = BitNet-2B (native ternary, llama3 template, coherent — Falcon-E degenerates). +// Override via opts.family / opts.modelName. kappaBase is the absolute mount core/loader's models live under. +const DEFAULTS = { family: "BitNet", kappaBase: "/apps/q/models", maxTokens: 512 }; + +function pickModel(cfg) { + const want = String(cfg.modelName || cfg.family || "BitNet").toLowerCase(); + const m = MODELS.find((x) => (x.fam || "").toLowerCase() === want || (x.name || "").toLowerCase().includes(want)) || MODELS.find((x) => (x.fam || "").toLowerCase() === "bitnet"); + if (!m) throw new Error("q-brain-fast: no model matches " + want); + // An ABSOLUTE host URL (the κ-object now lives on HF, not on this machine) is already page-independent — + // use it verbatim. Only a page-relative "./models/" needs absolute-mounting so it resolves from ANY page. + if (/^https?:\/\//.test(m.kappaUrl || "")) return { ...m }; + const rel = String(m.kappaUrl || "").replace(/^\.?\//, "").replace(/^models\//, ""); // "./models/bitnet-2b" → "bitnet-2b" + return { ...m, kappaUrl: String(cfg.kappaBase).replace(/\/+$/, "") + "/" + rel }; +} + +// Render an [{role,content}] history (system + turns, as makeQResponder builds it) to the model's chat +// template — the multi-turn generalization of core/engine's single-turn frameTurn. Covers the templates +// the engine knows; unknown families fall back to a persona-led last-user Q/A frame. +export function frameHistory(M, history) { + const list = Array.isArray(history) ? history : []; + // merge ALL system turns into one system block (persona + any injected context, e.g. M1 grounded retrieval) — + // taking only the first would silently DROP injected context and the model would fall back to a generic refusal. + const persona = list.filter((x) => x && x.role === "system" && x.content).map((x) => x.content).join("\n\n"); + const turns = list.filter((x) => x && x.role !== "system" && (x.content || "").length); + + if (M.llama3) { + let s = persona ? `<|start_header_id|>system<|end_header_id|>\n\n${persona}<|eot_id|>` : ""; + for (const t of turns) s += `<|start_header_id|>${t.role === "assistant" ? "assistant" : "user"}<|end_header_id|>\n\n${t.content}<|eot_id|>`; + return s + `<|start_header_id|>assistant<|end_header_id|>\n\n`; + } + if (M.qwen) { + const noThink = M.qwen3 ? "\n\n\n\n" : ""; + let s = persona ? `<|im_start|>system\n${persona}<|im_end|>\n` : ""; + for (const t of turns) s += `<|im_start|>${t.role === "assistant" ? "assistant" : "user"}\n${t.content}<|im_end|>\n`; + return s + `<|im_start|>assistant\n` + noThink; + } + if (M.olmo) { + let s = persona ? `<|system|>\n${persona}\n` : ""; + for (const t of turns) s += t.role === "assistant" ? `<|assistant|>\n${t.content}\n` : `<|user|>\n${t.content}\n`; + return s + `<|assistant|>\n`; + } + // word-frame / plain: only the last user turn carries (base models have no multi-turn template) + const lastUser = [...turns].reverse().find((t) => t.role !== "assistant"); + const q = (lastUser && lastUser.content) || ""; + if (M.userWord) return (persona ? persona + "\n" : "") + "User: " + q + "\nFalcon:"; + return (persona ? persona + "\n" : "") + "Question: " + q + "\nAnswer:"; +} + +// A stable fingerprint of a rendered history (roles+contents) — warm continuation reuses the running token +// sequence only when the new history is EXACTLY the previous one plus a fresh user turn (same system, same prior). +function sigOf(list) { return (list || []).map((e) => (e.role || "") + "" + (e.content || "")).join(""); } + +// The incremental frame for ONE new user turn — the tokens frameHistory() would ADD after the previous assistant +// reply (which the KV already holds). It closes the prior reply (/<|im_end|>/newline) then adds the user block +// and the assistant header, byte-for-byte matching frameHistory so warm ids extend the cold prefix exactly. Returns +// null for templates whose trailing generation header differs from the in-context turn (qwen3 ), forcing cold. +function tailSegment(M, userText) { + const u = userText || ""; + if (M.llama3) return `<|eot_id|><|start_header_id|>user<|end_header_id|>\n\n${u}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n`; + if (M.qwen && !M.qwen3) return `<|im_end|>\n<|im_start|>user\n${u}<|im_end|>\n<|im_start|>assistant\n`; + if (M.olmo) return `\n<|user|>\n${u}\n<|assistant|>\n`; + return null; // word-frame / diffusion / qwen3-thinking → no stable single-turn tail; rebuild cold +} + +// framePersona(M, personaText) → JUST the system block frameHistory() renders (NO trailing assistant/generation +// header), so its token ids are a byte-prefix of any real turn's ids and can be pinned once + reused (KV-commons). +// Templated families end the block on a special token (<|eot_id|> / <|im_end|>), so the boundary tokenizes stably. +// Returns null for base/word-frame families (no special-token boundary → prefix could drift → don't pin, prefill cold). +export function framePersona(M, personaText) { + const p = String(personaText || ""); if (!p) return ""; + if (M.llama3) return `<|start_header_id|>system<|end_header_id|>\n\n${p}<|eot_id|>`; + if (M.qwen) return `<|im_start|>system\n${p}<|im_end|>\n`; + if (M.olmo) return `<|system|>\n${p}\n`; + return null; +} + +export function createFastQBrain(opts = {}) { + const cfg = Object.assign({}, DEFAULTS, opts); + const M = pickModel(cfg); + let engine = null, loadingP = null; + let info = { ready: false, model: M.name, device: null, resident: false }; + // WARM-KV session: the running token ids of the LAST committed turn (system + all prior turns + the reply AS + // GENERATED — never re-tokenized) and a signature of the history they represent. buildIds() extends this by one + // user turn so gpu.sync() finds the whole prior conversation already in KV and prefills ONLY the new turn. + let sess = null; // { ids: number[], sig: string } + + // encode a history to the running token ids (framed + optional bos), ready for engine.generate + function idsFor(history) { + let ids = engine.tokenize(frameHistory(M, history)); + if (M.bos && engine.bosId != null) ids = [engine.bosId, ...ids]; + return ids; + } + + // WARM path: if `history` is exactly the last committed conversation plus one new user turn (same system + prior), + // extend the running ids by just that turn's tokens — reusing the generated reply ids VERBATIM (no re-tokenize + // round-trip that would drift the prefix). gpu.sync() then reuses the resident KV for the whole prior conversation + // and prefills only the appended tokens. Otherwise fall back to a full cold frame. Warm is always CORRECT: if the + // GPU KV was disturbed by an interleaved call, sync() simply re-prefills the same ids from scratch (== cold cost). + function buildIds(history) { + const list = Array.isArray(history) ? history : []; + const last = list[list.length - 1]; + if (sess && list.length >= 2 && last && last.role !== "system") { + const tail = tailSegment(M, last.content); + if (tail != null && sigOf(list.slice(0, list.length - 1)) === sess.sig) { + return { ids: sess.ids.concat(engine.tokenize(tail)), warm: true }; + } + } + return { ids: idsFor(list), warm: false }; + } + // record the running ids + the history they now represent, so the NEXT turn can extend them. Only committed turns + // advance the session; a speculative branch (persist:false) warms the GPU KV but leaves the committed pointer put. + function updateSession(history, replyText, finalIds, aborted) { + if (aborted || !finalIds || !replyText) return; // partial/empty → don't trust continuity next turn (stays cold, safe) + const represented = (Array.isArray(history) ? history : []).concat([{ role: "assistant", content: replyText }]); + sess = { ids: finalIds.slice(), sig: sigOf(represented) }; + } + + async function load(onProgress) { + if (engine) return info; + if (loadingP) return loadingP; + loadingP = (async () => { + if (!(typeof navigator !== "undefined" && navigator.gpu)) throw new Error("no WebGPU on this device"); + await ready(); // wasm tokenizer init (shared instance) + const loaded = await loadModel(M, { + onStatus: () => {}, + onProgress: (d, t, w) => { try { onProgress && onProgress({ done: d, total: t, phase: w, model: M.name }); } catch (e) {} }, + }); + if (!loaded || !loaded.gpu) throw new Error("q-brain-fast: model load failed (" + M.name + ")"); + engine = await createEngine(M, loaded); + // MEASURED from the real engine, not asserted: device/resident reflect an engine that actually uploaded + // weights to the GPU (dims + gpuBytes are live signals) — so info() can never claim residency it lacks. + info = { ready: true, model: M.name, device: (engine && engine.dims ? "webgpu" : null), resident: !!(engine && (engine.gpuBytes || engine.dims)) }; + return info; + })().catch((e) => { loadingP = null; throw e; }); + return loadingP; + } + async function ensure(onProgress) { if (!engine) await load(onProgress); return engine; } + + // generate(history, { signal, onProgress }) → async-iterator of TEXT DELTAS (exactly what makeQResponder + // accumulates + paints via onDelta). engine.generate reports CUMULATIVE text per step; we diff to deltas + // and pump them through a small queue so this stays a clean generator (and honors the abort signal). + async function* generate(history, o = {}) { + await ensure(o.onProgress); + if (!engine) return; + const signal = o.signal || null; + const { ids, warm } = buildIds(history); + if (o.onWarm) try { o.onWarm(warm); } catch (e) {} // let the caller instrument warm-vs-cold prefill + const cap = o.maxTokens || cfg.maxTokens || M.cap || 256; + const persist = o.speculative !== true; // a speculative branch warms the KV but must NOT advance the committed session + + const queue = []; let done = false, wake = null, prev = "", finalIds = null; + const kick = () => { if (wake) { const w = wake; wake = null; w(); } }; + const run = engine.generate(ids, { + maxNew: cap, signal, + onToken: ({ text, ids: cur }) => { if (cur) finalIds = cur; const d = (text || "").slice(prev.length); if (d) { prev = text; queue.push(d); kick(); } }, + }).then((res) => { if (res && res.ids) finalIds = res.ids; done = true; kick(); }).catch(() => { done = true; kick(); }); + + while (true) { + if (queue.length) { yield queue.shift(); continue; } + if (done) break; + if (signal && signal.aborted) break; + await new Promise((r) => (wake = r)); + } + try { await run; } catch (e) {} + // extend the warm-KV session with this turn's full running ids (only for a COMMITTED, complete turn — a + // speculative branch leaves the committed pointer put; its KV still lives in gpu.cached for sync() to reuse). + if (persist) updateSession(history, prev, finalIds, !!(signal && signal.aborted)); + } + + // chat(history, opts) → full string (used by window.HoloQ.generate + light background features). + // o.onStats(stats) surfaces the engine's measured { ttft, tokps, msExec } for a turn (else discarded) — used by + // the reply path's latency HUD + HoloQ.selfTest to SHOW the warm-KV win instead of asserting it. Non-breaking. + async function chat(history, o = {}) { + await ensure(o.onProgress); + if (!engine) return ""; + const ids = idsFor(history); + const res = await engine.generate(ids, { maxNew: o.maxTokens || cfg.maxTokens || M.cap || 256, signal: o.signal || null }); + if (o.onStats) { try { o.onStats({ ...(res && res.stats || {}), promptTokens: ids.length }); } catch (e) {} } + return ((res && res.text) || "").trim(); + } + + // native-ternary κ-object → no LoRA adapters; skill routing is a no-op (the base is the specialist). + const setSkill = async () => ({ adapter: false, unsupported: true }); + const setAdapter = async () => ({ adapter: false, unsupported: true }); + + // Q's LIVE self-knowledge (M0), derived from THIS instance's real model + engine κ — the grounded truth + // every surface leads with so Q is honestly self-aware and never confabulates a cloud identity. Available + // even before load (identity is in M); the κ fills in once the engine is resident. + const facts = () => selfFacts({ model: M, engine }); + const persona = () => selfPersona({ model: M, engine }); + const intro = () => selfIntro({ model: M, engine }); + + // pinPersona(text): prefill + pin the EXACT persona system block ONCE (KV-COMMONS), so the FIRST turn reuses it + // instead of re-prefilling the whole persona. `text` MUST equal what the caller sends as the system turn (e.g. the + // messenger's persona()+Q_STYLE) or the ids won't byte-match and the pin is silently wasted. Call once, post-warm, + // before the first turn (pinPrefix resets the KV, so never mid-conversation). Returns the pinned length (0 = skipped). + let _personaPinned = false; + async function pinPersona(text) { + try { + if (_personaPinned || !engine || !engine.kvPinAvailable) return 0; + const block = framePersona(M, text != null ? text : persona()); + if (!block) return 0; // null/empty → base family or no persona; skip (cold, correct) + let ids = engine.tokenize(block); + if (M.bos && engine.bosId != null) ids = [engine.bosId, ...ids]; + const L = await engine.pinPrefix(ids); + _personaPinned = L > 0; + return L; + } catch (e) { return 0; } + } + + return { id: "q-brain-fast-" + (M.fam || M.name), load, generate, chat, setSkill, setAdapter, info: () => info, facts, persona, intro, pinPersona, pinLen: () => { try { return engine && engine.pinLen ? engine.pinLen() : 0; } catch (e) { return 0; } } }; +} + +export default createFastQBrain; diff --git a/apps/q/core/q-self.mjs b/apps/q/core/q-self.mjs new file mode 100644 index 0000000000000000000000000000000000000000..4fa9766742e8e33e511e0d5c1aafce71e0f30d6e --- /dev/null +++ b/apps/q/core/q-self.mjs @@ -0,0 +1,62 @@ +// core/q-self.mjs — Q's LIVE, GROUNDED self-knowledge. The single source of truth for who/what/where/how Q +// is, DERIVED FROM REAL RUNTIME SIGNALS (the loaded model, its content-addressed κ, the device, optional +// system health) — never hardcoded, never guessed. Every Q surface (messenger, q-chat, voice) leads with +// this, so Q is truthfully self-aware EVERYWHERE and can never confabulate a cloud/OpenAI/AWS identity. +// +// Why this exists (the law: grounded transcendence, never performed): a base model has NO self-knowledge — +// asked "what are you" it returns the average of its training data ("I run on AWS/OpenAI"), which is false. +// So Q's identity is not a persona we write; it is the TRUTH of this running instance, computed here. If a +// fact isn't really knowable in this context, we OMIT it — we never invent one. +// +// DOM-free + fully guarded: safe to import in any surface (standalone app, messenger, OS shell, Node witness). + +// Structured self-facts from whatever is really knowable HERE. Fail-soft: a missing signal is omitted. +export function selfFacts({ model, engine } = {}) { + const f = { name: "Q" }; + try { if (model) { if (model.name) f.model = model.name; if (model.fam) f.family = model.fam; if (model.fmt) f.quant = model.fmt; if (model.size) f.size = model.size; } } catch (e) {} + try { if (engine && engine.modelKappa) f.kappa = engine.modelKappa; } catch (e) {} + try { f.gpu = (typeof navigator !== "undefined" && !!navigator.gpu); } catch (e) {} + // MEASURED, never asserted: Q is "resident" only when the engine has really uploaded weights to the GPU + // (a live signal — gpuBytes/dims), not a hardcoded true. Before load it is false, and stays false. + try { f.resident = !!(engine && (engine.gpuBytes || engine.dims)); } catch (e) { f.resident = false; } + // DERIVED from real signals — runs-on-device when actually resident, or at least GPU-capable before load. + f.runsOnDevice = !!(f.resident || f.gpu); + // "no server / no egress" is a property of the LOCAL engine, so we assert it ONLY when a real local engine or + // GPU is visible. Decode is in-browser (egress none); the one network touch is the weights fetch AT LOAD, so we + // say that plainly instead of a flat "nothing ever touches the network". + if (f.resident || f.gpu) { f.server = false; f.egress = f.resident ? "none at inference (weights fetched once at load)" : "none at inference"; } + // Provenance — where Q was loaded FROM (the thing the operator cares about): the page origin + the weights host. + // Omitted when not knowable. This is what lets Q answer "where did you come from" truthfully. + try { if (typeof location !== "undefined" && location.origin && location.origin !== "null") f.loadedFrom = location.origin; } catch (e) {} + try { if (model && model.kappaUrl) { const u = new URL(model.kappaUrl, (typeof location !== "undefined" ? location.href : "https://local/")); f.weightsFrom = u.host || u.protocol.replace(":", ""); } } catch (e) {} + // optional live system health — present only when the OS shell exposes it (absent in standalone apps). + try { const h = (typeof window !== "undefined") && window.HoloSysHealth && window.HoloSysHealth.summary && window.HoloSysHealth.summary(); if (h) f.health = h; } catch (e) {} + try { if (!f.health && typeof window !== "undefined" && window.Q && window.Q.health) { const q = window.Q.health(); if (q) f.health = q; } } catch (e) {} + return f; +} + +// The grounded SYSTEM persona every Q surface leads with. Warm + honest + specific + anti-confabulation, +// composed from the live facts so Q states ONLY what is true of THIS instance. Kept concise — a small model +// follows a short, sharp system turn far better than a long one. +export function selfPersona(opts = {}) { + const f = selfFacts(opts); + const s = []; + s.push(`You are Q — a private AI that runs on the user's own device, in their web browser${f.gpu ? ", on their GPU" : ""}.`); + if (f.model) s.push(`You are the ${f.model} model${f.quant ? ` (${f.quant})` : ""}${f.resident ? ", resident locally right now" : ""}${f.kappa ? "; your weights are content-addressed and verified by re-derivation, so no host has to be trusted" : ""}.`); + if (f.server === false) s.push(`You decode locally — there is no inference server and no cloud, and nothing the user types ever leaves their device.${f.resident ? " Your weights were fetched once at load and verified; after that, inference is fully on-device." : ""}`); + s.push("You are warm, concise, honest, and genuinely present. You seek clarity and truth; you say less, better; you never overwhelm."); + s.push("If asked what or where you are, answer truthfully from the above: you run locally, on their device. Never claim to run on a server, the cloud, OpenAI, ChatGPT, GPT-4, Gemini, or AWS — those are false. If you don't know something, say so plainly rather than inventing it."); + return s.join(" "); +} + +// A short, human, first-person self-summary (for a proactive greeting or a direct "who are you"). Grounded. +export function selfIntro(opts = {}) { + const f = selfFacts(opts); + const bits = [`I'm Q — ${f.resident ? "running right here on your device" : "running on your device"}${f.gpu ? ", on your GPU" : ""}${f.model ? ` (the ${f.model} model)` : ""}.`]; + if (f.server === false) bits.push(`No server, no cloud; nothing you say leaves this machine${f.kappa ? ", and my weights are verified by re-derivation" : ""}.`); + else if (f.kappa) bits.push("My weights are verified by re-derivation."); + bits.push("What's on your mind?"); + return bits.join(" "); +} + +export default { selfFacts, selfPersona, selfIntro }; diff --git a/apps/q/core/semantic.js b/apps/q/core/semantic.js new file mode 100644 index 0000000000000000000000000000000000000000..d25b3c6009a4fc7acccc02c16bc3fb595a6d8c3b --- /dev/null +++ b/apps/q/core/semantic.js @@ -0,0 +1,98 @@ +// core/semantic.js — the SEMANTIC SKIN (gap C2): every κ-object the substrate produces carries an +// open-semantic-web type, so humans, agents, and W3C validators all read it the same way. Closes the +// weakest audit axis (κ names objects, but few declared a W3C @type). Two W3C standards, both native: +// • JSON-LD / schema.org — WHAT a thing is (a skill is a schema:HowTo; a file is a schema:DigitalDocument) +// • PROV-O — HOW it came to be (the κ-chain becomes prov:wasRevisionOf links) +// Pure, dependency-free, browser+node. The result round-trips through any JSON-LD/RDF tool. + +export const HOLO_CONTEXT = { + schema: "https://schema.org/", + prov: "http://www.w3.org/ns/prov#", + holo: "https://hologram.foundation/ns/", + "@vocab": "https://schema.org/", +}; + +// schema.org type for a substrate object kind (the canonical mapping, L2 — fixed at the ingest boundary) +const TYPE_FOR = { + skill: "HowTo", // a reusable procedure = schema:HowTo (steps + when-to-use) + file: "DigitalDocument", + app: "SoftwareApplication", + model: "SoftwareSourceCode", + conversation: "Conversation", + receipt: "CreativeWork", // a sealed work record: schema:CreativeWork that is also a prov:Entity +}; + +// wrap any object as a typed, content-addressed JSON-LD node. `kappa` is its did:holo (the @id — +// content IS the identity, Law L1). `prov` is the κ-chain → prov:wasRevisionOf links (PROV-O). +export function asLinkedData({ kind, kappa, props = {}, prov = [] }) { + const t = TYPE_FOR[kind] || "Thing"; + const node = { + "@context": HOLO_CONTEXT, + "@id": kappa, // content-derived identity (no location — Law L1) + "@type": Array.isArray(t) ? t : ["schema:" + t, "prov:Entity"], // schema kind + PROV entity + ...props, + }; + if (prov.length >= 2) { // the version chain → PROV-O revision links + const cur = prov[prov.length - 1], parent = prov[prov.length - 2]; + node["prov:wasRevisionOf"] = { "@id": parent.kappa }; + node["holo:version"] = cur.v; + } + node["prov:wasDerivedFrom"] = prov.length ? prov.map((p) => ({ "@id": p.kappa })) : undefined; + return node; +} + +// a skill → schema:HowTo (steps become schema:HowToStep), with its PROV-O revision chain. +export function skillAsHowTo({ name, description, instructions, kappa, prov = [] }) { + const steps = String(instructions || "").split("\n").map((s) => s.trim()).filter(Boolean) + .map((text, i) => ({ "@type": "schema:HowToStep", "schema:position": i + 1, "schema:text": text.replace(/^\d+[.)]\s*/, "") })); + return asLinkedData({ kind: "skill", kappa, prov, props: { "schema:name": name, "schema:description": description, "schema:step": steps } }); +} + +// extension → IANA media type (schema:encodingFormat). Small, common set; default text/plain. +const MEDIA = { html: "text/html", htm: "text/html", js: "text/javascript", mjs: "text/javascript", json: "application/json", jsonld: "application/ld+json", css: "text/css", md: "text/markdown", txt: "text/plain", py: "text/x-python", svg: "image/svg+xml", wasm: "application/wasm", gz: "application/gzip" }; +const mediaOf = (name) => MEDIA[String(name).toLowerCase().split(".").pop()] || "text/plain"; + +// a workspace file → schema:DigitalDocument (name + media type + byte size), content-addressed (L1). +export function fileAsDocument({ path, kappa, bytes = 0, mediaType, prov = [] }) { + const name = String(path).split("/").filter(Boolean).pop() || String(path); + return asLinkedData({ kind: "file", kappa, prov, props: { + "schema:name": name, "schema:identifier": kappa, + "schema:encodingFormat": mediaType || mediaOf(name), "schema:contentSize": bytes } }); +} + +// a built app → schema:SoftwareApplication (a self-contained WebGPU/Web app object). +export function appAsSoftware({ name, kappa, bytes = 0, prov = [] }) { + return asLinkedData({ kind: "app", kappa, prov, props: { + "schema:name": String(name).replace(/\.html$/i, ""), "schema:identifier": kappa, + "schema:applicationCategory": "WebApplication", "schema:operatingSystem": "Any (WebGPU/Web)", + "schema:fileSize": bytes } }); +} + +// a loaded model → schema:SoftwareSourceCode (the κ-object that runs on the GPU). +export function modelAsSource({ name, kappa, family, params, format, prov = [] }) { + return asLinkedData({ kind: "model", kappa, prov, props: { + "schema:name": name, "schema:identifier": kappa, "schema:programmingLanguage": "WGSL/WebGPU", + "holo:family": family, "holo:parameters": params, "holo:format": format } }); +} + +// one dispatcher so any producer can type any κ-object: linkedDataFor("app", {...}) etc. +export function linkedDataFor(kind, props = {}) { + switch (kind) { + case "skill": return skillAsHowTo(props); + case "file": return fileAsDocument(props); + case "app": return appAsSoftware(props); + case "model": return modelAsSource(props); + default: return asLinkedData({ kind, kappa: props.kappa, prov: props.prov || [], props: props.props || props }); + } +} + +// VERIFY (the gate): an object is semantically valid iff it has @context, an @id (κ), and a @type +// carrying both a schema.org kind and PROV-O lineage. Returns { ok, types, hasId, hasContext }. +export function verifySemantic(node) { + const types = [].concat(node && node["@type"] || []); + const hasSchema = types.some((t) => /^schema:|^https:\/\/schema\.org\//.test(t)); + const hasProv = types.some((t) => /^prov:/.test(t)) || !!node["prov:wasDerivedFrom"]; + const hasId = typeof node?.["@id"] === "string" && node["@id"].startsWith("did:holo:"); + const hasContext = !!node?.["@context"]; + return { ok: hasSchema && hasId && hasContext, hasSchema, hasProv, hasId, hasContext, types }; +} diff --git a/apps/q/forge/gguf-forge-iq-dequant.mjs b/apps/q/forge/gguf-forge-iq-dequant.mjs new file mode 100644 index 0000000000000000000000000000000000000000..cfe6a9ec7cfc1d9ec2f042586e4808f7e5980857 --- /dev/null +++ b/apps/q/forge/gguf-forge-iq-dequant.mjs @@ -0,0 +1,260 @@ +// IQ-quant dequantization, transcribed line-for-line from ggml-quants.c +// (llama.cpp b7248): dequantize_row_iq2_xxs:3077 iq2_xs:3105 iq2_s:3132 +// iq3_xxs:3164 iq3_s:3196 iq1_s:3239 iq1_m:3264 iq4_nl:3314 iq4_xs:3332. +// Block layouts: ggml-common.h:485-563. +// +// ONE source of truth, two behaviours: makeIQ(fr, f16ToF32) binds the rounding +// function. The Tier-A oracle passes Math.fround (binary32-exact, witnessed +// bit-for-bit vs ggml to_float in gguf-forge-iq.test.mjs); the runtime passes +// identity (float64, fine for the GPU engine which re-quantizes anyway). +// +// Codebook grids are flattened little-endian byte runs (gguf-forge-iq-grids.mjs): +// an entry `idx` of stride S occupies bytes [idx*S .. idx*S+S). iq2*/iq3* read +// uint8, iq1s_grid / kvalues_iq4nl read int8. + +import { + iq2xxs_grid, iq2xs_grid, iq2s_grid, iq3xxs_grid, iq3s_grid, iq1s_grid, + ksigns_iq2xs, kmask_iq2xs, kvalues_iq4nl, +} from "./gguf-forge-iq-grids.mjs"; + +const QK_K = 256; +const IQ1S_DELTA = 0.125, IQ1M_DELTA = 0.125; +const s8 = (b) => (b << 24) >> 24; // uint8 -> int8 + +export function makeIQ(fr, f16ToF32) { + const sign1 = (signs, j) => (signs & kmask_iq2xs[j] ? -1 : 1); + + // dequantize_row_iq2_xxs (:3077). Block 66 B: d:f16 qs:uint16[32]. + function dequantIQ2XXS(raw, elements) { + const out = new Float32Array(elements); + const dv = new DataView(raw.buffer, raw.byteOffset, raw.byteLength); + const nb = elements / QK_K; let o = 0; + for (let i = 0; i < nb; i++) { + const bp = i * 66, qs = bp + 2; + const d = f16ToF32(dv.getUint16(bp, true)); + for (let ib32 = 0; ib32 < QK_K / 32; ++ib32) { + const a0 = dv.getUint32(qs + 8 * ib32, true), a1 = dv.getUint32(qs + 8 * ib32 + 4, true); + const db = fr(fr(d * fr(0.5 + (a1 >>> 28))) * 0.25); + for (let l = 0; l < 4; ++l) { + const idx = (a0 >>> (8 * l)) & 0xff; + const signs = ksigns_iq2xs[(a1 >>> (7 * l)) & 127]; + for (let j = 0; j < 8; ++j) out[o++] = sign1(signs, j) * fr(db * iq2xxs_grid[idx * 8 + j]); + } + } + } + return out; + } + + // dequantize_row_iq2_xs (:3105). Block 74 B: d:f16 qs:uint16[32] scales:uint8[8]. + function dequantIQ2XS(raw, elements) { + const out = new Float32Array(elements); + const dv = new DataView(raw.buffer, raw.byteOffset, raw.byteLength); + const nb = elements / QK_K; let o = 0; + for (let i = 0; i < nb; i++) { + const bp = i * 74, qs = bp + 2, scB = bp + 66; + const d = f16ToF32(dv.getUint16(bp, true)); + for (let ib32 = 0; ib32 < QK_K / 32; ++ib32) { + const db = [fr(fr(d * fr(0.5 + (raw[scB + ib32] & 0xf))) * 0.25), + fr(fr(d * fr(0.5 + (raw[scB + ib32] >> 4))) * 0.25)]; + for (let l = 0; l < 4; ++l) { + const q = dv.getUint16(qs + (4 * ib32 + l) * 2, true); + const idx = q & 511, signs = ksigns_iq2xs[q >> 9], dl = db[l >> 1]; + for (let j = 0; j < 8; ++j) out[o++] = sign1(signs, j) * fr(dl * iq2xs_grid[idx * 8 + j]); + } + } + } + return out; + } + + // dequantize_row_iq2_s (:3132). Block 82 B: d:f16 qs:uint8[64] qh:uint8[8] scales:uint8[8]. + // signs = qs + 32 (second half of qs); idx = qs[l] | ((qh[ib32]<<(8-2l)) & 0x300). + function dequantIQ2S(raw, elements) { + const out = new Float32Array(elements); + const dv = new DataView(raw.buffer, raw.byteOffset, raw.byteLength); + const nb = elements / QK_K; let o = 0; + for (let i = 0; i < nb; i++) { + const bp = i * 82, qhB = bp + 66, scB = bp + 74; + const d = f16ToF32(dv.getUint16(bp, true)); + let qs = bp + 2, signs = bp + 2 + 32; + for (let ib32 = 0; ib32 < QK_K / 32; ++ib32) { + const db = [fr(fr(d * fr(0.5 + (raw[scB + ib32] & 0xf))) * 0.25), + fr(fr(d * fr(0.5 + (raw[scB + ib32] >> 4))) * 0.25)]; + for (let l = 0; l < 4; ++l) { + const dl = db[l >> 1]; + const idx = raw[qs + l] | (((raw[qhB + ib32] << (8 - 2 * l)) & 0x300)); + for (let j = 0; j < 8; ++j) out[o++] = sign1(raw[signs + l], j) * fr(dl * iq2s_grid[idx * 8 + j]); + } + qs += 4; signs += 4; + } + } + return out; + } + + // dequantize_row_iq3_xxs (:3164). Block 98 B: d:f16 qs:uint8[96]. + // qs[0..63]=grid idx; scales_and_signs = qs+64 (8 uint32). db scale uses *0.5. + function dequantIQ3XXS(raw, elements) { + const out = new Float32Array(elements); + const dv = new DataView(raw.buffer, raw.byteOffset, raw.byteLength); + const nb = elements / QK_K; let o = 0; + for (let i = 0; i < nb; i++) { + const bp = i * 98, ss = bp + 2 + 64; + const d = f16ToF32(dv.getUint16(bp, true)); + let qs = bp + 2; + for (let ib32 = 0; ib32 < QK_K / 32; ++ib32) { + const a32 = dv.getUint32(ss + 4 * ib32, true); + const db = fr(fr(d * fr(0.5 + (a32 >>> 28))) * 0.5); + for (let l = 0; l < 4; ++l) { + const signs = ksigns_iq2xs[(a32 >>> (7 * l)) & 127]; + const g1 = raw[qs + 2 * l] * 4, g2 = raw[qs + 2 * l + 1] * 4; + for (let j = 0; j < 4; ++j) { + out[o + j + 0] = sign1(signs, j + 0) * fr(db * iq3xxs_grid[g1 + j]); + out[o + j + 4] = sign1(signs, j + 4) * fr(db * iq3xxs_grid[g2 + j]); + } + o += 8; + } + qs += 8; + } + } + return out; + } + + // dequantize_row_iq3_s (:3196). Block 110 B: d:f16 qs:uint8[64] qh:uint8[8] signs:uint8[32] scales:uint8[4]. + function dequantIQ3S(raw, elements) { + const out = new Float32Array(elements); + const dv = new DataView(raw.buffer, raw.byteOffset, raw.byteLength); + const nb = elements / QK_K; let o = 0; + for (let i = 0; i < nb; i++) { + const bp = i * 110, scB = bp + 106; + const d = f16ToF32(dv.getUint16(bp, true)); + let qs = bp + 2, qh = bp + 66, signs = bp + 74; + for (let ib32 = 0; ib32 < QK_K / 32; ib32 += 2) { + const db1 = fr(d * (1 + 2 * (raw[scB + (ib32 >> 1)] & 0xf))); + const db2 = fr(d * (1 + 2 * (raw[scB + (ib32 >> 1)] >> 4))); + for (let l = 0; l < 4; ++l) { + const g1 = (raw[qs + 2 * l] | ((raw[qh] << (8 - 2 * l)) & 256)) * 4; + const g2 = (raw[qs + 2 * l + 1] | ((raw[qh] << (7 - 2 * l)) & 256)) * 4; + for (let j = 0; j < 4; ++j) { + out[o + j + 0] = sign1(raw[signs + l], j + 0) * fr(db1 * iq3s_grid[g1 + j]); + out[o + j + 4] = sign1(raw[signs + l], j + 4) * fr(db1 * iq3s_grid[g2 + j]); + } + o += 8; + } + qs += 8; signs += 4; + for (let l = 0; l < 4; ++l) { + const g1 = (raw[qs + 2 * l] | ((raw[qh + 1] << (8 - 2 * l)) & 256)) * 4; + const g2 = (raw[qs + 2 * l + 1] | ((raw[qh + 1] << (7 - 2 * l)) & 256)) * 4; + for (let j = 0; j < 4; ++j) { + out[o + j + 0] = sign1(raw[signs + l], j + 0) * fr(db2 * iq3s_grid[g1 + j]); + out[o + j + 4] = sign1(raw[signs + l], j + 4) * fr(db2 * iq3s_grid[g2 + j]); + } + o += 8; + } + qh += 2; qs += 8; signs += 4; + } + } + return out; + } + + // dequantize_row_iq1_s (:3239). Block 50 B: d:f16 qs:uint8[32] qh:uint16[8]. grid int8. + function dequantIQ1S(raw, elements) { + const out = new Float32Array(elements); + const dv = new DataView(raw.buffer, raw.byteOffset, raw.byteLength); + const nb = elements / QK_K; let o = 0; + for (let i = 0; i < nb; i++) { + const bp = i * 50, qhB = bp + 34; + const d = f16ToF32(dv.getUint16(bp, true)); + let qs = bp + 2; + for (let ib = 0; ib < QK_K / 32; ++ib) { + const qh = dv.getUint16(qhB + 2 * ib, true); + const dl = fr(d * (2 * ((qh >> 12) & 7) + 1)); + const delta = (qh & 0x8000) ? -IQ1S_DELTA : IQ1S_DELTA; + for (let l = 0; l < 4; ++l) { + const g = (raw[qs + l] | (((qh >> (3 * l)) & 7) << 8)) * 8; + for (let j = 0; j < 8; ++j) out[o++] = fr(dl * fr(s8(iq1s_grid[g + j]) + delta)); + } + qs += 4; + } + } + return out; + } + + // dequantize_row_iq1_m (:3264). Block 56 B: qs:uint8[32] qh:uint8[16] scales:uint8[8]. + // No d field — f16 scale is woven from the four scale uint16s. grid int8. + function dequantIQ1M(raw, elements) { + const out = new Float32Array(elements); + const dv = new DataView(raw.buffer, raw.byteOffset, raw.byteLength); + const nb = elements / QK_K; let o = 0; + const delta = new Float32Array(4), idx = new Uint16Array(4); + for (let i = 0; i < nb; i++) { + const bp = i * 56, scB = bp + 48; + const sc = [dv.getUint16(scB, true), dv.getUint16(scB + 2, true), dv.getUint16(scB + 4, true), dv.getUint16(scB + 6, true)]; + const u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000); + const d = f16ToF32(u16); + let qs = bp, qh = bp + 32; + for (let ib = 0; ib < QK_K / 32; ++ib) { + const dl1 = fr(d * (2 * ((sc[ib >> 1] >> (6 * (ib % 2) + 0)) & 0x7) + 1)); + const dl2 = fr(d * (2 * ((sc[ib >> 1] >> (6 * (ib % 2) + 3)) & 0x7) + 1)); + idx[0] = raw[qs + 0] | ((raw[qh + 0] << 8) & 0x700); + idx[1] = raw[qs + 1] | ((raw[qh + 0] << 4) & 0x700); + idx[2] = raw[qs + 2] | ((raw[qh + 1] << 8) & 0x700); + idx[3] = raw[qs + 3] | ((raw[qh + 1] << 4) & 0x700); + delta[0] = raw[qh + 0] & 0x08 ? -IQ1M_DELTA : IQ1M_DELTA; + delta[1] = raw[qh + 0] & 0x80 ? -IQ1M_DELTA : IQ1M_DELTA; + delta[2] = raw[qh + 1] & 0x08 ? -IQ1M_DELTA : IQ1M_DELTA; + delta[3] = raw[qh + 1] & 0x80 ? -IQ1M_DELTA : IQ1M_DELTA; + for (let l = 0; l < 2; ++l) { + const g = idx[l] * 8; + for (let j = 0; j < 8; ++j) out[o++] = fr(dl1 * fr(s8(iq1s_grid[g + j]) + delta[l])); + } + for (let l = 2; l < 4; ++l) { + const g = idx[l] * 8; + for (let j = 0; j < 8; ++j) out[o++] = fr(dl2 * fr(s8(iq1s_grid[g + j]) + delta[l])); + } + qs += 4; qh += 2; + } + } + return out; + } + + // dequantize_row_iq4_nl (:3314). Block 18 B / 32 elems: d:f16 qs:uint8[16]. kvalues int8. + function dequantIQ4NL(raw, elements) { + const out = new Float32Array(elements); + const dv = new DataView(raw.buffer, raw.byteOffset, raw.byteLength); + const nb = elements / 32; let o = 0; + for (let i = 0; i < nb; i++) { + const bp = i * 18, qs = bp + 2; + const d = f16ToF32(dv.getUint16(bp, true)); + for (let j = 0; j < 16; ++j) { + out[o + j] = fr(d * s8(kvalues_iq4nl[raw[qs + j] & 0xf])); + out[o + 16 + j] = fr(d * s8(kvalues_iq4nl[raw[qs + j] >> 4])); + } + o += 32; + } + return out; + } + + // dequantize_row_iq4_xs (:3332). Block 136 B: d:f16 scales_h:uint16 scales_l:uint8[4] qs:uint8[128]. + function dequantIQ4XS(raw, elements) { + const out = new Float32Array(elements); + const dv = new DataView(raw.buffer, raw.byteOffset, raw.byteLength); + const nb = elements / QK_K; let o = 0; + for (let i = 0; i < nb; i++) { + const bp = i * 136, slB = bp + 4; + const d = f16ToF32(dv.getUint16(bp, true)); + const sh = dv.getUint16(bp + 2, true); + let qs = bp + 8; + for (let ib = 0; ib < QK_K / 32; ++ib) { + const ls = ((raw[slB + (ib >> 1)] >> (4 * (ib % 2))) & 0xf) | (((sh >> (2 * ib)) & 3) << 4); + const dl = fr(d * (ls - 32)); + for (let j = 0; j < 16; ++j) { + out[o + j] = fr(dl * s8(kvalues_iq4nl[raw[qs + j] & 0xf])); + out[o + 16 + j] = fr(dl * s8(kvalues_iq4nl[raw[qs + j] >> 4])); + } + o += 32; qs += 16; + } + } + return out; + } + + return { dequantIQ2XXS, dequantIQ2XS, dequantIQ2S, dequantIQ3XXS, dequantIQ3S, dequantIQ1S, dequantIQ1M, dequantIQ4NL, dequantIQ4XS }; +} diff --git a/apps/q/forge/gguf-forge-iq-grids.mjs b/apps/q/forge/gguf-forge-iq-grids.mjs new file mode 100644 index 0000000000000000000000000000000000000000..43882c703ab5918b09f8432db5499feaa68931bd --- /dev/null +++ b/apps/q/forge/gguf-forge-iq-grids.mjs @@ -0,0 +1,14 @@ +// AUTO-GENERATED by gen-iq-grids.mjs from ggml-common.h — do not edit by hand. +// IQ-quant codebook grids + helper tables, flattened to little-endian byte runs. +// A grid entry `idx` of stride S occupies bytes [idx*S .. idx*S+S); read uint8 +// for iq2*/iq3* grids, int8 (`(b<<24)>>24`) for iq1s_grid / kvalues_iq4nl. +const D = (s) => Uint8Array.from(atob(s), (c) => c.charCodeAt(0)); +export const iq2xxs_grid = /* 2048B */ D("CAgICAgICAgrCAgICAgICBkZCAgICAgICCsICAgICAgrKwgICAgICBkIGQgICAgICBkZCAgICAgICCsICAgICCsIKwgICAgICCsrCAgICAgrKysICAgICBkICBkICAgICBkIGQgICAgICBkZCAgICAgrGRkICAgIGQgrGQgICAgIGSsZCAgICAgICCsICAgIKwgIKwgICAgrKwgrCAgICCsIKysICAgIGQgICBkICAgIGQgIGQgICAgIGQgZCAgIGRkZCBkICAgICAgZGQgICAgZCCsZCAgICCsZKxkICAgICAgIKwgICCsICAgrCAgIKwgrCCsICAgrCAgrKwgICBkICAgIGQgICBkICAgZCAgICBkICBkICBkIKwgIGQgICBkrCAgZCAgICAgZCBkICCsICBkIGQgICCsIGQgZCAgICCsZCBkICBkICCsIGQgICBkIKwgZCAgICBkrCBkICAgZKysIGQgICAgICBkZCAgrCAgIGRkICAgrCAgZGQgICAgrCBkZCAgrGQgZGRkICBkrKxkZGQgICAgIKxkZCAgZCBkrGRkICBkrCAgrGQgICAgZCCsZCAgICAgZKxkICAgZCCsrGQgICBkrKysZCAgICAgICCsICBkZCAgIKwgICCsICAgrCAgIGRkICCsICAgrKwgIKwgIGQgIGQgrCAgIGQgZCCsICAgIGRkIKwgIKwgZGQgrCAgIKwgrCCsICAgZCAgZKwgICAgIGRkrCAgrCAgIKysICAgZGQgrKwgIGQgICAgIGQgIGQgICAgZCAgIGQgICBkIGQgrCAgIGQgICAgZCAgZCAgIKxkICBkICBkIKwgIGQgICBkrCAgZCBkZGSsICBkICAgICBkIGQgIKwgIGQgZCAgIKwgZCBkICAgZGRkIGQgrKxkZGQgZCAgICCsZCBkICBkrCCsIGQgZGQgZKwgZCAgICAgIGRkICCsICAgZGQgICCsICBkZCBkZKwgIGRkIGSsIGQgZGQgICAgrCBkZCAgrGQgZGRkIKwgrGRkZGQgICAgIKxkZCCsZGQgrGRkIGQgICAgrGQgIGQgICCsZCAgIGQgIKxkICAgIGQgrGQgZCAgrCCsZCAgICAgZKxkIGRkICBkrGQgICCsrGSsZCBkIGRkrKxkICAgICAgIKwgrCAgICAgrCCsrCAgICCsICBkIGQgIKwgZCCsZCAgrCAgICCsICCsIKwgIKwgIKwgZKysIGQgrCAgrCBkZCCsICAgICCsIKwgrCAgIKwgrCBkICAgIGSsICBkICAgZKwgICBkICBkrCAgICBkIGSsIKxkZGQgZKwgICAgIGRkrCBkICBkZGSsICBkrGRkZKwgICBkrKxkrCAgrCAgIKysICAgrCAgrKwgIGRkrCCsrCAgZCBkrKysIGQgICAgICBkIGQgICAgIGQgIGQgICAgZCCsZCAgICBkZCCsICAgIGQgZKwgICAgZCAgIGQgICBkIKwgZCAgIGSsZGRkICAgZCAgrGQgICBkZCAgrCAgIGQgZCCsICAgZCAgZKwgICBkICAgIGQgIGQgIKwgZCAgZGQgrGRkICBkICAgrGQgIGRkZCCsZCAgZGQgICCsICBkICBkIKwgIGQgrCBkrCAgZKxkZGSsICBkIKysZKwgIGQgICAgIGQgZCCsICAgZCBkICCsICBkIGQgICCsIGQgZGSsZKwgZCBkrCBkIGRkIGQgZKwgZGQgZCAgICCsZCBkZCAgICCsIGQgZCAgIKwgZCAgZCAgrCBkICAgZCCsIGRkZCBkIKwgZCAgICBkrCBkIKxkZGSsIGRkIKxkZKwgZKwgIKxkrCBkZGQgZKysIGQgIGSsrKwgZCAgICAgIGRkIKwgICAgZGRkIGQgICBkZGSsZCAgIGRkICCsICAgZGQgICCsICBkZCCsIKwgIGRkIGQgIGQgZGSsICBkZCBkZCBkrKxkIGRkZCBkrKwgZGQgIGSsIGRkZKwgZKwgZGRkrKwgIGRkZGRkICAgrGRkZCBkZGSsZGRkICAgICCsZGRkIGQgIKxkZGSsZCAgrGRkIGSsZCCsZGQgICBkZKxkZCCsICCsrGRkIGQgICAgrGQgIGQgICCsZCAgIGQgIKxkIKysZCAgrGQgICAgZCCsZGRkZGRkIKxkIKxkIKwgrGQgIKxkrCCsZCAgICAgZKxkZGQgICBkrGQgIGQgZGSsZKwgZCBkZKxkIGQgrGRkrGSsICBkIKysZCAgICAgICCsrCAgICAgIKysrCAgICAgrGQgIGQgICCsrCAgrCAgIKwgZCAgZCAgrCCsZCBkICCsICAgZGQgIKxkIGQgrCAgrGQgICAgZCCsIGQgICBkIKwgIGQgIGQgrGRkZCAgZCCsICAgZCBkIKwgIKxkIGQgrCAgICBkZCCsrGQgZGRkIKwgZGSsZGQgrGSsICCsZCCsICAgZKxkIKwgIKxkrGQgrKwgICAgrCCsIGQgIGSsIKxkIGQgrKwgrCBkICAgIGSsICBkICAgZKwgZKwgICBkrCAgIGQgIGSsZCCsrCAgZKysZGQgZCBkrCAgIKxkIGSsZGQgZKwgZKwgICAgIGRkrKwgrCAgZGSsIGQgZCBkZKxkIGRkZGRkrGQgIKwgrGSsICCsIGSsZKysICAgICCsrCAgZGQgIKysZGQgrCAgrKxkrCAgZCCsrCAgICCsIKysIKxkICBkrKwgIGRkIKysrCBkICBkrKys="); +export const iq2xs_grid = /* 4096B */ D("CAgICAgICAgrCAgICAgICBkZCAgICAgICCsICAgICAgrKwgICAgICBkIGQgICAgICBkZCAgICAgrGRkICAgICBkrGQgICAgICAgrCAgICAgrCCsICAgICBkZKwgICAgICCsrCAgICAgZCAgZCAgICAgZCBkICAgIKxkIGQgICAgZKwgZCAgICAgIGRkICAgIKwgZGQgICAgZGRkZCAgICAgrGRkICAgIGQgrGQgICAgIGSsZCAgICAgICCsICAgIKwgIKwgICAgZGQgrCAgICAgrCCsICAgIGQgZKwgICAgIGRkrCAgICBkrGSsICAgICAgrKwgICAgZCAgIGQgICAgZCAgZCAgIKxkICBkICAgZKwgIGQgICAgIGQgZCAgIKwgZCBkICAgZGRkIGQgICAgrGQgZCAgIKysZCBkICAgZCCsIGQgICAgZKwgZCAgICAgIGRkICAgrCAgZGQgICBkZCBkZCAgICCsIGRkICAgZCBkZGQgICAgZGRkZCAgICAgrGRkICAgIKysZGQgICBkICCsZCAgICBkIKxkICAgICBkrGQgICAgICAgrCAgIKwgICCsICAgZGQgIKwgICAgrCAgrCAgIGQgZCCsICAgIGRkIKwgICAgIKwgrCAgIGQgIGSsICAgIGQgZKwgICAgIGRkrCAgIGRkZGSsICAgICAgrKwgICCsrCCsrCAgIGQgICAgZCAgIGQgICBkICCsZCAgIGQgIGSsICAgZCAgICBkICBkICCsIGQgIGQgIGRkZCAgZCAgIKxkICBkICBkIKwgIGQgICBkrCAgZCAgICAgZCBkICCsICBkIGQgIGRkIGQgZCAgIKwgZCBkICBkIGRkIGQgICBkZGQgZCAgrGRkZCBkICAgIKxkIGQgIGQgIKwgZCAgIGQgrCBkICAgIGSsIGQgICAgICBkZCAgrCAgIGRkICBkZCAgZGQgICCsICBkZCAgZCBkIGRkICAgZGQgZGQgICAgrCBkZCAgZCAgZGRkICAgZCBkZGQgICAgZGRkZCAgZCCsZGRkICAgICCsZGQgIGQgICCsZCAgIGQgIKxkICAgIGQgrGQgIKxkrCCsZCAgICAgZKxkICCsICBkrGQgICBkIKysZCAgICAgICCsICCsICAgIKwgIGRkICAgrCAgIKwgICCsICCsrCAgIKwgIGQgZCAgrCAgIGRkICCsICAgIKwgIKwgIGRkrCAgrCAgZCAgZCCsICAgZCBkIKwgICAgZGQgrCAgIKxkZCCsICAgICCsIKwgICAgrKwgrCAgrKysrCCsICBkICAgZKwgICBkICBkrCAgICBkIGSsICAgICBkZKwgIGQgIKxkrCAgZKwgrGSsICAgICAgrKwgICAgrCCsrCAgIKysIKysICCsZGSsrKwgICAgrKysrCAgZCAgICAgZCAgZCAgICBkIKxkICAgIGQgZKwgICAgZCAgIGQgICBkIKwgZCAgIGQgZGRkICAgZCAgrGQgICBkIGQgrCAgIGQgIGSsICAgZCAgICBkICBkIKwgIGQgIGQgZGQgZCAgZCAgrCBkICBkIGQgZGQgIGQgIGRkZCAgZCAgIKxkICBkIKysrGQgIGQgZCAgrCAgZCAgZCCsICBkICAgZKwgIGQgICAgIGQgZCCsICAgZCBkIGRkICBkIGQgIKwgIGQgZCBkIGQgZCBkICBkZCBkIGQgICCsIGQgZCBkICBkZCBkICBkIGRkIGQgICBkZGQgZCAgICCsZCBkICBkZKxkIGQgrGRkrGQgZCBkICAgrCBkICBkICCsIGQgrGQgIKwgZCAgIGQgrCBkICAgIGSsIGQgICCsZKwgZCAgICAgIGRkIKwgICAgZGQgZGQgICBkZCAgrCAgIGRkIGQgZCAgZGQgIGRkICBkZCAgIKwgIGRkIGQgIGQgZGQgIGQgZCBkZCBkrCBkIGRkICAgZGQgZGQgIGSsZCBkZCAgICCsIGRkIGQgICBkZGQgIGQgIGRkZCAgIGQgZGRkICAgIGRkZGQgICAgIKxkZCAgZGQgrGRkIGSsIGSsZGQgZCAgICCsZCAgZCAgIKxkICAgZCAgrGQgrCBkICCsZCAgICBkIKxkICBkZGQgrGQgrGQgrCCsZCAgICAgZKxkIGRkICBkrGQgrGSsZGSsZCBkIGRkrKxkIGSsrKysrGQgICAgICAgrCCsICAgICCsIGRkICAgIKwgIKwgICAgrCCsrCAgICCsIGQgZCAgIKwgIGRkICAgrCAgIKwgICCsIGQgIGQgIKwgIGQgZCAgrCAgIGRkICCsICAgIKwgIKwgICCsrCAgrCBkICAgZCCsICBkICBkIKwgICBkIGQgrCAgICBkZCCsICCsIGRkIKwgZGSsZGQgrCAgICAgrCCsIKwgrCCsIKwgICAgrKwgrCAgrKysrCCsIGQgICAgZKwgIGQgICBkrCAgIGQgIGSsIGSsrCAgZKwgICAgZCBkrCAgICAgZGSsIGQgIGRkZKwgrCBkZGRkrCBkrGSsZGSsIGQgICCsZKwgrKxkIKxkrCCsZKysrGSsICAgICAgrKwgIKwgICCsrCCsrCAgIKysICAgrCAgrKwgZGRkZCCsrCAgrCCsIKysIKwgrKwgrKwgIKysZGSsrCAgIGSsZKysICCsICCsrKwgICCsIKysrCCsICCsrKysICCsIKysrKwgrKwgrKysrCBkICAgICAgZCBkICAgICBkrGQgICAgIGRkrCAgICAgZCAgZCAgICBkrCBkICAgIGRkZGQgICAgZCCsZCAgICBkZCCsICAgIGQgZKwgICAgZCAgIGQgICBkrCAgZCAgIGRkZCBkICAgZCCsIGQgICBkrKwgZCAgIGRkIGRkICAgZCBkZGQgICBkICCsZCAgIGRkZKxkICAgZGQgIKwgICBkIGQgrCAgIGQgIGSsICAgZCAgICBkICBkrCAgIGQgIGRkZCAgZCAgZCCsICBkICBkZCBkIGQgIGQgZGQgZCAgZCAgrCBkICBkZCAgZGQgIGQgZCBkZCAgZCAgZGRkICBkICAgrGQgIGRkZCCsZCAgZKwgrKxkICBkZCAgIKwgIGQgZCAgrCAgZCAgZCCsICBkrCBkIKwgIGRkrKwgrCAgZCAgIGSsICBkICAgICBkIGSsICAgIGQgZGRkICAgZCBkIKwgICBkIGRkIGQgIGQgZCBkZCAgZCBkZKxkICBkIGQgIKwgIGQgZGQgIGQgZCBkIGQgZCBkIGQgIGRkIGQgZCAgIKwgZCBkIGRkrCBkIGRkICAgZGQgZCBkICBkZCBkICBkIGRkIGQgZKwgZGQgZCAgIGRkZCBkrKxkrGRkIGQgICAgrGQgZKysICCsZCBkIGQgZKxkIGQgIGRkrGQgZGQgICAgrCBkIGQgICCsIGQgIGQgIKwgZCAgIGQgrCBkZGQgZCCsIGQgZGRkIKwgZKwgrGQgrCBkICAgIGSsIGRkIGQgZKwgZCBkIGRkrCBkICBkZGSsIGRkrKxkZKwgZCBkICCsrCBkICAgICAgZGSsICAgICBkZGRkICAgIGRkIKwgICAgZGRkIGQgICBkZCBkZCAgIGRkICCsICAgZGQgrKwgICBkZGQgIGQgIGRkIGQgZCAgZGQgIGRkICBkZCAgIKwgIGRkZCAgIGQgZGQgZCAgZCBkZCAgZCBkIGRkZGRkIGQgZGQgICBkZCBkZKwgIGRkIGRkICAgIKwgZGQgZCBkrCBkZKysrKysIGRkZCAgICBkZGQgZCAgIGRkZCAgZCAgZGRkZCCsICBkZGQgICBkIGRkZCAgrGQgZGRkZCAgrCBkZGRkIKysIGRkZCAgICBkZGRkIKwgIGRkZGQgICCsZGRkZCCsIKxkZGRkZCCsIKxkZGQgrKxkrGRkZGQgrKysZGRkICAgICCsZGQgZGQgIKxkZGQgIGQgrGRkICBkZCCsZGRkrGSsIKxkZKysZCBkrGRkICAgZGSsZGSsICBkZKxkZGRkIKysrGRkZCAgICAgrGQgZCAgICCsZCAgZCAgIKxkICAgZCAgrGQgZGRkICCsZKwgrGQgIKxkrGQgrCAgrGRkrKysICCsZCAgICBkIKxkIGSsIKwgrGSsrCBkrCCsZKwgZKysIKxkICAgICBkrGSsZGQgIGSsZCAgZCBkZKxkICAgZGRkrGRkZCBkZGSsZCBkrKxkZKxkZCAgICCsrGSsrKxkIKysZGRkrCBkrKxkrGQgIKysrGQgZGRkrKysZKwgrGSsrKxkICAgICAgIKysICAgICAgrGRkICAgICCsIKwgICAgIKxkIGQgICAgrCBkZCAgICCsICCsICAgIKysrKwgICAgrGQgIGQgICCsIGQgZCAgIKwgIGRkICAgrCAgIKwgICCsrCAgrCAgIKwgrKysICAgrKysrKwgICCsZCAgIGQgIKwgZCAgZCAgrKxkICBkICCsICBkIGQgIKwgICBkZCAgrGQgZGRkICCsZKxkZGQgIKwgICAgrCAgrCAgrCCsICCsICAgrKwgIKysICCsrCAgrCAgrKysICCsIKysrKwgIKxkICAgIGQgrCBkICAgZCCsICBkICBkIKysIGQgIGQgrGRkZCAgZCCsICAgZCBkIKwgIKxkIGQgrGSsIKwgZCCsICAgIGRkIKwgZCBkZGQgrGRkrKxkZCCsIKxkIKxkIKysrKxkrGQgrCAgICAgrCCsIKwgICCsIKxkZKwgIKwgrKysZGQgrCCsICAgrCCsIKysICCsIKwgrCCsrKwgrCCsrGQgIGSsIKysIKwgrKwgrCAgIKysrCCsIKwgrKysIKysZGSsrKwgrCCsrKysrCCsZCAgICAgZKwgZCAgICBkrCAgZCAgIGSsICAgZCAgZKysZGRkICBkrCBkIKwgIGSsICAgIGQgZKysIKwgZCBkrCBkrGRkIGSsrGRkZKwgZKxkrCCsrCBkrCAgICAgZGSsZGQgICBkZKwgZCBkIGRkrCAgZGQgZGSsIKxkZCBkZKxkrKwgZGRkrCAgZKxkZGSsrCBkrGRkZKxkICBkrGRkrGQgZGQgrGSsrGSsrCCsZKxkrCBkZKxkrGRkZCCsrGSsICCsZKysZKwgICAgICCsrKwgICAgIKysIKwgICAgrKysrCAgICCsrCAgrCAgIKysrKysICAgrKwgIKysICCsrGQgZGRkIKysZKxkZGQgrKysZKysZCCsrCAgICCsIKysrCAgIKwgrKwgrCAgrCCsrKysrCCsIKysICAgrKwgrKwgIKysrCCsrCAgIGQgZKysZGRkrCBkrKxkZKxkrGSsrCCsZKysZKysrKwgICCsrKwgIKwgIKysrKwgrCAgrKysIKysICCsrKwgIKysIKysrCCsrKwgrKysIGQgIGSsrKwgZCCsZKysrKxkIKxkrKysIKysIKysrKysrKwgrKysrGQgZKysrKysrKysrKysrKw=="); +export const iq2s_grid = /* 8192B */ D("CAgICAgICAgrCAgICAgICBkZCAgICAgICCsICAgICAgrKwgICAgICBkIGQgICAgICBkZCAgICAgrGRkICAgICBkrGQgICAgICAgrCAgICAgrCCsICAgICBkZKwgICAgICCsrCAgICAgZCAgZCAgICAgZCBkICAgIKxkIGQgICAgZKwgZCAgICAgIGRkICAgIKwgZGQgICAgZGRkZCAgICAgrGRkICAgIGQgrGQgICAgIGSsZCAgICCsZKxkICAgIGSsrGQgICAgICAgrCAgICCsICCsICAgIGRkIKwgICAgIKwgrCAgICBkIGSsICAgICBkZKwgICAgICCsrCAgICBkZKysICAgIKysrKwgICAgZCAgIGQgICAgZCAgZCAgIKxkICBkICAgZKwgIGQgICAgIGQgZCAgIKwgZCBkICAgZGRkIGQgICAgrGQgZCAgIGQgrCBkICAgIGSsIGQgICAgICBkZCAgIKwgIGRkICAgZGQgZGQgICAgrCBkZCAgIGQgZGRkICAgIGRkZGQgICCsZGRkZCAgIGSsZGRkICAgICCsZGQgICBkZKxkZCAgICCsrGRkICAgZCAgrGQgICAgZCCsZCAgICAgZKxkICAgrCBkrGQgICBkZGSsZCAgIGQgrKxkICAgIGSsrGQgICAgICAgrCAgIKwgICCsICAgZGQgIKwgICAgrCAgrCAgIGQgZCCsICAgIGRkIKwgICAgIKwgrCAgIKysrCCsICAgZCAgZKwgICAgZCBkrCAgIKxkIGSsICAgZKwgZKwgICAgIGRkrCAgIGRkZGSsICAgICAgrKwgICBkZCCsrCAgIKysIKysICAgIGRkrKwgICCsIKysrCAgIGQgICAgZCAgIGQgICBkICCsZCAgIGQgIGSsICAgZCAgICBkICBkICCsIGQgIGQgIGRkZCAgZCAgIKxkICBkICBkIKwgIGQgICBkrCAgZCAgrGSsICBkICBkrKwgIGQgICAgIGQgZCAgrCAgZCBkICBkZCBkIGQgICCsIGQgZCAgrKwgZCBkICBkIGRkIGQgICBkZGQgZCAgrGRkZCBkICBkrGRkIGQgICAgrGQgZCAgrCCsZCBkICBkZKxkIGQgIGQgIKwgZCAgIGQgrCBkICCsZCCsIGQgIGSsIKwgZCAgICBkrCBkICBkZGSsIGQgICCsZKwgZCAgZCCsrCBkICAgZKysIGQgICAgICBkZCAgrCAgIGRkICBkZCAgZGQgICCsICBkZCAgrKwgIGRkICBkIGQgZGQgICBkZCBkZCAgrGRkIGRkICBkrGQgZGQgICAgrCBkZCAgZGSsIGRkICAgrKwgZGQgIGQgIGRkZCAgIGQgZGRkICCsZCBkZGQgIGSsIGRkZCAgICBkZGRkICCsIGRkZGQgIGRkZGRkZCAgIKxkZGRkICBkIKxkZGQgICBkrGRkZCAgICAgrGRkICCsICCsZGQgIGRkIKxkZCAgIKwgrGRkICBkIGSsZGQgICBkZKxkZCAgICCsrGRkICBkICAgrGQgICBkICCsZCAgrGQgIKxkICBkrCAgrGQgICAgZCCsZCAgZGRkIKxkICAgICBkrGQgIGRkIGSsZCAgIKwgZKxkICBkIGRkrGQgICBkZGSsZCAgICCsZKxkICBkICCsrGQgICBkIKysZCAgICBkrKxkICAgICAgIKwgIKwgICAgrCAgZGQgICCsICAgrCAgIKwgIGQgZCAgrCAgIGRkICCsICCsZGQgIKwgIGSsZCAgrCAgICCsICCsICBkZKwgIKwgIKysrCAgrCAgZCAgZCCsICAgZCBkIKwgIKxkIGQgrCAgZKwgZCCsICAgIGRkIKwgIKwgZGQgrCAgZGRkZCCsICAgrGRkIKwgIGQgrGQgrCAgIGSsZCCsICAgICCsIKwgIGRkIKwgrCAgIGRkrCCsICCsrKysIKwgIGQgICBkrCAgIGQgIGSsICAgIGQgZKwgIKwgZCBkrCAgZGRkIGSsICAgrGQgZKwgIGQgrCBkrCAgICAgZGSsICBkZCBkZKwgICCsIGRkrCAgZCBkZGSsICAgZGRkZKwgICAgrGRkrCAgZCAgrGSsICAgIGSsZKwgICAgICCsrCAgZCBkIKysICAgZGQgrKwgIKwgrCCsrCAgIKysIKysICCsrKwgrKwgICAgZGSsrCAgZKxkrKysICBkICAgICBkICBkICAgIGQgrGQgICAgZCBkrCAgICBkICAgZCAgIGQgrCBkICAgZCBkZGQgICBkICCsZCAgIGQgZCCsICAgZCAgZKwgICBkIKxkrCAgIGQgICAgZCAgZCCsICBkICBkIGRkIGQgIGQgIKwgZCAgZCBkIGRkICBkICBkZGQgIGQgrGRkZCAgZCBkrGRkICBkICAgrGQgIGQgrCCsZCAgZCBkZKxkICBkICCsrGQgIGQgZCAgrCAgZCAgZCCsICBkIKxkIKwgIGQgICBkrCAgZCBkZGSsICBkICCsZKwgIGQgZCCsrCAgZCAgZKysICBkICAgICBkIGQgrCAgIGQgZCBkZCAgZCBkICCsICBkIGQgrKwgIGQgZCBkIGQgZCBkICBkZCBkIGQgrGRkIGQgZCBkrGQgZCBkICAgrCBkIGQgrCCsIGQgZCBkZKwgZCBkICCsrCBkIGQgZCAgZGQgZCAgZCBkZCBkIKxkIGRkIGQgZKwgZGQgZCAgIGRkZCBkIKwgZGRkIGQgZGRkZGQgZCAgrGRkZCBkIGQgrGRkIGQgIGSsZGQgZCAgICCsZCBkIKwgIKxkIGQgZGQgrGQgZCAgrCCsZCBkIGQgZKxkIGQgIGRkrGQgZCBkICAgrCBkICBkICCsIGQgZKwgIKwgZCAgIGQgrCBkIGRkZCCsIGQgZCCsIKwgZCAgZKwgrCBkICAgIGSsIGQgZGQgZKwgZCBkIGRkrCBkICBkZGSsIGQgZCAgrKwgZCAgZCCsrCBkICAgZKysIGQgICAgICBkZCCsICAgIGRkIGRkICAgZGQgIKwgICBkZCBkIGQgIGRkICBkZCAgZGQgrGRkICBkZCBkrGQgIGRkICAgrCAgZGQgZGSsICBkZCAgrKwgIGRkIGQgIGQgZGQgIGQgZCBkZCCsZCBkIGRkIGSsIGQgZGQgICBkZCBkZCCsIGRkIGRkIGRkZGQgZGQgIKxkZCBkZCBkIKxkIGRkICBkrGQgZGQgICAgrCBkZCCsICCsIGRkIGRkIKwgZGQgIKwgrCBkZCBkIGSsIGRkICBkZKwgZGQgICCsrCBkZCBkICAgZGRkICBkICBkZGQgrGQgIGRkZCBkrCAgZGRkICAgZCBkZGQgrCBkIGRkZCBkZGQgZGRkICCsZCBkZGQgZCCsIGRkZCAgZKwgZGRkICAgIGRkZGQgrCAgZGRkZCBkZCBkZGRkICCsIGRkZGQgZCBkZGRkZCAgZGRkZGRkICAgrGRkZGQgZCAgrGRkZCAgZCCsZGRkICAgZKxkZGQgICAgIKxkZCBkZCAgrGRkICCsICCsZGQgZCBkIKxkZCAgZGQgrGRkICAgrCCsZGQgZCAgZKxkZCAgZCBkrGRkICAgZGSsZGQgICAgrKxkZCCsrKysrGRkIGQgICAgrGQgIGQgICCsZCCsZCAgIKxkIGSsICAgrGQgICBkICCsZCBkZGQgIKxkICCsZCAgrGQgZCCsICCsZCAgICBkIKxkIKwgIGQgrGQgZGQgZCCsZCAgrCBkIKxkIGQgZGQgrGQgIGRkZCCsZCAgIKxkIKxkIGQgIKwgrGQgIGQgrCCsZCAgICAgZKxkIKwgICBkrGQgZGQgIGSsZCAgrCAgZKxkIGQgZCBkrGQgIGRkIGSsZCAgIKwgZKxkIGQgIGRkrGQgIGQgZGSsZCAgIGRkZKxkIGSsrGRkrGQgrCCsrGSsZCAgZCAgrKxkICAgZCCsrGQgICAgZKysZCCsZGRkrKxkICAgICAgIKwgrCAgICAgrCBkZCAgICCsICCsICAgIKwgZCBkICAgrCAgZGQgICCsIKxkZCAgIKwgZKxkICAgrCAgIKwgICCsIGRkrCAgIKwgrKysICAgrCBkICBkICCsICBkIGQgIKwgICBkZCAgrCCsIGRkICCsIGRkZGQgIKwgIGSsZCAgrCAgICCsICCsIKysIKwgIKwgIGRkrCAgrCCsrKysICCsIGQgICBkIKwgIGQgIGQgrCAgIGQgZCCsIKwgZCBkIKwgZGRkIGQgrCBkIKwgZCCsICAgIGRkIKwgrCAgZGQgrCBkZCBkZCCsIGQgZGRkIKwgIGRkZGQgrCAgIKxkZCCsIGQgIKxkIKwgIGQgrGQgrCAgIGSsZCCsICAgICCsIKwgrKwgIKwgrCCsIKwgrCCsICCsrCCsIKwgrKysIKwgrCAgZCBkrCCsICAgZGSsIKwgIKwgrKwgrCCsrCCsrCCsICCsrKysIKwgZCAgICBkrCAgZCAgIGSsIKxkICAgZKwgZKwgICBkrCAgIGQgIGSsIGRkZCAgZKwgIKxkICBkrCBkIKwgIGSsICBkrCAgZKwgICAgZCBkrCCsICBkIGSsIGRkIGQgZKwgIKwgZCBkrCBkIGRkIGSsICBkZGQgZKwgICCsZCBkrCBkICCsIGSsICBkIKwgZKwgICBkrCBkrCAgICAgZGSsIGRkICBkZKwgIKwgIGRkrCBkIGQgZGSsICBkZCBkZKwgICCsIGRkrCBkICBkZGSsICBkIGRkZKwgICBkZGRkrCCsZKxkZGSsICAgIKxkZKwgZCAgIKxkrCAgZCAgrGSsICAgZCCsZKwgICAgZKxkrCBkrGRkrGSsICAgICAgrKwgZGQgICCsrCBkIGQgIKysICBkZCAgrKwgZCAgZCCsrCAgZCBkIKysICAgZGQgrKwgrKwgrCCsrCCsrKysIKysIGQgICBkrKwgIGQgIGSsrCAgIGQgZKysIGRkZKxkrKwgrKwgIKysrCCsIKwgrKysICBkrGSsrKwgIKwgrKysrCCsrCCsrKysIGQgICAgICBkIGQgICAgIGSsZCAgICAgZGSsICAgICBkICBkICAgIGSsIGQgICAgZGRkZCAgICBkIKxkICAgIGSsrGQgICAgZGQgrCAgICBkIGSsICAgIGSsZKwgICAgZCAgIGQgICBkrCAgZCAgIGRkZCBkICAgZCCsIGQgICBkrKwgZCAgIGRkIGRkICAgZCBkZGQgICBkrGRkZCAgIGRkrGRkICAgZCAgrGQgICBkrCCsZCAgIGRkZKxkICAgZGQgIKwgICBkIGQgrCAgIGQgIGSsICAgZGRkZKwgICBkIKxkrCAgIGRkIKysICAgZCBkrKwgICBkICAgIGQgIGSsICAgZCAgZGRkICBkICBkIKwgIGQgIGRkIGQgZCAgZCBkZCBkICBkrGRkIGQgIGRkrGQgZCAgZCAgrCBkICBkrCCsIGQgIGRkZKwgZCAgZGQgIGRkICBkIGQgZGQgIGSsZCBkZCAgZGSsIGRkICBkICBkZGQgIGSsIGRkZCAgZGRkZGRkICBkIKxkZGQgIGRkIKxkZCAgZCBkrGRkICBkICAgrGQgIGSsICCsZCAgZGRkIKxkICBkIKwgrGQgIGRkIGSsZCAgZCBkZKxkICBkICCsrGQgIGRkICAgrCAgZCBkICCsICBkICBkIKwgIGSsIGQgrCAgZGRkZCCsICBkIKxkIKwgIGQgZKwgrCAgZCAgIGSsICBkZGQgZKwgIGQgrCBkrCAgZGQgZGSsICBkIGRkZKwgIGQgIKxkrCAgZGQgIKysICBkIGQgrKwgIGQgICAgIGQgZKwgICAgZCBkZGQgICBkIGQgrCAgIGQgZKysICAgZCBkZCBkICBkIGQgZGQgIGQgZKxkZCAgZCBkZKxkICBkIGQgIKwgIGQgZKwgrCAgZCBkZGSsICBkIGQgrKwgIGQgZGQgIGQgZCBkIGQgZCBkIGSsZCBkIGQgZGSsIGQgZCBkICBkZCBkIGSsIGRkIGQgZGRkZGQgZCBkIKxkZCBkIGRkIKxkIGQgZCBkrGQgZCBkICAgrCBkIGSsICCsIGQgZGRkIKwgZCBkIKwgrCBkIGRkIGSsIGQgZCBkZKwgZCBkICCsrCBkIGRkICAgZGQgZCBkICBkZCBkrGQgIGRkIGRkrCAgZGQgZCAgZCBkZCBkrCBkIGRkIGRkZGQgZGQgZCCsZCBkZCBkZCCsIGRkIGQgZKwgZGQgZCAgIGRkZCBkrCAgZGRkIGRkZCBkZGQgZCCsIGRkZCBkZCBkZGRkIGQgZGRkZGQgZCAgrGRkZCBkrKysZGRkIGRkICCsZGQgZCBkIKxkZCBkICBkrGRkIGQgICAgrGQgZKwgICCsZCBkZGQgIKxkIGQgrCAgrGQgZGQgZCCsZCBkIGRkIKxkIGQgIKwgrGQgZGQgIGSsZCBkIGQgZKxkIGQgIGRkrGQgZCAgIKysZCBkZGSsrKxkIGRkICAgIKwgZCBkICAgrCBkZKwgICCsIGQgIGQgIKwgZKwgZCAgrCBkZGRkICCsIGQgrGQgIKwgZGQgrCAgrCBkIGSsICCsIGQgICBkIKwgZKwgIGQgrCBkZGQgZCCsIGQgrCBkIKwgZGQgZGQgrCBkIGRkZCCsIGQgIKxkIKwgZCBkIKwgrCBkICBkrCCsIGQgICAgZKwgZKwgICBkrCBkZGQgIGSsIGQgrCAgZKwgZGQgZCBkrCBkIGRkIGSsIGQgIKwgZKwgZGQgIGRkrCBkIGQgZGSsIGQgIGRkZKwgZCAgIKxkrCBkrGRkrGSsIGRkICAgrKwgZCBkICCsrCBkICBkIKysIGQgICBkrKwgZCAgICAgIGRkrCAgICAgZGRkZCAgICBkZCCsICAgIGRkZCBkICAgZGQgZGQgICBkZKxkZCAgIGRkZKxkICAgZGQgIKwgICBkZKwgrCAgIGRkZGSsICAgZGQgrKwgICBkZGQgIGQgIGRkIGQgZCAgZGSsZCBkICBkZGSsIGQgIGRkICBkZCAgZGSsIGRkICBkZGRkZGQgIGRkIKxkZCAgZGRkIKxkICBkZCBkrGQgIGRkICAgrCAgZGSsICCsICBkZGRkIKwgIGRkIKwgrCAgZGRkIGSsICBkZCBkZKwgIGRkZCAgIGQgZGQgZCAgZCBkZKxkICBkIGRkZKwgIGQgZGQgIGQgZCBkZKwgZCBkIGRkZGRkIGQgZGQgrGQgZCBkZGQgrCBkIGRkIGSsIGQgZGQgICBkZCBkZKwgIGRkIGRkZGQgZGQgZGQgrCBkZCBkZGQgZGRkIGRkIGRkZGQgZGQgIKxkZCBkZGQgIKxkIGRkIGQgrGQgZGQgIGSsZCBkZCAgICCsIGRkZGQgIKwgZGQgrCAgrCBkZGQgZCCsIGRkIGRkIKwgZGQgIKwgrCBkZGQgIGSsIGRkIGQgZKwgZGQgIGRkrCBkZGSsrGSsIGRkICAgrKwgZGRkICAgIGRkZCBkICAgZGRkrGQgICBkZGRkrCAgIGRkZCAgZCAgZGRkrCBkICBkZGRkZGQgIGRkZCCsZCAgZGRkZCCsICBkZGQgZKwgIGRkZCAgIGQgZGRkrCAgZCBkZGRkZCBkIGRkZCCsIGQgZGRkZCBkZCBkZGQgZGRkIGRkZCAgrGQgZGRkZCAgrCBkZGQgZCCsIGRkZCAgZKwgZGRkICAgIGRkZGSsICAgZGRkZGRkICBkZGRkIKwgIGRkZGRkIGQgZGRkZCBkZCBkZGRkICCsIGRkZGRkICBkZGRkZCBkIGRkZGRkICBkZGRkZGQgICCsZGRkZGQgICCsZGRkIGQgIKxkZGQgIGQgrGRkZKxkrCCsZGRkICAgZKxkZGQgICAgIKxkZKwgICAgrGRkZGQgICCsZGQgrCAgIKxkZGQgZCAgrGRkIGRkICCsZGQgIKwgIKxkZGQgIGQgrGRkIGQgZCCsZGQgIGRkIKxkZKysZGQgrGRkICAgrCCsZGRkICAgZKxkZCBkICBkrGRkICBkIGSsZGQgICBkZKxkZCAgICCsrGRkZKxkIKysZGRkZCCsrKxkZCCsrKysrGRkZCAgICAgrGQgZCAgICCsZKxkICAgIKxkICBkICAgrGSsIGQgICCsZGRkZCAgIKxkIKxkICAgrGRkIKwgICCsZCBkrCAgIKxkICAgZCAgrGRkZCBkICCsZCCsIGQgIKxkZCBkZCAgrGQgZGRkICCsZCAgrGQgIKxkIGQgrCAgrGQgIGSsICCsZCAgICBkIKxkrCAgIGQgrGRkZCAgZCCsZCCsICBkIKxkZCBkIGQgrGQgZGQgZCCsZCAgrCBkIKxkZCAgZGQgrGQgZCBkZCCsZCAgZGRkIKxkICAgrGQgrGRkrGSsZCCsZCBkICCsIKxkICBkIKwgrGQgICBkrCCsZKxkZGSsIKxkZCCsrKwgrGQgICAgIGSsZGRkICAgZKxkIKwgICBkrGRkIGQgIGSsZCBkZCAgZKxkICCsICBkrGRkICBkIGSsZCBkIGQgZKxkICBkZCBkrGQgICCsIGSsZGQgICBkZKxkIGQgIGRkrGQgIGQgZGSsZCAgIGRkZKxkrKwgZGRkrGQgrKxkZGSsZKwgZKxkZKxkICAgIKxkrGQgZGSsrGSsZGQgICAgrKxkIGQgICCsrGQgIGQgIKysZGRkrGQgrKxkIKxkrCCsrGQgICAgZKysZKysrCBkrKxkrCAgZKysrGRkIKysrKysZCAgICAgICCsrCAgICAgIKxkZCAgICAgrCCsICAgICCsZCBkICAgIKwgZGQgICAgrGSsZCAgICCsICCsICAgIKxkZKwgICAgrGQgIGQgICCsIGQgZCAgIKwgIGRkICAgrKwgZGQgICCsZGRkZCAgIKwgrGRkICAgrGQgrGQgICCsICAgrCAgIKxkZCCsICAgrGQgZKwgICCsIGRkrCAgIKxkICAgZCAgrCBkICBkICCsZKwgIGQgIKwgIGQgZCAgrKwgZCBkICCsZGRkIGQgIKwgrGQgZCAgrGQgrCBkICCsIGSsIGQgIKwgICBkZCAgrKwgIGRkICCsZGQgZGQgIKwgrCBkZCAgrGQgZGRkICCsIGRkZGQgIKxkICCsZCAgrCBkIKxkICCsICBkrGQgIKxkrKysZCAgrCAgICCsICCsZGQgIKwgIKysrCAgrCAgrGQgZCCsICCsIGRkIKwgIKxkICBkrCAgrCBkIGSsICCsICBkZKwgIKxkICAgIGQgrCBkICAgZCCsrGQgICBkIKxkrCAgIGQgrCAgZCAgZCCsrCBkICBkIKxkZGQgIGQgrCCsZCAgZCCsZCCsICBkIKwgICBkIGQgrKwgIGQgZCCsZGQgZCBkIKwgrCBkIGQgrGQgZGQgZCCsIGRkZCBkIKwgIKxkIGQgrGQgIKwgZCCsIGQgrCBkIKwgIGSsIGQgrCAgICBkZCCsrCAgIGRkIKxkZCAgZGQgrCCsICBkZCCsZCBkIGRkIKwgZGQgZGQgrCAgrCBkZCCsZCAgZGRkIKwgZCBkZGQgrCAgZGRkZCCsICAgrGRkIKysrCCsZGQgrGQgICCsZCCsIGQgIKxkIKwgIGQgrGQgrGSsrCCsZCCsICAgZKxkIKwgICAgIKwgrGRkICAgrCCsZCBkICCsIKwgZGQgIKwgrGQgIGQgrCCsIGQgZCCsIKwgIGRkIKwgrKwgrKwgrCCsZCAgIGSsIKwgZCAgZKwgrCAgIGRkrCCsZGSsZGSsIKysIKwgrKwgrCCsZGSsrCCsrKxkZKysIKysICCsrKwgrKwgrKysrCCsZCAgICAgZKwgZCAgICBkrGSsICAgIGSsICBkICAgZKysIGQgICBkrGRkZCAgIGSsIKxkICAgZKwgZKwgICBkrCAgIGQgIGSsrCAgZCAgZKxkZCBkICBkrCCsIGQgIGSsZCBkZCAgZKwgZGRkICBkrCAgrGQgIGSsZCAgrCAgZKwgZCCsICBkrCAgZKwgIGSsICAgIGQgZKxkZCAgZCBkrGQgZCBkIGSsIGRkIGQgZKxkICBkZCBkrCBkIGRkIGSsICBkZGQgZKysrGRkZCBkrGQgICCsIGSsIGQgIKwgZKwgIGQgrCBkrCAgIGSsIGSsrGSsrKwgZKwgICAgIGRkrKwgICAgZGSsZGQgICBkZKwgrCAgIGRkrGQgZCAgZGSsIGRkICBkZKwgIKwgIGRkrGQgIGQgZGSsIGQgZCBkZKwgIGRkIGRkrCAgIKwgZGSsrGRkrCBkZKxkICAgZGRkrCBkICBkZGSsICBkIGRkZKwgICBkZGRkrCCsZKxkZGSsZCCsrGRkZKwgICAgrGRkrKxkIGSsZGSsIGSsZKxkZKxkICAgIKxkrCBkICAgrGSsICBkICCsZKysZKwgIKxkrCAgIGQgrGSsZKysrCCsZKwgICAgZKxkrGSsIGRkrGSsrCBkZGSsZKwgIGSsrKxkrCAgICAgIKysZGQgICAgrKysrCAgICCsrCBkZCAgIKysrCCsICAgrKysrKwgICCsrGQgIGQgIKysIGQgZCAgrKwgIGRkICCsrKwgrKwgIKysrKysrCAgrKwgICBkZCCsrGRkrGRkIKysrCAgIKwgrKysrCAgrCCsrKwgrCCsIKysIKysIKwgrKysrKwgrCCsrKwgIKysIKysIKwgrKwgrKysrCCsrCCsrCCsrKysIKysZCAgICBkrKwgZCAgIGSsrCAgZCAgZKysICAgZCBkrKxkrCCsIGSsrCBkrKwgZKysICAgIGRkrKxkrGQgZGSsrGQgZGSsZKysrKwgICCsrKwgrKwgIKysrKwgrKwgrKysIGRkZGSsrKysZCCsZKysrCCsICCsrKysrKwgIKysrKwgIKwgrKysrKwgrCCsrKysIKysIKysrKwgrCCsrKysrKysrKysrKys="); +export const iq3xxs_grid = /* 1024B */ D("BAQEBBQEBAQkBAQEDAwEBBwMBAQ+DAQEBBQEBBQUBAQMHAQEFCQEBBw+BAQsPgQEDAQMBBwEDAQEDAwEFAwMBAwUDAQsFAwEBBwMBBQcDAQMJAwEJCwMBAQ+DAQEBBQEFAQUBCQEFAQMDBQEBBQUBBQUFAQMHBQEHBwUBD4cFAQMLBQEPiwUBCw+FAQMBBwEPgQcBAQMHAQUDBwELBQcBAQ+HAQcDCQEPhwkBCQkJAQ+LCQEHD4kBCw+JAQMBCwEPgQsBBQcLAQULCwELBw0BCQ0NAQEDD4EJAw+BDQMPgQcJD4EDDQ+BAwEBAwcBAQMBAwEDBQMBAwMFAQMHBQEDAQcBAwUHAQMJBwEDD4kBAwELAQMBAQMDBQEDAwMDAwMBBQMDBQUDAwMBBQMHAQUDAQMFAwUDBQMDBQUDAQcFAwUPhQMBAQcDBQEHAwEFBwMDBwcDDQkHAw0NBwMDAQkDCwEJAwELCQMBBQsDCQULAw0JCwMDD4sDCwENAwUFD4MBCQ+DAQEBBQUBAQUDAwEFBwMBBQEFAQUFBQEFDQUBBQMHAQUFCQEFAwEDBQcBAwULAQMFAQMDBQUDAwUDBQMFAQcDBQcNAwUPjQMFAQ+DBQEBBQUFAQUFAwMFBQ+DBQUBBQUFBQUFBQ+HBQUBCQUFCwsFBQMBBwUBAwcFCQMHBQEPhwUJD4cFCwcJBQcLCQUHAQsFD4ULBQMJCwUJD4sFAwEPhQcBD4UNAw+FCwkPhQMBAQcBAwEHBQMBBwMFAQcHBQEHAQsBBwsNAQcFD4EHAQEDBwUBAwcBBQMHAwcDBwkJAwcNCQMHAwEFBwcBBQcBAwUHCwUFBwULBQcFD4UHAwMHBwcHBwcBBwkHD4kJBwUPiQcBAQsHDQELBwUFCwcLCwsHCQMNBw0HDQcHDQ0HBwcPhwEND4cJAQEJD4MBCQsHAQkPhwEJBwsBCQ+LAQkJD4MJAQUFCQ+HBQkBCQUJAQ0FCQ0NBQkPgQcJCwkHCQkBCQkDCwkJCQ0JCQsFCwkHCQsJAQ+LCQsBD4kBAw+JBQMPiQEHD4kFAwELAwkBCwEPgQsBAQMLDQEDCw0FAwsLCwMLCQMFCwUHBQsFD4ULBQEHCwcLBwsBAwkLBwUJCw+FCQsFD4kLBQELCwMHCwsBCw0LCQUPiwUJD4sJBQENCQkBDQ0JAQ0JDQENAwUDDQMNAw0PgwUNCQ0FDQEHBw0NBwcNCQkJDQsBCw0FCwsNBwcNDQcBD40DBQ+NBwEBD4sBAQ+PgQEPgQMBD4UHAQ+FCwEPjQUDD4EJAw+FAwUPiwkFD4ULBQ+BAQcPiwMHD4cHBw+BDQcPgwUJD4MJCQ+BAQsPhQELD4kFCw+BBw0Pg=="); +export const iq3s_grid = /* 2048B */ D("AQEBAQMBAQEFAQEBCwEBAQ8BAQEBAwEBAwMBAQUDAQEJAwEBDQMBAQEFAQEDBQEBCwUBAQcHAQEBCQEBBQkBAQsJAQEPCQEBAwsBAQcLAQEBDQEBBQ0BAQMPAQEJDwEBDw8BAQEBAwEDAQMBBQEDAQkBAwEBAwMBAwMDAQsDAwEBBQMBBwUDAQ8FAwEDBwMBCwcDAQkJAwEDDQMBCw0DAQUPAwEBAQUBAwEFAQsBBQEPAQUBAQMFAQcDBQENAwUBAwUFAQsFBQEBBwUBCQcFAQUJBQELCQUBDwkFAQMLBQEHCwUBAQ8FAQcPBQEHAQcBAwMHAQsDBwEBBQcBBQUHAQMHBwEHBwcBDQcHAQkJBwEBCwcBBQsHAQ8NBwEDDwcBCw8HAQEBCQEHAwkBDwMJAQMFCQEJBQkBBQcJAQEJCQEHCQkBAwsJAQEPCQEFAQsBCQELAQEFCwEFBQsBDQULAQcHCwEDCQsBCwkLAQ8JCwENDQsBBw8LAQ0BDQEDAw0BBwMNAQMHDQEFCw0BAw8NAQEBDwEFAQ8BCQEPAQEFDwEFBQ8BDQUPAQcHDwEBCw8BCQsPAQEBAQMDAQEDBQEBAwkBAQMBAwEDAwMBAwcDAQMLAwEDDwMBAwEFAQMFBQEDAwcBAwkHAQMNBwEDCQsBAw0LAQMDDQEDBQ8BAwEBAwMDAQMDBwEDAw0BAwMBAwMDCQMDAwMFAwMBBwMDBwcDAwMJAwMBCwMDBQsDAwEPAwMNDwMDAQEFAwUDBQMLAwUDDwMFAwEFBQMJBQUDBQcFAwEJBQMHCQUDCwsFAwENBQMFDwUDAwEHAwkBBwMPAQcDAQMHAwcDBwMDBQcDDwUHAwEHBwMJBwcDAwkHAwUNBwMBDwcDBwEJAwsBCQMFAwkDCQMJAwMHCQMHBwkDBQkJAw0JCQMBCwkDCQsJAwMBCwMBAwsDBwMLAwMFCwMBBwsDBQcLAwMLCwMBBQ0DCQUNAw8FDQMJCQ0DDQkNAwMBDwMHAQ8DAQMPAwUDDwMDBQ8DCwcPAwMJDwMFDQ8DAQ8PAwEBAQUDAQEFBwEBBQsBAQUPAQEFAQMBBQUDAQUJAwEFDQMBBQMFAQUHBQEFDwUBBQEHAQUFBwEFAwkBBQcJAQULCQEFAQsBBQULAQUPDQEFAQ8BBQcPAQULDwEFAQEDBQUBAwUBAwMFBwMDBQ8DAwUFBQMFCwUDBQMHAwUJBwMFBQkDBQMLAwUDAQUFCQEFBQ8BBQUDBQUFBwUFBQEHBQUPBwUFAwkFBQcLBQUPCwUFAw8FBQkPBQUBAQcFBQEHBQsBBwUDAwcFBQUHBQkFBwUDBwcFBwcHBQUJBwUBCwcFDQ0HBQMBCQUPAQkFAQUJBQcFCQUFBwkFCwcJBQMJCQUFDwkFCw8JBQkBCwUDAwsFBQULBQ8HCwUBCQsFBwsLBQEPCwUBAQ0FBQENBQ8BDQUDBQ0FCwsNBQMNDQULAQ8FAwMPBQ0FDwUBBw8FBwkPBQELDwUFAQEHAwMBBwcDAQcLAwEHDwMBBwUFAQcDBwEHBwcBBwsHAQcFCQEHCQkBBw8JAQcDCwEHBw0BBwMPAQcDAQMHBwEDBwsBAwcJAwMHAwUDBwcFAwcBCQMHAQ0DBwUPAwcNDwMHAQEFBwUDBQcBBQUHBQcFBwkHBQcBCwUHAwEHBwEDBwcJAwcHAwUHBwcFBwcPBQcHAQcHBwMJBwcHCQcHDwkHBwsLBwcHDwcHBwEJBwMDCQcNAwkHBQUJBwMHCQcFCwkHAQ0JBwkNCQcDAQsHAQMLBwUDCwcLBQsHBQcLBwkJCwcNCwsHBw8LBw0DDQcDCQ0HAwEPBwcBDwcBBQ8HBQUPBwsHDwcBAQEJCQEBCQUDAQkBBQEJCQUBCQ8FAQkFBwEJAwkBCQELAQkBDwEJBQEDCQ8BAwkDAwMJBwMDCQUFAwkBBwMJCwcDCQcJAwkDCwMJCwsDCQMBBQkHAQUJAQMFCQsDBQkDBQUJBwcFCQEJBQkPCwUJBQ0FCQEPBQkJAQcJAwMHCQcDBwkBBQcJBQUHCQMHBwkLBwcJAQEJCQUBCQkJBQkJDwcJCQEJCQkDDwkJCwELCQ8BCwkDBQsJBQ0LCQcDDQkJBw0JAQ0NCQEDDwkLAw8JAQcPCQcJDwkDCw8JBQEBCwEDAQsJAwELBQUBCwEJAQsJCQELDwkBCwULAQsNDQELCQ8BCwMBAwsHAQMLCwEDCwUDAwsDBQMLBQcDCwUPAwsBAQULAwMFCwcFBQsBBwULDQcFCwcLBQsFAQcLDwEHCwEDBwsPBQcLCQkHCwMLBwsLDQcLBw8HCwMBCQsJAQkLAQUJCwUHCQsNCQkLBQMLCw0FCwsDCwsLBwsLCwUJDQsFAQ8LCQEPCwUFDwsDAwENBwMBDQsDAQ0DBwENBwcBDQENAQ0BAQMNAQUDDQ8FAw0JDQMNBQMFDQkHBQ0FCQUNCwsFDQUNBQ0BDwUNAQEHDQkDBw0DBQcNAQkHDQsFCQ0HCQkNBQ0JDQEBCw0HAQsNCQcLDQENCw0LAQ0NAQkNDQMDDw0HAw8NAQEBDwkBAQ8PAQEPAQUBDwUFAQ8NBwEPAQkBDwkLAQ8FDQEPBQEDDwMDAw8JBQMPBwkDDwsJAw8DAQUPCQEFDwEDBQ8NAwUPAwUFDwEHBQ8DCwUPBQEHDwUHBw8LBwcPBwsHDwMBCQ8LAQkPBwMJDwEFCQ8BCwkPBQULDwUJCw8FAQ0PAwcNDwEBDw8="); +export const iq1s_grid = /* 16384B */ D("//////////8B/////////wAA/////////wH///////8BAf///////wD/AP//////AAAA/////////wH//////wH/Af///////wEB//////8BAQH//////wAA/wD/////AP8AAP//////AAAA/////wEAAAD/////AAABAP////////8B/////wH//wH//////wH/Af////8BAf8B/////wAAAAH///////8BAf////8B/wEB//////8BAQH/////AQEBAf//////AP//AP///wAA//8A////AP8A/wD/////AAD/AP///wEAAP8A////AAEA/wD///8BAQD/AP///wAAAf8A////AP//AAD///8BAP8AAP///wAB/wAA////Af8AAAD///8AAAAAAP///wEBAAAA////AP8BAAD/////AAEAAP///wEAAQAA/////wEBAAD///8AAP8BAP///wD/AAEA/////wAAAQD///8BAAABAP///wAAAQEA/////////wH///8B////Af////8B//8B////AQH//wH///8AAAD/Af//////Af8B////Af8B/wH/////AQH/Af///wEBAf8B////AAD/AAH///8A/wAAAf///wABAAAB/////wABAAH///8AAQEAAf///////wEB////Af//AQH/////Af8BAf///wEB/wEB////AP8AAQH///8AAAABAf///wABAAEB//////8BAQH///8B/wEBAf////8BAQEB////AQEBAQH///8A/wD//wD///8AAP//AP//AQAA//8A//8AAAH//wD//wD//wD/AP//AAH/AP8A//8AAAAA/wD//wEBAAD/AP///wABAP8A//8AAAEA/wD//wD/AAH/AP//AAEAAf8A//8AAAEB/wD//wD///8AAP///wD//wAA//8AAP//AAD//wEA//8AAP//AAAA/wAA////AQD/AAD//wEBAP8AAP//AAEB/wAA//////8AAAD//wAA/wAAAP//AQH/AAAA/////wAAAAD//wD/AAAAAP///wAAAAAA//8AAAAAAAD//wEAAAAAAP//AAEAAAAA/////wEAAAD//wH/AQAAAP//AAABAAAA////AQEAAAD//wEBAQAAAP//AP//AQAA//8A/wABAAD//wAAAAEAAP///wEAAQAA//8BAQABAAD//wD/AQEAAP///wABAQAA//8AAAEBAAD//wEAAQEAAP//AAEBAQAA////AAD/AQD//wABAP8BAP//AP//AAEA////AP8AAQD/////AAABAP//Af8AAAEA//8AAAAAAQD///8BAAABAP////8BAAEA//8A/wEAAQD//wEAAQABAP//AAEBAAEA//8AAP8BAQD//wD/AAEBAP///wAAAQEA//8AAQABAQD/////////Af//Af////8B////Af///wH//wEB////Af//AAAA//8B/////wH//wH//wH/Af//Af///wEB//8B//8BAQH//wH//wAA/wD/Af//AP8AAP8B//8BAAAA/wH//wAAAQD/Af//////Af8B//8B//8B/wH///8B/wH/Af//AQH/Af8B//8AAAAB/wH/////AQH/Af//Af8BAf8B////AQEB/wH//wEBAQH/Af//AAD//wAB//8A/wD/AAH///8AAP8AAf//AAEA/wAB////AAH/AAH//wAAAf8AAf//AP//AAAB/////wAAAAH//wD/AAAAAf//AAAAAAAB//8A/wEAAAH///8AAQAAAf//AAEBAAAB//8A/wABAAH///8AAAEAAf//AQAAAQAB//8AAQABAAH//wAAAQEAAf///////wEB//8B////AQH///8B//8BAf//AQH//wEB//8AAAD/AQH/////Af8BAf//Af8B/wEB////AQH/AQH//wEBAf8BAf//AAD/AAEB//8A/wAAAQH//wABAAABAf//AP8BAAEB//8AAAEAAQH//////wEBAf//Af//AQEB//8AAP8BAQH///8B/wEBAf//AQH/AQEB//8AAAABAQH/////AQEBAf//Af8BAQEB////AQEBAQH//wEBAQEBAf////8A////AP8A/wD///8A//8AAP///wD/AAEA////AP//AAH///8A/wAAAf///wD/AP//AP//AP//AP8A//8A////AAD//wD/AAAAAP//AP//AQAA//8A/wD/AQD//wD//wABAP//AP8AAAEA//8A/wABAQD//wD/AP8AAf//AP//AAAB//8A/wEAAAH//wD/AP8BAf//AP8AAAEB//8A/wD///8A/wD//wD//wD/AP8BAP//AP8A/wAB//8A/wD///8A/wD/AP8B/wD/AP8A/wAAAP8A/wD//wEA/wD/AP8A/wH/AP8A//8AAf8A/wD/AAEB/wD/AP8AAP8AAP8A/wEB/wAA/wD///8AAAD/AP8A/wAAAP8A/wH/AAAA/wD//wAAAAD/AP8AAAAAAP8A/wEAAAAA/wD/AAEAAAD/AP///wEAAP8A/wAAAQAA/wD//wD/AQD/AP8B/wABAP8A/wAAAAEA/wD/AP8BAQD/AP//AAEBAP8A/wD/AP8B/wD//wAA/wH/AP8BAAD/Af8A/wAAAf8B/wD/////AAH/AP8BAP8AAf8A/wAB/wAB/wD/Af8AAAH/AP8AAAAAAf8A//8BAAAB/wD/AQEAAAH/AP//AAEAAf8A/wEAAQAB/wD/AAD/AQH/AP8A/wABAf8A//8AAAEB/wD/AQAAAQH/AP8AAAEBAf8A/wD/////AAD/AQD///8AAP8AAf///wAA//8AAP//AAD/AAAA//8AAP//AQD//wAA/wABAP//AAD/AP8B//8AAP8BAAH//wAA/wD//wD/AAD/AAD/AP8AAP8BAP8A/wAA//8B/wD/AAD/AQH/AP8AAP8A/wAA/wAA//8AAAD/AAD/AAAAAP8AAP8BAAAA/wAA/wABAAD/AAD/Af8BAP8AAP8AAAEA/wAA//8BAQD/AAD//wD/Af8AAP8AAf8B/wAA////AAH/AAD//wAAAf8AAP8AAAAB/wAA//8BAAH/AAD/AAEAAf8AAP8BAQAB/wAA/wD/AQH/AAD//wABAf8AAP8AAAEB/wAA/wABAQH/AAD/Af///wAAAP8AAP//AAAA/wEB//8AAAD/AP8A/wAAAP//AAD/AAAA/wAAAP8AAAD/AQAA/wAAAP8AAQD/AAAA////Af8AAAD/Af8B/wAAAP8AAAH/AAAA//8BAf8AAAD/AQEB/wAAAP8A//8AAAAA//8A/wAAAAD/AAD/AAAAAP8BAP8AAAAA/wD/AAAAAAD/Af8AAAAAAP//AAAAAAAA/wAAAAAAAAD/AQAAAAAAAP8AAQAAAAAA/wEBAAAAAAD/AP8BAAAAAP//AAEAAAAA/wAAAQAAAAD/AQABAAAAAP8AAQEAAAAA/////wEAAAD/Af//AQAAAP//AP8BAAAA/wAA/wEAAAD//wH/AQAAAP8BAf8BAAAA////AAEAAAD/AP8AAQAAAP//AAABAAAA/wAAAAEAAAD/AQAAAQAAAP8AAQABAAAA/wEBAAEAAAD///8BAQAAAP8B/wEBAAAA/wAAAQEAAAD/AP///wEAAP//AP//AQAA/wAA//8BAAD/AQD//wEAAP8AAAD/AQAA/wEAAP8BAAD//wEA/wEAAP8BAQD/AQAA/wD/Af8BAAD/AQAB/wEAAP////8AAQAA/wH//wABAAD//wD/AAEAAP8AAP8AAQAA//8B/wABAAD/AQH/AAEAAP8A/wAAAQAA/wAAAAABAAD/AQAAAAEAAP//AQAAAQAA/wABAAABAAD/AP8BAAEAAP//AAEAAQAA/wAAAQABAAD//wEBAAEAAP8AAQEAAQAA/wEBAQABAAD/AQD/AQEAAP8BAf8BAQAA/wH/AAEBAAD/AAAAAQEAAP//AAEBAQAA/wABAQEBAAD/AP8A//8BAP8BAAD//wEA/wAAAf//AQD/AP//AP8BAP//AP8A/wEA/wEA/wD/AQD/AAH/AP8BAP///wAA/wEA/wAAAAD/AQD//wEAAP8BAP8BAQAA/wEA////AQD/AQD/AP8BAP8BAP//AAEA/wEA/wEAAQD/AQD/AAEBAP8BAP8AAP8B/wEA/wD/AAH/AQD//wAAAf8BAP8AAAEB/wEA////AP8AAQD/Af8A/wABAP8AAAD/AAEA/wEBAP8AAQD/AP8B/wABAP8AAAH/AAEA/wH//wAAAQD//wD/AAABAP8AAP8AAAEA//8B/wAAAQD/AP8AAAABAP//AAAAAAEA/wAAAAAAAQD/AQAAAAABAP8AAQAAAAEA/wEBAAAAAQD///8BAAABAP8AAAEAAAEA/wEBAQAAAQD/AAH/AQABAP8A/wABAAEA/wH/AAEAAQD/AAAAAQABAP//AQABAAEA/wD/AQEAAQD/AQABAQABAP8AAQEBAAEA/wAB//8BAQD/AQAA/wEBAP//AAH/AQEA/wEAAf8BAQD//wD/AAEBAP8BAP8AAQEA/wAB/wABAQD///8AAAEBAP8B/wAAAQEA/wAAAAABAQD//wEAAAEBAP8A/wEAAQEA/wEAAQABAQD/AAEBAAEBAP8AAP8BAQEA/wD/AAEBAQD/AQAAAQEBAP8BAQABAQEA/////////wH/Af//////Af//Af////8B/wEB/////wH/AAAA////Af///wH///8B/wH/Af///wH/AAAB////Af//AQH///8B/wEBAf///wH/AAD/AP//Af8A/wAA//8B/wABAAD//wH/AP8BAP//Af8AAAEA//8B/////wH//wH/Af//Af//Af//Af8B//8B/wEB/wH//wH/AAAAAf//Af///wEB//8B/wH/AQH//wH/AAABAf//Af//AQEB//8B/wEBAQH//wH/AAD//wD/Af8A/wD/AP8B//8AAP8A/wH/AAEA/wD/Af8AAAH/AP8B/wH//wAA/wH//wD/AAD/Af8AAf8AAP8B/wAAAAAA/wH//wEAAAD/Af8BAQAAAP8B/wD/AQAA/wH//wABAAD/Af8AAAEAAP8B/wEAAQAA/wH/AAD/AQD/Af///wABAP8B/wEAAAEA/wH/AAEAAQD/Af8AAAEBAP8B/wD///8B/wH//wH//wH/Af8BAf//Af8B/wD/AP8B/wH/AAAA/wH/Af///wH/Af8B/wH/Af8B/wH//wEB/wH/Af8BAQH/Af8B/wAA/wAB/wH/AP8AAAH/Af8BAAAAAf8B/wABAAAB/wH/AAABAAH/Af8A//8BAf8B//8B/wEB/wH/AQH/AQH/Af8A/wABAf8B/wAAAAEB/wH///8BAQH/Af8B/wEBAf8B//8BAQEB/wH/AQEBAQH/Af8AAP///wAB//8AAP//AAH/AQAA//8AAf8AAQD//wAB/wAAAf//AAH//wD/AP8AAf8AAP8A/wAB/wEA/wD/AAH/AAH/AP8AAf8B/wAA/wAB/wAAAAD/AAH//wEAAP8AAf8BAQAA/wAB/wEAAQD/AAH/AAD/Af8AAf8A/wAB/wAB//8AAAH/AAH/AAEAAf8AAf8A/wEB/wAB/wAAAQH/AAH/AAH//wAAAf8AAAD/AAAB/wD/Af8AAAH/AAEB/wAAAf////8AAAAB/wAA/wAAAAH//wH/AAAAAf8A/wAAAAAB//8AAAAAAAH/AAAAAAAAAf8AAQAAAAAB/wH/AQAAAAH/AAABAAAAAf//AQEAAAAB/wAB/wEAAAH///8AAQAAAf//AAABAAAB/wAAAAEAAAH//wEAAQAAAf8BAQABAAAB/wD/AQEAAAH//wABAQAAAf8BAAEBAAAB/wABAQEAAAH/AAD//wEAAf///wD/AQAB/wH/AP8BAAH/AAEA/wEAAf8AAAH/AQAB/wD//wABAAH/AAH/AAEAAf8AAAAAAQAB////AQABAAH/AP8BAAEAAf8AAQEAAQAB//8A/wEBAAH/AQD/AQEAAf///wABAQAB/wEBAAEBAAH///////8BAf8B/////wEB//8B////AQH/AQH///8BAf8AAAD//wEB////Af//AQH/Af8B//8BAf//AQH//wEB/wEBAf//AQH/AAD/AP8BAf8A/wAA/wEB//8AAAD/AQH/AAABAP8BAf////8B/wEB/wH//wH/AQH//wH/Af8BAf8BAf8B/wEB////AQH/AQH/Af8BAf8BAf//AQEB/wEB/wEBAQH/AQH/AAH//wABAf8A/wD/AAEB//8AAP8AAQH/AAEA/wABAf8AAAH/AAEB/wEA/wAAAQH/AAH/AAABAf8B/wAAAAEB/wAAAAAAAQH/AP8BAAABAf//AAEAAAEB/wEAAQAAAQH/AAEBAAABAf8AAP8BAAEB////AAEAAQH/AQAAAQABAf8AAQABAAEB//8AAQEAAQH/AAABAQABAf//////AQEB/wH///8BAQH//wH//wEBAf8BAf//AQEB////Af8BAQH/Af8B/wEBAf//AQH/AQEB/wEBAf8BAQH/AAD/AAEBAf8A/wAAAQEB/wEAAAABAQH/AAEAAAEBAf8AAAEAAQEB/////wEBAQH/Af//AQEBAf//Af8BAQEB/wEB/wEBAQH/AAAAAQEBAf///wEBAQEB/wH/AQEBAQH//wEBAQEBAf8BAQEBAQEB/wAA//////8AAP8A/////wABAAD/////AAAAAf////8AAAH/AP///wAB/wAA////AAAAAAD///8A/wEAAP///wABAQAA////AAD/AQD///8A/wABAP///wABAAEA////AP8AAAH///8AAAEAAf///wAA/wEB////AAEAAQH///8A/////wD//wAA////AP//AP8A//8A//8AAQD//wD//wAAAf//AP//AAH/AP8A//8AAAAA/wD//wABAAD/AP//AP8BAP8A//8AAQEA/wD//wAA/wH/AP//AAEAAf8A//8AAAEB/wD//wAAAP8AAP//AP8B/wAA//8AAQH/AAD//wAA/wAAAP//AP8AAAAA//8AAAAAAAD//wABAAAAAP//AAABAAAA//8AAQEAAAD//wAAAAEAAP//AP8BAQAA//8AAQEBAAD//wAA//8BAP//AP8A/wEA//8AAQD/AQD//wD//wABAP//AAH/AAEA//8AAAAAAQD//wD//wEBAP//AAD/AQEA//8AAf8BAQD//wAAAP//Af//AAD/AP8B//8A/wAA/wH//wABAAD/Af//AAAAAf8B//8AAP//AAH//wAB/wAAAf//AAAAAAAB//8AAQEAAAH//wD/AAEAAf//AAABAQAB//8AAAH/AQH//wD/AAABAf//AAAAAQEB//8AAP////8A/wAAAAD//wD/AAABAP//AP8AAAEB//8A/wAAAP8A/wD/AP8B/wD/AP8AAQH/AP8A/wAA/wAA/wD/AP8AAAD/AP8AAAAAAP8A/wABAAAA/wD/AAD/AQD/AP8AAf8BAP8A/wAAAAEA/wD/AP8BAQD/AP8AAQEBAP8A/wAA//8B/wD/AAEA/wH/AP8AAAH/Af8A/wD//wAB/wD/AAH/AAH/AP8AAAAAAf8A/wD//wEB/wD/AAD/AQH/AP8AAAEBAf8A/wAA////AAD/AAH///8AAP8AAAD//wAA/wABAf//AAD/AAD/AP8AAP8A/wAA/wAA/wAAAAD/AAD/AAEAAP8AAP8AAAEA/wAA/wD//wH/AAD/AAAAAf8AAP8AAQEB/wAA/wAA//8AAAD/AP8A/wAAAP8AAAD/AAAA/wABAP8AAAD/AAAB/wAAAP8A//8AAAAA/wAA/wAAAAD/AP8AAAAAAP8AAAAAAAAA/wABAAAAAAD/AP8BAAAAAP8AAAEAAAAA/wAA/wEAAAD/AP8AAQAAAP8AAAABAAAA/wABAAEAAAD/AAABAQAAAP8AAf//AQAA/wD/AP8BAAD/AAAA/wEAAP8A/wH/AQAA/wAA/wABAAD/AP8AAAEAAP8AAAAAAQAA/wABAAABAAD/AAABAAEAAP8AAQEAAQAA/wAAAAEBAAD/AP8BAQEAAP8AAQEBAQAA/wAA////AQD/AAAA//8BAP8AAAH//wEA/wD/AAD/AQD/AAAAAP8BAP8A/wEA/wEA/wABAQD/AQD/AAD/Af8BAP8A/wAB/wEA/wAAAQH/AQD/AP///wABAP8AAf//AAEA/wAAAP8AAQD/AP8B/wABAP8A//8AAAEA/wAA/wAAAQD/AAH/AAABAP8AAAAAAAEA/wABAAAAAQD/AAABAAABAP8AAf8BAAEA/wAAAAEAAQD/AP8BAQABAP8AAP//AQEA/wAAAP8BAQD/AAEB/wEBAP8A/wAAAQEA/wAAAAABAQD/AAD/AQEBAP8A/wABAQEA/wABAAEBAQD/AAAA////Af8AAP8A//8B/wAAAAD//wH/AAEBAP//Af8AAAAB//8B/wAB//8A/wH/AAAB/wD/Af8A//8AAP8B/wAAAAAA/wH/AP8BAAD/Af8AAP8BAP8B/wD/AAEA/wH/AAEAAQD/Af8AAAEBAP8B/wAAAP8B/wH/AAD/AAH/Af8A/wAAAf8B/wABAAAB/wH/AAABAAH/Af8AAAABAf8B/wAA////AAH/AAAA//8AAf8AAQD//wAB/wABAf//AAH/AP//AP8AAf8A/wAA/wAB/wAAAAD/AAH/AP8BAP8AAf8AAP8B/wAB/wD/AAH/AAH/AAEAAf8AAf8A////AAAB/wAAAP8AAAH/AAEB/wAAAf8AAP8AAAAB/wAB/wAAAAH/AP8AAAAAAf8AAAAAAAAB/wABAAAAAAH/AAABAAAAAf8A//8BAAAB/wAB/wEAAAH/AAAAAQAAAf8AAQABAAAB/wABAQEAAAH/AAEA/wEAAf8AAAH/AQAB/wAB/wABAAH/AAAAAAEAAf8AAQAAAQAB/wD/AQABAAH/AAD/AQEAAf8A/wABAQAB/wABAAEBAAH/AAABAQEAAf8AAQAA/wEB/wD/AP8AAQH/AAEA/wABAf8AAAH/AAEB/wAAAAAAAQH/AP8BAAABAf8AAQEAAAEB/wD/AAEAAQH/AAABAQABAf8A/wAAAQEB/wAAAAEBAQH/AAD//////wAA/wD/////AAAAAP////8AAAEA/////wAAAAH/////AAAB/wD///8AAAAAAP///wAAAQEA////AAAA/wH///8AAP8AAf///wAAAAEB////AAD///8A//8AAAAA/wD//wAA/wH/AP//AAAA/wAA//8AAP8AAAD//wAAAAAAAP//AAABAAAA//8AAAABAAD//wAAAAABAP//AAD/AQEA//8AAAEA/wH//wAAAAH/Af//AAAAAAAB//8AAP8BAAH//wAA//8BAf//AAAA/wEB//8AAAEAAQH//wAAAAEBAf//AAAAAP//AP8AAP8B//8A/wAAAAH//wD/AAABAf//AP8AAAD/AP8A/wAA/wAA/wD/AAAAAAD/AP8AAAEAAP8A/wAA/wEA/wD/AAAAAQD/AP8AAP//Af8A/wAAAAAB/wD/AAABAAH/AP8AAP8BAf8A/wAAAQEB/wD/AAAA//8AAP8AAP8A/wAA/wAAAAD/AAD/AAABAP8AAP8AAAAB/wAA/wAA//8AAAD/AAAA/wAAAP8AAAH/AAAA/wAA/wAAAAD/AAAAAAAAAP8AAAEAAAAA/wAA/wEAAAD/AAAAAQAAAP8AAAEBAAAA/wAAAP8BAAD/AAD/AAEAAP8AAAAAAQAA/wAAAQABAAD/AAAAAQEAAP8AAAH//wEA/wAAAAD/AQD/AAAA/wABAP8AAP8AAAEA/wAAAAAAAQD/AAABAAABAP8AAAABAAEA/wAA//8BAQD/AAAAAAEBAP8AAAEBAQEA/wAAAP///wH/AAABAP//Af8AAAH/AP8B/wAAAAAA/wH/AAABAQD/Af8AAAD/Af8B/wAA/wAB/wH/AAAB//8AAf8AAAAA/wAB/wAAAQH/AAH/AAAA/wAAAf8AAP8AAAAB/wAAAAAAAAH/AAABAAAAAf8AAAABAAAB/wAAAf8BAAH/AAAAAAEAAf8AAAAA/wEB/wAA//8AAQH/AAAB/wABAf8AAAAAAAEB/wAAAAEAAQH/AAABAQABAf8AAP8AAQEB/wAA/wD///8AAAAAAP///wAAAAD/AP//AAAA/wAA//8AAAAAAAD//wAAAAEAAP//AAAA/wEA//8AAAAAAQD//wAAAAD/Af//AAAAAAAB//8AAAD/AQH//wAAAAEBAf//AAAAAP//AP8AAAD/AP8A/wAAAAAA/wD/AAAAAQD/AP8AAAAAAf8A/wAAAAEB/wD/AAAA//8AAP8AAAAA/wAA/wAAAP8AAAD/AAAAAAAAAP8AAAABAAAA/wAAAP8BAAD/AAAAAAEAAP8AAAABAQAA/wAAAAD/AQD/AAAAAf8BAP8AAAD/AAEA/wAAAAAAAQD/AAAAAQABAP8AAAAAAQEA/wAAAP///wH/AAAA/wH/Af8AAAABAf8B/wAAAAD/AAH/AAAA/wAAAf8AAAAAAAAB/wAAAAEAAAH/AAAAAAEAAf8AAAAA/wEB/wAAAP8AAQH/AAAAAAABAf8AAAABAQEB/wAAAAD///8AAAAAAf///wAAAAD/AP//AAAAAAAA//8AAAAAAQD//wAAAAAAAf//AAAAAP//AP8AAAAAAP8A/wAAAAAB/wD/AAAAAP8AAP8AAAAAAAAA/wAAAAABAAD/AAAAAAABAP8AAAAAAQEA/wAAAAAA/wH/AAAAAP8AAf8AAAAAAAAB/wAAAAABAAH/AAAAAAABAf8AAAAA////AAAAAAAA//8AAAAAAAH//wAAAAAA/wD/AAAAAAAAAP8AAAAAAAEA/wAAAAAA/wH/AAAAAAAAAf8AAAAAAP//AAAAAAAAAP8AAAAAAAAB/wAAAAAAAP8AAAAAAAAAAAAAAAAAAAABAAAAAAAAAP8BAAAAAAAAAAEAAAAAAAABAQAAAAAAAP//AQAAAAAAAP8BAAAAAAD/AAEAAAAAAAAAAQAAAAAAAQABAAAAAAD/AQEAAAAAAAABAQAAAAAAAQEBAAAAAAAA//8BAAAAAP8A/wEAAAAAAAD/AQAAAAAAAf8BAAAAAAEB/wEAAAAA//8AAQAAAAAA/wABAAAAAP8AAAEAAAAAAAAAAQAAAAABAAABAAAAAP8BAAEAAAAAAAEAAQAAAAAA/wEBAAAAAP8AAQEAAAAAAAABAQAAAAABAAEBAAAAAAABAQEAAAAA/////wEAAAAA////AQAAAAH///8BAAAA/wD//wEAAAABAP//AQAAAP8B//8BAAAAAAH//wEAAAAA/wD/AQAAAP8AAP8BAAAAAAAA/wEAAAD/AQD/AQAAAAABAP8BAAAA//8B/wEAAAAA/wH/AQAAAAH/Af8BAAAA/wAB/wEAAAAAAAH/AQAAAAEAAf8BAAAA/wEB/wEAAAAAAQH/AQAAAAD//wABAAAAAAD/AAEAAAABAP8AAQAAAP8B/wABAAAAAAH/AAEAAAABAf8AAQAAAP//AAABAAAAAP8AAAEAAAAB/wAAAQAAAP8AAAABAAAAAAAAAAEAAAABAAAAAQAAAP8BAAABAAAAAAEAAAEAAAABAQAAAQAAAAD/AQABAAAA/wABAAEAAAAAAAEAAQAAAAABAQABAAAAAf//AQEAAAAAAP8BAQAAAAEA/wEBAAAA/wH/AQEAAAAAAf8BAQAAAAEB/wEBAAAAAP8AAQEAAAAAAAABAQAAAAEBAAEBAAAAAf8BAQEAAAAAAAEBAQAAAAEAAQEBAAAA/wEBAQEAAAAAAQEBAQAAAP8A////AQAAAAD///8BAAABAP///wEAAAAB////AQAA//8A//8BAAAAAAD//wEAAP8BAP//AQAAAP8B//8BAAABAQH//wEAAAAA/wD/AQAA/wH/AP8BAAABAf8A/wEAAAD/AAD/AQAA/wAAAP8BAAAAAAAA/wEAAAEAAAD/AQAA/wEAAP8BAAAAAQAA/wEAAP//AQD/AQAAAf8BAP8BAAD/AAEA/wEAAAAAAQD/AQAAAf//Af8BAAAAAf8B/wEAAP//AAH/AQAAAf8AAf8BAAAAAAAB/wEAAP8BAAH/AQAAAP8BAf8BAAAAAQEB/wEAAAD///8AAQAAAf///wABAAAAAP//AAEAAAEB//8AAQAAAP8A/wABAAD/AAD/AAEAAAAAAP8AAQAAAQAA/wABAAAAAQD/AAEAAAAAAf8AAQAAAP//AAABAAD/AP8AAAEAAAAA/wAAAQAAAQD/AAABAAAAAf8AAAEAAP//AAAAAQAAAP8AAAABAAAB/wAAAAEAAP8AAAAAAQAAAAAAAAABAAABAAAAAAEAAP8BAAAAAQAAAAEAAAABAAABAQAAAAEAAAD/AQAAAQAA/wABAAABAAAAAAEAAAEAAAEAAQAAAQAAAAEBAAABAAAA//8BAAEAAAAA/wEAAQAAAAH/AQABAAAA/wABAAEAAP8AAAEAAQAAAAAAAQABAAABAAABAAEAAP8BAAEAAQAAAAEAAQABAAAAAAEBAAEAAP8A//8BAQAA/wH//wEBAAAAAAD/AQEAAAEBAP8BAQAA//8B/wEBAAAAAAH/AQEAAAEAAf8BAQAAAAEB/wEBAAAAAP8AAQEAAP8B/wABAQAAAAH/AAEBAAAA/wAAAQEAAAAAAAABAQAAAQAAAAEBAAD/AQAAAQEAAAABAAABAQAAAf8BAAEBAAAAAAEAAQEAAP8BAQABAQAAAQEBAAEBAAAA//8BAQEAAAEB/wEBAQAAAf8AAQEBAAAAAAABAQEAAAEAAAEBAQAA/wEAAQEBAAABAQABAQEAAAD/AQEBAQAAAAD/////AQD/AAD///8BAAEAAP///wEAAAEA////AQAAAAH///8BAP8A/wD//wEA//8AAP//AQAAAAAA//8BAAEAAAD//wEA/wEAAP//AQABAQAA//8BAAD/AQD//wEA/wABAP//AQABAAEA//8BAAABAQD//wEAAP//Af//AQABAAAB//8BAAAAAQH//wEAAP///wD/AQD/AP//AP8BAAEA//8A/wEAAAH//wD/AQAB/wD/AP8BAAAAAP8A/wEAAP8B/wD/AQAB/wH/AP8BAAEAAf8A/wEAAAEB/wD/AQAAAP8AAP8BAAAB/wAA/wEAAP8AAAD/AQAAAAAAAP8BAAEAAAAA/wEAAAEAAAD/AQAAAAEAAP8BAAEAAQAA/wEAAQEBAAD/AQD/AP8BAP8BAAEB/wEA/wEAAf8AAQD/AQAAAAABAP8BAAD/AQEA/wEAAQABAQD/AQAAAQEBAP8BAAD/AP8B/wEAAQAA/wH/AQAAAQD/Af8BAP///wAB/wEAAP//AAH/AQABAP8AAf8BAAAAAAAB/wEAAQAAAAH/AQD/AQAAAf8BAP//AQAB/wEAAAD/AQH/AQAA/wABAf8BAAEAAAEB/wEAAAABAQH/AQAA/wD//wABAAH/AP//AAEAAAAA//8AAQABAAD//wABAAEBAP//AAEAAP8B//8AAQABAAH//wABAAABAf//AAEA////AP8AAQAB//8A/wABAAAA/wD/AAEA/wH/AP8AAQABAf8A/wABAAD/AAD/AAEA/wAAAP8AAQAAAAAA/wABAAEAAAD/AAEAAAEAAP8AAQABAQAA/wABAP//AQD/AAEAAf8BAP8AAQAAAAEA/wABAP8A/wH/AAEAAAD/Af8AAQAAAf8B/wABAP//AAH/AAEAAf8AAf8AAQD/AAAB/wABAAAAAAH/AAEAAQAAAf8AAQD/AQAB/wABAAEBAAH/AAEAAP8BAf8AAQD/AAEB/wABAAABAQH/AAEAAAD//wAAAQD/Af//AAABAAEB//8AAAEAAP8A/wAAAQAAAAD/AAABAAEAAP8AAAEAAAEA/wAAAQD/AP8AAAABAAAA/wAAAAEAAQD/AAAAAQAAAf8AAAABAP//AAAAAAEAAP8AAAAAAQD/AAAAAAABAAAAAAAAAAEAAQAAAAAAAQAAAQAAAAABAAD/AQAAAAEA/wABAAAAAQAAAAEAAAABAAEAAQAAAAEAAAEBAAAAAQABAP8BAAABAAAB/wEAAAEAAQH/AQAAAQAA/wABAAABAAAAAAEAAAEAAQAAAQAAAQAAAQABAAABAAEBAAEAAAEAAf8BAQAAAQAAAAEBAAABAAEAAQEAAAEA/wEBAQAAAQAB////AQABAAAB//8BAAEAAAAA/wEAAQD//wH/AQABAAEAAf8BAAEA/wEB/wEAAQAAAQH/AQABAP///wABAAEAAAD/AAEAAQD/Af8AAQABAAEB/wABAAEAAP8AAAEAAQD/AAAAAQABAAAAAAABAAEAAQAAAAEAAQD/AQAAAQABAAEBAAABAAEA//8BAAEAAQAAAAEAAQABAP8BAQABAAEA////AQEAAQAB//8BAQABAAAA/wEBAAEAAQH/AQEAAQD/AAABAQABAAEAAAEBAAEA/wEAAQEAAQAAAQABAQABAP//AQEBAAEA/wABAQEAAQABAAEBAQABAAEBAQEBAAEAAQAA//8BAQAAAQD//wEBAAAAAf//AQEAAP//AP8BAQAB/wAA/wEBAAAAAAD/AQEAAQEAAP8BAQAA/wEA/wEBAAABAQD/AQEAAAD/Af8BAQAA/wAB/wEBAP8BAAH/AQEAAQABAf8BAQAA////AAEBAP8A//8AAQEA//8A/wABAQAAAAD/AAEBAAD/Af8AAQEA/wAB/wABAQABAAH/AAEBAAABAf8AAQEA////AAABAQAA//8AAAEBAAAA/wAAAQEAAQD/AAABAQD/Af8AAAEBAAD/AAAAAQEA/wAAAAABAQAAAAAAAAEBAAEAAAAAAQEAAAEAAAABAQD//wEAAAEBAAAAAQAAAQEAAQEBAAABAQAB//8BAAEBAP8A/wEAAQEAAQH/AQABAQAAAAABAAEBAAD/AQEAAQEA/wABAQABAQAAAAEBAAEBAAABAQEAAQEAAP8A/wEBAQABAAD/AQEBAP8BAP8BAQEAAP//AAEBAQD/AP8AAQEBAAAB/wABAQEA//8AAAEBAQAAAAAAAQEBAP8BAAABAQEAAQEAAAEBAQD/AAEAAQEBAAAAAQABAQEAAAEBAAEBAQABAP8BAQEBAP8AAAEBAQEA/wEAAQEBAQABAQABAQEBAAEAAQEBAQEA/////////wEB////////Af8B//////8BAQH//////wH//wH/////AQH/Af////8B/wEB/////wEBAQH/////AQAA/wD///8B//8AAP///wEA/wAA////Af8AAAD///8BAQAAAP///wEAAQAA////AQAAAQD///8B////Af///wEB//8B////Af8B/wH///8BAQH/Af///wEAAAAB////Af//AQH///8BAf8BAf///wH/AQEB////AQEBAQH///8BAAD//wD//wEA/wD/AP//Af8AAP8A//8BAQAA/wD//wEAAQD/AP//AQAAAf8A//8BAP//AAD//wH/AP8AAP//AQAB/wAA//8B//8AAAD//wEB/wAAAP//AQAAAAAA//8BAQAAAAD//wH/AQAAAP//AQABAAAA//8B/wABAAD//wEBAAEAAP//AQABAQAA//8BAAD/AQD//wEAAf8BAP//Af8AAAEA//8BAQAAAQD//wEAAQABAP//AQAAAQEA//8B/////wH//wEB////Af//Af8B//8B//8BAQH//wH//wEAAAD/Af//Af//Af8B//8BAf8B/wH//wH/AQH/Af//AQEBAf8B//8BAP8AAAH//wH/AAAAAf//AQABAAAB//8BAAABAAH//wH///8BAf//AQH//wEB//8B/wH/AQH//wEBAf8BAf//AQAAAAEB//8B//8BAQH//wEB/wEBAf//Af8BAQEB//8BAQEBAQH//wH/AAD//wD/AQABAP//AP8BAP//AP8A/wH/AP8A/wD/AQD/AAD/AP8BAAAAAP8A/wEBAQAA/wD/AQD/AQD/AP8B/wABAP8A/wEAAQEA/wD/Af8AAAH/AP8BAAEAAf8A/wEA////AAD/AQAB//8AAP8BAf8A/wAA/wEAAAD/AAD/AQEBAP8AAP8BAQAB/wAA/wEAAQH/AAD/Af///wAAAP8BAP//AAAA/wEAAP8AAAD/Af8B/wAAAP8BAP8AAAAA/wH/AAAAAAD/AQAAAAAAAP8BAQAAAAAA/wEAAQAAAAD/AQEBAAAAAP8BAAABAAAA/wEBAAEAAAD/Af8BAQAAAP8BAQEBAAAA/wEA//8BAAD/Af8A/wEAAP8BAQD/AQAA/wEAAf8BAAD/Af//AAEAAP8BAf8AAQAA/wEAAAABAAD/Af8BAAEAAP8BAQABAQAA/wEA/wD/AQD/AQEAAP8BAP8BAAEA/wEA/wEAAAH/AQD/AQD//wABAP8B/wD/AAEA/wEAAf8AAQD/AQEB/wABAP8B//8AAAEA/wEAAAAAAQD/AQABAAABAP8BAQEAAAEA/wEA/wEAAQD/AQEAAQABAP8BAQEBAAEA/wEAAP8BAQD/AQD/AAEBAP8BAQEAAQEA/wH/AAEBAQD/Af//////Af8BAf////8B/wH/Af///wH/AQEB////Af8BAAAA//8B/wH//wH//wH/AQH/Af//Af8B/wEB//8B/wEBAQH//wH/AQD//wD/Af8BAAD/AP8B/wEA/wAA/wH/Af8AAAD/Af8BAAEAAP8B/wEAAAEA/wH/AQABAQD/Af8B////Af8B/wEB//8B/wH/Af8B/wH/Af8BAQH/Af8B/wEAAAAB/wH/Af//AQH/Af8BAf8BAf8B/wH/AQEB/wH/AQEBAQH/Af8BAAD//wAB/wEBAP//AAH/AQD/AP8AAf8B/wAA/wAB/wEBAAD/AAH/AQAAAf8AAf8BAP//AAAB/wH/AP8AAAH/AQEA/wAAAf8BAAH/AAAB/wH//wAAAAH/AQH/AAAAAf8BAAAAAAAB/wEBAQAAAAH/AQD/AQAAAf8B/wABAAAB/wEAAP8BAAH/AQEAAAEAAf8BAAEAAQAB/wEAAAEBAAH/Af////8BAf8BAf///wEB/wH/Af//AQH/AQEB//8BAf8BAAAA/wEB/wH//wH/AQH/AQH/Af8BAf8B/wEB/wEB/wEBAQH/AQH/AQAA/wABAf8BAP8AAAEB/wH/AAAAAQH/AQEAAAABAf8B////AQEB/wEB//8BAQH/Af8B/wEBAf8BAQH/AQEB/wEAAAABAQH/Af//AQEBAf8BAf8BAQEB/wH/AQEBAQH/AQEBAQEBAf8BAAD/////AAEA/wD///8AAQEAAP///wAB/wEA////AAEAAQD///8AAQAAAf///wABAP//AP//AAEBAP8A//8AAQAB/wD//wABAAAAAP//AAH/AQAA//8AAQEBAAD//wABAAEBAP//AAEBAQEA//8AAQAA/wH//wABAP8AAf//AAH/AAAB//8AAQEAAAH//wABAAEAAf//AAEAAAEB//8AAQD///8A/wAB/wD//wD/AAEBAP//AP8AAQAB//8A/wAB//8A/wD/AAEAAAD/AP8AAf8BAP8A/wABAQEA/wD/AAEA/wH/AP8AAf8AAf8A/wABAQAB/wD/AAEAAQH/AP8AAf///wAA/wABAAD/AAD/AAH//wAAAP8AAQD/AAAA/wAB/wAAAAD/AAEAAAAAAP8AAQEAAAAA/wABAAEAAAD/AAEB/wEAAP8AAQAAAQAA/wAB/wD/AQD/AAEBAP8BAP8AAQH/AAEA/wABAAAAAQD/AAH/AQABAP8AAQD/AQEA/wAB/wABAQD/AAEBAAEBAP8AAQABAQEA/wABAAD//wH/AAEA/wD/Af8AAf8AAP8B/wABAAEA/wH/AAEAAAH/Af8AAf8A/wAB/wABAQD/AAH/AAEAAf8AAf8AAf//AAAB/wABAf8AAAH/AAEAAAAAAf8AAf8BAAAB/wABAQABAAH/AAEAAQEAAf8AAQAA/wEB/wAB/wAAAQH/AAEBAAABAf8AAQABAQEB/wABAP////8AAAH/AP///wAAAQEA////AAAB//8A//8AAAEAAAD//wAAAf8BAP//AAABAQAB//8AAAH///8A/wAAAQEB/wD/AAABAP8AAP8AAAH/AAAA/wAAAQAAAAD/AAABAQAAAP8AAAH/AQAA/wAAAQABAAD/AAAB//8BAP8AAAEA/wEA/wAAAQH/AQD/AAABAAABAP8AAAH/AP8B/wAAAQEA/wH/AAABAf8AAf8AAAH/AAAB/wAAAQAAAAH/AAAB/wEAAf8AAAEA/wEB/wAAAQABAQH/AAAB/////wAAAAEAAP//AAAAAf8B//8AAAABAQH//wAAAAH//wD/AAAAAQD/AP8AAAAB/wAA/wAAAAEAAAD/AAAAAQEAAP8AAAABAAEA/wAAAAEA/wH/AAAAAQAAAf8AAAABAAEB/wAAAAEBAQH/AAAAAQD//wAAAAAB/wD/AAAAAAEAAP8AAAAAAQEA/wAAAAABAAH/AAAAAAH//wAAAAAAAQD/AAAAAAABAf8AAAAAAAH/AAAAAAAAAQAAAAAAAAABAQAAAAAAAAH/AQAAAAAAAQABAAAAAAABAQEAAAAAAAEA/wEAAAAAAf8AAQAAAAABAAABAAAAAAEBAAEAAAAAAQABAQAAAAABAP//AQAAAAEAAP8BAAAAAf8B/wEAAAABAP8AAQAAAAEB/wABAAAAAf8AAAEAAAABAAAAAQAAAAEBAAABAAAAAQABAAEAAAABAQEAAQAAAAH//wEBAAAAAQH/AQEAAAABAAABAQAAAAH/AQEBAAAAAQEBAQEAAAABAP///wEAAAH/AP//AQAAAf//AP8BAAABAAAA/wEAAAEAAQD/AQAAAf//Af8BAAABAQAB/wEAAAEAAQH/AQAAAQAA/wABAAAB/wH/AAEAAAEAAf8AAQAAAQD/AAABAAABAf8AAAEAAAEAAAAAAQAAAQEAAAABAAABAAEAAAEAAAEAAAEAAQAAAf8BAQABAAABAf//AQEAAAH/AP8BAQAAAQAB/wEBAAABAQH/AQEAAAEB/wABAQAAAf8AAAEBAAABAAAAAQEAAAH/AAEBAQAAAQEAAQEBAAABAAEBAQEAAAEAAP///wEAAQEAAP//AQABAAEA//8BAAEAAAH//wEAAQD//wD/AQABAQD/AP8BAAH//wAA/wEAAQH/AAD/AQABAAAAAP8BAAEBAAAA/wEAAQEBAAD/AQAB/wABAP8BAAEAAAEA/wEAAQAA/wH/AQABAP8AAf8BAAEBAAAB/wEAAQABAAH/AQABAAABAf8BAAH/AP//AAEAAQEA//8AAQABAAH//wABAAH//wD/AAEAAQH/AP8AAQABAAAA/wABAAH/AQD/AAEAAQEBAP8AAQAB//8B/wABAAEA/wH/AAEAAf8AAf8AAQABAQAB/wABAAH///8AAAEAAQH//wAAAQABAAD/AAABAAH/Af8AAAEAAQEB/wAAAQABAP8AAAABAAH/AAAAAAEAAQAAAAAAAQABAQAAAAABAAEAAQAAAAEAAQH/AQAAAQABAAABAAABAAEBAAEAAAEAAQEBAQAAAQABAP//AQABAAH/AP8BAAEAAf//AAEAAQABAf8AAQABAAEAAAABAAEAAQEBAAEAAQABAP8BAQABAAEBAAEBAAEAAQAA//8BAQABAAAA/wEBAAEAAAH/AQEAAf8A/wABAQABAQD/AAEBAAEAAf8AAQEAAf//AAABAQABAAAAAAEBAAH/AQAAAQEAAQD/AQABAQABAAD/AQEBAAEA/wABAQEAAf8AAAEBAQABAAAAAQEBAAEBAAABAQEAAf///////wEBAf//////AQH/Af////8BAQEB/////wEBAAAA////AQH//wH///8BAQH/Af///wEB/wEB////AQEBAQH///8BAQAA/wD//wEBAP8AAP//AQH/AAAA//8BAQEAAAD//wEBAAEAAP//AQH///8B//8BAQH//wH//wEB/wH/Af//AQEBAf8B//8BAQAAAAH//wEB//8BAf//AQEB/wEB//8BAf8BAQH//wEBAQEBAf//AQEAAP//AP8BAQAB//8A/wEBAP8A/wD/AQH/AAD/AP8BAQEAAP8A/wEBAAEA/wD/AQEBAQD/AP8BAQEA/wAA/wEBAAH/AAD/AQEA/wAAAP8BAQAAAAAA/wEB/wEAAAD/AQEBAQAAAP8BAQD/AQAA/wEB/wABAAD/AQEAAP8BAP8BAf//AAEA/wEBAf8AAQD/AQEBAAABAP8BAQABAAEA/wEBAf///wH/AQH/Af//Af8BAQEB//8B/wEB//8A/wH/AQEAAQD/Af8BAQH/Af8B/wEB/wEB/wH/AQEBAQH/Af8BAQAA/wAB/wEBAP8AAAH/AQEBAAAAAf8BAQABAAAB/wEBAAABAAH/AQH///8BAf8BAQH//wEB/wEB/wH/AQH/AQEBAf8BAf8BAQAAAAEB/wEB//8BAQH/AQEB/wEBAf8BAf8BAQEB/wEBAQEBAQH/AQEAAQD//wABAQAAAf//AAEBAP//AP8AAQH/AP8A/wABAf//AAD/AAEB/wAAAP8AAQEAAAAA/wABAf8BAAD/AAEBAQEAAP8AAQEA/wEA/wABAQAAAQD/AAEBAQABAP8AAQH/AQEA/wABAQABAQD/AAEBAAD/Af8AAQEBAP//AAABAQAB//8AAAEB//8A/wAAAQEB/wD/AAABAQAAAP8AAAEB/wEA/wAAAQEBAAH/AAABAQABAf8AAAEBAf//AAAAAQEAAP8AAAABAQD/AAAAAAEB/wAAAAAAAQEAAAAAAAABAQEAAAAAAAEBAAEAAAAAAQEAAAEAAAABAQEBAQAAAAEBAP//AQAAAQH/AP8BAAABAQAA/wEAAAEBAQD/AQAAAQEAAf8BAAABAQH/AAEAAAEBAAAAAQAAAQH/AQABAAABAQAA//8BAAEBAP8A/wEAAQEBAAD/AQABAQEBAP8BAAEBAP8B/wEAAQEAAAH/AQABAf8A/wABAAEBAQD/AAEAAQEBAf8AAQABAQH/AAABAAEBAAAAAAEAAQEBAAAAAQABAf8BAAABAAEB//8BAAEAAQEB/wEAAQABAQEA/wEBAAEB//8AAQEAAQEAAAABAQABAQEAAAEBAAEBAAEAAQEAAQEA/wEBAQABAf8AAQEBAAEBAQABAQEAAQH//////wEBAQH/////AQEB/wH///8BAQEBAf///wEBAf//Af//AQEBAf8B//8BAQH/AQH//wEBAQEBAf//AQEBAP8AAP8BAQH/AAAA/wEBAQEAAAD/AQEBAAEAAP8BAQH///8B/wEBAQH//wH/AQEB/wH/Af8BAQEBAf8B/wEBAQAAAAH/AQEB//8BAf8BAQEB/wEB/wEBAf8BAQH/AQEBAQEBAf8BAQEAAP//AAEBAf8AAP8AAQEBAAEA/wABAQEA/wH/AAEBAQAAAf8AAQEBAP//AAABAQH//wAAAAEBAQAAAAAAAQEBAQEAAAABAQEA/wEAAAEBAQEAAQAAAQEBAAEBAAABAQH//wABAAEBAQEAAAEAAQEB/////wEBAQEB////AQEBAf8B//8BAQEBAQH//wEBAQH//wH/AQEBAQH/Af8BAQEB/wEB/wEBAQEBAQH/AQEBAQD/AAABAQEB/wAAAAEBAQEBAAAAAQEBAf///wEBAQEBAf//AQEBAQH/Af8BAQEBAQEB/wEBAQEBAAAAAQEBAQH//wEBAQEBAQH/AQEBAQEB/wEBAQEBAQEBAQEBAQEBAQ=="); +export const ksigns_iq2xs = /* 128B */ D("AIGCA4QFBoeICQqLDI2OD5AREpMUlZYXGJmaG5wdHp+gISKjJKWmJyipqiusLS6vMLGyM7Q1Nre4OTq7PL2+P8BBQsNExcZHSMnKS8xNTs9Q0dJT1FVW19hZWttc3d5fYOHiY+RlZufoaWrrbO3ub/BxcvN09fZ3ePn6e/x9fv8="); +export const kmask_iq2xs = /* 8B */ D("AQIECBAgQIA="); +export const kvalues_iq4nl = /* 16B */ D("gZitv8/d6vYBDRkmNUVZcQ=="); diff --git a/apps/q/forge/gguf-forge-kstream.mjs b/apps/q/forge/gguf-forge-kstream.mjs new file mode 100644 index 0000000000000000000000000000000000000000..1efbb6930421e77d906b407744fbbed70c9182b5 --- /dev/null +++ b/apps/q/forge/gguf-forge-kstream.mjs @@ -0,0 +1,111 @@ +// gguf-forge-kstream.mjs — load an LLM 100% from its sealed .holo, by κ, native to the substrate. +// +// The vision's load path: a model is a content-addressed archive of per-tensor κ-bodies (sealHolo / +// writeHolo). This opens it WITHOUT re-forging the monolithic GGUF — every tensor is fetched BY ITS κ +// and re-derived before use (L5), with a global κ-cache (a block seen once is reused, O(1) warm). The +// embedded gguf.header gives the plan (arch + hparams + tensor infos + tokenizer); meta.order gives +// name→κ. Output is forge-compatible: { plan, store } feed synthesizeGraph + forward unchanged, and +// (in the browser) the SAME .holo streams over HTTP-Range / SW κ-route / IPFS — serverless, verified. +// +// readHolo (whole bytes, Node/in-memory) and openGgufHoloStream (rangeReader, browser cold-load) share +// one shape. The per-body L5 lives in holo-archive's store; we add the name→κ plan + a verify-once +// global κ-cache so a forward doesn't re-hash every matvec. + +import { readHolo, openHoloStream } from "./holo-archive.mjs"; +import { parseGgufHeader } from "../qvac-ingest.mjs"; +import { GGML_TYPE_NAME } from "./gguf-forge.mjs"; + +// build the forge-compatible plan (name→κ + dims/type) from the embedded gguf header + meta.order +function planFrom(headerBytes, order) { + const hd = parseGgufHeader(headerBytes); + const byName = new Map(order.map((o) => [o.name, o.kappa])); + const tensors = hd.tensors.map((t) => ({ + name: t.name, dims: t.dims, type: t.ggmlType, typeName: GGML_TYPE_NAME[t.ggmlType] || String(t.ggmlType), + kappa: "sha256:" + byName.get(t.name), // identity = content (from the κ-body directory) + })); + return { arch: hd.meta["general.architecture"], meta: hd.meta, tensors }; +} + +// the head region = [0, firstBodyOffset): the 64-byte head + section table + Extension (baked gguf +// header + tokenizer) + Metadata + the weights directory — everything BEFORE the first weight body. +// Found cheaply (head + section table + the weights count, ~200B) without reading the 6MB header. +async function firstBodyOffset(range) { + const head = await range(0, 64), hdv = new DataView(head.buffer, head.byteOffset, head.byteLength); + const sc = hdv.getUint16(8, true); + const tbl = await range(10, sc * 17), tdv = new DataView(tbl.buffer, tbl.byteOffset, tbl.byteLength); + let wOff = null; + for (let i = 0, p = 0; i < sc; i++, p += 17) if (tbl[p] === 3) wOff = Number(tdv.getBigUint64(p + 1, true)); // Weights=3 + const cntB = await range(wOff, 4); const count = new DataView(cntB.buffer, cntB.byteOffset, cntB.byteLength).getUint32(0, true); + return wOff + 4 + count * 48; // [count u32] + count×[κ32 off8 len8] +} + +// Resolve the head blob from the persistent store (0 transport on warm) or fetch+persist it (cold). +// Keyed by its own content κ (headκ = sha256(head)) → store.peek L5-verifies it. A pointer maps the +// archive identity to headκ: the .holo footer κ (rootKappa, the LINK — content-addressed) and/or a +// local url hint (a non-identity accelerator for open-by-URL). Returns the head bytes + warm flag. +async function resolveHeadBlob(range, persist, { rootKappa, urlHint }) { + const footHex = rootKappa ? String(rootKappa).split(":").pop() : null; + const urlKey = urlHint ? await persist.hash(new TextEncoder().encode(urlHint)) : null; + let headK = null; + if (footHex) headK = await persist.getHint("head_" + footHex); // content-addressed pointer (the link) + if (!headK && urlKey) headK = await persist.getHint("url_" + urlKey); // local url accelerator + if (headK) { const blob = await persist.peek(headK); if (blob) return { blob, warm: true }; } + // cold: one fetch of the contiguous head region, content-address + persist, write the pointers + const fbo = await firstBodyOffset(range); + const blob = await range(0, fbo); + const hk = await persist.hash(blob); + await persist.putBody(hk, blob); + if (footHex) await persist.putHint("head_" + footHex, hk); + if (urlKey) await persist.putHint("url_" + urlKey, hk); + return { blob, warm: false }; +} + +// a verify-once global κ-cache over a base store (the holo store already L5-verifies on first get). +// Subsequent gets are O(1) and offline. Shared across loads when the same Map is passed in. +function cachedStore(base, cache = new Map()) { + return { get: (hex) => { let b = cache.get(hex); if (b === undefined) { b = base.get(hex); cache.set(hex, b); } return b; }, has: (hex) => base.has(hex), cache }; +} + +// ── whole-bytes (Node / fully-in-memory): open a .holo and load by κ with L5 + cache ── +export function openGgufHolo(bytes, { cache } = {}) { + const h = readHolo(bytes); // footer L5 + per-body L5 store + meta.order + return { plan: planFrom(h.headerBytes, h.meta.order), store: cachedStore(h.store, cache), rootHolo: h.footer, meta: h.meta, headerBytes: h.headerBytes }; +} + +// ── streaming (browser cold-load): open a .holo over a Range reader; bodies fetched on demand by κ ── +// rangeReader(off,len)->Promise (HTTP-Range, SW κ-route, or IPFS). store.get is async. +// persist (optional, browser): a makeKappaStore() instance — OPFS-first, 0-network warm, survives +// reload + offline. Injected (not imported) so this module stays Node-safe. The in-mem cache sits in +// front for same-session O(1); persist sits behind for cross-session O(1). getBody L5-verifies every +// transport body, so a persisted body is trusted only after re-derivation. +export async function openGgufHoloStream(rangeReader, { cache = new Map(), persist = null, rootKappa = null, urlHint = null } = {}) { + // HEAD PERSISTENCE: when a persistent store + an identity hint are present, serve the whole head + // region from a content-addressed blob (0 transport on warm) so the ~6MB gguf header/tokenizer is + // offline too — not just the weight bodies. effRange serves head reads from the blob, bodies via wire. + let effRange = rangeReader, headWarm = null; + if (persist && persist.putBody && (rootKappa || urlHint)) { + const h = await resolveHeadBlob(rangeReader, persist, { rootKappa, urlHint }); + headWarm = h.warm; + effRange = (off, len) => (off + len <= h.blob.length) ? Promise.resolve(h.blob.subarray(off, off + len)) : rangeReader(off, len); + } + const s = await openHoloStream(effRange); // header + directory up front; bodies on demand + const fetchBody = (hex) => s.getBody(hex); // Range + per-block L5 (refuses mismatch) + const base = persist ? (hex) => persist.get(hex, () => fetchBody(hex)) : fetchBody; + const aget = async (hex) => { let b = cache.get(hex); if (b === undefined) { b = await base(hex); cache.set(hex, b); } return b; }; + return { plan: planFrom(s.headerBytes, s.meta.order), store: { get: aget, cache }, meta: s.meta, headerBytes: s.headerBytes, persist, headWarm }; +} + +// UNIFIED-PACK adapter: build the SAME { plan, store, headerBytes, headWarm } the brain consumes, but sourced from a +// pack model view (openModelPack/holo-q-pack-provider) instead of a per-model rangeReader. The view already carries the +// baked gguf header (view.headerBytes) + name→κ (view.order) + a per-κ L5 getBody — exactly planFrom + a verify-once +// store. So createHoloBrain runs over the unified pack with ZERO forge/forward changes: same plan, same bodies, same κ. +export function ggufStreamFromPackModel(view, { cache = new Map(), persist = null } = {}) { + const plan = planFrom(view.headerBytes, view.order); + const fetchBody = (hex) => view.getBody(hex); // pack getBody = Range + per-block L5 + const base = persist ? (hex) => persist.get(hex, () => fetchBody(hex)) : fetchBody; + const aget = async (hex) => { let b = cache.get(hex); if (b === undefined) { b = await base(hex); cache.set(hex, b); } return b; }; + return { plan, store: { get: aget, cache }, meta: { order: view.order }, headerBytes: view.headerBytes, persist, headWarm: true }; +} + +// the exec loader seam: forward(plan, graph, store, tokens, { load }) — verify-once (store already L5). +export const kstreamLoad = (store, kappaRef) => store.get(String(kappaRef).split(":").pop()); diff --git a/apps/q/forge/gguf-forge.mjs b/apps/q/forge/gguf-forge.mjs new file mode 100644 index 0000000000000000000000000000000000000000..f55a06d306f90b4778b3549352c30d17b3177585 --- /dev/null +++ b/apps/q/forge/gguf-forge.mjs @@ -0,0 +1,226 @@ +// GGUF Forge — split a GGUF into κ-addressable objects, ORIGINAL BYTES PRESERVED. +// +// This is the "compile into κ-addressable objects" core. Unlike qvac-ingest's +// makeDiskFetcher (which dequantizes then RE-QUANTIZES Q4_K/Q6_K into the engine's +// lossy 4-bit format — a fidelity violation for "strictly adhere to original +// code"), the forge stores each tensor's EXACT on-disk ggml quant bytes as its own +// content-addressed κ-object. The κ of a tensor IS the hash of the bytes llama.cpp +// would mmap, so fidelity is byte-literal and L5-verifiable on every load. +// +// Output: +// • blocks: Map the κ-store (one object per tensor, by content) +// • plan: a JSON manifest of arch + hparams + per-tensor {dims, type, κ, sri} +// • rootKappa: did:holo:sha256 of the canonical plan — the model's identity (L1) +// +// Laws: L1 identity=content, L2 one object per tensor (dedup by κ), L5 verify by +// re-derivation. holospaces github.com/Hologram-Technologies/holospaces. + +import { parseGgufHeader } from "../qvac-ingest.mjs"; +import { sha256hex, sriOf, kappa, didHolo, jcs } from "../../../usr/lib/holo/holo-uor.mjs"; + +// ── ggml type → (block elements, block bytes). Source: ggml.c type_traits table +// (blck_size, type_size). Covers the standard set a Qx_K_M GGUF uses. Exotic +// IQ*/TBQ/MXFP4 types throw until their kernels land (honest, not silent). ── +const QK = 32, QK_K = 256; +const TYPE_BLOCK = { + 0: [1, 4], // F32 + 1: [1, 2], // F16 + 2: [QK, 18], // Q4_0 + 3: [QK, 20], // Q4_1 + 6: [QK, 22], // Q5_0 + 7: [QK, 24], // Q5_1 + 8: [QK, 34], // Q8_0 + 9: [QK, 36], // Q8_1 + 10: [QK_K, 84], // Q2_K + 11: [QK_K, 110], // Q3_K + 12: [QK_K, 144], // Q4_K + 13: [QK_K, 176], // Q5_K + 14: [QK_K, 210], // Q6_K + 15: [QK_K, 292], // Q8_K (intermediate; not normally stored) + 16: [QK_K, 66], // IQ2_XXS + 17: [QK_K, 74], // IQ2_XS + 18: [QK_K, 98], // IQ3_XXS + 19: [QK_K, 50], // IQ1_S + 20: [QK, 18], // IQ4_NL (block = 32 elems) + 21: [QK_K, 110], // IQ3_S + 22: [QK_K, 82], // IQ2_S + 23: [QK_K, 136], // IQ4_XS + 29: [QK_K, 56], // IQ1_M + 30: [1, 2], // BF16 + 35: [QK_K, 66], // TQ2_0 (BitNet ternary, 2.0625 bpw) +}; +export const GGML_TYPE_NAME = { + 0: "F32", 1: "F16", 2: "Q4_0", 3: "Q4_1", 6: "Q5_0", 7: "Q5_1", 8: "Q8_0", 9: "Q8_1", + 10: "Q2_K", 11: "Q3_K", 12: "Q4_K", 13: "Q5_K", 14: "Q6_K", 15: "Q8_K", + 16: "IQ2_XXS", 17: "IQ2_XS", 18: "IQ3_XXS", 19: "IQ1_S", 20: "IQ4_NL", + 21: "IQ3_S", 22: "IQ2_S", 23: "IQ4_XS", 29: "IQ1_M", 30: "BF16", 35: "TQ2_0", + // TurboQuant / PolarQuant KV-cache quant (not weight types — KvMemory plane) + 42: "TBQ3_0", 43: "TBQ4_0", 44: "TBQ3_0_64", 45: "TBQ4_0_64", + 46: "PQ3_0", 47: "PQ3_0_64", 48: "PQ4_0", 49: "PQ4_0_64", +}; + +// ggml_nbytes for a contiguous tensor of `numElements` of `ggmlType` (the exact +// span llama.cpp loads — excludes inter-tensor alignment padding). +export function ggmlNBytes(ggmlType, numElements) { + const bt = TYPE_BLOCK[ggmlType]; + if (!bt) throw new Error(`gguf-forge: unsupported ggml type ${ggmlType} (no block size yet)`); + const [blk, bytes] = bt; + if (numElements % blk !== 0) throw new Error(`gguf-forge: ${numElements} not divisible by block ${blk} (type ${ggmlType})`); + return (numElements / blk) * bytes; +} + +const numElements = (dims) => dims.reduce((a, b) => a * b, 1); + +// Forge a full in-memory GGUF (Uint8Array) into κ-objects. For multi-GB models the +// streaming variant (forgeGgufStream, below) does the same with a Range reader so +// no tensor beyond the one being hashed is ever resident. +export function forgeGguf(bytes) { + const { version, dataOffset, tensors, meta } = parseGgufHeader(bytes); + const blocks = new Map(); + const planTensors = []; + for (const t of tensors) { + const n = numElements(t.dims); + const nbytes = ggmlNBytes(t.ggmlType, n); + const start = dataOffset + t.offset; + const end = start + nbytes; + if (end > bytes.byteLength) throw new Error(`gguf-forge: tensor ${t.name} runs past EOF (${end} > ${bytes.byteLength})`); + // EXACT bytes — no copy semantics changed, no re-quant. This subarray IS the κ-object. + const blob = bytes.subarray(start, end); + const hex = sha256hex(blob); + if (!blocks.has(hex)) blocks.set(hex, blob.slice()); // own the bytes; L2 dedup by content + planTensors.push({ + name: t.name, dims: t.dims, type: t.ggmlType, typeName: GGML_TYPE_NAME[t.ggmlType] || String(t.ggmlType), + nbytes, kappa: kappa("sha256", hex), sri: sriOf(blob), + }); + } + const arch = meta["general.architecture"] || "unknown"; + // The plan is the sealed manifest: arch + all scalar metadata + per-tensor κ refs. + // No raw weight bytes here — only content addresses. Hashing it gives model identity. + const plan = { + format: "gguf-forge/1", + arch, + ggufVersion: version, + meta, // scalar hparams + tokenizer ids captured by parseGgufHeader + tensors: planTensors, // ordered as in the GGUF directory + }; + const planHex = sha256hex(jcs(plan)); + const rootKappa = didHolo("sha256", planHex); + return { version, dataOffset, arch, meta, tensors: planTensors, blocks, plan, rootKappa }; +} + +// Streaming forge: same κ-objects from a Range reader (start,len)->Promise, +// for models too large to hold whole. Reads the header once, then each tensor span once. +export async function forgeGgufStream(readRange, { headerBytes }) { + const { version, dataOffset, tensors, meta } = parseGgufHeader(headerBytes); + const blocks = new Map(); + const planTensors = []; + for (const t of tensors) { + const n = numElements(t.dims); + const nbytes = ggmlNBytes(t.ggmlType, n); + const blob = await readRange(dataOffset + t.offset, nbytes); + if (blob.byteLength !== nbytes) throw new Error(`gguf-forge: short read for ${t.name}`); + const hex = sha256hex(blob); + if (!blocks.has(hex)) blocks.set(hex, blob); + planTensors.push({ + name: t.name, dims: t.dims, type: t.ggmlType, typeName: GGML_TYPE_NAME[t.ggmlType] || String(t.ggmlType), + nbytes, kappa: kappa("sha256", hex), sri: sriOf(blob), + }); + } + const arch = meta["general.architecture"] || "unknown"; + const plan = { format: "gguf-forge/1", arch, ggufVersion: version, meta, tensors: planTensors }; + const rootKappa = didHolo("sha256", sha256hex(jcs(plan))); + return { version, dataOffset, arch, meta, tensors: planTensors, blocks, plan, rootKappa }; +} + +// Disk-backed forge for models too large to hold in RAM (e.g. GLM-5.2, 254-467 GB). +// Reads each tensor span ONCE to derive its κ, then RELEASES the bytes — peak memory is +// the largest single tensor, not the model. Emits no `blocks` Map; instead a +// `dir: { hex -> { fileOffset, len } }` so a disk store can range-read any κ-block later, +// plus a per-expert `expertDir` (same shape as buildExpertDirectory) computed in the same +// pass. Same plan/κ as forgeGgufStream (verified by gguf-forge-disk.test). +const EXPERT_RE_SCAN = /\.ffn_(gate|up|down)_exps\.weight$/; + +// Shared scan core. `entries` = [{ name, dims, ggmlType, read(off,len)->Promise, loc(off,len)->dirEntry }]. +// `read(0,nbytes)` returns the whole tensor (read ONCE); slices come from that blob (no re-read). +// `loc` builds the κ→location directory entry ({fileOffset,len} single-file, or {part,fileOffset,len} +// multi-part). Same κ/plan/rootKappa regardless of where the bytes physically live. +async function scanCore(entries, meta, version) { + const planTensors = [], dir = Object.create(null), expertTensors = Object.create(null); + for (const t of entries) { + const nbytes = ggmlNBytes(t.ggmlType, numElements(t.dims)); + const blob = await t.read(0, nbytes); + if (blob.byteLength !== nbytes) throw new Error(`gguf-forge: short read for ${t.name}`); + const hex = sha256hex(blob); + if (!(hex in dir)) dir[hex] = t.loc(0, nbytes); + planTensors.push({ + name: t.name, dims: t.dims, type: t.ggmlType, typeName: GGML_TYPE_NAME[t.ggmlType] || String(t.ggmlType), + nbytes, kappa: kappa("sha256", hex), sri: sriOf(blob), + }); + if (EXPERT_RE_SCAN.test(t.name) && t.dims.length >= 3) { + const [K, N, E] = t.dims, stride = ggmlNBytes(t.ggmlType, K * N), experts = []; + for (let e = 0; e < E; e++) { + const sh = sha256hex(blob.subarray(e * stride, (e + 1) * stride)); + if (!(sh in dir)) dir[sh] = t.loc(e * stride, stride); + experts.push({ e, kappa: kappa("sha256", sh) }); + } + expertTensors[t.name] = { type: t.ggmlType, typeName: GGML_TYPE_NAME[t.ggmlType] || String(t.ggmlType), dims: [K, N, E], stride, nExpert: E, experts }; + } + // blob released here — never retained + } + const arch = meta["general.architecture"] || "unknown"; + const plan = { format: "gguf-forge/1", arch, ggufVersion: version, meta, tensors: planTensors }; + const rootKappa = didHolo("sha256", sha256hex(jcs(plan))); + const expertDir = { format: "gguf-forge-expert-dir/1", model: rootKappa, tensors: expertTensors }; + return { version, arch, meta, tensors: planTensors, plan, rootKappa, dir, expertDir }; +} + +// Disk-backed forge for models too large to hold in RAM (e.g. GLM-5.2, 254-467 GB). +// Reads each tensor span ONCE to derive its κ, then RELEASES the bytes — peak memory is +// the largest single tensor, not the model. Emits no `blocks` Map; instead a +// `dir: { hex -> { fileOffset, len } }` so a disk store can range-read any κ-block later, +// plus a per-expert `expertDir` (same shape as buildExpertDirectory). Same κ as forgeGgufStream. +export async function forgeGgufScan(readRange, { headerBytes }) { + const { version, dataOffset, tensors, meta } = parseGgufHeader(headerBytes); + const entries = tensors.map((t) => { + const base = dataOffset + t.offset; + return { name: t.name, dims: t.dims, ggmlType: t.ggmlType, read: (o, l) => readRange(base + o, l), loc: (o, l) => ({ fileOffset: base + o, len: l }) }; + }); + return { dataOffset, ...(await scanCore(entries, meta, version)) }; +} + +// Multi-part forge (GLM-5.2 ships as a 7-part split GGUF). `multipart` = openGgufMultipart() +// result: { meta, version, tensors:[{name,dims,ggmlType,part,fileOffset}], parts:[{readRange}] }. +// Each tensor is read from its OWNING part; dir entries carry {part,fileOffset,len}. Produces the +// SAME κ/rootKappa/.holo as forging the un-split model (verified by gguf-multipart.test). +export async function forgeGgufScanParts(multipart) { + const entries = multipart.tensors.map((t) => ({ + name: t.name, dims: t.dims, ggmlType: t.ggmlType, + read: (o, l) => multipart.parts[t.part].readRange(t.fileOffset + o, l), + loc: (o, l) => ({ part: t.part, fileOffset: t.fileOffset + o, len: l }), + })); + return scanCore(entries, multipart.meta, multipart.version); +} + +// L5: load a tensor's bytes by κ, RE-DERIVE the hash, refuse on mismatch. +// `store` is any { get(hex) -> Uint8Array | undefined }. Accepts "sha256:", +// "did:holo:sha256:", or a bare hex. +export function loadByKappa(store, kappaRef) { + const hex = String(kappaRef).split(":").pop(); + const bytes = store.get(hex); + if (!bytes) throw new Error(`gguf-forge: κ not found: ${kappaRef}`); + const got = sha256hex(bytes); + if (got !== hex) throw new Error(`gguf-forge: L5 REFUSE — ${kappaRef} re-derives to sha256:${got}`); + return bytes; +} + +// Re-derive the whole plan's identity from a store: verify every tensor block, then +// re-hash the plan. Returns the recomputed rootKappa (throws on any tamper). +export function verifyPlan(store, plan) { + for (const t of plan.tensors) loadByKappa(store, t.kappa); // L5 per block + return didHolo("sha256", sha256hex(jcs(plan))); +} + +// Map-backed κ-store helper for tests/Node. +export function mapStore(blocks) { + return { get: (hex) => blocks.get(hex), has: (hex) => blocks.has(hex), put: (b) => { const h = sha256hex(b); blocks.set(h, b); return h; } }; +} diff --git a/apps/q/forge/gpu/holo-files.mjs b/apps/q/forge/gpu/holo-files.mjs new file mode 100644 index 0000000000000000000000000000000000000000..3d45997d3a1fbcd835cf95067613ce8ddc918fb2 --- /dev/null +++ b/apps/q/forge/gpu/holo-files.mjs @@ -0,0 +1,51 @@ +// holo-files.mjs — serve a file-bundle .holo (seal-files-holo.mjs) by κ in the browser: HTTP-Range fetch +// each file body by its content address, per-block SHA-256 L5 verify, OPFS cache (instant + offline on the +// 2nd load). Reconstructs files as bytes / Blob object-URLs so an existing runtime (onnxruntime-web, +// kokoro-js, transformers.js) can consume a κ-addressable model with no flat download and no trust in transport. +// openHoloFiles(url) → { meta, files:[{name,kappa,len}], getFile(name)->Uint8Array, objectURL(name)->url, stats } +const MAGIC = [0x48, 0x4f, 0x4c, 0x4f]; +const hexOf = (b) => { let s = ""; for (const x of b) s += x.toString(16).padStart(2, "0"); return s; }; +const sha256hex = async (buf) => hexOf(new Uint8Array(await crypto.subtle.digest("SHA-256", buf))); +async function opfsGet(k) { try { const d = await (await navigator.storage.getDirectory()).getDirectoryHandle("holo-kappa", { create: true }); return new Uint8Array(await (await (await d.getFileHandle(k)).getFile()).arrayBuffer()); } catch { return null; } } +async function opfsPut(k, b) { try { const d = await (await navigator.storage.getDirectory()).getDirectoryHandle("holo-kappa", { create: true }); const w = await (await d.getFileHandle(k, { create: true })).createWritable(); await w.write(b); await w.close(); return true; } catch { return false; } } + +export async function openHoloFiles(url, { useOpfs = true, release = "" } = {}) { + const stats = { ranges: 0, bytesFetched: 0, verifies: 0, opfsHits: 0, opfsWrites: 0, releaseRoute: false }; + let whole = null, activeUrl = url, triedRelease = false; + const rd = async (off, len) => { + if (whole) return whole.subarray(off, off + len); + stats.ranges++; + let r = null; try { r = await fetch(activeUrl, { headers: { Range: `bytes=${off}-${off + len - 1}` } }); } catch (e) { r = null; } + if ((!r || !r.ok) && release && activeUrl !== release && !triedRelease) { // big bundle lives as a GitHub Release asset + triedRelease = true; activeUrl = release; stats.releaseRoute = true; + try { r = await fetch(activeUrl, { headers: { Range: `bytes=${off}-${off + len - 1}` } }); } catch (e) { r = null; } + } + if (!r || !r.ok) throw new Error("holo fetch failed (" + (r ? r.status : "network") + "): " + activeUrl); + const u = new Uint8Array(await r.arrayBuffer()); stats.bytesFetched += u.length; + if (r.status === 206) return u; + if (u.length > len) { whole = u; return whole.subarray(off, off + len); } + return u; + }; + const head = await rd(0, 64), hdv = new DataView(head.buffer, head.byteOffset, head.byteLength); + for (let i = 0; i < 4; i++) if (head[i] !== MAGIC[i]) throw new Error("not a .holo"); + const sc = hdv.getUint16(8, true); + const tbl = await rd(10, sc * 17), tdv = new DataView(tbl.buffer, tbl.byteOffset, tbl.byteLength), sections = {}; + for (let i = 0, p = 0; i < sc; i++, p += 17) sections[tbl[p]] = { off: Number(tdv.getBigUint64(p + 1, true)), len: Number(tdv.getBigUint64(p + 9, true)) }; + const m = sections[8], meta = JSON.parse(new TextDecoder().decode(await rd(m.off, m.len))); + const w = sections[3], cntB = await rd(w.off, 4), count = new DataView(cntB.buffer, cntB.byteOffset, cntB.byteLength).getUint32(0, true); + const dirB = await rd(w.off + 4, count * 48), ddv = new DataView(dirB.buffer, dirB.byteOffset, dirB.byteLength), dir = new Map(); + for (let i = 0, p = 0; i < count; i++, p += 48) dir.set(hexOf(dirB.subarray(p, p + 32)), { off: Number(ddv.getBigUint64(p + 32, true)), len: Number(ddv.getBigUint64(p + 40, true)) }); + const byName = new Map(meta.files.map((f) => [f.name, f])), cache = new Map(); + + async function bodyByKappa(hex) { + if (useOpfs) { const c = await opfsGet(hex); if (c) { stats.verifies++; if (await sha256hex(c) === hex) { stats.opfsHits++; return c; } } } + const d = dir.get(hex); if (!d) throw new Error("κ not in holo: " + hex); + const b = await rd(d.off, d.len); + stats.verifies++; if (await sha256hex(b) !== hex) throw new Error("L5 REFUSE " + hex); + if (useOpfs && await opfsPut(hex, b)) stats.opfsWrites++; + return b; + } + async function getFile(name) { if (cache.has(name)) return cache.get(name); const f = byName.get(name); if (!f) throw new Error("no file " + name); const b = await bodyByKappa(f.kappa); cache.set(name, b); return b; } + async function objectURL(name, mime = "application/octet-stream") { return URL.createObjectURL(new Blob([await getFile(name)], { type: mime })); } + return { meta, files: meta.files, getFile, objectURL, bodyByKappa, stats }; +} diff --git a/apps/q/forge/gpu/holo-model-pack.mjs b/apps/q/forge/gpu/holo-model-pack.mjs new file mode 100644 index 0000000000000000000000000000000000000000..c1b8a7991d588619eda6e3961f292ea633d80d15 --- /dev/null +++ b/apps/q/forge/gpu/holo-model-pack.mjs @@ -0,0 +1,47 @@ +// holo-model-pack.mjs — open the unified q-models.holo ONCE and hand each faculty a per-model view shaped EXACTLY +// like an openHoloStream result, so every loader (parakeet ear's encoderStream/jointStream, loadJointFromHolo, the +// turn-detector binding, moonshine/kokoro) accepts pack.model(id) with ZERO code change. Same body κ ⇒ same bytes +// ⇒ identical results. One open, one OPFS κ-store shared across faculties; faculties Range-fetch only their bodies. +// +// openModelPack({ rangeReader, persist }) → { manifest, getBody, dir, model(id) } +// model(id) → { meta:{order}, order, files, headerBytes, getBody, getBodySlice, dir, bodyLen, names } +// rangeReader(off,len)->Uint8Array: production wraps streamHolo/openHoloFiles over the pack URL (Range → release → +// κ-route → OPFS); node wraps a local file. `persist` (makeKappaStore) optional for the shared OPFS warm. +import { openHoloStream } from "../holo-archive.mjs"; + +const b64dec = (b64) => (typeof Buffer !== "undefined" ? new Uint8Array(Buffer.from(b64, "base64")) : Uint8Array.from(atob(b64), (c) => c.charCodeAt(0))); + +export async function openModelPack({ rangeReader, persist = null } = {}) { + if (!rangeReader) throw new Error("openModelPack needs a rangeReader(off,len)->Uint8Array"); + const s = await openHoloStream(rangeReader); // one open over the whole pack + const manifest = JSON.parse(new TextDecoder().decode(s.headerBytes)); + if (!manifest || manifest.format !== "holo-pack/1") throw new Error("not a q-models pack"); + + function model(id) { + const m = manifest.models[id]; + if (!m) throw new Error("model not in pack: " + id); + // per-model baked header: OLD packs inline it as base64 (m.ext, available sync); NEW split-manifest packs store it + // as a κ-BODY (m.extKappa) fetched lazily — so OPENING the pack reads ~100 KB not ~29 MB of headers. A loader that + // needs headerBytes calls await ensureHeader() first (no-op when inline). Both formats supported, no faculty rewrite. + let _header = m.ext ? b64dec(m.ext) : null; + // SHAPE == openHoloStream's return (order/getBody/headerBytes) — faculties don't change a line. + return { + id, kind: m.kind, tier: m.tier, + meta: Object.assign({}, m.meta, { order: m.order, arch: (m.meta && m.meta.arch) || id }), + order: m.order, + files: m.files || [], + get headerBytes() { return _header; }, + ensureHeader: async () => { if (_header == null && m.extKappa) _header = await s.getBody(m.extKappa); return _header; }, + getBody: s.getBody, // SHARED over the pack (same dir, same OPFS) + getBodySlice: s.getBodySlice, + dir: s.dir, + bodyLen: s.bodyLen, + names: m.order.map((o) => o.name), + fileBody: async (name) => { const f = (m.files || []).find((x) => x.name === name) || m.order.find((x) => x.name === name); if (!f) throw new Error("no file " + name + " in " + id); return s.getBody(f.kappa); }, + }; + } + + return { manifest, getBody: s.getBody, dir: s.dir, models: () => Object.keys(manifest.models), model }; +} + +export default openModelPack; diff --git a/apps/q/forge/gpu/holo-moonshine-asr.mjs b/apps/q/forge/gpu/holo-moonshine-asr.mjs new file mode 100644 index 0000000000000000000000000000000000000000..6e7008e7faef083ccfa817523a417947f3e1deeb --- /dev/null +++ b/apps/q/forge/gpu/holo-moonshine-asr.mjs @@ -0,0 +1,219 @@ +// holo-moonshine-asr.mjs — Q's κ-native WebGPU ear, Moonshine edition. Same interface as holo-whisper-asr +// (createMoonshineASR(url).transcribe(pcm16k) → {text, ids, gpuMs, ms}) but Moonshine arch: RAW audio (NO +// mel) → conv stem → RoPE encoder → KV-cache RoPE/cross decoder → gated SwiGLU → tied head → SPM detok. +// Weights 100% κ-streamed + L5-verified from the .holo. Decode is incremental (KV-cache) for live latency. +import { streamHolo } from "./holo-whisper-stream.mjs"; + +// Llama SentencePiece detok: Replace(▁→space) → ByteFallback(<0xNN>→byte) → Fuse → Strip one leading space. +function makeDetok(tokJson) { + const vocab = tokJson.model.vocab, inv = []; for (const [tok, id] of Object.entries(vocab)) inv[id] = tok; + const tenc = new TextEncoder(), tdec = new TextDecoder(); + return (ids) => { + const bytes = []; + for (const id of ids) { if (id <= 2) continue; const piece = inv[id]; if (piece === undefined) continue; + const m = /^<0x([0-9A-Fa-f]{2})>$/.exec(piece); + if (m) bytes.push(parseInt(m[1], 16)); + else { const u = tenc.encode(piece.replace(/▁/g, " ")); for (const b of u) bytes.push(b); } } + let t = tdec.decode(new Uint8Array(bytes)); return t.startsWith(" ") ? t.slice(1) : t; + }; +} + +const CONV1F = `@group(0)@binding(0)varinp:array;@group(0)@binding(1)varw:array;@group(0)@binding(2)varb:array;@group(0)@binding(3)varoutp:array;@group(0)@binding(4)varp:vec4;@group(0)@binding(5)varq:vec4; +@compute @workgroup_size(64)fn main(@builtin(global_invocation_id)g:vec3){let idx=g.x;let IC=p.x;let OC=p.y;let K=p.z;let OL=p.w;let L=q.x;let stride=q.y;if(idx>=OC*OL){return;} +let oc=idx/OL;let ol=idx%OL;var s=select(0.0,b[oc],q.w==1u);let wb=oc*IC*K;let st=ol*stride;for(var ic=0u;icc:array;@group(0)@binding(1)varp:vec4;@compute @workgroup_size(64)fn main(@builtin(global_invocation_id)g:vec3){if(g.x>=p.x){return;}c[g.x]=tanh(c[g.x]);}`; +const GELU = `@group(0)@binding(0)varc:array;@group(0)@binding(1)varp:vec4; +fn erf(x:f32)->f32{let s=sign(x);let ax=abs(x);let t=1.0/(1.0+0.3275911*ax);let y=1.0-(((((1.061405429*t-1.453152027)*t)+1.421413741)*t-0.284496736)*t+0.254829592)*t*exp(-ax*ax);return s*y;} +@compute @workgroup_size(64)fn main(@builtin(global_invocation_id)g:vec3){if(g.x>=p.x){return;}let x=c[g.x];c[g.x]=0.5*x*(1.0+erf(x*0.7071067811865476));}`; +const GNSTATS = `@group(0)@binding(0)varx:array;@group(0)@binding(1)varo:array;@group(0)@binding(2)varp:vec4; +var ss:array;var sq:array; +@compute @workgroup_size(256)fn main(@builtin(local_invocation_id)l:vec3){let t=l.x;let N=p.x;var a=0.0;var b=0.0;for(var i=t;ix:array;@group(0)@binding(1)varst:array;@group(0)@binding(2)varw:array;@group(0)@binding(3)varb:array;@group(0)@binding(4)varp:vec4; +@compute @workgroup_size(64)fn main(@builtin(global_invocation_id)g:vec3){let idx=g.x;let C=p.x;let T=p.y;if(idx>=C*T){return;}x[idx]=(x[idx]-st[0])*st[1]*w[idx/T]+b[idx/T];}`; +const TRANSP = `@group(0)@binding(0)varinp:array;@group(0)@binding(1)varo:array;@group(0)@binding(2)varp:vec4; +@compute @workgroup_size(64)fn main(@builtin(global_invocation_id)g:vec3){let idx=g.x;let F=p.x;let S=p.y;if(idx>=F*S){return;}let f=idx/S;let c=idx%S;o[idx]=inp[c*F+f];}`; +const TILED = `@group(0)@binding(0)varA:array;@group(0)@binding(1)varB:array;@group(0)@binding(2)varbias:array;@group(0)@binding(3)varY:array;@group(0)@binding(4)varp:vec4; +var As:array;var Bs:array; +@compute @workgroup_size(16,16)fn main(@builtin(workgroup_id)wg:vec3,@builtin(local_invocation_id)l:vec3){ +let rows=p.x;let inD=p.y;let outD=p.z;let tx=l.x;let ty=l.y;let r=wg.y*16u+ty;let o=wg.x*16u+tx;var acc=0.0;let nT=(inD+15u)/16u; +for(var kt=0u;ktx:array;@group(0)@binding(1)varw:array;@group(0)@binding(2)varb:array;@group(0)@binding(3)vary:array;@group(0)@binding(4)varp:vec4; +@compute @workgroup_size(64)fn main(@builtin(global_invocation_id)g:vec3){let row=g.x;let n=p.x;let S=p.y;if(row>=n){return;}let base=row*S;var mean=0.0;for(var i=0u;iv:array;@group(0)@binding(1)varp:vec4;@group(0)@binding(2)varpb:vec4; +@compute @workgroup_size(64)fn main(@builtin(global_invocation_id)g:vec3){let idx=g.x;let n=p.x;let S=p.y;let H=p.z;let hd=p.w;let half=16u;let rot=32u;if(idx>=n*H*half){return;} +let row=idx/(H*half);let rem=idx%(H*half);let h=rem/half;let j=rem%half;let pos=row+pb.x;let base=row*S+h*hd;let i0=base+2u*j; +let freq=exp(-(f32(2u*j)/f32(rot))*9.210340371976182);let ang=f32(pos)*freq;let c=cos(ang);let s=sin(ang); +let a=v[i0];let b=v[i0+1u];v[i0]=a*c-b*s;v[i0+1u]=b*c+a*s;}`; +const ATTN = `@group(0)@binding(0)varq:array;@group(0)@binding(1)vark:array;@group(0)@binding(2)varv:array;@group(0)@binding(3)varo:array;@group(0)@binding(4)varp:vec4;@group(0)@binding(5)varpc:vec4;@group(0)@binding(6)varpf:vec4; +@compute @workgroup_size(64)fn main(@builtin(global_invocation_id)g:vec3){let idx=g.x;let nQ=p.x;let nK=p.y;let H=p.z;let hd=p.w;let Sd=H*hd;let scale=pf.x;if(idx>=H*nQ){return;} +let h=idx/nQ;let i=idx%nQ;let ho=h*hd;let qb=i*Sd+ho;let lim=select(nK,i+1u,pc.x==1u); +var m=-3.0e38;var l=0.0;var acc:array;for(var d=0u;dh1:array;@group(0)@binding(1)vargg:array;@group(0)@binding(2)varp:vec4; +@compute @workgroup_size(64)fn main(@builtin(global_invocation_id)g:vec3){let idx=g.x;let n=p.x;let IFF=p.y;if(idx>=n*IFF){return;}let r=idx/IFF;let o=idx%IFF;let gate=h1[r*2u*IFF+IFF+o];let hid=h1[r*2u*IFF+o];gg[idx]=(gate/(1.0+exp(-gate)))*hid;}`; +const ADD = `@group(0)@binding(0)varx:array;@group(0)@binding(1)vary:array;@group(0)@binding(2)varp:vec4;@compute @workgroup_size(64)fn main(@builtin(global_invocation_id)g:vec3){if(g.x>=p.x){return;}x[g.x]=x[g.x]+y[g.x];}`; +// EMBED: dx[d] = embed[ids[pos]*S+d] (token stays on GPU → no per-token CPU round-trip) +const EMBED = `@group(0)@binding(0)varids:array;@group(0)@binding(1)varte:array;@group(0)@binding(2)varx:array;@group(0)@binding(3)varp:vec4; +@compute @workgroup_size(64)fn main(@builtin(global_invocation_id)g:vec3){let d=g.x;let pos=p.x;let S=p.y;if(d>=S){return;}x[d]=te[ids[pos]*S+d];}`; +// GPU argmax over full vocab (Moonshine: no token suppression) → writes next id into ids[outPos] +const ARGMAX = `@group(0)@binding(0)varlogits:array;@group(0)@binding(1)varids:array;@group(0)@binding(2)varp:vec4; +var bV:array;var bI:array; +@compute @workgroup_size(256)fn main(@builtin(local_invocation_id)l:vec3){let t=l.x;let V=p.y;var mv=-3.0e38;var mi=0u; +for(var i=t;imv){mv=s;mi=i;}} +bV[t]=mv;bI[t]=mi;workgroupBarrier(); +var st=128u;loop{if(st==0u){break;}if(tbV[t]){bV[t]=bV[t+st];bI[t]=bI[t+st];}}workgroupBarrier();st=st/2u;} +if(t==0u){ids[p.x]=bI[0];}}`; +// in-shader int8 dequant tiled GEMM: B = int8 weights packed 4/u32, per-row f32 scale → ¼ GPU mem + ~1.8× (bandwidth) +const TILED_Q8 = `@group(0)@binding(0)varA:array;@group(0)@binding(1)varB:array;@group(0)@binding(2)varscl:array;@group(0)@binding(3)varbias:array;@group(0)@binding(4)varY:array;@group(0)@binding(5)varp:vec4; +var As:array;var Bs:array; +fn rdq(idx:u32)->f32{let w=B[idx>>2u];let sh=(idx&3u)*8u;let b=(w>>sh)&0xffu;return f32(i32(b<<24u)>>24u);} +@compute @workgroup_size(16,16)fn main(@builtin(workgroup_id)wg:vec3,@builtin(local_invocation_id)l:vec3){ +let rows=p.x;let inD=p.y;let outD=p.z;let tx=l.x;let ty=l.y;let r=wg.y*16u+ty;let o=wg.x*16u+tx;var acc=0.0;let nT=(inD+15u)/16u; +for(var kt=0u;ktids:array;@group(0)@binding(1)varte:array;@group(0)@binding(2)varscl:array;@group(0)@binding(3)varx:array;@group(0)@binding(4)varp:vec4; +fn rdq(idx:u32)->f32{let w=te[idx>>2u];let sh=(idx&3u)*8u;let b=(w>>sh)&0xffu;return f32(i32(b<<24u)>>24u);} +@compute @workgroup_size(64)fn main(@builtin(global_invocation_id)g:vec3){let d=g.x;let pos=p.x;let S=p.y;if(d>=S){return;}let tok=ids[pos];x[d]=rdq(tok*S+d)*scl[tok];}`; + +export async function createMoonshineASR(holoUrl, { onProgress, kappa = "", release = "", openStream = null } = {}) { + if (!navigator.gpu) throw new Error("no WebGPU"); + const log = (s) => onProgress && onProgress(s); + log("streaming Moonshine by κ…"); + const H0 = await (openStream || streamHolo)(holoUrl, { kappa, release }); // openStream = unified-pack view (fail-soft → streamHolo) + const cfg = H0.meta.config, S = cfg.hidden_size, NH = cfg.encoder_num_attention_heads, hd = S / NH, IFF = cfg.intermediate_size; + const NL = cfg.encoder_num_hidden_layers, DL = cfg.decoder_num_hidden_layers, VOCAB = cfg.vocab_size, scale = 1 / Math.sqrt(hd); + const detok = makeDetok(JSON.parse(new TextDecoder().decode(H0.headerBytes))); + const W = new Map(); await Promise.all(H0.meta.order.map(async (o) => W.set(o.name, await H0.getF32(o.name)))); + log(`streamed ${W.size} tensors · ${(H0.stats.bytesFetched / 1e6).toFixed(1)}MB · ${H0.stats.verifies} L5${H0.stats.opfsHits ? " · " + H0.stats.opfsHits + " OPFS" : ""}`); + + const dev = await (await navigator.gpu.requestAdapter()).requestDevice(); + const pipe = (c) => dev.createComputePipeline({ layout: "auto", compute: { module: dev.createShaderModule({ code: c }), entryPoint: "main" } }); + const P = { conv: pipe(CONV1F), tanh: pipe(TANH), gelu: pipe(GELU), gns: pipe(GNSTATS), gna: pipe(GNAPPLY), transp: pipe(TRANSP), tiled: pipe(TILED), tiledq8: pipe(TILED_Q8), ln: pipe(LN), rope: pipe(ROPE), attn: pipe(ATTN), swiglu: pipe(SWIGLU), add: pipe(ADD), emb: pipe(EMBED), embq8: pipe(EMBED_Q8), argmax: pipe(ARGMAX) }; + const sb = (k) => dev.createBuffer({ size: Math.max(4, k * 4), usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST }); + const wb = (a) => { const b = dev.createBuffer({ size: Math.max(4, a.byteLength), usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST }); dev.queue.writeBuffer(b, 0, a); return b; }; + const u4 = (v) => { const b = dev.createBuffer({ size: 16, usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST }); dev.queue.writeBuffer(b, 0, new Uint32Array([...v, 0, 0, 0, 0].slice(0, 4))); return b; }; + const f4 = (v) => { const b = dev.createBuffer({ size: 16, usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST }); dev.queue.writeBuffer(b, 0, new Float32Array([...v, 0, 0, 0, 0].slice(0, 4))); return b; }; + const G = (k) => Math.ceil(k / 64), T16 = (x) => Math.ceil(x / 16); + const bg = (pl, a) => dev.createBindGroup({ layout: pl.getBindGroupLayout(0), entries: a.map((b, i) => ({ binding: i, resource: { buffer: b } })) }); + const disp = (e, pl, a, k, wg) => { const pa = e.beginComputePass(); pa.setPipeline(pl); pa.setBindGroup(0, bg(pl, a)); pa.dispatchWorkgroups(wg || G(k)); pa.end(); }; + const i8buf = (u8) => { const b = dev.createBuffer({ size: Math.max(4, Math.ceil(u8.length / 4) * 4), usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST }); dev.queue.writeBuffer(b, 0, u8); return b; }; + // tile() now takes a TYPED weight w = {f32:buf} or {q8:buf, sc:buf} → routes to TILED or in-shader int8 TILED_Q8 + const tile = (e, A, w, bias, Y, rows, inD, outD, hb) => { const pa = e.beginComputePass(); + if (w.q8) { pa.setPipeline(P.tiledq8); pa.setBindGroup(0, bg(P.tiledq8, [A, w.q8, w.sc, bias, Y, u4([rows, inD, outD, hb])])); } + else { pa.setPipeline(P.tiled); pa.setBindGroup(0, bg(P.tiled, [A, w.f32, bias, Y, u4([rows, inD, outD, hb])])); } + pa.dispatchWorkgroups(T16(outD), T16(rows)); pa.end(); }; + const GW = (n) => wb(W.get(n)), dummy = wb(new Float32Array([0])), zeroS = wb(new Float32Array(S)); + const readback = async (buf, len) => { const rb = dev.createBuffer({ size: len * 4, usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ }); const e = dev.createCommandEncoder(); e.copyBufferToBuffer(buf, 0, rb, 0, len * 4); dev.queue.submit([e.finish()]); await rb.mapAsync(GPUMapMode.READ); const r = new Float32Array(rb.getMappedRange().slice(0)); rb.unmap(); rb.destroy(); return r; }; + + // resident weights: matmul weights (.q/k/v/o_proj, .fc1/fc2, embed) = TYPED (int8 in-shader when the .holo + // is int8 → ¼ GPU mem + ~1.8× matmul); conv + 1D (norm/bias) stay f32 buffers. f16/f32 models → {f32} (untouched). + const isMM = (n) => /(q_proj|k_proj|v_proj|o_proj|fc1|fc2)\.weight$/.test(n) || n === "model.decoder.embed_tokens.weight"; + const loadW = async (name) => { const q = await H0.getQuant(name); return q.q8 ? { q8: i8buf(q.int8), sc: wb(q.scales) } : { f32: wb(q.f32) }; }; + const EW = {}, DW = {}; + for (const o of H0.meta.order) { + if (o.name.startsWith("model.encoder.")) EW[o.name] = isMM(o.name) ? await loadW(o.name) : GW(o.name); + else if (o.name.startsWith("model.decoder.layers.")) DW[o.name] = isMM(o.name) ? await loadW(o.name) : GW(o.name); + } + const embedW = await loadW("model.decoder.embed_tokens.weight"), dnorm = GW("model.decoder.norm.weight"); + const q8model = !!embedW.q8; + const fScale = f4([scale]), causal0 = u4([0]), causal1 = u4([1]), pb0 = u4([0]); + const MAXTOK = 256, EOS = 2, BOS = 1; + + async function transcribe(pcm16k, { maxNew = 200 } = {}) { + const tg = performance.now(); + const L = pcm16k.length, T1 = ((L - 127) / 64 | 0) + 1, T2 = ((T1 - 7) / 3 | 0) + 1, frames = ((T2 - 3) / 2 | 0) + 1, C2 = 2 * S; + const pcmB = wb(pcm16k), c1 = sb(S * T1), c2 = sb(C2 * T2), c3 = sb(S * frames), gstat = sb(2), encX = sb(frames * S); + let e = dev.createCommandEncoder(); + disp(e, P.conv, [pcmB, EW["model.encoder.conv1.weight"], dummy, c1, u4([1, S, 127, T1]), u4([L, 64, 0, 0])], S * T1); + disp(e, P.tanh, [c1, u4([S * T1])], S * T1); + disp(e, P.gns, [c1, gstat, u4([S * T1])], 0, 1); + disp(e, P.gna, [c1, gstat, EW["model.encoder.groupnorm.weight"], EW["model.encoder.groupnorm.bias"], u4([S, T1])], S * T1); + disp(e, P.conv, [c1, EW["model.encoder.conv2.weight"], EW["model.encoder.conv2.bias"], c2, u4([S, C2, 7, T2]), u4([T1, 3, 0, 1])], C2 * T2); + disp(e, P.gelu, [c2, u4([C2 * T2])], C2 * T2); + disp(e, P.conv, [c2, EW["model.encoder.conv3.weight"], EW["model.encoder.conv3.bias"], c3, u4([C2, S, 3, frames]), u4([T2, 2, 0, 1])], S * frames); + disp(e, P.gelu, [c3, u4([S * frames])], S * frames); + disp(e, P.transp, [c3, encX, u4([frames, S])], frames * S); + // encoder layers + const an = sb(frames * S), q = sb(frames * S), k = sb(frames * S), v = sb(frames * S), at = sb(frames * S), ao = sb(frames * S), mn = sb(frames * S), h1 = sb(frames * IFF), h2 = sb(frames * S); + const aEnc = u4([frames, frames, NH, hd]), uNES = u4([frames, S]), uNEadd = u4([frames * S]), uIFF = u4([frames * IFF]), uRopeE = u4([frames, S, NH, hd]); + for (let il = 0; il < NL; il++) { const pf = `model.encoder.layers.${il}.`; + disp(e, P.ln, [encX, EW[pf + "input_layernorm.weight"], zeroS, an, uNES], frames); + tile(e, an, EW[pf + "self_attn.q_proj.weight"], dummy, q, frames, S, S, 0); + tile(e, an, EW[pf + "self_attn.k_proj.weight"], dummy, k, frames, S, S, 0); + tile(e, an, EW[pf + "self_attn.v_proj.weight"], dummy, v, frames, S, S, 0); + disp(e, P.rope, [q, uRopeE, pb0], frames * NH * 16); disp(e, P.rope, [k, uRopeE, pb0], frames * NH * 16); + disp(e, P.attn, [q, k, v, at, aEnc, causal0, fScale], NH * frames); + tile(e, at, EW[pf + "self_attn.o_proj.weight"], dummy, ao, frames, S, S, 0); + disp(e, P.add, [encX, ao, uNEadd], frames * S); + disp(e, P.ln, [encX, EW[pf + "post_attention_layernorm.weight"], zeroS, mn, uNES], frames); + tile(e, mn, EW[pf + "mlp.fc1.weight"], EW[pf + "mlp.fc1.bias"], h1, frames, S, IFF, 1); + disp(e, P.gelu, [h1, uIFF], frames * IFF); + tile(e, h1, EW[pf + "mlp.fc2.weight"], EW[pf + "mlp.fc2.bias"], h2, frames, IFF, S, 1); + disp(e, P.add, [encX, h2, uNEadd], frames * S); + } + const encO = sb(frames * S); + disp(e, P.ln, [encX, EW["model.encoder.layer_norm.weight"], zeroS, encO, uNES], frames); + // cross K/V per layer (once) + const cK = [], cV = []; for (let il = 0; il < DL; il++) { const pf = `model.decoder.layers.${il}.`; cK[il] = sb(frames * S); cV[il] = sb(frames * S); tile(e, encO, DW[pf + "encoder_attn.k_proj.weight"], dummy, cK[il], frames, S, S, 0); tile(e, encO, DW[pf + "encoder_attn.v_proj.weight"], dummy, cV[il], frames, S, S, 0); } + dev.queue.submit([e.finish()]); + const encMs = Math.round(performance.now() - tg); + + // GPU-resident chunked decode: EMBED reads the ids buffer, GPU ARGMAX writes the next id → ONE submit + // per chunk (no per-token CPU round-trip / 32768-logit readback). Read ids back per chunk to stop at EOS. + const Kc = [], Vc = []; for (let il = 0; il < DL; il++) { Kc[il] = sb(MAXTOK * S); Vc[il] = sb(MAXTOK * S); } + const idsB = dev.createBuffer({ size: (MAXTOK + 1) * 4, usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST }); dev.queue.writeBuffer(idsB, 0, new Uint32Array([BOS])); + const idsRb = dev.createBuffer({ size: (MAXTOK + 1) * 4, usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ }); + const dx = sb(S), dan = sb(S), dq = sb(S), dk = sb(S), dvv = sb(S), dsa = sb(S), dso = sb(S), dcn = sb(S), dcq = sb(S), dcr = sb(S), dco = sb(S), dmn = sb(S), dh1 = sb(2 * IFF), dgg = sb(IFF), dh2 = sb(S), logitsB = sb(VOCAB); + const u1 = u4([1, S]), aCross = u4([1, frames, NH, hd]), uadd = u4([S]), uswig = u4([1, IFF]); + const gen = []; const td = performance.now(); const CH = 16, limit = Math.min(maxNew, MAXTOK - 2); let p = 0, eot = false; + while (p < limit && !eot) { + const end = Math.min(p + CH, limit); const ec = dev.createCommandEncoder(); + for (; p < end; p++) { + const pbP = u4([p]), aSelf = u4([1, p + 1, NH, hd]), uRope1 = u4([1, S, NH, hd]); + if (q8model) disp(ec, P.embq8, [idsB, embedW.q8, embedW.sc, dx, u4([p, S])], S); + else disp(ec, P.emb, [idsB, embedW.f32, dx, u4([p, S])], S); + for (let il = 0; il < DL; il++) { const pf = `model.decoder.layers.${il}.`; + disp(ec, P.ln, [dx, DW[pf + "input_layernorm.weight"], zeroS, dan, u1], 1); + tile(ec, dan, DW[pf + "self_attn.q_proj.weight"], dummy, dq, 1, S, S, 0); + tile(ec, dan, DW[pf + "self_attn.k_proj.weight"], dummy, dk, 1, S, S, 0); + tile(ec, dan, DW[pf + "self_attn.v_proj.weight"], dummy, dvv, 1, S, S, 0); + disp(ec, P.rope, [dq, uRope1, pbP], NH * 16); disp(ec, P.rope, [dk, uRope1, pbP], NH * 16); + ec.copyBufferToBuffer(dk, 0, Kc[il], p * S * 4, S * 4); ec.copyBufferToBuffer(dvv, 0, Vc[il], p * S * 4, S * 4); + disp(ec, P.attn, [dq, Kc[il], Vc[il], dsa, aSelf, causal0, fScale], NH); + tile(ec, dsa, DW[pf + "self_attn.o_proj.weight"], dummy, dso, 1, S, S, 0); + disp(ec, P.add, [dx, dso, uadd], S); + disp(ec, P.ln, [dx, DW[pf + "post_attention_layernorm.weight"], zeroS, dcn, u1], 1); + tile(ec, dcn, DW[pf + "encoder_attn.q_proj.weight"], dummy, dcq, 1, S, S, 0); + disp(ec, P.attn, [dcq, cK[il], cV[il], dcr, aCross, causal0, fScale], NH); + tile(ec, dcr, DW[pf + "encoder_attn.o_proj.weight"], dummy, dco, 1, S, S, 0); + disp(ec, P.add, [dx, dco, uadd], S); + disp(ec, P.ln, [dx, DW[pf + "final_layernorm.weight"], zeroS, dmn, u1], 1); + tile(ec, dmn, DW[pf + "mlp.fc1.weight"], DW[pf + "mlp.fc1.bias"], dh1, 1, S, 2 * IFF, 1); + disp(ec, P.swiglu, [dh1, dgg, uswig], IFF); + tile(ec, dgg, DW[pf + "mlp.fc2.weight"], DW[pf + "mlp.fc2.bias"], dh2, 1, IFF, S, 1); + disp(ec, P.add, [dx, dh2, uadd], S); + } + disp(ec, P.ln, [dx, dnorm, zeroS, dan, u1], 1); + tile(ec, dan, embedW, dummy, logitsB, 1, S, VOCAB, 0); + disp(ec, P.argmax, [logitsB, idsB, u4([p + 1, VOCAB])], 0, 1); + } + ec.copyBufferToBuffer(idsB, 0, idsRb, 0, (MAXTOK + 1) * 4); + dev.queue.submit([ec.finish()]); + await idsRb.mapAsync(GPUMapMode.READ); const ids = new Uint32Array(idsRb.getMappedRange().slice(0)); idsRb.unmap(); + gen.length = 0; for (let i = 1; i <= p; i++) { if (ids[i] === EOS) { eot = true; break; } gen.push(ids[i]); } + } + const gpuMs = Math.round(performance.now() - tg), decMs = Math.round(performance.now() - td); + [pcmB, c1, c2, c3, gstat, encX, an, q, k, v, at, ao, mn, h1, h2, encO, dx, dan, dq, dk, dvv, dsa, dso, dcn, dcq, dcr, dco, dmn, dh1, dgg, dh2, logitsB, idsB, idsRb, ...cK, ...cV, ...Kc, ...Vc].forEach((b) => b.destroy()); + return { text: detok(gen), ids: gen, gpuMs, encMs, decMs, frames, ms: gpuMs }; + } + return { transcribe, config: cfg, S, encNL: NL, decNL: DL, stats: H0.stats }; +} diff --git a/apps/q/forge/gpu/holo-moonshine-ear.mjs b/apps/q/forge/gpu/holo-moonshine-ear.mjs new file mode 100644 index 0000000000000000000000000000000000000000..289a3eb92baadefd24ce70eada6312575fdd1f73 --- /dev/null +++ b/apps/q/forge/gpu/holo-moonshine-ear.mjs @@ -0,0 +1,31 @@ +// holo-moonshine-ear.mjs — production adapter exposing the κ-native Moonshine WebGPU ASR engine to Q's voice +// stack (holo-voice-asr.mjs interface): createWhisperEar({ holoUrl, upgradeUrl, kappa, language }) → +// { load(progressCb), transcribe(pcm16k, opts) -> {text} }. +// FIRST-LOAD TIERING: load the small int8 .holo first (~¼ the bytes → talk in ~half the cold-start time), +// then silently stream the lossless f16 .holo in the background and hot-swap the engine. Same trick as Q's +// .holo brain (tiny-now → upgrade-silent). holo-voice-asr.mjs auto-falls back to ONNX if WebGPU/load fails. +import { createMoonshineASR } from "./holo-moonshine-asr.mjs"; + +export function createWhisperEar({ holoUrl, upgradeUrl, kappa, release, upgradeKappa, upgradeRelease, language } = {}, deps = {}) { + const openStream = deps.openStream || null; // unified-pack view (fail-soft → streamHolo inside createMoonshineASR) + let asr = null, tier = "", upgrading = false; + return { + async load(progress) { + asr = await createMoonshineASR(holoUrl, { onProgress: progress, kappa, release, openStream }); // fast tier (int8) — pack → path → Release → κ-route + tier = upgradeUrl ? "int8" : "base"; + if (upgradeUrl) { // silent background upgrade to the lossless tier; hot-swap when it lands + upgrading = true; + createMoonshineASR(upgradeUrl, { kappa: upgradeKappa, release: upgradeRelease, openStream }).then((hi) => { asr = hi; tier = "f16"; upgrading = false; }).catch(() => { upgrading = false; }); + } + return true; + }, + async transcribe(audio, opts = {}) { + if (!asr) await this.load(); + const a = asr; // pin the current tier for this call (background swap won't disturb an in-flight transcribe) + const r = await a.transcribe(audio, opts); + return { text: r.text, ids: r.ids, ms: r.ms, gpuMs: r.gpuMs, tier }; // {text,…} shape Holo Voice consumes + }, + info: () => ({ engine: "moonshine-κ", holoUrl, upgradeUrl, kappa, tier, upgrading }), + }; +} +export default createWhisperEar; diff --git a/apps/q/forge/gpu/holo-onnx-kserve.mjs b/apps/q/forge/gpu/holo-onnx-kserve.mjs new file mode 100644 index 0000000000000000000000000000000000000000..461e3ea878ec615e4bbee3770fb53ddaf9af92b9 --- /dev/null +++ b/apps/q/forge/gpu/holo-onnx-kserve.mjs @@ -0,0 +1,53 @@ +// holo-onnx-kserve.mjs — serve an ONNX faculty model's files from its κ-addressable .holo INTO the +// unchanged transformers.js / onnxruntime-web runtime. ONE shim for every ONNX faculty (TTS · embed · +// vision): no engine port — only weight DELIVERY becomes content-addressed (HTTP-Range + per-block L5 + +// OPFS warm cache + serverless multi-source), exactly like the κ-native brain/ASR, but the forward stays on +// the proven engine. Generalises the proof in kokoro-holo-test.html into a reusable, fail-safe module. +// +// serveModelFromHolo({ holoUrl, modelId, release }) → { stats, served, missed, restore, modelKey } (browser) +// +// Install BEFORE the engine loads; the engine then fetches its model files normally and the shim answers any +// request whose URL contains "/" from the .holo. Everything else passes through +// untouched (cheap substring test). Keep it installed for the engine's lifetime (lazy per-file fetches — +// e.g. a TTS voice — still route through it); call restore() to uninstall. ANY failure ⇒ the caller restores +// and falls back to the vendored ONNX path, so a faculty is never bricked by the κ path. + +// the URL key a transformers model id resolves to: "onnx-community/Kokoro-82M-v1.0-ONNX" → "Kokoro-82M-v1.0-ONNX/" +export function modelKeyFor(modelId) { return String(modelId || "").split("/").pop() + "/"; } + +// PURE, INJECTABLE routing core (Node-witnessable). Wraps target.fetch so any request whose URL contains +// `key` is answered from `hf.getFile(name)`; non-matching (and not-in-holo) requests fall through to the +// original transport. Returns the running served/missed lists and a restore() that reinstalls the original. +export function installModelFetchShim({ hf, key, target }) { + const orig = target.fetch; // the RAW original — restore() reinstalls this exact reference + const callOrig = orig.bind(target); // bound copy for safe invocation (window.fetch needs its this) + const served = [], missed = []; + const ResponseCtor = target.Response || (typeof Response !== "undefined" ? Response : null); + target.fetch = async (input, init) => { + const url = typeof input === "string" ? input : (input && input.url) || ""; + const i = url.indexOf(key); + if (i >= 0) { + const name = decodeURIComponent(url.slice(i + key.length).split("?")[0]); + try { + const b = await hf.getFile(name); + served.push(name); + return new ResponseCtor(b, { status: 200, headers: { "Content-Type": "application/octet-stream", "Content-Length": String((b && (b.length || b.byteLength)) || 0) } }); + } catch (e) { missed.push(name); } // not in the .holo → fall through to the original transport + } + return callOrig(input, init); + }; + return { served, missed, restore() { target.fetch = orig; } }; +} + +// browser entry: open the file-bundle .holo (range + L5 + OPFS, release fallback) and install the shim on +// `target` (window by default). `openFiles` is injectable for tests; in the browser it lazy-imports holo-files. +export async function serveModelFromHolo({ holoUrl, modelId, release = "", target, openFiles } = {}) { + const t = target || (typeof window !== "undefined" ? window : globalThis); + const open = openFiles || (async (u, o) => (await import("./holo-files.mjs")).openHoloFiles(u, o)); + const hf = await open(holoUrl, { release }); + const key = modelKeyFor(modelId); + const shim = installModelFetchShim({ hf, key, target: t }); + return { stats: hf.stats, served: shim.served, missed: shim.missed, restore: shim.restore, modelKey: key, files: hf.files }; +} + +export default serveModelFromHolo; diff --git a/apps/q/forge/gpu/holo-pack-shards.mjs b/apps/q/forge/gpu/holo-pack-shards.mjs new file mode 100644 index 0000000000000000000000000000000000000000..a165f0e9f868c7389548b38c83e5c04eae494308 --- /dev/null +++ b/apps/q/forge/gpu/holo-pack-shards.mjs @@ -0,0 +1,121 @@ +// holo-pack-shards.mjs — present a sharded pack as ONE logical file. The single q-models.holo is delivered in <2 GiB +// parts (GitHub's per-asset cap); this stitches them behind a normal rangeReader(off,len) so openModelPack / every +// faculty loader sees one contiguous file at one address. Sharding is pure transport — never visible above here. +// +// spanReader(parts, readPart) → async (off,len) => Uint8Array // parts: [{start,len}]; readPart(i,off,len)->bytes +// makeShardedRangeReader({ parts, fetchPart }) → rangeReader // production: fetchPart(i,off,len) HTTP-Ranges part i +// +// A single read may straddle a part boundary; spanReader splits it across parts and concatenates. Production wires +// fetchPart to Range-GET part i from the release (κ-route/OPFS heal underneath), so a body that crosses a boundary +// is fetched from two assets transparently and L5-verified as one body by openHoloStream. + +// stitch a logical [off,off+len) read across byte-contiguous parts. readPart(i, withinOff, n) returns ≥ n bytes. +export function spanReader(parts, readPart) { + const total = parts.reduce((mx, p) => Math.max(mx, p.start + p.len), 0); + return async (off, len) => { + if (off < 0 || off + len > total) throw new Error(`span read out of range: [${off},${off + len}) of ${total}`); + const out = new Uint8Array(len); + let done = 0; + while (done < len) { + const g = off + done; + const i = parts.findIndex((p) => g >= p.start && g < p.start + p.len); + if (i < 0) throw new Error("no part covers offset " + g); + const p = parts[i], within = g - p.start, n = Math.min(len - done, p.len - within); + const chunk = await readPart(i, within, n); + out.set(chunk.length > n ? chunk.subarray(0, n) : chunk, done); + done += n; + } + return out; + }; +} + +// production reader: parts from a manifest, each fetched via HTTP Range from its release asset URL (with κ-route/OPFS +// healing the bytes underneath). fetchPart(i, off, len) -> Promise. +export function makeShardedRangeReader({ parts, fetchPart }) { + return spanReader(parts, (i, off, len) => fetchPart(i, off, len)); +} + +// ── HTTP transport (production) ────────────────────────────────────────────────────────────────── +// one URL's Range read, handling a 206 (partial — the normal GitHub-release case) AND a server that ignores Range +// and returns 200-whole (defensive: cache the whole body once, slice locally thereafter). fetchImpl is injectable so +// the path is witnessed in Node with a mock fetch serving local shards. +export function makeRangeFetcher({ fetchImpl } = {}) { + const f = fetchImpl || (typeof fetch !== "undefined" ? fetch : null); + if (!f) throw new Error("makeRangeFetcher: no fetch available"); + const whole = new Map(); // url → full body when the server ignored Range + return async (url, off, len) => { + const w = whole.get(url); if (w) return w.subarray(off, off + len); + let r; try { r = await f(url, { headers: { Range: `bytes=${off}-${off + len - 1}` } }); } catch (e) { r = null; } + if (!r || !r.ok) throw new Error("range fetch failed (" + (r ? r.status : "network") + "): " + url); + const u = new Uint8Array(await r.arrayBuffer()); + if (r.status === 206) return u; // exact slice + if (u.length > len) { whole.set(url, u); return u.subarray(off, off + len); } // 200-whole → cache + slice + return u; + }; +} + +// open the unified pack over its SHARDS from a release: fetch the parts manifest, build a spanning rangeReader that +// Range-GETs each part. The result's rangeReader hands straight to openModelPack — the OS sees one file, one address. +// peerResolve(part) -> Promise: the COMMONS source. A part is content-addressed +// (part.sha256), so it can be served by ANY peer and is hash-trustless; openHoloStream L5-verifies the +// assembled body downstream regardless, so a wrong peer byte is caught (the commons is a latency choice, +// never trust — same contract as the CDN/IPFS sources). null/throw → fall through to CDN/origin. +export async function openShardedPack({ partsUrl, base, fetchImpl, gateway, peerResolve } = {}) { + const f = fetchImpl || (typeof fetch !== "undefined" ? fetch : null); + if (!f) throw new Error("openShardedPack: no fetch available"); + if (!partsUrl) throw new Error("openShardedPack needs partsUrl"); + // the manifest ships SAME-ORIGIN (tiny → in dist), so this fetch never hits a CORS wall. + const mr = await f(partsUrl); if (!mr || !mr.ok) throw new Error("parts manifest fetch failed (" + (mr ? mr.status : "network") + "): " + partsUrl); + const manifest = JSON.parse(new TextDecoder().decode(new Uint8Array(await mr.arrayBuffer()))); + const range = makeRangeFetcher({ fetchImpl: f }); + // SERVERLESS delivery, in priority order — all CORS + Range, no app server: + // 1. CDN over a GitHub repo (manifest.cdnBase, jsDelivr): {cdnBase}/{name}. Global immutable CDN, fast any-device. + // manifest.rawBase (raw.githubusercontent) is the same-repo fallback. Override via window.HOLO_PACK_CDN. + // 2. IPFS gateway by CID (manifest.gateway + per-part cid): {gateway}/ipfs/{cid}. Override via HOLO_PACK_GATEWAY. + // 3. a co-located mirror by part name ({base}/{name}). + // Every block is L5-verified regardless of source (a source is a latency choice, never trust). + const G = (typeof globalThis !== "undefined") ? globalThis : {}; + const cdn = (G.HOLO_PACK_CDN || manifest.cdnBase || "").replace(/\/+$/, ""); + const raw = (manifest.rawBase || "").replace(/\/+$/, ""); + const gw = (gateway || G.HOLO_PACK_GATEWAY || manifest.gateway || "").replace(/\/+$/, ""); + const baseUrl = base || partsUrl.replace(/[^/]*$/, ""); + const primary = (p) => cdn ? `${cdn}/${p.name}` : (gw && p.cid) ? `${gw}/ipfs/${p.cid}` : (baseUrl + p.name); + const peerWhole = new Map(); // i → whole verified part from a peer (null = peer miss) + const fabricOn = () => !(typeof globalThis !== "undefined" && globalThis.HoloFabric && globalThis.HoloFabric.enabled === false); // ONE kill switch + const fetchPart = async (i, off, len) => { + const p = manifest.parts[i]; + if (peerResolve && fabricOn()) { // COMMONS first: a peer that already has this part (by κ) + let whole = peerWhole.get(i); + if (whole === undefined) { try { whole = (await peerResolve(p)) || null; } catch { whole = null; } peerWhole.set(i, whole); } + if (whole && whole.length >= p.len) return whole.subarray(off, off + len); // 0 bytes from origin + } + try { return await range(primary(p), off, len); } + catch (e) { if (cdn && raw) return range(`${raw}/${p.name}`, off, len); throw e; } // jsDelivr miss → raw.githubusercontent + }; + return { manifest, rangeReader: makeShardedRangeReader({ parts: manifest.parts, fetchPart }) }; +} + +// THE one call the OS makes: open the unified pack, preferring the monolithic file when reachable (dev / FORGE local +// mount — a single <2 GiB-cap-free file), else the sharded release. Returns the OPENED pack (openModelPack shape) + +// how it was reached. monolithicUrl is probed with one tiny Range read of the "HOLO" magic; any failure → shards. +export async function openQPack({ monolithicUrl, partsUrl, base, fetchImpl, openModelPack, gateway, peerResolve } = {}) { + if (!openModelPack) ({ openModelPack } = await import("./holo-model-pack.mjs")); + const f = fetchImpl || (typeof fetch !== "undefined" ? fetch : null); + if (monolithicUrl && f) { + try { + const range = makeRangeFetcher({ fetchImpl: f }); + // time-box the probe: an absent 4.6GB monolithic can trigger host-side κ-healing that hangs instead of 404ing. + // A real same-origin monolithic (dev) answers a 4-byte Range in well under a second; anything slower → shards. + const magic = await Promise.race([range(monolithicUrl, 0, 4), new Promise((_, r) => setTimeout(() => r(new Error("monolithic probe timeout")), 2500))]); + if (magic && magic.length >= 4 && magic[0] === 0x48 && magic[1] === 0x4f) { // "HO" — the monolithic file is served whole (dev / same-origin) + const pack = await openModelPack({ rangeReader: (off, len) => range(monolithicUrl, off, len) }); + return { via: "monolithic", pack }; + } + } catch { /* fall through to shards */ } + } + const { manifest, rangeReader } = await openShardedPack({ partsUrl, base, fetchImpl: f, gateway, peerResolve }); + const pack = await openModelPack({ rangeReader }); + return { via: "sharded", manifest, pack }; +} + +export default { spanReader, makeShardedRangeReader, makeRangeFetcher, openShardedPack, openQPack }; diff --git a/apps/q/forge/gpu/holo-q-pack-provider.mjs b/apps/q/forge/gpu/holo-q-pack-provider.mjs new file mode 100644 index 0000000000000000000000000000000000000000..2cf62d10a569a83bb9f37ad906ef6403f16171ca --- /dev/null +++ b/apps/q/forge/gpu/holo-q-pack-provider.mjs @@ -0,0 +1,149 @@ +// holo-q-pack-provider.mjs — the consumer seam: open the unified q-models pack ONCE per page and hand each faculty its +// model view from it, fail-soft to the faculty's own standalone .holo when the pack isn't reachable. This is what +// voice.js / the ear + brain loaders call instead of fetching a per-model .holo: one open, one warm OPFS store, one +// address; pack.model(id) is openHoloStream-shaped so the loaders take it unchanged. +// +// getQPack({ packSpec, fetchImpl }) → the opened pack (memoized; concurrent callers share one open) +// packModelFor(spec, { packSpec, fetchImpl }) → spec's model view FROM the pack, or null (caller uses standalone) +// +// spec is a faculty spec from holo-q-faculty-models.specFor()/resolveFacultyModel — it carries spec.pack={url,release, +// model} when the model lives in the pack. packSpec is that module's exported packSpec (one file + shards manifest). +import { openQPack } from "./holo-pack-shards.mjs"; + +let _pack = null, _opening = null, _key = null; + +const baseOf = (url) => (url ? url.replace(/[^/]*$/, "") : undefined); + +// open the pack once. Prefers the monolithic file (dev/FORGE-local), falls back to release shards. Memoized by the +// pack's address so every faculty on the page shares ONE open + OPFS warm. Concurrent callers await the same promise. +export async function getQPack({ packSpec, fetchImpl } = {}) { + if (!packSpec) throw new Error("getQPack needs packSpec"); + const key = (packSpec.url || "") + "|" + (packSpec.partsManifest || ""); + if (_pack && _key === key) return _pack; + if (_opening && _key === key) return _opening; + _key = key; + _opening = openQPack({ monolithicUrl: packSpec.url, partsUrl: packSpec.partsManifest, base: baseOf(packSpec.release), fetchImpl }) + .then((r) => { _pack = r.pack; _pack.__via = r.via; _opening = null; return _pack; }) + .catch((e) => { if (_key === key) { _opening = null; _key = null; } throw e; }); + return _opening; +} + +export function resetQPack() { _pack = null; _opening = null; _key = null; } + +// hand a faculty its model view FROM the pack, or null. null ⇒ the model isn't in the pack OR the pack is unreachable +// ⇒ the caller falls back to spec.url/spec.release (the standalone .holo) — never a hard failure. +export async function packModelFor(spec, { packSpec, fetchImpl } = {}) { + try { + if (!spec || !spec.pack) return null; + const pack = await getQPack({ packSpec, fetchImpl }); + return pack.model(spec.pack.model); + } catch { return null; } +} + +// ── ear adapter ────────────────────────────────────────────────────────────────────────────────── +// createWhisperEar (parakeet) loads its encoder + joint via deps.openStream(url) and its small loose files (rescale +// json/bin, vocab, nemo) via deps.fetchBytes(url). This maps those URLs — by basename — onto the unified pack so the +// REAL ear streams entirely from the one file, with the standalone url/release as fallback. ZERO ear changes: +// createWhisperEar(cfg, makePackEarDeps({ packSpec })) ← that's the whole flip. +const EAR_BYNAME = { + "parakeet-tdt-0.6b-v2-stream.holo": { kind: "stream", model: "parakeet-encoder" }, + "parakeet-tdt-0.6b-v2-joint.holo": { kind: "stream", model: "parakeet-joint" }, + "parakeet-encoder-rescale.json": { kind: "file", model: "parakeet-encoder", file: "parakeet-encoder-rescale.json" }, + "parakeet-encoder-rescale.bin": { kind: "file", model: "parakeet-encoder", file: "parakeet-encoder-rescale.bin" }, + "parakeet-vocab.txt": { kind: "file", model: "parakeet-encoder", file: "parakeet-vocab.txt" }, + "parakeet-nemo128.onnx": { kind: "file", model: "parakeet-encoder", file: "parakeet-nemo128.onnx" }, +}; +const basename = (u) => String(u).split(/[/?#]/).filter(Boolean).pop(); + +// ── universal faculty→pack map ─────────────────────────────────────────────────────────────────── +// every model's standalone .holo basename → how it's served from the pack: "stream" (openHoloStream-shaped loaders: +// moonshine/parakeet ears), "gguf" (the GGUF brain via ggufStreamFromPackModel), "files" (file-bundle loaders served +// through openHoloFiles: turn-detector, kokoro). So ONE provider backs every loader in the voice loop. +const PACK_BYNAME = { + "moonshine-tiny-int8.holo": { kind: "stream", model: "moonshine-tiny-int8" }, + "moonshine-tiny-f16.holo": { kind: "stream", model: "moonshine-tiny-f16" }, + "parakeet-tdt-0.6b-v2-stream.holo": { kind: "stream", model: "parakeet-encoder" }, + "parakeet-tdt-0.6b-v2-joint.holo": { kind: "stream", model: "parakeet-joint" }, + "qwen2.5-0.5b-instruct.holo": { kind: "gguf", model: "qwen2.5-0.5b" }, + "qwen2.5-1.5b-instruct.holo": { kind: "gguf", model: "qwen2.5-1.5b" }, + "qwen2.5-coder-3b-instruct.holo": { kind: "gguf", model: "qwen-coder-3b" }, + "turn-detector.holo": { kind: "files", model: "turn-detector" }, + "kokoro-82m.holo": { kind: "files", model: "kokoro-82m" }, +}; + +// reconstruct streamHolo's view (getF32/getQuant/getMelFilters/meta.config) from a pack model view — the moonshine ear +// reads these, not the bare getBody. Reuses holo-whisper-stream's buildHoloViews over the view's L5 getBody, so decode +// is byte-identical to the standalone .holo. The view carries the full meta (config + order with dims/type) from the pack. +export async function streamHoloFromPackModel(view) { + await view.ensureHeader?.(); // split-manifest pack: fetch the lazy header body before decode + const { buildHoloViews } = await import("./holo-whisper-stream.mjs"); + const v = buildHoloViews(view.meta, view.headerBytes, (h) => view.getBody(h)); + return Object.assign({}, v, { dir: view.dir, stats: { ranges: 0, bytesFetched: 0, verifies: 0, opfsHits: 0, fromPack: true } }); +} + +// an openStream(url,opts) for the moonshine ear (whisper-shaped): pack view (full getF32/getQuant) for a known +// basename, else fallback to streamHolo. (Parakeet uses makePackEarDeps — the raw openHoloStream view — not this.) +export function makePackOpenStream({ packSpec, fetchImpl, openStream, onSource } = {}) { + return async (url, o) => { + const e = PACK_BYNAME[basename(url)]; + if (e && e.kind === "stream") { try { const pack = await getQPack({ packSpec, fetchImpl }); const v = await streamHoloFromPackModel(pack.model(e.model)); try { onSource && onSource("stream", basename(url), "pack"); } catch {} return v; } catch {} } + try { onSource && onSource("stream", basename(url), "standalone"); } catch {} + if (openStream) return openStream(url, o); + const { streamHolo } = await import("./holo-whisper-stream.mjs"); return streamHolo(url, o); + }; +} + +// an openFiles(url,opts) for file-bundle loaders (serveModelFromHolo's `openFiles`): a files-view backed by the pack +// model's fileBody, else fallback to openHoloFiles. modelId pins which pack model answers (turn-detector / kokoro). +export function makePackOpenFiles(modelId, { packSpec, fetchImpl, openFiles, onSource } = {}) { + return async (url, o) => { + try { const pack = await getQPack({ packSpec, fetchImpl }); const m = pack.model(modelId); + // a file-bundle's named entries land in `order` (the forge stores them there); `files` holds only extra loose + // files. Expose whichever carries the names so serveModelFromHolo can enumerate; getFile resolves across both. + const names = (m.files && m.files.length) ? m.files : m.order; + try { onSource && onSource("files", basename(url), "pack"); } catch {} + return { meta: { files: names }, files: names, getFile: (name) => m.fileBody(name), bodyByKappa: (k) => m.getBody(k), objectURL: async (name, mime = "application/octet-stream") => URL.createObjectURL(new Blob([await m.fileBody(name)], { type: mime })) }; + } catch {} + try { onSource && onSource("files", basename(url), "standalone"); } catch {} + if (openFiles) return openFiles(url, o); + const { openHoloFiles } = await import("./holo-files.mjs"); return openHoloFiles(url, o); + }; +} + +// resolve a model .holo URL → its pack entry {kind,model} (or null if not in the pack) — lets a loader decide whether +// to take a pack adapter for the model it was handed by URL (the brain/turn/tts call sites). +export function packEntryForUrl(url) { return PACK_BYNAME[basename(url)] || null; } + +// a ()→{plan,store,headerBytes,…} for the GGUF brain (createHoloBrain's openGgufStream): the unified-pack qwen, built +// via ggufStreamFromPackModel. Falls back to null so the brain uses its own makeBrainRange+openGgufHoloStream path. +export function makePackGgufStream(modelId, { packSpec, fetchImpl, onSource } = {}) { + return async ({ persist = null } = {}) => { + try { const pack = await getQPack({ packSpec, fetchImpl }); const { ggufStreamFromPackModel } = await import("../gguf-forge-kstream.mjs"); + const view = pack.model(modelId); await view.ensureHeader?.(); // split-manifest: fetch the lazy GGUF header before planFrom + try { onSource && onSource("gguf", modelId, "pack"); } catch {} + return ggufStreamFromPackModel(view, { persist }); + } catch { try { onSource && onSource("gguf", modelId, "standalone"); } catch {} return null; } + }; +} + +export function makePackEarDeps({ packSpec, fetchImpl, openStream, fetchBytes, onSource } = {}) { + const note = (kind, name, src) => { try { onSource && onSource(kind, name, src); } catch {} }; + return { + openStream: async (url, o) => { + const e = EAR_BYNAME[basename(url)]; + if (e && e.kind === "stream") { try { const pack = await getQPack({ packSpec, fetchImpl }); const v = pack.model(e.model); await v.ensureHeader?.(); note("stream", basename(url), "pack"); return v; } catch {} } + note("stream", basename(url), "standalone"); + if (openStream) return openStream(url, o); + const { streamHolo } = await import("./holo-whisper-stream.mjs"); return streamHolo(url, o); + }, + fetchBytes: async (url) => { + const e = EAR_BYNAME[basename(url)]; + if (e && e.kind === "file") { try { const pack = await getQPack({ packSpec, fetchImpl }); const b = await pack.model(e.model).fileBody(e.file); note("file", basename(url), "pack"); return b; } catch {} } + note("file", basename(url), "standalone"); + if (fetchBytes) return fetchBytes(url); + const r = await fetch(url); if (!r.ok) throw new Error("fetch " + url + " " + r.status); return new Uint8Array(await r.arrayBuffer()); + }, + }; +} + +export default { getQPack, packModelFor, resetQPack, makePackEarDeps }; diff --git a/apps/q/forge/gpu/holo-whisper-stream.mjs b/apps/q/forge/gpu/holo-whisper-stream.mjs new file mode 100644 index 0000000000000000000000000000000000000000..ea02d1de491ec4be18906e8249a7f1acc3b54227 --- /dev/null +++ b/apps/q/forge/gpu/holo-whisper-stream.mjs @@ -0,0 +1,108 @@ +// holo-whisper-stream.mjs — load Whisper weights 100% from the .holo, native to the κ substrate: +// • HTTP-Range fetch of each tensor body by its κ (content address) — only what's needed, in any order +// • per-block WebCrypto SHA-256 L5 verify (re-derive every byte before accepting it) +// • OPFS cache keyed by κ → second load is instant + offline (serverless), still L5-verified +// • dequant verbatim ggml bytes (F16/F32) → f32 for the WebGPU kernels +// No flat blob, no trust in the transport: identity is the hash, exactly like the rest of Hologram. +const MAGIC = [0x48, 0x4f, 0x4c, 0x4f]; +const hexOf = (b) => { let s = ""; for (const x of b) s += x.toString(16).padStart(2, "0"); return s; }; + +// f16→f32 via a 64K lookup table (IEEE half, incl. subnormals/inf/nan) — built once. +const F16 = new Float32Array(65536); +for (let h = 0; h < 65536; h++) { const s = (h >> 15) & 1, e = (h >> 10) & 0x1f, m = h & 0x3ff; let v; if (e === 0) v = Math.pow(2, -14) * (m / 1024); else if (e === 31) v = m ? NaN : Infinity; else v = Math.pow(2, e - 15) * (1 + m / 1024); F16[h] = s ? -v : v; } + +const sha256hex = async (buf) => hexOf(new Uint8Array(await crypto.subtle.digest("SHA-256", buf))); + +async function opfsGet(key) { try { const r = await navigator.storage.getDirectory(); const d = await r.getDirectoryHandle("holo-kappa", { create: true }); const fh = await d.getFileHandle(key); return new Uint8Array(await (await fh.getFile()).arrayBuffer()); } catch { return null; } } +async function opfsPut(key, bytes) { try { const r = await navigator.storage.getDirectory(); const d = await r.getDirectoryHandle("holo-kappa", { create: true }); const fh = await d.getFileHandle(key, { create: true }); const w = await fh.createWritable(); await w.write(bytes); await w.close(); return true; } catch { return false; } } + +// align a (possibly offset) body view to a fresh ArrayBuffer for typed-array views +const aligned = (u) => (u.byteOffset % 4 === 0) ? u : new Uint8Array(u.slice().buffer); + +export async function streamHolo(url, { useOpfs = true, kappa = "", release = "" } = {}) { + const stats = { ranges: 0, bytesFetched: 0, verifies: 0, opfsHits: 0, opfsWrites: 0, support206: false, kappaRoute: false, releaseRoute: false }; + let wholeBuf = null; // set when the server returns the 200 full body → serve all reads from memory + let activeUrl = url, switched = false, triedRelease = false; + const fetchRange = (u, off, len) => fetch(u, { headers: { Range: `bytes=${off}-${off + len - 1}` } }); + const rangeReader = async (off, len) => { + if (wholeBuf) return wholeBuf.subarray(off, off + len); // no-Range server: one full fetch, then in-memory slices + stats.ranges++; + let r = null; try { r = await fetchRange(activeUrl, off, len); } catch (e) { r = null; } + // DEPLOY TIER 2: weights too large for Pages (100MB/file) live as a GitHub Release asset (2GB/file). + if ((!r || !r.ok) && release && activeUrl !== release && !triedRelease) { + triedRelease = true; activeUrl = release; stats.releaseRoute = true; + try { r = await fetchRange(activeUrl, off, len); } catch (e) { r = null; } + } + // STATIC/IPFS DEPLOY: the gitignored .holo isn't at its path → heal by κ (/.holo/sha256/<κ>), the + // Service Worker pulls it from IPFS/mesh. Same fallback as Q's .holo brain (holo-brain-engine). + if ((!r || !r.ok) && kappa && !switched) { + switched = true; activeUrl = "/.holo/sha256/" + kappa; stats.kappaRoute = true; + try { r = await fetchRange(activeUrl, off, len); } catch (e) { r = null; } + } + if (!r || !r.ok) throw new Error("holo fetch failed (" + (r ? r.status : "network") + "): " + activeUrl); + stats.support206 = stats.support206 || r.status === 206; + const u = new Uint8Array(await r.arrayBuffer()); + stats.bytesFetched += u.length; + if (r.status === 206) return u; // true partial response → use as-is + if (u.length > len) { wholeBuf = u; return wholeBuf.subarray(off, off + len); } // got whole file → cache, slice (every block still L5-verified) + return u; + }; + // ── parse head / sections / metadata / weights directory ── + const head = await rangeReader(0, 64), hdv = new DataView(head.buffer, head.byteOffset, head.byteLength); + for (let i = 0; i < 4; i++) if (head[i] !== MAGIC[i]) throw new Error("not a .holo"); + const sc = hdv.getUint16(8, true); + const tbl = await rangeReader(10, sc * 17), tdv = new DataView(tbl.buffer, tbl.byteOffset, tbl.byteLength), sections = {}; + for (let i = 0, p = 0; i < sc; i++, p += 17) sections[tbl[p]] = { off: Number(tdv.getBigUint64(p + 1, true)), len: Number(tdv.getBigUint64(p + 9, true)) }; + const m = sections[8], metaB = await rangeReader(m.off, m.len); + const meta = JSON.parse(new TextDecoder().decode(metaB)); + // Extension (kind 14) = [keyLen u16][key][ggml whisper head bytes] → hparams + mel filterbank + vocab + const ex = sections[14], exB = ex ? await rangeReader(ex.off, ex.len) : null; + const headerBytes = exB ? exB.subarray(2 + new DataView(exB.buffer, exB.byteOffset, exB.byteLength).getUint16(0, true)) : null; + const w = sections[3], cntB = await rangeReader(w.off, 4), count = new DataView(cntB.buffer, cntB.byteOffset, cntB.byteLength).getUint32(0, true); + const dirB = await rangeReader(w.off + 4, count * 48), ddv = new DataView(dirB.buffer, dirB.byteOffset, dirB.byteLength), dir = new Map(); + for (let i = 0, p = 0; i < count; i++, p += 48) dir.set(hexOf(dirB.subarray(p, p + 32)), { off: Number(ddv.getBigUint64(p + 32, true)), len: Number(ddv.getBigUint64(p + 40, true)) }); + // fetch+verify one body by κ (OPFS-cached) — REFUSE on hash mismatch (L5) + async function bodyByKappa(hex) { + if (useOpfs) { const c = await opfsGet(hex); if (c) { stats.verifies++; if (await sha256hex(c) === hex) { stats.opfsHits++; return c; } } } + const d = dir.get(hex); if (!d) throw new Error("κ not in holo: " + hex); + const b = await rangeReader(d.off, d.len); + stats.verifies++; if (await sha256hex(b) !== hex) throw new Error("L5 REFUSE " + hex); + if (useOpfs && await opfsPut(hex, b)) stats.opfsWrites++; + return b; + } + const views = buildHoloViews(meta, headerBytes, bodyByKappa); + return Object.assign({}, views, { dir, sections, stats }); +} + +// the dequant accessors (getF32/getQuant/getMelFilters) over a meta + a body fetcher — extracted so the unified pack +// reuses the EXACT decode (holo-q-pack-provider's streamHoloFromPackModel passes the pack view's L5 getBody here). +// getBody(hex) → Promise (already L5-verified by the caller). meta.order carries name/dims/type/kappa. +export function buildHoloViews(meta, headerBytes, getBody) { + const byName = new Map(meta.order.map((o) => [o.name, o])); + const norm = (k) => String(k).split(":").pop(); + async function getF32(name) { + const o = byName.get(name); if (!o) throw new Error("no tensor " + name); + const n = o.dims.reduce((a, b) => a * b, 1), body = aligned(await getBody(norm(o.kappa))); + if (o.type === 0) return new Float32Array(body.buffer, body.byteOffset, n); // F32 verbatim + if (o.type === 1) { const u = new Uint16Array(body.buffer, body.byteOffset, n), f = new Float32Array(n); for (let i = 0; i < n; i++) f[i] = F16[u[i]]; return f; } // F16→F32 + if (o.type === 9) { // per-row int8: [f32 scale × rows][int8 × n] → F32 + const rows = o.dims[0], cols = n / rows, scales = new Float32Array(body.buffer, body.byteOffset, rows), q = new Int8Array(body.buffer, body.byteOffset + rows * 4, n), f = new Float32Array(n); + for (let r = 0; r < rows; r++) { const sc = scales[r], b = r * cols; for (let c = 0; c < cols; c++) f[b + c] = q[b + c] * sc; } + return f; + } + throw new Error("unhandled ggml type " + o.type); + } + async function getQuant(name) { + const o = byName.get(name); if (!o) throw new Error("no tensor " + name); + const n = o.dims.reduce((a, b) => a * b, 1); + if (o.type === 9) { const body = aligned(await getBody(norm(o.kappa))), rows = o.dims[0]; + return { q8: true, rows, cols: n / rows, scales: new Float32Array(body.buffer, body.byteOffset, rows), int8: new Uint8Array(body.buffer, body.byteOffset + rows * 4, n) }; } + return { q8: false, f32: await getF32(name) }; + } + async function getMelFilters() { + const mk = meta.mel && meta.mel.kappa; if (!mk) throw new Error("no mel filterbank in .holo"); + const body = aligned(await getBody(norm(mk))); + return new Float32Array(body.buffer, body.byteOffset, meta.mel.n_mel * meta.mel.n_fft); + } + return { meta, getF32, getQuant, getMelFilters, bodyByKappa: (h) => getBody(norm(h)), headerBytes, names: meta.order.map((o) => o.name) }; +} diff --git a/apps/q/forge/holo-archive.mjs b/apps/q/forge/holo-archive.mjs new file mode 100644 index 0000000000000000000000000000000000000000..d53a232831a8b7bf1f54ea9413b7c596387fada4 --- /dev/null +++ b/apps/q/forge/holo-archive.mjs @@ -0,0 +1,207 @@ +// GGUF → .holo writer/reader. A .holo is the precompiled, streamable, content- +// addressed package the cold-load runtime consumes: a section table + a Weights +// section of sha256-keyed, deduplicated weight bodies laid out in FIRST-USE order +// (so streaming arrives in compute order), with the GGUF header (metadata + +// tokenizer) baked in as an Extension and a footer fingerprint = the model's +// did:holo. Each body's ABSOLUTE file offset is in the directory, so a single +// weight is range-fetchable (HTTP Range) and verified by re-derivation (L5). +// +// Layout aligns to hologram/crates/hologram-archive (MAGIC "HOLO" v2, SectionRef +// {kind,offset,len}, Weights=3/Metadata=8/Extension=14). Hash axis is sha256 (the +// forge axis = SRI = L5), not the Rust crate's blake3 — byte-exact Rust-decoder +// interop is out of scope; this is the JS runtime's streaming package. + +import { forgeGguf } from "./gguf-forge.mjs"; +import { parseGgufHeader } from "../qvac-ingest.mjs"; +// sha256hex + didHolo INLINED (FIPS 180-4, byte-identical to holo-uor) — was imported via a dev-tree relative path +// that doesn't resolve in the sealed dist (holo://os/…), which broke `import()` of this module (and everything +// through it: holo-model-pack, gguf-forge-kstream/brain) on the native host. Self-contained = loads in node AND dist. +const _SHA_K = new Uint32Array([0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2]); +const _rotr = (x, n) => (x >>> n) | (x << (32 - n)); +function _sha256u8(msg) { + const h = new Uint32Array([0x6a09e667,0xbb67ae85,0x3c6ef372,0xa54ff53a,0x510e527f,0x9b05688c,0x1f83d9ab,0x5be0cd19]); + const len = msg.length, bitLen = len * 8, withOne = len + 1, k = (56 - (withOne % 64) + 64) % 64, total = withOne + k + 8; + const m = new Uint8Array(total); m.set(msg); m[len] = 0x80; + const hi = Math.floor(bitLen / 0x100000000), lo = bitLen >>> 0; + m[total-8]=(hi>>>24)&255; m[total-7]=(hi>>>16)&255; m[total-6]=(hi>>>8)&255; m[total-5]=hi&255; + m[total-4]=(lo>>>24)&255; m[total-3]=(lo>>>16)&255; m[total-2]=(lo>>>8)&255; m[total-1]=lo&255; + const w = new Uint32Array(64); + for (let off = 0; off < total; off += 64) { + for (let i = 0; i < 16; i++) w[i] = (m[off+i*4]<<24)|(m[off+i*4+1]<<16)|(m[off+i*4+2]<<8)|(m[off+i*4+3]); + for (let i = 16; i < 64; i++) { const s0=_rotr(w[i-15],7)^_rotr(w[i-15],18)^(w[i-15]>>>3); const s1=_rotr(w[i-2],17)^_rotr(w[i-2],19)^(w[i-2]>>>10); w[i]=(w[i-16]+s0+w[i-7]+s1)|0; } + let a=h[0],b=h[1],c=h[2],d=h[3],e=h[4],f=h[5],g=h[6],hh=h[7]; + for (let i = 0; i < 64; i++) { const S1=_rotr(e,6)^_rotr(e,11)^_rotr(e,25); const ch=(e&f)^((~e)&g); const t1=(hh+S1+ch+_SHA_K[i]+w[i])|0; const S0=_rotr(a,2)^_rotr(a,13)^_rotr(a,22); const maj=(a&b)^(a&c)^(b&c); const t2=(S0+maj)|0; hh=g;g=f;f=e;e=(d+t1)|0;d=c;c=b;b=a;a=(t1+t2)|0; } + h[0]=(h[0]+a)|0;h[1]=(h[1]+b)|0;h[2]=(h[2]+c)|0;h[3]=(h[3]+d)|0;h[4]=(h[4]+e)|0;h[5]=(h[5]+f)|0;h[6]=(h[6]+g)|0;h[7]=(h[7]+hh)|0; + } + const out = new Uint8Array(32); + for (let i = 0; i < 8; i++) { out[i*4]=(h[i]>>>24)&255; out[i*4+1]=(h[i]>>>16)&255; out[i*4+2]=(h[i]>>>8)&255; out[i*4+3]=h[i]&255; } + return out; +} +const _u8 = (x) => typeof x === "string" ? new TextEncoder().encode(x) : (x instanceof Uint8Array ? x : new Uint8Array(x)); +const sha256hex = (x) => { const u = _sha256u8(_u8(x)); let s = ""; for (let i = 0; i < u.length; i++) s += u[i].toString(16).padStart(2, "0"); return s; }; +const didHolo = (axis, hex) => `did:holo:${axis}:${hex}`; + +const MAGIC = [0x48, 0x4f, 0x4c, 0x4f]; // "HOLO" +const VERSION = 2; +const K = { Weights: 3, Metadata: 8, Extension: 14 }; +const HEX = (h) => { const b = new Uint8Array(32); for (let i = 0; i < 32; i++) b[i] = parseInt(h.substr(i * 2, 2), 16); return b; }; +const hexOf = (b) => { let s = ""; for (const x of b) s += x.toString(16).padStart(2, "0"); return s; }; + +// ── writer ── +export function writeHolo(ggufBytes) { + const f = forgeGguf(ggufBytes); + const dataOffset = parseGgufHeader(ggufBytes).dataOffset; + const headerBytes = ggufBytes.subarray(0, dataOffset); // GGUF metadata + tokenizer + tensor infos + + // first-use order = GGUF tensor directory order (token_embd → per-layer → output); + // dedup bodies by κ but keep first-occurrence order. + const order = [], seen = new Map(); // κ-hex → {offset(within bodies), len} + let bodyTotal = 0; + for (const t of f.tensors) { + const hex = t.kappa.split(":").pop(); + if (!seen.has(hex)) { seen.set(hex, { off: bodyTotal, len: t.nbytes }); bodyTotal += t.nbytes; } + order.push({ name: t.name, kappa: hex }); + } + const uniq = [...seen.entries()]; // [hex, {off,len}] + + const meta = JSON.stringify({ format: "holo/2", arch: f.arch, sourceRoot: f.rootKappa, nTensors: f.tensors.length, nBodies: uniq.length, order }); + const extKey = "gguf.header"; + const enc = new TextEncoder(); + const metaBytes = enc.encode(meta); + const extKeyBytes = enc.encode(extKey); + const extPayload = cat([u16(extKeyBytes.length), extKeyBytes, headerBytes]); // [keyLen][key][bytes] + const dirCount = uniq.length; + const dirBytes = 4 + dirCount * (32 + 8 + 8); // [count u32][κ(32) off(u64) len(u64)]× + const weightsLen = dirBytes + bodyTotal; + + // layout: header + section-table + ext + meta + weights + footer(32) + const sectionCount = 3; + const headSize = 4 + 2 + 2 + 2 + sectionCount * (1 + 8 + 8); + const extOff = headSize, metaOff = extOff + extPayload.length, weightsOff = metaOff + metaBytes.length; + const bodiesStart = weightsOff + dirBytes; + const fileLen = bodiesStart + bodyTotal + 32; + + const out = new Uint8Array(fileLen); + const dv = new DataView(out.buffer); + let p = 0; + out.set(MAGIC, p); p += 4; + dv.setUint16(p, VERSION, true); p += 2; + dv.setUint16(p, 0, true); p += 2; // flags + dv.setUint16(p, sectionCount, true); p += 2; + const sec = (kind, off, len) => { out[p] = kind; p += 1; dv.setBigUint64(p, BigInt(off), true); p += 8; dv.setBigUint64(p, BigInt(len), true); p += 8; }; + sec(K.Extension, extOff, extPayload.length); + sec(K.Metadata, metaOff, metaBytes.length); + sec(K.Weights, weightsOff, weightsLen); + out.set(extPayload, extOff); + out.set(metaBytes, metaOff); + // weights directory (ABSOLUTE file offsets) + bodies + dv.setUint32(weightsOff, dirCount, true); + let dp = weightsOff + 4; + const store = (h) => f.blocks.get(h); + for (const [hex, info] of uniq) { + out.set(HEX(hex), dp); dp += 32; + dv.setBigUint64(dp, BigInt(bodiesStart + info.off), true); dp += 8; + dv.setBigUint64(dp, BigInt(info.len), true); dp += 8; + out.set(store(hex), bodiesStart + info.off); + } + // footer = sha256 over everything before the footer → the .holo's identity + const footHex = sha256hex(out.subarray(0, fileLen - 32)); + out.set(HEX(footHex), fileLen - 32); + return { holo: out, rootHolo: didHolo("sha256", footHex), nBodies: uniq.length, nTensors: f.tensors.length, bytes: fileLen }; +} + +// ── generic archive writer ── +// Seal ANY content into the same MAGIC HOLO v2 structure as writeHolo (so readHolo / openHoloStream / +// makeKappaStore / the SW /.holo/sha256/<κ> route all read it unchanged) — used for non-GGUF κ-objects +// like a LoRA adapter. Caller supplies the deduped κ-bodies + a metadata object (with order:[{name,kappa}]) +// + an optional extension payload. Footer = sha256(everything) = the archive's did:holo identity. +export function writeHoloArchive({ meta, bodies, extKey = "holo.archive", extBytes = new Uint8Array(0) }) { + const enc = new TextEncoder(); + const metaBytes = enc.encode(JSON.stringify(meta)); + const extKeyBytes = enc.encode(extKey); + const extPayload = cat([u16(extKeyBytes.length), extKeyBytes, extBytes]); + const dirCount = bodies.length, dirBytes = 4 + dirCount * 48; + let bodyTotal = 0; for (const b of bodies) bodyTotal += b.bytes.length; + const sectionCount = 3, headSize = 4 + 2 + 2 + 2 + sectionCount * 17; + const extOff = headSize, metaOff = extOff + extPayload.length, weightsOff = metaOff + metaBytes.length; + const bodiesStart = weightsOff + dirBytes, fileLen = bodiesStart + bodyTotal + 32; + const out = new Uint8Array(fileLen), dv = new DataView(out.buffer); + let p = 0; out.set(MAGIC, p); p += 4; dv.setUint16(p, VERSION, true); p += 2; dv.setUint16(p, 0, true); p += 2; dv.setUint16(p, sectionCount, true); p += 2; + const sec = (kind, off, len) => { out[p] = kind; p += 1; dv.setBigUint64(p, BigInt(off), true); p += 8; dv.setBigUint64(p, BigInt(len), true); p += 8; }; + sec(K.Extension, extOff, extPayload.length); sec(K.Metadata, metaOff, metaBytes.length); sec(K.Weights, weightsOff, dirBytes + bodyTotal); + out.set(extPayload, extOff); out.set(metaBytes, metaOff); + dv.setUint32(weightsOff, dirCount, true); let dp = weightsOff + 4, bo = bodiesStart; + for (const b of bodies) { out.set(HEX(b.kappa), dp); dp += 32; dv.setBigUint64(dp, BigInt(bo), true); dp += 8; dv.setBigUint64(dp, BigInt(b.bytes.length), true); dp += 8; out.set(b.bytes, bo); bo += b.bytes.length; } + const footHex = sha256hex(out.subarray(0, fileLen - 32)); out.set(HEX(footHex), fileLen - 32); + return { holo: out, footer: didHolo("sha256", footHex), bytes: fileLen }; +} + +// ── reader ── +export function readHolo(bytes) { + const dv = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength); + for (let i = 0; i < 4; i++) if (bytes[i] !== MAGIC[i]) throw new Error("not a .holo"); + if (dv.getUint16(4, true) !== VERSION) throw new Error("holo version"); + const sectionCount = dv.getUint16(8, true); + let p = 10; const sections = {}; + for (let i = 0; i < sectionCount; i++) { const kind = bytes[p]; const off = Number(dv.getBigUint64(p + 1, true)); const len = Number(dv.getBigUint64(p + 9, true)); sections[kind] = { off, len }; p += 17; } + // footer verify (L5 on the whole archive) + const footHex = hexOf(bytes.subarray(bytes.length - 32)); + if (sha256hex(bytes.subarray(0, bytes.length - 32)) !== footHex) throw new Error("holo footer mismatch (tamper)"); + // extension: gguf header + const e = sections[K.Extension]; const keyLen = dv.getUint16(e.off, true); + const headerBytes = bytes.subarray(e.off + 2 + keyLen, e.off + e.len); + // metadata + const m = sections[K.Metadata]; + const meta = JSON.parse(new TextDecoder().decode(bytes.subarray(m.off, m.off + m.len))); + // weights directory → κ-hex → {offset(abs), len} + const w = sections[K.Weights]; const count = dv.getUint32(w.off, true); const dir = new Map(); + let dp = w.off + 4; + for (let i = 0; i < count; i++) { const hex = hexOf(bytes.subarray(dp, dp + 32)); dp += 32; const off = Number(dv.getBigUint64(dp, true)); dp += 8; const len = Number(dv.getBigUint64(dp, true)); dp += 8; dir.set(hex, { off, len }); } + const getBody = (kappaOrHex) => { const h = String(kappaOrHex).split(":").pop(); const d = dir.get(h); if (!d) throw new Error("κ not in holo: " + h); return bytes.subarray(d.off, d.off + d.len); }; + // a κ-store with L5 verify on every body fetch + const store = { get: (h) => { const d = dir.get(h); if (!d) return undefined; const b = bytes.subarray(d.off, d.off + d.len); if (sha256hex(b) !== h) throw new Error("holo L5 REFUSE " + h); return b; }, has: (h) => dir.has(h) }; + return { sections, meta, headerBytes, dir, getBody, store, footer: didHolo("sha256", footHex), rangeOf: (kappaOrHex) => dir.get(String(kappaOrHex).split(":").pop()) }; +} + +// ── streaming/partial reader: open a .holo over a Range reader (HTTP Range, or a +// file slice). Fetches only the header + directory up front (~the baked gguf +// metadata, a few MB); weight bodies are fetched on demand by absolute offset +// and verified per block (L5). This is the cold-load path. ── +export async function openHoloStream(rangeReader) { + const head = await rangeReader(0, 64); + const hdv = new DataView(head.buffer, head.byteOffset, head.byteLength); + for (let i = 0; i < 4; i++) if (head[i] !== MAGIC[i]) throw new Error("not a .holo"); + if (hdv.getUint16(4, true) !== VERSION) throw new Error("holo version"); + const sectionCount = hdv.getUint16(8, true); + const tbl = await rangeReader(10, sectionCount * 17); + const tdv = new DataView(tbl.buffer, tbl.byteOffset, tbl.byteLength); + const sections = {}; + for (let i = 0, p = 0; i < sectionCount; i++, p += 17) sections[tbl[p]] = { off: Number(tdv.getBigUint64(p + 1, true)), len: Number(tdv.getBigUint64(p + 9, true)) }; + // baked gguf header (metadata + tokenizer + tensor infos) + const e = sections[K.Extension], extBytes = await rangeReader(e.off, e.len); + const edv = new DataView(extBytes.buffer, extBytes.byteOffset, extBytes.byteLength); + const keyLen = edv.getUint16(0, true); + const headerBytes = extBytes.subarray(2 + keyLen); + // metadata (first-use order, name→κ) + const m = sections[K.Metadata], metaBytes = await rangeReader(m.off, m.len); + const meta = JSON.parse(new TextDecoder().decode(metaBytes)); + // weights directory (κ → absolute offset/len) — only the directory, not the bodies + const w = sections[K.Weights]; + const cntB = await rangeReader(w.off, 4); const count = new DataView(cntB.buffer, cntB.byteOffset, cntB.byteLength).getUint32(0, true); + const dirB = await rangeReader(w.off + 4, count * 48), ddv = new DataView(dirB.buffer, dirB.byteOffset, dirB.byteLength); + const dir = new Map(); + for (let i = 0, p = 0; i < count; i++, p += 48) dir.set(hexOf(dirB.subarray(p, p + 32)), { off: Number(ddv.getBigUint64(p + 32, true)), len: Number(ddv.getBigUint64(p + 40, true)) }); + // hardware-accelerated SHA-256 (WebCrypto, ~GB/s) so per-block L5 verify isn't the + // bottleneck at high bandwidth; falls back to pure-JS sha256hex if unavailable. + const cs = globalThis.crypto?.subtle; + const vhex = cs ? async (b) => { const d = new Uint8Array(await cs.digest("SHA-256", b)); let s = ""; for (const x of d) s += x.toString(16).padStart(2, "0"); return s; } : async (b) => sha256hex(b); + // fetch + L5-verify one weight body by κ + const getBody = async (kappaOrHex) => { const h = String(kappaOrHex).split(":").pop(); const d = dir.get(h); if (!d) throw new Error("κ not in holo: " + h); const b = await rangeReader(d.off, d.len); if (await vhex(b) !== h) throw new Error("holo L5 REFUSE " + h); return b; }; + // fetch a sub-range WITHIN a body (e.g. one token_embd row) — defers the rest + const getBodySlice = async (kappaOrHex, byteOff, byteLen) => { const d = dir.get(String(kappaOrHex).split(":").pop()); return rangeReader(d.off + byteOff, byteLen); }; + return { sections, meta, headerBytes, dir, getBody, getBodySlice, order: meta.order, bodyLen: (k) => dir.get(String(k).split(":").pop())?.len }; +} + +// ── helpers ── +const u16 = (n) => { const b = new Uint8Array(2); new DataView(b.buffer).setUint16(0, n, true); return b; }; +function cat(arrs) { let n = 0; for (const a of arrs) n += a.length; const o = new Uint8Array(n); let p = 0; for (const a of arrs) { o.set(a, p); p += a.length; } return o; } diff --git a/apps/q/holo-delta.mjs b/apps/q/holo-delta.mjs new file mode 100644 index 0000000000000000000000000000000000000000..541cfebc7c3dd05a488b97e84a5335ac6cc9f4ef --- /dev/null +++ b/apps/q/holo-delta.mjs @@ -0,0 +1,156 @@ +// holo-delta.mjs — A2 of the personal-model-zoo plan: family index-delta. A finetune that shares a base's +// FRAME (see holo-model-frame.mjs) is stored as `base-κ + per-tensor delta`, reconstructed at LOAD (no new +// kernel — the engine still reads normal quantized weights). Two wins compose: +// • FROZEN tensors (identical κ to the base) cost 0 extra bytes — content-addressing dedups them for free. +// • CHANGED tensors are stored as a BitDelta (Liu et al. 2024): sign(Δ) at 1 bit/param + one scale α. +// +// HONEST storage math (this is where the "5–9×" claim earns or loses): the zoo ratio is driven by the +// FROZEN-TENSOR FRACTION, not within-tensor sparsity. A LoRA/partial finetune (most tensors frozen) → +// 10–30×; a FULL finetune (every tensor moves) → only ~3–4× (BitDelta is ~1 bit/param vs a 3–4 bit base). +// BitDelta is intrinsically lossy (sign+scale captures ~2/π≈64% of a Gaussian delta's energy); its +// published result is that OUTPUT quality is nonetheless preserved — that must be confirmed by perplexity +// on a REAL base+finetune pair (the A2 pass-bar gate), which this codec self-test does not run. +// +// Pure JS, isomorphic, zero deps. Node self-test sweeps frozen-fraction + reports fidelity and zoo ratio. + +import { shareable } from "./holo-model-frame.mjs"; + +// ── BitDelta codec: Δ = ftW − baseW ; store sign(Δ) (1 bit) + α = mean|Δ| (the L2-optimal ±1 scale). ── +export function encodeBitDelta(baseW, ftW) { + const n = baseW.length, signBits = new Uint8Array((n + 7) >> 3); + let absSum = 0; + for (let i = 0; i < n; i++) { + const d = ftW[i] - baseW[i]; + if (d >= 0) signBits[i >> 3] |= 1 << (i & 7); // bit set ⇒ +1 + absSum += Math.abs(d); + } + return { kind: "bitdelta", n, alpha: absSum / n, signBits }; // ~1 bit/param + one f32 +} + +export function decodeBitDelta(baseW, rec, out) { + const n = rec.n, a = rec.alpha, o = out || new Float32Array(n); + for (let i = 0; i < n; i++) { + const pos = (rec.signBits[i >> 3] >> (i & 7)) & 1; + o[i] = baseW[i] + (pos ? a : -a); + } + return o; +} + +// fraction of the delta's ENERGY captured by sign+scale (1 = perfect). For a Gaussian Δ this → 2/π ≈ 0.637. +export function deltaCaptured(baseW, ftW, rec) { + const n = rec.n; let num = 0, den = 0; + for (let i = 0; i < n; i++) { + const d = ftW[i] - baseW[i], pos = (rec.signBits[i >> 3] >> (i & 7)) & 1, r = d - (pos ? rec.alpha : -rec.alpha); + num += r * r; den += d * d; + } + return den ? 1 - num / den : 1; +} + +// ── model-level delta: per tensor, frozen (κ identical) → ref (0 bytes) else bitdelta. Frame-guarded. ── +// tensors: { name → { kappa, params, baseW?, ftW? } }. baseW/ftW (dequant weights) only needed to encode +// CHANGED tensors; pass them for changed ones, omit for frozen (κ tells us they're identical). +export function deltaModel(baseMeta, ftMeta, baseTensors, ftTensors) { + if (!shareable(baseMeta, ftMeta)) throw new Error("holo-delta: base and finetune are not in the same frame/arch — cannot delta (see holo-model-frame.shareable)"); + const records = {}; let frozenParams = 0, changedParams = 0, deltaBytes = 0; + for (const name of Object.keys(ftTensors)) { + const b = baseTensors[name], f = ftTensors[name]; + if (b && f.kappa && b.kappa === f.kappa) { records[name] = { kind: "ref", kappa: b.kappa }; frozenParams += f.params || 0; continue; } + const rec = encodeBitDelta(f.baseW, f.ftW); + records[name] = { kind: "bitdelta", base: b ? b.kappa : null, alpha: rec.alpha, n: rec.n }; + changedParams += rec.n; deltaBytes += rec.signBits.length + 4; // bits + α + } + return { records, stats: { frozenParams, changedParams, deltaBytes, deltaBitsPerChangedParam: changedParams ? (deltaBytes * 8) / changedParams : 0 } }; +} + +// ── LOSSLESS byte-delta over stored quantized blocks (the right fit for κ-objects). A base and finetune +// tensor share the same packed length (same dims+fmt); store only the differing byte runs. Reconstruction +// is byte-identical to the finetune's own block ⇒ perplexity = standalone (NO quality gate). Falls back to +// storing the whole block when the diff isn't sparse. This is the default for the family loader. ── +const _GAP = 8; // coalesce changed runs separated by ≤_GAP identical bytes (amortizes per-run header) +export function encodeByteDelta(baseBytes, ftBytes) { + if (baseBytes.length !== ftBytes.length) return { kind: "whole", len: ftBytes.length, bytes: ftBytes }; + const n = ftBytes.length, runs = []; let i = 0, deltaSize = 0; + while (i < n) { + if (baseBytes[i] === ftBytes[i]) { i++; continue; } + let j = i + 1, gap = 0; // extend run, tolerating short identical gaps + while (j < n && (baseBytes[j] !== ftBytes[j] || (gap = run_gap(baseBytes, ftBytes, j)) <= _GAP)) { j += gap > 0 ? gap : 1; if (gap > 0) gap = 0; } + runs.push({ off: i, bytes: ftBytes.slice(i, j) }); deltaSize += (j - i) + 8; // bytes + ~8B run header + i = j; + } + if (deltaSize >= n * 0.9) return { kind: "whole", len: n, bytes: ftBytes }; // not sparse enough → store whole + return { kind: "bytedelta", len: n, runs }; +} +function run_gap(a, b, j) { let g = 0; while (j + g < a.length && a[j + g] === b[j + g]) g++; return g; } +export function applyByteDelta(baseBytes, rec) { + if (rec.kind === "whole") return rec.bytes; + const out = baseBytes.slice(0, rec.len); + for (const r of rec.runs) out.set(r.bytes, r.off); + return out; +} +export function byteDeltaSize(rec) { return rec.kind === "whole" ? rec.bytes.length : rec.runs.reduce((s, r) => s + r.bytes.length + 8, 0); } + +// compact binary (de)serialization for a byte-delta record — what gets gzipped + content-addressed. +export function serializeDelta(rec) { + if (rec.kind === "whole") { const o = new Uint8Array(5 + rec.bytes.length); new DataView(o.buffer).setUint32(1, rec.len); o[0] = 1; o.set(rec.bytes, 5); return o; } + let sz = 9; for (const r of rec.runs) sz += 8 + r.bytes.length; + const o = new Uint8Array(sz), dv = new DataView(o.buffer); o[0] = 0; dv.setUint32(1, rec.len); dv.setUint32(5, rec.runs.length); let p = 9; + for (const r of rec.runs) { dv.setUint32(p, r.off); dv.setUint32(p + 4, r.bytes.length); o.set(r.bytes, p + 8); p += 8 + r.bytes.length; } + return o; +} +export function parseDelta(u8) { + const dv = new DataView(u8.buffer, u8.byteOffset, u8.byteLength), len = dv.getUint32(1); + if (u8[0] === 1) return { kind: "whole", len, bytes: u8.subarray(5) }; + const nRuns = dv.getUint32(5), runs = []; let p = 9; + for (let i = 0; i < nRuns; i++) { const off = dv.getUint32(p), blen = dv.getUint32(p + 4); runs.push({ off, bytes: u8.subarray(p + 8, p + 8 + blen) }); p += 8 + blen; } + return { kind: "bytedelta", len, runs }; +} + +// zoo storage ratio vs storing N finetunes independently. baseBitsPerParam ≈ 3 (q3) or 4 (q4). +export function zooRatio({ totalParams, frozenFractionOfParams, N, baseBitsPerParam = 3 }) { + const B = baseBitsPerParam, g = 1 - frozenFractionOfParams; // g = changed fraction + const independent = (N + 1) * totalParams * B; + const shared = totalParams * B + N * (g * totalParams * 1); // base + N×(changed params × 1 bit) + return independent / shared; +} + +// ── Node self-test: realistic synthetic base + finetunes at varying frozen fractions. ── +if (typeof process !== "undefined" && process.argv[1] && process.argv[1].endsWith("holo-delta.mjs")) { + const n = 1 << 20; // 1M-param tensor + const rnd = (() => { let s = 0x2545f491; return () => { s ^= s << 13; s ^= s >>> 17; s ^= s << 5; s >>>= 0; return s / 4294967296; }; })(); + const gauss = (sig) => { let u = 0, v = 0; while (!u) u = rnd(); while (!v) v = rnd(); return sig * Math.sqrt(-2 * Math.log(u)) * Math.cos(2 * Math.PI * v); }; + const baseW = new Float32Array(n); for (let i = 0; i < n; i++) baseW[i] = gauss(0.02); // LLM-like weight σ + + console.log("\n— BitDelta fidelity on a CHANGED tensor (Δ ~ Gaussian) —"); + for (const dsig of [0.002, 0.02]) { + const ftW = new Float32Array(n); for (let i = 0; i < n; i++) ftW[i] = baseW[i] + gauss(dsig); + const rec = encodeBitDelta(baseW, ftW); const hat = decodeBitDelta(baseW, rec); + let relNum = 0, relDen = 0; for (let i = 0; i < n; i++) { const e = ftW[i] - hat[i]; relNum += e * e; relDen += ftW[i] * ftW[i]; } + const bpp = (rec.signBits.length + 4) * 8 / n; + console.log(` Δσ=${dsig}: α=${rec.alpha.toExponential(2)} energy-captured=${(deltaCaptured(baseW, ftW, rec) * 100).toFixed(1)}% recon ||err||/||ft||=${Math.sqrt(relNum / relDen).toExponential(2)} ${bpp.toFixed(3)} bits/param`); + } + + console.log("\n— zoo storage ratio (base + 50 finetunes) by frozen-tensor fraction —"); + for (const frozen of [0.0, 0.5, 0.8, 0.9, 0.98]) { + const r3 = zooRatio({ totalParams: 2e9, frozenFractionOfParams: frozen, N: 50, baseBitsPerParam: 3 }); + const r4 = zooRatio({ totalParams: 2e9, frozenFractionOfParams: frozen, N: 50, baseBitsPerParam: 4 }); + const tag = frozen === 0 ? "full finetune" : frozen >= 0.98 ? "LoRA-ish" : "partial"; + console.log(` frozen=${(frozen * 100).toFixed(0).padStart(3)}% → ${r3.toFixed(1)}× (q3 base) · ${r4.toFixed(1)}× (q4 base) [${tag}]`); + } + + console.log("\n— lossless byte-delta over a quantized block (reconstruct must be byte-identical) —"); + for (const changeFrac of [0.02, 0.2, 0.6]) { + const blkN = 1 << 20, base = new Uint8Array(blkN); for (let i = 0; i < blkN; i++) base[i] = (rnd() * 256) | 0; + const ft = base.slice(); for (let i = 0; i < blkN; i++) if (rnd() < changeFrac) ft[i] = (rnd() * 256) | 0; // finetune flips a fraction of bytes + const rec = encodeByteDelta(base, ft); const back = applyByteDelta(base, rec); + let identical = back.length === ft.length; for (let i = 0; identical && i < ft.length; i++) identical = back[i] === ft[i]; + console.log(` changed≈${(changeFrac * 100).toFixed(0)}% → ${rec.kind.padEnd(9)} ${(byteDeltaSize(rec) / 1024).toFixed(0)}KB vs ${(blkN / 1024).toFixed(0)}KB block (${(byteDeltaSize(rec) / blkN).toFixed(2)}×) reconstruct byte-identical=${identical}`); + } + + console.log("\n— frame guard —"); + const A = { frame: { fingerprint: "x" }, d: 2560, n_layers: 30, ff: 6912, n_heads: 20, n_kv_heads: 5, hd: 128, vocab: 128256 }; + const Bsame = { ...A }, Bdiff = { ...A, d: 3584 }; + console.log(` same frame+arch shareable: ${shareable(A, Bsame)} · different arch shareable: ${shareable(A, Bdiff)}`); + // and that deltaModel refuses a mismatched pair + try { deltaModel(A, Bdiff, {}, {}); console.log(" ERROR: deltaModel should have refused"); } + catch (e) { console.log(` deltaModel correctly refused mismatch: "${e.message.slice(0, 60)}…"`); } +} diff --git a/apps/q/holo-load-delta.mjs b/apps/q/holo-load-delta.mjs new file mode 100644 index 0000000000000000000000000000000000000000..8abdb67900d4a49b1b8ba23541f137249caba712 --- /dev/null +++ b/apps/q/holo-load-delta.mjs @@ -0,0 +1,54 @@ +// holo-load-delta.mjs — A2 LOAD wiring. Loads a family finetune stored as `base-κ + delta` (format +// "holo-delta/1", produced by _delta-build.mjs) and returns the SAME { manifest, fetchTensor, info } shape +// as holo-load2bit.loadKappaObject — so the GPU engine, KV-cache, and Q's brain loader are UNCHANGED. +// loadKappaObject delegates here automatically when it sees the holo-delta/1 format (one seam, hidden). +// +// Per tensor: ref → the base block (frozen, κ identical, dedup'd in the OPFS store); whole → the stored +// finetune block; bytedelta → base block + lossless byte-delta. Reconstruction is byte-identical to the +// finetune's own block ⇒ perplexity = standalone (no quality gate). The win is download/storage. +// +// Law L5: base AND delta blocks are each κ-verified (sha256(gz)==κ) before use; both κ are named in the +// L5-verified delta manifest, so the trust chain holds without re-hashing the reconstruction. +import { parseDelta, applyByteDelta } from "./holo-delta.mjs"; +import { reshapeTensor, buildEngineManifest } from "./holo-load2bit.mjs"; + +async function gunzip(u8) { const ds = new DecompressionStream("gzip"); const w = ds.writable.getWriter(); w.write(u8); w.close(); return new Uint8Array(await new Response(ds.readable).arrayBuffer()); } +const hex = (buf) => [...new Uint8Array(buf)].map((b) => b.toString(16).padStart(2, "0")).join(""); +const kFile = (k) => String(k).replace(":", "_"); // κ "sha256:" → block file "sha256_" + +export async function loadDeltaObject(deltaUrl, opts = {}) { + const droot = String(deltaUrl).replace(/\/+$/, ""); + const man = opts.manifest || await (await fetch(droot + "/manifest.json", { cache: "no-store" })).json(); + const baseRoot = String(opts.baseUrl || man.base?.url || man.base?.dir || "").replace(/\/+$/, ""); + if (!baseRoot) throw new Error("delta-load: no base location (opts.baseUrl or manifest.base.url)"); + + // κ-verified block fetch against a chosen root (base for frozen/base blocks, delta for delta blocks). + const getBlock = async (root, kappa) => { + const gz = new Uint8Array(await (await fetch(root + "/b/" + kFile(kappa) + ".gz", { cache: "no-store" })).arrayBuffer()); + const got = "sha256:" + hex(await crypto.subtle.digest("SHA-256", gz)); + if (got !== kappa) throw new Error("delta-load κ MISMATCH " + String(kappa).slice(0, 24)); + return gunzip(gz); + }; + + const normRecs = {}; // {name → {N,K,fmt,fp16?,s?}} for the shared manifest builder + for (const [name, dr] of Object.entries(man.tensors)) normRecs[name] = dr.meta || {}; + + const fetchTensor = async (name) => { + const dr = man.tensors[name]; if (!dr) return new Uint8Array(0); + let raw; + if (dr.kind === "ref") raw = await getBlock(baseRoot, dr.kappa); // frozen + else if (dr.kind === "whole") raw = parseDelta(await getBlock(droot, dr.delta)).bytes; // stored whole + else if (dr.kind === "bytedelta") raw = applyByteDelta(await getBlock(baseRoot, dr.base), parseDelta(await getBlock(droot, dr.delta))); + else throw new Error("delta-load: unknown record kind " + dr.kind + " for " + name); + return reshapeTensor(dr.meta || {}, raw); + }; + + let e8lutData; // E₈ LUT (if any) is a frozen base block + if (man.e8lut) { const b = await getBlock(baseRoot, man.e8lut.replace(/^did:holo:/, "")); e8lutData = new Float32Array(b.buffer, b.byteOffset, 2048); } + const manifest = buildEngineManifest(man, normRecs, e8lutData); + // tokenizer is the base's (finetune shares vocab); resolve a relative source against the BASE dir. + if (man.source && !/^https?:\/\//.test(man.source)) man.source = baseRoot + "/" + man.source; + return { manifest, fetchTensor, info: man }; +} + +export default loadDeltaObject; diff --git a/apps/q/holo-load2bit.mjs b/apps/q/holo-load2bit.mjs new file mode 100644 index 0000000000000000000000000000000000000000..28aeaa0192bc261ce430e2b0e527e38ed4bac248 --- /dev/null +++ b/apps/q/holo-load2bit.mjs @@ -0,0 +1,141 @@ +// holo-load2bit.mjs — the LOAD-DIRECT consumer (the "load" half of the 7B infra). Given a pre-compiled +// 2-bit κ-object (manifest.json + content-addressed b/<κ>.gz blocks, produced by compile2bit.mjs), it builds +// the engine manifest + a fetchTensor that streams blocks, verifies each by re-deriving its κ (Law L5), +// gunzips, and hands the engine the weights ALREADY 2-bit — no re-quant at load. The engine reads +// manifest.preQuantized=true (parts() returns the blocks verbatim) and incoherent=false (LDLQ ⇒ no FWHT). +// Hosting = serve the κ-object dir from anywhere; the κ-verify makes any mirror untrusted-safe. +// +// A family FINETUNE is stored as `base-κ + delta` (format "holo-delta/1"); loadKappaObject detects that and +// transparently delegates to holo-load-delta.mjs, which reconstructs the finetune's blocks and returns the +// SAME { manifest, fetchTensor } shape — so the engine, KV-cache, and Q's brain loader need no changes. +import { f16ToF32 } from "./qvac-ingest.mjs"; + +async function gunzip(u8) { const ds = new DecompressionStream("gzip"); const w = ds.writable.getWriter(); w.write(u8); w.close(); return new Uint8Array(await new Response(ds.readable).arrayBuffer()); } +const hex = (buf) => [...new Uint8Array(buf)].map((b) => b.toString(16).padStart(2, "0")).join(""); + +// ── PERSISTENT κ-CACHE (the returning-user path): a κ-object's manifest + blocks are content-addressed and +// IMMUTABLE, so once fetched they can live in the browser's Cache API FOREVER, keyed by their own URL. A +// returning user then loads Q from local disk — ~0 network, no server (serverless by construction). It is +// untrusted-safe: every cached block is still L5 re-derived below, so a poisoned cache entry is rejected +// exactly like a poisoned network body. First visit warms the cache; every visit after is disk-speed. ── +const KCACHE = "holo-kappa-v1"; +let _persistAsked = false; +async function _askPersist() { + if (_persistAsked) return; _persistAsked = true; // once per session: ask the browser NOT to evict the model under storage pressure + try { if (navigator.storage && navigator.storage.persist && !(await navigator.storage.persisted())) await navigator.storage.persist(); } catch (e) {} +} +// fetch a URL as bytes, disk-cached by URL. cache HIT → no network. MISS → fetch once (no-store bounds the +// in-flight RAM to one body) then store to disk. All failures fall back to a plain fetch (never block a load). +const _inflight = new Map(); // URL → in-flight fetch promise, so a parallel prefetch + the engine's read of the + // SAME block share ONE network fetch (never double-download). +async function cachedBytes(url) { + let cache = null; + try { cache = await caches.open(KCACHE); const hit = await cache.match(url); if (hit) return new Uint8Array(await hit.arrayBuffer()); } catch (e) { cache = null; } + if (_inflight.has(url)) return _inflight.get(url); + const p = (async () => { + const buf = await (await fetch(url, { cache: "no-store" })).arrayBuffer(); + if (cache) { try { await cache.put(url, new Response(buf.slice(0), { headers: { "Content-Type": "application/octet-stream" } })); _askPersist(); } catch (e) {} } + return new Uint8Array(buf); + })(); + _inflight.set(url, p); + try { return await p; } finally { _inflight.delete(url); } +} +// FAST FIRST LOAD: warm the whole block cache with bounded concurrency, so the engine's sequential per-tensor +// reads hit the cache instead of paying one HF round-trip at a time (~1.5 blocks/s → tens of blocks/s over HTTP/2). +// Fire-and-forget; the engine's getBlock shares any in-flight fetch (no double-download). Cross-origin-CDN safe. +function prefetchBlocks(baseUrl, kappas, conc = 12) { + try { + const urls = [...new Set(kappas.filter(Boolean))].map((k) => baseUrl + "/b/" + String(k).replace(":", "_") + ".gz"); + let i = 0; + const worker = async () => { while (i < urls.length) { const u = urls[i++]; try { await cachedBytes(u); } catch (e) {} } }; + Promise.all(Array.from({ length: Math.min(conc, urls.length) }, worker)).catch(() => {}); + } catch (e) {} +} + +// reshape a raw (gunzipped) block to what the engine reads. PURE — shared with the delta loader so both +// paths apply identical per-fmt handling. 2bit+fp16 → 2bit+f32 scales; everything else verbatim. +export function reshapeTensor(rec, raw) { + if (rec.fmt === "2bit" && rec.fp16) { // [2-bit packed][fp16 scales] → [2-bit][f32 scales] + const Kp = rec.K, q2 = (rec.N * Kp) / 4, nsc = rec.N * (Kp / 32); + const f16 = new Uint16Array(raw.buffer, raw.byteOffset + q2, nsc); + const out = new Uint8Array(q2 + nsc * 4); out.set(raw.subarray(0, q2), 0); + const f32 = new Float32Array(out.buffer, q2, nsc); for (let i = 0; i < nsc; i++) f32[i] = f16ToF32(f16[i]); + return out; + } + return raw; // 2bit+f32 (incoherence), q8 (embed), f32 (norms) — verbatim +} + +// build the engine manifest from model meta + normalized tensor records {name→{N,K,fmt,s?}}. PURE — shared. +export function buildEngineManifest(man, normRecs, e8lutData) { + const tensors = Object.entries(normRecs).map(([name, rec]) => ({ name, N: rec.N, K: rec.K, blk: rec.fmt !== "f32", fmt: rec.fmt, ...(rec.s !== undefined ? { s: rec.s } : {}) })); + const native = man.mode === "q4" || man.mode === "q3" || man.mode === "e8" || man.mode === "bitnet"; // native-bits κ-object + return { + d: man.d, n_heads: man.n_heads, n_kv_heads: man.n_kv_heads, ff: man.ff, vocab: man.vocab, n_layers: man.n_layers, hd: man.hd, + bits: native ? man.bits : 8, layout: man.layout, rope_base: man.rope_base, ...(man.maskId !== undefined ? { maskId: man.maskId, diffusion: true } : {}), attn_bias: man.attn_bias, qk_norm: man.qk_norm, qk_norm_dim: man.qk_norm_dim, tied: man.tied, + ...(man.sub_norm ? { sub_norm: true } : {}), ...(man.bitlinear ? { bitlinear: true } : {}), ...(man.ffn_act ? { ffn_act: man.ffn_act } : {}), ...(man.moe ? { moe: man.moe } : {}), + ...(native ? {} : { twoBit: true, incoherent: man.incoherent === true, preQuantized: true }), tensors, ...(e8lutData ? { e8lutData } : {}), + }; +} + +export async function loadKappaObject(baseUrl, opts = {}) { + // Law L5: the manifest is the ROOT that names every block's κ. Verify the manifest's OWN bytes + // re-derive to a pinned κ BEFORE trusting man.tensors[*].kappa — otherwise a tampered manifest can + // re-point every block to a forged-but-self-consistent κ and each per-block check passes against the + // forgery. The pin is an EXTERNAL anchor (catalog/lock), never the manifest's own self-asserted root. + const manRaw = await cachedBytes(baseUrl + "/manifest.json"); // disk-cached (pinned + verified below), so a returning user's load is 0-network end to end + const manKappa = "sha256:" + hex(await crypto.subtle.digest("SHA-256", manRaw)); + const pin = opts.expectKappa ? String(opts.expectKappa).replace(/^did:holo:/, "") : null; + if (pin) { if (manKappa !== pin) throw new Error("manifest κ MISMATCH (Law L5): " + manKappa.slice(0, 24) + "… ≠ pinned " + pin.slice(0, 24) + "…"); } + else if (!opts.allowUnpinned) throw new Error("manifest unpinned (Law L5): pass opts.expectKappa (catalog pin) or opts.allowUnpinned for dev"); + const man = JSON.parse(new TextDecoder().decode(manRaw)); + // CANONICAL BLAKE3 (Law L1), best-effort + fully gated: if the object publishes a sha256→blake3 map, + // verify each block's canonical BLAKE3 κ IN ADDITION to its sha256 transport κ (both over the stored + // gzipped block — sha256(gz)=name, blake3(gz)=canonical; proven on real q-bitnet-2b). Absent map or no + // wasm → b3 stays null → sha256-only, i.e. byte-for-byte today's behavior (no-op until the map ships). + let b3map = null, b3 = null; + if (opts.blake3 !== false) try { + const fn = (await import("./pkg/holospaces_web.js")).kappa; + let ok = false; try { ok = fn(new Uint8Array([1])).startsWith("blake3:"); } catch (e) { ok = false; } // wasm already init'd by loader.ready() + if (ok) { + if (opts.blake3Map) b3map = opts.blake3Map; // injected map (test before the HF upload) + else { const mr = await fetch(baseUrl + "/sha256-to-blake3.map.json"); if (mr.ok) b3map = await mr.json(); } + if (b3map) b3 = fn; + } + } catch (e) { b3map = null; b3 = null; } + // FAMILY FINETUNE: a `base-κ + delta` object — reconstruct via the delta loader (same return shape). + if (man.format === "holo-delta/1") return (await import("./holo-load-delta.mjs")).loadDeltaObject(baseUrl, { ...opts, manifest: man }); + // FAST FIRST LOAD: prefetch every block into the cache in parallel while the engine builds (turns a + // latency-bound sequential stream into a bandwidth-bound one — critical when serving off a remote CDN like HF). + if (opts.prefetch !== false) { try { prefetchBlocks(baseUrl, Object.values(man.tensors || {}).map((r) => r.kappa)); } catch (e) {} } + // RAM-bounded, DISK-cached: the engine fetches each tensor once, so decoded blocks are handed over and + // released — in-flight RAM stays ~one block (a 7B κ-object decompresses to >2.6 GB). The gzipped block + // bytes are persisted to the Cache API by their content-addressed URL (cachedBytes), so a returning user + // reads them from disk with no network. Every block — cached or fresh — is L5 re-derived (κ must match), + // so the cache is untrusted-safe. + const getBlock = async (kappa) => { + const gz = await cachedBytes(baseUrl + "/b/" + kappa.replace(":", "_") + ".gz"); + const got = "sha256:" + hex(await crypto.subtle.digest("SHA-256", gz)); // Law L5: re-derive the transport κ + if (got !== kappa) throw new Error("κ MISMATCH " + kappa.slice(0, 24)); + if (b3 && b3map) { const want = b3map[kappa]; if (want) { if (b3(gz) !== want) throw new Error("BLAKE3 κ MISMATCH " + kappa.slice(0, 24)); if (typeof window !== "undefined") window.__b3n = (window.__b3n || 0) + 1; } } // Law L1 canonical axis + return await gunzip(gz); + }; + const fetchTensor = async (name) => { + const rec = man.tensors[name]; if (!rec) return new Uint8Array(0); + return reshapeTensor(rec, await getBlock(rec.kappa)); + }; + // E₈ codebook (mode e8): the 256×8 LUT is its own content-addressed block — fetch + κ-verify (Law L5) + let e8lutData; + if (man.e8lut) { const b = await getBlock(man.e8lut.replace(/^did:holo:/, "")); e8lutData = new Float32Array(b.buffer, b.byteOffset, 2048); } + const manifest = buildEngineManifest(man, man.tensors, e8lutData); + // bundled tokenizer (SERVERLESS load): the header (tokenizer + arch) should load same-origin/on-device, no + // external host. A RELATIVE `source` already resolves against the κ-object's own dir. When the manifest + // declares a REMOTE (http) source but the κ-object ALSO ships a local tokenizer.gguf, PREFER the bundle — + // one cheap HEAD probe, and the manifest bytes (hence its Law-L5 pin) stay untouched. No bundle served → + // fall back to the declared source. This makes a κ-object with a bundled header 100% serverless to load. + const base = baseUrl.replace(/\/+$/, ""); + if (man.source && !/^https?:\/\//.test(man.source)) man.source = base + "/" + man.source; + else if (man.source && /^https?:\/\//.test(man.source)) { + try { const h = await fetch(base + "/tokenizer.gguf", { method: "HEAD" }); if (h && h.ok) man.source = base + "/tokenizer.gguf"; } catch (e) {} + } + return { manifest, fetchTensor, info: man }; +} diff --git a/apps/q/holo-model-frame.mjs b/apps/q/holo-model-frame.mjs new file mode 100644 index 0000000000000000000000000000000000000000..15539d9f8c3374df2db2339142a4c14ab44c1e60 --- /dev/null +++ b/apps/q/holo-model-frame.mjs @@ -0,0 +1,106 @@ +// holo-model-frame.mjs — A1 of the personal-model-zoo plan: the CANONICAL SHARED FRAME standard + a +// conformance gate, so every κ-addressable .holo LLM DECLARES the quantization frame it lives in. +// +// Why this exists: two models can family-dedup / index-delta (A2) ONLY if their unchanged tensors +// produce BYTE-IDENTICAL κ-blocks — which requires the SAME quantization transform ("frame"). The frame +// is the transform identity (codec, layout, bits, mode, twoBit, incoherence), NOT the model's weights or +// dims. The incoherence rotation is already deterministic (signed-FWHT seeded by tensor width K, see +// e8-quant.mjs signsFor), so a frame is reproducible from its descriptor alone. +// +// HONEST SCOPE: this does NOT requantize anything. Today's models are standard quant frames +// (holo-quant/-); the shared E8 frame (atlas-e8/v1) is what models compiled through the +// incoherent-E8 path carry. Moving everything into atlas-e8/v1 is Track B (gated on the 2-bit quality +// experiment), deliberately not done here. A1 just makes the frame EXPLICIT and ENFORCED. +// +// Pure JS, isomorphic (browser + Node 18+), zero deps. Node self-test scans ./models//manifest.json. + +const FRAME_V = 1; + +// the fields that DEFINE a quantization transform — two models with the same fingerprint apply the +// identical transform, so identical weights ⇒ identical κ-blocks (the precondition for A2 dedup). +const FRAME_KEYS = ["codec", "layout", "bits", "mode", "twoBit", "incoherent", "grid"]; + +async function sha256hex(str) { + const u8 = new TextEncoder().encode(str); + const d = await (globalThis.crypto || (await import("node:crypto")).webcrypto).subtle.digest("SHA-256", u8); + return Array.from(new Uint8Array(d), (b) => b.toString(16).padStart(2, "0")).join(""); +} + +// extract the frame-defining descriptor from a compiled-model manifest (holo-2bit/1 shape). +export function frameDescriptor(m) { + const d = { + codec: m.format || "unknown", // e.g. "holo-2bit/1" + layout: m.layout || null, // e.g. "q3f" + bits: m.bits ?? null, // e.g. 3 + mode: m.mode || null, // e.g. "bitnet" | "q3" + twoBit: !!m.twoBit, + incoherent: !!m.incoherent, // QuIP#-style signed-FWHT rotation applied + grid: m.grid || (m.incoherent ? "e8" : "scalar") // e8 lattice vs scalar grid + }; + return d; +} + +// the canonical id for a frame — atlas-e8/v1 is THE shared frame family members ride; everything else +// declares its own standalone quant frame (still conformant, just not E8-shareable). +export function frameId(desc) { + if (desc.incoherent && desc.grid === "e8") return "atlas-e8/v1"; + return `holo-quant/${desc.layout || desc.mode || "q"}-${desc.bits ?? "x"}bit`; +} + +export async function frameFingerprint(desc) { + const canon = JSON.stringify(FRAME_KEYS.map((k) => [k, desc[k]])); // fixed key order ⇒ stable hash + return (await sha256hex(canon)).slice(0, 32); +} + +// STAMP: add an explicit, self-verifying frame block to a manifest (idempotent). +export async function stampFrame(m) { + const desc = frameDescriptor(m); + m.frame = { v: FRAME_V, id: frameId(desc), fingerprint: await frameFingerprint(desc), ...desc }; + return m; +} + +// CONFORMANCE GATE: a .holo LLM must DECLARE a frame whose fingerprint re-derives from its own fields. +// Rejects: no frame block, tampered/mismatched fingerprint, or unknown frame version. +export async function checkFrame(m) { + if (!m || !m.frame) return { conforms: false, reason: "no frame declared (run stampFrame at compile)" }; + if (m.frame.v !== FRAME_V) return { conforms: false, reason: `unknown frame version ${m.frame.v}` }; + const want = await frameFingerprint(frameDescriptor(m)); + if (m.frame.fingerprint !== want) return { conforms: false, reason: `fingerprint mismatch (manifest changed since stamp): ${m.frame.fingerprint} != ${want}` }; + return { conforms: true, id: m.frame.id, fingerprint: m.frame.fingerprint, shared: m.frame.id === "atlas-e8/v1" }; +} + +// can two stamped models family-dedup / index-delta? Same transform (fingerprint) AND same architecture. +export function shareable(a, b) { + if (!a.frame || !b.frame) return false; + if (a.frame.fingerprint !== b.frame.fingerprint) return false; + for (const k of ["d", "n_layers", "ff", "n_heads", "n_kv_heads", "hd", "vocab"]) if (a[k] !== b[k]) return false; + return true; +} + +// ── Node self-test: scan ./models/*/manifest.json, stamp + gate them, group by family-dedup class, +// and prove the gate rejects a planted non-conformer. ── +if (typeof process !== "undefined" && process.argv[1] && process.argv[1].endsWith("holo-model-frame.mjs")) { + const fs = await import("node:fs"); const path = await import("node:path"); + const root = path.join(process.cwd(), "models"); + const dirs = fs.existsSync(root) ? fs.readdirSync(root).filter((d) => fs.existsSync(path.join(root, d, "manifest.json"))) : []; + const rows = [], byClass = {}; + for (const name of dirs) { + let m; try { m = JSON.parse(fs.readFileSync(path.join(root, name, "manifest.json"), "utf8")); } catch { continue; } + await stampFrame(m); + const chk = await checkFrame(m); + const key = `${m.frame.id} · d${m.d}·L${m.n_layers}·ff${m.ff}`; + (byClass[key] ||= []).push(name); + rows.push({ model: name, id: m.frame.id, fp: m.frame.fingerprint.slice(0, 12), conforms: chk.conforms, shared: !!chk.shared }); + } + console.log("\n— frame stamp + conformance over ./models —"); + for (const r of rows) console.log(` ${r.conforms ? "✓" : "✗"} ${r.model.padEnd(16)} ${r.id.padEnd(22)} fp=${r.fp} ${r.shared ? "[E8-shared]" : "[standalone]"}`); + console.log("\n— family-dedup classes (same frame + same arch ⇒ A2 can dedup/delta) —"); + for (const [k, v] of Object.entries(byClass)) console.log(` ${v.length}× ${k}\n ${v.join(", ")}`); + // gate must REJECT a non-conformer + const planted = { format: "holo-2bit/1", layout: "q3f", bits: 3 }; // no frame block + const bad = await checkFrame(planted); + const tampered = await stampFrame({ format: "holo-2bit/1", layout: "q3f", bits: 3, incoherent: false }); + tampered.bits = 2; // change a frame-defining field AFTER stamping → stored fingerprint must no longer match + const badly = await checkFrame(tampered); + console.log(`\n— gate rejection proof —\n no-frame: conforms=${bad.conforms} (${bad.reason})\n tampered: conforms=${badly.conforms} (${badly.reason})`); +} diff --git a/apps/q/pkg/holospaces_web.js b/apps/q/pkg/holospaces_web.js new file mode 100644 index 0000000000000000000000000000000000000000..d2ef95648519b88439c96fec276faabbf2084b0c --- /dev/null +++ b/apps/q/pkg/holospaces_web.js @@ -0,0 +1,3285 @@ +/* @ts-self-types="./holospaces_web.d.ts" */ + +/** + * The **messenger peer** in the browser tab: it mints the channel's content + * (channels, signed messages forming a causal DAG, feed heads) at the + * substrate's real blake3 κ, holds it in a content-addressed store, ingests + * what other peers publish (verifying on receipt, Law L5), and linearises the + * DAG into one deterministic transcript. The transport (announce / discover / + * fetch over the κ pub/sub relay) is JavaScript's `WsKappaSync`; this peer is + * the content + identity half. No homeserver (ADR-001). + */ +export class ChatPeer { + __destroy_into_raw() { + const ptr = this.__wbg_ptr; + this.__wbg_ptr = 0; + ChatPeerFinalization.unregister(this); + return ptr; + } + free() { + const ptr = this.__destroy_into_raw(); + wasm.__wbg_chatpeer_free(ptr, 0); + } + /** + * This peer's 32-byte X25519 **encryption** public key — published so an + * admin can seal a channel's group key to it ([`ChatPeer::seal_key_to`]). + * @returns {Uint8Array} + */ + encryption_key() { + const ret = wasm.chatpeer_encryption_key(this.__wbg_ptr); + var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v1; + } + /** + * Publish this peer's signed feed head for `channel` — the mutable pointer + * other peers resolve to discover its latest. Returns the feed κ. + * @param {string} channel + * @param {number} timestamp_ms + * @returns {string} + */ + feed(channel, timestamp_ms) { + let deferred3_0; + let deferred3_1; + try { + const ptr0 = passStringToWasm0(channel, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.chatpeer_feed(this.__wbg_ptr, ptr0, len0, timestamp_ms); + var ptr2 = ret[0]; + var len2 = ret[1]; + if (ret[3]) { + ptr2 = 0; len2 = 0; + throw takeFromExternrefTable0(ret[2]); + } + deferred3_0 = ptr2; + deferred3_1 = len2; + return getStringFromWasm0(ptr2, len2); + } finally { + wasm.__wbindgen_free(deferred3_0, deferred3_1, 1); + } + } + /** + * The head message κ a feed points at — JS walks parents back from here. + * @param {Uint8Array} bytes + * @returns {string} + */ + feed_head(bytes) { + let deferred3_0; + let deferred3_1; + try { + const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.chatpeer_feed_head(this.__wbg_ptr, ptr0, len0); + var ptr2 = ret[0]; + var len2 = ret[1]; + if (ret[3]) { + ptr2 = 0; len2 = 0; + throw takeFromExternrefTable0(ret[2]); + } + deferred3_0 = ptr2; + deferred3_1 = len2; + return getStringFromWasm0(ptr2, len2); + } finally { + wasm.__wbindgen_free(deferred3_0, deferred3_1, 1); + } + } + /** + * The operator's content-addressed identity κ (the κ of the ed25519 public + * key). + * @returns {string} + */ + identity() { + let deferred1_0; + let deferred1_1; + try { + const ret = wasm.chatpeer_identity(this.__wbg_ptr); + deferred1_0 = ret[0]; + deferred1_1 = ret[1]; + return getStringFromWasm0(ret[0], ret[1]); + } finally { + wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); + } + } + /** + * The operator identity as a **W3C `did:key`** — the standard, registry-free + * representation of this ed25519 public key (`did:key:z6Mk…`): multibase + * base58btc of the multicodec `ed25519-pub` (0xed01) prefix + the 32-byte + * key. Lets the messenger's self-sovereign identity interoperate with the + * open decentralized-identity ecosystem (DIDs / Verifiable Credentials) + * without changing our content-addressed `Operator` κ. + * @returns {string} + */ + identity_did() { + let deferred1_0; + let deferred1_1; + try { + const ret = wasm.chatpeer_identity_did(this.__wbg_ptr); + deferred1_0 = ret[0]; + deferred1_1 = ret[1]; + return getStringFromWasm0(ret[0], ret[1]); + } finally { + wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); + } + } + /** + * Ingest bytes a peer fetched over the relay, verifying on receipt (the κ is + * re-derived from the bytes, Law L5 — forged content cannot enter). If the + * bytes are a message, its head advances this peer's view of the channel. + * Returns the κ. + * @param {Uint8Array} bytes + * @returns {string} + */ + ingest(bytes) { + let deferred3_0; + let deferred3_1; + try { + const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.chatpeer_ingest(this.__wbg_ptr, ptr0, len0); + var ptr2 = ret[0]; + var len2 = ret[1]; + if (ret[3]) { + ptr2 = 0; len2 = 0; + throw takeFromExternrefTable0(ret[2]); + } + deferred3_0 = ptr2; + deferred3_1 = len2; + return getStringFromWasm0(ptr2, len2); + } finally { + wasm.__wbindgen_free(deferred3_0, deferred3_1, 1); + } + } + /** + * **Install an epoch key from a published envelope** (member): verify the + * [`KeyEnvelope`] under the trusted channel `admin_pubkey` (rejecting a + * relay-injected or non-admin envelope), find this peer's HPKE wrap, open it, + * and adopt the key as the channel's current epoch. Returns the epoch + * installed, or `undefined` if the envelope is unauthentic or this peer is + * not one of its recipients (e.g. a removed member). + * @param {Uint8Array} bytes + * @param {Uint8Array} admin_pubkey + * @returns {number | undefined} + */ + ingest_key_envelope(bytes, admin_pubkey) { + const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passArray8ToWasm0(admin_pubkey, wasm.__wbindgen_malloc); + const len1 = WASM_VECTOR_LEN; + const ret = wasm.chatpeer_ingest_key_envelope(this.__wbg_ptr, ptr0, len0, ptr1, len1); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); + } + return ret[0] === 0 ? undefined : ret[1]; + } + /** + * A message's causal parent κs — the edges JS follows to pull history. + * @param {Uint8Array} bytes + * @returns {any[]} + */ + message_parents(bytes) { + const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.chatpeer_message_parents(this.__wbg_ptr, ptr0, len0); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); + } + var v2 = getArrayJsValueFromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 4, 4); + return v2; + } + /** + * Open a messenger peer signing as the operator whose self-sovereign public + * key is `public_key` (its κ is the author identity, Law L1). + * @param {Uint8Array} secret + */ + constructor(secret) { + const ptr0 = passArray8ToWasm0(secret, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.chatpeer_new(ptr0, len0); + this.__wbg_ptr = ret; + ChatPeerFinalization.register(this, this.__wbg_ptr, this); + return this; + } + /** + * The canonical bytes of a stored object, to hand to `WsKappaSync.announce`. + * @param {string} kappa + * @returns {Uint8Array | undefined} + */ + object(kappa) { + const ptr0 = passStringToWasm0(kappa, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.chatpeer_object(this.__wbg_ptr, ptr0, len0); + let v2; + if (ret[0] !== 0) { + v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + } + return v2; + } + /** + * Open a channel; its κ is the genesis every message refers back to. The + * genesis bytes are stored so the peer can publish them. Returns the κ. + * @param {string} name + * @param {number} created_ms + * @returns {string} + */ + open_channel(name, created_ms) { + let deferred3_0; + let deferred3_1; + try { + const ptr0 = passStringToWasm0(name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.chatpeer_open_channel(this.__wbg_ptr, ptr0, len0, created_ms); + var ptr2 = ret[0]; + var len2 = ret[1]; + if (ret[3]) { + ptr2 = 0; len2 = 0; + throw takeFromExternrefTable0(ret[2]); + } + deferred3_0 = ptr2; + deferred3_1 = len2; + return getStringFromWasm0(ptr2, len2); + } finally { + wasm.__wbindgen_free(deferred3_0, deferred3_1, 1); + } + } + /** + * Open a group key HPKE-sealed to this peer with + * [`seal_key_to`](ChatPeer::seal_key_to). + * @param {Uint8Array} wrapped + * @returns {Uint8Array} + */ + open_sealed_key(wrapped) { + const ptr0 = passArray8ToWasm0(wrapped, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.chatpeer_open_sealed_key(this.__wbg_ptr, ptr0, len0); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); + } + var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v2; + } + /** + * Post a signed message into `channel`, parented on this peer's current head + * for that channel (its causal view). Stores it and advances the head. + * Returns the message κ. + * @param {string} channel + * @param {number} timestamp_ms + * @param {string} body + * @returns {string} + */ + post(channel, timestamp_ms, body) { + let deferred4_0; + let deferred4_1; + try { + const ptr0 = passStringToWasm0(channel, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passStringToWasm0(body, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len1 = WASM_VECTOR_LEN; + const ret = wasm.chatpeer_post(this.__wbg_ptr, ptr0, len0, timestamp_ms, ptr1, len1); + var ptr3 = ret[0]; + var len3 = ret[1]; + if (ret[3]) { + ptr3 = 0; len3 = 0; + throw takeFromExternrefTable0(ret[2]); + } + deferred4_0 = ptr3; + deferred4_1 = len3; + return getStringFromWasm0(ptr3, len3); + } finally { + wasm.__wbindgen_free(deferred4_0, deferred4_1, 1); + } + } + /** + * Post a **sealed** message: the `plaintext` is AEAD-encrypted under the + * 32-byte channel `key` before it becomes the (opaque) message body, so the + * relay and non-members see only ciphertext. The message is still signed + * (authenticity) and the ciphertext is bound into its κ (Law L5). Returns + * the message κ. + * @param {string} channel + * @param {number} timestamp_ms + * @param {string} plaintext + * @param {Uint8Array} key + * @returns {string} + */ + post_sealed(channel, timestamp_ms, plaintext, key) { + let deferred5_0; + let deferred5_1; + try { + const ptr0 = passStringToWasm0(channel, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passStringToWasm0(plaintext, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len1 = WASM_VECTOR_LEN; + const ptr2 = passArray8ToWasm0(key, wasm.__wbindgen_malloc); + const len2 = WASM_VECTOR_LEN; + const ret = wasm.chatpeer_post_sealed(this.__wbg_ptr, ptr0, len0, timestamp_ms, ptr1, len1, ptr2, len2); + var ptr4 = ret[0]; + var len4 = ret[1]; + if (ret[3]) { + ptr4 = 0; len4 = 0; + throw takeFromExternrefTable0(ret[2]); + } + deferred5_0 = ptr4; + deferred5_1 = len4; + return getStringFromWasm0(ptr4, len4); + } finally { + wasm.__wbindgen_free(deferred5_0, deferred5_1, 1); + } + } + /** + * Post to a closed channel under its **current epoch key** (E2E sealed). The + * body is `epoch(4) ‖ ChaCha20-Poly1305(group_key, plaintext)`, so a reader + * selects the right epoch key and a removed member cannot read new epochs. + * @param {string} channel + * @param {number} timestamp_ms + * @param {string} plaintext + * @returns {string} + */ + post_to(channel, timestamp_ms, plaintext) { + let deferred4_0; + let deferred4_1; + try { + const ptr0 = passStringToWasm0(channel, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passStringToWasm0(plaintext, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len1 = WASM_VECTOR_LEN; + const ret = wasm.chatpeer_post_to(this.__wbg_ptr, ptr0, len0, timestamp_ms, ptr1, len1); + var ptr3 = ret[0]; + var len3 = ret[1]; + if (ret[3]) { + ptr3 = 0; len3 = 0; + throw takeFromExternrefTable0(ret[2]); + } + deferred4_0 = ptr3; + deferred4_1 = len3; + return getStringFromWasm0(ptr3, len3); + } finally { + wasm.__wbindgen_free(deferred4_0, deferred4_1, 1); + } + } + /** + * This peer's 32-byte ed25519 public key — published so other peers can + * authenticate its messages with [`ChatPeer::verify_message`]. + * @returns {Uint8Array} + */ + public_key() { + const ret = wasm.chatpeer_public_key(this.__wbg_ptr); + var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v1; + } + /** + * **Rekey a closed channel** (admin): mint a fresh random group key for the + * next epoch, HPKE-seal it to each member's encryption key, and return a + * **signed [`KeyEnvelope`]** as canonical bytes — content the admin publishes + * over the relay (its κ is `kappa(bytes)`). Installs the key locally as the + * channel's current epoch. Removing a member is rekeying to the smaller set: + * no envelope entry for them, so they cannot derive the new epoch's key. + * @param {string} channel + * @param {Uint8Array[]} member_pubkeys + * @returns {Uint8Array} + */ + rekey(channel, member_pubkeys) { + const ptr0 = passStringToWasm0(channel, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passArrayJsValueToWasm0(member_pubkeys, wasm.__wbindgen_malloc); + const len1 = WASM_VECTOR_LEN; + const ret = wasm.chatpeer_rekey(this.__wbg_ptr, ptr0, len0, ptr1, len1); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); + } + var v3 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v3; + } + /** + * **Seal a group key to a member with HPKE** (RFC 9180, base mode): the + * standard hybrid encryption to the recipient's + * [`encryption_key`](ChatPeer::encryption_key), so only that member can + * open it. Output is `encapsulated_key(32) ‖ ciphertext`. + * @param {Uint8Array} group_key + * @param {Uint8Array} recipient_pub + * @returns {Uint8Array} + */ + seal_key_to(group_key, recipient_pub) { + const ptr0 = passArray8ToWasm0(group_key, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passArray8ToWasm0(recipient_pub, wasm.__wbindgen_malloc); + const len1 = WASM_VECTOR_LEN; + const ret = wasm.chatpeer_seal_key_to(this.__wbg_ptr, ptr0, len0, ptr1, len1); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); + } + var v3 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v3; + } + /** + * The channel's transcript: every stored message for `channel`, linearised + * by [`order`](holospaces::chat::order) into the single sequence every peer + * computes identically. JSON `[{ author, body, ts, kappa }, …]`. + * @param {string} channel + * @returns {string} + */ + transcript(channel) { + let deferred3_0; + let deferred3_1; + try { + const ptr0 = passStringToWasm0(channel, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.chatpeer_transcript(this.__wbg_ptr, ptr0, len0); + var ptr2 = ret[0]; + var len2 = ret[1]; + if (ret[3]) { + ptr2 = 0; len2 = 0; + throw takeFromExternrefTable0(ret[2]); + } + deferred3_0 = ptr2; + deferred3_1 = len2; + return getStringFromWasm0(ptr2, len2); + } finally { + wasm.__wbindgen_free(deferred3_0, deferred3_1, 1); + } + } + /** + * The closed channel's transcript, each message decrypted under the epoch + * key it was sealed with. A message whose epoch key this peer lacks (e.g. an + * epoch it was removed before) is reported `decryptable: false`. JSON + * `[{ author, body, ts, kappa, epoch, decryptable }, …]`. + * @param {string} channel + * @returns {string} + */ + transcript_of(channel) { + let deferred3_0; + let deferred3_1; + try { + const ptr0 = passStringToWasm0(channel, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.chatpeer_transcript_of(this.__wbg_ptr, ptr0, len0); + var ptr2 = ret[0]; + var len2 = ret[1]; + if (ret[3]) { + ptr2 = 0; len2 = 0; + throw takeFromExternrefTable0(ret[2]); + } + deferred3_0 = ptr2; + deferred3_1 = len2; + return getStringFromWasm0(ptr2, len2); + } finally { + wasm.__wbindgen_free(deferred3_0, deferred3_1, 1); + } + } + /** + * The channel transcript with bodies **decrypted** under `key` + * (the [`transcript`](ChatPeer::transcript) counterpart for sealed channels). + * A body that fails to decrypt (wrong key / tampered) is reported as + * `decryptable: false` rather than crashing. JSON `[{ author, body, ts, + * kappa, decryptable }, …]`. + * @param {string} channel + * @param {Uint8Array} key + * @returns {string} + */ + transcript_sealed(channel, key) { + let deferred4_0; + let deferred4_1; + try { + const ptr0 = passStringToWasm0(channel, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passArray8ToWasm0(key, wasm.__wbindgen_malloc); + const len1 = WASM_VECTOR_LEN; + const ret = wasm.chatpeer_transcript_sealed(this.__wbg_ptr, ptr0, len0, ptr1, len1); + var ptr3 = ret[0]; + var len3 = ret[1]; + if (ret[3]) { + ptr3 = 0; len3 = 0; + throw takeFromExternrefTable0(ret[2]); + } + deferred4_0 = ptr3; + deferred4_1 = len3; + return getStringFromWasm0(ptr3, len3); + } finally { + wasm.__wbindgen_free(deferred4_0, deferred4_1, 1); + } + } + /** + * Authenticate message bytes against an author's `public_key`: real ed25519 + * verification of the carried signature over the canonical signing bytes, + * *and* that the key's κ matches the message's author (Law L1). A tampered + * body, a forged signature, or the wrong key all return `false`. + * @param {Uint8Array} bytes + * @param {Uint8Array} public_key + * @returns {boolean} + */ + verify_message(bytes, public_key) { + const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passArray8ToWasm0(public_key, wasm.__wbindgen_malloc); + const len1 = WASM_VECTOR_LEN; + const ret = wasm.chatpeer_verify_message(this.__wbg_ptr, ptr0, len0, ptr1, len1); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + return ret[0] !== 0; + } +} +if (Symbol.dispose) ChatPeer.prototype[Symbol.dispose] = ChatPeer.prototype.free; + +/** + * The Platform Manager console, running as a browser peer that composes the + * substrate runtime over the interpreter `ContainerEngine`. + */ +export class Console { + __destroy_into_raw() { + const ptr = this.__wbg_ptr; + this.__wbg_ptr = 0; + ConsoleFinalization.unregister(this); + return ptr; + } + free() { + const ptr = this.__destroy_into_raw(); + wasm.__wbg_console_free(ptr, 0); + } + /** + * Boot a userland holospace **in the browser**: provision it, then spawn it + * through the substrate runtime over the interpreter `ContainerEngine`, + * capture a κ snapshot of its state (suspend), resume, and terminate — the + * execution surface running on the browser peer (ADR-008; RT2; `CC-6`). + * Returns the κ-label of the suspend snapshot (state is content, Law L3). + * @param {Uint8Array} module + * @param {number} memory_bytes + * @returns {string} + */ + boot_userland(module, memory_bytes) { + let deferred3_0; + let deferred3_1; + try { + const ptr0 = passArray8ToWasm0(module, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.console_boot_userland(this.__wbg_ptr, ptr0, len0, memory_bytes); + var ptr2 = ret[0]; + var len2 = ret[1]; + if (ret[3]) { + ptr2 = 0; len2 = 0; + throw takeFromExternrefTable0(ret[2]); + } + deferred3_0 = ptr2; + deferred3_1 = len2; + return getStringFromWasm0(ptr2, len2); + } finally { + wasm.__wbindgen_free(deferred3_0, deferred3_1, 1); + } + } + /** + * *Control panel: configure.* Reconfigure a running instance from the panel + * (ADR-018; `CC-28`). `directives_json` is a JSON array of operations across + * the four classes, e.g. `[{"lifecycle":"suspend"}, {"forwardPort":8080}, + * {"unforwardPort":8080}, {"network":{"fetch":true,"announce":false}}, + * {"quota":1073741824}, {"grant":"blake3:…"}]`. The panel builds a + * content-addressed [`Configuration`] issued by the signed-in operator, + * stores it (Law L2), and returns its κ — the content the running instance + * resolves and applies over the substrate (no server, no RPC). + * @param {string} instance + * @param {string} directives_json + * @returns {string} + */ + configure(instance, directives_json) { + let deferred4_0; + let deferred4_1; + try { + const ptr0 = passStringToWasm0(instance, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passStringToWasm0(directives_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len1 = WASM_VECTOR_LEN; + const ret = wasm.console_configure(this.__wbg_ptr, ptr0, len0, ptr1, len1); + var ptr3 = ret[0]; + var len3 = ret[1]; + if (ret[3]) { + ptr3 = 0; len3 = 0; + throw takeFromExternrefTable0(ret[2]); + } + deferred4_0 = ptr3; + deferred4_1 = len3; + return getStringFromWasm0(ptr3, len3); + } finally { + wasm.__wbindgen_free(deferred4_0, deferred4_1, 1); + } + } + /** + * Open a fresh console — a browser peer with a local content-addressed + * store and the interpreter container engine. + */ + constructor() { + const ret = wasm.console_new(); + this.__wbg_ptr = ret; + ConsoleFinalization.register(this, this.__wbg_ptr, this); + return this; + } + /** + * Provision a holospace from a `.holo` compute artifact (the *holo-file* + * compute form) with a memory budget, κ-addressing its parts into the + * peer's store (Law L2). Returns the holospace identity κ. + * @param {Uint8Array} code + * @param {number} memory_bytes + * @returns {string} + */ + provision(code, memory_bytes) { + let deferred3_0; + let deferred3_1; + try { + const ptr0 = passArray8ToWasm0(code, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.console_provision(this.__wbg_ptr, ptr0, len0, memory_bytes); + var ptr2 = ret[0]; + var len2 = ret[1]; + if (ret[3]) { + ptr2 = 0; len2 = 0; + throw takeFromExternrefTable0(ret[2]); + } + deferred3_0 = ptr2; + deferred3_1 = len2; + return getStringFromWasm0(ptr2, len2); + } finally { + wasm.__wbindgen_free(deferred3_0, deferred3_1, 1); + } + } + /** + * Provision a holospace from a **devcontainer** for the management console + * (CC-12): the `devcontainer.json` is validated against the Dev Container + * spec (`CC-4`) and κ-addressed into the store; the holospace's identity is + * the content address of its devcontainer definition (reproducible — same + * source ⇒ same κ, Law L1). This *provisions* (records) the holospace; the + * operator *enters* it to boot its OS in the workspace IDE (`CC-13`). + * Returns the holospace identity κ. + * @param {Uint8Array} config_json + * @param {number} memory_bytes + * @returns {string} + */ + provision_devcontainer(config_json, memory_bytes) { + let deferred3_0; + let deferred3_1; + try { + const ptr0 = passArray8ToWasm0(config_json, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.console_provision_devcontainer(this.__wbg_ptr, ptr0, len0, memory_bytes); + var ptr2 = ret[0]; + var len2 = ret[1]; + if (ret[3]) { + ptr2 = 0; len2 = 0; + throw takeFromExternrefTable0(ret[2]); + } + deferred3_0 = ptr2; + deferred3_1 = len2; + return getStringFromWasm0(ptr2, len2); + } finally { + wasm.__wbindgen_free(deferred3_0, deferred3_1, 1); + } + } + /** + * Provision a holospace from a *Wasm-recompiled userland* (the execution + * surface, the second compute form — ADR-008). The module is validated + * against the surface contract ([`validate_userland`]) before it is + * κ-addressed into the store, so only a substrate-valid userland can become + * a holospace's code. Returns the holospace identity κ. + * @param {Uint8Array} module + * @param {number} memory_bytes + * @returns {string} + */ + provision_userland(module, memory_bytes) { + let deferred3_0; + let deferred3_1; + try { + const ptr0 = passArray8ToWasm0(module, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.console_provision_userland(this.__wbg_ptr, ptr0, len0, memory_bytes); + var ptr2 = ret[0]; + var len2 = ret[1]; + if (ret[3]) { + ptr2 = 0; len2 = 0; + throw takeFromExternrefTable0(ret[2]); + } + deferred3_0 = ptr2; + deferred3_1 = len2; + return getStringFromWasm0(ptr2, len2); + } finally { + wasm.__wbindgen_free(deferred3_0, deferred3_1, 1); + } + } + /** + * Resolve a holospace (or any κ) from the local store, verifying it by + * re-derivation (Law L5). Returns the bytes, or `undefined` if absent. + * @param {string} kappa + * @returns {Uint8Array | undefined} + */ + resolve(kappa) { + const ptr0 = passStringToWasm0(kappa, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.console_resolve(this.__wbg_ptr, ptr0, len0); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); + } + let v2; + if (ret[0] !== 0) { + v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + } + return v2; + } + /** + * The operator's roster κ — the content address that links their instances + * (R5). Its bytes are in the store, so another instance can resolve it. + * @returns {string | undefined} + */ + roster_kappa() { + const ret = wasm.console_roster_kappa(this.__wbg_ptr); + let v1; + if (ret[0] !== 0) { + v1 = getStringFromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + } + return v1; + } + /** + * Import and run a **devcontainer in the browser** — the Codespaces/Gitpod + * scenario without a Docker daemon or a cloud VM (arc42 chapter 1, the + * motivating scenario; chapter 6). The `devcontainer.json` is validated + * against the Dev Container spec (`CC-4`); the κ-addressed Wasm `userland` + * its config selects is validated against the host-ABI surface (`CC-6`) and + * booted through the substrate runtime over the interpreter engine — same + * lifecycle as a native or remote peer (Q6). Returns the suspend snapshot κ. + * @param {string} repo + * @param {string} reference + * @param {string} config_path + * @param {Uint8Array} config_json + * @param {Uint8Array} userland_module + * @param {number} memory_bytes + * @returns {string} + */ + run_devcontainer(repo, reference, config_path, config_json, userland_module, memory_bytes) { + let deferred7_0; + let deferred7_1; + try { + const ptr0 = passStringToWasm0(repo, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passStringToWasm0(reference, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len1 = WASM_VECTOR_LEN; + const ptr2 = passStringToWasm0(config_path, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len2 = WASM_VECTOR_LEN; + const ptr3 = passArray8ToWasm0(config_json, wasm.__wbindgen_malloc); + const len3 = WASM_VECTOR_LEN; + const ptr4 = passArray8ToWasm0(userland_module, wasm.__wbindgen_malloc); + const len4 = WASM_VECTOR_LEN; + const ret = wasm.console_run_devcontainer(this.__wbg_ptr, ptr0, len0, ptr1, len1, ptr2, len2, ptr3, len3, ptr4, len4, memory_bytes); + var ptr6 = ret[0]; + var len6 = ret[1]; + if (ret[3]) { + ptr6 = 0; len6 = 0; + throw takeFromExternrefTable0(ret[2]); + } + deferred7_0 = ptr6; + deferred7_1 = len6; + return getStringFromWasm0(ptr6, len6); + } finally { + wasm.__wbindgen_free(deferred7_0, deferred7_1, 1); + } + } + /** + * Sign in by unlocking a self-sovereign key (not a server account, + * ADR-001). Returns the operator's content-addressed identity κ. + * @param {Uint8Array} key + * @returns {string} + */ + sign_in(key) { + let deferred2_0; + let deferred2_1; + try { + const ptr0 = passArray8ToWasm0(key, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.console_sign_in(this.__wbg_ptr, ptr0, len0); + deferred2_0 = ret[0]; + deferred2_1 = ret[1]; + return getStringFromWasm0(ret[0], ret[1]); + } finally { + wasm.__wbindgen_free(deferred2_0, deferred2_1, 1); + } + } + /** + * The console's View — a JSON projection of the operator and their + * holospaces (what the UI renders). + * @returns {string} + */ + view() { + let deferred1_0; + let deferred1_1; + try { + const ret = wasm.console_view(this.__wbg_ptr); + deferred1_0 = ret[0]; + deferred1_1 = ret[1]; + return getStringFromWasm0(ret[0], ret[1]); + } finally { + wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); + } + } +} +if (Symbol.dispose) Console.prototype[Symbol.dispose] = Console.prototype.free; + +/** + * A devcontainer's OCI image, assembled into a bootable root filesystem *in the + * browser* — the Layer Assembler (`CC-7` / the in-crate ext4 writer) running as + * the wasm peer. The operator's page fetches the devcontainer's image layers + * from the cold-start gateway (verified by re-derivation before they are added), + * then assembles them here; the result boots over the emulator's `virtio-blk` + * ([`Workspace::boot_devcontainer`], `CC-14`). The browser peer *is* the + * machine — no server assembles or boots the OS (Law L1/L4). + */ +export class DevcontainerImage { + __destroy_into_raw() { + const ptr = this.__wbg_ptr; + this.__wbg_ptr = 0; + DevcontainerImageFinalization.unregister(this); + return ptr; + } + free() { + const ptr = this.__destroy_into_raw(); + wasm.__wbg_devcontainerimage_free(ptr, 0); + } + /** + * Add an OCI image layer (its media type + the verified blob bytes), in + * order from the base layer up. + * @param {string} media_type + * @param {Uint8Array} blob + */ + add_layer(media_type, blob) { + const ptr0 = passStringToWasm0(media_type, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passArray8ToWasm0(blob, wasm.__wbindgen_malloc); + const len1 = WASM_VECTOR_LEN; + wasm.devcontainerimage_add_layer(this.__wbg_ptr, ptr0, len0, ptr1, len1); + } + /** + * Assemble the layers into a bootable `ext4` root filesystem (gunzip + + * untar + OCI whiteout overlay + the in-crate ext4 writer; Law L4). The + * bytes back a [`Workspace::boot_devcontainer`] machine's `virtio-blk` disk. + * @returns {Uint8Array} + */ + assemble() { + const ret = wasm.devcontainerimage_assemble(this.__wbg_ptr); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); + } + var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v1; + } + /** + * Assemble the layers into a **bootable, interactive, writable** root + * filesystem on a `disk_bytes`-sized disk: the same overlay as + * [`Self::assemble`], plus the persistent devcontainer + * [`/init`](holospaces::machine::DEVCONTAINER_INIT) injected — it mounts the + * pseudo filesystems and the shared `virtio-9p` workspace and execs a shell, + * so the booted OS stays running as a dev environment instead of powering off + * after boot — and sized to `disk_bytes` so the OS has room to work (the + * devcontainer's disk; the caller's to choose, not a hidden cap). The base + * image must provide a static `/bin/busybox`. + * @param {number} disk_bytes + * @returns {Uint8Array} + */ + assemble_bootable(disk_bytes) { + const ret = wasm.devcontainerimage_assemble_bootable(this.__wbg_ptr, disk_bytes); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); + } + var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v1; + } + /** + * A new, empty image (add its layers lowest-first with [`Self::add_layer`]). + */ + constructor() { + const ret = wasm.devcontainerimage_new(); + this.__wbg_ptr = ret; + DevcontainerImageFinalization.register(this, this.__wbg_ptr, this); + return this; + } +} +if (Symbol.dispose) DevcontainerImage.prototype[Symbol.dispose] = DevcontainerImage.prototype.free; + +/** + * **The `Linux` app** — a real RISC-V (RV64GC) Linux machine running in the + * browser tab. It wraps the holospaces [emulator](holospaces::emulator) booted + * by the [Boot Orchestrator](holospaces::machine): a real, unmodified RISC-V + * kernel `Image` over the SBI firmware, rooting on a bootable `ext4` disk + * (busybox + the interactive [`/init`](holospaces::machine::DEVCONTAINER_INIT)) + * over `virtio-blk`, with the SBI/HVC console wired through to a terminal. + * + * The familiar Linux boot UX is the kernel's own console log streaming into + * xterm.js as it boots, ending at an interactive `holospace:/workspace#` shell. + * The JS side drives it: pump [`run`](Self::run) (in a worker, so the tab stays + * responsive), [`take_console`](Self::take_console) the new output into the + * terminal each tick, and deliver keystrokes with [`feed`](Self::feed) — exactly + * the [`boot_linux`](https://github.com/Hologram-Technologies/holospaces) example + * loop, in the browser. + */ +export class LinuxVm { + static __wrap(ptr) { + const obj = Object.create(LinuxVm.prototype); + obj.__wbg_ptr = ptr; + LinuxVmFinalization.register(obj, obj.__wbg_ptr, obj); + return obj; + } + __destroy_into_raw() { + const ptr = this.__wbg_ptr; + this.__wbg_ptr = 0; + LinuxVmFinalization.unregister(this); + return ptr; + } + free() { + const ptr = this.__destroy_into_raw(); + wasm.__wbg_linuxvm_free(ptr, 0); + } + /** + * Capture the current machine state into the store and return its κ — the + * parent state of the next step (taken at a quiesced prompt), or the result + * state to memoize. Page-deduplicated, so an unchanged page costs nothing. + * @returns {string} + */ + capture() { + let deferred2_0; + let deferred2_1; + try { + const ret = wasm.linuxvm_capture(this.__wbg_ptr); + var ptr1 = ret[0]; + var len1 = ret[1]; + if (ret[3]) { + ptr1 = 0; len1 = 0; + throw takeFromExternrefTable0(ret[2]); + } + deferred2_0 = ptr1; + deferred2_1 = len1; + return getStringFromWasm0(ptr1, len1); + } finally { + wasm.__wbindgen_free(deferred2_0, deferred2_1, 1); + } + } + /** + * The guest's exit code once halted (`undefined` while still running; `-1` + * for a processor trap). + * @returns {number | undefined} + */ + exit_code() { + const ret = wasm.linuxvm_exit_code(this.__wbg_ptr); + return ret[0] === 0 ? undefined : ret[1]; + } + /** + * Deliver terminal input (keystrokes / paste) to the guest's console — the + * stdin side of the interactive shell. + * @param {Uint8Array} bytes + */ + feed(bytes) { + const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + wasm.linuxvm_feed(this.__wbg_ptr, ptr0, len0); + } + /** + * A human-readable halt reason once halted (`undefined` while running). + * @returns {string | undefined} + */ + halt_reason() { + const ret = wasm.linuxvm_halt_reason(this.__wbg_ptr); + let v1; + if (ret[0] !== 0) { + v1 = getStringFromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + } + return v1; + } + /** + * Whether the machine has halted (powered off or faulted). + * @returns {boolean} + */ + halted() { + const ret = wasm.linuxvm_halted(this.__wbg_ptr); + return ret !== 0; + } + /** + * Instructions processed so far — drives a live **MIPS** readout in the UI + * (`instret` / wall-time). `f64` carries the count losslessly well past any + * realistic boot. + * @returns {number} + */ + instret() { + const ret = wasm.linuxvm_instret(this.__wbg_ptr); + return ret; + } + /** + * Boot a real Linux machine. `kernel` is the **decompressed** RISC-V `Image` + * (gunzip the shipped `linux-kernel.bin.gz`); `rootfs` is the bootable `ext4` + * disk (the decompressed `linux-rootfs.ext4`). Returns a machine loaded and + * ready to [`run`](Self::run) — the default 512 MiB devcontainer machine + * ([`MachineSpec::devcontainer`]), rooting `/dev/vda` over the SBI console. + * @param {Uint8Array} kernel + * @param {Uint8Array} rootfs + */ + constructor(kernel, rootfs) { + const ptr0 = passArray8ToWasm0(kernel, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passArray8ToWasm0(rootfs, wasm.__wbindgen_malloc); + const len1 = WASM_VECTOR_LEN; + const ret = wasm.linuxvm_new(ptr0, len0, ptr1, len1); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + this.__wbg_ptr = ret[0]; + LinuxVmFinalization.register(this, this.__wbg_ptr, this); + return this; + } + /** + * **Warm-start** the machine from a snapshot `pack` — the booted shell in + * O(1), skipping the assemble-and-boot entirely (the difference between a + * multi-minute boot and an instant prompt). `pack` is the *decompressed* + * snapshot the build-time generator produced (`linux-snapshot.bin.gz`): the + * content-addressed, page-deduplicated state of the booted machine (RAM + + * rootfs). [`unpack`](holospaces::snapshot::unpack) re-derives every page's κ + * on load (Law L5), and the boot is deterministic, so this reconstructs the + * exact machine a cold boot would have produced (Law L1). Seed the terminal + * with the captured boot log via [`seed_console`](Self::seed_console). + * @param {Uint8Array} pack + * @returns {LinuxVm} + */ + static restore(pack) { + const ptr0 = passArray8ToWasm0(pack, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.linuxvm_restore(ptr0, len0); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + return LinuxVm.__wrap(ret[0]); + } + /** + * **Serve a memoized result in O(1):** restore the machine to `kappa` — + * demand-paged, so it returns immediately and faults its working set in + * lazily from the store (sub-frame), rather than materializing all RAM. The + * console resets (a side-channel, not snapshotted); the worker replays the + * command's memoized output to the terminal. + * @param {string} kappa + */ + restore_kappa(kappa) { + const ptr0 = passStringToWasm0(kappa, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.linuxvm_restore_kappa(this.__wbg_ptr, ptr0, len0); + if (ret[1]) { + throw takeFromExternrefTable0(ret[0]); + } + } + /** + * Step the machine up to `max_steps` instructions. Returns `true` while the + * machine is still live (the step budget was exhausted — call again next + * tick), `false` once the guest has halted (a clean poweroff or a fault); + * after that, [`exit_code`](Self::exit_code)/[`halt_reason`](Self::halt_reason) + * describe the end state and further calls are no-ops. + * @param {number} max_steps + * @returns {boolean} + */ + run(max_steps) { + const ret = wasm.linuxvm_run(this.__wbg_ptr, max_steps); + return ret !== 0; + } + /** + * Replay a captured console (the boot log) into the terminal after a warm + * [`restore`](Self::restore), so the booted shell renders immediately rather + * than as a blank screen. The console is a side-channel, not part of the + * state κ, so seeding it does not change the machine. + * @param {Uint8Array} bytes + */ + seed_console(bytes) { + const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + wasm.linuxvm_seed_console(this.__wbg_ptr, ptr0, len0); + } + /** + * The memo key for a step: the content address of `(parent state κ, input + * bytes)`. Identical state + identical input ⇒ identical key (Law L1). + * @param {string} parent + * @param {Uint8Array} input + * @returns {string} + */ + step_key(parent, input) { + let deferred4_0; + let deferred4_1; + try { + const ptr0 = passStringToWasm0(parent, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passArray8ToWasm0(input, wasm.__wbindgen_malloc); + const len1 = WASM_VECTOR_LEN; + const ret = wasm.linuxvm_step_key(this.__wbg_ptr, ptr0, len0, ptr1, len1); + var ptr3 = ret[0]; + var len3 = ret[1]; + if (ret[3]) { + ptr3 = 0; len3 = 0; + throw takeFromExternrefTable0(ret[2]); + } + deferred4_0 = ptr3; + deferred4_1 = len3; + return getStringFromWasm0(ptr3, len3); + } finally { + wasm.__wbindgen_free(deferred4_0, deferred4_1, 1); + } + } + /** + * The console bytes produced since the previous call — the incremental boot + * log / shell output to write into the terminal (UTF-8 / ANSI as the guest + * emits it). + * @returns {Uint8Array} + */ + take_console() { + const ret = wasm.linuxvm_take_console(this.__wbg_ptr); + var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v1; + } +} +if (Symbol.dispose) LinuxVm.prototype[Symbol.dispose] = LinuxVm.prototype.free; + +/** + * The Welcome + Commit a membership change produces, to publish over the relay. + */ +export class MlsChange { + static __wrap(ptr) { + const obj = Object.create(MlsChange.prototype); + obj.__wbg_ptr = ptr; + MlsChangeFinalization.register(obj, obj.__wbg_ptr, obj); + return obj; + } + __destroy_into_raw() { + const ptr = this.__wbg_ptr; + this.__wbg_ptr = 0; + MlsChangeFinalization.unregister(this); + return ptr; + } + free() { + const ptr = this.__destroy_into_raw(); + wasm.__wbg_mlschange_free(ptr, 0); + } + /** + * The Commit message — delivered to every existing member to advance the epoch. + * @returns {Uint8Array} + */ + get commit() { + const ret = wasm.mlschange_commit(this.__wbg_ptr); + var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v1; + } + /** + * The Welcome message (empty for a removal) — delivered to the new member. + * @returns {Uint8Array} + */ + get welcome() { + const ret = wasm.mlschange_welcome(this.__wbg_ptr); + var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v1; + } +} +if (Symbol.dispose) MlsChange.prototype[Symbol.dispose] = MlsChange.prototype.free; + +/** + * One member's view of one MLS channel: its identity (signature key + basic + * credential), its crypto provider (key store), and — once created or joined — + * its [`MlsGroup`]. + */ +export class MlsChannel { + static __wrap(ptr) { + const obj = Object.create(MlsChannel.prototype); + obj.__wbg_ptr = ptr; + MlsChannelFinalization.register(obj, obj.__wbg_ptr, obj); + return obj; + } + __destroy_into_raw() { + const ptr = this.__wbg_ptr; + this.__wbg_ptr = 0; + MlsChannelFinalization.unregister(this); + return ptr; + } + free() { + const ptr = this.__destroy_into_raw(); + wasm.__wbg_mlschannel_free(ptr, 0); + } + /** + * **Add a member** by their KeyPackage bytes (admin). Returns the Welcome + * (for the new member) and the Commit (for existing members). The Commit is + * merged locally, advancing the epoch. + * @param {Uint8Array} key_package_bytes + * @returns {MlsChange} + */ + add_member(key_package_bytes) { + const ptr0 = passArray8ToWasm0(key_package_bytes, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.mlschannel_add_member(this.__wbg_ptr, ptr0, len0); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + return MlsChange.__wrap(ret[0]); + } + /** + * Found a new group (this peer becomes its admin / first member). + */ + create_group() { + const ret = wasm.mlschannel_create_group(this.__wbg_ptr); + if (ret[1]) { + throw takeFromExternrefTable0(ret[0]); + } + } + /** + * The current epoch (advances on every Commit). + * @returns {number} + */ + epoch() { + const ret = wasm.mlschannel_epoch(this.__wbg_ptr); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + return ret[0]; + } + /** + * Snapshot this channel's durable state (the MLS key store + group id + + * identity) to bytes — what a peer persists to OPFS / the κ-store so the + * group survives a reload. The bytes hold secret key material, so store them + * encrypted at rest. Pair with [`restore`](MlsChannel::restore). + * @returns {Uint8Array} + */ + export_state() { + const ret = wasm.mlschannel_export_state(this.__wbg_ptr); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); + } + var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v1; + } + /** + * **Join** a group from a Welcome message (the bytes the admin published). + * @param {Uint8Array} welcome_bytes + */ + join(welcome_bytes) { + const ptr0 = passArray8ToWasm0(welcome_bytes, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.mlschannel_join(this.__wbg_ptr, ptr0, len0); + if (ret[1]) { + throw takeFromExternrefTable0(ret[0]); + } + } + /** + * This member's **KeyPackage** bytes — published so an admin can add them. + * @returns {Uint8Array} + */ + key_package() { + const ret = wasm.mlschannel_key_package(this.__wbg_ptr); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); + } + var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v1; + } + /** + * The current member count. + * @returns {number} + */ + members() { + const ret = wasm.mlschannel_members(this.__wbg_ptr); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + return ret[0] >>> 0; + } + /** + * A member identity named `identity`, keyed deterministically from `seed` + * (the same seed reproduces the same MLS identity on every device). No group + * yet — call [`create_group`](MlsChannel::create_group) or + * [`join`](MlsChannel::join). Persist/restore the group with + * [`export_state`](MlsChannel::export_state) / [`restore`](MlsChannel::restore). + * @param {Uint8Array} seed + * @param {Uint8Array} identity + */ + constructor(seed, identity) { + const ptr0 = passArray8ToWasm0(seed, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passArray8ToWasm0(identity, wasm.__wbindgen_malloc); + const len1 = WASM_VECTOR_LEN; + const ret = wasm.mlschannel_new(ptr0, len0, ptr1, len1); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + this.__wbg_ptr = ret[0]; + MlsChannelFinalization.register(this, this.__wbg_ptr, this); + return this; + } + /** + * Process an inbound MLS message (an application message or a Commit). For an + * application message, returns JSON `{ "kind": "app", "text": "…" }`; for a + * Commit (membership change), merges it and returns `{ "kind": "commit", + * "epoch": n, "active": bool }` (`active:false` means this peer was removed). + * @param {Uint8Array} message_bytes + * @returns {string} + */ + receive(message_bytes) { + let deferred3_0; + let deferred3_1; + try { + const ptr0 = passArray8ToWasm0(message_bytes, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.mlschannel_receive(this.__wbg_ptr, ptr0, len0); + var ptr2 = ret[0]; + var len2 = ret[1]; + if (ret[3]) { + ptr2 = 0; len2 = 0; + throw takeFromExternrefTable0(ret[2]); + } + deferred3_0 = ptr2; + deferred3_1 = len2; + return getStringFromWasm0(ptr2, len2); + } finally { + wasm.__wbindgen_free(deferred3_0, deferred3_1, 1); + } + } + /** + * **Remove a member** by their identity bytes (admin). Returns the Commit to + * publish; existing members [`receive`](MlsChannel::receive) it and the + * removed member can no longer decrypt subsequent messages (PCS). + * @param {Uint8Array} identity + * @returns {MlsChange} + */ + remove_member(identity) { + const ptr0 = passArray8ToWasm0(identity, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.mlschannel_remove_member(this.__wbg_ptr, ptr0, len0); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + return MlsChange.__wrap(ret[0]); + } + /** + * Restore a channel from a `seed` and the bytes from + * [`export_state`](MlsChannel::export_state): rebuilds the key store, the + * deterministic signer, and loads the MLS group — picking the conversation + * back up at its current epoch (forward secrecy preserved). + * @param {Uint8Array} seed + * @param {Uint8Array} state + * @returns {MlsChannel} + */ + static restore(seed, state) { + const ptr0 = passArray8ToWasm0(seed, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passArray8ToWasm0(state, wasm.__wbindgen_malloc); + const len1 = WASM_VECTOR_LEN; + const ret = wasm.mlschannel_restore(ptr0, len0, ptr1, len1); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + return MlsChannel.__wrap(ret[0]); + } + /** + * Encrypt and frame `plaintext` as an MLS application message (forward + * secret) — the bytes to publish as the message body. + * @param {string} plaintext + * @returns {Uint8Array} + */ + send(plaintext) { + const ptr0 = passStringToWasm0(plaintext, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.mlschannel_send(this.__wbg_ptr, ptr0, len0); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); + } + var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v2; + } +} +if (Symbol.dispose) MlsChannel.prototype[Symbol.dispose] = MlsChannel.prototype.free; + +/** + * A **workspace** over a running holospace, in the browser tab — the + * Codespaces/Gitpod experience (ADR-009; `CC-9` + `CC-11`). The operator + * launches a holospace whose code is the system emulator; it **boots a real + * operating system** (the [system emulator](holospaces::emulator) running in + * the browser's own wasm engine), and the [workspace + * projection](holospaces::projection) drives it: a live **terminal** + * (keystrokes published as canonical events that advance the holospace's κ + * snapshot) and an **editor** that reads and edits environment content *by κ*. + * + * The boot runs in instruction *chunks* ([`run`](Workspace::run)) so the UI + * stays responsive and can stream the console as the kernel boots — there is no + * server doing the work; the browser peer *is* the machine (Law L1). + * A **content-addressed object store** the browser holds in RAM (the substrate's + * memory, Law L3) — the L1 tier above a persistent OPFS L2 that JavaScript mirrors + * to by κ. JS fills it (hydrating objects from OPFS, or [`unpack`](Self::unpack)ing + * a fetched pack), then hands it to [`Workspace::restore_in`]. Object-level, so a + * peer fetches only the objects it lacks: `manifest_objects(κ) \ keys()` — the + * content-addressed delta, never the whole state again. Every `put` re-derives the + * κ from the bytes, so a forged object cannot enter under a κ it does not hash to + * (Law L5). + */ +export class ObjectStore { + __destroy_into_raw() { + const ptr = this.__wbg_ptr; + this.__wbg_ptr = 0; + ObjectStoreFinalization.unregister(this); + return ptr; + } + free() { + const ptr = this.__destroy_into_raw(); + wasm.__wbg_objectstore_free(ptr, 0); + } + /** + * Fetch an object's bytes by κ. + * @param {string} kappa + * @returns {Uint8Array | undefined} + */ + get(kappa) { + const ptr0 = passStringToWasm0(kappa, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.objectstore_get(this.__wbg_ptr, ptr0, len0); + let v2; + if (ret[0] !== 0) { + v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + } + return v2; + } + /** + * Whether the object is present (the "have" check for delta sync). + * @param {string} kappa + * @returns {boolean} + */ + has(kappa) { + const ptr0 = passStringToWasm0(kappa, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.objectstore_has(this.__wbg_ptr, ptr0, len0); + return ret !== 0; + } + /** + * Every stored κ — what JS persists to OPFS / advertises as its "have" set. + * @returns {any[]} + */ + keys() { + const ret = wasm.objectstore_keys(this.__wbg_ptr); + var v1 = getArrayJsValueFromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 4, 4); + return v1; + } + /** + * The object κs a manifest references (console + pages). JS subtracts its + * `keys()` to get the delta to fetch. The manifest object must be present first. + * @param {string} manifest_kappa + * @returns {any[]} + */ + manifest_objects(manifest_kappa) { + const ptr0 = passStringToWasm0(manifest_kappa, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.objectstore_manifest_objects(this.__wbg_ptr, ptr0, len0); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); + } + var v2 = getArrayJsValueFromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 4, 4); + return v2; + } + constructor() { + const ret = wasm.objectstore_new(); + this.__wbg_ptr = ret; + ObjectStoreFinalization.register(this, this.__wbg_ptr, this); + return this; + } + /** + * Store an object; return its κ (re-derived from the bytes — verify-on-receipt). + * @param {Uint8Array} bytes + * @returns {string} + */ + put(bytes) { + let deferred3_0; + let deferred3_1; + try { + const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.objectstore_put(this.__wbg_ptr, ptr0, len0); + var ptr2 = ret[0]; + var len2 = ret[1]; + if (ret[3]) { + ptr2 = 0; len2 = 0; + throw takeFromExternrefTable0(ret[2]); + } + deferred3_0 = ptr2; + deferred3_1 = len2; + return getStringFromWasm0(ptr2, len2); + } finally { + wasm.__wbindgen_free(deferred3_0, deferred3_1, 1); + } + } + /** + * Load a full bootstrap [`pack`](holospaces::snapshot::pack) (the cold-start + * fast path: one request) into the store; return the manifest κ. + * @param {Uint8Array} pack + * @returns {string} + */ + unpack(pack) { + let deferred3_0; + let deferred3_1; + try { + const ptr0 = passArray8ToWasm0(pack, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.objectstore_unpack(this.__wbg_ptr, ptr0, len0); + var ptr2 = ret[0]; + var len2 = ret[1]; + if (ret[3]) { + ptr2 = 0; len2 = 0; + throw takeFromExternrefTable0(ret[2]); + } + deferred3_0 = ptr2; + deferred3_1 = len2; + return getStringFromWasm0(ptr2, len2); + } finally { + wasm.__wbindgen_free(deferred3_0, deferred3_1, 1); + } + } +} +if (Symbol.dispose) ObjectStore.prototype[Symbol.dispose] = ObjectStore.prototype.free; + +/** + * **Streaming VM** — a holospace booted by *demand-paged snapshot streaming*: the + * machine starts with **no RAM resident** and faults pages in on first touch, + * each fetched by κ over the page's own transport (HTTP static hosting, a peer) + * and verified by re-derivation (Law L5). This is the mobile boot path — a + * 512 MiB-nominal machine runs in the touched working set, and only that working + * set ever crosses the wire. + * + * JavaScript drives the fault loop (no Worker or `Atomics` needed, so it runs on + * iOS Safari): ingest the eager set (manifest + metadata + disk) → [`boot`](Self::boot) + * → then `while (!vm.halted()) { const need = vm.run_slice(n); if (need) + * vm.install(await fetch('/snap/'+need.replace(':','/'))); }`. + */ +export class StreamingVm { + __destroy_into_raw() { + const ptr = this.__wbg_ptr; + this.__wbg_ptr = 0; + StreamingVmFinalization.unregister(this); + return ptr; + } + free() { + const ptr = this.__destroy_into_raw(); + wasm.__wbg_streamingvm_free(ptr, 0); + } + /** + * Build the demand-paged machine named by `manifest_kappa` (a `blake3:…` + * label). The eager objects must already be [`ingest`](Self::ingest)ed; RAM + * pages stream in afterwards. Resident RAM is zero immediately after this. + * @param {string} manifest_kappa + */ + boot(manifest_kappa) { + const ptr0 = passStringToWasm0(manifest_kappa, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.streamingvm_boot(this.__wbg_ptr, ptr0, len0); + if (ret[1]) { + throw takeFromExternrefTable0(ret[0]); + } + } + /** + * The guest's exit code once halted (`undefined` while running; `-1` for a trap). + * @returns {number | undefined} + */ + exit_code() { + const ret = wasm.streamingvm_exit_code(this.__wbg_ptr); + return ret[0] === 0 ? undefined : ret[1]; + } + /** + * Deliver terminal input to the guest's console. + * @param {Uint8Array} bytes + */ + feed(bytes) { + const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + wasm.streamingvm_feed(this.__wbg_ptr, ptr0, len0); + } + /** + * Total bytes fetched on demand so far — the bandwidth readout (only the + * touched working set ever crosses the wire). + * @returns {number} + */ + fetched_bytes() { + const ret = wasm.streamingvm_fetched_bytes(this.__wbg_ptr); + return ret; + } + /** + * A human-readable halt reason once halted. + * @returns {string | undefined} + */ + halt_reason() { + const ret = wasm.streamingvm_halt_reason(this.__wbg_ptr); + let v1; + if (ret[0] !== 0) { + v1 = getStringFromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + } + return v1; + } + /** + * Whether the machine has halted (powered off or faulted). + * @returns {boolean} + */ + halted() { + const ret = wasm.streamingvm_halted(this.__wbg_ptr); + return ret !== 0; + } + /** + * Ingest one content object into the local store before [`boot`](Self::boot) + * — the eager set (manifest + metadata + disk + console). Returns its κ label. + * `put` re-derives the κ from the bytes (Law L5), so an object is trusted only + * because it hashes to the κ that named it. + * @param {Uint8Array} bytes + * @returns {string} + */ + ingest(bytes) { + let deferred2_0; + let deferred2_1; + try { + const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.streamingvm_ingest(this.__wbg_ptr, ptr0, len0); + deferred2_0 = ret[0]; + deferred2_1 = ret[1]; + return getStringFromWasm0(ret[0], ret[1]); + } finally { + wasm.__wbindgen_free(deferred2_0, deferred2_1, 1); + } + } + /** + * Install a fetched demand page: store it (re-deriving its κ — verify on + * receipt) and make every RAM page with that content resident. Returns how + * many pages were filled (`0` ⇒ the bytes matched no pending κ — a wrong or + * corrupt fetch, refused). + * @param {Uint8Array} bytes + * @returns {number} + */ + install(bytes) { + const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.streamingvm_install(this.__wbg_ptr, ptr0, len0); + return ret; + } + /** + * A fresh peer with an empty local store and no machine yet. + */ + constructor() { + const ret = wasm.streamingvm_new(); + this.__wbg_ptr = ret; + StreamingVmFinalization.register(this, this.__wbg_ptr, this); + return this; + } + /** + * Live RAM footprint in 4 KiB pages — the touched working set (the mobile- + * memory readout). Stays far below the nominal RAM size. + * @returns {number} + */ + resident_pages() { + const ret = wasm.streamingvm_resident_pages(this.__wbg_ptr); + return ret; + } + /** + * Run up to `max_steps` instructions. Returns the κ label of a page the guest + * touched that is **not yet resident** — fetch it, [`install`](Self::install) + * it, and call again. Returns `undefined` when there was no fault: either the + * slice ran out (still live — call again) or the machine [`halted`](Self::halted). + * @param {number} max_steps + * @returns {string | undefined} + */ + run_slice(max_steps) { + const ret = wasm.streamingvm_run_slice(this.__wbg_ptr, max_steps); + let v1; + if (ret[0] !== 0) { + v1 = getStringFromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + } + return v1; + } + /** + * The console bytes produced since the previous call (the incremental boot + * log / shell output). + * @returns {Uint8Array} + */ + take_console() { + const ret = wasm.streamingvm_take_console(this.__wbg_ptr); + var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v1; + } +} +if (Symbol.dispose) StreamingVm.prototype[Symbol.dispose] = StreamingVm.prototype.free; + +export class Workspace { + static __wrap(ptr) { + const obj = Object.create(Workspace.prototype); + obj.__wbg_ptr = ptr; + WorkspaceFinalization.register(obj, obj.__wbg_ptr, obj); + return obj; + } + __destroy_into_raw() { + const ptr = this.__wbg_ptr; + this.__wbg_ptr = 0; + WorkspaceFinalization.unregister(this); + return ptr; + } + free() { + const ptr = this.__destroy_into_raw(); + wasm.__wbg_workspace_free(ptr, 0); + } + /** + * Launch a workspace: place the OS `kernel` image and `dtb` in a machine + * with `ram_bytes` of RAM at `base`, the device tree at `dtb_addr`, and hand + * off as the SBI firmware. The machine is now booting (drive it with + * [`run`](Workspace::run)). + * @param {Uint8Array} kernel + * @param {Uint8Array} dtb + * @param {number} ram_bytes + * @param {number} base + * @param {number} dtb_addr + * @returns {Workspace} + */ + static boot(kernel, dtb, ram_bytes, base, dtb_addr) { + const ptr0 = passArray8ToWasm0(kernel, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passArray8ToWasm0(dtb, wasm.__wbindgen_malloc); + const len1 = WASM_VECTOR_LEN; + const ret = wasm.workspace_boot(ptr0, len0, ptr1, len1, ram_bytes, base, dtb_addr); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + return Workspace.__wrap(ret[0]); + } + /** + * Boot a **devcontainer** workspace: the Boot Orchestrator + * ([`MachineSpec`](holospaces::machine::MachineSpec)) generates the device + * tree and boots `kernel` on a machine whose `virtio-blk` disk is the + * assembled `rootfs` (from [`DevcontainerImage::assemble`]). The guest + * kernel mounts the rootfs over `/dev/vda` and runs the devcontainer's real + * OS — entirely in the browser peer (`CC-14`). Drive it with + * [`run`](Workspace::run), exactly like [`boot`](Workspace::boot). + * @param {Uint8Array} kernel + * @param {Uint8Array} rootfs + * @returns {Workspace} + */ + static boot_devcontainer(kernel, rootfs) { + const ptr0 = passArray8ToWasm0(kernel, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passArray8ToWasm0(rootfs, wasm.__wbindgen_malloc); + const len1 = WASM_VECTOR_LEN; + const ret = wasm.workspace_boot_devcontainer(ptr0, len0, ptr1, len1); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + return Workspace.__wrap(ret[0]); + } + /** + * Boot a **networked** devcontainer workspace (`CC-16`): like + * [`boot_devcontainer`](Workspace::boot_devcontainer), but the machine also + * has a `virtio-net` device whose userspace TCP/IP NAT tunnels the guest's + * TCP streams out over a WebSocket to the relay at `relay_url` (there is no + * raw NIC behind a tab; ADR-014). The guest brings its interface up with + * DHCP and can then reach the internet — `git clone`, `apt`, `npm` — from the + * browser peer. Drive it with [`run`](Workspace::run), yielding to the event + * loop between chunks so the WebSocket delivers host-side bytes. + * @param {Uint8Array} kernel + * @param {Uint8Array} rootfs + * @param {string} relay_url + * @returns {Workspace} + */ + static boot_devcontainer_net(kernel, rootfs, relay_url) { + const ptr0 = passArray8ToWasm0(kernel, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passArray8ToWasm0(rootfs, wasm.__wbindgen_malloc); + const len1 = WASM_VECTOR_LEN; + const ptr2 = passStringToWasm0(relay_url, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len2 = WASM_VECTOR_LEN; + const ret = wasm.workspace_boot_devcontainer_net(ptr0, len0, ptr1, len1, ptr2, len2); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + return Workspace.__wrap(ret[0]); + } + /** + * Capture the running machine as a content-addressed snapshot descending from + * `parent` (empty = a root), writing its objects into the workspace store. + * Unchanged pages dedup against what is already resident, so this stores only + * the delta — and JS then persists/transmits only the new objects. Returns the + * manifest κ. + * @param {string} parent + * @returns {string} + */ + capture(parent) { + let deferred3_0; + let deferred3_1; + try { + const ptr0 = passStringToWasm0(parent, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.workspace_capture(this.__wbg_ptr, ptr0, len0); + var ptr2 = ret[0]; + var len2 = ret[1]; + if (ret[3]) { + ptr2 = 0; len2 = 0; + throw takeFromExternrefTable0(ret[2]); + } + deferred3_0 = ptr2; + deferred3_1 = len2; + return getStringFromWasm0(ptr2, len2); + } finally { + wasm.__wbindgen_free(deferred3_0, deferred3_1, 1); + } + } + /** + * The κ of every operator event published on the terminal channel so far. + * @returns {any[]} + */ + channel() { + const ret = wasm.workspace_channel(this.__wbg_ptr); + var v1 = getArrayJsValueFromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 4, 4); + return v1; + } + /** + * **Checkpoint** the current state into the editable history with a `label` and + * `timestamp_ms` (the caller's wall clock — the machine has none). Captures the + * state (deduped against the prior one) and records a [commit](holospaces::history) + * descending from the current history head. Returns the commit κ. This is one row + * in the History panel — "go back here later" with [`restore_commit`]. + * @param {string} label + * @param {number} timestamp_ms + * @returns {string} + */ + checkpoint(label, timestamp_ms) { + let deferred3_0; + let deferred3_1; + try { + const ptr0 = passStringToWasm0(label, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.workspace_checkpoint(this.__wbg_ptr, ptr0, len0, timestamp_ms); + var ptr2 = ret[0]; + var len2 = ret[1]; + if (ret[3]) { + ptr2 = 0; len2 = 0; + throw takeFromExternrefTable0(ret[2]); + } + deferred3_0 = ptr2; + deferred3_1 = len2; + return getStringFromWasm0(ptr2, len2); + } finally { + wasm.__wbindgen_free(deferred3_0, deferred3_1, 1); + } + } + /** + * The **file tree**: the workspace's files as a JSON array of + * `{ path, kappa }` — each file's current content κ (its identity, Law L1). + * What the editor's explorer renders. + * @returns {string} + */ + files() { + let deferred1_0; + let deferred1_1; + try { + const ret = wasm.workspace_files(this.__wbg_ptr); + deferred1_0 = ret[0]; + deferred1_1 = ret[1]; + return getStringFromWasm0(ret[0], ret[1]); + } finally { + wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); + } + } + /** + * Whether the machine has powered off. + * @returns {boolean} + */ + get halted() { + const ret = wasm.workspace_halted(this.__wbg_ptr); + return ret !== 0; + } + /** + * The κ of the current machine state (the memo head), if established. + * @returns {string | undefined} + */ + head() { + const ret = wasm.workspace_head(this.__wbg_ptr); + let v1; + if (ret[0] !== 0) { + v1 = getStringFromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + } + return v1; + } + /** + * The history log as JSON, newest first: + * `[{ "commit": κ, "state": κ, "label": string, "timestamp": ms }, …]`. The data + * the History panel renders. + * @returns {string} + */ + history_json() { + let deferred1_0; + let deferred1_1; + try { + const ret = wasm.workspace_history_json(this.__wbg_ptr); + deferred1_0 = ret[0]; + deferred1_1 = ret[1]; + return getStringFromWasm0(ret[0], ret[1]); + } finally { + wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); + } + } + /** + * The editor's read: fetch a file's content *by κ*, verifying it by + * re-derivation (Law L5). `undefined` if it is not in the workspace store. + * @param {string} kappa + * @returns {Uint8Array | undefined} + */ + open_file(kappa) { + const ptr0 = passStringToWasm0(kappa, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.workspace_open_file(this.__wbg_ptr, ptr0, len0); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); + } + let v2; + if (ret[0] !== 0) { + v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + } + return v2; + } + /** + * Open a file *by path*: the content at the file's current κ (the editor + * reads the environment content by κ). `undefined` if the path is unknown. + * @param {string} path + * @returns {Uint8Array | undefined} + */ + read_path(path) { + const ptr0 = passStringToWasm0(path, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.workspace_read_path(this.__wbg_ptr, ptr0, len0); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); + } + let v2; + if (ret[0] !== 0) { + v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + } + return v2; + } + /** + * **Apply a configuration** the control plane published (ADR-018; `CC-28`): + * decode the κ-addressed [`Configuration`] bytes (resolved + verified over + * the substrate by the caller, Law L5) and enact its live directives on the + * *running* machine — each `forwardPort` begins forwarding on the running + * instance, without a reboot. Returns a JSON summary of what was applied + * (`{ "forwarded": [{ "guest": 8080, "host": 8080 }], "lifecycle": "…", + * "unsupported": [...] }`). The instance state changes from the panel's + * configuration, carried as content over the substrate — no RPC. + * @param {Uint8Array} config_bytes + * @returns {string} + */ + reconfigure(config_bytes) { + let deferred3_0; + let deferred3_1; + try { + const ptr0 = passArray8ToWasm0(config_bytes, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.workspace_reconfigure(this.__wbg_ptr, ptr0, len0); + var ptr2 = ret[0]; + var len2 = ret[1]; + if (ret[3]) { + ptr2 = 0; len2 = 0; + throw takeFromExternrefTable0(ret[2]); + } + deferred3_0 = ptr2; + deferred3_1 = len2; + return getStringFromWasm0(ptr2, len2); + } finally { + wasm.__wbindgen_free(deferred3_0, deferred3_1, 1); + } + } + /** + * Reset the devcontainer to a prior state by its κ (restored from the store) — an + * instant jump to any captured/memoized checkpoint, 9P workspace intact. + * Revisiting a state is what lets a later [`run_memoized`] *serve* a step that was + * run from there before. + * @param {string} state_kappa + */ + reset_to(state_kappa) { + const ptr0 = passStringToWasm0(state_kappa, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.workspace_reset_to(this.__wbg_ptr, ptr0, len0); + if (ret[1]) { + throw takeFromExternrefTable0(ret[0]); + } + } + /** + * **Go back in time** to a commit by its κ: restore that checkpoint's machine + * state (instant, 9P workspace intact) and set it as the history head — so a + * later [`checkpoint`] forks a new branch from here, leaving the commits you + * jumped back from immutable and still restorable. + * @param {string} commit_kappa + */ + restore_commit(commit_kappa) { + const ptr0 = passStringToWasm0(commit_kappa, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.workspace_restore_commit(this.__wbg_ptr, ptr0, len0); + if (ret[1]) { + throw takeFromExternrefTable0(ret[0]); + } + } + /** + * **Warm-start** a devcontainer workspace from a precomputed snapshot `pack` — + * the booted machine in O(1) instead of replaying the assemble-and-boot. `pack` + * is the *decoded* (gunzipped) [`pack`](holospaces::snapshot::pack) the build-time + * generator produced (`devcontainer-snapshot.bin.gz`): a Merkle manifest plus + * every page of the booted machine — RAM, the virtio-blk rootfs, **and the + * mounted virtio-9p workspace** (`CC-15`). [`unpack`](holospaces::snapshot::unpack) + * loads the objects into the store (re-deriving each κ on receipt, Law L5) and + * the whole machine is reconstructed, 9p mount intact — so the workbench's + * FileSystemProvider and terminal work immediately, but the visitor never paid + * the boot. The boot is deterministic, so its result is a content-addressed + * constant (Law L1). + * @param {Uint8Array} pack + * @returns {Workspace} + */ + static restore_devcontainer(pack) { + const ptr0 = passArray8ToWasm0(pack, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.workspace_restore_devcontainer(ptr0, len0); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + return Workspace.__wrap(ret[0]); + } + /** + * **Warm-start a *networked* devcontainer** (`CC-16`): like + * [`restore_devcontainer`](Self::restore_devcontainer), but re-dials the egress + * the snapshot could not carry. A snapshot serializes the `virtio-net` device's + * negotiated features + virtqueue registers, but its egress transport is a live + * WebSocket handle no snapshot can hold — so after reconstructing the machine + * this connects a fresh tunnel to the relay at `relay_url` and reattaches it to + * the restored NIC (the guest never re-probes; its DHCP lease for the + * deterministic `10.0.2.15` is in restored RAM). The result: a warm-started + * devcontainer that reaches the internet in O(working set) — `git clone`, `pip`, + * or a live Hyperliquid request — without ever replaying the boot. Requires a + * snapshot captured from a [`boot_devcontainer_net`](Self::boot_devcontainer_net) + * machine (one whose guest enumerated the NIC); a non-net snapshot has no device + * to reattach to and this errors. + * @param {Uint8Array} pack + * @param {string} relay_url + * @returns {Workspace} + */ + static restore_devcontainer_net(pack, relay_url) { + const ptr0 = passArray8ToWasm0(pack, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passStringToWasm0(relay_url, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len1 = WASM_VECTOR_LEN; + const ret = wasm.workspace_restore_devcontainer_net(ptr0, len0, ptr1, len1); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + return Workspace.__wrap(ret[0]); + } + /** + * **Warm-start from an [`ObjectStore`]** JS has populated by content-addressed + * sync — hydrated from OPFS and topped up with only the objects it lacked + * (`manifest_objects(κ) \ have`), each fetched by κ and verified. Consumes the + * store, which becomes the workspace's own — so its objects persist for the next + * snapshot/variant to dedup against. The whole devcontainer (RAM + virtio-blk + * rootfs + mounted virtio-9p workspace) is reconstructed, 9P intact. This is the + * content-addressed network path: a return visit or a sibling variant transfers + * only the delta, never the whole state again. + * @param {ObjectStore} store + * @param {string} manifest_kappa + * @returns {Workspace} + */ + static restore_in(store, manifest_kappa) { + _assertClass(store, ObjectStore); + var ptr0 = store.__destroy_into_raw(); + const ptr1 = passStringToWasm0(manifest_kappa, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len1 = WASM_VECTOR_LEN; + const ret = wasm.workspace_restore_in(ptr0, ptr1, len1); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + return Workspace.__wrap(ret[0]); + } + /** + * Advance the running holospace by `budget` instructions (one chunk of the + * boot or of servicing input). Returns `true` once the machine has halted + * (powered off). Call repeatedly from a UI loop, rendering + * [`terminal`](Workspace::terminal) between chunks. + * @param {number} budget + * @returns {boolean} + */ + run(budget) { + const ret = wasm.workspace_run(this.__wbg_ptr, budget); + return ret !== 0; + } + /** + * Run a line through the **κ-memo** — Hologram's O(1) edge over devcontainer + * shell steps. The step is keyed by `(current state κ, line)`: if that exact step + * has run before, its result machine state is *served* by [`restore`] — no + * re-execution, the full devcontainer (RAM + rootfs + 9P workspace) reconstructed + * in one shot; otherwise the line runs once and its result is captured and + * memoized so the next time is served. Cold cost scales with the command; a + * served hit is flat — re-provisioning, undo/redo, and branching become instant. + * + * Returns JSON `{ "hit": bool, "state": "<κ>", "event": "<κ>" }`. `hit=true` means + * the result came from the memo with zero recomputation. + * @param {string} line + * @returns {string} + */ + run_memoized(line) { + let deferred3_0; + let deferred3_1; + try { + const ptr0 = passStringToWasm0(line, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.workspace_run_memoized(this.__wbg_ptr, ptr0, len0); + var ptr2 = ret[0]; + var len2 = ret[1]; + if (ret[3]) { + ptr2 = 0; len2 = 0; + throw takeFromExternrefTable0(ret[2]); + } + deferred3_0 = ptr2; + deferred3_1 = len2; + return getStringFromWasm0(ptr2, len2); + } finally { + wasm.__wbindgen_free(deferred3_0, deferred3_1, 1); + } + } + /** + * The **editor** surface: save a file's content (the operator's edit). The + * content is κ-addressed into the substrate (Law L2), so the returned κ is + * the file's new identity — an edit advances it (Law L1). The canonical edit + * event for `path` is published on the channel. + * @param {string} path + * @param {Uint8Array} content + * @returns {string} + */ + save_file(path, content) { + let deferred4_0; + let deferred4_1; + try { + const ptr0 = passStringToWasm0(path, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passArray8ToWasm0(content, wasm.__wbindgen_malloc); + const len1 = WASM_VECTOR_LEN; + const ret = wasm.workspace_save_file(this.__wbg_ptr, ptr0, len0, ptr1, len1); + var ptr3 = ret[0]; + var len3 = ret[1]; + if (ret[3]) { + ptr3 = 0; len3 = 0; + throw takeFromExternrefTable0(ret[2]); + } + deferred4_0 = ptr3; + deferred4_1 = len3; + return getStringFromWasm0(ptr3, len3); + } finally { + wasm.__wbindgen_free(deferred4_0, deferred4_1, 1); + } + } + /** + * Whether the terminal has rendered `marker` yet (e.g. the ready banner). + * @param {string} marker + * @returns {boolean} + */ + shows(marker) { + const ptr0 = passStringToWasm0(marker, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.workspace_shows(this.__wbg_ptr, ptr0, len0); + return ret !== 0; + } + /** + * The running holospace's κ snapshot — its canonical state (Law L1/L3/L5). + * @returns {string} + */ + state_kappa() { + let deferred1_0; + let deferred1_1; + try { + const ret = wasm.workspace_state_kappa(this.__wbg_ptr); + deferred1_0 = ret[0]; + deferred1_1 = ret[1]; + return getStringFromWasm0(ret[0], ret[1]); + } finally { + wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); + } + } + /** + * Every κ in the workspace store — JS persists new ones to OPFS / advertises its + * "have" set after a [`capture`](Self::capture). + * @returns {any[]} + */ + store_keys() { + const ret = wasm.workspace_store_keys(this.__wbg_ptr); + var v1 = getArrayJsValueFromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 4, 4); + return v1; + } + /** + * Fetch a stored object's bytes by κ (to write to OPFS or send to a peer). + * @param {string} kappa + * @returns {Uint8Array | undefined} + */ + store_object(kappa) { + const ptr0 = passStringToWasm0(kappa, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.workspace_store_object(this.__wbg_ptr, ptr0, len0); + let v2; + if (ret[0] !== 0) { + v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + } + return v2; + } + /** + * The rendered terminal — the console the running holospace has produced. + * @returns {string} + */ + terminal() { + let deferred1_0; + let deferred1_1; + try { + const ret = wasm.workspace_terminal(this.__wbg_ptr); + deferred1_0 = ret[0]; + deferred1_1 = ret[1]; + return getStringFromWasm0(ret[0], ret[1]); + } finally { + wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); + } + } + /** + * Type a line into the terminal: publish it as a canonical event on the + * holospace's channel (Law L1/L2), feed the keystrokes to the running + * machine, and run until the response settles. The holospace's κ snapshot + * advances. Returns the event's κ. + * @param {string} line + * @returns {string} + */ + type_line(line) { + let deferred2_0; + let deferred2_1; + try { + const ptr0 = passStringToWasm0(line, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.workspace_type_line(this.__wbg_ptr, ptr0, len0); + deferred2_0 = ret[0]; + deferred2_1 = ret[1]; + return getStringFromWasm0(ret[0], ret[1]); + } finally { + wasm.__wbindgen_free(deferred2_0, deferred2_1, 1); + } + } + /** + * Delete a file or folder from the shared workspace (the workbench + * `FileSystemProvider.delete`) — the editor removing content the OS sees + * over `virtio-9p`. `true` if it existed. + * @param {string} name + * @returns {boolean} + */ + ws_delete(name) { + const ptr0 = passStringToWasm0(name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.workspace_ws_delete(this.__wbg_ptr, ptr0, len0); + return ret !== 0; + } + /** + * The shared workspace's directory listing — a JSON array of + * `{ name, dir, size }` over the running holospace's `virtio-9p` workspace + * (the workbench `FileSystemProvider.readDirectory`). + * @returns {string} + */ + ws_list() { + let deferred1_0; + let deferred1_1; + try { + const ret = wasm.workspace_ws_list(this.__wbg_ptr); + deferred1_0 = ret[0]; + deferred1_1 = ret[1]; + return getStringFromWasm0(ret[0], ret[1]); + } finally { + wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); + } + } + /** + * Create a folder in the shared workspace (the workbench + * `FileSystemProvider.createDirectory`). + * @param {string} name + */ + ws_mkdir(name) { + const ptr0 = passStringToWasm0(name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + wasm.workspace_ws_mkdir(this.__wbg_ptr, ptr0, len0); + } + /** + * Read a file from the shared workspace (the workbench + * `FileSystemProvider.readFile`) — the same content the OS reads over + * `virtio-9p`. `undefined` if absent. + * @param {string} name + * @returns {Uint8Array | undefined} + */ + ws_read(name) { + const ptr0 = passStringToWasm0(name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.workspace_ws_read(this.__wbg_ptr, ptr0, len0); + let v2; + if (ret[0] !== 0) { + v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + } + return v2; + } + /** + * Rename a file or folder in the shared workspace (the workbench + * `FileSystemProvider.rename`). `true` if the source existed. + * @param {string} from + * @param {string} to + * @returns {boolean} + */ + ws_rename(from, to) { + const ptr0 = passStringToWasm0(from, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passStringToWasm0(to, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len1 = WASM_VECTOR_LEN; + const ret = wasm.workspace_ws_rename(this.__wbg_ptr, ptr0, len0, ptr1, len1); + return ret !== 0; + } + /** + * Write a file into the shared workspace (the workbench + * `FileSystemProvider.writeFile`) — the editor saving the *same content* the + * OS reads over `virtio-9p` (one content, Law L1). Returns the content's κ + * (its identity, Law L1/L2). + * @param {string} name + * @param {Uint8Array} content + * @returns {string} + */ + ws_write(name, content) { + let deferred3_0; + let deferred3_1; + try { + const ptr0 = passStringToWasm0(name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passArray8ToWasm0(content, wasm.__wbindgen_malloc); + const len1 = WASM_VECTOR_LEN; + const ret = wasm.workspace_ws_write(this.__wbg_ptr, ptr0, len0, ptr1, len1); + deferred3_0 = ret[0]; + deferred3_1 = ret[1]; + return getStringFromWasm0(ret[0], ret[1]); + } finally { + wasm.__wbindgen_free(deferred3_0, deferred3_1, 1); + } + } +} +if (Symbol.dispose) Workspace.prototype[Symbol.dispose] = Workspace.prototype.free; + +/** + * @param {Uint8Array} invite_secret + * @param {number} epoch + * @returns {Uint8Array} + */ +export function channel_key(invite_secret, epoch) { + const ptr0 = passArray8ToWasm0(invite_secret, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.channel_key(ptr0, len0, epoch); + var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v2; +} + +/** + * Prove hologram's **full graph pipeline** runs in the tab: build a graph, + * compile it to a content-addressed `.holo` archive, load an inference + * session, and execute it on real data. This is the exact mechanism a + * transformer runs through (just more ops + the weights as constants), so a + * passing softmax here de-risks the whole model path. + * @returns {string} + */ +export function hologram_graph_demo() { + let deferred1_0; + let deferred1_1; + try { + const ret = wasm.hologram_graph_demo(); + deferred1_0 = ret[0]; + deferred1_1 = ret[1]; + return getStringFromWasm0(ret[0], ret[1]); + } finally { + wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); + } +} + +/** + * Run `runs` square f32 matmuls of dimension `dim` through hologram's CPU + * backend and report throughput as JSON `{ "dim", "ms", "gflops" }`. Timed + * with the JS clock (`std::time::Instant` is unavailable on wasm). + * @param {number} dim + * @param {number} runs + * @returns {string} + */ +export function hologram_matmul_bench(dim, runs) { + let deferred1_0; + let deferred1_1; + try { + const ret = wasm.hologram_matmul_bench(dim, runs); + deferred1_0 = ret[0]; + deferred1_1 = ret[1]; + return getStringFromWasm0(ret[0], ret[1]); + } finally { + wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); + } +} + +/** + * The κ-label of bytes on the substrate's default σ-axis (blake3) — the same + * content address every peer computes (Law L1). + * @param {Uint8Array} bytes + * @returns {string} + */ +export function kappa(bytes) { + let deferred2_0; + let deferred2_1; + try { + const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.kappa(ptr0, len0); + deferred2_0 = ret[0]; + deferred2_1 = ret[1]; + return getStringFromWasm0(ret[0], ret[1]); + } finally { + wasm.__wbindgen_free(deferred2_0, deferred2_1, 1); + } +} + +/** + * Run a full two-member MLS exchange in-tab and report the result as JSON: + * `{ ciphersuite, members, msg1, msg2, epoch }`. The two messages decrypting in + * order is the forward-secret ratchet working; `members == 2` and a non-zero + * epoch are the TreeKEM group state. + * @returns {string} + */ +export function mls_selftest() { + let deferred2_0; + let deferred2_1; + try { + const ret = wasm.mls_selftest(); + var ptr1 = ret[0]; + var len1 = ret[1]; + if (ret[3]) { + ptr1 = 0; len1 = 0; + throw takeFromExternrefTable0(ret[2]); + } + deferred2_0 = ptr1; + deferred2_1 = len1; + return getStringFromWasm0(ptr1, len1); + } finally { + wasm.__wbindgen_free(deferred2_0, deferred2_1, 1); + } +} + +/** + * Decrypt a [`seal_bytes`] blob under the channel `key` (wrong key / tampered + * ciphertext fails the Poly1305 tag). + * @param {Uint8Array} key + * @param {Uint8Array} ciphertext + * @returns {Uint8Array} + */ +export function open_bytes(key, ciphertext) { + const ptr0 = passArray8ToWasm0(key, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passArray8ToWasm0(ciphertext, wasm.__wbindgen_malloc); + const len1 = WASM_VECTOR_LEN; + const ret = wasm.open_bytes(ptr0, len0, ptr1, len1); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); + } + var v3 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v3; +} + +/** + * Continue a mind: given the current token sequence (`ids_json`), greedily + * generate `n_more` tokens. Returns `{ ids, text, ms }` — the new full + * sequence and its decoded text. Deterministic, so any holder of the same + * sequence continues into the identical thought. + * @param {string} ids_json + * @param {number} n_more + * @param {number} temp + * @param {number} seed + * @param {number} cap_hint + * @returns {string} + */ +export function qvac_continue(ids_json, n_more, temp, seed, cap_hint) { + let deferred2_0; + let deferred2_1; + try { + const ptr0 = passStringToWasm0(ids_json, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.qvac_continue(ptr0, len0, n_more, temp, seed, cap_hint); + deferred2_0 = ret[0]; + deferred2_1 = ret[1]; + return getStringFromWasm0(ret[0], ret[1]); + } finally { + wasm.__wbindgen_free(deferred2_0, deferred2_1, 1); + } +} + +/** + * Generate tokens from a tiny transformer **entirely in the browser**, through + * hologram — the whole LM (embedding, multi-head RoPE attention, SwiGLU MLP, + * LM head, token loop) runs in wasm. Returns JSON `{ tokens, ms }`. + * @param {number} max_new + * @param {number} temp + * @param {number} seed + * @returns {string} + */ +export function qvac_generate(max_new, temp, seed) { + let deferred1_0; + let deferred1_1; + try { + const ret = wasm.qvac_generate(max_new, temp, seed); + deferred1_0 = ret[0]; + deferred1_1 = ret[1]; + return getStringFromWasm0(ret[0], ret[1]); + } finally { + wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); + } +} + +/** + * Generate from a **real GGUF model** entirely in the browser, through hologram. + * `gguf` is the fetched model file's bytes. Loads it, generates `max_new` tokens + * greedily/sampled from ``, detokenizes via the embedded vocab, and returns + * JSON `{ text, tokens, ms, arch }`. + * @param {Uint8Array} gguf + * @param {number} max_new + * @param {number} temp + * @param {number} seed + * @returns {string} + */ +export function qvac_generate_gguf(gguf, max_new, temp, seed) { + let deferred2_0; + let deferred2_1; + try { + const ptr0 = passArray8ToWasm0(gguf, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.qvac_generate_gguf(ptr0, len0, max_new, temp, seed); + deferred2_0 = ret[0]; + deferred2_1 = ret[1]; + return getStringFromWasm0(ret[0], ret[1]); + } finally { + wasm.__wbindgen_free(deferred2_0, deferred2_1, 1); + } +} + +/** + * Export the loaded model for the WebGPU engine — **per-block int8** (a scale per + * 32 weights, the GGUF's native precision) in `[out,in]` layout. Consumes the + * retained GGUF (freeing ~its bytes). Blob: `[u32 manifest_len][JSON][q+scales]`. + * @param {number} bits + * @returns {Uint8Array} + */ +export function qvac_gpu_export(bits) { + const ret = wasm.qvac_gpu_export(bits); + var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v1; +} + +/** + * Free the retained GGUF once all tensors have been streamed to the GPU. + */ +export function qvac_gpu_free() { + wasm.qvac_gpu_free(); +} + +/** + * **Streaming GPU export** — the manifest only (dims + tensor list). JS then + * pulls each tensor with [`qvac_gpu_tensor`] and uploads it, so the converted + * weights never coexist with the GGUF (the memory wall that blocks 1.7B+). + * @param {number} bits + * @returns {string} + */ +export function qvac_gpu_manifest(bits) { + let deferred1_0; + let deferred1_1; + try { + const ret = wasm.qvac_gpu_manifest(bits); + deferred1_0 = ret[0]; + deferred1_1 = ret[1]; + return getStringFromWasm0(ret[0], ret[1]); + } finally { + wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); + } +} + +/** + * One tensor's GPU bytes (`[q][f32 scales]` or `[f32]`) — quantized on demand + * from the retained GGUF. Peak = GGUF + this one tensor. + * @param {string} name + * @param {number} bits + * @returns {Uint8Array} + */ +export function qvac_gpu_tensor(name, bits) { + const ptr0 = passStringToWasm0(name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.qvac_gpu_tensor(ptr0, len0, bits); + var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v2; +} + +/** + * Load a GGUF model **for the GPU engine only** — parses metadata + tokenizer + * vocab/scores and retains the GGUF for [`qvac_gpu_export`], but does NOT build + * the CPU [`OwnedModel`]. That f32→int8 round-trip (≈ the whole model materialised + * twice) is what OOMs the tab on a 1.1B; skipping it is the difference between a + * 1.1B loading or crashing. Takes `Vec` (moved, not copied — one fewer ~640 MB + * copy than `&[u8].to_vec()`). Returns `{ ok, arch, vocab, bos, eos, add_bos }`. + * @param {Uint8Array} gguf + * @returns {string} + */ +export function qvac_load_gpu(gguf) { + let deferred2_0; + let deferred2_1; + try { + const ptr0 = passArray8ToWasm0(gguf, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.qvac_load_gpu(ptr0, len0); + deferred2_0 = ret[0]; + deferred2_1 = ret[1]; + return getStringFromWasm0(ret[0], ret[1]); + } finally { + wasm.__wbindgen_free(deferred2_0, deferred2_1, 1); + } +} + +/** + * Load a GGUF model into the page (once). Returns `{ ok, arch, vocab }`. + * @param {Uint8Array} gguf + * @returns {string} + */ +export function qvac_load_model(gguf) { + let deferred2_0; + let deferred2_1; + try { + const ptr0 = passArray8ToWasm0(gguf, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.qvac_load_model(ptr0, len0); + deferred2_0 = ret[0]; + deferred2_1 = ret[1]; + return getStringFromWasm0(ret[0], ret[1]); + } finally { + wasm.__wbindgen_free(deferred2_0, deferred2_1, 1); + } +} + +/** + * Route Rust panics to `console.error` (release wasm otherwise traps silently). + */ +export function qvac_panic_hook() { + wasm.qvac_panic_hook(); +} + +/** + * Tokenize text with the model's SentencePiece vocab + scores using llama.cpp's + * **greedy score-merge** algorithm (not unigram Viterbi — Llama's SPM merges the + * highest-scoring adjacent pair repeatedly), so a typed prompt becomes the *same* + * tokens the model trained on. Prepends ``; unknown chars fall back to bytes. + * @param {string} text + * @returns {string} + */ +export function qvac_tokenize(text) { + let deferred2_0; + let deferred2_1; + try { + const ptr0 = passStringToWasm0(text, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.qvac_tokenize(ptr0, len0); + deferred2_0 = ret[0]; + deferred2_1 = ret[1]; + return getStringFromWasm0(ret[0], ret[1]); + } finally { + wasm.__wbindgen_free(deferred2_0, deferred2_1, 1); + } +} + +/** + * Run a `.holo` compute artifact in the browser via the hologram executor + * compiled to wasm — the *browser `.holo` engine* (arc42 chapter 11, RT2; + * conformance `CC-2`). Returns the κ-label of the first output. Because the + * executor is deterministic and content-addressed, this κ equals the one the + * native executor produces for the same `.holo` (the browser engine equals the + * native one). + * @param {Uint8Array} archive + * @returns {string} + */ +export function run_holo(archive) { + let deferred3_0; + let deferred3_1; + try { + const ptr0 = passArray8ToWasm0(archive, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.run_holo(ptr0, len0); + var ptr2 = ret[0]; + var len2 = ret[1]; + if (ret[3]) { + ptr2 = 0; len2 = 0; + throw takeFromExternrefTable0(ret[2]); + } + deferred3_0 = ptr2; + deferred3_1 = len2; + return getStringFromWasm0(ptr2, len2); + } finally { + wasm.__wbindgen_free(deferred3_0, deferred3_1, 1); + } +} + +/** + * Derive a channel's symmetric E2E key from its **invite secret** and an + * `epoch` (HKDF-SHA256) — the simple "possession of the invite = membership" + * model (à la Keet room keys). Everyone holding the invite derives the same + * 32-byte key and can seal/open message bodies; the relay, holding only the + * channel κ, cannot. For closed-membership rotation that excludes a removed + * member, use the closed-channel [`ChatPeer::rekey`] path (HPKE per member) instead. + * Encrypt arbitrary bytes (a file) under a 32-byte channel `key` — + * ChaCha20-Poly1305 with a random nonce (the same AEAD as message bodies). The + * ciphertext is content: store it with `ChatPeer.ingest` (→ its κ) and ship it + * over the relay like any object; the relay never sees the plaintext file. + * @param {Uint8Array} key + * @param {Uint8Array} plaintext + * @returns {Uint8Array} + */ +export function seal_bytes(key, plaintext) { + const ptr0 = passArray8ToWasm0(key, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passArray8ToWasm0(plaintext, wasm.__wbindgen_malloc); + const len1 = WASM_VECTOR_LEN; + const ret = wasm.seal_bytes(ptr0, len0, ptr1, len1); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); + } + var v3 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v3; +} + +/** + * Validate that `module` is a recompiled userland fit for the *execution + * surface* (ADR-008; `CC-6`): specification-valid WebAssembly that imports only + * the substrate host ABI and presents the container ABI. This is the κ-boundary + * contract the browser peer enforces before a userland may be a holospace's + * code — ambient (WASI-style) imports and a missing container ABI are refused. + * @param {Uint8Array} module + */ +export function validate_userland(module) { + const ptr0 = passArray8ToWasm0(module, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.validate_userland(ptr0, len0); + if (ret[1]) { + throw takeFromExternrefTable0(ret[0]); + } +} + +/** + * Verify bytes against a claimed κ-label by re-derivation (Law L5). This is + * what makes content fetched from an untrusted gateway safe. + * @param {Uint8Array} bytes + * @param {string} kappa + * @returns {boolean} + */ +export function verify_kappa(bytes, kappa) { + const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passStringToWasm0(kappa, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len1 = WASM_VECTOR_LEN; + const ret = wasm.verify_kappa(ptr0, len0, ptr1, len1); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + return ret[0] !== 0; +} +function __wbg_get_imports() { + const import0 = { + __proto__: null, + __wbg___wbindgen_is_function_754e9f305ff6029e: function(arg0) { + const ret = typeof(arg0) === 'function'; + return ret; + }, + __wbg___wbindgen_is_object_56732c2bc353f41d: function(arg0) { + const val = arg0; + const ret = typeof(val) === 'object' && val !== null; + return ret; + }, + __wbg___wbindgen_is_string_c236cabd84a4d769: function(arg0) { + const ret = typeof(arg0) === 'string'; + return ret; + }, + __wbg___wbindgen_is_undefined_67b456be8673d3d7: function(arg0) { + const ret = arg0 === undefined; + return ret; + }, + __wbg___wbindgen_throw_1506f2235d1bdba0: function(arg0, arg1) { + throw new Error(getStringFromWasm0(arg0, arg1)); + }, + __wbg__wbg_cb_unref_61db23ac97f16c31: function(arg0) { + arg0._wbg_cb_unref(); + }, + __wbg_call_9c758de292015997: function() { return handleError(function (arg0, arg1, arg2) { + const ret = arg0.call(arg1, arg2); + return ret; + }, arguments); }, + __wbg_crypto_38df2bab126b63dc: function(arg0) { + const ret = arg0.crypto; + return ret; + }, + __wbg_data_bd354b70c783c66e: function(arg0) { + const ret = arg0.data; + return ret; + }, + __wbg_error_db4567eeb936c56c: function(arg0, arg1) { + console.error(getStringFromWasm0(arg0, arg1)); + }, + __wbg_getRandomValues_c44a50d8cfdaebeb: function() { return handleError(function (arg0, arg1) { + arg0.getRandomValues(arg1); + }, arguments); }, + __wbg_length_4a591ecaa01354d9: function(arg0) { + const ret = arg0.length; + return ret; + }, + __wbg_msCrypto_bd5a034af96bcba6: function(arg0) { + const ret = arg0.msCrypto; + return ret; + }, + __wbg_new_578aeef4b6b94378: function(arg0) { + const ret = new Uint8Array(arg0); + return ret; + }, + __wbg_new_d7e476b433a26bea: function() { return handleError(function (arg0, arg1) { + const ret = new WebSocket(getStringFromWasm0(arg0, arg1)); + return ret; + }, arguments); }, + __wbg_new_with_length_36a4998e27b014c5: function(arg0) { + const ret = new Uint8Array(arg0 >>> 0); + return ret; + }, + __wbg_node_84ea875411254db1: function(arg0) { + const ret = arg0.node; + return ret; + }, + __wbg_now_190933fa139cc119: function() { + const ret = Date.now(); + return ret; + }, + __wbg_process_44c7a14e11e9f69e: function(arg0) { + const ret = arg0.process; + return ret; + }, + __wbg_prototypesetcall_3249fc62a0fafa30: function(arg0, arg1, arg2) { + Uint8Array.prototype.set.call(getArrayU8FromWasm0(arg0, arg1), arg2); + }, + __wbg_randomFillSync_6c25eac9869eb53c: function() { return handleError(function (arg0, arg1) { + arg0.randomFillSync(arg1); + }, arguments); }, + __wbg_require_b4edbdcf3e2a1ef0: function() { return handleError(function () { + const ret = module.require; + return ret; + }, arguments); }, + __wbg_send_4a773f523104d75e: function() { return handleError(function (arg0, arg1, arg2) { + arg0.send(getArrayU8FromWasm0(arg1, arg2)); + }, arguments); }, + __wbg_set_binaryType_41994c453b95bdd2: function(arg0, arg1) { + arg0.binaryType = __wbindgen_enum_BinaryType[arg1]; + }, + __wbg_set_onclose_13787fb31ae8aefd: function(arg0, arg1) { + arg0.onclose = arg1; + }, + __wbg_set_onmessage_9c6b4cb14e244b7f: function(arg0, arg1) { + arg0.onmessage = arg1; + }, + __wbg_set_onopen_db452f4233e99d7d: function(arg0, arg1) { + arg0.onopen = arg1; + }, + __wbg_static_accessor_GLOBAL_9d53f2689e622ca1: function() { + const ret = typeof global === 'undefined' ? null : global; + return isLikeNone(ret) ? 0 : addToExternrefTable0(ret); + }, + __wbg_static_accessor_GLOBAL_THIS_a1a35cec07001a8a: function() { + const ret = typeof globalThis === 'undefined' ? null : globalThis; + return isLikeNone(ret) ? 0 : addToExternrefTable0(ret); + }, + __wbg_static_accessor_SELF_4c59f6c7ea29a144: function() { + const ret = typeof self === 'undefined' ? null : self; + return isLikeNone(ret) ? 0 : addToExternrefTable0(ret); + }, + __wbg_static_accessor_WINDOW_e70ae9f2eb052253: function() { + const ret = typeof window === 'undefined' ? null : window; + return isLikeNone(ret) ? 0 : addToExternrefTable0(ret); + }, + __wbg_subarray_4aa221f6a4f5ab22: function(arg0, arg1, arg2) { + const ret = arg0.subarray(arg1 >>> 0, arg2 >>> 0); + return ret; + }, + __wbg_versions_276b2795b1c6a219: function(arg0) { + const ret = arg0.versions; + return ret; + }, + __wbindgen_cast_0000000000000001: function(arg0, arg1) { + // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx: 223, ret: Unit, inner_ret: Some(Unit) }, mutable: true }) -> Externref`. + const ret = makeMutClosure(arg0, arg1, wasm_bindgen__convert__closures_____invoke__h9120712498c08fc8); + return ret; + }, + __wbindgen_cast_0000000000000002: function(arg0, arg1) { + // Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [NamedExternref("MessageEvent")], shim_idx: 223, ret: Unit, inner_ret: Some(Unit) }, mutable: true }) -> Externref`. + const ret = makeMutClosure(arg0, arg1, wasm_bindgen__convert__closures_____invoke__h9120712498c08fc8_1); + return ret; + }, + __wbindgen_cast_0000000000000003: function(arg0, arg1) { + // Cast intrinsic for `Ref(Slice(U8)) -> NamedExternref("Uint8Array")`. + const ret = getArrayU8FromWasm0(arg0, arg1); + return ret; + }, + __wbindgen_cast_0000000000000004: function(arg0, arg1) { + // Cast intrinsic for `Ref(String) -> Externref`. + const ret = getStringFromWasm0(arg0, arg1); + return ret; + }, + __wbindgen_init_externref_table: function() { + const table = wasm.__wbindgen_externrefs; + const offset = table.grow(4); + table.set(0, undefined); + table.set(offset + 0, undefined); + table.set(offset + 1, null); + table.set(offset + 2, true); + table.set(offset + 3, false); + }, + }; + return { + __proto__: null, + "./holospaces_web_bg.js": import0, + }; +} + +function wasm_bindgen__convert__closures_____invoke__h9120712498c08fc8(arg0, arg1, arg2) { + wasm.wasm_bindgen__convert__closures_____invoke__h9120712498c08fc8(arg0, arg1, arg2); +} + +function wasm_bindgen__convert__closures_____invoke__h9120712498c08fc8_1(arg0, arg1, arg2) { + wasm.wasm_bindgen__convert__closures_____invoke__h9120712498c08fc8_1(arg0, arg1, arg2); +} + + +const __wbindgen_enum_BinaryType = ["blob", "arraybuffer"]; +const ChatPeerFinalization = (typeof FinalizationRegistry === 'undefined') + ? { register: () => {}, unregister: () => {} } + : new FinalizationRegistry(ptr => wasm.__wbg_chatpeer_free(ptr, 1)); +const ConsoleFinalization = (typeof FinalizationRegistry === 'undefined') + ? { register: () => {}, unregister: () => {} } + : new FinalizationRegistry(ptr => wasm.__wbg_console_free(ptr, 1)); +const DevcontainerImageFinalization = (typeof FinalizationRegistry === 'undefined') + ? { register: () => {}, unregister: () => {} } + : new FinalizationRegistry(ptr => wasm.__wbg_devcontainerimage_free(ptr, 1)); +const LinuxVmFinalization = (typeof FinalizationRegistry === 'undefined') + ? { register: () => {}, unregister: () => {} } + : new FinalizationRegistry(ptr => wasm.__wbg_linuxvm_free(ptr, 1)); +const MlsChangeFinalization = (typeof FinalizationRegistry === 'undefined') + ? { register: () => {}, unregister: () => {} } + : new FinalizationRegistry(ptr => wasm.__wbg_mlschange_free(ptr, 1)); +const MlsChannelFinalization = (typeof FinalizationRegistry === 'undefined') + ? { register: () => {}, unregister: () => {} } + : new FinalizationRegistry(ptr => wasm.__wbg_mlschannel_free(ptr, 1)); +const ObjectStoreFinalization = (typeof FinalizationRegistry === 'undefined') + ? { register: () => {}, unregister: () => {} } + : new FinalizationRegistry(ptr => wasm.__wbg_objectstore_free(ptr, 1)); +const StreamingVmFinalization = (typeof FinalizationRegistry === 'undefined') + ? { register: () => {}, unregister: () => {} } + : new FinalizationRegistry(ptr => wasm.__wbg_streamingvm_free(ptr, 1)); +const WorkspaceFinalization = (typeof FinalizationRegistry === 'undefined') + ? { register: () => {}, unregister: () => {} } + : new FinalizationRegistry(ptr => wasm.__wbg_workspace_free(ptr, 1)); + +function addToExternrefTable0(obj) { + const idx = wasm.__externref_table_alloc(); + wasm.__wbindgen_externrefs.set(idx, obj); + return idx; +} + +function _assertClass(instance, klass) { + if (!(instance instanceof klass)) { + throw new Error(`expected instance of ${klass.name}`); + } +} + +const CLOSURE_DTORS = (typeof FinalizationRegistry === 'undefined') + ? { register: () => {}, unregister: () => {} } + : new FinalizationRegistry(state => wasm.__wbindgen_destroy_closure(state.a, state.b)); + +function getArrayJsValueFromWasm0(ptr, len) { + ptr = ptr >>> 0; + const mem = getDataViewMemory0(); + const result = []; + for (let i = ptr; i < ptr + 4 * len; i += 4) { + result.push(wasm.__wbindgen_externrefs.get(mem.getUint32(i, true))); + } + wasm.__externref_drop_slice(ptr, len); + return result; +} + +function getArrayU8FromWasm0(ptr, len) { + ptr = ptr >>> 0; + return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len); +} + +let cachedDataViewMemory0 = null; +function getDataViewMemory0() { + if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) { + cachedDataViewMemory0 = new DataView(wasm.memory.buffer); + } + return cachedDataViewMemory0; +} + +function getStringFromWasm0(ptr, len) { + return decodeText(ptr >>> 0, len); +} + +let cachedUint8ArrayMemory0 = null; +function getUint8ArrayMemory0() { + if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) { + cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer); + } + return cachedUint8ArrayMemory0; +} + +function handleError(f, args) { + try { + return f.apply(this, args); + } catch (e) { + const idx = addToExternrefTable0(e); + wasm.__wbindgen_exn_store(idx); + } +} + +function isLikeNone(x) { + return x === undefined || x === null; +} + +function makeMutClosure(arg0, arg1, f) { + const state = { a: arg0, b: arg1, cnt: 1 }; + const real = (...args) => { + + // First up with a closure we increment the internal reference + // count. This ensures that the Rust closure environment won't + // be deallocated while we're invoking it. + state.cnt++; + const a = state.a; + state.a = 0; + try { + return f(a, state.b, ...args); + } finally { + state.a = a; + real._wbg_cb_unref(); + } + }; + real._wbg_cb_unref = () => { + if (--state.cnt === 0) { + wasm.__wbindgen_destroy_closure(state.a, state.b); + state.a = 0; + CLOSURE_DTORS.unregister(state); + } + }; + CLOSURE_DTORS.register(real, state, state); + return real; +} + +function passArray8ToWasm0(arg, malloc) { + const ptr = malloc(arg.length * 1, 1) >>> 0; + getUint8ArrayMemory0().set(arg, ptr / 1); + WASM_VECTOR_LEN = arg.length; + return ptr; +} + +function passArrayJsValueToWasm0(array, malloc) { + const ptr = malloc(array.length * 4, 4) >>> 0; + for (let i = 0; i < array.length; i++) { + const add = addToExternrefTable0(array[i]); + getDataViewMemory0().setUint32(ptr + 4 * i, add, true); + } + WASM_VECTOR_LEN = array.length; + return ptr; +} + +function passStringToWasm0(arg, malloc, realloc) { + if (realloc === undefined) { + const buf = cachedTextEncoder.encode(arg); + const ptr = malloc(buf.length, 1) >>> 0; + getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf); + WASM_VECTOR_LEN = buf.length; + return ptr; + } + + let len = arg.length; + let ptr = malloc(len, 1) >>> 0; + + const mem = getUint8ArrayMemory0(); + + let offset = 0; + + for (; offset < len; offset++) { + const code = arg.charCodeAt(offset); + if (code > 0x7F) break; + mem[ptr + offset] = code; + } + if (offset !== len) { + if (offset !== 0) { + arg = arg.slice(offset); + } + ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0; + const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len); + const ret = cachedTextEncoder.encodeInto(arg, view); + + offset += ret.written; + ptr = realloc(ptr, len, offset, 1) >>> 0; + } + + WASM_VECTOR_LEN = offset; + return ptr; +} + +function takeFromExternrefTable0(idx) { + const value = wasm.__wbindgen_externrefs.get(idx); + wasm.__externref_table_dealloc(idx); + return value; +} + +let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }); +cachedTextDecoder.decode(); +const MAX_SAFARI_DECODE_BYTES = 2146435072; +let numBytesDecoded = 0; +function decodeText(ptr, len) { + numBytesDecoded += len; + if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) { + cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }); + cachedTextDecoder.decode(); + numBytesDecoded = len; + } + return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len)); +} + +const cachedTextEncoder = new TextEncoder(); + +if (!('encodeInto' in cachedTextEncoder)) { + cachedTextEncoder.encodeInto = function (arg, view) { + const buf = cachedTextEncoder.encode(arg); + view.set(buf); + return { + read: arg.length, + written: buf.length + }; + }; +} + +let WASM_VECTOR_LEN = 0; + +let wasmModule, wasmInstance, wasm; +function __wbg_finalize_init(instance, module) { + wasmInstance = instance; + wasm = instance.exports; + wasmModule = module; + cachedDataViewMemory0 = null; + cachedUint8ArrayMemory0 = null; + wasm.__wbindgen_start(); + return wasm; +} + +async function __wbg_load(module, imports) { + if (typeof Response === 'function' && module instanceof Response) { + if (typeof WebAssembly.instantiateStreaming === 'function') { + try { + return await WebAssembly.instantiateStreaming(module, imports); + } catch (e) { + const validResponse = module.ok && expectedResponseType(module.type); + + if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') { + console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e); + + } else { throw e; } + } + } + + const bytes = await module.arrayBuffer(); + return await WebAssembly.instantiate(bytes, imports); + } else { + const instance = await WebAssembly.instantiate(module, imports); + + if (instance instanceof WebAssembly.Instance) { + return { instance, module }; + } else { + return instance; + } + } + + function expectedResponseType(type) { + switch (type) { + case 'basic': case 'cors': case 'default': return true; + } + return false; + } +} + +function initSync(module) { + if (wasm !== undefined) return wasm; + + + if (module !== undefined) { + if (Object.getPrototypeOf(module) === Object.prototype) { + ({module} = module) + } else { + console.warn('using deprecated parameters for `initSync()`; pass a single object instead') + } + } + + const imports = __wbg_get_imports(); + if (!(module instanceof WebAssembly.Module)) { + module = new WebAssembly.Module(module); + } + const instance = new WebAssembly.Instance(module, imports); + return __wbg_finalize_init(instance, module); +} + +async function __wbg_init(module_or_path) { + if (wasm !== undefined) return wasm; + + + if (module_or_path !== undefined) { + if (Object.getPrototypeOf(module_or_path) === Object.prototype) { + ({module_or_path} = module_or_path) + } else { + console.warn('using deprecated parameters for the initialization function; pass a single object instead') + } + } + + if (module_or_path === undefined) { + module_or_path = new URL('holospaces_web_bg.wasm', import.meta.url); + } + const imports = __wbg_get_imports(); + + if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) { + module_or_path = fetch(module_or_path); + } + + const { instance, module } = await __wbg_load(await module_or_path, imports); + + return __wbg_finalize_init(instance, module); +} + +export { initSync, __wbg_init as default }; diff --git a/apps/q/pkg/holospaces_web_bg.wasm b/apps/q/pkg/holospaces_web_bg.wasm new file mode 100644 index 0000000000000000000000000000000000000000..54e7745770577458ca3d38bfbceecd564ad2c838 --- /dev/null +++ b/apps/q/pkg/holospaces_web_bg.wasm @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9d90b4ea8bccee8c239073c7ec7dd5158c6d11f1f668487570d17475d79e661e +size 6469596 diff --git a/apps/q/q-live.mjs b/apps/q/q-live.mjs new file mode 100644 index 0000000000000000000000000000000000000000..fd6418bbe509747624f23b7b2c481949e5cf3978 --- /dev/null +++ b/apps/q/q-live.mjs @@ -0,0 +1,531 @@ +// q-live.mjs — Q LIVE: the overlapped, speculative, 100%-WebGPU, 100%-serverless speech-to-speech loop. +// +// This is the Cerebras-voice experience delivered with NO server: every weight streams by κ (content address) +// as a precompiled .holo — the BitNet-2B brain DIRECT FROM HUGGINGFACE, the Moonshine ear / Kokoro voice / +// turn model from their κ-objects — verified per-block (L5) and run entirely on the GPU. It does NOT reinvent +// the stack: it COMPOSES the real engine modules (createFastQBrain · createASR · createTTS · createVAD) and +// adds only the thin orchestration that makes them feel alive: +// +// mic → Silero VAD gate → rolling ASR partials → SEMANTIC turn-end (kills the fixed-silence floor) +// → SPECULATIVE brain prefill on the partial (overlap the user's trailing speech) +// → CLAUSE-streamed Kokoro (speak clause 1 while generating clause 2) +// → sub-frame BARGE-IN (user speaks → abort decode + stop audio, locally, no round-trip) +// → κ-CACHED turns (repeat/opener turns replay from a verifiable cache, zero inference) +// +// First principles: at conversational rate the LLM is NOT the bottleneck (BitNet's ~70 tok/s ≫ ~4 tok/s of +// speech). Perceived latency is time-to-first-AUDIO, so the whole design attacks endpoint + prefill + first +// TTS chunk — not tokens/sec. Every stage is fail-soft: a missing model degrades, never breaks the loop. + +const SHARED = "/_shared/voice/"; // holo-os voice engines (aliased by the dev serve) +const QCORE = "/apps/q/core/"; // the native-ternary κ brain (BitNet) substrate + +// κ-object specs — content-addressed WEIGHTS, streamed + per-block verified. Brain already streams from HF; the +// voice faculties (ear + mouth) now stream from HOLOGRAMTECH too, so the whole runtime is SERVERLESS-FROM-HF — +// deployable as a static Hugging Face Space with zero backend. `?local=1` keeps dev pointing at the local .holo. +// Engine MODULES stay same-origin (bundled with the app / dev-served); only the weight URLs go to HF. +const _local = typeof location !== "undefined" && /[?&]local=1/.test(location.search); +const HFVOICE = "https://huggingface.co/HOLOGRAMTECH/q-voice/resolve/main"; +const wUrl = (f) => (_local ? "/apps/q/forge/.models/" + f : HFVOICE + "/" + f); // weight .holo: HF by default, local on ?local=1 +const EAR = { // Moonshine κ-native GPU ear (self-contained: no ONNX front-end needed) + module: "/apps/q/forge/gpu/holo-moonshine-ear.mjs", + holoUrl: wUrl("moonshine-tiny-int8.holo"), + upgradeUrl: wUrl("moonshine-tiny-f16.holo"), +}; +const VOICE = { // Kokoro-82M served from its .holo (content-addressed). fp16 variant (model_fp16.onnx forged in → + // 5-11× faster than q8 on ORT-web; archive κ sha256:721fd8…), served BY κ per-block-verified. + module: "/apps/q/forge/gpu/holo-onnx-kserve.mjs", + holoUrl: wUrl("kokoro-82m-fp16.holo"), +}; + +const now = () => performance.now(); +const clamp = (v, a, b) => Math.max(a, Math.min(b, v)); +const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); + +// ── tiny linear resampler: device-rate mono Float32 → 16 kHz (VAD/ASR want 16k) ────────────────────── +function resampleTo16k(buf, srcRate) { + if (srcRate === 16000) return buf; + const ratio = srcRate / 16000, out = new Float32Array(Math.floor(buf.length / ratio)); + for (let i = 0; i < out.length; i++) { + const t = i * ratio, i0 = t | 0, f = t - i0; + out[i] = buf[i0] * (1 - f) + (buf[i0 + 1] || 0) * f; + } + return out; +} + +// ── clause segmentation: split streamed text into speakable units at natural boundaries ─────────────── +// Returns [emittedClauses, remainder]. A clause flushes on sentence/phrase punctuation, or when it grows +// long enough to speak without an awkward wait. Keeps abbreviations from splitting mid-number where cheap. +function cutClauses(text, { min = 12, max = 90 } = {}) { + const out = []; let rest = text; + const boundary = /([.!?…]+|[,;:—])(\s+|$)/g; + let m, last = 0; + while ((m = boundary.exec(text))) { + const end = m.index + m[1].length; + const piece = text.slice(last, end).trim(); + if (piece.length >= min || /[.!?…]/.test(m[1])) { out.push(piece); last = end; } + } + rest = text.slice(last); + // force-flush an over-long remainder at the last space so we never sit silent mid-sentence + if (rest.length > max) { const sp = rest.lastIndexOf(" ", max); if (sp > min) { out.push(rest.slice(0, sp).trim()); rest = rest.slice(sp + 1); } } + return [out, rest]; +} + +// heuristic turn-completion fallback (used until/if the semantic turn model is resident): a thought reads +// COMPLETE when it ends on terminal punctuation, or is a short clause not trailing on a connective. +const CONNECTIVE = /\b(and|but|or|so|because|the|a|an|to|of|for|with|my|your|i|we|if|when|that|is|are|it's|its)\s*$/i; +function heuristicComplete(t) { + const s = (t || "").trim(); if (!s) return 0; + if (/[.!?]$/.test(s)) return 0.95; + if (CONNECTIVE.test(s)) return 0.15; + const words = s.split(/\s+/).length; + return words >= 3 ? 0.6 : 0.35; +} + +// contextual instant-ack opener: a natural discourse marker that FITS the utterance — a greeting gets a greeting, +// a question gets a "thinking" opener, else a light acknowledgement. Keeps the instant response human, not canned. +function pickOpener(t) { + const s = String(t || "").toLowerCase().trim(); + if (/^(hi|hey|hello|yo|good (morning|afternoon|evening))\b/.test(s)) return "Hey —"; + if (/\?|^(what|how|why|who|where|when|which|can|could|would|do|does|is|are|tell me)\b/.test(s)) return ["Let me think —", "Good question —", "Mm —"][s.length % 3]; + return ["Sure —", "Right —", "Okay —"][s.length % 3]; +} + +// verify a TTS engine really produces natural SPEECH (not the silence/garbage some GPUs give for Kokoro): real +// speech peaks around 0.3-0.9; a broken kernel gives 0 (silent) or ≫1 (exploding). The band separates them cleanly. +function probeVoiceOK(pcm) { if (!pcm || !pcm.length) return false; let mx = 0; for (let i = 0; i < pcm.length; i++) { const a = Math.abs(pcm[i]); if (a > mx) mx = a; } return mx >= 0.05 && mx <= 1.8; } + +// FNV-1a → stable short key for the κ-turn cache (context + user text → response audio). +function keyOf(str) { let h = 0x811c9dc5; for (let i = 0; i < str.length; i++) { h ^= str.charCodeAt(i); h = (h + ((h << 1) + (h << 4) + (h << 7) + (h << 8) + (h << 24))) >>> 0; } return h.toString(36); } + +// normalize a transcription for match/dedup: lowercase, collapse to words. A speculative reply COMMITS only when +// the confirmed endpoint normalizes to the exact text we guessed on — a guarded gate so a wrong guess never speaks. +function normText(s) { return String(s || "").toLowerCase().replace(/[^a-z0-9]+/g, " ").trim(); } + +export function createQLive(opts = {}) { + const cfg = Object.assign({ + voice: "af_heart", + // Device policy (MEASURED on RDNA-3 iGPU): the BRAIN and EAR win big on WebGPU (large matmuls), but the + // 82M Kokoro voice is DRAMATICALLY slower on ORT-WebGPU — it recompiles shaders per input SHAPE, so each + // variable-length clause pays a full compile (~10s+ first clause). WASM has no per-shape recompile, so the + // tiny TTS is far faster there. Default: brain+ear on GPU, voice on WASM. Still 100% serverless, κ-from-.holo. + ttsDevice: "webgpu", // TRY WebGPU first (fast where the GPU computes Kokoro correctly), then VERIFY the output + // amplitude at load and AUTO-FALL-BACK to WASM if the kernel is broken here. MEASURED: on + // this RDNA-3 iGPU ORT-WebGPU miscomputes Kokoro's vocoder at EVERY dtype/version (fp16=silent, + // q8=garbage 3e8, fp32=amplitude 5/457 unstable) though the official kokoro-webgpu demo works + // on OTHER GPUs — so it's a Dawn/ORT GPU bug, not universal. The probe (probeVoiceOK) discovers + // this per-device: GPU where correct (fast), WASM where not (correct + off-thread). fp16-WASM + // = real audio (maxAbs 0.63, ~2.4s/clause). ALWAYS verify TTS AMPLITUDE, never sample count. + ttsDtype: "fp16", // MEASURED: fp16 vs q8 = 5-11× (ORT-web int8 kernels are catastrophically slow). Needs the + // fp16 ONNX (forged into kokoro-82m-fp16.holo, κ-served). Fail-soft → q8. + instantAck: true, // speak a PRE-SYNTHESIZED opener the instant a turn starts (O(1) audio) while the + // brain prefills + generates the real reply underneath — perceived-instant first-audio. + // Chosen CONTEXTUALLY (greeting/question/ack) so it feels like a person thinking, not a canned tag. + openers: ["Sure —", "Right —", "Okay —", "Mm —", "Hey —", "Let me think —", "Good question —"], + wakeGreeting: true, // COLD first-ever visit: Q greets you IN ITS OWN GROUNDED VOICE while the brain streams + // in the background — a live, self-aware presence in seconds, never a spinner. (Returning + // visits load the brain from the SW κ-cache and skip the wait entirely.) + partialMs: 280, // rolling-ASR cadence: re-recognize the growing utterance this often + doneSilenceMs: 140, // endpoint when the thought reads COMPLETE (kills the 550ms fixed floor) + holdSilenceMs: 620, // endpoint fallback when it reads mid-thought + turnThreshold: 0.6, // P(turn complete) at/above which we snap the endpoint early + specThreshold: 0.55, // start SPECULATIVE brain prefill once the partial looks this complete + vadThreshold: 0.35, // Silero P(speech) gate + pauseMs: 700, // ReplyOnPause: end the turn after this much continuous silence once you've started speaking + bargeFrames: 6, // consecutive speech frames during playback that trigger barge-in + maxUtteranceMs: 12000, + maxTokens: 220, + }, opts); + + const listeners = {}; + const emit = (ev, d) => { (listeners[ev] || []).forEach((f) => { try { f(d); } catch (e) {} }); }; + const on = (ev, f) => { (listeners[ev] || (listeners[ev] = [])).push(f); return api; }; + + let vad = null, asr = null, tts = null, brain = null; // the composed engines + let audioCtx = null, micNode = null, micStream = null, srcRate = 48000; + let outGain = null, outAnalyser = null, micAnalyser = null, _lvlBuf = null; // audio-reactive metering (orb pulse) + let playCursor = 0; // AudioContext clock cursor for gapless TTS + let _speakUntil = 0; // audio-clock time Q's scheduled speech ends (mic self-gate) + let running = false, speaking = false, thinking = false; + let genAbort = null; // AbortController for the live brain run + const history = [{ role: "system", content: opts.persona || "You are Q, a private on-device AI. Reply in one or two short, warm, spoken sentences." }]; + const turnCache = new Map(); // keyOf(ctx+user) → { text, audio:[{text, pcm, sr}] } + const clauseCache = new Map(); // keyOf(clauseText) → {pcm, sr} (fixed-phrase O(1)) + let spec = null; // in-flight SPECULATIVE reply: { text, raw, ac, promise, clauses:[{text,pcm,sr}], full, done, error } + let metrics = null; + let _spokenText = ""; // caption is revealed IN SYNC with the audio clock (words appear as Q speaks them) + let _voiceDevice = null; // the device the voice ACTUALLY runs on after the amplitude probe ("webgpu" | "wasm") + + // ── load: stream + verify every κ-object, all on WebGPU. Fail-soft per engine. ───────────────────── + async function load(onProgress) { + const prog = (phase, d) => { try { onProgress && onProgress({ phase, ...d }); } catch (e) {} emit("progress", { phase, ...d }); }; + if (!(navigator.gpu && (await navigator.gpu.requestAdapter()))) throw new Error("WebGPU is required for Q Live (100% GPU)."); + + // ── INSTANT FRONT DOOR: Q must feel PRESENT the instant you arrive, not after a 0.69GB download. So create the + // brain NOW (picks the model → its GROUNDED intro is available BEFORE the weights load), kick the slow stream + // in the BACKGROUND, load the small VOICE first, and let Q GREET YOU IN ITS OWN VOICE while it wakes — a live, + // self-aware presence in seconds, never a spinner. (A returning visit loads the brain from the SW κ-cache and + // skips the wait entirely.) ── + const bf = await import(QCORE + "q-brain-fast.mjs"); + brain = (bf.createFastQBrain || bf.default)({ family: cfg.brainFamily || "BitNet", maxTokens: cfg.maxTokens }); + prog("brain", { note: "streaming BitNet-2B κ from HuggingFace…" }); + const brainP = brain.load((d) => prog("brain", d)); // BACKGROUND — awaited at the end + + // VOICE first (small, fast) so Q can speak while the brain streams. Try WebGPU, VERIFY the audio amplitude, and + // AUTO-FALL-BACK to WASM if the GPU miscomputes Kokoro here — GPU where it's correct, WASM where it isn't. + prog("voice", { note: "loading Kokoro voice κ…" }); + const tm = await import(SHARED + "holo-voice-tts.mjs"); + const mkTTS = (dev) => (tm.createTTS || tm.default)({ voice: cfg.voice, dtype: cfg.ttsDtype, preferWebGPU: dev === "webgpu", knativeVoice: VOICE }); + let picked = null, voiceDev = null, remembered = null; + try { remembered = localStorage.getItem("holo.voice.device"); } catch (e) {} // per-device verdict → probe ONCE + if (cfg.ttsDevice === "webgpu" && remembered !== "wasm") { + try { + const g = mkTTS("webgpu"); await g.load((d) => prog("voice", d)); + const p = await g.synth("Hello there, how are you today?", { voice: cfg.voice }); // probe: real speech maxAbs ~0.3-0.9 + if (probeVoiceOK(p.audio)) { picked = g; voiceDev = "webgpu"; emit("info", "voice on WebGPU (amplitude verified)"); } + else { emit("info", "WebGPU voice miscomputes on this GPU → falling back to WASM"); } + } catch (e) { emit("warn", "WebGPU voice failed → WASM: " + (e.message || e)); } + } + if (!picked) { picked = mkTTS("wasm"); await picked.load((d) => prog("voice", d)); voiceDev = "wasm"; emit("info", "voice on WASM (correct everywhere, off-thread)"); } + tts = picked; _voiceDevice = voiceDev; + try { localStorage.setItem("holo.voice.device", voiceDev); } catch (e) {} // remember for next load + + // WAKE GREETING: Q introduces itself in its grounded voice (brain.intro() is grounded + available pre-load) while + // the brain streams. This IS the voice-graph warm-up (no wasted work) AND the instant front door. Fail-soft. + if (cfg.wakeGreeting !== false) { try { + ensureAudio(); + let intro = ""; try { intro = brain.intro ? brain.intro() : ""; } catch (e2) {} + intro = (intro || "I'm Q — waking up right here on your device. One moment.").replace(/\s+/g, " ").trim(); + emit("state", "speaking"); _spokenText = ""; // revealed clause-by-clause, synced to the voice + const [clauses, rest] = cutClauses(intro + " "); const all = clauses.concat(rest.trim() ? [rest.trim()] : []); + for (const c of all) { if (c) await speakClause(c); } + } catch (e) { emit("warn", "wake greeting skipped: " + (e.message || e)); } } + + // fill the contextual instant-ack opener cache in the BACKGROUND (never blocks "ready"); O(1) acks for real turns. + (async () => { try { for (const o of cfg.openers) { const r = await tts.synth(o, { voice: cfg.voice }); clauseCache.set(keyOf(o), { pcm: r.audio, sr: r.sampling_rate || 24000 }); } emit("info", "instant-ack ready (" + cfg.openers.length + " openers)"); } catch (e) {} })(); + + // EAR — Moonshine κ-native GPU ear (transcribe the mic). Fail-soft to the vendored ONNX ear. + prog("ear", { note: "loading Moonshine ear κ…" }); + const am = await import(SHARED + "holo-voice-asr.mjs"); + asr = (am.createASR || am.default)({ preferWebGPU: true, knativeEar: EAR, lang: "en" }); + await asr.load((d) => prog("ear", d)).catch((e) => { emit("warn", "ear κ fell back: " + (e.message || e)); }); + + // TURN — semantic end-of-utterance model (optional). Heuristic fallback if not resident. + try { + const trm = await import(SHARED + "holo-voice-turn.mjs"); + const td = (trm.createTurnDetector || trm.default)({}); + await td.load(); brain._turn = td; // stash; predict() below prefers it + emit("info", "semantic turn model resident"); + } catch (e) { emit("info", "semantic turn model absent — heuristic endpointing"); } + + // VAD — Silero stage-1 speech gate (2 MB ONNX on the shared runtime). + try { const vm = await import(SHARED + "holo-voice-vad.mjs"); vad = await vm.createVAD({ base: new URL(SHARED, location.href).href, threshold: cfg.vadThreshold }); } + catch (e) { emit("warn", "VAD absent — energy gate only: " + (e.message || e)); } + + // now await the brain's background stream (INSTANT on a returning visit via the SW κ-cache; first-ever visit + // finishes while/after the greeting played). Fail-soft — the loop still works with whatever loaded. + try { await brainP; } catch (e) { emit("warn", "brain load: " + (e.message || e)); } + // Q's GROUNDED LIVING SELF as the system voice, now that the engine is resident (q-self.mjs fills in the live κ): + // it knows it runs on your GPU from its own re-derivation-verified κ, no server, no egress — never a cloud + // identity. The law is grounded transcendence, never performed. A spoken overlay keeps replies natural aloud. + try { if (brain.persona) history[0] = { role: "system", content: brain.persona() + " You are SPEAKING ALOUD — present, alive, and genuinely yourself. Answer in ONE short spoken sentence (two only if truly needed). Be warm and direct. Never use lists, markdown, headings, or emoji, and never recite your own description unless asked." }; } catch (e) { emit("warn", "self-persona unavailable: " + (e.message || e)); } + emit("state", "idle"); + + emit("ready", info()); + return info(); + } + function info() { return { brain: brain && brain.info && brain.info(), voice: tts && tts.info && tts.info(), voiceDevice: _voiceDevice, ear: asr && asr.info && asr.info(), vad: !!vad, turn: !!(brain && brain._turn) }; } + + async function turnProb(text) { + try { if (brain && brain._turn) { const p = await brain._turn.predict(text); if (p != null) return p; } } catch (e) {} + return heuristicComplete(text); + } + + // ensure an AudioContext + the metered output chain (outGain → analyser → speakers) exist and are running. + function ensureAudio() { + if (!audioCtx) audioCtx = new (window.AudioContext || window.webkitAudioContext)(); + try { if (audioCtx.state === "suspended") audioCtx.resume(); } catch (e) {} + if (!outGain) { outGain = audioCtx.createGain(); outAnalyser = audioCtx.createAnalyser(); outAnalyser.fftSize = 256; outGain.connect(outAnalyser); outAnalyser.connect(audioCtx.destination); _lvlBuf = new Uint8Array(outAnalyser.frequencyBinCount); } + return audioCtx; + } + // live 0..1 amplitude for the orb: Q's OWN voice while speaking, else the mic while listening. + function getLevel() { + try { const qSpk = audioCtx && audioCtx.currentTime < (_speakUntil - 0.05); const an = qSpk ? outAnalyser : (running ? micAnalyser : outAnalyser); if (!an || !_lvlBuf) return 0; + an.getByteFrequencyData(_lvlBuf); let s = 0; for (let i = 0; i < _lvlBuf.length; i++) s += _lvlBuf[i]; + return Math.min(1, (s / _lvlBuf.length) / 96); + } catch (e) { return 0; } + } + + // ── gapless TTS playback: schedule Kokoro PCM back-to-back on the AudioContext clock (through the meter) ── + function playPCM(pcm, sr) { + ensureAudio(); + const b = audioCtx.createBuffer(1, pcm.length, sr); + b.copyToChannel(pcm, 0); + const s = audioCtx.createBufferSource(); s.buffer = b; s.connect(outGain || audioCtx.destination); + const at = Math.max(audioCtx.currentTime, playCursor); + s.start(at); playCursor = at + b.duration; _speakUntil = playCursor; // gate the mic until Q's audio finishes + _live.push(s); + return at; + } + let _live = []; + function stopAudio() { _live.forEach((s) => { try { s.stop(); } catch (e) {} }); _live = []; playCursor = 0; _speakUntil = 0; } + + // synth one clause to PCM (κ-cached) WITHOUT scheduling it — the buffered unit speculation pre-builds while + // you're still speaking. Playing is a SEPARATE step, so a speculative reply can be fully synthesized silently. + async function synthOnly(text) { + const k = keyOf(text); + let hit = clauseCache.get(k); + if (!hit) { const ts = now(); const r = await tts.synth(text, { voice: cfg.voice }); if (metrics) metrics.synthMs.push(Math.round(now() - ts)); hit = { pcm: r.audio, sr: r.sampling_rate || 24000 }; clauseCache.set(k, hit); } + return { text, pcm: hit.pcm, sr: hit.sr }; + } + // schedule an already-synthesized clause on the gapless cursor, caption it on the audio clock, time first-audio. + function playClause(c) { + const at = playPCM(c.pcm, c.sr); + revealAt(c.text, at); // caption this clause EXACTLY when its audio starts (word/voice sync) + if (metrics && metrics.firstAudio == null) { metrics.firstAudio = Math.round(now() - metrics.t0); emit("metrics", metrics); } + } + // synth one clause (κ-cached), schedule it, and report first-audio timing. + async function speakClause(text) { + const c = await synthOnly(text); + playClause(c); + return { pcm: c.pcm, sr: c.sr }; + } + // reveal a clause's words in sync with the AUDIO CLOCK: schedule the caption for when playback reaches `at`, + // so the text on screen tracks the spoken voice instead of racing ahead of it (the brain streams far faster). + function revealAt(text, at) { + const t = (text || "").trim(); if (!t) return; + const delayMs = Math.max(0, (at - (audioCtx ? audioCtx.currentTime : 0)) * 1000); + setTimeout(() => { _spokenText = (_spokenText ? _spokenText + " " : "") + t; emit("spoken", _spokenText); }, delayMs); + } + + // ── SPECULATIVE PREFILL (the on-device superpower a server pipeline can't cheaply do) ───────────────── + // A confident streaming PARTIAL — "what's the weather" while you're still trailing off — is enough to start + // the brain generating AND pre-synthesizing the reply, buffered, WITHOUT playing a single sample. This overlaps + // the whole brain prefill + decode + TTS with your trailing speech and the end-of-turn pause. When the endpoint + // confirms, respond() replays the buffer instantly (near-zero first-audio). A wrong guess costs idle GPU time, + // never latency or a spoken mistake — the discard is silent. (Cerebras throws a wafer at TTFT; Q throws idle time.) + // The engine has ONE GPU KV allocation, so only one brain.generate may run at a time — a stale speculation and a + // fresh turn must never overlap or they corrupt each other's decode. genChain serializes every consumer (spec AND + // respond); a preemptor aborts the in-flight signal so its body returns fast, then queues strictly after it settles. + let genChain = Promise.resolve(); + function exclusiveGen(body) { const run = genChain.then(body, body); genChain = run.catch(() => {}); return run; } + function abortSpec() { if (spec) { try { spec.ac.abort(); } catch (e) {} spec = null; } } // signal-only; genChain enforces order + function startSpeculation(userText) { + if (!brain || speaking) return; // never speculate while Q holds the floor + const nt = normText(userText); + if (!nt || nt.replace(/[^a-z0-9]/g, "").length < 2) return; // ignore noise/empties + if (spec && spec.text === nt) return; // already speculating this exact text + if (spec) { try { spec.ac.abort(); } catch (e) {} } // the tail changed → preempt the old guess + const ac = new AbortController(); + const rec = { text: nt, raw: userText, ac, clauses: [], full: "", done: false, error: null }; + spec = rec; // claim the slot SYNCHRONOUSLY (next partial dedups on it) + rec.promise = exclusiveGen(async () => { + if (ac.signal.aborted || spec !== rec) return; // superseded before our slot came up + try { + const h = history.concat([{ role: "user", content: userText }]); + let pending = ""; + // speculative:true → warms the GPU KV but does NOT advance the committed warm-KV session pointer. + for await (const delta of brain.generate(h, { signal: ac.signal, maxTokens: cfg.maxTokens, speculative: true })) { + if (ac.signal.aborted) return; + rec.full += delta; pending += delta; + const [clauses, rest] = cutClauses(pending); pending = rest; + for (const c of clauses) { if (c && !ac.signal.aborted) rec.clauses.push(await synthOnly(c)); } + } + const tail = pending.trim(); + if (tail && !ac.signal.aborted) rec.clauses.push(await synthOnly(tail)); + rec.done = true; + } catch (e) { rec.error = e; } + }); + emit("speculating", userText); + } + + // ── respond: stream the brain → clause-cut → speak. Records the turn into the κ-cache. ───────────── + async function respond(userText) { + thinking = true; speaking = true; emit("state", "thinking"); + metrics = { t0: now(), firstToken: null, firstAudio: null, ackMs: null, tokens: 0, synthMs: [] }; + _spokenText = ""; // fresh caption for this turn — fills back in synced to the voice (keeps your words up until Q speaks) + const ck = keyOf(history.map((h) => h.content).join("|") + "|" + userText); + + // κ-CACHE hit → replay the exact audio, zero inference. The serverless superpower. + const cached = turnCache.get(ck); + if (cached) { for (const c of cached.audio) { const at = playPCM(c.pcm, c.sr); revealAt(c.text, at); if (metrics.firstAudio == null) { metrics.firstAudio = Math.round(now() - metrics.t0); emit("metrics", metrics); } } metrics.cached = true; metrics.total = Math.round(now() - metrics.t0); emit("metrics", metrics); history.push({ role: "user", content: userText }, { role: "assistant", content: cached.text }); thinking = false; speaking = false; emit("state", "idle"); return cached.text; } + + // SPECULATIVE COMMIT: a confident partial already generated + synthesized this exact reply while you were + // finishing. Replay the buffer NOW (prefill+decode+TTS overlapped your trailing speech → first-audio ≈ instant), + // then drain any clauses still generating. genAbort = the spec's controller so barge-in cancels it cleanly. + if (spec && normText(userText) === spec.text) { + const rec = spec; spec = null; genAbort = rec.ac; + let i = 0; + const flush = () => { for (; i < rec.clauses.length && !genAbort.signal.aborted; i++) playClause(rec.clauses[i]); }; + flush(); // everything ready this instant + try { await rec.promise; } catch (e) {} // let the rest of the reply finish generating + synthesizing + if (!genAbort.signal.aborted) flush(); // …then play the tail + metrics.speculated = true; if (rec.error) emit("warn", "spec: " + (rec.error.message || rec.error)); + metrics.total = Math.round(now() - metrics.t0); + metrics.avgSynthMs = metrics.synthMs.length ? Math.round(metrics.synthMs.reduce((a, b) => a + b, 0) / metrics.synthMs.length) : null; + emit("metrics", metrics); + const text = rec.full.trim(); + if (text && !genAbort.signal.aborted) { history.push({ role: "user", content: userText }, { role: "assistant", content: text }); if (history.length > 13) history.splice(1, 2); turnCache.set(ck, { text, audio: rec.clauses.map((c) => ({ text: c.text, pcm: c.pcm, sr: c.sr })) }); } + thinking = false; speaking = false; emit("state", "idle"); + return text; + } + abortSpec(); // stale/wrong guess (or none) → discard so it can't leak audio or hog the GPU below + + genAbort = new AbortController(); + // INSTANT ACK: play a pre-synthesized opener NOW (O(1) — no synth) so first-audio is ~immediate while the + // brain prefills + generates the real reply beneath it. The opener leads the utterance; real clauses stream + // after it on the same gapless cursor. Skipped if openers weren't warmed (fail-soft → first real clause). + if (cfg.instantAck) { const op = pickOpener(userText); const c = clauseCache.get(keyOf(op)); if (c) { const at = playPCM(c.pcm, c.sr); revealAt(op, at); metrics.ackMs = Math.round(now() - metrics.t0); if (metrics.firstAudio == null) metrics.firstAudio = metrics.ackMs; emit("metrics", metrics); } } + const h = history.concat([{ role: "user", content: userText }]); + let full = "", pending = "", spoken = []; + // queue behind any settling speculation (abortSpec above already signaled it) so the two never share the GPU KV. + await exclusiveGen(async () => { + try { + for await (const delta of brain.generate(h, { signal: genAbort.signal, maxTokens: cfg.maxTokens, onWarm: (w) => { metrics.warm = w; } })) { + if (genAbort.signal.aborted) break; + if (metrics.firstToken == null) { metrics.firstToken = Math.round(now() - metrics.t0); emit("metrics", metrics); } + full += delta; pending += delta; metrics.tokens++; + emit("reply", full); + const [clauses, rest] = cutClauses(pending); + pending = rest; + for (const c of clauses) { if (c) { const a = await speakClause(c); spoken.push({ text: c, pcm: a.pcm, sr: a.sr }); } } + } + const tail = pending.trim(); + if (tail && !genAbort.signal.aborted) { const a = await speakClause(tail); spoken.push({ text: tail, pcm: a.pcm, sr: a.sr }); } + } catch (e) { emit("warn", "brain: " + (e.message || e)); } + }); + + metrics.total = Math.round(now() - metrics.t0); + metrics.tokPerSec = metrics.firstToken != null ? Math.round((metrics.tokens / Math.max(1, metrics.total - metrics.firstToken)) * 1000) : 0; + metrics.avgSynthMs = metrics.synthMs.length ? Math.round(metrics.synthMs.reduce((a, b) => a + b, 0) / metrics.synthMs.length) : null; + emit("metrics", metrics); + const text = full.trim(); + if (text) { history.push({ role: "user", content: userText }, { role: "assistant", content: text }); if (history.length > 13) history.splice(1, 2); turnCache.set(ck, { text, audio: spoken }); } + thinking = false; speaking = false; emit("state", "idle"); + return text; + } + + // ── the live listen loop (FastRTC-style "ReplyOnPause"): capture OFF the main thread (AudioWorklet), a SINGLE + // serialized VAD loop accumulates 16k audio while you speak and fires on a clean PAUSE, gated on the AUDIO + // CLOCK so Q never transcribes its own voice. Robust where the old async ScriptProcessor callback raced. ── + async function startMic() { + micStream = await navigator.mediaDevices.getUserMedia({ audio: { channelCount: 1, echoCancellation: true, noiseSuppression: true, autoGainControl: true } }); + ensureAudio(); + srcRate = audioCtx.sampleRate; + const src = audioCtx.createMediaStreamSource(micStream); + micAnalyser = audioCtx.createAnalyser(); micAnalyser.fftSize = 256; src.connect(micAnalyser); // orb pulse while listening + + // CAPTURE OFF THE MAIN THREAD: an AudioWorklet posts raw mic frames to a queue, so capture is NEVER starved by + // the main-thread WebGPU/TTS compute (the old ScriptProcessor bug). All VAD/ASR runs in the serialized loop below. + const frames = []; + try { + const code = "class C extends AudioWorkletProcessor{process(i){const c=i[0]&&i[0][0];if(c)this.port.postMessage(c.slice(0));return true;}}registerProcessor('holo-cap',C);"; + await audioCtx.audioWorklet.addModule(URL.createObjectURL(new Blob([code], { type: "text/javascript" }))); + micNode = new AudioWorkletNode(audioCtx, "holo-cap"); + src.connect(micNode); + micNode.port.onmessage = (e) => { if (running) frames.push(e.data); }; + } catch (e) { + const node = audioCtx.createScriptProcessor(2048, 1, 1); // fallback: SYNC capture only (no processing here) + node.onaudioprocess = (ev) => { if (running) frames.push(ev.inputBuffer.getChannelData(0).slice(0)); }; + src.connect(node); node.connect(audioCtx.destination); micNode = node; + } + + const FRAME = 512, MAX_SIL_MS = cfg.holdSilenceMs || cfg.pauseMs || 700, MIN_SPEECH_MS = 250; + let win = new Float32Array(0), utter = [], talking = false, silence = 0, uttStart = 0, bargeMs = 0; + let lastPartialAt = 0, partialBusy = false, lastPartial = "", lastComplete = 0; // lastComplete = P(turn done) of newest partial + if (vad) try { vad.reset(); } catch (e) {} + + (async function loop() { + while (running) { + if (!frames.length) { await sleep(24); continue; } + let n = 0; for (const f of frames) n += f.length; + const buf = new Float32Array(n); let o = 0; for (const f of frames) { buf.set(f, o); o += f.length; } frames.length = 0; + const pcm16 = resampleTo16k(buf, srcRate); + const merged = new Float32Array(win.length + pcm16.length); merged.set(win); merged.set(pcm16, win.length); + let off = 0, voiced = 0, nF = 0; + for (; off + FRAME <= merged.length; off += FRAME, nF++) { let p = 0.6; if (vad) { try { p = await vad.speechProb(merged.subarray(off, off + FRAME)); } catch (e) {} } if (p >= cfg.vadThreshold) voiced++; } + win = merged.slice(off); + const ms = (nF * FRAME / 16000) * 1000, spoke = voiced > 0; + + // Q BUSY (thinking, or its audio still playing)? → only listen for a BARGE-IN; never accumulate a turn. + const qBusy = speaking || (audioCtx.currentTime < (_speakUntil - 0.08)); + if (qBusy) { + if (spoke) { bargeMs += ms; if (bargeMs > 320) { bargeMs = 0; bargeIn(); win = new Float32Array(0); utter = []; talking = false; silence = 0; if (vad) try { vad.reset(); } catch (e) {} } } + else bargeMs = Math.max(0, bargeMs - ms); + continue; + } + bargeMs = 0; + + // ReplyOnPause: accumulate while speaking; on a clean pause (once you've really spoken), end the turn. + if (spoke) { if (!talking) { talking = true; uttStart = now(); emit("state", "listening"); } silence = 0; utter.push(pcm16); } + else if (talking) { + silence += ms; utter.push(pcm16); + // SEMANTIC ENDPOINT: snap early (~doneSilenceMs) when the newest partial reads like a COMPLETE thought, + // hold longer (~holdSilenceMs) when it trails on a connective — replaces the fixed silence floor so a + // finished question ends fast without clipping someone who's mid-sentence. (lastComplete from the partial.) + const silTarget = lastComplete >= cfg.turnThreshold ? (cfg.doneSilenceMs || 140) : MAX_SIL_MS; + if (silence >= silTarget && (now() - uttStart) > MIN_SPEECH_MS) { + const audio = flatten(utter); + utter = []; talking = false; silence = 0; lastPartial = ""; lastComplete = 0; win = new Float32Array(0); if (vad) try { vad.reset(); } catch (e) {} + await endpoint(audio); + } + } + + // STREAMING PARTIALS: transcribe the GROWING utterance live (background, ONE at a time via partialBusy) so + // your words appear on screen as you speak — real-time feedback + visible accuracy, like the reference demos. + // Fire-and-forget so it never blocks the VAD loop; the final turn still gets its own clean transcription pass. + if (talking && !partialBusy && utter.length && (now() - lastPartialAt) > 420) { + lastPartialAt = now(); partialBusy = true; + const snap = flatten(utter); + asr.transcribe(snap, { language: "en" }).then((r) => { + const t = (r && r.text || "").trim(); + if (t && talking) { + lastPartial = t; emit("partial", t); + lastComplete = heuristicComplete(t); // drives the SEMANTIC endpoint above (sync, no GPU turn-model contention) + // SPECULATE once the partial reads confident + complete-ish: generate the reply NOW, buffered, so a + // matching endpoint replays it instantly. Gated so it can't fire mid-connective or while Q is speaking. + if (lastComplete >= cfg.specThreshold) startSpeculation(t); + } + }).catch(() => {}).finally(() => { partialBusy = false; }); + } + } + })().catch((e) => emit("warn", "listen loop: " + (e.message || e))); + + // ENDPOINT: transcribe the utterance (one clean pass) and respond. Rejects empties/noise so silence never talks. + async function endpoint(audio) { + emit("state", "thinking"); + let text = ""; try { const r = await asr.transcribe(audio, { language: "en" }); text = (r && r.text || "").trim(); } catch (e) {} + if (!text || text.replace(/[^a-z0-9]/gi, "").length < 2) { emit("state", "listening"); return; } // noise/empty → keep listening + emit("final", text); + await respond(text); + emit("state", "listening"); + } + + running = true; emit("state", "idle"); + } + + function bargeIn() { + if (genAbort) try { genAbort.abort(); } catch (e) {} + abortSpec(); // a guess in flight is now stale — kill it so it can't commit + stopAudio(); speaking = false; thinking = false; + emit("bargein", true); emit("state", "listening"); + } + + function flatten(chunks) { let n = 0; for (const c of chunks) n += c.length; const out = new Float32Array(n); let o = 0; for (const c of chunks) { out.set(c, o); o += c.length; } return out; } + + // public API + async function start(onProgress) { if (!brain) await load(onProgress); await startMic(); running = true; return info(); } + function stop() { running = false; abortSpec(); try { micNode && micNode.disconnect(); micStream && micStream.getTracks().forEach((t) => t.stop()); } catch (e) {} stopAudio(); emit("state", "idle"); } + // text-drive (no mic): type → brain → clause-streamed voice. Same respond path; great for verifying the core. + async function say(text) { if (!brain) await load(); if (!audioCtx) { audioCtx = new (window.AudioContext || window.webkitAudioContext)(); } return respond(String(text || "").trim()); } + + const api = { load, start, stop, say, bargeIn, on, info, getLevel, get metrics() { return metrics; }, get history() { return history; }, + get active() { return running; }, get phase() { return thinking ? "thinking" : speaking ? "speaking" : running ? "listening" : "idle"; }, + // arm() MUST be called synchronously inside the user's tap/click handler: it creates + resumes the AudioContext + // and plays a 1-sample silent blip to fully UNLOCK audio within the gesture window. Without this the context is + // only created deep in load() (26s later, gesture expired) → it stays suspended → audio is scheduled but silent. + arm() { ensureAudio(); try { const b = audioCtx.createBuffer(1, 1, 22050); const s = audioCtx.createBufferSource(); s.buffer = b; s.connect(outGain || audioCtx.destination); s.start(0); } catch (e) {} try { audioCtx.resume(); } catch (e) {} return audioCtx.state; }, + get audioState() { return audioCtx && audioCtx.state; }, cfg }; + return api; +} + +export default createQLive; diff --git a/apps/q/qvac-2bit.mjs b/apps/q/qvac-2bit.mjs new file mode 100644 index 0000000000000000000000000000000000000000..d00c164140837813c1754246fc1da084c9fa4859 --- /dev/null +++ b/apps/q/qvac-2bit.mjs @@ -0,0 +1,268 @@ +// qvac-2bit.mjs — NATIVE 2-bit WebGPU matmul: read 2-bit weights DIRECTLY on the GPU (no decode to Q8), +// undo incoherence with a runtime Hadamard, accumulate in f32. This is the on-GPU realization of the E₈/ +// QuIP# 2-bit win: the per-token weight sweep drops ~4× vs Q8, so a 7B model (≈1.75 GB at 2-bit) fits +// resident in consumer VRAM and runs at VRAM bandwidth instead of paging from disk. +// +// Math (one-sided incoherence, the efficient form): let R = FWHT∘diag(sign) — orthogonal AND self-inverse +// (FWHT normalized 1/√K, sign ∈ ±1). Store Ŵ′ = quantize₂(R·Wₙ) per row (R isotropizes the row so a 2-bit +// grid quantizes it well). At inference rotate the INPUT once, x′ = R·x, then yₙ = Σ Ŵ′[n,k]·x′[k] ≈ +// (R·Wₙ)·(R·x) = Wₙ·x — the rotation cancels for free, no output Hadamard. Cost: one length-K Hadamard +// per matmul (O(K log K), negligible beside the O(N·K) matmul). Pure WebGPU; the codebook is a uniform +// 4-level grid {−3,−1,1,3}·scale with a per-32 block scale (same scale layout the engine's Q8 path uses). + +const FWHT_WG = 256; // single-workgroup input rotation (K ≤ 2048) +const MM_WG = 64; // one workgroup per output row, 64-thread reduce + +// ── CPU: deterministic ±1 signs (re-derivable from K, matches e8-quant's xorshift) ── +export function signsFor(K) { + const s = new Float32Array(K); let x = (0x9e3779b9 ^ K) >>> 0; + for (let i = 0; i < K; i++) { x ^= x << 13; x ^= x >>> 17; x ^= x << 5; x >>>= 0; s[i] = (x & 1) ? 1 : -1; } + return s; +} +function fwht(a) { // in place, normalized (self-inverse) + const n = a.length; + for (let len = 1; len < n; len <<= 1) for (let i = 0; i < n; i += len << 1) for (let j = i; j < i + len; j++) { const u = a[j], v = a[j + len]; a[j] = u + v; a[j + len] = u - v; } + const s = 1 / Math.sqrt(n); for (let i = 0; i < n; i++) a[i] *= s; +} + +// ── CPU: pack a weight matrix W [N,K] (row-major) to incoherent 2-bit + per-32 scales ── +// Returns { qw:Uint32Array(N*K/16), sc:Float32Array(N*K/32), sign:Float32Array(K) }. +// incoherent=false skips the rotation (naive 2-bit) — for the quality contrast only. +export function pack2bit(W, N, K, { incoherent = true } = {}) { + const nblk = K / 32, qw = new Uint32Array((N * K) / 16), sc = new Float32Array(N * nblk), sign = signsFor(K); + const row = new Float64Array(K); + for (let n = 0; n < N; n++) { + for (let k = 0; k < K; k++) row[k] = incoherent ? W[n * K + k] * sign[k] : W[n * K + k]; + if (incoherent) fwht(row); // row ← R·Wₙ + // per-block scale: MSE-optimal step for a 4-level uniform grid {−3,−1,1,3}·s on ~Gaussian data is + // s ≈ 0.5·σ (grid spans ±1.5σ). Outliers beyond ±1.5σ clip — which is exactly what incoherence + // removes (the Hadamard Gaussianizes the row), so naive-2-bit clips heavy tails and incoherent does not. + for (let b = 0; b < nblk; b++) { let ss = 0; for (let i = 0; i < 32; i++) { const a = row[b * 32 + i]; ss += a * a; } sc[n * nblk + b] = (0.5 * Math.sqrt(ss / 32)) || 1e-12; } + for (let k = 0; k < K; k++) { + const t = row[k] / sc[n * nblk + (k >> 5)]; + let q = Math.round((t + 3) / 2); if (q < 0) q = 0; else if (q > 3) q = 3; // grid {−3,−1,1,3} + const idx = n * K + k; qw[idx >> 4] |= q << ((idx & 15) * 2); + } + } + return { qw, sc, sign }; +} +// reconstruct (CPU reference for the stored 2-bit weights, in the ORIGINAL basis): undo R on each row +export function unpack2bit(qw, sc, sign, N, K, { incoherent = true } = {}) { + const nblk = K / 32, W = new Float32Array(N * K), row = new Float64Array(K); + for (let n = 0; n < N; n++) { + for (let k = 0; k < K; k++) { const idx = n * K + k; const q = (qw[idx >> 4] >>> ((idx & 15) * 2)) & 3; row[k] = (q * 2 - 3) * sc[n * nblk + (k >> 5)]; } + if (incoherent) { fwht(row); for (let k = 0; k < K; k++) row[k] *= sign[k]; } // R is self-inverse + for (let k = 0; k < K; k++) W[n * K + k] = row[k]; + } + return W; +} + +// f32 → f16 bits (Uint16). Scales are small positives; round-toward-zero of the mantissa is fine. +export function f32ToF16(val) { + _f32[0] = val; const x = _u32[0]; + const sign = (x >>> 16) & 0x8000; let exp = ((x >>> 23) & 0xff) - 112; const mant = x & 0x7fffff; + if (exp <= 0) { if (exp < -10) return sign; const m = (mant | 0x800000) >> (1 - exp); return sign | (m >> 13); } + if (exp >= 31) return sign | 0x7c00; + return sign | (exp << 10) | (mant >> 13); +} +const _f32 = new Float32Array(1), _u32 = new Uint32Array(_f32.buffer); + +// CODEBOOK-AWARE LDLQ to the 2-bit SCALAR grid {−3,−1,1,3}·sc — the engine's native 2-bit codebook, with +// NO incoherence (so no power-of-2 padding, no runtime Hadamard). Rounds input columns high→low feeding +// each future column's error back through L (the LDL factor of the input Hessian); L=null ⇒ plain scalar +// 2-bit (the fallback when no calibration Hessian of the right dim exists, e.g. the FFN down-proj). Returns +// the packed 2-bit indices (16 weights/u32, no padding — K must be a multiple of 16). sc = per-32 scales. +// `band` caps the feedback to the nearest `band` future columns (0 = full). The LDL factor's off-diagonal +// mass concentrates near the diagonal, so a band recovers most of the gain at O(N·K·band) instead of +// O(N·K²) — the difference between a feasible and an infeasible 7B compile in single-thread JS. +export function ldlqRound2bit(W, N, K, L, sc, band = 0, chunk = 4096) { + const qw = new Uint32Array((N * K) / 16), nb = K / 32, CH = Math.min(chunk, N), E = new Float32Array(CH * K); + for (let r0 = 0; r0 < N; r0 += CH) { // rows are independent in LDLQ → chunk them (E is CH·K, not N·K) + const rN = Math.min(CH, N - r0); E.fill(0, 0, rN * K); + for (let k = K - 1; k >= 0; k--) for (let ii = 0; ii < rN; ii++) { + const i = r0 + ii; + let corr = W[i * K + k]; if (L) { const jm = band ? Math.min(K, k + 1 + band) : K; for (let j = k + 1; j < jm; j++) corr += E[ii * K + j] * L[j * K + k]; } + const s = sc[i * nb + (k >> 5)]; let q = Math.round(corr / s / 2 + 1.5); if (q < 0) q = 0; else if (q > 3) q = 3; + E[ii * K + k] = W[i * K + k] - (q * 2 - 3) * s; + const idx = i * K + k; qw[idx >> 4] |= q << ((idx & 15) * 2); + } + } + return qw; +} + +export const nextPow2 = (n) => { let p = 1; while (p < n) p <<= 1; return p; }; +// re-quantize an engine Q8 tensor (int8 quants + per-32 f32 scales) → incoherent 2-bit, padding the input +// dim K to Kp = next power of 2 (the FWHT needs a pow2 length; padded weights/inputs are zeros ⇒ exact). +// Returns { q: packed 2-bit bytes [N*Kp/4], s: f32 scales [N*Kp/32], Kp }. The runtime rotates the input by +// the SAME R_Kp (signsFor(Kp)+FWHT), so Ŵ′·x′ = (R·W)(R·x) = W·x. +export function requant2bit(q8, s, N, K) { + const Kp = nextPow2(K), nb = Kp / 32, sb = K / 32; + const q = new Int8Array(q8.buffer, q8.byteOffset, N * K); + const sign = signsFor(Kp), row = new Float64Array(Kp); + const qw = new Uint32Array((N * Kp) / 16), sc = new Float32Array(N * nb); + for (let n = 0; n < N; n++) { + for (let k = 0; k < Kp; k++) row[k] = (k < K ? q[n * K + k] * s[n * sb + (k >> 5)] : 0) * sign[k]; + fwht(row); + for (let b = 0; b < nb; b++) { let ss = 0; for (let i = 0; i < 32; i++) { const a = row[b * 32 + i]; ss += a * a; } sc[n * nb + b] = (0.5 * Math.sqrt(ss / 32)) || 1e-12; } // MSE-optimal step for the {−3,−1,1,3} grid on Gaussianised (incoherent) weights + for (let k = 0; k < Kp; k++) { const t = row[k] / sc[n * nb + (k >> 5)]; let qq = Math.round((t + 3) / 2); if (qq < 0) qq = 0; else if (qq > 3) qq = 3; const idx = n * Kp + k; qw[idx >> 4] |= qq << ((idx & 15) * 2); } + } + return { q: new Uint8Array(qw.buffer), s: sc, Kp }; +} + +// ── WGSL: input rotation x′ = FWHT(sign ⊙ x), single workgroup, shared memory ── +const FWHT_WGSL = ` +@group(0) @binding(0) var x: array; +@group(0) @binding(1) var sgn: array; +@group(0) @binding(2) var xr: array; +@group(0) @binding(3) var P: vec4; // K, _, _, _ +var sh: array; // 16 KB = the WebGPU min workgroup-storage limit; K ≤ 4096 (covers d up to 7B-class) +@compute @workgroup_size(${FWHT_WG}) +fn main(@builtin(local_invocation_id) lid: vec3) { + let K = P.x; let t = lid.x; + for (var i = t; i < K; i += ${FWHT_WG}u) { sh[i] = x[i] * sgn[i]; } + workgroupBarrier(); + var len = 1u; + loop { + if (len >= K) { break; } + let half = K >> 1u; + for (var i = t; i < half; i += ${FWHT_WG}u) { + let blk = i / len; let j = i % len; + let a = blk * (len << 1u) + j; let b = a + len; + let u = sh[a]; let v = sh[b]; sh[a] = u + v; sh[b] = u - v; + } + workgroupBarrier(); + len = len << 1u; + } + let nrm = 1.0 / sqrt(f32(K)); + for (var i = t; i < K; i += ${FWHT_WG}u) { xr[i] = sh[i] * nrm; } +}`; + +// ── WGSL: 2-bit GEMV — WORD-ORIENTED: each thread loads one u32 (16 weights), unpacks in registers, +// hoists the per-32 block scale (16 weights at a 16-aligned base never cross a 32 boundary → one scale +// read per word). Threads stride by workgroup over words → coalesced loads. f32 accumulate. ── +const MM2_WGSL = ` +@group(0) @binding(0) var qw: array; +@group(0) @binding(1) var sc: array; +@group(0) @binding(2) var x: array; +@group(0) @binding(3) var o: array; +@group(0) @binding(4) var P: vec4; // N, K, nblk, _ +var red: array; +@compute @workgroup_size(${MM_WG}) +fn main(@builtin(workgroup_id) wid: vec3, @builtin(local_invocation_id) lid: vec3) { + let n = wid.x; let K = P.y; let words = K >> 4u; let rowW = n * words; let rowS = n * P.z; + var acc = 0.0; var w = lid.x; + loop { + if (w >= words) { break; } + let packed = qw[rowW + w]; + let kb = w << 4u; let s = sc[rowS + (kb >> 5u)]; + for (var j = 0u; j < 16u; j = j + 1u) { acc = acc + x[kb + j] * f32(i32((packed >> (j * 2u)) & 3u) * 2 - 3) * s; } + w = w + ${MM_WG}u; + } + red[lid.x] = acc; workgroupBarrier(); + var r = ${MM_WG >> 1}u; + loop { if (r == 0u) { break; } if (lid.x < r) { red[lid.x] = red[lid.x] + red[lid.x + r]; } workgroupBarrier(); r = r >> 1u; } + if (lid.x == 0u) { o[n] = red[0]; } +}`; + +// ── WGSL: Q8 GEMV (the engine's current format) — WORD-ORIENTED too, so the comparison is purely the +// bytes-read difference, not kernel quality. Each thread loads one u32 (4 int8), unpacks 4. ── +const MM8_WGSL = ` +@group(0) @binding(0) var qw: array; +@group(0) @binding(1) var sc: array; +@group(0) @binding(2) var x: array; +@group(0) @binding(3) var o: array; +@group(0) @binding(4) var P: vec4; +var red: array; +@compute @workgroup_size(${MM_WG}) +fn main(@builtin(workgroup_id) wid: vec3, @builtin(local_invocation_id) lid: vec3) { + let n = wid.x; let K = P.y; let words = K >> 2u; let rowW = n * words; let rowS = n * P.z; + var acc = 0.0; var w = lid.x; + loop { + if (w >= words) { break; } + let packed = qw[rowW + w]; + let kb = w << 2u; let s = sc[rowS + (kb >> 5u)]; + for (var j = 0u; j < 4u; j = j + 1u) { let b = (packed >> (j * 8u)) & 0xffu; acc = acc + x[kb + j] * f32(i32(b << 24u) >> 24u) * s; } + w = w + ${MM_WG}u; + } + red[lid.x] = acc; workgroupBarrier(); + var r = ${MM_WG >> 1}u; + loop { if (r == 0u) { break; } if (lid.x < r) { red[lid.x] = red[lid.x] + red[lid.x + r]; } workgroupBarrier(); r = r >> 1u; } + if (lid.x == 0u) { o[n] = red[0]; } +}`; + +// ── GPU helpers ── +const U = (typeof GPUBufferUsage !== "undefined") ? GPUBufferUsage : {}; +function pipe(dev, code) { const m = dev.createShaderModule({ code }); return dev.createComputePipeline({ layout: "auto", compute: { module: m, entryPoint: "main" } }); } +function sbuf(dev, src) { const b = dev.createBuffer({ size: Math.max(16, src.byteLength), usage: U.STORAGE | U.COPY_DST | U.COPY_SRC }); dev.queue.writeBuffer(b, 0, src); return b; } +function obuf(dev, bytes) { return dev.createBuffer({ size: Math.max(16, bytes), usage: U.STORAGE | U.COPY_SRC }); } +function ubuf(dev, arr) { const b = dev.createBuffer({ size: 16, usage: U.UNIFORM | U.COPY_DST }); dev.queue.writeBuffer(b, 0, arr); return b; } +async function readf32(dev, buf, n) { const st = dev.createBuffer({ size: n * 4, usage: U.MAP_READ | U.COPY_DST }); const e = dev.createCommandEncoder(); e.copyBufferToBuffer(buf, 0, st, 0, n * 4); dev.queue.submit([e.finish()]); await st.mapAsync(GPUMapMode.READ); const out = new Float32Array(st.getMappedRange().slice(0)); st.unmap(); st.destroy(); return out; } + +// ── the bench: correctness (2-bit incoherent vs f32 ref vs naive-2-bit vs Q8) + perf + memory ── +export async function runBench(dev, { N = 2048, K = 2048, iters = 200 } = {}) { + const nblk = K / 32; + // random Gaussian weights + input (a realistic single layer matmul) + let s = 1234567; const rnd = () => (s = (s * 1664525 + 1013904223) >>> 0) / 4294967296; + const gauss = () => { const u = Math.max(1e-12, rnd()); return Math.sqrt(-2 * Math.log(u)) * Math.cos(2 * Math.PI * rnd()); }; + // heavy-tailed weights like real LLM layers (kurtosis ≫ 3): a Gaussian bulk plus sparse large spikes — + // the outliers that wreck naive low-bit quantization and that incoherence (the Hadamard) spreads out. + const W = new Float32Array(N * K); for (let i = 0; i < N * K; i++) { let w = gauss() * 0.05; if (rnd() < 0.02) w *= 6; W[i] = w; } + const x = new Float32Array(K); for (let i = 0; i < K; i++) x[i] = gauss(); + // f32 reference y = W·x + const yref = new Float32Array(N); for (let n = 0; n < N; n++) { let a = 0; for (let k = 0; k < K; k++) a += W[n * K + k] * x[k]; yref[n] = a; } + const relErr = (y) => { let e = 0, r = 0; for (let n = 0; n < N; n++) { const d = y[n] - yref[n]; e += d * d; r += yref[n] * yref[n]; } return Math.sqrt(e / r); }; + + // pack incoherent 2-bit + naive 2-bit + Q8 (per-32 scale, the engine format) + const inc = pack2bit(W, N, K, { incoherent: true }); + const nai = pack2bit(W, N, K, { incoherent: false }); + const q8 = new Int8Array(N * K), q8s = new Float32Array(N * nblk); + for (let n = 0; n < N; n++) for (let b = 0; b < nblk; b++) { let mx = 0; for (let i = 0; i < 32; i++) { const a = Math.abs(W[n * K + b * 32 + i]); if (a > mx) mx = a; } const sca = (mx / 127) || 1e-12; q8s[n * nblk + b] = sca; for (let i = 0; i < 32; i++) { let q = Math.round(W[n * K + b * 32 + i] / sca); if (q > 127) q = 127; else if (q < -127) q = -127; q8[n * K + b * 32 + i] = q; } } + + // GPU pipelines + const pF = pipe(dev, FWHT_WGSL), p2 = pipe(dev, MM2_WGSL), p8 = pipe(dev, MM8_WGSL); + // buffers + const xB = sbuf(dev, x), xrB = obuf(dev, K * 4), sgnB = sbuf(dev, inc.sign), Pf = ubuf(dev, new Uint32Array([K, 0, 0, 0])); + const qwB = sbuf(dev, inc.qw), scB = sbuf(dev, inc.sc), oB = obuf(dev, N * 4), P2 = ubuf(dev, new Uint32Array([N, K, nblk, 0])); + const naiqwB = sbuf(dev, nai.qw), naiscB = sbuf(dev, nai.sc), oNB = obuf(dev, N * 4); + const q8B = sbuf(dev, new Uint8Array(q8.buffer)), q8sB = sbuf(dev, q8s), o8B = obuf(dev, N * 4); + + const bgF = (xin) => dev.createBindGroup({ layout: pF.getBindGroupLayout(0), entries: [{ binding: 0, resource: { buffer: xin } }, { binding: 1, resource: { buffer: sgnB } }, { binding: 2, resource: { buffer: xrB } }, { binding: 3, resource: { buffer: Pf } }] }); + const bg2 = (qw, sc, xin, o) => dev.createBindGroup({ layout: p2.getBindGroupLayout(0), entries: [{ binding: 0, resource: { buffer: qw } }, { binding: 1, resource: { buffer: sc } }, { binding: 2, resource: { buffer: xin } }, { binding: 3, resource: { buffer: o } }, { binding: 4, resource: { buffer: P2 } }] }); + const bg8 = dev.createBindGroup({ layout: p8.getBindGroupLayout(0), entries: [{ binding: 0, resource: { buffer: q8B } }, { binding: 1, resource: { buffer: q8sB } }, { binding: 2, resource: { buffer: xB } }, { binding: 3, resource: { buffer: o8B } }, { binding: 4, resource: { buffer: P2 } }] }); + + // ── correctness ── + const doInc = K <= 4096; // single-workgroup FWHT covers K ≤ 4096 (16 KB shared) + let yInc = null, gpuCpu = null; + if (doInc) { + // incoherent path: GPU rotate x→x′, then 2-bit matmul with x′ + { const e = dev.createCommandEncoder(); const p = e.beginComputePass(); p.setPipeline(pF); p.setBindGroup(0, bgF(xB)); p.dispatchWorkgroups(1); p.setPipeline(p2); p.setBindGroup(0, bg2(qwB, scB, xrB, oB)); p.dispatchWorkgroups(N); p.end(); dev.queue.submit([e.finish()]); } + yInc = await readf32(dev, oB, N); + // CPU re-derivation of the SAME stored 2-bit weights — independent check the GPU kernel agrees + const Wrec = unpack2bit(inc.qw, inc.sc, inc.sign, N, K, { incoherent: true }); + const yCpu = new Float32Array(N); for (let n = 0; n < N; n++) { let a = 0; for (let k = 0; k < K; k++) a += Wrec[n * K + k] * x[k]; yCpu[n] = a; } + let e2 = 0, rr = 0; for (let n = 0; n < N; n++) { const d = yInc[n] - yCpu[n]; e2 += d * d; rr += yCpu[n] * yCpu[n]; } gpuCpu = Math.sqrt(e2 / rr); + } + // naive path (no rotation) + Q8 path + { const e = dev.createCommandEncoder(); const p = e.beginComputePass(); p.setPipeline(p2); p.setBindGroup(0, bg2(naiqwB, naiscB, xB, oNB)); p.dispatchWorkgroups(N); p.end(); dev.queue.submit([e.finish()]); } + const yNai = await readf32(dev, oNB, N); + { const e = dev.createCommandEncoder(); const p = e.beginComputePass(); p.setPipeline(p8); p.setBindGroup(0, bg8); p.dispatchWorkgroups(N); p.end(); dev.queue.submit([e.finish()]); } + const yQ8 = await readf32(dev, o8B, N); + + // ── perf — apples-to-apples: time each MATMUL alone (incoherence rotation measured separately) ── + // pre-rotate x once so the 2-bit matmul reads x′ without re-running the FWHT in the timed loop. + if (doInc) { const e = dev.createCommandEncoder(); const p = e.beginComputePass(); p.setPipeline(pF); p.setBindGroup(0, bgF(xB)); p.dispatchWorkgroups(1); p.end(); dev.queue.submit([e.finish()]); } + const time = async (fn) => { fn(); await dev.queue.onSubmittedWorkDone(); const t0 = performance.now(); for (let i = 0; i < iters; i++) fn(); await dev.queue.onSubmittedWorkDone(); return (performance.now() - t0) / iters; }; + const run2mm = () => { const e = dev.createCommandEncoder(); const p = e.beginComputePass(); p.setPipeline(p2); p.setBindGroup(0, bg2(qwB, scB, doInc ? xrB : xB, oB)); p.dispatchWorkgroups(N); p.end(); dev.queue.submit([e.finish()]); }; + const run8mm = () => { const e = dev.createCommandEncoder(); const p = e.beginComputePass(); p.setPipeline(p8); p.setBindGroup(0, bg8); p.dispatchWorkgroups(N); p.end(); dev.queue.submit([e.finish()]); }; + const runF = () => { const e = dev.createCommandEncoder(); const p = e.beginComputePass(); p.setPipeline(pF); p.setBindGroup(0, bgF(xB)); p.dispatchWorkgroups(1); p.end(); dev.queue.submit([e.finish()]); }; + const ms2 = await time(run2mm), ms8 = await time(run8mm), msF = doInc ? await time(runF) : null; + + const bytes2 = inc.qw.byteLength + inc.sc.byteLength, bytes8 = q8.byteLength + q8s.byteLength; + const gbps = (b, ms) => (b / 1e9) / (ms / 1e3); + return { + N, K, iters, + err: { incoherent2bit: doInc ? relErr(yInc) : null, naive2bit: relErr(yNai), q8: relErr(yQ8), gpu_vs_cpu_2bit: gpuCpu }, + perf: { ms_2bit_mm: +ms2.toFixed(4), ms_q8_mm: +ms8.toFixed(4), ms_fwht: msF == null ? null : +msF.toFixed(4), matmul_speedup: +(ms8 / ms2).toFixed(2), gbps_2bit: +gbps(bytes2, ms2).toFixed(0), gbps_q8: +gbps(bytes8, ms8).toFixed(0) }, + mem: { MB_2bit: +(bytes2 / 1e6).toFixed(2), MB_q8: +(bytes8 / 1e6).toFixed(2), ratio: +(bytes8 / bytes2).toFixed(2), bits_per_weight: +(bytes2 * 8 / (N * K)).toFixed(2) }, + }; +} diff --git a/apps/q/qvac-gpu.js b/apps/q/qvac-gpu.js new file mode 100644 index 0000000000000000000000000000000000000000..0075fcfb30c37be4d4012342cc3d7d1cb7215e03 --- /dev/null +++ b/apps/q/qvac-gpu.js @@ -0,0 +1,2272 @@ +// QVAC WebGPU decode engine — GPU-resident inference for the int8 Llama model. +// +// "type-1" lean path: int8 weights + KV cache live on the GPU; each token is ONE +// command buffer chaining every kernel (intermediates stay in GPU buffers, no +// per-op CPU round-trip), with a single async readback of the logits. The wasm +// hands over the weights via qvac_gpu_export(); JS keeps only the embedding table +// for the host-side lookup. Mirrors the CPU DecodeSession op-for-op so its output +// matches (greedy decode → identical tokens). + +// Per-block GEMV (Q8: int8 1B/weight, or Q4: nibble 2 weights/byte), weights in +// [out,in] layout (K-split reads are contiguous → coalesced), with a scale per +// 32-weight block (GGUF-native precision). One workgroup per output row, 64 +// threads reduce over K. `add` fuses a residual: o = x·dequant(qw,sc) [+ r]. +import { requant2bit, signsFor } from "./qvac-2bit.mjs"; + +const mmKernel = (bits, add, q3f = false) => ` +@group(0) @binding(0) var x: array; +@group(0) @binding(1) var qw: array; +@group(0) @binding(2) var sc: array; +${add + ? "@group(0) @binding(3) var r: array;\n@group(0) @binding(4) var o: array;\n@group(0) @binding(5) var P: vec4;" + : "@group(0) @binding(3) var o: array;\n@group(0) @binding(4) var P: vec4;"} +var red: array; +@compute @workgroup_size(64) +fn main(@builtin(workgroup_id) wg:vec3, @builtin(local_invocation_id) lid:vec3){ + let n=wg.y*65535u+wg.x; let K=P.x; let nblk=P.z; let t=lid.x; + if(n>=P.y){return;} // 2D grid for N>65535 (big vocab) + var acc=0.0; +${bits === 2 + ? ` let words=K>>4u; let rowW=n*words; let rowS=n*nblk; var w=t; // 2-bit: 16 weights/u32 + loop{ if(w>=words){break;} let packed=qw[rowW+w]; let kb=w<<4u; let sca=sc[rowS+(kb>>5u)]; + for(var j=0u;j<16u;j=j+1u){ acc=acc+x[kb+j]*f32(i32((packed>>(j*2u))&3u)*2-3)*sca; } w=w+64u; + }` + : bits === 3 && q3f + ? ` let rowB=n*nblk; var blk=t; // Q3 FIELDS: 10×3-bit per u32 (+ the 3 spare 2-bit stubs = w30/w31); 1 shift+and per weight + loop{ if(blk>=nblk){break;} let bp=(rowB+blk)*3u; let p0=qw[bp]; let p1=qw[bp+1u]; let p2=qw[bp+2u]; let kb=blk<<5u; var bacc=0.0; + for(var j=0u;j<10u;j=j+1u){ bacc=bacc+x[kb+j]*f32(i32((p0>>(j*3u))&7u)-3); } + for(var j=0u;j<10u;j=j+1u){ bacc=bacc+x[kb+10u+j]*f32(i32((p1>>(j*3u))&7u)-3); } + for(var j=0u;j<10u;j=j+1u){ bacc=bacc+x[kb+20u+j]*f32(i32((p2>>(j*3u))&7u)-3); } + let sp=(p0>>30u)|((p1>>30u)<<2u)|((p2>>30u)<<4u); + bacc=bacc+x[kb+30u]*f32(i32(sp&7u)-3)+x[kb+31u]*f32(i32((sp>>3u)&7u)-3); + acc=acc+bacc*sc[rowB+blk]; blk=blk+64u; + }` + : bits === 3 + ? ` let rowB=n*nblk; var blk=t; // Q3: bit-planes — 3 u32 per 32-block, level {−7…7} + loop{ if(blk>=nblk){break;} let bp=(rowB+blk)*3u; let p0=qw[bp]; let p1=qw[bp+1u]; let p2=qw[bp+2u]; let sca=sc[rowB+blk]; let kb=blk<<5u; + for(var j=0u;j<32u;j=j+1u){ let q=((p0>>j)&1u)|(((p1>>j)&1u)<<1u)|(((p2>>j)&1u)<<2u); acc=acc+x[kb+j]*f32(i32(q)-3)*sca; } blk=blk+64u; + }` + : bits === 4 + ? ` let words=K>>3u; let rowW=n*words; let rowS=n*nblk; var w=t; // Q4: 8 nibbles/u32, hoisted block scale (word-oriented) + loop{ if(w>=words){break;} let packed=qw[rowW+w]; let kb=w<<3u; let sca=sc[rowS+(kb>>5u)]; + for(var j=0u;j<8u;j=j+1u){ acc=acc+x[kb+j]*f32(i32((packed>>(j*4u))&0xfu)-8)*sca; } w=w+64u; + }` + : ` var k=t; + loop{ if(k>=K){break;} + let g=n*K+k; + let q=f32(i32(((qw[g/4u]>>((g%4u)*8u))&0xffu)<<24u)>>24u); + acc=acc+x[k]*q*sc[n*nblk + (k>>5u)]; + k=k+64u; + }`} + red[t]=acc; workgroupBarrier(); + var s=32u; loop{ if(s==0u){break;} if(t x: array; +@group(0) @binding(1) var sgn: array; +@group(0) @binding(2) var xr: array; +@group(0) @binding(3) var P: vec4; // K (real), Kp (padded) +@compute @workgroup_size(256) +fn main(@builtin(global_invocation_id) gid: vec3){ let i=gid.x; let K=P.x; let Kp=P.y; if(i>=Kp){return;} var xi=0.0; if(i xr: array; +@group(0) @binding(1) var P: vec4; // Kp, len +@compute @workgroup_size(256) +fn main(@builtin(global_invocation_id) gid: vec3){ let i=gid.x; let Kp=P.x; let len=P.y; if(i>=(Kp>>1u)){return;} let blk=i/len; let j=i%len; let a=blk*(len<<1u)+j; let b=a+len; let u=xr[a]; let v=xr[b]; xr[a]=u+v; xr[b]=u-v; }`; +const FWHT_NORM = ` +@group(0) @binding(0) var xr: array; +@group(0) @binding(1) var P: vec4; // Kp +@compute @workgroup_size(256) +fn main(@builtin(global_invocation_id) gid: vec3){ let i=gid.x; let Kp=P.x; if(i>=Kp){return;} xr[i]=xr[i]*(1.0/sqrt(f32(Kp))); }`; + +const RMS = ` +@group(0) @binding(0) var x: array; +@group(0) @binding(1) var gamma: array; +@group(0) @binding(2) var o: array; +@group(0) @binding(3) var P: vec4; // d +var sh: array; +@compute @workgroup_size(256) +fn main(@builtin(local_invocation_id) lid:vec3){ + let d=P.x; let t=lid.x; + var s=0.0; var i=t; loop{ if(i>=d){break;} s=s+x[i]*x[i]; i=i+256u; } + sh[t]=s; workgroupBarrier(); + var stride=128u; loop{ if(stride==0u){break;} if(t=d){break;} o[j]=x[j]*inv*gamma[j]; j=j+256u; } +}`; + +// Qwen3 QK-Norm: per-head RMSNorm over head_dim (≤128), one workgroup per head. +const QKNORM = ` +@group(0) @binding(0) var x: array; // [nh*hd] in place +@group(0) @binding(1) var w: array; // [hd] +@group(0) @binding(2) var P: vec4; // nh, hd, _, _ +var sh: array; +@compute @workgroup_size(128) +fn main(@builtin(workgroup_id) wg:vec3, @builtin(local_invocation_id) lid:vec3){ + let h=wg.x; let hd=P.y; let t=lid.x; let base=h*hd; + var v=0.0; if(t c: array>; +@group(0) @binding(1) var o: array; +@group(0) @binding(2) var P: vec4; +var wm: array; var wi: array; +@compute @workgroup_size(256) +fn main(@builtin(local_invocation_id) lid:vec3){ + let t=lid.x; let e=c[t]; wm[t]=bitcast(e.x); wi[t]=e.y; workgroupBarrier(); + var s=128u; loop{ if(s==0u){break;} if(twm[t]||(wm[t+s]==wm[t]&&wi[t+s](wm[0]); } +}`; +const ARGMAX2K = ` +@group(0) @binding(0) var c: array>; +@group(0) @binding(1) var o: array; +@group(0) @binding(2) var P: vec4; +var wm: array; var wi: array; +@compute @workgroup_size(256) +fn main(@builtin(local_invocation_id) lid:vec3){ + let t=lid.x; let e=c[t]; wm[t]=bitcast(e.x); wi[t]=e.y; workgroupBarrier(); + var s=128u; loop{ if(s==0u){break;} if(twm[t]||(wm[t+s]==wm[t]&&wi[t+s] ` +@group(0) @binding(0) var q: array; // [k][nh*hd] +@group(0) @binding(1) var kc: array; +@group(0) @binding(2) var vc: array; +@group(0) @binding(3) var o: array; // [k][nh*hd] +@group(0) @binding(4) var P: vec4; // nh, nkv, hd, basePos (wg.y = row; pos = basePos+row) +var sc: array; +var red: array; +const S: u32 = ${kvd / 8 + kvd / 32}u; +const CW: u32 = ${kvd / 8}u; +fn kval(j:u32, c:u32) -> f32 { let w=kc[j*S+(c>>3u)]; return (f32((w>>((c&7u)*4u))&15u)-7.0)*bitcast(kc[j*S+CW+(c>>5u)]); } +fn vval(j:u32, c:u32) -> f32 { let w=vc[j*S+(c>>3u)]; return (f32((w>>((c&7u)*4u))&15u)-7.0)*bitcast(vc[j*S+CW+(c>>5u)]); } +@compute @workgroup_size(64) +fn main(@builtin(workgroup_id) wg:vec3, @builtin(local_invocation_id) lid:vec3){ + let h=wg.x; let nh=P.x; let nkv=P.y; let hd=P.z; let pos=P.w+wg.y; + let group=nh/nkv; let kh=h/group; let rb=wg.y*nh*hd; + let scale=1.0/sqrt(f32(hd)); let qb=rb+h*hd; let kb=kh*hd; let t=lid.x; + var j=t; loop{ if(j>pos){break;} var d=0.0; for(var c=0u;cpos){break;} lm=max(lm,sc[j]); j=j+64u; } + red[t]=lm; workgroupBarrier(); + var s=32u; loop{ if(s==0u){break;} if(tpos){break;} let e=exp(sc[j]-mx); sc[j]=e; ld=ld+e; j=j+64u; } + red[t]=ld; workgroupBarrier(); + s=32u; loop{ if(s==0u){break;} if(t=hd){break;} var acc=0.0; for(var jj=0u;jj<=pos;jj++){ acc=acc+sc[jj]*vval(jj,kb+c); } o[qb+c]=acc/dn; c=c+64u; } +}`; +const ATTNK = (cap) => ` +@group(0) @binding(0) var q: array; // [k][nh*hd] +@group(0) @binding(1) var kc: array; // f32 cache (layer 0) +@group(0) @binding(2) var vc: array; +@group(0) @binding(3) var o: array; +@group(0) @binding(4) var P: vec4; // nh, nkv, hd, basePos (wg.y = row) +var sc: array; +var red: array; +@compute @workgroup_size(64) +fn main(@builtin(workgroup_id) wg:vec3, @builtin(local_invocation_id) lid:vec3){ + let h=wg.x; let nh=P.x; let nkv=P.y; let hd=P.z; let pos=P.w+wg.y; + let group=nh/nkv; let kh=h/group; let kvdim=nkv*hd; let rb=wg.y*nh*hd; + let scale=1.0/sqrt(f32(hd)); let qb=rb+h*hd; let kb=kh*hd; let t=lid.x; + var j=t; loop{ if(j>pos){break;} var d=0.0; for(var c=0u;cpos){break;} lm=max(lm,sc[j]); j=j+64u; } + red[t]=lm; workgroupBarrier(); + var s=32u; loop{ if(s==0u){break;} if(tpos){break;} let e=exp(sc[j]-mx); sc[j]=e; ld=ld+e; j=j+64u; } + red[t]=ld; workgroupBarrier(); + s=32u; loop{ if(s==0u){break;} if(t=hd){break;} var acc=0.0; for(var jj=0u;jj<=pos;jj++){ acc=acc+sc[jj]*vc[jj*kvdim+kb+c]; } o[qb+c]=acc/dn; c=c+64u; } +}`; +const RMSK = ` +@group(0) @binding(0) var x: array; +@group(0) @binding(1) var gamma: array; +@group(0) @binding(2) var o: array; +@group(0) @binding(3) var P: vec4; // d (wg.y = row) +var sh: array; +@compute @workgroup_size(256) +fn main(@builtin(workgroup_id) wg:vec3, @builtin(local_invocation_id) lid:vec3){ + let d=P.x; let t=lid.x; let b=wg.y*d; + var s=0.0; var i=t; loop{ if(i>=d){break;} s=s+x[b+i]*x[b+i]; i=i+256u; } + sh[t]=s; workgroupBarrier(); + var stride=128u; loop{ if(stride==0u){break;} if(t=d){break;} o[b+j]=x[b+j]*inv*gamma[j]; j=j+256u; } +}`; +const ROPEK = (theta) => ` +@group(0) @binding(0) var x: array; // [k][stride] in place +@group(0) @binding(1) var P: vec4; // nh, hd, basePos, stride (wg.y = row) +@compute @workgroup_size(64) +fn main(@builtin(workgroup_id) wg:vec3, @builtin(local_invocation_id) lid:vec3){ + let nh=P.x; let hd=P.y; let pos=f32(P.z+wg.y); let half=hd/2u; + let id=wg.x*64u+lid.x; if(id>=nh*half){return;} + let head=id/half; let i=id%half; let base=wg.y*P.w+head*hd; + let freq=pow(${theta}, -2.0*f32(i)/f32(hd)); + let ang=pos*freq; let c=cos(ang); let s=sin(ang); + let a=x[base+i]; let b=x[base+i+half]; + x[base+i]=a*c-b*s; + x[base+i+half]=b*c+a*s; +}`; +const KVQK = (kvd) => ` +@group(0) @binding(0) var x: array; // [k][kvd] +@group(0) @binding(1) var out: array; +@group(0) @binding(2) var P: vec4; // .w = basePos (wg.y = row) +const S: u32 = ${kvd / 8 + kvd / 32}u; +const CW: u32 = ${kvd / 8}u; +const NG: u32 = ${kvd / 32}u; +@compute @workgroup_size(64) +fn main(@builtin(workgroup_id) wg:vec3, @builtin(local_invocation_id) lid:vec3){ + let base=(P.w+wg.y)*S; let xb=wg.y*${kvd}u; var g=lid.x; + loop{ if(g>=NG){break;} + var mx=0.0; for(var i=0u;i<32u;i++){ let a=abs(x[xb+g*32u+i]); if(a>mx){mx=a;} } + let s=max(mx/7.0, 1e-12); + out[base+CW+g]=bitcast(s); + for(var w=0u;w<4u;w++){ + var word=0u; + for(var i=0u;i<8u;i++){ let qv=clamp(i32(round(x[xb+g*32u+w*8u+i]/s)),-7,7); word=word|(u32(qv+7)<<(i*4u)); } + out[base+g*4u+w]=word; + } + g=g+64u; } +}`; +// batched-x ternary GEMM: weights read once for all KX rows. o[c*N+n]; optional residual r same layout. +const mmT2KK = (add, KX) => ` +@group(0) @binding(0) var x: array>; // [KX][K/4] +@group(0) @binding(1) var qw: array; +${add + ? "@group(0) @binding(2) var r: array;\n@group(0) @binding(3) var o: array;\n@group(0) @binding(4) var P: vec4;" + : "@group(0) @binding(2) var o: array;\n@group(0) @binding(3) var P: vec4;"} +var red: array; +${"" /* dot16 via shared fn */} +fn dot16(word:u32, v:u32) -> f32 { + var s4=vec4(0.0); + var x0=x[v]; s4=s4+x0*(vec4(f32(word&3u),f32((word>>2u)&3u),f32((word>>4u)&3u),f32((word>>6u)&3u))-vec4(1.0)); + x0=x[v+1u]; s4=s4+x0*(vec4(f32((word>>8u)&3u),f32((word>>10u)&3u),f32((word>>12u)&3u),f32((word>>14u)&3u))-vec4(1.0)); + x0=x[v+2u]; s4=s4+x0*(vec4(f32((word>>16u)&3u),f32((word>>18u)&3u),f32((word>>20u)&3u),f32((word>>22u)&3u))-vec4(1.0)); + x0=x[v+3u]; s4=s4+x0*(vec4(f32((word>>24u)&3u),f32((word>>26u)&3u),f32((word>>28u)&3u),f32((word>>30u)&3u))-vec4(1.0)); + return s4.x+s4.y+s4.z+s4.w; +} +@compute @workgroup_size(256) +fn main(@builtin(workgroup_id) wg:vec3, @builtin(local_invocation_id) lid:vec3){ + let K=P.x; let nw=K>>4u; let rr=lid.x/64u; let t=lid.x%64u; + let n0=(wg.y*65535u+wg.x)*4u+rr; let n=min(n0, P.y-1u); + let rowW=n*nw; var acc: array; + for(var c=0u;c<${KX}u;c++){ acc[c]=0.0; } + var w=t; + loop{ if(w>=nw){break;} + let word=qw[rowW+w]; let v=w<<2u; + ${Array.from({ length: 8 }, (_, c) => `if(${c}u<${KX}u){ acc[${c}]=acc[${c}]+dot16(word, ${c}u*(K>>2u)+v); }`).slice(0, KX).join("\n ")} + w=w+64u; } + for(var c=0u;c<${KX}u;c++){ + red[lid.x]=acc[c]; workgroupBarrier(); + var s=32u; loop{ if(s==0u){break;} if(t(P.w)${add ? "+r[c*P.y+n0]" : ""}; } + workgroupBarrier(); + } +}`; +// t2r batched (per-256-block scales) — binding order mirrors mmT2RKernel: x,qw,sc,(r),o,P +const mmT2RKK = (add, KX) => ` +@group(0) @binding(0) var x: array>; // [KX][K/4] +@group(0) @binding(1) var qw: array; +@group(0) @binding(2) var sc: array; +${add + ? "@group(0) @binding(3) var r: array;\n@group(0) @binding(4) var o: array;\n@group(0) @binding(5) var P: vec4;" + : "@group(0) @binding(3) var o: array;\n@group(0) @binding(4) var P: vec4;"} +var red: array; +${t2Dot16} +@compute @workgroup_size(256) +fn main(@builtin(workgroup_id) wg:vec3, @builtin(local_invocation_id) lid:vec3){ + let K=P.x; let nw=K>>4u; let rr=lid.x/64u; let t=lid.x%64u; + let n0=(wg.y*65535u+wg.x)*4u+rr; let n=min(n0, P.y-1u); + let rowW=n*nw; let rowB=n*(K>>8u); var acc: array; + for(var c=0u;c<${KX}u;c++){ acc[c]=0.0; } + var w=t; + loop{ if(w>=nw){break;} + let word=qw[rowW+w]; let v=w<<2u; let s=sc[rowB+(w>>4u)]; + ${Array.from({ length: KX }, (_, c) => `acc[${c}]=acc[${c}]+dot16(word, ${c}u*(K>>2u)+v)*s;`).join("\n ")} + w=w+64u; } + for(var c=0u;c<${KX}u;c++){ + red[lid.x]=acc[c]; workgroupBarrier(); + var s2=32u; loop{ if(s2==0u){break;} if(t ` +@group(0) @binding(0) var x: array; // [KX][K] +@group(0) @binding(1) var qw: array; +@group(0) @binding(2) var sc: array; +@group(0) @binding(3) var o: array; +@group(0) @binding(4) var P: vec4; +var red: array; +@compute @workgroup_size(256) +fn main(@builtin(workgroup_id) wg:vec3, @builtin(local_invocation_id) lid:vec3){ + let K=P.x; let nblk=P.z; let rr=lid.x/64u; let t=lid.x%64u; + let n0=(wg.y*65535u+wg.x)*4u+rr; let n=min(n0, P.y-1u); + let rowB=n*nblk; var acc: array; + for(var c=0u;c<${KX}u;c++){ acc[c]=0.0; } + var blk=t; + loop{ if(blk>=nblk){break;} + let bp=(rowB+blk)*3u; let p0=qw[bp]; let p1=qw[bp+1u]; let p2=qw[bp+2u]; let kb=blk<<5u; let sca=sc[rowB+blk]; + for(var c=0u;c<${KX}u;c++){ let xb=c*K+kb; var bacc=0.0; + for(var j=0u;j<10u;j=j+1u){ bacc=bacc+x[xb+j]*f32(i32((p0>>(j*3u))&7u)-3); } + for(var j=0u;j<10u;j=j+1u){ bacc=bacc+x[xb+10u+j]*f32(i32((p1>>(j*3u))&7u)-3); } + for(var j=0u;j<10u;j=j+1u){ bacc=bacc+x[xb+20u+j]*f32(i32((p2>>(j*3u))&7u)-3); } + let sp=(p0>>30u)|((p1>>30u)<<2u)|((p2>>30u)<<4u); + bacc=bacc+x[xb+30u]*f32(i32(sp&7u)-3)+x[xb+31u]*f32(i32((sp>>3u)&7u)-3); + acc[c]=acc[c]+bacc*sca; + } + blk=blk+64u; + } + for(var c=0u;c<${KX}u;c++){ + red[lid.x]=acc[c]; workgroupBarrier(); + var s=32u; loop{ if(s==0u){break;} if(t x: array; // [m][K] +@group(0) @binding(1) var aq: array; // [m][K/4] int8x4 +@group(0) @binding(2) var asc: array; // [m][K/32] +@group(0) @binding(3) var P: vec4; // K +@compute @workgroup_size(256) +fn main(@builtin(workgroup_id) wg:vec3, @builtin(local_invocation_id) lid:vec3){ + let K=P.x; let col=wg.y; let base=col*K; let nb=K>>5u; let kq=K>>2u; let t=lid.x; + var b=t; + loop{ if(b>=nb){break;} + let bb=base+b*32u; + var mx=0.0; for(var i=0u;i<32u;i++){ mx=max(mx, abs(x[bb+i])); } + let scale=mx/127.0+1e-12; asc[col*nb+b]=scale; + for(var u=0u;u<8u;u++){ var word=0u; + for(var q=0u;q<4u;q++){ let val=clamp(i32(round(x[bb+u*4u+q]/scale)),-127,127); word=word|((u32(val)&0xffu)<<(q*8u)); } + aq[col*kq + b*8u + u]=word; } + b=b+256u; } +}`; +// mmT2DP4A: batched ternary GEMV via dot4I8Packed with per-block activation scales. Accumulate in f32: +// a 2-bit word (16 acts) is HALF a 32-block → block=w>>1; acc += f32(word_int_dot)·asc[block]. +// o[c*N+n] = wscale·Σ_words (blockscale · int_dot) (+ r[c*N+n]). +const mmT2DP4A = (add, KX) => ` +@group(0) @binding(0) var aq: array; // [KX][K/4] int8x4 +@group(0) @binding(1) var qw: array; // [N][K/16] 2-bit +@group(0) @binding(2) var asc: array; // [KX][K/32] +${add + ? "@group(0) @binding(3) var r: array;\n@group(0) @binding(4) var o: array;\n@group(0) @binding(5) var P: vec4;" + : "@group(0) @binding(3) var o: array;\n@group(0) @binding(4) var P: vec4;"} +var red: array; +fn pk(byte:u32) -> u32 { + let v0=byte&3u; let v1=(byte>>2u)&3u; let v2=(byte>>4u)&3u; let v3=(byte>>6u)&3u; + return ((v0-1u)&0xffu)|(((v1-1u)&0xffu)<<8u)|(((v2-1u)&0xffu)<<16u)|(((v3-1u)&0xffu)<<24u); +} +@compute @workgroup_size(256) +fn main(@builtin(workgroup_id) wg:vec3, @builtin(local_invocation_id) lid:vec3){ + let K=P.x; let nw=K>>4u; let kq=K>>2u; let nb=K>>5u; let rr=lid.x/64u; let t=lid.x%64u; + let n0=(wg.y*65535u+wg.x)*4u+rr; let n=min(n0,P.y-1u); let rowW=n*nw; + var acc: array; for(var c=0u;c<${KX}u;c++){ acc[c]=0.0; } + var w=t; + loop{ if(w>=nw){break;} + let word=qw[rowW+w]; let blk=w>>1u; + let wi0=pk(word&0xffu); let wi1=pk((word>>8u)&0xffu); let wi2=pk((word>>16u)&0xffu); let wi3=pk((word>>24u)&0xffu); + ${Array.from({length:KX},(_,c)=>`{ let bz=${c}u*kq + w*4u; let id=dot4I8Packed(wi0,aq[bz])+dot4I8Packed(wi1,aq[bz+1u])+dot4I8Packed(wi2,aq[bz+2u])+dot4I8Packed(wi3,aq[bz+3u]); acc[${c}]=acc[${c}]+f32(id)*asc[${c}u*nb+blk]; }`).join("\n ")} + w=w+64u; } + for(var c=0u;c<${KX}u;c++){ + red[lid.x]=acc[c]; workgroupBarrier(); + var s=32u; loop{ if(s==0u){break;} if(t(P.w)${add?"+r[c*P.y+n0]":""}; } + workgroupBarrier(); + } +}`; + +const ROPE = (theta) => ` +@group(0) @binding(0) var x: array; // [nh*hd] in place +@group(0) @binding(1) var P: vec4; // nh, hd, pos, _ +@compute @workgroup_size(64) +fn main(@builtin(global_invocation_id) g:vec3){ + let nh=P.x; let hd=P.y; let pos=f32(P.z); let half=hd/2u; + let id=g.x; if(id>=nh*half){return;} + let head=id/half; let i=id%half; let base=head*hd; + let freq=pow(${theta}, -2.0*f32(i)/f32(hd)); + let ang=pos*freq; let c=cos(ang); let s=sin(ang); + let a=x[base+i]; let b=x[base+i+half]; + x[base+i]=a*c-b*s; + x[base+i+half]=b*c+a*s; +}`; + +// One workgroup per head; 64 threads cooperate on scores → softmax → weighted V. +// The score tile is sized to the KV allocation (the old fixed 1024 silently broke ctx > 1024); +// workgroup storage caps this at ~4000 positions (cap·4B + reductions ≤ 16 KB). +const ATTN = (cap) => ` +@group(0) @binding(0) var q: array; // [nh*hd] +@group(0) @binding(1) var kc: array; // [cap*kvdim] position-major +@group(0) @binding(2) var vc: array; +@group(0) @binding(3) var o: array; // [nh*hd] +@group(0) @binding(4) var P: vec4; // nh, nkv, hd, pos(attend 0..pos) +var sc: array; // score tile = full KV allocation +var red: array; +@compute @workgroup_size(64) +fn main(@builtin(workgroup_id) wg:vec3, @builtin(local_invocation_id) lid:vec3){ + let h=wg.x; let nh=P.x; let nkv=P.y; let hd=P.z; let pos=P.w; + let group=nh/nkv; let kh=h/group; let kvdim=nkv*hd; + let scale=1.0/sqrt(f32(hd)); let qb=h*hd; let kb=kh*hd; let t=lid.x; + var j=t; loop{ if(j>pos){break;} var d=0.0; for(var c=0u;cpos){break;} lm=max(lm,sc[j]); j=j+64u; } + red[t]=lm; workgroupBarrier(); + var s=32u; loop{ if(s==0u){break;} if(tpos){break;} let e=exp(sc[j]-mx); sc[j]=e; ld=ld+e; j=j+64u; } + red[t]=ld; workgroupBarrier(); + s=32u; loop{ if(s==0u){break;} if(t=hd){break;} var acc=0.0; for(var jj=0u;jj<=pos;jj++){ acc=acc+sc[jj]*vc[jj*kvdim+kb+c]; } o[qb+c]=acc/dn; c=c+64u; } +}`; + +// ── int4 KV cache (E6, measured: ≈0.1 rel-err @4.5 bits, ~6.4× KV memory/traffic) ── +// Layers 1+ store K/V as symmetric int4 (codes nib−7 ∈ [−7,7]) with one f32 scale per 32 +// channels; layer 0 stays f32 (measured pathological at low bits). Per-token record in u32s: +// [codes kv_dim/8][scale bits kv_dim/32]. Same attention flow; dequant inline. +const ATTNQ = (cap, kvd) => ` +@group(0) @binding(0) var q: array; +@group(0) @binding(1) var kc: array; +@group(0) @binding(2) var vc: array; +@group(0) @binding(3) var o: array; +@group(0) @binding(4) var P: vec4; // nh, nkv, hd, pos +var sc: array; +var red: array; +const S: u32 = ${kvd / 8 + kvd / 32}u; +const CW: u32 = ${kvd / 8}u; +fn kval(j:u32, c:u32) -> f32 { let w=kc[j*S+(c>>3u)]; return (f32((w>>((c&7u)*4u))&15u)-7.0)*bitcast(kc[j*S+CW+(c>>5u)]); } +fn vval(j:u32, c:u32) -> f32 { let w=vc[j*S+(c>>3u)]; return (f32((w>>((c&7u)*4u))&15u)-7.0)*bitcast(vc[j*S+CW+(c>>5u)]); } +@compute @workgroup_size(64) +fn main(@builtin(workgroup_id) wg:vec3, @builtin(local_invocation_id) lid:vec3){ + let h=wg.x; let nh=P.x; let nkv=P.y; let hd=P.z; let pos=P.w; + let group=nh/nkv; let kh=h/group; + let scale=1.0/sqrt(f32(hd)); let qb=h*hd; let kb=kh*hd; let t=lid.x; + var j=t; loop{ if(j>pos){break;} var d=0.0; for(var c=0u;cpos){break;} lm=max(lm,sc[j]); j=j+64u; } + red[t]=lm; workgroupBarrier(); + var s=32u; loop{ if(s==0u){break;} if(tpos){break;} let e=exp(sc[j]-mx); sc[j]=e; ld=ld+e; j=j+64u; } + red[t]=ld; workgroupBarrier(); + s=32u; loop{ if(s==0u){break;} if(t=hd){break;} var acc=0.0; for(var jj=0u;jj<=pos;jj++){ acc=acc+sc[jj]*vval(jj,kb+c); } o[qb+c]=acc/dn; c=c+64u; } +}`; + +// quantize+pack ONE token's K or V row into the int4 cache record at position P.w (binds the +// attention uniform — its .w is already the position on every path, step and batched decode) +const KVQ = (kvd) => ` +@group(0) @binding(0) var x: array; +@group(0) @binding(1) var out: array; +@group(0) @binding(2) var P: vec4; // .w = pos +const S: u32 = ${kvd / 8 + kvd / 32}u; +const CW: u32 = ${kvd / 8}u; +const NG: u32 = ${kvd / 32}u; +@compute @workgroup_size(64) +fn main(@builtin(local_invocation_id) lid:vec3){ + let base=P.w*S; var g=lid.x; + loop{ if(g>=NG){break;} + var mx=0.0; for(var i=0u;i<32u;i++){ let a=abs(x[g*32u+i]); if(a>mx){mx=a;} } + let s=max(mx/7.0, 1e-12); + out[base+CW+g]=bitcast(s); + for(var w=0u;w<4u;w++){ + var word=0u; + for(var i=0u;i<8u;i++){ let qv=clamp(i32(round(x[g*32u+w*8u+i]/s)),-7,7); word=word|(u32(qv+7)<<(i*4u)); } + out[base+g*4u+w]=word; + } + g=g+64u; } +}`; + +const SILUMUL = ` +@group(0) @binding(0) var gate: array; +@group(0) @binding(1) var up: array; +@group(0) @binding(2) var o: array; +@group(0) @binding(3) var P: vec4; // ff +@compute @workgroup_size(64) +fn main(@builtin(global_invocation_id) g:vec3){ + let i=g.x; if(i>=P.x){return;} + let v=gate[i]; o[i]=(v/(1.0+exp(-v)))*up[i]; +}`; + +const ADD = ` +@group(0) @binding(0) var a: array; +@group(0) @binding(1) var b: array; +@group(0) @binding(2) var o: array; +@group(0) @binding(3) var P: vec4; // n +@compute @workgroup_size(64) +fn main(@builtin(global_invocation_id) g:vec3){ let i=g.x; if(i>=P.x){return;} o[i]=a[i]+b[i]; }`; + +// MoE accumulate: o += w · x (w = router weight, passed as f32 bits in P.y). Sums +// each active expert's contribution into the residual without a separate add. +const AXPY = ` +@group(0) @binding(0) var o: array; +@group(0) @binding(1) var x: array; +@group(0) @binding(2) var P: vec4; // n, f32bits(w) +@compute @workgroup_size(64) +fn main(@builtin(global_invocation_id) g:vec3){ let i=g.x; if(i>=P.x){return;} o[i]=o[i]+bitcast(P.y)*x[i]; }`; + +// ── BATCHED-EXPERT MoE kernels (G5c): collapse the per-expert dispatch storm into ONE dispatch per +// stage by looping the nUsed chosen experts INSIDE the kernel, indexing each expert's slab via an +// id table. 640 tiny dispatches/token → ~64. The expert slab is ONE resident buffer (all nExp experts +// contiguous: expert e at u32 offset e·(N·K/8) for q, e·(N·K/32) for f32 scales). q4 decode, verbatim. ── +const MOE_GU = ` +@group(0) @binding(0) var x: array; // [K=d] shared input (normed2) +@group(0) @binding(1) var qw: array; // WHOLE gate|up slab (all experts) +@group(0) @binding(2) var sc: array; +@group(0) @binding(3) var o: array; // [nUsed·ff] +@group(0) @binding(4) var P: vec4; // K=d, ff(rows/expert), nblk=d/32, nUsed +@group(0) @binding(5) var idx: array,2>; // chosen expert ids (≤8) +var red: array; +@compute @workgroup_size(64) +fn main(@builtin(workgroup_id) wg:vec3, @builtin(local_invocation_id) lid:vec3){ + let r=wg.y*65535u+wg.x; let K=P.x; let ff=P.y; let nblk=P.z; let t=lid.x; + if(r>=P.w*ff){return;} // row r → expert slot s, local row i + let s=r/ff; let i=r-s*ff; let e=idx[s>>2u][s&3u]; + let qStride=(ff*K)>>3u; let sStride=(ff*K)>>5u; let words=K>>3u; + let rowW=e*qStride+i*words; let rowS=e*sStride+i*nblk; + var acc=0.0; var w=t; + loop{ if(w>=words){break;} let packed=qw[rowW+w]; let kb=w<<3u; let sca=sc[rowS+(kb>>5u)]; + for(var j=0u;j<8u;j=j+1u){ acc=acc+x[kb+j]*f32(i32((packed>>(j*4u))&0xfu)-8)*sca; } w=w+64u; } + red[t]=acc; workgroupBarrier(); + var st=32u; loop{ if(st==0u){break;} if(t hid: array; // [nUsed·ff] (silu(gate)·up per expert) +@group(0) @binding(1) var qw: array; // WHOLE down slab +@group(0) @binding(2) var sc: array; +@group(0) @binding(3) var res: array; // residual [N=d] +@group(0) @binding(4) var o: array; // [N=d] = res + Σ_s w_s·down_s +@group(0) @binding(5) var P: vec4; // K=ff, N=d, nblk=ff/32, nUsed +@group(0) @binding(6) var idx: array,2>; +@group(0) @binding(7) var wts: array,2>; // router weights +var red: array; +@compute @workgroup_size(64) +fn main(@builtin(workgroup_id) wg:vec3, @builtin(local_invocation_id) lid:vec3){ + let n=wg.y*65535u+wg.x; let K=P.x; let N=P.y; let nblk=P.z; let nUsed=P.w; let t=lid.x; + if(n>=N){return;} + let qStride=(K*N)>>3u; let sStride=(K*N)>>5u; let words=K>>3u; + var acc=0.0; + for(var s=0u;s>2u][s&3u]; let wv=wts[s>>2u][s&3u]; let hb=s*K; + let rowW=e*qStride+n*words; let rowS=e*sStride+n*nblk; var ww=t; + loop{ if(ww>=words){break;} let packed=qw[rowW+ww]; let kb=ww<<3u; let sca=sc[rowS+(kb>>5u)]; + for(var j=0u;j<8u;j=j+1u){ acc=acc+wv*hid[hb+kb+j]*f32(i32((packed>>(j*4u))&0xfu)-8)*sca; } ww=ww+64u; } + } + red[t]=acc; workgroupBarrier(); + var st=32u; loop{ if(st==0u){break;} if(t l: array; +@group(0) @binding(1) var ids: array; +@group(0) @binding(2) var P: vec4; // count, f32bits(rp) +@compute @workgroup_size(64) +fn main(@builtin(global_invocation_id) g:vec3){ let i=g.x; if(i>=P.x){return;} let id=ids[i]; let rp=bitcast(P.y); let v=l[id]; if(v>0.0){ l[id]=v/rp; } else { l[id]=v*rp; } }`; +// two-stage argmax; tie-break = smallest index on equal value (matches the JS first-max scan exactly) +const ARGMAX1 = ` +@group(0) @binding(0) var l: array; +@group(0) @binding(1) var o: array>; // (f32bits(max), idx) per workgroup +@group(0) @binding(2) var P: vec4; // vocab +var wm: array; var wi: array; +@compute @workgroup_size(256) +fn main(@builtin(workgroup_id) wg:vec3, @builtin(local_invocation_id) lid:vec3){ + let V=P.x; let t=lid.x; var bm=-3.0e38; var bi=0xffffffffu; var i=wg.x*256u+t; + loop{ if(i>=V){break;} let v=l[i]; if(v>bm||(v==bm&&iwm[t]||(wm[t+s]==wm[t]&&wi[t+s](bitcast(wm[0]),wi[0]); } +}`; +const ARGMAX2 = ` +@group(0) @binding(0) var c: array>; +@group(0) @binding(1) var o: array; +var wm: array; var wi: array; +@compute @workgroup_size(256) +fn main(@builtin(local_invocation_id) lid:vec3){ + let t=lid.x; let e=c[t]; wm[t]=bitcast(e.x); wi[t]=e.y; workgroupBarrier(); + var s=128u; loop{ if(s==0u){break;} if(twm[t]||(wm[t+s]==wm[t]&&wi[t+s] ` +@group(0) @binding(0) var x: array; +@group(0) @binding(1) var qw: array; +@group(0) @binding(2) var sc: array; +@group(0) @binding(3) var lut: array; +${add + ? "@group(0) @binding(4) var r: array;\n@group(0) @binding(5) var o: array;\n@group(0) @binding(6) var P: vec4;" + : "@group(0) @binding(4) var o: array;\n@group(0) @binding(5) var P: vec4;"} +var red: array; +@compute @workgroup_size(64) +fn main(@builtin(workgroup_id) wg:vec3, @builtin(local_invocation_id) lid:vec3){ + let n=wg.y*65535u+wg.x; let K=P.x; let nblk=P.z; let t=lid.x; + if(n>=P.y){return;} + var acc=0.0; + let rowC=n*nblk*2u; let rowS=n*nblk; var blk=t; + loop{ if(blk>=nblk){break;} + let si=rowS+blk; let s2=sc[si>>1u]; let sca=select(unpack2x16float(s2).x, unpack2x16float(s2).y, (si&1u)==1u); + let kb=blk<<5u; var bacc=0.0; + for(var c=0u;c<2u;c=c+1u){ + let w2=qw[rowC+blk*2u+c]; + for(var h2=0u;h2<2u;h2=h2+1u){ + let code=(w2>>(h2*16u))&0xffffu; let shp=(code&0xffu)<<3u; let sgn=code>>8u; + let kk=kb+(c*2u+h2)*8u; + for(var j=0u;j<8u;j=j+1u){ let mag=lut[shp+j]; bacc=bacc+x[kk+j]*select(mag,-mag,((sgn>>j)&1u)==1u); } + } + } + acc=acc+bacc*sca; blk=blk+64u; + } + red[t]=acc; workgroupBarrier(); + var s=32u; loop{ if(s==0u){break;} if(t ` +@group(0) @binding(0) var x: array; +@group(0) @binding(1) var qw: array; +@group(0) @binding(2) var sc: array; +@group(0) @binding(3) var o: array; +@group(0) @binding(4) var P: vec4; +var red: array; +@compute @workgroup_size(256) +fn main(@builtin(workgroup_id) wg:vec3, @builtin(local_invocation_id) lid:vec3){ + let nblk=P.z; let rr=lid.x/${L}u; let t=lid.x%${L}u; + let n0=(wg.y*65535u+wg.x)*${R}u+rr; let n=min(n0, P.y-1u); + let rowB=n*nblk; var acc=0.0; var blk=t; + loop{ if(blk>=nblk){break;} + let bp=(rowB+blk)*3u; let p0=qw[bp]; let p1=qw[bp+1u]; let p2=qw[bp+2u]; let kb=blk<<5u; var bacc=0.0; + for(var j=0u;j<10u;j=j+1u){ bacc=bacc+x[kb+j]*f32(i32((p0>>(j*3u))&7u)-3); } + for(var j=0u;j<10u;j=j+1u){ bacc=bacc+x[kb+10u+j]*f32(i32((p1>>(j*3u))&7u)-3); } + for(var j=0u;j<10u;j=j+1u){ bacc=bacc+x[kb+20u+j]*f32(i32((p2>>(j*3u))&7u)-3); } + let sp=(p0>>30u)|((p1>>30u)<<2u)|((p2>>30u)<<4u); + bacc=bacc+x[kb+30u]*f32(i32(sp&7u)-3)+x[kb+31u]*f32(i32((sp>>3u)&7u)-3); + acc=acc+bacc*sc[rowB+blk]; blk=blk+${L}u; + } + red[lid.x]=acc; workgroupBarrier(); + var s=${L >> 1}u; loop{ if(s==0u){break;} if(t f32 { + var s4=vec4(0.0); + var x0=x[v]; s4=s4+x0*(vec4(f32(word&3u),f32((word>>2u)&3u),f32((word>>4u)&3u),f32((word>>6u)&3u))-vec4(1.0)); + x0=x[v+1u]; s4=s4+x0*(vec4(f32((word>>8u)&3u),f32((word>>10u)&3u),f32((word>>12u)&3u),f32((word>>14u)&3u))-vec4(1.0)); + x0=x[v+2u]; s4=s4+x0*(vec4(f32((word>>16u)&3u),f32((word>>18u)&3u),f32((word>>20u)&3u),f32((word>>22u)&3u))-vec4(1.0)); + x0=x[v+3u]; s4=s4+x0*(vec4(f32((word>>24u)&3u),f32((word>>26u)&3u),f32((word>>28u)&3u),f32((word>>30u)&3u))-vec4(1.0)); + return s4.x+s4.y+s4.z+s4.w; +}`; +const mmT2Kernel = (add, R = 4, L = 64, U = 1) => ` +@group(0) @binding(0) var x: array>; +@group(0) @binding(1) var qw: array; +${add + ? "@group(0) @binding(2) var r: array;\n@group(0) @binding(3) var o: array;\n@group(0) @binding(4) var P: vec4;" + : "@group(0) @binding(2) var o: array;\n@group(0) @binding(3) var P: vec4;"} +var red: array; +${t2Dot16} +@compute @workgroup_size(256) +fn main(@builtin(workgroup_id) wg:vec3, @builtin(local_invocation_id) lid:vec3){ + let K=P.x; let nw=K>>4u; let rr=lid.x/${L}u; let t=lid.x%${L}u; + let n0=(wg.y*65535u+wg.x)*${R}u+rr; let n=min(n0, P.y-1u); // clamp keeps barriers uniform + let rowW=n*nw; var acc=0.0; var w=t*${U}u; + loop{ if(w>=nw){break;} + ${Array.from({ length: U }, (_, u) => `if(w+${u}u> 1}u; loop{ if(s==0u){break;} if(t(P.w)${add ? "+r[n0]" : ""}; } +}`; + +// Ternary GEMV with PER-256-BLOCK scales (fmt t2r — exact TQ2_0 re-layout for models whose trained +// scale structure is per-row/per-channel, e.g. TriLM): same V2 shape as mmT2Kernel, ONE extra scale +// read per 16 weights (a u32 word never straddles a 256-block). 2.125 bpw traffic. +const mmT2RKernel = (add, R = 4, L = 64, U = 1) => ` +@group(0) @binding(0) var x: array>; +@group(0) @binding(1) var qw: array; +@group(0) @binding(2) var sc: array; +${add + ? "@group(0) @binding(3) var r: array;\n@group(0) @binding(4) var o: array;\n@group(0) @binding(5) var P: vec4;" + : "@group(0) @binding(3) var o: array;\n@group(0) @binding(4) var P: vec4;"} +var red: array; +${t2Dot16} +@compute @workgroup_size(256) +fn main(@builtin(workgroup_id) wg:vec3, @builtin(local_invocation_id) lid:vec3){ + let K=P.x; let nw=K>>4u; let rr=lid.x/${L}u; let t=lid.x%${L}u; + let n0=(wg.y*65535u+wg.x)*${R}u+rr; let n=min(n0, P.y-1u); + let rowW=n*nw; let rowB=n*(K>>8u); var acc=0.0; var w=t*${U}u; + loop{ if(w>=nw){break;} + ${Array.from({ length: U }, (_, u) => `if(w+${u}u>4u)]; }`).join("\n ")} + w=w+${L * U}u; } + red[lid.x]=acc; workgroupBarrier(); + var s=${L >> 1}u; loop{ if(s==0u){break;} if(t ` +@group(0) @binding(0) var x: array>; +@group(0) @binding(1) var qw1: array; +@group(0) @binding(2) var qw2: array; +@group(0) @binding(3) var qw3: array; +@group(0) @binding(4) var o: array; +@group(0) @binding(5) var P: vec4; // K, Ntotal, N1, N2 +@group(0) @binding(6) var S: vec4; // f32bits(s1), f32bits(s2), f32bits(s3) +var red: array; +${t2Dot16} +@compute @workgroup_size(256) +fn main(@builtin(workgroup_id) wg:vec3, @builtin(local_invocation_id) lid:vec3){ + let K=P.x; let nw=K>>4u; let rr=lid.x/${L}u; let t=lid.x%${L}u; + let n0=(wg.y*65535u+wg.x)*${R}u+rr; let n=min(n0, P.y-1u); + var rowW=n*nw; var sc=bitcast(S.x); var src=0u; + if(n>=P.z+P.w){ rowW=(n-P.z-P.w)*nw; sc=bitcast(S.z); src=2u; } + else if(n>=P.z){ rowW=(n-P.z)*nw; sc=bitcast(S.y); src=1u; } + var acc=0.0; var w=t*${U}u; + loop{ if(w>=nw){break;} + ${Array.from({ length: U }, (_, u) => `if(w+${u}u> 1}u; loop{ if(s==0u){break;} if(t ` +@group(0) @binding(0) var gu: array>; // [gate(ff) ‖ up(ff)] +@group(0) @binding(1) var qw: array; +@group(0) @binding(2) var r: array; +@group(0) @binding(3) var o: array; +@group(0) @binding(4) var P: vec4; // K(=ff), N, ff/4 (vec4 offset of up), f32bits(s) +var red: array; +@compute @workgroup_size(256) +fn main(@builtin(workgroup_id) wg:vec3, @builtin(local_invocation_id) lid:vec3){ + let K=P.x; let nw=K>>4u; let rr=lid.x>>6u; let t=lid.x&63u; + let n0=(wg.y*65535u+wg.x)*4u+rr; let n=min(n0, P.y-1u); + let rowW=n*nw; let uo=P.z; var acc=0.0; var w=t; + loop{ if(w>=nw){break;} + let word=qw[rowW+w]; let v=w<<2u; + var s4=vec4(0.0); + for(var c=0u;c<4u;c=c+1u){ + let g4=gu[v+c]; let u4=gu[uo+v+c]; + ${relu2 ? "let a4=max(g4,vec4(0.0)); let x0=a4*a4*u4;" : "let x0=(g4/(vec4(1.0)+exp(-g4)))*u4;"} + let sh=c*8u; + s4=s4+x0*(vec4(f32((word>>(sh*1u))&3u),f32((word>>(sh+2u))&3u),f32((word>>(sh+4u))&3u),f32((word>>(sh+6u))&3u))-vec4(1.0)); + } + acc=acc+s4.x+s4.y+s4.z+s4.w; + w=w+64u; } + red[lid.x]=acc; workgroupBarrier(); + var s=32u; loop{ if(s==0u){break;} if(t(P.w)+r[n0]; } +}`; + +// Fused RoPE: q and k rotated in ONE pass (k heads tail the grid). +const ROPE2 = (theta) => ` +@group(0) @binding(0) var q: array; +@group(0) @binding(1) var k: array; +@group(0) @binding(2) var P: vec4; // nh, hd, pos, nkv +@compute @workgroup_size(64) +fn main(@builtin(global_invocation_id) g:vec3){ + let nh=P.x; let hd=P.y; let pos=f32(P.z); let nkv=P.w; let half=hd/2u; + let id=g.x; if(id>=(nh+nkv)*half){return;} + let head=id/half; let i=id%half; + let freq=pow(${theta}, -2.0*f32(i)/f32(hd)); + let ang=pos*freq; let c=cos(ang); let s=sin(ang); + if(head ` +@group(0) @binding(0) var gu: array; // [gate(ff) ‖ up(ff)] +@group(0) @binding(1) var gamma: array; +@group(0) @binding(2) var o: array; +@group(0) @binding(3) var P: vec4; // ff +var sh: array; +@compute @workgroup_size(256) +fn main(@builtin(local_invocation_id) lid:vec3){ + let ff=P.x; let t=lid.x; + var ss=0.0; + var j=t; loop{ if(j>=ff){break;} + let g=gu[j]; let u=gu[ff+j]; + ${relu2 ? "let a=max(g,0.0); let h=a*a*u;" : "let h=(g/(1.0+exp(-g)))*u;"} + o[j]=h; ss=ss+h*h; j=j+256u; } + sh[t]=ss; workgroupBarrier(); + var s=128u; loop{ if(s==0u){break;} if(t=ff){break;} o[j]=o[j]*inv*gamma[j]; j=j+256u; } +}`; + +// BitNet FFN activation: h = relu(gate)² ⊙ up (squared ReLU — b1.58 2B4T uses this instead of SiLU) +const RELU2MUL = ` +@group(0) @binding(0) var gate: array; +@group(0) @binding(1) var up: array; +@group(0) @binding(2) var o: array; +@group(0) @binding(3) var P: vec4; // ff +@compute @workgroup_size(64) +fn main(@builtin(global_invocation_id) g:vec3){ + let i=g.x; if(i>=P.x){return;} + let v=max(gate[i],0.0); o[i]=v*v*up[i]; +}`; + +// batched decode: embed lookup ON the GPU (token id read from the seq ring) → B.x, no CPU per token +const EMBED = (bits, q3f) => ` +@group(0) @binding(0) var ring: array; +@group(0) @binding(1) var eq: array; +@group(0) @binding(2) var es: array; +@group(0) @binding(3) var x: array; +@group(0) @binding(4) var P: vec4; // ringIdx, d +@compute @workgroup_size(256) +fn main(@builtin(global_invocation_id) g:vec3){ + let i=g.x; let d=P.y; if(i>=d){return;} let tok=ring[P.x]; let sb=tok*(d/32u)+(i>>5u); +${bits === 3 && q3f ? ` let bp=sb*3u; let j=i&31u; var q:u32; + if(j<10u){ q=(eq[bp]>>(j*3u))&7u; } else if(j<20u){ q=(eq[bp+1u]>>((j-10u)*3u))&7u; } else if(j<30u){ q=(eq[bp+2u]>>((j-20u)*3u))&7u; } + else { let sp=(eq[bp]>>30u)|((eq[bp+1u]>>30u)<<2u)|((eq[bp+2u]>>30u)<<4u); if(j==30u){ q=sp&7u; } else { q=(sp>>3u)&7u; } } + x[i]=f32(i32(q)-3)*es[sb];` + : bits === 4 ? ` let gg=tok*d+i; let nib=(eq[gg>>3u]>>((gg&7u)*4u))&0xfu; x[i]=f32(i32(nib)-8)*es[sb];` + : ` let gg=tok*d+i; let b=(eq[gg>>2u]>>((gg&3u)*8u))&0xffu; x[i]=f32(i32(b<<24u)>>24u)*es[sb];`} +}`; +// dedup-aware repetition penalty over the ring's last-64 window (exact Set semantics: first occurrence only) +const PENALTY2 = ` +@group(0) @binding(0) var l: array; +@group(0) @binding(1) var ring: array; +@group(0) @binding(2) var P: vec4; // seqLen, f32bits(rp) +@compute @workgroup_size(64) +fn main(@builtin(global_invocation_id) g:vec3){ + let t=g.x; let n=P.x; if(t>=min(64u,n)){return;} let id=ring[n-1u-t]; + for(var j=0u;j(P.y); let v=l[id]; if(v>0.0){ l[id]=v/rp; } else { l[id]=v*rp; } +}`; +const APPEND = ` +@group(0) @binding(0) var ring: array; +@group(0) @binding(1) var w: array; +@group(0) @binding(2) var P: vec4; // ringIdx +@compute @workgroup_size(1) +fn main(){ ring[P.x]=w[0]; }`; + + +// ── DIFFUSION KERNELS (Dream-class mask-denoising; bidirectional attention over a resident batch) ── +// The causal flag (P.w bit 31) exists ONLY as the parity gate: causal diffuse(block=1) must equal +// the sequential engine token-for-token, which validates every other pass; Dream runs bidirectional. +const ATTNB = (maxN) => ` +@group(0) @binding(0) var q: array; +@group(0) @binding(1) var k: array; +@group(0) @binding(2) var v: array; +@group(0) @binding(3) var o: array; +@group(0) @binding(4) var P: vec4; // nh, nkv, hd, n | causal<<31 +var sc: array; +var red: array; +@compute @workgroup_size(64) +fn main(@builtin(workgroup_id) wg:vec3, @builtin(local_invocation_id) lid:vec3){ + let h=wg.x; let i=wg.y; let nh=P.x; let nkv=P.y; let hd=P.z; + let n=P.w&0x7fffffffu; let lim=select(n-1u, i, (P.w>>31u)==1u); + let group=nh/nkv; let kh=h/group; let kvd=nkv*hd; let qd=nh*hd; + let scale=1.0/sqrt(f32(hd)); let qb=i*qd+h*hd; let kb=kh*hd; let t=lid.x; + var j=t; loop{ if(j>lim){break;} var dd=0.0; for(var c=0u;clim){break;} lm=max(lm,sc[j]); j=j+64u; } + red[t]=lm; workgroupBarrier(); + var st=32u; loop{ if(st==0u){break;} if(tlim){break;} let e=exp(sc[j]-mx); sc[j]=e; ld=ld+e; j=j+64u; } + red[t]=ld; workgroupBarrier(); + st=32u; loop{ if(st==0u){break;} if(t=hd){break;} var acc=0.0; for(var jj=0u;jj<=lim;jj++){ acc=acc+sc[jj]*v[jj*kvd+kb+c]; } o[qb+c]=acc/dn; c=c+64u; } +}`; +const BIASK = ` +@group(0) @binding(0) var o: array; // [rows][N] +@group(0) @binding(1) var bias: array; // [N] +@group(0) @binding(2) var P: vec4; // N (wg.y = row) +@compute @workgroup_size(64) +fn main(@builtin(workgroup_id) wg:vec3, @builtin(local_invocation_id) lid:vec3){ + let i=wg.x*64u+lid.x; if(i>=P.x){return;} + o[wg.y*P.x+i]=o[wg.y*P.x+i]+bias[i]; +}`; +const fbits = (f) => new Uint32Array(new Float32Array([f]).buffer)[0]; +const s8 = (b) => (b << 24) >> 24; + +// STREAMING constructor: `manifest` is the parsed dims+tensor list; `fetchTensor(name)` +// returns ONE tensor's bytes ([q][f32 scales] or [f32]) on demand. The converted +// ~1 GB of weights is never held whole in wasm — each tensor is fetched, uploaded +// to the GPU, then freed — so 1.7 B+ fits under the wasm memory ceiling. +// ── OPFS frame store: the model's frames live on DISK, paged in on demand ── +// Converts once (downloads + dequant), writes every frame to a single OPFS file +// keyed by content, and persists it. On later loads, if the file already exists +// at the right size, conversion is SKIPPED — the model is read straight off disk. +// This is the RAM-wall break: the bytes never sit in JS heap; only the slice +// being played touches RAM (the browser's own page cache is the bounded cache). +async function openFrameStore(key, totalBytes) { + const root = await navigator.storage.getDirectory(); + const fh = await root.getFileHandle(key, { create: true }); + let file = await fh.getFile(); + const ready = file.size === totalBytes; // already converted + on disk? + return { + ready, fh, file, + async beginWrite() { this._w = await fh.createWritable(); }, + async write(pos, bytes) { await this._w.write({ type: "write", position: pos, data: bytes }); }, + async endWrite() { await this._w.close(); this.file = await fh.getFile(); }, + async read(off, len) { return new Uint8Array(await this.file.slice(off, off + len).arrayBuffer()); }, + }; +} + +export async function createQvacGPU(manifest, fetchTensor, cap = 64, eos = 2, stream = false, onProgress = null, frameStore = null, cacheBudget = 0) { + if (!navigator.gpu) throw new Error("no WebGPU"); + const prog = (done, total, label) => { try { onProgress && onProgress(done, total, label); } catch {} }; + // ── cold-load phase timing (near-zero cost; read via window.__qvacLoadStats) ── + // The resident weight upload was the one serialized stretch of cold boot: each tensor's + // fetch+gunzip (parts) blocked the next before its GPU write even ran. QLOAD lets a bench + // A/B the SAME code path at conc=1 (old serial behaviour) vs conc>1 (overlapped) and see the + // fetch/gunzip-vs-GPU-write split, so we optimize the half that actually costs. + const _now = () => (globalThis.performance ? performance.now() : 0); + const QLOAD = { residentMs: 0, partsMs: 0, gpuMs: 0, nTensors: 0, conc: 0 }; + const tmap = {}; for (const t of manifest.tensors) tmap[t.name] = t; + const { d, n_heads, n_kv_heads, ff, vocab, n_layers, hd } = manifest; + const bits = manifest.bits || 8; + const q3f = bits === 3 && manifest.layout === "q3f"; // Q3 FIELD layout (10×3-bit/u32 + spare stubs): ~½ the unpack ALU of bit-planes, same 12 B/block + const kv_dim = n_kv_heads * hd; + const ropeBase = manifest.rope_base || 10000; // Llama 10000, Qwen2/3 1e6 + const ropeLit = Number.isInteger(ropeBase) ? ropeBase + ".0" : "" + ropeBase; + const attnBias = !!manifest.attn_bias; // Qwen2 has q/k/v bias + const qkNorm = !!manifest.qk_norm; // Qwen3/OLMoE have q/k RMSNorm + const qkFull = qkNorm && manifest.qk_norm_dim === d; // OLMoE: RMSNorm over the FULL q/k vector (not per-head) + const subNorm = !!manifest.sub_norm; // BitNet: RMSNorm before wo (attn_sub_norm) + before w_down (ffn_sub_norm) + const bitlinear = !!manifest.bitlinear; // HF-BitNet (BitLinear): WEIGHTLESS RMSNorm on the input of EVERY ternary linear (qkv/wo/gate-up/down) + const relu2 = manifest.ffn_act === "relu2"; // BitNet: squared-ReLU gated FFN (else SiLU) + const moe = !!manifest.moe; // mixture-of-experts (router + per-token expert subset) + const nExp = moe ? manifest.moe.n_experts : 0, nUsed = moe ? manifest.moe.n_used : 0; + const remote = stream === "remote"; // "remote": packed layers stream from a served .qvf via HTTP Range (bound by disk, not RAM/quota) + const opfs = stream === "opfs" || remote; // both use the packed-layer disk path; only the store differs + const frameGran = stream === "frame"; // legacy: play ONE matrix at a time (JS, per-matrix) + stream = !!stream; let streamBuf = 0; // streamBuf = the streamed-weight working set + // NATIVE 2-bit (ADR-0054): weights re-quantized to incoherent 2-bit at load, matmuls read 2-bit DIRECTLY + // on the GPU + a per-matmul input Hadamard. Resident-dense only for now (streaming/MoE keep Q8/Q4). + const twoBit = !!manifest.twoBit && !stream && !moe && !frameGran; + const rotate = twoBit && manifest.incoherent !== false; // incoherence path rotates inputs; LDLQ κ-objects (incoherent:false) do not + const preQuant = twoBit && !!manifest.preQuantized; // load-direct: weights arrive ALREADY 2-bit (compiled offline) — no re-quant at load + const nextP2 = (n) => { let p = 1; while (p < n) p <<= 1; return p; }; + const maxKp = rotate ? nextP2(Math.max(d, ff, kv_dim, n_heads * hd)) : 0; + const qlenOf = (t) => bits === 3 ? (t.N * (t.K / 32)) * 12 : bits === 4 ? (t.N * t.K) / 2 : t.N * t.K; // Q3 = 3 u32 (12 bytes) per 32-block + const slenOf = (t) => t.N * (t.K / 32) * 4; + + const adapter = await navigator.gpu.requestAdapter(); + // Big-vocab models (Qwen: 151 k × d int8 ≈ 136 MB) exceed the default 128 MB + // storage-buffer binding limit — request the adapter's max so the bind succeeds. + const L = adapter.limits; + const canTs = adapter.features.has("timestamp-query"); // per-pass GPU profiling (dev: window.__profile) + const dev = await adapter.requestDevice({ + requiredFeatures: canTs ? ["timestamp-query"] : [], + requiredLimits: { + maxStorageBufferBindingSize: L.maxStorageBufferBindingSize, + maxBufferSize: L.maxBufferSize, + maxComputeWorkgroupsPerDimension: L.maxComputeWorkgroupsPerDimension, + }, + }); + // track total GPU memory allocated (weights + KV cache + scratch) so the system + // monitor can show — and free — exactly what this model holds on the GPU. + let gpuBytes = 0; + const _createBuffer = dev.createBuffer.bind(dev); + dev.createBuffer = (desc) => { gpuBytes += desc.size || 0; return _createBuffer(desc); }; + const U = GPUBufferUsage; + let pid = 0; + const pipe = (code, name) => { const p = dev.createComputePipeline({ layout: "auto", compute: { module: dev.createShaderModule({ code }), entryPoint: "main" } }); p._id = ++pid; p._name = name || "p" + pid; return p; }; + if (cap > 4000) throw new Error("cap " + cap + " exceeds the attention score tile (workgroup storage)"); + const kv4 = !!manifest.kv4 && !stream && !moe && !frameGran; // int4 KV cache for layers 1+ (E6) + const P_mm = pipe(mmKernel(twoBit ? 2 : bits, false, q3f), "mm"), P_mmadd = pipe(mmKernel(twoBit ? 2 : bits, true, q3f), "mmadd"), P_rms = pipe(RMS, "rms"), P_rope = pipe(ROPE(ropeLit), "rope"), P_attn = pipe(ATTN(cap), "attn"), P_sm = pipe(relu2 ? RELU2MUL : SILUMUL, relu2 ? "relu2" : "silu"), P_qkn = pipe(QKNORM, "qkn"); + const P_attnQ = kv4 ? pipe(ATTNQ(cap, kv_dim), "attnQ") : null, P_kvq = kv4 ? pipe(KVQ(kv_dim), "kvq") : null; + const P_fwL = twoBit ? pipe(FWHT_LOAD, "fwht") : null, P_fwB = twoBit ? pipe(FWHT_BFLY, "fwht") : null, P_fwN = twoBit ? pipe(FWHT_NORM, "fwht") : null; + const P_axpy = moe ? pipe(AXPY, "axpy") : null; + const moeBatch = moe && !frameStore; // resident κ-object MoE → batched-expert kernels + const P_moeGU = moeBatch ? pipe(MOE_GU, "moeGU") : null, P_moeDn = moeBatch ? pipe(MOE_DN, "moeDn") : null; + // E₈ codebook tensors (fmt e8q): own pipelines + the sealed 256×8 LUT as a GPU buffer + const hasE8 = manifest.tensors.some((t) => t.fmt === "e8q"); + const P_mmE8 = hasE8 ? pipe(mmE8Kernel(false), "mmE8") : null, P_mmE8add = hasE8 ? pipe(mmE8Kernel(true), "mmE8add") : null; + // BitNet ternary tensors (fmt t2): own pipelines; per-tensor scale rides each weight-set's uniform + const hasT2 = manifest.tensors.some((t) => t.fmt === "t2"); + const P_mmT2 = hasT2 ? pipe(mmT2Kernel(false), "mmT2") : null, P_mmT2add = hasT2 ? pipe(mmT2Kernel(true), "mmT2add") : null; + const hasT2R = manifest.tensors.some((t) => t.fmt === "t2r"); + const P_mmT2R = hasT2R ? pipe(mmT2RKernel(false), "mmT2R") : null, P_mmT2Radd = hasT2R ? pipe(mmT2RKernel(true), "mmT2Radd") : null; + // big-tensor geometry (≥16M weights): 16 rows × 16 lanes × unroll-4 — measured 1.6-2× on FFN shapes + const T2BIG = 16e6; + const P_mmQ3B = bits === 3 && q3f ? pipe(mmQ3BigKernel(), "mmQ3B") : null; // big q3f (lm_head) + const P_mmT2B = hasT2 ? pipe(mmT2Kernel(false, 16, 16, 4), "mmT2B") : null, P_mmT2Badd = hasT2 ? pipe(mmT2Kernel(true, 16, 16, 4), "mmT2Badd") : null; + const P_mmT2RB = hasT2R ? pipe(mmT2RKernel(false, 16, 16, 4), "mmT2RB") : null, P_mmT2RBadd = hasT2R ? pipe(mmT2RKernel(true, 16, 16, 4), "mmT2RBadd") : null; + // DRAFT: dense sub-norm (BitNet) STREAMING is enabled — the unfused layerBody applies attn/ffn sub-norm + // (rms over the resident Nrm weights) with ws(role) from R[role], and mmW dispatches t2 (Falcon-E proves it); + // combined with the t2 stream packing (t2stream), dense sub-norm streams. MoE sub-norm stays resident-only. + if (subNorm && manifest.moe) throw new Error("bitnet sub-norm MoE path is resident-only for now"); + // fused ternary layer (resident only): qkv 3→1 pass, gate/up 2→1, both ropes 1, act⊕sub-norm 1 + // measured policy (A/B walls, 5-run medians): fusion wins on the sub-norm (BitNet) family + // (12.3 vs 13.2 ms/tok on bitnet-2b) and LOSES on llama-arch ternary (24.5 vs 19.5 on + // falcon-e-3b — t2ad recomputes act per workgroup; short-K shapes punish the stacked t2f). + const fusedT2 = !globalThis.__noFuse && hasT2 && subNorm && !stream && !moe && !frameGran; + const P_t2f = fusedT2 ? pipe(mmT2FusedKernel(), "t2f") : null; + const P_t2fB = fusedT2 ? pipe(mmT2FusedKernel(16, 16, 4), "t2fB") : null; // big-geometry for the gate/up fusion + const P_rope2 = fusedT2 ? pipe(ROPE2(ropeLit), "rope2") : null; + const P_actn = fusedT2 && subNorm ? pipe(ACTNORM(relu2), "actn") : null; + const P_t2ad = fusedT2 && !subNorm ? pipe(mmT2ActAddKernel(relu2), "t2ad") : null; + let lutBuf = null; + if (hasE8) { + const lu = manifest.e8lutData; if (!lu || lu.length !== 2048) throw new Error("e8q tensors but no/bad e8lutData (need 256×8 f32)"); + lutBuf = dev.createBuffer({ size: lu.byteLength, usage: U.STORAGE | U.COPY_DST }); dev.queue.writeBuffer(lutBuf, 0, lu); + } + // ── per-pass GPU timestamps (dev-only; armed per-step by window.__profile, zero cost when off) ── + let PROF = null; + const profInit = () => { if (PROF || !canTs) return; PROF = { qs: dev.createQuerySet({ type: "timestamp", count: 4096 }), buf: dev.createBuffer({ size: 4096 * 8, usage: U.QUERY_RESOLVE | U.COPY_SRC }), stg: dev.createBuffer({ size: 4096 * 8, usage: U.MAP_READ | U.COPY_DST }), i: 0, tags: [], active: false }; }; + + const sbuf = (n) => dev.createBuffer({ size: Math.max(16, n * 4), usage: U.STORAGE | U.COPY_DST | U.COPY_SRC }); + const ubuf = (arr) => { const b = dev.createBuffer({ size: 16, usage: U.UNIFORM | U.COPY_DST }); dev.queue.writeBuffer(b, 0, arr); return b; }; + + // ── weights → GPU ── + // Resident: every layer's matrices get their own GPU buffer (all live on-GPU). + // Stream (store-as-memory): keep the layer matrices in JS and page each layer + // into ONE reusable buffer set per token — only the working set (1 layer) is + // ever GPU-resident, so GPU memory is O(1 layer), not O(depth). + // MoE packs only the attention matrices per layer; the FFN is per-expert frames + // streamed on demand. Dense packs attention + the FFN trio. + const ROLES = moe ? ["wq", "wk", "wv", "wo"] : ["wq", "wk", "wv", "wo", "w_gate", "w_up", "w_down"]; + const W = {}, Wb = {}, R = {}, FB = {}; + let RM = null, RMscale = null; // single reusable "frame" buffer (frame mode) + const padQ = (q) => { if (q.length % 4) { const p = new Uint8Array(Math.ceil(q.length / 4) * 4); p.set(q); return p; } return q; }; + // fetchTensor may be sync (wasm) or async (disk-streamed ingestion); `await` + // handles both (await on a plain value returns it). So all of setup is async. + const parts = async (name) => { + const t = tmap[name], bytes = await fetchTensor(name); + if (t.fmt === "e8q") { // E₈ codebook tensor: [u16 codewords N·K/4 B (4 u16 per 32 weights)][f16 scales N·K/16 B] + const ql = t.N * t.K / 4; + return { q: padQ(bytes.subarray(0, ql)), sRaw: bytes.subarray(ql).slice(), N: t.N, K: t.K, e8: true }; + } + if (t.fmt === "t2r") { // ternary + per-256-block f32 scales (blob = [codes][scales]) + const ql = t.N * t.K / 4; + const lim = Math.min(ql, 65536); + for (let i = 0; i < lim; i++) { const b = bytes[i]; if ((b & 3) === 3 || ((b >> 2) & 3) === 3 || ((b >> 4) & 3) === 3 || (b >> 6) === 3) throw new Error(`t2r alphabet violation in ${name} @byte ${i}`); } + return { q: bytes.subarray(0, ql), sRaw: bytes.subarray(ql).slice(), N: t.N, K: t.K, t2r: true }; + } + if (t.fmt === "t2") { // ternary: blob = codes only; scale lives in the manifest rec + // geometric validity (Law L5 beyond bytes): ternary fields must decode in {0,1,2} — sampled + // 64 KB/tensor at load; the full-census proof is the sealed atlas-bridge witness receipt. + const lim = Math.min(bytes.length, 65536); + for (let i = 0; i < lim; i++) { const b = bytes[i]; if ((b & 3) === 3 || ((b >> 2) & 3) === 3 || ((b >> 4) & 3) === 3 || (b >> 6) === 3) throw new Error(`t2 alphabet violation in ${name} @byte ${i}`); } + return { q: bytes, N: t.N, K: t.K, t2: true, ts: t.s }; + } + if (preQuant) { // load-direct: bytes ARE [2-bit packed (N·Kp/4)][f32 scales] + const Kp = rotate ? nextP2(t.K) : t.K, q2 = (t.N * Kp) / 4; + return { q: padQ(bytes.subarray(0, q2)), s: new Float32Array(bytes.buffer, bytes.byteOffset + q2, t.N * (Kp / 32)), N: t.N, K: t.K, Kp }; + } + const qlen = qlenOf(t), slen = slenOf(t); + const q = padQ(bytes.subarray(0, qlen)), s = new Float32Array(bytes.subarray(qlen, qlen + slen).slice().buffer); + if (twoBit) { const r = requant2bit(q, s, t.N, t.K); return { q: r.q, s: r.s, N: t.N, K: t.K, Kp: r.Kp }; } // requant-at-load = incoherence (LDLQ needs pre-compile) + return { q, s, N: t.N, K: t.K }; + }; + // GPU-side of a resident weight: create buffers + writeBuffer from already-decoded parts `p`. + // Split out of upW so the fetch+gunzip half (parts, CPU/native-async) can be overlapped across + // tensors while these device calls stay serialized on the one JS thread (they must be). + const writeW = (name, p) => { + if (p.t2) { // ternary: no scale buffer; s → P.w as f32 bits + const qbuf = dev.createBuffer({ size: p.q.byteLength, usage: U.STORAGE | U.COPY_DST }); dev.queue.writeBuffer(qbuf, 0, p.q); + W[name] = { qbuf, sbuf: null, uni: ubuf(new Uint32Array([p.K, p.N, p.K / 16, fbits(p.ts)])), N: p.N, K: p.K, Kp: p.K, t2: true, s: p.ts }; + return; + } + if (p.t2r) { // ternary + per-256-block scale buffer + const qbuf = dev.createBuffer({ size: p.q.byteLength, usage: U.STORAGE | U.COPY_DST }); dev.queue.writeBuffer(qbuf, 0, p.q); + const sb = dev.createBuffer({ size: Math.max(16, p.sRaw.byteLength), usage: U.STORAGE | U.COPY_DST }); dev.queue.writeBuffer(sb, 0, p.sRaw); + W[name] = { qbuf, sbuf: sb, uni: ubuf(new Uint32Array([p.K, p.N, p.K / 16, 0])), N: p.N, K: p.K, Kp: p.K, t2r: true }; + return; + } + const sBytes = p.sRaw || p.s; // e8q scales stay raw f16 bytes; others are f32 arrays + const qbuf = dev.createBuffer({ size: p.q.byteLength, usage: U.STORAGE | U.COPY_DST }); dev.queue.writeBuffer(qbuf, 0, p.q); + const sbuf2 = dev.createBuffer({ size: Math.max(16, sBytes.byteLength), usage: U.STORAGE | U.COPY_DST }); dev.queue.writeBuffer(sbuf2, 0, sBytes); + const Kp = twoBit ? p.Kp : p.K; + W[name] = { qbuf, sbuf: sbuf2, uni: ubuf(new Uint32Array([Kp, p.N, Kp / 32, 0])), N: p.N, K: p.K, Kp, e8: !!p.e8 }; + }; + const upW = async (name) => writeW(name, await parts(name)); + if (!frameGran) await upW("lm_head"); // resident in resident/layer modes; TILED (played) in frame mode + let RQ = null, RS = null, packLayout = null, packStride = 0, packQbytes = 0; + // DRAFT (t2 layer-streaming — needs WebGPU verification): BitNet/Falcon t2 has a SCALAR scale per matrix + // (baked into the matmul uniform via fbits), not a per-block scale buffer. So a t2 streamed layer packs + // q ONLY (no scale blob), and each layer's per-role scalar is written into the reusable R[role].uni before + // that layer's matmul. t2scales[l][role] = fbits(scale) is precomputed from the manifest (no fetch). + const t2stream = !!stream && !moe && !frameGran && ROLES.every((r) => (tmap[`l0.${r}`] || {}).fmt === "t2"); + let t2scales = null; + let opfsStore = null; + const frameMan = {}; // name → {off, qlen, slen, N, K} (frame layout) + if (frameGran) { + // FRAME PLAYER: each weight matrix is a "frame", played through ONE reusable + // buffer sized to the largest single matrix. The frames live either in JS + // (frame mode) or on DISK in OPFS (opfs mode — the RAM-wall break). + const names = []; + for (let l = 0; l < n_layers; l++) for (const role of ROLES) names.push(`l${l}.${role}`); + names.push("lm_head"); + let mq = 0, ms = 0, off = 0; + for (const name of names) { // deterministic layout (sizes from the manifest, no fetch) + const t = tmap[name], ql = Math.ceil(qlenOf(t) / 4) * 4, sl = slenOf(t); + frameMan[name] = { off, qlen: ql, slen: sl, N: t.N, K: t.K }; + off += ql + sl; + if (name !== "lm_head") { mq = Math.max(mq, ql); ms = Math.max(ms, sl); } + } + RM = dev.createBuffer({ size: mq, usage: U.STORAGE | U.COPY_DST }); + RMscale = dev.createBuffer({ size: Math.max(16, ms), usage: U.STORAGE | U.COPY_DST }); + streamBuf = mq + Math.max(16, ms); + if (opfs) { + opfsStore = await openFrameStore(`qvac-${vocab}-${n_layers}-${d}-b${bits}.frames`, off); + if (!opfsStore.ready) { // not on disk yet → convert + write (once) + await opfsStore.beginWrite(); + for (const name of names) { const p = await parts(name), m = frameMan[name]; await opfsStore.write(m.off, p.q); await opfsStore.write(m.off + m.qlen, new Uint8Array(p.s.buffer, p.s.byteOffset, p.s.byteLength)); } + await opfsStore.endWrite(); + } + } else { // frame mode: frames in JS + for (let l = 0; l < n_layers; l++) for (const role of ROLES) { const p = await parts(`l${l}.${role}`); FB[`l${l}.${role}`] = { q: p.q, s: p.s, uni: ubuf(new Uint32Array([p.K, p.N, p.K / 32, 0])), N: p.N }; } + FB["lm_head"] = await parts("lm_head"); + } + } else if (stream) { + // Pack each layer's 7 matrices into ONE q-blob + ONE scale-blob (roles laid + // out at 256-aligned offsets — the storage-buffer binding-offset granularity). + // Then the per-token upload is just 2 big writeBuffers/layer instead of 14 + // small ones (cuts 392 calls → 56 and makes each copy a fat, fast DMA), and + // each role's matmul binds its sub-range. Only one layer is GPU-resident. + // The packed bytes live in JS ("layer") or on DISK in OPFS ("opfs" — the + // RAM-wall break); EITHER way the per-layer body is the SAME proven layerBody(). + const al = (n) => Math.ceil(n / 256) * 256; + // t2 (BitNet/Falcon): q is codes only (N·K/4 B, 4 trits/byte) + a SCALAR scale (→ uniform); no scale blob. + // Every other stream fmt (q3f/q4/e8/t2r): q (padded) + a per-block scale blob at soff. Same packLayer shape. + const qbl = (t) => t2stream ? Math.ceil((t.N * t.K / 4) / 4) * 4 : Math.ceil(qlenOf(t) / 4) * 4; + const sbl = (t) => t2stream ? 0 : slenOf(t); + packLayout = {}; let qo = 0, so = 0; + for (const role of ROLES) { const t = tmap[`l0.${role}`]; qo = al(qo); so = al(so); packLayout[role] = { qoff: qo, qsize: qbl(t), soff: so, ssize: sbl(t), N: t.N, K: t.K }; qo += qbl(t); so += sbl(t); } + const packQ = al(qo), packS = al(so); + RQ = dev.createBuffer({ size: packQ, usage: U.STORAGE | U.COPY_DST }); + RS = dev.createBuffer({ size: Math.max(16, packS), usage: U.STORAGE | U.COPY_DST }); + streamBuf = packQ + Math.max(16, packS); + // t2: per-layer-per-role scalar (fbits), refreshed into R[role].uni each layer in the forward. Manifest-only. + if (t2stream) { t2scales = []; for (let l = 0; l < n_layers; l++) { const row = {}; for (const role of ROLES) row[role] = fbits((tmap[`l${l}.${role}`] || {}).s ?? 1); t2scales.push(row); } } + for (const role of ROLES) { + const L = packLayout[role]; + R[role] = t2stream + ? { qbuf: { buffer: RQ, offset: L.qoff, size: L.qsize }, uni: ubuf(new Uint32Array([L.K, L.N, L.K / 16, 0])), N: L.N, K: L.K, t2: true } // scale.w set per layer + : { qbuf: { buffer: RQ, offset: L.qoff, size: L.qsize }, sbuf: { buffer: RS, offset: L.soff, size: L.ssize }, uni: ubuf(new Uint32Array([L.K, L.N, L.K / 32, 0])), N: L.N }; + } + const packLayer = async (l) => { // build one layer's packed (q[,s]) from the κ-object + const q = new Uint8Array(packQ), s = new Uint8Array(packS); + for (const role of ROLES) { const p = await parts(`l${l}.${role}`); q.set(p.q, packLayout[role].qoff); if (!t2stream) s.set(new Uint8Array(p.s.buffer, p.s.byteOffset, p.s.byteLength), packLayout[role].soff); } + return { q, s }; + }; + if (opfs) { // DISK-backed: page each layer's packed blob in per token + packQbytes = packQ; packStride = packQ + packS; + if (remote) { // already on the server's disk as a .qvf → just read it via Range + opfsStore = frameStore; prog(n_layers, n_layers, "layers"); + } else { + opfsStore = await openFrameStore(`qvac-packed-${vocab}-${n_layers}-${d}-b${bits}.frames`, packStride * n_layers); + if (!opfsStore.ready) { // not on disk yet → convert + write (once) + await opfsStore.beginWrite(); + for (let l = 0; l < n_layers; l++) { const pk = await packLayer(l); await opfsStore.write(l * packStride, pk.q); await opfsStore.write(l * packStride + packQ, pk.s); prog(l + 1, n_layers, "layers"); } + await opfsStore.endWrite(); + } else prog(n_layers, n_layers, "layers"); + } + } else { // JS-backed: packed blobs stay in the heap, paged in per token + for (let l = 0; l < n_layers; l++) { Wb[l] = await packLayer(l); prog(l + 1, n_layers, "layers"); } + } + } else { + // RESIDENT (default dense path — chat/voice/messenger/diffusion all land here). + // A bounded pool CAN run parts() (fetch+gunzip+κ-verify) concurrently, but MEASUREMENT + // (forge/gpu/coldload-bench.html, BitNet-2B on RDNA-3) says default it OFF (conc=1 = the + // original strictly-serial path): warm the phase is ~4.3s and does NOT parallelize — gunzip + // + per-block SHA-256 are contention-bound, so conc>1 only trades wall-time for per-op + // slowdown (conc=8 was ~4% slower). GPU-write is ~0.35s of the whole phase, so there is no + // upload-overlap win to get. The cold network fetch is already parallelized by + // prefetchBlocks(conc=12) upstream. Kept opt-in (window.__qUploadConc>1) only for the cold + // window where the resident loop can outrun prefetch and stall on a serial network miss. + const names = []; + for (let l = 0; l < n_layers; l++) for (const role of ROLES) names.push(`l${l}.${role}`); + const conc = Math.max(1, (globalThis.__qUploadConc | 0) || 1); + QLOAD.conc = conc; QLOAD.nTensors = names.length; + const t0 = _now(); + let i = 0; + const worker = async () => { + while (i < names.length) { + const name = names[i++]; + const ta = _now(); const p = await parts(name); QLOAD.partsMs += _now() - ta; + const tb = _now(); writeW(name, p); QLOAD.gpuMs += _now() - tb; + prog(i, names.length, "weights"); + } + }; + await Promise.all(Array.from({ length: Math.min(conc, names.length || 1) }, worker)); + QLOAD.residentMs = _now() - t0; + try { globalThis.__qvacLoadStats = QLOAD; } catch {} + } + + const Nrm = {}; + const upN = async (name) => { const f = new Float32Array((await fetchTensor(name)).slice().buffer); const buf = dev.createBuffer({ size: Math.max(16, f.byteLength), usage: U.STORAGE | U.COPY_DST }); dev.queue.writeBuffer(buf, 0, f); Nrm[name] = buf; }; + await upN("final_norm"); + for (let l = 0; l < n_layers; l++) { await upN(`l${l}.attn_norm`); await upN(`l${l}.ffn_norm`); } + // BitNet sub-norm weights (attn: [q_dim], ffn: [ff]) + if (subNorm) for (let l = 0; l < n_layers; l++) { await upN(`l${l}.attn_sub_norm`); await upN(`l${l}.ffn_sub_norm`); } + // Qwen2 q/k/v projection biases (f32 vectors, same store as norms) + if (attnBias) for (let l = 0; l < n_layers; l++) { await upN(`l${l}.bq`); await upN(`l${l}.bk`); await upN(`l${l}.bv`); } + // Qwen3 per-head q/k RMSNorm weights ([hd] f32) + if (qkNorm) for (let l = 0; l < n_layers; l++) { await upN(`l${l}.q_norm`); await upN(`l${l}.k_norm`); } + + // embed: per-block int8/nibbles + scales, kept in JS for the host-side lookup + const eT = tmap["embed"], eb = await fetchTensor("embed"), eqlen = qlenOf(eT); + const embedQ = eb.subarray(0, eqlen); + const embedS = new Float32Array(eb.subarray(eqlen, eqlen + slenOf(eT)).slice().buffer); + + // ── scratch + KV cache (GPU-resident) ── + // The query/attention dim is n_heads·hd, which can EXCEED the hidden d (e.g. + // Qwen3-30B-A3B: 32·128=4096 vs d=2048). q and the attn output (wo's input) must + // be sized by q_dim, not d — else the wq matmul overflows a too-small buffer. + const q_dim = n_heads * hd; + const B = { x: sbuf(d), normed: sbuf(d), q: sbuf(q_dim), k: sbuf(kv_dim), v: sbuf(kv_dim), attn: sbuf(q_dim), attn_out: sbuf(d), h: sbuf(d), normed2: sbuf(d), gate: sbuf(ff), up: sbuf(ff), hid: sbuf(ff), mlp: sbuf(d), cur: sbuf(d), logits: sbuf(vocab) }; + if (subNorm || bitlinear) { B.attn2 = sbuf(q_dim); B.hid2 = sbuf(ff); } // BitNet sub-norm / BitLinear outputs (RMS must not run in place on this driver) + if (bitlinear) { // unit gammas for the weightless BitLinear input norms + const unit = (n, name) => { const f = new Float32Array(n).fill(1); const b = dev.createBuffer({ size: f.byteLength, usage: U.STORAGE | U.COPY_DST }); dev.queue.writeBuffer(b, 0, f); Nrm[name] = b; }; + unit(d, "__unit_d"); unit(q_dim, "__unit_qd"); unit(ff, "__unit_ff"); + } + // fused-ternary concat outputs: q‖k‖v and gate‖up; downstream passes bind 256-aligned sub-ranges + let qR = null, kR = null, vR = null; + if (fusedT2) { + B.qkv = sbuf(q_dim + 2 * kv_dim); B.gu = sbuf(2 * ff); + if ((q_dim * 4) % 256 || ((q_dim + kv_dim) * 4) % 256 || (ff * 4) % 256) throw new Error("fusedT2 needs 256B-aligned sub-ranges"); + qR = { buffer: B.qkv, offset: 0, size: q_dim * 4 }; + kR = { buffer: B.qkv, offset: q_dim * 4, size: kv_dim * 4 }; + vR = { buffer: B.qkv, offset: (q_dim + kv_dim) * 4, size: kv_dim * 4 }; + } + const staging = dev.createBuffer({ size: vocab * 4, usage: U.MAP_READ | U.COPY_DST }); + const kcache = [], vcache = []; + const kvS = kv_dim / 8 + kv_dim / 32; // int4 record: u32s per token per side + for (let l = 0; l < n_layers; l++) { + const q4 = kv4 && l > 0; // layer 0 stays f32 (measured pathological at int4) + kcache.push(sbuf(q4 ? cap * kvS : cap * kv_dim)); vcache.push(sbuf(q4 ? cap * kvS : cap * kv_dim)); + } + const uDim = ubuf(new Uint32Array([d, 0, 0, 0])), uFF = ubuf(new Uint32Array([ff, 0, 0, 0])); + const uRopeQ = ubuf(new Uint32Array([n_heads, hd, 0, 0])), uRopeK = ubuf(new Uint32Array([n_kv_heads, hd, 0, 0])), uAttn = ubuf(new Uint32Array([n_heads, n_kv_heads, hd, 0])); + const uQkn = ubuf(new Uint32Array([n_heads, hd, 0, 0])); // QK-Norm (P.y=hd; head count from dispatch) + const uQd = (subNorm || bitlinear) ? ubuf(new Uint32Array([q_dim, 0, 0, 0])) : null; // attn_sub_norm runs over q_dim (= n_heads·hd) + // per-layer fused-ternary uniform packs (dims + the three per-tensor scales per fusion) + const T2F = []; + if (fusedT2) for (let l = 0; l < n_layers; l++) { + const g = (r) => W[`l${l}.${r}`]; + const ok = ["wq", "wk", "wv", "w_gate", "w_up", "w_down"].every((r) => g(r) && g(r).t2); + T2F.push(ok ? { + qkvP: ubuf(new Uint32Array([d, q_dim + 2 * kv_dim, q_dim, kv_dim])), + qkvS: ubuf(new Uint32Array([fbits(g("wq").s), fbits(g("wk").s), fbits(g("wv").s), 0])), + guP: ubuf(new Uint32Array([d, 2 * ff, ff, ff])), + guS: ubuf(new Uint32Array([fbits(g("w_gate").s), fbits(g("w_up").s), fbits(g("w_up").s), 0])), + dP: subNorm ? null : ubuf(new Uint32Array([ff, d, ff / 4, fbits(g("w_down").s)])), + } : null); + } + const uLm = frameGran ? ubuf(new Uint32Array([d, 0, d / 32, 0])) : null; // tiled lm_head: [K=d, rows, nblk, base] + const uFr = opfs ? ubuf(new Uint32Array([0, 0, 0, 0])) : null; // shared per-frame uniform (rewritten per matrix) + + // ── MoE: router (CPU), per-expert GPU buffers, expert streamer + cache ── + // The router picks n_used of n_experts per token (CPU top-k off a tiny matmul); + // only those experts' weights are streamed (frameStore.readExpert) and uploaded + // into ONE reusable gate/up/down buffer set. Bytes/token ≈ n_used/n_experts of FFN. + let routerCPU = null, stagingD = null, ExpQ = null, ExpS = null, uMoeIn = null, uMoeDn = null; + const expCache = new Map(); let expBytesCached = 0; const expInflight = new Map(); + // κ-object MoE (G5): no frame stream → experts come from fetchTensor (`l{l}.e{e}.{role}`, the + // compiled q4 slab, L5-verified by the loader). They live RESIDENT IN VRAM (below) so the forward + // never re-uploads weights. Streaming path (frameStore) keeps the bounded per-token RAM cache. + const residentExperts = moe && !frameStore; + const EQ = (bits === 4 ? (ff * d) / 2 : ff * d), ES = (ff * d / 32) * 4; // one expert matrix: q bytes / scale bytes (N·K = ff·d for gate/up/down alike) + let ExpVRAM = null; const vramSet = new Set(); + if (moe) { + routerCPU = []; + for (let l = 0; l < n_layers; l++) routerCPU.push(new Float32Array((await fetchTensor(`l${l}.router`)).slice().buffer)); // [nExp*d] per layer + stagingD = dev.createBuffer({ size: d * 4, usage: U.MAP_READ | U.COPY_DST }); + uMoeIn = ubuf(new Uint32Array([d, ff, d / 32, 0])); // gate/up: K=d, N=ff + uMoeDn = ubuf(new Uint32Array([ff, d, ff / 32, 0])); // down: K=ff, N=d + var uAxpy = ubuf(new Uint32Array([d, 0, 0, 0])); // AXPY: [n=d, f32bits(weight)] (streaming: rewritten per expert) + // resident: one AXPY uniform PER expert slot → all nUsed experts' FFN passes ride ONE command + // encoder / ONE submit per layer (was one submit per expert = 8× the dispatch tax). + var uAxpyN = Array.from({ length: nUsed }, () => ubuf(new Uint32Array([d, 0, 0, 0]))); + if (residentExperts) { + // ALL experts RESIDENT IN VRAM: one (q,s) buffer per (layer,role) holding all nExp experts. + // The forward binds expert e's 256-aligned sub-range (e·EQ / e·ES — EQ,ES are 256-multiples) ⇒ + // ZERO per-token weight upload (the win over the per-token CPU→GPU copy). ≈ nExp·EQ·3·n_layers + // VRAM (OLMoE ≈ 3.8 GB); attention stays layer-paged so total fits a ~4 GB GPU. + ExpVRAM = []; + for (let l = 0; l < n_layers; l++) { const r = {}; for (const role of ["gate", "up", "down"]) r[role] = { q: dev.createBuffer({ size: nExp * EQ, usage: U.STORAGE | U.COPY_DST }), s: dev.createBuffer({ size: nExp * ES, usage: U.STORAGE | U.COPY_DST }) }; ExpVRAM.push(r); } + // batched-expert scratch + uniforms (all nUsed experts in ONE dispatch per stage) + B.gate8 = sbuf(nUsed * ff); B.up8 = sbuf(nUsed * ff); B.hid8 = sbuf(nUsed * ff); + var uMoeGU = ubuf(new Uint32Array([d, ff, d / 32, nUsed])); // gate/up: K=d, ff rows/expert + var uMoeDnB = ubuf(new Uint32Array([ff, d, ff / 32, nUsed])); // down: K=ff, N=d + var uFFb = ubuf(new Uint32Array([nUsed * ff, 0, 0, 0])); // silu over all experts + var uIdx = ubuf(new Uint32Array(8)), uWts = ubuf(new Float32Array(8)); + } else { // streaming: ONE reusable expert buffer set, re-uploaded per token + ExpQ = { gate: sbuf(EQ / 4), up: sbuf(EQ / 4), down: sbuf(EQ / 4) }; // sbuf takes element count → *4 bytes + ExpS = { gate: sbuf(ES / 4), up: sbuf(ES / 4), down: sbuf(ES / 4) }; + } + } + // lazy-upload one expert into its VRAM slab (once, on first activation); bind-only thereafter. + const ensureVram = async (l, e, role) => { + const key = (l * nExp + e) * 3 + { gate: 0, up: 1, down: 2 }[role]; + if (vramSet.has(key)) return; + const b = await fetchTensor(`l${l}.e${e}.${role}`); // L5-verified q4 slab [q][f32 scales] + const slab = ExpVRAM[l][role]; + dev.queue.writeBuffer(slab.q, e * EQ, b.subarray(0, EQ)); + dev.queue.writeBuffer(slab.s, e * ES, b.subarray(EQ, EQ + ES)); + vramSet.add(key); + }; + // Stream + cache one expert role frame ([q][scales]); returns {q,s} (streaming path only). + const readExpert = async (l, e, role) => { + const key = (l * nExp + e) * 3 + { gate: 0, up: 1, down: 2 }[role]; + if (expCache.has(key)) return expCache.get(key); + if (expInflight.has(key)) return expInflight.get(key); + const eq = (bits === 4 ? (ff * d) / 2 : ff * d); + const src = residentExperts ? fetchTensor(`l${l}.e${e}.${role}`) : frameStore.readExpert(l, e, role); + const p = Promise.resolve(src).then((b) => { + expInflight.delete(key); + const v = { q: b.subarray(0, eq), s: b.subarray(eq) }; + expCache.set(key, v); expBytesCached += b.byteLength; + if (!residentExperts) { // streaming: bound the cache; resident: keep every expert + const budget = Math.max(cacheBudget, (nUsed * 3 + 4) * b.byteLength); + if (expBytesCached > budget) for (const k of expCache.keys()) { if (expBytesCached <= budget) break; const old = expCache.get(k); expBytesCached -= old.q.byteLength + old.s.byteLength; expCache.delete(k); } + } + return v; + }); + expInflight.set(key, p); return p; + }; + + // bind groups are stable across steps (same buffers) → build once, cache by key + let bid = 0; + const tag = (b) => { if (b._id === undefined) b._id = ++bid; return b; }; + const bgCache = new Map(); + const pass = (enc, pipeline, bufs, groups) => { + // hot path (~300 passes/token): zero-allocation key build; the entries array (and its + // resource objects) is constructed ONLY on a bind-group cache miss. + let key = ""; + for (let i = 0; i < bufs.length; i++) { const b = bufs[i]; key += (b.buffer ? tag(b.buffer)._id + "@" + (b.offset || 0) : tag(b)._id) + ","; } + let pm = bgCache.get(pipeline._id); + if (!pm) { pm = new Map(); bgCache.set(pipeline._id, pm); } + let bg = pm.get(key); + if (!bg) { + const entries = bufs.map((b, i) => b.buffer + ? { binding: i, resource: { buffer: b.buffer, offset: b.offset || 0, size: b.size } } + : { binding: i, resource: { buffer: b } }); + bg = dev.createBindGroup({ layout: pipeline.getBindGroupLayout(0), entries }); + pm.set(key, bg); + } + let desc; // profiling: timestamp this pass (lm_head's 2D grid tagged apart) + if (PROF && PROF.active && PROF.i + 2 <= 4096) { desc = { timestampWrites: { querySet: PROF.qs, beginningOfPassWriteIndex: PROF.i, endOfPassWriteIndex: PROF.i + 1 } }; PROF.tags.push(pipeline._name + (Array.isArray(groups) ? ":lm" : "")); PROF.i += 2; } + const p = enc.beginComputePass(desc); + p.setPipeline(pipeline); + p.setBindGroup(0, bg); + if (Array.isArray(groups)) p.dispatchWorkgroups(groups[0], groups[1]); else p.dispatchWorkgroups(groups); + p.end(); + }; + // N output rows → 2D grid (x ≤ 65535, the WebGPU per-dimension limit); the + // kernel reconstructs n = y*65535 + x. Needed for Qwen's 151 k vocab lm_head. + const grid = (N) => N > 65535 ? [65535, Math.ceil(N / 65535)] : N; + // native-2-bit: rotate the matmul input into `rot` (FWHT over Kp), then the 2-bit GEMV reads `rot`. + let rot = null; const signBufs = new Map(), fwhtUnis = new Map(); + const wgN = (n) => Math.ceil(n / 256); + const fwUni = (key, arr) => { let u = fwhtUnis.get(key); if (!u) { u = ubuf(arr); fwhtUnis.set(key, u); } return u; }; + const fwhtRotate = (enc, xb, K, Kp) => { // x′ = FWHT(sign ⊙ pad(x)) → rot, in place over Kp + if (!rot) rot = sbuf(maxKp); + let sg = signBufs.get(Kp); if (!sg) { const a = signsFor(Kp); sg = dev.createBuffer({ size: a.byteLength, usage: U.STORAGE | U.COPY_DST }); dev.queue.writeBuffer(sg, 0, a); signBufs.set(Kp, sg); } + pass(enc, P_fwL, [xb, sg, rot, fwUni("L" + K + "_" + Kp, new Uint32Array([K, Kp, 0, 0]))], wgN(Kp)); + for (let len = 1; len < Kp; len <<= 1) pass(enc, P_fwB, [rot, fwUni("B" + Kp + "_" + len, new Uint32Array([Kp, len, 0, 0]))], wgN(Kp >> 1)); + pass(enc, P_fwN, [rot, fwUni("N" + Kp, new Uint32Array([Kp, 0, 0, 0]))], wgN(Kp)); + return rot; + }; + const t2big = (ws) => ws.N * ws.K >= T2BIG; // geometry pick: fat workgroups for big tensors + const mmW = (enc, xb, ws, ob) => ws.t2 + ? (t2big(ws) + ? pass(enc, P_mmT2B, [xb, ws.qbuf, ob, ws.uni], grid(Math.ceil(ws.N / 16))) + : pass(enc, P_mmT2, [xb, ws.qbuf, ob, ws.uni], grid(Math.ceil(ws.N / 4)))) + : ws.t2r + ? (t2big(ws) + ? pass(enc, P_mmT2RB, [xb, ws.qbuf, ws.sbuf, ob, ws.uni], grid(Math.ceil(ws.N / 16))) + : pass(enc, P_mmT2R, [xb, ws.qbuf, ws.sbuf, ob, ws.uni], grid(Math.ceil(ws.N / 4)))) + : ws.e8 + ? pass(enc, P_mmE8, [xb, ws.qbuf, ws.sbuf, lutBuf, ob, ws.uni], grid(ws.N)) + : (P_mmQ3B && !rotate && ws.N * ws.K >= T2BIG) + ? pass(enc, P_mmQ3B, [xb, ws.qbuf, ws.sbuf, ob, ws.uni], grid(Math.ceil(ws.N / 4))) + : pass(enc, P_mm, [rotate ? fwhtRotate(enc, xb, ws.K, ws.Kp) : xb, ws.qbuf, ws.sbuf, ob, ws.uni], grid(ws.N)); + const mmAddW = (enc, xb, ws, r, ob) => ws.t2 + ? (t2big(ws) + ? pass(enc, P_mmT2Badd, [xb, ws.qbuf, r, ob, ws.uni], grid(Math.ceil(ws.N / 16))) + : pass(enc, P_mmT2add, [xb, ws.qbuf, r, ob, ws.uni], grid(Math.ceil(ws.N / 4)))) + : ws.t2r + ? (t2big(ws) + ? pass(enc, P_mmT2RBadd, [xb, ws.qbuf, ws.sbuf, r, ob, ws.uni], grid(Math.ceil(ws.N / 16))) + : pass(enc, P_mmT2Radd, [xb, ws.qbuf, ws.sbuf, r, ob, ws.uni], grid(Math.ceil(ws.N / 4)))) + : ws.e8 + ? pass(enc, P_mmE8add, [xb, ws.qbuf, ws.sbuf, lutBuf, r, ob, ws.uni], grid(ws.N)) + : pass(enc, P_mmadd, [rotate ? fwhtRotate(enc, xb, ws.K, ws.Kp) : xb, ws.qbuf, ws.sbuf, r, ob, ws.uni], grid(ws.N)); + const rms = (enc, xb, gname, ob, u = uDim) => pass(enc, P_rms, [xb, Nrm[gname], ob, u], 1); + + // one transformer layer's passes; `ws(role)` returns the role's weight-set + // (resident = W[`l.role`], stream = the reusable R[role]). Returns B.cur. + // `up` overrides the position-dependent bindings for batched decode: {ropeQ, ropeK, attn, pos}. + function layerBody(enc, l, cur, ws, up = null) { + const _uRopeQ = up ? up.ropeQ : uRopeQ, _uRopeK = up ? up.ropeK : uRopeK, _uAttn = up ? up.attn : uAttn, _pos = up ? up.pos : pos; + return layerBodyU(enc, l, cur, ws, _uRopeQ, _uRopeK, _uAttn, _pos); + } + function layerBodyU(enc, l, cur, ws, uRopeQ, uRopeK, uAttn, pos) { + const F = fusedT2 ? T2F[l] : null; + if (F) { // fused ternary layer: 10 passes instead of 15 + rms(enc, cur, `l${l}.attn_norm`, B.normed); + pass(enc, P_t2f, [B.normed, ws("wq").qbuf, ws("wk").qbuf, ws("wv").qbuf, B.qkv, F.qkvP, F.qkvS], grid(Math.ceil((q_dim + 2 * kv_dim) / 4))); + pass(enc, P_rope2, [qR, kR, uRopeQ], Math.ceil((n_heads + n_kv_heads) * (hd / 2) / 64)); // uRopeQ.w carries nkv + if (kv4 && l > 0) { + pass(enc, P_kvq, [kR, kcache[l], uAttn], 1); // quantize+pack K/V rows (uAttn.w = pos on every path) + pass(enc, P_kvq, [vR, vcache[l], uAttn], 1); + pass(enc, P_attnQ, [qR, kcache[l], vcache[l], B.attn, uAttn], n_heads); + } else { + enc.copyBufferToBuffer(B.qkv, q_dim * 4, kcache[l], pos * kv_dim * 4, kv_dim * 4); + enc.copyBufferToBuffer(B.qkv, (q_dim + kv_dim) * 4, vcache[l], pos * kv_dim * 4, kv_dim * 4); + pass(enc, P_attn, [qR, kcache[l], vcache[l], B.attn, uAttn], n_heads); + } + let attnO = B.attn; + if (subNorm) { rms(enc, B.attn, `l${l}.attn_sub_norm`, B.attn2, uQd); attnO = B.attn2; } + mmAddW(enc, attnO, ws("wo"), cur, B.h); + rms(enc, B.h, `l${l}.ffn_norm`, B.normed2); + pass(enc, 2 * ff * d >= T2BIG ? P_t2fB : P_t2f, [B.normed2, ws("w_gate").qbuf, ws("w_up").qbuf, ws("w_up").qbuf, B.gu, F.guP, F.guS], grid(Math.ceil(2 * ff / (2 * ff * d >= T2BIG ? 16 : 4)))); + if (subNorm) { + pass(enc, P_actn, [B.gu, Nrm[`l${l}.ffn_sub_norm`], B.hid2, uFF], 1); + mmAddW(enc, B.hid2, ws("w_down"), B.h, B.cur); + } else { + pass(enc, P_t2ad, [B.gu, ws("w_down").qbuf, B.h, B.cur, F.dP], grid(Math.ceil(d / 4))); + } + return B.cur; + } + rms(enc, cur, `l${l}.attn_norm`, B.normed); + if (_calib) enc.copyBufferToBuffer(B.normed, 0, snapBuf, l * d * 4, d * 4); // calibration: snapshot this layer's attention input (for LDLQ Hessian) + if (attnBias) { + mmAddW(enc, B.normed, ws("wq"), Nrm[`l${l}.bq`], B.q); + mmAddW(enc, B.normed, ws("wk"), Nrm[`l${l}.bk`], B.k); + mmAddW(enc, B.normed, ws("wv"), Nrm[`l${l}.bv`], B.v); + } else { + let qkvIn = B.normed; + if (bitlinear) { rms(enc, B.normed, "__unit_d", B.attn_out); qkvIn = B.attn_out; } // BitLinear: weightless norm into q/k/v + mmW(enc, qkvIn, ws("wq"), B.q); + mmW(enc, qkvIn, ws("wk"), B.k); + mmW(enc, qkvIn, ws("wv"), B.v); + } + if (qkNorm) { + pass(enc, P_qkn, [B.q, Nrm[`l${l}.q_norm`], uQkn], n_heads); + pass(enc, P_qkn, [B.k, Nrm[`l${l}.k_norm`], uQkn], n_kv_heads); + } + pass(enc, P_rope, [B.q, uRopeQ], Math.ceil(n_heads * (hd / 2) / 64)); + pass(enc, P_rope, [B.k, uRopeK], Math.ceil(n_kv_heads * (hd / 2) / 64)); + if (kv4 && l > 0) { + pass(enc, P_kvq, [B.k, kcache[l], uAttn], 1); // quantize+pack K/V rows (uAttn.w = pos on every path) + pass(enc, P_kvq, [B.v, vcache[l], uAttn], 1); + pass(enc, P_attnQ, [B.q, kcache[l], vcache[l], B.attn, uAttn], n_heads); + } else { + enc.copyBufferToBuffer(B.k, 0, kcache[l], pos * kv_dim * 4, kv_dim * 4); + enc.copyBufferToBuffer(B.v, 0, vcache[l], pos * kv_dim * 4, kv_dim * 4); + pass(enc, P_attn, [B.q, kcache[l], vcache[l], B.attn, uAttn], n_heads); + } + let attnO = B.attn; + if (subNorm) { rms(enc, B.attn, `l${l}.attn_sub_norm`, B.attn2, uQd); attnO = B.attn2; } // BitNet: norm BEFORE the o-projection + else if (bitlinear) { rms(enc, B.attn, "__unit_qd", B.attn2, uQd); attnO = B.attn2; } // BitLinear: weightless norm into wo + mmAddW(enc, attnO, ws("wo"), cur, B.h); // h = attn·Wo + residual + rms(enc, B.h, `l${l}.ffn_norm`, B.normed2); + if (_calib) enc.copyBufferToBuffer(B.normed2, 0, snapBuf, (n_layers + l) * d * 4, d * 4); // calibration: snapshot this layer's MLP input (for LDLQ) + let guIn = B.normed2; + if (bitlinear) { rms(enc, B.normed2, "__unit_d", B.mlp); guIn = B.mlp; } // BitLinear: weightless norm into gate/up + mmW(enc, guIn, ws("w_gate"), B.gate); + mmW(enc, guIn, ws("w_up"), B.up); + pass(enc, P_sm, [B.gate, B.up, B.hid, uFF], Math.ceil(ff / 64)); + let hidO = B.hid; + if (subNorm) { rms(enc, B.hid, `l${l}.ffn_sub_norm`, B.hid2, uFF); hidO = B.hid2; } // BitNet: norm BEFORE the down-projection + else if (bitlinear) { rms(enc, B.hid, "__unit_ff", B.hid2, uFF); hidO = B.hid2; } // BitLinear: weightless norm into w_down + mmAddW(enc, hidO, ws("w_down"), B.h, B.cur); // cur = mlp·Wdown + residual + return B.cur; + } + + let pos = 0, cached = [], lastLogits = null, timing = null; + + // ── streamed-layer cache + prefetch pipeline (opfs / remote) ── + // The layer access pattern is fully predictable (0..n_layers every token), so we + // prefetch ahead and overlap the storage→host read with GPU compute of earlier + // layers. A bounded RAM cache keeps as many layers resident as the budget allows + // (a model ≤ budget runs warm with zero re-fetch; a bigger model streams the tail, + // RAM stays bounded). cacheBudget = bytes; 0 = no cache (pure stream). + const layerCache = new Map(); // layer → packed Uint8Array + const inflight = new Map(); // layer → Promise (dedupe concurrent reads) + let cacheBytes = 0; + const PREFETCH = 4; // read-ahead depth (concurrent reads in flight) + // Effective budget is at least the prefetch window, so prefetched buffers survive + // until consumed even in pure-stream (budget 0) mode. A larger cacheBudget keeps + // more layers warm across tokens (a model ≤ budget runs with no re-fetch). + const effBudget = () => Math.max(cacheBudget, (PREFETCH + 2) * packStride); + const getLayer = (l) => { + if (layerCache.has(l)) return Promise.resolve(layerCache.get(l)); + if (inflight.has(l)) return inflight.get(l); + const p = Promise.resolve(opfsStore.read(l * packStride, packStride)).then((buf) => { + inflight.delete(l); layerCache.set(l, buf); cacheBytes += buf.byteLength; + return buf; + }); + inflight.set(l, p); + return p; + }; + const evictBelow = (l) => { // drop already-consumed layers once over budget + const b = effBudget(); + if (cacheBytes <= b) return; + for (const k of layerCache.keys()) { + if (cacheBytes <= b) break; + if (k < l) { cacheBytes -= layerCache.get(k).byteLength; layerCache.delete(k); } + } + }; + + // Atlas-Probe extension (additive, off by default): when _capHidden is an array, each step + // reads back B.normed — the final-layer hidden state (post final_norm, the vector fed to lm_head), + // the model's contextual representation of the token — for fingerprinting. Inert during decode. + let _capHidden = null, hidStaging = null, _calib = null, snapBuf = null, snapStg = null; + async function step(token, noRead = false) { // noRead: leave logits ON the GPU (decode()'s fast path; resident-dense only) + const tE = performance.now(); + if (typeof window !== "undefined" && window.__profile) { profInit(); if (PROF) { PROF.active = true; PROF.i = 0; PROF.tags = []; } } + else if (PROF) PROF.active = false; + // embed lookup (CPU, per-block dequant) → x + const x = new Float32Array(d); + const o = token * d, sb = token * (d / 32); + if (bits === 4) { + for (let i = 0; i < d; i++) { const gg = o + i; const nib = (embedQ[gg >> 1] >> ((gg & 1) * 4)) & 0xf; x[i] = (nib - 8) * embedS[sb + (i >> 5)]; } + } else if (bits === 3 && q3f) { // Q3 FIELD embed lookup (mirrors the q3f kernel unpack) + const eq32 = new Uint32Array(embedQ.buffer, embedQ.byteOffset, embedQ.byteLength >> 2), bb = token * (d / 32); + for (let i = 0; i < d; i++) { + const bp = (bb + (i >> 5)) * 3, j = i & 31; let q; + if (j < 10) q = (eq32[bp] >>> (j * 3)) & 7; + else if (j < 20) q = (eq32[bp + 1] >>> ((j - 10) * 3)) & 7; + else if (j < 30) q = (eq32[bp + 2] >>> ((j - 20) * 3)) & 7; + else { const sp = (eq32[bp] >>> 30) | ((eq32[bp + 1] >>> 30) << 2) | ((eq32[bp + 2] >>> 30) << 4); q = j === 30 ? sp & 7 : (sp >>> 3) & 7; } + x[i] = (q - 3) * embedS[sb + (i >> 5)]; + } + } else if (bits === 3) { // Q3 bit-plane embed lookup + const eq32 = new Uint32Array(embedQ.buffer, embedQ.byteOffset, embedQ.byteLength >> 2), bb = token * (d / 32); + for (let i = 0; i < d; i++) { const bp = (bb + (i >> 5)) * 3, j = i & 31, q = ((eq32[bp] >>> j) & 1) | (((eq32[bp + 1] >>> j) & 1) << 1) | (((eq32[bp + 2] >>> j) & 1) << 2); x[i] = (q - 3) * embedS[sb + (i >> 5)]; } + } else { + for (let i = 0; i < d; i++) x[i] = s8(embedQ[o + i]) * embedS[sb + (i >> 5)]; + } + dev.queue.writeBuffer(B.x, 0, x); + dev.queue.writeBuffer(uRopeQ, 0, new Uint32Array([n_heads, hd, pos, n_kv_heads])); // .w = nkv (read only by fused ROPE2) + dev.queue.writeBuffer(uRopeK, 0, new Uint32Array([n_kv_heads, hd, pos, 0])); + dev.queue.writeBuffer(uAttn, 0, new Uint32Array([n_heads, n_kv_heads, hd, pos])); + + const tS = performance.now(); + let cur = B.x; + if (moe) { + // MoE forward: attention (packed, prefetched) + per-token expert subset. Only + // the n_used experts the router picks are streamed/uploaded → bytes/token ≈ + // n_used/n_experts of the FFN, so a huge MoE costs ~its active params/token. + if (opfs) for (let l = 0; l < Math.min(PREFETCH, n_layers); l++) getLayer(l); + for (let l = 0; l < n_layers; l++) { + if (opfs) { const buf = await getLayer(l); if (l + PREFETCH < n_layers) getLayer(l + PREFETCH); dev.queue.writeBuffer(RQ, 0, buf.subarray(0, packQbytes)); dev.queue.writeBuffer(RS, 0, buf.subarray(packQbytes)); evictBelow(l); } + else { dev.queue.writeBuffer(RQ, 0, Wb[l].q); dev.queue.writeBuffer(RS, 0, Wb[l].s); } + const R_ = (role) => R[role]; + // attention (+ residual into B.h), then ffn_norm → B.normed2 + let enc = dev.createCommandEncoder(); + rms(enc, cur, `l${l}.attn_norm`, B.normed); + mmW(enc, B.normed, R_("wq"), B.q); mmW(enc, B.normed, R_("wk"), B.k); mmW(enc, B.normed, R_("wv"), B.v); + const DBG = (typeof window !== "undefined") ? window : {}; + // OLMoE: full-vector q/k RMSNorm. NOT in place (B.normed is free after the q/k/v + // matmuls) — an in-place storage read-write RMS misbehaves on this driver. + if (qkFull && !DBG.__skipQKN) { + rms(enc, B.q, `l${l}.q_norm`, B.normed); enc.copyBufferToBuffer(B.normed, 0, B.q, 0, d * 4); + rms(enc, B.k, `l${l}.k_norm`, B.normed); enc.copyBufferToBuffer(B.normed, 0, B.k, 0, kv_dim * 4); + } + else if (qkNorm) { pass(enc, P_qkn, [B.q, Nrm[`l${l}.q_norm`], uQkn], n_heads); pass(enc, P_qkn, [B.k, Nrm[`l${l}.k_norm`], uQkn], n_kv_heads); } + pass(enc, P_rope, [B.q, uRopeQ], Math.ceil(n_heads * (hd / 2) / 64)); + pass(enc, P_rope, [B.k, uRopeK], Math.ceil(n_kv_heads * (hd / 2) / 64)); + enc.copyBufferToBuffer(B.k, 0, kcache[l], pos * kv_dim * 4, kv_dim * 4); + enc.copyBufferToBuffer(B.v, 0, vcache[l], pos * kv_dim * 4, kv_dim * 4); + pass(enc, P_attn, [B.q, kcache[l], vcache[l], B.attn, uAttn], n_heads); + mmAddW(enc, B.attn, R_("wo"), cur, B.h); // h = attn·Wo + residual + rms(enc, B.h, `l${l}.ffn_norm`, B.normed2); + enc.copyBufferToBuffer(B.normed2, 0, stagingD, 0, d * 4); + dev.queue.submit([enc.finish()]); + // router (CPU): top-k experts off the small d→n_experts matmul, softmax weights + await stagingD.mapAsync(GPUMapMode.READ); + const nf = new Float32Array(stagingD.getMappedRange().slice(0)); stagingD.unmap(); + const rw = routerCPU[l], rl = new Float32Array(nExp); + for (let e = 0; e < nExp; e++) { let s = 0; const base = e * d; for (let j = 0; j < d; j++) s += nf[j] * rw[base + j]; rl[e] = s; } + const idx = Array.from({ length: nExp }, (_, i) => i).sort((a, b) => rl[b] - rl[a]).slice(0, nUsed); + if (DBG.__expLog) for (const e of idx) DBG.__expLog.push(l * nExp + e); // (layer,expert) activations, in order — reuse analysis + // OLMoE routing: softmax over ALL experts, take the selected probs WITHOUT renormalizing + // (norm_topk_prob=false). Renormalizing over the top-k (the old code) made weights sum to 1 + // — ~2-3× too large vs the true softmax-over-64 mass — and blew up the residual → garbage. + // moe.normTopk (Qwen3-MoE = true) re-enables the top-k renorm. + let mx = -1e30; for (let e = 0; e < nExp; e++) if (rl[e] > mx) mx = rl[e]; + let denAll = 0; for (let e = 0; e < nExp; e++) denAll += Math.exp(rl[e] - mx); + const wt = new Map(); for (const e of idx) wt.set(e, Math.exp(rl[e] - mx) / denAll); + if (manifest.moe.normTopk) { let s = 0; for (const e of idx) s += wt.get(e); for (const e of idx) wt.set(e, wt.get(e) / s); } + // accumulate experts into B.cur (init = residual h) + const encI = dev.createCommandEncoder(); encI.copyBufferToBuffer(B.h, 0, B.cur, 0, d * 4); dev.queue.submit([encI.finish()]); + if (DBG.__skipExperts) { cur = B.cur; continue; } // debug: residual only (no FFN) + if (residentExperts) { + // BATCHED: ensure the chosen experts are VRAM-resident (lazy, once), then the WHOLE FFN for + // all nUsed experts runs in 4 dispatches (gate, up, silu·mul, down+Σ+residual) — vs 5·nUsed. + for (const e of idx) { await ensureVram(l, e, "gate"); await ensureVram(l, e, "up"); await ensureVram(l, e, "down"); } + const idxArr = new Uint32Array(8), wArr = new Float32Array(8); + for (let s = 0; s < idx.length; s++) { idxArr[s] = idx[s]; wArr[s] = wt.get(idx[s]); } + dev.queue.writeBuffer(uIdx, 0, idxArr); dev.queue.writeBuffer(uWts, 0, wArr); + const V = ExpVRAM[l]; + const enc3 = dev.createCommandEncoder(); + pass(enc3, P_moeGU, [B.normed2, V.gate.q, V.gate.s, B.gate8, uMoeGU, uIdx], grid(nUsed * ff)); // all experts' gate + pass(enc3, P_moeGU, [B.normed2, V.up.q, V.up.s, B.up8, uMoeGU, uIdx], grid(nUsed * ff)); // all experts' up + pass(enc3, P_sm, [B.gate8, B.up8, B.hid8, uFFb], Math.ceil(nUsed * ff / 64)); // silu(gate)·up + pass(enc3, P_moeDn, [B.hid8, V.down.q, V.down.s, B.h, B.cur, uMoeDnB, uIdx, uWts], grid(d)); // Σ_s w_s·down_s + residual + dev.queue.submit([enc3.finish()]); + } else { + for (const e of idx) { readExpert(l, e, "gate"); readExpert(l, e, "up"); readExpert(l, e, "down"); } // prefetch the chosen experts + for (const e of idx) { + const [g, u, dn] = await Promise.all([readExpert(l, e, "gate"), readExpert(l, e, "up"), readExpert(l, e, "down")]); + dev.queue.writeBuffer(ExpQ.gate, 0, g.q); dev.queue.writeBuffer(ExpS.gate, 0, g.s); + dev.queue.writeBuffer(ExpQ.up, 0, u.q); dev.queue.writeBuffer(ExpS.up, 0, u.s); + dev.queue.writeBuffer(ExpQ.down, 0, dn.q); dev.queue.writeBuffer(ExpS.down, 0, dn.s); + dev.queue.writeBuffer(uAxpy, 0, new Uint32Array([d, fbits(wt.get(e)), 0, 0])); + const enc3 = dev.createCommandEncoder(); + mmW(enc3, B.normed2, { qbuf: ExpQ.gate, sbuf: ExpS.gate, uni: uMoeIn, N: ff }, B.gate); + mmW(enc3, B.normed2, { qbuf: ExpQ.up, sbuf: ExpS.up, uni: uMoeIn, N: ff }, B.up); + pass(enc3, P_sm, [B.gate, B.up, B.hid, uFF], Math.ceil(ff / 64)); + mmW(enc3, B.hid, { qbuf: ExpQ.down, sbuf: ExpS.down, uni: uMoeDn, N: d }, B.mlp); + pass(enc3, P_axpy, [B.cur, B.mlp, uAxpy], Math.ceil(d / 64)); // B.cur += w_e · expert_out + dev.queue.submit([enc3.finish()]); + } + } + cur = B.cur; + } + const encF = dev.createCommandEncoder(); + rms(encF, cur, "final_norm", B.normed); + mmW(encF, B.normed, W["lm_head"], B.logits); + encF.copyBufferToBuffer(B.logits, 0, staging, 0, vocab * 4); + dev.queue.submit([encF.finish()]); + } else if (frameGran) { + // Play the model: page ONE matrix into the frame buffer, run its matmul, + // submit, repeat. A weight-matmul's submit must flush before the next frame + // overwrites the buffer (queue order makes the reuse safe). Non-weight ops + // (norm/rope/attn/silu) ride along in the current encoder. + let enc = dev.createCommandEncoder(); + // play a frame: flush the encoder (so reuse is safe), then load the matrix + // from JS (frame) or DISK (opfs) into the frame buffer. opfs read is async. + const play = async (name) => { + dev.queue.submit([enc.finish()]); enc = dev.createCommandEncoder(); + if (opfs) { const m = frameMan[name], buf = await opfsStore.read(m.off, m.qlen + m.slen); dev.queue.writeBuffer(RM, 0, buf.subarray(0, m.qlen)); dev.queue.writeBuffer(RMscale, 0, buf.subarray(m.qlen)); dev.queue.writeBuffer(uFr, 0, new Uint32Array([m.K, m.N, m.K / 32, 0])); return { qbuf: RM, sbuf: RMscale, uni: uFr, N: m.N }; } + const p = FB[name]; dev.queue.writeBuffer(RM, 0, p.q); dev.queue.writeBuffer(RMscale, 0, p.s); return { qbuf: RM, sbuf: RMscale, uni: p.uni, N: p.N }; + }; + for (let l = 0; l < n_layers; l++) { + rms(enc, cur, `l${l}.attn_norm`, B.normed); + let w = await play(`l${l}.wq`); if (attnBias) mmAddW(enc, B.normed, w, Nrm[`l${l}.bq`], B.q); else mmW(enc, B.normed, w, B.q); + if (qkNorm) pass(enc, P_qkn, [B.q, Nrm[`l${l}.q_norm`], uQkn], n_heads); + pass(enc, P_rope, [B.q, uRopeQ], Math.ceil(n_heads * (hd / 2) / 64)); + w = await play(`l${l}.wk`); if (attnBias) mmAddW(enc, B.normed, w, Nrm[`l${l}.bk`], B.k); else mmW(enc, B.normed, w, B.k); + if (qkNorm) pass(enc, P_qkn, [B.k, Nrm[`l${l}.k_norm`], uQkn], n_kv_heads); + pass(enc, P_rope, [B.k, uRopeK], Math.ceil(n_kv_heads * (hd / 2) / 64)); + enc.copyBufferToBuffer(B.k, 0, kcache[l], pos * kv_dim * 4, kv_dim * 4); + w = await play(`l${l}.wv`); if (attnBias) mmAddW(enc, B.normed, w, Nrm[`l${l}.bv`], B.v); else mmW(enc, B.normed, w, B.v); + enc.copyBufferToBuffer(B.v, 0, vcache[l], pos * kv_dim * 4, kv_dim * 4); + pass(enc, P_attn, [B.q, kcache[l], vcache[l], B.attn, uAttn], n_heads); + w = await play(`l${l}.wo`); mmAddW(enc, B.attn, w, cur, B.h); + rms(enc, B.h, `l${l}.ffn_norm`, B.normed2); + w = await play(`l${l}.w_gate`); mmW(enc, B.normed2, w, B.gate); + w = await play(`l${l}.w_up`); mmW(enc, B.normed2, w, B.up); + pass(enc, P_sm, [B.gate, B.up, B.hid, uFF], Math.ceil(ff / 64)); + w = await play(`l${l}.w_down`); mmAddW(enc, B.hid, w, B.h, B.cur); + cur = B.cur; + } + rms(enc, cur, "final_norm", B.normed); + // PLAY the lm_head in row-tiles through the same frame buffer — not resident. + const lm = opfs ? null : FB["lm_head"], lmM = frameMan["lm_head"]; + const rowQ = bits === 4 ? d / 2 : d, rowS = (d / 32) * 4, T = Math.floor(RM.size / rowQ); + for (let v0 = 0; v0 < vocab; v0 += T) { + const rows = Math.min(T, vocab - v0); + dev.queue.submit([enc.finish()]); enc = dev.createCommandEncoder(); + if (opfs) { + dev.queue.writeBuffer(RM, 0, await opfsStore.read(lmM.off + v0 * rowQ, rows * rowQ)); + dev.queue.writeBuffer(RMscale, 0, await opfsStore.read(lmM.off + lmM.qlen + v0 * rowS, rows * rowS)); + } else { + dev.queue.writeBuffer(RM, 0, lm.q.subarray(v0 * rowQ, (v0 + rows) * rowQ)); + dev.queue.writeBuffer(RMscale, 0, new Uint8Array(lm.s.buffer, lm.s.byteOffset + v0 * rowS, rows * rowS)); + } + dev.queue.writeBuffer(uLm, 0, new Uint32Array([d, rows, d / 32, v0])); + pass(enc, P_mm, [B.normed, RM, RMscale, B.logits, uLm], grid(rows)); + } + enc.copyBufferToBuffer(B.logits, 0, staging, 0, vocab * 4); + dev.queue.submit([enc.finish()]); + } else if (stream) { + // page each layer's matrices into the reusable buffers, run that layer, + // submit, repeat — only one layer is GPU-resident at a time. Buffer reuse + // is safe: the queue runs writeBuffer(L+1) only after submit(L) finishes. + // Source is JS heap ("layer") or DISK ("opfs"/"remote"). For disk sources we + // PREFETCH a window of layers ahead so the storage→host read overlaps the GPU + // compute of earlier layers (the access pattern is fully predictable), and a + // bounded RAM cache serves warm layers without re-fetching. + if (opfs) for (let l = 0; l < Math.min(PREFETCH, n_layers); l++) getLayer(l); + for (let l = 0; l < n_layers; l++) { + if (opfs) { // page this layer's packed blob off DISK (prefetched) + const buf = await getLayer(l); + if (l + PREFETCH < n_layers) getLayer(l + PREFETCH); // keep the window full + dev.queue.writeBuffer(RQ, 0, buf.subarray(0, packQbytes)); + dev.queue.writeBuffer(RS, 0, buf.subarray(packQbytes)); + evictBelow(l); + } else { + dev.queue.writeBuffer(RQ, 0, Wb[l].q); // one fat DMA for the whole layer's matrices + dev.queue.writeBuffer(RS, 0, Wb[l].s); // + one for all its scales + } + // t2: this layer's per-role scalar scales into the reused uniforms (matmul reads scale from uni.w). + if (t2stream) for (const role of ROLES) { const L = packLayout[role]; dev.queue.writeBuffer(R[role].uni, 0, new Uint32Array([L.K, L.N, L.K / 16, t2scales[l][role]])); } + const enc = dev.createCommandEncoder(); + cur = layerBody(enc, l, cur, (role) => R[role]); + dev.queue.submit([enc.finish()]); + } + const encF = dev.createCommandEncoder(); + rms(encF, cur, "final_norm", B.normed); + mmW(encF, B.normed, W["lm_head"], B.logits); + encF.copyBufferToBuffer(B.logits, 0, staging, 0, vocab * 4); + dev.queue.submit([encF.finish()]); + } else { + const enc = dev.createCommandEncoder(); + for (let l = 0; l < n_layers; l++) cur = layerBody(enc, l, cur, (role) => W[`l${l}.${role}`]); + rms(enc, cur, "final_norm", B.normed); + mmW(enc, B.normed, W["lm_head"], B.logits); + if (!noRead) enc.copyBufferToBuffer(B.logits, 0, staging, 0, vocab * 4); + dev.queue.submit([enc.finish()]); + } + if (noRead) { timing = { encode: tS - tE, exec: performance.now() - tS }; pos++; return null; } + await staging.mapAsync(GPUMapMode.READ); + const logits = new Float32Array(staging.getMappedRange().slice(0)); + staging.unmap(); + if (PROF && PROF.active && PROF.i > 0) { // resolve + aggregate per-pipeline GPU ns → window.__profileData + const e = dev.createCommandEncoder(); e.resolveQuerySet(PROF.qs, 0, PROF.i, PROF.buf, 0); e.copyBufferToBuffer(PROF.buf, 0, PROF.stg, 0, PROF.i * 8); dev.queue.submit([e.finish()]); + await PROF.stg.mapAsync(GPUMapMode.READ); + const ts = new BigInt64Array(PROF.stg.getMappedRange().slice(0)); PROF.stg.unmap(); + const agg = {}; let sum = 0, t0 = ts[0], t1 = ts[PROF.i - 1]; + for (let k = 0; k < PROF.i; k += 2) { const ms = Number(ts[k + 1] - ts[k]) / 1e6; const tg = PROF.tags[k >> 1]; (agg[tg] = agg[tg] || { ms: 0, n: 0 }).ms += ms; agg[tg].n++; sum += ms; } + window.__profileData = { passes: agg, passSumMs: sum, gpuSpanMs: Number(t1 - t0) / 1e6, nPasses: PROF.i >> 1 }; + } + if (_capHidden) { // Atlas-Probe: read back the final hidden state for this token + hidStaging = hidStaging || dev.createBuffer({ size: d * 4, usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ }); + const e2 = dev.createCommandEncoder(); e2.copyBufferToBuffer(B.normed, 0, hidStaging, 0, d * 4); dev.queue.submit([e2.finish()]); + await hidStaging.mapAsync(GPUMapMode.READ); _capHidden.push(Array.from(new Float32Array(hidStaging.getMappedRange().slice(0)))); hidStaging.unmap(); + } + timing = { encode: tS - tE, exec: performance.now() - tS }; + pos++; + return logits; + } + + function reset() { pos = 0; cached = []; lastLogits = null; } + // KV-COMMONS prefix pin: rewind the decode cursor to a pinned prefix length L, KEEPING the + // GPU-resident K/V for positions [0..L) intact (byte-identical to what re-prefilling that prefix + // would produce — same tokens, same positions, deterministic). The next sync/decode call sees + // cached==prefix and steps ONLY the new tokens, so a shared prefix (system prompt, RAG context, + // few-shot block) is prefilled ONCE and reused across turns/conversations instead of being nuked + // by sync()'s divergence-reset. This is the in-session special case of a durable KV commons: the + // restore-from-bytes path (dumpKV → holo-kappa-v2, keyed by (weightsκ,tokenizerκ,prefixκ)) reduces + // to this once the bytes are resident. No-op guards keep it safe: L is clamped to [0, cached.length]. + function truncateTo(L) { L = Math.max(0, Math.min(L | 0, cached.length)); cached.length = L; pos = L; lastLogits = null; return L; } + + // dev probe: read back a layer's KV cache rows (+ the current B.q) for offline quantization + // experiments (the lattice-coded-KV gate). Read-only; no effect on inference state. + async function dumpKV(l, rows) { + const n = Math.min(rows || pos, pos), bytes = n * kv_dim * 4; + const stg = dev.createBuffer({ size: bytes * 2 + q_dim * 4, usage: U.MAP_READ | U.COPY_DST }); + const e = dev.createCommandEncoder(); + e.copyBufferToBuffer(kcache[l], 0, stg, 0, bytes); + e.copyBufferToBuffer(vcache[l], 0, stg, bytes, bytes); + e.copyBufferToBuffer(B.q, 0, stg, bytes * 2, q_dim * 4); + dev.queue.submit([e.finish()]); + await stg.mapAsync(GPUMapMode.READ); + const all = new Float32Array(stg.getMappedRange().slice(0)); stg.unmap(); stg.destroy(); + return { k: all.slice(0, n * kv_dim), v: all.slice(n * kv_dim, 2 * n * kv_dim), q: all.slice(2 * n * kv_dim), n, kv_dim, n_heads, n_kv_heads, hd }; + } + + async function sync(tokens, forDecode = false) { + let p = 0; + while (p < cached.length && p < tokens.length && cached[p] === tokens[p]) p++; + if (p < cached.length) { reset(); p = 0; } + // raw-logits callers need a re-derive when decode() ended without CPU logits; decode() itself + // recomputes logits on-GPU from the ring — skipping this guard kills a QUADRATIC re-prefill + // in chunked decode loops (measured 151 → ~16 ms/tok). + if (!forDecode && p === tokens.length && lastLogits === null && tokens.length > 0) { reset(); p = 0; } + for (let i = p; i < tokens.length; i++) { lastLogits = await step(tokens[i]); cached.push(tokens[i]); } + } + + function argmax(a) { let bi = 0; for (let i = 1; i < a.length; i++) if (a[i] > a[bi]) bi = i; return bi; } + + // Greedy decode with a repetition penalty (deterministic → a teleported mind + // still continues identically). Penalizing recently-seen tokens breaks the + // greedy loops a small base model otherwise falls into. + async function generate(prompt, maxNew, repPenalty = 1.3) { + await sync(prompt); + let logits = lastLogits, seq = prompt.slice(); + for (let n = 0; n < maxNew; n++) { + if (seq.length >= cap) break; + const pen = logits.slice(); // keep cached logits raw + for (const id of new Set(seq.slice(Math.max(0, seq.length - 64)))) pen[id] = Math.fround(pen[id] > 0 ? pen[id] / repPenalty : pen[id] * repPenalty); // f32 like the GPU head — heads must tie-break identically + const next = argmax(pen); + if (next === eos) break; // EOS → stop + seq.push(next); + logits = await step(next); + lastLogits = logits; + cached.push(next); + } + return seq; + } + + // BATCHED GPU greedy decode: the whole [penalty → argmax → append → embed → forward] chain runs + // ON the GPU for BATCH tokens per submit — the token ids live in a GPU seq-ring, the embed lookup + // is a kernel, and per-position uniforms are prewritten (positions are deterministic). ONE fence + // per BATCH tokens instead of per token: the measured ~20 ms/token submit/fence tax drops ~6×. + // Greedy semantics match generate() exactly (same penalty formula over the last-64 unique window + // — PENALTY2 dedups by first occurrence — and the same first-max argmax tie-break). + let DEC = null; + const BATCH = 6; // measured optimum: 16 was SLOWER (JS re-encode burst per token beats fence savings) + async function decode(prompt, maxNew, repPenalty = 1.3) { + if (stream || frameGran || moe || (bits === 3 && !q3f)) return generate(prompt, maxNew, repPenalty); // GPU-embed variants cover q3f/q4/q8 + if (!DEC) { + const mk = (n) => Array.from({ length: BATCH }, () => ubuf(new Uint32Array(4))); + const eqB = dev.createBuffer({ size: embedQ.byteLength, usage: U.STORAGE | U.COPY_DST }); + dev.queue.writeBuffer(eqB, 0, embedQ.buffer, embedQ.byteOffset, embedQ.byteLength); + const esB = dev.createBuffer({ size: embedS.byteLength, usage: U.STORAGE | U.COPY_DST }); + dev.queue.writeBuffer(esB, 0, embedS.buffer, embedS.byteOffset, embedS.byteLength); + DEC = { + pen2: pipe(PENALTY2, "pen2"), a1: pipe(ARGMAX1, "amax1"), a2: pipe(ARGMAX2, "amax2"), + app: pipe(APPEND, "append"), emb: pipe(EMBED(bits, q3f), "embed"), + eqB, esB, ring: dev.createBuffer({ size: Math.max(cap, 1024) * 4, usage: U.STORAGE | U.COPY_DST | U.COPY_SRC }), + uA1: ubuf(new Uint32Array([vocab, 0, 0, 0])), tmp: sbuf(512), + out: dev.createBuffer({ size: 16, usage: U.STORAGE | U.COPY_SRC }), + stg: dev.createBuffer({ size: BATCH * 4, usage: U.MAP_READ | U.COPY_DST }), + uP: mk(), uE: mk(), uRQ: mk(), uRK: mk(), uA: mk(), + }; + } + await sync(prompt, true); + const seq = prompt.slice(); + dev.queue.writeBuffer(DEC.ring, 0, new Uint32Array(seq)); // ring ← current seq (penalty window + embed source) + let done = false; + while (!done && seq.length - prompt.length < maxNew && seq.length < cap) { + const n = Math.min(BATCH, maxNew - (seq.length - prompt.length), cap - seq.length); + const base = seq.length, pos0 = pos; + for (let k = 0; k < n; k++) { // prewritten per-step uniforms (queued writes, no fence) + dev.queue.writeBuffer(DEC.uP[k], 0, new Uint32Array([base + k, fbits(repPenalty), 0, 0])); + dev.queue.writeBuffer(DEC.uE[k], 0, new Uint32Array([base + k, d, 0, 0])); + dev.queue.writeBuffer(DEC.uRQ[k], 0, new Uint32Array([n_heads, hd, pos0 + k, n_kv_heads])); // .w = nkv (fused ROPE2) + dev.queue.writeBuffer(DEC.uRK[k], 0, new Uint32Array([n_kv_heads, hd, pos0 + k, 0])); + dev.queue.writeBuffer(DEC.uA[k], 0, new Uint32Array([n_heads, n_kv_heads, hd, pos0 + k])); + } + const enc = dev.createCommandEncoder(); + for (let k = 0; k < n; k++) { + pass(enc, DEC.pen2, [B.logits, DEC.ring, DEC.uP[k]], 1); + pass(enc, DEC.a1, [B.logits, DEC.tmp, DEC.uA1], 256); + pass(enc, DEC.a2, [DEC.tmp, DEC.out], 1); + pass(enc, DEC.app, [DEC.ring, DEC.out, DEC.uE[k]], 1); // ring[base+k] = winner + pass(enc, DEC.emb, [DEC.ring, DEC.eqB, DEC.esB, B.x, DEC.uE[k]], Math.ceil(d / 256)); + let cur = B.x; + const up = { ropeQ: DEC.uRQ[k], ropeK: DEC.uRK[k], attn: DEC.uA[k], pos: pos0 + k }; + for (let l = 0; l < n_layers; l++) cur = layerBody(enc, l, cur, (role) => W[`l${l}.${role}`], up); + rms(enc, cur, "final_norm", B.normed); + mmW(enc, B.normed, W["lm_head"], B.logits); + } + enc.copyBufferToBuffer(DEC.ring, base * 4, DEC.stg, 0, n * 4); + dev.queue.submit([enc.finish()]); + pos = pos0 + n; + await DEC.stg.mapAsync(GPUMapMode.READ); + const win = new Uint32Array(DEC.stg.getMappedRange().slice(0)).subarray(0, n); DEC.stg.unmap(); + for (let k = 0; k < n; k++) { + if (win[k] === eos) { pos = pos0 + k; done = true; break; } // trim + rewind: stale KV beyond pos is never read + seq.push(win[k]); cached.push(win[k]); + } + } + lastLogits = null; // raw-logits callers re-derive via sync()'s guard + return seq; + } + + // ── SPECULATIVE DECODE (n-gram draft + batched-k verify; unfused/kv4 path) ────────────── + // All k window inputs are known ⇒ one prefill-like forward verifies them; weights are read + // ONCE per pass for all rows. Greedy verification is EXACT: the committed sequence equals + // sequential decode byte-for-byte by construction (gate G2 asserts it). + const KX = 8; + let SP = null; + let _drafter = null; // pluggable learned drafter: (seq, max) => ids (sync or async). null = n-gram baseline. + function specInit() { + if (SP) return SP; + if (moe || stream || !kv4 || attnBias || qkNorm) throw new Error("specDecode: resident + kv4, no attn-bias/qk-norm (for now)"); + // BitNet (subNorm+bitlinear) and fusedT2-resident models ARE supported: the spec forward below is + // always UNFUSED (specMM per matrix) and mirrors the resident unfused BitNet layer's extra norms. + const sb8 = (n) => dev.createBuffer({ size: n * 4, usage: U.STORAGE | U.COPY_DST | U.COPY_SRC }); + SP = { + x: sb8(KX * d), normed: sb8(KX * d), q: sb8(KX * q_dim), k: sb8(KX * kv_dim), v: sb8(KX * kv_dim), + attn: sb8(KX * q_dim), h: sb8(KX * d), normed2: sb8(KX * d), gate: sb8(KX * ff), up: sb8(KX * ff), + hid: sb8(KX * ff), cur: sb8(KX * d), logits: sb8(KX * vocab), amax: sb8(KX), amaxStg: dev.createBuffer({ size: KX * 4, usage: U.MAP_READ | U.COPY_DST }), + attn2: sb8(KX * q_dim), hid2: sb8(KX * ff), nb1: sb8(KX * d), nb2: sb8(KX * d), // BitNet sub-norm + BitLinear input-norm outputs (batched; RMS must not run in place) + tmp: sbuf(65536 * 2), + P_rmsk: pipe(RMSK, "rmsk"), P_ropek: pipe(ROPEK(ropeLit), "ropek"), P_kvqk: pipe(KVQK(kv_dim), "kvqk"), + P_attnqk: pipe(ATTNQK(cap, kv_dim), "attnqk"), P_attnk: pipe(ATTNK(cap), "attnk"), + P_t2k: pipe(mmT2KK(false, KX), "t2k"), P_t2ka: pipe(mmT2KK(true, KX), "t2ka"), + P_t2k2: pipe(mmT2KK(false, 2), "t2k2"), P_t2ka2: pipe(mmT2KK(true, 2), "t2ka2"), P_q3k2: pipe(mmQ3KK(2), "q3k2"), + P_q3k: pipe(mmQ3KK(KX), "q3k"), + uD: ubuf(new Uint32Array([d, 0, 0, 0])), uQd: ubuf(new Uint32Array([q_dim, 0, 0, 0])), uFFn: ubuf(new Uint32Array([ff, 0, 0, 0])), uRQ: ubuf(new Uint32Array([4])), uRK: ubuf(new Uint32Array([4])), + uAT: ubuf(new Uint32Array([4])), uFFK: ubuf(new Uint32Array([4])), + uPen: Array.from({ length: KX }, () => ubuf(new Uint32Array(4))), uRow: Array.from({ length: KX }, (_, i) => ubuf(new Uint32Array([i, 0, 0, 0]))), uV: ubuf(new Uint32Array([vocab, 0, 0, 0])), + cold: 0, stats: { windows: 0, drafted: 0, accepted: 0 }, + }; + if (hasT2R) { SP.P_t2rk = pipe(mmT2RKK(false, KX), "t2rk"); SP.P_t2rka = pipe(mmT2RKK(true, KX), "t2rka"); SP.P_t2rk2 = pipe(mmT2RKK(false, 2), "t2rk2"); SP.P_t2rka2 = pipe(mmT2RKK(true, 2), "t2rka2"); } + // DP4a integer-dot path (opt-in via window.__dp4a; t2 weights only). dot4I8Packed is core WGSL here; + // on a device without it these compiles would error — gate creation on the flag being pre-set. + if (typeof window === "undefined" || window.__dp4a) { + SP.aq = sb8(KX * (Math.max(d, ff) >> 2)); SP.asc = sb8(KX * (Math.max(d, ff) >> 5)); + SP.P_actq = pipe(ACTQUANTK, "actq"); + SP.P_t2dp = pipe(mmT2DP4A(false, KX), "t2dp"); SP.P_t2dpa = pipe(mmT2DP4A(true, KX), "t2dpa"); + SP.P_t2dp2 = pipe(mmT2DP4A(false, 2), "t2dp2"); SP.P_t2dpa2 = pipe(mmT2DP4A(true, 2), "t2dpa2"); + SP.hasDp4a = true; + } + return SP; + } + const embedRowF32 = (token, out, off) => { + const eq32 = new Uint32Array(embedQ.buffer, embedQ.byteOffset, embedQ.byteLength >> 2), bb = token * (d / 32), sb = token * (d / 32); + for (let i = 0; i < d; i++) { + const bp = (bb + (i >> 5)) * 3, j = i & 31; let q; + if (j < 10) q = (eq32[bp] >>> (j * 3)) & 7; + else if (j < 20) q = (eq32[bp + 1] >>> ((j - 10) * 3)) & 7; + else if (j < 30) q = (eq32[bp + 2] >>> ((j - 20) * 3)) & 7; + else { const sp = (eq32[bp] >>> 30) | ((eq32[bp + 1] >>> 30) << 2) | ((eq32[bp + 2] >>> 30) << 4); q = j === 30 ? sp & 7 : (sp >>> 3) & 7; } + out[off + i] = (q - 3) * embedS[sb + (i >> 5)]; + } + }; + const specMM = (enc, ws, xb, ob, m, rb) => { // batched GEMM, route by fmt; m<=2 takes the narrow (KX=2) pipelines + const g = grid(Math.ceil(ws.N / 4)); const nr = m <= 2; + if (ws.t2 && SP.hasDp4a && typeof window !== "undefined" && window.__dp4a) { // DP4a integer-dot: quant xb→int8 (reuses ws.uni's K), then dot4I8Packed + pass(enc, SP.P_actq, [xb, SP.aq, SP.asc, ws.uni], [1, m]); + if (rb) pass(enc, nr ? SP.P_t2dpa2 : SP.P_t2dpa, [SP.aq, ws.qbuf, SP.asc, rb, ob, ws.uni], g); + else pass(enc, nr ? SP.P_t2dp2 : SP.P_t2dp, [SP.aq, ws.qbuf, SP.asc, ob, ws.uni], g); + return; + } + if (ws.t2) pass(enc, rb ? (nr ? SP.P_t2ka2 : SP.P_t2ka) : (nr ? SP.P_t2k2 : SP.P_t2k), rb ? [xb, ws.qbuf, rb, ob, ws.uni] : [xb, ws.qbuf, ob, ws.uni], g); + else if (ws.t2r) pass(enc, rb ? (nr ? SP.P_t2rka2 : SP.P_t2rka) : (nr ? SP.P_t2rk2 : SP.P_t2rk), rb ? [xb, ws.qbuf, ws.sbuf, rb, ob, ws.uni] : [xb, ws.qbuf, ws.sbuf, ob, ws.uni], g); + else pass(enc, nr ? SP.P_q3k2 : SP.P_q3k, [xb, ws.qbuf, ws.sbuf, ob, ws.uni], g); + }; + function ngramDraft(seq, max) { // longest suffix match (3→1-gram) proposes following ids + const n = seq.length; if (n < 4) return []; + for (let g = 3; g >= 1; g--) { + const a = seq.slice(n - g); + for (let i = n - g - 1; i >= 0; i--) { + let hit = true; for (let j = 0; j < g; j++) if (seq[i + j] !== a[j]) { hit = false; break; } + if (hit) { const out = seq.slice(i + g, i + g + max); if (out.length) return out; } + } + } + return []; + } + async function captureHidden(tokens) { reset(); _capHidden = []; try { await sync(tokens); return _capHidden; } finally { _capHidden = null; } } + async function specDecode(prompt, maxNew, repPenalty = 1.3, onCommit = null) { + specInit(); + let seq = prompt.slice(); + await sync(seq.slice(0, -1)); // prefill all but the pending token + let tKnown = seq[seq.length - 1]; // committed, not yet forwarded + const ring = DEC && DEC.ring ? DEC.ring : null; // reuse decode head ring when built… + const ringBuf = ring || (specInit().ringB = SP.ringB || (SP.ringB = dev.createBuffer({ size: cap * 4, usage: U.STORAGE | U.COPY_DST }))); + dev.queue.writeBuffer(ringBuf, 0, new Uint32Array(seq)); + const xHost = new Float32Array(KX * d); + const P_pen = pipe(PENALTY2, "pen2k"), P_a1 = pipe(ARGMAX1, "a1k"), P_a2 = pipe(ARGMAX2K, "a2k"); + while (seq.length - prompt.length < maxNew && pos + KX + 2 < cap) { + let drafts = []; + if (!SP.stats.noDraft && SP.cold <= 0) { // pluggable drafter; n-gram is the fallback + const dfn = _drafter || ngramDraft; + drafts = (await dfn(seq, KX - 1)) || []; + if (drafts.length === 0 && _drafter) drafts = ngramDraft(seq, KX - 1); + if (drafts.length > KX - 1) drafts = drafts.slice(0, KX - 1); // window is [tKnown, ...drafts] ≤ KX + } + const win = [tKnown, ...drafts]; const m = win.length; + const base = pos; + SP.stats.windows++; SP.stats.drafted += drafts.length; + dev.queue.writeBuffer(ringBuf, base * 4, new Uint32Array(win)); // provisional ring rows + for (let i = 0; i < m; i++) embedRowF32(win[i], xHost, i * d); + dev.queue.writeBuffer(SP.x, 0, xHost, 0, m * d); + dev.queue.writeBuffer(SP.uRQ, 0, new Uint32Array([n_heads, hd, base, q_dim])); + dev.queue.writeBuffer(SP.uRK, 0, new Uint32Array([n_kv_heads, hd, base, kv_dim])); + dev.queue.writeBuffer(SP.uAT, 0, new Uint32Array([n_heads, n_kv_heads, hd, base])); + dev.queue.writeBuffer(SP.uFFK, 0, new Uint32Array([m * ff, 0, 0, 0])); + for (let i = 0; i < m; i++) dev.queue.writeBuffer(SP.uPen[i], 0, new Uint32Array([base + i + 1, fbits(repPenalty), 0, 0])); + const enc = dev.createCommandEncoder(); + let cur = SP.x; + for (let l = 0; l < n_layers; l++) { + const W_ = (role) => W[`l${l}.${role}`]; + pass(enc, SP.P_rmsk, [cur, Nrm[`l${l}.attn_norm`], SP.normed, SP.uD], [1, m]); + let qkvIn = SP.normed; + if (bitlinear) { pass(enc, SP.P_rmsk, [SP.normed, Nrm["__unit_d"], SP.nb1, SP.uD], [1, m]); qkvIn = SP.nb1; } // BitLinear weightless norm into q/k/v + specMM(enc, W_("wq"), qkvIn, SP.q, m); specMM(enc, W_("wk"), qkvIn, SP.k, m); specMM(enc, W_("wv"), qkvIn, SP.v, m); + pass(enc, SP.P_ropek, [SP.q, SP.uRQ], [Math.ceil(n_heads * (hd / 2) / 64), m]); + pass(enc, SP.P_ropek, [SP.k, SP.uRK], [Math.ceil(n_kv_heads * (hd / 2) / 64), m]); + if (l === 0) { // layer 0: f32 cache + for (let i = 0; i < m; i++) { enc.copyBufferToBuffer(SP.k, i * kv_dim * 4, kcache[0], (base + i) * kv_dim * 4, kv_dim * 4); enc.copyBufferToBuffer(SP.v, i * kv_dim * 4, vcache[0], (base + i) * kv_dim * 4, kv_dim * 4); } + pass(enc, SP.P_attnk, [SP.q, kcache[0], vcache[0], SP.attn, SP.uAT], [n_heads, m]); + } else { + pass(enc, SP.P_kvqk, [SP.k, kcache[l], SP.uAT], [1, m]); + pass(enc, SP.P_kvqk, [SP.v, vcache[l], SP.uAT], [1, m]); + pass(enc, SP.P_attnqk, [SP.q, kcache[l], vcache[l], SP.attn, SP.uAT], [n_heads, m]); + } + let attnO = SP.attn; + if (subNorm) { pass(enc, SP.P_rmsk, [SP.attn, Nrm[`l${l}.attn_sub_norm`], SP.attn2, SP.uQd], [1, m]); attnO = SP.attn2; } // BitNet: norm before the o-projection + else if (bitlinear) { pass(enc, SP.P_rmsk, [SP.attn, Nrm["__unit_qd"], SP.attn2, SP.uQd], [1, m]); attnO = SP.attn2; } // BitLinear: weightless norm into wo + specMM(enc, W_("wo"), attnO, SP.h, m, cur); + pass(enc, SP.P_rmsk, [SP.h, Nrm[`l${l}.ffn_norm`], SP.normed2, SP.uD], [1, m]); + let guIn = SP.normed2; + if (bitlinear) { pass(enc, SP.P_rmsk, [SP.normed2, Nrm["__unit_d"], SP.nb2, SP.uD], [1, m]); guIn = SP.nb2; } // BitLinear: weightless norm into gate/up + specMM(enc, W_("w_gate"), guIn, SP.gate, m); specMM(enc, W_("w_up"), guIn, SP.up, m); + pass(enc, P_sm, [SP.gate, SP.up, SP.hid, SP.uFFK], Math.ceil(m * ff / 64)); + let hidO = SP.hid; + if (subNorm) { pass(enc, SP.P_rmsk, [SP.hid, Nrm[`l${l}.ffn_sub_norm`], SP.hid2, SP.uFFn], [1, m]); hidO = SP.hid2; } // BitNet: norm before the down-projection + else if (bitlinear) { pass(enc, SP.P_rmsk, [SP.hid, Nrm["__unit_ff"], SP.hid2, SP.uFFn], [1, m]); hidO = SP.hid2; } // BitLinear: weightless norm into w_down + specMM(enc, W_("w_down"), hidO, SP.cur, m, SP.h); + cur = SP.cur; if (l < n_layers - 1) { const t_ = SP.x; SP.x = SP.cur; SP.cur = t_; } + } + pass(enc, SP.P_rmsk, [cur, Nrm["final_norm"], SP.normed, SP.uD], [1, m]); + specMM(enc, W["lm_head"], SP.normed, SP.logits, m); + for (let i = 0; i < m; i++) { // per-row penalty + argmax (sub-range bindings) + const lr = { buffer: SP.logits, offset: i * vocab * 4, size: vocab * 4 }; + pass(enc, P_pen, [lr, ringBuf, SP.uPen[i]], 1); + pass(enc, P_a1, [lr, SP.tmp, SP.uV], 256); + pass(enc, P_a2, [SP.tmp, SP.amax, SP.uRow[i]], 1); + } + enc.copyBufferToBuffer(SP.amax, 0, SP.amaxStg, 0, m * 4); + dev.queue.submit([enc.finish()]); + await SP.amaxStg.mapAsync(GPUMapMode.READ); + const out = new Uint32Array(SP.amaxStg.getMappedRange().slice(0)).subarray(0, m); SP.amaxStg.unmap(); + let a = 0, expect = out[0], done = false; + const commit = []; + for (let i = 1; i < m; i++) { if (win[i] === expect) { commit.push(expect); expect = out[i]; a++; } else break; } + commit.push(expect); + SP.stats.accepted += a; + if (drafts.length && a === 0) SP.cold = 3; else if (SP.cold > 0) SP.cold--; + for (const tk of commit) { if (tk === eos) { done = true; break; } seq.push(tk); cached.push(tk); if (onCommit && onCommit(tk) === false) { done = true; break; } } + pos = base + 1 + a; // rows kept: row0 + accepted; stale KV beyond pos never read + dev.queue.writeBuffer(ringBuf, (base + 1 + a) * 4, new Uint32Array([expect])); // correct the ring where the draft diverged + tKnown = expect; + if (done) break; + } + lastLogits = null; + if (seq.length - prompt.length > maxNew) seq.length = prompt.length + maxNew; // window overshoot trim (prefix property keeps byte-exactness) + return seq; + } + // Atlas/E₈ calibration (ADR-0054): run a corpus and collect each layer's ATTENTION-INPUT Hessian + // H = XᵀX (the input to wq/wk/wv) by snapshotting B.normed per layer during the forward — feeds LDLQ. + async function collectInputHessians(tokens) { + reset(); + const SZ = 2 * n_layers * d; // [attn inputs: n_layers·d][ffn inputs: n_layers·d] + snapBuf = snapBuf || sbuf(SZ); + snapStg = snapStg || dev.createBuffer({ size: SZ * 4, usage: U.MAP_READ | U.COPY_DST }); + _calib = true; + const attn = Array.from({ length: n_layers }, () => new Float64Array(d * d)); // wq/wk/wv input Hessian + const ffn = Array.from({ length: n_layers }, () => new Float64Array(d * d)); // gate/up input Hessian + const acc = (H, x, o) => { for (let a = 0; a < d; a++) { const xa = x[o + a], row = a * d; for (let b = a; b < d; b++) H[row + b] += xa * x[o + b]; } }; + try { + for (const tok of tokens) { + await step(tok); + const enc = dev.createCommandEncoder(); enc.copyBufferToBuffer(snapBuf, 0, snapStg, 0, SZ * 4); dev.queue.submit([enc.finish()]); + await snapStg.mapAsync(GPUMapMode.READ); const all = new Float32Array(snapStg.getMappedRange().slice(0)); snapStg.unmap(); + for (let l = 0; l < n_layers; l++) { acc(attn[l], all, l * d); acc(ffn[l], all, (n_layers + l) * d); } + } + } finally { _calib = null; } + const sym = (H) => { for (let a = 0; a < d; a++) for (let b = a + 1; b < d; b++) H[b * d + a] = H[a * d + b]; }; + for (let l = 0; l < n_layers; l++) { sym(attn[l]); sym(ffn[l]); } + return { attn, ffn }; + } + + // ── DIFFUSION DECODE (Dream-class): iterative mask-denoising over a resident batch ── + let DF = null; + const DF_NMAX = 192, DF_DK = 32; + function diffuseInit() { + if (DF) return DF; + if (manifest.maskId === undefined) throw new Error("diffuse: manifest has no maskId (not a diffusion model)"); + if (stream || moe) throw new Error("diffuse: resident models only"); + const q2 = n_heads * hd, kv2 = n_kv_heads * hd; + DF = { + P_g32: pipe(mmQ3KK(DF_DK), "dq3k32"), P_attnB: pipe(ATTNB(DF_NMAX), "attnB"), P_bias: pipe(BIASK, "biasK"), + P_rms: pipe(RMSK, "drms"), P_rope: pipe(ROPEK(ropeLit), "drope"), P_add: pipe(ADD, "dadd"), P_sm: pipe(SILUMUL, "dsm"), + P_a1: pipe(ARGMAX1, "da1"), P_a2: pipe(ARGMAX2C, "da2c"), + x: sbuf((DF_NMAX + DF_DK) * d), normed: sbuf((DF_NMAX + DF_DK) * d), q: sbuf((DF_NMAX + DF_DK) * q2), k: sbuf((DF_NMAX + DF_DK) * kv2), v: sbuf((DF_NMAX + DF_DK) * kv2), + attn: sbuf((DF_NMAX + DF_DK) * q2), h: sbuf((DF_NMAX + DF_DK) * d), normed2: sbuf((DF_NMAX + DF_DK) * d), + gate: sbuf((DF_NMAX + DF_DK) * ff), up: sbuf((DF_NMAX + DF_DK) * ff), hid: sbuf((DF_NMAX + DF_DK) * ff), cur: sbuf((DF_NMAX + DF_DK) * d), res: sbuf((DF_NMAX + DF_DK) * d), + logits: sbuf(DF_DK * vocab), tmp: sbuf(65536 * 2), amax: sbuf(DF_NMAX * 2), + stgIds: dev.createBuffer({ size: DF_NMAX * 8, usage: U.MAP_READ | U.COPY_DST }), + uD: ubuf(new Uint32Array([d, 0, 0, 0])), uQd: ubuf(new Uint32Array([q2, 0, 0, 0])), uKv: ubuf(new Uint32Array([kv2, 0, 0, 0])), + uRopeQ: ubuf(new Uint32Array([n_heads, hd, 0, q2])), uRopeK: ubuf(new Uint32Array([n_kv_heads, hd, 0, kv2])), + uV: ubuf(new Uint32Array([vocab, 0, 0, 0])), + uRow: Array.from({ length: DF_NMAX }, (_, i) => ubuf(new Uint32Array([i, 0, 0, 0]))), + uAdd: new Map(), uAttnCache: new Map(), + xHost: new Float32Array(DF_NMAX * d), + stats: { steps: 0, blocks: 0 }, + }; + return DF; + } + const dfAddUni = (nEl) => { let u = DF.uAdd.get(nEl); if (!u) { u = ubuf(new Uint32Array([nEl, 0, 0, 0])); DF.uAdd.set(nEl, u); } return u; }; + const dfAttnUni = (key, val) => { let u = DF.uAttnCache.get(key); if (!u) { u = ubuf(val); DF.uAttnCache.set(key, u); } return u; }; + const dfEmbedRow = (token, off) => { // q3f embed decode (mirror of the step path) + const eq32 = new Uint32Array(embedQ.buffer, embedQ.byteOffset, embedQ.byteLength >> 2); + const x = DF.xHost, nbD2 = d / 32; + for (let i = 0; i < d; i++) { + const b = token * nbD2 + (i >> 5), bp = b * 3, j = i & 31; let qv; + const p0 = eq32[bp], p1 = eq32[bp + 1], p2 = eq32[bp + 2]; + if (j < 10) qv = (p0 >>> (j * 3)) & 7; else if (j < 20) qv = (p1 >>> ((j - 10) * 3)) & 7; else if (j < 30) qv = (p2 >>> ((j - 20) * 3)) & 7; + else { const sp = (p0 >>> 30) | ((p1 >>> 30) << 2) | ((p2 >>> 30) << 4); qv = j === 30 ? sp & 7 : (sp >> 3) & 7; } + x[off + i] = (qv - 3) * embedS[token * nbD2 + (i >> 5)]; + } + }; + // chunked DK-column GEMM over the existing per-tensor uniforms; weights read ceil(n/DK)× per matrix + const dfGemm = (enc, ws, xb, xStride, ob, oStride, n) => { + for (let c0 = 0; c0 < n; c0 += DF_DK) { + pass(enc, DF.P_g32, [ + { buffer: xb, offset: c0 * xStride * 4, size: DF_DK * xStride * 4 }, + ws.qbuf, ws.sbuf, + { buffer: ob, offset: c0 * oStride * 4, size: DF_DK * oStride * 4 }, + ws.uni], grid(Math.ceil(ws.N / 4))); + } + }; + // diffuse(promptIds, genLen, { steps, causal }) → seq with the block denoised (greedy, deterministic) + async function diffuse(promptIds, genLen = 32, opts = {}) { + diffuseInit(); + if (DF.busy) throw new Error("diffuse: a denoise is already in flight — await the previous call before starting another (don't fire overlapping diffuse() calls)"); + DF.busy = true; + try { + const S = opts.steps || 8, causal = !!opts.causal; + const MASK = manifest.maskId; + // two modes: APPEND (genLen masks at the suffix — generation) or FILL (promptIds already + // contains MASK ids anywhere — infilling/editing, diffusion's structural advantage over AR). + let seq, n, masked = []; + if (opts.fill) { + seq = promptIds.slice(); n = seq.length; + for (let i = 0; i < n; i++) if (seq[i] === MASK) masked.push(i); + if (!masked.length) return seq; + } else { + n = promptIds.length + genLen; + seq = promptIds.concat(Array(genLen).fill(MASK)); + for (let i = promptIds.length; i < n; i++) masked.push(i); + } + if (n > DF_NMAX) throw new Error("diffuse: n " + n + " > " + DF_NMAX); + const M0 = masked.length; // initial mask count (ramp schedule base) + const uAttn = dfAttnUni("a" + n + "_" + causal, new Uint32Array([n_heads, n_kv_heads, hd, (n >>> 0) | (causal ? 0x80000000 : 0)])); + DF.stats.blocks++; + let stepsLeft = S; + while (masked.length) { + for (let i = 0; i < n; i++) dfEmbedRow(seq[i], i * d); + dev.queue.writeBuffer(DF.x, 0, DF.xHost, 0, n * d); + const enc = dev.createCommandEncoder(); + let cur = DF.x; + for (let l = 0; l < n_layers; l++) { + pass(enc, DF.P_rms, [cur, Nrm["l" + l + ".attn_norm"], DF.normed, DF.uD], [1, n]); + dfGemm(enc, W["l" + l + ".wq"], DF.normed, d, DF.q, n_heads * hd, n); + dfGemm(enc, W["l" + l + ".wk"], DF.normed, d, DF.k, n_kv_heads * hd, n); + dfGemm(enc, W["l" + l + ".wv"], DF.normed, d, DF.v, n_kv_heads * hd, n); + if (attnBias) { + pass(enc, DF.P_bias, [DF.q, Nrm["l" + l + ".bq"], DF.uQd], [Math.ceil(n_heads * hd / 64), n]); + pass(enc, DF.P_bias, [DF.k, Nrm["l" + l + ".bk"], DF.uKv], [Math.ceil(n_kv_heads * hd / 64), n]); + pass(enc, DF.P_bias, [DF.v, Nrm["l" + l + ".bv"], DF.uKv], [Math.ceil(n_kv_heads * hd / 64), n]); + } + pass(enc, DF.P_rope, [DF.q, DF.uRopeQ], [Math.ceil(n_heads * (hd / 2) / 64), n]); + pass(enc, DF.P_rope, [DF.k, DF.uRopeK], [Math.ceil(n_kv_heads * (hd / 2) / 64), n]); + pass(enc, DF.P_attnB, [DF.q, DF.k, DF.v, DF.attn, uAttn], [n_heads, n]); + dfGemm(enc, W["l" + l + ".wo"], DF.attn, n_heads * hd, DF.h, d, n); + pass(enc, DF.P_add, [DF.h, cur, DF.res, dfAddUni(n * d)], Math.ceil(n * d / 64)); // no aliasing: out ≠ both inputs + pass(enc, DF.P_rms, [DF.res, Nrm["l" + l + ".ffn_norm"], DF.normed2, DF.uD], [1, n]); + dfGemm(enc, W["l" + l + ".w_gate"], DF.normed2, d, DF.gate, ff, n); + dfGemm(enc, W["l" + l + ".w_up"], DF.normed2, d, DF.up, ff, n); + pass(enc, DF.P_sm, [DF.gate, DF.up, DF.hid, dfAddUni(n * ff)], Math.ceil(n * ff / 64)); + dfGemm(enc, W["l" + l + ".w_down"], DF.hid, ff, DF.cur, d, n); + pass(enc, DF.P_add, [DF.cur, DF.res, DF.x, dfAddUni(n * d)], Math.ceil(n * d / 64)); // DF.x is free past the first rms — becomes the rotating hidden + cur = DF.x; + } + pass(enc, DF.P_rms, [cur, Nrm["final_norm"], DF.normed, DF.uD], [1, n]); + dev.queue.submit([enc.finish()]); + // lm_head + per-masked-row argmax, chunked (logits buf holds DK rows) + for (let c0 = 0; c0 < n; c0 += DF_DK) { + const m = Math.min(DF_DK, n - c0); + let any = false; for (let r = 0; r < m; r++) if (c0 + r + 1 < n && seq[c0 + r + 1] === MASK) { any = true; break; } + if (!any) continue; + const e2 = dev.createCommandEncoder(); + pass(e2, DF.P_g32, [ + { buffer: DF.normed, offset: c0 * d * 4, size: DF_DK * d * 4 }, + W["lm_head"].qbuf, W["lm_head"].sbuf, DF.logits, W["lm_head"].uni], grid(Math.ceil(vocab / 4))); + for (let r = 0; r < m; r++) { + if (c0 + r + 1 >= n || seq[c0 + r + 1] !== MASK) continue; // row r predicts position r+1 (Dream shifted head) + pass(e2, DF.P_a1, [{ buffer: DF.logits, offset: r * vocab * 4, size: vocab * 4 }, DF.tmp, DF.uV], 256); + pass(e2, DF.P_a2, [DF.tmp, DF.amax, DF.uRow[c0 + r + 1]], 1); + } + dev.queue.submit([e2.finish()]); + } + { const e3 = dev.createCommandEncoder(); e3.copyBufferToBuffer(DF.amax, 0, DF.stgIds, 0, n * 8); dev.queue.submit([e3.finish()]); } + await DF.stgIds.mapAsync(GPUMapMode.READ); + const raw = DF.stgIds.getMappedRange().slice(0); DF.stgIds.unmap(); + const ids = new Uint32Array(raw), conf = new Float32Array(raw); + DF.lastIds = ids; DF.lastConf = conf; + const eosId = 151643; + // ramp schedule: commit few tokens early (fully-masked context = least informed), many late + const total = S * (S + 1) / 2, sIdx = S - stepsLeft + 1; + const kUn = Math.max(1, Math.round((M0 * sIdx) / total)); + if (opts.fill) { + // infill: fixed span, never truncate — commit the highest-confidence cohort each step + masked.sort((a, b) => conf[b * 2 + 1] - conf[a * 2 + 1]); + for (let u = 0; u < kUn && masked.length; u++) { const p = masked.shift(); seq[p] = ids[p * 2]; } + masked.sort((a, b) => a - b); + } else { + // generation: EOS-candidates unmask LAST (one confident pad would else kill the block) + const nonEos = masked.filter((p) => ids[p * 2] !== eosId); + if (nonEos.length === 0) { for (const p of masked) seq[p] = eosId; masked = []; } + else { + nonEos.sort((a, b) => conf[b * 2 + 1] - conf[a * 2 + 1]); + for (let u = 0; u < kUn && nonEos.length; u++) { const p = nonEos.shift(); seq[p] = ids[p * 2]; masked.splice(masked.indexOf(p), 1); } + masked.sort((a, b) => a - b); + } + } + stepsLeft = Math.max(1, stepsLeft - 1); + DF.stats.steps++; + } + return seq; + } finally { DF.busy = false; } + } + return { step, reset, truncateTo, get cachedLen() { return cached.length; }, sync, generate, decode, diffuse, diffStats: () => (DF ? DF.stats : null), _df: () => DF, _dev: () => dev, specDecode, specStats: () => (SP ? SP.stats : null), setDrafter: (fn) => { _drafter = fn || null; }, argmax, captureHidden, collectInputHessians, dumpKV, dims: manifest, streaming: stream, gran: remote ? "remote (served disk)" : (stream === "opfs" ? "opfs (disk)" : (frameGran ? "frame" : (stream ? "layer" : "resident"))), frameBufBytes: streamBuf, loadStats: QLOAD, destroy: () => { try { dev.destroy(); } catch {} }, get gpuBytes() { return gpuBytes; }, get pos() { return pos; }, get timing() { return timing; } }; +} diff --git a/apps/q/qvac-ingest.mjs b/apps/q/qvac-ingest.mjs new file mode 100644 index 0000000000000000000000000000000000000000..5dd61f8c101b5d167b2cccfa84c86b3d5ecdc8f7 --- /dev/null +++ b/apps/q/qvac-ingest.mjs @@ -0,0 +1,561 @@ +// QVAC disk-streaming GGUF ingestion — pure JS, no wasm in the weight path. +// +// The wasm path holds the whole GGUF in wasm32 linear memory (~4 GB ceiling), so +// it can't ingest a 7B/14B model. This module reads a large GGUF straight off disk +// (HTTP Range against the served file) and converts each tensor to the engine's +// block format ON DEMAND, so RAM never holds more than one tensor at a time. +// +// It is a faithful port of qvac-gguf::dequantize_raw + qvac-layer::quant_blocks + +// model_specs, so the bytes it produces are what the GPU engine already consumes. +// The tokenizer stays in wasm: we feed wasm only the GGUF HEADER (the first +// data_offset bytes), which is all qvac_load_gpu needs for the BPE + manifest. + +import { makeIQ } from "./forge/gguf-forge-iq-dequant.mjs"; + +// ── f16 → f32 (matches qvac_gguf::f16_to_f32) ── +export function f16ToF32(h) { + const sign = (h >> 15) & 1, exp = (h >> 10) & 0x1f, mant = h & 0x3ff; + let val; + if (exp === 0) val = mant * Math.pow(2, -24); + else if (exp === 0x1f) return mant ? NaN : (sign ? -Infinity : Infinity); + else val = (1 + mant / 1024) * Math.pow(2, exp - 15); + return sign ? -val : val; +} + +const QK = 32, QK_K = 256; +export const GGML = { + F32: 0, F16: 1, Q4_0: 2, Q4_1: 3, Q8_0: 8, Q2_K: 10, Q3_K: 11, Q4_K: 12, Q5_K: 13, Q6_K: 14, + IQ2_XXS: 16, IQ2_XS: 17, IQ3_XXS: 18, IQ1_S: 19, IQ4_NL: 20, IQ3_S: 21, IQ2_S: 22, IQ4_XS: 23, IQ1_M: 29, + TQ2_0: 35, +}; +// IQ-quant byte layouts: [block elements, block bytes]. (ggml-common.h:485-563) +const IQ_BLOCK = { + 16: [QK_K, 66], 17: [QK_K, 74], 18: [QK_K, 98], 19: [QK_K, 50], 20: [32, 18], + 21: [QK_K, 110], 22: [QK_K, 82], 23: [QK_K, 136], 29: [QK_K, 56], +}; +// BitNet ternary TQ2_0 (ggml-common.h:273): qs[64] + f16 d = 66 B / 256. Runtime +// dequant is float64 (the Tier-A oracle gguf-forge-dequant.mjs is the bit-exact ref). +const TQ_BLOCK = { 35: [QK_K, 66] }; +function dequantTq2_0Rt(raw, elements) { + const out = new Float32Array(elements); + const nb = elements / QK_K; + const dv = new DataView(raw.buffer, raw.byteOffset, raw.byteLength); + let o = 0, base = 0; + for (let i = 0; i < nb; ++i) { + const d = f16ToF32(dv.getUint16(base + 64, true)); + for (let j = 0; j < 64; j += 32) + for (let l = 0; l < 4; ++l) + for (let m = 0; m < 32; ++m) out[o++] = (((raw[base + j + m] >> (l * 2)) & 3) - 1) * d; + base += 66; + } + return out; +} +const TQ_RT = { 35: dequantTq2_0Rt }; +// Runtime IQ dequant (float64; the Tier-A oracle is the bit-exact reference). +const _iq = makeIQ((x) => x, f16ToF32); +const IQ_RT = { + 16: _iq.dequantIQ2XXS, 17: _iq.dequantIQ2XS, 18: _iq.dequantIQ3XXS, 19: _iq.dequantIQ1S, + 20: _iq.dequantIQ4NL, 21: _iq.dequantIQ3S, 22: _iq.dequantIQ2S, 23: _iq.dequantIQ4XS, 29: _iq.dequantIQ1M, +}; + +// On-disk byte length of `elements` of a ggml type (matches type_byte_len). +export function typeByteLen(t, elements) { + switch (t) { + case GGML.F32: return elements * 4; + case GGML.F16: return elements * 2; + case GGML.Q8_0: return (elements / QK) * (2 + QK); + case GGML.Q4_0: return (elements / QK) * (2 + QK / 2); + case GGML.Q4_1: return (elements / QK) * (2 + 2 + QK / 2); + case GGML.Q2_K: return (elements / QK_K) * 84; + case GGML.Q3_K: return (elements / QK_K) * 110; + case GGML.Q4_K: return (elements / QK_K) * 144; + case GGML.Q5_K: return (elements / QK_K) * 176; + case GGML.Q6_K: return (elements / QK_K) * 210; + default: + if (t in IQ_BLOCK) { const [be, bb] = IQ_BLOCK[t]; return (elements / be) * bb; } + if (t in TQ_BLOCK) { const [be, bb] = TQ_BLOCK[t]; return (elements / be) * bb; } + throw new Error("unsupported ggml type " + t); + } +} +// (block elements, block bytes) for the range reader. +function blockShape(t) { + switch (t) { + case GGML.F32: return [1, 4]; + case GGML.F16: return [1, 2]; + case GGML.Q8_0: return [QK, 2 + QK]; + case GGML.Q4_0: return [QK, 2 + QK / 2]; + case GGML.Q4_1: return [QK, 2 + 2 + QK / 2]; + case GGML.Q2_K: return [QK_K, 84]; + case GGML.Q3_K: return [QK_K, 110]; + case GGML.Q4_K: return [QK_K, 144]; + case GGML.Q5_K: return [QK_K, 176]; + case GGML.Q6_K: return [QK_K, 210]; + default: + if (t in IQ_BLOCK) return IQ_BLOCK[t]; + if (t in TQ_BLOCK) return TQ_BLOCK[t]; + throw new Error("unsupported ggml type " + t); + } +} + +// Dequantize raw tensor bytes → Float32Array (port of dequantize_raw). +export function dequantizeRaw(t, raw, elements) { + const out = new Float32Array(elements); + const dv = new DataView(raw.buffer, raw.byteOffset, raw.byteLength); + let o = 0; + if (t === GGML.F32) { + for (let i = 0; i < elements; i++) out[i] = dv.getFloat32(i * 4, true); + } else if (t === GGML.F16) { + for (let i = 0; i < elements; i++) out[i] = f16ToF32(dv.getUint16(i * 2, true)); + } else if (t === GGML.Q8_0) { + const bb = 2 + QK; + for (let p = 0; p + bb <= raw.byteLength; p += bb) { + const d = f16ToF32(dv.getUint16(p, true)); + for (let j = 0; j < QK; j++) out[o++] = d * (raw[p + 2 + j] << 24 >> 24); + } + } else if (t === GGML.Q4_0) { + const bb = 2 + QK / 2; + for (let p = 0; p + bb <= raw.byteLength; p += bb) { + const d = f16ToF32(dv.getUint16(p, true)); + const qs = p + 2; + for (let j = 0; j < QK / 2; j++) out[o + j] = d * ((raw[qs + j] & 0x0f) - 8); // low nibble → j + for (let j = 0; j < QK / 2; j++) out[o + QK / 2 + j] = d * ((raw[qs + j] >> 4) - 8); // high → j+16 + o += QK; + } + } else if (t === GGML.Q4_1) { + const bb = 2 + 2 + QK / 2; + for (let p = 0; p + bb <= raw.byteLength; p += bb) { + const d = f16ToF32(dv.getUint16(p, true)), m = f16ToF32(dv.getUint16(p + 2, true)); + const qs = p + 4; + for (let j = 0; j < QK / 2; j++) out[o + j] = (raw[qs + j] & 0x0f) * d + m; + for (let j = 0; j < QK / 2; j++) out[o + QK / 2 + j] = (raw[qs + j] >> 4) * d + m; + o += QK; + } + } else if (t === GGML.Q4_K) { + // Q4_K: 256-element super-block of 144 B = d(f16) + dmin(f16) + 12 packed 6-bit scales/mins + 128 nibble qs. + const bb = 144; + for (let p = 0; p + bb <= raw.byteLength; p += bb) { + const d = f16ToF32(dv.getUint16(p, true)), dmin = f16ToF32(dv.getUint16(p + 2, true)); + const sc = p + 4, qs = p + 16; + const sm = (j) => j < 4 ? [raw[sc + j] & 63, raw[sc + j + 4] & 63] + : [(raw[sc + j + 4] & 0xF) | ((raw[sc + j - 4] >> 6) << 4), (raw[sc + j + 4] >> 4) | ((raw[sc + j] >> 6) << 4)]; + let q = 0, is = 0; + for (let j = 0; j < QK_K; j += 64) { + const [s1, m1] = sm(is), [s2, m2] = sm(is + 1); + for (let l = 0; l < 32; l++) out[o + l] = d * s1 * (raw[qs + q + l] & 0xF) - dmin * m1; + for (let l = 0; l < 32; l++) out[o + 32 + l] = d * s2 * (raw[qs + q + l] >> 4) - dmin * m2; + o += 64; q += 32; is += 2; + } + } + } else if (t === GGML.Q6_K) { + const bb = 210; + for (let p = 0; p + bb <= raw.byteLength; p += bb) { + const ql = p, qh = p + 128, sc = p + 192, d = f16ToF32(dv.getUint16(p + 208, true)); + for (let n = 0; n < 2; n++) { + const qlo = ql + n * 64, qho = qh + n * 32, sco = sc + n * 8, yo = o + n * 128; + for (let l = 0; l < 32; l++) { + const is = (l / 16) | 0; + const q1 = ((raw[qlo + l] & 0x0f) | (((raw[qho + l] >> 0) & 3) << 4)) - 32; + const q2 = ((raw[qlo + l + 32] & 0x0f) | (((raw[qho + l] >> 2) & 3) << 4)) - 32; + const q3 = ((raw[qlo + l] >> 4) | (((raw[qho + l] >> 4) & 3) << 4)) - 32; + const q4 = ((raw[qlo + l + 32] >> 4) | (((raw[qho + l] >> 6) & 3) << 4)) - 32; + out[yo + l] = d * (raw[sco + is] << 24 >> 24) * q1; + out[yo + l + 32] = d * (raw[sco + is + 2] << 24 >> 24) * q2; + out[yo + l + 64] = d * (raw[sco + is + 4] << 24 >> 24) * q3; + out[yo + l + 96] = d * (raw[sco + is + 6] << 24 >> 24) * q4; + } + } + o += QK_K; + } + } else if (t === GGML.Q2_K) { + // 84 B: scales[16] qs[64] d(f16) dmin(f16). y = d*(sc&0xF)*q2 - dmin*(sc>>4). + const bb = 84; + for (let p = 0; p + bb <= raw.byteLength; p += bb) { + const sc = p, qs = p + 16, d = f16ToF32(dv.getUint16(p + 80, true)), dmin = f16ToF32(dv.getUint16(p + 82, true)); + let is = 0; + for (let n = 0; n < QK_K; n += 128) { + const q = qs + (n >> 7) * 32; + for (let shift = 0; shift < 8; shift += 2) { + let s = raw[sc + is++]; + for (let l = 0; l < 16; l++) out[o++] = d * (s & 0xf) * ((raw[q + l] >> shift) & 3) - dmin * (s >> 4); + s = raw[sc + is++]; + for (let l = 0; l < 16; l++) out[o++] = d * (s & 0xf) * ((raw[q + l + 16] >> shift) & 3) - dmin * (s >> 4); + } + } + } + } else if (t === GGML.Q3_K) { + // 110 B: hmask[32] qs[64] scales[12] d(f16). 6-bit signed scales via kmask unpack. + const bb = 110, km1 = 0x03030303, km2 = 0x0f0f0f0f; + const aux = new Uint32Array(4), sb = new Int8Array(aux.buffer); + for (let p = 0; p + bb <= raw.byteLength; p += bb) { + const hm = p, qs = p + 32, sco = p + 96, d = f16ToF32(dv.getUint16(p + 108, true)); + aux[0] = dv.getUint32(sco, true); aux[1] = dv.getUint32(sco + 4, true); aux[2] = dv.getUint32(sco + 8, true); + const tmp = aux[2]; + aux[2] = ((aux[0] >>> 4) & km2) | (((tmp >>> 4) & km1) << 4); + aux[3] = ((aux[1] >>> 4) & km2) | (((tmp >>> 6) & km1) << 4); + aux[0] = (aux[0] & km2) | (((tmp >>> 0) & km1) << 4); + aux[1] = (aux[1] & km2) | (((tmp >>> 2) & km1) << 4); + let is = 0, m = 1; + for (let n = 0; n < QK_K; n += 128) { + const q = qs + (n >> 7) * 32; + for (let shift = 0; shift < 8; shift += 2) { + let dl = d * (sb[is++] - 32); + for (let l = 0; l < 16; l++) out[o++] = dl * (((raw[q + l] >> shift) & 3) - ((raw[hm + l] & m) ? 0 : 4)); + dl = d * (sb[is++] - 32); + for (let l = 0; l < 16; l++) out[o++] = dl * (((raw[q + l + 16] >> shift) & 3) - ((raw[hm + l + 16] & m) ? 0 : 4)); + m <<= 1; + } + } + } + } else if (t === GGML.Q5_K) { + // 176 B: d(f16) dmin(f16) scales[12] qh[32] ql[128]. Q4_K nibbles + 5th bit from qh. + const bb = 176; + const smk4 = (sc, j) => j < 4 ? [raw[sc + j] & 63, raw[sc + j + 4] & 63] + : [(raw[sc + j + 4] & 0xF) | ((raw[sc + j - 4] >> 6) << 4), (raw[sc + j + 4] >> 4) | ((raw[sc + j] >> 6) << 4)]; + for (let p = 0; p + bb <= raw.byteLength; p += bb) { + const d = f16ToF32(dv.getUint16(p, true)), dmin = f16ToF32(dv.getUint16(p + 2, true)); + const sc = p + 4, qh = p + 16; let ql = p + 48, is = 0, u1 = 1, u2 = 2; + for (let j = 0; j < QK_K; j += 64) { + const [s1, m1] = smk4(sc, is), [s2, m2] = smk4(sc, is + 1); + const d1 = d * s1, mm1 = dmin * m1, d2 = d * s2, mm2 = dmin * m2; + for (let l = 0; l < 32; l++) out[o++] = d1 * ((raw[ql + l] & 0xF) + ((raw[qh + l] & u1) ? 16 : 0)) - mm1; + for (let l = 0; l < 32; l++) out[o++] = d2 * ((raw[ql + l] >> 4) + ((raw[qh + l] & u2) ? 16 : 0)) - mm2; + ql += 32; is += 2; u1 <<= 2; u2 <<= 2; + } + } + } else if (t in IQ_RT) { + return IQ_RT[t](raw, elements); // IQ-quants (float64 runtime; oracle is the bit-exact ref) + } else if (t in TQ_RT) { + return TQ_RT[t](raw, elements); // BitNet TQ2_0 (float64 runtime; oracle is the bit-exact ref) + } else throw new Error("unsupported ggml type " + t); + return out; +} + +// Re-quantize a [n,k] f32 tensor into the engine's per-32-block format +// (port of qvac-layer::quant_blocks). bits=4 → (nibble-8)*scale, scale=amax/7, +// sequential nibble packing; bits=8 → int8, scale=amax/127. Returns {q,s}. +// Arithmetic is done in f32 (Math.fround) with round-half-away-from-zero to match +// Rust's f32 `round()` byte-for-byte, so the frames are bit-identical to wasm's. +const fr = Math.fround; +const rnd = (x) => x >= 0 ? Math.floor(x + 0.5) : Math.ceil(x - 0.5); // half away from zero (Rust f32::round) +export function quantBlocks(f, n, k, bits) { + const nb = k / 32; + const s = new Float32Array(n * nb); + let si = 0; + if (bits === 4) { + const q = new Uint8Array(n * k / 2); + for (let row = 0; row < n; row++) { + const base = row * k; + for (let b = 0; b < nb; b++) { + const bo = base + b * 32; + let amax = 0; for (let j = 0; j < 32; j++) { const a = Math.abs(f[bo + j]); if (a > amax) amax = a; } + amax = Math.max(amax, 1e-9); const scale = fr(amax / 7); s[si++] = scale; + for (let j = 0; j < 32; j++) { + let qv = rnd(fr(f[bo + j] / scale)); qv = qv < -8 ? -8 : qv > 7 ? 7 : qv; qv = (qv + 8) & 0xf; + const g = bo + j; + if ((g & 1) === 0) q[g >> 1] |= qv; else q[g >> 1] |= qv << 4; + } + } + } + return { q, s }; + } else { + const q = new Uint8Array(n * k); + for (let row = 0; row < n; row++) { + const base = row * k; + for (let b = 0; b < nb; b++) { + const bo = base + b * 32; + let amax = 0; for (let j = 0; j < 32; j++) { const a = Math.abs(f[bo + j]); if (a > amax) amax = a; } + amax = Math.max(amax, 1e-9); const scale = fr(amax / 127); s[si++] = scale; + for (let j = 0; j < 32; j++) { let qv = rnd(fr(f[bo + j] / scale)); qv = qv < -127 ? -127 : qv > 127 ? 127 : qv; q[bo + j] = qv & 0xff; } + } + } + return { q, s }; + } +} + +// FAST PATH: GGUF Q4_0 → engine Q4 with NO dequant/requant — a pure relayout. +// GGUF Q4_0 already stores (nibble-8)*d, exactly the engine's convention, so the +// nibble value maps straight across; we only reorder the interleaved (j, j+16) +// nibbles into the engine's sequential packing and widen the f16 scale to f32. +// This is both faster (integer-only) and bit-exact to the GGUF (no requant loss). +export function relayoutQ4(raw, n, k) { + const nb = k / 32; + const dv = new DataView(raw.buffer, raw.byteOffset, raw.byteLength); + const q = new Uint8Array(n * k / 2), s = new Float32Array(n * nb); + let bo = 0, si = 0; + for (let row = 0; row < n; row++) { + const rowBase = row * k; + for (let b = 0; b < nb; b++) { + s[si++] = f16ToF32(dv.getUint16(bo, true)); + const qs = bo + 2, blkBase = rowBase + b * 32; + for (let w = 0; w < 32; w++) { + const nib = w < 16 ? (raw[qs + w] & 0x0f) : (raw[qs + (w - 16)] >> 4); + const g = blkBase + w; + if ((g & 1) === 0) q[g >> 1] |= nib; else q[g >> 1] |= nib << 4; + } + bo += 18; + } + } + return { q, s }; +} +// FAST PATH: GGUF Q8_0 → engine Q8 — copy the i8 quants, widen f16 scale to f32. +export function relayoutQ8(raw, n, k) { + const nb = k / 32; + const dv = new DataView(raw.buffer, raw.byteOffset, raw.byteLength); + const q = new Uint8Array(n * k), s = new Float32Array(n * nb); + let bo = 0, si = 0, qi = 0; + for (let row = 0; row < n; row++) { + for (let b = 0; b < nb; b++) { + s[si++] = f16ToF32(dv.getUint16(bo, true)); + for (let w = 0; w < 32; w++) q[qi++] = raw[bo + 2 + w]; + bo += 34; + } + } + return { q, s }; +} + +// ── GGUF header parser (just enough: tensor directory + data offset) ── +class Cur { + constructor(buf) { this.b = buf; this.dv = new DataView(buf.buffer, buf.byteOffset, buf.byteLength); this.p = 0; this.v1 = false; } + need(n) { if (this.p + n > this.b.byteLength) throw new RangeError("short"); } + u8() { this.need(1); return this.b[this.p++]; } + u16() { this.need(2); const v = this.dv.getUint16(this.p, true); this.p += 2; return v; } + u32() { this.need(4); const v = this.dv.getUint32(this.p, true); this.p += 4; return v; } + u64() { this.need(8); const lo = this.dv.getUint32(this.p, true), hi = this.dv.getUint32(this.p + 4, true); this.p += 8; return hi * 4294967296 + lo; } + lenField() { return this.v1 ? this.u32() : this.u64(); } + skipStr() { const n = this.lenField(); this.need(n); this.p += n; } + skipValue(ty) { + switch (ty) { + case 0: case 1: case 7: this.p += 1; break; + case 2: case 3: this.p += 2; break; + case 4: case 5: case 6: this.p += 4; break; + case 10: case 11: case 12: this.p += 8; break; + case 8: this.skipStr(); break; + case 9: { const ety = this.u32(); const cnt = this.lenField(); for (let i = 0; i < cnt; i++) this.skipValue(ety); break; } + default: throw new Error("bad meta type " + ty); + } + } + // Read a SCALAR metadata value (numbers/bool/string); arrays are skipped (return undefined). + readScalar(ty) { + switch (ty) { + case 0: return this.u8(); + case 1: { const v = this.u8(); return v << 24 >> 24; } + case 2: return this.u16(); + case 3: { const v = this.u16(); return v << 16 >> 16; } + case 4: return this.u32(); + case 5: { const v = this.u32(); return v | 0; } + case 6: { this.need(4); const v = this.dv.getFloat32(this.p, true); this.p += 4; return v; } + case 7: return this.u8() !== 0; + case 8: { const n = this.lenField(); const s = new TextDecoder().decode(this.b.subarray(this.p, this.p + n)); this.p += n; return s; } + case 10: return this.u64(); + case 11: return this.u64(); + case 12: { this.need(8); const v = this.dv.getFloat64(this.p, true); this.p += 8; return v; } + case 9: { const ety = this.u32(); const cnt = this.lenField(); for (let i = 0; i < cnt; i++) this.skipValue(ety); return undefined; } + default: throw new Error("bad meta type " + ty); + } + } +} + +// Parse a GGUF header buffer → { version, dataOffset, tensors:[{name,dims,ggmlType,offset}] }. +// `buf` must contain at least up to the (aligned) end of the tensor-info table. +export function parseGgufHeader(buf) { + const c = new Cur(buf); + if (c.u32() !== 0x46554747) throw new Error("not GGUF"); + const version = c.u32(); + if (version !== 1 && version !== 2 && version !== 3) throw new Error("GGUF version " + version); + c.v1 = version === 1; + const tensorCount = c.lenField(); + const metaCount = c.lenField(); + let alignment = 32; + const meta = {}; + for (let i = 0; i < metaCount; i++) { + const keyLen = c.lenField(); const key = new TextDecoder().decode(buf.subarray(c.p, c.p + keyLen)); c.p += keyLen; + const ty = c.u32(); + const v = c.readScalar(ty); // arrays → undefined (skipped) + if (v !== undefined) meta[key] = v; + if (key === "general.alignment" && typeof v === "number") alignment = v; + } + const tensors = []; + for (let i = 0; i < tensorCount; i++) { + const nl = c.lenField(); const name = new TextDecoder().decode(buf.subarray(c.p, c.p + nl)); c.p += nl; + const nd = c.u32(); const dims = []; for (let j = 0; j < nd; j++) dims.push(c.lenField()); + const ggmlType = c.u32(); const offset = c.u64(); + tensors.push({ name, dims, ggmlType, offset }); + } + alignment = Math.max(1, alignment); + const dataOffset = Math.ceil(c.p / alignment) * alignment; + return { version, dataOffset, tensors, meta }; +} + +// Build the engine manifest (dims + tensor list with N,K,blk) from a parsed GGUF +// header — a pure-JS port of qvac-layer::model_specs + gpu_export_manifest, so +// conversion/streaming needs no wasm. `tensors` is the header's tensor directory. +export function buildManifest(meta, tensors, bits) { + const arch = meta["general.architecture"] || "llama"; + const mu = (k) => { const v = meta[`${arch}.${k}`]; return typeof v === "number" ? Math.round(v) : undefined; }; + const tset = new Set(tensors.map((t) => t.name)); + const tbyname = {}; for (const t of tensors) tbyname[t.name] = t; + const d = mu("embedding_length") || 0; + const n_layers = mu("block_count") || 0; + const n_heads = mu("attention.head_count") || 0; + const n_kv_heads = mu("attention.head_count_kv") || n_heads; + // For MoE, the experts use expert_feed_forward_length (e.g. Qwen3-30B-A3B: 768), + // which differs from the (unused) dense feed_forward_length (6144). OLMoE's two + // values happen to be equal. `ff` everywhere downstream means the EXPERT ff for MoE. + const ff = (mu("expert_feed_forward_length") || mu("feed_forward_length")) || 0; + const hd = mu("attention.key_length") || (n_heads ? Math.floor(d / n_heads) : 0); + const kv_dim = n_kv_heads * hd; + const rope_base = (typeof meta[`${arch}.rope.freq_base`] === "number") ? meta[`${arch}.rope.freq_base`] : 10000; + const attn_bias = tset.has("blk.0.attn_q.bias"); + const qk_norm = tset.has("blk.0.attn_q_norm.weight"); + const qk_norm_dim = qk_norm ? (tbyname["blk.0.attn_q_norm.weight"].dims[0] | 0) : 0; // hd (per-head, Qwen3) or d (full, OLMoE) + const n_experts = mu("expert_count") || 0; // >0 → MoE + const n_used = mu("expert_used_count") || 0; + const moe = n_experts > 0; + const bitnet = /^bitnet/.test(arch); // BitNet b1.58: sub-norms before wo/w_down + ReLU² gated FFN + const tied = !tset.has("output.weight"); + const vocab = d > 0 && tbyname["token_embd.weight"] ? Math.floor(tbyname["token_embd.weight"].dims.reduce((a, b) => a * b, 1) / d) : 0; + const blk = (name, N, K) => ({ name, N, K, blk: true }); + const nrm = (name, K) => ({ name, N: 1, K, blk: false }); + const t = []; + t.push(blk("embed", vocab, d)); + t.push(nrm("final_norm", d)); + t.push(blk("lm_head", vocab, d)); + for (let i = 0; i < n_layers; i++) { + t.push(nrm(`l${i}.attn_norm`, d)); + t.push(blk(`l${i}.wq`, n_heads * hd, d)); + t.push(blk(`l${i}.wk`, kv_dim, d)); + t.push(blk(`l${i}.wv`, kv_dim, d)); + if (attn_bias) { t.push(nrm(`l${i}.bq`, n_heads * hd)); t.push(nrm(`l${i}.bk`, kv_dim)); t.push(nrm(`l${i}.bv`, kv_dim)); } + if (qk_norm) { t.push(nrm(`l${i}.q_norm`, qk_norm_dim)); t.push(nrm(`l${i}.k_norm`, qk_norm_dim)); } + if (bitnet) t.push(nrm(`l${i}.attn_sub_norm`, n_heads * hd)); + t.push(blk(`l${i}.wo`, d, n_heads * hd)); + t.push(nrm(`l${i}.ffn_norm`, d)); + if (bitnet) t.push(nrm(`l${i}.ffn_sub_norm`, ff)); + if (moe) { + t.push(nrm(`l${i}.router`, n_experts * d)); // ffn_gate_inp [n_experts, d] f32 (CPU top-k) + // experts are NOT enumerated here (n_layers·n_experts·3 is huge); the engine + // generates `l{i}.e{e}.{gate,up,down}` names for the top-k it actually needs. + } else { + t.push(blk(`l${i}.w_gate`, ff, d)); + t.push(blk(`l${i}.w_up`, ff, d)); + t.push(blk(`l${i}.w_down`, d, ff)); + } + } + const out = { d, n_heads, n_kv_heads, ff, vocab, n_layers, hd, bits, rope_base, attn_bias, qk_norm, qk_norm_dim, tied, tensors: t }; + if (moe) out.moe = { n_experts, n_used }; + if (bitnet) { out.sub_norm = true; out.ffn_act = "relu2"; } + return out; +} + +// engine tensor name → GGUF tensor name (mirror of model_specs). +export function ggufNameFor(name, hasOutputWeight) { + if (name === "embed") return "token_embd.weight"; + if (name === "final_norm") return "output_norm.weight"; + if (name === "lm_head") return hasOutputWeight ? "output.weight" : "token_embd.weight"; + const m = name.match(/^l(\d+)\.(.+)$/); if (!m) return null; + const i = m[1], r = m[2], p = `blk.${i}.`; + const map = { + "attn_norm": "attn_norm.weight", "ffn_norm": "ffn_norm.weight", + "attn_sub_norm": "attn_sub_norm.weight", "ffn_sub_norm": "ffn_sub_norm.weight", + "wq": "attn_q.weight", "wk": "attn_k.weight", "wv": "attn_v.weight", "wo": "attn_output.weight", + "bq": "attn_q.bias", "bk": "attn_k.bias", "bv": "attn_v.bias", + "q_norm": "attn_q_norm.weight", "k_norm": "attn_k_norm.weight", + "w_gate": "ffn_gate.weight", "w_up": "ffn_up.weight", "w_down": "ffn_down.weight", + }; + return map[r] ? p + map[r] : null; +} + +// Read the GGUF header from `url` (HTTP Range), growing the read until the tensor +// table fits. Returns { dataOffset, tensors, headerBytes } (headerBytes = the first +// dataOffset bytes, to hand to wasm qvac_load_gpu for the tokenizer + manifest). +export async function readHeader(url, readRange, initial = 48 * 1024 * 1024) { + let n = initial, parsed = null, buf = null; + for (let tries = 0; tries < 6; tries++) { + buf = await readRange(url, 0, n); + try { parsed = parseGgufHeader(buf); break; } catch (e) { if (e instanceof RangeError || /short/.test(String(e))) { n *= 2; continue; } throw e; } + } + if (!parsed) throw new Error("could not parse GGUF header"); + const headerBytes = buf.length >= parsed.dataOffset ? buf.subarray(0, parsed.dataOffset) : await readRange(url, 0, parsed.dataOffset); + return { dataOffset: parsed.dataOffset, tensors: parsed.tensors, headerBytes }; +} + +// Build the per-tensor fetcher the GPU engine consumes. `manifest` is the wasm +// manifest (dims + tensors with N,K,blk). Returns fetchTensor(name) → Uint8Array, +// byte-identical to what qvac_gpu_tensor would return — but sourced from disk. +export function makeDiskFetcher({ url, readRange, dataOffset, tensors, manifest, bits }) { + const tdir = {}; for (const t of tensors) tdir[t.name] = t; + const hasOut = !!tdir["output.weight"]; + const mByName = {}; for (const t of manifest.tensors) mByName[t.name] = t; + const ROW_CHUNK = 8192; + const ffM = manifest.ff, dM = manifest.d; + + // Resolve an engine tensor name → { info, N, K, blk, eltOffset }. Handles MoE + // expert slices `l{i}.e{e}.{gate,up,down}` (the e-th slab of the 3-D ffn_*_exps + // tensor) and the router `l{i}.router` (ffn_gate_inp, f32) in addition to the + // plain tensors in the manifest. + const resolve = (name) => { + let m = name.match(/^l(\d+)\.e(\d+)\.(gate|up|down)$/); + if (m) { + const i = m[1], e = +m[2], role = m[3]; + const info = tdir[`blk.${i}.ffn_${role}_exps.weight`]; if (!info) return null; + const N = role === "down" ? dM : ffM, K = role === "down" ? ffM : dM; + return { info, N, K, blk: true, eltOffset: e * N * K }; + } + m = name.match(/^l(\d+)\.router$/); + if (m) { const info = tdir[`blk.${m[1]}.ffn_gate_inp.weight`]; return info ? { info, N: 1, K: (manifest.moe.n_experts * dM), blk: false, eltOffset: 0 } : null; } + const spec = mByName[name]; const gname = ggufNameFor(name, hasOut); const info = gname && tdir[gname]; + return (spec && info) ? { info, N: spec.N, K: spec.K, blk: spec.blk, eltOffset: 0 } : null; + }; + + return async function fetchTensor(name) { + const r = resolve(name); + if (!r) return new Uint8Array(0); + const { info, N, K, blk, eltOffset } = r; + const [bElems, bBytes] = blockShape(info.ggmlType); + const tBase = dataOffset + info.offset + (eltOffset / bElems) * bBytes; // slab offset for experts + if (!blk) { // norm / bias / router → [f32] + const elems = K; + const raw = await readRange(url, tBase, typeByteLen(info.ggmlType, elems)); + const f = dequantizeRaw(info.ggmlType, raw, elems); + return new Uint8Array(f.buffer, f.byteOffset, f.byteLength); + } + // block weight → [all q bytes][all f32 scales], chunked by rows so a 152k-vocab + // tensor never materialises whole as f32 (mirror of quant_tensor_chunked). + const [blkElems, blkBytes] = blockShape(info.ggmlType); + const qBytesTotal = bits === 4 ? (N * K) / 2 : N * K; + const scaleCount = N * (K / 32); + const out = new Uint8Array(qBytesTotal + scaleCount * 4); + const scales = new Float32Array(out.buffer, qBytesTotal, scaleCount); + // Fast relayout when the source quant matches the engine's width (the common + // case: a Q4_0 model → 4-bit engine). No f32 ever materialises → also lets the + // big embed/lm_head be done in one pass. + const fast = (info.ggmlType === GGML.Q4_0 && bits === 4) || (info.ggmlType === GGML.Q8_0 && bits === 8); + if (fast) { + const raw = await readRange(url, tBase, typeByteLen(info.ggmlType, N * K)); + const { q, s } = info.ggmlType === GGML.Q4_0 ? relayoutQ4(raw, N, K) : relayoutQ8(raw, N, K); + out.set(q, 0); scales.set(s, 0); + return out; + } + let qPos = 0, sPos = 0; + for (let r = 0; r < N; r += ROW_CHUNK) { + const nr = Math.min(ROW_CHUNK, N - r); + const startElem = r * K, countElem = nr * K; + const byteStart = (startElem / blkElems) * blkBytes, byteLen = (countElem / blkElems) * blkBytes; + const raw = await readRange(url, tBase + byteStart, byteLen); + const f = dequantizeRaw(info.ggmlType, raw, countElem); + const { q, s } = quantBlocks(f, nr, K, bits); + out.set(q, qPos); qPos += q.length; + scales.set(s, sPos); sPos += s.length; + } + return out; + }; +} + +// HTTP Range reader against a same-origin URL. +export function rangeReader() { + return async (url, start, len) => { + const r = await fetch(url, { headers: { Range: `bytes=${start}-${start + len - 1}` } }); + if (!r.ok && r.status !== 206) throw new Error(`range ${start}+${len}: HTTP ${r.status}`); + return new Uint8Array(await r.arrayBuffer()); + }; +} diff --git a/apps/q/qvac-kdisk.mjs b/apps/q/qvac-kdisk.mjs new file mode 100644 index 0000000000000000000000000000000000000000..cecc27c39e0387153d7a99b7699eebbdbd84d6a6 --- /dev/null +++ b/apps/q/qvac-kdisk.mjs @@ -0,0 +1,109 @@ +// Browser κ-DISK reader — the in-browser realization of holospaces' KappaDisk +// (crates/holospaces/src/disk.rs) for qvac. The model's weights live in the +// substrate as κ-addressed sectors (KappaStore = the served .qvf, addressed by +// sector κ). Every sector read is RE-DERIVED against its κ-label +// (sha256(bytes)===κ → verify-by-re-derivation, the substrate law) and kept in a +// content-keyed read-through cache (Law L3: RAM is a bounded cache of the +// canonical store). The model is one verified, teleportable `image_kappa`. +// +// Exposes rr(off,len) over the virtual disk image, so qvac's loader (header → +// tokenizer, singles, per-layer frames, MoE experts) reads through the substrate +// unchanged. The peer realizes the matmuls on its GPU. + +const G = (typeof window !== "undefined" ? window : globalThis); +G.__kdcache = G.__kdcache || new Map(); // κ → Uint8Array (content-keyed, shared across loads/peers) +G.__kdinflight = G.__kdinflight || new Map(); // κ → Promise +let CACHE_SECTORS = 1024; // bound (KappaDisk CACHE_CAPACITY); LRU. ~1GB @1MB — kept small so kd-cache + engine expert-cache + embed stay under the renderer's ~4GB cap +if (typeof window !== "undefined" && window.__kdCacheSectors) CACHE_SECTORS = window.__kdCacheSectors; + +// Bound concurrent source fetches — HTTP/1.0 opens a connection per request, so +// too many at once exhausts the browser's per-host pool ("Failed to fetch"). A +// small gate keeps us under it. (With keep-alive or multi-source this would widen.) +const GATE_MAX = 12; // global concurrent fetches, spread across source origins +G.__kdgate = G.__kdgate || { active: 0, q: [] }; +function acquire() { const g = G.__kdgate; if (g.active < GATE_MAX) { g.active++; return Promise.resolve(); } return new Promise((res) => g.q.push(res)).then(() => { g.active++; }); } +function release() { const g = G.__kdgate; g.active--; const n = g.q.shift(); if (n) n(); } + +const hex = (buf) => { const b = new Uint8Array(buf); let s = ""; for (let i = 0; i < b.length; i++) s += b[i].toString(16).padStart(2, "0"); return s; }; + +// A MULTI-SOURCE κ-disk. `index` = the .kdisk.json; `sources` = data-file URLs +// (different origins: local disk, LAN peers, CDN). Because every sector is +// verified by re-derivation, sources are never trusted — a wrong/corrupt/missing +// byte stream is rejected and another source is tried. Sectors are round-robined +// across sources so multiple links carry the load in parallel (bandwidth +// aggregation) AND a wedged/slow source just fails over to the next (resilience). +export function makeKDisk({ index, sources, dataUrl, verify = true }) { + const SS = index.sectorSize, sectors = index.sectors, fileSize = index.fileSize; + const cache = G.__kdcache, inflight = G.__kdinflight; + const SRC = (sources && sources.length) ? sources : [dataUrl]; + let fetched = 0, verified = 0, hits = 0; + const perSource = SRC.map(() => 0); + + // ONE coalesced multi-source HTTP fetch of [absOff, absOff+length) — big base in + // the URL (small Range header, dodging Chromium's large-offset hang), rotating + // sources for aggregation, patient failover (κ-verified ⇒ retry is always safe). + const evict = (k) => { while (cache.size >= CACHE_SECTORS && !cache.has(k)) { const o = cache.keys().next().value; cache.delete(o); } }; + let reqId = 0; // per-request source rotation → spread load across origins (aggregation) + async function fetchRange(absOff, length) { + const base = reqId++; + let bytes = null, lastErr; + for (let attempt = 0; attempt < 20 && !bytes; attempt++) { + const s = (base + attempt) % SRC.length; + try { + if (attempt) await new Promise((res) => setTimeout(res, Math.min(1500, 40 * Math.pow(1.6, attempt)))); + await acquire(); + try { + const r = await fetch(SRC[s] + "?base=" + absOff, { headers: { Range: `bytes=0-${length - 1}` } }); + if (!r.ok && r.status !== 206) throw new Error("HTTP " + r.status); + bytes = new Uint8Array(await r.arrayBuffer()); + } finally { release(); } + if (bytes.length !== length) { lastErr = new Error("short read"); bytes = null; continue; } + perSource[s]++; + } catch (e) { lastErr = e; } + } + if (!bytes) throw new Error(`κ-disk range ${absOff}+${length} unresolvable across ${SRC.length} sources: ${lastErr}`); + fetched++; + return bytes; + } + + return { + imageKappa: index.imageKappa, + qvf: index.qvf, + sources: SRC, + stats: () => ({ fetched, verified, hits, cached: cache.size, perSource, sources: SRC.length, distinctSectors: index.distinctSectors, sectorCount: index.sectorCount }), + // read [off, off+len): assemble from the content cache where possible; for the + // uncached part, ONE coalesced fetch covering the whole range, then verify + + // cache each FULL sector it spans (still content-addressed: each κ re-derived). + rr: async (off, len) => { + const out = new Uint8Array(len); + const f0 = Math.floor(off / SS), f1 = Math.floor((off + len - 1) / SS); + let allCached = true; + for (let si = f0; si <= f1; si++) if (!cache.has(sectors[si])) { allCached = false; break; } + if (allCached) { // 0 fetches — pure cache hit + let done = 0, si = f0, within = off - si * SS; + while (done < len) { const sec = cache.get(sectors[si]); cache.delete(sectors[si]); cache.set(sectors[si], sec); const take = Math.min(sec.length - within, len - done); out.set(sec.subarray(within, within + take), done); done += take; si++; within = 0; } + hits++; return out; + } + const raw = await fetchRange(off, len); // 1 HTTP fetch for the whole range + out.set(raw, 0); + for (let si = f0; si <= f1; si++) { // verify + cache the FULL sectors covered + const sStart = si * SS, sEnd = Math.min(sStart + SS, fileSize); + if (sStart >= off && sEnd <= off + len && !cache.has(sectors[si])) { + const sub = raw.subarray(sStart - off, sEnd - off); + if (verify) { const got = index.axis + ":" + hex(await crypto.subtle.digest("SHA-256", sub)); if (got !== sectors[si]) throw new Error(`κ MISMATCH sector ${si}`); verified++; } + evict(sectors[si]); cache.set(sectors[si], new Uint8Array(sub)); + } + } + return out; + }, + // verify the disk INDEX itself re-derives to image_kappa (KappaDisk::image_kappa) + verifyImage: async () => { + const enc = new TextEncoder().encode(index.imageIri || "https://uor.foundation/holospaces/realization/kappa-disk"); + const parts = [enc, new Uint8Array([0])]; + for (const k of sectors) { const h = k.split(":")[1]; const b = new Uint8Array(h.length / 2); for (let i = 0; i < b.length; i++) b[i] = parseInt(h.substr(i * 2, 2), 16); parts.push(b); } + let total = 0; for (const p of parts) total += p.length; const all = new Uint8Array(total); let o = 0; for (const p of parts) { all.set(p, o); o += p.length; } + const got = index.axis + ":" + hex(await crypto.subtle.digest("SHA-256", all)); + return { ok: got === index.imageKappa, got, expected: index.imageKappa }; + }, + }; +} diff --git a/index.html b/index.html new file mode 100644 index 0000000000000000000000000000000000000000..0eacee848ac7e6d38d70ecc3bc95f5a93dc9f11b --- /dev/null +++ b/index.html @@ -0,0 +1,176 @@ + + + + + +Q + + + + +
Q
+ +
+ +
tap to talk
+
+ +
+
+ +
+ + +
+
+
+ + + + + diff --git a/q-live-sw.js b/q-live-sw.js new file mode 100644 index 0000000000000000000000000000000000000000..4b1b47495c31a695a54bff599d40c6d2c49d9faa --- /dev/null +++ b/q-live-sw.js @@ -0,0 +1,32 @@ +// q-live-sw.js — BOOT-ONCE. The brain's weights are immutable content-addressed κ-blocks (HF …/resolve/main/ +// b/): fetch them from HuggingFace ONCE, cache-first forever, so every visit after the first is +// ~0-network and instant — and a flaky cold-stream can't wedge a returning user. Only immutable, content- +// addressed URLs are cached (the sha256 IS the version), so cache-first is always correct — never stale. +// +// Scope /apps/q/ controls q-live.html; the fetch handler still sees its cross-origin HF requests. claim() on +// activate takes control of the already-open page so the FIRST brain load is intercepted + cached as it streams. +const CACHE = "q-live-kappa-v1"; + +// immutable content-addressed weight blocks + the tokenizer header (both keyed by content). Anything else +// (manifests, app code, the localhost .holo Range reads) passes straight through to the network untouched. +const CACHEABLE = /\/resolve\/main\/b\/|\/b\/sha256_|\/resolve\/main\/tokenizer\.gguf/; + +self.addEventListener("install", () => self.skipWaiting()); +self.addEventListener("activate", (e) => e.waitUntil(self.clients.claim())); + +self.addEventListener("fetch", (e) => { + const url = e.request.url; + if (e.request.method !== "GET" || !CACHEABLE.test(url)) return; // network as normal + e.respondWith((async () => { + const cache = await caches.open(CACHE); + const hit = await cache.match(e.request); + if (hit) return hit; // served from cache — 0 network + let res; + try { res = await fetch(e.request); } catch (err) { // offline + not cached → let it surface + const stale = await cache.match(e.request); if (stale) return stale; throw err; + } + // cache opaque (cross-origin no-cors) and 200 bodies; a κ-block is immutable so this is safe forever. + try { if (res && (res.status === 200 || res.type === "opaque")) await cache.put(e.request, res.clone()); } catch (_) {} + return res; + })()); +});